-
Notifications
You must be signed in to change notification settings - Fork 15
/
screen.s
676 lines (656 loc) · 22.7 KB
/
screen.s
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
PANEL_BG1 = $0c
PANEL_BG2 = $0b
GAMESCR_D018 = $ea
TEXTSCR_D018 = $e8
PANEL_D018 = $88
MIN_SPRX = 9
MAX_SPRX = 337
MAX_SPRX_EXPANDED = $1e8
MIN_SPRY = 34
MAX_SPRY = IRQ4_LINE+1
SPR_TOP_SAFETY = 4
SPR_BOTTOM_SAFETY = 21
; Wait for bottom of screen
;
; Parameters: -
; Returns: -
; Modifies: A
WaitBottom: lda $d011 ;Wait until bottom of screen
bmi WaitBottom
WB_Loop2: lda $d011
bpl WB_Loop2
rts
; Redraw game map
;
; Parameters: -
; Returns: A=0
; Modifies: A
RedrawScreen: jsr BlankScreen
lda #$00
ldx #blockY-scrollX
RS_ClearScrollVars:
sta scrollX,x
dex
bpl RS_ClearScrollVars
jsr RS_ToggleOptimize
lda zoneBg1 ;Set zone colors
sta Irq1_Bg1+1
lda zoneBg2
sta Irq1_Bg2+1
lda zoneBg3
sta Irq1_Bg3+1
lda #GAMESCR_D018
sta Irq1_D018+1
jsr PrepareDrawScreen
jsr DrawScreen
RS_ToggleOptimize:
ldx #$00 ;During redraw, draw colors to all blocks
RS_ToggleOptimizeLoop:
lda blkColors,x ;Then re-enable optimizing mode for scrolling
asl ;Check bit 6
bpl RS_ToggleOptimizeSkip
ror
eor #$80
sta blkColors,x
RS_ToggleOptimizeSkip:
inx
bne RS_ToggleOptimizeLoop
rts
; Set sprite Y-range for fastload. Called before the load operation.
; Note: must not be called with IRQs off
;
; Parameters: -
; Returns: -
; Modifies: A,X,Y
SetSpriteRange: lda newFrameFlag
bne SetSpriteRange
lda Irq1_D015+1
beq SSR_NoSprites
ldx Irq1_FirstSortSpr+1
lda sortSprY,x
sec
sbc #SPR_TOP_SAFETY
sta FL_MinSprY+1
SSR_LastSortSpr:ldx #$00
lda sortSprY-1,x
adc #SPR_BOTTOM_SAFETY ;C=1, add one more
SSR_NoSprites: sta FL_MaxSprY+1
rts
; Blank screen & turn off sprites
;
; Parameters: -
; Returns: A=0
; Modifies: A
BlankScreen: jsr WaitBottom
lda #$57 ;Blank screen & remove sprites
sta Irq1_ScrollX+1
sta Irq1_ScrollY+1
lda #$00
sta Irq1_D015+1
sta drawScreenFlag ;Cancel drawscreen in case it's still pending
sta Irq5_CharAnimFlag+1 ;Disable charset animation if we're going to load
rts
; Perform scrolling logic
;
; Parameters: -
; Returns: X,Y subtract lowbyte for X & Y actor render
; Modifies: A,X,Y
ScrollLogic: ldy scrCounter
bne SL_SecondFrame
SL_FirstFrame: lda scrollSX ;Check if already at right edge
bmi SL_NoLimitXPre
lda mapX
SL_MapXLimit: cmp #$00
bcs SL_LimitXPre
SL_NoLimitXPre: lda scrollX
clc
adc scrollSX
bpl SL_LimitXOK1
SL_LimitXPre: lda #$00
SL_LimitXOK1: cmp #$40
bcc SL_LimitXOK2
lda #$3f
SL_LimitXOK2: sta scrollX
lda scrollSY
bmi SL_NoLimitYPre ;Check if already at bottom edge
lda mapY
SL_MapYLimit: cmp #$00
bcs SL_LimitYPre
SL_NoLimitYPre: lda scrollY ;Add scrolling speed, limit inside char for this frame
clc
adc scrollSY
bpl SL_LimitYOK1
SL_LimitYPre: lda #$00
SL_LimitYOK1: cmp #$40
bcc SL_LimitYOK2
lda #$3f
SL_LimitYOK2: sta scrollY
SL_CalcSprSub: lda blockX
lsr ;Bit 1 to carry
ror ;$80 or $00
ror ;$40 or $00
ora scrollX
and #$70
sbc #$78-1 ;C=0
and #$78
tax
lda mapX
sbc #$02
sta DA_SprSubXH+1
lda blockY
lsr
ror
ror
ora scrollY
sbc #$30-1 ;C=0
and #$78
tay
lda mapY
sbc #$03
sta DA_SprSubYH+1
rts
SL_SecondFrame:
SL_PrecalcX: lda #$00
sta scrollX
SL_PrecalcY: lda #$00
sta scrollY
bpl SL_CalcSprSub
; Update frame. Followed by scorepanel update.
;
; Parameters: -
; Returns: -
; Modifies: A,X,Y
UpdateFrame: lda scrollX
lsr
lsr
lsr
eor #$07
ora #$10
pha
and #$01
sta UF_SprXLSB1+1
sta UF_SprXLSB2+1
lda scrollY
lsr
lsr
lsr
eor #$07
ora #$10
pha
ldx #$00
stx zpDestLo ;D010 bits for first IRQ
stx zpDestHi ;X-expand bits for first IRQ
ldy numSpr
bne UF_SprCountOK
iny ;Cannot have zero sprites, as the loop would overflow / crash
UF_SprCountOK: tya
UF_LastNumSpr: cpy #$ff ;If sprite amount changes, sort all, otherwise just the current amount of sprites
beq UF_SameSprAmount
ldy #MAX_SPR
UF_SameSprAmount:
sty UF_SortEndCmp+1
sta UF_LastNumSpr+1
txa
UF_SortLoop: ldy sprOrder,x ;Check for coordinates being in order
cmp sprY,y
beq UF_SortNoSwap2
bcc UF_SortNoSwap1
stx zpSrcLo ;If not in order, begin insertion loop
sty zpSrcHi
lda sprY,y
ldy sprOrder-1,x
sty sprOrder,x
dex
beq UF_SortSwapDone1
UF_SortSwap1: ldy sprOrder-1,x
sty sprOrder,x
cmp sprY,y
bcs UF_SortSwapDone1
dex
bne UF_SortSwap1
UF_SortSwapDone1:
ldy zpSrcHi
sty sprOrder,x
ldx zpSrcLo
ldy sprOrder,x
UF_SortNoSwap1: lda sprY,y
UF_SortNoSwap2: inx
UF_SortEndCmp: cpx #MAX_SPR
bne UF_SortLoop
UF_SortDone: lda #$01 ;Make sure IRQs are on to prevent getting stuck in the wait below
sta $d01a
if SHOW_FREE_TIME > 0
dec $d020
endif
UF_WaitSprites: lda newFrameFlag ;Wait for previous sprite update & screen redraw to complete
ora drawScreenFlag
bmi UF_WaitSprites
if SHOW_FREE_TIME > 0
inc $d020
endif
lda firstSortSpr ;Switch sprite doublebuffer side
eor #MAX_SPR
sta firstSortSpr
tax
lda #<sprOrder
sec
sbc firstSortSpr
sta UF_CopyLoop1+1
txa
adc #8-1 ;C=1
sta UF_CopyLoop1EndCmp+1 ;Set endpoint for first copyloop
lda #$80
sta zpBitBuf ;OR-bit for $d010 and x-expand
bmi UF_CopyLoop1
UF_CopyLoop1Skip:
inc UF_CopyLoop1+1
UF_CopyLoop1: ldy sprOrder,x
lda sprY,y ;If reach the maximum Y-coord endmark, all done
cmp #MAX_SPRY
bcs UF_CopyLoop1Done
sta sortSprY,x
lda sprC,y
bmi UF_CopyLoop1Skip ;Flickering sprite?
sta sortSprC,x
and #COLOR_XEXPAND
beq UF_CopyLoop1NoXExpand
lda zpDestHi
ora zpBitBuf
sta zpDestHi
lda sprX,y
cmp #MAX_SPRX_EXPANDED/2 ;Do X-coord edge adjust for expanded sprite
bcc UF_CopyLoop1EdgeAdjustDone
sbc #$04
bcs UF_CopyLoop1EdgeAdjustDone
UF_CopyLoop1NoXExpand:
lda sprX,y
UF_CopyLoop1EdgeAdjustDone:
asl
UF_SprXLSB1: ora #$00
sta sortSprX,x
bcc UF_CopyLoop1MsbLow
lda zpDestLo
ora zpBitBuf
sta zpDestLo
UF_CopyLoop1MsbLow:
lda sprF,y
sta sortSprF,x
lsr zpBitBuf
inx
UF_CopyLoop1EndCmp:
cpx #$00
bcc UF_CopyLoop1
UF_CopyLoop1Done:
lda zpDestLo
sta sortSprXMSB-1,x
lda zpDestHi
sta sortSprXExpand-1,x
lda #$7f ;Sprite AND-bit (needing OR is rarer)
sta zpBitBuf
lda UF_CopyLoop1+1 ;Copy sortindex from first copyloop
sta UF_CopyLoop2+1 ;to second
sta UF_CopyLoop2B+1
cpx firstSortSpr ;Any sprites at all?
beq UF_NoSprites ;Make first (and final) IRQ endmask
jmp UF_EndMark
UF_NoSprites: jmp UF_AllDone ;C=1 if jumping to AllDone
UF_CopyLoop2SkipB:
inc UF_CopyLoop2+1
inc UF_CopyLoop2B+1
bne UF_CopyLoop2B
UF_CopyLoop2Next:
lda sprC,y ;TODO: check if can be made to combine IRQs more eagerly
bmi UF_CopyLoop2SkipB ;Flickering sprite?
UF_CopyLoop2Begin:
sta sortSprC,x
and #COLOR_XEXPAND
beq UF_CopyLoop2NoXExpand
lda zpBitBuf
eor #$ff
ora sortSprXExpand-1,x
sta sortSprXExpand,x
lda sprX,y
cmp #MAX_SPRX_EXPANDED/2 ;Do X-coord edge adjust for expanded sprite
bcc UF_CopyLoop2EdgeAdjustDone
sbc #$04
bcs UF_CopyLoop2EdgeAdjustDone
UF_CopyLoop2NoXExpand:
lda sortSprXExpand-1,x
and zpBitBuf
sta sortSprXExpand,x
lda sprX,y
UF_CopyLoop2EdgeAdjustDone:
asl
UF_SprXLSB2: ora #$00
sta sortSprX,x
bcc UF_CopyLoop2MsbLow
lda zpBitBuf
eor #$ff
ora sortSprXMSB-1,x
bne UF_CopyLoop2MsbDone
UF_CopyLoop2MsbLow:
lda sortSprXMSB-1,x
and zpBitBuf
UF_CopyLoop2MsbDone:
sta sortSprXMSB,x
lda sprF,y
sta sortSprF,x
inx
sec
ror zpBitBuf
bcs UF_CopyLoop2B
ror zpBitBuf ;Start the AND bit over from $7f
UF_CopyLoop2B: ldy sprOrder,x
lda sprY,y
cmp #MAX_SPRY
bcs UF_CopyLoop2EndIrq ;Reached end - no further IRQs
sta sortSprY,x
sbc #21-1
cmp sortSprY-8,x ;Check for physical sprite overlap
bcc UF_CopyLoop2SkipB
UF_CopyLoop2YCmp:
cmp #$00 ;Include in the same IRQ?
bcc UF_CopyLoop2Next
UF_CopyLoop2EndIrq:
txa
sbc zpSrcLo
tay
lda sprIrqAdvanceTbl-1,y
ldy zpSrcLo
adc sortSprY,y
sta sprIrqLine-1,y ;Store IRQ start line (with advance based on sprite count)
UF_EndMark: lda sortSprC-1,x ;Make IRQ endmark
ora #$80
sta sortSprC-1,x
UF_CopyLoop2: ldy sprOrder,x
lda sprY,y
cmp #MAX_SPRY
bcs UF_FinalEndMark ;Reached end - no further IRQs
sta sortSprY,x
sbc #21-1
cmp sortSprY-8,x ;Check for physical sprite overlap
bcc UF_CopyLoop2Skip
stx zpSrcLo ;Store IRQ startindex
adc #5-1 ;Store end Y compare for further sprites
sta UF_CopyLoop2YCmp+1
lda sprC,y
bmi UF_CopyLoop2Skip
jmp UF_CopyLoop2Begin
UF_CopyLoop2Skip:
inc UF_CopyLoop2+1
inc UF_CopyLoop2B+1
bne UF_CopyLoop2
UF_FinalEndMark:lda #$00 ;Make final endmark, C=1 here
sta sprIrqLine-1,x
UF_AllDone: stx SSR_LastSortSpr+1 ;C=1 if jumping here from earlier
txa ;Check which sprites are on
sbc firstSortSpr
cmp #$09
bcc UF_NotMoreThan8
lda #$08
UF_NotMoreThan8:tay
UF_RemoveLSBLoop:
dex
bmi UF_SpritesDone
lda sortSprC,x
and #COLOR_OVERLAY
beq UF_RemoveLSBNext
lda sortSprX,x
ora #$01
sta sortSprX,x
UF_RemoveLSBNext:
cpx firstSortSpr
bne UF_RemoveLSBLoop
UF_SpritesDone: lda d015Tbl,y
pha
lda scrCounter ;Is it the screen shift? Needs strict timing, so in that case trigger
beq UF_NoScreenShift ;whole frameupdate from the IRQ, *but* we can calculate already ahead
UF_ScreenShift: lda firstSortSpr
sta Irq3_FirstSortSpr+1
pla
sta Irq3_D015+1
pla
sta Irq3_ScrollY+1
pla
sta Irq3_ScrollX+1
jsr PrepareDrawScreen
dec drawScreenFlag
jmp UpdatePanel
UF_NoScreenShift:
UF_BlockUpdateFlag: ;Check whether to shift screen next frame + precalculate scrolling
ldx #$00 ;Assume no shifting, unless there are blockupdates
lda scrollX
clc
adc scrollSX
tay
bmi UF_HasXShift
cpy #$40
bcc UF_XDone
UF_HasXShift: lda blockX ;Update block & map-coords
eor #$01
sta blockX
bcc UF_XNeg
UF_XPos: bne UF_XShiftOK
inc mapX
lsr ;A=0, set zero flag for the branch below
UF_XNeg: beq UF_XShiftOK
lda mapX
beq UF_XLimit
dec mapX
UF_XShiftOK: inx
UF_XDone: lda scrollSX ;Are we on the edge of the
bmi UF_XDone2 ;map (right) and going right?
lda mapX
UF_MapXLimit: cmp #$00
bcc UF_XDone2
UF_XLimit: ldy #$00 ;Limit scroll before storing
sty blockX
UF_XDone2: tya
and #$3f
sta SL_PrecalcX+1
lda scrollY
clc
adc scrollSY
tay
bmi UF_HasYShift
cpy #$40
bcc UF_YDone
UF_HasYShift: lda blockY
eor #$01
sta blockY
bcc UF_YNeg
UF_YPos: bne UF_YShiftOK
inc mapY
lsr
UF_YNeg: beq UF_YShiftOK
lda mapY
beq UF_YLimit
dec mapY
UF_YShiftOK: inx
UF_YDone: lda scrollSY ;Are we on the edge of the map
bmi UF_YDone2 ;(bottom) and going down?
lda mapY
UF_MapYLimit: cmp #$00
bcc UF_YDone2
UF_YLimit: ldy #$00 ;Limit scroll before storing
sty blockY
UF_YDone2: tya
and #$3f
sta SL_PrecalcY+1
txa ;Any screen shifting?
beq UF_NoRedraw
inc scrCounter ;Move forward to screen redraw frame
UF_NoRedraw: if SHOW_FREE_TIME > 0
dec $d020
endif
UF_WaitNormal: lda newFrameFlag ;Wait until previous frameupdate is completely finished
bne UF_WaitNormal
lda $d011 ;If no colorshift, just need to make sure we
bmi UF_WaitDone ;are not late from the frameupdate
lda $d012
cmp #IRQ1_LINE+$02
bcs UF_WaitDone
cmp #IRQ1_LINE-$05
bcs UF_WaitNormal
UF_WaitDone: if SHOW_FREE_TIME > 0
inc $d020
endif
lda firstSortSpr
sta Irq1_FirstSortSpr+1
pla
sta Irq1_D015+1
pla
sta Irq1_ScrollY+1
pla
sta Irq1_ScrollX+1
dec newFrameFlag ;$ff = process new frame
UF_NoScrollWork:jmp UpdatePanel
; Prepare full redraw of screen.
;
; Parameters: -
; Returns: -
; Modifies: A,X,Y
PrepareDrawScreen:
lda blockY
sta dsBlockY
tay
asl
adc blockX
tax
lda dsStartTbl,x
sta dsStartX
lda #>blkTL
eor blockX
sta dsTopPtrHi
ora #$02
sta dsBottomPtrHi
lda dsEdgeTbl,x
sta dsEdgeX
lda dsEndTbl,y
sta DSUpperHalfEndCmp+1
sta DSLowerHalfEndCmp+1
lda upperHalfJumpTblLo,y
sta DSUpperHalfJump+1
lda upperHalfJumpTblHi,y
sta DSUpperHalfJump+2
lda lowerHalfJumpTblLo,y
sta DSLowerHalfJump+1
lda lowerHalfJumpTblHi,y
sta DSLowerHalfJump+2
tya
adc mapY
tay
lda #$00
sta UF_BlockUpdateFlag+1 ;Clear blockupdates now
sta scrCounter ;And reset scrollcounter
sta zpSrcHi
lda blockX
sta dsBlockX
adc mapX ;C=0
asl
rol zpSrcHi
sec
sbc dsStartTbl,x
tax
bcs PDS_MSBOK
dec zpSrcHi
PDS_MSBOK: clc
loadrow DSRow0,DSEdge0,0
bcc PDS_NoCarrySet ;If carry set after first row load, adjust the low offset
dex
PDS_NoCarrySet: loadrow DSRow1,DSEdge1,1
loadrow DSRow2,DSEdge2,2
loadrow DSRow3,DSEdge3,3
loadrow DSRow4,DSEdge4,4
loadrow DSRow5,DSEdge5,5
loadrow DSRow6,DSEdge6,6
loadrow DSRow7,DSEdge7,7
loadrow DSRow8,DSEdge8,8
loadrow DSRow9,DSEdge9,9
loadrow DSRow10,DSEdge10,10
lda blockY
beq PDS_SkipLoadTop
loadrow DSTopRow,DSTopEdge,-1
jmp PDS_SkipLoadBottom
PDS_SkipLoadTop:loadrow DSBottomRow,DSBottomEdge,11
PDS_SkipLoadBottom:
lda blockX ;Adjust edge in case of right one
bne PDS_SkipAdjustEdge ;Increment-adjust is easier than decrement
adjustedge DSEdge0
adjustedge DSEdge1
adjustedge DSEdge2
adjustedge DSEdge3
adjustedge DSEdge4
adjustedge DSEdge5
adjustedge DSEdge6
adjustedge DSEdge7
adjustedge DSEdge8
adjustedge DSEdge9
adjustedge DSEdge10
lda blockY
beq PDS_SkipAdjustTop
adjustedge DSTopEdge
rts
PDS_SkipAdjustTop:
adjustedge DSBottomEdge
PDS_SkipAdjustEdge:
rts
; Redraw screen. Called from main program at zone changes, or from the IRQ when scrolling
;
; Parameters: -
; Returns: -
; Modifies: A,X,Y
DrawScreen: clc
ldx dsEdgeX
lda dsBlockY
beq DSEdge0
DSTopEdge: drawbottomedge -1*40
DSEdge0: drawedge 0*80
DSEdge1: drawedge 1*80
DSEdge2: drawedge 2*80
DSEdge3: drawedge 3*80
DSEdge4: drawedge 4*80
DSEdge5: drawedge 5*80
ldx dsStartX
lda dsBlockY
beq DSRow0
DSTopRow: drawbottom -1*40-1
DSRow0: drawfullblock 0*80-1
DSRow1: drawfullblock 1*80-1
DSRow2: drawfullblock 2*80-1
DSRow3: drawfullblock 3*80-1
DSRow4: drawfullblock 4*80-1
DSRow5: drawfullblock 5*80-1
DSUpperHalfEndCmp:
cpx #37
bcs DSUpperHalfDone
inx
inx
DSUpperHalfJump:jmp DSRow0
DSUpperHalfDone:clc
ldx dsEdgeX
lda dsBlockY
bne DSEdge6
DSBottomEdge: drawtopedge 11*80
DSEdge6: drawedge 6*80
DSEdge7: drawedge 7*80
DSEdge8: drawedge 8*80
DSEdge9: drawedge 9*80
DSEdge10: drawedge 10*80
DSLowerHalfNoLeft:
ldx dsStartX
lda dsBlockY
bne DSRow6
DSBottomRow: drawtop 11*80-1
DSRow6: drawfullblock 6*80-1
DSRow7: drawfullblock 7*80-1
DSRow8: drawfullblock 8*80-1
DSRow9: drawfullblock 9*80-1
DSRow10: drawfullblock 10*80-1
DSLowerHalfEndCmp:
cpx #37
bcs DSLowerHalfDone
inx
inx
DSLowerHalfJump:jmp DSRow6
DSLowerHalfDone:rts