Skip to content

Commit

Permalink
nss: opcodes for ADPCM-A/B volume
Browse files Browse the repository at this point in the history
  • Loading branch information
dciabrin committed Jun 25, 2024
1 parent a9c69e0 commit 5d51a4d
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 21 deletions.
130 changes: 111 additions & 19 deletions nullsound/nss-adpcm.s
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

;;; ADPCM playback state tracker
;;; ------
_state_adpcm_start:

;;; context: current adpcm channel for opcode actions
state_adpcm_a_channel::
Expand All @@ -44,6 +45,12 @@ state_adpcm_a_channel::
state_adpcm_b_start_cmd::
.db 0

;;; current volumes for ADPCM-A channels
state_adpcm_a_vol:: .blkb 6
;;; current volumes for ADPCM-B channel
state_adpcm_b_vol:: .blkb 1

_state_adpcm_end:

.area CODE

Expand All @@ -56,6 +63,16 @@ init_nss_adpcm_state_tracker::
ld (state_adpcm_a_channel), a
ld a, #0x80 ; start flag
ld (state_adpcm_b_start_cmd), a
;; default volumes
ld a, #0x1f
ld (state_adpcm_a_vol), a
ld (state_adpcm_a_vol+1), a
ld (state_adpcm_a_vol+2), a
ld (state_adpcm_a_vol+3), a
ld (state_adpcm_a_vol+4), a
ld (state_adpcm_a_vol+5), a
ld a, #0xff
ld (state_adpcm_b_vol), a
ret

;;; Reset ADPCM-A playback state.
Expand Down Expand Up @@ -203,6 +220,26 @@ _adpcm_a_loop:
dec d
jp nz, _adpcm_a_loop

;; d: ADPCM-A channel
ld a, (state_adpcm_a_channel)
ld d, a

;; b: volume register for this channel
ld a, #REG_ADPCM_A1_PAN_VOLUME
add d
ld b, a

;; c: current channel volume (8bit add)
ld hl, #state_adpcm_a_vol
ld a, l
add d
ld l, a
ld a, (hl)
or #0xc0 ; default pan (L+R)
ld c, a

call ym2610_write_port_b

pop de
pop hl
pop bc
Expand Down Expand Up @@ -233,15 +270,6 @@ adpcm_a_on::
ld a, (state_adpcm_a_channel)
ld d, a

;; TODO remove this default pan+volume
ld a, #REG_ADPCM_A1_PAN_VOLUME
add d
ld b, a
ld a, #0x1d ; default vol
or #0xc0 ; default pan (L+R)
ld c, a
call ym2610_write_port_b

;; a: bitwise channel
ld a, #0
inc d
Expand Down Expand Up @@ -317,6 +345,46 @@ _off_bit:
ret


;;; ADPCM_A_VOL
;;; Set playback volume of the current ADPCM-A channel
;;; ------
adpcm_a_vol::
push bc

;; c: volume
ld c, (hl)
inc hl

push hl

;; hl: current volume for channel (bit add)
ld hl, #state_adpcm_a_vol
ld a, (state_adpcm_a_channel)
add l
ld l, a
;; update current volume for channel
ld a, c
ld (hl), a

;; b: ADPCM-A channel
ld a, (state_adpcm_a_channel)
add a, #REG_ADPCM_A1_PAN_VOLUME
ld b, a

;; c: volume + default pan (L/R)
ld a, c
or #0xc0
ld c, a

;; set volume for channel in the YM2610
call ym2610_write_port_b

pop hl
pop bc
ld a, #1
ret


;;; ADPCM_B_INSTRUMENT
;;; Configure the ADPCM-B channel based on an instrument's data
;;; ------
Expand Down Expand Up @@ -367,6 +435,17 @@ _adpcm_b_loop:
_adpcm_b_post_loop_chk:
ld (state_adpcm_b_start_cmd), a

;; set a default pan
ld b, #REG_ADPCM_B_PAN
ld c, #0xc0 ; default pan (L+R)
call ym2610_write_port_a

;; current volume
ld b, #REG_ADPCM_B_VOLUME
ld a, (state_adpcm_b_vol)
ld c, a
call ym2610_write_port_a

pop de
pop hl
pop bc
Expand Down Expand Up @@ -415,16 +494,6 @@ adpcm_b_note_on::
ld c, #1 ; reset flag (clears start and repeat in YM2610)
call ym2610_write_port_a

;; TODO remove this default pan
ld b, #REG_ADPCM_B_PAN
ld c, #0xc0 ; default pan (L+R)
call ym2610_write_port_a

;; TODO remove this default volume
ld b, #REG_ADPCM_B_VOLUME
ld c, #0xff ; default vol
call ym2610_write_port_a

;; a: semitone
ld a, d
and #0xf
Expand Down Expand Up @@ -502,3 +571,26 @@ adpcm_b_note_off::
pop bc
ld a, #1
ret


;;; ADPCM_B_VOL
;;; Set playback volume of the ADPCM-B channel
;;; ------
adpcm_b_vol::
push bc

;; a: volume
ld a, (hl)
inc hl

;; new configured volume for ADPCM-B
ld (state_adpcm_b_vol), a

;; set volume in the YM2610
ld b, #REG_ADPCM_B_VOLUME
ld c, a
call ym2610_write_port_a

pop bc
ld a, #1
ret
2 changes: 2 additions & 0 deletions nullsound/stream.s
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ nss_opcodes:
.dw fm_vibrato
.dw fm_slide_up
.dw fm_slide_down
.dw adpcm_b_vol
.dw adpcm_a_vol


;;; Process a single NSS opcode
Expand Down
10 changes: 8 additions & 2 deletions tools/nsstool.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ def register_nss_ops():
("fm_vibrato", ["speed_depth"]),
("fm_slide_u", ["speed_depth"]),
("fm_slide_d", ["speed_depth"]),
("b_vol" , ["volume"]),
("a_vol" , ["volume"]),
# reserved opcodes
("nss_label", ["pat"])
)
Expand Down Expand Up @@ -377,7 +379,9 @@ def convert_a_row(row, channel, opcodes):
# instrument
if row.ins != -1:
opcodes.append(a_instr(row.ins))
# TODO volume
# volume
if row.vol != -1:
opcodes.append(a_vol(row.vol))
# effects
for fx, fxval in row.fx:
if fx == -1: # empty fx
Expand All @@ -403,7 +407,9 @@ def convert_b_row(row, channel, opcodes):
# instrument
if row.ins != -1:
opcodes.append(b_instr(row.ins))
# TODO volume
# volume
if row.vol != -1:
opcodes.append(b_vol(row.vol))
# effects
for fx, fxval in row.fx:
if fx == -1: # empty fx
Expand Down

0 comments on commit 5d51a4d

Please sign in to comment.