"""Data processing: the barrel shifter, the sixteen ARM ALU operations and their Thumb forms. 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). """ from __future__ import annotations from arm7tdmi_python._core import MASK32, CoreMixin from arm7tdmi_python._modes import FLAG_C, FLAG_N, FLAG_V, FLAG_Z S_BIT = 1 << 20 def ror32(value: int, amount: int) -> int: amount &= 31 return ((value >> amount) | (value << (32 - amount))) & MASK32 if amount else value def shift_by_immediate(kind: int, value: int, amount: int, carry: int) -> tuple[int, int]: """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. """ if kind == 3 and amount == 0: # RRX return (value >> 1) | (carry << 31), value & 1 if amount == 0 and kind in (1, 2): amount = 32 return shift_by_register(kind, value, amount, carry) def shift_by_register(kind: int, value: int, amount: int, carry: int) -> tuple[int, int]: """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. """ if amount == 0: return value, carry if kind == 0: # LSL if amount > 32: return 0, 0 return (value << amount) & MASK32, (value >> (32 - amount)) & 1 if kind == 1: # LSR if amount > 32: return 0, 0 return value >> amount, (value >> (amount - 1)) & 1 if kind == 2: # ASR signed = value - (1 << 32) if value & FLAG_N else value if amount >= 32: return (MASK32 if signed < 0 else 0), (1 if signed < 0 else 0) return (signed >> amount) & MASK32, (signed >> (amount - 1)) & 1 result = ror32(value, amount) # ROR return result, result >> 31 class ALUMixin(CoreMixin): # ------------------------------------------------------------------ helpers def _carry(self) -> int: return 1 if self.cpsr & FLAG_C else 0 def _add(self, a: int, b: int, carry_in: int, set_flags: bool) -> int: """a + b + carry_in; with set_flags, N Z C V of the sum (DS 4.5.4).""" total = a + b + carry_in result = total & MASK32 if set_flags: self.cpsr = ( (self.cpsr & ~(FLAG_N | FLAG_Z | FLAG_C | FLAG_V)) | (result & FLAG_N) | (FLAG_Z if result == 0 else 0) | (FLAG_C if total > MASK32 else 0) | (FLAG_V if ~(a ^ b) & (b ^ result) & FLAG_N else 0) ) return result def _subtract(self, a: int, b: int, carry_in: int, set_flags: bool) -> int: """a - b - (1 - carry_in); C is NOT borrow (DS 4.5.4).""" borrow = carry_in ^ 1 result = (a - b - borrow) & MASK32 if set_flags: self.cpsr = ( (self.cpsr & ~(FLAG_N | FLAG_Z | FLAG_C | FLAG_V)) | (result & FLAG_N) | (FLAG_Z if result == 0 else 0) | (FLAG_C if a >= b + borrow else 0) | (FLAG_V if (a ^ b) & (a ^ result) & FLAG_N else 0) ) return result def _logical_flags(self, result: int, carry: int) -> None: """N and Z from the result, C from the shifter; V unchanged (DS 4.5.4).""" self._set_nz(result) self.cpsr = (self.cpsr & ~FLAG_C) | (FLAG_C if carry else 0) def _operand2(self, op: int) -> tuple[int, int]: """The ARM Op2 and the shifter's carry out (DS 4.5.3, figure 4-6).""" carry = self._carry() if op & (1 << 25): # A rotated immediate: C becomes bit 31 when the rotation is non-zero. value = ror32(op & 0xFF, ((op >> 8) & 0xF) * 2) return value, (value >> 31 if (op >> 8) & 0xF else carry) kind = (op >> 5) & 3 if op & 0x10: amount = self._reg((op >> 8) & 0xF) & 0xFF # The shift by register takes an I cycle at pc+3L; the PC has advanced (U9). self.r[15] = (self.r[15] + 4) & MASK32 self._idle(self.r[15]) return shift_by_register(kind, self._reg(op & 0xF), amount, carry) return shift_by_immediate(kind, self._reg(op & 0xF), (op >> 7) & 0x1F, carry) def _finish(self, op: int, result: int | None) -> None: """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. """ rd = (op >> 12) & 0xF register_shift = not op & (1 << 25) and op & 0x10 if result is not None: self._set_reg(rd, result) self.next_fetch_seq = True # S, or merged IS after a register shift (C1) if rd == 15: if op & S_BIT: self._restore_cpsr_from_spsr() if result is not None: self._refill() return if not register_shift: self.r[15] = (self.r[15] + 4) & MASK32 def _logical(self, op: int, result: int, carry: int) -> None: if op & S_BIT: self._logical_flags(result, carry) self._finish(op, result) # ------------------------------------------------------------------ ARM def _arm_and(self, op: int) -> None: """AND -- Rd = Rn AND Op2 (DS 4.5).""" op2, carry = self._operand2(op) self._logical(op, self._reg((op >> 16) & 0xF) & op2, carry) def _arm_eor(self, op: int) -> None: """EOR -- Rd = Rn EOR Op2 (DS 4.5).""" op2, carry = self._operand2(op) self._logical(op, self._reg((op >> 16) & 0xF) ^ op2, carry) def _arm_orr(self, op: int) -> None: """ORR -- Rd = Rn OR Op2 (DS 4.5).""" op2, carry = self._operand2(op) self._logical(op, self._reg((op >> 16) & 0xF) | op2, carry) def _arm_bic(self, op: int) -> None: """BIC -- Rd = Rn AND NOT Op2 (DS 4.5).""" op2, carry = self._operand2(op) self._logical(op, self._reg((op >> 16) & 0xF) & ~op2 & MASK32, carry) def _arm_mov(self, op: int) -> None: """MOV -- Rd = Op2; Rn is ignored (DS 4.5).""" op2, carry = self._operand2(op) self._logical(op, op2, carry) def _arm_mvn(self, op: int) -> None: """MVN -- Rd = NOT Op2; Rn is ignored (DS 4.5).""" op2, carry = self._operand2(op) self._logical(op, ~op2 & MASK32, carry) def _arm_tst(self, op: int) -> None: """TST -- flags of Rn AND Op2; with Rd = R15 also CPSR <- SPSR (DS 4.5, U13).""" op2, carry = self._operand2(op) self._logical_flags(self._reg((op >> 16) & 0xF) & op2, carry) self._finish(op, None) def _arm_teq(self, op: int) -> None: """TEQ -- flags of Rn EOR Op2; with Rd = R15 also CPSR <- SPSR (DS 4.5, U13).""" op2, carry = self._operand2(op) self._logical_flags(self._reg((op >> 16) & 0xF) ^ op2, carry) self._finish(op, None) def _arm_add(self, op: int) -> None: """ADD -- Rd = Rn + Op2 (DS 4.5).""" op2, _ = self._operand2(op) self._finish(op, self._add(self._reg((op >> 16) & 0xF), op2, 0, bool(op & S_BIT))) def _arm_adc(self, op: int) -> None: """ADC -- Rd = Rn + Op2 + C (DS 4.5).""" op2, _ = self._operand2(op) rn = self._reg((op >> 16) & 0xF) self._finish(op, self._add(rn, op2, self._carry(), bool(op & S_BIT))) def _arm_sub(self, op: int) -> None: """SUB -- Rd = Rn - Op2 (DS 4.5).""" op2, _ = self._operand2(op) self._finish(op, self._subtract(self._reg((op >> 16) & 0xF), op2, 1, bool(op & S_BIT))) def _arm_sbc(self, op: int) -> None: """SBC -- Rd = Rn - Op2 - NOT C (DS 4.5).""" op2, _ = self._operand2(op) rn = self._reg((op >> 16) & 0xF) self._finish(op, self._subtract(rn, op2, self._carry(), bool(op & S_BIT))) def _arm_rsb(self, op: int) -> None: """RSB -- Rd = Op2 - Rn (DS 4.5).""" op2, _ = self._operand2(op) self._finish(op, self._subtract(op2, self._reg((op >> 16) & 0xF), 1, bool(op & S_BIT))) def _arm_rsc(self, op: int) -> None: """RSC -- Rd = Op2 - Rn - NOT C (DS 4.5).""" op2, _ = self._operand2(op) rn = self._reg((op >> 16) & 0xF) self._finish(op, self._subtract(op2, rn, self._carry(), bool(op & S_BIT))) def _arm_cmp(self, op: int) -> None: """CMP -- flags of Rn - Op2; with Rd = R15 also CPSR <- SPSR (DS 4.5, U13).""" op2, _ = self._operand2(op) self._subtract(self._reg((op >> 16) & 0xF), op2, 1, True) self._finish(op, None) def _arm_cmn(self, op: int) -> None: """CMN -- flags of Rn + Op2; with Rd = R15 also CPSR <- SPSR (DS 4.5, U13).""" op2, _ = self._operand2(op) self._add(self._reg((op >> 16) & 0xF), op2, 0, True) self._finish(op, None) # ------------------------------------------------------------------ Thumb def _thumb_next(self) -> None: """An ordinary one-cycle Thumb instruction: the PC advances, the next fetch is S.""" self.next_fetch_seq = True self.r[15] = (self.r[15] + 2) & MASK32 def _thumb_shift_imm(self, op: int, kind: int) -> None: rd, rs = op & 7, (op >> 3) & 7 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() def _thumb_lsl_imm(self, op: int) -> None: """LSL Rd,Rs,#Offset5 -- format 1 (DS 5.1).""" self._thumb_shift_imm(op, 0) def _thumb_lsr_imm(self, op: int) -> None: """LSR Rd,Rs,#Offset5 -- format 1; #0 means 32 (DS 5.1, U10).""" self._thumb_shift_imm(op, 1) def _thumb_asr_imm(self, op: int) -> None: """ASR Rd,Rs,#Offset5 -- format 1; #0 means 32 (DS 5.1, U10).""" self._thumb_shift_imm(op, 2) def _thumb_add3(self, op: int) -> None: """ADD Rd,Rs,Rn/ADD Rd,Rs,#Offset3 -- format 2 (DS 5.2).""" field = (op >> 6) & 7 operand = field if op & (1 << 10) else self.r[field] self.r[op & 7] = self._add(self.r[(op >> 3) & 7], operand, 0, True) self._thumb_next() def _thumb_sub3(self, op: int) -> None: """SUB Rd,Rs,Rn/SUB Rd,Rs,#Offset3 -- format 2 (DS 5.2).""" field = (op >> 6) & 7 operand = field if op & (1 << 10) else self.r[field] self.r[op & 7] = self._subtract(self.r[(op >> 3) & 7], operand, 1, True) self._thumb_next() def _thumb_mov_imm(self, op: int) -> None: """MOV Rd,#Offset8 -- format 3; N and Z set, C and V unchanged (DS 5.3).""" value = op & 0xFF self.r[(op >> 8) & 7] = value self._set_nz(value) self._thumb_next() def _thumb_cmp_imm(self, op: int) -> None: """CMP Rd,#Offset8 -- format 3 (DS 5.3).""" self._subtract(self.r[(op >> 8) & 7], op & 0xFF, 1, True) self._thumb_next() def _thumb_add_imm(self, op: int) -> None: """ADD Rd,#Offset8 -- format 3 (DS 5.3).""" rd = (op >> 8) & 7 self.r[rd] = self._add(self.r[rd], op & 0xFF, 0, True) self._thumb_next() def _thumb_sub_imm(self, op: int) -> None: """SUB Rd,#Offset8 -- format 3 (DS 5.3).""" rd = (op >> 8) & 7 self.r[rd] = self._subtract(self.r[rd], op & 0xFF, 1, True) self._thumb_next() def _thumb_logic(self, op: int, result: int) -> None: self.r[op & 7] = result & MASK32 self._set_nz(result) self._thumb_next() def _thumb_and(self, op: int) -> None: """AND Rd,Rs -- format 4 (DS 5.4).""" self._thumb_logic(op, self.r[op & 7] & self.r[(op >> 3) & 7]) def _thumb_eor(self, op: int) -> None: """EOR Rd,Rs -- format 4 (DS 5.4).""" self._thumb_logic(op, self.r[op & 7] ^ self.r[(op >> 3) & 7]) def _thumb_orr(self, op: int) -> None: """ORR Rd,Rs -- format 4 (DS 5.4).""" self._thumb_logic(op, self.r[op & 7] | self.r[(op >> 3) & 7]) def _thumb_bic(self, op: int) -> None: """BIC Rd,Rs -- format 4 (DS 5.4).""" self._thumb_logic(op, self.r[op & 7] & ~self.r[(op >> 3) & 7]) def _thumb_mvn(self, op: int) -> None: """MVN Rd,Rs -- format 4 (DS 5.4).""" self._thumb_logic(op, ~self.r[(op >> 3) & 7]) def _thumb_tst(self, op: int) -> None: """TST Rd,Rs -- format 4; N and Z only (DS 5.4).""" self._set_nz(self.r[op & 7] & self.r[(op >> 3) & 7]) self._thumb_next() def _thumb_adc(self, op: int) -> None: """ADC Rd,Rs -- format 4 (DS 5.4).""" rd = op & 7 self.r[rd] = self._add(self.r[rd], self.r[(op >> 3) & 7], self._carry(), True) self._thumb_next() def _thumb_sbc(self, op: int) -> None: """SBC Rd,Rs -- format 4 (DS 5.4).""" rd = op & 7 self.r[rd] = self._subtract(self.r[rd], self.r[(op >> 3) & 7], self._carry(), True) self._thumb_next() def _thumb_neg(self, op: int) -> None: """NEG Rd,Rs -- format 4: Rd = 0 - Rs (DS 5.4).""" self.r[op & 7] = self._subtract(0, self.r[(op >> 3) & 7], 1, True) self._thumb_next() def _thumb_cmp(self, op: int) -> None: """CMP Rd,Rs -- format 4 (DS 5.4).""" self._subtract(self.r[op & 7], self.r[(op >> 3) & 7], 1, True) self._thumb_next() def _thumb_cmn(self, op: int) -> None: """CMN Rd,Rs -- format 4 (DS 5.4).""" self._add(self.r[op & 7], self.r[(op >> 3) & 7], 0, True) self._thumb_next() def _thumb_shift_reg(self, op: int, kind: int) -> None: rd = op & 7 amount = self.r[(op >> 3) & 7] & 0xFF # The shift by register takes an I cycle at pc+3L (timing.md table 6-4). self.r[15] = (self.r[15] + 2) & MASK32 self._idle(self.r[15]) 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) def _thumb_lsl(self, op: int) -> None: """LSL Rd,Rs -- format 4, shift by register (DS 5.4, U10).""" self._thumb_shift_reg(op, 0) def _thumb_lsr(self, op: int) -> None: """LSR Rd,Rs -- format 4, shift by register (DS 5.4, U10).""" self._thumb_shift_reg(op, 1) def _thumb_asr(self, op: int) -> None: """ASR Rd,Rs -- format 4, shift by register (DS 5.4, U10).""" self._thumb_shift_reg(op, 2) def _thumb_ror(self, op: int) -> None: """ROR Rd,Rs -- format 4, rotate by register (DS 5.4, U10).""" self._thumb_shift_reg(op, 3) def _hi_operands(self, op: int) -> tuple[int, int]: """Format 5's Rd/Hd and the Rs/Hs value. H1 = H2 = 0 is executed on the low registers (U19, alyosha-tas thumb/cpy).""" rd = (op & 7) | ((op >> 4) & 8) return rd, self.r[(op >> 3) & 0xF] def _thumb_add_hi(self, op: int) -> None: """ADD Rd,Hs/ADD Hd,Rs/ADD Hd,Hs -- format 5; no flags; Hd = PC branches (DS 5.5).""" rd, value = self._hi_operands(op) self.r[rd] = (self.r[rd] + value) & MASK32 if rd == 15: self._set_pc(self.r[15] & ~1) else: self._thumb_next() def _thumb_cmp_hi(self, op: int) -> None: """CMP Rd,Hs/CMP Hd,Rs/CMP Hd,Hs -- format 5 (DS 5.5).""" rd, value = self._hi_operands(op) self._subtract(self.r[rd], value, 1, True) self._thumb_next() def _thumb_mov_hi(self, op: int) -> None: """MOV Rd,Hs/MOV Hd,Rs/MOV Hd,Hs -- format 5; no flags; Hd = PC branches (DS 5.5).""" rd, value = self._hi_operands(op) self.r[rd] = value if rd == 15: self._set_pc(self.r[15] & ~1) else: self._thumb_next() def _thumb_add_pc_rel(self, op: int) -> None: """ADD Rd,PC,#Imm -- format 12: Rd = (PC AND NOT 2) + Word8 x 4; no flags (DS 5.12).""" self.r[(op >> 8) & 7] = ((self.r[15] & ~2) + ((op & 0xFF) << 2)) & MASK32 self._thumb_next() def _thumb_add_sp_rel(self, op: int) -> None: """ADD Rd,SP,#Imm -- format 12: Rd = SP + Word8 x 4; no flags (DS 5.12).""" self.r[(op >> 8) & 7] = (self.r[13] + ((op & 0xFF) << 2)) & MASK32 self._thumb_next() def _thumb_add_sp(self, op: int) -> None: """ADD SP,#Imm/ADD SP,#-Imm -- format 13: SP = SP +/- SWord7 x 4 (DS 5.13).""" offset = (op & 0x7F) << 2 self.r[13] = (self.r[13] + (-offset if op & 0x80 else offset)) & MASK32 self._thumb_next()