// SPDX-License-Identifier: MIT //! Data processing: the barrel shifter, the sixteen ARM ALU operations and their Thumb forms. //! //! Transcribed from `arm7tdmi_python/_alu.py`. //! //! ARM (DS 4.5): `Rd = Rn Op2`, where Op2 is a rotated 8-bit immediate or a register //! shifted by an immediate or by the bottom byte of another register. With S set, logical //! operations set N and Z and take C from the shifter; arithmetic operations set N, Z, C, V. //! Thumb formats 1-5, 12 and 13 (DS 5.1-5.5, 5.12-5.13) are the same operations on fewer //! registers; Thumb always sets the flags (except ADD/MOV with a hi register and the SP/PC adds). //! //! A register-specified shift costs one internal cycle at pc+3L, during which the PC advances, so //! R15 as an operand then reads plus 12 (timing.md table 6-4, U9). use crate::core::{Bus, ARM7TDMI, MASK32}; use crate::modes::{FLAG_C, FLAG_N, FLAG_V, FLAG_Z}; pub const S_BIT: u32 = 1 << 20; #[inline] pub fn ror32(value: u32, amount: u32) -> u32 { value.rotate_right(amount & 31) } /// Shift by a 5-bit immediate: (result, carry out) (DS 4.5.2, U10). /// /// LSL #0 passes the value and C unchanged; LSR #0 and ASR #0 encode a shift by 32; ROR #0 /// encodes RRX, a 33-bit rotate through C. #[inline] pub fn shift_by_immediate(kind: u32, value: u32, amount: u32, carry: u32) -> (u32, u32) { if kind == 3 && amount == 0 { // RRX return ((value >> 1) | (carry << 31), value & 1); } let mut amount = amount; if amount == 0 && (kind == 1 || kind == 2) { amount = 32; } shift_by_register(kind, value, amount, carry) } /// Shift by the bottom byte of a register: (result, carry out) (DS 4.5.2, U10). /// /// 0 passes the value and C unchanged. LSL/LSR by 32 give 0 with C = bit 0 / bit 31; by more /// than 32, 0 with C = 0. ASR by 32 or more fills with bit 31, and C = bit 31. ROR by a /// multiple of 32 leaves the value and sets C = bit 31. #[inline] pub fn shift_by_register(kind: u32, value: u32, amount: u32, carry: u32) -> (u32, u32) { if amount == 0 { return (value, carry); } if kind == 0 { // LSL if amount > 32 { return (0, 0); } return ( (((value as u64) << amount) & MASK32 as u64) as u32, (value >> (32 - amount)) & 1, ); } if kind == 1 { // LSR if amount > 32 { return (0, 0); } return ( ((value as u64) >> amount) as u32, (value >> (amount - 1)) & 1, ); } if kind == 2 { // ASR let signed = value as i32; if amount >= 32 { return ( if signed < 0 { MASK32 } else { 0 }, if signed < 0 { 1 } else { 0 }, ); } return ( (signed >> amount) as u32, ((signed >> (amount - 1)) & 1) as u32, ); } let result = ror32(value, amount); // ROR (result, result >> 31) } impl ARM7TDMI { // ------------------------------------------------------------------ helpers #[inline] pub(crate) fn carry(&self) -> u32 { if self.cpsr & FLAG_C != 0 { 1 } else { 0 } } /// a + b + carry_in; with set_flags, N Z C V of the sum (DS 4.5.4). #[inline] pub(crate) fn add(&mut self, a: u32, b: u32, carry_in: u32, set_flags: bool) -> u32 { let total = a as u64 + b as u64 + carry_in as u64; let result = total as u32; if set_flags { self.cpsr = (self.cpsr & !(FLAG_N | FLAG_Z | FLAG_C | FLAG_V)) | (result & FLAG_N) | if result == 0 { FLAG_Z } else { 0 } | if total > MASK32 as u64 { FLAG_C } else { 0 } | if !(a ^ b) & (b ^ result) & FLAG_N != 0 { FLAG_V } else { 0 }; } result } /// a - b - (1 - carry_in); C is NOT borrow (DS 4.5.4). #[inline] pub(crate) fn subtract(&mut self, a: u32, b: u32, carry_in: u32, set_flags: bool) -> u32 { let borrow = carry_in ^ 1; let result = a.wrapping_sub(b).wrapping_sub(borrow); if set_flags { self.cpsr = (self.cpsr & !(FLAG_N | FLAG_Z | FLAG_C | FLAG_V)) | (result & FLAG_N) | if result == 0 { FLAG_Z } else { 0 } | if a as u64 >= b as u64 + borrow as u64 { FLAG_C } else { 0 } | if (a ^ b) & (a ^ result) & FLAG_N != 0 { FLAG_V } else { 0 }; } result } /// N and Z from the result, C from the shifter; V unchanged (DS 4.5.4). #[inline] pub(crate) fn logical_flags(&mut self, result: u32, carry: u32) { self.set_nz(result); self.cpsr = (self.cpsr & !FLAG_C) | if carry != 0 { FLAG_C } else { 0 }; } /// The ARM Op2 and the shifter's carry out (DS 4.5.3, figure 4-6). #[inline] pub(crate) fn operand2(&mut self, op: u32) -> (u32, u32) { let carry = self.carry(); if op & (1 << 25) != 0 { // A rotated immediate: C becomes bit 31 when the rotation is non-zero. let value = ror32(op & 0xFF, ((op >> 8) & 0xF) * 2); return ( value, if (op >> 8) & 0xF != 0 { value >> 31 } else { carry }, ); } let kind = (op >> 5) & 3; if op & 0x10 != 0 { let amount = self.reg(((op >> 8) & 0xF) as usize) & 0xFF; // The shift by register takes an I cycle at pc+3L; the PC has advanced (U9). self.r[15] = self.r[15].wrapping_add(4); self.idle(self.r[15]); return shift_by_register(kind, self.reg((op & 0xF) as usize), amount, carry); } shift_by_immediate(kind, self.reg((op & 0xF) as usize), (op >> 7) & 0x1F, carry) } /// Write Rd and settle R15 (DS 4.5.4-4.5.5, timing.md table 6-4). /// /// `result` None is a test operation, which writes no register. With Rd = R15 and S set /// the CPSR is restored from the SPSR, for the test operations too (U13); a write to R15 /// refills the pipeline. Otherwise the PC advances, unless the register shift already /// advanced it. #[inline] pub(crate) fn finish(&mut self, op: u32, result: Option) { let rd = ((op >> 12) & 0xF) as usize; let register_shift = op & (1 << 25) == 0 && op & 0x10 != 0; if let Some(value) = result { self.set_reg(rd, value); } self.next_fetch_seq = true; // S, or merged IS after a register shift (C1) if rd == 15 { if op & S_BIT != 0 { self.restore_cpsr_from_spsr(); } if result.is_some() { self.refill(); return; } } if !register_shift { self.r[15] = self.r[15].wrapping_add(4); } } #[inline] fn logical(&mut self, op: u32, result: u32, carry: u32) { if op & S_BIT != 0 { self.logical_flags(result, carry); } self.finish(op, Some(result)); } // ------------------------------------------------------------------ ARM /// AND -- Rd = Rn AND Op2 (DS 4.5). pub(crate) fn arm_and(&mut self, op: u32) { let (op2, carry) = self.operand2(op); let result = self.reg(((op >> 16) & 0xF) as usize) & op2; self.logical(op, result, carry); } /// EOR -- Rd = Rn EOR Op2 (DS 4.5). pub(crate) fn arm_eor(&mut self, op: u32) { let (op2, carry) = self.operand2(op); let result = self.reg(((op >> 16) & 0xF) as usize) ^ op2; self.logical(op, result, carry); } /// ORR -- Rd = Rn OR Op2 (DS 4.5). pub(crate) fn arm_orr(&mut self, op: u32) { let (op2, carry) = self.operand2(op); let result = self.reg(((op >> 16) & 0xF) as usize) | op2; self.logical(op, result, carry); } /// BIC -- Rd = Rn AND NOT Op2 (DS 4.5). pub(crate) fn arm_bic(&mut self, op: u32) { let (op2, carry) = self.operand2(op); let result = self.reg(((op >> 16) & 0xF) as usize) & !op2; self.logical(op, result, carry); } /// MOV -- Rd = Op2; Rn is ignored (DS 4.5). pub(crate) fn arm_mov(&mut self, op: u32) { let (op2, carry) = self.operand2(op); self.logical(op, op2, carry); } /// MVN -- Rd = NOT Op2; Rn is ignored (DS 4.5). pub(crate) fn arm_mvn(&mut self, op: u32) { let (op2, carry) = self.operand2(op); self.logical(op, !op2, carry); } /// TST -- flags of Rn AND Op2; with Rd = R15 also CPSR <- SPSR (DS 4.5, U13). pub(crate) fn arm_tst(&mut self, op: u32) { let (op2, carry) = self.operand2(op); let value = self.reg(((op >> 16) & 0xF) as usize) & op2; self.logical_flags(value, carry); self.finish(op, None); } /// TEQ -- flags of Rn EOR Op2; with Rd = R15 also CPSR <- SPSR (DS 4.5, U13). pub(crate) fn arm_teq(&mut self, op: u32) { let (op2, carry) = self.operand2(op); let value = self.reg(((op >> 16) & 0xF) as usize) ^ op2; self.logical_flags(value, carry); self.finish(op, None); } /// ADD -- Rd = Rn + Op2 (DS 4.5). pub(crate) fn arm_add(&mut self, op: u32) { let (op2, _) = self.operand2(op); let rn = self.reg(((op >> 16) & 0xF) as usize); let result = self.add(rn, op2, 0, op & S_BIT != 0); self.finish(op, Some(result)); } /// ADC -- Rd = Rn + Op2 + C (DS 4.5). pub(crate) fn arm_adc(&mut self, op: u32) { let (op2, _) = self.operand2(op); let rn = self.reg(((op >> 16) & 0xF) as usize); let carry = self.carry(); let result = self.add(rn, op2, carry, op & S_BIT != 0); self.finish(op, Some(result)); } /// SUB -- Rd = Rn - Op2 (DS 4.5). pub(crate) fn arm_sub(&mut self, op: u32) { let (op2, _) = self.operand2(op); let rn = self.reg(((op >> 16) & 0xF) as usize); let result = self.subtract(rn, op2, 1, op & S_BIT != 0); self.finish(op, Some(result)); } /// SBC -- Rd = Rn - Op2 - NOT C (DS 4.5). pub(crate) fn arm_sbc(&mut self, op: u32) { let (op2, _) = self.operand2(op); let rn = self.reg(((op >> 16) & 0xF) as usize); let carry = self.carry(); let result = self.subtract(rn, op2, carry, op & S_BIT != 0); self.finish(op, Some(result)); } /// RSB -- Rd = Op2 - Rn (DS 4.5). pub(crate) fn arm_rsb(&mut self, op: u32) { let (op2, _) = self.operand2(op); let rn = self.reg(((op >> 16) & 0xF) as usize); let result = self.subtract(op2, rn, 1, op & S_BIT != 0); self.finish(op, Some(result)); } /// RSC -- Rd = Op2 - Rn - NOT C (DS 4.5). pub(crate) fn arm_rsc(&mut self, op: u32) { let (op2, _) = self.operand2(op); let rn = self.reg(((op >> 16) & 0xF) as usize); let carry = self.carry(); let result = self.subtract(op2, rn, carry, op & S_BIT != 0); self.finish(op, Some(result)); } /// CMP -- flags of Rn - Op2; with Rd = R15 also CPSR <- SPSR (DS 4.5, U13). pub(crate) fn arm_cmp(&mut self, op: u32) { let (op2, _) = self.operand2(op); let rn = self.reg(((op >> 16) & 0xF) as usize); self.subtract(rn, op2, 1, true); self.finish(op, None); } /// CMN -- flags of Rn + Op2; with Rd = R15 also CPSR <- SPSR (DS 4.5, U13). pub(crate) fn arm_cmn(&mut self, op: u32) { let (op2, _) = self.operand2(op); let rn = self.reg(((op >> 16) & 0xF) as usize); self.add(rn, op2, 0, true); self.finish(op, None); } // ------------------------------------------------------------------ Thumb /// An ordinary one-cycle Thumb instruction: the PC advances, the next fetch is S. #[inline] pub(crate) fn thumb_next(&mut self) { self.next_fetch_seq = true; self.r[15] = self.r[15].wrapping_add(2); } #[inline] fn thumb_shift_imm(&mut self, op: u32, kind: u32) { let (rd, rs) = ((op & 7) as usize, ((op >> 3) & 7) as usize); let (result, carry) = shift_by_immediate(kind, self.r[rs], (op >> 6) & 0x1F, self.carry()); self.logical_flags(result, carry); self.r[rd] = result; self.thumb_next(); } /// LSL Rd,Rs,#Offset5 -- format 1 (DS 5.1). pub(crate) fn thumb_lsl_imm(&mut self, op: u32) { self.thumb_shift_imm(op, 0); } /// LSR Rd,Rs,#Offset5 -- format 1; #0 means 32 (DS 5.1, U10). pub(crate) fn thumb_lsr_imm(&mut self, op: u32) { self.thumb_shift_imm(op, 1); } /// ASR Rd,Rs,#Offset5 -- format 1; #0 means 32 (DS 5.1, U10). pub(crate) fn thumb_asr_imm(&mut self, op: u32) { self.thumb_shift_imm(op, 2); } /// ADD Rd,Rs,Rn/ADD Rd,Rs,#Offset3 -- format 2 (DS 5.2). pub(crate) fn thumb_add3(&mut self, op: u32) { let field = (op >> 6) & 7; let operand = if op & (1 << 10) != 0 { field } else { self.r[field as usize] }; self.r[(op & 7) as usize] = self.add(self.r[((op >> 3) & 7) as usize], operand, 0, true); self.thumb_next(); } /// SUB Rd,Rs,Rn/SUB Rd,Rs,#Offset3 -- format 2 (DS 5.2). pub(crate) fn thumb_sub3(&mut self, op: u32) { let field = (op >> 6) & 7; let operand = if op & (1 << 10) != 0 { field } else { self.r[field as usize] }; self.r[(op & 7) as usize] = self.subtract(self.r[((op >> 3) & 7) as usize], operand, 1, true); self.thumb_next(); } /// MOV Rd,#Offset8 -- format 3; N and Z set, C and V unchanged (DS 5.3). pub(crate) fn thumb_mov_imm(&mut self, op: u32) { let value = op & 0xFF; self.r[((op >> 8) & 7) as usize] = value; self.set_nz(value); self.thumb_next(); } /// CMP Rd,#Offset8 -- format 3 (DS 5.3). pub(crate) fn thumb_cmp_imm(&mut self, op: u32) { self.subtract(self.r[((op >> 8) & 7) as usize], op & 0xFF, 1, true); self.thumb_next(); } /// ADD Rd,#Offset8 -- format 3 (DS 5.3). pub(crate) fn thumb_add_imm(&mut self, op: u32) { let rd = ((op >> 8) & 7) as usize; self.r[rd] = self.add(self.r[rd], op & 0xFF, 0, true); self.thumb_next(); } /// SUB Rd,#Offset8 -- format 3 (DS 5.3). pub(crate) fn thumb_sub_imm(&mut self, op: u32) { let rd = ((op >> 8) & 7) as usize; self.r[rd] = self.subtract(self.r[rd], op & 0xFF, 1, true); self.thumb_next(); } #[inline] fn thumb_logic(&mut self, op: u32, result: u32) { self.r[(op & 7) as usize] = result; self.set_nz(result); self.thumb_next(); } /// AND Rd,Rs -- format 4 (DS 5.4). pub(crate) fn thumb_and(&mut self, op: u32) { self.thumb_logic( op, self.r[(op & 7) as usize] & self.r[((op >> 3) & 7) as usize], ); } /// EOR Rd,Rs -- format 4 (DS 5.4). pub(crate) fn thumb_eor(&mut self, op: u32) { self.thumb_logic( op, self.r[(op & 7) as usize] ^ self.r[((op >> 3) & 7) as usize], ); } /// ORR Rd,Rs -- format 4 (DS 5.4). pub(crate) fn thumb_orr(&mut self, op: u32) { self.thumb_logic( op, self.r[(op & 7) as usize] | self.r[((op >> 3) & 7) as usize], ); } /// BIC Rd,Rs -- format 4 (DS 5.4). pub(crate) fn thumb_bic(&mut self, op: u32) { self.thumb_logic( op, self.r[(op & 7) as usize] & !self.r[((op >> 3) & 7) as usize], ); } /// MVN Rd,Rs -- format 4 (DS 5.4). pub(crate) fn thumb_mvn(&mut self, op: u32) { self.thumb_logic(op, !self.r[((op >> 3) & 7) as usize]); } /// TST Rd,Rs -- format 4; N and Z only (DS 5.4). pub(crate) fn thumb_tst(&mut self, op: u32) { self.set_nz(self.r[(op & 7) as usize] & self.r[((op >> 3) & 7) as usize]); self.thumb_next(); } /// ADC Rd,Rs -- format 4 (DS 5.4). pub(crate) fn thumb_adc(&mut self, op: u32) { let rd = (op & 7) as usize; let carry = self.carry(); self.r[rd] = self.add(self.r[rd], self.r[((op >> 3) & 7) as usize], carry, true); self.thumb_next(); } /// SBC Rd,Rs -- format 4 (DS 5.4). pub(crate) fn thumb_sbc(&mut self, op: u32) { let rd = (op & 7) as usize; let carry = self.carry(); self.r[rd] = self.subtract(self.r[rd], self.r[((op >> 3) & 7) as usize], carry, true); self.thumb_next(); } /// NEG Rd,Rs -- format 4: Rd = 0 - Rs (DS 5.4). pub(crate) fn thumb_neg(&mut self, op: u32) { self.r[(op & 7) as usize] = self.subtract(0, self.r[((op >> 3) & 7) as usize], 1, true); self.thumb_next(); } /// CMP Rd,Rs -- format 4 (DS 5.4). pub(crate) fn thumb_cmp(&mut self, op: u32) { self.subtract( self.r[(op & 7) as usize], self.r[((op >> 3) & 7) as usize], 1, true, ); self.thumb_next(); } /// CMN Rd,Rs -- format 4 (DS 5.4). pub(crate) fn thumb_cmn(&mut self, op: u32) { self.add( self.r[(op & 7) as usize], self.r[((op >> 3) & 7) as usize], 0, true, ); self.thumb_next(); } #[inline] fn thumb_shift_reg(&mut self, op: u32, kind: u32) { let rd = (op & 7) as usize; let amount = self.r[((op >> 3) & 7) as usize] & 0xFF; // The shift by register takes an I cycle at pc+3L (timing.md table 6-4). self.r[15] = self.r[15].wrapping_add(2); self.idle(self.r[15]); let (result, carry) = shift_by_register(kind, self.r[rd], amount, self.carry()); self.logical_flags(result, carry); self.r[rd] = result; self.next_fetch_seq = true; // merged IS (C1) } /// LSL Rd,Rs -- format 4, shift by register (DS 5.4, U10). pub(crate) fn thumb_lsl(&mut self, op: u32) { self.thumb_shift_reg(op, 0); } /// LSR Rd,Rs -- format 4, shift by register (DS 5.4, U10). pub(crate) fn thumb_lsr(&mut self, op: u32) { self.thumb_shift_reg(op, 1); } /// ASR Rd,Rs -- format 4, shift by register (DS 5.4, U10). pub(crate) fn thumb_asr(&mut self, op: u32) { self.thumb_shift_reg(op, 2); } /// ROR Rd,Rs -- format 4, rotate by register (DS 5.4, U10). pub(crate) fn thumb_ror(&mut self, op: u32) { self.thumb_shift_reg(op, 3); } /// Format 5's (Rd or Hd, the value of Rs or Hs): H1 (bit 7) adds 8 to Rd, H2 (bit 6) /// to Rs (DS 5.5). With both clear the data sheet calls ADD, CMP and MOV undefined; the /// GBA runs them on the low registers (U19: alyosha-tas `thumb/cpy`, run on a Game Boy /// Player, for MOV; the corpus for ADD and CMP, OBSERVED corpus v1). #[inline] fn hi_operands(&self, op: u32) -> (usize, u32) { let rd = ((op & 7) | ((op >> 4) & 8)) as usize; (rd, self.r[((op >> 3) & 0xF) as usize]) } /// ADD Rd,Hs/ADD Hd,Rs/ADD Hd,Hs -- format 5; no flags; Hd = PC branches (DS 5.5). pub(crate) fn thumb_add_hi(&mut self, op: u32) { let (rd, value) = self.hi_operands(op); self.r[rd] = self.r[rd].wrapping_add(value); if rd == 15 { self.set_pc(self.r[15] & !1); } else { self.thumb_next(); } } /// CMP Rd,Hs/CMP Hd,Rs/CMP Hd,Hs -- format 5 (DS 5.5). pub(crate) fn thumb_cmp_hi(&mut self, op: u32) { let (rd, value) = self.hi_operands(op); self.subtract(self.r[rd], value, 1, true); self.thumb_next(); } /// MOV Rd,Hs/MOV Hd,Rs/MOV Hd,Hs -- format 5; no flags; Hd = PC branches (DS 5.5). pub(crate) fn thumb_mov_hi(&mut self, op: u32) { let (rd, value) = self.hi_operands(op); self.r[rd] = value; if rd == 15 { self.set_pc(self.r[15] & !1); } else { self.thumb_next(); } } /// ADD Rd,PC,#Imm -- format 12: Rd = (PC AND NOT 2) + Word8 x 4; no flags (DS 5.12). pub(crate) fn thumb_add_pc_rel(&mut self, op: u32) { self.r[((op >> 8) & 7) as usize] = (self.r[15] & !2).wrapping_add((op & 0xFF) << 2); self.thumb_next(); } /// ADD Rd,SP,#Imm -- format 12: Rd = SP + Word8 x 4; no flags (DS 5.12). pub(crate) fn thumb_add_sp_rel(&mut self, op: u32) { self.r[((op >> 8) & 7) as usize] = self.r[13].wrapping_add((op & 0xFF) << 2); self.thumb_next(); } /// ADD SP,#Imm/ADD SP,#-Imm -- format 13: SP = SP +/- SWord7 x 4 (DS 5.13). pub(crate) fn thumb_add_sp(&mut self, op: u32) { let offset = (op & 0x7F) << 2; self.r[13] = if op & 0x80 != 0 { self.r[13].wrapping_sub(offset) } else { self.r[13].wrapping_add(offset) }; self.thumb_next(); } }