// SPDX-License-Identifier: MIT //! The core under wasm: raw exports for wasm/bench.mjs (no bindings generator). //! //! `alloc(len)` returns a buffer the host fills with a ROM; `add_rom(ptr, len)` keeps it; //! `suite()` runs every kept ROM from a fresh harness to its branch to itself and returns the //! instructions executed; `cycles()` the bus cycles of the last suite. use arm7tdmi::harness::Harness; static mut ROMS: Vec> = Vec::new(); static mut CYCLES: u64 = 0; #[no_mangle] pub extern "C" fn alloc(len: usize) -> *mut u8 { let mut buffer = vec![0u8; len]; let ptr = buffer.as_mut_ptr(); std::mem::forget(buffer); ptr } /// # Safety /// `ptr` and `len` must come from `alloc` (the buffer is taken over). #[no_mangle] pub unsafe extern "C" fn add_rom(ptr: *mut u8, len: usize) { let rom = Vec::from_raw_parts(ptr, len, len); (*std::ptr::addr_of_mut!(ROMS)).push(rom); } #[no_mangle] pub extern "C" fn suite() -> u64 { let (mut steps, mut cycles) = (0u64, 0u64); // SAFETY: single-threaded wasm; ROMS is only touched by these exports. let roms = unsafe { &*std::ptr::addr_of!(ROMS) }; for rom in roms { let mut harness = Harness::new(rom, None); while !harness.at_branch_to_self() { if harness.step().is_err() { return 0; } steps += 1; } cycles += harness.cycles(); } unsafe { CYCLES = cycles }; steps } #[no_mangle] pub extern "C" fn cycles() -> u64 { unsafe { CYCLES } }