-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathD3_DR_A2.ASM
779 lines (669 loc) · 15.8 KB
/
D3_DR_A2.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
IDEAL
MODEL MEDIUM,C
P286
SCREENSEG = 0a000h
FINEANGLES = 3600
DEG90 = 900
DEG180 = 1800
DEG270 = 2700
DEG360 = 3600
OP_JLE = 07eh
OP_JGE = 07dh
EXTRN finetangent:DWORD ; far array, starts at offset 0
EXTRN HitHorizWall:FAR
EXTRN HitVertWall:FAR
EXTRN HitHorizDoor:FAR
EXTRN HitVertDoor:FAR
EXTRN HitHorizPWall:FAR
EXTRN HitVertPWall:FAR
DATASEG
EXTRN viewwidth:WORD
EXTRN tilemap:BYTE
EXTRN spotvis:BYTE
EXTRN pixelangle:WORD
EXTRN midangle:WORD
EXTRN angle:WORD
EXTRN focaltx:WORD
EXTRN focalty:WORD
EXTRN viewtx:WORD
EXTRN viewty:WORD
EXTRN viewx:DWORD
EXTRN viewy:DWORD
EXTRN xpartialup:WORD
EXTRN ypartialup:WORD
EXTRN xpartialdown:WORD
EXTRN ypartialdown:WORD
EXTRN tilehit:WORD
EXTRN pixx:WORD
EXTRN wallheight:WORD ; array of VIEWWIDTH entries
EXTRN xtile:WORD
EXTRN ytile:WORD
EXTRN xtilestep:WORD
EXTRN ytilestep:WORD
EXTRN xintercept:DWORD
EXTRN yintercept:DWORD
EXTRN xstep:DWORD
EXTRN ystep:DWORD
EXTRN doorposition:WORD ; table of door position values
EXTRN pwallpos:WORD ; amound a pushable wall has been moved
CODESEG
;-------------------
;
; xpartialbyystep
;
; multiplies long [ystep] (possibly negative), by word [xpartial] (in BX)
;
; returns dx:ax
; trashes bx,cx,di
;
;-------------------
PROC xpartialbyystep NEAR
;
; setup
;
mov ax,[WORD ystep]
mov cx,[WORD ystep+2]
or cx,cx ; is ystep negatice?
jns @@multpos
;
; multiply negative cx:ax by bx
;
neg cx
neg ax
sbb cx,0
mul bx ; fraction*fraction
mov di,dx ; di is low word of result
mov ax,cx ;
mul bx ; units*fraction
add ax,di
adc dx,0
neg dx
neg ax
sbb dx,0
ret
;
; multiply positive cx:ax by bx
;
EVEN
@@multpos:
mul bx ; fraction*fraction
mov di,dx ; di is low word of result
mov ax,cx ;
mul bx ; units*fraction
add ax,di
adc dx,0
ret
ENDP
;-------------------
;
; ypartialbyxstep
;
; multiplies long [xstep] (possibly negative), by word [ypartial] (in BP)
;
; returns dx:ax
; trashes cx,di,bp
;
;-------------------
PROC ypartialbyxstep NEAR
;
; setup
;
mov ax,[WORD xstep]
mov cx,[WORD xstep+2]
or cx,cx ; is ystep negatice?
jns @@multpos
;
; multiply negative cx:ax by bx
;
neg cx
neg ax
sbb cx,0
mul bp ; fraction*fraction
mov di,dx ; di is low word of result
mov ax,cx ;
mul bp ; units*fraction
add ax,di
adc dx,0
neg dx
neg ax
sbb dx,0
ret
;
; multiply positive cx:ax by bx
;
EVEN
@@multpos:
mul bp ; fraction*fraction
mov di,dx ; di is low word of result
mov ax,cx ;
mul bp ; units*fraction
add ax,di
adc dx,0
ret
ENDP
;============================
;
; AsmRefresh
;
;
;============================
PROC AsmRefresh
PUBLIC AsmRefresh
push si
push di
push bp
mov [pixx],0
;---------------------------------------------------------------------------
;
; Setup to trace a ray through pixx view pixel
;
; CX : angle of the ray through pixx
; ES : points to segment of finetangent array for this block of code
;
; Upon entrance to initialize block
;
; BX : xpartial
; BP : ypartial
;
;---------------------------------------------------------------------------
EVEN
pixxloop:
mov ax,SEG finetangent
mov es,ax
mov cx,[midangle] ; center of view area
mov bx,[pixx]
shl bx,1
add cx,[pixelangle+bx] ; delta for this pixel
cmp cx,0
jge not0
;----------
;
; -90 - -1 degree arc
;
;----------
add cx,FINEANGLES ; -90 is the same as 270
jmp entry360
not0:
cmp cx,DEG90
jge not90
;----------
;
; 0-89 degree arc
;
;----------
entry90:
mov [xtilestep],1 ; xtilestep = 1
mov [ytilestep],-1 ; ytilestep = -1
mov [BYTE cs:horizop],OP_JGE ; patch a jge in
mov [BYTE cs:vertop],OP_JLE ; patch a jle in
mov bx,DEG90-1
sub bx,cx
shl bx,2
mov ax,[es:bx]
mov dx,[es:bx+2]
mov [WORD xstep],ax
mov [WORD xstep+2],dx ; xstep = finetangent[DEG90-1-angle]
mov bx,cx
shl bx,2
mov ax,[es:bx]
mov dx,[es:bx+2]
neg dx
neg ax
sbb dx,0
mov [WORD ystep],ax
mov [WORD ystep+2],dx ; ystep = -finetangent[angle]
mov bx,[xpartialup] ; xpartial = xpartialup
mov bp,[ypartialdown] ; ypartial = ypartialdown
jmp initvars
not90:
cmp cx,DEG180
jge not180
;----------
;
; 90-179 degree arc
;
;----------
mov ax,-1
mov [xtilestep],ax ; xtilestep = -1
mov [ytilestep],ax ; ytilestep = -1
mov [BYTE cs:horizop],OP_JLE ; patch a jle in
mov [BYTE cs:vertop],OP_JLE ; patch a jle in
mov bx,cx
shl bx,2
mov ax,[es:bx-DEG90*4]
mov dx,[es:bx+2-DEG90*4]
neg dx
neg ax
sbb dx,0
mov [WORD xstep],ax
mov [WORD xstep+2],dx ; xstep = -finetangent[angle-DEG90]
mov bx,DEG180-1
sub bx,cx
shl bx,2
mov ax,[es:bx]
mov dx,[es:bx+2]
neg dx
neg ax
sbb dx,0
mov [WORD ystep],ax
mov [WORD ystep+2],dx ; ystep = -finetangent[DEG180-1-angle]
mov bx,[xpartialdown] ; xpartial = xpartialdown
mov bp,[ypartialdown] ; ypartial = ypartialdown
jmp initvars
not180:
cmp cx,DEG270
jge not270
;----------
;
; 180-269 degree arc
;
;----------
mov [xtilestep],-1 ; xtilestep = -1
mov [ytilestep],1 ; ytilestep = 1
mov [BYTE cs:horizop],OP_JLE ; patch a jle in
mov [BYTE cs:vertop],OP_JGE ; patch a jge in
mov bx,DEG270-1
sub bx,cx
shl bx,2
mov ax,[es:bx]
mov dx,[es:bx+2]
neg dx
neg ax
sbb dx,0
mov [WORD xstep],ax
mov [WORD xstep+2],dx ; xstep = -finetangent[DEG270-1-angle]
mov bx,cx
shl bx,2
mov ax,[es:bx-DEG180*4]
mov dx,[es:bx+2-DEG180*4]
mov [WORD ystep],ax
mov [WORD ystep+2],dx ; ystep = finetangent[angle-DEG180]
mov bx,[xpartialdown] ; xpartial = xpartialdown
mov bp,[ypartialup] ; ypartial = ypartialup
jmp initvars
not270:
cmp cx,DEG360
jge not360
;----------
;
; 270-359 degree arc
;
;----------
entry360:
mov ax,1
mov [xtilestep],ax ; xtilestep = 1
mov [ytilestep],ax ; ytilestep = 1
mov [BYTE cs:horizop],OP_JGE ; patch a jge in
mov [BYTE cs:vertop],OP_JGE ; patch a jge in
mov bx,cx
shl bx,2
mov ax,[es:bx-DEG270*4]
mov dx,[es:bx+2-DEG270*4]
mov [WORD xstep],ax
mov [WORD xstep+2],dx ; xstep = finetangent[angle-DEG270]
mov bx,DEG360-1
sub bx,cx
shl bx,2
mov ax,[es:bx]
mov dx,[es:bx+2]
mov [WORD ystep],ax
mov [WORD ystep+2],dx ; ystep = finetangent[DEG360-1-angle]
mov bx,[xpartialup] ; xpartial = xpartialup
mov bp,[ypartialup] ; ypartial = ypartialup
jmp initvars
not360:
;----------
;
; 360-449 degree arc
;
;----------
sub cx,FINEANGLES ; -449 is the same as 89
jmp entry90
;---------------------------------------------------------------------------
;
; initialise variables for intersection testing
;
;---------------------------------------------------------------------------
initvars:
call NEAR xpartialbyystep ; xpartial is in BX
add ax,[WORD viewy]
adc dx,[WORD viewy+2]
mov [WORD yintercept],ax
mov [WORD yintercept+2],dx
mov si,[focaltx]
add si,[xtilestep]
mov [xtile],si ; xtile = focaltx+xtilestep
shl si,6
add si,dx ; xspot = (xtile<<6) + yinttile
call NEAR ypartialbyxstep ; ypartial is in BP
add ax,[WORD viewx]
adc dx,[WORD viewx+2]
mov [WORD xintercept],ax
mov cx,dx
mov bx,[focalty]
add bx,[ytilestep]
mov bp,bx ; ytile = focalty+ytilestep
mov di,dx
shl di,6
add di,bx ; yspot = (xinttile<<6) + ytile
mov bx,[xtile]
mov dx,[WORD yintercept+2]
mov ax,SCREENSEG
mov es,ax ; faster than mov es,[screenseg]
;---------------------------------------------------------------------------
;
; trace along this angle until we hit a wall
;
; CORE LOOP!
;
; All variables are killed when a wall is hit
;
; AX : scratch
; BX : xtile
; CX : high word of xintercept
; DX : high word of yintercept
; SI : xspot (yinttile<<6)+xtile (index into tilemap and spotvis)
; DI : yspot (xinttile<<6)+ytile (index into tilemap and spotvis)
; BP : ytile
; ES : screenseg
;
;---------------------------------------------------------------------------
;-----------
;
; check intersections with vertical walls
;
;-----------
EVEN
vertcheck:
cmp dx,bp
vertop: ; 0x7e = jle (ytilestep==-1)
jle horizentry ; 0x7d = jge (ytilestep==1)
vertentry:
test [BYTE tilemap+si],0ffh ; tilehit = *((byte *)tilemap+xspot);
jnz hitvert
passvert:
mov [BYTE spotvis+si],1 ; *((byte *)spotvis+xspot) = true;
add bx,[xtilestep] ; xtile+=xtilestep
mov ax,[WORD ystep]
add [WORD yintercept],ax ; yintercept += ystep
adc dx,[WORD ystep+2]
mov si,bx
shl si,6
add si,dx ; xspot = (xtile<<6)+yinttile
jmp vertcheck
EVEN
hitvert:
mov al,[BYTE tilemap+si] ; tilehit = *((byte *)tilemap+xspot);
mov [BYTE tilehit],al
or al,al ; set flags
jns notvertdoor
jmp vertdoor
notvertdoor:
mov [WORD xintercept],0
mov [WORD xintercept+2],bx
mov [xtile],bx
mov [WORD yintercept+2],dx
mov [ytile],dx
call FAR HitVertWall
jmp nextpix
;-----------
;
; check intersections with horizontal walls
;
;-----------
EVEN
horizcheck:
cmp cx,bx
horizop: ; 0x7e = jle (xtilestep==-1)
jle vertentry ; 0x7d = jge (xtilestep==1)
horizentry:
test [BYTE tilemap+di],0ffh ; tilehit = *((byte *)tilemap+yspot);
jnz hithoriz
passhoriz:
mov [BYTE spotvis+di],1 ; *((byte *)spotvis+yspot) = true;
add bp,[ytilestep] ; ytile+=ytilestep
mov ax,[WORD xstep]
add [WORD xintercept],ax ; xintercept += xstep
adc cx,[WORD xstep+2]
mov di,cx
shl di,6
add di,bp ; yspot = (xinttile<<6)+ytile
jmp horizcheck
EVEN
hithoriz:
mov al,[BYTE tilemap+di] ; tilehit = *((byte *)tilemap+yspot);
mov [BYTE tilehit],al
or al,al ; set flags
js horizdoor
mov [WORD xintercept+2],cx
mov [xtile],cx
mov [WORD yintercept],0
mov [WORD yintercept+2],bp
mov [ytile],bp
call FAR HitHorizWall
jmp nextpix
;---------------------------------------------------------------------------
;
; next pixel over
;
;---------------------------------------------------------------------------
nextpix:
mov ax,[pixx]
inc ax
mov [pixx],ax
cmp ax,[viewwidth]
jge done
jmp pixxloop
done:
pop bp
pop di
pop si
retf
;===========================================================================
;=============
;
; hit a special horizontal wall, so find which coordinate a door would be
; intersected at, and check to see if the door is open past that point
;
;=============
horizdoor:
mov [xtile],bx ; save off live register variables
mov [WORD yintercept+2],dx
test al,040h ; both high bits set == pushable wall
jnz horizpushwall
mov bx,ax
and bx,7fh ; strip high bit
shl bx,1 ; index into word width door table
mov ax,[WORD xstep]
mov dx,[WORD xstep+2]
sar dx,1
rcr ax,1 ; half a step gets to door position
add ax,[WORD xintercept] ; add half step to current intercept pos
adc dx,cx ; CX hold high word of xintercept
cmp cx,dx ; is it still in the same tile?
je hithmid
;
; midpoint is outside tile, so it hit the side of the wall before a door
;
continuehoriz:
mov bx,[xtile] ; reload register variables
mov dx,[WORD yintercept+2]
jmp passhoriz ; continue tracing
;
; the trace hit the door plane at pixel position AX, see if the door is
; closed that much
;
; draw the door
;
hithmid:
cmp ax,8000h
jb horiztrekfirst
; ax is in second half of door
mov dx,[doorposition+bx] ; position of leading edge of door
shr dx,1
add dx,8000h
cmp ax,dx
jb continuehoriz
jmp horiztrekdraw
horiztrekfirst:
; ax is in first half of door
mov dx,[doorposition+bx] ; position of leading edge of door
shr dx,1
mov bx,07fffh
sub bx,dx
cmp ax,bx
jg continuehoriz
;
; draw the trek door
;
horiztrekdraw:
mov [WORD xintercept],ax ; save pixel intercept position
mov [WORD xintercept+2],cx
mov [WORD yintercept],8000h ; intercept in middle of tile
mov [WORD yintercept+2],bp
call FAR HitHorizDoor
jmp nextpix
;============
;
; hit a sliding horizontal wall
;
;============
horizpushwall:
mov ax,[WORD xstep+2] ; multiply xstep by pwallmove (0-63)
mul [pwallpos]
mov bx,ax
mov ax,[WORD xstep]
mul [pwallpos]
add dx,bx
sar dx,1 ; then divide by 64 to accomplish a
rcr ax,1 ; fixed point multiplication
sar dx,1
rcr ax,1
sar dx,1
rcr ax,1
sar dx,1
rcr ax,1
sar dx,1
rcr ax,1
sar dx,1
rcr ax,1
add ax,[WORD xintercept] ; add partial step to current intercept
adc dx,cx ; CX hold high word of xintercept
cmp cx,dx ; is it still in the same tile?
jne continuehoriz ; no, it hit the side
;
; draw the pushable wall at the new height
;
mov [WORD xintercept],ax ; save pixel intercept position
mov [WORD xintercept+2],dx
mov [WORD yintercept+2],bp
mov [WORD yintercept],0
call FAR HitHorizPWall
jmp nextpix
;===========================================================================
;=============
;
; hit a special vertical wall, so find which coordinate a door would be
; intersected at, and check to see if the door is open past that point
;
;=============
vertdoor:
mov [xtile],bx ; save off live register variables
mov [WORD yintercept+2],dx
test al,040h ; both high bits set == pushable wall
jnz vertpushwall
mov bx,ax
and bx,7fh ; strip high bit
shl bx,1 ; index into word width doorposition
mov ax,[WORD ystep]
mov dx,[WORD ystep+2]
sar dx,1
rcr ax,1 ; half a step gets to door position
add ax,[WORD yintercept] ; add half step to current intercept pos
adc dx,[WORD yintercept+2]
cmp [WORD yintercept+2],dx ; is it still in the same tile?
je hitvmid
;
; midpoint is outside tile, so it hit the side of the wall before a door
;
continuevert:
mov bx,[xtile] ; reload register variables
mov dx,[WORD yintercept+2]
jmp passvert ; continue tracing
;
; the trace hit the door plane at pixel position AX, see if the door is
; closed that much
;
;
; draw the door
;
hitvmid:
cmp ax,08000h
jb verttrekfirst
; ax is in second half of door
mov dx,[doorposition+bx] ; position of leading edge of door
shr dx,1
add dx,08000h
cmp ax,dx
jb continuevert
jmp verttrekdraw
verttrekfirst:
; ax is in first half of door
mov dx,[doorposition+bx] ; position of leading edge of door
shr dx,1
mov bx,07fffh
sub bx,dx
cmp ax,bx
jg continuevert
;
; draw the trek door
;
verttrekdraw:
mov [WORD yintercept],ax ; save pixel intercept position
mov [WORD xintercept],8000h ; intercept in middle of tile
mov ax,[xtile]
mov [WORD xintercept+2],ax
call FAR HitVertDoor
jmp nextpix
;============
;
; hit a sliding vertical wall
;
;============
vertpushwall:
mov ax,[WORD ystep+2] ; multiply ystep by pwallmove (0-63)
mul [pwallpos]
mov bx,ax
mov ax,[WORD ystep]
mul [pwallpos]
add dx,bx
sar dx,1 ; then divide by 64 to accomplish a
rcr ax,1 ; fixed point multiplication
sar dx,1
rcr ax,1
sar dx,1
rcr ax,1
sar dx,1
rcr ax,1
sar dx,1
rcr ax,1
sar dx,1
rcr ax,1
add ax,[WORD yintercept] ; add partial step to current intercept
adc dx,[WORD yintercept+2]
cmp [WORD yintercept+2],dx ; is it still in the same tile?
je drawvertpushw
jmp continuevert ; no, it hit the side
;
; draw the pushable wall at the new height
;
drawvertpushw:
mov [WORD yintercept],ax ; save pixel intercept position
mov [WORD yintercept+2],dx
mov bx,[xtile]
mov [WORD xintercept+2],bx
mov [WORD xintercept],0
call FAR HitVertPWall
jmp nextpix
ENDP
END