fix(apu): don't spin if ringbuf is full

This commit is contained in:
Rekai Nyangadzayi Musuka 2021-07-28 16:23:31 -05:00
parent 8724da824e
commit e43cac8041
4 changed files with 31 additions and 31 deletions

View File

@ -131,10 +131,11 @@ impl Apu {
self.div_prev = Some(bit_5);
if let Some(ref mut prod) = self.prod {
if self.sample_counter >= SM83_CLOCK_SPEED {
self.sample_counter %= SM83_CLOCK_SPEED;
if let Some(ref mut prod) = self.prod {
if prod.two_available() {
// Sample the APU
let ch1_amplitude = self.ch1.amplitude();
let ch1_left = self.ctrl.output.ch1_left() as u8 as f32 * ch1_amplitude;
@ -161,6 +162,7 @@ impl Apu {
}
}
}
}
pub fn set_producer(&mut self, prod: SampleProducer<f32>) {
self.prod = Some(prod);

View File

@ -47,6 +47,11 @@ impl<T> SampleProducer<T> {
pub(crate) fn is_full(&self) -> bool {
self.inner.is_full()
}
#[inline]
pub(crate) fn two_available(&self) -> bool {
self.inner.slots() > 2
}
}
impl<T> std::fmt::Debug for SampleProducer<T> {

View File

@ -117,16 +117,9 @@ impl Cpu {
}
};
// TODO: With how we currently handle audio
// this --while being correct-- incurs a performance penalty
// as our emu is audio-bound.
let mut elapsed = 0x00;
let pending: u32 = cycles.into();
while elapsed < pending {
if !self.bus.apu().is_full() {
for _ in 0..pending {
self.bus.clock();
elapsed += 1;
}
}
self.handle_interrupts();

View File

@ -112,7 +112,7 @@ fn main() -> Result<()> {
if cycle_count >= gb::emu::CYCLES_IN_FRAME {
// Draw Frame
cycle_count = Cycle::new(0);
cycle_count %= gb::emu::CYCLES_IN_FRAME;
gb::emu::draw(game_boy.ppu(), pixels.get_frame());
window.request_redraw();