Compare commits
3 Commits
b13444c885
...
192bdffd64
Author | SHA1 | Date |
---|---|---|
Rekai Nyangadzayi Musuka | 192bdffd64 | |
Rekai Nyangadzayi Musuka | afdfcfce2c | |
Rekai Nyangadzayi Musuka | 44b605970f |
|
@ -4,7 +4,7 @@ use crate::interrupt::{Interrupt, InterruptFlag};
|
|||
use crate::joypad::Joypad;
|
||||
use crate::ppu::{Ppu, PpuMode};
|
||||
use crate::serial::Serial;
|
||||
use crate::sound::{SampleSender, Sound};
|
||||
use crate::sound::{AudioSender, Sound};
|
||||
use crate::timer::Timer;
|
||||
use crate::work_ram::{VariableWorkRam, WorkRam};
|
||||
use std::{fs::File, io::Read};
|
||||
|
@ -66,7 +66,7 @@ impl Bus {
|
|||
self.cartridge.as_ref()?.title()
|
||||
}
|
||||
|
||||
pub(crate) fn pass_audio_src(&mut self, sender: SampleSender) {
|
||||
pub(crate) fn pass_audio_src(&mut self, sender: AudioSender) {
|
||||
self.snd.set_audio_src(sender)
|
||||
}
|
||||
|
||||
|
|
29
src/cpu.rs
29
src/cpu.rs
|
@ -3,7 +3,7 @@ use crate::instruction::{Cycle, Instruction};
|
|||
use crate::interrupt::{InterruptEnable, InterruptFlag};
|
||||
use crate::joypad::Joypad;
|
||||
use crate::ppu::Ppu;
|
||||
use crate::sound::SampleSender;
|
||||
use crate::sound::AudioSender;
|
||||
use crate::timer::Timer;
|
||||
use bitfield::bitfield;
|
||||
use std::fmt::{Display, Formatter, Result as FmtResult};
|
||||
|
@ -45,7 +45,7 @@ impl Cpu {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn set_audio_src(&mut self, sender: SampleSender) {
|
||||
pub fn set_audio_src(&mut self, sender: AudioSender) {
|
||||
self.bus.pass_audio_src(sender)
|
||||
}
|
||||
|
||||
|
@ -125,6 +125,8 @@ impl Cpu {
|
|||
}
|
||||
};
|
||||
|
||||
// FIXME: Regression, this is not a proper fix,
|
||||
// the FIFO behaves weirdly using this code
|
||||
let pending: u32 = cycles.into();
|
||||
let mut offset = 0;
|
||||
for _ in 0..(pending + offset) {
|
||||
|
@ -361,6 +363,25 @@ 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<()> {
|
||||
write!(writer, "A: {:02X} ", self.reg.a)?;
|
||||
write!(writer, "F: {:02X} ", u8::from(self.flags))?;
|
||||
|
@ -376,9 +397,7 @@ impl Cpu {
|
|||
write!(writer, "{:02X} ", self.read_byte(self.reg.pc + 1))?;
|
||||
write!(writer, "{:02X} ", self.read_byte(self.reg.pc + 2))?;
|
||||
writeln!(writer, "{:02X})", self.read_byte(self.reg.pc + 3))?;
|
||||
writer.flush()?;
|
||||
|
||||
Ok(())
|
||||
writer.flush()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
pub use gui::Egui;
|
||||
pub use instruction::Cycle;
|
||||
pub use sound::AudioSenderReceiver;
|
||||
pub use sound::AudioMPSC;
|
||||
|
||||
pub const GB_WIDTH: usize = 160;
|
||||
pub const GB_HEIGHT: usize = 144;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use anyhow::{anyhow, Result};
|
||||
use clap::{crate_authors, crate_description, crate_name, crate_version, App, Arg};
|
||||
use gb::{AudioSenderReceiver, Cycle, Egui, GB_HEIGHT, GB_WIDTH};
|
||||
use gb::{AudioMPSC, Cycle, Egui, GB_HEIGHT, GB_WIDTH};
|
||||
use gilrs::Gilrs;
|
||||
use pixels::{Pixels, SurfaceTexture};
|
||||
use rodio::{OutputStream, Sink};
|
||||
|
@ -66,7 +66,7 @@ fn main() -> Result<()> {
|
|||
(pixels, egui)
|
||||
};
|
||||
|
||||
let (send, recv) = AudioSenderReceiver::new();
|
||||
let (send, recv) = AudioMPSC::new();
|
||||
game_boy.set_audio_src(send);
|
||||
|
||||
// Initialize Audio
|
||||
|
|
|
@ -67,12 +67,12 @@ impl BusIo for Ppu {
|
|||
|
||||
impl Ppu {
|
||||
pub(crate) fn clock(&mut self) {
|
||||
self.cycle += 1;
|
||||
|
||||
if !self.ctrl.lcd_enabled() {
|
||||
return;
|
||||
}
|
||||
|
||||
self.cycle += 1;
|
||||
|
||||
match self.stat.mode() {
|
||||
PpuMode::OamScan => {
|
||||
if self.cycle >= 80.into() {
|
||||
|
|
22
src/sound.rs
22
src/sound.rs
|
@ -27,7 +27,7 @@ pub(crate) struct Sound {
|
|||
frame_seq_state: FrameSequencerState,
|
||||
div_prev: Option<u8>,
|
||||
|
||||
sender: Option<SampleSender>,
|
||||
sender: Option<AudioSender>,
|
||||
sample_counter: u64,
|
||||
|
||||
is_mpsc_full: bool,
|
||||
|
@ -104,7 +104,7 @@ impl Sound {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn set_audio_src(&mut self, sender: SampleSender) {
|
||||
pub(crate) fn set_audio_src(&mut self, sender: AudioSender) {
|
||||
self.sender = Some(sender);
|
||||
}
|
||||
|
||||
|
@ -1172,22 +1172,22 @@ impl From<ChannelControl> for u8 {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct AudioSenderReceiver;
|
||||
pub struct AudioMPSC;
|
||||
|
||||
impl AudioSenderReceiver {
|
||||
pub fn new() -> (SampleSender, SampleReceiver) {
|
||||
impl AudioMPSC {
|
||||
pub fn new() -> (AudioSender, AudioReceiver) {
|
||||
let (send, recv) = crossbeam_channel::bounded(AUDIO_BUFFER_LEN * 2);
|
||||
|
||||
(SampleSender { inner: send }, SampleReceiver { inner: recv })
|
||||
(AudioSender { inner: send }, AudioReceiver { inner: recv })
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SampleSender {
|
||||
pub struct AudioSender {
|
||||
inner: Sender<f32>,
|
||||
}
|
||||
|
||||
impl SampleSender {
|
||||
impl AudioSender {
|
||||
fn send_samples(&self, left: f32, right: f32) -> Result<(), TrySendError<f32>> {
|
||||
self.inner.try_send(left).and(self.inner.try_send(right))?;
|
||||
Ok(())
|
||||
|
@ -1198,11 +1198,11 @@ impl SampleSender {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct SampleReceiver {
|
||||
pub struct AudioReceiver {
|
||||
inner: Receiver<f32>,
|
||||
}
|
||||
|
||||
impl Iterator for SampleReceiver {
|
||||
impl Iterator for AudioReceiver {
|
||||
type Item = f32;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
|
@ -1211,7 +1211,7 @@ impl Iterator for SampleReceiver {
|
|||
}
|
||||
}
|
||||
|
||||
impl Source for SampleReceiver {
|
||||
impl Source for AudioReceiver {
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
// A frame changes when the samples rate or
|
||||
// number of channels change. This will never happen, so
|
||||
|
|
Loading…
Reference in New Issue