forked from keendreams/keen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
id_vw_ae.asm
1713 lines (1339 loc) · 29.4 KB
/
id_vw_ae.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
; Keen Dreams Source Code
; Copyright (C) 2014 Javier M. Chavez
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License along
; with this program; if not, write to the Free Software Foundation, Inc.,
; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
;=================================
;
; EGA view manager routines
;
;=================================
;============================================================================
;
; All EGA drawing routines that write out words need to have alternate forms
; for starting on even and odd addresses, because writing a word at segment
; offset 0xffff causes an exception! To work around this, write a single
; byte out to make the address even, so it wraps cleanly at the end.
;
; All of these routines assume read/write mode 0, and will allways return
; in that state.
; The direction flag should be clear
; readmap/writemask is left in an undefined state
;
;============================================================================
;============================================================================
;
; VW_Plot (int x,y,color)
;
;============================================================================
DATASEG
plotpixels db 128,64,32,16,8,4,2,1
CODESEG
PROC VW_Plot x:WORD, y:WORD, color:WORD
PUBLIC VW_Plot
USES SI,DI
mov es,[screenseg]
mov dx,SC_INDEX
mov ax,SC_MAPMASK+15*256
WORDOUT
mov dx,GC_INDEX
mov ax,GC_MODE+2*256 ;write mode 2
WORDOUT
mov di,[bufferofs]
mov bx,[y]
shl bx,1
add di,[ylookup+bx]
mov bx,[x]
mov ax,bx
shr ax,1
shr ax,1
shr ax,1
add di,ax ; di = byte on screen
and bx,7
mov ah,[plotpixels+bx]
mov al,GC_BITMASK ;mask off other pixels
WORDOUT
mov bl,[BYTE color]
xchg bl,[es:di] ; load latches and write pixel
mov dx,GC_INDEX
mov ah,0ffh ;no bit mask
WORDOUT
mov ax,GC_MODE+0*256 ;write mode 0
WORDOUT
ret
ENDP
;============================================================================
;
; VW_Vlin (int yl,yh,x,color)
;
;============================================================================
PROC VW_Vlin yl:WORD, yh:WORD, x:WORD, color:WORD
PUBLIC VW_Vlin
USES SI,DI
mov es,[screenseg]
mov dx,SC_INDEX
mov ax,SC_MAPMASK+15*256
WORDOUT
mov dx,GC_INDEX
mov ax,GC_MODE+2*256 ;write mode 2
WORDOUT
mov di,[bufferofs]
mov bx,[yl]
shl bx,1
add di,[ylookup+bx]
mov bx,[x]
mov ax,bx
shr ax,1
shr ax,1
shr ax,1
add di,ax ; di = byte on screen
and bx,7
mov ah,[plotpixels+bx]
mov al,GC_BITMASK ;mask off other pixels
WORDOUT
mov cx,[yh]
sub cx,[yl]
inc cx ;number of pixels to plot
mov bh,[BYTE color]
mov dx,[linewidth]
@@plot:
mov bl,bh
xchg bl,[es:di] ; load latches and write pixel
add di,dx
loop @@plot
mov dx,GC_INDEX
mov ah,0ffh ;no bit mask
WORDOUT
mov ax,GC_MODE+0*256 ;write mode 0
WORDOUT
ret
ENDP
;============================================================================
;===================
;
; VW_DrawTile8
;
; xcoord in bytes (8 pixels), ycoord in pixels
; All Tile8s are in one grseg, so an offset is calculated inside it
;
;===================
PROC VW_DrawTile8 xcoord:WORD, ycoord:WORD, tile:WORD
PUBLIC VW_DrawTile8
USES SI,DI
mov es,[screenseg]
mov di,[bufferofs]
add di,[xcoord]
mov bx,[ycoord]
shl bx,1
add di,[ylookup+bx]
mov [ss:screendest],di ;screen destination
mov bx,[linewidth]
dec bx
mov si,[tile]
shl si,1
shl si,1
shl si,1
shl si,1
shl si,1
mov ds,[grsegs+STARTTILE8*2] ; segment for all tile8s
mov cx,4 ;planes to draw
mov ah,0001b ;map mask
mov dx,SC_INDEX
mov al,SC_MAPMASK
;
; start drawing
;
@@planeloop:
WORDOUT
shl ah,1 ;shift plane mask over for next plane
mov di,[ss:screendest] ;start at same place in all planes
REPT 7
movsb
add di,bx
ENDM
movsb
loop @@planeloop
mov ax,ss
mov ds,ax ;restore turbo's data segment
ret
ENDP
;============================================================================
;
; VW_MaskBlock
;
; Draws a masked block shape to the screen. bufferofs is NOT accounted for.
; The mask comes first, then four planes of data.
;
;============================================================================
DATASEG
UNWOUNDMASKS = 10
maskroutines dw mask0,mask0,mask1E,mask1E,mask2E,mask2O,mask3E,mask3O
dw mask4E,mask4O,mask5E,mask5O,mask6E,mask6O
dw mask7E,mask7O,mask8E,mask8O,mask9E,mask9O
dw mask10E,mask10O
routinetouse dw ?
CODESEG
PROC VW_MaskBlock segm:WORD, ofs:WORD, dest:WORD, wide:WORD, height:WORD, planesize:WORD
PUBLIC VW_MaskBlock
USES SI,DI
mov es,[screenseg]
mov [BYTE planemask],1
mov [BYTE planenum],0
mov di,[wide]
mov dx,[linewidth]
sub dx,[wide]
mov [linedelta],dx ;amount to add after drawing each line
mov bx,[planesize] ; si+bx = data location
cmp di,UNWOUNDMASKS
jbe @@unwoundroutine
mov [routinetouse],OFFSET generalmask
jmp NEAR @@startloop
;=================
;
; use the unwound routines
;
;=================
@@unwoundroutine:
mov cx,[dest]
shr cx,1
rcl di,1 ;shift a 1 in if destination is odd
shl di,1 ;to index into a word width table
mov ax,[maskroutines+di] ;call the right routine
mov [routinetouse],ax
@@startloop:
mov ds,[segm]
@@drawplane:
mov dx,SC_INDEX
mov al,SC_MAPMASK
mov ah,[ss:planemask]
WORDOUT
mov dx,GC_INDEX
mov al,GC_READMAP
mov ah,[ss:planenum]
WORDOUT
mov si,[ofs] ;start back at the top of the mask
mov di,[dest] ;start at same place in all planes
mov cx,[height] ;scan lines to draw
mov dx,[ss:linedelta]
jmp [ss:routinetouse] ;draw one plane
planereturn: ;routine jmps back here
add bx,[ss:planesize] ;start of mask = start of next plane
inc [ss:planenum]
shl [ss:planemask],1 ;shift plane mask over for next plane
cmp [ss:planemask],10000b ;done all four planes?
jne @@drawplane
mask0:
mov ax,ss
mov ds,ax
ret ;width of 0 = no drawing
;==============
;
; General purpose masked block drawing. This could be optimised into
; four routines to use words, but few play loop sprites should be this big!
;
;==============
generalmask:
mov dx,cx
@@lineloopgen:
mov cx,[wide]
@@byteloop:
mov al,[es:di]
and al,[si]
or al,[bx+si]
inc si
stosb
loop @@byteloop
add di,[ss:linedelta]
dec dx
jnz @@lineloopgen
jmp planereturn
;=================
;
; Horizontally unwound routines to draw certain masked blocks faster
;
;=================
MACRO MASKBYTE
mov al,[es:di]
and al,[si]
or al,[bx+si]
inc si
stosb
ENDM
MACRO MASKWORD
mov ax,[es:di]
and ax,[si]
or ax,[bx+si]
inc si
inc si
stosw
ENDM
MACRO SPRITELOOP addr
add di,dx
loop addr
jmp planereturn
ENDM
EVEN
mask1E:
MASKBYTE
SPRITELOOP mask1E
EVEN
mask2E:
MASKWORD
SPRITELOOP mask2E
EVEN
mask2O:
MASKBYTE
MASKBYTE
SPRITELOOP mask2O
EVEN
mask3E:
MASKWORD
MASKBYTE
SPRITELOOP mask3E
EVEN
mask3O:
MASKBYTE
MASKWORD
SPRITELOOP mask3O
EVEN
mask4E:
MASKWORD
MASKWORD
SPRITELOOP mask4E
EVEN
mask4O:
MASKBYTE
MASKWORD
MASKBYTE
SPRITELOOP mask4O
EVEN
mask5E:
MASKWORD
MASKWORD
MASKBYTE
SPRITELOOP mask5E
EVEN
mask5O:
MASKBYTE
MASKWORD
MASKWORD
SPRITELOOP mask5O
EVEN
mask6E:
MASKWORD
MASKWORD
MASKWORD
SPRITELOOP mask6E
EVEN
mask6O:
MASKBYTE
MASKWORD
MASKWORD
MASKBYTE
SPRITELOOP mask6O
EVEN
mask7E:
MASKWORD
MASKWORD
MASKWORD
MASKBYTE
SPRITELOOP mask7E
EVEN
mask7O:
MASKBYTE
MASKWORD
MASKWORD
MASKWORD
SPRITELOOP mask7O
EVEN
mask8E:
MASKWORD
MASKWORD
MASKWORD
MASKWORD
SPRITELOOP mask8E
EVEN
mask8O:
MASKBYTE
MASKWORD
MASKWORD
MASKWORD
MASKBYTE
SPRITELOOP mask8O
EVEN
mask9E:
MASKWORD
MASKWORD
MASKWORD
MASKWORD
MASKBYTE
SPRITELOOP mask9E
EVEN
mask9O:
MASKBYTE
MASKWORD
MASKWORD
MASKWORD
MASKWORD
SPRITELOOP mask9O
EVEN
mask10E:
MASKWORD
MASKWORD
MASKWORD
MASKWORD
MASKWORD
SPRITELOOP mask10E
EVEN
mask10O:
MASKBYTE
MASKWORD
MASKWORD
MASKWORD
MASKWORD
MASKBYTE
SPRITELOOP mask10O
ENDP
;============================================================================
;
; VW_ScreenToScreen
;
; Basic block copy routine. Copies one block of screen memory to another,
; using write mode 1 (sets it and returns with write mode 0). bufferofs is
; NOT accounted for.
;
;============================================================================
PROC VW_ScreenToScreen source:WORD, dest:WORD, wide:WORD, height:WORD
PUBLIC VW_ScreenToScreen
USES SI,DI
pushf
cli
mov dx,SC_INDEX
mov ax,SC_MAPMASK+15*256
WORDOUT
mov dx,GC_INDEX
mov ax,GC_MODE+1*256
WORDOUT
popf
mov bx,[linewidth]
sub bx,[wide]
mov ax,[screenseg]
mov es,ax
mov ds,ax
mov si,[source]
mov di,[dest] ;start at same place in all planes
mov dx,[height] ;scan lines to draw
mov ax,[wide]
@@lineloop:
mov cx,ax
rep movsb
add si,bx
add di,bx
dec dx
jnz @@lineloop
mov dx,GC_INDEX
mov ax,GC_MODE+0*256
WORDOUT
mov ax,ss
mov ds,ax ;restore turbo's data segment
ret
ENDP
;============================================================================
;
; VW_MemToScreen
;
; Basic block drawing routine. Takes a block shape at segment pointer source
; with four planes of width by height data, and draws it to dest in the
; virtual screen, based on linewidth. bufferofs is NOT accounted for.
; There are four drawing routines to provide the best optimized code while
; accounting for odd segment wrappings due to the floating screens.
;
;============================================================================
DATASEG
memtoscreentable dw eventoeven,eventoodd,oddtoeven,oddtoodd
CODESEG
PROC VW_MemToScreen source:WORD, dest:WORD, wide:WORD, height:WORD
PUBLIC VW_MemToScreen
USES SI,DI
mov es,[screenseg]
mov bx,[linewidth]
sub bx,[wide]
mov ds,[source]
xor si,si ;block is segment aligned
xor di,di
shr [wide],1 ;change wide to words, and see if carry is set
rcl di,1 ;1 if wide is odd
mov ax,[dest]
shr ax,1
rcl di,1 ;shift a 1 in if destination is odd
shl di,1 ;to index into a word width table
mov ax,SC_MAPMASK+0001b*256 ;map mask for plane 0
jmp [ss:memtoscreentable+di] ;call the right routine
;==============
;
; Copy an even width block to an even video address
;
;==============
eventoeven:
mov dx,SC_INDEX
WORDOUT
mov di,[dest] ;start at same place in all planes
mov dx,[height] ;scan lines to draw
@@lineloopEE:
mov cx,[wide]
rep movsw
add di,bx
dec dx
jnz @@lineloopEE
shl ah,1 ;shift plane mask over for next plane
cmp ah,10000b ;done all four planes?
jne eventoeven
mov ax,ss
mov ds,ax ;restore turbo's data segment
ret
;==============
;
; Copy an odd width block to an even video address
;
;==============
oddtoeven:
mov dx,SC_INDEX
WORDOUT
mov di,[dest] ;start at same place in all planes
mov dx,[height] ;scan lines to draw
@@lineloopOE:
mov cx,[wide]
rep movsw
movsb ;copy the last byte
add di,bx
dec dx
jnz @@lineloopOE
shl ah,1 ;shift plane mask over for next plane
cmp ah,10000b ;done all four planes?
jne oddtoeven
mov ax,ss
mov ds,ax ;restore turbo's data segment
ret
;==============
;
; Copy an even width block to an odd video address
;
;==============
eventoodd:
dec [wide] ;one word has to be handled seperately
EOplaneloop:
mov dx,SC_INDEX
WORDOUT
mov di,[dest] ;start at same place in all planes
mov dx,[height] ;scan lines to draw
@@lineloopEO:
movsb
mov cx,[wide]
rep movsw
movsb
add di,bx
dec dx
jnz @@lineloopEO
shl ah,1 ;shift plane mask over for next plane
cmp ah,10000b ;done all four planes?
jne EOplaneloop
mov ax,ss
mov ds,ax ;restore turbo's data segment
ret
;==============
;
; Copy an odd width block to an odd video address
;
;==============
oddtoodd:
mov dx,SC_INDEX
WORDOUT
mov di,[dest] ;start at same place in all planes
mov dx,[height] ;scan lines to draw
@@lineloopOO:
movsb
mov cx,[wide]
rep movsw
add di,bx
dec dx
jnz @@lineloopOO
shl ah,1 ;shift plane mask over for next plane
cmp ah,10000b ;done all four planes?
jne oddtoodd
mov ax,ss
mov ds,ax ;restore turbo's data segment
ret
ENDP
;===========================================================================
;
; VW_ScreenToMem
;
; Copies a block of video memory to main memory, in order from planes 0-3.
; This could be optimized along the lines of VW_MemToScreen to take advantage
; of word copies, but this is an infrequently called routine.
;
;===========================================================================
PROC VW_ScreenToMem source:WORD, dest:WORD, wide:WORD, height:WORD
PUBLIC VW_ScreenToMem
USES SI,DI
mov es,[dest]
mov bx,[linewidth]
sub bx,[wide]
mov ds,[screenseg]
mov ax,GC_READMAP ;read map for plane 0
xor di,di
@@planeloop:
mov dx,GC_INDEX
WORDOUT
mov si,[source] ;start at same place in all planes
mov dx,[height] ;scan lines to draw
@@lineloop:
mov cx,[wide]
rep movsb
add si,bx
dec dx
jnz @@lineloop
inc ah
cmp ah,4 ;done all four planes?
jne @@planeloop
mov ax,ss
mov ds,ax ;restore turbo's data segment
ret
ENDP
;============================================================================
;
; VWL_UpdateScreenBlocks
;
; Scans through the update matrix and copies any areas that have changed
; to the visable screen, then zeros the update array
;
;============================================================================
; AX 0/1 for scasb, temp for segment register transfers
; BX width for block copies
; CX REP counter
; DX line width deltas
; SI source for copies
; DI scas dest / movsb dest
; BP pointer to end of bufferblocks
PROC VWL_UpdateScreenBlocks
PUBLIC VWL_UpdateScreenBlocks
USES SI,DI,BP
jmp SHORT @@realstart
@@done:
;
; all tiles have been scanned
;
mov dx,GC_INDEX ; restore write mode 0
mov ax,GC_MODE+0*256
WORDOUT
xor ax,ax ; clear out the update matrix
mov cx,UPDATEWIDE*UPDATEHIGH/2
mov di,[updateptr]
rep stosw
ret
@@realstart:
mov dx,SC_INDEX
mov ax,SC_MAPMASK+15*256
WORDOUT
mov dx,GC_INDEX
mov ax,GC_MODE+1*256
WORDOUT
mov di,[updateptr] ; start of floating update screen
mov bp,di
add bp,UPDATEWIDE*UPDATEHIGH+1 ; when di = bp, all tiles have been scanned
push di
mov cx,-1 ; definately scan the entire thing
;
; scan for a 1 in the update list, meaning a tile needs to be copied
; from the master screen to the current screen
;
@@findtile:
pop di ; place to continue scaning from
mov ax,ss
mov es,ax ; search in the data segment
mov ds,ax
mov al,1
repne scasb
cmp di,bp
jae @@done
cmp [BYTE di],al
jne @@singletile
jmp @@tileblock
;============
;
; copy a single tile
;
;============
@@singletile:
inc di ; we know the next tile is nothing
push di ; save off the spot being scanned
sub di,[updateptr]
shl di,1
mov di,[blockstarts-4+di] ; start of tile location on screen
mov si,di
add si,[bufferofs]
add di,[displayofs]
mov dx,[linewidth]
sub dx,2
mov ax,[screenseg]
mov ds,ax
mov es,ax
REPT 15
movsb
movsb
add si,dx
add di,dx
ENDM
movsb
movsb
jmp @@findtile
;============
;
; more than one tile in a row needs to be updated, so do it as a group
;
;============
EVEN
@@tileblock:
mov dx,di ; hold starting position + 1 in dx
inc di ; we know the next tile also gets updated
repe scasb ; see how many more in a row
push di ; save off the spot being scanned
mov bx,di
sub bx,dx ; number of tiles in a row
shl bx,1 ; number of bytes / row
mov di,dx ; lookup position of start tile
sub di,[updateptr]
shl di,1
mov di,[blockstarts-2+di] ; start of tile location
mov si,di
add si,[bufferofs]
add di,[displayofs]
mov dx,[linewidth]
sub dx,bx ; offset to next line on screen
mov ax,[screenseg]
mov ds,ax
mov es,ax
REPT 15
mov cx,bx
rep movsb
add si,dx
add di,dx
ENDM
mov cx,bx
rep movsb
dec cx ; was 0 from last rep movsb, now $ffff for scasb
jmp @@findtile
ENDP
;===========================================================================
;
; MISC EGA ROUTINES
;
;===========================================================================
;==============
;
; VW_SetScreen
;
;==============
PROC VW_SetScreen crtc:WORD, pel:WORD
PUBLIC VW_SetScreen
if waitforvbl
mov dx,STATUS_REGISTER_1
;
; wait util the CRTC just starts scaning a diplayed line to set the CRTC start
;
cli
@@waitnodisplay:
in al,dx
test al,01b
jz @@waitnodisplay
@@waitdisplay:
in al,dx
test al,01b
jnz @@waitdisplay
endif
;
; set CRTC start
;
; for some reason, my XT's EGA card doesn't like word outs to the CRTC
; index...
;
mov cx,[crtc]
mov dx,CRTC_INDEX
mov al,0ch ;start address high register
out dx,al