Skip to content

Commit

Permalink
Add framework to support more than one active RF5C68 channel
Browse files Browse the repository at this point in the history
  • Loading branch information
viciious committed Jun 12, 2024
1 parent a734bb5 commit d5721e9
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 53 deletions.
11 changes: 9 additions & 2 deletions src-md/crt0.s
Original file line number Diff line number Diff line change
Expand Up @@ -2608,6 +2608,7 @@ bump_fm:
move.w RF5C68_LAH.w,rf5c68_loop_ofs
move.b RF5C68_ENV.w,rf5c68_envelope
move.b RF5C68_CHN.w,rf5c68_chanmask
move.b RF5C68_CTL.w,rf5c68_chanctl
bra.b 7f

6:
Expand Down Expand Up @@ -2644,7 +2645,7 @@ bump_fm:
move.l d1,-(sp)
move.l fm_stream_len,-(sp)
move.l fm_stream_ofs,-(sp)
jsr vgm_play_samples
jsr vgm_play_dac_samples
lea 12(sp),sp
bra.b 18f
17:
Expand All @@ -2670,8 +2671,12 @@ bump_fm:
move.l d1,-(sp)
move.w rf5c68_start,d1
move.l d1,-(sp)
moveq #0,d1
move.b rf5c68_chanctl,d1
andi.b #0x7,d1
move.l d1,-(sp)
jsr vgm_play_rf5c68_samples
lea 16(sp),sp
lea 20(sp),sp
20:
move.w (sp)+,sr /* restore int level */
rts
Expand Down Expand Up @@ -3170,6 +3175,8 @@ crsr_y:
dc.w 0
dbug_color:
dc.w 0
rf5c68_chanctl:
dc.b 0

.text
.align 4
Expand Down
48 changes: 26 additions & 22 deletions src-md/vgm.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
#include "lzss.h"
#include "scd_pcm.h"

#define VGM_MCD_BUFFER_ID 127
#define VGM_MCD_SOURCE_ID 8
#define VGM_MCD_BUFFER_ID 126
#define VGM_MCD_SOURCE_ID 7

#define VGM_MCD_BUFFER_ID2 127
#define VGM_MCD_SOURCE_ID2 8

#define VGM_WORDRAM_OFS 0x3000
#define VGM_READAHEAD 0x200
Expand Down Expand Up @@ -176,25 +179,19 @@ void *vgm_cache_scd(const char *name, int offset, int length)
return ptr;
}

#ifndef VGM_USE_PWM_FOR_DAC
void vgm_play_scd_samples(int offset, int length, int freq)
{
void *ptr = (char *)MCD_WORDRAM_VGM_PTR + pcm_baseoffs + offset;

scd_queue_setptr_buf(VGM_MCD_BUFFER_ID, ptr, length);
scd_queue_play_src(VGM_MCD_SOURCE_ID, VGM_MCD_BUFFER_ID, freq, 128, 255, 0);
}
#endif

