Skip to content

SuperH: decode the SH-4's instructions - #440

Open
spriteice wants to merge 3 commits into
encounter:mainfrom
spriteice:superh-sh4-instructions
Open

spriteice wants to merge 3 commits into
encounter:mainfrom
spriteice:superh-sh4-instructions

Conversation

@spriteice

Copy link
Copy Markdown

Requested by spriteice · project thread

Builds on #438 and #439; only the last commit is new here.

The SuperH decoder knows the SH-2's instructions only, so SH-4 code (the Dreamcast's) shows the SH-4's additions as .word. This decodes every instruction that GNU objdump decodes for the SH-4 (-m sh4) and objdiff didn't, 5,537 encodings, with the same operands:

  • every floating-point instruction: the fmov forms, fadd, fsub, fmul, fdiv, fmac, fcmp/eq, fcmp/gt, flds, fsts, float, ftrc, fneg, fabs, fsqrt, fldi0, fldi1, fcnvsd, fcnvds, fipr, ftrv, fschg, frchg, fsca and fsrra
  • lds and sts (and .l) with fpscr and fpul
  • pref, movca.l, ocbi, ocbp, ocbwb, shad, shld, ldtlb, clrs, sets
  • ldc and stc (and .l) with ssr, spc, sgr, dbr and the banked registers
  • mova, an SH-1 instruction that was missing

Every register is an opaque argument, so the diff compares it. Like objdump, fmov and the arithmetic show single fr registers, as when FPSCR.SZ and FPSCR.PR are clear, because the bytes can't say which mode the code runs in. mova shows its offset as mov.l @(disp,PC) does.

Checked

  • Every 16-bit value, little-endian, in one function through objdiff-cli, against GNU objdump 2.42 (-m sh4): objdiff and objdump now decode the same 59,033 values, and agree on every one, mnemonic and operands (PC-relative operands compared as addresses). capstone 5.0.9 agrees wherever both decode; it misses fcnvds, fcnvsd and fipr, and decodes some SH-2A and SH-4A instructions in its SH-4 mode.
  • On little-endian SH-4 objects from a retail Dreamcast game, 1.65 million instructions: objdiff now agrees with capstone on every instruction both decode, operands included. Before, 211,294 of them showed as .word.
  • New tests: test_sh4_display_instructions (every addition, with a different register in each field, every register an argument, and encodings the SH-4 doesn't have) and test_sh4_mova.
  • cargo test -p objdiff-core, cargo +nightly fmt --check and cargo clippy --all-targets pass.

🤖 Generated with Claude Code

spriteice and others added 3 commits December 4, 2026 20:08
The SuperH backend read every opcode, and the literal pool values shown
after mov.w and mov.l, as big-endian. That suits the Saturn, but Dreamcast
(SH-4) objects are little-endian, so every instruction was decoded wrongly.
Take the byte order from the object, as the ARM and MIPS backends do, and
test both orders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The diff compares instructions by their arguments, and the SuperH decoder
pushed the register of add #imm,Rn and both registers of
mov.l Rm,@(disp,Rn) and mov.l @(disp,Rm),Rn as plain text. Two functions
that differed only in one of those registers showed as a 100% match. Make
them arguments, like every other register, and show tas.b @rn with its @.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The decoder knew the SH-2's instructions only, so SH-4 code showed the
SH-4's additions as .word. Decode every instruction GNU objdump decodes for
the SH-4 that it did not: the floating-point instructions, lds and sts with
fpscr and fpul, pref, movca.l, ocbi, ocbp, ocbwb, shad, shld, ldtlb, clrs,
sets, and ldc and stc with the SH-3 and SH-4's control and banked
registers, with the same operands. Also decode mova, an SH-1 instruction.
Every register is an argument. Test every addition.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant