-
Notifications
You must be signed in to change notification settings - Fork 0
/
neslib.s
1606 lines (1098 loc) · 19.9 KB
/
neslib.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
;NES hardware-dependent functions by Shiru (shiru@mail.ru)
;with improvements by VEG
;Feel free to do anything you want with this code, consider it Public Domain
;modified to work with the FamiTracker music driver
.export _pal_all,_pal_bg,_pal_spr,_pal_col,_pal_clear
.export _pal_bright,_pal_spr_bright,_pal_bg_bright
.export _ppu_off,_ppu_on_all,_ppu_on_bg,_ppu_on_spr,_ppu_mask,_ppu_system
.export _oam_clear,_oam_size,_oam_spr,_oam_meta_spr,_oam_hide_rest
.export _ppu_wait_frame,_ppu_wait_nmi
.export _scroll,_split
.export _bank_spr,_bank_bg
.export _vram_read,_vram_write
.export _music_play,_music_stop,_music_pause
.export _sfx_play
.export _pad_poll,_pad_trigger,_pad_state
.export _rand8,_rand16,_set_rand
.export _vram_adr,_vram_put,_vram_fill,_vram_inc,_vram_unrle
.export _set_vram_update,_flush_vram_update
.export _memcpy,_memfill,_delay
;NMI handler
nmi:
pha
txa
pha
tya
pha
lda <PPU_MASK_VAR ;if rendering is disabled, do not access the VRAM at all
and #%00011000
bne @doUpdate
jmp @skipAll
@doUpdate:
lda #>OAM_BUF ;update OAM
sta PPU_OAM_DMA
lda <PAL_UPDATE ;update palette if needed
bne @updPal
jmp @updVRAM
@updPal:
ldx #0
stx <PAL_UPDATE
lda #$3f
sta PPU_ADDR
stx PPU_ADDR
ldy PAL_BUF ;background color, remember it in X
lda (PAL_BG_PTR),y
sta PPU_DATA
tax
.repeat 3,I
ldy PAL_BUF+1+I
lda (PAL_BG_PTR),y
sta PPU_DATA
.endrepeat
.repeat 3,J
stx PPU_DATA ;background color
.repeat 3,I
ldy PAL_BUF+5+(J*4)+I
lda (PAL_BG_PTR),y
sta PPU_DATA
.endrepeat
.endrepeat
.repeat 4,J
stx PPU_DATA ;background color
.repeat 3,I
ldy PAL_BUF+17+(J*4)+I
lda (PAL_SPR_PTR),y
sta PPU_DATA
.endrepeat
.endrepeat
@updVRAM:
lda <VRAM_UPDATE
beq @skipUpd
lda #0
sta <VRAM_UPDATE
lda <NAME_UPD_ENABLE
beq @skipUpd
jsr _flush_vram_update_nmi
@skipUpd:
lda #0
sta PPU_ADDR
sta PPU_ADDR
lda <SCROLL_X
sta PPU_SCROLL
lda <SCROLL_Y
sta PPU_SCROLL
lda <PPU_CTRL_VAR
sta PPU_CTRL
@skipAll:
lda <PPU_MASK_VAR
sta PPU_MASK
inc <FRAME_CNT1
inc <FRAME_CNT2
lda <FRAME_CNT2
cmp #6
bne @skipNtsc
lda #0
sta <FRAME_CNT2
@skipNtsc:
;play music, the code is modified to put data into output buffer instead of APU registers
lda <MUSIC_PLAY
ror
bcc :+
jsr ft_music_play
jmp :++
:
lda #$30 ;mute channels when music does not play
sta <BUF_4000
sta <BUF_4004
sta <BUF_400C
lda #$00
sta <BUF_4008
:
;process all sound effect streams
.if(FT_SFX_ENABLE)
.if FT_SFX_STREAMS>0
ldx #FT_SFX_CH0
jsr _FT2SfxUpdate
.endif
.if FT_SFX_STREAMS>1
ldx #FT_SFX_CH1
jsr _FT2SfxUpdate
.endif
.if FT_SFX_STREAMS>2
ldx #FT_SFX_CH2
jsr _FT2SfxUpdate
.endif
.if FT_SFX_STREAMS>3
ldx #FT_SFX_CH3
jsr _FT2SfxUpdate
.endif
.endif
;send data from the output buffer to the APU
lda <BUF_4000
sta $4000
lda <BUF_4001
sta $4001
lda <BUF_4002
sta $4002
lda <BUF_4003
cmp <PREV_4003
beq :+
sta <PREV_4003
sta $4003
:
lda <BUF_4004
sta $4004
lda <BUF_4005
sta $4005
lda <BUF_4006
sta $4006
lda <BUF_4007
cmp <PREV_4007
beq :+
sta <PREV_4007
sta $4007
:
lda <BUF_4008
sta $4008
lda <BUF_4009
sta $4009
lda <BUF_400A
sta $400A
lda <BUF_400B
sta $400B
lda <BUF_400C
sta $400C
lda <BUF_400D
sta $400D
lda <BUF_400E
sta $400E
lda <BUF_400F
sta $400F
pla
tay
pla
tax
pla
irq:
rti
;famitone sound effects code and structures
FT_VARS=FT_BASE_ADR
.if(FT_PAL_SUPPORT)
.if(FT_NTSC_SUPPORT)
FT_PITCH_FIX = (FT_PAL_SUPPORT|FT_NTSC_SUPPORT) ;add PAL/NTSC pitch correction code only when both modes are enabled
.endif
.endif
;zero page variables
FT_TEMP_PTR = FT_TEMP ;word
FT_TEMP_PTR_L = FT_TEMP_PTR+0
FT_TEMP_PTR_H = FT_TEMP_PTR+1
FT_TEMP_VAR1 = FT_TEMP+2
;sound effect stream variables, 2 bytes and 15 bytes per stream
;when sound effects are disabled, this memory is not used
FT_PAL_ADJUST = FT_VARS+0
FT_SFX_ADR_L = FT_VARS+1
FT_SFX_ADR_H = FT_VARS+2
FT_SFX_BASE_ADR = FT_VARS+3
FT_SFX_STRUCT_SIZE = 15
FT_SFX_REPEAT = FT_SFX_BASE_ADR+0
FT_SFX_PTR_L = FT_SFX_BASE_ADR+1
FT_SFX_PTR_H = FT_SFX_BASE_ADR+2
FT_SFX_OFF = FT_SFX_BASE_ADR+3
FT_SFX_BUF = FT_SFX_BASE_ADR+4 ;11 bytes
;aliases for sound effect channels to use in user calls
FT_SFX_CH0 = FT_SFX_STRUCT_SIZE*0
FT_SFX_CH1 = FT_SFX_STRUCT_SIZE*1
FT_SFX_CH2 = FT_SFX_STRUCT_SIZE*2
FT_SFX_CH3 = FT_SFX_STRUCT_SIZE*3
.if(FT_SFX_ENABLE)
;------------------------------------------------------------------------------
; init sound effects player, set pointer to data
; in: X,Y is address of sound effects data
;------------------------------------------------------------------------------
FamiToneSfxInit:
stx <TEMP+0
sty <TEMP+1
ldy #0
.if(FT_PITCH_FIX)
lda FT_PAL_ADJUST ;add 2 to the sound list pointer for PAL
bne @ntsc
iny
iny
@ntsc:
.endif
lda (TEMP),y ;read and store pointer to the effects list
sta FT_SFX_ADR_L
iny
lda (TEMP),y
sta FT_SFX_ADR_H
ldx #FT_SFX_CH0 ;init all the streams
@set_channels:
jsr _FT2SfxClearChannel
txa
clc
adc #FT_SFX_STRUCT_SIZE
tax
cpx #FT_SFX_CH0+FT_SFX_STRUCT_SIZE*FT_SFX_STREAMS
bne @set_channels
rts
;internal routine, clears output buffer of a sound effect
;in: A is 0
; X is offset of sound effect stream
_FT2SfxClearChannel:
lda #0
sta FT_SFX_PTR_H,x ;this stops the effect
sta FT_SFX_REPEAT,x
sta FT_SFX_OFF,x
sta FT_SFX_BUF+6,x ;mute triangle
lda #$30
sta FT_SFX_BUF+0,x ;mute pulse1
sta FT_SFX_BUF+3,x ;mute pulse2
sta FT_SFX_BUF+9,x ;mute noise
rts
;------------------------------------------------------------------------------
; play sound effect
; in: A is a number of the sound effect 0..127
; X is offset of sound effect channel, should be FT_SFX_CH0..FT_SFX_CH3
;------------------------------------------------------------------------------
FamiToneSfxPlay:
asl a ;get offset in the effects list
tay
jsr _FT2SfxClearChannel ;stops the effect if it plays
lda FT_SFX_ADR_L
sta <TEMP+0
lda FT_SFX_ADR_H
sta <TEMP+1
lda (TEMP),y ;read effect pointer from the table
sta FT_SFX_PTR_L,x ;store it
iny
lda (TEMP),y
sta FT_SFX_PTR_H,x ;this write enables the effect
rts
;internal routine, update one sound effect stream
;in: X is offset of sound effect stream
_FT2SfxUpdate:
lda FT_SFX_REPEAT,x ;check if repeat counter is not zero
beq @no_repeat
dec FT_SFX_REPEAT,x ;decrement and return
bne @update_buf ;just mix with output buffer
@no_repeat:
lda FT_SFX_PTR_H,x ;check if MSB of the pointer is not zero
bne @sfx_active
rts ;return otherwise, no active effect
@sfx_active:
sta <FT_TEMP_PTR_H ;load effect pointer into temp
lda FT_SFX_PTR_L,x
sta <FT_TEMP_PTR_L
ldy FT_SFX_OFF,x
clc
@read_byte:
lda (FT_TEMP_PTR),y ;read byte of effect
bmi @get_data ;if bit 7 is set, it is a register write
beq @eof
iny
sta FT_SFX_REPEAT,x ;if bit 7 is reset, it is number of repeats
tya
sta FT_SFX_OFF,x
jmp @update_buf
@get_data:
iny
stx <FT_TEMP_VAR1 ;it is a register write
adc <FT_TEMP_VAR1 ;get offset in the effect output buffer
and #$7f
tax
lda (FT_TEMP_PTR),y ;read value
iny
sta FT_SFX_BUF,x ;store into output buffer
ldx <FT_TEMP_VAR1
jmp @read_byte ;and read next byte
@eof:
sta FT_SFX_PTR_H,x ;mark channel as inactive
@update_buf:
lda <BUF_4000 ;compare effect output buffer with main output buffer
and #$0f ;if volume of pulse 1 of effect is higher than that of the
sta <FT_TEMP_VAR1 ;main buffer, overwrite the main buffer value with the new one
lda FT_SFX_BUF+0,x
and #$0f
cmp <FT_TEMP_VAR1
bcc @no_pulse1
lda FT_SFX_BUF+0,x
sta <BUF_4000
lda FT_SFX_BUF+1,x
sta <BUF_4002
lda FT_SFX_BUF+2,x
sta <BUF_4003
@no_pulse1:
lda <BUF_4004 ;same for pulse 2
and #$0f
sta <FT_TEMP_VAR1
lda FT_SFX_BUF+3,x
and #$0f
cmp <FT_TEMP_VAR1
bcc @no_pulse2
lda FT_SFX_BUF+3,x
sta <BUF_4004
lda FT_SFX_BUF+4,x
sta <BUF_4006
lda FT_SFX_BUF+5,x
sta <BUF_4007
@no_pulse2:
lda FT_SFX_BUF+6,x ;overwrite triangle of main output buffer if it is active
beq @no_triangle
sta <BUF_4008
lda FT_SFX_BUF+7,x
sta <BUF_400A
lda FT_SFX_BUF+8,x
sta <BUF_400B
@no_triangle:
lda <BUF_400C ;same as for pulse 1 and 2, but for noise
and #$0f
sta <FT_TEMP_VAR1
lda FT_SFX_BUF+9,x
and #$0f
cmp <FT_TEMP_VAR1
bcc @no_noise
lda FT_SFX_BUF+9,x
sta <BUF_400C
lda FT_SFX_BUF+10,x
sta <BUF_400E
@no_noise:
rts
.endif
;void __fastcall__ pal_all(const char *data);
_pal_all:
sta <PTR
stx <PTR+1
ldx #$00
lda #$20
pal_copy:
sta <LEN
ldy #$00
@0:
lda (PTR),y
sta PAL_BUF,x
inx
iny
dec <LEN
bne @0
inc <PAL_UPDATE
rts
;void __fastcall__ pal_bg(const char *data);
_pal_bg:
sta <PTR
stx <PTR+1
ldx #$00
lda #$10
bne pal_copy ;bra
;void __fastcall__ pal_spr(const char *data);
_pal_spr:
sta <PTR
stx <PTR+1
ldx #$10
txa
bne pal_copy ;bra
;void __fastcall__ pal_col(unsigned char index,unsigned char color);
_pal_col:
sta <PTR
jsr popa
and #$1f
tax
lda <PTR
sta PAL_BUF,x
inc <PAL_UPDATE
rts
;void __fastcall__ pal_clear(void);
_pal_clear:
lda #$0f
ldx #0
@1:
sta PAL_BUF,x
inx
cpx #$20
bne @1
stx <PAL_UPDATE
rts
;void __fastcall__ pal_spr_bright(unsigned char bright);
_pal_spr_bright:
tax
lda palBrightTableL,x
sta <PAL_SPR_PTR
lda palBrightTableH,x ;MSB is never zero
sta <PAL_SPR_PTR+1
sta <PAL_UPDATE
rts
;void __fastcall__ pal_bg_bright(unsigned char bright);
_pal_bg_bright:
tax
lda palBrightTableL,x
sta <PAL_BG_PTR
lda palBrightTableH,x ;MSB is never zero
sta <PAL_BG_PTR+1
sta <PAL_UPDATE
rts
;void __fastcall__ pal_bright(unsigned char bright);
_pal_bright:
jsr _pal_spr_bright
txa
jmp _pal_bg_bright
;void __fastcall__ ppu_off(void);
_ppu_off:
lda <PPU_MASK_VAR
and #%11100111
sta <PPU_MASK_VAR
jmp _ppu_wait_nmi
;void __fastcall__ ppu_on_all(void);
_ppu_on_all:
lda <PPU_MASK_VAR
ora #%00011000
ppu_onoff:
sta <PPU_MASK_VAR
jmp _ppu_wait_nmi
;void __fastcall__ ppu_on_bg(void);
_ppu_on_bg:
lda <PPU_MASK_VAR
ora #%00001000
bne ppu_onoff ;bra
;void __fastcall__ ppu_on_spr(void);
_ppu_on_spr:
lda <PPU_MASK_VAR
ora #%00010000
bne ppu_onoff ;bra
;void __fastcall__ ppu_mask(unsigned char mask);
_ppu_mask:
sta <PPU_MASK_VAR
rts
;unsigned char __fastcall__ ppu_system(void);
_ppu_system:
lda <NTSC_MODE
rts
;void __fastcall__ oam_clear(void);
_oam_clear:
ldx #0
lda #$ff
@1:
sta OAM_BUF,x
inx
inx
inx
inx
bne @1
rts
;void __fastcall__ oam_size(unsigned char size);
_oam_size:
asl a
asl a
asl a
asl a
asl a
and #$20
sta <TEMP
lda <PPU_CTRL_VAR
and #$df
ora <TEMP
sta <PPU_CTRL_VAR
rts
;unsigned char __fastcall__ oam_spr(unsigned char x,unsigned char y,unsigned char chrnum,unsigned char attr,unsigned char sprid);
_oam_spr:
tax
ldy #0 ;four popa calls replacement
lda (sp),y
iny
sta OAM_BUF+2,x
lda (sp),y
iny
sta OAM_BUF+1,x
lda (sp),y
iny
sta OAM_BUF+0,x
lda (sp),y
sta OAM_BUF+3,x
lda <sp
clc
adc #4
sta <sp
bcc @1
inc <sp+1
@1:
txa
clc
adc #4
rts
;unsigned char __fastcall__ oam_meta_spr(unsigned char x,unsigned char y,unsigned char sprid,const unsigned char *data);
_oam_meta_spr:
sta <PTR
stx <PTR+1
ldy #2 ;three popa calls replacement, performed in reversed order
lda (sp),y
dey
sta <SCRX
lda (sp),y
dey
sta <SCRY
lda (sp),y
tax
@1:
lda (PTR),y ;x offset
cmp #$80
beq @2
iny
clc
adc <SCRX
sta OAM_BUF+3,x
lda (PTR),y ;y offset
iny
clc
adc <SCRY
sta OAM_BUF+0,x
lda (PTR),y ;tile
iny
sta OAM_BUF+1,x
lda (PTR),y ;attribute
iny
sta OAM_BUF+2,x
inx
inx
inx
inx
jmp @1
@2:
lda <sp
adc #2 ;carry is always set here, so it adds 3
sta <sp
bcc @3
inc <sp+1
@3:
txa
rts
;void __fastcall__ oam_hide_rest(unsigned char sprid);
_oam_hide_rest:
tax
lda #240
@1:
sta OAM_BUF,x
inx
inx
inx
inx
bne @1
rts
;void __fastcall__ ppu_wait_frame(void);
_ppu_wait_frame:
lda #1
sta <VRAM_UPDATE
lda <FRAME_CNT1
@1:
cmp <FRAME_CNT1
beq @1
lda <NTSC_MODE
beq @3
@2:
lda <FRAME_CNT2
cmp #5
beq @2
@3:
rts
;void __fastcall__ ppu_wait_nmi(void);
_ppu_wait_nmi:
lda #1
sta <VRAM_UPDATE
lda <FRAME_CNT1
@1:
cmp <FRAME_CNT1
beq @1
rts
;void __fastcall__ vram_unrle(const unsigned char *data);
_vram_unrle:
tay
stx <RLE_HIGH
lda #0
sta <RLE_LOW
lda (RLE_LOW),y
sta <RLE_TAG
iny
bne @1
inc <RLE_HIGH
@1:
lda (RLE_LOW),y
iny
bne @11
inc <RLE_HIGH
@11:
cmp <RLE_TAG
beq @2
sta PPU_DATA
sta <RLE_BYTE
bne @1
@2:
lda (RLE_LOW),y
beq @4
iny
bne @21
inc <RLE_HIGH
@21:
tax
lda <RLE_BYTE
@3:
sta PPU_DATA
dex
bne @3
beq @1
@4:
rts
;void __fastcall__ scroll(unsigned int x,unsigned int y);
_scroll:
sta <TEMP
txa
bne @1
lda <TEMP
cmp #240
bcs @1
sta <SCROLL_Y
lda #0
sta <TEMP
beq @2 ;bra
@1:
sec
lda <TEMP
sbc #240
sta <SCROLL_Y
lda #2
sta <TEMP
@2:
jsr popax
sta <SCROLL_X
txa
and #$01
ora <TEMP
sta <TEMP
lda <PPU_CTRL_VAR
and #$fc
ora <TEMP
sta <PPU_CTRL_VAR
rts
;;void __fastcall__ split(unsigned int x,unsigned int y);
_split:
jsr popax
sta <SCROLL_X1
txa
and #$01
sta <TEMP
lda <PPU_CTRL_VAR
and #$fc
ora <TEMP
sta <PPU_CTRL_VAR1
@3:
bit PPU_STATUS
bvs @3
@4:
bit PPU_STATUS
bvc @4
lda <SCROLL_X1
sta PPU_SCROLL
lda #0
sta PPU_SCROLL
lda <PPU_CTRL_VAR1
sta PPU_CTRL
rts
;void __fastcall__ bank_spr(unsigned char n);
_bank_spr:
and #$01
asl a
asl a
asl a
sta <TEMP
lda <PPU_CTRL_VAR
and #%11110111
ora <TEMP
sta <PPU_CTRL_VAR
rts
;void __fastcall__ bank_bg(unsigned char n);
_bank_bg:
and #$01