diff --git a/src/constants.rs b/src/constants.rs new file mode 100644 index 0000000..c7d16e5 --- /dev/null +++ b/src/constants.rs @@ -0,0 +1,2 @@ +// Hz +pub const CLOCK_SPEED: usize = 4194304; diff --git a/src/main.rs b/src/main.rs index 6789ec8..949b788 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ #![feature(exclusive_range_pattern, let_chains, slice_flatten, async_closure)] +mod constants; mod processor; mod util; diff --git a/src/processor/memory/mmio/apu.rs b/src/processor/memory/mmio/apu.rs index 45915eb..15bb435 100644 --- a/src/processor/memory/mmio/apu.rs +++ b/src/processor/memory/mmio/apu.rs @@ -1,6 +1,7 @@ use self::types::{Channels, DacSample, Mixer, VinEnable, Volume}; use crate::{ - processor::{memory::Address, timer::CLOCK_SPEED}, + constants::CLOCK_SPEED, + processor::memory::Address, util::{get_bit, set_or_clear_bit}, }; use async_ringbuf::{AsyncHeapProducer, AsyncHeapRb}; diff --git a/src/processor/memory/mmio/mod.rs b/src/processor/memory/mmio/mod.rs index 50b454a..7f65850 100644 --- a/src/processor/memory/mmio/mod.rs +++ b/src/processor/memory/mmio/mod.rs @@ -2,7 +2,9 @@ mod apu; pub(crate) mod gpu; mod joypad; mod serial; +mod timer; pub use apu::Apu; pub use gpu::Gpu; pub use joypad::Joypad; pub use serial::Serial; +pub use timer::Timer; diff --git a/src/processor/timer.rs b/src/processor/memory/mmio/timer.rs similarity index 91% rename from src/processor/timer.rs rename to src/processor/memory/mmio/timer.rs index 020d72c..4136de8 100644 --- a/src/processor/timer.rs +++ b/src/processor/memory/mmio/timer.rs @@ -3,13 +3,13 @@ use crate::{ util::{get_bit, set_bit}, }; -pub(super) struct Timers { +pub struct Timer { div_counter: usize, tima_counter: usize, } -impl Timers { - pub(super) fn init() -> Self { +impl Timer { + pub fn init() -> Self { Self { div_counter: 0, tima_counter: 0, @@ -17,14 +17,12 @@ impl Timers { } } -// Hz -pub const CLOCK_SPEED: usize = 4194304; // this will need to change when cgb mode is implemented // as it uses bit 5 in double speed mode const AUDIO_BIT: u8 = 4; impl Cpu { - pub(super) fn increment_timers(&mut self, machine_cycles: u8) { + pub fn increment_timers(&mut self, machine_cycles: u8) { let clock_cycles = (machine_cycles as usize) * 4; self.advance_mmio_clocks(clock_cycles); diff --git a/src/processor/mod.rs b/src/processor/mod.rs index 6278c4d..cdbf6a2 100644 --- a/src/processor/mod.rs +++ b/src/processor/mod.rs @@ -1,6 +1,6 @@ use std::time::Instant; -use self::{memory::Memory, timer::Timers}; +use self::memory::{mmio::Timer, Memory}; use crate::{ util::{clear_bit, get_bit}, verbose_println, @@ -10,7 +10,6 @@ use gilrs::Gilrs; mod instructions; pub mod memory; mod opcodes; -mod timer; #[derive(PartialEq)] pub(crate) enum Flags { @@ -31,7 +30,7 @@ pub struct Cpu { pub last_instruction: u8, last_instruction_addr: u16, halted: bool, - timers: Timers, + timers: Timer, gamepad_handler: Gilrs, cycle_start: Instant, } @@ -47,7 +46,7 @@ impl Cpu { last_instruction: 0x0, last_instruction_addr: 0x0, halted: false, - timers: Timers::init(), + timers: Timer::init(), gamepad_handler, cycle_start: Instant::now(), }