Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

refactored code, in particular mmio access

Layled7bed91edaed
diff --git a/crates/gekko/src/cpu/interpreter.rs b/crates/gekko/src/cpu/interpreter.rs
index 7f93e66..c974c81 100644
--- a/crates/gekko/src/cpu/interpreter.rs
+++ b/crates/gekko/src/cpu/interpreter.rs
@@ -178,7 +178,7 @@ pub fn store_load<const OP: u32>(
.cpu
.read_gpr(instr.ra())
.wrapping_add_signed(instr.disp());
- ctx.mmio.virt_write_u32(addr, ctx.cpu.read_gpr(instr.rs()));
+ ctx.write_u32(addr, ctx.cpu.read_gpr(instr.rs()));
if OP == crate::cpu::lut::OP_STWU {
ctx.cpu.write_gpr(instr.ra(), addr);
}
@@ -188,8 +188,7 @@ pub fn store_load<const OP: u32>(
.cpu
.read_gpr(instr.ra())
.wrapping_add_signed(instr.disp());
- ctx.mmio
- .virt_write_u16(addr, (ctx.cpu.read_gpr(instr.rs()) & 0xffff) as u16);
+ ctx.write_u16(addr, (ctx.cpu.read_gpr(instr.rs()) & 0xffff) as u16);
if OP == crate::cpu::lut::OP_STHU {
ctx.cpu.write_gpr(instr.ra(), addr);
}
@@ -199,7 +198,8 @@ pub fn store_load<const OP: u32>(
.cpu
.read_gpr(instr.ra())
.wrapping_add_signed(instr.disp());
- ctx.cpu.write_gpr(instr.rd(), ctx.mmio.virt_read_u32(addr));
+ let val = ctx.read_u32(addr);
+ ctx.cpu.write_gpr(instr.rd(), val);
if OP == crate::cpu::lut::OP_LWZU {
ctx.cpu.write_gpr(instr.ra(), addr);
}
diff --git a/crates/gekko/src/gekko.rs b/crates/gekko/src/gekko.rs
index cbd678e..e1bef45 100644
--- a/crates/gekko/src/gekko.rs
+++ b/crates/gekko/src/gekko.rs
@@ -1,19 +1,23 @@
use crate::{
- cpu::{self, semantics::Instruction},
- mmio, scheduler,
+ cpu::{self, Cpu, semantics::Instruction},
+ mmio::Mmio,
+ scheduler::Scheduler,
+ vi::Vi,
};
+use dol::Dol;
pub struct Gekko {
- pub cpu: cpu::Cpu,
- pub scheduler: scheduler::Scheduler,
- pub mmio: mmio::Mmio,
+ pub cpu: Cpu,
+ pub scheduler: Scheduler,
+ pub mmio: Mmio,
+ pub vi: Vi,
}
impl Gekko {
pub fn new(path: &str) -> Self {
- let mut mmio = mmio::Mmio::new();
+ let mut mmio = Mmio::new();
let data = std::fs::read(path).expect("failed to read ROM");
- let dol = dol::Dol::parse(&data);
+ let dol = Dol::parse(&data);
// Copy TEXT sections to memory
for section in &dol.text_sections {
@@ -40,9 +44,10 @@ impl Gekko {
}
Gekko {
- cpu: cpu::Cpu::new(dol.entry_point),
- scheduler: scheduler::Scheduler { cycles: 0 },
+ cpu: Cpu::new(dol.entry_point),
+ scheduler: Scheduler { cycles: 0 },
mmio,
+ vi: Vi::new(),
}
}
@@ -50,6 +55,7 @@ impl Gekko {
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/mmio.rs b/crates/gekko/src/mmio.rs
index ff619b4..08be590 100644
--- a/crates/gekko/src/mmio.rs
+++ b/crates/gekko/src/mmio.rs
@@ -3,6 +3,7 @@ mod tests;
pub mod constants;
pub mod traits;
+pub mod bus;
use constants::*;
diff --git a/crates/gekko/src/mmio/bus.rs b/crates/gekko/src/mmio/bus.rs
new file mode 100644
index 0000000..0c09a17
--- /dev/null
+++ b/crates/gekko/src/mmio/bus.rs
@@ -0,0 +1,76 @@
+use crate::{
+ gekko::Gekko,
+ mmio::{
+ Mmio,
+ constants::{VI_BASE, VI_END},
+ },
+};
+
+enum BusTarget {
+ Vi,
+ Fallback,
+}
+
+#[rustfmt::skip]
+fn route(phys: u32) -> (BusTarget, u32) {
+ match phys {
+ VI_BASE..=VI_END => (BusTarget::Vi, phys - VI_BASE),
+ _ => (BusTarget::Fallback, phys),
+ }
+}
+
+impl Gekko {
+ #[rustfmt::skip]
+ pub fn read_u8(&mut self, addr: u32) -> u8 {
+ let (target, offset) = route(Mmio::virt_to_phys(addr));
+ match target {
+ BusTarget::Vi => self.vi.mmio_read_u8(offset),
+ BusTarget::Fallback => self.mmio.phys_read_u8(offset),
+ }
+ }
+
+ #[rustfmt::skip]
+ pub fn read_u16(&mut self, addr: u32) -> u16 {
+ let (target, offset) = route(Mmio::virt_to_phys(addr));
+ match target {
+ BusTarget::Vi => self.vi.mmio_read_u16(offset),
+ BusTarget::Fallback => self.mmio.phys_read_u16(offset),
+ }
+ }
+
+ #[rustfmt::skip]
+ pub fn read_u32(&mut self, addr: u32) -> u32 {
+ let (target, offset) = route(Mmio::virt_to_phys(addr));
+ match target {
+ BusTarget::Vi => self.vi.mmio_read_u32(offset),
+ BusTarget::Fallback => self.mmio.phys_read_u32(offset),
+ }
+ }
+
+ #[rustfmt::skip]
+ pub fn write_u8(&mut self, addr: u32, val: u8) {
+ let (target, offset) = route(Mmio::virt_to_phys(addr));
+ match target {
+ BusTarget::Vi => self.vi.mmio_write_u8(offset, val),
+ BusTarget::Fallback => self.mmio.phys_write_u8(offset, val),
+ }
+ }
+
+ #[rustfmt::skip]
+ pub fn write_u16(&mut self, addr: u32, val: u16) {
+ let (target, offset) = route(Mmio::virt_to_phys(addr));
+ match target {
+ BusTarget::Vi => self.vi.mmio_write_u16(offset, val),
+ BusTarget::Fallback => self.mmio.phys_write_u16(offset, val),
+ }
+ }
+
+ #[rustfmt::skip]
+ pub fn write_u32(&mut self, addr: u32, val: u32) {
+ let (target, offset) = route(Mmio::virt_to_phys(addr));
+ match target {
+ BusTarget::Vi => self.vi.mmio_write_u32(offset, val),
+ BusTarget::Fallback => self.mmio.phys_write_u32(offset, val),
+ }
+ }
+}
diff --git a/crates/gekko/src/mmio/constants.rs b/crates/gekko/src/mmio/constants.rs
index e866f13..a8eb83c 100644
--- a/crates/gekko/src/mmio/constants.rs
+++ b/crates/gekko/src/mmio/constants.rs
@@ -8,4 +8,8 @@ pub const EFB_SIZE: usize = 0x0020_0000; // 2 MB
pub const HW_REG_BASE: u32 = 0x0C00_0000;
pub const HW_REG_END: u32 = 0x0C7F_FFFF;
-pub const HW_REG_SIZE: usize = 0x0080_0000; // 8 MB
\ No newline at end of file
+pub const HW_REG_SIZE: usize = 0x0080_0000; // 8 MB
+
+// Hardware block address ranges (physical)
+pub const VI_BASE: u32 = 0x0C00_2000;
+pub const VI_END: u32 = 0x0C00_27FF;
\ No newline at end of file
diff --git a/crates/gekko/src/mmio/tests.rs b/crates/gekko/src/mmio/tests.rs
index 9503c6b..fd3c161 100644
--- a/crates/gekko/src/mmio/tests.rs
+++ b/crates/gekko/src/mmio/tests.rs
@@ -45,17 +45,16 @@ mod tests {
}
#[test]
- #[should_panic(expected = "unmapped physical")]
- fn unmapped_address_panics() {
+ fn unmapped_address_returns_fallback() {
let mmio = Mmio::new();
- mmio.phys_read_u8(0x1000_0000);
+ let _ = mmio.phys_read_u8(0x1000_0000);
}
#[test]
- #[should_panic(expected = "unimplemented HW register")]
- fn hw_register_panics() {
+ fn hw_register_range_reads_from_hwr_fallback() {
+ // HW reg reads go to the hwr fallback Vec; default value is 0
let mmio = Mmio::new();
- mmio.phys_read_u8(0x0C00_0000);
+ assert_eq!(mmio.phys_read_u8(0x0C00_0000), 0);
}
#[test]
diff --git a/crates/gekko/src/mmio/traits.rs b/crates/gekko/src/mmio/traits.rs
index 682b090..e2782a4 100644
--- a/crates/gekko/src/mmio/traits.rs
+++ b/crates/gekko/src/mmio/traits.rs
@@ -4,4 +4,9 @@ pub trait MmioRegister: Sized {
fn from_raw(raw: u32) -> Self;
fn to_raw(self) -> u32;
+}
+
+pub trait MmioAccess<C>: MmioRegister {
+ fn read(component: &C) -> Self;
+ fn write(self, component: &mut C);
}
\ No newline at end of file
diff --git a/crates/gekko/src/vi.rs b/crates/gekko/src/vi.rs
index 3ce4b03..b61ce98 100644
--- a/crates/gekko/src/vi.rs
+++ b/crates/gekko/src/vi.rs
@@ -1,38 +1,112 @@
pub mod regs;
-use crate::mmio::Mmio;
+use crate::{
+ gekko::Gekko,
+ mmio::constants::VI_BASE,
+ mmio::traits::{MmioAccess, MmioRegister},
+};
-pub fn xfb_addr(mmio: &Mmio) -> u32 {
- let top = mmio.read_register::<regs::TopFieldBase>();
- let addr = (top.xfb_addr() << 9) | ((top.page_offset() as u32) << 24);
- addr
+pub struct Vi {
+ pub dcr: regs::DisplayConfiguration,
+ pub top_field_base: regs::TopFieldBase,
+ pub bottom_field_base: regs::BottomFieldBase,
+}
+
+impl Vi {
+ pub fn new() -> Self {
+ Vi {
+ dcr: regs::DisplayConfiguration::from_raw(0),
+ top_field_base: regs::TopFieldBase::from_raw(0),
+ bottom_field_base: regs::BottomFieldBase::from_raw(0),
+ }
+ }
+
+ pub fn xfb_addr(&self) -> u32 {
+ let top = self.top_field_base;
+ (top.xfb_addr() << 9) | ((top.page_offset() as u32) << 24)
+ }
+
+ pub fn mmio_read_u8(&self, offset: u32) -> u8 {
+ tracing::warn!(offset = format!("{offset:#06X}"), "unhandled VI read_u8");
+ 0
+ }
+
+ pub fn mmio_write_u8(&mut self, offset: u32, _val: u8) {
+ tracing::warn!(offset = format!("{offset:#06X}"), "unhandled VI write_u8");
+ }
+
+ pub fn mmio_read_u16(&self, offset: u32) -> u16 {
+ match VI_BASE + offset {
+ regs::DisplayConfiguration::ADDR => {
+ regs::DisplayConfiguration::read(self).to_raw() as u16
+ }
+ _ => {
+ tracing::warn!(offset = format!("{offset:#06X}"), "unhandled VI read_u16");
+ 0
+ }
+ }
+ }
+
+ pub fn mmio_write_u16(&mut self, offset: u32, val: u16) {
+ match VI_BASE + offset {
+ regs::DisplayConfiguration::ADDR => {
+ <regs::DisplayConfiguration as MmioRegister>::from_raw(val as u32).write(self)
+ }
+ _ => tracing::warn!(offset = format!("{offset:#06X}"), "unhandled VI write_u16"),
+ }
+ }
+
+ pub fn mmio_read_u32(&self, offset: u32) -> u32 {
+ match VI_BASE + offset {
+ regs::TopFieldBase::ADDR => regs::TopFieldBase::read(self).to_raw(),
+ regs::BottomFieldBase::ADDR => regs::BottomFieldBase::read(self).to_raw(),
+ _ => {
+ tracing::warn!(offset = format!("{offset:#06X}"), "unhandled VI read_u32");
+ 0
+ }
+ }
+ }
+
+ pub fn mmio_write_u32(&mut self, offset: u32, val: u32) {
+ match VI_BASE + offset {
+ regs::TopFieldBase::ADDR => {
+ <regs::TopFieldBase as MmioRegister>::from_raw(val).write(self)
+ }
+ regs::BottomFieldBase::ADDR => {
+ <regs::BottomFieldBase as MmioRegister>::from_raw(val).write(self)
+ }
+ _ => tracing::warn!(offset = format!("{offset:#06X}"), "unhandled VI write_u32"),
+ }
+ }
}
pub const XFB_WIDTH: usize = 640;
pub const XFB_HEIGHT: usize = 574;
-#[rustfmt::skip]
-pub fn render_xfb(mmio: &mut Mmio) -> Vec<u32> {
- let mut pixels = vec![0u32; XFB_WIDTH * XFB_HEIGHT];
- let xfb_addr = xfb_addr(mmio);
-
- // XFB is YUY2 (YCbCr 4:2:2): each 32-bit word = [Y0][Cb][Y1][Cr] (big-endian)
- // One word -> two adjacent pixels sharing Cb and Cr.
- let ycbcr_to_rgb = |y: f32, cb: f32, cr: f32| -> u32 {
- let r = (1.164 * y + 1.596 * cr).clamp(0.0, 255.0) as u8;
- let g = (1.164 * y - 0.813 * cr - 0.391 * cb).clamp(0.0, 255.0) as u8;
- let b = (1.164 * y + 2.018 * cb).clamp(0.0, 255.0) as u8;
- ((r as u32) << 16) | ((g as u32) << 8) | (b as u32)
- };
-
- for i in 0..(XFB_WIDTH * XFB_HEIGHT / 2) {
- let word = mmio.phys_read_u32(xfb_addr + (i as u32) * 4);
- let y0 = ((word >> 24) & 0xFF) as f32 - 16.0;
- let cb = ((word >> 16) & 0xFF) as f32 - 128.0;
- let y1 = ((word >> 8) & 0xFF) as f32 - 16.0;
- let cr = ( word & 0xFF) as f32 - 128.0;
- pixels[i * 2] = ycbcr_to_rgb(y0, cb, cr);
- pixels[i * 2 + 1] = ycbcr_to_rgb(y1, cb, cr);
- }
- pixels
-}
\ No newline at end of file
+impl Gekko {
+ #[rustfmt::skip]
+ pub fn render_xfb(&self) -> Vec<u32> {
+ let mut pixels = vec![0u32; XFB_WIDTH * XFB_HEIGHT];
+ let xfb_addr = self.vi.xfb_addr();
+
+ // XFB is YUY2 (YCbCr 4:2:2): each 32-bit word = [Y0][Cb][Y1][Cr] (big-endian)
+ // One word -> two adjacent pixels sharing Cb and Cr.
+ let ycbcr_to_rgb = |y: f32, cb: f32, cr: f32| -> u32 {
+ let r = (1.164 * y + 1.596 * cr).clamp(0.0, 255.0) as u8;
+ let g = (1.164 * y - 0.813 * cr - 0.391 * cb).clamp(0.0, 255.0) as u8;
+ let b = (1.164 * y + 2.018 * cb).clamp(0.0, 255.0) as u8;
+ ((r as u32) << 16) | ((g as u32) << 8) | (b as u32)
+ };
+
+ for i in 0..(XFB_WIDTH * XFB_HEIGHT / 2) {
+ let word = self.mmio.phys_read_u32(xfb_addr + (i as u32) * 4);
+ let y0 = ((word >> 24) & 0xFF) as f32 - 16.0;
+ let cb = ((word >> 16) & 0xFF) as f32 - 128.0;
+ let y1 = ((word >> 8) & 0xFF) as f32 - 16.0;
+ let cr = ( word & 0xFF) as f32 - 128.0;
+ pixels[i * 2] = ycbcr_to_rgb(y0, cb, cr);
+ pixels[i * 2 + 1] = ycbcr_to_rgb(y1, cb, cr);
+ }
+ pixels
+ }
+}
diff --git a/crates/gekko/src/vi/regs.rs b/crates/gekko/src/vi/regs.rs
index b05ed63..e098d9f 100644
--- a/crates/gekko/src/vi/regs.rs
+++ b/crates/gekko/src/vi/regs.rs
@@ -1,6 +1,17 @@
-use crate::mmio::{Mmio, traits::MmioRegister};
+use crate::mmio::{Mmio, traits::{MmioAccess, MmioRegister}};
use chapa::BitEnum;
+use super::Vi;
+
+macro_rules! impl_mmio_access {
+ ($reg:ty, $owner:ty, $field:ident) => {
+ impl MmioAccess<$owner> for $reg {
+ fn read(dev: &$owner) -> Self { dev.$field }
+ fn write(self, dev: &mut $owner) { dev.$field = self; }
+ }
+ };
+}
+
#[derive(Debug, BitEnum)]
pub enum VideoFormat {
Ntsc = 0,
@@ -30,15 +41,30 @@ impl MmioRegister for DisplayConfiguration {
fn to_raw(self) -> u32 { self.raw() as u32 }
}
+impl MmioAccess<Vi> for DisplayConfiguration {
+ fn read(vi: &Vi) -> Self {
+ vi.dcr
+ }
+
+ fn write(self, vi: &mut Vi) {
+ // TODO: Rising-edge on RST clears the register? Just to test for now
+ if self.reset() && !vi.dcr.reset() {
+ vi.dcr = <DisplayConfiguration as MmioRegister>::from_raw(0);
+ } else {
+ vi.dcr = self;
+ }
+ }
+}
+
#[chapa::bitfield(u32, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct TopFieldBase {
#[bits(9..=23, alias = "fbb")]
pub xfb_addr: u32,
-
+
#[bits(24..=27, alias = "xof")]
pub horizontal_offset: u8,
-
+
#[bits(28)]
pub page_offset: bool,
@@ -71,4 +97,7 @@ impl MmioRegister for BottomFieldBase {
const SIZE: usize = 4;
fn from_raw(raw: u32) -> Self { raw.into() }
fn to_raw(self) -> u32 { self.raw() }
-}
\ No newline at end of file
+}
+
+impl_mmio_access!(TopFieldBase, Vi, top_field_base);
+impl_mmio_access!(BottomFieldBase, Vi, bottom_field_base);
diff --git a/crates/tinyemu/src/main.rs b/crates/tinyemu/src/main.rs
index ff4a4e6..62ae6a2 100644
--- a/crates/tinyemu/src/main.rs
+++ b/crates/tinyemu/src/main.rs
@@ -94,9 +94,10 @@ fn main() {
prev_snapshot = curr_snapshot;
}
- dump_mmio(&gekko.mmio);
+ dump_mmio(&gekko.vi);
- let pixels = vi::render_xfb(&mut gekko.mmio);
+ println!("Render current XFB:");
+ let pixels = gekko.render_xfb();
render_kitty(&pixels, vi::XFB_WIDTH, vi::XFB_HEIGHT);
}
@@ -188,14 +189,11 @@ fn dump_memory(mmio: &gekko::mmio::Mmio, addr: u32) {
}
}
-fn dump_mmio(mmio: &gekko::mmio::Mmio) {
- let dcr = mmio.read_register::<vi::regs::DisplayConfiguration>();
- println!("Display Configuration: {:?}", dcr);
- let tfbl = mmio.read_register::<vi::regs::BottomFieldBase>();
- println!("Bottom Field Base: {:08X?}", tfbl);
- let tfbr = mmio.read_register::<vi::regs::TopFieldBase>();
- println!("Top Field Base: {:08X?}", tfbr);
- println!("XFB Address: {:08X}", vi::xfb_addr(mmio));
+fn dump_mmio(vi: &gekko::vi::Vi) {
+ println!("Display Configuration: {:?}", vi.dcr);
+ println!("Bottom Field Base: {:08X?}", vi.bottom_field_base);
+ println!("Top Field Base: {:08X?}", vi.top_field_base);
+ println!("XFB Address: {:08X}", vi.xfb_addr());
}
/// Render pixels (packed 0x00RRGGBB u32s) to the terminal via the Kitty
@@ -206,6 +204,7 @@ fn dump_mmio(mmio: &gekko::mmio::Mmio) {
/// f=32 – 32-bit RGBA pixels
/// s=W,v=H – dimensions
/// m=1 – more chunks follow; m=0 – last (or only) chunk
+#[rustfmt::skip]
fn render_kitty(pixels: &[u32], width: usize, height: usize) {
use std::io::Write as _;
@@ -215,7 +214,7 @@ fn render_kitty(pixels: &[u32], width: usize, height: usize) {
rgba.push(((px >> 16) & 0xFF) as u8); // R
rgba.push(((px >> 8) & 0xFF) as u8); // G
rgba.push(( px & 0xFF) as u8); // B
- rgba.push(0xFF); // A
+ rgba.push(0xFF); // A
}
let encoded = base64::engine::general_purpose::STANDARD.encode(&rgba);