// SPDX-License-Identifier: MIT //! A snapshot of everything the ARM7TDMI holds, and the SingleStepTests layout. //! //! Transcribed from `arm7tdmi_python/state.py`. `registers` is what the current mode sees //! (R0-R15). `banks` holds every bank in full, including the current mode's (kept in step //! with `registers`), so a snapshot can be restored in any mode: `banks[0]` is R8-R14 of //! User/System, `banks[1]` R8-R14_fiq, and banks 2-5 (svc, abt, irq, und) use slots 5 and 6 //! for their R13 and R14. `spsr` is indexed the same way (slot 0 unused). `pipeline` is the //! two prefetched opcodes, oldest first; `next_fetch_seq` is whether the next opcode fetch is //! an S cycle (the SEQ pin, merged IS included). use crate::access::{CODE, SEQ}; use crate::modes::{bank_of, BANK_COUNT, BANK_FIQ, BANK_INVALID, BANK_USER}; #[derive(Clone, Debug, PartialEq, Eq)] pub struct CPUState { pub registers: [u32; 16], pub banks: [[u32; 7]; BANK_COUNT - 1], pub cpsr: u32, pub spsr: [u32; BANK_COUNT - 1], pub pipeline: [u32; 2], pub next_fetch_seq: bool, } impl Default for CPUState { fn default() -> Self { CPUState { registers: [0; 16], banks: [[0; 7]; BANK_COUNT - 1], cpsr: 0xD3, // Supervisor, ARM, I and F set: the reset values (TRM 2.10) spsr: [0; BANK_COUNT - 1], pipeline: [0, 0], next_fetch_seq: false, } } } impl CPUState { /// A state from the 40 words of a SingleStepTests ARM7TDMI state record. /// /// The corpus's layout is undocumented and was measured (docs/validation.md "The corpus's /// state layout" in the reference): `R` holds R0-R15 with R8-R14 always the User bank, the /// mode banks always hold their own registers, then CPSR, the five SPSRs (fiq, svc, abt, /// irq, und), the two pipeline opcodes and NBA's next-fetch access bits. pub fn from_sst(words: &[u32]) -> CPUState { let mut banks = [[0u32; 7]; BANK_COUNT - 1]; banks[BANK_USER].copy_from_slice(&words[8..15]); banks[BANK_FIQ].copy_from_slice(&words[16..23]); for (index, bank) in (2..6).enumerate() { banks[bank][5] = words[23 + 2 * index]; banks[bank][6] = words[24 + 2 * index]; } let cpsr = words[31]; let mut registers = [0u32; 16]; registers.copy_from_slice(&words[0..16]); let current = bank_of(cpsr); if current == BANK_FIQ { registers[8..15].copy_from_slice(&banks[BANK_FIQ]); } else if current != BANK_USER && current != BANK_INVALID { registers[13..15].copy_from_slice(&banks[current][5..7]); } let mut spsr = [0u32; BANK_COUNT - 1]; spsr[1..6].copy_from_slice(&words[32..37]); CPUState { registers, banks, cpsr, spsr, pipeline: [words[37], words[38]], next_fetch_seq: words[39] & SEQ != 0, } } /// The 40 words of a SingleStepTests state record (the inverse of [`CPUState::from_sst`]). /// /// The last word is NBA's access bits for the next fetch, `CODE` plus `SEQ` as the core /// reports it; the rung-1 comparison maps it through `gba_access` first. pub fn to_sst(&self) -> [u32; 40] { let mut words = [0u32; 40]; words[0..8].copy_from_slice(&self.registers[0..8]); words[8..15].copy_from_slice(&self.banks[BANK_USER]); words[15] = self.registers[15]; words[16..23].copy_from_slice(&self.banks[BANK_FIQ]); let mut at = 23; for bank in 2..6 { words[at..at + 2].copy_from_slice(&self.banks[bank][5..7]); at += 2; } words[31] = self.cpsr; words[32..37].copy_from_slice(&self.spsr[1..6]); words[37..39].copy_from_slice(&self.pipeline); words[39] = CODE | if self.next_fetch_seq { SEQ } else { 0 }; words } }