-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEMA32.ASM
2015 lines (1725 loc) · 26.8 KB
/
EMA32.ASM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;ema32.asm
; Expanded Memory Array
INCLUDE UBDEF.H
INCLUDE UB.H
shl_dxax_cl macro
db 0fh,0a5h,0c2h ;=shld dx,ax,cl
shl ax,cl
endm
shl_edxeax_cl macro
set32
db 0fh,0a5h,0c2h ;=shld dx,ax,cl
set32
shl ax,cl
endm
makemaptbl macro reg1,reg2
mov [reg1],reg2
inc reg2
mov [reg1+4],reg2
inc reg2
mov [reg1+8],reg2
inc reg2
mov [reg1+12],reg2
sub reg2,3
endm
get_dxax macro
local jp
backsp_mac
xor dx,dx
lodsw
or ax,ax
jz jp
cmp ax,2
lodsw
jb jp
mov dx,[si]
je jp
jmp ilgerr
jp:
endm
_emabaseadr equ 0
_emabasehigh equ 2
_ema1stlim equ 4
_ema2ndlim equ 8
_emaattribsize equ 16 ;>= _ema2ndlim + 4
_emaattribsizeword equ 8 ;_emaattribsize/2
_emaattribsizelog equ 4 ;log_2 of _emaattribsize
EMSpagesize equ 4000h
EMSmaxhigh equ 128*16 ;128*16*64K=128M
CODE SEGMENT PUBLIC
ASSUME CS:CODE,DS:DATA
public getemaadr,emaword,letema,inpema,dimema,useema
public swapema,emain,ema_close
public clrema,negema,incema,decema
extrn mainlp:near,hotst:near
extrn clr_next:near,neg_next:near
extrn inc_next:near,dec_next:near
extrn negallsi:near,lvincin:near,lvdecin:near
extrn blockout:near
extrn let_extended:near
extrn ready:near,print2:near,ahedsp:near
extrn FORMUL:near,KAKKO:near
extrn addin:near,subin:near,mulin:near,divin:near
extrn resin:near,idivin:near,ratdivin:near
extrn farFORMUL:far
extrn farGET_AX:far,farGET_EAX:far
extrn farPRNTBX:far,farBINASC:far
extrn UNDEFERR:near,OVRERR:near,DBLDEF:near,emaerr:near
extrn cantdo:near,synerr:near,sperr:near
extrn ilgerr:near,diskerr:near,nofile:near,existerr:near
extrn nolerr:near,indexerr:near,blockerr:near
extrn undeferr:near,nofile:near
;
; * return EMS handle
;
ema_close:
mov dx,[emahandle]
or dx,dx
jz emacloseret
mov ah,45h
int 67h
mov [emahandle],0
emacloseret:
ret
;
; * get ema adr
; out: ecx = file pointer
getemacantdo:
jmp undeferr
getemadirect:
mov ax,[bp+1]
add bp,3
jmps getema5
getemaadr:
cmp [emahandle],0
je getemacantdo
mov al,[bp]
mov ah,[bp+3]
cmp ax,0c1f1h ;1 word number + ; ?
je getemadirect
call formul ;get ema number
cmp byte ptr [bp],0c1h ;code of ;
je getema2
xor ax,ax ;ema number is not specified
push ax ;* attribute base adr
jmp getema7
getema2:
backsp_mac
mov ax,[si]
cmp ax,1
jb getema5
jne getemailg
mov ax,[si+2]
getema5:
cmp ax,16
jae getemailg
my_shl ax,_emaattribsizelog
push ax ;* attribute base adr
inc bp
call formul
getema7:
get_dxax
mov cl,[bp]
cmp cl,0c2h ;code of ,
je getema20 ;2 dim case
; 1 dim case
cmp cl,')'
jne getemasynerr
inc bp
if FLG98
mov cx,emaattributeseg
else
mov cx,ss
add cx,emaattributeseg
endif
mov es,cx
set32
shl dx,16
mov dx,ax
pop di ;* attrib offset
cmp word ptr es:[di+_emabasehigh],-1
je getemaundef
set32
cmp dx,es:[di+_ema1stlim]
ja getemailg
set32
mov ax,es:[di+_ema2ndlim]
set32
or ax,ax
jnz getemailg
mov cl,[emaunitlog]
set32
shl dx,cl
set32
add dx,es:[di+_emabaseadr]
set32
mov cx,dx
smov es,ss
ret
getemailg:
jmp ilgerr
getemasynerr:
jmp synerr
getemaundef:
jmp undeferr
getema20: ;2 dim case
push dx ;** 1st argument
push ax ;
inc bp
call kakko
get_dxax
push dx ;*** 2nd argument
push ax ;
getema50:
if FLG98
mov ax,emaattributeseg
else
mov ax,ss
add ax,emaattributeseg
endif
mov es,ax
set32
pop bx ;*** 2nd arg
set32
pop ax ;** 1st arg
pop di ;* attrib offset
cmp word ptr es:[di+_emabasehigh],-1
je getemaundef
set32
cmp ax,es:[di+_ema1stlim]
ja getemailg
set32
mov cx,es:[di+_ema2ndlim]
set32
cmp bx,cx
ja getemailg
set32
inc cx
set32
mul cx
set32
add ax,bx ;now eax = local element number
mov cl,[emaunitlog]
set32
shl ax,cl
set32
add ax,es:[di+_emabaseadr]
set32
mov cx,ax
smov es,ss
ret
;
; map logical pages to physical pages
; in :ecx file pointer
emsmaperror:
call ema_close
jmp emaerr
mappage_ax:
push ax
push cx
push dx
push si
; make allocation map
mov si,offset maptable
mov cx,ax
makemaptbl si,cx
; do mapping
mov ax,5000h
mov dx,[emahandle]
mov cx,4 ;64K/pagesize
mov si,offset maptable
int 67h
or ah,ah
jnz emsmaperror
pop si
pop dx
pop cx
pop ax
ret
;farmappage:
; call mappage
; retf
;
;* expanded MEMORY ARRAY used as function
;
EMA1ilg:
jmp ilgerr
EMAIN:
call getemaadr ;ecx = offset
; jmp getfromema ;call & ret
;
; * copy from ema to calc stack
; in : ecx file pointer
; result : [calcsp] and its value
; destroy : cx,di,si
getfromema:
set32
mov ax,cx
set32
shr ax,14
and al,0fch ;cut lowest 2 bits
cmp ax,[maptable] ;mapped top page
je getfromema10 ;already mapped
call mappage_ax
getfromema10:
call ahedsp
mov di,si ;dest
push ds ;*
mov ds,[EMSbaseseg] ;EMS seg
mov si,cx ;EMS adr
mov cx,[si]
and cx,lenmask
inc cx
rep movsw
pop ds ;*
ret
;
; * copy to ema from calc stack
; in :ecx file pointer
; [calcsp] and its value
; [calcsp] will be updated
; destroy : ax,bx,cx,dx,si
puttoema:
set32
mov ax,cx
set32
shr ax,14
and al,0fch ;cut lowest 2 bits
cmp ax,[maptable] ;mapped top page
je puttoema10 ;already mapped
call mappage_ax
puttoema10:
mov si,[calcsp] ;source
push es ;*
mov es,[EMSbaseseg] ;EMS seg
mov di,cx ;EMS adr
mov cx,[si]
and cx,lenmask
cmp cx,[emawords]
ja emaover
inc cx
rep movsw
pop es ;*
ret
farputtoema:
call puttoema
retf
emaover:
jmp ovrerr
;
; * access to expanded ram
;
letema:
inc bp
call getemaadr ;ecx = offset
set32
push cx
;calc value to assign
cmp byte ptr [bp],codeofequal
JNE letema_expanded
INC BP
CALL FORMUL
letema_30:
;store value
set32
pop cx
call puttoema
add [calcsp],unitbyte
jmp mainlp
emasynerr:
jmp synerr
letema_expanded:
call getfromema
call let_extended
jmp letema_30
;
;* input to ema
;
inpemaover:
jmp ovrerr
inpema:
winc bp
call getemaadr ;ecx = offset
call puttoema
add [calcsp],unitbyte
ret
;
;* dim ema
;
dimdblerr:
jmp dbldef
dimema:
push es
call far ptr farinitema
winc bp
call formul
cmp byte ptr [bp],0c1h ;code of ;
je dimema5
xor ax,ax ;ema number is not specified
push ax ;* attribute base
jmp dimema15
dimema5:
backsp_mac
lodsw
cmp ax,1
jb dimema10 ;if 0
jne dimemailg
mov ax,[si]
cmp ax,16
jae dimemailg ;must <= 15
dimema10:
my_shl ax,_emaattribsizelog
push ax ;* attribute base
mov di,ax
cmp byte ptr [bp],0c1h ;code of ;
jne dimemailg
inc bp
call formul
dimema15:
get_dxax
push dx ;** 1st limit
push ax ;
cmp byte ptr [bp],0c2h ;code of ,
jne dimema40
inc bp
call formul
get_dxax
push dx ;*** 2nd limit
push ax ;
jmp dimema50
dimema40:
set32
xor ax,ax
set32
push ax ;*** 2nd limit(for 1 dim case)
dimema50:
cmp byte ptr [bp],')'
jne dimemailg
inc bp
;set ema attributes
if FLG98
mov ax,emaattributeseg
else
mov ax,ss
add ax,emaattributeseg
endif
mov es,ax
set32
pop ax ;***
set32
pop bx ;**
pop di ;*
cmp word ptr es:[di+_emabasehigh],-1
je dimema55
jmp dimdblerr ;duplicate definition
dimema55:
set32
mov es:[di+_ema2ndlim],ax ;2nd limit
set32
inc ax
jz dimemailg
set32
mov es:[di+_ema1stlim],bx ;1st limit
set32
inc bx
jz dimemailg
set32
mul bx ;edx:eax = # of elements
set32
or dx,dx
jnz dimemaover
set32
push ax ;*
mov cl,[emaunitlog]
shl_edxeax_cl
set32
or dx,dx
jnz dimemaover
set32
mov bx,[emanow] ;ebx = limit+1
set32
add ax,bx
dimema60:
set32
mov es:[di+_emabaseadr],bx ;start adr
set32
mov [emanow],ax ;next start adr
set32
push bx ;* start adr
;check EMS free space
set32
mov dx,ax
set32
shr dx,16
cmp dx,EMSmaxhigh
jae dimemaover
mov cx,EMSpagesize
div cx
or dx,dx
jz chkEMS10
inc ax
chkEMS10:
add ax,3 ;pages-1
and ax,0fffch ;cut lowest 3bits
mov bx,ax ;total page number
mov ah,51h ;reallocation
mov dx,[emahandle]
int 67h
or ah,ah
jnz dimemaover
; make allocation map
xor bx,bx ;map 0 page
mov si,offset maptable
makemaptbl si,bx
; do mapping
mov ax,5000h
mov dx,[emahandle]
mov cx,4
mov si,offset maptable
int 67h
or ah,ah
jnz emsmaperror
; set all elements to 0
pop di ;si:di = start ptr
pop si ;
set32
pop dx ;edx = # of elements
mov es,[EMSbaseseg]
dimema90:
set32
push dx
mov ax,si
my_shl ax,2
cmp ax,[maptable] ;mapped top page
je dimema95 ;already mapped
push si
push di
; make allocation map
mov bx,offset maptable
my_shl si,2 ;*4
makemaptbl bx,si
; do mapping
mov ax,5000h
mov dx,[emahandle]
mov cx,4
mov si,offset maptable
int 67h
or ah,ah
jnz emsmaperror
pop di
pop si
dimema95:
mov word ptr es:[di],0 ;set 0
add di,[emaunitbytes]
adc si,0
set32
pop dx
set32
dec dx
jnz dimema90
pop es
ret
dimemaover:
jmp emaerr
dimemailg:
jmp ilgerr
;
; * use expanded ram
;
useema:
call far ptr farinitema
jmp mainlp
;
; * check expanded ram
;
EMAword:
jmp far ptr farEMAword
;
; * swap ema
;
swapema:
call getemaadr ;ecx = offset
set32
push cx ;* offset1
call getfromema
cmp byte ptr [bp],0c2h ;code of ,
jne swapemasynerr
INC BP
cmp word ptr [bp],0f580h ;code of ema(
jne swapemasynerr
add bp,2
call getemaadr ;ecx = offset
set32
pop ax
set32
push cx ;* offset2
set32
push ax ;** offset1
call getfromema
set32
pop cx ;** offset1
call puttoema
add [calcsp],unitbyte
set32
pop cx ;* offset2
call puttoema
add [calcsp],unitbyte
jmp mainlp
swapemasynerr:
jmp synerr
farSwapemasub:
mov ax,code2
mov es,ax
set32
mov ax,es:[emablocknowB]
set32
push ax ;*p2
set32
mov cx,es:[emablocknow]
set32
push cx ;**p1
mov ax,ss
mov es,ax
call getfromema ;get valA from var1
set32
pop ax ;**p1
set32
pop cx ;*p2
set32
push cx ;*p2
set32
push ax ;**p1
call getfromema ;get valB from var2
set32
pop cx ;**p1
call puttoema ;write valB to var1
add [calcsp],unitbyte
set32
pop cx ;*p2
call puttoema ;write valA to var2
add [calcsp],unitbyte
retf
;
;* copy ema to ema by block
;
copyemasub:
mov ax,code2
mov es,ax
set32
mov ax,es:[emablocknowB]
set32
push ax
set32
mov cx,es:[emablocknow]
mov ax,ss
mov es,ax
call getfromema
set32
pop cx
call puttoema
add [calcsp],unitbyte
retf
;
;* add ema (block+=const)
;
addemablkconstsub:
mov ax,code2
mov es,ax
set32
mov cx,es:[emablocknow]
set32
push cx
mov ax,ss
mov es,ax
call getfromema
call ahedsp
mov di,si
add si,2*unitbyte
copy_si2di
call word ptr [blockoperation]
set32
pop cx
call puttoema
add [calcsp],unitbyte
retf
;
; * add ema (block+=block)
;
addemablkblksub:
mov ax,code2
mov es,ax
set32
mov cx,es:[emablocknowB]
set32
push cx ;*
set32
mov ax,es:[emablocknow]
set32
push ax ;**
mov ax,ss
mov es,ax
call getfromema
set32
pop cx ;**
call getfromema
call word ptr [blockoperation]
set32
pop cx ;*
call puttoema
add [calcsp],unitbyte
retf
;
; * negate ema
;
negema:
call getemaadr ;ecx = offset
call negemasub
jmp neg_next
negemasub:
set32
push cx
call getfromema
mov si,[calcsp]
call negallsi
set32
pop cx
call puttoema
add [calcsp],unitbyte
ret
farnegemasub:
call negemasub
retf
;
; * increment ema
;
incema:
call getemaadr ;ecx = offset
set32
push cx ;offset
call getfromema
mov di,[calcsp]
mov ax,[emawords]
mov [limitlen],ax
call lvincin
set32
pop cx ;offset
call puttoema
add [calcsp],unitbyte
jmp inc_next
;
; * decrement ema
;
decema:
call getemaadr ;ecx = offset
set32
push cx ;offset
call getfromema
mov di,[calcsp]
mov ax,[emawords]
mov [limitlen],ax
call lvdecin
set32
pop cx ;offset
call puttoema
add [calcsp],unitbyte
jmp dec_next
;
; * clear ema
;
clrema:
call getemaadr ;ecx = offset
call clremasub
jmp clr_next
clremasub:
set32
mov ax,cx
set32
shr ax,14
and al,0fch ;cut lowest 2 bits
cmp ax,[maptable] ;mapped top page
je clrema10 ;already mapped
call mappage_ax
clrema10:
push ds
mov ds,[EMSbaseseg]
mov si,cx
mov word ptr [si],0
pop ds
ret
farclremasub:
call clremasub
retf
code ends
DATA SEGMENT PUBLIC
public emahandle,emanow,emawords
extrn fnamebuf:byte
extrn calcsp:word
extrn maxword:word,limitlen:word
extrn blocksw:byte,blockoperation:word
extrn calcsp_limit:word
EMSbaseseg dw 0
emahandle dw 0
emanow dw 0,0
emaunitlog db 0,0
emaunitbytes dw 0,0
emawords dw 0
maptable dw 0,0,0,1,0,2,0,3
data ends
code2 segment public
assume cs:code2,ds:data
public swapemablk,clremablk,negemablk,emablkin
public faremaword
public fardeleteema,emausing
extrn ahedsp2:near
extrn msg2:near,msg_cs2:near,get_char2:near,letnl2:near
extrn farBEEPSUB:far
extrn ex3ilgerr:near
;
; * jumped from freeze
;
emausing:
mov dx,offset emausingmsg
call msg_cs2
jmp far ptr ready
if JAPANESE
emausingmsg db 'EMA配列使用中はfreezeできません',0
else
emausingmsg db 'cannot freeze including ema-arrays',0
endif
;
; * use expanded ram
;
farinitema:
call initema
retf
initema:
mov bx,[emahandle]
or bx,bx
jnz initemaret ;already got handle
call getemahandle
useemain:
set32
xor ax,ax
set32
mov [emanow],ax
if FLG98
mov ax,emaattributeseg
else
mov ax,ss
add ax,emaattributeseg
endif
mov es,ax
xor di,di
mov cx,_emaattribsizeword*16 ;16emas
mov ax,-1
rep stosw
smov es,ss
mov ax,[maxword]
inc ax ;+ attribute
add ax,ax
call goodEMSunitlog
mov [emaunitlog],cl
mov [emaunitbytes],ax
shr ax,1
dec ax
mov [emawords],ax
initemaret: