Compare commits

..

No commits in common. "192bdffd646571aad437fcf3e33fd3910d3bd7c0" and "b13444c88591ea6fd6d467b01dd7955aebf7a9da" have entirely different histories.

6 changed files with 23 additions and 42 deletions

View File

@ -4,7 +4,7 @@ use crate::interrupt::{Interrupt, InterruptFlag};
use crate::joypad::Joypad; use crate::joypad::Joypad;
use crate::ppu::{Ppu, PpuMode}; use crate::ppu::{Ppu, PpuMode};
use crate::serial::Serial; use crate::serial::Serial;
use crate::sound::{AudioSender, Sound}; use crate::sound::{SampleSender, Sound};
use crate::timer::Timer; use crate::timer::Timer;
use crate::work_ram::{VariableWorkRam, WorkRam}; use crate::work_ram::{VariableWorkRam, WorkRam};
use std::{fs::File, io::Read}; use std::{fs::File, io::Read};
@ -66,7 +66,7 @@ impl Bus {
self.cartridge.as_ref()?.title() self.cartridge.as_ref()?.title()
} }
pub(crate) fn pass_audio_src(&mut self, sender: AudioSender) { pub(crate) fn pass_audio_src(&mut self, sender: SampleSender) {
self.snd.set_audio_src(sender) self.snd.set_audio_src(sender)
} }

View File

