Compare commits

..

No commits in common. "2af673d7c5eb59a3b8f1804243ce63d8327f5369" and "903cfacad3676071f917ff0aaa2baf1f436146d5" have entirely different histories.

5 changed files with 46 additions and 31 deletions

View File

@ -131,35 +131,33 @@ impl Apu {
self.div_prev = Some(bit_5);
if self.sample_counter >= SM83_CLOCK_SPEED {
self.sample_counter %= SM83_CLOCK_SPEED;
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;
let ch1_right = self.ctrl.output.ch1_right() as u8 as f32 * ch1_amplitude;
// Sample the APU
let ch1_amplitude = self.ch1.amplitude();
let ch1_left = self.ctrl.output.ch1_left() as u8 as f32 * ch1_amplitude;
let ch1_right = self.ctrl.output.ch1_right() as u8 as f32 * ch1_amplitude;
let ch2_amplitude = self.ch2.amplitude();
let ch2_left = self.ctrl.output.ch2_left() as u8 as f32 * ch2_amplitude;
let ch2_right = self.ctrl.output.ch2_right() as u8 as f32 * ch2_amplitude;
let ch2_amplitude = self.ch2.amplitude();
let ch2_left = self.ctrl.output.ch2_left() as u8 as f32 * ch2_amplitude;
let ch2_right = self.ctrl.output.ch2_right() as u8 as f32 * ch2_amplitude;
let ch3_amplitude = self.ch3.amplitude();
let ch3_left = self.ctrl.output.ch3_left() as u8 as f32 * ch3_amplitude;
let ch3_right = self.ctrl.output.ch3_right() as u8 as f32 * ch3_amplitude;
let ch3_amplitude = self.ch3.amplitude();
let ch3_left = self.ctrl.output.ch3_left() as u8 as f32 * ch3_amplitude;
let ch3_right = self.ctrl.output.ch3_right() as u8 as f32 * ch3_amplitude;
let ch4_amplitude = self.ch4.amplitude();
let ch4_left = self.ctrl.output.ch4_left() as u8 as f32 * ch4_amplitude;
let ch4_right = self.ctrl.output.ch4_right() as u8 as f32 * ch4_amplitude;
let ch4_amplitude = self.ch4.amplitude();
let ch4_left = self.ctrl.output.ch4_left() as u8 as f32 * ch4_amplitude;
let ch4_right = self.ctrl.output.ch4_right() as u8 as f32 * ch4_amplitude;
let left = (ch1_left + ch2_left + ch3_left + ch4_left) / 4.0;
let right = (ch1_right + ch2_right + ch3_right + ch4_right) / 4.0;
let left = (ch1_left + ch2_left + ch3_left + ch4_left) / 4.0;
let right = (ch1_right + ch2_right + ch3_right + ch4_right) / 4.0;
prod.push(left)
.and(prod.push(right))
.expect("Add samples to ring buffer");
}
prod.push(left)
.and(prod.push(right))
.expect("Add samples to ring buffer");
}
}
}
@ -168,6 +166,13 @@ impl Apu {
self.prod = Some(prod);
}
pub(crate) fn is_full(&self) -> bool {
match self.prod.as_ref() {
Some(prod) => prod.is_full(),
None => false,
}
}
/// 0xFF26 | NR52 - Sound On/Off
pub(crate) fn set_status(&mut self, byte: u8) {
self.ctrl.enabled = (byte >> 7) & 0x01 == 0x01;

View File

@ -3,7 +3,7 @@ use rtrb::{Consumer, Producer, PushError, RingBuffer};
pub(crate) const SAMPLE_RATE: u32 = 48000; // Hz
const CHANNEL_COUNT: usize = 2;
const BUFFER_CAPACITY: usize = 4096 * CHANNEL_COUNT; // # of samples * the # of channels
const AUDIO_BUFFER_LEN: usize = 4096;
pub struct AudioSPSC<T> {
inner: RingBuffer<T>,
@ -12,7 +12,7 @@ pub struct AudioSPSC<T> {
impl<T> Default for AudioSPSC<T> {
fn default() -> Self {
Self {
inner: RingBuffer::new(BUFFER_CAPACITY),
inner: RingBuffer::new(AUDIO_BUFFER_LEN * CHANNEL_COUNT),
}
}
}
@ -43,9 +43,8 @@ impl<T> SampleProducer<T> {
self.inner.push(value)
}
#[inline]
pub(crate) fn two_available(&self) -> bool {
self.inner.slots() > 2
pub(crate) fn is_full(&self) -> bool {
self.inner.is_full()
}
}

View File

@ -66,6 +66,10 @@ impl Bus {
self.cartridge.as_ref()?.title()
}
pub(crate) fn apu(&self) -> &Apu {
&self.apu
}
pub(crate) fn clock(&mut self) {
self.ppu.clock();
self.timer.clock();

View File

@ -117,9 +117,16 @@ 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();
for _ in 0..pending {
self.bus.clock();
while elapsed < pending {
if !self.bus.apu().is_full() {
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 %= gb::emu::CYCLES_IN_FRAME;
cycle_count = Cycle::new(0);
gb::emu::draw(game_boy.ppu(), pixels.get_frame());
window.request_redraw();