Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

scale to wii clockspeed incl. artifical delays

Layle0579d08f9f09
diff --git a/Dockerfile.native b/Dockerfile.native
new file mode 100644
index 0000000..59ff8cc
--- /dev/null
+++ b/Dockerfile.native
@@ -0,0 +1,72 @@
+# Cross-compiles tinyapp, debugger and multitool for x86_64 Linux and Windows.
+# Produces a debug build and a release build separately. For the release build
+# only, all chipi bindings are switched from fn_ptr_lut to jump_table.
+#
+# Build:
+# docker build -f Dockerfile.native --output type=local,dest=docker-build .
+#
+# Output:
+# docker-build/debug/ {tinyapp, debugger, multitool}
+# docker-build/release/ {tinyapp, debugger, multitool}
+
+FROM rust:latest AS base
+
+ENV DEBIAN_FRONTEND=noninteractive
+RUN apt-get update && apt-get install -y --no-install-recommends \
+ mingw-w64 \
+ libwayland-dev libxkbcommon-dev libx11-dev libxi-dev libxrandr-dev libxcursor-dev \
+ && rm -rf /var/lib/apt/lists/*
+
+RUN update-alternatives --set x86_64-w64-mingw32-gcc /usr/bin/x86_64-w64-mingw32-gcc-posix \
+ && update-alternatives --set x86_64-w64-mingw32-g++ /usr/bin/x86_64-w64-mingw32-g++-posix
+
+RUN rustup target add x86_64-unknown-linux-gnu x86_64-pc-windows-gnu
+
+ENV CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER=x86_64-w64-mingw32-gcc \
+ CC_x86_64_pc_windows_gnu=x86_64-w64-mingw32-gcc \
+ CXX_x86_64_pc_windows_gnu=x86_64-w64-mingw32-g++ \
+ AR_x86_64_pc_windows_gnu=x86_64-w64-mingw32-ar
+
+WORKDIR /app
+COPY . .
+
+
+FROM base AS debug-build
+RUN cargo build --profile dev \
+ --target x86_64-unknown-linux-gnu \
+ --package tinyapp --package debugger --package multitool \
+ && cargo build --profile dev \
+ --target x86_64-pc-windows-gnu \
+ --package tinyapp --package debugger --package multitool
+
+RUN mkdir -p /out/debug \
+ && cp target/x86_64-unknown-linux-gnu/debug/tinyapp /out/debug/ \
+ && cp target/x86_64-unknown-linux-gnu/debug/debugger /out/debug/ \
+ && cp target/x86_64-unknown-linux-gnu/debug/multitool /out/debug/ \
+ && cp target/x86_64-pc-windows-gnu/debug/tinyapp.exe /out/debug/ \
+ && cp target/x86_64-pc-windows-gnu/debug/debugger.exe /out/debug/ \
+ && cp target/x86_64-pc-windows-gnu/debug/multitool.exe /out/debug/
+
+
+FROM base AS release-build
+RUN sed -i 's/strategy fn_ptr_lut/strategy jump_table/g' crates/gecko/*.bindings.chipi
+
+RUN cargo build --release \
+ --target x86_64-unknown-linux-gnu \
+ --package tinyapp --package debugger --package multitool \
+ && cargo build --release \
+ --target x86_64-pc-windows-gnu \
+ --package tinyapp --package debugger --package multitool
+
+RUN mkdir -p /out/release \
+ && cp target/x86_64-unknown-linux-gnu/release/tinyapp /out/release/ \
+ && cp target/x86_64-unknown-linux-gnu/release/debugger /out/release/ \
+ && cp target/x86_64-unknown-linux-gnu/release/multitool /out/release/ \
+ && cp target/x86_64-pc-windows-gnu/release/tinyapp.exe /out/release/ \
+ && cp target/x86_64-pc-windows-gnu/release/debugger.exe /out/release/ \
+ && cp target/x86_64-pc-windows-gnu/release/multitool.exe /out/release/
+
+
+FROM scratch AS artifacts
+COPY --from=debug-build /out/debug /debug
+COPY --from=release-build /out/release /release
diff --git a/crates/dbglib/src/lib.rs b/crates/dbglib/src/lib.rs
index fb8c4e3..f9f4acc 100644
--- a/crates/dbglib/src/lib.rs
+++ b/crates/dbglib/src/lib.rs
@@ -4,7 +4,7 @@ pub mod windows;
#[cfg(not(target_arch = "wasm32"))]
use std::io::Write;
-use gecko::scheduler::{CPU_CYCLES_PER_DSP_TICK, DSP_BATCH_SIZE, ScheduledFn, dsp_batch_handler};
+use gecko::scheduler::{DSP_BATCH_SIZE, ScheduledFn, cpu_cycles_per_dsp_tick, dsp_batch_handler};
use gecko::system::{System, SystemId};
/// Identify the DSP batch handler so the debugger can intercept it for per-instruction tracing.
@@ -168,9 +168,10 @@ impl Debugger {
}
}
gecko::flipper::dsp::refresh_interrupts(emulator);
- emulator
- .scheduler
- .schedule_in(CPU_CYCLES_PER_DSP_TICK * DSP_BATCH_SIZE, dsp_batch_handler::<SYSTEM>);
+ emulator.scheduler.schedule_in(
+ cpu_cycles_per_dsp_tick(SYSTEM) * DSP_BATCH_SIZE,
+ dsp_batch_handler::<SYSTEM>,
+ );
} else {
f(emulator);
}
@@ -193,9 +194,10 @@ impl Debugger {
}
}
gecko::flipper::dsp::refresh_interrupts(emulator);
- emulator
- .scheduler
- .schedule_in(CPU_CYCLES_PER_DSP_TICK * DSP_BATCH_SIZE, dsp_batch_handler::<SYSTEM>);
+ emulator.scheduler.schedule_in(
+ cpu_cycles_per_dsp_tick(SYSTEM) * DSP_BATCH_SIZE,
+ dsp_batch_handler::<SYSTEM>,
+ );
} else {
f(emulator);
}
diff --git a/crates/gecko/src/dvd.rs b/crates/gecko/src/dvd.rs
index e982398..ee7d3da 100644
--- a/crates/gecko/src/dvd.rs
+++ b/crates/gecko/src/dvd.rs
@@ -124,17 +124,20 @@ pub fn start_transfer<const SYSTEM: SystemId>(gc: &mut System<SYSTEM>) {
// that the interrupt wont fire until after it has returned and updated the state.
// Else it will get trapped inside restore_irq, right after mtmsr, executing in a loop
// of the DVD dispatch handler which in turn re-issues the same command...
- const DI_TRANSFER_DELAY: u64 = 10_000; // Based off of vxpm and hazel
- gc.scheduler.schedule_in(DI_TRANSFER_DELAY, |gc| {
- gc.di.control.set_tstart(false);
- // DMA length tracks the progress of the transfer, so when it hits 0, the
- // transfer is complete. On failure, this would denote how many bytes were
- // not transferred, but we close our eyes and just hope nothing depends on
- // that!
- gc.di.dma_length = regs::DiDmaLengthRegister::from_raw(0);
- gc.di.status.set_transfer_complete(true);
- self::refresh_interrupts(gc);
- });
+ const DI_TRANSFER_DELAY_US: u64 = 20; // Based off of vxpm and hazel (~10k cycles at GC clock)
+ gc.scheduler.schedule_in(
+ crate::scheduler::microseconds_to_cycles(SYSTEM, DI_TRANSFER_DELAY_US),
+ |gc| {
+ gc.di.control.set_tstart(false);
+ // DMA length tracks the progress of the transfer, so when it hits 0, the
+ // transfer is complete. On failure, this would denote how many bytes were
+ // not transferred, but we close our eyes and just hope nothing depends on
+ // that!
+ gc.di.dma_length = regs::DiDmaLengthRegister::from_raw(0);
+ gc.di.status.set_transfer_complete(true);
+ self::refresh_interrupts(gc);
+ },
+ );
}
#[inline(always)]
diff --git a/crates/gecko/src/flipper/ai.rs b/crates/gecko/src/flipper/ai.rs
index 3b02649..cfc8ca0 100644
--- a/crates/gecko/src/flipper/ai.rs
+++ b/crates/gecko/src/flipper/ai.rs
@@ -1,9 +1,9 @@
pub mod regs;
use crate::flipper::dsp;
+use crate::scheduler;
use crate::system::{System, SystemId};
-const CPU_CORE_CLOCK: u64 = 486_000_000;
const AUDIO_DMA_BLOCK_BYTES: u32 = 32;
const AUDIO_DMA_FRAMES_PER_BLOCK: u64 = 8;
@@ -35,7 +35,7 @@ impl AudioInterface {
/// Compute the current sample counter based on elapsed cycles
#[inline(always)]
- pub fn sample_count(&self, current_cycles: u64) -> u32 {
+ pub fn sample_count(&self, system: SystemId, current_cycles: u64) -> u32 {
if self.control.playback_status() != regs::Status::Play {
return 0;
}
@@ -45,7 +45,7 @@ impl AudioInterface {
regs::SampleRate::Rate32KHz => 32_000,
regs::SampleRate::Rate48KHz => 48_000,
};
- ((elapsed as u128 * sample_rate) / CPU_CORE_CLOCK as u128) as u32
+ ((elapsed as u128 * sample_rate) / scheduler::cpu_clock(system) as u128) as u32
}
}
@@ -143,7 +143,7 @@ fn cycles_per_audio_dma_block<const SYSTEM: SystemId>(gc: &System<SYSTEM>) -> u6
48_000
};
- AUDIO_DMA_FRAMES_PER_BLOCK * CPU_CORE_CLOCK / sample_rate
+ AUDIO_DMA_FRAMES_PER_BLOCK * scheduler::cpu_clock(SYSTEM) / sample_rate
}
#[inline(always)]
@@ -152,7 +152,7 @@ pub fn refresh_interrupts<const SYSTEM: SystemId>(gc: &mut System<SYSTEM>) {
let threshold = gc.ai.interrupt_timing.sample_count();
if threshold != 0 {
- let count = gc.ai.sample_count(gc.scheduler.cycles);
+ let count = gc.ai.sample_count(SYSTEM, gc.scheduler.cycles);
gc.ai.sample_counter = regs::AiSampleCounter::from_raw(count);
if count >= threshold {
gc.ai.control = gc.ai.control.with_interrupt(true);
diff --git a/crates/gecko/src/flipper/ai/regs.rs b/crates/gecko/src/flipper/ai/regs.rs
index f867898..f81d084 100644
--- a/crates/gecko/src/flipper/ai/regs.rs
+++ b/crates/gecko/src/flipper/ai/regs.rs
@@ -100,7 +100,7 @@ crate::mmio_reg!(AiSampleCounter: u32 @ 0xCC006C08);
impl<const SYSTEM: SystemId> MmioAccess<System<SYSTEM>> for AiSampleCounter {
fn read(gc: &mut System<SYSTEM>) -> Self {
- let count = gc.ai.sample_count(gc.scheduler.cycles);
+ let count = gc.ai.sample_count(SYSTEM, gc.scheduler.cycles);
AiSampleCounter::from_raw(count)
}
diff --git a/crates/gecko/src/flipper/dsp/regs.rs b/crates/gecko/src/flipper/dsp/regs.rs
index ba1427d..8c68753 100644
--- a/crates/gecko/src/flipper/dsp/regs.rs
+++ b/crates/gecko/src/flipper/dsp/regs.rs
@@ -334,10 +334,13 @@ impl<const SYSTEM: SystemId> MmioAccess<System<SYSTEM>> for AramDmaControl {
fn write(self, gc: &mut System<SYSTEM>, _: WriteMask) {
gc.dsp.aram_dma_control = self;
- const ARAM_DMA_DELAY: u64 = 10_000;
- gc.scheduler.schedule_in(ARAM_DMA_DELAY, |gc| {
- gc.dsp.process_aram_dma(&mut gc.mmio);
- dsp::refresh_interrupts(gc);
- });
+ const ARAM_DMA_DELAY_US: u64 = 20;
+ gc.scheduler.schedule_in(
+ crate::scheduler::microseconds_to_cycles(SYSTEM, ARAM_DMA_DELAY_US),
+ |gc| {
+ gc.dsp.process_aram_dma(&mut gc.mmio);
+ dsp::refresh_interrupts(gc);
+ },
+ );
}
}
diff --git a/crates/gecko/src/flipper/vi.rs b/crates/gecko/src/flipper/vi.rs
index 3cb3047..54bb270 100644
--- a/crates/gecko/src/flipper/vi.rs
+++ b/crates/gecko/src/flipper/vi.rs
@@ -1,8 +1,8 @@
pub mod regs;
+use crate::scheduler;
use crate::system::{System, SystemId};
-const CPU_CORE_CLOCK: u64 = 486_000_000;
const CLOCK_FREQUENCIES: [u64; 2] = [27_000_000, 54_000_000];
pub struct VideoInterface {
@@ -90,13 +90,13 @@ impl VideoInterface {
}
}
- pub fn ticks_per_sample(&self) -> u64 {
+ pub fn ticks_per_sample(&self, system: SystemId) -> u64 {
let clock_idx = self.viclk.clock_select() as usize & 1;
- 2 * CPU_CORE_CLOCK / CLOCK_FREQUENCIES[clock_idx]
+ 2 * scheduler::cpu_clock(system) / CLOCK_FREQUENCIES[clock_idx]
}
- pub fn ticks_per_half_line(&self) -> u64 {
- self.ticks_per_sample() * self.htr0.halfline_width() as u64
+ pub fn ticks_per_half_line(&self, system: SystemId) -> u64 {
+ self.ticks_per_sample(system) * self.htr0.halfline_width() as u64
}
fn half_lines_per_even_field(&self) -> u32 {
@@ -129,9 +129,9 @@ impl VideoInterface {
}
/// Compute the current DPH value from the cycle count.
- pub fn dph_value(&self, cycles: u64) -> u16 {
+ pub fn dph_value(&self, system: SystemId, cycles: u64) -> u16 {
let hl_width = self.htr0.halfline_width() as u64;
- let ticks_per_hl = self.ticks_per_half_line();
+ let ticks_per_hl = self.ticks_per_half_line(system);
if ticks_per_hl == 0 || hl_width == 0 {
return 1;
}
@@ -267,7 +267,7 @@ crate::mmio_device_dispatch! {
#[inline(always)]
pub fn ensure_half_line_scheduled<const SYSTEM: SystemId>(gc: &mut System<SYSTEM>) {
if !gc.vi.half_line_scheduled {
- let ticks_per_hl = gc.vi.ticks_per_half_line();
+ let ticks_per_hl = gc.vi.ticks_per_half_line(SYSTEM);
if ticks_per_hl > 0 {
gc.vi.half_line_scheduled = true;
gc.scheduler.schedule_in(ticks_per_hl, |gc| {
diff --git a/crates/gecko/src/flipper/vi/regs.rs b/crates/gecko/src/flipper/vi/regs.rs
index 29f8df8..8c42227 100644
--- a/crates/gecko/src/flipper/vi/regs.rs
+++ b/crates/gecko/src/flipper/vi/regs.rs
@@ -34,11 +34,11 @@ pub enum RefreshRate {
impl RefreshRate {
#[inline(always)]
- pub fn cycles_per_frame(&self) -> u64 {
- const CPU_CORE_CLOCK: u64 = 486_000_000;
+ pub const fn cycles_per_frame(&self, system: SystemId) -> u64 {
+ let clock = crate::scheduler::cpu_clock(system);
match self {
- RefreshRate::Hz60 => CPU_CORE_CLOCK / 60,
- RefreshRate::Hz50 => CPU_CORE_CLOCK / 50,
+ RefreshRate::Hz60 => clock / 60,
+ RefreshRate::Hz50 => clock / 50,
}
}
}
@@ -312,7 +312,7 @@ crate::mmio_reg!(DisplayPositionHorizontal: u16 @ 0xCC00202E);
impl<const SYSTEM: SystemId> MmioAccess<System<SYSTEM>> for DisplayPositionHorizontal {
fn read(gc: &mut System<SYSTEM>) -> Self {
let cycles = gc.scheduler.cycles;
- DisplayPositionHorizontal::from_raw(gc.vi.dph_value(cycles))
+ DisplayPositionHorizontal::from_raw(gc.vi.dph_value(SYSTEM, cycles))
}
fn write(self, _gc: &mut System<SYSTEM>, _: WriteMask) {
@@ -334,7 +334,7 @@ crate::mmio_reg!(DisplayPositionCombined: u32 @ 0xCC00202C);
impl<const SYSTEM: SystemId> MmioAccess<System<SYSTEM>> for DisplayPositionCombined {
fn read(gc: &mut System<SYSTEM>) -> Self {
let dpv = gc.vi.dpv.raw() as u32;
- let dph = gc.vi.dph_value(gc.scheduler.cycles) as u32;
+ let dph = gc.vi.dph_value(SYSTEM, gc.scheduler.cycles) as u32;
DisplayPositionCombined::from_raw((dpv << 16) | dph)
}
diff --git a/crates/gecko/src/scheduler.rs b/crates/gecko/src/scheduler.rs
index e3b0dd6..e936160 100644
--- a/crates/gecko/src/scheduler.rs
+++ b/crates/gecko/src/scheduler.rs
@@ -1,12 +1,36 @@
use std::collections::VecDeque;
use crate::flipper::vi::regs::RefreshRate;
-use crate::system::{System, SystemId};
+use crate::system::{self, System, SystemId};
pub const TIMEBASE_DIVISOR: u64 = 12;
-pub const CPU_CYCLES_PER_DSP_TICK: u64 = 6; // ~486MHz CPU / ~81MHz DSP
pub const DSP_BATCH_SIZE: u64 = 1024;
+#[inline(always)]
+#[rustfmt::skip]
+pub const fn cpu_clock(system: SystemId) -> u64 {
+ match system {
+ system::WII => 729_000_000,
+ system::GC => 486_000_000,
+ _ => unreachable!(),
+ }
+}
+
+#[inline(always)]
+#[rustfmt::skip]
+pub const fn cpu_cycles_per_dsp_tick(system: SystemId) -> u64 {
+ match system {
+ system::WII => 9, // 729 MHz / 81 MHz
+ system::GC => 6, // 486 MHz / 81 MHz
+ _ => unreachable!(),
+ }
+}
+
+#[inline(always)]
+pub const fn microseconds_to_cycles(system: SystemId, us: u64) -> u64 {
+ us * (self::cpu_clock(system) / 1_000_000)
+}
+
pub type ScheduledFn<const SYSTEM: SystemId> = fn(&mut System<SYSTEM>);
#[derive(Clone, Copy)]
@@ -112,9 +136,12 @@ impl<const SYSTEM: SystemId> Scheduler<SYSTEM> {
fn with_default_events() -> Self {
let mut s = Self::empty();
let initial_refresh_rate = RefreshRate::Hz60; // TODO: Detect IPL and schedule accordingly
- s.schedule_at(initial_refresh_rate.cycles_per_frame(), self::vsync_handler::<SYSTEM>);
s.schedule_at(
- CPU_CYCLES_PER_DSP_TICK * DSP_BATCH_SIZE,
+ initial_refresh_rate.cycles_per_frame(SYSTEM),
+ self::vsync_handler::<SYSTEM>,
+ );
+ s.schedule_at(
+ self::cpu_cycles_per_dsp_tick(SYSTEM) * self::DSP_BATCH_SIZE,
self::dsp_batch_handler::<SYSTEM>,
);
s.schedule_at(
@@ -130,14 +157,14 @@ pub fn vsync_handler<const SYSTEM: SystemId>(gc: &mut System<SYSTEM>) {
gc.vsync_pending = true;
let rate = gc.vi.dcr.video_format().refresh_rate();
gc.scheduler
- .schedule_in(rate.cycles_per_frame(), self::vsync_handler::<SYSTEM>);
+ .schedule_in(rate.cycles_per_frame(SYSTEM), self::vsync_handler::<SYSTEM>);
}
/// Reschedules itself every DSP batch.
pub fn dsp_batch_handler<const SYSTEM: SystemId>(gc: &mut System<SYSTEM>) {
gc.execute_dsp_batch();
gc.scheduler.schedule_in(
- self::CPU_CYCLES_PER_DSP_TICK * self::DSP_BATCH_SIZE,
+ self::cpu_cycles_per_dsp_tick(SYSTEM) * self::DSP_BATCH_SIZE,
self::dsp_batch_handler::<SYSTEM>,
);
}
diff --git a/crates/gecko/src/starlet.rs b/crates/gecko/src/starlet.rs
index 3c4cbbd..d2cd55c 100644
--- a/crates/gecko/src/starlet.rs
+++ b/crates/gecko/src/starlet.rs
@@ -1,15 +1,16 @@
use crate::System;
use crate::hollywood::ipc::{DeviceContext, IosDevice};
+use crate::scheduler::microseconds_to_cycles;
use crate::system::SystemId;
use std::collections::{HashMap, VecDeque};
use std::path::PathBuf;
-// As per zayd
-const FINALIZE_DELAY_CYCLES: u64 = 10_000;
+// As per zayd. Tuned at GC clock (486 MHz, ~20 us).
+const FINALIZE_DELAY_US: u64 = 20;
// Give the CPU breathing room between back-to-back IPC IRQs so other interrupt
// sources (DSP) actually get serviced. The BT stub's "no events" replies
// would otherwise hammer the IPC handler nonstop and starve the DSP IRQ.
-const ACK_TO_NEXT_DELAY_CYCLES: u64 = 50_000;
+const ACK_TO_NEXT_DELAY_US: u64 = 100;
pub struct PendingResponse {
pub cmd_paddr: u32,
@@ -144,13 +145,17 @@ impl System<{ crate::WII }> {
pub fn dispatch_command<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>, cmd_paddr: u32) {
let result = self::process_command(sys, cmd_paddr);
sys.starlet.pending.push_back(PendingResponse { cmd_paddr, result });
- sys.scheduler
- .schedule_in(FINALIZE_DELAY_CYCLES, self::deliver_pending::<SYSTEM>);
+ sys.scheduler.schedule_in(
+ microseconds_to_cycles(SYSTEM, FINALIZE_DELAY_US),
+ self::deliver_pending::<SYSTEM>,
+ );
}
pub fn schedule_drain<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>) {
- sys.scheduler
- .schedule_in(ACK_TO_NEXT_DELAY_CYCLES, self::deliver_pending::<SYSTEM>);
+ sys.scheduler.schedule_in(
+ microseconds_to_cycles(SYSTEM, ACK_TO_NEXT_DELAY_US),
+ self::deliver_pending::<SYSTEM>,
+ );
}
fn process_command<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>, cmd_paddr: u32) -> i32 {
diff --git a/crates/tinybench/src/main.rs b/crates/tinybench/src/main.rs
index 1b50c20..33f1fac 100644
--- a/crates/tinybench/src/main.rs
+++ b/crates/tinybench/src/main.rs
@@ -124,8 +124,8 @@ fn run_bench(emulator: &mut GameCube, args: &Args) {
let total_elapsed = bench_start.elapsed().as_secs_f64();
let total_fps = frame_count as f64 / total_elapsed;
let rate = emulator.vi.dcr.video_format().refresh_rate();
- let emulated_cycles = frame_count * rate.cycles_per_frame();
- let emulated_seconds = emulated_cycles as f64 / 486_000_000.0;
+ let emulated_cycles = frame_count * rate.cycles_per_frame(gecko::system::GC);
+ let emulated_seconds = emulated_cycles as f64 / gecko::scheduler::cpu_clock(gecko::system::GC) as f64;
let speed_pct = (emulated_seconds / total_elapsed) * 100.0;
println!();