Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Grarak committed Nov 7, 2024
1 parent 9904662 commit 1b01516
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
9 changes: 5 additions & 4 deletions src/core/memory/mmu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,17 @@ impl MmuArm9Inner {
}

fn update_wram_no_tcm(&mut self, emu: &Emu) {
let shm = &get_mem!(emu).shm;
let mem = get_mem!(emu);
let shm = &mem.shm;
let wram = &mem.wram;

for addr in (SHARED_WRAM_OFFSET..IO_PORTS_OFFSET).step_by(MMU_PAGE_SIZE) {
self.vmem.destroy_map(addr as usize, MMU_PAGE_SIZE);

let mmu_read = &mut self.mmu_read[(addr as usize) >> MMU_PAGE_SHIFT];
let mmu_write = &mut self.mmu_write[(addr as usize) >> MMU_PAGE_SHIFT];

let shm_offset = get_mem!(emu).wram.get_shm_offset::<{ ARM9 }>(addr);
let shm_offset = wram.get_shm_offset::<{ ARM9 }>(addr);
if shm_offset != usize::MAX {
self.vmem.create_map(shm, shm_offset, addr as usize, MMU_PAGE_SIZE, true, true, false).unwrap();
*mmu_read = shm_offset;
Expand All @@ -118,6 +120,7 @@ impl MmuArm9Inner {
fn update_tcm(&mut self, start: u32, end: u32, emu: &Emu) {
let shm = &get_mem!(emu).shm;

let cp15 = get_cp15!(emu, ARM9);
for addr in (start..end).step_by(MMU_PAGE_SIZE) {
self.vmem_tcm.destroy_map(addr as usize, MMU_PAGE_SIZE);

Expand Down Expand Up @@ -177,7 +180,6 @@ impl MmuArm9Inner {
_ => {}
}

let cp15 = get_cp15!(emu, ARM9);
if addr < cp15.itcm_size {
if cp15.itcm_state == TcmState::RW {
let addr_offset = (addr as usize) & (ITCM_REGION.size - 1);
Expand All @@ -198,7 +200,6 @@ impl MmuArm9Inner {
}
}

let cp15 = get_cp15!(emu, ARM9);
self.current_itcm_size = cp15.itcm_size;
self.current_dtcm_addr = cp15.dtcm_addr;
self.current_dtcm_size = cp15.dtcm_size;
Expand Down
4 changes: 2 additions & 2 deletions src/jit/assembler/block_reg_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const SCRATCH_REGS: RegReserve = reg_reserve!(Reg::R0, Reg::R1, Reg::R2, Reg::R3
pub struct BlockRegAllocator {
pub global_mapping: NoHashMap<u16, Reg>,
stored_mapping: HeapMem<Reg, { ANY_REG_LIMIT as usize }>, // mappings to real registers
stored_mapping_reverse: [Option<u16>; Reg::SP as usize],
stored_mapping_reverse: [Option<u16>; Reg::PC as usize],
spilled: BlockRegSet,
pub dirty_regs: RegReserve,
pub pre_allocate_insts: Vec<BlockInst>,
Expand All @@ -27,7 +27,7 @@ impl BlockRegAllocator {
BlockRegAllocator {
global_mapping: NoHashMap::default(),
stored_mapping: HeapMem::new(),
stored_mapping_reverse: [None; Reg::SP as usize],
stored_mapping_reverse: [None; Reg::PC as usize],
spilled: BlockRegSet::new(),
dirty_regs: RegReserve::new(),
pre_allocate_insts: Vec::new(),
Expand Down
48 changes: 35 additions & 13 deletions src/jit/jit_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::jit::op::Op;
use crate::jit::reg::Reg;
use crate::jit::reg::{reg_reserve, RegReserve};
use crate::logging::debug_println;
use crate::{get_jit_asm_ptr, DEBUG_LOG, IS_DEBUG};
use crate::{get_jit_asm_ptr, DEBUG_LOG, IS_DEBUG, LOGGING};
use std::arch::asm;
use std::cell::UnsafeCell;
use std::hint::unreachable_unchecked;
Expand Down Expand Up @@ -190,31 +190,39 @@ fn emit_code_block_internal<const CPU: CpuType, const THUMB: bool>(asm: &mut Jit

let mut block_asm = asm.new_block_asm(false);

if DEBUG_LOG {
block_asm.call1(debug_enter_block::<CPU> as *const (), guest_pc | (THUMB as u32));
block_asm.restore_reg(Reg::CPSR);
}
// if DEBUG_LOG {
block_asm.call2(debug_enter_block::<CPU> as *const (), asm as *mut _ as u32, guest_pc | (THUMB as u32));
block_asm.restore_reg(Reg::CPSR);
// }

for i in 0..asm.jit_buf.insts.len() {
asm.jit_buf.current_index = i;
asm.jit_buf.current_pc = guest_pc + (i << if THUMB { 1 } else { 2 }) as u32;
debug_println!("{CPU:?} emitting {:?} at pc: {:x}", asm.jit_buf.current_inst(), asm.jit_buf.current_pc);

// if asm.jit_buf.current_pc == 0x20018cc {
// if asm.jit_buf.current_pc == 0x2019766 {
// block_asm.bkpt(1);
// }

if asm.jit_buf.current_pc == 0x201972c {
// block_asm.bkpt(1);
let cpsr_reg = block_asm.new_reg();
block_asm.mrs_cpsr(cpsr_reg);
block_asm.call(enable_debug as *const ());
block_asm.msr_cpsr(cpsr_reg);
}

if THUMB {
asm.emit_thumb(&mut block_asm);
} else {
asm.emit(&mut block_asm);
}

if DEBUG_LOG {
block_asm.save_context();
block_asm.call2(debug_after_exec_op::<CPU> as *const (), asm.jit_buf.current_pc, asm.jit_buf.current_inst().opcode);
block_asm.restore_reg(Reg::CPSR);
}
// if DEBUG_LOG {
// block_asm.save_context();
// block_asm.call2(debug_after_exec_op::<CPU> as *const (), asm.jit_buf.current_pc, asm.jit_buf.current_inst().opcode);
// block_asm.restore_reg(Reg::CPSR);
// }
}

let opcodes_len = block_asm.emit_opcodes(guest_pc, THUMB);
Expand Down Expand Up @@ -242,6 +250,17 @@ fn emit_code_block_internal<const CPU: CpuType, const THUMB: bool>(asm: &mut Jit
jit_entry(store_host_sp);
}

static mut DEBUG_COUNT: u8 = 0;

unsafe extern "C" fn enable_debug() {
DEBUG_COUNT += 1;
if DEBUG_COUNT == 2 {
eprintln!("enable debug");
println!("enable debug");
LOGGING = true;
}
}

fn execute_internal<const CPU: CpuType>(guest_pc: u32) -> u16 {
let asm = unsafe { get_jit_asm_ptr::<CPU>().as_mut().unwrap_unchecked() };

Expand Down Expand Up @@ -354,6 +373,9 @@ unsafe extern "C" fn debug_after_exec_op<const CPU: CpuType>(pc: u32, opcode: u3
debug_inst_info::<CPU>(get_regs!((*asm).emu, CPU), pc, &format!("\n\t{:?} {:?}", CPU, inst_info));
}

extern "C" fn debug_enter_block<const CPU: CpuType>(pc: u32) {
println!("{CPU:?} execute {pc:x}")
unsafe extern "C" fn debug_enter_block<const CPU: CpuType>(asm: *mut JitAsm<CPU>, pc: u32) {
if LOGGING {
println!("{CPU:?} execute {pc:x}");
debug_inst_info::<CPU>(get_regs!((*asm).emu, CPU), pc, &format!("break in\n\t{:?}", CPU));
}
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ mod presenter;
mod settings;
mod utils;

pub static mut LOGGING: bool = false;

const BUILD_PROFILE_NAME: &str = include_str!(concat!(env!("OUT_DIR"), "/build_profile_name"));
pub const DEBUG_LOG: bool = const_str_equal(BUILD_PROFILE_NAME, "debug");
pub const IS_DEBUG: bool = !const_str_equal(BUILD_PROFILE_NAME, "release");
Expand Down

0 comments on commit 1b01516

Please sign in to comment.