-
Notifications
You must be signed in to change notification settings - Fork 11
/
hUGEDriver.asm
1896 lines (1576 loc) · 35.3 KB
/
hUGEDriver.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
include "include/hardware.inc"
include "include/hUGE.inc"
MACRO add_a_to_r16
add LOW(\1)
ld LOW(\1), a
adc HIGH(\1)
sub LOW(\1)
ld HIGH(\1), a
ENDM
;; Thanks PinoBatch!
MACRO sub_from_r16 ;; (high, low, value)
ld a, \2
sub \3
ld \2, a
sbc a ; A = -1 if borrow or 0 if not
add \1
ld \1, a
ENDM
MACRO retMute
bit \1, a
ret nz
ENDM
MACRO checkMute
ld a, [mute_channels]
bit \1, a
jr nz, \2
ENDM
;; Maximum pattern length
DEF PATTERN_LENGTH EQU 64
SECTION "Playback variables", WRAM0
;; Active song descriptor
order_cnt: db
_start_song_descriptor_pointers:
;; Pointers to the song's current four orders (one per channel)
order1: dw
order2: dw
order3: dw
order4: dw
;; Pointers to the instrument tables
duty_instruments: dw
wave_instruments: dw
noise_instruments: dw
;; Misc. pointers
routines: dw
waves: dw
_end_song_descriptor_pointers:
;; Pointers to the current patterns (sort of a cache)
pattern1: dw
pattern2: dw
pattern3: dw
pattern4: dw
;; How long a row lasts in ticks (1 = one row per call to `hUGE_dosound`, etc. 0 translates to 256)
ticks_per_row: db
_hUGE_current_wave::
hUGE_current_wave::
;; ID of the wave currently loaded into wave RAM
current_wave: db
def hUGE_NO_WAVE equ 100
EXPORT hUGE_NO_WAVE
;; Everything between this and `end_zero` is zero-initialized by `hUGE_init`
start_zero:
_hUGE_mute_mask::
mute_channels: db
counter: db
tick: db
row_break: db
next_order: db
row: db
current_order: db
IF DEF(PREVIEW_MODE)
loop_order: db
single_stepping: db
single_step_stopped: db
ENDC
channels:
;;;;;;;;;;;
;;Channel 1
;;;;;;;;;;;
channel1:
channel_period1: dw
toneporta_target1: dw
channel_note1: db
highmask1: db
vibrato_tremolo_phase1: db
envelope1: db
table1: dw
table_row1: db
ds 5
;;;;;;;;;;;
;;Channel 2
;;;;;;;;;;;
channel2:
channel_period2: dw
toneporta_target2: dw
channel_note2: db
highmask2: db
vibrato_tremolo_phase2: db
envelope2: db
table2: dw
table_row2: db
ds 5
;;;;;;;;;;;
;;Channel 3
;;;;;;;;;;;
channel3:
channel_period3: dw
toneporta_target3: dw
channel_note3: db
highmask3: db
vibrato_tremolo_phase3: db
envelope3: db
table3: dw
table_row3: db
ds 5
;;;;;;;;;;;
;;Channel 4
;;;;;;;;;;;
channel4:
channel_period4: dw
toneporta_target4: dw
channel_note4: db
highmask4: db
step_width4: db
vibrato_tremolo_phase4: db
envelope4: db
table4: dw
table_row4: db
ds 4
end_zero:
SECTION "Sound Driver", ROM0
IF DEF(GBDK)
_hUGE_init::
ld h, d
ld l, e
ENDC
;;; Sets up hUGEDriver to play a song.
;;; !!! BE SURE THAT `hUGE_dosound` WILL NOT BE CALLED WHILE THIS RUNS !!!
;;; Param: HL = Pointer to the "song descriptor" you wish to load (typically exported by hUGETracker).
;;; Destroys: AF C DE HL
hUGE_init::
ld a, [hl+] ; tempo
ld [ticks_per_row], a
ld a, [hl+]
ld e, a
ld a, [hl+]
ld d, a
ld a, [de]
ld [order_cnt], a
ld c, _end_song_descriptor_pointers - (_start_song_descriptor_pointers)
ld de, order1
.copy_song_descriptor_loop:
ld a, [hl+]
ld [de], a
inc de
dec c
jr nz, .copy_song_descriptor_loop
IF !DEF(PREVIEW_MODE)
;; Zero some ram
ld c, end_zero - start_zero
ld hl, start_zero
xor a
.fill_loop:
ld [hl+], a
dec c
jr nz, .fill_loop
ENDC
;; These two are zero-initialized by the loop above, so these two writes must come after
ld a, %11110000
ld [envelope1], a
ld [envelope2], a
;; Force loading the next wave
ld a, hUGE_NO_WAVE
ld [current_wave], a
;; Preview mode needs to load the order ID from memory
IF !DEF(PREVIEW_MODE)
ld c, 0
ELSE
ld a, [current_order]
ld c, a
ENDC
;; fallthrough (load the pattern pointers)
;;; Sets all 4 pattern pointers from a certain index in the respective 4 orders.
;;; Param: C = The index (in increments of 2)
;;; Destroy: AF DE HL
load_patterns:
IF DEF(PREVIEW_MODE)
db $fc ; signal order update to tracker
ENDC
ld hl, order1
ld de, pattern1
call .load_pattern
ld hl, order2
call .load_pattern
ld hl, order3
call .load_pattern
ld hl, order4
;; fallthrough
.load_pattern:
ld a, [hl+]
add c
ld h, [hl]
ld l, a
adc h
sub l
ld h, a
ld a, [hl+]
ld [de], a
inc de
ld a, [hl]
ld [de], a
inc de
ret
IF DEF(GBDK)
_hUGE_mute_channel::
ld b, a
ld c, e
ENDC
;;; Sets a channel's muting status.
;;; Muted channels are left entirely alone by the driver, so that you can repurpose them,
;;; for example for sound effects, CH3 sample playback, etc.
;;; If muting the channel, the note being played will be cut.
;;; Param: B = Which channel to enable; 0 for CH1, 1 for CH2, etc.
;;; Param: C = 0 to unmute the channel, 1 to mute it
;;; Destroy: A C E HL
hUGE_mute_channel::
ld e, $fe
ld a, b
or a
jr z, .enable_cut
.enable_loop:
sla c
rlc e
dec a
jr nz, .enable_loop
.enable_cut:
ld a, [mute_channels]
and e
or c
ld [mute_channels], a
and c
jp nz, note_cut
ret
;;; Reads a pattern's current row.
;;; Param: BC = Pointer to the pattern
;;; Param: [row] = Index of the current row
;;; Return: A = Note ID
;;; Return: B = Instrument (upper nibble) & effect code (lower nibble)
;;; Return: C = Effect parameter
;;; Destroy: HL
get_current_row:
ld a, [row]
.row_in_a:
ld h, a
;; Multiply by 3 for the note value
add h
add h
ld h, 0
ld l, a
add hl, bc ; HL now points at the 3rd byte of the note
ld a, [hl+]
ld b, [hl]
inc hl
ld c, [hl]
ret
;;; Gets the "period" of a pattern's current note.
;;; Param: HL = Pointer to the pattern pointer
;;; Param: [row] = Index of the current row
;;; Param: DE = Location to write the note's index to, if applicable
;;; Return: HL = Note's period
;;; Return: CF = Set if and only if a "valid" note (i.e. not a "rest")
;;; Return: [DE] = Note's ID, not updated if a "rest"
;;; Return: B = Instrument (upper nibble) & effect code (lower nibble)
;;; Return: C = Effect parameter
;;; Destroy: AF
get_current_note:
ld a, [hl+]
ld c, a
ld b, [hl]
call get_current_row
ld hl, 0
;; If the note we found is greater than LAST_NOTE, then it's not a valid note
;; and nothing needs to be updated.
cp LAST_NOTE
ret nc
;; Store the loaded note value in channel_noteX
ld [de], a
;;; Gets a note's "period", i.e. what should be written to NRx3 and NRx4.
;;; Param: A = Note ID
;;; Return: HL = Note's period
;;; Return: CF = 1
;;; Destroy: AF
get_note_period:
add a ; double it to get index into hi/lo table
add LOW(note_table)
ld l, a
adc HIGH(note_table)
sub l
ld h, a
ld a, [hl+]
ld h, [hl]
ld l, a
scf
ret
;;; Gets a note's "polynomial counter", i.e. what should be written to NR44.
;;; Param: A = Note ID
;;; Return: A = Note's poly
;;; Destroy: F HL
get_note_poly:
;; Invert the order of the numbers
add 192 ; (255 - 63)
cpl
;; Thanks to RichardULZ for this formula
;; https://docs.google.com/spreadsheets/d/1O9OTAHgLk1SUt972w88uVHp44w7HKEbS/edit#gid=75028951
; if A > 7 then begin
; B := (A-4) div 4;
; C := (A mod 4)+4;
; A := (C or (B shl 4))
; end;
; if A < 7 then return
cp 7
ret c
ld h, a
; B := (A-4) div 4;
srl a
srl a
dec a
ld l, a
; C := (A mod 4)+4;
ld a, h
and 3 ; mod 4
add 4
; A := (C or (B shl 4))
swap l
or l
ret
;;; Computes the pointer to a member of a channel.
;;; Param: B = Which channel (0 = CH1, 1 = CH2, etc.)
;;; Param: D = Offset within the channel struct
;;; Return: HL = Pointer to the channel's member
;;; Destroy: AF
ptr_to_channel_member:
ld a, b
swap a
add d
add LOW(channels)
ld l, a
adc HIGH(channels)
sub l
ld h, a
ret
;; TODO: Make this take HL instead of DE
;;; Updates a channel's frequency, and possibly restarts it.
;;; Note that CH4 is *never* restarted by this!
;;; Param: B = Which channel to update (0 = CH1, 1 = CH2, etc.)
;;; Param: (for CH4) E = Note ID
;;; Param: (otherwise) DE = Note period
;;; Destroy: AF C
;;; Destroy: (for CH4) HL
update_channel_freq:
ld h, 0
.nonzero_highmask:
ld c, b
ld a, [mute_channels]
dec c
jr z, .update_channel2
dec c
jr z, .update_channel3
dec c
jr z, .update_channel4
.update_channel1:
retMute 0
ld a, e
ld [channel_period1], a
ldh [rAUD1LOW], a
ld a, d
ld [channel_period1+1], a
or h
ldh [rAUD1HIGH], a
ret
.update_channel2:
retMute 1
ld a, e
ld [channel_period2], a
ldh [rAUD2LOW], a
ld a, d
ld [channel_period2+1], a
or h
ldh [rAUD2HIGH], a
ret
.update_channel3:
retMute 2
ld a, e
ld [channel_period3], a
ldh [rAUD3LOW], a
ld a, d
ld [channel_period3+1], a
or h
ldh [rAUD3HIGH], a
ret
.update_channel4:
retMute 3
ld d, h
ld a, e
call get_note_poly
ld hl, step_width4
or [hl]
ldh [rAUD4POLY], a
ld a, d
ldh [rAUD4GO], a
ret
play_note_routines:
jr play_ch1_note
jr play_ch2_note
jr play_ch3_note
jr play_ch4_note
play_ch1_note:
ld a, [mute_channels]
retMute 0
;; Play a note on channel 1 (square wave)
ld hl, channel_period1
ld a, [hl+]
ldh [rAUD1LOW], a
;; Get the highmask and apply it.
ld a, [highmask1]
or [hl]
ldh [rAUD1HIGH], a
ret
play_ch2_note:
ld a, [mute_channels]
retMute 1
;; Play a note on channel 2 (square wave)
ld hl, channel_period2
ld a, [hl+]
ldh [rAUD2LOW], a
;; Get the highmask and apply it.
ld a, [highmask2]
or [hl]
ldh [rAUD2HIGH], a
ret
play_ch3_note:
ld a, [mute_channels]
retMute 2
;; Triggering CH3 while it's reading a byte corrupts wave RAM.
;; To avoid this, we kill the wave channel (0 → NR30), then re-enable it.
;; This way, CH3 will be paused when we trigger it by writing to NR34.
;; TODO: what if `highmask3` bit 7 is not set, though?
ldh a, [rAUDTERM]
push af
and %10111011
ldh [rAUDTERM], a
xor a
ldh [rAUD3ENA], a
cpl
ldh [rAUD3ENA], a
;; Play a note on channel 3 (waveform)
ld hl, channel_period3
ld a, [hl+]
ldh [rAUD3LOW], a
;; Get the highmask and apply it.
ld a, [highmask3]
or [hl]
ldh [rAUD3HIGH], a
pop af
ldh [rAUDTERM], a
ret
play_ch4_note:
ld a, [mute_channels]
retMute 3
;; Play a "note" on channel 4 (noise)
ld a, [channel_period4]
ldh [rAUD4POLY], a
;; Get the highmask and apply it.
ld a, [highmask4]
ldh [rAUD4GO], a
ret
;;; Executes a row of a table.
;;; Param: BC = Pointer to which table to run
;;; Param: [HL] = Which row the table is on
;;; Param: E = Which channel to run the table on
do_table:
;; Increment the current row
ld a, [hl]
inc [hl]
push hl
;; Grab the cell values, return if no note.
;; Save BC for doing effects.
call get_current_row.row_in_a
pop hl ; TODO: don't trash HL in the first place
push bc
ld d, a
;; If there's a jump, change the current row
ld a, b
and $F0
bit 7, d
jr z, .no_steal
res 7, d
set 0, a
.no_steal:
swap a
jr z, .no_jump
dec a
ld [hl], a
.no_jump:
ld a, d
;; If there's no note, don't update channel frequencies
cp NO_NOTE
jr z, .no_note2
sub 36 ; bring the number back in the range of -36, +35
ld b, e
ld e, a
ld d, 4
call ptr_to_channel_member
ld a, e
add [hl]
inc hl
ld d, [hl]
;; A = note index
;; B = channel
;; D = highmask
;; pushed = instrument/effect
;; If ch4, don't get note period (update_channel_freq gets the poly for us)
ld e, a
inc b
bit 2, b
ld c, d
jr nz, .is_ch4
call get_note_period
ld d, h
ld e, l
.is_ch4:
ld h, c
res 7, h
dec b
call update_channel_freq.nonzero_highmask
.no_note:
ld e, b
.no_note2:
pop bc
ld d, 1
jr do_effect.no_set_offset
;;; Performs an effect on a given channel.
;;; Param: E = Channel ID (0 = CH1, 1 = CH2, etc.)
;;; Param: B = Effect type (upper 4 bits ignored)
;;; Param: C = Effect parameters (depend on FX type)
;;; Destroy: AF BC DE HL
do_effect:
;; Return immediately if effect is 000
ld d, 0
.no_set_offset:
ld a, b
and $0F
or c
ret z
;; Strip the instrument bits off leaving only effect code
ld a, b
and $0F
;; Multiply by 2 to get offset into table
add a
add LOW(.jump)
ld l, a
adc HIGH(.jump)
sub l
ld h, a
ld a, [hl+]
ld h, [hl]
ld l, a
bit 0, d
jr z, .no_offset
inc hl
.no_offset:
ld b, e
ld a, [tick]
or a ; We can return right off the bat if it's tick zero
jp hl
.jump:
;; Jump table for effect
dw fx_arpeggio ;0xy
dw fx_porta_up ;1xy
dw fx_porta_down ;2xy
dw fx_toneporta ;3xy
dw fx_vibrato ;4xy
dw fx_set_master_volume ;5xy ; global
dw fx_call_routine ;6xy
dw fx_note_delay ;7xy
dw fx_set_pan ;8xy ; global
dw fx_set_duty ;9xy
dw fx_vol_slide ;Axy
dw fx_pos_jump ;Bxy ; global
dw fx_set_volume ;Cxy
dw fx_pattern_break ;Dxy ; global
dw fx_note_cut ;Exy
dw fx_set_speed ;Fxy ; global
;;; Processes (global) effect 5, "set master volume".
;;; Param: C = Value to write to NR50
;;; Param: ZF = Set if and only if on tick 0
;;; Destroy: A
fx_set_master_volume:
ret nz
ld a, c
ldh [rAUDVOL], a
ret
;;; Processes effect 6, "call routine".
;;; Param: B = Current channel ID (0 = CH1, 1 = CH2, etc.)
;;; Param: C = Routine ID
;;; Param: A = Current tick
;;; Param: ZF = Set if and only if on tick 0
;;; Destroy: Anything the routine does
fx_call_routine:
nop ; In place of `ret cc`. Allows to be used in subpatterns
ld hl, routines
ld a, $0f
and c
add a
add [hl]
ld e, a
inc hl
ld a, $0
adc [hl]
ld h, a
ld l, e
ld a, [hl+]
ld h, [hl]
ld l, a
ld d, b
ld e, c ; SDCC compatibility
ld a, [tick]
or a ; set zero flag if tick 0 for compatibility
jp hl
;;; Processes (global) effect 8, "set pan".
;;; Param: B = Current channel ID (0 = CH1, 1 = CH2, etc.)
;;; Param: C = Value to write to NR51
;;; Param: ZF = Set if and only if on tick 0
;;; Destroy: A
fx_set_pan:
ret nz
;; Pretty simple. The editor can create the correct value here without a bunch
;; of bit shifting manually.
ld a, c
ldh [rAUDTERM], a
ret
;;; Processes effect 9, "set duty cycle".
;;; Param: B = Current channel ID (0 = CH1, anything else = CH2)
;;; Param: C = Value to write to NRx1
;;; Param: ZF = Set if and only if on tick 0
;;; Destroy: AF
fx_set_duty:
ret nz
;; $900 = 12.5%
;; $940 = 25%
;; $980 = 50%
;; $9C0 = 75%
ld a, [mute_channels]
dec b
jr z, .chan2
dec b
jr z, .chan3
dec b
jr z, .chan4
.chan1:
retMute 0
ld a, c
ldh [rAUD1LEN], a
ret
.chan2:
retMute 1
ld a, c
ldh [rAUD2LEN], a
ret
.chan4:
retMute 3
ldh a, [rAUD4POLY]
res 3, a
or c
ldh [rAUD4POLY], a
ret
.chan3:
retMute 2
ld a, c
ld hl, current_wave
call update_ch3_waveform
ld b, 2
jp play_note
update_ch3_waveform:
ld [hl], a
;; Get pointer to new wave
swap a
ld hl, waves
add [hl]
inc hl
ld h, [hl]
ld l, a
adc h
sub l
ld h, a
ldh a, [rAUDTERM]
push af
and %10111011
ldh [rAUDTERM], a
xor a
ldh [rAUD3ENA], a
FOR OFS, 16
ld a, [hl+]
ldh [_AUD3WAVERAM + OFS], a
ENDR
ld a, %10000000
ldh [rAUD3ENA], a
pop af
ldh [rAUDTERM], a
ret
;;; Processes (global) effect F, "set speed".
;;; Param: C = New amount of ticks per row
;;; Param: ZF = Set if and only if on tick 0
;;; Destroy: A
fx_set_speed:
ret nz
ld a, c
ld [ticks_per_row], a
ret
IF DEF(GBDK)
_hUGE_set_position::
ld c, a
xor a
ENDC
hUGE_set_position::
;;; Processes (global) effect B, "position jump".
;;; Param: C = ID of the order to jump to
;;; Destroy: A
fx_pos_jump:
ret nz
ld hl, row_break
or [hl] ; a = 0 since we know we're on tick 0
jr nz, .already_broken
ld [hl], 1
.already_broken:
inc hl
ld [hl], c
ret
;;; Processes (global) effect D, "pattern break".
;;; Param: C = ID of the next order's row to start on
;;; Destroy: A
fx_pattern_break:
ret nz
ld a, c
ld [row_break], a
ret
;;; Processes effect E, "note cut".
;;; Param: B = Current channel ID (0 = CH1, 1 = CH2, etc.)
;;; Param: C = Tick to cut the note on
;;; Param: A = Current tick
;;; Destroy: A
fx_note_cut:
cp c
ret nz
;; check channel mute
ld a, b
;; 0 → $01, 1 → $02, 2 → $04, 3 → $05
;; Overall, these two instructions add 1 to the number.
;; However, the first instruction will generate a carry for inputs of $02 and $03;
;; the `adc` will pick the carry up, and "separate" 0 / 1 from 2 / 3 by an extra 1.
;; Luckily, this yields correct results for 0 ($01), 1 ($02), and 2 ($03 + 1 = $04).
;; We'll see about fixing 3 afterwards.
add -2
adc 3
;; After being shifted left, the inputs are $02, $04, $08 and $0A; all are valid BCD,
;; except for $0A. Since we just performed `add a`, DAA will correct the latter to $10.
;; (This should be correctly emulated everywhere, since the inputs are identical to
;; "regular" BCD.)
;; When shifting the results back, we'll thus get $01, $02, $04 and $08!
add a
daa
rra
ld d, a
ld a, [mute_channels]
cpl
and d
ret z
;; fallthrough
;;; Cuts note on a channel.
;;; Param: B = Current channel ID (0 = CH1, 1 = CH2, etc.)
;;; Destroy: AF HL
note_cut:
ld a, b
add a
add a
add b ; multiply by 5
add LOW(rAUD1ENV)
ld l, a
ld h, HIGH(rAUD1ENV)
xor a
ld [hl+], a
ld a, b
cp 2
ret z ; return early if CH3-- no need to retrigger note
;; Retrigger note
inc l ; Not `inc hl` because H stays constant (= $FF)
ld [hl], $FF
ret
;;; Processes effect C, "set volume".
;;; Param: B = Current channel ID (0 = CH1, 1 = CH2, etc.)
;;; Param: C = Volume to set the channel to
;;; Param: ZF = Set if and only if on tick 0
;;; Destroy: AF BC
fx_set_volume:
ret nz ; Return if we're not on tick zero.
swap c
ld a, [mute_channels]
dec b
jr z, .set_chn_2_vol
dec b
jr z, .set_chn_3_vol
dec b
jr z, .set_chn_4_vol
.set_chn_1_vol:
retMute 0
ldh a, [rAUD1ENV]
and %00001111
or c
ldh [rAUD1ENV], a
jp play_ch1_note
.set_chn_2_vol:
retMute 1
ldh a, [rAUD2ENV]
and %00001111
or c
ldh [rAUD2ENV], a
jp play_ch2_note
.set_chn_3_vol:
retMute 2
;; "Quantize" the more finely grained volume control down to one of 4 values.
ld a, c
cp 10 << 4
jr nc, .one
cp 5 << 4
jr nc, .two
or a
jr z, .done ; Zero maps to zero
.three:
ld a, %01100000
jr .done
.two:
ld a, %01000000
jr .done