Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

skeleton for exi, pi etc

Laylec7f7472c632e
diff --git a/crates/gekko/src/ai.rs b/crates/gekko/src/ai.rs
new file mode 100644
index 0000000..8a6327b
--- /dev/null
+++ b/crates/gekko/src/ai.rs
@@ -0,0 +1,57 @@
+pub mod regs;
+
+use crate::mmio::constants::AI_BASE;
+use crate::mmio::traits::{MmioAccess, MmioRegister};
+
+pub struct Ai {
+ pub cr: regs::Control,
+}
+
+impl Ai {
+ pub fn new() -> Self {
+ Ai {
+ cr: regs::Control::from_raw(0),
+ }
+ }
+
+ crate::impl_mmio_dispatch!(regs::Control,);
+
+ pub fn mmio_read_u8(&self, offset: u32) -> u8 {
+ self.read_raw(AI_BASE + offset, 1).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled AI read_u8");
+ 0
+ }) as u8
+ }
+
+ pub fn mmio_write_u8(&mut self, offset: u32, val: u8) {
+ if !self.write_raw(AI_BASE + offset, 1, val as u32) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled AI write_u8");
+ }
+ }
+
+ pub fn mmio_read_u16(&self, offset: u32) -> u16 {
+ self.read_raw(AI_BASE + offset, 2).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled AI read_u16");
+ 0
+ }) as u16
+ }
+
+ pub fn mmio_write_u16(&mut self, offset: u32, val: u16) {
+ if !self.write_raw(AI_BASE + offset, 2, val as u32) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled AI write_u16");
+ }
+ }
+
+ pub fn mmio_read_u32(&self, offset: u32) -> u32 {
+ self.read_raw(AI_BASE + offset, 4).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled AI read_u32");
+ 0
+ })
+ }
+
+ pub fn mmio_write_u32(&mut self, offset: u32, val: u32) {
+ if !self.write_raw(AI_BASE + offset, 4, val) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled AI write_u32");
+ }
+ }
+}
diff --git a/crates/gekko/src/ai/regs.rs b/crates/gekko/src/ai/regs.rs
new file mode 100644
index 0000000..bd8212c
--- /dev/null
+++ b/crates/gekko/src/ai/regs.rs
@@ -0,0 +1,9 @@
+use super::Ai;
+
+// 0xCC006C00 4 R/W AICR - Audio Interface Control Register
+
+crate::mmio_register! {
+ Control: u32 @ 0xCC006C00 => Ai.cr {
+ #[bits(0..=31, alias = "cr")] pub value: u32,
+ }
+}
diff --git a/crates/gekko/src/cpu/interpreter/system.rs b/crates/gekko/src/cpu/interpreter/system.rs
index 4a07345..b1516c5 100644
--- a/crates/gekko/src/cpu/interpreter/system.rs
+++ b/crates/gekko/src/cpu/interpreter/system.rs
@@ -52,6 +52,4 @@ pub fn mftb(ctx: &mut crate::gekko::Gekko, instr: crate::cpu::semantics::Instruc
ctx.cpu.write_gpr(instr.rd(), val);
}
-pub fn nop<const OP: u32>(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) {
- tracing::warn!("Executing instruction treated as NOP: OP = {OP:#x}");
-}
+pub fn nop<const OP: u32>(_ctx: &mut crate::gekko::Gekko, _instr: crate::cpu::semantics::Instruction) {}
diff --git a/crates/gekko/src/dsp.rs b/crates/gekko/src/dsp.rs
new file mode 100644
index 0000000..56f1484
--- /dev/null
+++ b/crates/gekko/src/dsp.rs
@@ -0,0 +1,57 @@
+pub mod regs;
+
+use crate::mmio::constants::DSP_BASE;
+use crate::mmio::traits::{MmioAccess, MmioRegister};
+
+pub struct Dsp {
+ pub csr: regs::ControlStatus,
+}
+
+impl Dsp {
+ pub fn new() -> Self {
+ Dsp {
+ csr: regs::ControlStatus::from_raw(0),
+ }
+ }
+
+ crate::impl_mmio_dispatch!(regs::ControlStatus,);
+
+ pub fn mmio_read_u8(&self, offset: u32) -> u8 {
+ self.read_raw(DSP_BASE + offset, 1).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled DSP read_u8");
+ 0
+ }) as u8
+ }
+
+ pub fn mmio_write_u8(&mut self, offset: u32, val: u8) {
+ if !self.write_raw(DSP_BASE + offset, 1, val as u32) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled DSP write_u8");
+ }
+ }
+
+ pub fn mmio_read_u16(&self, offset: u32) -> u16 {
+ self.read_raw(DSP_BASE + offset, 2).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled DSP read_u16");
+ 0
+ }) as u16
+ }
+
+ pub fn mmio_write_u16(&mut self, offset: u32, val: u16) {
+ if !self.write_raw(DSP_BASE + offset, 2, val as u32) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled DSP write_u16");
+ }
+ }
+
+ pub fn mmio_read_u32(&self, offset: u32) -> u32 {
+ self.read_raw(DSP_BASE + offset, 4).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled DSP read_u32");
+ 0
+ })
+ }
+
+ pub fn mmio_write_u32(&mut self, offset: u32, val: u32) {
+ if !self.write_raw(DSP_BASE + offset, 4, val) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled DSP write_u32");
+ }
+ }
+}
diff --git a/crates/gekko/src/dsp/regs.rs b/crates/gekko/src/dsp/regs.rs
new file mode 100644
index 0000000..7f0fb64
--- /dev/null
+++ b/crates/gekko/src/dsp/regs.rs
@@ -0,0 +1,9 @@
+use super::Dsp;
+
+// 0xCC00500A 2 R/W CSR - DSP Control/Status Register
+
+crate::mmio_register! {
+ ControlStatus: u16 @ 0xCC00500A => Dsp.csr {
+ #[bits(0..=15, alias = "csr")] pub value: u16,
+ }
+}
diff --git a/crates/gekko/src/exi.rs b/crates/gekko/src/exi.rs
new file mode 100644
index 0000000..34aeed0
--- /dev/null
+++ b/crates/gekko/src/exi.rs
@@ -0,0 +1,104 @@
+pub mod regs;
+
+use crate::mmio::constants::EXI_BASE;
+use crate::mmio::traits::{MmioAccess, MmioRegister};
+
+pub struct Exi {
+ // Channel 0
+ pub ch0_csr: regs::Channel0Status,
+ pub ch0_mar: regs::Channel0DmaAddress,
+ pub ch0_length: regs::Channel0DmaLength,
+ pub ch0_cr: regs::Channel0Control,
+ pub ch0_data: regs::Channel0Data,
+ // Channel 1
+ pub ch1_csr: regs::Channel1Status,
+ pub ch1_mar: regs::Channel1DmaAddress,
+ pub ch1_length: regs::Channel1DmaLength,
+ pub ch1_cr: regs::Channel1Control,
+ pub ch1_data: regs::Channel1Data,
+ // Channel 2
+ pub ch2_csr: regs::Channel2Status,
+ pub ch2_mar: regs::Channel2DmaAddress,
+ pub ch2_length: regs::Channel2DmaLength,
+ pub ch2_cr: regs::Channel2Control,
+ pub ch2_data: regs::Channel2Data,
+}
+
+impl Exi {
+ pub fn new() -> Self {
+ Exi {
+ ch0_csr: regs::Channel0Status::from_raw(0),
+ ch0_mar: regs::Channel0DmaAddress::from_raw(0),
+ ch0_length: regs::Channel0DmaLength::from_raw(0),
+ ch0_cr: regs::Channel0Control::from_raw(0),
+ ch0_data: regs::Channel0Data::from_raw(0),
+ ch1_csr: regs::Channel1Status::from_raw(0),
+ ch1_mar: regs::Channel1DmaAddress::from_raw(0),
+ ch1_length: regs::Channel1DmaLength::from_raw(0),
+ ch1_cr: regs::Channel1Control::from_raw(0),
+ ch1_data: regs::Channel1Data::from_raw(0),
+ ch2_csr: regs::Channel2Status::from_raw(0),
+ ch2_mar: regs::Channel2DmaAddress::from_raw(0),
+ ch2_length: regs::Channel2DmaLength::from_raw(0),
+ ch2_cr: regs::Channel2Control::from_raw(0),
+ ch2_data: regs::Channel2Data::from_raw(0),
+ }
+ }
+
+ crate::impl_mmio_dispatch!(
+ regs::Channel0Status,
+ regs::Channel0DmaAddress,
+ regs::Channel0DmaLength,
+ regs::Channel0Control,
+ regs::Channel0Data,
+ regs::Channel1Status,
+ regs::Channel1DmaAddress,
+ regs::Channel1DmaLength,
+ regs::Channel1Control,
+ regs::Channel1Data,
+ regs::Channel2Status,
+ regs::Channel2DmaAddress,
+ regs::Channel2DmaLength,
+ regs::Channel2Control,
+ regs::Channel2Data,
+ );
+
+ pub fn mmio_read_u8(&self, offset: u32) -> u8 {
+ self.read_raw(EXI_BASE + offset, 1).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled EXI read_u8");
+ 0
+ }) as u8
+ }
+
+ pub fn mmio_write_u8(&mut self, offset: u32, val: u8) {
+ if !self.write_raw(EXI_BASE + offset, 1, val as u32) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled EXI write_u8");
+ }
+ }
+
+ pub fn mmio_read_u16(&self, offset: u32) -> u16 {
+ self.read_raw(EXI_BASE + offset, 2).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled EXI read_u16");
+ 0
+ }) as u16
+ }
+
+ pub fn mmio_write_u16(&mut self, offset: u32, val: u16) {
+ if !self.write_raw(EXI_BASE + offset, 2, val as u32) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled EXI write_u16");
+ }
+ }
+
+ pub fn mmio_read_u32(&self, offset: u32) -> u32 {
+ self.read_raw(EXI_BASE + offset, 4).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled EXI read_u32");
+ 0
+ })
+ }
+
+ pub fn mmio_write_u32(&mut self, offset: u32, val: u32) {
+ if !self.write_raw(EXI_BASE + offset, 4, val) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled EXI write_u32");
+ }
+ }
+}
diff --git a/crates/gekko/src/exi/regs.rs b/crates/gekko/src/exi/regs.rs
new file mode 100644
index 0000000..1f6dd2c
--- /dev/null
+++ b/crates/gekko/src/exi/regs.rs
@@ -0,0 +1,161 @@
+use super::Exi;
+
+// --- Channel 0 ---
+
+// 0xCC006800 4 R/W EXI0CSR - EXI Channel 0 Status Register
+
+crate::mmio_register! {
+ Channel0Status: u32 @ 0xCC006800 => Exi.ch0_csr {
+ #[bits(0, alias = "exiintmask")] pub exi_interrupt_mask: bool,
+ #[bits(1, alias = "exiint")] pub exi_interrupt: bool,
+ #[bits(2, alias = "tcintmask")] pub tc_interrupt_mask: bool,
+ #[bits(3, alias = "tcint")] pub tc_interrupt: bool,
+ #[bits(4..=6, alias = "clk")] pub clock: u8,
+ #[bits(7..=9, alias = "cs")] pub chip_select: u8,
+ #[bits(10, alias = "extintmask")] pub ext_interrupt_mask: bool,
+ #[bits(11, alias = "extint")] pub ext_interrupt: bool,
+ #[bits(12, alias = "ext")] pub device_connected: bool,
+ #[bits(13, alias = "romdis")] pub rom_descramble_disabled: bool,
+ }
+}
+
+// 0xCC006804 4 R/W EXI0MAR - EXI Channel 0 DMA Start Address
+
+crate::mmio_register! {
+ Channel0DmaAddress: u32 @ 0xCC006804 => Exi.ch0_mar {
+ #[bits(5..=25, alias = "addr")] pub address: u32,
+ }
+}
+
+// 0xCC006808 4 R/W EXI0LENGTH - EXI Channel 0 DMA Transfer Length
+
+crate::mmio_register! {
+ Channel0DmaLength: u32 @ 0xCC006808 => Exi.ch0_length {
+ #[bits(5..=25, alias = "len")] pub length: u32,
+ }
+}
+
+// 0xCC00680C 4 R/W EXI0CR - EXI Channel 0 Control Register
+
+crate::mmio_register! {
+ Channel0Control: u32 @ 0xCC00680C => Exi.ch0_cr {
+ #[bits(0, alias = "tstart")] pub transfer_start: bool,
+ #[bits(1, alias = "dma")] pub dma_mode: bool,
+ #[bits(2..=3, alias = "rw")] pub transfer_type: u8,
+ #[bits(4..=5, alias = "tlen")] pub transfer_length: u8,
+ }
+}
+
+// 0xCC006810 4 R/W EXI0DATA - EXI Channel 0 Immediate Data
+
+crate::mmio_register! {
+ Channel0Data: u32 @ 0xCC006810 => Exi.ch0_data {
+ #[bits(0..=31, alias = "data")] pub value: u32,
+ }
+}
+
+// --- Channel 1 ---
+
+// 0xCC006814 4 R/W EXI1CSR - EXI Channel 1 Status Register
+
+crate::mmio_register! {
+ Channel1Status: u32 @ 0xCC006814 => Exi.ch1_csr {
+ #[bits(0, alias = "exiintmask")] pub exi_interrupt_mask: bool,
+ #[bits(1, alias = "exiint")] pub exi_interrupt: bool,
+ #[bits(2, alias = "tcintmask")] pub tc_interrupt_mask: bool,
+ #[bits(3, alias = "tcint")] pub tc_interrupt: bool,
+ #[bits(4..=6, alias = "clk")] pub clock: u8,
+ #[bits(7..=9, alias = "cs")] pub chip_select: u8,
+ #[bits(10, alias = "extintmask")] pub ext_interrupt_mask: bool,
+ #[bits(11, alias = "extint")] pub ext_interrupt: bool,
+ #[bits(12, alias = "ext")] pub device_connected: bool,
+ }
+}
+
+// 0xCC006818 4 R/W EXI1MAR - EXI Channel 1 DMA Start Address
+
+crate::mmio_register! {
+ Channel1DmaAddress: u32 @ 0xCC006818 => Exi.ch1_mar {
+ #[bits(5..=25, alias = "addr")] pub address: u32,
+ }
+}
+
+// 0xCC00681C 4 R/W EXI1LENGTH - EXI Channel 1 DMA Transfer Length
+
+crate::mmio_register! {
+ Channel1DmaLength: u32 @ 0xCC00681C => Exi.ch1_length {
+ #[bits(5..=25, alias = "len")] pub length: u32,
+ }
+}
+
+// 0xCC006820 4 R/W EXI1CR - EXI Channel 1 Control Register
+
+crate::mmio_register! {
+ Channel1Control: u32 @ 0xCC006820 => Exi.ch1_cr {
+ #[bits(0, alias = "tstart")] pub transfer_start: bool,
+ #[bits(1, alias = "dma")] pub dma_mode: bool,
+ #[bits(2..=3, alias = "rw")] pub transfer_type: u8,
+ #[bits(4..=5, alias = "tlen")] pub transfer_length: u8,
+ }
+}
+
+// 0xCC006824 4 R/W EXI1DATA - EXI Channel 1 Immediate Data
+
+crate::mmio_register! {
+ Channel1Data: u32 @ 0xCC006824 => Exi.ch1_data {
+ #[bits(0..=31, alias = "data")] pub value: u32,
+ }
+}
+
+// --- Channel 2 ---
+
+// 0xCC006828 4 R/W EXI2CSR - EXI Channel 2 Status Register
+
+crate::mmio_register! {
+ Channel2Status: u32 @ 0xCC006828 => Exi.ch2_csr {
+ #[bits(0, alias = "exiintmask")] pub exi_interrupt_mask: bool,
+ #[bits(1, alias = "exiint")] pub exi_interrupt: bool,
+ #[bits(2, alias = "tcintmask")] pub tc_interrupt_mask: bool,
+ #[bits(3, alias = "tcint")] pub tc_interrupt: bool,
+ #[bits(4..=6, alias = "clk")] pub clock: u8,
+ #[bits(7..=9, alias = "cs")] pub chip_select: u8,
+ #[bits(10, alias = "extintmask")] pub ext_interrupt_mask: bool,
+ #[bits(11, alias = "extint")] pub ext_interrupt: bool,
+ #[bits(12, alias = "ext")] pub device_connected: bool,
+ }
+}
+
+// 0xCC00682C 4 R/W EXI2MAR - EXI Channel 2 DMA Start Address
+
+crate::mmio_register! {
+ Channel2DmaAddress: u32 @ 0xCC00682C => Exi.ch2_mar {
+ #[bits(5..=25, alias = "addr")] pub address: u32,
+ }
+}
+
+// 0xCC006830 4 R/W EXI2LENGTH - EXI Channel 2 DMA Transfer Length
+
+crate::mmio_register! {
+ Channel2DmaLength: u32 @ 0xCC006830 => Exi.ch2_length {
+ #[bits(5..=25, alias = "len")] pub length: u32,
+ }
+}
+
+// 0xCC006834 4 R/W EXI2CR - EXI Channel 2 Control Register
+
+crate::mmio_register! {
+ Channel2Control: u32 @ 0xCC006834 => Exi.ch2_cr {
+ #[bits(0, alias = "tstart")] pub transfer_start: bool,
+ #[bits(1, alias = "dma")] pub dma_mode: bool,
+ #[bits(2..=3, alias = "rw")] pub transfer_type: u8,
+ #[bits(4..=5, alias = "tlen")] pub transfer_length: u8,
+ }
+}
+
+// 0xCC006838 4 R/W EXI2DATA - EXI Channel 2 Immediate Data
+
+crate::mmio_register! {
+ Channel2Data: u32 @ 0xCC006838 => Exi.ch2_data {
+ #[bits(0..=31, alias = "data")] pub value: u32,
+ }
+}
diff --git a/crates/gekko/src/gekko.rs b/crates/gekko/src/gekko.rs
index cc0bd11..67d1634 100644
--- a/crates/gekko/src/gekko.rs
+++ b/crates/gekko/src/gekko.rs
@@ -1,6 +1,11 @@
use crate::{
+ ai::Ai,
cpu::{self, Cpu, semantics::Instruction},
+ dsp::Dsp,
+ exi::Exi,
+ mi::Mi,
mmio::Mmio,
+ pi::Pi,
scheduler::Scheduler,
vi::Vi,
};
@@ -11,6 +16,11 @@ pub struct Gekko {
pub scheduler: Scheduler,
pub mmio: Mmio,
pub vi: Vi,
+ pub pi: Pi,
+ pub mi: Mi,
+ pub dsp: Dsp,
+ pub exi: Exi,
+ pub ai: Ai,
}
impl Gekko {
@@ -48,6 +58,11 @@ impl Gekko {
scheduler: Scheduler { cycles: 0 },
mmio,
vi: Vi::new(),
+ pi: Pi::new(),
+ mi: Mi::new(),
+ dsp: Dsp::new(),
+ exi: Exi::new(),
+ ai: Ai::new(),
}
}
diff --git a/crates/gekko/src/lib.rs b/crates/gekko/src/lib.rs
index 43c0912..8f30db3 100644
--- a/crates/gekko/src/lib.rs
+++ b/crates/gekko/src/lib.rs
@@ -1,5 +1,10 @@
+pub mod ai;
pub mod cpu;
+pub mod dsp;
+pub mod exi;
pub mod gekko;
+pub mod mi;
pub mod mmio;
+pub mod pi;
pub mod scheduler;
pub mod vi;
diff --git a/crates/gekko/src/mi.rs b/crates/gekko/src/mi.rs
new file mode 100644
index 0000000..63c4ab9
--- /dev/null
+++ b/crates/gekko/src/mi.rs
@@ -0,0 +1,57 @@
+pub mod regs;
+
+use crate::mmio::constants::MI_BASE;
+use crate::mmio::traits::{MmioAccess, MmioRegister};
+
+pub struct Mi {
+ pub intmr: regs::InterruptMask,
+}
+
+impl Mi {
+ pub fn new() -> Self {
+ Mi {
+ intmr: regs::InterruptMask::from_raw(0),
+ }
+ }
+
+ crate::impl_mmio_dispatch!(regs::InterruptMask,);
+
+ pub fn mmio_read_u8(&self, offset: u32) -> u8 {
+ self.read_raw(MI_BASE + offset, 1).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled MI read_u8");
+ 0
+ }) as u8
+ }
+
+ pub fn mmio_write_u8(&mut self, offset: u32, val: u8) {
+ if !self.write_raw(MI_BASE + offset, 1, val as u32) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled MI write_u8");
+ }
+ }
+
+ pub fn mmio_read_u16(&self, offset: u32) -> u16 {
+ self.read_raw(MI_BASE + offset, 2).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled MI read_u16");
+ 0
+ }) as u16
+ }
+
+ pub fn mmio_write_u16(&mut self, offset: u32, val: u16) {
+ if !self.write_raw(MI_BASE + offset, 2, val as u32) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled MI write_u16");
+ }
+ }
+
+ pub fn mmio_read_u32(&self, offset: u32) -> u32 {
+ self.read_raw(MI_BASE + offset, 4).unwrap_or_else(|| {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled MI read_u32");
+ 0
+ })
+ }
+
+ pub fn mmio_write_u32(&mut self, offset: u32, val: u32) {
+ if !self.write_raw(MI_BASE + offset, 4, val) {
+ tracing::error!(offset = format!("{offset:08X}"), "unhandled MI write_u32");
+ }
+ }
+}
diff --git a/crates/gekko/src/mi/regs.rs b/crates/gekko/src/mi/regs.rs
new file mode 100644
index 0000000..b8e8a16
--- /dev/null
+++ b/crates/gekko/src/mi/regs.rs
@@ -0,0 +1,9 @@
+use super::Mi;
+
+// 0xCC00401C 4 R/W INTMR - Memory Interface Interrupt Mask
+
+crate::mmio_register! {
+ InterruptMask: u32 @ 0xCC00401C => Mi.intmr {
+ #[bits(0..=31, alias = "intmr")] pub mask: u32,
+ }
+}
diff --git a/crates/gekko/src/mmio/bus.rs b/crates/gekko/src/mmio/bus.rs
index 0c09a17..87afe36 100644
--- a/crates/gekko/src/mmio/bus.rs
+++ b/crates/gekko/src/mmio/bus.rs
@@ -2,20 +2,32 @@ use crate::{
gekko::Gekko,
mmio::{
Mmio,
- constants::{VI_BASE, VI_END},
+ constants::{
+ AI_BASE, AI_END, DSP_BASE, DSP_END, EXI_BASE, EXI_END, MI_BASE, MI_END, PI_BASE, PI_END, VI_BASE, VI_END,
+ },
},
};
enum BusTarget {
Vi,
+ Pi,
+ Mi,
+ Dsp,
+ Exi,
+ Ai,
Fallback,
}
#[rustfmt::skip]
fn route(phys: u32) -> (BusTarget, u32) {
match phys {
- VI_BASE..=VI_END => (BusTarget::Vi, phys - VI_BASE),
- _ => (BusTarget::Fallback, phys),
+ VI_BASE..=VI_END => (BusTarget::Vi, phys - VI_BASE),
+ PI_BASE..=PI_END => (BusTarget::Pi, phys - PI_BASE),
+ MI_BASE..=MI_END => (BusTarget::Mi, phys - MI_BASE),
+ DSP_BASE..=DSP_END => (BusTarget::Dsp, phys - DSP_BASE),
+ EXI_BASE..=EXI_END => (BusTarget::Exi, phys - EXI_BASE),
+ AI_BASE..=AI_END => (BusTarget::Ai, phys - AI_BASE),
+ _ => (BusTarget::Fallback, phys),
}
}
@@ -25,6 +37,11 @@ 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::Mi => self.mi.mmio_read_u8(offset),
+ BusTarget::Dsp => self.dsp.mmio_read_u8(offset),
+ BusTarget::Exi => self.exi.mmio_read_u8(offset),
+ BusTarget::Ai => self.ai.mmio_read_u8(offset),
BusTarget::Fallback => self.mmio.phys_read_u8(offset),
}
}
@@ -34,6 +51,11 @@ 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::Mi => self.mi.mmio_read_u16(offset),
+ BusTarget::Dsp => self.dsp.mmio_read_u16(offset),
+ BusTarget::Exi => self.exi.mmio_read_u16(offset),
+ BusTarget::Ai => self.ai.mmio_read_u16(offset),
BusTarget::Fallback => self.mmio.phys_read_u16(offset),
}
}
@@ -43,6 +65,11 @@ 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::Mi => self.mi.mmio_read_u32(offset),
+ BusTarget::Dsp => self.dsp.mmio_read_u32(offset),
+ BusTarget::Exi => self.exi.mmio_read_u32(offset),
+ BusTarget::Ai => self.ai.mmio_read_u32(offset),
BusTarget::Fallback => self.mmio.phys_read_u32(offset),
}
}
@@ -52,6 +79,11 @@ 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::Mi => self.mi.mmio_write_u8(offset, val),
+ BusTarget::Dsp => self.dsp.mmio_write_u8(offset, val),
+ BusTarget::Exi => self.exi.mmio_write_u8(offset, val),
+ BusTarget::Ai => self.ai.mmio_write_u8(offset, val),
BusTarget::Fallback => self.mmio.phys_write_u8(offset, val),
}
}
@@ -61,6 +93,11 @@ 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::Mi => self.mi.mmio_write_u16(offset, val),
+ BusTarget::Dsp => self.dsp.mmio_write_u16(offset, val),
+ BusTarget::Exi => self.exi.mmio_write_u16(offset, val),
+ BusTarget::Ai => self.ai.mmio_write_u16(offset, val),
BusTarget::Fallback => self.mmio.phys_write_u16(offset, val),
}
}
@@ -70,6 +107,11 @@ 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::Mi => self.mi.mmio_write_u32(offset, val),
+ BusTarget::Dsp => self.dsp.mmio_write_u32(offset, val),
+ BusTarget::Exi => self.exi.mmio_write_u32(offset, val),
+ BusTarget::Ai => self.ai.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 19552bd..72f2458 100644
--- a/crates/gekko/src/mmio/constants.rs
+++ b/crates/gekko/src/mmio/constants.rs
@@ -13,3 +13,18 @@ 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;
+
+pub const PI_BASE: u32 = 0x0C00_3000;
+pub const PI_END: u32 = 0x0C00_3FFF;
+
+pub const MI_BASE: u32 = 0x0C00_4000;
+pub const MI_END: u32 = 0x0C00_4FFF;
+
+pub const DSP_BASE: u32 = 0x0C00_5000;
+pub const DSP_END: u32 = 0x0C00_5FFF;
+
+pub const EXI_BASE: u32 = 0x0C00_6800;
+pub const EXI_END: u32 = 0x0C00_6BFF;
+
+pub const AI_BASE: u32 = 0x0C00_6C00;
+pub const AI_END: u32 = 0x0C00_6FFF;
diff --git a/crates/gekko/src/pi.rs b/crates/gekko/src/pi.rs
new file mode 100644
index 0000000..67ecf8f
--- /dev/null
+++ b/crates/gekko/src/pi.rs
@@ -0,0 +1,59 @@
+pub mod regs;
+
+use crate::mmio::constants::PI_BASE;
+use crate::mmio::traits::{MmioAccess, MmioRegister};
+
+pub struct Pi {
+ pub intmr: regs::InterruptMask,
+ pub console_type: regs::ConsoleType,
+}
+
+impl Pi {
+ pub fn new() -> Self {
+ Pi {
+ intmr: regs::InterruptMask::from_raw(0),
+ console_type: regs::ConsoleType::from_raw(0),
+ }
+ }
+
+ crate::impl_mmio_dispatch!(regs::InterruptMask, regs::ConsoleType,);
+
+ 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/pi/regs.rs b/crates/gekko/src/pi/regs.rs
new file mode 100644
index 0000000..124f085
--- /dev/null
+++ b/crates/gekko/src/pi/regs.rs
@@ -0,0 +1,17 @@
+use super::Pi;
+
+// 0xCC003004 4 R/W INTMR - Processor Interface Interrupt Mask
+
+crate::mmio_register! {
+ InterruptMask: u32 @ 0xCC003004 => Pi.intmr {
+ #[bits(0..=31, alias = "intmr")] pub mask: u32,
+ }
+}
+
+// 0xCC00302C 4 R Console Type
+
+crate::mmio_register! {
+ ConsoleType: u32 @ 0xCC00302C => Pi.console_type {
+ #[bits(0..=31, alias = "ct")] pub value: u32,
+ }
+}
diff --git a/crates/tinyemu/src/dump.rs b/crates/tinyemu/src/dump.rs
new file mode 100644
index 0000000..1692ff7
--- /dev/null
+++ b/crates/tinyemu/src/dump.rs
@@ -0,0 +1,271 @@
+use colored::Colorize;
+
+use crate::snaptshot::CpuSnapshot;
+
+pub fn registers(curr: &CpuSnapshot, prev: &CpuSnapshot) {
+ let fmt_reg = |label: &str, val: u32, prev_val: u32| -> String {
+ let value = format!("{:08X}", val);
+ if val != prev_val {
+ format!("{} {} ", label.yellow().bold(), value.bright_red().bold())
+ } else {
+ format!("{} {} ", label.dimmed(), value.dimmed())
+ }
+ };
+
+ for row in 0..8 {
+ let line: String = (0..4)
+ .map(|col| {
+ let i = row * 4 + col;
+ fmt_reg(&format!("r{:<2}", i), curr.gprs[i], prev.gprs[i])
+ })
+ .collect();
+ println!("{}", line.trim_end());
+ }
+
+ println!(
+ "{}",
+ format!(
+ "{}{}",
+ fmt_reg("lr ", curr.lr, prev.lr),
+ fmt_reg("ctr", curr.ctr, prev.ctr)
+ )
+ .trim_end()
+ );
+
+ let cr_fields = [
+ ("cr0", curr.cr.cr0(), prev.cr.cr0()),
+ ("cr1", curr.cr.cr1(), prev.cr.cr1()),
+ ("cr2", curr.cr.cr2(), prev.cr.cr2()),
+ ("cr3", curr.cr.cr3(), prev.cr.cr3()),
+ ("cr4", curr.cr.cr4(), prev.cr.cr4()),
+ ("cr5", curr.cr.cr5(), prev.cr.cr5()),
+ ("cr6", curr.cr.cr6(), prev.cr.cr6()),
+ ("cr7", curr.cr.cr7(), prev.cr.cr7()),
+ ];
+
+ let fmt_cr_field =
+ |label: &str, val: gekko::cpu::condition::ConditionField, prev_val: gekko::cpu::condition::ConditionField| {
+ let flags = format!(
+ "{}{}{}{}",
+ if val.lt() { "L" } else { "·" },
+ if val.gt() { "G" } else { "·" },
+ if val.eq() { "Z" } else { "·" },
+ if val.so() { "O" } else { "·" },
+ );
+ let text = format!("{}[{}] ", label, flags);
+ if val.raw() != prev_val.raw() {
+ format!("{}", text.bright_red().bold())
+ } else {
+ format!("{}", text.dimmed())
+ }
+ };
+
+ let cr_line: String = cr_fields
+ .iter()
+ .map(|(label, val, prev_val)| fmt_cr_field(label, *val, *prev_val))
+ .collect();
+ println!("{}", cr_line.trim_end());
+
+ println!();
+}
+
+pub fn memory(mmio: &gekko::mmio::Mmio, addr: u32) {
+ let aligned_addr = addr & !0xF;
+ let start = aligned_addr.wrapping_sub(0x40);
+ let data = mmio.virt_slice(start, 0x80);
+
+ for (i, line) in data.chunks(16).enumerate() {
+ let line_addr = start.wrapping_add((i as u32) * 16);
+ let hex = line
+ .chunks(4)
+ .map(|chunk| {
+ let word = u32::from_be_bytes(chunk.try_into().unwrap());
+ format!("{:08X}", word)
+ })
+ .collect::<Vec<_>>()
+ .join(" ");
+
+ println!("{} {}", format!("{:08X}:", line_addr).blue().bold(), hex);
+ }
+}
+
+pub fn vi(vi: &gekko::vi::Vi) {
+ println!("Display Configuration: {:?}", vi.dcr);
+ println!("Bottom Field Base: {:08X?}", vi.bfbl);
+ println!("Top Field Base: {:08X?}", vi.tfbl);
+ println!("XFB Address: {:08X}", vi.xfb_addr());
+}
+
+fn fmt_selected_devices(cs: u8, device_names: &[&str; 3]) -> String {
+ let mut selected = Vec::new();
+ for i in 0..3u8 {
+ if cs & (1 << i) != 0 {
+ selected.push(format!("{} ({})", i, device_names[i as usize]));
+ }
+ }
+ if selected.is_empty() {
+ "none".to_string()
+ } else {
+ selected.join(", ")
+ }
+}
+
+fn fmt_clock(clk: u8) -> &'static str {
+ match clk {
+ 0 => "1 MHz",
+ 1 => "2 MHz",
+ 2 => "4 MHz",
+ 3 => "8 MHz",
+ 4 => "16 MHz",
+ 5 => "32 MHz",
+ _ => "reserved",
+ }
+}
+
+fn fmt_transfer_type(rw: u8) -> &'static str {
+ match rw {
+ 0 => "read",
+ 1 => "write",
+ 2 => "read+write",
+ _ => "undefined",
+ }
+}
+
+fn fmt_enabled(val: bool) -> &'static str {
+ if val { "yes" } else { "no" }
+}
+
+fn fmt_pending(val: bool) -> &'static str {
+ if val { "PENDING" } else { "clear" }
+}
+
+struct ExiChannelView {
+ name: &'static str,
+ device_names: &'static [&'static str; 3],
+ csr_raw: u32,
+ chip_select: u8,
+ clock: u8,
+ device_connected: bool,
+ exi_interrupt: bool,
+ exi_interrupt_mask: bool,
+ tc_interrupt: bool,
+ tc_interrupt_mask: bool,
+ ext_interrupt: bool,
+ ext_interrupt_mask: bool,
+ rom_descramble_disabled: Option<bool>,
+ dma_address: u32,
+ dma_length: u32,
+ transfer_start: bool,
+ dma_mode: bool,
+ transfer_type: u8,
+ transfer_length: u8,
+ immediate_data: u32,
+}
+
+fn exi_channel(ch: &ExiChannelView) {
+ println!(" {} (CSR={:08X}):", ch.name, ch.csr_raw);
+ println!(
+ " Selected Device: {}",
+ fmt_selected_devices(ch.chip_select, ch.device_names)
+ );
+ println!(" Clock: {}", fmt_clock(ch.clock));
+ println!(" Device Connected: {}", fmt_enabled(ch.device_connected));
+ if let Some(romdis) = ch.rom_descramble_disabled {
+ println!(" ROM Descrambler Off: {}", fmt_enabled(romdis));
+ }
+ println!(
+ " EXI Interrupt: {} (enabled: {})",
+ fmt_pending(ch.exi_interrupt),
+ fmt_enabled(ch.exi_interrupt_mask)
+ );
+ println!(
+ " Transfer Complete: {} (enabled: {})",
+ fmt_pending(ch.tc_interrupt),
+ fmt_enabled(ch.tc_interrupt_mask)
+ );
+ println!(
+ " External Interrupt: {} (enabled: {})",
+ fmt_pending(ch.ext_interrupt),
+ fmt_enabled(ch.ext_interrupt_mask)
+ );
+ println!(" DMA Address: 0x{:08X}", ch.dma_address);
+ println!(" DMA Length: 0x{:08X}", ch.dma_length);
+ println!(" Transfer Started: {}", fmt_enabled(ch.transfer_start));
+ println!(
+ " Transfer Mode: {}",
+ if ch.dma_mode { "DMA" } else { "immediate" }
+ );
+ println!(" Transfer Direction: {}", fmt_transfer_type(ch.transfer_type));
+ println!(" Transfer Size: {} byte(s)", ch.transfer_length as u32 + 1);
+ println!(" Immediate Data: 0x{:08X}", ch.immediate_data);
+}
+
+pub fn exi(exi: &gekko::exi::Exi) {
+ println!("EXI:");
+ exi_channel(&ExiChannelView {
+ name: "Channel 0",
+ device_names: &["Memory Card Slot A", "Mask ROM/RTC/SRAM/UART", "Serial Port 1"],
+ csr_raw: exi.ch0_csr.raw(),
+ chip_select: exi.ch0_csr.chip_select(),
+ clock: exi.ch0_csr.clock(),
+ device_connected: exi.ch0_csr.device_connected(),
+ exi_interrupt: exi.ch0_csr.exi_interrupt(),
+ exi_interrupt_mask: exi.ch0_csr.exi_interrupt_mask(),
+ tc_interrupt: exi.ch0_csr.tc_interrupt(),
+ tc_interrupt_mask: exi.ch0_csr.tc_interrupt_mask(),
+ ext_interrupt: exi.ch0_csr.ext_interrupt(),
+ ext_interrupt_mask: exi.ch0_csr.ext_interrupt_mask(),
+ rom_descramble_disabled: Some(exi.ch0_csr.rom_descramble_disabled()),
+ dma_address: exi.ch0_mar.raw(),
+ dma_length: exi.ch0_length.raw(),
+ transfer_start: exi.ch0_cr.transfer_start(),
+ dma_mode: exi.ch0_cr.dma_mode(),
+ transfer_type: exi.ch0_cr.transfer_type(),
+ transfer_length: exi.ch0_cr.transfer_length(),
+ immediate_data: exi.ch0_data.raw(),
+ });
+ exi_channel(&ExiChannelView {
+ name: "Channel 1",
+ device_names: &["Memory Card Slot B", "-", "-"],
+ csr_raw: exi.ch1_csr.raw(),
+ chip_select: exi.ch1_csr.chip_select(),
+ clock: exi.ch1_csr.clock(),
+ device_connected: exi.ch1_csr.device_connected(),
+ exi_interrupt: exi.ch1_csr.exi_interrupt(),
+ exi_interrupt_mask: exi.ch1_csr.exi_interrupt_mask(),
+ tc_interrupt: exi.ch1_csr.tc_interrupt(),
+ tc_interrupt_mask: exi.ch1_csr.tc_interrupt_mask(),
+ ext_interrupt: exi.ch1_csr.ext_interrupt(),
+ ext_interrupt_mask: exi.ch1_csr.ext_interrupt_mask(),
+ rom_descramble_disabled: None,
+ dma_address: exi.ch1_mar.raw(),
+ dma_length: exi.ch1_length.raw(),
+ transfer_start: exi.ch1_cr.transfer_start(),
+ dma_mode: exi.ch1_cr.dma_mode(),
+ transfer_type: exi.ch1_cr.transfer_type(),
+ transfer_length: exi.ch1_cr.transfer_length(),
+ immediate_data: exi.ch1_data.raw(),
+ });
+ exi_channel(&ExiChannelView {
+ name: "Channel 2",
+ device_names: &["AD16 (debug)", "-", "-"],
+ csr_raw: exi.ch2_csr.raw(),
+ chip_select: exi.ch2_csr.chip_select(),
+ clock: exi.ch2_csr.clock(),
+ device_connected: exi.ch2_csr.device_connected(),
+ exi_interrupt: exi.ch2_csr.exi_interrupt(),
+ exi_interrupt_mask: exi.ch2_csr.exi_interrupt_mask(),
+ tc_interrupt: exi.ch2_csr.tc_interrupt(),
+ tc_interrupt_mask: exi.ch2_csr.tc_interrupt_mask(),
+ ext_interrupt: exi.ch2_csr.ext_interrupt(),
+ ext_interrupt_mask: exi.ch2_csr.ext_interrupt_mask(),
+ rom_descramble_disabled: None,
+ dma_address: exi.ch2_mar.raw(),
+ dma_length: exi.ch2_length.raw(),
+ transfer_start: exi.ch2_cr.transfer_start(),
+ dma_mode: exi.ch2_cr.dma_mode(),
+ transfer_type: exi.ch2_cr.transfer_type(),
+ transfer_length: exi.ch2_cr.transfer_length(),
+ immediate_data: exi.ch2_data.raw(),
+ });
+}
diff --git a/crates/tinyemu/src/kitty.rs b/crates/tinyemu/src/kitty.rs
new file mode 100644
index 0000000..22f4344
--- /dev/null
+++ b/crates/tinyemu/src/kitty.rs
@@ -0,0 +1,51 @@
+/// Render pixels (packed 0x00RRGGBB u32s) to the terminal via the Kitty
+/// graphics protocol.
+///
+/// Protocol: APC escape \x1b_G<key=value,...>;<base64-payload>\x1b\\
+/// a=T – transmit + display immediately
+/// f=32 – 32-bit RGBA pixels
+/// s=W,v=H – dimensions
+/// m=1 – more chunks follow; m=0 – last (or only) chunk
+#[rustfmt::skip]
+pub fn render_xfb(pixels: &[u32], width: usize, height: usize) {
+ use std::io::Write as _;
+ use base64::Engine as _;
+
+ // Convert packed RGB to RGBA (alpha = 0xFF).
+ let mut rgba: Vec<u8> = Vec::with_capacity(width * height * 4);
+ for &px in pixels {
+ 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
+ }
+
+ let encoded = base64::engine::general_purpose::STANDARD.encode(&rgba);
+
+ // Kitty protocol requires chunks of at most 4096 base64 characters
+ const CHUNK: usize = 4096;
+ let chunks: Vec<&str> = encoded
+ .as_bytes()
+ .chunks(CHUNK)
+ .map(|c| std::str::from_utf8(c).unwrap())
+ .collect();
+
+ let stdout = std::io::stdout();
+ let mut out = stdout.lock();
+
+ for (idx, chunk) in chunks.iter().enumerate() {
+ let more = if idx + 1 < chunks.len() { 1 } else { 0 };
+ if idx == 0 {
+ write!(
+ out,
+ "\x1b_Ga=T,f=32,s={},v={},m={};{}\x1b\\",
+ width, height, more, chunk
+ )
+ .unwrap();
+ } else {
+ write!(out, "\x1b_Gm={};{}\x1b\\", more, chunk).unwrap();
+ }
+ }
+
+ writeln!(out).unwrap();
+}
diff --git a/crates/tinyemu/src/main.rs b/crates/tinyemu/src/main.rs
index 748d5f7..971dc7a 100644
--- a/crates/tinyemu/src/main.rs
+++ b/crates/tinyemu/src/main.rs
@@ -1,7 +1,8 @@
+mod dump;
mod fmt;
+mod kitty;
mod snaptshot;
-use base64::Engine as _;
use clap::Parser;
use colored::Colorize;
use disasm::gekko::GekkoInstruction;
@@ -57,13 +58,14 @@ fn main() {
run_emulator(&mut gekko, &args, symbols.as_ref());
if !args.quiet {
- dump_mmio(&gekko.vi);
+ dump::vi(&gekko.vi);
+ dump::exi(&gekko.exi);
println!("Render current XFB:");
}
let pixels = gekko.render_xfb();
let video_format = gekko.vi.dcr.video_format();
- render_kitty(&pixels, video_format.columns(), video_format.lines());
+ kitty::render_xfb(&pixels, video_format.columns(), video_format.lines());
}
fn run_emulator(gekko: &mut gekko::gekko::Gekko, args: &Args, symbols: Option<&image::symbols::SymbolTable>) {
@@ -104,7 +106,7 @@ fn run_emulator(gekko: &mut gekko::gekko::Gekko, args: &Args, symbols: Option<&i
}
if args.debug && !in_busyloop && !args.quiet {
- dump_registers(&curr_snapshot, &prev_snapshot);
+ dump::registers(&curr_snapshot, &prev_snapshot);
}
prev_pc = curr_pc;
@@ -119,8 +121,8 @@ fn run_emulator(gekko: &mut gekko::gekko::Gekko, args: &Args, symbols: Option<&i
fn print_instruction(gekko: &gekko::gekko::Gekko, prev_snapshot: &CpuSnapshot, debug: bool) {
let instr = GekkoInstruction::decode(gekko.mmio.virt_slice(gekko.cpu.pc, 4))
.unwrap_or_else(|| {
- dump_registers(prev_snapshot, prev_snapshot);
- dump_memory(&gekko.mmio, gekko.cpu.read_gpr(1));
+ dump::registers(prev_snapshot, prev_snapshot);
+ dump::memory(&gekko.mmio, gekko.cpu.read_gpr(1));
panic!("Failed to decode instruction at {:08X}", gekko.cpu.pc)
})
.0;
@@ -147,148 +149,3 @@ fn print_instruction(gekko: &gekko::gekko::Gekko, prev_snapshot: &CpuSnapshot, d
println!("{}{}{}", prefix, " ".repeat(pad), comment);
}
}
-
-fn dump_registers(curr: &CpuSnapshot, prev: &CpuSnapshot) {
- let fmt_reg = |label: &str, val: u32, prev_val: u32| -> String {
- let value = format!("{:08X}", val);
- if val != prev_val {
- format!("{} {} ", label.yellow().bold(), value.bright_red().bold())
- } else {
- format!("{} {} ", label.dimmed(), value.dimmed())
- }
- };
-
- for row in 0..8 {
- let line: String = (0..4)
- .map(|col| {
- let i = row * 4 + col;
- fmt_reg(&format!("r{:<2}", i), curr.gprs[i], prev.gprs[i])
- })
- .collect();
- println!("{}", line.trim_end());
- }
-
- println!(
- "{}",
- format!(
- "{}{}",
- fmt_reg("lr ", curr.lr, prev.lr),
- fmt_reg("ctr", curr.ctr, prev.ctr)
- )
- .trim_end()
- );
-
- let cr_fields = [
- ("cr0", curr.cr.cr0(), prev.cr.cr0()),
- ("cr1", curr.cr.cr1(), prev.cr.cr1()),
- ("cr2", curr.cr.cr2(), prev.cr.cr2()),
- ("cr3", curr.cr.cr3(), prev.cr.cr3()),
- ("cr4", curr.cr.cr4(), prev.cr.cr4()),
- ("cr5", curr.cr.cr5(), prev.cr.cr5()),
- ("cr6", curr.cr.cr6(), prev.cr.cr6()),
- ("cr7", curr.cr.cr7(), prev.cr.cr7()),
- ];
-
- let fmt_cr_field =
- |label: &str, val: gekko::cpu::condition::ConditionField, prev_val: gekko::cpu::condition::ConditionField| {
- let flags = format!(
- "{}{}{}{}",
- if val.lt() { "L" } else { "·" },
- if val.gt() { "G" } else { "·" },
- if val.eq() { "Z" } else { "·" },
- if val.so() { "O" } else { "·" },
- );
- let text = format!("{}[{}] ", label, flags);
- if val.raw() != prev_val.raw() {
- format!("{}", text.bright_red().bold())
- } else {
- format!("{}", text.dimmed())
- }
- };
-
- let cr_line: String = cr_fields
- .iter()
- .map(|(label, val, prev_val)| fmt_cr_field(label, *val, *prev_val))
- .collect();
- println!("{}", cr_line.trim_end());
-
- println!();
-}
-
-fn dump_memory(mmio: &gekko::mmio::Mmio, addr: u32) {
- let aligned_addr = addr & !0xF;
- let start = aligned_addr.wrapping_sub(0x40);
- let data = mmio.virt_slice(start, 0x80);
-
- for (i, line) in data.chunks(16).enumerate() {
- let line_addr = start.wrapping_add((i as u32) * 16);
- let hex = line
- .chunks(4)
- .map(|chunk| {
- let word = u32::from_be_bytes(chunk.try_into().unwrap());
- format!("{:08X}", word)
- })
- .collect::<Vec<_>>()
- .join(" ");
-
- println!("{} {}", format!("{:08X}:", line_addr).blue().bold(), hex);
- }
-}
-
-fn dump_mmio(vi: &gekko::vi::Vi) {
- println!("Display Configuration: {:?}", vi.dcr);
- println!("Bottom Field Base: {:08X?}", vi.bfbl);
- println!("Top Field Base: {:08X?}", vi.tfbl);
- println!("XFB Address: {:08X}", vi.xfb_addr());
-}
-
-/// Render pixels (packed 0x00RRGGBB u32s) to the terminal via the Kitty
-/// graphics protocol.
-///
-/// Protocol: APC escape \x1b_G<key=value,...>;<base64-payload>\x1b\\
-/// a=T – transmit + display immediately
-/// 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 _;
-
- // Convert packed RGB to RGBA (alpha = 0xFF).
- let mut rgba: Vec<u8> = Vec::with_capacity(width * height * 4);
- for &px in pixels {
- 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
- }
-
- let encoded = base64::engine::general_purpose::STANDARD.encode(&rgba);
-
- // Kitty protocol requires chunks of at most 4096 base64 characters
- const CHUNK: usize = 4096;
- let chunks: Vec<&str> = encoded
- .as_bytes()
- .chunks(CHUNK)
- .map(|c| std::str::from_utf8(c).unwrap())
- .collect();
-
- let stdout = std::io::stdout();
- let mut out = stdout.lock();
-
- for (idx, chunk) in chunks.iter().enumerate() {
- let more = if idx + 1 < chunks.len() { 1 } else { 0 };
- if idx == 0 {
- write!(
- out,
- "\x1b_Ga=T,f=32,s={},v={},m={};{}\x1b\\",
- width, height, more, chunk
- )
- .unwrap();
- } else {
- write!(out, "\x1b_Gm={};{}\x1b\\", more, chunk).unwrap();
- }
- }
-
- writeln!(out).unwrap();
-}