;;;
;;; nullsound - modular sound driver
;;; Copyright (c) 2024-2025 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 .
;;; The following driver is based on doc found on neogeodev wiki
;;; https://wiki.neogeodev.org/index.php?title=68k/Z80_communication
.module nullsound
.include "ym2610.inc"
.include "ports.inc"
.include "timer.inc"
.include "struct-fx.inc"
.include "pipeline.inc"
;;;
;;; Volume state tracker
;;; -------------------
;;; Keep track of the global volume and fade out state
;;;
.area DATA
;;; Current global level for music [0..15]
;;; -------------------
;;; This acts as a way of controlling how loud music playback is
;;; The global level serves as the basic to 4 independent levels
;;; (resp. FM, SSG, ADPCM-A and ADPCM-B) because all these channels
;;; have different level sensitivity (resp. 128, 16, 32, 256) as
;;; well as different volume decay profiles.
;;; The volume capabilities are modelled against the SSG properties,
;;; because that's the most limiting type of sound in the YM2610.
;;; . There are only 16 levels because that's what SSG can output
;;; . The volume decay for SSG follows an exponential curve:
;;; volume gets half as loud every two steps. FM follows a faster
;;; curve, ADPCM-A a slower one. ADPCM-B decays linearly.
;;; . to accomodate those decays, we define a volume ramp for
;;; each channel type, so every 16 levels can map to a consistent
;;; volume drop across channels.
state_volume_music_level:: .blkb 1
;;; Are music and SFX currently muted
state_volume_muted:: .blkb 1
;;; Music level catpured at the time the volume was last muted
state_volume_mute_level:: .blkb 1
;;; Master level attenuation for ADPCM-A
;;; NOTE: the lowest volume achievable per channel with ADPCM-A seems
;;; to be a bit too loud, so for the time being the fade out action
;;; relies on the master ADPCM-A volume to implement the effect.
;;; This has the unfortunate side effect that SFX are faded out at the
;;; same time.
state_volume_adpcm_a_master:: .blkb 1
;;; Music fade out in progress
state_volume_fade_out:: .blkb 1
;;; Current music volume during the fade out
;;; this is a fixed point value (4bit integer + 2bit fractional) that
;;; goes from the current music level down to 0, for a total of 64
;;; possible levels for channels with that capability.
;;; the level is updated at every music tick
state_volume_fade_progress:: .blkb 1
;;; Fade out speed. Fixed point value (4bit integer + 2bit fractional)
state_volume_fade_speed:: .blkb 1
.area CODE
;;; SSG: the linear decrease ramp [0..0xf] yields an exponential volume decay
state_volume_ssg_ramp::
.db 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, 0x00
;;; FM: scaled linear decrease [0..0x7f] to approximate the exponential volume decay from SSG
state_volume_fm_ramp::
.db 0x7f, 0x40, 0x37, 0x33, 0x2e, 0x2a, 0x26, 0x22, 0x1d, 0x19, 0x15, 0x11, 0x0c, 0x08, 0x04, 0x00, 0x00
;;; ADPCM-A: scaled linear decrease [0..0x1f] to approximate the exponential volume decay from SSG
;;; NOTE: MAME seems to have a problem with 0x1f? some channels play a faint volume, others cut it
state_volume_adpcm_a_ramp::
.db 0x1f, 0x1e, 0x1e, 0x1d, 0x1d, 0x1c, 0x1c, 0x1b, 0x1a, 0x18, 0x15, 0x12, 0x0b, 0x07, 0x03, 0x00, 0x00
;;; ADPCM-B: scaled log decrease [0..0x40] to approximate the exponential volume decay from SSG
state_volume_adpcm_b_ramp::
.db 0x40, 0x3f, 0x3f, 0x3e, 0x3e, 0x3d, 0x3d, 0x3c, 0x3b, 0x39, 0x35, 0x31, 0x2a, 0x21, 0x13, 0x0, 0x0
;;; ADPCM-A master: linear volume decay [0..0x3f]
state_volume_adpcm_a_master_ramp::
.db 0x3f, 0x3b, 0x37, 0x33, 0x2e, 0x2a, 0x26, 0x22, 0x1d, 0x19, 0x15, 0x11, 0x0c, 0x08, 0x04, 0x00, 0x00
init_volume_state_tracker::
;; reset music volume to max (no attenuation), start unmuted
ld a, #0x0f
ld (state_volume_music_level), a
ld (state_volume_mute_level), a
xor a
ld (state_volume_muted), a
;; reset channel levels
ld a, #0
ld (state_fm_volume_attenuation), a
ld (state_ssg_volume_attenuation), a
ld (state_adpcm_a_volume_attenuation), a
ld (state_adpcm_b_volume_attenuation), a
ld (state_volume_adpcm_a_master), a
;; reset fade out operation
ld a, #0
ld (state_volume_fade_out), a
ld (state_volume_fade_progress), a
ld a, #1
ld (state_volume_fade_speed), a
ret
;;; Reset music output levels
;;; ------
;;; [a modified - other registers saved]
volume_reset_music_levels::
push bc
push de
push hl
;; disable any ongoing fade out
ld a, #0
ld (state_volume_fade_out), a
ld (state_volume_fade_progress), a
ld a, #1
ld (state_volume_fade_speed), a
;; reset ADPCM-A master attenuation
ld a, (state_volume_muted)
cp #0
jr nz, _post_reset_master_a
ld a, #0
ld (state_volume_adpcm_a_master), a
_post_reset_master_a:
;; reset channels level based on current music level
call volume_update_channels_levels
call volume_update_stream_state
pop hl
pop de
pop bc
ret
;;; Reconfigure each channel's output level after the music level
;;; was updated by a volume up/down operation
;;; ------
;;; [a modified - other registers saved]
volume_update_channels_levels:
push hl
push bc
ld a, (state_volume_music_level)
ld c, a
ld b, #0
ld hl, #state_volume_fm_ramp
add hl, bc
ld a, (hl)
ld (state_fm_volume_attenuation), a
ld hl, #state_volume_ssg_ramp
add hl, bc
ld a, (hl)
ld (state_ssg_volume_attenuation), a
ld hl, #state_volume_adpcm_a_ramp
add hl, bc
ld a, (hl)
ld (state_adpcm_a_volume_attenuation), a
ld hl, #state_volume_adpcm_b_ramp
add hl, bc
ld a, (hl)
ld (state_adpcm_b_volume_attenuation), a
pop bc
pop hl
ret
;;; Return an interpolated output level from a output level ramp
;;; . The position in the ramp is given by a fixed point level
;;; (4bit integer + 2bit fractional)
;;; . The output level is the interpolated level between the two
;;; adjacent integer levels in the ramp
;;; ------
;;; hl: output level ramp
;;; a: fixed point level
;;; [a, bc, de, hl modified]
volume_level_from_ramp::
;; bc: 16bit-extended level
ld a, (state_volume_fade_progress)
sra a
sra a
ld c, a
ld b, #0
;; d: volume distance between level+1 and level
inc bc
add hl, bc
ld d, (hl)
dec hl
ld a, (hl)
sub d
ld d, a
;; e: intermediate pos between level and level+1 [0..3]
ld a, (state_volume_fade_progress)
and #0x3
ld e, a
;; a: pos * distance => result between [0..3*distance]
ld a, #0
bit 0, e
jr z, _fade_post_bit0
add d
_fade_post_bit0:
sla d
bit 1, e
jr z, _fade_post_bit1
add d
_fade_post_bit1:
;; d: scale distance back to [0..distance]
sra a
sra a
ld d, a
;; a: level distance with intermediate distance
ld a, (hl)
sub d
ret
;;; Update currently playing notes in the YM2610 to reflect how
;;; the channels' output levels are currently configured in nullsound
;;; ------
;;; [a, de, bc, hl, modified]
volume_update_stream_state:
push iy
;; d: FM + SSG channels in use
ld a, (state_ch_bits)
ld d, a
;; Notify the FM pipeline
bit 0, d
jr z, _vol_upd_post_fm1
ld iy, #state_fm1_pipeline
set BIT_LOAD_VOL, PIPELINE(iy)
_vol_upd_post_fm1:
bit 1, d
jr z, _vol_upd_post_fm2
ld iy, #state_fm2_pipeline
set BIT_LOAD_VOL, PIPELINE(iy)
_vol_upd_post_fm2:
bit 2, d
jr z, _vol_upd_post_fm3
ld iy, #state_fm3_pipeline
set BIT_LOAD_VOL, PIPELINE(iy)
_vol_upd_post_fm3:
bit 3, d
jr z, _vol_upd_post_fm4
ld iy, #state_fm4_pipeline
set BIT_LOAD_VOL, PIPELINE(iy)
_vol_upd_post_fm4:
;; Notify the SSG pipeline
bit 4, d
jr z, _vol_upd_post_ssg_a
ld iy, #state_mirrored_ssg_a
set BIT_LOAD_VOL, PIPELINE_FROM_NOTE(iy)
_vol_upd_post_ssg_a:
bit 5, d
jr z, _vol_upd_post_ssg_b
ld iy, #state_mirrored_ssg_b
set BIT_LOAD_VOL, PIPELINE_FROM_NOTE(iy)
_vol_upd_post_ssg_b:
bit 6, d
jr z, _vol_upd_post_ssg_c
ld iy, #state_mirrored_ssg_c
set BIT_LOAD_VOL, PIPELINE_FROM_NOTE(iy)
_vol_upd_post_ssg_c:
;; d: ADPCM channels in use
ld a, (state_ch_bits+1)
ld d, a
;; Loop over all the ADPCM-A channels that need to be updated
;; e: total number of ADPCM-A channels
ld e, #6
ld iy, #state_a_pipeline
ld a, #