-
Notifications
You must be signed in to change notification settings - Fork 173
/
id_rf_a.asm
743 lines (600 loc) · 14.5 KB
/
id_rf_a.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
; 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.
; ID_RF_A.ASM
IDEAL
MODEL MEDIUM,C
INCLUDE "ID_ASM.EQU"
CACHETILES = 1 ;enable master screen tile caching
;============================================================================
TILESWIDE = 21
TILESHIGH = 14
UPDATESIZE = (TILESWIDE+1)*TILESHIGH+1
DATASEG
EXTRN screenseg:WORD
EXTRN updateptr:WORD
EXTRN updatestart:WORD
EXTRN masterofs:WORD ;start of master tile port
EXTRN bufferofs:WORD ;start of current buffer port
EXTRN screenstart:WORD ;starts of three screens (0/1/master) in EGA mem
EXTRN grsegs:WORD
EXTRN mapsegs:WORD
EXTRN originmap:WORD
EXTRN updatemapofs:WORD
EXTRN tilecache:WORD
EXTRN tinf:WORD ;seg pointer to map header and tile info
EXTRN blockstarts:WORD ;offsets from bufferofs for each update block
planemask db ?
planenum db ?
CODESEG
screenstartcs dw ? ;in code segment for accesability
IFE GRMODE-CGAGR
;============================================================================
;
; CGA refresh routines
;
;============================================================================
TILEWIDTH = 4
;=================
;
; RFL_NewTile
;
; Draws a composit two plane tile to the master screen and sets the update
; spot to 1 in both update pages, forcing the tile to be copied to the
; view pages the next two refreshes
;
; Called to draw newlly scrolled on strips and animating tiles
;
;=================
PROC RFL_NewTile updateoffset:WORD
PUBLIC RFL_NewTile
USES SI,DI
;
; mark both update lists at this spot
;
mov di,[updateoffset]
mov bx,[updateptr] ;start of update matrix
mov [BYTE bx+di],1
mov dx,SCREENWIDTH-TILEWIDTH ;add to get to start of next line
;
; set di to the location in screenseg to draw the tile
;
shl di,1
mov si,[updatemapofs+di] ;offset in map from origin
add si,[originmap]
mov di,[blockstarts+di] ;screen location for tile
add di,[masterofs]
;
; set BX to the foreground tile number and SI to the background number
; If either BX or SI = 0xFFFF, the tile does not need to be masked together
; as one of the planes totally eclipses the other
;
mov es,[mapsegs+2] ;foreground plane
mov bx,[es:si]
mov es,[mapsegs] ;background plane
mov si,[es:si]
mov es,[screenseg]
or bx,bx
jz @@singletile
jmp @@maskeddraw ;draw both together
;=============
;
; Draw single background tile from main memory
;
;=============
@@singletile:
shl si,1
mov ds,[grsegs+STARTTILE16*2+si]
xor si,si ;block is segment aligned
REPT 15
movsw
movsw
add di,dx
ENDM
movsw
movsw
mov ax,ss
mov ds,ax ;restore turbo's data segment
ret
;=========
;
; Draw a masked tile combo
; Interupts are disabled and the stack segment is reassigned
;
;=========
@@maskeddraw:
cli ; don't allow ints when SS is set
shl bx,1
mov ss,[grsegs+STARTTILE16M*2+bx]
shl si,1
mov ds,[grsegs+STARTTILE16*2+si]
xor si,si ;first word of tile data
REPT 16
mov ax,[si] ;background tile
and ax,[ss:si] ;mask
or ax,[ss:si+64] ;masked data
stosw
mov ax,[si+2] ;background tile
and ax,[ss:si+2] ;mask
or ax,[ss:si+66] ;masked data
stosw
add si,4
add di,dx
ENDM
mov ax,@DATA
mov ss,ax
sti
mov ds,ax
ret
ENDP
ENDIF
IFE GRMODE-EGAGR
;===========================================================================
;
; EGA refresh routines
;
;===========================================================================
TILEWIDTH = 2
;=================
;
; RFL_NewTile
;
; Draws a composit two plane tile to the master screen and sets the update
; spot to 1 in both update pages, forcing the tile to be copied to the
; view pages the next two refreshes
;
; Called to draw newlly scrolled on strips and animating tiles
;
; Assumes write mode 0
;
;=================
PROC RFL_NewTile updateoffset:WORD
PUBLIC RFL_NewTile
USES SI,DI
;
; mark both update lists at this spot
;
mov di,[updateoffset]
mov bx,[updatestart] ;page 0 pointer
mov [BYTE bx+di],1
mov bx,[updatestart+2] ;page 1 pointer
mov [BYTE bx+di],1
;
; set screenstartcs to the location in screenseg to draw the tile
;
shl di,1
mov si,[updatemapofs+di] ;offset in map from origin
add si,[originmap]
mov di,[blockstarts+di] ;screen location for tile
add di,[masterofs]
mov [cs:screenstartcs],di
;
; set BX to the foreground tile number and SI to the background number
; If either BX or SI = 0xFFFF, the tile does not need to be masked together
; as one of the planes totally eclipses the other
;
mov es,[mapsegs+2] ;foreground plane
mov bx,[es:si]
mov es,[mapsegs] ;background plane
mov si,[es:si]
mov es,[screenseg]
mov dx,SC_INDEX ;for stepping through map mask planes
or bx,bx
jz @@singletile
jmp @@maskeddraw ;draw both together
;=========
;
; No foreground tile, so draw a single background tile.
; Use the master screen cache if possible
;
;=========
@@singletile:
mov bx,SCREENWIDTH-2 ;add to get to start of next line
shl si,1
IFE CACHETILES
jmp @@singlemain
ENDIF
mov ax,[tilecache+si]
or ax,ax
jz @@singlemain
;=============
;
; Draw single tile from cache
;
;=============
mov si,ax
mov ax,SC_MAPMASK + 15*256 ;all planes
WORDOUT
mov dx,GC_INDEX
mov ax,GC_MODE + 1*256 ;write mode 1
WORDOUT
mov di,[cs:screenstartcs]
mov ds,[screenseg]
REPT 15
movsb
movsb
add si,bx
add di,bx
ENDM
movsb
movsb
xor ah,ah ;write mode 0
WORDOUT
mov ax,ss
mov ds,ax ;restore turbo's data segment
ret
;=============
;
; Draw single tile from main memory
;
;=============
@@singlemain:
mov ax,[cs:screenstartcs]
mov [tilecache+si],ax ;next time it can be drawn from here with latch
mov ds,[grsegs+STARTTILE16*2+si]
xor si,si ;block is segment aligned
mov ax,SC_MAPMASK+0001b*256 ;map mask for plane 0
mov cx,4 ;draw four planes
@@planeloop:
mov dx,SC_INDEX
WORDOUT
mov di,[cs:screenstartcs] ;start at same place in all planes
REPT 15
movsw
add di,bx
ENDM
movsw
shl ah,1 ;shift plane mask over for next plane
loop @@planeloop
mov ax,ss
mov ds,ax ;restore turbo's data segment
ret
;=========
;
; Draw a masked tile combo
; Interupts are disabled and the stack segment is reassigned
;
;=========
@@maskeddraw:
cli ; don't allow ints when SS is set
shl bx,1
mov ss,[grsegs+STARTTILE16M*2+bx]
shl si,1
mov ds,[grsegs+STARTTILE16*2+si]
xor si,si ;first word of tile data
mov ax,SC_MAPMASK+0001b*256 ;map mask for plane 0
mov di,[cs:screenstartcs]
@@planeloopm:
WORDOUT
tileofs = 0
lineoffset = 0
REPT 16
mov bx,[si+tileofs] ;background tile
and bx,[ss:tileofs] ;mask
or bx,[ss:si+tileofs+32] ;masked data
mov [es:di+lineoffset],bx
tileofs = tileofs + 2
lineoffset = lineoffset + SCREENWIDTH
ENDM
add si,32
shl ah,1 ;shift plane mask over for next plane
cmp ah,10000b
je @@done ;drawn all four planes
jmp @@planeloopm
@@done:
mov ax,@DATA
mov ss,ax
sti
mov ds,ax
ret
ENDP
ENDIF
IFE GRMODE-VGAGR
;============================================================================
;
; VGA refresh routines
;
;============================================================================
ENDIF
;============================================================================
;
; reasonably common refresh routines
;
;============================================================================
;=================
;
; RFL_UpdateTiles
;
; Scans through the update matrix pointed to by updateptr, looking for 1s.
; A 1 represents a tile that needs to be copied from the master screen to the
; current screen (a new row or an animated tiled). If more than one adjacent
; tile in a horizontal row needs to be copied, they will be copied as a group.
;
; Assumes write mode 1
;
;=================
; 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 UPDATETERMINATE
;
; DS
; ES
; SS
PROC RFL_UpdateTiles
PUBLIC RFL_UpdateTiles
USES SI,DI,BP
jmp SHORT @@realstart
@@done:
;
; all tiles have been scanned
;
ret
@@realstart:
mov di,[updateptr]
mov bp,(TILESWIDE+1)*TILESHIGH+1
add bp,di ; when di = bx, 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
je @@done
cmp [BYTE di],al
jne @@singletile
jmp @@tileblock
;============
;
; copy a single tile
;
;============
EVEN
@@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 di,[bufferofs] ; dest in current screen
add si,[masterofs] ; source in master screen
mov dx,SCREENWIDTH-TILEWIDTH
mov ax,[screenseg]
mov ds,ax
mov es,ax
;--------------------------
IFE GRMODE-CGAGR
REPT 15
movsw
movsw
add si,dx
add di,dx
ENDM
movsw
movsw
ENDIF
;--------------------------
IFE GRMODE-EGAGR
REPT 15
movsb
movsb
add si,dx
add di,dx
ENDM
movsb
movsb
ENDIF
;--------------------------
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 di,[bufferofs] ; dest in current screen
add si,[masterofs] ; source in master screen
mov dx,SCREENWIDTH
sub dx,bx ; offset to next line on screen
IFE GRMODE-CGAGR
sub dx,bx ; bx is words wide in CGA tiles
ENDIF
mov ax,[screenseg]
mov ds,ax
mov es,ax
REPT 15
mov cx,bx
IFE GRMODE-CGAGR
rep movsw
ENDIF
IFE GRMODE-EGAGR
rep movsb
ENDIF
add si,dx
add di,dx
ENDM
mov cx,bx
IFE GRMODE-CGAGR
rep movsw
ENDIF
IFE GRMODE-EGAGR
rep movsb
ENDIF
dec cx ; was 0 from last rep movsb, now $ffff for scasb
jmp @@findtile
ENDP
;============================================================================
;=================
;
; RFL_MaskForegroundTiles
;
; Scan through update looking for 3's. If the foreground tile there is a
; masked foreground tile, draw it to the screen
;
;=================
PROC RFL_MaskForegroundTiles
PUBLIC RFL_MaskForegroundTiles
USES SI,DI,BP
jmp SHORT @@realstart
@@done:
;
; all tiles have been scanned
;
ret
@@realstart:
mov di,[updateptr]
mov bp,(TILESWIDE+1)*TILESHIGH+2
add bp,di ; when di = bx, all tiles have been scanned
push di
mov cx,-1 ; definately scan the entire thing
;
; scan for a 3 in the update list
;
@@findtile:
mov ax,ss
mov es,ax ; scan in the data segment
mov al,3
pop di ; place to continue scaning from
repne scasb
cmp di,bp
je @@done
;============
;
; found a tile, see if it needs to be masked on
;
;============
push di
sub di,[updateptr]
shl di,1
mov si,[updatemapofs-2+di] ; offset from originmap
add si,[originmap]
mov es,[mapsegs+2] ; foreground map plane segment
mov si,[es:si] ; foreground tile number
or si,si
jz @@findtile ; 0 = no foreground tile
mov bx,si
add bx,INTILE ;INTILE tile info table
mov es,[tinf]
test [BYTE PTR es:bx],80h ;high bit = masked tile
jz @@findtile
;-------------------
IFE GRMODE-CGAGR
;=================
;
; mask the tile CGA
;
;=================
mov di,[blockstarts-2+di]
add di,[bufferofs]
mov es,[screenseg]
shl si,1
mov ds,[grsegs+STARTTILE16M*2+si]
mov bx,64 ;data starts 64 bytes after mask
xor si,si
lineoffset = 0
REPT 16
mov ax,[es:di+lineoffset] ;background
and ax,[si] ;mask
or ax,[si+bx] ;masked data
mov [es:di+lineoffset],ax ;background
inc si
inc si
mov ax,[es:di+lineoffset+2] ;background
and ax,[si] ;mask
or ax,[si+bx] ;masked data
mov [es:di+lineoffset+2],ax ;background
inc si
inc si
lineoffset = lineoffset + SCREENWIDTH
ENDM
ENDIF
;-------------------
IFE GRMODE-EGAGR
;=================
;
; mask the tile
;
;=================
mov [BYTE planemask],1
mov [BYTE planenum],0
mov di,[blockstarts-2+di]
add di,[bufferofs]
mov [cs:screenstartcs],di
mov es,[screenseg]
shl si,1
mov ds,[grsegs+STARTTILE16M*2+si]
mov bx,32 ;data starts 32 bytes after mask
@@planeloopm:
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
xor si,si
mov di,[cs:screenstartcs]
lineoffset = 0
REPT 16
mov cx,[es:di+lineoffset] ;background
and cx,[si] ;mask
or cx,[si+bx] ;masked data
inc si
inc si
mov [es:di+lineoffset],cx
lineoffset = lineoffset + SCREENWIDTH
ENDM
add bx,32 ;the mask is now further away
inc [ss:planenum]
shl [ss:planemask],1 ;shift plane mask over for next plane
cmp [ss:planemask],10000b ;done all four planes?
je @@drawn ;drawn all four planes
jmp @@planeloopm
@@drawn:
ENDIF
;-------------------
mov ax,ss
mov ds,ax
mov cx,-1 ;definately scan the entire thing
jmp @@findtile
ENDP
END