// SPDX-License-Identifier: MIT AND BSD-3-Clause // From MAME 0.285's ssp1601.cpp (BSD-3-Clause) via ssp1601-python: notice below and in NOTICE.md. // // Through ssp1601-python, this file 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, transcribed from `ssp1601-python` at c863a2c. //! //! One instruction per [`Ssp1601::step`]. The chip has no datasheet instruction set; the //! semantics are those of MAME 0.285's `ssp1601.cpp`, as the reference's module docstring //! and `docs/claims.md` lay out. Nothing is hardware-measured. //! //! **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) or program memory, which the host implements as [`Host`]. //! //! Rust's differences: the reference's three callables are a trait, passed to each call //! so the host can own the core. `ext_read` and `ext_write` are also told ST, because the //! SVP's memory controller looks at the DSP's ST bits and the reference reads them from //! the object the callable closed over. `unhandled` is a count where the reference keeps //! a list, which on a long run would only grow. pub const FLAG_L: u32 = 1 << 12; pub const FLAG_Z: u32 = 1 << 13; pub const FLAG_V: u32 = 1 << 14; pub const FLAG_N: u32 = 1 << 15; const SSP_NULL: u16 = 0; const SSP_X: u16 = 1; const SSP_Y: u16 = 2; const SSP_A: u16 = 3; const SSP_ST: u16 = 4; const SSP_STACK: u16 = 5; const SSP_PC: u16 = 6; const SSP_P: u16 = 7; /// Program memory and the external registers. pub trait Host { /// The 16-bit word at a 16-bit word address. fn program_read(&mut self, address: u16) -> u16; /// EXT`n`, `n` in 0-7; `st` is the DSP's ST as it stands. fn ext_read(&mut self, n: u16, st: u16) -> u16; fn ext_write(&mut self, n: u16, value: u16, st: u16); } fn signed16(value: u16) -> i64 { i64::from(value as i16) } /// The DSP. #[derive(Clone, Debug)] pub struct Ssp1601 { pub x: u16, pub y: u16, /// 32 bits; "A" in an instruction is its high word, "AL" its low. pub a: u32, pub st: u16, pub pc: u16, pub ppc: u16, pub stack: [u16; 6], pub sp: i32, /// r0-r3 point into RAM0, r4-r7 into RAM1; r3 and r7 are fixed. pub r: [u8; 8], /// RAM0 at 0, RAM1 at $100. pub ram: [u16; 0x200], /// What the instruction being executed has cost so far. pub cycles: u32, /// How many times the chip's map had no answer. pub unhandled: u64, } impl Default for Ssp1601 { fn default() -> Self { Self::new() } } impl Ssp1601 { pub fn new() -> Self { Ssp1601 { x: 0, y: 0, a: 0, st: 0, pc: 0x400, ppc: 0, stack: [0; 6], sp: 0, r: [0; 8], ram: [0; 0x200], cycles: 0, unhandled: 0, } } /// PC to the entry point ($400), the stack empty and ST clear; nothing else. pub fn reset(&mut self) { self.pc = 0x400; self.sp = 0; self.st = 0; } /// P is always X * Y * 2, both signed, as a 32-bit value. pub fn p(&self) -> u32 { (signed16(self.x) * signed16(self.y) * 2) as u32 } // -- registers 0-15 as an instruction names them -------------------------------- pub fn read_register(&mut self, number: u16, host: &mut impl Host) -> u16 { match number { SSP_NULL => 0xFFFF, SSP_X => self.x, SSP_Y => self.y, SSP_A => (self.a >> 16) as u16, SSP_ST => self.st, SSP_STACK => { self.sp -= 1; if self.sp < 0 { self.sp = 5; // underflow wraps; MAME logs it } self.stack[self.sp as usize] } SSP_PC => self.pc, SSP_P => (self.p() >> 16) as u16, 15 => { // AL: "reading AL causes some effect on the EXT bus, VR depends on it" host.ext_read(7, self.st); self.a as u16 } _ => host.ext_read(number & 7, self.st), } } pub fn write_register(&mut self, number: u16, value: u16, host: &mut impl Host) { match number { SSP_NULL | SSP_P => {} SSP_X => self.x = value, SSP_Y => self.y = value, SSP_A => self.a = (self.a & 0xFFFF) | (u32::from(value) << 16), SSP_ST => self.st = value, SSP_STACK => { if self.sp >= 6 { self.sp = 0; // overflow wraps; MAME logs it } self.stack[self.sp as usize] = value; self.sp += 1; } SSP_PC => { self.pc = value; self.cycles += 1; // a jump through PC costs a cycle more } 15 => { host.ext_write(7, value, self.st); self.a = (self.a & 0xFFFF_0000) | u32::from(value); } _ => host.ext_write(number & 7, value, self.st), } } // -- pointer registers ---------------------------------------------------------- /// `(ri)` with its modifier: the RAM word it addresses, the pointer then moved. /// /// `j` is 0 for RAM0 and 4 for RAM1, `modifier` 0 for none, 1 for `+!`, 2 for `-` and /// 3 for `+`. A read applies RPL (ST bits 0-2) to `-` and `+`; a write does not. fn pointer_read(&mut self, op: u16, j: usize, modifier: u16) -> u16 { let index = usize::from(op & 3); let base = if j != 0 { 0x100 } else { 0 }; if index == 3 { // r3 and r7: the first four words of the bank, by modifier return self.ram[base + usize::from(modifier)]; } let pointer = j | index; let address = self.r[pointer]; let value = self.ram[base + usize::from(address)]; if modifier == 1 { self.r[pointer] = address.wrapping_add(1); } else if modifier != 0 { let moved = if modifier == 2 { address.wrapping_sub(1) } else { address.wrapping_add(1) }; let size = self.st & 7; self.r[pointer] = if size == 0 { moved } else { let mask = ((1u16 << size) - 1) as u8; (address & !mask) | (moved & mask) }; } value } fn pointer_write(&mut self, op: u16, value: u16) { let index = usize::from(op & 3); let j = usize::from((op >> 6) & 4); let modifier = (op >> 2) & 3; let base = if j != 0 { 0x100 } else { 0 }; if index == 3 { self.ram[base + usize::from(modifier)] = value; return; } let pointer = j | index; let address = self.r[pointer]; self.ram[base + usize::from(address)] = value; if modifier == 1 || modifier == 3 { self.r[pointer] = address.wrapping_add(1); } else if modifier == 2 { self.r[pointer] = address.wrapping_sub(1); } } /// `((ri))`: program memory at the address in RAM, that RAM word incremented. /// /// Only the unmodified forms exist (MAME logs the rest and returns 0). fn double_indirect(&mut self, op: u16, host: &mut impl Host) -> u16 { let index = usize::from(op & 3); let modifier = usize::from((op >> 2) & 3); let j = usize::from((op >> 6) & 4); let base = if j != 0 { 0x100 } else { 0 }; let slot = if index == 3 { base + modifier } else if modifier == 0 { base + usize::from(self.r[j | index]) } else { self.unhandled += 1; return 0; }; let address = self.ram[slot]; self.ram[slot] = address.wrapping_add(1); host.program_read(address) } // -- the ALU -------------------------------------------------------------------- /// Z and N from the full 32-bit accumulator; L and V clear (MAME emulates no more). fn flags(&mut self, value: u32) { let mut st = u32::from(self.st) & !(FLAG_L | FLAG_Z | FLAG_V | FLAG_N); if value == 0 { st |= FLAG_Z; } else { st |= (value >> 16) & FLAG_N; } self.st = st as u16; } /// The logical operations leave L and V as they were. fn flags_zn(&mut self, value: u32) { let mut st = u32::from(self.st) & !(FLAG_Z | FLAG_N); if value == 0 { st |= FLAG_Z; } else { st |= (value >> 16) & FLAG_N; } self.st = st as u16; } /// `kind` is bits 15-13 of the opcode; `operand` is already a 32-bit value. fn alu(&mut self, kind: u16, operand: u32) { let a = self.a; match kind { 1 => { self.a = a.wrapping_sub(operand); self.flags(self.a); } 3 => self.flags(a.wrapping_sub(operand)), 4 => { self.a = a.wrapping_add(operand); self.flags(self.a); } 5 => { self.a = a & operand; self.flags_zn(self.a); } 6 => { self.a = a | operand; self.flags_zn(self.a); } _ => { self.a = a ^ operand; self.flags_zn(self.a); } } } /// Always, Z and N matching bit 8 (`f`); the chip's other conditions are not /// modelled (MAME logs one and treats it as false). fn condition(&mut self, op: u16) -> bool { let st = u32::from(self.st); let op32 = u32::from(op); match op & 0xF0 { 0x00 => true, 0x50 => (st ^ (op32 << 5)) & FLAG_Z == 0, 0x70 => (st ^ (op32 << 7)) & FLAG_N == 0, _ => { self.unhandled += 1; false } } } // -- fetch and execute ---------------------------------------------------------- fn fetch(&mut self, host: &mut impl Host) -> u16 { let word = host.program_read(self.pc); self.pc = self.pc.wrapping_add(1); word } /// Execute one instruction and return the cycles it took. pub fn step(&mut self, host: &mut impl Host) -> u32 { self.ppc = self.pc; self.cycles = 1; let op = self.fetch(host); let d = (op >> 4) & 0x0F; let kind = op >> 13; let top = op >> 9; match top { 0x00 => self.ld_d_s(op, host), 0x01 => { let value = self.pointer_read(op, usize::from((op >> 6) & 4), (op >> 2) & 3); self.write_register(d, value, host); } 0x02 => { let value = self.read_register(d, host); self.pointer_write(op, value); } 0x03 => { self.a = (self.a & 0xFFFF) | (u32::from(self.ram[usize::from(op & 0x1FF)]) << 16); } 0x04 => { let value = self.fetch(host); self.write_register(d, value, host); self.cycles += 1; } 0x05 => { let value = self.double_indirect(op, host); self.write_register(d, value, host); self.cycles += 2; } 0x06 => { let value = self.fetch(host); self.pointer_write(op, value); self.cycles += 1; } 0x07 => self.ram[usize::from(op & 0x1FF)] = (self.a >> 16) as u16, 0x09 => { let value = u16::from(self.r[usize::from((op & 3) | ((op >> 6) & 4))]); self.write_register(d, value, host); } 0x0A => { let value = self.read_register(d, host); self.r[usize::from((op & 3) | ((op >> 6) & 4))] = value as u8; } 0x0C..=0x0F => self.r[usize::from((op >> 8) & 7)] = op as u8, 0x24 => { if self.condition(op) { let target = self.fetch(host); self.write_register(SSP_STACK, self.pc, host); self.pc = target; } else { self.pc = self.pc.wrapping_add(1); } self.cycles += 1; // always two cycles } 0x25 => { let value = host.program_read((self.a >> 16) as u16); self.write_register(d, value, host); self.cycles += 2; } 0x26 => { if self.condition(op) { self.pc = self.fetch(host); } else { self.pc = self.pc.wrapping_add(1); } self.cycles += 1; } 0x48 => self.modify(op), 0x1B => { self.a = self.a.wrapping_sub(self.p()); self.flags_zn(self.a); self.load_xy(op); } 0x4B => { self.a = self.a.wrapping_add(self.p()); self.flags_zn(self.a); self.load_xy(op); } 0x5B => { self.a = 0; self.st = (self.st & 0x0FFF) | FLAG_Z as u16; self.load_xy(op); } _ => { // The ALU forms: kinds 1, 3, 4, 5, 6 and 7 in bits 6-4 of the index. let group = top >> 4; if !matches!(group, 1 | 3 | 4 | 5 | 6 | 7) { self.unhandled += 1; return self.cycles; } match top & 0x0F { 0x00 => { let number = op & 0x0F; let operand = if number == SSP_P { self.p() } else if number == SSP_A { self.a } else { u32::from(self.read_register(number, host)) << 16 }; self.alu(kind, operand); } 0x01 => { let value = self.pointer_read(op, usize::from((op >> 6) & 4), (op >> 2) & 3); self.alu(kind, u32::from(value) << 16); } 0x03 => { let value = self.ram[usize::from(op & 0x1FF)]; self.alu(kind, u32::from(value) << 16); } 0x04 => { let value = self.fetch(host); self.alu(kind, u32::from(value) << 16); self.cycles += 1; } 0x05 => { let value = self.double_indirect(op, host); self.alu(kind, u32::from(value) << 16); self.cycles += 2; } 0x09 => { let value = self.r[usize::from((op & 3) | ((op >> 6) & 4))]; self.alu(kind, u32::from(value) << 16); } 0x0C => self.alu(kind, u32::from(op & 0xFF) << 16), _ => self.unhandled += 1, } } } self.cycles } /// Execute whole instructions until `cycles` have been used; the cycles taken (which /// can overshoot by the last instruction). pub fn run(&mut self, cycles: u64, host: &mut impl Host) -> u64 { let mut used = 0; while used < cycles { used += u64::from(self.step(host)); } used } fn ld_d_s(&mut self, op: u16, host: &mut impl Host) { if op == 0 { return; } if op == (SSP_A << 4) | SSP_P { // A <- P, all 32 bits self.a = self.p(); return; } let value = self.read_register(op & 0x0F, host); self.write_register((op >> 4) & 0x0F, value, host); } fn modify(&mut self, op: u16) { if !self.condition(op) { return; } let mut a = self.a; match op & 7 { 2 => a = ((a as i32) >> 1) as u32, 3 => a <<= 1, 6 => a = a.wrapping_neg(), 7 => { if a & 0x8000_0000 != 0 { a = a.wrapping_neg(); } } _ => self.unhandled += 1, } self.a = a; self.flags_zn(a); } /// P = X * Y * 2 is formed from the *old* X and Y, then X and Y are reloaded. fn load_xy(&mut self, op: u16) { self.x = self.pointer_read(op, 0, (op >> 2) & 3); self.y = self.pointer_read(op >> 4, 4, (op >> 6) & 3); } }