# SPDX-License-Identifier: MIT """OP and RT: one ALU operation, one move, the pointer updates and, for RT, a return. The order inside the instruction (U1; DB p.9, DS p.16 and p.26; timing.md "Inside one instruction"): SRC drives the internal data bus (IDB); the ALU reads P and the selected accumulator and latches its result; the move writes DST; DP and RP change; RT pops last. The multiplier step that ends every instruction is in ``cpu.py``. LD shares the move (``_move``) with OP and RT. """ from __future__ import annotations from upd7725_python._alu import FLAG_S1 from upd7725_python._fields import ( DST_A, DST_B, DST_DP, DST_DR, DST_K, DST_KLM, DST_KLR, DST_L, DST_MEM, DST_NON, DST_RP, DST_SOL, DST_SOM, DST_SR, DST_TR, DST_TRB, SRC_A, SRC_B, SRC_DP, SRC_DR, SRC_DRNF, SRC_K, SRC_L, SRC_RO, SRC_RP, SRC_SGN, SRC_SIL, SRC_SIM, SRC_SR, SRC_TR, SRC_TRB, ) SR_RQM = 0x8000 SR_DRS = 0x1000 SR_DRC = 0x0400 # U8: a program write to SR keeps RQM, DRS and bits 6-2 (DB p.7: "The RQM and DRS bits can only # be affected by parallel data moves"; DS (17): bits 6-2 read 0). Every emulator keeps 907Ch. SR_KEPT_ON_WRITE = 0x907C def reverse16(value: int) -> int: """The 16 bits of ``value`` in the opposite order (MAME's ``bitswap<16>`` for C4).""" out = 0 for _ in range(16): out = out << 1 | (value & 1) value >>= 1 return out class OpMixin: """OP (bits 23-22 = 00), RT (01) and the move every OP, RT and LD makes.""" #: C3. False (default): the ALU runs and sets its flags, then a move to the same #: accumulator overwrites the result (ares, MAME; the DSP-4's two #: ``SHL2 A`` + ``MOV @A`` words exist only to clear CA, program evidence). True: the #: 1992 data book's "the ALU operation becomes a NOP" (DB p.9): no flags change. ALU_NOP_WHEN_MOVE_TO_ACC = False #: C4. False (default): @SOL stores and SIL reads the bus unchanged (ares; the #: ST011's JMPSO tables hold plain addresses). True: MAME's bit reversal. SERIAL_BIT_REVERSE = False #: C16. False (default): DPINC, DPDEC and DPCLR change DP bits 3-0 and nothing else #: (DS (5); fullsnes "DP.3-0 adjust"), which on the uPD96050's 11-bit DP keeps bits 10-8. #: True: ares and MAME, which rebuild DP as ``(DP & F0h) + low`` and so #: clear bits 10-8 on the uPD96050. The uPD7725 (8-bit DP) cannot tell them apart. DPL_CLEARS_DP_BITS_10_8 = False a: int b: int tr: int trb: int dp: int rp: int k: int l: int m: int n: int sr: int dr: int si: int so: int sp: int pc: int flaga: int stack: list[int] ram: list[int] data_rom: list[int] _dp_mask: int _rp_mask: int _stack_mask: int def _op(self, word: int) -> None: source = word >> 4 & 0xF destination = word & 0xF # 1. SRC drives the bus first (C15: a @KLR/@KLM move from K or L reads the old value). idb = self._read_source(source) # 2. The ALU, from P and the selected accumulator. operation = word >> 16 & 0xF if operation: # ALU code 0 is NOP: both flag sets kept (U12) select_b = word >> 15 & 1 if not (self.ALU_NOP_WHEN_MOVE_TO_ACC and destination == DST_A + select_b): p_select = word >> 20 & 3 if p_select == 0: p = self.ram[self.dp] # C14: RAM before this instruction's @MEM writes it elif p_select == 1: p = idb # the move's value is on the bus for the ALU (DB p.9) elif p_select == 2: p = self.m else: p = self.n self._alu(operation, select_b, p) # 3. The move (an @A/@B destination overwrites the ALU's result: C3). self._move(destination, idb) # 4. Pointer changes, "at the end of the instruction cycle after their values have been # used for data moves" (DB p.9). A move to DP or RP cancels them (U2). if destination != DST_DP: dp = self.dp low_change = word >> 13 & 3 if low_change: kept = dp & 0xF0 if self.DPL_CLEARS_DP_BITS_10_8 else dp & ~0xF if low_change == 1: dp = kept | (dp + 1) & 0xF # DPINC: DP bits 3-0 wrap, no carry (U6) elif low_change == 2: dp = kept | (dp - 1) & 0xF # DPDEC else: dp = kept # DPCLR self.dp = dp ^ (word >> 9 & 0xF) << 4 # DPHM: XOR DP bits 7-4 (DS p.20) if word & 0x100 and destination != DST_RP: self.rp = (self.rp - 1) & self._rp_mask # RPDEC; 0 wraps to all ones (U9) def _return(self) -> None: """RT: after everything else, pop the return address (DB p.9: "executed last").""" self.sp = (self.sp - 1) & self._stack_mask # U10: the pointer wraps in its depth self.pc = self.stack[self.sp] def _read_source(self, source: int) -> int: """The value SRC puts on the internal data bus (DS p.21).""" if source == SRC_TRB: return self.trb # "NON" outputs TRB (DS SRC note 1; U15) if source == SRC_A: return self.a if source == SRC_B: return self.b if source == SRC_TR: return self.tr if source == SRC_DP: return self.dp if source == SRC_RP: return self.rp if source == SRC_RO: return self.data_rom[self.rp] # RO: the data ROM word RP points at if source == SRC_SGN: # 8000h if SA1 = 0, 7FFFh if SA1 = 1, whatever ASL selects (DS (16); U4). return 0x7FFF if self.flaga & FLAG_S1 else 0x8000 if source == SRC_DR: self.sr |= SR_RQM # reading DR requests the host's next transfer (U7) return self.dr if source == SRC_DRNF: return self.dr # "the RQM flag is not set" (DS SRC note 2) if source == SRC_SR: return self.sr if source == SRC_SIM: return self.si if source == SRC_SIL: return reverse16(self.si) if self.SERIAL_BIT_REVERSE else self.si # C4 if source == SRC_K: return self.k if source == SRC_L: return self.l return self.ram[self.dp] # MEM def _move(self, destination: int, value: int) -> None: """Write the bus value to DST (DS p.22). OP, RT and LD all end their move here.""" if destination == DST_NON: return if destination == DST_A: self.a = value elif destination == DST_B: self.b = value elif destination == DST_TR: self.tr = value elif destination == DST_DP: self.dp = value & self._dp_mask # the chip's width (U9; MAME keeps 16 bits: C9) elif destination == DST_RP: self.rp = value & self._rp_mask elif destination == DST_DR: self.dr = value self.sr |= SR_RQM # a DR write requests the host's read (U7) elif destination == DST_SR: self.sr = (self.sr & SR_KEPT_ON_WRITE) | (value & ~SR_KEPT_ON_WRITE) # U8 elif destination == DST_SOL: self.so = reverse16(value) if self.SERIAL_BIT_REVERSE else value # C4 elif destination == DST_SOM: self.so = value elif destination == DST_K: self.k = value elif destination == DST_KLR: self.k = value self.l = self.data_rom[self.rp] # L from RO (DS DST note 3; U5) elif destination == DST_KLM: self.l = value self.k = self.ram[self.dp | 0x40] # K from RAM at DP6 = 1 (DS DST note 4; U5) elif destination == DST_L: self.l = value elif destination == DST_TRB: self.trb = value elif destination == DST_MEM: self.ram[self.dp] = value __all__ = ["SR_DRC", "SR_DRS", "SR_KEPT_ON_WRITE", "SR_RQM", "OpMixin", "reverse16"]