mirror of
https://github.com/ekeeke/Genesis-Plus-GX.git
synced 2024-11-05 10:25:07 +01:00
. improved PSG core runtime accuracy
. improved PSG & FM cores synchronization at the end of frame
This commit is contained in:
parent
18a04500bc
commit
39dc73d406
@ -84,12 +84,12 @@ void blip_add( blip_buffer_t* s, int clocks, int delta )
|
|||||||
|
|
||||||
int blip_clocks_needed( const blip_buffer_t* s, int samples )
|
int blip_clocks_needed( const blip_buffer_t* s, int samples )
|
||||||
{
|
{
|
||||||
int fixed_needed;
|
/*int fixed_needed;
|
||||||
if ( samples > s->size )
|
if ( samples > s->size )
|
||||||
samples = s->size;
|
samples = s->size;*/
|
||||||
|
|
||||||
/* Fixed-point number of samples needed in addition to those in buffer */
|
/* Fixed-point number of samples needed in addition to those in buffer */
|
||||||
fixed_needed = samples * time_unit - s->offset;
|
int fixed_needed = samples * time_unit - s->offset;
|
||||||
|
|
||||||
/* If more are needed, convert to clocks and round up */
|
/* If more are needed, convert to clocks and round up */
|
||||||
return (fixed_needed <= 0) ? 0 : (fixed_needed - 1) / s->factor + 1;
|
return (fixed_needed <= 0) ? 0 : (fixed_needed - 1) / s->factor + 1;
|
||||||
@ -105,12 +105,9 @@ int blip_samples_avail( const blip_buffer_t* s )
|
|||||||
return s->offset >> time_bits;
|
return s->offset >> time_bits;
|
||||||
}
|
}
|
||||||
|
|
||||||
void blip_read_samples( blip_buffer_t* s, short out[], int count)
|
int blip_read_samples( blip_buffer_t* s, short out[])
|
||||||
{
|
{
|
||||||
/* can't read more than available */
|
int count = s->offset >> time_bits;
|
||||||
int avail = s->offset >> time_bits;
|
|
||||||
if ( count > avail )
|
|
||||||
count = avail;
|
|
||||||
|
|
||||||
if ( count )
|
if ( count )
|
||||||
{
|
{
|
||||||
@ -135,11 +132,12 @@ void blip_read_samples( blip_buffer_t* s, short out[], int count)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Copy remaining samples to beginning of buffer and clear the rest */
|
/* Copy remaining samples to beginning of buffer and clear the rest */
|
||||||
i = (s->offset >> time_bits) + buf_extra - count;
|
memmove( s->buf, &s->buf [count], buf_extra * sizeof (buf_t) );
|
||||||
memmove( s->buf, &s->buf [count], i * sizeof (buf_t) );
|
memset( &s->buf [buf_extra], 0, count * sizeof (buf_t) );
|
||||||
memset( &s->buf [i], 0, count * sizeof (buf_t) );
|
|
||||||
|
|
||||||
/* Remove samples */
|
/* Remove samples */
|
||||||
s->offset -= count * time_unit;
|
s->offset -= count * time_unit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,7 @@ int blip_samples_avail( const blip_buffer_t* );
|
|||||||
|
|
||||||
/* Reads at most n samples out of buffer into out, removing them from from
|
/* Reads at most n samples out of buffer into out, removing them from from
|
||||||
the buffer. */
|
the buffer. */
|
||||||
void blip_read_samples( blip_buffer_t*, short out [], int n);
|
int blip_read_samples( blip_buffer_t*, short out []);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
@ -309,13 +309,10 @@ static void RunNoise(int clock_length)
|
|||||||
SN76489.ToneFreqVals[3] = time - clock_length;
|
SN76489.ToneFreqVals[3] = time - clock_length;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SN76489_Update(INT16 *buffer, int length)
|
int SN76489_Update(INT16 *buffer, int clock_length)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* Determine how many clocks we need to run until 'length' samples are available */
|
|
||||||
int clock_length = blip_clocks_needed(blip, length);
|
|
||||||
|
|
||||||
/* Run noise first, since it might use current value of third tone frequency counter */
|
/* Run noise first, since it might use current value of third tone frequency counter */
|
||||||
RunNoise(clock_length);
|
RunNoise(clock_length);
|
||||||
|
|
||||||
@ -325,5 +322,10 @@ void SN76489_Update(INT16 *buffer, int length)
|
|||||||
|
|
||||||
/* Read samples into output buffer */
|
/* Read samples into output buffer */
|
||||||
blip_end_frame(blip, clock_length);
|
blip_end_frame(blip, clock_length);
|
||||||
blip_read_samples(blip, buffer, length);
|
return blip_read_samples(blip, buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
int SN76489_Sync(unsigned int samples)
|
||||||
|
{
|
||||||
|
return blip_clocks_needed(blip, samples);
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ extern void SN76489_GetContext(uint8 *data);
|
|||||||
extern uint8 *SN76489_GetContextPtr(void);
|
extern uint8 *SN76489_GetContextPtr(void);
|
||||||
extern int SN76489_GetContextSize(void);
|
extern int SN76489_GetContextSize(void);
|
||||||
extern void SN76489_Write(int data);
|
extern void SN76489_Write(int data);
|
||||||
extern void SN76489_Update(INT16 *buffer, int length);
|
extern int SN76489_Update(INT16 *buffer, int clock_length);
|
||||||
|
extern int SN76489_Sync(unsigned int samples);
|
||||||
extern void SN76489_BoostNoise(int boost);
|
extern void SN76489_BoostNoise(int boost);
|
||||||
|
|
||||||
#endif /* _SN76489_H_ */
|
#endif /* _SN76489_H_ */
|
@ -86,15 +86,14 @@ INLINE void psg_update(unsigned int cycles)
|
|||||||
{
|
{
|
||||||
if (cycles > psg_cycles_count)
|
if (cycles > psg_cycles_count)
|
||||||
{
|
{
|
||||||
/* samples to run */
|
/* clocks to run */
|
||||||
unsigned int samples = (cycles - psg_cycles_count + psg_cycles_ratio - 1) / psg_cycles_ratio;
|
unsigned int clocks = (cycles - psg_cycles_count + psg_cycles_ratio - 1) / psg_cycles_ratio;
|
||||||
|
|
||||||
/* update cycle count */
|
/* update cycle count */
|
||||||
psg_cycles_count += samples * psg_cycles_ratio;
|
psg_cycles_count += clocks * psg_cycles_ratio;
|
||||||
|
|
||||||
/* run PSG chip & get samples */
|
/* run PSG chip & get samples */
|
||||||
SN76489_Update(snd.psg.pos, samples);
|
snd.psg.pos += SN76489_Update(snd.psg.pos, clocks);
|
||||||
snd.psg.pos += samples;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,18 +123,20 @@ void sound_init(void)
|
|||||||
/* */
|
/* */
|
||||||
double mclk = snd.frame_rate ? (MCYCLES_PER_LINE * lines_per_frame * snd.frame_rate) : system_clock;
|
double mclk = snd.frame_rate ? (MCYCLES_PER_LINE * lines_per_frame * snd.frame_rate) : system_clock;
|
||||||
|
|
||||||
/* For better accuracy, sound chips run in synchronization with 68k and Z80 cpus */
|
/* For maximal accuracy, sound chips run in synchronization with 68k and Z80 cpus */
|
||||||
/* These values give the exact number of M-cycles executed per rendered samples. */
|
/* These values give the exact number of M-cycles executed per internal sample clock: */
|
||||||
/* we use 21.11 fixed point precision (max. mcycle value is 3420*313 i.e 21 bits max) */
|
/* . PSG chip runs at original rate and audio is resampled internally after each update */
|
||||||
psg_cycles_ratio = (unsigned int)(mclk / (double) snd.sample_rate * 2048.0);
|
/* . FM chips run by default (if HQ mode disabled) at the output rate directly */
|
||||||
fm_cycles_ratio = psg_cycles_ratio;
|
/* We use 21.11 fixed point precision (max. mcycle value is 3420*313 i.e 21 bits max) */
|
||||||
fm_cycles_count = 0;
|
psg_cycles_ratio = 16 * 15 * (1 << 11);
|
||||||
|
fm_cycles_ratio = (unsigned int)(mclk / (double) snd.sample_rate * 2048.0);
|
||||||
psg_cycles_count = 0;
|
psg_cycles_count = 0;
|
||||||
|
fm_cycles_count = 0;
|
||||||
|
|
||||||
/* Initialize core emulation (input clock based on input frequency for 100% accuracy) */
|
/* Initialize PSG core (input clock should be based on emulated system clock) */
|
||||||
/* By default, both PSG & FM chips are running at the output frequency. */
|
|
||||||
SN76489_Init(mclk/15.0,snd.sample_rate);
|
SN76489_Init(mclk/15.0,snd.sample_rate);
|
||||||
|
|
||||||
|
/* Initialize FM cores (input clock and samplerate are only used when HQ mode is disabled) */
|
||||||
if ((system_hw & SYSTEM_PBC) == SYSTEM_MD)
|
if ((system_hw & SYSTEM_PBC) == SYSTEM_MD)
|
||||||
{
|
{
|
||||||
/* YM2612 */
|
/* YM2612 */
|
||||||
@ -144,8 +145,8 @@ void sound_init(void)
|
|||||||
YM_Update = YM2612Update;
|
YM_Update = YM2612Update;
|
||||||
YM_Write = YM2612Write;
|
YM_Write = YM2612Write;
|
||||||
|
|
||||||
/* In HQ mode, YM2612 is running at its original rate (one sample each 144*7 M-cycles) */
|
/* In HQ mode, YM2612 is running at original rate (one sample each 144*7 M-cycles) */
|
||||||
/* FM stream is resampled to the output frequency at the end of a frame. */
|
/* Audio is resampled externally at the end of a frame. */
|
||||||
if (config.hq_fm)
|
if (config.hq_fm)
|
||||||
{
|
{
|
||||||
fm_cycles_ratio = 144 * 7 * (1 << 11);
|
fm_cycles_ratio = 144 * 7 * (1 << 11);
|
||||||
@ -160,8 +161,8 @@ void sound_init(void)
|
|||||||
YM_Update = YM2413Update;
|
YM_Update = YM2413Update;
|
||||||
YM_Write = YM2413Write;
|
YM_Write = YM2413Write;
|
||||||
|
|
||||||
/* In HQ mode, YM2413 is running at its original rate (one sample each 72*15 M-cycles) */
|
/* In HQ mode, YM2413 is running at original rate (one sample each 72*15 M-cycles) */
|
||||||
/* FM stream is resampled to the output frequency at the end of a frame. */
|
/* Audio is resampled externally at the end of a frame. */
|
||||||
if (config.hq_fm)
|
if (config.hq_fm)
|
||||||
{
|
{
|
||||||
fm_cycles_ratio = 72 * 15 * (1 << 11);
|
fm_cycles_ratio = 72 * 15 * (1 << 11);
|
||||||
@ -270,50 +271,43 @@ int sound_context_load(uint8 *state)
|
|||||||
/* End of frame update, return the number of samples run so far. */
|
/* End of frame update, return the number of samples run so far. */
|
||||||
int sound_update(unsigned int cycles)
|
int sound_update(unsigned int cycles)
|
||||||
{
|
{
|
||||||
int size;
|
int size, delta;
|
||||||
|
|
||||||
/* run PSG & FM chips until end of frame */
|
/* run PSG & FM chips until end of frame */
|
||||||
cycles <<= 11;
|
cycles <<= 11;
|
||||||
psg_update(cycles);
|
psg_update(cycles);
|
||||||
fm_update(cycles);
|
fm_update(cycles);
|
||||||
|
|
||||||
/* available PSG samples */
|
/* return available FM samples */
|
||||||
size = snd.psg.pos - snd.psg.buffer;
|
|
||||||
#ifdef LOGSOUND
|
|
||||||
error("%d PSG samples available\n",size);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* FM resampling */
|
|
||||||
if (config.hq_fm)
|
if (config.hq_fm)
|
||||||
{
|
{
|
||||||
/* get available FM samples */
|
size = Fir_Resampler_avail();
|
||||||
int avail = Fir_Resampler_avail();
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
size = (snd.fm.pos - snd.fm.buffer) >> 1;
|
||||||
|
}
|
||||||
|
#ifdef LOGSOUND
|
||||||
|
error("%d FM samples available\n",size);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* compare with number of PSG samples available */
|
||||||
|
delta = size - (snd.psg.pos - snd.psg.buffer);
|
||||||
|
#ifdef LOGSOUND
|
||||||
|
error("%d PSG samples available\n", snd.psg.pos - snd.psg.buffer);
|
||||||
|
#endif
|
||||||
|
|
||||||
/* resynchronize FM & PSG chips */
|
/* resynchronize FM & PSG chips */
|
||||||
if (avail < size)
|
if (delta > 0)
|
||||||
{
|
{
|
||||||
/* FM chip is late for one (or two) samples */
|
/* PSG chip is late, get needed samples */
|
||||||
do
|
snd.psg.pos += SN76489_Update(snd.psg.pos, SN76489_Sync(delta));
|
||||||
{
|
|
||||||
YM_Update(Fir_Resampler_buffer(), 1);
|
|
||||||
Fir_Resampler_write(2);
|
|
||||||
avail = Fir_Resampler_avail();
|
|
||||||
}
|
|
||||||
while (avail < size);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* FM chip is ahead */
|
/* PSG chip is ahead, adjust clocks count */
|
||||||
fm_cycles_count += (avail - size) * psg_cycles_ratio;
|
psg_cycles_count += (SN76489_Sync(-delta) * psg_cycles_ratio);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef LOGSOUND
|
|
||||||
if (config.hq_fm)
|
|
||||||
error("%d FM samples (%d) available\n",Fir_Resampler_avail(), Fir_Resampler_written() >> 1);
|
|
||||||
else
|
|
||||||
error("%d FM samples available\n",(snd.fm.pos - snd.fm.buffer)>>1);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef LOGSOUND
|
#ifdef LOGSOUND
|
||||||
error("%lu PSG cycles run\n",psg_cycles_count);
|
error("%lu PSG cycles run\n",psg_cycles_count);
|
||||||
|
Loading…
Reference in New Issue
Block a user