;;; ;;; nullsound - modular sound driver ;;; Copyright (c) 2023-2026 Damien Ciabrini ;;; This file is part of ngdevkit ;;; ;;; ngdevkit is free software: you can redistribute it and/or modify ;;; it under the terms of the GNU Lesser General Public License as ;;; published by the Free Software Foundation, either version 3 of the ;;; License, or (at your option) any later version. ;;; ;;; ngdevkit is distributed in the hope that it will be useful, ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;; GNU Lesser General Public License for more details. ;;; ;;; You should have received a copy of the GNU Lesser General Public License ;;; along with ngdevkit. If not, see . ;;; NSS opcode for ADPCM channels ;;; .module nullsound .include "align.inc" .include "ym2610.inc" .include "struct-fx.inc" .include "pipeline.inc" .lclequ ADPCM_A_STATE_SIZE,(state_a_end-state_a_start) .lclequ ADPCM_A_MAX_VOL,0x1f ;; getters for ADPCM-A state .lclequ INSTR_VOL, (state_a_instr_vol-state_a) .lclequ OUT_VOL, (state_a_out_vol-state_a) .lclequ PAN, (state_a_pan-state_a) .equ NSS_ADPCM_A_INSTRUMENT_PROPS, 4 .equ NSS_ADPCM_A_NEXT_REGISTER, 8 ;; specific pipeline state for ADPCM-A channel .lclequ STATE_START_NOTE, 0x04 .lclequ BIT_START_NOTE, 2 .area DATA ;;; ADPCM playback state tracker ;;; ------ .align_begin state_adpcm ;;; { ... ;;; ADPCM-A1 state_a1: ;;; { ... state_a_start: ;;; volume state tracker state_a_vol_cfg: .blkb 1 ; configured volume state_a_vol16: .blkb 2 ; current decimal volume ;;; FX state trackers state_a_fx: .blkb 1 ; enabled FX for this channel state_a_fx_vol_slide: .blkb SLIDE_SIZE state_a_fx_trigger: .blkb TRIGGER_SIZE ;;; actions to run at the end of every tick state_a: state_a_pipeline: .blkb 1 ; action: load note, load vol, load other regs ;;; ADPCM-A-specific state state_a_instr_vol: .blkb 1 ; instrument volume state_a_out_vol: .blkb 1 ; ym2610 volume after the FX pipeline ;;; pan state_a_pan: .blkb 1 ; configured pan (b7: left, b6: right) ;;; state_a_end: ;;; ADPCM-A2 state_a2: .blkb ADPCM_A_STATE_SIZE ;;; ADPCM-A3 state_a3: .blkb ADPCM_A_STATE_SIZE ;;; ADPCM-A4 state_a4: .blkb ADPCM_A_STATE_SIZE ;;; ADPCM-A5 state_a5: .blkb ADPCM_A_STATE_SIZE ;;; ADPCM-A6 state_a6: .blkb ADPCM_A_STATE_SIZE ;;; ... } state_a6_end: ;;; ... } .align_end state_adpcm ;;; context: current adpcm channel for opcode actions state_adpcm_a_channel:: .blkb 1 ;;; Global volume attenuation for all ADPCM-A channels state_adpcm_a_volume_attenuation:: .blkb 1 .area CODE ;;; context: channel action functions for FM state_a_action_funcs:: .dw adpcm_a_configure_on .dw adpcm_a_configure_vol .dw adpcm_a_stop_playback ;;; Reset ADPCM playback state. ;;; Called before playing a stream ;;; ------ ;;; [a modified - other registers saved] init_nss_adpcm_state_tracker:: ld hl, #state_a_start ld d, h ld e, l inc de ;; zero states ld (hl), #0 ld bc, #(state_a6_end-1-state_a_start) ldir ;; init flags ld iy, #state_a_pipeline ld bc, #ADPCM_A_STATE_SIZE ld d, #6 _a_init: ;; set default ld VOL_CTX+SLIDE_MAX(iy), #ADPCM_A_MAX_VOL ; max volume for channel ld PAN(iy), #0xc0 ; default PAN to L+R ld INSTR_VOL(iy), #0xff ; default volume to force a VOL load add iy, bc dec d jr nz, _a_init ;; global ADPCM volumes are initialized in the volume state tracker ret ;;; Reset ADPCM-A playback state. ;;; Called before waiting for the next tick ;;; ------ ;;; [a modified - other registers saved] adpcm_a_ctx_reset:: ld a, #0 ld (state_adpcm_a_channel), a ret ;;; Set the current ADPCM-A track and YM2610 load function for this track ;;; ------ ;;; a : ADPCM-A channel adpcm_a_ctx_set_current:: ;; set ADPCM-A context ld (state_adpcm_a_channel), a ;; set ADPCM-A struct pointer for context ld ix, #state_a push bc bit 0, a jr z, _adpcm_a_ctx_post_bit0 ld bc, #ADPCM_A_STATE_SIZE add ix, bc _adpcm_a_ctx_post_bit0: bit 1, a jr z, _adpcm_a_ctx_post_bit1 ld bc, #ADPCM_A_STATE_SIZE*2 add ix, bc _adpcm_a_ctx_post_bit1: bit 2, a jr z, _adpcm_a_ctx_post_bit2 ld bc, #ADPCM_A_STATE_SIZE*4 add ix, bc _adpcm_a_ctx_post_bit2: pop bc ;; return 1 to follow NSS processing semantics ld a, #1 ret ;;; run_adpcm_a_pipeline ;;; ------ ;;; Run the entire ADPCM-A pipeline once. for each ADPCM-A channels: ;;; - update the state of all enabled FX ;;; - load specific parts of the state (pan, vol...) into ADPCM-A registers ;;; Meant to run once per tick run_adpcm_a_pipeline:: push de push iy push ix ;; we loop though every channel during the execution, ;; so save the current channel context ld a, (state_adpcm_a_channel) push af ;; update state of all ADPCM-A channels, starting from A1 xor a _a_update_loop: call adpcm_a_ctx_set_current ;; bail out if the current channel is not in use ld a, PIPELINE(ix) or a, FX(ix) cp #0 jp z, _end_a_channel_pipeline ;; Pipeline action: evaluate one FX step for each enabled FX bit BIT_FX_TRIGGER, FX(ix) jr z, _a_post_fx_trigger ld hl, #state_a_action_funcs call eval_trigger_step _a_post_fx_trigger: ;; iy: volume FX state for channel push ix pop iy ld bc, #VOL_CTX add iy, bc bit BIT_FX_SLIDE, FX(ix) jr z, _a_post_fx_vol_slide call eval_slide_step set BIT_LOAD_VOL, PIPELINE(ix) _a_post_fx_vol_slide: ;; Pipeline action: make sure no load note takes place when not playing bit BIT_PLAYING, PIPELINE(ix) jr nz, _a_post_check_playing res BIT_START_NOTE, PIPELINE(ix) _a_post_check_playing: ;; Pipeline action: compute volume registers when the volume state is modified bit BIT_LOAD_VOL, PIPELINE(ix) jr z, _post_load_a_vol call compute_ym2610_a_vol _post_load_a_vol: ;; Pipeline action: load pan+volume register when it is modified ld a, PIPELINE(ix) and a, #(STATE_LOAD_VOL|STATE_LOAD_PAN) jr z, _post_load_a_pan_vol res BIT_LOAD_VOL, PIPELINE(ix) res BIT_LOAD_PAN, PIPELINE(ix) ;; c: volume + pan ld a, OUT_VOL(ix) add PAN(ix) ld c, a ;; set volume + pan for channel in the YM2610 ;; b: ADPCM-A channel ld a, (state_adpcm_a_channel) add a, #REG_ADPCM_A1_PAN_VOLUME ld b, a call ym2610_write_port_b _post_load_a_pan_vol: ;; Pipeline action: load note register when the note state is modified bit BIT_START_NOTE, PIPELINE(ix) jr z, _post_load_a_note res BIT_START_NOTE, PIPELINE(ix) ;; d: ADPCM-A channel ld a, (state_adpcm_a_channel) ld d, a ;; a: bitwise channel ld a, #0 inc d scf _a_on_bit: rla dec d jp nz, _a_on_bit ;; start channel ld b, #REG_ADPCM_A_START_STOP ld c, a call ym2610_write_port_b _post_load_a_note: _end_a_channel_pipeline: ;; next context ld a, (state_adpcm_a_channel) inc a cp #6 jr nc, _a_end_pipeline jp _a_update_loop _a_end_pipeline: ;; restore the real channel context pop af call adpcm_a_ctx_set_current pop ix pop iy pop de ret ;;; ADPCM NSS opcodes ;;; ------ ;;; ADPCM_A_CTX_1 ;;; Set the current ADPCM-A context to channel 1 ADPCM-A opcode processing ;;; ------ adpcm_a_ctx_1:: ;; set new current ADPCM-A channel ld a, #0 jp adpcm_a_ctx_set_current ;;; ADPCM_A_CTX_2 ;;; Set the current ADPCM-A context to channel 2 ADPCM-A opcode processing ;;; ------ adpcm_a_ctx_2:: ;; set new current ADPCM-A channel ld a, #1 jp adpcm_a_ctx_set_current ;;; ADPCM_A_CTX_3 ;;; Set the current ADPCM-A context to channel 3 ADPCM-A opcode processing ;;; ------ adpcm_a_ctx_3:: ;; set new current ADPCM-A channel ld a, #2 jp adpcm_a_ctx_set_current ;;; ADPCM_A_CTX_4 ;;; Set the current ADPCM-A context to channel 4 ADPCM-A opcode processing ;;; ------ adpcm_a_ctx_4:: ;; set new current ADPCM-A channel ld a, #3 jp adpcm_a_ctx_set_current ;;; ADPCM_A_CTX_5 ;;; Set the current ADPCM-A context to channel 5 ADPCM-A opcode processing ;;; ------ adpcm_a_ctx_5:: ;; set new current ADPCM-A channel ld a, #4 jp adpcm_a_ctx_set_current ;;; ADPCM_A_CTX_6 ;;; Set the current ADPCM-A context to channel 6 ADPCM-A opcode processing ;;; ------ adpcm_a_ctx_6:: ;; set new current ADPCM-A channel ld a, #5 jp adpcm_a_ctx_set_current ;;; ADPCM_A_INSTRUMENT ;;; Configure an ADPCM-A channel based on an instrument's data ;;; ------ ;;; [ hl ]: instrument number adpcm_a_instrument:: push bc ;; b: ADPCM-A channel ld a, (state_adpcm_a_channel) ld b, a ;; a: instrument ld a, (hl) inc hl push hl push de ;; hl: instrument address in ROM ;; (ADPCM-A channel still saved in b) sla a ld c, a ld a, b ld b, #0 ld hl, (state_stream_instruments) add hl, bc ld b, a ld e, (hl) inc hl ld d, (hl) inc hl push de pop hl ;; d: all ADPCM-A properties ld d, #NSS_ADPCM_A_INSTRUMENT_PROPS ;; b: ADPCM-A channel ld a, (state_adpcm_a_channel) ld b, a ;; a: start of ADPCM-A property registers ld a, #REG_ADPCM_A1_ADDR_START_LSB add b _adpcm_a_loop: ld b, a ld c, (hl) call ym2610_write_port_b add a, #NSS_ADPCM_A_NEXT_REGISTER inc hl dec d jp nz, _adpcm_a_loop ;; instrument volume ld a, (hl) cp INSTR_VOL(ix) jp z, _adpcm_a_instr_post_vol ld INSTR_VOL(ix), a ;; reload configured vol at the next pipeline run set BIT_LOAD_VOL, PIPELINE(ix) _adpcm_a_instr_post_vol: pop de pop hl pop bc ld a, #1 ret ;;; Configure state for new note and trigger a load in the pipeline ;;; ------ adpcm_a_configure_on: ;; load a new note means "restart the current sample" ld a, PIPELINE(ix) or #(STATE_PLAYING|STATE_START_NOTE) ld PIPELINE(ix), a ret ;;; Configure state for new volume and trigger a load in the pipeline ;;; ------ adpcm_a_configure_vol: ;; if a volume slide is ongoing, treat it as a volume slide FX update bit BIT_FX_SLIDE, FX(ix) jr z, _a_cfg_vol_update push bc ld bc, #VOL_CTX call slide_update pop bc jr _a_cfg_vol_end _a_cfg_vol_update: ld VOL(ix), a ld VOL16+1(ix), a ld VOL16(ix), #0 ;; reload configured vol at the next pipeline run set BIT_LOAD_VOL, PIPELINE(ix) _a_cfg_vol_end: ret ;;; ADPCM_A_ON ;;; Start sound playback on the current ADPCM-A channel ;;; ------ adpcm_a_on:: ;; delay the start via the trigger FX? bit BIT_TRIGGER_ACTION_DELAY, TRIGGER_ACTION(ix) jr z, _a_on_immediate ;; ld TRIGGER_NOTE(ix), a set BIT_TRIGGER_LOAD_NOTE, TRIGGER_ACTION(ix) jr _a_on_end _a_on_immediate: ;; else load note immediately call adpcm_a_configure_on _a_on_end: ;; ADPCM-A context will now target the next channel ld a, (state_adpcm_a_channel) inc a ld (state_adpcm_a_channel), a ld a, #1 ret ;;; ADPCM_A_ON_AND_WAIT ;;; Start sound playback on the current ADPCM-A channel and ;;; immediately wait as many rows as the last wait ;;; ------ adpcm_a_on_and_wait:: ;; process a regular note opcode call adpcm_a_on ;; wait rows call wait_last_rows ret ;;; Release the note on a ADPCM-A channel and update the pipeline state ;;; ------ adpcm_a_stop_playback: push bc push de ;; d: ADPCM-A channel (1..6) ld a, (state_adpcm_a_channel) inc a ld d, a ;; a: bit channel + stop bit ld a, #0 scf _off_bit: rla dec d jp nz, _off_bit or #0x80 ;; start channel ld b, #0 ld c, a call ym2610_write_port_b pop de pop bc ret ;;; ADPCM_A_OFF ;;; Stop the playback on a ADPCM-A channel ;;; ------ adpcm_a_off:: call adpcm_a_stop_playback ;; ADPCM-A context will now target the next channel ld a, (state_adpcm_a_channel) inc a ld (state_adpcm_a_channel), a ld a, #1 ret ;;; adpcm_a_scale_output ;;; adjust a channel volume to match configured ADPCM-A output level ;;; the YM2610's ADPCM-A output level ramp follows an exponential ;;; curve, so we implement this output level attenuation via a basic ;;; substraction, clamped to 0. ;;; ------ ;;; a: input level [0x00..0x1f] ;;; modified: bc adpcm_a_scale_output:: ;; b: pan info ld c, a and #0xc0 ld b, a ;; c: volume info ld a, c and #0x1f ld c, a ;; attenuation to match the configured ADPCM-A output level ld a, (state_adpcm_a_volume_attenuation) neg add c bit 7, a jr nz, _adpcm_a_clamp_level ;; restore pan info add b ret _adpcm_a_clamp_level: ;; NOTE: ADPCM-A oddity: it seems that channels with volume set to 0 ;; still outputs something? For now, reset the pan to force mute ld a, #0 ret ;;; Compute the YM2610 output volume from the current channel ;;; ------ ;;; IN: ;;; ix: FM pipeline ;;; iy: Volume FX ;;; modified: c compute_ym2610_a_vol:: ;; c: instrument attenuation [-0x1f..0] ld a, #-0x1f add INSTR_VOL(ix) ld c, a ;; c: current volume after instrument attenuation [-0x1f..0x1f] ld a, VOL16+1(ix) add c ld c, a ;; attenuation to match the configured ADPCM-A output level ld a, (state_adpcm_a_volume_attenuation) neg add c bit 7, a jr z, _a_post_global_vol xor a _a_post_global_vol: ld OUT_VOL(ix), a ret ;;; ADPCM_A_VOL ;;; Register a pending volume change for the current ADPCM-A channel ;;; The next note to be played or instrument change will pick ;;; up this volume configuration change ;;; ------ ;;; [ hl ]: volume [0-0x1f] adpcm_a_vol:: ;; a: volume ld a, (hl) inc hl ;; delay load via the trigger FX? bit BIT_TRIGGER_ACTION_DELAY, TRIGGER_ACTION(ix) jr z, _a_vol_immediate ld TRIGGER_VOL(ix), a set BIT_TRIGGER_LOAD_VOL, TRIGGER_ACTION(ix) jr _a_vol_end _a_vol_immediate: ;; else load vol immediately call adpcm_a_configure_vol _a_vol_end: ld a, #1 ret ;;; ADPCM_A_DELAY ;;; Enable delayed trigger for the next note and volume ;;; (note and volume and played after a number of ticks) ;;; ------ ;;; [ hl ]: delay adpcm_a_delay:: call trigger_delay_init ld a, #1 ret ;;; ADPCM_A_CUT ;;; Record that the note being played must be stopped after some steps ;;; ------ ;;; [ hl ]: delay adpcm_a_cut:: call trigger_cut_init ld a, #1 ret ;;; ADPCM_A_RETRIGGER ;;; Enable another trigger of the current note after a number of steps ;;; ------ ;;; [ hl ]: delay adpcm_a_retrigger:: call trigger_retrigger_init ld a, #1 ret ;;; ADPCM_A_PAN ;;; Set the pan (l/r) for the current ADPCM-A channel ;;; ------ ;;; [ hl ]: pan (b7: left, b6: right) adpcm_a_pan:: ld a, (hl) inc hl ld PAN(ix), a set BIT_LOAD_PAN, PIPELINE(ix) ld a, #1 ret