-
Notifications
You must be signed in to change notification settings - Fork 2
/
MISCA.ASM
849 lines (708 loc) · 17.6 KB
/
MISCA.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
;-----------------------------------------------------------------
; misca.asm
; ---------
; Miscellaneous minor assembly-language routines.
;-----------------------------------------------------------------
include asm.inc
include main.inc
include ega.inc
HEADER misca
;-----------------------------------------------------------------
; --- seg & offs of BIOS equipment flag word (we only use its lower byte)
EQUIP_SEG equ 40h
EQUIP_OFFS equ 10h
DSEG
; --- Most recent i/o error code. NOTE: if there was not an error,
; this is undefined.
public _ioError
_ioError dw 0
; --- used by shift_right
public right_shift_masks
right_shift_masks equ this word
db 11111111b, 11111111b
db 01111111b, 11111111b
db 00111111b, 11111111b
db 00011111b, 11111111b
db 00001111b, 11111111b
db 00000111b, 11111111b
db 00000011b, 11111111b
db 00000001b, 11111111b
db 00000000b, 11111111b
db 00000000b, 01111111b
db 00000000b, 00111111b
db 00000000b, 00011111b
db 00000000b, 00001111b
db 00000000b, 00000111b
db 00000000b, 00000011b
db 00000000b, 00000001b
; --- used by shift_left
left_shift_masks equ this word
db 11111111b, 11111111b
db 11111111b, 11111110b
db 11111111b, 11111100b
db 11111111b, 11111000b
db 11111111b, 11110000b
db 11111111b, 11100000b
db 11111111b, 11000000b
db 11111111b, 10000000b
db 11111111b, 00000000b
db 11111110b, 00000000b
db 11111100b, 00000000b
db 11111000b, 00000000b
db 11110000b, 00000000b
db 11100000b, 00000000b
db 11000000b, 00000000b
db 10000000b, 00000000b
ENDDS
;-----------------------------------------------------------------
PSEG misca
;-----------------------------------------------------------------
; WORD dataseg ();
; Returns the current value of the DS register.
;-----------------------------------------------------------------
public _dataseg
STARTPROC _dataseg
mov ax,ds
ret
ENDPROC _dataseg
;-----------------------------------------------------------------
; WORD codeseg ();
; Returns the current value of the CS register.
;-----------------------------------------------------------------
public _codeseg
STARTPROC _codeseg
mov ax,cs
ret
ENDPROC _codeseg
;-----------------------------------------------------------------
; void far_setmem (seg, offs, length, value);
; WORD seg, offs;
; UWORD length;
; WORD value;
; Set length bytes starting at seg:offs to value.
; NOTE: This could be optimized by using stosw if possible.
;-----------------------------------------------------------------
public _far_setmem
STARTPROC _far_setmem
push bp
mov bp,sp
push es
push si
push di
mov es,ARGB[bp] ; segment
mov di,ARGB+2[bp] ; offset
mov cx,ARGB+4[bp] ; count
mov al,ARGB+6[bp] ; value
jcxz far_setmem_done
cld
rep stosb
far_setmem_done:
pop di
pop si
pop es
pop bp
ret
ENDPROC _far_setmem
;-----------------------------------------------------------------
; void far_movmem (sseg, soffs, dseg, doffs, length);
; WORD sseg, soffs, dseg, doffs;
; UWORD length;
; Move length bytes starting at sseg:soffs to dseg:doffs.
; This proc doesn't handle overlapping source & dest.
; NOTE: Could speed this up by using WORD move!
;-----------------------------------------------------------------
public _far_movmem
STARTPROC _far_movmem
push bp
mov bp,sp
push ds
push es
push si
push di
mov ds,ARGB[bp] ; source seg
mov si,ARGB+2[bp] ; source offs
mov es,ARGB+4[bp] ; dest seg
mov di,ARGB+6[bp] ; dest offs
mov0:
mov cx,ARGB+8[bp] ; # bytes
jcxz far_movmem_done
move_left_to_right:
cld
rep movsb
far_movmem_done:
pop di
pop si
pop es
pop ds
pop bp
ret
ENDPROC _far_movmem
;-----------------------------------------------------------------
; void far_movmem_same_seg(UWORD seg, UWORD soffs, UWORD doffs, UWORD length);
; Move length bytes starting at seg:soffs to seg:doffs,
; dealing with possible OVERLAP --
; when destination starts AFTER source, move RIGHT-TO-LEFT.
; NOTE: Could speed this up by using WORD move!
; NOTE: must have same register push/pop sequence as "_far_movmem".
;-----------------------------------------------------------------
public _far_movmem_same_seg
STARTPROC _far_movmem_same_seg
push bp
mov bp,sp
push ds
push es
push si
push di
mov ds,ARGB[bp] ; source seg
mov si,ARGB+2[bp] ; source offs
mov es,ARGB[bp] ; dest seg
mov di,ARGB+4[bp] ; dest offs
mov cx,ARGB+6[bp] ; # bytes
jcxz far_movmem_same_seg_done
cmp di,si
jbe move_left_to_right
std ; Backwards through memory.
add si,cx
dec si ; Address of last byte.
add di,cx
dec di ; Address of last byte.
rep movsb
far_movmem_same_seg_done:
cld
pop di
pop si
pop es
pop ds
pop bp
ret
ENDPROC _far_movmem_same_seg
;-----------------------------------------------------------------
; void far_swapmem (sseg, soffs, dseg, doffs, length);
; WORD sseg, soffs, dseg, doffs;
; UWORD length;
; Swap length bytes starting at sseg:soffs with dseg:doffs.
; This proc doesn't handle overlapping source & dest.
; NOTE: Could speed this up by using WORD operations! (and by using xchg)
;-----------------------------------------------------------------
public _far_swapmem
STARTPROC _far_swapmem
push bp
mov bp,sp
push ds
push es
push si
push di
mov ds,ARGB[bp] ; source seg
mov si,ARGB+2[bp] ; source offs
mov es,ARGB+4[bp] ; dest seg
mov di,ARGB+6[bp] ; dest offs
mov cx,ARGB+8[bp] ; # bytes
jcxz far_swapmem_done
cld
swap_loop:
mov al,[si]
mov bl,es:[di]
mov [si],bl
mov es:[di],al
inc si
inc di
loop swap_loop
far_swapmem_done:
pop di
pop si
pop es
pop ds
pop bp
ret
ENDPROC _far_swapmem
;-----------------------------------------------------------------
; LongFrac LFMULT (a, b);
; LongFrac a, b;
; Multiply two long fractions (ie, 32-bit fixed point numbers with 16 bits
; of binary fraction).
;
; register usage:
; [bp+ARGB] a.low
; [bp+ARGB+2] a.hi
; [bp+ARGB+4] b.lo
; [bp+ARGB+6] b.hi
; ax,dx = result (lo,hi)
;-----------------------------------------------------------------
public _LFMULT
STARTPROC _LFMULT
push bp
mov bp,sp
push di
push si
xor si,si
mov ax,[bp+ARGB+2]
test ax,ax ; a>0 ?
jns aplus
not word ptr [bp+ARGB+2] ; a = -a
neg word ptr [bp+ARGB]
sbb word ptr [bp+ARGB+2],0ffffh
not si
aplus: mov ax,[bp+ARGB+6]
test ax,ax ; b>0?
jns bplus
not word ptr [bp+ARGB+6] ; b = -b
neg word ptr [bp+ARGB+4]
sbb word ptr [bp+ARGB+6],0ffffh
not si
bplus: mov ax,[bp+ARGB] ; get a.lo
mul word ptr [bp+ARGB+4] ; a.lo*b.lo
mov bx,dx ; res.lo = highhalf(a.lo*b.lo)
mov ax,[bp+ARGB+2]
mul word ptr [bp+ARGB+6]; a.hi*b.hi
mov cx,ax ; res.hi = lowhalf(a.hi*b.hi)
mov ax,[bp+ARGB]
mul word ptr [bp+ARGB+6] ; a.lo*b.hi
add bx,ax
adc cx,dx
mov ax,[bp+ARGB+2]
mul word ptr [bp+ARGB+4] ; a.hi*b.lo
add ax,bx ; ax = lo
adc dx,cx ; dx = hi
test si,si
jns leave ; if (neg)
not dx ; res = -res
neg ax
sbb dx,0ffffh
leave:
pop si
pop di
pop bp
ret
ENDPROC _LFMULT
;-----------------------------------------------------------------
; Return TRUE if there is a char in the keyboard buffer, or FALSE otherwise.
;-----------------------------------------------------------------
public _char_avail
STARTPROC _char_avail
mov ah,1
int 16h
jz no_char_avail
mov ax,-1
ret
no_char_avail:
xor ax,ax
ret
ENDPROC _char_avail
;-----------------------------------------------------------------
; Get an "extended" char, where normal chars are in the range 0-255, and
; non-ascii chars (for example, the function keys) are >= 256.
;-----------------------------------------------------------------
public _get_char
STARTPROC _get_char
mov ah,0
int 16h
or al,al
jz gc_done
xor ah,ah
gc_done:
ret
ENDPROC _get_char
;-----------------------------------------------------------------
; set_ioError: if an io or critical error occurred, squirrels
; away the error code and clears did_crit_error and returns -1
;
; Assumes that the carry flag and ax are the results of the last
; io operation (int 21h).
;
; If no error occurred, ax is unchanged.
;-----------------------------------------------------------------
did_crit_error dw 0 ;NOTE: didn't include crit error handler in program.
STARTPROC set_ioError
pushf ; hold carry flag
test cs:did_crit_error, 0ffffh
jz noCritError
push cs:did_crit_error
pop _ioError
popflags
jmp short ioerr
noCritError:
popflags
jnc iodone ; okay; leave ax unchanged
mov _ioError,ax
ioerr:
mov cs:did_crit_error,0
mov ax,0ffffh ; return -1 for any error
iodone: ret
ENDPROC set_ioError
;-----------------------------------------------------------------
; Open Dos File: fileHandle = opendos (name, access);
;-----------------------------------------------------------------
public _opendos
STARTPROC _opendos
push bp
mov bp,sp
mov al,[bp+ARGB+2] ; get access code
mov dx,[bp+ARGB] ; pointer to name
mov ah,03dh
int 021h
call set_ioError
pop bp
ret
ENDPROC _opendos
;-----------------------------------------------------------------
; Create Dos File: fileHandle = creatdos (name);
;-----------------------------------------------------------------
public _creatdos
STARTPROC _creatdos
push bp
mov bp,sp
xor cx,cx ; attribute= normal files
mov dx,[bp+ARGB] ; pointer to name
mov ah,03ch
int 021h ; do open or create
jnb cdx ; if carry flag is set
mov ax,0FFFFh ; return -1
cdx:
pop bp
ret
ENDPROC _creatdos
;-----------------------------------------------------------------
; Close Dos File: closedos (filehandle);
;-----------------------------------------------------------------
public _closedos
STARTPROC _closedos
push bp
mov bp,sp
mov bx,[bp+ARGB] ; get file handle
mov ah,03eh
int 021h ; do read or write
pop bp
ret
ENDPROC _closedos
;----------------------------------------------------------------------
; Delete Dos File: deletedos (name);
;---------------------------------------------------------------------
public _deletedos
STARTPROC _deletedos
push bp
mov bp,sp
mov dx,ARGB[bp] ; pointer to name
mov ah,41h
int 21h ; delete file
call set_ioError
pop bp
ret
ENDPROC _deletedos
;-----------------------------------------------------------------
; Read from Dos File: nBytes = readdos (file, seg, offset, length);
;-----------------------------------------------------------------
O_FILE = ARGB
O_SEG = ARGB+2
O_OFFSET = ARGB+4
O_LENGTH = ARGB+6
public _readdos
STARTPROC _readdos
push bp
mov bp,sp
push ds
mov ah,3fh
mov bx,O_FILE[bp]
mov cx,O_LENGTH[bp]
mov ds,O_SEG[bp]
mov dx,O_OFFSET[bp]
int 021h
call set_ioError
pop ds
pop bp
ret
ENDPROC _readdos
;-----------------------------------------------------------------
; Write to Dos File: nBytes = writedos (file, seg, offset, length);
; RETURN -1 on error.
;-----------------------------------------------------------------
O_FILE = ARGB
O_SEG = ARGB+2
O_OFFSET = ARGB+4
O_LENGTH = ARGB+6
public _writedos
STARTPROC _writedos
push bp
mov bp,sp
push ds
mov ah,40h
mov bx,O_FILE[bp]
mov cx,O_LENGTH[bp]
mov ds,O_SEG[bp]
mov dx,O_OFFSET[bp]
int 021h
call set_ioError
pop ds
pop bp
ret
ENDPROC _writedos
;-----------------------------------------------------------------
; LFDIV (num, denom);
;-----------------------------------------------------------------
NUM = ARGB
DENOM = ARGB+4
public _LFDIV
STARTPROC _LFDIV
push bp
mov bp,sp
; neg = FALSE
xor cx,cx
; if (num < 0) { num = -num; neg = !neg; }
cmp word ptr NUM+2[bp],0
jge lfdiv1
mov ax,NUM[bp] ; num = -num
mov dx,NUM+2[bp]
neg ax
adc dx,0
neg dx
mov NUM[bp],ax
mov NUM+2[bp],dx
not cx ; neg = !neg
lfdiv1:
; if (denom < 0) { denom = -denom; neg = !neg; }
cmp word ptr DENOM+2[bp],0
jge lfdiv2
mov ax,DENOM[bp] ; denom = -denom
mov dx,DENOM+2[bp]
neg ax
adc dx,0
neg dx
mov DENOM[bp],ax
mov DENOM+2[bp],dx
not cx ; neg = !neg
lfdiv2:
; while (denom >= 0x10000) { num >>= 1; denom >>= 1; }
mov bx,cx ; save neg in bx
lfdiv5:
mov cx,DENOM+2[bp]
jcxz lfdiv3
sar word ptr NUM+2[bp],1 ; num >>= 1
rcr word ptr NUM[bp],1
sar word ptr DENOM+2[bp],1 ; denom >>= 1
rcr word ptr DENOM[bp],1
jmp lfdiv5
lfdiv3:
mov cx,bx ; restore neg to cx
; high word of result = num / denom
mov ax,NUM[bp]
mov dx,NUM+2[bp]
cmp dx,DENOM[bp] ; test for overflow
jb no_overflow
mov ax,7fffh ; overflow, so use a large #
xor dx,dx
jmp lfdiv6
no_overflow:
div word ptr DENOM[bp]
mov bx,ax
; low word of result = ((num % denom) << 16) / denom
lfdiv6:
xor ax,ax
div word ptr DENOM[bp]
mov dx,bx ; dx = high word of result
; if (neg) result = -result;
jcxz lfdiv4
neg ax
adc dx,0
neg dx
lfdiv4:
pop bp
ret
ENDPROC _LFDIV
;-----------------------------------------------------------------
; WORD get_first_mcb ();
; Returns address of first MCB in DOS free memory list.
;-----------------------------------------------------------------
public _get_first_mcb
STARTPROC _get_first_mcb
mov ah,52h
int 21h
mov ax,es:[bx-2]
ret
ENDPROC _get_first_mcb
;-----------------------------------------------------------------
; WORD cdist (a, b);
; LONG a, b;
; Calculate the distance between RGB color values a and b. This is
; done using the sum of the absolute R,G,B deltas. It would be more
; accurate (but much slower) to calculate the actual distance between them.
;
; Improved: absdR + absdG + absdB + MAX(absdR, absdG, absdB).
; This approximates the sum of squares, by forcing large component to
; have double contribution to error term.
;-----------------------------------------------------------------
A = ARGB
B = ARGB+4
public _cdist
STARTPROC _cdist
push bp
mov bp,sp
; --- clear top half of ax so we can use it as a word
sub ah,ah
; --- get absolute distance between BLUE coords, put result in bx
mov bl,A[bp]
mov cl,B[bp]
cmp bl,cl
jae cdist_p1
xchg bl,cl
cdist_p1:
sub bl,cl
sub bh,bh ; BX: abs(delta R).
mov dx,bx ; dx will be MAX.
; --- get absolute distance between GREEN coords, add to bx
mov al,A+1[bp]
mov cl,B+1[bp]
cmp al,cl
jae cdist_p2
xchg al,cl
cdist_p2:
sub al,cl
add bx,ax ; BX: absdR + absdG.
cmp dx,ax
jge cdist_R_ge_G
mov dx,ax
cdist_R_ge_G: ; DX: MAX(absdR, absdG).
; --- get absolute distance between RED coords, add bx to this value
mov al,A+2[bp]
mov cl,B+2[bp]
cmp al,cl
jae cdist_p3
xchg al,cl
cdist_p3:
sub al,cl
add ax,bx ; AX: absdR + absdG + absdB.
cmp dx,ax
jge cdist_RG_ge_B
mov dx,ax
cdist_RG_ge_B: ; DX: MAX(absdR, absdG, absdB).
add ax,dx ; AX: absdR + absdG + absdB + MAX(...).
pop bp
ret
ENDPROC _cdist
;-----------------------------------------------------------------
; void dos_print_string (string);
; char *string;
; Call DOS to print the given string, in text mode. The string should
; not contain a '$', since DOS will treat that as the end of the string,
; and not print any chars after that!
;-----------------------------------------------------------------
public _dos_print_string
STARTPROC _dos_print_string
push bp
mov bp,sp
push si
; --- replace the NULL terminator with a '$', like DOS wants
mov si,ARGB[bp]
dec si
find_dollar:
inc si
mov al,[si]
or al,al
jnz find_dollar
mov byte ptr [si],'$'
; --- call DOS to print the string
push si
mov ah,9
mov dx,ARGB[bp]
int 21h
pop si
; --- restore NULL string terminator
mov byte ptr [si],0
pop si
pop bp
ret
ENDPROC _dos_print_string
;-----------------------------------------------------------------
; Allocate Memory: segment = allocsome (paragraphs, &size)
; Will allocate request or largest remaining, whichever is bigger.
; Returns segment = 0 if failed. Puts number of paragraphs actually
; allocated into *size.
; Right now, this is only used by FixTandyMemory.
;-----------------------------------------------------------------
public _allocsome
STARTPROC _allocsome
push bp
mov bp,sp
mov bx,[bp+ARGB]
allocscont:
mov di,[bp+ARGB+2]
mov [di],bx ; return number of words allocated
mov ax,0
cmp ax,bx
je allocsret ; fail nothing requested
mov ax,04800h
int 021h ; execute DOS function
jnc allocsret
mov cx,ax
mov ax,0 ; error: return NIL
cmp cx,7 ; arena trashed
jne allocscont ; keep trying until we alloc max left or fail
allocsret:
pop bp
ret
ENDPROC _allocsome
;-----------------------------------------------------------------
; LONG FreeDiskSpace (drive);
; WORD drive; /* 0 = drive A, 1 = drive B, etc... */
; Returns free disk space in bytes on the specified drive. If there
; is an error accessing that drive, it returns 0.
;-----------------------------------------------------------------
public _FreeDiskSpace
STARTPROC _FreeDiskSpace
push bp
mov bp,sp
mov ah,36h
mov dl,ARGB[bp]
inc dl ; this func uses 1 = drive A, 2 = drive B...
int 21h
cmp ax,0ffffh
jne fds_no_error
; --- error getting info, so return 0
xor ax,ax
xor dx,dx
jmp fds_done
; --- no error, so return (sectorsPerCluster * bytesPerSector * freeClusters)
; NOTE: we assume that sectorsPerCluster * bytesPerSector < 64k.
fds_no_error:
mul cx
mul bx
fds_done:
pop bp
ret
ENDPROC _FreeDiskSpace
;-----------------------------------------------------------------
; LONG GetSystemTime ();
; Returns the number of timer ticks (18.2 per second) since midnight.
;-----------------------------------------------------------------
public _GetSystemTime
STARTPROC _GetSystemTime
mov ah,0
int 1ah
mov ax,dx ; move low word to ax
mov dx,cx ; move high word to dx
ret
ENDPROC _GetSystemTime
;-----------------------------------------------------------------
; name WordMulDiv --
;
; c = (a*num)/denom;
;
; UWORD muldiv(a,num,denom) UWORD a,num,denom;
;
; register usage
; [bp+ARGB] a
; [bp+ARGB+2] num
; [bp+ARGB+4] denom
;-----------------------------------------------------------------
public _WordMulDiv
STARTPROC _WordMulDiv
push bp
mov bp,sp
mov ax,[bp+ARGB]
mul word ptr[bp+ARGB+2]
div word ptr[bp+ARGB+4]
pop bp
ret
ENDPROC _WordMulDiv
;-----------------------------------------------------------------
ENDPS misca
END