// SPDX-License-Identifier: MIT //! Rung 0: every instruction class's bus schedule equals the reference's docs/timing.md (TRM //! chapter 6): a transcription of the reference's `tests/test_bus_schedule.py`, case for case. //! //! Each case is one row group of a timing.md table, written as (type, address, width) per //! cycle with the table's symbols: `pc` the instruction's address, `alu` the computed address //! or target, `pc'` a value loaded into R15, `Xn` a vector. Type is N, S, I or C as converted //! in timing.md (each row carries its own type), and `next` is the type of the following //! instruction's cycle 1, the core's `next_fetch_seq`. Cycle 1 ("prev") is S here because each //! case starts with `next_fetch_seq` set. This is manufacturer documentation, the TRM's; where //! the core follows the TRM against NBA and the corpus (merged IS, C1; the undefined trap's I //! cycle, C5) these tables are the judge. use std::collections::HashMap; use arm7tdmi::{Bus, CPUState, ARM7TDMI, CODE, LOCK, SEQ}; const PC_ARM: u32 = 0x0800_0100; const PC_THUMB: u32 = 0x0800_0200; const SYS: u32 = 0x1F; const THUMB: u32 = 0x20; const A: u32 = PC_ARM; const T: u32 = PC_THUMB; const ALU: u32 = 0x0300_0040; // a data address const TARGET: u32 = 0x0800_0400; // a branch target / loaded PC type Cycle = (&'static str, u32, Option); /// A logged cycle with its access bits. type Logged = (&'static str, u32, Option, u32); #[derive(Default)] struct Log { memory: HashMap, log: Vec, } impl Bus for Log { fn read(&mut self, address: u32, width: u32, access: u32) -> u32 { let kind = if access & SEQ != 0 { "S" } else { "N" }; self.log.push((kind, address, Some(width), access)); *self.memory.get(&address).unwrap_or(&0) } fn write(&mut self, address: u32, width: u32, _value: u32, access: u32) { let kind = if access & SEQ != 0 { "S" } else { "N" }; self.log.push((kind, address, Some(width), access)); } fn idle(&mut self, address: u32, coprocessor_cycle: bool) { self.log .push((if coprocessor_cycle { "C" } else { "I" }, address, None, 0)); } } /// Run one instruction (or an IRQ entry); return its cycles and the next fetch's type. fn run( op: u32, regs: &[(usize, u32)], thumb: bool, cpsr: u32, memory: &[(u32, u32)], irq: bool, ) -> (Vec, &'static str) { let mut cpu = ARM7TDMI::new(Log { memory: memory.iter().copied().collect(), log: Vec::new(), }); let pc = if thumb { PC_THUMB } else { PC_ARM }; let mut registers = [0u32; 16]; for &(n, value) in regs { registers[n] = value; } registers[15] = pc + if thumb { 4 } else { 8 }; cpu.restore_state(&CPUState { registers, cpsr: cpsr | if thumb { THUMB } else { 0 }, pipeline: [op, 0], next_fetch_seq: true, ..CPUState::default() }); cpu.irq = irq; let cycles = cpu.step(); assert_eq!(cycles as usize, cpu.bus.log.len()); let next = if cpu.next_fetch_seq { "S" } else { "N" }; (std::mem::take(&mut cpu.bus.log), next) } fn cycles(log: &[Logged]) -> Vec { log.iter().map(|&(k, a, w, _)| (k, a, w)).collect() } fn s(address: u32, width: u32) -> Cycle { ("S", address, Some(width)) } fn n(address: u32, width: u32) -> Cycle { ("N", address, Some(width)) } fn i(address: u32) -> Cycle { ("I", address, None) } fn c(address: u32) -> Cycle { ("C", address, None) } #[allow(clippy::type_complexity)] fn arm_cases() -> Vec<( &'static str, u32, Vec<(usize, u32)>, Vec, &'static str, )> { vec![ // Table 6-1: B and BL: prefetch, N at alu, S at alu+4; next S. ( "B", 0xEA00003E, vec![], vec![s(A + 8, 32), n(A + 8 + 0xF8, 32), s(A + 12 + 0xF8, 32)], "S", ), ( "BL", 0xEB00003E, vec![], vec![s(A + 8, 32), n(A + 8 + 0xF8, 32), s(A + 12 + 0xF8, 32)], "S", ), // Table 6-3: BX to Thumb: new width from cycle 2. ( "BX to Thumb", 0xE12FFF10, vec![(0, TARGET | 1)], vec![s(A + 8, 32), n(TARGET, 16), s(TARGET + 2, 16)], "S", ), ( "BX to ARM", 0xE12FFF10, vec![(0, TARGET)], vec![s(A + 8, 32), n(TARGET, 32), s(TARGET + 4, 32)], "S", ), // Table 6-4: data processing. ("ADD normal", 0xE0810002, vec![], vec![s(A + 8, 32)], "S"), ( "MOV pc", 0xE1A0F001, vec![(1, TARGET)], vec![s(A + 8, 32), n(TARGET, 32), s(TARGET + 4, 32)], "S", ), ( "ADD shift by Rs", 0xE0810312, vec![], vec![s(A + 8, 32), i(A + 12)], "S", ), ( "MOV pc shift by Rs", 0xE1A0F311, vec![(1, TARGET)], vec![s(A + 8, 32), i(A + 12), n(TARGET, 32), s(TARGET + 4, 32)], "S", ), ("MRS", 0xE10F0000, vec![], vec![s(A + 8, 32)], "S"), ("MSR", 0xE129F000, vec![(0, SYS)], vec![s(A + 8, 32)], "S"), // Table 6-9: loads. S+N+I; next S at pc+12 (merged IS). ( "LDR", 0xE5910000, vec![(1, ALU)], vec![s(A + 8, 32), n(ALU, 32), i(A + 12)], "S", ), ( "LDRB", 0xE5D10000, vec![(1, ALU)], vec![s(A + 8, 32), n(ALU, 8), i(A + 12)], "S", ), ( "LDRH", 0xE1D100B0, vec![(1, ALU)], vec![s(A + 8, 32), n(ALU, 16), i(A + 12)], "S", ), ( "LDRSB", 0xE1D100D0, vec![(1, ALU)], vec![s(A + 8, 32), n(ALU, 8), i(A + 12)], "S", ), ( "LDR pc", 0xE591F000, vec![(1, ALU)], vec![ s(A + 8, 32), n(ALU, 32), i(A + 12), n(TARGET, 32), s(TARGET + 4, 32), ], "S", ), // Table 6-11: stores. S+N; next N. ( "STR", 0xE5810000, vec![(1, ALU)], vec![s(A + 8, 32), n(ALU, 32)], "N", ), ( "STRB", 0xE5C10000, vec![(1, ALU)], vec![s(A + 8, 32), n(ALU, 8)], "N", ), ( "STRH", 0xE1C100B0, vec![(1, ALU)], vec![s(A + 8, 32), n(ALU, 16)], "N", ), // Table 6-12: LDM. N then S per register, I at pc+12; with PC, then N, S. ( "LDM 1", 0xE8910001, vec![(1, ALU)], vec![s(A + 8, 32), n(ALU, 32), i(A + 12)], "S", ), ( "LDM 3", 0xE8910007, vec![(1, ALU)], vec![ s(A + 8, 32), n(ALU, 32), s(ALU + 4, 32), s(ALU + 8, 32), i(A + 12), ], "S", ), ( "LDM with pc", 0xE8918001, vec![(1, ALU)], vec![ s(A + 8, 32), n(ALU, 32), s(ALU + 4, 32), i(A + 12), n(TARGET, 32), s(TARGET + 4, 32), ], "S", ), ( "LDMDB", 0xE9110003, vec![(1, ALU)], vec![s(A + 8, 32), n(ALU - 8, 32), s(ALU - 4, 32), i(A + 12)], "S", ), // Table 6-13: STM. (n-1)S+2N; next N. ( "STM 3", 0xE8810007, vec![(1, ALU)], vec![s(A + 8, 32), n(ALU, 32), s(ALU + 4, 32), s(ALU + 8, 32)], "N", ), // Table 6-14: SWP. S+2N+I, locked read and write. ( "SWP", 0xE1010092, vec![(1, ALU)], vec![s(A + 8, 32), n(ALU, 32), n(ALU, 32), i(A + 12)], "S", ), ( "SWPB", 0xE1410092, vec![(1, ALU)], vec![s(A + 8, 32), n(ALU, 8), n(ALU, 8), i(A + 12)], "S", ), // Table 6-15: SWI. 2S+N at the vector. ( "SWI", 0xEF000000, vec![], vec![s(A + 8, 32), n(0x08, 32), s(0x0C, 32)], "S", ), // Table 6-21: undefined and absent coprocessor: 2S+N+I, I at pc+8 (C5). ( "undefined", 0xE7F000F0, vec![], vec![s(A + 8, 32), i(A + 8), n(0x04, 32), s(0x08, 32)], "S", ), ( "CDP", 0xEE000000, vec![], vec![s(A + 8, 32), i(A + 8), n(0x04, 32), s(0x08, 32)], "S", ), ( "LDC", 0xED900000, vec![], vec![s(A + 8, 32), i(A + 8), n(0x04, 32), s(0x08, 32)], "S", ), ( "MRC p15", 0xEE100F10, vec![], vec![s(A + 8, 32), i(A + 8), n(0x04, 32), s(0x08, 32)], "S", ), // Tables 6-19, 6-20: CP14 register transfers (U17). ( "MRC p14", 0xEE100E10, vec![], vec![s(A + 8, 32), c(A + 12), i(A + 12)], "S", ), ( "MCR p14", 0xEE010E10, vec![], vec![s(A + 8, 32), c(A + 12)], "N", ), // Table 6-22: an instruction whose condition fails. ("unexecuted", 0x00810002, vec![], vec![s(A + 8, 32)], "S"), ] } #[test] fn test_arm_schedule() { for (name, op, regs, expected, after) in arm_cases() { let (log, next_type) = run( op, ®s, false, SYS, &[(ALU, TARGET), (ALU + 4, TARGET)], false, ); assert_eq!(cycles(&log), expected, "{name}"); assert_eq!(next_type, after, "{name}"); } } /// Tables 6-5 to 6-8: MUL m I cycles at pc+12; MLA one more, the first at pc+8; UMULL and /// SMULL m+1, the accumulating forms m+2; m by the significant bytes of Rs (TRM 6.20). #[test] fn test_multiply_schedules() { for (rs, m) in [ (0x7F, 1), (0xFFFFFF80, 1), (0x1234, 2), (0xFF001234, 3), (0x12345678, 4), ] { let regs = [(1, 3), (2, rs)]; let prefetch = vec![s(A + 8, 32)]; let (log, after) = run(0xE0000291, ®s, false, SYS, &[], false); // MUL r0, r1, r2 assert_eq!( cycles(&log), [prefetch.clone(), vec![i(A + 12); m]].concat() ); assert_eq!(after, "S"); let (log, _) = run(0xE0203291, ®s, false, SYS, &[], false); // MLA r0, r1, r2, r3 assert_eq!( cycles(&log), [prefetch.clone(), vec![i(A + 8)], vec![i(A + 12); m]].concat() ); let (log, _) = run(0xE0C54291, ®s, false, SYS, &[], false); // SMULL r4, r5, r1, r2 assert_eq!( cycles(&log), [prefetch.clone(), vec![i(A + 12); m + 1]].concat() ); let (log, _) = run(0xE0E54291, ®s, false, SYS, &[], false); // SMLAL r4, r5, r1, r2 assert_eq!( cycles(&log), [prefetch.clone(), vec![i(A + 12); m + 2]].concat() ); } } /// UMULL stops early only when the upper bits are zero (DS 4.8.3, U11). #[test] fn test_umull_terminates_only_on_zeros() { let (log, _) = run( 0xE0854291, &[(1, 3), (2, 0xFFFFFF80)], false, SYS, &[], false, ); // UMULL r4, r5, r1, r2 assert_eq!(log.len(), 1 + 5); } #[allow(clippy::type_complexity)] fn thumb_cases() -> Vec<( &'static str, u32, Vec<(usize, u32)>, Vec, &'static str, )> { vec![ ("LSL imm", 0x0048, vec![], vec![s(T + 4, 16)], "S"), ( "LSL by register", 0x4088, vec![], vec![s(T + 4, 16), i(T + 6)], "S", ), ( "LDR imm", 0x6808, vec![(1, ALU)], vec![s(T + 4, 16), n(ALU, 32), i(T + 6)], "S", ), ( "STR imm", 0x6008, vec![(1, ALU)], vec![s(T + 4, 16), n(ALU, 32)], "N", ), ( "LDRH imm", 0x8808, vec![(1, ALU)], vec![s(T + 4, 16), n(ALU, 16), i(T + 6)], "S", ), ( "B", 0xE010, vec![], vec![s(T + 4, 16), n(T + 4 + 0x20, 16), s(T + 6 + 0x20, 16)], "S", ), ("BEQ not taken", 0xD010, vec![], vec![s(T + 4, 16)], "S"), ("BL prefix", 0xF000, vec![], vec![s(T + 4, 16)], "S"), ( "BX to ARM", 0x4708, vec![(1, TARGET)], vec![s(T + 4, 16), n(TARGET, 32), s(TARGET + 4, 32)], "S", ), ( "MOV pc", 0x468F, vec![(1, TARGET)], vec![s(T + 4, 16), n(TARGET, 16), s(TARGET + 2, 16)], "S", ), ( "PUSH 2", 0xB403, vec![(13, ALU)], vec![s(T + 4, 16), n(ALU - 8, 32), s(ALU - 4, 32)], "N", ), ( "POP 2", 0xBC03, vec![(13, ALU)], vec![s(T + 4, 16), n(ALU, 32), s(ALU + 4, 32), i(T + 6)], "S", ), ( "POP pc", 0xBD00, vec![(13, ALU)], vec![ s(T + 4, 16), n(ALU, 32), i(T + 6), n(TARGET, 16), s(TARGET + 2, 16), ], "S", ), ( "SWI", 0xDF00, vec![], vec![s(T + 4, 16), n(0x08, 32), s(0x0C, 32)], "S", ), ( "undefined DExx", 0xDE00, vec![], vec![s(T + 4, 16), i(T + 4), n(0x04, 32), s(0x08, 32)], "S", ), ] } #[test] fn test_thumb_schedule() { for (name, op, regs, expected, after) in thumb_cases() { let (log, next_type) = run(op, ®s, true, SYS, &[(ALU, TARGET)], false); assert_eq!(cycles(&log), expected, "{name}"); assert_eq!(next_type, after, "{name}"); } } /// Table 6-2: the second half is 2S+N: its prefetch at pc+6 (pc of the first half), then /// the refill at LR + offset. #[test] fn test_thumb_bl_suffix() { let (log, _) = run(0xF810, &[(14, TARGET)], true, SYS, &[], false); assert_eq!( cycles(&log), vec![s(T + 4, 16), n(TARGET + 0x20, 16), s(TARGET + 0x22, 16)] ); } /// Table 6-15 for IRQ: cycle 1 fetches (discarded), then N at 0x18, S at 0x1C. #[test] fn test_irq_entry() { for thumb in [false, true] { let (log, after) = run(0, &[], thumb, SYS, &[], true); let first = if thumb { s(T + 4, 16) } else { s(A + 8, 32) }; assert_eq!(cycles(&log), vec![first, n(0x18, 32), s(0x1C, 32)]); assert_eq!(after, "S"); } } /// Answers every read with MOV R0, R0 and records the addresses fetched. #[derive(Default)] struct Nops { fetched: Vec, } impl Bus for Nops { fn read(&mut self, address: u32, _width: u32, _access: u32) -> u32 { self.fetched.push(address); 0xE1A00000 // MOV R0, R0 } fn write(&mut self, _: u32, _: u32, _: u32, _: u32) {} } /// After IRQ (FIQ) entry the line is still high, but the entry set I (and F): the next /// step runs the instruction at the vector, not a second entry (U14, TRM 2.8.4-2.8.5). #[test] fn test_entry_masks_the_still_high_line() { for line in ["irq", "fiq"] { let vector = if line == "irq" { 0x18 } else { 0x1C }; let mut cpu = ARM7TDMI::new(Nops::default()); let mut registers = [0u32; 16]; registers[15] = PC_ARM + 8; cpu.restore_state(&CPUState { registers, cpsr: SYS, pipeline: [0xE1A00000; 2], next_fetch_seq: true, ..CPUState::default() }); if line == "irq" { cpu.irq = true; } else { cpu.fiq = true; } cpu.step(); assert_eq!(cpu.pending_interrupt(), None); cpu.bus.fetched.clear(); cpu.step(); // the vector's first instruction: one fetch, two words past it assert_eq!(cpu.bus.fetched, vec![vector + 8]); assert_eq!(cpu.cpsr & 0x1F, if line == "irq" { 0x12 } else { 0x11 }); } } /// U14: a line is masked only if I was set when the instruction started and is still set /// when it ends (TEST ROM: alyosha-tas psr for setting; irq/IRQ_sub and IRQ_sub_slow with /// Nintendo's BIOS for clearing). /// /// The reference raises the line inside the MSR's first bus cycle; a Rust bus cannot reach /// the CPU calling it, so the line is raised after that step instead. The core samples the /// line only when a step begins, so the two are the same case. #[test] fn test_irq_mask_change() { for (cpsr, msr, taken) in [ (SYS | 0x80, 0xE321F01F, true), // clears I: the IRQ is taken right after it (SYS, 0xE321F09F, true), // sets I: still taken right after it (one instruction late) (SYS | 0x80, 0xE321F09F, false), // I set before and after: masked ] { let mut cpu = ARM7TDMI::new(Nops::default()); let mut registers = [0u32; 16]; registers[15] = PC_ARM + 8; cpu.restore_state(&CPUState { registers, cpsr, pipeline: [msr, 0xE1A00000], next_fetch_seq: true, ..CPUState::default() }); cpu.step(); // the MSR cpu.irq = true; // the line rose during the MSR's first cycle assert_eq!(cpu.pending_interrupt() == Some(0x12), taken); cpu.step(); assert_eq!(cpu.cpsr & 0x1F == 0x12, taken); } } /// Fetches carry CODE; SWP's read and write carry LOCK (TRM table 6-14, C10). #[test] fn test_access_bits() { let (log, _) = run(0xE1010092, &[(1, ALU)], false, SYS, &[], false); let access: Vec = log.iter().map(|&(_, _, _, a)| a).collect(); assert_eq!(access, vec![CODE | SEQ, LOCK, LOCK, 0]); }