Removed for loop in APU

This commit is contained in:
Lior Halphon 2016-09-13 17:06:35 +03:00
parent ff7b8a6854
commit eefc998e43

View File

@ -117,22 +117,22 @@ void GB_apu_get_samples_and_update_pcm_regs(GB_gameboy_t *gb, GB_sample_t *sampl
static void GB_apu_run_internal(GB_gameboy_t *gb) static void GB_apu_run_internal(GB_gameboy_t *gb)
{ {
for (;gb->apu.apu_cycles >= CPU_FREQUENCY/APU_FREQUENCY; gb->apu.apu_cycles -= CPU_FREQUENCY/APU_FREQUENCY) { uint32_t steps = gb->apu.apu_cycles / (CPU_FREQUENCY/APU_FREQUENCY);
gb->apu.apu_cycles %= (CPU_FREQUENCY/APU_FREQUENCY);
for (uint8_t i = 0; i < 4; i++) { for (uint8_t i = 0; i < 4; i++) {
/* Phase */ /* Phase */
gb->apu.wave_channels[i].phase++; gb->apu.wave_channels[i].phase += steps;
if (gb->apu.wave_channels[i].wave_length && gb->apu.wave_channels[i].phase >= gb->apu.wave_channels[i].wave_length) { while (gb->apu.wave_channels[i].wave_length && gb->apu.wave_channels[i].phase >= gb->apu.wave_channels[i].wave_length) {
if (i == 3) { if (i == 3) {
gb->apu.lfsr = step_lfsr(gb->apu.lfsr, gb->apu.lfsr_7_bit); gb->apu.lfsr = step_lfsr(gb->apu.lfsr, gb->apu.lfsr_7_bit);
} }
gb->apu.wave_channels[i].phase %= gb->apu.wave_channels[i].wave_length; gb->apu.wave_channels[i].phase -= gb->apu.wave_channels[i].wave_length;
} }
/* Stop on Length */ /* Stop on Length */
if (gb->apu.wave_channels[i].stop_on_length) { if (gb->apu.wave_channels[i].stop_on_length) {
if (gb->apu.wave_channels[i].sound_length > 0) { if (gb->apu.wave_channels[i].sound_length > 0) {
gb->apu.wave_channels[i].sound_length -= 1; gb->apu.wave_channels[i].sound_length -= steps;
} }
if (gb->apu.wave_channels[i].sound_length <= 0) { if (gb->apu.wave_channels[i].sound_length <= 0) {
gb->apu.wave_channels[i].amplitude = 0; gb->apu.wave_channels[i].amplitude = 0;
@ -142,8 +142,8 @@ static void GB_apu_run_internal(GB_gameboy_t *gb)
} }
} }
gb->apu.envelope_step_timer += 1; gb->apu.envelope_step_timer += steps;
if (gb->apu.envelope_step_timer >= APU_FREQUENCY / 64) { while (gb->apu.envelope_step_timer >= APU_FREQUENCY / 64) {
gb->apu.envelope_step_timer -= APU_FREQUENCY / 64; gb->apu.envelope_step_timer -= APU_FREQUENCY / 64;
for (uint8_t i = 0; i < 4; i++) { for (uint8_t i = 0; i < 4; i++) {
if (gb->apu.wave_channels[i].envelope_steps && !--gb->apu.wave_channels[i].cur_envelope_steps) { if (gb->apu.wave_channels[i].envelope_steps && !--gb->apu.wave_channels[i].cur_envelope_steps) {
@ -153,8 +153,8 @@ static void GB_apu_run_internal(GB_gameboy_t *gb)
} }
} }
gb->apu.sweep_step_timer += 1; gb->apu.sweep_step_timer += steps;
if (gb->apu.sweep_step_timer >= APU_FREQUENCY / 128) { while (gb->apu.sweep_step_timer >= APU_FREQUENCY / 128) {
gb->apu.sweep_step_timer -= APU_FREQUENCY / 128; gb->apu.sweep_step_timer -= APU_FREQUENCY / 128;
if (gb->apu.wave_channels[0].sweep_steps && !--gb->apu.wave_channels[0].cur_sweep_steps) { if (gb->apu.wave_channels[0].sweep_steps && !--gb->apu.wave_channels[0].cur_sweep_steps) {
@ -175,7 +175,6 @@ static void GB_apu_run_internal(GB_gameboy_t *gb)
gb->apu.wave_channels[0].cur_sweep_steps = gb->apu.wave_channels[0].sweep_steps; gb->apu.wave_channels[0].cur_sweep_steps = gb->apu.wave_channels[0].sweep_steps;
} }
} }
}
} }
void GB_apu_run(GB_gameboy_t *gb) void GB_apu_run(GB_gameboy_t *gb)