Add non-mutating single-instruction disassembler - #135
Open
mre wants to merge 1 commit into
Open
Conversation
Add `instruction::disassemble_one(pc, read)` and `DisasmInstr`: a side-effect-free decoder that resolves one instruction at `pc` without advancing the PC or dereferencing operands, so it can be driven from a peek-style bus. - Decodes the full HuC6280 superset via the existing `Huc6280::decode` table, so it can never drift from `fetch_next_and_decode` on which opcodes exist or how long they are (`length == 1 + mode.extra_bytes()`). - Resolves branch targets to absolute addresses (`Relative` and the BBR/BBS `ZeroPageRelative` form); all other operands are static and as-encoded (no register offsets, no zero-page relocation, no pointer dereferencing). - Never panics on unknown opcodes (defensive `.db $XX` fallback). Supporting changes: - `Instruction::mnemonic()` + `Display for Instruction`. - Derive `PartialEq, Eq` for `AddressingMode`. - Factor branch sign-extension into `sign_extend_offset`, now shared by the executing decoder and the disassembler. - Add a default-on `alloc` feature (required by `DisasmInstr::text`), keeping the crate usable on bare-metal via `default-features = false`.
omarandlorraine
approved these changes
Jul 7, 2026
omarandlorraine
left a comment
Collaborator
There was a problem hiding this comment.
Great job, I only had a few nitpicks, sorry for the delay
| /// Requires the `alloc` feature (enabled by default). | ||
| #[cfg(feature = "alloc")] | ||
| #[must_use] | ||
| pub fn disassemble_one(pc: u16, read: impl Fn(u16) -> u8) -> DisasmInstr { |
Collaborator
There was a problem hiding this comment.
Question: why is this one hardcoded to HuC6280? The way I see it, it could take a type parameter.
| let text = if operand_text.is_empty() { | ||
| mnemonic.to_string() | ||
| } else { | ||
| format!("{mnemonic} {operand_text}") |
Collaborator
There was a problem hiding this comment.
Consider if this could/should pad with spaces, now that some mnemonics are four characters long (for example, bbr0), so that the operand column will line up
| pub const fn mnemonic(&self) -> &'static str { | ||
| match self { | ||
| Instruction::ADC | Instruction::ADCnd => "ADC", | ||
| Instruction::AND => "AND", |
Collaborator
There was a problem hiding this comment.
nitpick: my preference would be for these to be lower case
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a super simple instruction debugger.