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)]
mod constants;
mod processor;
mod util;

View file

@ -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};

View file

@ -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;

View file

@ -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);

View file

@ -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(),
}