// SPDX-License-Identifier: MIT //! The speed workload: sh2-python's ``benchmarks/harness_speed.py``, transcribed. //! //! A loop that mixes what real SH-2 code does: longword and byte loads and stores //! through a pointer, ALU operations, a MULS.W and an STS, a TST and a delayed //! conditional branch (BF/S with work in its slot), over a 256-byte buffer in area 3, //! on the minimal harness with WCR = 0 (no programmed waits), the cache off or on. Used //! by ``src/bin/sh2-bench.rs`` (native) and ``src/wasm.rs`` (the browser's number). use crate::cpu::Options; use crate::harness::Harness; pub const LOOP: [u16; 15] = [ 0xD10A, // MOV.L @(40,PC),R1 buffer 0xE240, // MOV #64,R2 count (longwords) 0xE000, // MOV #0,R0 sum // loop: 0x6316, // MOV.L @R1+,R3 0x6434, // MOV.B @R3+,R4 -> uses R3 as an address: replaced below 0x303C, // ADD R3,R0 0x234F, // MULS.W R4,R3 0x051A, // STS MACL,R5 0x305C, // ADD R5,R0 0x2338, // TST R3,R3 0x4210, // DT R2 0x8FF7, // BF/S loop 0x7001, // ADD #1,R0 (slot) 0xAFFE, // BRA $ 0x0009, // NOP ]; /// ``program()``: the image, loaded at H'00000000. pub fn program() -> Vec { let mut words = LOOP; words[4] = 0x6403; // MOV R0,R4 (keep the loop's memory accesses inside the buffer) let mut body = Vec::new(); body.extend_from_slice(&0x100u32.to_be_bytes()); body.extend_from_slice(&0x06008000u32.to_be_bytes()); body.resize(0x100, 0); let mut code: Vec = words.iter().flat_map(|w| w.to_be_bytes()).collect(); let pad = (4 - code.len() % 4) % 4; code.resize(code.len() + pad + 40 - 4, 0); let mut image = vec![0u8; 0x200]; let blob: Vec = body.into_iter().chain(code).collect(); image[..blob.len()].copy_from_slice(&blob); // Place the pool where MOV.L @(40,PC) at 0x100 reads: (0x100 + 4) & ~3 + 40. image[0x104 + 40..0x104 + 44].copy_from_slice(&0x06001000u32.to_be_bytes()); image } /// A fresh harness, reset, with WCR = 0 and the cache on or off (``run``'s setup). pub fn setup(cache: bool) -> Harness { let mut harness = Harness::new(&program(), 0, Vec::new(), Options::default()); let chip = &mut harness.chip; chip.reset(true); // One-wait-free BSC: area 0 and 3 as 32-bit, no programmed waits (WCR = 0). chip.write(0xFFFFFFE8, 32, 0xA55A0000); if cache { chip.write(0xFFFFFE92, 8, 0x11); } harness } /// The same loop with the 32X's bus (not the reference's benchmark: a second workload /// for the speed report). Area 0 16 bits wide (the cartridge), the 32X boot ROMs' BSC /// settings (sh2-python docs/timing.md, "Synchronous DRAM": BCR1 = H'0001, BCR2 = /// H'00A8, WCR = H'0055, MCR = H'0AB8, RTCOR = H'59, RTCSR = H'08: CS3 synchronous DRAM /// with refresh), the cache on or off, the buffer in SDRAM. pub fn setup_32x(cache: bool) -> Harness { let options = Options { area0_width: 16, ..Options::default() }; let mut harness = Harness::new(&program(), 0, Vec::new(), options); let chip = &mut harness.chip; chip.reset(true); for (address, value) in [(0xFFFFFFE0u32, 0x0001u32), (0xFFFFFFE4, 0x00A8), (0xFFFFFFE8, 0x0055), (0xFFFFFFEC, 0x0AB8)] { chip.write(address, 32, 0xA55A0000 | value); } chip.write(0xFFFFFFF8, 32, 0xA55A0059); chip.write(0xFFFFFFF0, 32, 0xA55A0008); if cache { chip.write(0xFFFFFE92, 8, 0x11); } harness } /// ``steps`` instructions of the loop (the BRA $ starts it again); returns the chip's /// clock afterwards, so the work cannot be optimised away. pub fn run(harness: &mut Harness, steps: u64) -> i64 { let chip = &mut harness.chip; for _ in 0..steps { if chip.pc == 0x100 + 2 * 13 { // the BRA $: start again chip.pc = 0x100; } chip.step(); } chip.clock }