# SPDX-License-Identifier: MIT """A small 6502 disassembler for reading one 8 KiB PRG bank of an iNES file. python tools/dis6502.py ROM(.nes|.zip) BANK BASE START COUNT BANK is the 8 KiB bank number, BASE the CPU address the bank is viewed at (hex), START the first address to decode (hex), COUNT the number of instructions (a negative COUNT stops at the first RTS, RTI or JMP). Official opcodes only; anything else prints as .db. Written from the public 6502 opcode table; used to read the 559 dump's patched stores and IRQ handler (audit entry 16). """ from __future__ import annotations import sys import zipfile OPCODES = ( "00 BRK imp|01 ORA izx|05 ORA zp|06 ASL zp|08 PHP imp|09 ORA imm|0A ASL acc|0D ORA abs|" "0E ASL abs|10 BPL rel|11 ORA izy|15 ORA zpx|16 ASL zpx|18 CLC imp|19 ORA aby|1D ORA abx|" "1E ASL abx|20 JSR abs|21 AND izx|24 BIT zp|25 AND zp|26 ROL zp|28 PLP imp|29 AND imm|" "2A ROL acc|2C BIT abs|2D AND abs|2E ROL abs|30 BMI rel|31 AND izy|35 AND zpx|36 ROL zpx|" "38 SEC imp|39 AND aby|3D AND abx|3E ROL abx|40 RTI imp|41 EOR izx|45 EOR zp|46 LSR zp|" "48 PHA imp|49 EOR imm|4A LSR acc|4C JMP abs|4D EOR abs|4E LSR abs|50 BVC rel|51 EOR izy|" "55 EOR zpx|56 LSR zpx|58 CLI imp|59 EOR aby|5D EOR abx|5E LSR abx|60 RTS imp|61 ADC izx|" "65 ADC zp|66 ROR zp|68 PLA imp|69 ADC imm|6A ROR acc|6C JMP ind|6D ADC abs|6E ROR abs|" "70 BVS rel|71 ADC izy|75 ADC zpx|76 ROR zpx|78 SEI imp|79 ADC aby|7D ADC abx|7E ROR abx|" "81 STA izx|84 STY zp|85 STA zp|86 STX zp|88 DEY imp|8A TXA imp|8C STY abs|8D STA abs|" "8E STX abs|90 BCC rel|91 STA izy|94 STY zpx|95 STA zpx|96 STX zpy|98 TYA imp|99 STA aby|" "9A TXS imp|9D STA abx|A0 LDY imm|A1 LDA izx|A2 LDX imm|A4 LDY zp|A5 LDA zp|A6 LDX zp|" "A8 TAY imp|A9 LDA imm|AA TAX imp|AC LDY abs|AD LDA abs|AE LDX abs|B0 BCS rel|B1 LDA izy|" "B4 LDY zpx|B5 LDA zpx|B6 LDX zpy|B8 CLV imp|B9 LDA aby|BA TSX imp|BC LDY abx|BD LDA abx|" "BE LDX aby|C0 CPY imm|C1 CMP izx|C4 CPY zp|C5 CMP zp|C6 DEC zp|C8 INY imp|C9 CMP imm|" "CA DEX imp|CC CPY abs|CD CMP abs|CE DEC abs|D0 BNE rel|D1 CMP izy|D5 CMP zpx|D6 DEC zpx|" "D8 CLD imp|D9 CMP aby|DD CMP abx|DE DEC abx|E0 CPX imm|E1 SBC izx|E4 CPX zp|E5 SBC zp|" "E6 INC zp|E8 INX imp|E9 SBC imm|EA NOP imp|EC CPX abs|ED SBC abs|EE INC abs|F0 BEQ rel|" "F1 SBC izy|F5 SBC zpx|F6 INC zpx|F8 SED imp|F9 SBC aby|FD SBC abx|FE INC abx" ) SIZE = { "imp": 1, "acc": 1, "imm": 2, "zp": 2, "zpx": 2, "zpy": 2, "izx": 2, "izy": 2, "rel": 2, "abs": 3, "abx": 3, "aby": 3, "ind": 3, } FORMAT = { "imp": "", "acc": "A", "imm": "#${:02X}", "zp": "${:02X}", "zpx": "${:02X},X", "zpy": "${:02X},Y", "izx": "(${:02X},X)", "izy": "(${:02X}),Y", "abs": "${:04X}", "abx": "${:04X},X", "aby": "${:04X},Y", "ind": "(${:04X})", } TABLE = {} for entry in OPCODES.split("|"): code, name, mode = entry.split() TABLE[int(code, 16)] = (name, mode) def disassemble(data: bytes, base: int, start: int, count: int) -> list[str]: pc, out = start, [] for _ in range(abs(count) if count else 1 << 16): i = pc - base if not 0 <= i < len(data): break op = data[i] if op not in TABLE: out.append(f"{pc:04X} {op:02X} .db ${op:02X}") pc += 1 continue name, mode = TABLE[op] raw = data[i : i + SIZE[mode]] if mode == "rel": text = f"${(pc + 2 + ((raw[1] ^ 0x80) - 0x80)) & 0xFFFF:04X}" elif len(raw) == 2: text = FORMAT[mode].format(raw[1]) elif len(raw) == 3: text = FORMAT[mode].format(raw[1] | raw[2] << 8) else: text = FORMAT[mode] out.append(f"{pc:04X} {raw.hex(' '):9} {name} {text}") pc += len(raw) if count < 0 and name in ("RTS", "RTI", "JMP"): break return out def read_rom(path: str) -> bytes: if path.lower().endswith(".zip"): with zipfile.ZipFile(path) as z: name = next(n for n in z.namelist() if n.lower().endswith(".nes")) return z.read(name) with open(path, "rb") as f: return f.read() def main() -> None: if len(sys.argv) != 6: raise SystemExit(__doc__) rom = read_rom(sys.argv[1]) banks16 = rom[4] if rom[7] & 0x0C == 0x08: # NES 2.0: the high nibble of the PRG size banks16 |= (rom[9] & 0x0F) << 8 prg = rom[16 : 16 + banks16 * 0x4000] bank, base, start, count = int(sys.argv[2]), int(sys.argv[3], 16), int(sys.argv[4], 16), int(sys.argv[5]) print("\n".join(disassemble(prg[bank * 0x2000 : (bank + 1) * 0x2000], base, start, count))) if __name__ == "__main__": main()