-
Notifications
You must be signed in to change notification settings - Fork 6
/
lowlevel.asm
1667 lines (1536 loc) · 69.5 KB
/
lowlevel.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
;**
;* Cosmore
;* Copyright (c) 2020-2023 Scott Smitelli and contributors
;*
;* Based on COSMO{1..3}.EXE distributed with "Cosmo's Cosmic Adventure"
;* Copyright (c) 1992 Apogee Software, Ltd.
;*
;* This source code is licensed under the MIT license found in the LICENSE file
;* in the root directory of this source tree.
;*
;******************************************************************************
;* COSMORE LOW-LEVEL ASSEMBLY FUNCTIONS *
;* *
;* The majority of the code here handles EGA setup, page flipping, and the *
;* low-level drawing for individual 8x8 pixel tiles. There is also a CPU *
;* detection routine at the end of the file. *
;* *
;* References: *
;* - [ECD]: IBM PC Hardware Reference Library - Enhanced Color Display, *
;* August 2, 1984 *
;* - [EGA]: IBM PC Hardware Reference Library - Enhanced Graphics Adapter, *
;* August 2, 1984 *
;* - [iAPX]: Intel iAPX 86/88, 186/188 User's Manual / Programmer's *
; Reference, 1985 *
;* - [Smith]: CPUID by Bob Smith, PC Tech Journal Vol 4 No 4, April 1986 *
;* - [Tischer]: PC System Programming for Developers by Michael Tischer, 1988 *
;******************************************************************************
; This game uses EGA mode Dh's default palette with a few rare exceptions. This
; palette has several useful properties: Bits 0,1,2,3 in the palette index
; correspond directly to memory planes 0,1,2,3 and to bits 0,1,2,4 in the color
; value. When shown on screen, each bit, regardless of the context, affects the
; visible B,G,R,I channels in a predictable way. (See the documentation for
; the SetPaletteRegister procedure for an explanation of why the color value
; bits are not contiguous.)
;
; Palette | Plane Number/Channel || Color |
; Index | 3/I | 2/R | 1/G | 0/B || Value | Color Name
; --------+-----+-----+-----+-----++-------+-----------
; 0 | 0 | 0 | 0 | 0 || 0 | Black
; 1 | 0 | 0 | 0 | 1 || 1 | Blue
; 2 | 0 | 0 | 1 | 0 || 2 | Green
; 3 | 0 | 0 | 1 | 1 || 3 | Cyan
; 4 | 0 | 1 | 0 | 0 || 4 | Red
; 5 | 0 | 1 | 0 | 1 || 5 | Magenta
; 6 | 0 | 1 | 1 | 0 || 6 | Brown
; 7 | 0 | 1 | 1 | 1 || 7 | Dark White (Light Gray)
; 8 | 1 | 0 | 0 | 0 || 16 | Bright Black (Dark Gray)
; 9 | 1 | 0 | 0 | 1 || 17 | Bright Blue
; 10 | 1 | 0 | 1 | 0 || 18 | Bright Green
; 11 | 1 | 0 | 1 | 1 || 19 | Bright Cyan
; 12 | 1 | 1 | 0 | 0 || 20 | Bright Red
; 13 | 1 | 1 | 0 | 1 || 21 | Bright Magenta
; 14 | 1 | 1 | 1 | 0 || 22 | Bright Yellow
; 15 | 1 | 1 | 1 | 1 || 23 | Bright White
IDEAL
ASSUME cs:_TEXT, ds:DGROUP, es:NOTHING
INCLUDE "lowlevel.equ"
SEGMENT _DATA
;
; Global storage in the data segment.
;
EXTRN _yOffsetTable:WORD:25 ; Maps tile Y coordinate to EGA memory offset
yOffsetTable EQU _yOffsetTable ; Defined in game2.c
ENDS
SEGMENT _TEXT
;
; Global storage in the code segment.
;
drawPageNumber dw 0 ; Most recent SelectDrawPage call argument
drawPageSegment dw EGA_SEGMENT ; EGA memory segment to be written to
; Subsequent instructions can use 80286 opcodes if desired, as that's the
; minimum supported CPU in-game.
P286
;
; Select the Map Mask [EGA, pg. 20] via the Sequencer Address Register [EGA, pg.
; 18].
;
MACRO SELECT_EGA_SEQ_MAP_MASK
mov dx,SEQUENCER_ADDR
mov al,SEQ_MAP_MASK
out dx,al
ENDM
;
; Set the Color Don't Care [EGA, pg. 53] bits for all four color planes via the
; Graphics 1 & 2 Address Register [EGA, pg. 46] using the provided `mask` value.
;
; The bits in `mask` refer to planes 3-2-1-0. Two different byte-sized registers
; are being written with a single word-sized OUT; the high byte goes to the
; GRAPHICS_DATA I/O port.
;
MACRO SET_EGA_COLOR_DONT_CARE mask
mov dx,GRAPHICS_1_2_ADDR
mov ax,(mask SHL 8) OR GFX_COLOR_DONT_CARE
out dx,ax
ENDM
;
; Load a `mask` into the Bit Mask Register [EGA, pg. 54] via the Graphics 1 & 2
; Address Register [EGA, pg. 46].
;
; Each bit in `mask` refers to one screen pixel positon. Two different byte-
; sized registers are being written with a single word-sized OUT; the high byte
; goes to the GRAPHICS_DATA I/O port.
;
MACRO SET_EGA_BIT_MASK mask
mov dx,GRAPHICS_1_2_ADDR
mov ax,(mask SHL 8) OR GFX_BIT_MASK
out dx,ax
ENDM
;
; Load a `mask` into the Map Mask [EGA, pg. 20] via the Sequencer Address
; Register [EGA, pg. 18].
;
; The bits in `mask` refer to planes 3-2-1-0. Two different byte-sized registers
; are being written with a single word-sized OUT; the high byte goes to the
; SEQUENCER_DATA I/O port.
;
MACRO SET_EGA_MAP_MASK mask
mov dx,SEQUENCER_ADDR
mov ax,(mask SHL 8) OR SEQ_MAP_MASK
out dx,ax
ENDM
;
; Load `map` into the Read Map Select register [EGA, pg. 50] via the Graphics 1
; & 2 Address Register [EGA, pg. 46] to select one of the four color planes
; using decimal notation.
;
; Plane numbers range from 0 (blue) to 3 (intensity). Two bytes are sent in one
; word OUT; the high byte goes to the GRAPHICS_DATA I/O port.
;
MACRO SET_EGA_READ_MAP map
mov dx,GRAPHICS_1_2_ADDR
mov ax,(map SHL 8) OR GFX_READ_MAP_SELECT
out dx,ax
ENDM
;
; Load a `mode` byte into the Mode register [EGA, pg. 50] via the Graphics 1 & 2
; Address Register [EGA, pg. 46].
;
; Two different byte- sized registers are being written with a single word-sized
; OUT; the high byte goes to the GRAPHICS_DATA I/O port.
;
MACRO SET_EGA_GC_MODE mode
mov dx,GRAPHICS_1_2_ADDR
mov ax,(mode SHL 8) OR GFX_MODE
out dx,ax
ENDM
;
; Set the video mode to the specified mode number and initialize the EGA card.
;
; mode_num (word): The BIOS video mode to enter. Only the low byte is used.
; Returns: Nothing
; Registers destroyed: AX, DX
;
PROC _SetVideoMode FAR @@mode_num:WORD
PUBLIC _SetVideoMode
push bp
mov bp,sp
; Change video mode via BIOS video service interrupt [EGA, pg. 104].
mov ax,[@@mode_num]
mov ah,VSVC_SET_VIDEO_MODE
int INT_VIDEO_SERVICE
; Turn *on* Color Don't Care for all four color planes. This appears to
; be an instance where the IBM documentation is either confusing or
; flat-out incorrect -- bit value 1 means the Color Compare register is
; used during applicable memory read operations, and value 0 means the
; Color Compare register is ignored. This only affects memory read
; operations when Read Mode = 1, and the only place these types of reads
; occur is in the DrawSpriteTileWhite procedure.
SET_EGA_COLOR_DONT_CARE 0000b
; Pre-select the Map Mask, but don't actually write anything to it yet.
; This probably isn't needed -- _usually_ any further changes to the map
; mask are accompanied by a re-select of this register.
SELECT_EGA_SEQ_MAP_MASK
pop bp
ret
ENDP
;
; Load the video border (overscan) register with the specified color value.
;
; See the documentation for the SetPaletteRegister procedure for a detailed
; description of the color value.
;
; color_value (word): The color value to program into the register (0..63). Only
; the low byte is used.
; Returns: Nothing
; Registers destroyed: AX, BX
;
PROC _SetBorderColorRegister FAR @@color_value:WORD
PUBLIC _SetBorderColorRegister
push bp
mov bp,sp
; Change border color via BIOS video service interrupt [EGA, pg. 104].
mov ah,VSVC_PALETTE_REGISTERS
mov al,PALREG_SET_BORDER_COLOR
mov bx,[@@color_value]
mov bh,bl ; Value in BL is not used again
int INT_VIDEO_SERVICE
pop bp
ret
ENDP
;
; Load one video palette register with the specified color value.
;
; The color value is a byte with the highest two bits unused. The exact meaning
; of the bits changes depending on the video mode and the type of display
; connected to the adapter. IBM had three different display units that used
; similar 9-pin connectors:
; - IBM Monochrome Display
; - IBM Color Display
; - IBM Enhanced Color Display
;
; The three display types were electrically interchangeable, and the EGA
; hardware was capable of driving any of the three displays provided it was set
; to a video mode that was supported by that display. Since the signal at each
; pin could be either on or off (with no intermediate levels), the two signal
; pins on the Monochrome Display supported 4 different shades of mono, the four
; signal pins on the Color Display supported 16 colors, and the six signal pins
; on the Enhanced Color Display supported 64 colors. There is a direct mapping
; between the individual bits in a color value and the signals on the display
; connector pins, as shown on the abbreviated pinouts [ECD, pg. 6]:
;
; Pin | Monochrome | Color | Enhanced Color | Color Value Bit
; ----+-------------+---------------+-----------------+----------------
; 5 | -- | Blue | Blue | 00.....X
; 4 | -- | Green | Green | 00....X.
; 3 | -- | Red | Red | 00...X..
; 7 | Mono Signal | -- (Reserved) | Blue Intensity | 00..X...
; 6 | Intensity | Intensity | Green Intensity | 00.X....
; 2 | -- (Ground) | -- (Ground) | Red Intensity | 00X.....
;
; NOTE: The base Red/Green/Blue signals contribute 67% of the power of a color
; channel, and the corresponding Intensity signal(s) provide the remaining 33%.
; Some sources refer to the Intensity signals as "least significant" due to the
; amount of output they generate, but this is confusing from the perspective of
; color value bit packing and we will never say it that way here.
;
; The Enhanced Color Display supported two modes [ECD, pg. 3]:
; - Mode 1: 200 lines, 15.75 kHz HSync rate, positive VSync pulse
; - Mode 2: 350 lines, 21.8 kHz HSync rate, negative VSync pulse
;
; The display used the polarity of the Vertical Sync pulse to determine which
; mode to use. In display mode 2, all six color inputs contributed to the
; picture and 64 colors were available for use. In mode 1, the Red Intensity and
; Blue Intensity inputs to the display were ignored, and the Green Intensity
; signal was applied across all three color channels [ECD, pg. 4]. This limited
; the display to 16 colors using the same RGBI pinout the Color Display used.
; Mode 1 also emulated the Color Display's handling of the color brown: When
; RGBI 1100 was received, the display fudged *just* the Green Intensity bit on
; to avoid an unpleasant dark yellow [ECD, pg. 4].
;
; This game uses video mode Dh exclusively -- a 200-line mode -- so there is
; only one intensity bit that can visibly change the screen color and it is not
; contiguous with the R/G/B bits. The 64 color values are really 16 distinct
; display colors, each repeated four times. The effective bit positions are:
; Bits | Meaning
; ---------+--------
; .......X | Blue
; ......X. | Green
; .....X.. | Red
; ....0... | Not Used
; ...X.... | Intensity
; 000..... | Not Used
;
; A silver lining to this is that, with the default EGA palette loaded, the bits
; in a palette register index correspond to bits in the color value, which match
; the RGBI signals being shown on the display. This is what allows EGA memory
; planes 0123 to be thought of as BGRI instead. If the sequence were changed or
; custom-mixed colors were used, this reasonable mental model would fall apart.
;
; palette_index (word): The palette register index (0..15). Only the low byte is
; used.
; color_value (word): The color value to program into the register (0..63). Only
; the low byte is used.
; Returns: Nothing
; Registers destroyed: AX, BX
;
PROC _SetPaletteRegister FAR @@palette_index:WORD, @@color_value:WORD
PUBLIC _SetPaletteRegister
push bp
mov bp,sp
; Change one palette register via BIOS video service interrupt [EGA, pg.
; 105]. Two byte-sized subfunction numbers are being loaded in one MOV
; into AX.
mov ax,(VSVC_PALETTE_REGISTERS SHL 8) OR PALREG_SET_ONE_COLOR
mov bl,[BYTE PTR @@palette_index]
mov bh,[BYTE PTR @@color_value]
int INT_VIDEO_SERVICE
pop bp
ret
ENDP
;
; Draw a single 8x8 pixel solid tile to the current draw page.
;
; This procedure draws non-transparent tiles that are part of the game map and
; backdrops. It also draws solid backgrounds for UI elements.
;
; Source data is read from EGA memory at SOLID_TILE_SEGMENT:src_offset.
; The destination address is drawPageSegment:dest_offset.
; The EGA *must* be in latched write mode for this to work correctly.
;
; Each tile is an 8x8 pixel square. Each 8-pixel tile row occupies one byte of
; EGA address space (1 bit per pixel), for a total of 8 bytes per tile. These 8
; bytes are stored sequentially in the source memory, or at 40-byte intervals
; in the destination memory.
;
; Within the EGA, each memory read/write operation is quadrupled across the four
; color planes. Although only 8 bytes of address space are handled during each
; call to this procedure, 32 bytes of physical memory are copied internally.
;
; src_offset (word): Memory offset of the source tile. This value should always
; be a multiple of 8.
; dst_offset (word): Memory offset to write to, relative to the current draw
; page segment.
; Returns: Nothing
; Registers destroyed: AL, DX, ES
;
PROC _DrawSolidTile FAR @@src_offset:WORD, @@dst_offset:WORD
PUBLIC _DrawSolidTile
push bp
mov bp,sp
push ds
push si
push di
; Set up source and destination pointers from the arguments:
; DS:SI <- Source tile data address (in EGA memory)
; ES:DI <- Destination draw page address (in EGA memory)
mov dx,[drawPageSegment]
mov es,dx
mov dx,SOLID_TILE_SEGMENT
mov ds,dx
ASSUME ds:NOTHING
mov si,[@@src_offset]
mov di,[@@dst_offset]
; Draw eight rows of tile pixels. All four color planes are copied, in
; parallel, through the EGA's internal latches. The memory read/write
; cycles are doing the actual work here -- the value in AL has no effect
; on the visual result. Latched write mode must be enabled here!
srcoff = 0
dstoff = 0
REPT 8
mov al,[si+srcoff]
mov [es:di+dstoff],al
srcoff = srcoff + 1
dstoff = dstoff + SCREEN_Y_STRIDE
ENDM
pop di
pop si
pop ds
ASSUME ds:DGROUP
pop bp
ret
ENDP
;
; Recalculate the drawPageSegment address from the current drawPageNumber.
;
; In all typical cases:
;
; PgNum | Segment
; ------+--------
; 0 | A000h
; 1 | A200h
;
; Returns: Nothing
; Registers destroyed: AX, BX
;
PROC UpdateDrawPageSegment FAR
push bp
mov bp,sp
push ds
; drawPageSegment = EGA_SEGMENT + (drawPageNumber * 200h)
mov ax,[drawPageNumber]
xchg ah,al
shl ah,1
mov bx,EGA_SEGMENT
add bx,ax
mov [drawPageSegment],bx
pop ds
pop bp
ret
ENDP
;
; Select the video page where subsequent writes should occur.
;
; Although higher page numbers are accepted and will store a sensible result,
; pages beyond number 1 are used for tile storage and writing to them will
; corrupt the graphics.
;
; page_num (word): Page number to write to (0..1).
; Returns: Nothing
; Registers destroyed: AX, and those destroyed by UpdateDrawPageSegment
;
PROC _SelectDrawPage FAR @@page_num:WORD
PUBLIC _SelectDrawPage
push bp
mov bp,sp
; Stash the selected draw page number
mov ax,[@@page_num]
mov [drawPageNumber],ax
; Update the segment address that the page number refers to
call UpdateDrawPageSegment
pop bp
ret
ENDP
;
; Draw a single 8x8 pixel sprite tile, with translucency, to the draw page.
;
; The translucency effect is created by turning the "intensity" bit on for every
; pixel covered by the sprite, while leaving uncovered pixels alone. Visually
; this causes dark colors to lighten, and light colors to stay the same. The
; other color planes do not change.
;
; NOTE: This color effect fundamentally changes the pixels, meaning that
; "magenta" areas become "bright magenta" and no longer match for palette
; animation purposes.
;
; The tile data contains five color planes -- mask, blue, green, red, and
; intensity -- with 1-bit color depth on each plane. This procedure only deals
; with mask, ignoring the content of the other four planes. Tile image data is
; stored byte-planar in MBGRI order. Transparent areas have their mask bits set
; to 1.
;
; The basic theory of operation is as follows. For each pixel row in the tile:
; 1. Read a mask byte from the source data. Each bit maps to one screen pixel.
; 2. Invert the mask data bits to match the EGA's expectations.
; 3. Load the mask data into the EGA bit mask register. For each 0 bit in the
; bit mask register, the EGA will ignore the data sent by the processor in
; subsequent memory write operations and instead use the contents of a latch.
; 4. Load the map mask register with the binary value 1000, indicating that we
; only want the writes to apply to the intensity plane. All other color
; planes will remain unaffected.
; 5. Read the EGA memory to freshen the content of the latches, then write
; binary value 11111111 back to the EGA memory, invoking a refresh of every
; pixel on the row. Pixels that belong to a transparent area will be masked
; off by the bit mask register and will not change. Planes that are not
; intensity (i.e. RGB) will be masked off by the map mask register and will
; not change either. The net result is that the intensity bit will be turned
; on for every pixel belonging to an opaque part of the tile, while
; transparent areas retain their previous value.
; 6. Advance the read position by a 5-byte step, and advance the write position
; by a 40-byte step, in preparation for the next pixel row.
;
; Intersting tidbit: The written value in step 5 could just as easily be changed
; to all zeros, in which case the intensity bit would be turned *off* for every
; opaque pixel, leading to a darkening of light areas. Try it someday; the
; effect is pleasant.
;
; src (far pointer): Memory address of the first byte of tile data to read.
; x (word): X-position on the screen, in tiles. (0..39, leftmost column is 0)
; y (word): Y-position on the screen, in tiles. (0..24, topmost row is 0)
; Returns: Nothing
; Registers destroyed: AX, BX, CX, DX, ES
;
PROC _DrawSpriteTileTranslucent FAR @@src:FAR PTR, @@x:WORD, @@y:WORD
PUBLIC _DrawSpriteTileTranslucent
push bp
mov bp,sp
push ds
push di
push si
; Set up source and destination pointers from the arguments:
; DS:SI <- Source tile data address (could be anywhere in memory)
; ES:BX <- Destination draw page address (in EGA memory)
lds si,[@@src]
ASSUME ds:NOTHING
mov dx,[drawPageSegment]
mov es,dx
; NOTE: This procedure eschews the global yOffsetTable in favor of
; calculating the destination address directly. That may have been a
; missed optimization, as this drawing procedure is seldom used in the
; game. The following instructions are effectively doing:
; BX = (y * 320) + x
; using shifts/adds instead of multiplication. An alternative:
; mov di,[@@y]
; shl di,1
; mov bx,[yOffsetTable+di]
; add bx,[@@x]
; is equivalent to:
; BX = yOffsetTable[y] + x
mov bx,[@@y]
mov ax,bx
shl ax,1
shl ax,1
add ax,bx
REPT 6
shl ax,1
ENDM
mov bx,[@@x]
add bx,ax
; Pre-select the Map Mask before the loop is entered.
SELECT_EGA_SEQ_MAP_MASK
; Draw eight rows of tile pixels.
mov cx,8
@@do_row:
; Read one byte of mask data (8 bits for an 8-pixel row) from the source
; memory. Write the mask data back into the EGA Bit Mask Register [EGA,
; pg. 54] via the Graphics 1 & 2 Address Register [EGA, pg. 46] to
; disallow changes to pixels that are not part of the sprite's shape.
; Two different byte-sized registers are being written with a single
; word-sized OUT; the high byte goes to the GRAPHICS_DATA I/O port.
mov dx,GRAPHICS_1_2_ADDR
lodsb
not al ; Tile data stores mask opposite to how EGA does
mov ah,GFX_BIT_MASK
xchg ah,al
out dx,ax
; Program the map mask (which was selected before this loop was entered)
; to only operate on plane 3 -- the intensity bit in the default game
; palette.
mov dx,SEQUENCER_DATA
mov al,1000b ; Intensity -- bits are planes 3210
out dx,al
; Since the tile data stores plane bytes in MBGRI order, but we only
; care about mask, we must advance SI an additional 4 bytes in order for
; the next iteration to read the correct plane. In the middle of that,
; we also read and then write back a byte of video memory to actually
; commit the changes that were previously set up. Each memory bit gets
; set to 1 if it satisfies the mask conditions, otherwise the written
; bit is ignored and the latched value is used in its place.
inc si
mov al,11111111b ; All 8 pixels of the row are candidates
xchg al,[es:bx] ; Result in AL is not used again
inc si
inc si
inc si
; Advance the destination write position, then do the next row.
add bx,SCREEN_Y_STRIDE
loop @@do_row
pop si
pop di
pop ds
ASSUME ds:DGROUP
pop bp
ret
ENDP
;
; Lighten the right half of a single 8x8 pixel screen tile.
;
; Applies the following pattern to the selected screen tile's contents:
; .......@
; ......@@
; .....@@@
; ....@@@@
; ...@@@@@
; ..@@@@@@
; .@@@@@@@
; @@@@@@@@
; where pixels marked `.` do not change and pixels marked `@` have their
; "intensity" bit unconditionally turned on. Visually this causes dark colors to
; lighten, and light colors to stay the same. The other color planes do not
; change. This is usually applied to the "west" (i.e. left) edge of light beams
; in maps.
;
; NOTE: This assumes port 3C4h has been set to 2h (sequencer index = map mask).
; This is the only state that the game ever leaves the register in, but it's an
; unsafe assumption.
;
; NOTE: This color effect fundamentally changes the pixels, meaning that
; "magenta" areas become "bright magenta" and no longer match for palette
; animation purposes.
;
; The basic theory of operation is as follows. For each pixel row in the tile:
; 1. Select a new mask. The mask follows a pattern of 1 bits shifting in from
; the right while 0 bits shift out of the left.
; 2. Load the mask into the EGA bit mask register. For each 0 bit in the bit
; mask register, the EGA will ignore the data sent by the processor in
; subsequent memory write operations and instead use the contents of a latch.
; 3. Read the EGA memory to freshen the content of the latches, then write
; binary 1's back to the EGA memory. Pixels that were masked off by the bit
; mask register will not change. Planes that are not intensity (i.e. RGB)
; will be masked off by the map mask register and will not change either.
; The net result is that the intensity bit will be turned on for every pixel
; covered by a "light" region of the mask, while uncovered areas retain their
; previous value.
; 4. Advance the write position by a 40-byte step in preparation for the next
; pixel row.
;
; x (word): X-position on the screen, in tiles. (0..39, leftmost column is 0)
; y (word): Y-position on the screen, in tiles. (0..24, topmost row is 0)
; Returns: Nothing
; Registers destroyed: AX, BX, DX, ES
;
PROC _LightenScreenTileWest FAR @@x:WORD, @@y:WORD
PUBLIC _LightenScreenTileWest
push bp
mov bp,sp
push ds
push di
push si
; Set up destination pointer from the arguments:
; ES:BX <- Destination draw page address (in EGA memory)
mov dx,[drawPageSegment]
mov es,dx
mov di,[@@y]
shl di,1
mov bx,[yOffsetTable+di]
add bx,[@@x]
; Program the EGA Map Mask [EGA, pg. 20] to only operate on plane 3 --
; the intensity bit in the default game palette. NOTE: This is making an
; *unsafe* assumption that the value SEQ_MAP_MASK has been previously
; loaded into the SEQUENCER_ADDR I/O port. The assumption seems to not
; cause issues in the game.
mov dx,SEQUENCER_DATA
mov al,1000b ; Intensity -- bits are planes 3210
out dx,al
; Redraw eight rows of tile pixels. Each iteration uses a new bit mask
; loaded into the Bit Mask Register [EGA, pg. 54] via the Graphics 1 & 2
; Address Register [EGA, pg. 46] which progressively lightens more and
; more of each row. Two different byte-sized registers are being written
; with a single word-sized OUT; the high byte goes to the GRAPHICS_DATA
; I/O port.
mov dx,GRAPHICS_1_2_ADDR
dstoff = 0
IRP mask,<00000001b,00000011b,00000111b,00001111b,00011111b,00111111b,01111111b,11111111b>
mov ax,(mask SHL 8) OR GFX_BIT_MASK
out dx,ax
; Read and then write back a byte of video memory to actually commit the
; changes that were previously set up. Each memory bit gets set to 1 if
; it satisfies the mask conditions, otherwise the written bit is ignored
; and the latched value is used in its place. The precise value written
; doesn't matter as long as there is a 1 bit set for every pixel that
; needs lightening. The mask left in AH conveniently has this property.
xchg ah,[es:bx+dstoff] ; Result in AH is not used again
; Advance the destination write position, then the block repeats.
dstoff = dstoff + SCREEN_Y_STRIDE
ENDM
pop si
pop di
pop ds
pop bp
ret
ENDP
;
; Lighten the full area of a single 8x8 pixel screen tile.
;
; Every pixel in the specified tile has its "intensity" bit unconditionally
; turned on. Visually this causes dark colors to lighten, and light colors to
; stay the same. The other color planes do not change. This is usually applied
; to the main body of light beams in maps.
;
; NOTE: This assumes port 3C4h has been set to 2h (sequencer index = map mask).
; This is the only state that the game ever leaves the register in, but it's an
; unsafe assumption.
;
; NOTE: This color effect fundamentally changes the pixels, meaning that
; "magenta" areas become "bright magenta" and no longer match for palette
; animation purposes.
;
; x (word): X-position on the screen, in tiles. (0..39, leftmost column is 0)
; y (word): Y-position on the screen, in tiles. (0..24, topmost row is 0)
; Returns: Nothing
; Registers destroyed: AX, BX, DX, ES
;
PROC _LightenScreenTile FAR @@x:WORD, @@y:WORD
PUBLIC _LightenScreenTile
push bp
mov bp,sp
push ds
push di
push si
; Set up destination pointer from the arguments:
; ES:BX <- Destination draw page address (in EGA memory)
mov dx,[drawPageSegment]
mov es,dx
mov di,[@@y]
shl di,1
mov bx,[yOffsetTable+di]
add bx,[@@x]
; Load a mask containing all 1 bits into the Bit Mask Register,
; permitting writes to every pixel in the row.
SET_EGA_BIT_MASK 11111111b
; Program the EGA Map Mask [EGA, pg. 20] to only operate on plane 3 --
; the intensity bit in the default game palette. NOTE: This is making an
; *unsafe* assumption that the value SEQ_MAP_MASK has been previously
; loaded into the SEQUENCER_ADDR I/O port. The assumption seems to not
; cause issues in the game.
mov dx,SEQUENCER_DATA
mov al,1000b ; Intensity -- bits are planes 3210
out dx,al
; Redraw eight rows of tile pixels. Since no part of the row is masked
; off, there is no need to set up the latches by reading first. All of
; the pixels on each row of the intensity plane are set to a binary 1.
mov al,11111111b
dstoff = 0
REPT 8
mov [es:bx+dstoff],al
; Advance the destination write position, then the block repeats.
dstoff = dstoff + SCREEN_Y_STRIDE
ENDM
pop si
pop di
pop ds
pop bp
ret
ENDP
;
; Lighten the left half of a single 8x8 pixel screen tile.
;
; Applies the following pattern to the selected screen tile's contents:
; @.......
; @@......
; @@@.....
; @@@@....
; @@@@@...
; @@@@@@..
; @@@@@@@.
; @@@@@@@@
; where pixels marked `.` do not change and pixels marked `@` have their
; "intensity" bit unconditionally turned on. Visually this causes dark colors to
; lighten, and light colors to stay the same. The other color planes do not
; change. This is usually applied to the "east" (i.e. right) edge of light beams
; in maps.
;
; NOTE: This assumes port 3C5h has been set to 1000b (map mask = intensity plane
; only). LightenScreenTile and LightenScreenTileWest both leave the map mask in
; the correct state, but there's a chance that, if this procedure runs first,
; the mask could be incorrectly set.
;
; NOTE: This color effect fundamentally changes the pixels, meaning that
; "magenta" areas become "bright magenta" and no longer match for palette
; animation purposes.
;
; The basic theory of operation is as follows. For each pixel row in the tile:
; 1. Select a new mask. The mask follows a pattern of 1 bits shifting in from
; the left while 0 bits shift out of the right.
; 2. Load the mask into the EGA bit mask register. For each 0 bit in the bit
; mask register, the EGA will ignore the data sent by the processor in
; subsequent memory write operations and instead use the contents of a latch.
; 3. Read the EGA memory to freshen the content of the latches, then write
; binary 1's back to the EGA memory. Pixels that were masked off by the bit
; mask register will not change. Planes that are not intensity (i.e. RGB)
; will be masked off by the map mask register and will not change either.
; The net result is that the intensity bit will be turned on for every pixel
; covered by a "light" region of the mask, while uncovered areas retain their
; previous value.
; 4. Advance the write position by a 40-byte step in preparation for the next
; pixel row.
;
; x (word): X-position on the screen, in tiles. (0..39, leftmost column is 0)
; y (word): Y-position on the screen, in tiles. (0..24, topmost row is 0)
; Returns: Nothing
; Registers destroyed: AX, BX, DX, ES
;
PROC _LightenScreenTileEast FAR @@x:WORD, @@y:WORD
PUBLIC _LightenScreenTileEast
push bp
mov bp,sp
push ds
push di
push si
; Set up destination pointer from the arguments:
; ES:BX <- Destination draw page address (in EGA memory)
mov dx,[drawPageSegment]
mov es,dx
mov di,[@@y]
shl di,1
mov bx,[yOffsetTable+di]
add bx,[@@x]
; Pre-select the Map Mask before the repeat block is entered.
SELECT_EGA_SEQ_MAP_MASK
; NOTE: Here we are making an *unsafe* assumption that the value 1000b
; has been previously loaded into the SEQUENCER_DATA I/O port, thus
; limiting memory write operations to the intensity color plane only.
; The assumption seems to not cause issues in the game, but if it did
; they would appear as lighted areas having an incorrect color along
; their east edges.
; Redraw eight rows of tile pixels. Each iteration uses a new bit mask
; loaded into the Bit Mask Register [EGA, pg. 54] via the Graphics 1 & 2
; Address Register [EGA, pg. 46] which progressively lightens more and
; more of each row. Two different byte-sized registers are being written
; with a single word-sized OUT; the high byte goes to the GRAPHICS_DATA
; I/O port.
mov dx,GRAPHICS_1_2_ADDR
dstoff = 0
IRP mask,<10000000b,11000000b,11100000b,11110000b,11111000b,11111100b,11111110b,11111111b>
mov ax,(mask SHL 8) OR GFX_BIT_MASK
out dx,ax
; Read and then write back a byte of video memory to actually commit the
; changes that were previously set up. Each memory bit gets set to 1 if
; it satisfies the mask conditions, otherwise the written bit is ignored
; and the latched value is used in its place. The precise value written
; doesn't matter as long as there is a 1 bit set for every pixel that
; needs lightening. The mask left in AH conveniently has this property.
xchg ah,[es:bx+dstoff]
; Advance the destination write position, then the block repeats.
dstoff = dstoff + SCREEN_Y_STRIDE
ENDM
pop si
pop di
pop ds
pop bp
ret
ENDP
;
; Change the video page that is currently displayed on the screen.
;
; Although higher page numbers are accepted and will behave sensibly, pages
; beyond number 1 are used for tile storage and they will render as garbage if
; sent to the screen.
;
; page_num (word): Page number to show (0..n). Maximum page number varies based
; on the video mode and installed adapter memory, but common values for the
; EGA are 0, 1, 3, and 7. Only the low byte is used.
; Returns: Nothing
; Registers destroyed: AX
;
PROC _SelectActivePage FAR @@page_num:WORD
PUBLIC _SelectActivePage
push bp
mov bp,sp
; Change the active display page via BIOS video service interrupt [EGA,
; pg. 104].
mov ax,[@@page_num]
mov ah,VSVC_SET_ACTIVE_PAGE
int INT_VIDEO_SERVICE
pop bp
ret
ENDP
;
; Draw a single 8x8 pixel sprite tile to the draw page.
;
; This procedure draws tiles that are part of actors, decorations, the player,
; and other non-map sprites, with transparency. It is also responsible for
; drawing UI font characters.
;
; The tile data contains five color planes -- mask, blue, green, red, and
; intensity -- with 1-bit color depth on each plane. Tile image data is stored
; byte-planar in MBGRI order. Transparent areas have their mask bits set to 1.
;
; NOTE: Transparent areas in the tile *must* have the bits for all other color
; planes set to 0.
;
; The basic theory of operation is as follows:
; 1. Read the first four rows of mask bits from the source tile data. This
; starts at the 0th byte of the tile data and repeats at 5-byte intervals.
; Store these rows in four byte-width registers. (Ideally all eight rows
; would be read, but the 286 does not have enough registers to do this. Four
; rows are all that can be stored.)
; 2. For each of the four color planes:
; a: Set the EGA map mask register to only apply memory write operations to
; the current color plane being operated on.
; b: Set the EGA read map select register to only load data from the color
; plane being operated on during memory read operations.
; c: For each pixel row in the tile:
; A: Read one byte (i.e. eight pixels, or one tile row) from the EGA
; memory to capture what's been written onto the current color plane in
; the draw page thus far.
; B: AND this with the mask data for the row. The first four rows are in
; registers from step 1, the remaining rows must be read from the tile
; source. This turns off (zeros) the pixels that are covered by an
; opaque area of the tile.
; C: OR the result with the color data from the tile source. This data is
; located at 5-byte intervals, starting at an offset between 1 and 4
; depending on the current color plane being operated on.
; D: Write the result back to the original location in the EGA memory.
; This updates one row of pixels on the current color plane.
; E. Advance the read position by a 5-byte step, and advance the write
; position by a 40-byte step, in preparation for the next pixel row.
;
; src (far pointer): Memory address of the first byte of tile data to read.
; x (word): X-position on the screen, in tiles. (0..39, leftmost column is 0)
; y (word): Y-position on the screen, in tiles. (0..24, topmost row is 0)
; Returns: Nothing
; Registers destroyed: AX, BX, CX, DX, ES
;
PROC _DrawSpriteTile FAR @@src:FAR PTR, @@x:WORD, @@y:WORD
PUBLIC _DrawSpriteTile
push bp
mov bp,sp
push si
push di
push ds
; Set up source and destination pointers from the arguments:
; DS:SI <- Source tile data address (could be anywhere in memory)
; ES:DI <- Destination draw page address (in EGA memory)
mov di,[@@y]
shl di,1
mov di,[yOffsetTable+di]
add di,[@@x]
mov ax,[drawPageSegment]
lds si,[@@src]
ASSUME ds:NOTHING
mov es,ax
; Mask data uses eight bytes in each tile (one byte per pixel row), but
; there are only four byte-width registers available to hold this data.
; Load the first four rows of mask data into registers, starting from
; DS:SI and continuing in 5-byte steps, then accept the fact that the
; last four rows must be repeatedly re-read from slower memory.
mov bl,[si]
mov bh,[si+5]
mov cl,[si+10]
mov ch,[si+15]
; The actual data movement is largely the same across the four color
; planes, so use a repeating macro to handle it.
IRP plane_num,<0,1,2,3> ; 0,1,2,3 = blue,green,red,intensity
; Configure the Map Mask and enable only one of the four color planes.
; This restricts EGA memory writes to only the current color plane.
SET_EGA_MAP_MASK <1 SHL plane_num>
; Configure the Read Map Select register, which makes future EGA memory
; reads only fetch data from the current color plane.
SET_EGA_READ_MAP plane_num
srcoff = 0
dstoff = 0
; The first four rows of mask data are in byte registers, and the last
; four rows are still in memory at DS:SI+20 and 5-byte intervals beyond.
; The first row of color data is at DS:SI+plane_num+1, and subsequent
; rows are also spaced at 5-byte intervals.
;
; For each row of pixels in the tile, read the EGA memory contents for
; what's already been drawn on the preselected color plane. AND this
; with the mask data, so each pixel that is covered by an opaque area
; gets set to 0. OR the result with the pixel data for the tile, and
; write the result back into the original location in EGA memory.
IRP maskreg,<bl,bh,cl,ch,[si+20],[si+25],[si+30],[si+35]>
mov al,[es:di+dstoff]
and al,maskreg
or al,[si+(plane_num + 1 + srcoff)]
mov [es:di+dstoff],al
srcoff = srcoff + MASKED_TILE_ROW_STRIDE
dstoff = dstoff + SCREEN_Y_STRIDE
ENDM
ENDM
pop ds
ASSUME ds:DGROUP
pop di