-
Notifications
You must be signed in to change notification settings - Fork 2
/
credits.asm
1082 lines (865 loc) · 17.5 KB
/
credits.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
; asmsyntax=ca65
.import cr_data_groups
.importzp CR_GROUP_COUNT
.include "credits_ram.asm"
CLEAR_TILE_ID = 0
CR_T2_SPEED = 8 ; color cycle speed (in frames) for the tier two names
CR_INIT_NAME_COUNT = 11
CR_START_GROUP = 1
CR_FIRST_SCREEN_SIZE = 8 ; number of names on the first screen
; that will not need a scroll.
CR_SCROLL_SPEED = 3 ; frames to wait for the next scroll update
CR_SCROLL_WAIT = 120 ; frames to wait to start scrolling
CR_TOP = %10011000
CR_BOTTOM = %10011010
CR_UPDATE_TILE = %10000000
CR_UPDATE_ATTR = %01000000
CR_PADDING = $20
CR_CHUNK_TIMER = 16 ; frames between chunk loading (for cr_nextChunkWait)
CR_NEXT_GROUP_PAUSE = 60 ; time to pause between groups (n * CR_SCROLL_SPEED) frames
CR_SCROLL_TO_PAUSE = 48 ; lines to scroll before pausing.
Init_Credits:
; "Disable" NMI
.NMI_Disable
; Disable drawing BG and sprites
lda #$00
sta $2001
jsr MMC1_Select_Horiz
lda #<Credits_Palette
sta AddressPointer3
lda #>Credits_Palette
sta AddressPointer3+1
jsr WritePaletteData
lda #$00
jsr LoadChrData
;jsr LoadPalettes
;jsr WritePalettes
;lda #$00
;jsr FillNametable0
;lda #$00
;jsr FillNametable2
;jsr ClearAttrTable0
;jsr ClearAttrTable2
jsr ClearSprites
jsr WriteSprites
jsr Clear_CreditRam
lda #$23
sta AddressPointer2
lda #CR_SCROLL_WAIT
sta cr_scrollWait
lda #32
sta cr_nextChunkWait
lda #$21
sta cr_tier3Color
lda #0
sta cr_tier2Color
lda #CR_T2_SPEED
sta cr_t2Count
;lda #$00
;sta $2000
; reset scroll
;bit $2002
;lda #$00
;sta $2005
;sta $2005
lda #CR_TOP
sta cr_scroll_table
lda #CR_START_GROUP
sta cr_currentGroup
jmp credits_LoadGroup
;jmp Credits_Frame
;rts
; Loads up a name, and changes the
; group if necessary.
credits_LoadData:
jsr credits_LoadName
beq :++
inc cr_currentGroup
lda cr_currentGroup
cmp #CR_GROUP_COUNT
bcc :+
lda #1
sta cr_currentGroup
:
jsr credits_LoadGroup
jsr credits_LoadName
:
rts
credits_LoadNextGroup:
inc cr_currentGroup
lda cr_currentGroup
cmp #CR_GROUP_COUNT
bcc :+
lda #1
sta cr_currentGroup
: ; Fall-through to credits_LoadGroup
; Group index in A
; Switches to the next group, clears the screen, draws
; the header, and draws the initial screen for the group.
credits_LoadGroup:
pha
.NMI_Disable
jsr WaitForNMI
pla
asl a
tax
; Load pointer to group data
lda cr_data_groups, x
sta AddressPointer1
lda cr_data_groups+1, x
sta AddressPointer1+1
lda #0
sta cr_nextGroup
; Load pointer to the group's init code
lda credits_GroupInits, x
sta AddressPointer0
lda credits_GroupInits+1, x
sta AddressPointer0+1
; Wait for the next NMI to avoid artifacts
.NMI_Set NMI_Bare
jsr WaitForNMI
; Disable drawing BG and sprites
lda #$00
sta $2001
; Clear the screen
lda #$00
jsr FillNametable0
lda #$00
jsr FillNametable2
jsr ClearAttrTable0
jsr ClearAttrTable2
; Reset the scroll stuff
lda #CR_TOP
sta cr_scroll_table
lda #CR_SCROLL_TO_PAUSE
sta cr_scrollToPause
lda #0
sta cr_scroll
sta cr_AttributeReady
sta cr_AttributeByte
sta cr_AttrSecondWrite
lda #1
sta cr_nextChunkWait
; Draw the header (this might move later)
jsr credits_DrawTwitchHeader
lda #$23
sta AddressPointer2
; Jump to the group's init code
jmp (AddressPointer0)
credits_GroupInits:
.word credits_group_attrib
.word credits_group_1year
.word credits_group_9months
.word credits_group_6months
.word credits_group_3months
.word credits_group_1month
credits_GroupLabels:
.word :+
.word :++
.word :+++
.word :++++
.word :+++++
.word :++++++
: .byte "what", $00
: .byte "One Year", $00
: .byte "Nine Months", $00
: .byte "Six Months", $00
: .byte "Three Months", $00
: .byte "One Month", $00
; Draws the group's names to the screen, up to the
; max names per screen.
credits_group_drawNames:
lda #6
sta cr_currentPPULine
lda #CR_INIT_NAME_COUNT
sta TmpX
lda #3
sta cr_currentAttrOffset
; Number of names initially
; written to the screen.
lda #0
sta TmpZ
lda #CR_NEXT_GROUP_PAUSE
sta cr_nextGroupPaused
@nameLoop:
jsr credits_LoadName
bne @loopEnd
jsr credits_WriteBuffer
inc TmpZ
dec TmpX
bne @nameLoop
@loopEnd:
lda #CR_SCROLL_WAIT
sta cr_scrollWait
lda TmpZ
cmp #CR_FIRST_SCREEN_SIZE
; if TmpZ > CR_FIRST_SCREEN_SIZE, scroll the screen
; else, draw new screen instead of scrolling.
bcs :+
; Pause for the same amount of time as the multi-screen groups
; by not pausing before a non-existent scroll
lda #CR_SCROLL_SPEED
sta cr_scrollWait
lda #$80
sta cr_singleScreen
jmp :++
:
lda #$00
sta cr_singleScreen
:
jsr credits_clearTileBuffer
jmp Credits_Frame
; Label ID in A
; PPU address should already be set before
; calling this.
credits_DrawGroupLabel:
asl a
tax
lda credits_GroupLabels, x
sta AddressPointer0
lda credits_GroupLabels+1, x
sta AddressPointer0+1
ldy #0
: lda (AddressPointer0), y
beq :+
sta $2007
iny
jmp :-
:
rts
credits_group_attrib:
rts
credits_group_1year:
lda #$21
sta $2006
lda #$0C
sta $2006
lda #1
jsr credits_DrawGroupLabel
jmp credits_group_drawNames
; Clear tile buffer
credits_clearTileBuffer:
ldy #64
ldx #0
lda #0
: sta cr_TileBuffer, x
inx
dey
bne :-
rts
credits_group_9months:
lda #$21
sta $2006
lda #$0B
sta $2006
lda #2
jsr credits_DrawGroupLabel
jmp credits_group_drawNames
credits_group_6months:
lda #$21
sta $2006
lda #$0C
sta $2006
lda #3
jsr credits_DrawGroupLabel
jmp credits_group_drawNames
credits_group_3months:
lda #$21
sta $2006
lda #$0A
sta $2006
lda #4
jsr credits_DrawGroupLabel
jmp credits_group_drawNames
credits_group_1month:
lda #$21
sta $2006
lda #$0C
sta $2006
lda #5
jsr credits_DrawGroupLabel
jmp credits_group_drawNames
credits_DrawTwitchHeader:
; three rows down, seven columns in
lda #$20
sta $2006
lda #$47
sta $2006
; increment byte loop
ldy #$10
ldx #3
:
sty $2007
iny
dex
bne :-
; increment byte loop
ldy #$02
ldx #7
:
sty $2007
iny
dex
bne :-
; second row
lda #$20
sta $2006
lda #$67
sta $2006
; increment byte loop
ldy #$13
ldx #3
:
sty $2007
iny
dex
bne :-
; increment byte loop
ldy #$80
ldx #15
:
sty $2007
iny
dex
bne :-
lda #$20
sta $2006
lda #$87
sta $2006
; increment byte loop
ldy #$16
ldx #3
:
sty $2007
iny
dex
bne :-
; increment byte loop
ldy #$90
ldx #15
:
sty $2007
iny
dex
bne :-
rts
Credits_WriteAttr:
lda #0
sta cr_AttributeReady
lda AddressPointer2
sta $2006
lda AddressPointer2+1
sta $2006
ldx #8
lda cr_AttributeByte
@loop:
sta $2007
dex
bne @loop
lda #0
sta cr_AttributeByte
lda AddressPointer2+1
cmp #$F8
beq :+
rts
: lda AddressPointer2
cmp #$23
bne @firstNT
jmp @secondNT
@firstNT:
lda #$23
sta AddressPointer2
jmp @done
@secondNT:
lda #$2B
sta AddressPointer2
@done:
rts
; Write the cr_TileBuffer to the PPU
credits_WriteBuffer:
lda #0
sta cr_UpdateReady
lda cr_currentPPULine
cmp #30
bcc @noWrap
lda #0
sta cr_currentPPULine
@noWrap:
tax
inc cr_currentPPULine
lda PPU_RowStartLookup_High, x
sta $2006
lda PPU_RowStartLookup_Low, x
sta $2006
ldx #0
ldy #64
@loop:
lda cr_TileBuffer, x
sta $2007
inx
dey
bne @loop
bit cr_AttributeReady
bvc @noAttr
jmp Credits_WriteAttr
@noAttr:
rts
; Two byte increment. Increment by value in A.
cr_Decode_Opcode_IncAddr:
clc
adc AddressPointer1
sta AddressPointer1
lda AddressPointer1+1
adc #0
sta AddressPointer1+1
rts
; Start at the given byte and increment N times
cr_op_IncrementByte:
; increment past OP code
lda #1
jsr cr_Decode_Opcode_IncAddr
ldy #0
; number of bytes to write
lda (AddressPointer1), y
tax
lda #1
jsr cr_Decode_Opcode_IncAddr
; Data to start at
lda (AddressPointer1), y
sta cr_tmpByte
; move length from X to Y
txa
tay
ldx cr_tileBufferOffset
@loop:
lda cr_tmpByte
sta cr_TileBuffer, x
inc cr_tmpByte
inx
dey
bne @loop
; store the buffer offset
stx cr_tileBufferOffset
lda #1
jsr cr_Decode_Opcode_IncAddr
;jmp cr_Decode_Opcode
rts
; Repeat a byte N times
cr_op_RunLength:
; increment past OP code
lda #1
jsr cr_Decode_Opcode_IncAddr
ldy #0
; number of bytes to write
lda (AddressPointer1), y
sta cr_chunkLength
;inc cr_chunkLength
lda #1
jsr cr_Decode_Opcode_IncAddr
; data byte
lda (AddressPointer1), y
ldy cr_chunkLength
ldx cr_tileBufferOffset
@loop:
sta cr_TileBuffer, x
inx
dey
bne @loop
; Update buffer offset
lda cr_tileBufferOffset
clc
adc cr_chunkLength
sta cr_tileBufferOffset
lda #1
jsr cr_Decode_Opcode_IncAddr
;jmp cr_Decode_Opcode
rts
cr_ByteList:
lda #1
jsr cr_Decode_Opcode_IncAddr
ldx cr_tileBufferOffset
ldy #0
sty cr_tmpByte
@loop:
lda (AddressPointer1), y
beq @done
sta cr_TileBuffer, x
inx
inc cr_tmpByte
iny
jmp @loop
@done:
lda cr_tmpByte
clc
adc cr_tileBufferOffset
sta cr_tileBufferOffset
inc cr_tmpByte
lda cr_tmpByte
jsr cr_Decode_Opcode_IncAddr
rts
cr_op_Attr:
; don't increment if coming from cr_op_Name
bit cr_AttrTmp
bmi :+
lda #1
jsr cr_Decode_Opcode_IncAddr
: lda cr_AttrSecondWrite
bne @secondWrite
lda cr_currentAttrOffset
cmp #7
beq @lastRow
lda cr_AttrTmp
bmi @notChunkA
ldy #0
lda (AddressPointer1), y
jmp @notChunkADone
@notChunkA:
and #$0F
@notChunkADone:
sta cr_AttributeByte
lda #0
sta cr_AttrTmp
inc cr_AttrSecondWrite
jmp cr_op_EndOfChunk
@secondWrite:
lda cr_AttrTmp
bmi @notChunkB
lda (AddressPointer1), y
jmp @notChunkBDone
@notChunkB:
and #$0F
@notChunkBDone:
asl A
asl A
asl A
asl A
ora cr_AttributeByte
sta cr_AttributeByte
ldx cr_currentAttrOffset
inc cr_currentAttrOffset
lda PPU_AttrLookup_Low, x
sta AddressPointer2+1
lda #$FF
sta cr_AttributeReady
lda #0
sta cr_AttrSecondWrite
sta cr_AttrTmp
jmp cr_op_EndOfChunk
@lastRow:
lda cr_AttrTmp
bmi @notChunkC
ldy #0
lda (AddressPointer1), y
jmp @notChunkCDone
@notChunkC:
and #$0F
; Last row stuff
@notChunkCDone:
sta cr_AttributeByte
asl A
asl A
asl A
asl A
ora cr_AttributeByte
sta cr_AttributeByte
ldx cr_currentAttrOffset
lda PPU_AttrLookup_Low, x
sta AddressPointer2+1
; wrap to the next nametable
lda #0
sta cr_currentAttrOffset
sta cr_AttrTmp
lda #$FF
sta cr_AttributeReady
cr_op_EndOfChunk:
lda #1
jmp cr_Decode_Opcode_IncAddr
credits_LoadAttrib:
ldx #0 ; offset in the buffer
; type and length of data
ldy #0
@attribLoop:
lda (AddressPointer1), y
beq @done
bmi @name ; TODO: do different things for label and name
sta cr_nameLength ; length of label, not name, but w/e
iny
: lda (AddressPointer1), y
sta cr_attribBuffer, x
iny
dec cr_nameLength
bne :-
jmp @attribLoop
@name:
and #$7F
sta cr_nameLength ; it's a name this time.
iny
: lda (AddressPointer1), y
sta cr_attribBuffer, x
iny
dec cr_nameLength
bne :-
jmp @attribLoop
@done:
rts
; Load the current name into RAM
credits_LoadName:
lda #CR_UPDATE_TILE
sta cr_UpdateReady
; Metadata byte. bits 7-6 are attribute, rest are length.
ldy #0
lda (AddressPointer1), y
bne :+
lda #1
sta cr_nextGroup
rts
:
and #$3F
sta cr_nameLength
lda (AddressPointer1), y
and #$C0 ; get Attribute bits
; Rotate bits 7-6 to bits 1-0
clc
rol a
rol a
rol a
sta cr_AttrTmp
; fill the nibble
asl a
asl a
ora cr_AttrTmp
ora #$80
sta cr_AttrTmp
; write attribute stuff
jsr cr_op_Attr
; Length
lda cr_nameLength
lsr a ; divide by two
sta TmpY
lda #16
sec
sbc TmpY ; the amount of padding
sta cr_loopCounter
ldy #0
lda #CR_PADDING
ldx cr_tileBufferOffset
: sta cr_TileBuffer, x
inx
iny
cpy cr_loopCounter
bne :-
ldy #0
lda cr_nameLength
sta cr_loopCounter
sta TmpY
; Loop through data for the given length
: lda (AddressPointer1), y
sta cr_TileBuffer, x
iny
inx
dec TmpY
bne :-
; suffix padding
lda #CR_PADDING
: sta cr_TileBuffer, x
inx
cpx #64 ; 32 is end of row, 64 is end of second row.
bne :-
lda cr_nameLength
jsr cr_Decode_Opcode_IncAddr
lda #0
rts
; padding to fix the dissassembly in the debugger
;.byte $EA, $EA
nop
nop
cr_colors_T2:
.byte $00
cr_colors_T3:
.byte $00
cr_TierColors:
lda cr_t2Count
bne @t3check
lda #CR_T2_SPEED
sta cr_t2Count
; Tier two color
inc cr_tier2Color
ldx cr_tier2Color
cpx #6
bne @t3check
ldx #0
stx cr_tier2Color
@t3check:
dec cr_t2Count
lda cr_frameOdd
bne :+
lda #1
sta cr_frameOdd
rts
: lda #0
sta cr_frameOdd
; Tier three color
inc cr_tier3Color
lda cr_tier3Color
cmp #$2C
bne :+
lda #$21
sta cr_tier3Color
:
rts
Credits_FrameBare:
jsr WaitForNMI
jmp Credits_FrameBare
Credits_Frame:
.NMI_Set Credits_NMI
jsr ReadControllers
lda #BUTTON_START
jsr ButtonPressedP1
beq :+
lda #0
jmp JumpToInit
:
jsr cr_TierColors
bit cr_singleScreen
bpl @multiScreen
; single screen, pause for a few frames before loading the next group
lda cr_scrollWait
beq :+
dec cr_scrollWait
jmp @nextFrame
:
lda #CR_SCROLL_SPEED
sta cr_scrollWait
dec cr_nextGroupPaused ; should be set when loading a group.
bne @nextFrame
; timer hit zero, load next group
jmp credits_LoadNextGroup
@multiScreen:
lda cr_scrollWait
beq :+
; don't scroll yet
dec cr_scrollWait
jmp @nextFrame
:
; scroll a line, reload scroll wait for next time
lda #CR_SCROLL_SPEED
sta cr_scrollWait
lda cr_nextGroup
beq @nextName
; cr_nextGroup isn't zero, we've hit the end.
; scroll to the point where we should pause.
lda cr_scrollToPause
beq :+
dec cr_scrollToPause
jsr credits_IncScroll
jmp @nextFrame
: ; finished scrolling to the end
; wait to go to next group.
dec cr_nextGroupPaused
bne @nextFrame
; counter hit zero, load next group
jmp credits_LoadNextGroup
@nextName:
; not paused, continue scrolling
jsr credits_IncScroll
; Do we need to draw the next name?
dec cr_nextChunkWait
bne @nextFrame
; it's time to draw a new chunk
lda #CR_CHUNK_TIMER
sta cr_nextChunkWait
jsr credits_LoadName
beq :+
; clear the buffer if we've hit the end
jsr credits_clearTileBuffer
:
@nextFrame:
jsr WaitForNMI
jmp Credits_Frame
Credits_NMI:
lda #%00011110
sta $2001
;jsr WritePalettes
bit $2002
; Tier 2 colors
lda #$3F
sta $2006
lda #$05
sta $2006
ldx cr_tier2Color
lda Credits_Tier2, x
sta $2007
; Tier 3 colors
lda #$3F
sta $2006
lda #$09
sta $2006