-
Notifications
You must be signed in to change notification settings - Fork 0
/
hUGEDriver.asm
1674 lines (1358 loc) · 27.7 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"
add_a_to_r16: MACRO
add \2
ld \2, a
adc \1
sub \2
ld \1, a
ENDM
;; Thanks PinoBatch!
sub_from_r16: MACRO ;; (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
add_a_to_hl: MACRO
add_a_to_r16 h, l
ENDM
add_a_to_de: MACRO
add_a_to_r16 d, e
ENDM
ret_dont_call_playnote: MACRO
pop hl
pop af
ld a, 6 ; How many bytes until the next channel's code
add_a_to_hl
jp hl
ENDM
add_a_ind_ret_hl: MACRO
ld hl, \1
add [hl]
inc hl
ld h, [hl]
ld l, a
adc h
sub l
ld h, a
ENDM
load_hl_ind: MACRO
ld hl, \1
ld a, [hl+]
ld h, [hl]
ld l, a
ENDM
load_de_ind: MACRO
ld a, [\1]
ld e, a
ld a, [\1+1]
ld d, a
ENDM
;; Maximum pattern length
PATTERN_LENGTH EQU 64
;; Amount to be shifted in order to skip a channel.
CHANNEL_SIZE_EXPONENT EQU 3
SECTION "Playback variables", WRAM0
_start_vars:
;; active song descriptor
order_cnt: db
_start_song_descriptor_pointers:
order1: dw
order2: dw
order3: dw
order4: dw
duty_instruments: dw
wave_instruments: dw
noise_instruments: dw
routines: dw
waves: dw
_end_song_descriptor_pointers:
;; variables
mute_channels: db
pattern1: dw
pattern2: dw
pattern3: dw
pattern4: dw
ticks_per_row: db
current_order: db
next_order: db
row_break: db
temp_note_value: dw
row: db
tick: db
counter: db
_hUGE_current_wave::
current_wave: db
channels:
;;;;;;;;;;;
;;Channel 1
;;;;;;;;;;;
channel1:
channel_period1: dw
toneporta_target1: dw
channel_note1: db
vibrato_tremolo_phase1: db
envelope1: db
highmask1: db
;;;;;;;;;;;
;;Channel 2
;;;;;;;;;;;
channel2:
channel_period2: dw
toneporta_target2: dw
channel_note2: db
vibrato_tremolo_phase2: db
envelope2: db
highmask2: db
;;;;;;;;;;;
;;Channel 3
;;;;;;;;;;;
channel3:
channel_period3: dw
toneporta_target3: dw
channel_note3: db
vibrato_tremolo_phase3: db
envelope3: db
highmask3: db
;;;;;;;;;;;
;;Channel 4
;;;;;;;;;;;
channel4:
channel_period4: dw
toneporta_target4: dw
channel_note4: db
vibrato_tremolo_phase4: db
envelope4: db
highmask4: db
_end_vars:
SECTION "Sound Driver", ROM0
_hUGE_init_banked::
ld hl, sp+2+4
jr continue_init
_hUGE_init::
ld hl, sp+2
continue_init:
push bc
ld a, [hl+]
ld h, [hl]
ld l, a
call hUGE_init
pop bc
ret
_hUGE_mute_channel_banked::
ld hl, sp+3+4
jr continue_mute
_hUGE_mute_channel::
ld hl, sp+3
continue_mute:
push bc
ld a, [hl-]
and 1
ld c, a
ld b, [hl]
call hUGE_mute_channel
pop bc
ret
hUGE_mute_channel::
;; B: channel
;; C: enable flag
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
call nz, note_cut
ret
_hUGE_set_position_banked::
ld hl, sp+2+4
jr continue_set_position
_hUGE_set_position::
ld hl, sp+2
continue_set_position:
push bc
ld c, [hl]
call hUGE_set_position
pop bc
ret
hUGE_init::
push hl
if !DEF(PREVIEW_MODE)
;; Zero some ram
ld c, _end_vars - _start_vars
ld hl, _start_vars
xor a
.fill_loop:
ld [hl+], a
dec c
jr nz, .fill_loop
ENDC
ld a, %11110000
ld [envelope1], a
ld [envelope2], a
ld a, 100
ld [current_wave], a
pop hl
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
ld a, [current_order]
ld c, a ;; Current order index
;; Fall through into _refresh_patterns
_refresh_patterns:
;; Loads pattern registers with pointers to correct pattern based on
;; an order index
;; Call with c set to what order to load
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
jr .load_pattern
.load_pattern:
ld a, [hl+]
ld h, [hl]
ld l, a
ld a, c
add_a_to_hl
ld a, [hl+]
ld [de], a
inc de
ld a, [hl]
ld [de], a
inc de
ret
_load_note_data:
;; Call with:
;; Pattern pointer in BC
;; Stores instrument/effect code in B
;; Stores effect params in C
;; Stores note number in A
ld a, [row]
ld h, a
;; Multiply by 3 for the note value
add h
add h
add 2
ld h, 0
ld l, a
add hl, bc ; HL now points at the 3rd byte of the note
ld a, [hl-]
ld c, a
ld a, [hl-]
ld b, a
ld a, [hl]
ret
_lookup_note:
;; Call with:
;; Pattern pointer in BC
;; channel_noteX pointer in DE
;; Stores note period value in HL
;; Stores instrument/effect code in B
;; Stores effect params in C
;; Stores note number in the memory pointed to by DE
call _load_note_data
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
_convert_note:
;; Call with:
;; Note number in A
;; Stores note period value in HL
add a ;; double it to get index into hi/lo table
ld hl, note_table
add_a_to_hl
ld a, [hl+]
ld h, [hl]
ld l, a
scf
ret
_convert_ch4_note:
;; Call with:
;; Note number in A
;; Stores polynomial counter in A
;; Free: HL
;; 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;
sub 4
srl a
srl 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
_update_channel:
;; Call with:
;; Highmask in A
;; Channel in B
;; Note tone in DE
ld c, a
dec b
jr z, _update_channel2
dec b
jr z, _update_channel3
dec b
jr z, _update_channel4
retMute: MACRO
ld a, [mute_channels]
bit \1, a
ret nz
ENDM
_update_channel1:
retMute 0
ld a, e
ld [rAUD1LOW], a
ld a, d
or c
ld [rAUD1HIGH], a
ret
_update_channel2:
retMute 1
ld a, e
ld [rAUD2LOW], a
ld a, d
or c
ld [rAUD2HIGH], a
ret
_update_channel3:
retMute 2
ld a, e
ld [rAUD3LOW], a
ld a, d
or c
ld [rAUD3HIGH], a
ret
_update_channel4:
retMute 3
ld a, e
call _convert_ch4_note
ld [rAUD4POLY], a
xor a
ld [rAUD4GO], a
ret
_play_note_routines:
jr _playnote1
jr _playnote2
jr _playnote3
jr _playnote4
_playnote1:
retMute 0
;; Play a note on channel 1 (square wave)
ld a, [temp_note_value+1]
ld [channel_period1], a
ld [rAUD1LOW], a
ld a, [temp_note_value]
ld [channel_period1+1], a
;; Get the highmask and apply it.
ld hl, highmask1
or [hl]
ld [rAUD1HIGH], a
ret
_playnote2:
retMute 1
;; Play a note on channel 2 (square wave)
ld a, [temp_note_value+1]
ld [channel_period2], a
ld [rAUD2LOW], a
ld a, [temp_note_value]
ld [channel_period2+1], a
;; Get the highmask and apply it.
ld hl, highmask2
or [hl]
ld [rAUD2HIGH], a
ret
_playnote3:
retMute 2
;; This fixes a gameboy hardware quirk, apparently.
;; The problem is emulated accurately in BGB.
;; https://gbdev.gg8.se/wiki/articles/Gameboy_sound_hardware
xor a
ld [rAUD3ENA], a
cpl
ld [rAUD3ENA], a
;; Play a note on channel 3 (waveform)
ld a, [temp_note_value+1]
ld [channel_period3], a
ld [rAUD3LOW], a
ld a, [temp_note_value]
ld [channel_period3+1], a
;; Get the highmask and apply it.
ld hl, highmask3
or [hl]
ld [rAUD3HIGH], a
ret
_playnote4:
retMute 3
;; Play a "note" on channel 4 (noise)
ld a, [temp_note_value]
ld [channel_period4+1], a
ld [rAUD4POLY], a
;; Get the highmask and apply it.
ld a, [highmask4]
ld [rAUD4GO], a
ret
_doeffect:
;; Call with:
;; B: instrument nibble + effect type nibble
;; C: effect parameters
;; E: channel
;; free: A, D, H, L
;; Strip the instrument bits off leaving only effect code
ld a, b
and %00001111
;; Multiply by 2 to get offset into table
add a, a
ld hl, .jump
add_a_to_hl
ld a, [hl+]
ld h, [hl]
ld l, a
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
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
setup_channel_pointer:
;; Call with:
;; Channel value in B
;; Offset in D
;; Returns value in HL
ld a, b
REPT CHANNEL_SIZE_EXPONENT
add a
ENDR
add d
ld hl, channels
add_a_to_hl
ret
fx_set_master_volume:
ret nz
;; A: tick
;; ZF: (tick == 0)
;; B: channel
;; C: effect parameters
;; free registers: A, D, E, H, L
;; Upper 4 bits contain volume for left, lower 4 bits for right
;; Format is ?LLL ?RRR where ? is just a random bit, since we don't use
;; the Vin
;; This can be used as a more fine grained control over channel 3's output,
;; if you pan it completely.
ld a, c
ld [rAUDVOL], a
ret
fx_call_routine:
;; A: tick
;; ZF: (tick == 0)
;; B: channel
;; C: effect parameters
;; free registers: A, D, E, H, L
;; Routines are 16 bytes. Shift left to multiply by 16, then
;; jump to that location.
load_hl_ind routines
ld a, h
or l
ret z
ld a, c
and $0f
add a
add_a_to_hl
ld a, [hl+]
ld h, [hl]
ld l, a
ld a, [tick]
push af
inc sp
push bc
or a ; set zero flag if tick 0 for compatibility
call .call_hl
add sp, 3
ret
.call_hl:
jp hl
fx_set_pan:
ret nz
;; A: tick
;; ZF: (tick == 0)
;; B: channel
;; C: effect parameters
;; free registers: A, D, E, H, L
;; Pretty simple. The editor can create the correct value here without a bunch
;; of bit shifting manually.
ld a, c
ld [rAUDTERM], a
ret
fx_set_duty:
ret nz
;; A: tick
;; ZF: (tick == 0)
;; B: channel
;; C: effect parameters
;; free registers: A, D, E, H, L
;; $900 = 12.5%
;; $940 = 25%
;; $980 = 50%
;; $9C0 = 75%
ld a, b
or a
jr z, .chan1
.chan2:
retMute 1
ld a, c
ld [rAUD2LEN], a
ret
.chan1:
retMute 0
ld a, c
ld [rAUD1LEN], a
ret
fx_vol_slide:
ret nz
;; This is really more of a "retrigger note with lower volume" effect and thus
;; isn't really that useful. Instrument envelopes should be used instead.
;; Might replace this effect with something different if a new effect is
;; ever needed.
;; A: tick
;; ZF: (tick == 0)
;; B: channel
;; C: effect parameters
;; free registers: A, D, E, H, L
;; Todo
;; check channel mute
ld d, 1
ld a, b
or a
jr z, .cont
.loop:
sla d
dec a
jr nz, .loop
.cont:
ld a, [mute_channels]
and d
ret nz
ld d, 0
call setup_channel_pointer
ld a, [hl+]
ld [temp_note_value+1], a
ld a, [hl]
ld [temp_note_value], a
ld a, b
add a
ld hl, _envelope_registers
add_a_to_hl
ld a, [hl+]
ld h, [hl]
ld l, a
;; setup the up and down params
ld a, c
and %00001111
ld d, a
ld a, c
and %11110000
ld e, a
swap e
ld a, [hl]
and %11110000
swap a
sub d
jr nc, .cont1
xor a
.cont1:
add e
cp $10
jr c, .cont2
ld a, $F
.cont2:
swap a
ld [hl+], a
inc hl
ld a, [hl]
or %10000000
ld [hl], a
ld hl, _play_note_routines
ld a, b
add a
add_a_to_hl
jp hl
_envelope_registers:
dw rAUD1ENV
dw rAUD2ENV
dw rAUD3LEVEL
dw rAUD4ENV
fx_note_delay:
;; A: tick
;; ZF: (tick == 0)
;; B: channel
;; C: effect parameters
;; free registers: A, D, E, H, L
jr nz, .play_note
;; Just store the note into the channel period, and don't play a note.
ld d, 0
call setup_channel_pointer
ld a, [temp_note_value]
ld [hl+], a
ld a, [temp_note_value+1]
ld [hl], a
;; Don't call _playnote. This is done by grabbing the return
;; address and manually skipping the next call instruction.
ret_dont_call_playnote
.play_note:
ld a, [tick]
cp c
ret nz ; wait until the correct tick to play the note
ld d, 0
call setup_channel_pointer
;; TODO: Change this to accept HL instead?
ld a, [hl+]
ld [temp_note_value], a
ld a, [hl]
ld [temp_note_value+1], a
;; TODO: Generalize this somehow?
ld hl, _play_note_routines
ld a, b
add a
add_a_to_hl
jp hl
fx_set_speed:
ret nz
;; A: tick
;; ZF: (tick == 0)
;; B: channel
;; C: effect parameters
;; free registers: A, D, E, H, L
ld a, c
ld [ticks_per_row], a
ret
hUGE_set_position::
fx_pos_jump:
;; A: tick
;; ZF: (tick == 0)
;; B: channel
;; C: effect parameters
;; free registers: A, D, E, H, L
ld a, 1
ld [row_break], a
ld a, c
ld [next_order], a
ret
fx_pattern_break:
;; A: tick
;; ZF: (tick == 0)
;; B: channel
;; C: effect parameters
;; free registers: A, D, E, H, L
ld a, c
ld [row_break], a
ret
fx_note_cut:
;; A: tick
;; ZF: (tick == 0)
;; B: channel
;; C: effect parameters
;; free registers: A, D, E, H, L
cp c
ret nz
;; check channel mute
ld d, 1
ld a, b
or a
jr z, .cont
.loop:
sla d
dec a
jr nz, .loop
.cont:
ld a, [mute_channels]
and d
ret nz
note_cut:
ld hl, rAUD1ENV
ld a, b
add a
add a
add b ; multiply by 5
add_a_to_hl
ld [hl], 0
ld a, b
cp 2
ret z ; return early if CH3-- no need to retrigger note
;; Retrigger note
inc hl
inc hl
ld [hl], %11111111
ret
fx_set_volume:
ret nz ;; Return if we're not on tick zero.
;; A: tick
;; ZF: (tick == 0)
;; B: channel
;; C: effect parameters
;; free registers: A, D, E, H, L
;; Arguments to this effect will be massaged to correct form for the channel
;; in the editor so we don't have to AND and SWAP and stuff.
set_channel_volume:
;; Call with:
;; Correct volume value in C
;; Channel number in B
ld a, b
cp 3 ; check if it's channel 4
jr z, set_chn_4_vol
cp 1 ; check if it's channel 2
jr c, set_chn_1_vol
jr z, set_chn_2_vol
set_chn_3_vol:
retMute 2
;; "Quantize" the more finely grained volume control down to one of 4 values.
ld a, c
cp 10
jr nc, .one
cp 5
jr nc, .two
or a
jr z, .zero
.three:
ld a, %01100000
jr .done
.two:
ld a, %01000000
jr .done
.one:
ld a, %00100000
jr .done
.zero:
xor a
.done:
ld [rAUD3LEVEL], a
ret
set_chn_2_vol:
retMute 1
ld a, [rAUD2ENV]
and %00001111
swap c
or c
ld [rAUD2ENV], a
ret
set_chn_1_vol:
retMute 0
ld a, [rAUD1ENV]
and %00001111
swap c
or c
ld [rAUD1ENV], a
ret
set_chn_4_vol:
retMute 3
swap c
ld a, c
ld [rAUD4ENV], a
ret
fx_vibrato:
ret z
;; A: tick
;; ZF: (tick == 0)