use super::cpu::CpuCore;
pub(crate) const NO_CODE_PAGE: u32 = 1;
impl CpuCore {
#[inline]
pub(crate) fn refresh_window_mmu(&mut self) {
self.fm_mmu = if self.fm_len != 0 {
crate::mmu::ResidentMmu::of(self)
} else {
crate::mmu::ResidentMmu::default()
};
self.forget_code_page();
}
#[inline(always)]
pub(crate) fn forget_code_page(&mut self) {
self.fm_code_page = NO_CODE_PAGE;
}
#[inline(always)]
pub(crate) fn code_window_off(&mut self, addr: u32) -> Option<u32> {
debug_assert!(self.fm_mmu.enabled());
match self.remembered_code_off(addr) {
Some(off) => Some(off),
None => self.remember_code_page(addr),
}
}
#[inline(always)]
pub(crate) fn remembered_code_off(&self, addr: u32) -> Option<u32> {
if addr & !self.fm_mmu.page_mask != self.fm_code_page {
return None;
}
let off = addr.wrapping_add(self.fm_code_delta);
let off = (off <= self.fm_len - 2).then_some(off);
debug_assert_eq!(off, self.translated_window_off(addr, 2, false, true));
off
}
#[inline(never)]
fn remember_code_page(&mut self, addr: u32) -> Option<u32> {
let off = self.translated_window_off(addr, 2, false, true)?;
self.fm_code_page = addr & !self.fm_mmu.page_mask;
self.fm_code_delta = off.wrapping_sub(addr);
Some(off)
}
#[cfg(any(not(feature = "jit"), target_family = "wasm", test))]
#[inline(always)]
pub(crate) fn window_off(
&self,
addr: u32,
bytes: u32,
write: bool,
instruction: bool,
) -> Option<u32> {
debug_assert!((1..=4).contains(&bytes));
debug_assert!(self.fm_len >= 4, "window_off without an installed window");
if self.fm_mmu.enabled() {
return self.translated_window_off(addr, bytes, write, instruction);
}
let off = addr.wrapping_sub(self.fm_base);
(off <= self.fm_len - bytes).then_some(off)
}
#[cfg(any(not(feature = "jit"), target_family = "wasm", test))]
#[inline(always)]
pub(crate) fn window_range_off(&self, addr: u32, bytes: u32, write: bool) -> Option<u32> {
if self.fm_mmu.enabled() {
return self.translated_window_off(addr, bytes, write, false);
}
let off = addr.wrapping_sub(self.fm_base);
(bytes <= self.fm_len && off <= self.fm_len - bytes).then_some(off)
}
#[inline(never)]
pub(crate) fn translated_window_off(
&self,
addr: u32,
bytes: u32,
write: bool,
instruction: bool,
) -> Option<u32> {
let mmu = self.fm_mmu;
if (addr & mmu.page_mask).wrapping_add(bytes - 1) > mmu.page_mask {
return None;
}
if self.mmu_fc_override.is_some() {
return None;
}
if !instruction
&& (self
.mmu_read_override
.is_some_and(|(at, _)| at.wrapping_sub(addr) < bytes)
|| self
.mmu_write_suppress
.is_some_and(|at| at.wrapping_sub(addr) < bytes))
{
return None;
}
let phys = crate::mmu::probe_resident(self, mmu, addr, write, instruction)?;
let off = self.address(phys).wrapping_sub(self.fm_base);
(bytes <= self.fm_len && off <= self.fm_len - bytes).then_some(off)
}
}
#[cfg(all(feature = "jit", not(target_family = "wasm")))]
pub(crate) extern "C" fn trace_window_off(cpu: *const CpuCore, addr: u32, request: u32) -> u32 {
let cpu = unsafe { &*cpu };
cpu.translated_window_off(addr, request & 0xFF, request & 0x100 != 0, false)
.unwrap_or(u32::MAX)
}