-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
eznsf.s
1091 lines (1018 loc) · 15.6 KB
/
eznsf.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
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
;
; EZNSF
;
; bradsmith, 2016
; http://rainwarrior.ca
;
; ram usage
;
.segment "OAM"
.align 256
oam: .res 256
.segment "ZEROPAGE"
ptr: .res 2 ; pointer for indirect addressing
nmi_count: .res 1 ; incremented by NMI handler
gamepad: .res 1 ; gamepad poll result
.segment "BSS"
pal: .res 1 ; 0 = NTSC (60 fps), 1 = PAL (50 fps)
fps: .res 1 ; reflects NTSC/PAL
frame: .res 1 ; counts 0 to fps to measure a second
sec0: .res 1 ; display timer
sec1: .res 1
min0: .res 1
min1: .res 1
title_choose: .res 1 ; title screen selection
track_choose: .res 1 ; track selection
play_through: .res 1 ; 0 = stop at end of track, 1 = stop at end of album
play_paused: .res 1 ; 0 = playing, 1 = paused, 2 = stopped
play_len: .res 2 ; seconds in track
play_secs: .res 2 ; seconds played so far
gamepad_last: .res 1 ; gamepad from last poll
gamepad_new: .res 1 ; new buttons this poll
temp: .res 2
;
; useful definitions
;
PAD_A = $01
PAD_B = $02
PAD_SELECT = $04
PAD_START = $08
PAD_U = $10
PAD_D = $20
PAD_L = $40
PAD_R = $80
.macro PPU_LATCH addr
lda $2002
lda #>(addr)
sta $2006
lda #<(addr)
sta $2006
.endmacro
.define PPU_TILE(ax,ay) ($2000+(ay*32)+ax)
.macro PTR_LOAD addr
lda #<(addr)
sta ptr+0
lda #>(addr)
sta ptr+1
.endmacro
; shorthand for lda/sta
.macro STB addr, val
lda val
sta addr
.endmacro
;
; generated data
;
.segment "CODE"
.include "enums.sh"
.include "tables.sh"
;
; title
;
.proc title_sprite
jsr oam_clear
STB oam+(0*4)+1, #eConst::SPRITE_CHOOSE
STB oam+(0*4)+2, #2
lda title_choose
bne @choose_info
@choose_start:
STB oam+(0*4)+3, #eCoord::TITLE_START_X
STB oam+(0*4)+0, #eCoord::TITLE_START_Y-1
rts
@choose_info:
STB oam+(0*4)+3, #eCoord::TITLE_INFO_X
STB oam+(0*4)+0, #eCoord::TITLE_INFO_Y-1
rts
.endproc
.proc mode_title
lda #eScreen::TITLE
jsr load_screen
PPU_LATCH PPU_TILE(eCoord::TITLE_TITLE_X, eCoord::TITLE_TITLE_Y)
PTR_LOAD dString::title
jsr ppu_string
PPU_LATCH PPU_TILE(eCoord::TITLE_ARTIST_X, eCoord::TITLE_ARTIST_Y)
PTR_LOAD dString::artist
jsr ppu_string
PPU_LATCH PPU_TILE(eCoord::TITLE_COPYRIGHT_X, eCoord::TITLE_COPYRIGHT_Y)
PTR_LOAD dString::copyright
jsr ppu_string
@loop:
jsr title_sprite
jsr render_on
jsr gamepad_poll
lda gamepad_new
and #(PAD_L | PAD_R | PAD_U | PAD_D | PAD_SELECT)
beq @move_end
lda title_choose
eor #1
sta title_choose
jmp @loop
@move_end:
lda gamepad_new
and #(PAD_START | PAD_A | PAD_B)
beq @loop
lda title_choose
bne :+
jmp mode_tracks
:
jmp mode_info
;
;
.endproc
;
; info
;
.proc mode_info
lda #eScreen::INFO
jsr load_screen
INFO_ADDR = PPU_TILE(eCoord::INFO_X, eCoord::INFO_Y)
lda #>INFO_ADDR
sta temp+1
sta $2006
lda #<INFO_ADDR
sta temp+0
sta $2006
PTR_LOAD dString::info
:
; print lines until a 0 is reached
jsr ppu_string
lda (ptr), Y
beq :+
jsr ppu_temp_line
jmp :-
:
jsr oam_clear
@loop:
jsr render_on
jsr gamepad_poll
lda gamepad_new
beq @loop
jmp mode_title
.endproc
;
; tracks
;
.proc track_sprite
jsr oam_clear
STB oam+(0*4)+1, #eConst::SPRITE_CHOOSE
STB oam+(0*4)+2, #2
STB oam+(0*4)+3, #(eCoord::TRACKS_TRACK_X-2)*8
lda track_choose
asl
asl
asl
clc
adc #(eCoord::TRACKS_TRACK_Y*8)-1
sta oam+(0*4)+0
rts
.endproc
.proc mode_tracks
; silence
lda #0
sta $4015
sta $4011
; load the screen
lda #eScreen::TRACKS
jsr load_screen
PPU_LATCH PPU_TILE(eCoord::TRACKS_TITLE_X, eCoord::TRACKS_TITLE_Y)
PTR_LOAD dString::title
jsr ppu_string
PPU_LATCH PPU_TILE(eCoord::TRACKS_ARTIST_X, eCoord::TRACKS_ARTIST_Y)
PTR_LOAD dString::artist
jsr ppu_string
PPU_LATCH PPU_TILE(eCoord::TRACKS_COPYRIGHT_X, eCoord::TRACKS_COPYRIGHT_Y)
PTR_LOAD dString::copyright
jsr ppu_string
TRACK_ADDR = PPU_TILE(eCoord::TRACKS_TRACK_X, eCoord::TRACKS_TRACK_Y)
lda #>TRACK_ADDR
sta temp+1
sta $2006
lda #<TRACK_ADDR
sta temp+0
sta $2006
ldx #0
:
lda dTrack::string_table+0, X
sta ptr+0
lda dTrack::string_table+1, X
sta ptr+1
inx
inx
jsr ppu_string
cpx #(eNSF::TRACKS * 2)
bcs :+
jsr ppu_temp_line
jmp :-
:
@loop:
lda gamepad
sta gamepad_last
jsr track_sprite
jsr render_on
jsr gamepad_poll
lda gamepad_new
and #(PAD_L | PAD_U)
beq :+
lda track_choose
beq @move_end
dec track_choose
jmp @move_end
:
lda gamepad_new
and #(PAD_R | PAD_D)
beq :+
lda track_choose
cmp #(eNSF::TRACKS-1)
bcs @move_end
inc track_choose
jmp @move_end
:
@move_end:
lda gamepad_new
and #(PAD_SELECT)
beq :+
jmp mode_title
:
lda gamepad_new
and #(PAD_A | PAD_START)
beq :+
lda #1
sta play_through
jmp mode_play
:
lda gamepad_new
and #(PAD_B)
beq :+
lda #0
sta play_through
jmp mode_play
:
jmp @loop
.endproc
;
; play
;
.proc play_sprite
; timer
lda min1
clc
adc #eConst::SPRITE_ZERO
sta oam+(0*4)+1
lda min0
clc
adc #eConst::SPRITE_ZERO
sta oam+(1*4)+1
lda sec1
clc
adc #eConst::SPRITE_ZERO
sta oam+(3*4)+1
lda sec0
clc
adc #eConst::SPRITE_ZERO
sta oam+(4*4)+1
; playback indicator
lda play_paused
cmp #2
bcc :+
lda #eConst::SPRITE_STOP
jmp @indicator
:
cmp #1
bcc :+
lda #eConst::SPRITE_PAUSE
jmp @indicator
:
lda play_through
beq :+
lda #eConst::SPRITE_PLAY_ALL
jmp @indicator
:
lda #eConst::SPRITE_PLAY
;jmp @indicator
@indicator:
sta oam+(5*4)+1
rts
.endproc
.proc play_sprite_init
; position all 6 sprites
STB temp, #eCoord::PLAY_TIME_X+(0*8)
ldx #0
:
lda #eCoord::PLAY_TIME_Y-1
sta oam+0, X
lda #2
sta oam+2, X
lda temp
sta oam+3, X
clc
adc #8
sta temp
inx
inx
inx
inx
cpx #(6*4)
bcc :-
lda temp
sta oam+(5*4)+3
STB oam+(2*4)+1, #eConst::SPRITE_COLON
jmp play_sprite
.endproc
.proc mode_play
; silence
lda #0
sta $4015
sta $4011
; load the screen
lda #eScreen::PLAY
jsr load_screen
PPU_LATCH PPU_TILE(eCoord::PLAY_TRACK_X, eCoord::PLAY_TRACK_Y)
lda track_choose
asl
tax
lda dTrack::string_table+0, X
sta ptr+0
lda dTrack::string_table+1, X
sta ptr+1
jsr ppu_string
jsr play_sprite_init
jsr play_init
@loop:
jsr play_sprite
jsr render_on
jsr play_play
jsr gamepad_poll
lda play_paused
cmp #2
bcs @stopped
@playing:
lda gamepad_new
and #(PAD_START)
beq :+
lda play_paused
eor #1
jsr play_pause
:
lda gamepad_new
and #(PAD_B)
beq :+
lda #0
jsr play_pause
lda #0
sta play_through
:
lda gamepad_new
and #(PAD_A)
beq :+
lda #0
jsr play_pause
lda #1
sta play_through
:
;jmp @shared
@shared:
lda gamepad_new
and #(PAD_SELECT)
beq :+
jmp mode_tracks
:
lda gamepad_new
and #(PAD_L | PAD_U)
beq :+
lda track_choose
beq :+
dec track_choose
jmp mode_play
:
lda gamepad_new
and #(PAD_R | PAD_D)
beq :+
lda track_choose
cmp #(eNSF::TRACKS-1)
bcs :+
inc track_choose
jmp mode_play
:
jmp @loop
@stopped:
lda play_through
beq :+
@play_next:
lda track_choose
cmp #(eNSF::TRACKS-1)
bcs :+
; auto-advance to next track
inc track_choose
jmp mode_play
:
lda gamepad_new
and #(PAD_START)
beq :+
jmp mode_play
:
lda gamepad_new
and #(PAD_B)
beq :+
lda #0
sta play_through
jmp mode_play
:
lda gamepad_new
and #(PAD_A)
beq :+
lda #1
sta play_through
jmp mode_play
:
jmp @shared
.endproc
;
; player
;
.proc play_init
; NSF pre-initialize
; RAM init
lda #0
tax
:
sta $00, X
inx
cpx #$FC
bcc :-
;lda #0
tax
:
sta $0200, X
sta $0300, X
sta $0400, X
sta $0500, X
inx
bne :-
; APU init
;lda #0
;tax
:
sta $4000, X
inx
cpx #$14
bcc :-
STB $4015, #$0F
STB $4017, #$40
; player init
lda #0
sta frame
sta sec0
sta sec1
sta min0
sta min1
sta play_paused
sta play_secs+0
sta play_secs+1
lda track_choose
asl
tax
lda dTrack::length_table+0, X
sta play_len+0
lda dTrack::length_table+1, X
sta play_len+1
; call NSF INIT
ldx track_choose
lda dTrack::song_table, X
jmp ramcode_nsf_init
.endproc
.proc play_play
lda play_paused
beq :+
rts
:
jsr ramcode_nsf_play
inc frame
lda frame
cmp fps
bcc @second_end
; increment timer display
lda #0
sta frame
inc sec0
lda sec0
cmp #10
bcc :+
lda #0
sta sec0
inc sec1
lda sec1
cmp #6
bcc :+
lda #0
sta sec1
inc min0
lda min0
cmp #10
bcc :+
lda #0
sta min0
inc min1
lda min1
cmp #10
bcc :+
lda #9
sta min1
sta min0
sta sec0
lda #5
sta sec1
:
; increment track timer
inc play_secs+0
bne :+
inc play_secs+1
:
lda play_secs+0
cmp play_len+0
lda play_secs+1
sbc play_len+1
bcc @second_end
lda play_len+0
sta play_secs+0
lda play_len+1
sta play_secs+1
; stop and silence
lda #2
sta play_paused
lda #0
sta $4015
@second_end:
rts
.endproc
; A = pause
.proc play_pause
cmp play_paused
bne :+
rts
:
cmp #0
bne @pause
@unpause:
lda #$0F
sta $4015
lda #0
sta play_paused
rts
@pause:
lda #0
sta $4015
lda #1
sta play_paused
rts
.endproc
.segment "RAMCODE"
.if (MAPPER = 31)
ramcode_reset:
lda #$FF
sta $5FFF
jmp vec_reset
ramcode_nmi:
inc nmi_count
ramcode_irq:
rti
.endif
; A = track to play
.proc ramcode_nsf_init
.if (::MAPPER = 31)
pha
; setup NSF banks
.repeat 8, I
STB $5FF8+I, dNSF::bank+I
.endrepeat
pla
.endif
ldx pal
ldy #0
jsr eNSF::INIT
jmp ramcode_return
.endproc
.proc ramcode_nsf_play
.if (::MAPPER = 31)
; restore NSF high bank
lda #eNSF::BANK_F000
sta $5FFF
.endif
lda #0
tax
tay
jsr eNSF::PLAY
jmp ramcode_return
.endproc
.proc ramcode_return
.if (::MAPPER = 31)
; restore EZNSF to bank $F000
lda #$FF
sta $5FFF
.endif
rts
.endproc
.segment "CODE"
.import __RAMCODE_SIZE__
.import __RAMCODE_LOAD__
.import __RAMCODE_RUN__
.proc load_ramcode
.assert (__RAMCODE_SIZE__ < 256), error, "RAMCODE segment is too large."
.assert (__RAMCODE_SIZE__ > 0), error, "RAMCODE segment empty."
ldx #0
:
lda __RAMCODE_LOAD__, X
sta __RAMCODE_RUN__, X
inx
cpx #<__RAMCODE_SIZE__
bcc :-
rts
.endproc
;
; main
;
.proc main
; clear second nametable (never used)
PPU_LATCH $2400
ldy #4
lda #0
tax
:
sta $2007
inx
bne :-
dey
bne :-
jmp mode_title
.endproc
;
; various helpful functions
;
; auto-incrementing read
; ptr = pointer to be read from
; Y = 0
.proc read_ptr
lda (ptr), Y
inc ptr+0
bne :+
inc ptr+1
:
cmp #0
rts
.endproc
; A = ePPU index to a PPU data chunk
; data will be unpacked and written directly to $2007
.proc ppu_unpack
asl
tax
lda dPPU::data_table+0, X
sta ptr+0
lda dPPU::data_table+1, X
sta ptr+1
; RLE format
; 1. 0 = RLE data follow, 1-255 = this many bytes of uncompressed data follows (return to 1)
; 2. 0 = data stream is finished, 1-255 = this any bytes of the same byte follows
; 3. byte to repeated number of times specified in 2, return to 1
ldy #0
@rle_loop:
jsr read_ptr
beq @compressed
@uncompressed:
tax
:
jsr read_ptr
sta $2007
dex
bne :-
jmp @rle_loop
@compressed:
jsr read_ptr
beq @finished
tax
jsr read_ptr
:
sta $2007
dex
bne :-
jmp @rle_loop
@finished:
rts
.endproc
; ptr = null or newline terminated string to write to screen
.proc ppu_string
ldy #0
:
jsr read_ptr
beq :+
cmp #13 ; newline
beq :+
sta $2007
jmp :-
:
rts
.endproc
; A = eScreen to load
.proc load_screen
sta temp
jsr load_ppu_banks
jsr render_off
; palettes first so they'll be within vblank
PPU_LATCH $3F00
ldx temp
lda dScreen::pal0_table, X
jsr ppu_unpack
ldx temp
lda dScreen::pal1_table, X
jsr ppu_unpack
PPU_LATCH $0000
ldx temp
lda dScreen::chr0_table, X
jsr ppu_unpack
ldx temp
lda dScreen::chr1_table, X
jsr ppu_unpack
PPU_LATCH $2000
ldx temp
lda dScreen::name_table, X
jsr ppu_unpack
rts
.endproc
.proc oam_clear
lda #$FF
ldx #0
:
sta oam, X
inx
inx
inx
inx
bne :-
rts
.endproc
.proc ppu_temp_line
lda temp+0
clc
adc #<32
sta temp+0
lda temp+1
adc #>32
sta temp+1
sta $2006
lda temp+0
sta $2006
rts
.endproc
.proc wait_nmi
lda #%10000000
sta $2000 ; ensure NMI is running before waiting on it
lda nmi_count
:
cmp nmi_count
beq :-
rts
.endproc
.proc render_off
jsr wait_nmi
lda #0
sta $2001
rts
.endproc
.proc render_on
jsr wait_nmi
; update sprites
lda #0
sta $2003
lda #>oam
sta $4014
; set scroll
lda $2002
lda #0
sta $2005
sta $2005
; turn on rendering
lda #%00011110
sta $2001
rts
.endproc
;
; gamepad
;
.proc gamepad_poll
; remember last state
lda gamepad
sta gamepad_last
; latch the current controller state
lda #1
sta $4016
lda #0
sta $4016
; store high bit in gamepad to mark end of read
lda #%10000000
sta gamepad
; read 8 bits from controller port
:
lda $4016
and #%00000011
cmp #%00000001
ror gamepad
bcc :-
; DPCM conflict may have corrupted first read, test again to make sure
@reread:
lda gamepad
pha ; store previous read on the stack
lda #1
sta $4016
lda #0
sta $4016
lda #%10000000
sta gamepad
:
lda $4016
and #%00000011
cmp #%00000001
ror gamepad
bcc :-
pla ; pop the first read to compare
cmp gamepad
bne @reread
; store buttons pressed this frame
lda gamepad_last
eor gamepad
and gamepad
sta gamepad_new
rts
.endproc
;
; vector handlers
;
.proc vec_reset
; standard startup
sei ; set interrupt flag (unnecessary, unless reset is called from code)
cld ; disable decimal mode
ldx #$40
stx $4017 ; disable APU IRQ
ldx #$ff
txs ; set up stack
ldx #$00
stx $2000 ; disable NMI
stx $2001 ; disable render
stx $4010 ; disable DPCM IRQ
stx $4015 ; mute APU
;
bit $2002 ; clear vblank flag
; wait for vblank
:
bit $2002
bpl :-
; clear memory
ldx #$00
:
lda #$00
sta $0000, X
sta $0100, X
sta $0200, X
sta $0300, X
sta $0500, X
sta $0600, X
sta $0700, X
lda #$FF ; OAM clear
sta $0400, X
inx
bne :-
; wait for second vblank
:
bit $2002
bpl :-
; PPU is now warmed up, NES is ready to go!
jsr load_ramcode
; detect NTSC/PAL
lda $2002
lda #%10000000
sta $2000
jsr detect_region
bne :+
lda #60
sta fps
lda #0
jmp :++
:
lda #50
sta fps
lda #1
:
sta pal
; leave NMI on forever and begin
jmp main
.endproc
vec_nmi:
inc nmi_count
vec_irq:
rti
;
; mapper 31
;
.if (MAPPER = 31)
dPPU::data_base = $8000
INES_CHR = 0 ; CHR RAM
.segment "CODE"
.proc load_ppu_banks
; prepare PPU data at the correct location
ldx #eNSF::BANKS
stx $5FF8
inx
stx $5FF9
inx
stx $5FFA
inx
stx $5FFB
inx
stx $5FFC
inx
stx $5FFD
inx
stx $5FFE
rts
.endproc
.segment "NSF_F000"
.incbin NSF_F000
.segment "NSF_VECTORS"
.addr ramcode_nmi
.addr ramcode_reset
.addr ramcode_irq