Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

basîc irq?

Layle47b97f0c5d90
diff --git a/Cargo.lock b/Cargo.lock
index d11526b..8700844 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -82,7 +82,7 @@ checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
[[package]]
name = "chapa"
version = "0.1.0"
-source = "git+https://github.com/ioncodes/chapa#3eb2346560b9dc6bb8ca875b9158924c8d9f5105"
+source = "git+https://github.com/ioncodes/chapa.git#a718929a3cce9a6a219242511d52b0938480373f"
dependencies = [
"chapa-macros",
]
@@ -90,7 +90,7 @@ dependencies = [
[[package]]
name = "chapa-macros"
version = "0.1.0"
-source = "git+https://github.com/ioncodes/chapa#3eb2346560b9dc6bb8ca875b9158924c8d9f5105"
+source = "git+https://github.com/ioncodes/chapa.git#a718929a3cce9a6a219242511d52b0938480373f"
dependencies = [
"proc-macro2",
"quote",
@@ -271,9 +271,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]]
name = "libc"
-version = "0.2.182"
+version = "0.2.183"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6800badb6cb2082ffd7b6a67e6125bb39f18782f793520caee8cb8846be06112"
+checksum = "b5b646652bf6661599e1da8901b3b9522896f01e736bad5f723fe7a3a27f899d"
[[package]]
name = "linux-raw-sys"
@@ -387,9 +387,9 @@ dependencies = [
[[package]]
name = "quote"
-version = "1.0.44"
+version = "1.0.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4"
+checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
dependencies = [
"proc-macro2",
]
diff --git a/crates/gekko/Cargo.toml b/crates/gekko/Cargo.toml
index 8fb6571..44e1afc 100644
--- a/crates/gekko/Cargo.toml
+++ b/crates/gekko/Cargo.toml
@@ -7,6 +7,6 @@ edition = "2024"
chipi = "0.5.1"
[dependencies]
-chapa = { git = "https://github.com/ioncodes/chapa" }
+chapa = { git = "https://github.com/ioncodes/chapa.git" }
tracing = "0.1.44"
image = { path = "../image" }
\ No newline at end of file
diff --git a/crates/gekko/src/cpu.rs b/crates/gekko/src/cpu.rs
index ab6a787..96559b9 100644
--- a/crates/gekko/src/cpu.rs
+++ b/crates/gekko/src/cpu.rs
@@ -4,6 +4,7 @@ pub mod msr;
pub mod semantics;
pub mod spr;
pub mod sr;
+pub mod irq;
use crate::cpu::condition::ConditionRegister;
@@ -24,8 +25,8 @@ pub struct Cpu {
// These are used during instruction execution to track the current
// and next PC values. In essence, by writing to `next_pc`, instructions
// can change the flow of execution (e.g. for branches and jumps).
- pub cia: u32, // Current Instruction Address
- pub nia: u32, // Next Instruction Address
+ pub cia: u32, // Current Instruction Address
+ pub nia: u32, // Next Instruction Address
pub reserve_addr: Option<u32>, // lwarx/stwcx. reservation address
}
diff --git a/crates/gekko/src/cpu/interpreter/store_load.rs b/crates/gekko/src/cpu/interpreter/store_load.rs
index 57b207f..6175e35 100644
--- a/crates/gekko/src/cpu/interpreter/store_load.rs
+++ b/crates/gekko/src/cpu/interpreter/store_load.rs
@@ -169,13 +169,9 @@ pub fn stwcx_dot(ctx: &mut crate::gekko::Gekko, instr: crate::cpu::semantics::In
ctx.cpu.reserve_addr = None;
if store_performed {
ctx.write_u32(addr, ctx.cpu.read_gpr(instr.rs()));
- ctx.cpu
- .cr
- .set_cr0(ConditionField::new().with_eq(true).with_so(so));
+ ctx.cpu.cr.set_cr0(ConditionField::new().with_eq(true).with_so(so));
} else {
- ctx.cpu
- .cr
- .set_cr0(ConditionField::new().with_so(so));
+ ctx.cpu.cr.set_cr0(ConditionField::new().with_so(so));
}
}
diff --git a/crates/gekko/src/cpu/irq.rs b/crates/gekko/src/cpu/irq.rs
new file mode 100644
index 0000000..1e053af
--- /dev/null
+++ b/crates/gekko/src/cpu/irq.rs
@@ -0,0 +1,52 @@
+use crate::{cpu::spr::Srr0, gekko::Gekko};
+
+// Exception vectors
+pub const IRQ_SYSTEM_RESET: u32 = 0x8000_0100;
+pub const IRQ_MACHINE_CHECK: u32 = 0x8000_0200;
+pub const IRQ_DSI: u32 = 0x8000_0300;
+pub const IRQ_ISI: u32 = 0x8000_0400;
+pub const IRQ_EXTERNAL: u32 = 0x8000_0500; // traditional IRQ
+pub const IRQ_ALIGNMENT: u32 = 0x8000_0600;
+pub const IRQ_PROGRAM: u32 = 0x8000_0700;
+pub const IRQ_FP_UNAVAILABLE: u32 = 0x8000_0800;
+pub const IRQ_DECREMENTER: u32 = 0x8000_0900;
+pub const IRQ_SYSTEM_CALL: u32 = 0x8000_0C00;
+pub const IRQ_TRACE: u32 = 0x8000_0D00;
+pub const IRQ_PERFORMANCE_MONITOR: u32 = 0x8000_0F00;
+pub const IRQ_IABR: u32 = 0x8000_1300;
+pub const IRQ_THERMAL: u32 = 0x8000_1400;
+
+impl Gekko {
+ pub fn cause_external_interrupt(&mut self) {
+ let base: u32 = if self.cpu.msr.exception_prefix() {
+ 0xFFF0_0000
+ } else {
+ 0
+ };
+
+ // Table 4-7. System Reset Exception—Register Settings
+ self.cpu.spr.srr0 = Srr0::from(self.cpu.pc);
+ self.cpu.spr.srr1 = chapa::extract_bits!(self.cpu.msr; 0, 5..=9, 16..=31).raw();
+
+ self.cpu.msr = self
+ .cpu
+ .msr
+ .with_pow(false)
+ .with_fp(false)
+ .with_be(false)
+ .with_dr(false)
+ .with_fe1(false)
+ .with_pm(false)
+ .with_ee(false)
+ .with_fe0(false)
+ .with_ri(false)
+ .with_pr(false)
+ .with_se(false)
+ .with_ir(false)
+ .with_le(self.cpu.msr.ile());
+
+ self.cpu.pc = base | IRQ_EXTERNAL;
+
+ tracing::trace!(addr = format!("{:08X}", self.cpu.pc), "IRQ triggered");
+ }
+}
diff --git a/crates/gekko/src/cpu/msr.rs b/crates/gekko/src/cpu/msr.rs
index 6b5749a..312c368 100644
--- a/crates/gekko/src/cpu/msr.rs
+++ b/crates/gekko/src/cpu/msr.rs
@@ -3,28 +3,49 @@
pub struct Msr {
#[bits(13, alias = "pow")]
pub power_management: bool,
+
#[bits(15, alias = "ile")]
pub exception_little_endian: bool,
+
#[bits(16, alias = "ee")]
pub external_interrupt_enable: bool,
+
#[bits(17, alias = "pr")]
pub privilege_level: bool,
+
#[bits(18, alias = "fp")]
pub floating_point_available: bool,
+
#[bits(19, alias = "me")]
pub machine_check_enable: bool,
+
#[bits(20, alias = "fe0")]
pub floating_point_exception_mode_0: bool,
+
#[bits(21, alias = "se")]
pub single_step_trace: bool,
+
#[bits(22, alias = "be")]
pub branch_trace: bool,
+
#[bits(23, alias = "fe1")]
pub floating_point_exception_mode_1: bool,
+
#[bits(25, alias = "ip")]
pub exception_prefix: bool,
+
#[bits(26, alias = "ir")]
pub instruction_address_translation: bool,
+
#[bits(27, alias = "dr")]
pub data_address_translation: bool,
+
+ #[bits(29, alias = "pm")]
+ pub performance_monitor: bool,
+
+ #[bits(30, alias = "ri")]
+ pub recoverable_interrupt: bool,
+
+ #[bits(31, alias = "le")]
+ pub little_endian: bool,
}
diff --git a/crates/gekko/src/exi/device.rs b/crates/gekko/src/exi/device.rs
index 3169fec..442dbf2 100644
--- a/crates/gekko/src/exi/device.rs
+++ b/crates/gekko/src/exi/device.rs
@@ -22,4 +22,4 @@ impl ExiDevice for ExiDummy {
fn transfer_byte(&mut self, byte: &mut u8) {
*byte = 0;
}
-}
\ No newline at end of file
+}
diff --git a/crates/gekko/src/flipper.rs b/crates/gekko/src/flipper.rs
index c869291..7a41a35 100644
--- a/crates/gekko/src/flipper.rs
+++ b/crates/gekko/src/flipper.rs
@@ -1,2 +1,3 @@
+pub mod dsp;
+pub mod pi;
pub mod vi;
-pub mod dsp;
\ No newline at end of file
diff --git a/crates/gekko/src/flipper/pi.rs b/crates/gekko/src/flipper/pi.rs
new file mode 100644
index 0000000..4093c07
--- /dev/null
+++ b/crates/gekko/src/flipper/pi.rs
@@ -0,0 +1,92 @@
+pub mod regs;
+
+use crate::mmio::constants::PI_BASE;
+use crate::mmio::traits::{MmioAccess, MmioRegister};
+
+pub struct Pi {
+ pub intsr: regs::InterruptCause,
+ pub intmr: regs::InterruptMask,
+}
+
+/// Bitmask constants for each PI interrupt source
+#[repr(u32)]
+pub enum InterruptFlag {
+ Error = 1 << 0,
+ Rsw = 1 << 1,
+ Di = 1 << 2,
+ Si = 1 << 3,
+ Exi = 1 << 4,
+ Ai = 1 << 5,
+ Dsp = 1 << 6,
+ Mem = 1 << 7,
+ Vi = 1 << 8,
+ PeToken = 1 << 9,
+ PeFinish = 1 << 10,
+ Cp = 1 << 11,
+ Debug = 1 << 12,
+ Hsp = 1 << 13,
+}
+
+impl Pi {
+ pub fn new() -> Self {
+ Pi {
+ intsr: regs::InterruptCause::from_raw(0),
+ intmr: regs::InterruptMask::from_raw(0),
+ }
+ }
+
+ pub fn assert_interrupt(&mut self, flag: InterruptFlag) {
+ let raw = self.intsr.raw() | (flag as u32);
+ self.intsr = regs::InterruptCause::from_raw(raw);
+ }
+
+ pub fn clear_interrupt(&mut self, flag: InterruptFlag) {
+ let raw = self.intsr.raw() & !(flag as u32);
+ self.intsr = regs::InterruptCause::from_raw(raw);
+ }
+
+ pub fn interrupt_pending(&self) -> bool {
+ (self.intsr.raw() & self.intmr.raw()) != 0
+ }
+
+ crate::impl_mmio_dispatch!(regs::InterruptCause, regs::InterruptMask,);
+
+ pub fn mmio_read_u8(&self, offset: u32) -> u8 {
+ self.read_raw(PI_BASE + offset, 1).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled PI read_u8");
+ 0
+ }) as u8
+ }
+
+ pub fn mmio_write_u8(&mut self, offset: u32, val: u8) {
+ if !self.write_raw(PI_BASE + offset, 1, val as u32) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled PI write_u8");
+ }
+ }
+
+ pub fn mmio_read_u16(&self, offset: u32) -> u16 {
+ self.read_raw(PI_BASE + offset, 2).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled PI read_u16");
+ 0
+ }) as u16
+ }
+
+ pub fn mmio_write_u16(&mut self, offset: u32, val: u16) {
+ if !self.write_raw(PI_BASE + offset, 2, val as u32) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled PI write_u16");
+ }
+ }
+
+ pub fn mmio_read_u32(&self, offset: u32) -> u32 {
+ self.read_raw(PI_BASE + offset, 4).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled PI read_u32");
+ 0
+ })
+ }
+
+ pub fn mmio_write_u32(&mut self, offset: u32, val: u32) {
+ if !self.write_raw(PI_BASE + offset, 4, val) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled PI write_u32");
+ }
+ }
+}
diff --git a/crates/gekko/src/flipper/pi/regs.rs b/crates/gekko/src/flipper/pi/regs.rs
new file mode 100644
index 0000000..0a80e84
--- /dev/null
+++ b/crates/gekko/src/flipper/pi/regs.rs
@@ -0,0 +1,98 @@
+use super::Pi;
+
+// 0xCC003000 4 r INTSR - Interrupt Cause
+crate::mmio_register! {
+ InterruptCause: u32 @ 0xCC003000 => Pi.intsr {
+ #[bits(0, alias = "error")]
+ pub gp_runtime_error: bool,
+
+ #[bits(1, alias = "rsw")]
+ pub reset_switch: bool,
+
+ #[bits(2, alias = "di")]
+ pub dvd: bool,
+
+ #[bits(3, alias = "si")]
+ pub serial: bool,
+
+ #[bits(4)]
+ pub exi: bool,
+
+ #[bits(5, alias = "ai")]
+ pub streaming: bool,
+
+ #[bits(6)]
+ pub dsp: bool,
+
+ #[bits(7, alias = "mem")]
+ pub memory: bool,
+
+ #[bits(8, alias = "vi")]
+ pub video: bool,
+
+ #[bits(9, alias = "pe_token")]
+ pub token_assertion_in_cmd_list: bool,
+
+ #[bits(10, alias = "pe_finish")]
+ pub frame_is_ready: bool,
+
+ #[bits(11, alias = "cp")]
+ pub command_fifo: bool,
+
+ #[bits(12)]
+ pub debug: bool,
+
+ #[bits(13, alias = "hsp")]
+ pub highspeed_port: bool,
+
+ #[bits(16)]
+ pub rswst: bool,
+ }
+}
+
+// 0xCC003004 4 r/w INTMR - Interrupt Mask
+crate::mmio_register! {
+ InterruptMask: u32 @ 0xCC003004 => Pi.intmr {
+ #[bits(0, alias = "error")]
+ pub gp_runtime_error: bool,
+
+ #[bits(1, alias = "rsw")]
+ pub reset_switch: bool,
+
+ #[bits(2, alias = "di")]
+ pub dvd: bool,
+
+ #[bits(3, alias = "si")]
+ pub serial: bool,
+
+ #[bits(4)]
+ pub exi: bool,
+
+ #[bits(5, alias = "ai")]
+ pub streaming: bool,
+
+ #[bits(6)]
+ pub dsp: bool,
+
+ #[bits(7, alias = "mem")]
+ pub memory: bool,
+
+ #[bits(8, alias = "vi")]
+ pub video: bool,
+
+ #[bits(9, alias = "pe_token")]
+ pub token_assertion_in_cmd_list: bool,
+
+ #[bits(10, alias = "pe_finish")]
+ pub frame_is_ready: bool,
+
+ #[bits(11, alias = "cp")]
+ pub command_fifo: bool,
+
+ #[bits(12)]
+ pub debug: bool,
+
+ #[bits(13, alias = "hsp")]
+ pub highspeed_port: bool,
+ }
+}
diff --git a/crates/gekko/src/gekko.rs b/crates/gekko/src/gekko.rs
index 39aa47b..c6645a4 100644
--- a/crates/gekko/src/gekko.rs
+++ b/crates/gekko/src/gekko.rs
@@ -1,10 +1,13 @@
use crate::{
cpu::{self, Cpu, semantics::Instruction},
exi::Exi,
- flipper::dsp::Dsp,
- flipper::vi::Vi,
+ flipper::{
+ dsp::Dsp,
+ pi::{InterruptFlag, Pi},
+ vi::Vi,
+ },
mmio::Mmio,
- scheduler::Scheduler,
+ scheduler::{CYCLES_PER_VSYNC, EventKind, Scheduler},
};
use image::Executable;
@@ -13,6 +16,7 @@ pub struct Gekko {
pub scheduler: Scheduler,
pub mmio: Mmio,
pub vi: Vi,
+ pub pi: Pi,
pub dsp: Dsp,
pub exi: Exi,
}
@@ -46,49 +50,40 @@ impl Gekko {
let addr = bss_start + i;
mmio.virt_write_u8(addr, 0);
}
+
Gekko {
cpu: Cpu::new(exe.entry_point()),
- scheduler: Scheduler { cycles: 0 },
+ scheduler: Scheduler::new(),
mmio,
vi: Vi::new(),
+ pi: Pi::new(),
dsp: Dsp::new(),
exi: Exi::dummy(),
}
}
- /// Load a 64-bit double from memory
- #[inline]
- pub fn read_f64(&mut self, addr: u32) -> f64 {
- let hi = self.read_u32(addr) as u64;
- let lo = self.read_u32(addr.wrapping_add(4)) as u64;
- f64::from_bits((hi << 32) | lo)
- }
-
- /// Store a 64-bit double to memory
- #[inline]
- pub fn write_f64(&mut self, addr: u32, val: f64) {
- let bits = val.to_bits();
- self.write_u32(addr, (bits >> 32) as u32);
- self.write_u32(addr.wrapping_add(4), bits as u32);
- }
-
- /// Load a 32-bit float from memory, return as f64
- #[inline]
- pub fn read_f32(&mut self, addr: u32) -> f64 {
- f32::from_bits(self.read_u32(addr)) as f64
- }
+ pub fn step(&mut self) {
+ // Fire any events that are due
+ while let Some(event) = self.scheduler.poll() {
+ match event {
+ EventKind::VSync => {
+ self.pi.assert_interrupt(InterruptFlag::Vi);
+ let next = self.scheduler.cycles + CYCLES_PER_VSYNC;
+ self.scheduler.schedule_at(next, EventKind::VSync);
+ }
+ }
+ }
- /// Store f64 as 32-bit float to memory
- #[inline]
- pub fn write_f32(&mut self, addr: u32, val: f64) {
- self.write_u32(addr, (val as f32).to_bits());
- }
+ // Deliver external interrupt when EE=1 and any enabled PI interrupt is pending
+ if self.cpu.msr.external_interrupt_enable() && self.pi.interrupt_pending() {
+ self.cause_external_interrupt();
+ self.scheduler.cycles += 1;
+ return;
+ }
- pub fn run_until_event(&mut self) {
+ // Fetch and execute next instruction
self.cpu.cia = self.cpu.pc;
self.cpu.nia = self.cpu.cia.wrapping_add(4);
-
- // Bypass the bus, instructions are always fetched from RAM
let instr = Instruction(self.mmio.virt_read_u32(self.cpu.cia));
cpu::lut::dispatch(self, instr);
self.scheduler.cycles += 1;
diff --git a/crates/gekko/src/lib.rs b/crates/gekko/src/lib.rs
index 089f39d..d5d5521 100644
--- a/crates/gekko/src/lib.rs
+++ b/crates/gekko/src/lib.rs
@@ -1,6 +1,6 @@
pub mod cpu;
-pub mod flipper;
pub mod exi;
+pub mod flipper;
pub mod gekko;
pub mod mmio;
pub mod scheduler;
diff --git a/crates/gekko/src/mmio.rs b/crates/gekko/src/mmio.rs
index 3b75190..10c20c8 100644
--- a/crates/gekko/src/mmio.rs
+++ b/crates/gekko/src/mmio.rs
@@ -5,6 +5,7 @@ pub mod bus;
pub mod constants;
pub mod macros;
pub mod traits;
+pub mod fp;
use constants::*;
diff --git a/crates/gekko/src/mmio/bus.rs b/crates/gekko/src/mmio/bus.rs
index 6febf8c..6832a47 100644
--- a/crates/gekko/src/mmio/bus.rs
+++ b/crates/gekko/src/mmio/bus.rs
@@ -2,14 +2,13 @@ use crate::{
gekko::Gekko,
mmio::{
Mmio,
- constants::{
- DSP_BASE, DSP_END, EXI_BASE, EXI_END, VI_BASE, VI_END,
- },
+ constants::{DSP_BASE, DSP_END, EXI_BASE, EXI_END, PI_BASE, PI_END, VI_BASE, VI_END},
},
};
enum BusTarget {
Vi,
+ Pi,
Dsp,
Exi,
Fallback,
@@ -19,6 +18,7 @@ enum BusTarget {
fn route(phys: u32) -> (BusTarget, u32) {
match phys {
VI_BASE..=VI_END => (BusTarget::Vi, phys - VI_BASE),
+ PI_BASE..=PI_END => (BusTarget::Pi, phys - PI_BASE),
DSP_BASE..=DSP_END => (BusTarget::Dsp, phys - DSP_BASE),
EXI_BASE..=EXI_END => (BusTarget::Exi, phys - EXI_BASE),
_ => (BusTarget::Fallback, phys),
@@ -31,6 +31,7 @@ impl Gekko {
let (target, offset) = route(Mmio::virt_to_phys(addr));
match target {
BusTarget::Vi => self.vi.mmio_read_u8(offset),
+ BusTarget::Pi => self.pi.mmio_read_u8(offset),
BusTarget::Dsp => self.dsp.mmio_read_u8(offset),
BusTarget::Exi => self.exi.mmio_read_u8(offset),
BusTarget::Fallback => self.mmio.phys_read_u8(offset),
@@ -42,6 +43,7 @@ impl Gekko {
let (target, offset) = route(Mmio::virt_to_phys(addr));
match target {
BusTarget::Vi => self.vi.mmio_read_u16(offset),
+ BusTarget::Pi => self.pi.mmio_read_u16(offset),
BusTarget::Dsp => self.dsp.mmio_read_u16(offset),
BusTarget::Exi => self.exi.mmio_read_u16(offset),
BusTarget::Fallback => self.mmio.phys_read_u16(offset),
@@ -53,6 +55,7 @@ impl Gekko {
let (target, offset) = route(Mmio::virt_to_phys(addr));
match target {
BusTarget::Vi => self.vi.mmio_read_u32(offset),
+ BusTarget::Pi => self.pi.mmio_read_u32(offset),
BusTarget::Dsp => self.dsp.mmio_read_u32(offset),
BusTarget::Exi => self.exi.mmio_read_u32(offset),
BusTarget::Fallback => self.mmio.phys_read_u32(offset),
@@ -64,12 +67,13 @@ impl Gekko {
let (target, offset) = route(Mmio::virt_to_phys(addr));
match target {
BusTarget::Vi => self.vi.mmio_write_u8(offset, val),
+ BusTarget::Pi => self.pi.mmio_write_u8(offset, val),
BusTarget::Dsp => {
self.dsp.mmio_write_u8(offset, val);
self.dsp.process_pending_dma(&mut self.mmio);
}
- BusTarget::Exi => {
- self.exi.mmio_write_u8(offset, val);
+ BusTarget::Exi => {
+ self.exi.mmio_write_u8(offset, val);
self.exi.process_dma_transfers(&mut self.mmio);
}
BusTarget::Fallback => self.mmio.phys_write_u8(offset, val),
@@ -81,12 +85,13 @@ impl Gekko {
let (target, offset) = route(Mmio::virt_to_phys(addr));
match target {
BusTarget::Vi => self.vi.mmio_write_u16(offset, val),
+ BusTarget::Pi => self.pi.mmio_write_u16(offset, val),
BusTarget::Dsp => {
self.dsp.mmio_write_u16(offset, val);
self.dsp.process_pending_dma(&mut self.mmio);
}
- BusTarget::Exi => {
- self.exi.mmio_write_u16(offset, val);
+ BusTarget::Exi => {
+ self.exi.mmio_write_u16(offset, val);
self.exi.process_dma_transfers(&mut self.mmio);
}
BusTarget::Fallback => self.mmio.phys_write_u16(offset, val),
@@ -98,13 +103,13 @@ impl Gekko {
let (target, offset) = route(Mmio::virt_to_phys(addr));
match target {
BusTarget::Vi => self.vi.mmio_write_u32(offset, val),
-
+ BusTarget::Pi => self.pi.mmio_write_u32(offset, val),
BusTarget::Dsp => {
self.dsp.mmio_write_u32(offset, val);
self.dsp.process_pending_dma(&mut self.mmio);
}
- BusTarget::Exi => {
- self.exi.mmio_write_u32(offset, val);
+ BusTarget::Exi => {
+ self.exi.mmio_write_u32(offset, val);
self.exi.process_dma_transfers(&mut self.mmio);
}
BusTarget::Fallback => self.mmio.phys_write_u32(offset, val),
diff --git a/crates/gekko/src/mmio/fp.rs b/crates/gekko/src/mmio/fp.rs
new file mode 100644
index 0000000..5f11cbe
--- /dev/null
+++ b/crates/gekko/src/mmio/fp.rs
@@ -0,0 +1,31 @@
+use crate::gekko::Gekko;
+
+impl Gekko {
+ // Load a 64-bit double from memory
+ #[inline]
+ pub fn read_f64(&mut self, addr: u32) -> f64 {
+ let hi = self.read_u32(addr) as u64;
+ let lo = self.read_u32(addr.wrapping_add(4)) as u64;
+ f64::from_bits((hi << 32) | lo)
+ }
+
+ /// Store a 64-bit double to memory
+ #[inline]
+ pub fn write_f64(&mut self, addr: u32, val: f64) {
+ let bits = val.to_bits();
+ self.write_u32(addr, (bits >> 32) as u32);
+ self.write_u32(addr.wrapping_add(4), bits as u32);
+ }
+
+ /// Load a 32-bit float from memory, return as f64
+ #[inline]
+ pub fn read_f32(&mut self, addr: u32) -> f64 {
+ f32::from_bits(self.read_u32(addr)) as f64
+ }
+
+ /// Store f64 as 32-bit float to memory
+ #[inline]
+ pub fn write_f32(&mut self, addr: u32, val: f64) {
+ self.write_u32(addr, (val as f32).to_bits());
+ }
+}
\ No newline at end of file
diff --git a/crates/gekko/src/mmio/macros.rs b/crates/gekko/src/mmio/macros.rs
index 7514525..c6ff747 100644
--- a/crates/gekko/src/mmio/macros.rs
+++ b/crates/gekko/src/mmio/macros.rs
@@ -54,7 +54,6 @@ macro_rules! mmio_register {
$($body:tt)*
}
) => {
- #[rustfmt::skip]
#[chapa::bitfield($raw, order = lsb0)]
#[derive(Copy, Clone, Debug)]
$(#[$attr])*
@@ -82,7 +81,6 @@ macro_rules! mmio_register {
$($body:tt)*
}
) => {
- #[rustfmt::skip]
#[chapa::bitfield($raw, order = lsb0)]
#[derive(Copy, Clone, Debug)]
$(#[$attr])*
diff --git a/crates/gekko/src/scheduler.rs b/crates/gekko/src/scheduler.rs
index 7d7aaf5..e5f70ee 100644
--- a/crates/gekko/src/scheduler.rs
+++ b/crates/gekko/src/scheduler.rs
@@ -1,3 +1,37 @@
+use std::cmp::Reverse;
+use std::collections::BinaryHeap;
+
+pub const CYCLES_PER_VSYNC: u64 = 486_000_000 / 60; // TODO: fix
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
+pub enum EventKind {
+ VSync,
+}
+
pub struct Scheduler {
pub cycles: u64,
+ events: BinaryHeap<Reverse<(u64, EventKind)>>,
+}
+
+impl Scheduler {
+ pub fn new() -> Self {
+ let mut s = Scheduler {
+ cycles: 0,
+ events: BinaryHeap::new(),
+ };
+ s.schedule_at(CYCLES_PER_VSYNC, EventKind::VSync);
+ s
+ }
+
+ pub fn schedule_at(&mut self, deadline: u64, kind: EventKind) {
+ self.events.push(Reverse((deadline, kind)));
+ }
+
+ pub fn poll(&mut self) -> Option<EventKind> {
+ if self.events.peek().map_or(false, |Reverse((d, _))| self.cycles >= *d) {
+ Some(self.events.pop().unwrap().0.1)
+ } else {
+ None
+ }
+ }
}
diff --git a/crates/tinyemu/src/main.rs b/crates/tinyemu/src/main.rs
index 7e625fd..5e45f8a 100644
--- a/crates/tinyemu/src/main.rs
+++ b/crates/tinyemu/src/main.rs
@@ -91,7 +91,7 @@ fn run_emulator(gekko: &mut gekko::gekko::Gekko, args: &Args, symbols: Option<&i
print_instruction(gekko, &prev_snapshot, args.debug);
}
- gekko.run_until_event();
+ gekko.step();
let curr_snapshot = CpuSnapshot::from_cpu(&gekko.cpu);
let curr_pc = gekko.cpu.pc;