Cache cycles_per_sample to avoid FP arithmetic

This commit is contained in:
Lior Halphon 2019-01-01 00:42:40 +02:00
parent 9d947c7ce6
commit 4051f190a5
3 changed files with 15 additions and 3 deletions

View File

@ -457,10 +457,9 @@ void GB_apu_run(GB_gameboy_t *gb)
if (gb->apu_output.sample_rate) { if (gb->apu_output.sample_rate) {
gb->apu_output.cycles_since_render += cycles; gb->apu_output.cycles_since_render += cycles;
double cycles_per_sample = 2 * GB_get_clock_rate(gb) / (double)gb->apu_output.sample_rate; /* 2 * because we use 8MHz units */
if (gb->apu_output.sample_cycles > cycles_per_sample) { if (gb->apu_output.sample_cycles > gb->apu_output.cycles_per_sample) {
gb->apu_output.sample_cycles -= cycles_per_sample; gb->apu_output.sample_cycles -= gb->apu_output.cycles_per_sample;
render(gb, false, NULL); render(gb, false, NULL);
} }
} }
@ -976,9 +975,17 @@ void GB_set_sample_rate(GB_gameboy_t *gb, unsigned int sample_rate)
if (sample_rate) { if (sample_rate) {
gb->apu_output.highpass_rate = pow(0.999958, GB_get_clock_rate(gb) / (double)sample_rate); gb->apu_output.highpass_rate = pow(0.999958, GB_get_clock_rate(gb) / (double)sample_rate);
} }
GB_apu_update_cycles_per_sample(gb);
} }
void GB_set_highpass_filter_mode(GB_gameboy_t *gb, GB_highpass_mode_t mode) void GB_set_highpass_filter_mode(GB_gameboy_t *gb, GB_highpass_mode_t mode)
{ {
gb->apu_output.highpass_mode = mode; gb->apu_output.highpass_mode = mode;
} }
void GB_apu_update_cycles_per_sample(GB_gameboy_t *gb)
{
if (gb->apu_output.sample_rate) {
gb->apu_output.cycles_per_sample = 2 * GB_get_clock_rate(gb) / (double)gb->apu_output.sample_rate; /* 2 * because we use 8MHz units */
}
}

View File

@ -140,6 +140,7 @@ typedef struct {
volatile bool lock; volatile bool lock;
double sample_cycles; // In 8 MHz units double sample_cycles; // In 8 MHz units
double cycles_per_sample;
// Samples are NOT normalized to MAX_CH_AMP * 4 at this stage! // Samples are NOT normalized to MAX_CH_AMP * 4 at this stage!
unsigned cycles_since_render; unsigned cycles_since_render;
@ -164,6 +165,7 @@ uint8_t GB_apu_read(GB_gameboy_t *gb, uint8_t reg);
void GB_apu_div_event(GB_gameboy_t *gb); void GB_apu_div_event(GB_gameboy_t *gb);
void GB_apu_init(GB_gameboy_t *gb); void GB_apu_init(GB_gameboy_t *gb);
void GB_apu_run(GB_gameboy_t *gb); void GB_apu_run(GB_gameboy_t *gb);
void GB_apu_update_cycles_per_sample(GB_gameboy_t *gb);
#endif #endif
#endif /* apu_h */ #endif /* apu_h */

View File

@ -638,6 +638,8 @@ void GB_reset(GB_gameboy_t *gb)
/* Todo: Ugly, fixme, see comment in the timer state machine */ /* Todo: Ugly, fixme, see comment in the timer state machine */
gb->div_state = 3; gb->div_state = 3;
GB_apu_update_cycles_per_sample(gb);
gb->magic = (uintptr_t)'SAME'; gb->magic = (uintptr_t)'SAME';
} }
@ -725,6 +727,7 @@ void *GB_get_direct_access(GB_gameboy_t *gb, GB_direct_access_t access, size_t *
void GB_set_clock_multiplier(GB_gameboy_t *gb, double multiplier) void GB_set_clock_multiplier(GB_gameboy_t *gb, double multiplier)
{ {
gb->clock_multiplier = multiplier; gb->clock_multiplier = multiplier;
GB_apu_update_cycles_per_sample(gb);
} }
uint32_t GB_get_clock_rate(GB_gameboy_t *gb) uint32_t GB_get_clock_rate(GB_gameboy_t *gb)