-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlevel-select.asm
1840 lines (1535 loc) · 29.5 KB
/
level-select.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
ls_PalStack:
.byte $0F, $0F, $09, $20
ls_PalCables:
.byte $0F, $12, $1A, $16
data_ls_BGPalettes:
.byte $0F, $19, $2A, $09
.byte $0F, $39, $0F, $09
.byte $0F, $00, $10, $30
nop
nop
; Intial load of palettes
ls_LoadPalettes:
ldx #0
@stack:
lda ls_PalStack, x
sta PaletteBufferSprites, x
inx
cpx #4
bne @stack
; background palettes
ldx #0
:
lda data_ls_BGPalettes, x
sta PaletteBuffer, x
inx
cpx #12
bne :-
; TODO: Read this from the animation data
lda #1
ldx #7
:
sta ls_PaletteFrameTimer, x
dex
bpl :-
; cable palette
ldx #0
ldy #8
:
lda ls_PalCables, x
sta PaletteBufferSprites, y
iny
inx
cpx #4
bne :-
rts
Init_LevelSelect:
.NMI_Disable
.Disable_Drawing
jsr MMC1_Select_Vert
jsr Clear_NonGlobalZp
jsr ClearSprites
jsr ls_LoadPalettes
jsr ClearAttrTable0
jsr ClearAttrTable1
lda #ChrData::LevelSelect
jsr LoadChrData
lda #ChrData::LevelSelectUi
jsr LoadChrData
bit $2000
lda #$22
sta $2006
lda #$60
sta $2006
; Draw a line above the box
lda #$FD
ldx #32
:
sta $2007
dex
bne :-
; draw a box for the bottom non-map portion
ldx #32
ldy #10
lda #$FE
:
sta $2007
dex
bne :-
ldx #32
dey
bne :-
lda #$26
sta $2006
lda #$60
sta $2006
ldx #32
lda #$FD
:
sta $2007
dex
bne :-
; Draw a column on the right side of the bottom
lda #$22
sta $2006
lda #$9F
sta $2006
.Update_PpuControl PPU_CTRL_VERTICAL
lda #$FF
ldx #10
:
sta $2007
dex
bne :-
;.Update_PpuControl PPU_CTRL_NMI
.Update_PpuControl 0
lda #$20
sta $2006
lda #$00
sta $2006
; Draw the background
ldx #0
:
lda data_LS_BackgroundA, x
sta $2007
inx
bne :-
:
lda data_LS_BackgroundA+256, x
sta $2007
inx
bne :-
:
lda data_LS_BackgroundA+512, x
sta $2007
inx
cpx #128
bne :-
lda #$24
sta $2006
lda #$00
sta $2006
; Draw the second background
ldx #0
:
lda data_LS_BackgroundB, x
sta $2007
inx
bne :-
:
lda data_LS_BackgroundB+256, x
sta $2007
inx
bne :-
:
lda data_LS_BackgroundB+512, x
sta $2007
inx
cpx #128
bne :-
; Draw the level icons
lda #0
sta menu_DrawnSprites
jsr ls_LoadSprites
; Setup sprite zero
lda #$FD
sta Sprites+1
lda #$C0 ;x
sta Sprites+3
lda #151 ;y
sta Sprites+0
lda #0
sta Sprites+2
; Load up current selection data
lda menu_PrevLevel
asl a
tax
lda data_Level_Progression_Idx, x
sta ls_LevelProg
lda data_Level_Progression_Idx+1, x
sta ls_LevelProg+1
; load data into cursor section stuff
ldy #0
ldx #0
stx ls_NumActiveLevels
:
lda (ls_LevelProg), y
bmi :+
sta ls_ActiveLevels, x
inc ls_NumActiveLevels
inx
iny
jmp :-
:
lda #LS_CURSOR_ANIM_FRAMES
sta ls_cursorAnim
lda #LS_CURSOR_COLOR_START
sta ls_cursorColorBuff
; Light up previous traces
lda #0
sta TmpW
lda ls_PrevTraceCount
beq @noPrevTraces
sta IdxB
@prevTraceLoop:
lda TmpW
asl a
tax
inc TmpW
lda ls_PrevTraces, x
sta AddressPointer1
lda ls_PrevTraces+1, x
sta AddressPointer1+1
jsr ls_LightGivenTrace
jsr ls_WriteTraceAttr
lda AddressPointer2
sta ls_PrevTraceAddr
lda AddressPointer2+1
sta ls_PrevTraceAddr+1
lda IdxD
sta ls_PrevTraceData
dec IdxB
bne @prevTraceLoop
@noPrevTraces:
lda #0
sta ls_SelectedLevel
jsr ls_LightTrace
jsr ls_LoadCursor
; Draw bottom UI
lda #$22
sta $2006
lda #$80
sta $2006
;ldy data_ls_ui2_len
;lda data_LS_Ui_Len+0
;sta AddressPointer0+0
;lda data_LS_Ui_Len+1
;sta AddressPointer0+1
lda #<data_LS_Ui
sta AddressPointer1+0
lda #>data_LS_Ui
sta AddressPointer1+1
ldy #0
@uiLoop:
lda (AddressPointer1), y
cmp #$FF
beq @uiDone
sta $2007
; increment pointer
inc AddressPointer1
bne :+
inc AddressPointer1+1
:
jmp @uiLoop
@uiDone:
; Attributes for the bottom
lda #$23
sta $2006
lda #$E8
sta $2006
ldy #data_LS_Ui_Attr_Len
ldx #0
:
lda data_LS_Ui_Attr, x
sta $2007
inx
dey
bne :-
; Level title
lda #$22
sta $2006
lda #$C6
sta $2006
ldx #$B0
:
stx $2007
inx
cpx #$B8
bne :-
; Level text, line one
lda #$23
sta $2006
lda #$06
sta $2006
ldx #$B8
:
stx $2007
inx
cpx #$C8
bne :-
; Level text, line two
lda #$23
sta $2006
lda #$26
sta $2006
ldx #$C8
:
stx $2007
inx
cpx #$D8
bne :-
lda #$26
sta $2006
lda #$80
sta $2006
lda #$FE
ldy #10
:
.repeat 32
sta $2007
.endrepeat
dey
bne :-
lda #1
sta ls_WriteText
; title text
jsr ls_PrepLevelText
jsr ls_WriteTextTiles
;lda #$1B
;sta AddressPointer0+1
;lda #$00
;sta AddressPointer0+0
;lda #8
;jsr WriteTextBuffer_LevelSelect_8
; line 1 text
jsr ls_PrepLevelText
jsr ls_WriteTextTiles
jsr ls_WriteTextTiles
;lda #$1B
;sta AddressPointer0+1
;lda #$80
;sta AddressPointer0+0
;lda #16
;jsr WriteTextBuffer_LevelSelect_15
; line 2 text
jsr ls_PrepLevelText
jsr ls_WriteTextTiles
jsr ls_WriteTextTiles
;lda #$1C
;sta AddressPointer0+1
;lda #$80
;sta AddressPointer0+0
;lda #16
;jsr WriteTextBuffer_LevelSelect_15
lda #0
sta ls_WriteText
; Draw the cable sprites on the right
LSCABLE_YSTART = 160
LSCABLE_XSTART = 226
;ldx #4 * 6 ; destination offset
ldx #2 ; attr
.repeat 4, i
lda #LSCABLE_YSTART
sta Sprites+(5*4)+(i*4)+0
lda #$B0 + i
sta Sprites+(5*4)+(i*4)+1
stx Sprites+(5*4)+(i*4)+2
lda #LSCABLE_XSTART + (i * 8)
sta Sprites+(5*4)+(i*4)+3
.endrepeat
.repeat 4, i
lda #LSCABLE_YSTART+8
sta Sprites+(9*4)+(i*4)+0
lda #$B4 + i
sta Sprites+(9*4)+(i*4)+1
stx Sprites+(9*4)+(i*4)+2
lda #LSCABLE_XSTART + (i * 8)
sta Sprites+(9*4)+(i*4)+3
.endrepeat
.repeat 4, i
lda #LSCABLE_YSTART+16
sta Sprites+(13*4)+(i*4)+0
lda #$B8 + i
sta Sprites+(13*4)+(i*4)+1
stx Sprites+(13*4)+(i*4)+2
lda #LSCABLE_XSTART + (i * 8)
sta Sprites+(13*4)+(i*4)+3
.endrepeat
.repeat 4, i
lda #LSCABLE_YSTART+24
sta Sprites+(17*4)+(i*4)+0
lda #$BC + i
sta Sprites+(17*4)+(i*4)+1
stx Sprites+(17*4)+(i*4)+2
lda #LSCABLE_XSTART + (i * 8)
sta Sprites+(17*4)+(i*4)+3
.endrepeat
ldx #$42 ; attr
.repeat 4, i
lda #LSCABLE_YSTART+(8*4)+(i*8)-3
sta Sprites+(21*4)+(i*4)+0
lda #$B0 + (i * 4)
sta Sprites+(21*4)+(i*4)+1
stx Sprites+(21*4)+(i*4)+2
lda #3
sta Sprites+(21*4)+(i*4)+3
.endrepeat
lda #1
sta ls_SpinnerCount
lda #0
sta ls_SpinnerFlip
;jsr ls_PrepLevelText
jsr TextClearBuffer
jsr TextClearStringBuffer
.NMI_Set NMI_LevelSelect
.Update_PpuControl PPU_CTRL_NMI
jsr WaitForNMI
lda #1
sta ls_WriteText
Frame_LevelSelect:
lda #0
sta TmpZ
jsr ls_AnimateSpinner
jsr ReadControllers
lda #BUTTON_RIGHT
jsr ButtonPressedP1
beq :+
lda #1
sta ls_WriteText
lda ls_SelectedLevel
sta ls_SelectedLevel_Prev
inc ls_SelectedLevel
inc TmpZ
jmp @incOver
:
lda #BUTTON_DOWN
jsr ButtonPressedP1
beq :+
lda #1
sta ls_WriteText
lda ls_SelectedLevel
sta ls_SelectedLevel_Prev
inc ls_SelectedLevel
inc TmpZ
:
@incOver:
; handle overflow
lda ls_SelectedLevel
cmp ls_NumActiveLevels
bne :+
lda #0
sta ls_SelectedLevel
:
lda #BUTTON_LEFT
jsr ButtonPressedP1
beq :+
lda #1
sta ls_WriteText
lda ls_SelectedLevel
sta ls_SelectedLevel_Prev
dec ls_SelectedLevel
inc TmpZ
jmp @decUnder
:
lda #BUTTON_UP
jsr ButtonPressedP1
beq :+
lda #1
sta ls_WriteText
lda ls_SelectedLevel
sta ls_SelectedLevel_Prev
dec ls_SelectedLevel
inc TmpZ
:
@decUnder:
; handle underflow
lda ls_SelectedLevel
bpl :+
lda ls_NumActiveLevels
sec
sbc #1
sta ls_SelectedLevel
:
; Don't inc/dec selection for first level
lda menu_PrevLevel
cmp #$0F
bne :+
lda #0
sta ls_SelectedLevel
:
; Write trace attributes if selection changed
lda TmpZ
beq :+
jsr ls_ResetTrace
jsr ls_LightTrace
:
jsr ls_LoadCursor
lda ls_WriteText
beq :+
jsr ls_PrepLevelText
:
lda #BUTTON_A
jsr ButtonPressedP1
beq :+
jmp ls_StartLevel
:
lda #BUTTON_START
jsr ButtonPressedP1
beq :+
jmp ls_StartLevel
:
; On select, got to a "Map View" mode
lda #BUTTON_SELECT
jsr ButtonPressedP1
beq :+
jsr WaitForSpriteZero
lda #0
sta $2005
sta $2005
lda #PPU_CTRL_NMI | PPU_CTRL_BG_PATTERN
sta $2000
jsr WaitForNMI
jsr ls_DoAnimations
jmp Frame_LevelSelect_ViewMap
:
jsr WaitForSpriteZero
lda #0
sta $2005
sta $2005
lda #PPU_CTRL_NMI | PPU_CTRL_BG_PATTERN
sta $2000
jsr ls_DoAnimations
;jsr TextClearBuffer
jsr WaitForNMI
jmp Frame_LevelSelect
Frame_LevelSelect_ViewMap:
jsr ReadControllers
lda controller1
and #BUTTON_LEFT
beq :+
lda menu_ScrollValue
beq :+
dec menu_ScrollValue
:
lda controller1
and #BUTTON_RIGHT
beq :+
lda menu_ScrollValue
cmp #$FF
beq :+
inc menu_ScrollValue
:
; Go back to the main menu on SELECT
lda #BUTTON_SELECT
jsr ButtonPressedP1
beq :+
jsr ls_DrawCursorXY
jsr ls_DoAnimations
jsr ls_AnimateSpinner
jsr WaitForSpriteZero
lda #0
sta $2005
sta $2005
lda #PPU_CTRL_NMI | PPU_CTRL_BG_PATTERN
sta $2000
jsr WaitForNMI
jmp Frame_LevelSelect
:
ldx #11
ldy #10
lda #228
sta TmpX
lda #139
sta TmpY
jsr ls_DrawCursorXY
jsr ls_DoAnimations
jsr ls_AnimateSpinner
jsr WaitForSpriteZero
lda #0
sta $2005
sta $2005
lda #PPU_CTRL_NMI | PPU_CTRL_BG_PATTERN
sta $2000
jsr WaitForNMI
jmp Frame_LevelSelect_ViewMap
ls_StartLevel:
; Skip previous traces if previous level is $0F or $00
lda menu_PrevLevel
cmp #$0F
beq :+
; Save the selected trace's data pointer
; so we can relight it later
lda ls_PrevTraceCount
asl a
tax
lda ls_TmpTracePointer
sta ls_PrevTraces, x
lda ls_TmpTracePointer+1
sta ls_PrevTraces+1, x
inc ls_PrevTraceCount
:
ldx ls_SelectedLevel
lda ls_ActiveLevels, x
sta CurrentBoard
jsr WaitForNMI
lda #1
jmp JumpToInit
ls_DoAnimations:
; Cursor palette animation
dec ls_cursorAnim
bne @noCursorAnim
lda #LS_CURSOR_ANIM_FRAMES
sta ls_cursorAnim
inc ls_cursorColorBuff
lda ls_cursorColorBuff
cmp #LS_CURSOR_COLOR_END
bne :+
lda #LS_CURSOR_COLOR_START
sta ls_cursorColorBuff
:
@noCursorAnim:
lda #0
sta menu_LoadedSprites
sta menu_DrawnSprites
sta IdxA
; Load visible sprite animations
; ;.repeat SPRITE_OBJ_COUNT, count
; .repeat 11, count
; lda #count
; jsr ls_SpriteAnimate
; .endrepeat
@animLoop:
lda IdxA
jsr ls_SpriteAnimate
;lda IdxA
;jsr ls_SpriteScroll
inc IdxA
lda IdxA
cmp #SPRITE_OBJ_COUNT
bne @animLoop
; scroll animation sprites
; ldx #0
; stx IdxA
;@scrollLoop:
; lda IdxA
; jsr ls_SpriteScroll
;
; inc IdxA
; lda IdxA
; cmp menu_LoadedSprites
; bne @scrollLoop
; sprite ID -> offset
lda menu_DrawnSprites
clc
adc #LEVELSELECT_SPRITE_OFFSET
asl a
asl a
tax
lda #$FF
:
sta Sprites, x
inx
bne :-
; Animate all palettes
ldx #0
stx IdxA
@palLoop:
lda IdxA
jsr ls_PaletteAnimate
inc IdxA
lda IdxA
cmp #PAL_ANIM_COUNT
bne @palLoop
rts
tr_frames:
; scroll the screen
; move the cursor offset
; find offset between two steps
; move cursor one step per frame
; ignore all input during animation
; animate other sprites during this transition
jsr ls_DoAnimations
jsr ls_StepCursor
jsr WaitForNMI
jmp Frame_LevelSelect
ls_StepCursor:
rts
NMI_LevelSelect:
pha
txa
pha
tya
pha
jsr WritePalettes
.WriteSprites
lda ls_AttrUpdate
ora ls_AttrUpdate_Clr
beq :+
jsr ls_WriteTraceAttr
:
jsr ls_WriteTextTiles
;.SetScroll_Var 0
; TODO: nametable
bit $2000
lda #$20
sta $2006
lda #$00
sta $2006
lda menu_ScrollValue
sta $2005
lda #0
sta $2005
.Update_PpuControl PPU_CTRL_NMI
.Update_PpuMask PPU_MASK_ON
lda #$FF
sta Sleeping
pla
tay
pla
tax
pla
rti
ls_WriteTextTiles:
lda #0
sta $2001
ldx ls_WriteText
beq @noWrite
dex
lda ls_writeTilesJmp_Lo, x
sta AddressPointer0+0
lda ls_writeTilesJmp_Hi, x
sta AddressPointer0+1
jmp (AddressPointer0)
@noWrite:
rts
ls_writeTiles_Title:
lda #$1B
sta AddressPointer0+1
lda #$00
sta AddressPointer0+0
jsr WriteTextBuffer_LevelSelect_8
jmp ls_writeTiles_done
ls_writeTiles_Line1a:
lda #$1B
sta AddressPointer0+1
lda #$80
sta AddressPointer0+0
jsr WriteTextBuffer_LevelSelect_8
jmp ls_writeTiles_done
ls_writeTiles_Line1b:
lda #$1C
sta AddressPointer0+1
lda #$00
sta AddressPointer0+0
jsr WriteTextBuffer_LevelSelect_7
jmp ls_writeTiles_done
ls_writeTiles_Line2a:
lda #$1C
sta AddressPointer0+1
lda #$80
sta AddressPointer0+0
jsr WriteTextBuffer_LevelSelect_8
jmp ls_writeTiles_done
ls_writeTiles_Line2b:
lda #$1D
sta AddressPointer0+1
lda #$00
sta AddressPointer0+0
jsr WriteTextBuffer_LevelSelect_7
jmp ls_writeTiles_done
ls_writeTiles_done:
inc ls_WriteText
lda ls_WriteText
cmp #6
bne :+
lda #0
sta ls_WriteText
sta IgnoreInput
:
rts
ls_writeTilesJmp_Hi:
.byte .hibyte(ls_writeTiles_Title)
.byte .hibyte(ls_writeTiles_Line1a)
.byte .hibyte(ls_writeTiles_Line1b)
.byte .hibyte(ls_writeTiles_Line2a)
.byte .hibyte(ls_writeTiles_Line2b)
ls_writeTilesJmp_Lo:
.byte .lobyte(ls_writeTiles_Title)
.byte .lobyte(ls_writeTiles_Line1a)
.byte .lobyte(ls_writeTiles_Line1b)
.byte .lobyte(ls_writeTiles_Line2a)
.byte .lobyte(ls_writeTiles_Line2b)
; jsr to AddressPointer0
JsrPointer:
lda #>@end-1
pha
lda #<@end-1
pha
jmp (AddressPointer0)
@end:
rts
; PPU Address in AddressPointer0
; Width of tiles in TmpX
; Height of tiles in TmpY
; Tile ID start in TmpW
ls_DrawLevelIcon:
;ldy TmpY
@row:
lda AddressPointer0+1
sta $2006
lda AddressPointer0+0
sta $2006
ldy TmpW
ldx TmpX
@col:
sty $2007
iny
dex
bne @col
dec TmpY
beq @done
; Get the start ID of the next tile row
lda TmpW
clc
adc #16
sta TmpW
tay
; Increment address to next row
lda AddressPointer0
clc
adc #32
sta AddressPointer0
bcc :+
inc AddressPointer0+1
:
jmp @row
@done:
rts
; Animate palettes
; Palette animation index in A
ls_PaletteAnimate:
asl a
tax
lda data_PaletteAnim_List+0, x
sta AddressPointer0+0
lda data_PaletteAnim_List+1, x
sta AddressPointer0+1
ldy #0