Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

implement macronix exi device. bs1 transitions to bs2 now

Layled4de26574596
diff --git a/crates/gekko/src/exi.rs b/crates/gekko/src/exi.rs
index e26fc36..3e608c7 100644
--- a/crates/gekko/src/exi.rs
+++ b/crates/gekko/src/exi.rs
@@ -1,5 +1,6 @@
pub mod device;
pub mod regs;
+pub mod macronix;
use crate::exi::regs::TransferType;
use crate::mmio::Mmio;
@@ -27,6 +28,7 @@ pub struct Exi {
pub ch2_data: regs::Channel2Data,
// Devices: [channel][device_slot], 3 channels x 3 slots
devices: [[Option<Box<dyn device::ExiDevice>>; 3]; 3],
+ prev_cs: [u8; 3],
}
impl Exi {
@@ -48,6 +50,7 @@ impl Exi {
ch2_cr: regs::Channel2Control::from_raw(0),
ch2_data: regs::Channel2Data::from_raw(0),
devices: std::array::from_fn(|_| std::array::from_fn(|_| None)),
+ prev_cs: [0; 3],
}
}
@@ -184,6 +187,28 @@ impl Exi {
Some((chip_select, transfer_type, address << 5, length << 5))
}
+ pub fn process_cs_changes(&mut self) {
+ let current = [
+ self.ch0_csr.chip_select(),
+ self.ch1_csr.chip_select(),
+ self.ch2_csr.chip_select(),
+ ];
+
+ for channel in 0..3 {
+ let prev = self.prev_cs[channel];
+ let curr = current[channel];
+ if curr != prev && curr != 0 {
+ if let Some(slot) = Self::cs_to_slot(curr)
+ && let Some(device) = &mut self.devices[channel][slot]
+ {
+ device.on_select();
+ }
+ }
+ }
+
+ self.prev_cs = current;
+ }
+
pub fn process_dma_transfers(&mut self, mmio: &mut Mmio) {
const CHANNEL_COUNT: usize = 3;
diff --git a/crates/gekko/src/exi/device.rs b/crates/gekko/src/exi/device.rs
index 442dbf2..fb83392 100644
--- a/crates/gekko/src/exi/device.rs
+++ b/crates/gekko/src/exi/device.rs
@@ -1,4 +1,5 @@
pub trait ExiDevice {
+ fn on_select(&mut self) {}
fn transfer_byte(&mut self, byte: &mut u8);
fn dma_read(&mut self, buf: &mut [u8]) {
@@ -22,4 +23,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/exi/macronix.rs b/crates/gekko/src/exi/macronix.rs
new file mode 100644
index 0000000..34a2932
--- /dev/null
+++ b/crates/gekko/src/exi/macronix.rs
@@ -0,0 +1,185 @@
+// GameCube IPL ROM / SRAM / RTC device (EXI channel 0, device 1)
+// Macronix device according to yagcd, TODO: update name
+// - Address 0x000000..0x1FFFFF: Mask ROM (2 MB)
+// - Address 0x800000..0x800043: SRAM (TODO: 64 or 68 bytes??)
+// - Address 0x840000: RTC (seconds since 2000-01-01?)
+
+const IPL_START: u32 = 0x000000;
+const IPL_END: u32 = 0x1FFFFF;
+
+const SRAM_START: u32 = 0x800000;
+const SRAM_END: u32 = 0x800043;
+
+const RTC_START: u32 = 0x840000;
+const RTC_END: u32 = 0x840004;
+
+pub struct ExiMacronix {
+ rom: Vec<u8>,
+ sram: [u8; 0x44],
+ command: u32,
+ bytes_received: usize,
+ cursor: usize,
+}
+
+impl ExiMacronix {
+ pub const CHANNEL: usize = 0;
+ pub const DEVICE: usize = 1;
+
+ pub fn new(rom: Vec<u8>) -> Self {
+ Self {
+ rom,
+ sram: [0u8; 0x44],
+ command: 0,
+ bytes_received: 0,
+ cursor: 0,
+ }
+ }
+
+ fn address(&self) -> u32 {
+ (self.command >> 6) & 0x1FF_FFFF
+ }
+
+ fn is_write(&self) -> bool {
+ self.command & 0x8000_0000 != 0
+ }
+
+ fn rtc_seconds() -> u32 {
+ use std::time::{SystemTime, UNIX_EPOCH};
+ const GC_EPOCH_OFFSET: u64 = 946_684_800;
+ SystemTime::now()
+ .duration_since(UNIX_EPOCH)
+ .map(|d| (d.as_secs() - GC_EPOCH_OFFSET) as u32)
+ .unwrap_or(0)
+ }
+}
+
+impl super::device::ExiDevice for ExiMacronix {
+ fn on_select(&mut self) {
+ self.command = 0;
+ self.bytes_received = 0;
+ self.cursor = 0;
+ }
+
+ fn transfer_byte(&mut self, byte: &mut u8) {
+ if self.bytes_received < 4 {
+ self.command = (self.command << 8) | (*byte as u32);
+ *byte = 0xFF;
+ self.bytes_received += 1;
+ return;
+ }
+
+ let addr = self.address();
+
+ match addr {
+ IPL_START..=IPL_END => {
+ if !self.is_write() {
+ let offset = (addr as usize + self.cursor) % self.rom.len().max(1);
+ *byte = self.rom[offset];
+ tracing::debug!(
+ addr = format!("{:06X}", addr),
+ cursor = self.cursor,
+ value = format!("{:02X}", byte),
+ "IPL byte read"
+ );
+ }
+ }
+ SRAM_START..=SRAM_END => {
+ let offset = ((addr - SRAM_START) as usize + self.cursor) % 0x44;
+ if self.is_write() {
+ self.sram[offset] = *byte;
+ tracing::debug!(
+ addr = format!("{:06X}", addr),
+ cursor = self.cursor,
+ value = format!("{:02X}", byte),
+ "SRAM byte write"
+ );
+ } else {
+ *byte = self.sram[offset];
+ tracing::debug!(
+ addr = format!("{:06X}", addr),
+ cursor = self.cursor,
+ value = format!("{:02X}", byte),
+ "SRAM byte read"
+ );
+ }
+ }
+ RTC_START..=RTC_END => {
+ if !self.is_write() {
+ let rtc = Self::rtc_seconds().to_be_bytes();
+ *byte = rtc[self.cursor % 4];
+ tracing::debug!(
+ addr = format!("{:06X}", addr),
+ cursor = self.cursor,
+ value = format!("{:02X}", byte),
+ "RTC byte read"
+ );
+ }
+ }
+ _ => {
+ if !self.is_write() {
+ *byte = 0;
+ tracing::debug!(addr = format!("{:06X}", addr), "Unknown address read");
+ }
+ }
+ }
+
+ self.cursor += 1;
+ }
+
+ fn dma_read(&mut self, buf: &mut [u8]) {
+ let addr = self.address();
+ match addr {
+ IPL_START..=IPL_END => {
+ let start = addr as usize;
+ for (i, b) in buf.iter_mut().enumerate() {
+ *b = self.rom[start + i];
+ }
+ tracing::debug!(
+ addr = format!("{:06X}", addr),
+ size = buf.len(),
+ data = format!("{:02X?}", &buf),
+ "IPL DMA read"
+ );
+ }
+ SRAM_START..=SRAM_END => {
+ let base = (addr - SRAM_START) as usize;
+ for (i, b) in buf.iter_mut().enumerate() {
+ *b = self.sram[(base + i) % 0x44];
+ }
+ tracing::debug!(
+ addr = format!("{:06X}", addr),
+ size = buf.len(),
+ data = format!("{:02X?}", &buf),
+ "SRAM DMA read"
+ );
+ }
+ RTC_START..=RTC_END => {
+ let rtc = Self::rtc_seconds();
+ let rtc_bytes = rtc.to_be_bytes();
+ for (i, b) in buf.iter_mut().enumerate() {
+ *b = rtc_bytes[i % 4];
+ }
+ tracing::debug!(
+ addr = format!("{:06X}", addr),
+ size = buf.len(),
+ data = format!("{:02X?}", &buf),
+ time = rtc,
+ "RTC DMA read"
+ );
+ }
+ _ => {
+ buf.fill(0);
+ }
+ }
+ }
+
+ fn dma_write(&mut self, buf: &[u8]) {
+ let addr = self.address();
+ if addr >= SRAM_START && addr <= SRAM_END {
+ let base = (addr - SRAM_START) as usize;
+ for (i, b) in buf.iter().enumerate() {
+ self.sram[(base + i) % 0x44] = *b;
+ }
+ }
+ }
+}
diff --git a/crates/gekko/src/gekko.rs b/crates/gekko/src/gekko.rs
index 8afc273..9329a68 100644
--- a/crates/gekko/src/gekko.rs
+++ b/crates/gekko/src/gekko.rs
@@ -1,6 +1,6 @@
use crate::{
cpu::{self, Cpu, IPL_RESET_VECTOR, semantics::Instruction},
- exi::Exi,
+ exi::{Exi, macronix::ExiMacronix},
flipper::{dsp::Dsp, gx::Gx, pe::Pe, pi::Pi, vi::Vi},
idle::{IDLE_LOOP_MAX_INSTRS, IdleCheck, IdleDetector},
mmio::Mmio,
@@ -81,20 +81,15 @@ impl Gekko {
// |-----|--------|-------|------|-----|
// Entry point: 0x81300000
// BSS: 0x00000000 - 0x00000000 (size: 0x00000000)
+ // => BS2 DOL, does not apply to the actual IPL here!!
- let dol = image::Dol::parse(ipl.to_vec());
let mut gekko = Gekko::new(IPL_RESET_VECTOR, idle_skip);
- let data = dol.data();
-
- for section in dol.text_sections().iter().chain(dol.data_sections()) {
- for i in 0..section.size {
- let addr = section.vaddr + i;
- let value = data[(section.offset + i) as usize];
- gekko.mmio.virt_write_u8(addr, value);
- }
- }
-
gekko.mmio.ipl = ipl.to_vec();
+ gekko.exi.attach_device(
+ ExiMacronix::CHANNEL,
+ ExiMacronix::DEVICE,
+ Box::new(ExiMacronix::new(ipl.to_vec())),
+ );
gekko
}
diff --git a/crates/gekko/src/mmio/bus.rs b/crates/gekko/src/mmio/bus.rs
index 7f85873..e08ad91 100644
--- a/crates/gekko/src/mmio/bus.rs
+++ b/crates/gekko/src/mmio/bus.rs
@@ -131,6 +131,7 @@ impl Gekko {
}
BusTarget::Exi => {
self.exi.mmio_write_u8(offset, val);
+ self.exi.process_cs_changes();
self.exi.process_dma_transfers(&mut self.mmio);
}
BusTarget::Gx => {
@@ -170,6 +171,7 @@ impl Gekko {
}
BusTarget::Exi => {
self.exi.mmio_write_u16(offset, val);
+ self.exi.process_cs_changes();
self.exi.process_dma_transfers(&mut self.mmio);
}
BusTarget::Gx => {
@@ -210,6 +212,7 @@ impl Gekko {
}
BusTarget::Exi => {
self.exi.mmio_write_u32(offset, val);
+ self.exi.process_cs_changes();
self.exi.process_dma_transfers(&mut self.mmio);
}
BusTarget::Gx => {