Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

OPTIMIZATIONS

Layle585a3d06ddc7
diff --git a/crates/gecko/src/cpu.rs b/crates/gecko/src/cpu.rs
index 3a43cbc..2d2a0a5 100644
--- a/crates/gecko/src/cpu.rs
+++ b/crates/gecko/src/cpu.rs
@@ -54,35 +54,41 @@ impl Cpu {
#[inline(always)]
pub fn read_gpr(&self, index: u8) -> u32 {
- self.gprs[index as usize]
+ debug_assert!(index < 32);
+ unsafe { *self.gprs.get_unchecked(index as usize) }
}
#[inline(always)]
pub fn write_gpr(&mut self, index: u8, value: u32) {
- self.gprs[index as usize] = value;
+ debug_assert!(index < 32);
+ unsafe { *self.gprs.get_unchecked_mut(index as usize) = value }
}
#[inline(always)]
pub fn read_fpr(&self, index: u8) -> f64 {
- self.fprs[index as usize]
+ debug_assert!(index < 32);
+ unsafe { *self.fprs.get_unchecked(index as usize) }
}
#[inline(always)]
pub fn write_fpr(&mut self, index: u8, value: f64) {
- self.fprs[index as usize] = value;
+ debug_assert!(index < 32);
+ unsafe { *self.fprs.get_unchecked_mut(index as usize) = value }
}
#[inline(always)]
pub fn read_ps1(&self, index: u8) -> f64 {
- self.ps1s[index as usize]
+ debug_assert!(index < 32);
+ unsafe { *self.ps1s.get_unchecked(index as usize) }
}
#[inline(always)]
pub fn write_ps1(&mut self, index: u8, value: f64) {
- self.ps1s[index as usize] = value;
+ debug_assert!(index < 32);
+ unsafe { *self.ps1s.get_unchecked_mut(index as usize) = value }
}
- #[inline]
+ #[inline(always)]
pub fn update_cr0(&mut self, val: u32) {
let so = self.spr.xer.summary_overflow(); // SO is copied from XER[SO]
self.cr.set_cr0(
@@ -95,7 +101,7 @@ impl Cpu {
}
/// Update CR1 with FPSCR[0:3] (used by Rc=1 FP instructions)
- #[inline]
+ #[inline(always)]
pub fn update_cr1(&mut self) {
let cr1 = condition::ConditionField::from((self.fpscr >> 28) as u8);
self.cr.set_field(1, cr1);
@@ -104,7 +110,12 @@ impl Cpu {
/// Read GPR with the PowerPC "rA|0" convention: returns 0 when index is 0
#[inline(always)]
pub fn read_gpr_or_zero(&self, index: u8) -> u32 {
- if index == 0 { 0 } else { self.gprs[index as usize] }
+ debug_assert!(index < 32);
+ if index == 0 {
+ 0
+ } else {
+ unsafe { *self.gprs.get_unchecked(index as usize) }
+ }
}
/// Get XER carry bit
diff --git a/crates/gecko/src/cpu/condition.rs b/crates/gecko/src/cpu/condition.rs
index 9cdd312..479e1b4 100644
--- a/crates/gecko/src/cpu/condition.rs
+++ b/crates/gecko/src/cpu/condition.rs
@@ -12,6 +12,7 @@ pub enum BranchControl {
}
impl BranchControl {
+ #[inline(always)]
pub fn from_bo(value: u8) -> Self {
let branch_hint = value & 0b1 == 0;
tracing::trace!("Branch hint: {branch_hint}");
@@ -29,6 +30,7 @@ impl BranchControl {
}
}
+ #[inline(always)]
pub fn should_branch(&self, ctr: u32, condition: bool) -> bool {
match self {
Self::BranchIfConditionTrue => condition,
@@ -43,6 +45,7 @@ impl BranchControl {
}
}
+ #[inline(always)]
pub fn should_decrement_ctr(&self) -> bool {
matches!(
self,
@@ -91,7 +94,7 @@ pub struct ConditionRegister {
}
impl ConditionRegister {
- #[inline]
+ #[inline(always)]
pub fn set_field(&mut self, index: u8, value: ConditionField) {
match index {
0 => self.set_cr0(value),
@@ -106,18 +109,19 @@ impl ConditionRegister {
}
}
- #[inline]
+ #[inline(always)]
pub fn get_bit(&self, bit: u8) -> bool {
(self.raw() >> (31 - bit)) & 1 != 0
}
- #[inline]
+ #[inline(always)]
pub fn set_bit(&mut self, bit: u8, val: bool) {
let mask = 1u32 << (31 - bit);
let raw = if val { self.raw() | mask } else { self.raw() & !mask };
*self = Self::from(raw);
}
+ #[inline(always)]
pub fn field_from_ord(ord: std::cmp::Ordering) -> ConditionField {
match ord {
std::cmp::Ordering::Less => ConditionField::new().with_lt(true),
@@ -126,7 +130,7 @@ impl ConditionRegister {
}
}
- #[inline]
+ #[inline(always)]
pub fn get_field(&self, index: u8) -> ConditionField {
match index {
0 => self.cr0(),
diff --git a/crates/gecko/src/cpu/interpreter/branch.rs b/crates/gecko/src/cpu/interpreter/branch.rs
index 1d08a22..5f6c70d 100644
--- a/crates/gecko/src/cpu/interpreter/branch.rs
+++ b/crates/gecko/src/cpu/interpreter/branch.rs
@@ -25,14 +25,7 @@ pub fn branch<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::
ctx.cpu.spr.ctr = ctx.cpu.spr.ctr.wrapping_sub(1);
}
- let (crf, bit) = (instr.bi() >> 2, instr.bi() & 0b11);
- let condition = match bit {
- 0 => ctx.cpu.cr.get_field(crf).lt(),
- 1 => ctx.cpu.cr.get_field(crf).gt(),
- 2 => ctx.cpu.cr.get_field(crf).eq(),
- 3 => ctx.cpu.cr.get_field(crf).so(),
- _ => panic!("Invalid CR bit index: {}", bit),
- };
+ let condition = ctx.cpu.cr.get_bit(instr.bi());
if !ctrl.should_branch(ctx.cpu.spr.ctr, condition) {
return;
}
diff --git a/crates/gecko/src/flipper/dsp/core.rs b/crates/gecko/src/flipper/dsp/core.rs
index 75e4855..d3f9e1e 100644
--- a/crates/gecko/src/flipper/dsp/core.rs
+++ b/crates/gecko/src/flipper/dsp/core.rs
@@ -79,6 +79,7 @@ pub struct Registers {
}
impl Registers {
+ #[inline(always)]
pub fn snapshot(&self) -> Snapshot {
Snapshot {
ac0_high: self.ac0_high,
@@ -91,6 +92,7 @@ impl Registers {
}
}
+ #[inline(always)]
pub fn restore(&mut self, snap: &Snapshot) {
self.ac0_high = snap.ac0_high;
self.ac0_mid = snap.ac0_mid;
@@ -314,9 +316,18 @@ impl Registers {
#[inline(always)]
pub fn read<const ALLOW_SATURATION: bool>(&mut self, index: u8) -> u16 {
match index {
- 0..=3 => self.ar[index as usize],
- 4..=7 => self.ix[(index - 4) as usize],
- 8..=11 => self.wr[(index - 8) as usize],
+ 0 => self.ar[0],
+ 1 => self.ar[1],
+ 2 => self.ar[2],
+ 3 => self.ar[3],
+ 4 => self.ix[0],
+ 5 => self.ix[1],
+ 6 => self.ix[2],
+ 7 => self.ix[3],
+ 8 => self.wr[0],
+ 9 => self.wr[1],
+ 10 => self.wr[2],
+ 11 => self.wr[3],
12 => self.call_stack.pop(),
13 => self.data_stack.pop(),
14 => self.loop_addr.pop(),
@@ -332,8 +343,10 @@ impl Registers {
21 => self.product_mid1,
22 => self.product_high & 0xFF,
23 => self.product_mid2,
- 24..=25 => self.ax[(index - 24) as usize],
- 26..=27 => self.axh[(index - 26) as usize],
+ 24 => self.ax[0],
+ 25 => self.ax[1],
+ 26 => self.axh[0],
+ 27 => self.axh[1],
28 => self.ac0_low,
29 => self.ac1_low,
30 => {
@@ -399,9 +412,18 @@ impl Registers {
#[inline(always)]
pub fn write<const ALLOW_SIGN_EXTENSION: bool>(&mut self, index: u8, value: u16) {
match index {
- 0..=3 => self.ar[index as usize] = value,
- 4..=7 => self.ix[(index - 4) as usize] = value,
- 8..=11 => self.wr[(index - 8) as usize] = value,
+ 0 => self.ar[0] = value,
+ 1 => self.ar[1] = value,
+ 2 => self.ar[2] = value,
+ 3 => self.ar[3] = value,
+ 4 => self.ix[0] = value,
+ 5 => self.ix[1] = value,
+ 6 => self.ix[2] = value,
+ 7 => self.ix[3] = value,
+ 8 => self.wr[0] = value,
+ 9 => self.wr[1] = value,
+ 10 => self.wr[2] = value,
+ 11 => self.wr[3] = value,
12 => self.call_stack.push(value),
13 => self.data_stack.push(value),
14 => self.loop_addr.push(value),
@@ -417,8 +439,10 @@ impl Registers {
21 => self.product_mid1 = value,
22 => self.product_high = value,
23 => self.product_mid2 = value,
- 24..=25 => self.ax[(index - 24) as usize] = value,
- 26..=27 => self.axh[(index - 26) as usize] = value,
+ 24 => self.ax[0] = value,
+ 25 => self.ax[1] = value,
+ 26 => self.axh[0] = value,
+ 27 => self.axh[1] = value,
28 => self.ac0_low = value,
29 => self.ac1_low = value,
30 => {
diff --git a/crates/gecko/src/flipper/dsp/core/stack.rs b/crates/gecko/src/flipper/dsp/core/stack.rs
index 356794b..948dcc8 100644
--- a/crates/gecko/src/flipper/dsp/core/stack.rs
+++ b/crates/gecko/src/flipper/dsp/core/stack.rs
@@ -14,23 +14,27 @@ impl<const N: usize> DspStack<N> {
#[inline(always)]
pub fn top(&self) -> u16 {
- self.data[self.ptr as usize]
+ debug_assert!((self.ptr as usize) < N);
+ unsafe { *self.data.get_unchecked(self.ptr as usize) }
}
#[inline(always)]
pub fn set_top(&mut self, value: u16) {
- self.data[self.ptr as usize] = value;
+ debug_assert!((self.ptr as usize) < N);
+ unsafe { *self.data.get_unchecked_mut(self.ptr as usize) = value }
}
#[inline(always)]
pub fn push(&mut self, value: u16) {
self.ptr = (self.ptr + 1) & Self::MASK;
- self.data[self.ptr as usize] = value;
+ debug_assert!((self.ptr as usize) < N);
+ unsafe { *self.data.get_unchecked_mut(self.ptr as usize) = value }
}
#[inline(always)]
pub fn pop(&mut self) -> u16 {
- let value = self.data[self.ptr as usize];
+ debug_assert!((self.ptr as usize) < N);
+ let value = unsafe { *self.data.get_unchecked(self.ptr as usize) };
self.ptr = (self.ptr.wrapping_sub(1)) & Self::MASK;
value
}
diff --git a/crates/gecko/src/flipper/vi/regs.rs b/crates/gecko/src/flipper/vi/regs.rs
index 06ebba9..e2731eb 100644
--- a/crates/gecko/src/flipper/vi/regs.rs
+++ b/crates/gecko/src/flipper/vi/regs.rs
@@ -8,13 +8,17 @@ use chapa::BitEnum;
#[chapa::bitfield(u16, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct VerticalTiming {
- #[bits(0..=3, alias = "equ")] pub equalization_pulse: u8,
- #[bits(4..=13, alias = "acv")] pub active_video: u16,
+ #[bits(0..=3, alias = "equ")]
+ pub equalization_pulse: u8,
+ #[bits(4..=13, alias = "acv")]
+ pub active_video: u16,
}
crate::mmio_reg!(VerticalTiming: u16 @ 0xCC002000);
impl MmioAccess<GameCube> for VerticalTiming {
- fn read(gc: &mut GameCube) -> Self { gc.vi.vtr }
+ fn read(gc: &mut GameCube) -> Self {
+ gc.vi.vtr
+ }
fn write(self, gc: &mut GameCube, _: WriteMask) {
gc.vi.vtr = self;
vi::ensure_half_line_scheduled(gc);
@@ -59,13 +63,20 @@ impl VideoFormat {
#[chapa::bitfield(u16, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct DisplayConfiguration {
- #[bits(0, alias = "enb")] pub enable: bool,
- #[bits(1, alias = "rst")] pub reset: bool,
- #[bits(2, alias = "nin")] pub interlace_selector: bool,
- #[bits(3, alias = "dlr")] pub display_mode_3d: bool,
- #[bits(4..=5, alias = "le0")] pub display_latch0: u8,
- #[bits(6..=7, alias = "le1")] pub display_latch1: u8,
- #[bits(8..=9, alias = "fmt")] pub video_format: VideoFormat,
+ #[bits(0, alias = "enb")]
+ pub enable: bool,
+ #[bits(1, alias = "rst")]
+ pub reset: bool,
+ #[bits(2, alias = "nin")]
+ pub interlace_selector: bool,
+ #[bits(3, alias = "dlr")]
+ pub display_mode_3d: bool,
+ #[bits(4..=5, alias = "le0")]
+ pub display_latch0: u8,
+ #[bits(6..=7, alias = "le1")]
+ pub display_latch1: u8,
+ #[bits(8..=9, alias = "fmt")]
+ pub video_format: VideoFormat,
}
crate::mmio_reg!(DisplayConfiguration: u16 @ 0xCC002002);
@@ -76,7 +87,9 @@ impl DisplayConfiguration {
}
impl MmioAccess<GameCube> for DisplayConfiguration {
- fn read(gc: &mut GameCube) -> Self { gc.vi.dcr }
+ fn read(gc: &mut GameCube) -> Self {
+ gc.vi.dcr
+ }
fn write(self, gc: &mut GameCube, _: WriteMask) {
// TODO: Rising edge on RST clears the register. Just a test for now.
@@ -94,14 +107,19 @@ impl MmioAccess<GameCube> for DisplayConfiguration {
#[chapa::bitfield(u32, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct HorizontalTiming0 {
- #[bits(0..=8, alias = "hlw")] pub halfline_width: u16,
- #[bits(16..=22, alias = "hce")] pub horizontal_sync_end: u8,
- #[bits(24..=30, alias = "hcs")] pub horizontal_sync_start: u8,
+ #[bits(0..=8, alias = "hlw")]
+ pub halfline_width: u16,
+ #[bits(16..=22, alias = "hce")]
+ pub horizontal_sync_end: u8,
+ #[bits(24..=30, alias = "hcs")]
+ pub horizontal_sync_start: u8,
}
crate::mmio_reg!(HorizontalTiming0: u32 @ 0xCC002004);
impl MmioAccess<GameCube> for HorizontalTiming0 {
- fn read(gc: &mut GameCube) -> Self { gc.vi.htr0 }
+ fn read(gc: &mut GameCube) -> Self {
+ gc.vi.htr0
+ }
fn write(self, gc: &mut GameCube, _: WriteMask) {
gc.vi.htr0 = self;
vi::ensure_half_line_scheduled(gc);
@@ -113,9 +131,12 @@ impl MmioAccess<GameCube> for HorizontalTiming0 {
#[chapa::bitfield(u32, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct HorizontalTiming1 {
- #[bits(0..=6, alias = "hsy")] pub horizontal_sync_width: u8,
- #[bits(7..=16, alias = "hbe")] pub horizontal_blank_end: u16,
- #[bits(17..=26, alias = "hbs")] pub horizontal_blank_start: u16,
+ #[bits(0..=6, alias = "hsy")]
+ pub horizontal_sync_width: u8,
+ #[bits(7..=16, alias = "hbe")]
+ pub horizontal_blank_end: u16,
+ #[bits(17..=26, alias = "hbs")]
+ pub horizontal_blank_start: u16,
}
crate::mmio_reg!(HorizontalTiming1: u32 @ 0xCC002008);
crate::mmio_default_access!(HorizontalTiming1 => GameCube.vi.htr1);
@@ -125,13 +146,17 @@ crate::mmio_default_access!(HorizontalTiming1 => GameCube.vi.htr1);
#[chapa::bitfield(u32, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct VerticalTimingOdd {
- #[bits(0..=9, alias = "prb")] pub pre_blanking_in_half_lines: u16,
- #[bits(16..=25, alias = "psb")] pub post_blanking_in_half_lines: u16,
+ #[bits(0..=9, alias = "prb")]
+ pub pre_blanking_in_half_lines: u16,
+ #[bits(16..=25, alias = "psb")]
+ pub post_blanking_in_half_lines: u16,
}
crate::mmio_reg!(VerticalTimingOdd: u32 @ 0xCC00200C);
impl MmioAccess<GameCube> for VerticalTimingOdd {
- fn read(gc: &mut GameCube) -> Self { gc.vi.vto }
+ fn read(gc: &mut GameCube) -> Self {
+ gc.vi.vto
+ }
fn write(self, gc: &mut GameCube, _: WriteMask) {
gc.vi.vto = self;
vi::ensure_half_line_scheduled(gc);
@@ -143,13 +168,17 @@ impl MmioAccess<GameCube> for VerticalTimingOdd {
#[chapa::bitfield(u32, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct VerticalTimingEven {
- #[bits(0..=9, alias = "prb")] pub pre_blanking_in_half_lines: u16,
- #[bits(16..=25, alias = "psb")] pub post_blanking_in_half_lines: u16,
+ #[bits(0..=9, alias = "prb")]
+ pub pre_blanking_in_half_lines: u16,
+ #[bits(16..=25, alias = "psb")]
+ pub post_blanking_in_half_lines: u16,
}
crate::mmio_reg!(VerticalTimingEven: u32 @ 0xCC002010);
impl MmioAccess<GameCube> for VerticalTimingEven {
- fn read(gc: &mut GameCube) -> Self { gc.vi.vte }
+ fn read(gc: &mut GameCube) -> Self {
+ gc.vi.vte
+ }
fn write(self, gc: &mut GameCube, _: WriteMask) {
gc.vi.vte = self;
vi::ensure_half_line_scheduled(gc);
@@ -161,10 +190,14 @@ impl MmioAccess<GameCube> for VerticalTimingEven {
#[chapa::bitfield(u32, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct BurstBlankingEvenInterval {
- #[bits(0..=4, alias = "bs1")] pub burst_start_1: u8,
- #[bits(5..=15, alias = "be1")] pub burst_end_1: u16,
- #[bits(16..=20, alias = "bs3")] pub burst_start_3: u8,
- #[bits(21..=31, alias = "be3")] pub burst_end_3: u16,
+ #[bits(0..=4, alias = "bs1")]
+ pub burst_start_1: u8,
+ #[bits(5..=15, alias = "be1")]
+ pub burst_end_1: u16,
+ #[bits(16..=20, alias = "bs3")]
+ pub burst_start_3: u8,
+ #[bits(21..=31, alias = "be3")]
+ pub burst_end_3: u16,
}
crate::mmio_reg!(BurstBlankingEvenInterval: u32 @ 0xCC002014);
crate::mmio_default_access!(BurstBlankingEvenInterval => GameCube.vi.bbei);
@@ -174,10 +207,14 @@ crate::mmio_default_access!(BurstBlankingEvenInterval => GameCube.vi.bbei);
#[chapa::bitfield(u32, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct BurstBlankingOddInterval {
- #[bits(0..=4, alias = "bs2")] pub burst_start_2: u8,
- #[bits(5..=15, alias = "be2")] pub burst_end_2: u16,
- #[bits(16..=20, alias = "bs4")] pub burst_start_4: u8,
- #[bits(21..=31, alias = "be4")] pub burst_end_4: u16,
+ #[bits(0..=4, alias = "bs2")]
+ pub burst_start_2: u8,
+ #[bits(5..=15, alias = "be2")]
+ pub burst_end_2: u16,
+ #[bits(16..=20, alias = "bs4")]
+ pub burst_start_4: u8,
+ #[bits(21..=31, alias = "be4")]
+ pub burst_end_4: u16,
}
crate::mmio_reg!(BurstBlankingOddInterval: u32 @ 0xCC002018);
crate::mmio_default_access!(BurstBlankingOddInterval => GameCube.vi.bboi);
@@ -205,7 +242,8 @@ crate::mmio_default_access!(TopFieldBase => GameCube.vi.tfbl);
#[chapa::bitfield(u32, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct TopFieldBaseRight {
- #[bits(0..=23, alias = "fbb")] pub xfb_addr: u32,
+ #[bits(0..=23, alias = "fbb")]
+ pub xfb_addr: u32,
}
crate::mmio_reg!(TopFieldBaseRight: u32 @ 0xCC002020);
crate::mmio_default_access!(TopFieldBaseRight => GameCube.vi.tfbr);
@@ -229,7 +267,8 @@ crate::mmio_default_access!(BottomFieldBase => GameCube.vi.bfbl);
#[chapa::bitfield(u32, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct BottomFieldBaseRight {
- #[bits(0..=23, alias = "fbb")] pub xfb_addr: u32,
+ #[bits(0..=23, alias = "fbb")]
+ pub xfb_addr: u32,
}
crate::mmio_reg!(BottomFieldBaseRight: u32 @ 0xCC002028);
crate::mmio_default_access!(BottomFieldBaseRight => GameCube.vi.bfbr);
@@ -239,7 +278,8 @@ crate::mmio_default_access!(BottomFieldBaseRight => GameCube.vi.bfbr);
#[chapa::bitfield(u16, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct DisplayPositionVertical {
- #[bits(0..=10, alias = "vct")] pub vertical_count: u16,
+ #[bits(0..=10, alias = "vct")]
+ pub vertical_count: u16,
}
crate::mmio_reg!(DisplayPositionVertical: u16 @ 0xCC00202C);
crate::mmio_default_access!(DisplayPositionVertical => GameCube.vi.dpv);
@@ -252,7 +292,8 @@ crate::mmio_default_access!(DisplayPositionVertical => GameCube.vi.dpv);
#[chapa::bitfield(u16, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct DisplayPositionHorizontal {
- #[bits(0..=10, alias = "hct")] pub horizontal_count: u16,
+ #[bits(0..=10, alias = "hct")]
+ pub horizontal_count: u16,
}
crate::mmio_reg!(DisplayPositionHorizontal: u16 @ 0xCC00202E);
@@ -299,15 +340,21 @@ impl MmioAccess<GameCube> for DisplayPositionCombined {
#[chapa::bitfield(u32, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct DisplayInterrupt0 {
- #[bits(0..=9, alias = "hct")] pub horizontal_count: u16,
- #[bits(16..=25, alias = "vct")] pub vertical_count: u16,
- #[bits(28, alias = "enb")] pub enable: bool,
- #[bits(31, alias = "int")] pub interrupt: bool,
+ #[bits(0..=9, alias = "hct")]
+ pub horizontal_count: u16,
+ #[bits(16..=25, alias = "vct")]
+ pub vertical_count: u16,
+ #[bits(28, alias = "enb")]
+ pub enable: bool,
+ #[bits(31, alias = "int")]
+ pub interrupt: bool,
}
crate::mmio_reg!(DisplayInterrupt0: u32 @ 0xCC002030);
impl MmioAccess<GameCube> for DisplayInterrupt0 {
- fn read(gc: &mut GameCube) -> Self { gc.vi.di0 }
+ fn read(gc: &mut GameCube) -> Self {
+ gc.vi.di0
+ }
fn write(self, gc: &mut GameCube, _: WriteMask) {
gc.vi.di0 = self;
vi::refresh_interrupts(gc);
@@ -319,15 +366,21 @@ impl MmioAccess<GameCube> for DisplayInterrupt0 {
#[chapa::bitfield(u32, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct DisplayInterrupt1 {
- #[bits(0..=9, alias = "hct")] pub horizontal_count: u16,
- #[bits(16..=25, alias = "vct")] pub vertical_count: u16,
- #[bits(28, alias = "enb")] pub enable: bool,
- #[bits(31, alias = "int")] pub interrupt: bool,
+ #[bits(0..=9, alias = "hct")]
+ pub horizontal_count: u16,
+ #[bits(16..=25, alias = "vct")]
+ pub vertical_count: u16,
+ #[bits(28, alias = "enb")]
+ pub enable: bool,
+ #[bits(31, alias = "int")]
+ pub interrupt: bool,
}
crate::mmio_reg!(DisplayInterrupt1: u32 @ 0xCC002034);
impl MmioAccess<GameCube> for DisplayInterrupt1 {
- fn read(gc: &mut GameCube) -> Self { gc.vi.di1 }
+ fn read(gc: &mut GameCube) -> Self {
+ gc.vi.di1
+ }
fn write(self, gc: &mut GameCube, _: WriteMask) {
gc.vi.di1 = self;
vi::refresh_interrupts(gc);
@@ -339,15 +392,21 @@ impl MmioAccess<GameCube> for DisplayInterrupt1 {
#[chapa::bitfield(u32, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct DisplayInterrupt2 {
- #[bits(0..=9, alias = "hct")] pub horizontal_count: u16,
- #[bits(16..=25, alias = "vct")] pub vertical_count: u16,
- #[bits(28, alias = "enb")] pub enable: bool,
- #[bits(31, alias = "int")] pub interrupt: bool,
+ #[bits(0..=9, alias = "hct")]
+ pub horizontal_count: u16,
+ #[bits(16..=25, alias = "vct")]
+ pub vertical_count: u16,
+ #[bits(28, alias = "enb")]
+ pub enable: bool,
+ #[bits(31, alias = "int")]
+ pub interrupt: bool,
}
crate::mmio_reg!(DisplayInterrupt2: u32 @ 0xCC002038);
impl MmioAccess<GameCube> for DisplayInterrupt2 {
- fn read(gc: &mut GameCube) -> Self { gc.vi.di2 }
+ fn read(gc: &mut GameCube) -> Self {
+ gc.vi.di2
+ }
fn write(self, gc: &mut GameCube, _: WriteMask) {
gc.vi.di2 = self;
vi::refresh_interrupts(gc);
@@ -359,15 +418,21 @@ impl MmioAccess<GameCube> for DisplayInterrupt2 {
#[chapa::bitfield(u32, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct DisplayInterrupt3 {
- #[bits(0..=9, alias = "hct")] pub horizontal_count: u16,
- #[bits(16..=25, alias = "vct")] pub vertical_count: u16,
- #[bits(28, alias = "enb")] pub enable: bool,
- #[bits(31, alias = "int")] pub interrupt: bool,
+ #[bits(0..=9, alias = "hct")]
+ pub horizontal_count: u16,
+ #[bits(16..=25, alias = "vct")]
+ pub vertical_count: u16,
+ #[bits(28, alias = "enb")]
+ pub enable: bool,
+ #[bits(31, alias = "int")]
+ pub interrupt: bool,
}
crate::mmio_reg!(DisplayInterrupt3: u32 @ 0xCC00203C);
impl MmioAccess<GameCube> for DisplayInterrupt3 {
- fn read(gc: &mut GameCube) -> Self { gc.vi.di3 }
+ fn read(gc: &mut GameCube) -> Self {
+ gc.vi.di3
+ }
fn write(self, gc: &mut GameCube, _: WriteMask) {
gc.vi.di3 = self;
vi::refresh_interrupts(gc);
@@ -379,9 +444,12 @@ impl MmioAccess<GameCube> for DisplayInterrupt3 {
#[chapa::bitfield(u32, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct DisplayLatch0 {
- #[bits(0..=10, alias = "hct")] pub horizontal_count: u16,
- #[bits(16..=26, alias = "vct")] pub vertical_count: u16,
- #[bits(31, alias = "trg")] pub trigger: bool,
+ #[bits(0..=10, alias = "hct")]
+ pub horizontal_count: u16,
+ #[bits(16..=26, alias = "vct")]
+ pub vertical_count: u16,
+ #[bits(31, alias = "trg")]
+ pub trigger: bool,
}
crate::mmio_reg!(DisplayLatch0: u32 @ 0xCC002040);
crate::mmio_default_access!(DisplayLatch0 => GameCube.vi.dl0);
@@ -391,9 +459,12 @@ crate::mmio_default_access!(DisplayLatch0 => GameCube.vi.dl0);
#[chapa::bitfield(u32, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct DisplayLatch1 {
- #[bits(0..=10, alias = "hct")] pub horizontal_count: u16,
- #[bits(16..=26, alias = "vct")] pub vertical_count: u16,
- #[bits(31, alias = "trg")] pub trigger: bool,
+ #[bits(0..=10, alias = "hct")]
+ pub horizontal_count: u16,
+ #[bits(16..=26, alias = "vct")]
+ pub vertical_count: u16,
+ #[bits(31, alias = "trg")]
+ pub trigger: bool,
}
crate::mmio_reg!(DisplayLatch1: u32 @ 0xCC002044);
crate::mmio_default_access!(DisplayLatch1 => GameCube.vi.dl1);
@@ -403,7 +474,8 @@ crate::mmio_default_access!(DisplayLatch1 => GameCube.vi.dl1);
#[chapa::bitfield(u16, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct HorizontalScalingWidth {
- #[bits(0..=9, alias = "srcwidth")] pub source_width: u16,
+ #[bits(0..=9, alias = "srcwidth")]
+ pub source_width: u16,
}
crate::mmio_reg!(HorizontalScalingWidth: u16 @ 0xCC002048);
crate::mmio_default_access!(HorizontalScalingWidth => GameCube.vi.hsw);
@@ -413,8 +485,10 @@ crate::mmio_default_access!(HorizontalScalingWidth => GameCube.vi.hsw);
#[chapa::bitfield(u16, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct HorizontalScalingRegister {
- #[bits(0..=8, alias = "stp")] pub step_size: u16,
- #[bits(12, alias = "hs_en")] pub horizontal_scaling_enable: bool,
+ #[bits(0..=8, alias = "stp")]
+ pub step_size: u16,
+ #[bits(12, alias = "hs_en")]
+ pub horizontal_scaling_enable: bool,
}
crate::mmio_reg!(HorizontalScalingRegister: u16 @ 0xCC00204A);
crate::mmio_default_access!(HorizontalScalingRegister => GameCube.vi.hsr);
@@ -424,9 +498,12 @@ crate::mmio_default_access!(HorizontalScalingRegister => GameCube.vi.hsr);
#[chapa::bitfield(u32, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct FilterCoefficient0 {
- #[bits(0..=9, alias = "t0")] pub tap0: u16,
- #[bits(10..=19, alias = "t1")] pub tap1: u16,
- #[bits(20..=29, alias = "t2")] pub tap2: u16,
+ #[bits(0..=9, alias = "t0")]
+ pub tap0: u16,
+ #[bits(10..=19, alias = "t1")]
+ pub tap1: u16,
+ #[bits(20..=29, alias = "t2")]
+ pub tap2: u16,
}
crate::mmio_reg!(FilterCoefficient0: u32 @ 0xCC00204C);
crate::mmio_default_access!(FilterCoefficient0 => GameCube.vi.fct0);
@@ -436,9 +513,12 @@ crate::mmio_default_access!(FilterCoefficient0 => GameCube.vi.fct0);
#[chapa::bitfield(u32, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct FilterCoefficient1 {
- #[bits(0..=9, alias = "t3")] pub tap3: u16,
- #[bits(10..=19, alias = "t4")] pub tap4: u16,
- #[bits(20..=29, alias = "t5")] pub tap5: u16,
+ #[bits(0..=9, alias = "t3")]
+ pub tap3: u16,
+ #[bits(10..=19, alias = "t4")]
+ pub tap4: u16,
+ #[bits(20..=29, alias = "t5")]
+ pub tap5: u16,
}
crate::mmio_reg!(FilterCoefficient1: u32 @ 0xCC002050);
crate::mmio_default_access!(FilterCoefficient1 => GameCube.vi.fct1);
@@ -448,9 +528,12 @@ crate::mmio_default_access!(FilterCoefficient1 => GameCube.vi.fct1);
#[chapa::bitfield(u32, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct FilterCoefficient2 {
- #[bits(0..=9, alias = "t6")] pub tap6: u16,
- #[bits(10..=19, alias = "t7")] pub tap7: u16,
- #[bits(20..=29, alias = "t8")] pub tap8: u16,
+ #[bits(0..=9, alias = "t6")]
+ pub tap6: u16,
+ #[bits(10..=19, alias = "t7")]
+ pub tap7: u16,
+ #[bits(20..=29, alias = "t8")]
+ pub tap8: u16,
}
crate::mmio_reg!(FilterCoefficient2: u32 @ 0xCC002054);
crate::mmio_default_access!(FilterCoefficient2 => GameCube.vi.fct2);
@@ -460,10 +543,14 @@ crate::mmio_default_access!(FilterCoefficient2 => GameCube.vi.fct2);
#[chapa::bitfield(u32, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct FilterCoefficient3 {
- #[bits(0..=7, alias = "t9")] pub tap9: u8,
- #[bits(8..=15, alias = "t10")] pub tap10: u8,
- #[bits(16..=23, alias = "t11")] pub tap11: u8,
- #[bits(24..=31, alias = "t12")] pub tap12: u8,
+ #[bits(0..=7, alias = "t9")]
+ pub tap9: u8,
+ #[bits(8..=15, alias = "t10")]
+ pub tap10: u8,
+ #[bits(16..=23, alias = "t11")]
+ pub tap11: u8,
+ #[bits(24..=31, alias = "t12")]
+ pub tap12: u8,
}
crate::mmio_reg!(FilterCoefficient3: u32 @ 0xCC002058);
crate::mmio_default_access!(FilterCoefficient3 => GameCube.vi.fct3);
@@ -473,10 +560,14 @@ crate::mmio_default_access!(FilterCoefficient3 => GameCube.vi.fct3);
#[chapa::bitfield(u32, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct FilterCoefficient4 {
- #[bits(0..=7, alias = "t13")] pub tap13: u8,
- #[bits(8..=15, alias = "t14")] pub tap14: u8,
- #[bits(16..=23, alias = "t15")] pub tap15: u8,
- #[bits(24..=31, alias = "t16")] pub tap16: u8,
+ #[bits(0..=7, alias = "t13")]
+ pub tap13: u8,
+ #[bits(8..=15, alias = "t14")]
+ pub tap14: u8,
+ #[bits(16..=23, alias = "t15")]
+ pub tap15: u8,
+ #[bits(24..=31, alias = "t16")]
+ pub tap16: u8,
}
crate::mmio_reg!(FilterCoefficient4: u32 @ 0xCC00205C);
crate::mmio_default_access!(FilterCoefficient4 => GameCube.vi.fct4);
@@ -486,10 +577,14 @@ crate::mmio_default_access!(FilterCoefficient4 => GameCube.vi.fct4);
#[chapa::bitfield(u32, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct FilterCoefficient5 {
- #[bits(0..=7, alias = "t17")] pub tap17: u8,
- #[bits(8..=15, alias = "t18")] pub tap18: u8,
- #[bits(16..=23, alias = "t19")] pub tap19: u8,
- #[bits(24..=31, alias = "t20")] pub tap20: u8,
+ #[bits(0..=7, alias = "t17")]
+ pub tap17: u8,
+ #[bits(8..=15, alias = "t18")]
+ pub tap18: u8,
+ #[bits(16..=23, alias = "t19")]
+ pub tap19: u8,
+ #[bits(24..=31, alias = "t20")]
+ pub tap20: u8,
}
crate::mmio_reg!(FilterCoefficient5: u32 @ 0xCC002060);
crate::mmio_default_access!(FilterCoefficient5 => GameCube.vi.fct5);
@@ -499,10 +594,14 @@ crate::mmio_default_access!(FilterCoefficient5 => GameCube.vi.fct5);
#[chapa::bitfield(u32, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct FilterCoefficient6 {
- #[bits(0..=7, alias = "t21")] pub tap21: u8,
- #[bits(8..=15, alias = "t22")] pub tap22: u8,
- #[bits(16..=23, alias = "t23")] pub tap23: u8,
- #[bits(24..=31, alias = "t24")] pub tap24: u8,
+ #[bits(0..=7, alias = "t21")]
+ pub tap21: u8,
+ #[bits(8..=15, alias = "t22")]
+ pub tap22: u8,
+ #[bits(16..=23, alias = "t23")]
+ pub tap23: u8,
+ #[bits(24..=31, alias = "t24")]
+ pub tap24: u8,
}
crate::mmio_reg!(FilterCoefficient6: u32 @ 0xCC002064);
crate::mmio_default_access!(FilterCoefficient6 => GameCube.vi.fct6);
@@ -512,12 +611,15 @@ crate::mmio_default_access!(FilterCoefficient6 => GameCube.vi.fct6);
#[chapa::bitfield(u16, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct ViClockSelect {
- #[bits(0, alias = "clk")] pub clock_select: bool,
+ #[bits(0, alias = "clk")]
+ pub clock_select: bool,
}
crate::mmio_reg!(ViClockSelect: u16 @ 0xCC00206C);
impl MmioAccess<GameCube> for ViClockSelect {
- fn read(gc: &mut GameCube) -> Self { gc.vi.viclk }
+ fn read(gc: &mut GameCube) -> Self {
+ gc.vi.viclk
+ }
fn write(self, gc: &mut GameCube, _: WriteMask) {
gc.vi.viclk = self;
vi::ensure_half_line_scheduled(gc);
@@ -529,7 +631,8 @@ impl MmioAccess<GameCube> for ViClockSelect {
#[chapa::bitfield(u16, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct ViDtvStatus {
- #[bits(2, alias = "visel")] pub dtv_status: bool,
+ #[bits(2, alias = "visel")]
+ pub dtv_status: bool,
}
crate::mmio_reg!(ViDtvStatus: u16 @ 0xCC00206E);
crate::mmio_default_access!(ViDtvStatus => GameCube.vi.visel);
@@ -547,8 +650,10 @@ crate::mmio_default_access!(ViUnknown70 => GameCube.vi.unknown_70);
#[chapa::bitfield(u16, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct BorderHbe {
- #[bits(0..=9, alias = "hbe656")] pub horizontal_blank_end_656: u16,
- #[bits(15, alias = "brdr_en")] pub border_enable: bool,
+ #[bits(0..=9, alias = "hbe656")]
+ pub horizontal_blank_end_656: u16,
+ #[bits(15, alias = "brdr_en")]
+ pub border_enable: bool,
}
crate::mmio_reg!(BorderHbe: u16 @ 0xCC002072);
crate::mmio_default_access!(BorderHbe => GameCube.vi.border_hbe);
@@ -558,7 +663,8 @@ crate::mmio_default_access!(BorderHbe => GameCube.vi.border_hbe);
#[chapa::bitfield(u16, order = lsb0)]
#[derive(Copy, Clone, Debug)]
pub struct BorderHbs {
- #[bits(0..=9, alias = "hbs656")] pub horizontal_blank_start_656: u16,
+ #[bits(0..=9, alias = "hbs656")]
+ pub horizontal_blank_start_656: u16,
}
crate::mmio_reg!(BorderHbs: u16 @ 0xCC002074);
crate::mmio_default_access!(BorderHbs => GameCube.vi.border_hbs);
diff --git a/crates/gecko/src/mmio.rs b/crates/gecko/src/mmio.rs
index 3ae732a..d1d8a2c 100644
--- a/crates/gecko/src/mmio.rs
+++ b/crates/gecko/src/mmio.rs
@@ -4,7 +4,6 @@ pub mod fp;
pub mod macros;
pub mod traits;
-
use constants::*;
pub struct Mmio {
diff --git a/crates/gecko/src/scheduler.rs b/crates/gecko/src/scheduler.rs
index 74e69b8..cde2bb5 100644
--- a/crates/gecko/src/scheduler.rs
+++ b/crates/gecko/src/scheduler.rs
@@ -1,6 +1,4 @@
-use std::cmp::Ordering;
-use std::collections::BinaryHeap;
-use std::collections::binary_heap::PeekMut;
+use std::collections::VecDeque;
use crate::gamecube::GameCube;
@@ -11,42 +9,26 @@ pub const DSP_BATCH_SIZE: u64 = 1024;
pub type ScheduledFn = fn(&mut GameCube);
-#[derive(Clone, Copy, Eq)]
+#[derive(Clone, Copy)]
struct ScheduledEvent {
deadline: u64,
f: ScheduledFn,
}
-impl PartialEq for ScheduledEvent {
- fn eq(&self, other: &Self) -> bool {
- self.deadline == other.deadline
- }
-}
-impl Ord for ScheduledEvent {
- fn cmp(&self, other: &Self) -> Ordering {
- other.deadline.cmp(&self.deadline)
- }
-}
-impl PartialOrd for ScheduledEvent {
- fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
- Some(self.cmp(other))
- }
-}
-
pub struct Scheduler {
pub cycles: u64,
next_deadline: u64,
timebase_offset: i64,
- events: BinaryHeap<ScheduledEvent>,
+ events: VecDeque<ScheduledEvent>,
}
impl Scheduler {
pub fn new() -> Self {
let mut s = Scheduler {
cycles: 0,
- next_deadline: 0,
+ next_deadline: u64::MAX,
timebase_offset: 0,
- events: BinaryHeap::new(),
+ events: VecDeque::with_capacity(8),
};
s.schedule_at(CYCLES_PER_VSYNC, crate::scheduler::vsync_handler);
s.schedule_at(
@@ -56,12 +38,17 @@ impl Scheduler {
s
}
+ #[inline(always)]
+ fn refresh_deadline(&mut self) {
+ self.next_deadline = self.events.front().map_or(u64::MAX, |e| e.deadline);
+ }
+
/// Set `next_deadline` to the next event deadline so the CPU knows
/// how far it can run before an event must be serviced.
/// This may later be updated if an event is scheduled sooner than the current target.
#[inline(always)]
pub fn update_deadline(&mut self) {
- self.next_deadline = self.events.peek().map_or(self.cycles, |e| e.deadline);
+ self.refresh_deadline();
}
pub fn timebase(&self) -> u64 {
@@ -88,11 +75,11 @@ impl Scheduler {
(self.timebase() >> 32) as u32
}
+ /// Insert an event keeping the deque sorted by deadline (earliest first).
pub fn schedule_at(&mut self, deadline: u64, f: ScheduledFn) {
- self.events.push(ScheduledEvent { deadline, f });
- if deadline < self.next_deadline {
- self.next_deadline = deadline;
- }
+ let pos = self.events.partition_point(|e| e.deadline <= deadline);
+ self.events.insert(pos, ScheduledEvent { deadline, f });
+ self.next_deadline = self.next_deadline.min(deadline);
}
pub fn schedule_in(&mut self, delay: u64, f: ScheduledFn) {
@@ -100,10 +87,13 @@ impl Scheduler {
self.schedule_at(deadline, f);
}
+ #[inline(always)]
pub fn poll(&mut self) -> Option<ScheduledFn> {
- let top = self.events.peek_mut()?;
- if self.cycles >= top.deadline {
- Some(PeekMut::pop(top).f)
+ let front = self.events.front()?;
+ if self.cycles >= front.deadline {
+ let f = self.events.pop_front().unwrap().f;
+ self.refresh_deadline();
+ Some(f)
} else {
None
}