# SPDX-License-Identifier: MIT """The ALU: ADD, SUB, SUBR, CMP, CMPR, the logic operations, shifts, sign extension, MUL. Each handler takes the instruction word; the high byte's sub-field (bits 9-8) is the shift of A (``SHIFTS``) and the low byte the operand (a register code or an 8-bit immediate). Flags (start-here.md "Flags"; U9): N = bit 23 and Z = (24-bit result = 0) from every ALU result; C and V only from ADD and the subtractions, C = carry out of bit 23 for ADD and **1 = no borrow** for the subtractions (fullsnes "Carry Flag is CLEARED on borrow"). MUL, loads, stores and moves leave the flags alone. Contested here (undocumented-behavior.md): - **C1** shift counts 24-31: ``SHIFT_LIMIT``. - **C2** A< int: """A 24-bit value as a signed integer.""" return value - ((value & SIGN) << 1) class ALUMixin: #: C1. "ares" (default; byuu's model, ares ``instructions.cpp`` l.46, 65, 73, 81 #: ``if(s > 24) s = 0``): counts above 24 shift by 0, 24 shifts everything out. #: "mesen2-observed": counts 24-31 leave A unchanged, N and Z set from it, as a #: closed-box Mesen2 run shows (OBSERVED, ``scripts/cx4_probe.py shift``; C1). SHIFT_LIMIT = "ares" #: C6. 0 (default; byuu's model, ares): MH:ML hold the product from the next #: instruction. 1 (fullsnes "After Multiply opcodes one should insert one nop"): one #: instruction later. MUL_LATENCY = 0 # ----- the operand A< int: return self.a << SHIFTS[sub] & MASK # cut to 24 bits (C2) # ----- the adder (byuu's algorithmADD / algorithmSUB: ares instructions.cpp l.29, 88) -- def _add(self, x: int, y: int) -> int: z = x + y self.n = bool(z & SIGN) self.z = not z & MASK self.c = z > MASK self.v = bool(~(x ^ y) & (x ^ z) & SIGN) return z & MASK def _subtract(self, x: int, y: int) -> int: z = x - y self.z = not z & MASK self.n = bool(z & SIGN) self.c = z >= 0 # 1 = no borrow (U9) self.v = bool(~(x ^ y) & (x ^ z) & SIGN) # the ADD formula, as byuu's model (C10) return z & MASK def _logic_result(self, value: int) -> None: value &= MASK self.a = value self.n = bool(value & SIGN) self.z = value == 0 # ----- ADD, SUB, SUBR, CMP, CMPR ------------------------------------------------------ def _op_add_reg(self, word: int) -> None: self.a = self._add(self._shifted_a(word >> 8 & 3), self._get(word)) def _op_add_imm(self, word: int) -> None: self.a = self._add(self._shifted_a(word >> 8 & 3), word & 0xFF) def _op_sub_reg(self, word: int) -> None: self.a = self._subtract(self._shifted_a(word >> 8 & 3), self._get(word)) def _op_sub_imm(self, word: int) -> None: self.a = self._subtract(self._shifted_a(word >> 8 & 3), word & 0xFF) def _op_subr_reg(self, word: int) -> None: operand = self._get(word) self.a = self._subtract(operand, self._shifted_a(word >> 8 & 3)) def _op_subr_imm(self, word: int) -> None: self.a = self._subtract(word & 0xFF, self._shifted_a(word >> 8 & 3)) def _op_cmp_reg(self, word: int) -> None: self._subtract(self._shifted_a(word >> 8 & 3), self._get(word)) def _op_cmp_imm(self, word: int) -> None: self._subtract(self._shifted_a(word >> 8 & 3), word & 0xFF) def _op_cmpr_reg(self, word: int) -> None: operand = self._get(word) self._subtract(operand, self._shifted_a(word >> 8 & 3)) def _op_cmpr_imm(self, word: int) -> None: self._subtract(word & 0xFF, self._shifted_a(word >> 8 & 3)) # ----- XNOR, XOR, AND, OR: N and Z only ------------------------------------------------ def _op_xnor_reg(self, word: int) -> None: self._logic_result(~self._shifted_a(word >> 8 & 3) ^ self._get(word)) def _op_xnor_imm(self, word: int) -> None: self._logic_result(~self._shifted_a(word >> 8 & 3) ^ (word & 0xFF)) def _op_xor_reg(self, word: int) -> None: self._logic_result(self._shifted_a(word >> 8 & 3) ^ self._get(word)) def _op_xor_imm(self, word: int) -> None: self._logic_result(self._shifted_a(word >> 8 & 3) ^ (word & 0xFF)) def _op_and_reg(self, word: int) -> None: self._logic_result(self._shifted_a(word >> 8 & 3) & self._get(word)) def _op_and_imm(self, word: int) -> None: self._logic_result(self._shifted_a(word >> 8 & 3) & word & 0xFF) def _op_or_reg(self, word: int) -> None: self._logic_result(self._shifted_a(word >> 8 & 3) | self._get(word)) def _op_or_imm(self, word: int) -> None: self._logic_result(self._shifted_a(word >> 8 & 3) | (word & 0xFF)) # ----- shifts by a register or a 5-bit immediate (C1) --------------------------------- def _shift(self, kind: int, count: int) -> None: """kind 0 SHR, 1 ASR, 2 ROR, 3 SHL; ``count`` is the operand's low 5 bits.""" a = self.a rule = self.SHIFT_LIMIT if rule == "ares": if count > 24: count = 0 # 25-31 shift by nothing; 24 shifts everything out elif rule == "mesen2-observed": if count >= 24: count = 0 # OBSERVED: A unchanged, N and Z still set from it else: raise ValueError(f"SHIFT_LIMIT must be ares or mesen2-observed, not {rule!r}") if kind == 0: result = a >> count elif kind == 1: result = _signed(a) >> count elif kind == 2: result = a >> count | a << (24 - count) # count 0 and 24 both give A back else: result = a << count self._logic_result(result) def _op_shr_reg(self, word: int) -> None: self._shift(0, self._get(word) & 0x1F) def _op_shr_imm(self, word: int) -> None: self._shift(0, word & 0x1F) def _op_asr_reg(self, word: int) -> None: self._shift(1, self._get(word) & 0x1F) def _op_asr_imm(self, word: int) -> None: self._shift(1, word & 0x1F) def _op_ror_reg(self, word: int) -> None: self._shift(2, self._get(word) & 0x1F) def _op_ror_imm(self, word: int) -> None: self._shift(2, word & 0x1F) def _op_shl_reg(self, word: int) -> None: self._shift(3, self._get(word) & 0x1F) def _op_shl_imm(self, word: int) -> None: self._shift(3, word & 0x1F) # ----- sign extension (U19) ----------------------------------------------------------- def _op_sxb(self, word: int) -> None: a = self.a & 0xFF self._logic_result(a - ((a & 0x80) << 1)) def _op_sxw(self, word: int) -> None: a = self.a & 0xFFFF self._logic_result(a - ((a & 0x8000) << 1)) # ----- the multiplier (U8): signed 24 x 24 -> 48; the immediate is unsigned ----------- def _multiply(self, operand: int) -> None: product = (_signed(self.a) * operand) & 0xFFFFFFFFFFFF if self.MUL_LATENCY: self._mul_pending = product # visible after the next instruction (C6) else: self.mul = product def _op_mul_reg(self, word: int) -> None: self._multiply(_signed(self._get(word))) def _op_mul_imm(self, word: int) -> None: self._multiply(word & 0xFF) __all__ = ["MASK", "SIGN", "ALUMixin"]