@ -3,7 +3,7 @@ use crate::instruction::{Cycle, Instruction};
use crate::interrupt::{InterruptEnable, InterruptFlag}; use crate::interrupt::{InterruptEnable, InterruptFlag};
use crate::joypad::Joypad; use crate::joypad::Joypad;
use crate::ppu::Ppu; use crate::ppu::Ppu;
use crate::sound::AudioSender; use crate::sound::SampleSender;
use crate::timer::Timer; use crate::timer::Timer;
use bitfield::bitfield; use bitfield::bitfield;
use std::fmt::{Display, Formatter, Result as FmtResult}; use std::fmt::{Display, Formatter, Result as FmtResult};
@ -45,7 +45,7 @@ impl Cpu {
}) })
} }
pub fn set_audio_src(&mut self, sender: AudioSender) { pub fn set_audio_src(&mut self, sender: SampleSender) {
self.bus.pass_audio_src(sender) self.bus.pass_audio_src(sender)
} }
@ -125,8 +125,6 @@ impl Cpu {
} }
}; };
// FIXME: Regression, this is not a proper fix,
// the FIFO behaves weirdly using this code
let pending: u32 = cycles.into(); let pending: u32 = cycles.into();
let mut offset = 0; let mut offset = 0;
for _ in 0..(pending + offset) { for _ in 0..(pending + offset) {
@ -363,25 +361,6 @@ impl Cpu {
} }
impl Cpu { impl Cpu {
fn _debug_log(&self, mut w: impl std::io::Write, instr: &Instruction) -> std::io::Result<()> {
write!(w, "A: {:02X} ", self.reg.a)?;
write!(w, "F: {:02X} ", u8::from(self.flags))?;
write!(w, "B: {:02X} ", self.reg.b)?;
write!(w, "C: {:02X} ", self.reg.c)?;
write!(w, "D: {:02X} ", self.reg.d)?;
write!(w, "E: {:02X} ", self.reg.e)?;
write!(w, "H: {:02X} ", self.reg.h)?;
write!(w, "L: {:02X} ", self.reg.l)?;
write!(w, "SP: {:04X} ", self.reg.sp)?;
write!(w, "PC: 00:{:04X} ", self.reg.pc)?;
write!(w, "({:02X} ", self.read_byte(self.reg.pc))?;
write!(w, "{:02X} ", self.read_byte(self.reg.pc + 1))?;
write!(w, "{:02X} ", self.read_byte(self.reg.pc + 2))?;
write!(w, "{:02X}) ", self.read_byte(self.reg.pc + 3))?;
writeln!(w, "| {:?}", instr)?;
w.flush()
}
fn _log_state(&self, mut writer: impl std::io::Write) -> std::io::Result<()> { fn _log_state(&self, mut writer: impl std::io::Write) -> std::io::Result<()> {
write!(writer, "A: {:02X} ", self.reg.a)?; write!(writer, "A: {:02X} ", self.reg.a)?;
write!(writer, "F: {:02X} ", u8::from(self.flags))?; write!(writer, "F: {:02X} ", u8::from(self.flags))?;
@ -397,7 +376,9 @@ impl Cpu {
write!(writer, "{:02X} ", self.read_byte(self.reg.pc + 1))?; write!(writer, "{:02X} ", self.read_byte(self.reg.pc + 1))?;
write!(writer, "{:02X} ", self.read_byte(self.reg.pc + 2))?; write!(writer, "{:02X} ", self.read_byte(self.reg.pc + 2))?;
writeln!(writer, "{:02X})", self.read_byte(self.reg.pc + 3))?; writeln!(writer, "{:02X})", self.read_byte(self.reg.pc + 3))?;
writer.flush() writer.flush()?;
Ok(())
} }
} }

View File

@ -1,6 +1,6 @@
pub use gui::Egui; pub use gui::Egui;
pub use instruction::Cycle; pub use instruction::Cycle;
pub use sound::AudioMPSC; pub use sound::AudioSenderReceiver;
pub const GB_WIDTH: usize = 160; pub const GB_WIDTH: usize = 160;
pub const GB_HEIGHT: usize = 144; pub const GB_HEIGHT: usize = 144;

View File

@ -1,6 +1,6 @@
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use clap::{crate_authors, crate_description, crate_name, crate_version, App, Arg}; use clap::{crate_authors, crate_description, crate_name, crate_version, App, Arg};
use gb::{AudioMPSC, Cycle, Egui, GB_HEIGHT, GB_WIDTH}; use gb::{AudioSenderReceiver, Cycle, Egui, GB_HEIGHT, GB_WIDTH};
use gilrs::Gilrs; use gilrs::Gilrs;
use pixels::{Pixels, SurfaceTexture}; use pixels::{Pixels, SurfaceTexture};
use rodio::{OutputStream, Sink}; use rodio::{OutputStream, Sink};
@ -66,7 +66,7 @@ fn main() -> Result<()> {
(pixels, egui) (pixels, egui)
}; };
let (send, recv) = AudioMPSC::new(); let (send, recv) = AudioSenderReceiver::new();
game_boy.set_audio_src(send); game_boy.set_audio_src(send);
// Initialize Audio // Initialize Audio

View File

@ -67,12 +67,12 @@ impl BusIo for Ppu {
impl Ppu { impl Ppu {
pub(crate) fn clock(&mut self) { pub(crate) fn clock(&mut self) {
self.cycle += 1;
if !self.ctrl.lcd_enabled() { if !self.ctrl.lcd_enabled() {
return; return;
} }
self.cycle += 1;
match self.stat.mode() { match self.stat.mode() {
PpuMode::OamScan => { PpuMode::OamScan => {
if self.cycle >= 80.into() { if self.cycle >= 80.into() {

View File

@ -27,7 +27,7 @@ pub(crate) struct Sound {
frame_seq_state: FrameSequencerState, frame_seq_state: FrameSequencerState,
div_prev: Option<u8>, div_prev: Option<u8>,
sender: Option<AudioSender>, sender: Option<SampleSender>,
sample_counter: u64, sample_counter: u64,
is_mpsc_full: bool, is_mpsc_full: bool,
@ -104,7 +104,7 @@ impl Sound {
} }
} }
pub(crate) fn set_audio_src(&mut self, sender: AudioSender) { pub(crate) fn set_audio_src(&mut self, sender: SampleSender) {
self.sender = Some(sender); self.sender = Some(sender);
} }
@ -1172,22 +1172,22 @@ impl From<ChannelControl> for u8 {
} }
} }
pub struct AudioMPSC; pub struct AudioSenderReceiver;
impl AudioMPSC { impl AudioSenderReceiver {
pub fn new() -> (AudioSender, AudioReceiver) { pub fn new() -> (SampleSender, SampleReceiver) {
let (send, recv) = crossbeam_channel::bounded(AUDIO_BUFFER_LEN * 2); let (send, recv) = crossbeam_channel::bounded(AUDIO_BUFFER_LEN * 2);
(AudioSender { inner: send }, AudioReceiver { inner: recv }) (SampleSender { inner: send }, SampleReceiver { inner: recv })
} }
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct AudioSender { pub struct SampleSender {
inner: Sender<f32>, inner: Sender<f32>,
} }
impl AudioSender { impl SampleSender {
fn send_samples(&self, left: f32, right: f32) -> Result<(), TrySendError<f32>> { fn send_samples(&self, left: f32, right: f32) -> Result<(), TrySendError<f32>> {
self.inner.try_send(left).and(self.inner.try_send(right))?; self.inner.try_send(left).and(self.inner.try_send(right))?;
Ok(()) Ok(())
@ -1198,11 +1198,11 @@ impl AudioSender {
} }
} }
pub struct AudioReceiver { pub struct SampleReceiver {
inner: Receiver<f32>, inner: Receiver<f32>,
} }
impl Iterator for AudioReceiver { impl Iterator for SampleReceiver {
type Item = f32; type Item = f32;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
@ -1211,7 +1211,7 @@ impl Iterator for AudioReceiver {
} }
} }
impl Source for AudioReceiver { impl Source for SampleReceiver {
fn current_frame_len(&self) -> Option<usize> { fn current_frame_len(&self) -> Option<usize> {
// A frame changes when the samples rate or // A frame changes when the samples rate or
// number of channels change. This will never happen, so // number of channels change. This will never happen, so