-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtad-audio.s
More file actions
1512 lines (1186 loc) · 39.4 KB
/
tad-audio.s
File metadata and controls
1512 lines (1186 loc) · 39.4 KB
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
;; Terrific Audio Driver ca65 API
; This file MUST be recompiled if the memory map changes.
;
; SPDX-FileCopyrightText: © 2024 Marcus Rowe <undisbeliever@gmail.com>
; SPDX-License-Identifier: Zlib
;
; Copyright © 2024 Marcus Rowe <undisbeliever@gmail.com>
;
; This software is provided 'as-is', without any express or implied warranty. In
; no event will the authors be held liable for any damages arising from the use of
; this software.
;
; Permission is granted to anyone to use this software for any purpose, including
; commercial applications, and to alter it and redistribute it freely, subject to
; the following restrictions:
;
; 1. The origin of this software must not be misrepresented; you must not
; claim that you wrote the original software. If you use this software in
; a product, an acknowledgment in the product documentation would be
; appreciated but is not required.
;
; 2. Altered source versions must be plainly marked as such, and must not be
; misrepresented as being the original software.
;
; 3. This notice may not be removed or altered from any source distribution.
.setcpu "65816"
.smart
; Ensure autoimport is disabled
.autoimport -
.export Tad_Init : far, Tad_Process : far, Tad_FinishLoadingData : far
.export Tad_QueueCommand, Tad_QueueCommandOverride
.export Tad_QueuePannedSoundEffect, Tad_QueueSoundEffect
.export Tad_LoadSong, Tad_LoadSongIfChanged, Tad_GetSong, Tad_ReloadCommonAudioData
.export Tad_SongsStartImmediately, Tad_SongsStartPaused
.export Tad_GlobalVolumesResetOnSongStart, Tad_GlobalVolumesPersist
.export Tad_SetTransferSize
.export Tad_IsLoaderActive, Tad_IsSongLoaded, Tad_IsSfxPlaying, Tad_IsSongPlaying
.exportzp Tad_sfxQueue_sfx, Tad_sfxQueue_pan
.export Tad_flags, Tad_audioMode
;; =======
;; DEFINES
;; =======
;; Memory Map
;; ----------
;;
;; `tad-audio.s` requires either a `LOROM` or `HIROM` symbol to determine the memory map used by the ROM.
.if .defined(LOROM) && .defined(HIROM)
.error "Cannot use HIROM and LOROM at the same time"
.endif
.if ! (.defined(LOROM) || .defined(HIROM))
.error "Unknown memory map: Missing LOROM or HIROM define"
.endif
;; Segments
;; --------
;;
;; The following optional defines are used to determine the segment to place the code in.
;;
;; * `TAD_PROCESS_SEGMENT` defines the segment to store the subroutines that processes the queues
;; and loads data into Audio-RAM (`Tad_Init`, `Tad_Process`, `Tad_FinishLoadingData`).
;; * The exported subroutines in this segment are called using `JSL` long addressing.
;; * If `TAD_PROCESS_SEGMENT` is undefined, `TAD_CODE_SEGMENT` is used.
;;
;; * `TAD_CODE_SEGMENT` defines the segment to store the remaining subroutines.
;; * The subroutines in this segment are called using `JSR` absolute addressing.
;; * If `TAD_CODE_SEGMENT` is undefined, "CODE" will be used.
;;
;;
;; NOTE: Because ca65 only allows numbers in `-D name=value` command line arguments, the only
;; way to set these defines is to create a new source file that defines `TAD_CODE_SEGMENT`
;; and/or `TAD_PROCESS_SEGMENT` and then includes `tad-audio.s`.
;;
;; For example:
;;
;; .define TAD_CODE_SEGMENT "CODE1"
;; .define TAD_PROCESS_SEGMENT "CODE3"
;;
;; .include "../terrific-audio-driver/audio-driver/ca65-api/tad-audio.s"
;;
.if .not .match({TAD_CODE_SEGMENT}, {""})
.define TAD_CODE_SEGMENT "CODE"
.endif
.if .not .match({TAD_PROCESS_SEGMENT}, {""})
.define TAD_PROCESS_SEGMENT TAD_CODE_SEGMENT
.endif
;; ===========
;; Binary Data
;; ===========
;;
;; These 3 files MUST be embedded (using `.incbin`) into the ROM if the developer uses a custom
;; `LoadAudioData` callback.
;;
;; Terrific Audio Driver spc700 Loader (loader.bin)
.import Tad_Loader_Bin
.importzp Tad_Loader_SIZE
;; Terrific Audio Driver spc700 driver (audio-driver.bin)
.import Tad_AudioDriver_Bin, Tad_AudioDriver_SIZE
.assert Tad_Loader_SIZE > 64 && Tad_Loader_SIZE < 128, lderror, "Invalid Tad_Loader_Bin size"
.assert .bankbyte(Tad_Loader_Bin) = .bankbyte(Tad_Loader_Bin + Tad_Loader_SIZE), lderror, "Tad_Loader_Bin does not fit inside a single bank"
.assert Tad_AudioDriver_SIZE > $600 && Tad_AudioDriver_SIZE < $d00, lderror, "Invalid Tad_AudioDriver_Bin size"
; `Tad_AudioDriver_Bin` can cross bank boundaries
;; =========
;; CALLBACKS
;; =========
;; LoadAudioData callback
;;
;; IN: A = 0 - Common audio data (MUST return carry set)
;; IN: A >= 1 - Song data (might be invalid)
;; OUT: Carry set if input (`A`) was valid
;; OUT: A:X = far address
;; OUT: Y = size
;;
;; Called with JSL long addressing (returns with RTL).
.a8
.i16
;; DB access registers
.import LoadAudioData: Far
;; =========
;; CONSTANTS
;; =========
;; Address to store the loader (in Audio-RAM).
;; Address (in Audio-RAM) to execute after loading the Loader.
;; MUST match LOADER_ADDR in `audio-driver/src/common_memmap.wiz`.
TAD_LOADER_ARAM_ADDR = $0200
;; Minimum transfer size accepted by `Tad_SetTransferSize`
;;
;; MUST BE > 0
TAD_MIN_TRANSFER_PER_FRAME = 32
;; Maximum transfer size accepted by `Tad_SetTransferSize`
;;
;; The loader can transfer ~849 bytes per 60Hz frame SlowROM or FastROM
TAD_MAX_TRANSFER_PER_FRAME = 800
;; ========
;; IO Ports
;; ========
;; IO communication protocol version.
;;
;; Used by `tad-compiler ca65-export` to verify the IO protocol in `tad-audio.s` matches the audio-driver.
;;
;; This constant MUST be increased if `LOADER_ADDR` or the IO Communication protocol changes.
.export TAD_IO_VERSION : abs = 20
; MUST match `audio-driver/src/io-commands.wiz`
.enum TadCommand
PAUSE = 0
PAUSE_MUSIC_PLAY_SFX = 2
UNPAUSE = 4
PLAY_SOUND_EFFECT = 6
STOP_SOUND_EFFECTS = 8
SET_MAIN_VOLUME = 10
SET_MUSIC_CHANNELS = 12
SET_SONG_TIMER = 14
SET_GLOBAL_MUSIC_VOLUME = 16
SET_GLOBAL_SFX_VOLUME = 18
SET_GLOBAL_VOLUMES = 20
.endenum
TAD_MAX_PAN = 128
TAD_CENTER_PAN = TAD_MAX_PAN / 2
;; MUST match `audio-driver/src/io-commands.wiz`
.scope TadIO_ToDriver
;; The command to execute.
;;
;; iiicccci
;; cccc = command
;; i = command id, MUST be different on every command.
;; Used to detect when a new command has been sent to the driver.
;;
;; NOTES:
;; * The command will only be execute if the `command` byte has changed.
;; * This value MUST be written last.
;; * The command and parameter bytes MUST NOT change unless the previous command
;; has been acknowledged.
COMMAND_PORT = $2140 ; APUIO0
COMMAND_MASK = %00011110
COMMAND_I_MASK = %11100001
;; The first command parameter port
PARAMETER0_PORT = $2141 ; APUIO1
;; The second command parameter port
PARAMETER1_PORT = $2142 ; APUIO2
;; Writing `SWITCH_TO_LOADER` to this port should stop execution and start the loader.
;;
;; If the audio-driver is running; if the `SWITCH_TO_LOADER_BIT` is set,
;; the audio driver will stop and execute the loader.
;;
;; If the loader is in the middle of a transfer and both the `SWITCH_TO_LOADER_BIT`
;; and MSB (bit 7) bits are set, the loader will restart.
SWITCH_TO_LOADER_PORT = $2143 ; APUIO3
SWITCH_TO_LOADER_BIT = 5
SWITCH_TO_LOADER = $80 | (1 << SWITCH_TO_LOADER_BIT)
.endscope
;; MUST match `audio-driver/src/io-commands.wiz`
.scope TadIO_ToScpu
;; Audio driver command acknowledgment.
;;
;; Acknowledgment of the `ToDriver.command` byte. Not used in the loader.
;;
;; After the command has been processed, the `IO.ToDriver.command` value will be written to this port.
COMMAND_ACK_PORT = $2140 ; APUIO0
;; The mode the S-SMP is currently executing.
;;
;; Used by both the loader and the audio-driver.
;;
;; NOTE: The IPL sets this value after at has cleared the zero-page.
;; Do not read this value immediately after reset.
;; Make sure enough time has passed for the IPL to set IO Port 1
;; to $bb before reading this port.
MODE_PORT = $2141 ; APUIO1
;; The S-SMP is at the start of the IPL, waiting for the ready signal.
MODE_IPL = $bb
;; The S-SMP is running the loader.
MODE_LOADER = $4c ; 'L', Loader.LOADER_READY_L
;; The S-SMP is running the audio-driver.
MODE_AUDIO_DRIVER = $61 ; 'a'
.endscope
;; MUST match `audio-driver/src/io-commands.wiz`
.scope TadLoaderDataType
CODE = 0
COMMON_DATA = 1
SONG_DATA_FLAG = 1 << 7
PLAY_SONG_FLAG = 1 << 6
RESET_GLOBAL_VOLUMES_FLAG = 1 << 5
STEREO_FLAG = 1 << 1
SURROUND_FLAG = 1 << 0
.endscope
;; MUST match `audio-driver/src/io-commands.wiz`
.scope TadIO_Loader_Init
LOADER_DATA_TYPE_PORT = $2141 ; APUIO1
READY_PORT_L = $2142 ; APUIO2
READY_PORT_H = $2143 ; APUIO3
READY_PORT_HL = $2142 ; APUIO2 & APUIO3
LOADER_READY_L = %01001100 ; 'L'
LOADER_READY_H = %01000100 ; 'D'
LOADER_READY_HL = LOADER_READY_L | (LOADER_READY_H << 8)
.endscope
;; MUST match `audio-driver/src/io-commands.wiz`
.scope TadIO_Loader
DATA_PORT_L = $2141 ; APUIO1
DATA_PORT_H = $2142 ; APUIO2
SPINLOCK_PORT = $2143 ; APUIO3
;; The spinlock value when the audio driver starts playing a song
SPINLOCK_INIT_VALUE = 0
;; Only the lower 4 bits of the spinlock should be set while sending data to the loader
SPINLOCK_MASK = $0f
;; Signal to the loader that the transfer has completed.
SPINLOCK_COMPLETE = $80
;; If this value is written to the spinlock, the loader will restart;
SPINLOCK_SWITCH_TO_LOADER = TadIO_ToDriver::SWITCH_TO_LOADER
.endscope
.enum TadState
NULL = $00
;; Waiting for loader to send the ready signal before loading common-audio-data
WAITING_FOR_LOADER_COMMON = $7b
;; Waiting for loader to send the ready signal before loading song data
WAITING_FOR_LOADER_SONG = $7c
;; Loading common audio data.
LOADING_COMMON_AUDIO_DATA = $7d
;; Loading a song and the TadLoaderDataType::PLAY_SONG_FLAG was clear.
LOADING_SONG_DATA_PAUSED = $7e
;; Loading a song and the TadLoaderDataType::PLAY_SONG_FLAG was set.
LOADING_SONG_DATA_PLAY = $7f
;; Song is loaded into Audio-RAM and the audio driver is paused.
;; No play-sound-effect commands will be sent when the driver is paused.
PAUSED = $80
;; Song is loaded into Audio-RAM and the audio driver is playing sfx (song paused).
PLAYING_SFX = $81
;; Song is loaded into Audio-RAM and the audio driver is playing the song.
PLAYING = $82
.endenum
TAD__FIRST_WAITING_STATE = TadState::WAITING_FOR_LOADER_COMMON
TAD__FIRST_LOADING_STATE = TadState::LOADING_COMMON_AUDIO_DATA
TAD__FIRST_LOADING_SONG_STATE = TadState::LOADING_SONG_DATA_PAUSED
.scope TadFlags
RELOAD_COMMON_AUDIO_DATA = 1 << 7
PLAY_SONG_IMMEDIATELY = 1 << 6
RESET_GLOBAL_VOLUMES_ON_SONG_START = 1 << 5
;; A mask for the flags that are sent to the loader
_ALL_FLAGS = RELOAD_COMMON_AUDIO_DATA | PLAY_SONG_IMMEDIATELY | RESET_GLOBAL_VOLUMES_ON_SONG_START
.endscope
.enum TadAudioMode
MONO = 0
STEREO = 1
SURROUND = 2
.endenum
TAD_N_AUDIO_MODES = 3
;; Default values
;; ==============
; Using a single symbol to enable custom defaults as I am unable to detect if a `.define`
; exists using an if statement.
;
; I recommend using a `.define` for custom defaults so `TadFlags` and `TadAudioMode` values
; can be referenced before they are defined.
.ifndef TAD_CUSTOM_DEFAULTS
;; Default TAD flags
;; MUST NOT set RELOAD_COMMON_AUDIO_DATA
TAD_DEFAULT_FLAGS = TadFlags::PLAY_SONG_IMMEDIATELY
;; Starting audio mode
TAD_DEFAULT_AUDIO_MODE = TadAudioMode::MONO
;; Default number of bytes to transfer to Audio-RAM per `Tad_Process` call.
;;
;; MUST be between the TAD_MIN_TRANSFER_PER_FRAME and TAD_MAX_TRANSFER_PER_FRAME
TAD_DEFAULT_TRANSFER_PER_FRAME = 256
.endif
;; =========
;; Variables
;; =========
.bss
;; The current audio driver state
;; (`TadState` enum)
TadPrivate_state: .res 1
;; `TadFlags` bitfield
;; (see `TadFlags` namespace)
Tad_flags: .res 1
;; Mono/Stereo/Surround audio mode
;; (`TadAudioMode` enum)
Tad_audioMode: .res 1
;; Number of bytes to transfer per `Tad_Process` call
;;
;; MUST be > 0
TadPrivate_bytesToTransferPerFrame: .res 2
;; The previous `TadIO_ToScpu::COMMAND_PORT` sent to the S-SMP audio driver.
TadPrivate_previousCommand: .res 1
;; ---------------------------------------------------
;; Queue 1 - remaining data to transfer into Audio-RAM
;; ---------------------------------------------------
.bss
;; A far pointer to the remaining data to transfer
TadPrivate_dataToTransfer_addr: .res 2
TadPrivate_dataToTransfer_bank: .res 1
;; The remaining number of bytes to transfer
TadPrivate_dataToTransfer_size: .res 2
;; The previous value written to the loader spinLock
TadPrivate_dataToTransfer_prevSpinLock: .res 1
;; ----------------------------------------------
;; Queue 2 - The next song to load into Audio-RAM
;; ----------------------------------------------
.bss
;; The next song to load into Audio-RAM
;; Used by the `WAITING_FOR_LOADER_*` states
;; If this value is 0 or an invalid song, a blank silent song will be loaded instead.
TadPrivate_nextSong: .res 1
;; ------------------------------------------------------
;; Queue 3 - The next command to send to the audio driver
;; ------------------------------------------------------
.bss
;; The next `TadCommand` to send to the audio driver.
;; If this value is negative, the queue is empty.
TadPrivate_nextCommand_id: .res 1
;; The two parameters of the next command (if any)
TadPrivate_nextCommand_parameter0: .res 1
TadPrivate_nextCommand_parameter1: .res 1
;; ---------------------------------------
;; Queue 4 - The next sound effect to play
;; ---------------------------------------
.zeropage
;; see tad-audio.inc
Tad_sfxQueue_sfx: .res 1
Tad_sfxQueue_pan: .res 1
;; Memory Map Asserts
;; ==================
.bss
.assert ((* > $100) && (* < $2000)) || ((* > $7e0100) && (* < $7e2000)), lderror, ".bss is not in lowram"
;; ==================
;; Loader subroutines
;; ==================
.segment TAD_PROCESS_SEGMENT
;; Transfer and execute Loader using the IPL
;;
;; REQUIRES: S-SMP reset and no data has been written to it yet
;;
;; This macro MUST only be called once. There is no way to reset the S-SMP and restart the IPL.
;;
;; A8
;; I16
;; DB access registers
.macro TadPrivate_Loader_TransferLoaderViaIpl
.assert .asize = 8, error
.assert .isize = 16, error
APUIO0 = $2140
APUIO1 = $2141
APUIO2 = $2142
APUIO3 = $2143
; Clear start command port (just in case APUIO0 has $cc in it)
; SOURCE: `blarggapu.s` from lorom-template, originally written by blargg (Shay Green)
stz APUIO0
; Wait for ready signal
ldy #$bbaa
:
cpy APUIO0
bne :-
ldx #TAD_LOADER_ARAM_ADDR
lda #$cc
stx APUIO2 ; destination ARAM address
sta APUIO1 ; non-zero = write data to address
sta APUIO0 ; New data command (non-zero and APUIO0 + more than 2, or $cc on the first transfer)
; Wait for a response from the IPL
:
cmp APUIO0
bne :-
; Transfer the data
.assert Tad_Loader_SIZE < $ff, error, "Cannot fit Tad_Loader_SIZE in an 8 bit index"
sep #$30
.i8
ldx #0
@IplLoop:
; Send the next byte to the IPL
lda f:Tad_Loader_Bin,x
sta APUIO1
; Tell the IPL the next byte is ready
stx APUIO0
; Wait for a response form the IPL
:
cpx APUIO0
bne :-
inx
cpx #Tad_Loader_SIZE
bcc @IplLoop
rep #$10
.i16
; Send an execute program command to the IPL
ldx #TAD_LOADER_ARAM_ADDR
stx APUIO2 ; A-RAM address
stz APUIO1 ; zero = execute program at A-RAM address
lda #Tad_Loader_SIZE + 2
sta APUIO0 ; New data command (must be +2 the previous APUIO0 write)
.endmacro
;; Sends a TadLoaderDataType byte to the loader if the loader is ready
;;
;; Assumes loader just started OR a `SWITCH_TO_LOADER` message was sent to the audio driver/loader.
;;
;; IN: A = TadLoaderDataType value
;; OUT: carry = loader is ready and TadLoaderDataType sent
;;
.a8
.i16
;; DB access registers
.proc TadPrivate_Loader_CheckReadyAndSendLoaderDataType
; Test if the loader is ready
ldx #TadIO_Loader_Init::LOADER_READY_HL
cpx TadIO_Loader_Init::READY_PORT_HL
bne ReturnFalse
; Send the ready signal and the TadLoaderDataType
sta TadIO_Loader_Init::LOADER_DATA_TYPE_PORT
lda #TadIO_Loader_Init::LOADER_READY_L
sta TadIO_Loader_Init::READY_PORT_L
lda #TadIO_Loader_Init::LOADER_READY_H
sta TadIO_Loader_Init::READY_PORT_H
; The S-CPU must wait for the loader to write 0 to the spinlock before transferring data.
stz TadPrivate_dataToTransfer_prevSpinLock
; return true
sec
rts
ReturnFalse:
clc
rts
.endproc
;; Set the data transfer queue
;;
;; IN: A:X = far address
;; IN: Y = size
.a8
.i16
;; DB access registers
.proc TadPrivate_Loader_SetDataToTransfer
stx TadPrivate_dataToTransfer_addr
sta TadPrivate_dataToTransfer_bank
sty TadPrivate_dataToTransfer_size
rts
.endproc
;; Transfer data to the audio loader.
;;
;; ASSUMES: `check_ready_and_send_loader_data_type` and `set_data_to_transfer` were previously called.
;;
;; NOTE: This function may read one byte past the end of the transfer queue.
;;
;; OUT: carry set if all data in the transfer queue was sent to Audio-RAM.
;;
.a8
.i16
;; DB access lowram
.proc TadPrivate_Loader_TransferData
; Early exit if the loader is not ready
;
; This test doubles as a lock for the previous transfer.
;
; This also prevents a freeze in `process()` if the loader has crashed/glitched.
; (`finish_loading_data()` will freeze if the loader has crashed/glitched.
lda TadPrivate_dataToTransfer_prevSpinLock
cmp f:TadIO_Loader::SPINLOCK_PORT
bne @ReturnFalse
phd
phb
rep #$30
.a16
; Calculate number of words to read
lda TadPrivate_dataToTransfer_size
cmp TadPrivate_bytesToTransferPerFrame
bcc :+
lda TadPrivate_bytesToTransferPerFrame
:
inc ; required
lsr
; Prevent corrupting all of Audio-RAM if number of words == 0
bne :+
inc
:
; Store word to read in X
tax
; Reverse subtract TadPrivate_dataToTransfer_size (with clamping)
asl ; convert number of words to number of bytes
eor #$ffff
sec
adc TadPrivate_dataToTransfer_size
bcs :+
lda #0
:
sta TadPrivate_dataToTransfer_size
lda #$2100
tcd
; D = $2100
sep #$20
.a8
lda TadPrivate_dataToTransfer_bank
ldy TadPrivate_dataToTransfer_addr
pha
plb
; DB = TadPrivate_dataToTransfer_bank
@Loop:
; x = number of words remaining
; y = data address (using y to force addr,y addressing mode)
lda a:0,y
sta z:.lobyte(TadIO_Loader::DATA_PORT_L)
; The bank overflow test must be done here as `TadPrivate_dataToTransfer_addr` might point to an odd memory address.
iny
beq @BankOverflow_1
@BankOverflow_1_Resume:
lda a:0,y
sta z:.lobyte(TadIO_Loader::DATA_PORT_H)
; Increment this spinloack value
;
; The upper 4 bits of the spinlock must be clear'
; Cannot be 0. Zero is used to spinlock the loader init before this loop starts
; (see Loader Step 3 in `terrific-audio-driver/audio-driver/src/io-commands.wiz`)
.assert ($ffff & 7) + 1 < TadIO_Loader::SPINLOCK_MASK, error
tya ; y = address of data, it should always increment by 2
and #7
inc
sta z:.lobyte(TadIO_Loader::SPINLOCK_PORT)
iny
beq @BankOverflow_2
@BankOverflow_2_Resume:
dex
beq @EndLoop
; Spinloop until the S-SMP has acknowledged the data
:
cmp z:.lobyte(TadIO_Loader::SPINLOCK_PORT)
bne :-
bra @Loop
@EndLoop:
plb
pld
; DB restored
; D = 0
sty TadPrivate_dataToTransfer_addr
sta TadPrivate_dataToTransfer_prevSpinLock
ldy TadPrivate_dataToTransfer_size
bne @ReturnFalse
; End of data transfer
; Wait for Loader to acknowledge the last write
:
cmp f:TadIO_Loader::SPINLOCK_PORT
bne :-
; No more data to transfer
lda #TadIO_Loader::SPINLOCK_COMPLETE
sta f:TadIO_Loader::SPINLOCK_PORT
sec
rts
@ReturnFalse:
clc
rts
@BankOverflow_1:
jsr TadPrivate_Loader_GotoNextBank
bra @BankOverflow_1_Resume
@BankOverflow_2:
; Must save/restore A, it holds the spinlock
pha
jsr TadPrivate_Loader_GotoNextBank
pla
bra @BankOverflow_2_Resume
.endproc
;; Advance to the next bank
;;
;; MUST only be called to TadPrivate_Loader_TransferData
;;
;; ASSUMES: Y = 0 (Y addr overflowed to 0)
;;
;; IN: Y = 0
;; IN: DB = TadPrivate_dataToTransfer_bank
;;
;; OUT: Y = new address
;; OUT: DB = new bank
;;
;; KEEP: X
.a8
.i16
;; DB = TadPrivate_dataToTransfer_bank
.proc TadPrivate_Loader_GotoNextBank
phb
pla
inc
sta f:TadPrivate_dataToTransfer_bank
pha
plb
; DB = new TadPrivate_dataToTransfer_bank value
; MUST NOT CHANGE X
; Y = 0
.if .defined(LOROM)
and #$fe
cmp #$7e
beq :+
; Bank is not Work-RAM
ldy #$8000
:
.elseif .defined(HIROM)
and #$7f
cmp #$40
bcs :+
; Bank is a register bank
; set Y to the first ROM address
ldy #$8000
:
.else
.error "Unknown memory map"
.endif
; Y = 0 or $8000
rts
.endproc
;; OUT: carry set if state is LOADING_*
;; A8
.macro TadPrivate_IsLoaderActive
.assert .asize = 8, error
.assert TadState::NULL < TAD__FIRST_LOADING_STATE, error
.assert TadState::WAITING_FOR_LOADER_COMMON < TAD__FIRST_LOADING_STATE, error
.assert TadState::WAITING_FOR_LOADER_SONG < TAD__FIRST_LOADING_STATE, error
.assert (TadState::PAUSED & $7f) < TAD__FIRST_LOADING_STATE, error
.assert (TadState::PLAYING & $7f) < TAD__FIRST_LOADING_STATE, error
lda TadPrivate_state
and #$7f
cmp #TAD__FIRST_LOADING_STATE
.endmacro
;; ==========
;; Public API
;; ==========
;; -------------------------------
;; TAD_PROCESS_SEGMENT subroutines
;; -------------------------------
.segment TAD_PROCESS_SEGMENT
; JSL/RTL subroutine
.a8
.i16
; DB unknown
.proc Tad_Init : far
phb
lda #$80
pha
plb
; DB = $80
TadPrivate_Loader_TransferLoaderViaIpl
; Set default settings
.assert (TAD_DEFAULT_FLAGS) & TadFlags::RELOAD_COMMON_AUDIO_DATA = 0, error, "RELOAD_COMMON_AUDIO_DATA flag must not be use in TAD_DEFAULT_FLAGS"
.assert (TAD_DEFAULT_FLAGS) & TadFlags::_ALL_FLAGS = (TAD_DEFAULT_FLAGS), error, "Invalid TAD_DEFAULT_FLAGS"
.assert (TAD_DEFAULT_AUDIO_MODE) >= 0 && (TAD_DEFAULT_AUDIO_MODE) < TAD_N_AUDIO_MODES, error, "Invalid TAD_DEFAULT_AUDIO_MODE"
.assert Tad_flags + 1 = Tad_audioMode, error
ldx #(TAD_DEFAULT_FLAGS) | ((TAD_DEFAULT_AUDIO_MODE) << 8)
stx Tad_flags
ldx #TAD_DEFAULT_TRANSFER_PER_FRAME
stx TadPrivate_bytesToTransferPerFrame
lda #.bankbyte(Tad_AudioDriver_Bin)
ldx #.loword(Tad_AudioDriver_Bin)
ldy #Tad_AudioDriver_SIZE
jsr TadPrivate_Loader_SetDataToTransfer
lda #$ff
sta TadPrivate_nextCommand_id
sta Tad_sfxQueue_sfx
stz TadPrivate_nextSong
@DataTypeLoop:
lda #TadLoaderDataType::CODE
jsr TadPrivate_Loader_CheckReadyAndSendLoaderDataType
bcc @DataTypeLoop
@TransferLoop:
jsr TadPrivate_Loader_TransferData
bcc @TransferLoop
lda #TadState::WAITING_FOR_LOADER_COMMON
sta TadPrivate_state
plb
; DB restored
rtl
.endproc
;; Sends a command to the audio driver.
;;
;; REQUIRES: state == PAUSED or state == PLAYING.
;; REQUIRES: The previous command has been processed by the audio-driver.
;; REQUIRES: `TadPrivate_nextCommand_id` is not a play-sound-effect command.
;; REQUIRES: `TadPrivate_nextCommand_id` is a valid comma.
;;
;; IN: Y = TadPrivate_nextCommand_id
.a8
.i8
;; DB access lowram
.macro TadPrivate_Process_SendCommand
.assert .asize = 8, error
.assert .isize = 8, error
lda TadPrivate_nextCommand_parameter0
sta f:TadIO_ToDriver::PARAMETER0_PORT
lda TadPrivate_nextCommand_parameter1
sta f:TadIO_ToDriver::PARAMETER1_PORT
lda TadPrivate_previousCommand
and #TadIO_ToDriver::COMMAND_I_MASK ; Clear the non i bits of the command
eor #TadIO_ToDriver::COMMAND_I_MASK ; Flip the i bits
ora TadPrivate_nextCommand_id ; Set the c bits
sta f:TadIO_ToDriver::COMMAND_PORT
sta TadPrivate_previousCommand
cpy #TadCommand::UNPAUSE + 1
bcs @NotPauseOrPlay
; Change state if the command is a pause or play command
.assert TadCommand::PAUSE = 0, error
.assert TadCommand::PAUSE_MUSIC_PLAY_SFX = 2, error
.assert TadCommand::UNPAUSE = 4, error
.assert (TadCommand::PAUSE >> 1) & 3 | $80 = TadState::PAUSED, error
.assert (TadCommand::PAUSE_MUSIC_PLAY_SFX >> 1) & 3 | $80 = TadState::PLAYING_SFX, error
.assert (TadCommand::UNPAUSE >> 1) & 3 | $80 = TadState::PLAYING, error
lsr
and #3
ora #$80
sta TadPrivate_state
@NotPauseOrPlay:
; Reset command queue
lda #$ff
sta TadPrivate_nextCommand_id
.endmacro
;; Send a play-sound-effect command to the audio driver.
;;
;; REQUIRES: state == PLAYING
;; REQUIRES: The previous command has been processed by the audio-driver.
;;
;; IN: A = Tad_sfxQueue_sfx
;;
;; A8
;; I8
;; DB access lowram
.macro TadPrivate_Process_SendSfxCommand
.assert .asize = 8, error
.assert .isize = 8, error
; parameter 0 = sfx_id
sta f:TadIO_ToDriver::PARAMETER0_PORT
; parameter 1 = pan
lda Tad_sfxQueue_pan
cmp #TAD_MAX_PAN + 1
bcc :+
lda #TAD_CENTER_PAN
:
sta f:TadIO_ToDriver::PARAMETER1_PORT
; Send play-sound-effect command
lda TadPrivate_previousCommand
and #TadIO_ToDriver::COMMAND_I_MASK ; Clear the non i bits of the command
eor #TadIO_ToDriver::COMMAND_I_MASK ; Flip the i bits
ora #TadCommand::PLAY_SOUND_EFFECT ; Set the c bits
sta f:TadIO_ToDriver::COMMAND_PORT
sta TadPrivate_previousCommand
; Reset the SFX queue
ldy #$ff
sty Tad_sfxQueue_sfx
sty Tad_sfxQueue_pan
.endmacro
; JSL/RTL subroutine
.a8
.i16
; DB access lowram
.proc Tad_Process : far
.assert TadState::PAUSED = $80, error
.assert TadState::PLAYING > $80, error
lda TadPrivate_state
bpl @NotLoaded