yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
disassembler.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EMU_DEBUG_DISASSEMBLER_H_
2#define YAZE_APP_EMU_DEBUG_DISASSEMBLER_H_
3
4#include <cstdint>
5#include <functional>
6#include <string>
7#include <unordered_map>
8#include <vector>
9
10#include "absl/strings/str_format.h"
11
12namespace yaze {
13namespace emu {
14namespace debug {
15
20 kImplied, // No operand
21 kAccumulator, // A
22 kImmediate8, // #$xx (8-bit)
23 kImmediate16, // #$xxxx (16-bit, depends on M/X flags)
24 kImmediateM, // #$xx or #$xxxx (depends on M flag)
25 kImmediateX, // #$xx or #$xxxx (depends on X flag)
26 kDirectPage, // $xx
27 kDirectPageIndexedX, // $xx,X
28 kDirectPageIndexedY, // $xx,Y
29 kDirectPageIndirect, // ($xx)
34 kAbsolute, // $xxxx
35 kAbsoluteIndexedX, // $xxxx,X
36 kAbsoluteIndexedY, // $xxxx,Y
37 kAbsoluteLong, // $xxxxxx
38 kAbsoluteLongIndexedX, // $xxxxxx,X
39 kAbsoluteIndirect, // ($xxxx)
40 kAbsoluteIndirectLong, // [$xxxx]
41 kAbsoluteIndexedIndirect, // ($xxxx,X)
42 kProgramCounterRelative, // 8-bit relative branch
43 kProgramCounterRelativeLong, // 16-bit relative branch
44 kStackRelative, // $xx,S
46 kBlockMove, // src,dst (MVN/MVP)
47};
48
53 std::string mnemonic; // e.g., "LDA", "STA", "JSR"
54 AddressingMode65816 mode; // Addressing mode
55 uint8_t base_size; // Base size in bytes (1 for opcode alone)
56
58 InstructionInfo(const std::string& m, AddressingMode65816 am, uint8_t size)
59 : mnemonic(m), mode(am), base_size(size) {}
60};
61
66 uint32_t address; // Full 24-bit address
67 uint8_t opcode; // The opcode byte
68 std::vector<uint8_t> operands; // Operand bytes
69 std::string mnemonic; // Instruction mnemonic
70 std::string operand_str; // Formatted operand (e.g., "#$FF", "$1234,X")
71 std::string full_text; // Complete disassembly line
72 uint8_t size; // Total instruction size
73 bool is_branch; // Is this a branch instruction?
74 bool is_call; // Is this JSR/JSL?
75 bool is_return; // Is this RTS/RTL/RTI?
76 uint32_t branch_target; // Target address for branches/jumps
77
79 : address(0), opcode(0), size(1), is_branch(false),
80 is_call(false), is_return(false), branch_target(0) {}
81};
82
102 public:
103 using MemoryReader = std::function<uint8_t(uint32_t)>;
104 using SymbolResolver = std::function<std::string(uint32_t)>;
105
107
116 DisassembledInstruction Disassemble(uint32_t address,
117 MemoryReader read_byte,
118 bool m_flag = true,
119 bool x_flag = true) const;
120
130 std::vector<DisassembledInstruction> DisassembleRange(
131 uint32_t start_address,
132 size_t count,
133 MemoryReader read_byte,
134 bool m_flag = true,
135 bool x_flag = true) const;
136
141 symbol_resolver_ = resolver;
142 }
143
147 const InstructionInfo& GetInstructionInfo(uint8_t opcode) const;
148
152 uint8_t GetInstructionSize(uint8_t opcode, bool m_flag, bool x_flag) const;
153
154 private:
155 // Initialize opcode tables
157
158 // Format operand based on addressing mode
159 std::string FormatOperand(AddressingMode65816 mode,
160 const std::vector<uint8_t>& operands,
161 uint32_t address,
162 bool m_flag,
163 bool x_flag) const;
164
165 // Calculate branch target
166 uint32_t CalculateBranchTarget(uint32_t address,
167 const std::vector<uint8_t>& operands,
169 uint8_t instruction_size) const;
170
171 // Opcode to instruction info mapping
173
174 // Optional symbol resolver
176};
177
178} // namespace debug
179} // namespace emu
180} // namespace yaze
181
182#endif // YAZE_APP_EMU_DEBUG_DISASSEMBLER_H_
65816 CPU disassembler for debugging and ROM hacking
std::function< std::string(uint32_t)> SymbolResolver
DisassembledInstruction Disassemble(uint32_t address, MemoryReader read_byte, bool m_flag=true, bool x_flag=true) const
Disassemble a single instruction.
uint8_t GetInstructionSize(uint8_t opcode, bool m_flag, bool x_flag) const
Calculate actual instruction size based on flags.
const InstructionInfo & GetInstructionInfo(uint8_t opcode) const
Get instruction info for an opcode.
std::vector< DisassembledInstruction > DisassembleRange(uint32_t start_address, size_t count, MemoryReader read_byte, bool m_flag=true, bool x_flag=true) const
Disassemble multiple instructions.
std::function< uint8_t(uint32_t)> MemoryReader
uint32_t CalculateBranchTarget(uint32_t address, const std::vector< uint8_t > &operands, AddressingMode65816 mode, uint8_t instruction_size) const
std::string FormatOperand(AddressingMode65816 mode, const std::vector< uint8_t > &operands, uint32_t address, bool m_flag, bool x_flag) const
void SetSymbolResolver(SymbolResolver resolver)
Set optional symbol resolver for address lookups.
AddressingMode65816
Addressing modes for the 65816 CPU.
Result of disassembling a single instruction.
Information about a single 65816 instruction.
InstructionInfo(const std::string &m, AddressingMode65816 am, uint8_t size)