// SPDX-License-Identifier: MIT //! The 16-bit YA instructions: MOVW ADDW SUBW CMPW INCW DECW, and MUL and DIV //! (`_word.py`). //! //! YA is Y:A paired. Direct-page words take their high byte from `(d + 1) & $FF` //! in the same page (U2). //! //! Differences forced by Rust: the reference's `ya` property is //! [`Spc700::ya`] and [`Spc700::set_ya`]; the arithmetic that goes below zero //! in Python (CMPW's difference, DECW's low byte, DIV's subtraction) is in `i32` //! or `i64` with the reference's masks. use crate::core::{pack, Bus, Spc700}; use crate::flags::{FLAG_C, FLAG_H, FLAG_N, FLAG_V, FLAG_Z}; impl Spc700 { /// The direct word of MOVW YA,d, ADDW and SUBW: an internal cycle between the bytes. fn read_word_with_idle(&mut self) -> u16 { let offset = self.fetch() as u32; let low = self.load(offset); self.idle(); pack(self.load(offset + 1), low) } /// Z from all sixteen bits, after two chained 8-bit operations (U8). fn set_z16(&mut self, value: u16) { self.psw = self.psw & !FLAG_Z | if value == 0 { FLAG_Z } else { 0 }; } /// MOVW YA,d -- load a word; N Z from 16 bits (timing.md MOVW YA,d). pub(crate) fn op_movw_ya_d(&mut self, _op: u8) { let word = self.read_word_with_idle(); self.set_ya(word); self.set_nz16(self.ya()); } /// MOVW d,YA -- store a word; no flags (timing.md MOVW d,YA). pub(crate) fn op_movw_d_ya(&mut self, _op: u8) { let offset = self.fetch() as u32; self.read_dummy(self.dp(offset)); // U1: "RMW on low byte only" self.store(offset, self.a); self.store(offset + 1, self.y); } /// ADDW YA,d -- two chained ADCs, C cleared first; H from bit 11, Z from 16 bits (U8). pub(crate) fn op_addw(&mut self, _op: u8) { let word = self.read_word_with_idle(); let ya = self.ya(); self.psw &= !FLAG_C; let low = self.adc((ya & 0xFF) as u8, (word & 0xFF) as u8); let high = self.adc((ya >> 8) as u8, (word >> 8) as u8); self.set_ya(pack(high, low)); self.set_z16(self.ya()); } /// SUBW YA,d -- two chained SBCs, C set first; H from bit 11, Z from 16 bits (U8). pub(crate) fn op_subw(&mut self, _op: u8) { let word = self.read_word_with_idle(); let ya = self.ya(); self.psw |= FLAG_C; let low = self.sbc((ya & 0xFF) as u8, (word & 0xFF) as u8); let high = self.sbc((ya >> 8) as u8, (word >> 8) as u8); self.set_ya(pack(high, low)); self.set_z16(self.ya()); } /// CMPW YA,d -- N Z C from the 16-bit subtraction, one cycle shorter (U8, timing.md). pub(crate) fn op_cmpw(&mut self, _op: u8) { let offset = self.fetch() as u32; let word = self.load_pointer(offset); let difference = self.ya() as i32 - word as i32; self.psw = self.psw & !FLAG_C | if difference >= 0 { FLAG_C } else { 0 }; self.set_nz16((difference & 0xFFFF) as u16); } fn step_word(&mut self, step: i32) { let offset = self.fetch() as u32; let mut value = self.load(offset) as i32 + step; self.store(offset, (value & 0xFF) as u8); // the carry or borrow of the low byte reaches the high byte, read only now value = (value + ((self.load(offset + 1) as i32) << 8)) & 0xFFFF; self.store(offset + 1, (value >> 8) as u8); self.set_nz16(value as u16); } /// INCW d -- N Z from 16 bits; C stays (U8, timing.md INCW/DECW). pub(crate) fn op_incw(&mut self, _op: u8) { self.step_word(1); } /// DECW d -- N Z from 16 bits; C stays (U8, timing.md INCW/DECW). pub(crate) fn op_decw(&mut self, _op: u8) { self.step_word(-1); } /// MUL YA -- YA = Y * A; N Z from Y alone, the high byte (U3). pub(crate) fn op_mul(&mut self, _op: u8) { self.read_dummy(self.pc); for _ in 0..7 { self.idle(); } self.set_ya(self.y as u16 * self.a as u16); self.set_nz(self.y); } /// DIV YA,X -- anomie's 9-step shift-subtract loop, X = 0 included (U5). pub(crate) fn op_div(&mut self, _op: u8) { self.read_dummy(self.pc); for _ in 0..10 { self.idle(); } let divisor = (self.x as i64) << 9; let half = if self.y & 0x0F >= self.x & 0x0F { FLAG_H } else { 0 }; // U5: the original Y and X let mut value = self.ya() as i64; for _ in 0..9 { value = (value << 1 | value >> 16) & 0x1FFFF; // a 17-bit rotate if value >= divisor { value ^= 1; } if value & 1 != 0 { value = (value - divisor) & 0x1FFFF; } } self.y = (value >> 9 & 0xFF) as u8; self.a = (value & 0xFF) as u8; let overflow = if value & 0x100 != 0 { FLAG_V } else { 0 }; // bit 8: the quotient's ninth bit self.psw = self.psw & !(FLAG_N | FLAG_V | FLAG_H | FLAG_Z) | overflow | half; self.set_nz(self.a); } }