// SPDX-License-Identifier: MIT //! Software interrupts, the undefined instruction, and the coprocessor instructions. //! //! Transcribed from `arm7tdmi_python/_exception.py`. //! //! SWI (DS 4.13, 5.17) is 2S+N: the prefetch, then the refill at vector 0x08 in Supervisor mode; //! R14_svc is the address of the next instruction (TRM table 2-3). //! //! The undefined instruction and every coprocessor instruction no coprocessor answers (DS 4.14, //! 4.15-4.18) take the undefined trap, 2S+N+I: the prefetch, an internal cycle at pc+2L while //! the core waits for a coprocessor that never answers, then the refill at vector 0x04 in //! Undefined mode; R14_und is the address of the next instruction (TRM table 6-21, C5, user //! decision 5). //! //! CP14 is answered on the ARM7TDMI: MRC and MCR to it do not trap (U17; alyosha-tas //! `arm/Coprocessor_14`, run on a Game Boy Player, C6). MRC is S+C+I and writes Rd with the //! opcode the prefetch read (bits 31:28 to N Z C V when Rd is R15); MCR is S+C and changes //! nothing; its next fetch is N (TRM tables 6-19, 6-20). CDP, LDC and STC to CP14 still trap //! (HYPOTHESIS; the corpus agrees, OBSERVED corpus v1). use crate::core::{Bus, ARM7TDMI}; use crate::modes::{SVC, UND}; pub const CP14: u32 = 14; impl ARM7TDMI { /// The undefined trap; `length` is the instruction's size in bytes (TRM table 6-21). fn undefined(&mut self, length: u32) { self.idle(self.r[15]); self.enter_exception(0x04, UND, self.r[15].wrapping_sub(length), false); } /// SWI -- enter Supervisor mode at vector 0x08 (DS 4.13, table 6-15). pub(crate) fn arm_swi(&mut self, _op: u32) { self.enter_exception(0x08, SVC, self.r[15].wrapping_sub(4), false); } /// UNDEFINED -- the undefined trap (DS 4.16, C5). pub(crate) fn arm_undefined(&mut self, _op: u32) { self.undefined(4); } /// CDP -- no coprocessor answers: the undefined trap (DS 4.14, C5). pub(crate) fn arm_cdp(&mut self, _op: u32) { self.undefined(4); } /// LDC -- no coprocessor answers: the undefined trap (DS 4.15, C5). pub(crate) fn arm_ldc(&mut self, _op: u32) { self.undefined(4); } /// STC -- no coprocessor answers: the undefined trap (DS 4.15, C5). pub(crate) fn arm_stc(&mut self, _op: u32) { self.undefined(4); } /// MRC -- CP14: Rd = the prefetched opcode; others: the undefined trap (U17, C6). pub(crate) fn arm_mrc(&mut self, op: u32) { if (op >> 8) & 0xF != CP14 { self.undefined(4); return; } self.r[15] = self.r[15].wrapping_add(4); self.coprocessor_cycle(self.r[15]); self.idle(self.r[15]); let value = self.pipeline[1]; let rd = ((op >> 12) & 0xF) as usize; if rd == 15 { self.cpsr = (self.cpsr & 0x0FFF_FFFF) | (value & 0xF000_0000); } else { self.set_reg(rd, value); } self.next_fetch_seq = true; // merged IS (C1) } /// MCR -- CP14: nothing changes; others: the undefined trap (U17, C6). pub(crate) fn arm_mcr(&mut self, op: u32) { if (op >> 8) & 0xF != CP14 { self.undefined(4); return; } self.r[15] = self.r[15].wrapping_add(4); self.coprocessor_cycle(self.r[15]); self.next_fetch_seq = false; } /// SWI -- enter Supervisor mode in ARM state at vector 0x08 (DS 5.17, table 6-15). pub(crate) fn thumb_swi(&mut self, _op: u32) { self.enter_exception(0x08, SVC, self.r[15].wrapping_sub(2), false); } /// UNDEFINED -- the undefined trap, also for 0xDExx and 0xE8xx (DS 5.0, C7, C8). pub(crate) fn thumb_undefined(&mut self, _op: u32) { self.undefined(2); } }