// SPDX-License-Identifier: MIT //! The cached interpreter's invalidation tests (src/blocks.rs, feature `block-cache`): //! self-modifying code, DMA into code, a cache purge after another master's write, and //! another master's write with and without the board's report. Each runs one program on //! a plain chip and on chips with the blocks on (verify and trust modes) and compares //! every bus callback (address, width, kind, clock, value), the registers and the clock. //! //! cargo test --release --features block-cache --test blocks #![cfg(feature = "block-cache")] use sh2::bus::{Bus, BusContext}; use sh2::{Options, Sh7604}; #[derive(Clone, Debug, PartialEq, Eq)] struct Cycle { write: bool, address: u32, width: u32, kind: u32, clock: i64, value: u32, } struct Board { ram: Vec, log: Vec, } impl Board { fn at(address: u32) -> usize { (address & 0xFFFF) as usize } fn get(&self, address: u32, width: u32) -> u32 { let a = Self::at(address); (0..width / 8).fold(0, |v, i| (v << 8) | self.ram[a + i as usize] as u32) } /// A write the chip does not make (the other SH-2, the 68000, a VDP). fn poke16(&mut self, address: u32, value: u32) { let a = Self::at(address); self.ram[a] = (value >> 8) as u8; self.ram[a + 1] = value as u8; } } impl Bus for Board { fn read(&mut self, address: u32, width: u32, kind: u32, cx: &mut BusContext) -> u32 { let value = self.get(address, width); self.log.push(Cycle { write: false, address, width, kind, clock: cx.clock, value }); value } fn write(&mut self, address: u32, width: u32, value: u32, kind: u32, cx: &mut BusContext) { let a = Self::at(address); for i in 0..width / 8 { self.ram[a + i as usize] = (value >> (width - 8 * (i + 1))) as u8; } self.log.push(Cycle { write: true, address, width, kind, clock: cx.clock, value }); } } const PATCH: u32 = 0x10A; const NEW_OP: u32 = 0x7005; // ADD #5,R0 in place of ADD #1,R0 /// The loop: R0 counts; the instruction at H'10A is the one that gets rewritten. /// ``store`` puts ``MOV.W R2,@R1`` (R1 = H'10A, R2 = ``NEW_OP``) in the loop: the /// program rewrites its own code; otherwise a NOP stands there. fn image(store: bool, count: u32) -> Vec { let mut ram = vec![0u8; 0x10000]; ram[0..4].copy_from_slice(&0x100u32.to_be_bytes()); ram[4..8].copy_from_slice(&0x0000_8000u32.to_be_bytes()); let words: [u32; 11] = [ 0xE000, // 100 MOV #0,R0 0xD105, // 102 MOV.L @(5,PC),R1 (H'118) 0x920A, // 104 MOV.W @(10,PC),R2 (H'11C) 0xE300 | count, // 106 MOV #count,R3 0x7001, // 108 loop: ADD #1,R0 0x7001, // 10A ADD #1,R0 (rewritten) if store { 0x2121 } else { 0x0009 }, // 10C MOV.W R2,@R1 / NOP 0x4310, // 10E DT R3 0x8BFA, // 110 BF loop 0xAFFE, // 112 BRA $ 0x0009, // 114 NOP ]; for (i, w) in words.iter().enumerate() { ram[0x100 + 2 * i..0x102 + 2 * i].copy_from_slice(&(*w as u16).to_be_bytes()); } ram[0x118..0x11C].copy_from_slice(&PATCH.to_be_bytes()); ram[0x11C..0x11E].copy_from_slice(&(NEW_OP as u16).to_be_bytes()); ram[0x200..0x202].copy_from_slice(&(NEW_OP as u16).to_be_bytes()); // a DMA source ram } #[derive(Clone, Copy, PartialEq, Eq, Debug)] enum Mode { Plain, Verify, Trust, } fn chip(ram: Vec, mode: Mode, cache: bool) -> Sh7604 { let mut chip = Sh7604::new(Board { ram, log: Vec::new() }, Options::default()); chip.blocks.enabled = mode != Mode::Plain; chip.blocks.verify = mode != Mode::Trust; chip.reset(true); chip.write(0xFFFFFFE8, 32, 0xA55A0000); // WCR = 0 if cache { chip.write(0xFFFFFE92, 8, 0x01); } chip } /// The outcome every mode must agree on: the bus log, the registers, the clock. fn outcome(chip: &Sh7604) -> (Vec, [u32; 16], u32, i64) { (chip.bus.log.clone(), chip.r, chip.pc, chip.clock) } /// Run ``steps`` steps, calling ``host`` before step ``at``. fn run(mut chip: Sh7604, steps: usize, at: usize, host: &dyn Fn(&mut Sh7604)) -> Sh7604 { for n in 0..steps { if n == at { host(&mut chip); } chip.step(); } chip } fn all_modes( store: bool, count: u32, cache: bool, steps: usize, at: usize, host: &dyn Fn(&mut Sh7604), ) -> Vec> { [Mode::Plain, Mode::Verify, Mode::Trust].iter().map(|&m| run(chip(image(store, count), m, cache), steps, at, host)).collect() } #[test] fn self_modifying_code() { for cache in [false, true] { let chips = all_modes(true, 6, cache, 60, usize::MAX, &|_| {}); let want = outcome(&chips[0]); // The rewrite changed what ran: 1 + 1 on the first of six passes, 1 + 5 on the rest. assert_eq!(want.1[0], 2 + 5 * 6, "cache {cache}"); for c in &chips[1..] { assert_eq!(outcome(c), want, "cache {cache}"); assert!(c.blocks.stats.replayed > 0 && c.blocks.stats.invalidated > 0, "{:?}", c.blocks.stats); assert_eq!(c.blocks.stats.mismatches, 0, "the write paths dropped the block first"); } } } #[test] fn dma_into_code() { // After two passes the host starts DMA channel 0: one word, auto request, from H'200 // to the instruction at H'10A (SAR0, DAR0, TCR0, CHCR0 = DE|AR|TS=word, DMAOR.DME). let dma = |chip: &mut Sh7604| { chip.write(0xFFFFFF80, 32, 0x200); chip.write(0xFFFFFF84, 32, PATCH); chip.write(0xFFFFFF88, 32, 1); chip.write(0xFFFFFF8C, 32, 0x0601); chip.write(0xFFFFFFB0, 32, 0x0001); }; for cache in [false, true] { let chips = all_modes(false, 8, cache, 70, 14, &dma); let want = outcome(&chips[0]); assert!(want.0.iter().any(|c| c.write && c.address == PATCH && c.kind & sh2::DMA != 0), "the DMA wrote the code"); if cache { // DMA does not snoop (HM 8.5.2): the cached line keeps running the old code. assert_eq!(want.1[0], 2 * 8); } else { assert!(want.1[0] > 2 * 8); } for c in &chips[1..] { assert_eq!(outcome(c), want, "cache {cache}"); assert_eq!(c.blocks.stats.mismatches, 0); } } } #[test] fn purge_after_another_masters_write() { // The cache on: another master rewrites H'10A in memory (no report); the cached line // keeps the old code until the program's associative purge of the line, then the // refill brings the new code. Trust mode needs no report here: the fill drops blocks. let other = |chip: &mut Sh7604| { chip.bus.poke16(PATCH, NEW_OP); chip.write(0x4000_0000 | 0x108, 32, 0); // associative purge (HM 8.4.7) }; let chips = all_modes(false, 8, true, 70, 20, &other); let want = outcome(&chips[0]); assert!(want.1[0] > 2 * 8, "the refill brought the new code"); for c in &chips[1..] { assert_eq!(outcome(c), want); } } #[test] fn another_masters_write_needs_the_report() { // The cache off: another master rewrites H'10A (the 32X: the other SH-2 or the 68000 // writing SDRAM). The plain chip fetches the new code. Verify mode sees the fetched // opcode differ and drops the block; trust mode runs the stale record unless the board // reports the write (``invalidate_code``). let silent = |chip: &mut Sh7604| chip.bus.poke16(PATCH, NEW_OP); let reported = |chip: &mut Sh7604| { chip.bus.poke16(PATCH, NEW_OP); chip.invalidate_code(PATCH, 2); }; let plain = run(chip(image(false, 8), Mode::Plain, false), 70, 20, &silent); let verify = run(chip(image(false, 8), Mode::Verify, false), 70, 20, &silent); let trust = run(chip(image(false, 8), Mode::Trust, false), 70, 20, &silent); let trust_reported = run(chip(image(false, 8), Mode::Trust, false), 70, 20, &reported); assert_eq!(outcome(&verify), outcome(&plain)); assert!(verify.blocks.stats.mismatches > 0); assert_ne!(trust.r[0], plain.r[0], "trust mode without the report runs stale code (the expected failure)"); assert_eq!(outcome(&trust_reported), outcome(&plain)); } #[test] fn data_array_code_written_through_its_other_name() { // The cache on: after two passes the loop's line is cached; the host moves the PC to // the same code under the data array's name (H'C0000000 + way and entry), lets it run, // then the CPU writes the instruction at H'10A through the cache area: a write-through // hit, which changes the data-array bytes the fetches read (rung U found this name). let to_data_array = |chip: &mut Sh7604| { let way = chip.cache.lookup(0x108); assert!(way >= 0, "the loop's line is cached"); chip.r[3] = 8; chip.pc = 0xC000_0000 | ((way as u32) << 10) | 0x108; }; let write = |chip: &mut Sh7604| chip.write(PATCH, 16, NEW_OP); let mut chips = Vec::new(); for mode in [Mode::Plain, Mode::Verify, Mode::Trust] { let chip = run(chip(image(false, 8), mode, true), 20, 18, &to_data_array); chips.push(run(chip, 60, 20, &write)); } let want = outcome(&chips[0]); assert!(chips[0].pc >> 29 == 6 || chips[0].r[3] == 0); for c in &chips[1..] { assert_eq!(outcome(c), want); assert_eq!(c.blocks.stats.mismatches, 0); } }