# SPDX-License-Identifier: MIT AND BSD-3-Clause # Transcribed from MAME 0.285's ssp1601.cpp (BSD-3-Clause): notice below and in NOTICE.md. # # This module is a transcription of MAME 0.285's src/devices/cpu/ssp1601/ssp1601.cpp, which is # under the BSD-3-Clause licence; its notice is kept here as that licence requires # (docs/provenance.md): # # license:BSD-3-Clause, copyright-holders: Pierpaolo Prazzoli, Grazvydas Ignotas # Copyright 2008, Grazvydas Ignotas # # Redistribution and use in source and binary forms, with or without modification, are # permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this list of # conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright notice, this list of # conditions and the following disclaimer in the documentation and/or other materials # provided with the distribution. # 3. Neither the name of the copyright holder nor the names of its contributors may be used # to endorse or promote products derived from this software without specific prior # written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED # OF THE POSSIBILITY OF SUCH DAMAGE. """The SSP1601 instruction core. One instruction per :meth:`SSP1601.step`. The chip has no datasheet instruction set (the Samsung sheet in ``reference/`` lists pins and clocks only); the semantics are those of MAME 0.285's ``ssp1601.cpp`` (Pierpaolo Prazzoli, Grazvydas Ignotas), which is what the cycle counts and every corner below follow, over the opcode map in notaz's ``svpdoc.txt``. See docs/claims.md for what is and is not claimed. **The host.** The core owns the registers, the two 256-word RAM banks, the six-level stack, the pointer registers and the ALU. It does not own the chip's *external* registers (EXT0-EXT7, which the 68000-side memory controller of the SVP implements) or program memory, which the host passes as callables: * ``program_read(address)`` -> 16-bit word at a 16-bit *word* address; * ``ext_read(n)`` / ``ext_write(n, value)`` for EXT``n`` (``n`` in 0-7). Registers 8-14 of the instruction encoding are EXT0-EXT6 and register 15 (``AL``) is EXT7 *and* the accumulator's low word: the chip drives the external bus for it either way. """ from __future__ import annotations from collections.abc import Callable FLAG_L = 1 << 12 FLAG_Z = 1 << 13 FLAG_V = 1 << 14 FLAG_N = 1 << 15 #: Register numbers of the instruction encoding. SSP_NULL, SSP_X, SSP_Y, SSP_A, SSP_ST, SSP_STACK, SSP_PC, SSP_P = range(8) _M16 = 0xFFFF _M32 = 0xFFFFFFFF def _signed16(value: int) -> int: return value - 0x10000 if value & 0x8000 else value def _signed32(value: int) -> int: return value - 0x100000000 if value & 0x80000000 else value class SSP1601: """The DSP. ``program_read(word_address)``, ``ext_read(n)``, ``ext_write(n, value)``.""" def __init__( self, program_read: Callable[[int], int], ext_read: Callable[[int], int], ext_write: Callable[[int, int], None], ) -> None: self.program_read = program_read self.ext_read = ext_read self.ext_write = ext_write self.x = 0 self.y = 0 self.a = 0 # 32 bits; "A" in an instruction is its high word, "AL" its low self.st = 0 self.pc = 0x400 self.ppc = 0 self.stack = [0] * 6 self.sp = 0 self.r = [0] * 8 # r0-r3 point into RAM0, r4-r7 into RAM1; r3 and r7 are fixed self.ram = [0] * 0x200 # RAM0 at 0, RAM1 at $100 self.cycles = 0 # what the instruction being executed has cost so far self.unhandled: list[tuple[int, int]] = [] # (pc, opcode) the chip's map lacks self._table = self._build_table() # -- reset ------------------------------------------------------------------ def reset(self) -> None: """PC to the entry point ($400), the stack empty and ST clear; nothing else. MAME clears the rest once at power-on and not here, and the board follows.""" self.pc = 0x400 self.sp = 0 self.st = 0 # -- the P register --------------------------------------------------------- @property def p(self) -> int: """P is always X * Y * 2, both signed, as a 32-bit value.""" return (_signed16(self.x) * _signed16(self.y) * 2) & _M32 # -- registers 0-15 as an instruction names them ----------------------------- def read_register(self, number: int) -> int: if number == SSP_NULL: return 0xFFFF if number == SSP_X: return self.x if number == SSP_Y: return self.y if number == SSP_A: return self.a >> 16 if number == SSP_ST: return self.st if number == SSP_STACK: self.sp -= 1 if self.sp < 0: self.sp = 5 # underflow wraps; MAME logs it return self.stack[self.sp] if number == SSP_PC: return self.pc if number == SSP_P: return self.p >> 16 if number == 15: # AL: "reading AL causes some effect on the EXT bus, VR depends on it" self.ext_read(7) return self.a & _M16 return self.ext_read(number & 7) & _M16 def write_register(self, number: int, value: int) -> None: value &= _M16 if number in (SSP_NULL, SSP_P): return if number == SSP_X: self.x = value elif number == SSP_Y: self.y = value elif number == SSP_A: self.a = (self.a & 0xFFFF) | (value << 16) elif number == SSP_ST: self.st = value elif number == SSP_STACK: if self.sp >= 6: self.sp = 0 # overflow wraps; MAME logs it self.stack[self.sp] = value self.sp += 1 elif number == SSP_PC: self.pc = value self.cycles += 1 # a jump through PC costs a cycle more elif number == 15: self.ext_write(7, value) self.a = (self.a & 0xFFFF0000) | value else: self.ext_write(number & 7, value) # -- pointer registers ------------------------------------------------------ def _pointer_read(self, op: int, j: int, mod: int) -> int: """``(ri)`` with its modifier: the RAM word it addresses, the pointer then moved. ``j`` is 0 for RAM0 and 4 for RAM1 (as an index into :attr:`r`), ``mod`` is 0 for none, 1 for ``+!``, 2 for ``-`` and 3 for ``+``. A read applies RPL (ST bits 0-2) to ``-`` and ``+``; a write does not (MAME: "loop repeat mode ... is ignored"). """ index = op & 3 base = 0x100 if j else 0 if index == 3: # r3 and r7: the first four words of the bank, by modifier return self.ram[base + mod] pointer = j | index address = self.r[pointer] value = self.ram[base + address] if mod == 1: self.r[pointer] = (address + 1) & 0xFF elif mod: step = -1 if mod == 2 else 1 size = self.st & 7 if not size: self.r[pointer] = (address + step) & 0xFF else: mask = (1 << size) - 1 self.r[pointer] = (address & ~mask) | ((address + step) & mask) return value def _pointer_write(self, op: int, value: int) -> None: index = op & 3 j = (op >> 6) & 4 mod = (op >> 2) & 3 base = 0x100 if j else 0 if index == 3: self.ram[base + mod] = value & _M16 return pointer = j | index address = self.r[pointer] self.ram[base + address] = value & _M16 if mod == 1 or mod == 3: self.r[pointer] = (address + 1) & 0xFF elif mod == 2: self.r[pointer] = (address - 1) & 0xFF def _double_indirect(self, op: int) -> int: """``((ri))``: program memory at the address in RAM, that RAM word incremented. Only the unmodified forms exist (MAME logs the rest and returns 0).""" index = op & 3 mod = (op >> 2) & 3 j = (op >> 6) & 4 base = 0x100 if j else 0 if index == 3: slot = base + mod elif mod == 0: slot = base + self.r[j | index] else: self.unhandled.append((self.ppc, op)) return 0 address = self.ram[slot] self.ram[slot] = (address + 1) & _M16 return self.program_read(address) # -- the ALU ---------------------------------------------------------------- def _flags(self, value: int) -> None: """Z and N from the full 32-bit accumulator; L and V clear (MAME emulates no more).""" st = self.st & ~(FLAG_L | FLAG_Z | FLAG_V | FLAG_N) if not value: st |= FLAG_Z else: st |= (value >> 16) & FLAG_N self.st = st def _flags_zn(self, value: int) -> None: """The logical operations leave L and V as they were.""" st = self.st & ~(FLAG_Z | FLAG_N) if not value: st |= FLAG_Z else: st |= (value >> 16) & FLAG_N self.st = st def _alu(self, kind: int, operand: int) -> None: """``kind`` is bits 15-13 of the opcode; ``operand`` is already a 32-bit value.""" a = self.a if kind == 1: # sub self.a = (a - operand) & _M32 self._flags(self.a) elif kind == 3: # cmp self._flags((a - operand) & _M32) elif kind == 4: # add self.a = (a + operand) & _M32 self._flags(self.a) elif kind == 5: # and self.a = a & operand self._flags_zn(self.a) elif kind == 6: # or self.a = a | operand self._flags_zn(self.a) else: # eor self.a = a ^ operand self._flags_zn(self.a) def _condition(self, op: int) -> bool: """Always, Z and N matching bit 8 (``f``); the chip's other conditions are not modelled (MAME logs one and treats it as false).""" selector = op & 0xF0 if selector == 0x00: return True if selector == 0x50: return not ((self.st ^ (op << 5)) & FLAG_Z) if selector == 0x70: return not ((self.st ^ (op << 7)) & FLAG_N) self.unhandled.append((self.ppc, op)) return False # -- fetch and execute ------------------------------------------------------ def _fetch(self) -> int: word = self.program_read(self.pc) self.pc = (self.pc + 1) & _M16 return word def step(self) -> int: """Execute one instruction and return the cycles it took.""" self.ppc = self.pc self.cycles = 1 op = self._fetch() handler = self._table[op >> 9] if handler is None: self.unhandled.append((self.ppc, op)) else: handler(op) return self.cycles def run(self, cycles: int) -> int: """Execute whole instructions until ``cycles`` have been used; the cycles taken (which can overshoot by the last instruction).""" used = 0 while used < cycles: used += self.step() return used # -- the instruction map ---------------------------------------------------- def _build_table(self): table: list = [None] * 128 table[0x00] = self._ld_d_s table[0x01] = self._ld_d_ri_ind table[0x02] = self._ld_ri_ind_s table[0x03] = self._ld_a_adr table[0x04] = self._ldi_d table[0x05] = self._ld_d_ri_ind2 table[0x06] = self._ldi_ri_ind table[0x07] = self._ld_adr_a table[0x09] = self._ld_d_ri table[0x0A] = self._ld_ri_s for n in range(0x0C, 0x10): table[n] = self._ldi_ri table[0x24] = self._call table[0x25] = self._ld_d_a_ind table[0x26] = self._bra table[0x48] = self._mod table[0x1B] = self._mpys table[0x4B] = self._mpya table[0x5B] = self._mld for kind in (1, 3, 4, 5, 6, 7): top = kind << 4 table[top | 0x00] = self._alu_s table[top | 0x01] = self._alu_ri_ind table[top | 0x03] = self._alu_adr table[top | 0x04] = self._alu_imm table[top | 0x05] = self._alu_ri_ind2 table[top | 0x09] = self._alu_ri table[top | 0x0C] = self._alu_simm return table # loads def _ld_d_s(self, op: int) -> None: if op == 0: return if op == (SSP_A << 4) | SSP_P: # A <- P, all 32 bits self.a = self.p return value = self.read_register(op & 0x0F) self.write_register((op >> 4) & 0x0F, value) def _ld_d_ri_ind(self, op: int) -> None: value = self._pointer_read(op, (op >> 6) & 4, (op >> 2) & 3) self.write_register((op >> 4) & 0x0F, value) def _ld_ri_ind_s(self, op: int) -> None: self._pointer_write(op, self.read_register((op >> 4) & 0x0F)) def _ld_a_adr(self, op: int) -> None: self.a = (self.a & 0xFFFF) | (self.ram[op & 0x1FF] << 16) def _ldi_d(self, op: int) -> None: self.write_register((op >> 4) & 0x0F, self._fetch()) self.cycles += 1 def _ld_d_ri_ind2(self, op: int) -> None: self.write_register((op >> 4) & 0x0F, self._double_indirect(op)) self.cycles += 2 def _ldi_ri_ind(self, op: int) -> None: value = self._fetch() self._pointer_write(op, value) self.cycles += 1 def _ld_adr_a(self, op: int) -> None: self.ram[op & 0x1FF] = self.a >> 16 def _ld_d_ri(self, op: int) -> None: self.write_register((op >> 4) & 0x0F, self.r[(op & 3) | ((op >> 6) & 4)]) def _ld_ri_s(self, op: int) -> None: self.r[(op & 3) | ((op >> 6) & 4)] = self.read_register((op >> 4) & 0x0F) & 0xFF def _ldi_ri(self, op: int) -> None: self.r[(op >> 8) & 7] = op & 0xFF def _ld_d_a_ind(self, op: int) -> None: self.write_register((op >> 4) & 0x0F, self.program_read(self.a >> 16)) self.cycles += 2 # control flow def _call(self, op: int) -> None: if self._condition(op): target = self._fetch() self.write_register(SSP_STACK, self.pc) self.pc = target else: self.pc = (self.pc + 1) & _M16 self.cycles += 1 # always two cycles def _bra(self, op: int) -> None: if self._condition(op): self.pc = self._fetch() else: self.pc = (self.pc + 1) & _M16 self.cycles += 1 def _mod(self, op: int) -> None: if not self._condition(op): return selector = op & 7 a = self.a if selector == 2: a = (_signed32(a) >> 1) & _M32 elif selector == 3: a = (a << 1) & _M32 elif selector == 6: a = -_signed32(a) & _M32 elif selector == 7: if a & 0x80000000: a = -_signed32(a) & _M32 else: self.unhandled.append((self.ppc, op)) self.a = a self._flags_zn(a) # multiplies: P = X * Y * 2 is formed from the *old* X and Y, then X and Y are reloaded def _load_xy(self, op: int) -> None: self.x = self._pointer_read(op, 0, (op >> 2) & 3) self.y = self._pointer_read(op >> 4, 4, (op >> 6) & 3) def _mpys(self, op: int) -> None: self.a = (self.a - self.p) & _M32 self._flags_zn(self.a) self._load_xy(op) def _mpya(self, op: int) -> None: self.a = (self.a + self.p) & _M32 self._flags_zn(self.a) self._load_xy(op) def _mld(self, op: int) -> None: self.a = 0 self.st = (self.st & 0x0FFF) | FLAG_Z self._load_xy(op) # ALU forms; the opcode's top three bits (15-13) are the operation def _alu_s(self, op: int) -> None: number = op & 0x0F if number == SSP_P: operand = self.p elif number == SSP_A: operand = self.a else: operand = self.read_register(number) << 16 self._alu(op >> 13, operand) def _alu_ri_ind(self, op: int) -> None: self._alu(op >> 13, self._pointer_read(op, (op >> 6) & 4, (op >> 2) & 3) << 16) def _alu_adr(self, op: int) -> None: self._alu(op >> 13, self.ram[op & 0x1FF] << 16) def _alu_imm(self, op: int) -> None: self._alu(op >> 13, self._fetch() << 16) self.cycles += 1 def _alu_ri_ind2(self, op: int) -> None: self._alu(op >> 13, self._double_indirect(op) << 16) self.cycles += 2 def _alu_ri(self, op: int) -> None: self._alu(op >> 13, self.r[(op & 3) | ((op >> 6) & 4)] << 16) def _alu_simm(self, op: int) -> None: self._alu(op >> 13, (op & 0xFF) << 16)