move timer to mmio

This commit is contained in:
Alex Janka 2023-02-24 09:39:37 +11:00
parent eea5245d78
commit 5c44401c68
6 changed files with 14 additions and 11 deletions

2
src/constants.rs Normal file
View file

@ -0,0 +1,2 @@
// Hz
pub const CLOCK_SPEED: usize = 4194304;

View file

@ -1,5 +1,6 @@
#![feature(exclusive_range_pattern, let_chains, slice_flatten, async_closure)] #![feature(exclusive_range_pattern, let_chains, slice_flatten, async_closure)]
mod constants;
mod processor; mod processor;
mod util; mod util;

View file

@ -1,6 +1,7 @@
use self::types::{Channels, DacSample, Mixer, VinEnable, Volume}; use self::types::{Channels, DacSample, Mixer, VinEnable, Volume};
use crate::{ use crate::{
processor::{memory::Address, timer::CLOCK_SPEED}, constants::CLOCK_SPEED,
processor::memory::Address,
util::{get_bit, set_or_clear_bit}, util::{get_bit, set_or_clear_bit},
}; };
use async_ringbuf::{AsyncHeapProducer, AsyncHeapRb}; use async_ringbuf::{AsyncHeapProducer, AsyncHeapRb};

View file

@ -2,7 +2,9 @@ mod apu;
pub(crate) mod gpu; pub(crate) mod gpu;
mod joypad; mod joypad;
mod serial; mod serial;
mod timer;
pub use apu::Apu; pub use apu::Apu;
pub use gpu::Gpu; pub use gpu::Gpu;
pub use joypad::Joypad; pub use joypad::Joypad;
pub use serial::Serial; pub use serial::Serial;
pub use timer::Timer;

View file

@ -3,13 +3,13 @@ use crate::{
util::{get_bit, set_bit}, util::{get_bit, set_bit},
}; };
pub(super) struct Timers { pub struct Timer {
div_counter: usize, div_counter: usize,
tima_counter: usize, tima_counter: usize,
} }
impl Timers { impl Timer {
pub(super) fn init() -> Self { pub fn init() -> Self {
Self { Self {
div_counter: 0, div_counter: 0,
tima_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 // this will need to change when cgb mode is implemented
// as it uses bit 5 in double speed mode // as it uses bit 5 in double speed mode
const AUDIO_BIT: u8 = 4; const AUDIO_BIT: u8 = 4;
impl Cpu { 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; let clock_cycles = (machine_cycles as usize) * 4;
self.advance_mmio_clocks(clock_cycles); self.advance_mmio_clocks(clock_cycles);

View file

@ -1,6 +1,6 @@
use std::time::Instant; use std::time::Instant;
use self::{memory::Memory, timer::Timers}; use self::memory::{mmio::Timer, Memory};
use crate::{ use crate::{
util::{clear_bit, get_bit}, util::{clear_bit, get_bit},
verbose_println, verbose_println,
@ -10,7 +10,6 @@ use gilrs::Gilrs;
mod instructions; mod instructions;
pub mod memory; pub mod memory;
mod opcodes; mod opcodes;
mod timer;
#[derive(PartialEq)] #[derive(PartialEq)]
pub(crate) enum Flags { pub(crate) enum Flags {
@ -31,7 +30,7 @@ pub struct Cpu {
pub last_instruction: u8, pub last_instruction: u8,
last_instruction_addr: u16, last_instruction_addr: u16,
halted: bool, halted: bool,
timers: Timers, timers: Timer,
gamepad_handler: Gilrs, gamepad_handler: Gilrs,
cycle_start: Instant, cycle_start: Instant,
} }
@ -47,7 +46,7 @@ impl Cpu {
last_instruction: 0x0, last_instruction: 0x0,
last_instruction_addr: 0x0, last_instruction_addr: 0x0,
halted: false, halted: false,
timers: Timers::init(), timers: Timer::init(),
gamepad_handler, gamepad_handler,
cycle_start: Instant::now(), cycle_start: Instant::now(),
} }