// SPDX-License-Identifier: MIT //! Branches: BF, BT, BF/S, BT/S, BRA, BRAF, BSR, BSRF, JMP, JSR, RTS. //! //! sh2-python's `_branch.py`. PM 6.6-6.13, 6.25-6.26, 6.51. The PC in every displacement //! is the branch's address + 4 (``self.pc + 2`` here; ``core.rs``). ``BT``/``BF`` are not //! delayed: taken, they jump at once. The others are delayed: they record the target in //! ``delay_target`` and the next step runs the slot (``core.rs``); the target register //! is read now, before the slot can change it (PM 4.1.5). ``BT/S``/``BF/S`` make no slot //! when not taken (U2). What a taken branch costs (3 states for ``BT``/``BF``, 2 for the //! delayed ones, PM table A.50) and the fetches it discards are the scheduler's //! (pipeline.rs, PM 7.7.5). //! //! C6: HM chapter 2 prints the descriptions of ``BT`` and ``BT/S`` swapped; PM and SWM //! are followed. PM table A.50 also misprints BT/S's not-taken case as "When T = 1, nop". use crate::arith::T; use crate::bus::Bus; use crate::cpu::Sh7604; fn disp8(op: u32) -> u32 { let d = (op & 0xFF) as u8 as i8 as i32; (d << 1) as u32 } fn disp12(op: u32) -> u32 { let d = op & 0xFFF; let d = if d & 0x800 != 0 { d as i32 - 0x1000 } else { d as i32 }; (d << 1) as u32 } impl Sh7604 { /// BF label -- if T = 0, disp x 2 + PC -> PC; else nop (PM 6.6). pub(crate) fn op_bf(&mut self, op: u32) { if self.sr & T == 0 { self.pc = self.pc.wrapping_add(2).wrapping_add(disp8(op)); } } /// BT label -- if T = 1, disp x 2 + PC -> PC; else nop (PM 6.12). pub(crate) fn op_bt(&mut self, op: u32) { if self.sr & T != 0 { self.pc = self.pc.wrapping_add(2).wrapping_add(disp8(op)); } } /// BF/S label -- if T = 0, delayed branch to disp x 2 + PC; else nop (PM 6.7). pub(crate) fn op_bf_s(&mut self, op: u32) { if self.sr & T == 0 { self.delay_target = self.pc.wrapping_add(2).wrapping_add(disp8(op)) as i64; } } /// BT/S label -- if T = 1, delayed branch to disp x 2 + PC; else nop (PM 6.13). pub(crate) fn op_bt_s(&mut self, op: u32) { if self.sr & T != 0 { self.delay_target = self.pc.wrapping_add(2).wrapping_add(disp8(op)) as i64; } } /// BRA label -- delayed branch to disp x 2 + PC (PM 6.8). pub(crate) fn op_bra(&mut self, op: u32) { self.delay_target = self.pc.wrapping_add(2).wrapping_add(disp12(op)) as i64; } /// BRAF Rm -- delayed branch to Rm + PC (PM 6.9). pub(crate) fn op_braf(&mut self, op: u32) { self.delay_target = self.pc.wrapping_add(2).wrapping_add(self.r[((op >> 8) & 15) as usize]) as i64; } /// BSR label -- PC -> PR; delayed branch to disp x 2 + PC (PM 6.10). pub(crate) fn op_bsr(&mut self, op: u32) { let pc = self.pc.wrapping_add(2); self.pr = pc; self.delay_target = pc.wrapping_add(disp12(op)) as i64; } /// BSRF Rm -- PC -> PR; delayed branch to Rm + PC (PM 6.11). pub(crate) fn op_bsrf(&mut self, op: u32) { let pc = self.pc.wrapping_add(2); let target = pc.wrapping_add(self.r[((op >> 8) & 15) as usize]); // Rm read before PR is written self.pr = pc; self.delay_target = target as i64; } /// JMP @Rm -- delayed branch to Rm (PM 6.25). pub(crate) fn op_jmp(&mut self, op: u32) { self.delay_target = self.r[((op >> 8) & 15) as usize] as i64; } /// JSR @Rm -- PC -> PR; delayed branch to Rm (PM 6.26). pub(crate) fn op_jsr(&mut self, op: u32) { self.delay_target = self.r[((op >> 8) & 15) as usize] as i64; self.pr = self.pc.wrapping_add(2); } /// RTS -- delayed branch to PR (PM 6.51). pub(crate) fn op_rts(&mut self, _op: u32) { self.delay_target = self.pr as i64; } }