// SPDX-License-Identifier: MIT //! A disassembler for the SH-2, from the same decode table the core runs. //! //! sh2-python's `disasm.py`. Mnemonics and operand order are PM's (appendix A, section //! 6). Numbers follow MAME's ``sh_dasm.cpp`` so a line can be compared with MAME's //! debugger and ``unidasm``: immediates as ``#$xx`` (the raw byte), displacements scaled //! to bytes (``@($0C,R2)``; four digits for the PC forms and the word and longword GBR //! forms), branch targets and PC-relative addresses absolute (``BRA $00014490``, //! ``MOV.L @($008C,PC),R1 [0001A2D4]``). Where MAME's text differs from PM's mnemonics, //! PM is followed (the reference's tests/test_disasm.py lists the differences). //! Undefined codes disassemble as ``.word $xxxx``. use crate::cpu::tables; use crate::decode::{Form, FORMS}; /// One disassembled instruction. #[derive(Clone, Debug, PartialEq, Eq)] pub struct Instruction { pub address: u32, pub opcode: u32, pub mnemonic: String, pub operands: String, /// The branch target or PC-relative address the instruction names, if any. pub target: Option, } impl Instruction { pub fn text(&self) -> String { if self.operands.is_empty() { return self.mnemonic.clone(); } format!("{:<8}{}", self.mnemonic, self.operands) } pub fn length(&self) -> u32 { 2 } pub fn form(&self) -> Option<&'static Form> { tables().index[self.opcode as usize].map(|n| &FORMS[n as usize]) } } fn scale(mnemonic: &str) -> u32 { if mnemonic.ends_with(".W") { return 2; } if mnemonic.ends_with(".L") || mnemonic == "MOVA" { return 4; } 1 } fn field(code: &str, letter: char, opcode: u32) -> u32 { let first = code.find(letter).unwrap() as u32; (opcode >> (12 - first)) & 15 } /// Disassemble one 16-bit ``opcode`` that sits at ``address``. pub fn disassemble_word(opcode: u32, address: u32) -> Instruction { let opcode = opcode & 0xFFFF; let form = match tables().index[opcode as usize] { None => { return Instruction { address, opcode, mnemonic: ".word".into(), operands: format!("${opcode:04X}"), target: None, } } Some(n) => &FORMS[n as usize], }; let mut text = form.operands.to_string(); let mut target = None; // The code pattern says where each register field is: "nnnn" or "mmmm" in bits // 11-8 or 7-4 (LDC Rm,SR has Rm in 11-8; MOV.B @(disp,Rm),R0 has it in 7-4). if form.code.contains('n') { text = text.replace("Rn", &format!("R{}", field(form.code, 'n', opcode))); } if form.code.contains('m') { text = text.replace("Rm", &format!("R{}", field(form.code, 'm', opcode))); } let scale = scale(form.mnemonic); if text.contains("disp") { if text.contains("PC") { let disp = (opcode & 0xFF) * scale; let base = if scale == 4 { address & !3 } else { address }; let t = base.wrapping_add(4).wrapping_add(disp); target = Some(t); text = text.replace("disp", &format!("${disp:04X}")) + &format!(" [{t:08X}]"); } else if text.contains("GBR") { let value = (opcode & 0xFF) * scale; // MAME prints the byte form with two digits let shown = if scale == 1 { format!("${value:02X}") } else { format!("${value:04X}") }; text = text.replace("disp", &shown); } else { text = text.replace("disp", &format!("${:02X}", (opcode & 15) * scale)); } } if text.contains("#imm") { text = text.replace("#imm", &format!("#${:02X}", opcode & 0xFF)); } if text == "label" { let d = if form.code.starts_with("1000") { // BT, BF, BT/S, BF/S: 8-bit displacement (opcode & 0xFF) as u8 as i8 as i32 } else { // BRA, BSR: 12-bit let d = (opcode & 0xFFF) as i32; if d & 0x800 != 0 { d - 0x1000 } else { d } }; let t = address.wrapping_add(4).wrapping_add((2 * d) as u32); target = Some(t); text = format!("${t:08X}"); } Instruction { address, opcode, mnemonic: form.mnemonic.to_string(), operands: text, target } } /// Disassemble the instruction at ``address`` using a side-effect-free word reader. pub fn disassemble(peek16: &mut dyn FnMut(u32) -> u32, address: u32) -> Instruction { disassemble_word(peek16(address), address) } pub fn disassemble_range(peek16: &mut dyn FnMut(u32) -> u32, address: u32, count: u32) -> Vec { let mut out = Vec::new(); let mut address = address; for _ in 0..count { out.push(disassemble(peek16, address)); address = address.wrapping_add(2); } out }