void vgm_play_samples(int offset, int length, int freq)
void vgm_play_dac_samples(int offset, int length, int freq)
{
#ifdef VGM_USE_PWM_FOR_DAC
extern uint16_t dac_freq, dac_len, dac_center;
extern void *dac_samples;

if (!cd_ok)
return;

if (dac_freq != freq)
{
int cycle;
Expand All @@ -216,28 +213,26 @@ void vgm_play_samples(int offset, int length, int freq)

dac_samples = (char *)MD_WORDRAM_VGM_PTR + pcm_baseoffs + offset;
dac_len = length;
#else
if (cd_ok)
vgm_play_scd_samples(offset, length, freq);
#endif
}

void vgm_stop_samples(void)
void vgm_stop_dac_samples(void)
{
#ifdef VGM_USE_PWM_FOR_DAC
extern uint16_t dac_len, dac_center;

dac_len = 0;
MARS_PWM_MONO = dac_center;
MARS_PWM_MONO = dac_center;
MARS_PWM_MONO = dac_center;
#else
if (cd_ok)
scd_queue_stop_src(VGM_MCD_SOURCE_ID);
#endif
}

void vgm_play_rf5c68_samples(int offset, int loopstart, int incr, int vol)
void vgm_stop_rf5c68_samples(int chan)
{
if (chan != 0 && chan != 1)
return;
scd_queue_stop_src(VGM_MCD_SOURCE_ID+chan);
}

void vgm_play_rf5c68_samples(int chan, int offset, int loopstart, int incr, int vol)
{
//int freq = ((uint16_t)incr * 32604) >> 11;
int freq = incr << 4; // good enough
Expand All @@ -246,7 +241,16 @@ void vgm_play_rf5c68_samples(int offset, int loopstart, int incr, int vol)

if (!cd_ok || !incr)
return;
if (chan != 0 && chan != 1)
return;

scd_queue_setptr_buf(VGM_MCD_BUFFER_ID, ptr, length);
scd_queue_play_src(VGM_MCD_SOURCE_ID, VGM_MCD_BUFFER_ID, freq, 128, vol, 0);
scd_queue_setptr_buf(VGM_MCD_BUFFER_ID+chan, ptr, length);
scd_queue_play_src(VGM_MCD_SOURCE_ID+chan, VGM_MCD_BUFFER_ID+chan, freq, 128, vol, 0);
}

void vgm_stop_samples(void)
{
vgm_stop_rf5c68_samples(0);
vgm_stop_rf5c68_samples(1);
vgm_stop_dac_samples();
}
85 changes: 56 additions & 29 deletions src-md/z80_vgm.s80
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ RF5C68_LAH EQU 0x15
RF5C68_STA EQU 0x16
RF5C68_CTL EQU 0x17
RF5C68_CHN EQU 0x18
RF5C68_CNT EQU 0x19

HLBACKUP EQU $1A ; backup space for HL register

Expand Down Expand Up @@ -126,6 +127,9 @@ start_loop
OR A
JR Z,start_loop ; not playing VGM music

LD A,0
LD (RF5C68_CNT),A

LD BC,(LOCAL_MEM+FM_START)
LD HL,VGMBUF
ADD HL,BC
Expand Down Expand Up @@ -711,34 +715,7 @@ set_bank

RET

; rf5c68 register
rf5c68_regwr
LD A,(IX+FM_RQPND)
OR A
JR NZ,rf5c68_regwr ; request still pending

CALL strm_get ; parameter
PUSH AF
CALL strm_get ; parameter value
PUSH AF

LD (HLBACKUP),HL
LD HL,RF5C68_ENV

POP AF ; parameter value
LD B,A
POP AF ; parameter
LD C,A
LD A,B
LD B,0
ADD HL,BC
LD (HL),A
LD HL,(HLBACKUP)

LD A,(HL)
CP $B0
JP Z,play_loop

rf5c68_action:
LD A,$FF
LD (REQ_ACT),A ; request action - locked
LD (IX+FM_RQPND),A ; request pending
Expand Down Expand Up @@ -768,8 +745,58 @@ rf5c68_regwr
LD A,(RF5C68_CHN)
LD (MD_RF5C68_CHN),A

LD A,0x0
LD (RF5C68_CNT),A

LD A,$06
LD (REQ_ACT),A ; request action - stream start
JP play_loop

RET

; rf5c68 register
rf5c68_regwr
LD A,(IX+FM_RQPND)
OR A
JR NZ,rf5c68_regwr ; request still pending

CALL strm_get ; parameter
PUSH AF

CP 0x07 ; if it's not a channel control, cont reading
JR NZ,rf5c68_regwr_2

LD A,(RF5C68_CNT) ; if we don't have any commands buffered, cont reading
CP 0
JR Z,rf5c68_regwr_2

CALL rf5c68_action ; send buffered commands to the 68000
LD A,0x07

rf5c68_regwr_2:
CALL strm_get ; parameter value
PUSH AF

LD (HLBACKUP),HL
LD HL,RF5C68_ENV

POP AF ; parameter value
LD B,A
POP AF ; parameter
LD C,A
LD A,B
LD B,0
ADD HL,BC
LD (HL),A
LD HL,(HLBACKUP)

LD A,(RF5C68_CNT)
INC A
LD (RF5C68_CNT),A

LD A,(HL)
CP $B0
JP Z,play_loop

CALL rf5c68_action
JP play_loop
#end

0 comments on commit d5721e9

Please sign in to comment.