46 lines
1.1 KiB
Rust
46 lines
1.1 KiB
Rust
#[derive(Default, Debug)]
|
|
pub struct Timer {
|
|
tima: u8,
|
|
tma: u8,
|
|
tac: u8,
|
|
timer_interrupt: bool
|
|
}
|
|
|
|
impl Timer {
|
|
pub fn new() -> Timer {
|
|
Timer::default()
|
|
}
|
|
|
|
pub fn write_byte(&mut self, addr: u16, val: u8) {
|
|
match addr {
|
|
0xFF05 => self.tima = val,
|
|
0xFF06 => self.tma = val,
|
|
0xFF07 => self.tac = val,
|
|
_ => println!("Timer: Write {:02X} to {:04X} unsupported", val, addr),
|
|
}
|
|
}
|
|
|
|
pub fn read_byte(&self, addr: u16) -> u8 {
|
|
match addr {
|
|
0xFF05 => self.tima,
|
|
0xFF06 => self.tma,
|
|
0xFF07 => self.tac,
|
|
_ => {
|
|
println!("Timer: Read from {:04X} unsupported", addr);
|
|
0
|
|
},
|
|
}
|
|
}
|
|
|
|
pub fn timer_interrupt(&mut self) -> bool {
|
|
// Returns whether or not a vblank interrupt should be done
|
|
// Yes, this is polling, and yes, this sucks.\
|
|
if self.timer_interrupt {
|
|
self.timer_interrupt = false;
|
|
true
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
}
|