Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

timebase

Layle474851023f80
diff --git a/crates/gekko/src/cpu/interpreter/system.rs b/crates/gekko/src/cpu/interpreter/system.rs
index 1709b28..2a4c777 100644
--- a/crates/gekko/src/cpu/interpreter/system.rs
+++ b/crates/gekko/src/cpu/interpreter/system.rs
@@ -21,10 +21,22 @@ pub fn msr<const OP: u32>(ctx: &mut crate::gekko::Gekko, instr: crate::cpu::sema
pub fn spr<const OP: u32>(ctx: &mut crate::gekko::Gekko, instr: crate::cpu::semantics::Instruction) {
match OP {
crate::cpu::lut::OP_MTSPR => {
- ctx.cpu.spr.write(instr.spr_swapped(), ctx.cpu.read_gpr(instr.rs()));
+ let spr_num = instr.spr_swapped();
+ let val = ctx.cpu.read_gpr(instr.rs());
+ match spr_num {
+ 284 => ctx.scheduler.set_timebase_lower(val),
+ 285 => ctx.scheduler.set_timebase_upper(val),
+ _ => ctx.cpu.spr.write(spr_num, val),
+ }
}
crate::cpu::lut::OP_MFSPR => {
- ctx.cpu.write_gpr(instr.rd(), ctx.cpu.spr.read(instr.spr_swapped()));
+ let spr_num = instr.spr_swapped();
+ let val = match spr_num {
+ 268 => ctx.scheduler.timebase_lower(),
+ 269 => ctx.scheduler.timebase_upper(),
+ _ => ctx.cpu.spr.read(spr_num),
+ };
+ ctx.cpu.write_gpr(instr.rd(), val);
}
_ => todo!("SPR instruction with OP = {OP:#x}"),
}
@@ -45,8 +57,8 @@ pub fn segment<const OP: u32>(ctx: &mut crate::gekko::Gekko, instr: crate::cpu::
pub fn mftb(ctx: &mut crate::gekko::Gekko, instr: crate::cpu::semantics::Instruction) {
let tbr = instr.spr_swapped();
let val = match tbr {
- 268 => ctx.scheduler.cycles as u32, // TBL
- 269 => (ctx.scheduler.cycles >> 32) as u32, // TBU
+ 268 => ctx.scheduler.timebase_lower(),
+ 269 => ctx.scheduler.timebase_upper(),
_ => panic!("unknown TBR {tbr}"),
};
ctx.cpu.write_gpr(instr.rd(), val);
diff --git a/crates/gekko/src/scheduler.rs b/crates/gekko/src/scheduler.rs
index ac82b52..0b97ea0 100644
--- a/crates/gekko/src/scheduler.rs
+++ b/crates/gekko/src/scheduler.rs
@@ -2,6 +2,7 @@ use std::cmp::Reverse;
use std::collections::BinaryHeap;
pub const CYCLES_PER_VSYNC: u64 = 486_000_000 / 60; // TODO: fix
+pub const TIMEBASE_DIVISOR: u64 = 12;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum EventKind {
@@ -11,6 +12,7 @@ pub enum EventKind {
pub struct Scheduler {
pub cycles: u64,
+ timebase_offset: i64,
events: BinaryHeap<Reverse<(u64, EventKind)>>,
}
@@ -18,12 +20,37 @@ impl Scheduler {
pub fn new() -> Self {
let mut s = Scheduler {
cycles: 0,
+ timebase_offset: 0,
events: BinaryHeap::new(),
};
s.schedule_at(CYCLES_PER_VSYNC, EventKind::VSync);
s
}
+ pub fn timebase(&self) -> u64 {
+ ((self.cycles / TIMEBASE_DIVISOR) as i64 + self.timebase_offset) as u64
+ }
+
+ pub fn set_timebase_lower(&mut self, val: u32) {
+ let current = self.timebase();
+ let new_tb = (current & 0xFFFF_FFFF_0000_0000) | val as u64;
+ self.timebase_offset = new_tb as i64 - (self.cycles / TIMEBASE_DIVISOR) as i64;
+ }
+
+ pub fn set_timebase_upper(&mut self, val: u32) {
+ let current = self.timebase();
+ let new_tb = ((val as u64) << 32) | (current & 0xFFFF_FFFF);
+ self.timebase_offset = new_tb as i64 - (self.cycles / TIMEBASE_DIVISOR) as i64;
+ }
+
+ pub fn timebase_lower(&self) -> u32 {
+ self.timebase() as u32
+ }
+
+ pub fn timebase_upper(&self) -> u32 {
+ (self.timebase() >> 32) as u32
+ }
+
pub fn schedule_at(&mut self, deadline: u64, kind: EventKind) {
self.events.push(Reverse((deadline, kind)));
}