. improved PSG core runtime accuracy

. improved PSG & FM cores synchronization at the end of frame
This commit is contained in:
ekeeke31 2012-04-07 17:15:38 +00:00
parent 18a04500bc
commit 39dc73d406
5 changed files with 63 additions and 69 deletions

View File

@ -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 fixed_needed;
/*int fixed_needed;
if ( samples > s->size )
samples = s->size;
samples = s->size;*/
/* 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 */
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;
}
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 avail = s->offset >> time_bits;
if ( count > avail )
count = avail;
int count = s->offset >> time_bits;
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 */
i = (s->offset >> time_bits) + buf_extra - count;
memmove( s->buf, &s->buf [count], i * sizeof (buf_t) );
memset( &s->buf [i], 0, count * sizeof (buf_t) );
memmove( s->buf, &s->buf [count], buf_extra * sizeof (buf_t) );
memset( &s->buf [buf_extra], 0, count * sizeof (buf_t) );
/* Remove samples */
s->offset -= count * time_unit;
}
return count;
}

View File

@ -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
the buffer. */
void blip_read_samples( blip_buffer_t*, short out [], int n);
int blip_read_samples( blip_buffer_t*, short out []);
#ifdef __cplusplus
}

View File

@ -309,13 +309,10 @@ static void RunNoise(int 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;
/* 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 */
RunNoise(clock_length);
@ -325,5 +322,10 @@ void SN76489_Update(INT16 *buffer, int length)
/* Read samples into output buffer */
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);
}

View File

@ -16,7 +16,7 @@ extern void SN76489_GetContext(uint8 *data);
extern uint8 *SN76489_GetContextPtr(void);
extern int SN76489_GetContextSize(void);
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);
#endif /* _SN76489_H_ */

View File

@ -86,15 +86,14 @@ INLINE void psg_update(unsigned int cycles)
{
if (cycles > psg_cycles_count)
{
/* samples to run */
unsigned int samples = (cycles - psg_cycles_count + psg_cycles_ratio - 1) / psg_cycles_ratio;
/* clocks to run */
unsigned int clocks = (cycles - psg_cycles_count + psg_cycles_ratio - 1) / psg_cycles_ratio;
/* update cycle count */
psg_cycles_count += samples * psg_cycles_ratio;
psg_cycles_count += clocks * psg_cycles_ratio;
/* run PSG chip & get samples */
SN76489_Update(snd.psg.pos, samples);
snd.psg.pos += samples;
snd.psg.pos += SN76489_Update(snd.psg.pos, clocks);
}
}
@ -124,18 +123,20 @@ void sound_init(void)
/* */
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 */
/* These values give the exact number of M-cycles executed per rendered samples. */
/* we use 21.11 fixed point precision (max. mcycle value is 3420*313 i.e 21 bits max) */
psg_cycles_ratio = (unsigned int)(mclk / (double) snd.sample_rate * 2048.0);
fm_cycles_ratio = psg_cycles_ratio;
fm_cycles_count = 0;
/* For maximal accuracy, sound chips run in synchronization with 68k and Z80 cpus */
/* These values give the exact number of M-cycles executed per internal sample clock: */
/* . PSG chip runs at original rate and audio is resampled internally after each update */
/* . FM chips run by default (if HQ mode disabled) at the output rate directly */
/* We use 21.11 fixed point precision (max. mcycle value is 3420*313 i.e 21 bits max) */
psg_cycles_ratio = 16 * 15 * (1 << 11);
fm_cycles_ratio = (unsigned int)(mclk / (double) snd.sample_rate * 2048.0);
psg_cycles_count = 0;
fm_cycles_count = 0;
/* Initialize core emulation (input clock based on input frequency for 100% accuracy) */
/* By default, both PSG & FM chips are running at the output frequency. */
/* Initialize PSG core (input clock should be based on emulated system clock) */
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)
{
/* YM2612 */
@ -144,8 +145,8 @@ void sound_init(void)
YM_Update = YM2612Update;
YM_Write = YM2612Write;
/* In HQ mode, YM2612 is running at its original rate (one sample each 144*7 M-cycles) */
/* FM stream is resampled to the output frequency at the end of a frame. */
/* In HQ mode, YM2612 is running at original rate (one sample each 144*7 M-cycles) */
/* Audio is resampled externally at the end of a frame. */
if (config.hq_fm)
{
fm_cycles_ratio = 144 * 7 * (1 << 11);
@ -160,8 +161,8 @@ void sound_init(void)
YM_Update = YM2413Update;
YM_Write = YM2413Write;
/* In HQ mode, YM2413 is running at its original rate (one sample each 72*15 M-cycles) */
/* FM stream is resampled to the output frequency at the end of a frame. */
/* In HQ mode, YM2413 is running at original rate (one sample each 72*15 M-cycles) */
/* Audio is resampled externally at the end of a frame. */
if (config.hq_fm)
{
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. */
int sound_update(unsigned int cycles)
{
int size;
int size, delta;
/* run PSG & FM chips until end of frame */
cycles <<= 11;
psg_update(cycles);
fm_update(cycles);
/* available PSG samples */
size = snd.psg.pos - snd.psg.buffer;
#ifdef LOGSOUND
error("%d PSG samples available\n",size);
#endif
/* FM resampling */
/* return available FM samples */
if (config.hq_fm)
{
/* get available FM samples */
int avail = Fir_Resampler_avail();
size = 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 */
if (avail < size)
if (delta > 0)
{
/* FM chip is late for one (or two) samples */
do
{
YM_Update(Fir_Resampler_buffer(), 1);
Fir_Resampler_write(2);
avail = Fir_Resampler_avail();
}
while (avail < size);
/* PSG chip is late, get needed samples */
snd.psg.pos += SN76489_Update(snd.psg.pos, SN76489_Sync(delta));
}
else
{
/* FM chip is ahead */
fm_cycles_count += (avail - size) * psg_cycles_ratio;
/* PSG chip is ahead, adjust clocks count */
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
error("%lu PSG cycles run\n",psg_cycles_count);