Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/cpu/apu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ pub struct Apu {
pub div_apu: u8,
/// 0xFF10..=0xFF3F
pub apu_mem: [u8; 0x30],
pub gba_fifo_a: super::fifo::Fifo,
pub gba_fifo_b: super::fifo::Fifo,
pub gba_fifo_a_sample_rate: f32,
pub gba_fifo_b_sample_rate: f32,
pub gba_sound_a_enabled: (bool, bool),
pub gba_sound_b_enabled: (bool, bool),
}

impl Apu {
Expand All @@ -44,6 +50,12 @@ impl Apu {
// We default to 7 as the next tick wraps us back to 0
div_apu: 7,
apu_mem: [0; 0x30],
gba_fifo_a: super::fifo::Fifo::new(),
gba_fifo_b: super::fifo::Fifo::new(),
gba_fifo_a_sample_rate: 0.,
gba_fifo_b_sample_rate: 0.,
gba_sound_a_enabled: (false, false),
gba_sound_b_enabled: (false, false),
}
}

Expand Down
60 changes: 60 additions & 0 deletions src/cpu/fifo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//! GBA speciifc FIFO

use std::collections::VecDeque;

#[derive(Debug, Clone)]
pub struct Fifo {
data: VecDeque<i8>,
sound_buffer: Vec<i8>,
pub reset_flag: bool,
}

impl Fifo {
pub fn new() -> Self {
Self {
data: VecDeque::with_capacity(32),
sound_buffer: vec![],
reset_flag: false,
}
}

pub fn push(&mut self, data: i8) {
if self.data.len() >= 32 {
self.data.pop_back();
}
self.data.push_front(data)
}

/*
/// Buffer uncircularized, copied into a linear array
pub fn get_buffer(&self) -> [i8; 32] {
let mut out = [0; 32];
let n_elements = 32 - self.idx;
(&mut out[..n_elements]).copy_from_slice(&self.data[self.idx..]);
(&mut out[n_elements..]).copy_from_slice(&self.data[..self.idx]);
out
}
*/

pub fn reset(&mut self) {
self.reset_flag = true;
self.data.clear();
self.sound_buffer.clear();
}

pub fn get_current_data(&self) -> &[i8] {
&self.sound_buffer
}
pub fn inc_note(&mut self) {
if let Some(d) = self.data.pop_back() {
self.sound_buffer.push(d);
}
}
pub fn ready_for_more_data(&self) -> bool {
self.data.len() <= 16
//self.sound_buffer.len() <= 16
}
pub fn clear_sound_buffer(&mut self) {
self.sound_buffer.clear();
}
}
2 changes: 1 addition & 1 deletion src/cpu/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl IndexMut<usize> for Memory {
0xFF02 => {
//self.io_ports[0x02] = 0x7E;
// interrupt handler should be called, but let's try not doing it for now
print!("{}", self.io_ports[0x01] as char);
//print!("{}", self.io_ports[0x01] as char);
&mut self.io_ports[0x02]
}
_ => &mut self.io_ports[index - 0xFF00],
Expand Down
41 changes: 41 additions & 0 deletions src/cpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod macros;
pub mod apu;
pub mod cartridge;
pub mod constants;
pub mod fifo;
pub mod memory;
pub mod memvis;
mod tests;
Expand Down Expand Up @@ -2290,3 +2291,43 @@ impl Cpu {
}
}
}

use crate::io::graphics::renderer::Button;

impl crate::io::graphics::renderer::InputReceiver for Cpu {
fn press(&mut self, button: Button) {
match button {
Button::A => self.press_a(),
Button::B => self.press_b(),
Button::Start => self.press_start(),
Button::Select => self.press_select(),
Button::Up => self.press_up(),
Button::Down => self.press_down(),
Button::Left => self.press_left(),
Button::Right => self.press_right(),
_ => (),
}
}
fn unpress(&mut self, button: Button) {
match button {
Button::A => self.unpress_a(),
Button::B => self.unpress_b(),
Button::Start => self.unpress_start(),
Button::Select => self.unpress_select(),
Button::Up => self.unpress_up(),
Button::Down => self.unpress_down(),
Button::Left => self.unpress_left(),
Button::Right => self.unpress_right(),
_ => (),
}
}
fn reset(&mut self) {
Cpu::reset(self);
}
fn toggle_logger(&mut self) {
Cpu::toggle_logger(self);
}
fn reinit_logger(&mut self) {
Cpu::reinit_logger(self);
}
}
Loading