yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
rom_debug_agent.h
Go to the documentation of this file.
1#ifndef YAZE_CLI_SERVICE_AGENT_ROM_DEBUG_AGENT_H_
2#define YAZE_CLI_SERVICE_AGENT_ROM_DEBUG_AGENT_H_
3
4#include <cstdint>
5#include <map>
6#include <memory>
7#include <string>
8#include <vector>
9
10#include "absl/status/status.h"
11#include "absl/status/statusor.h"
15#include "protos/emulator_service.grpc.pb.h"
16
17namespace yaze {
18namespace cli {
19namespace agent {
20
36 public:
41 uint32_t address; // Breakpoint address
42 std::string location_description; // Human-readable location (e.g., "MainGameLoop+$10")
43 std::string disassembly; // Disassembled instruction
44 std::string instruction_explanation; // AI-friendly explanation of what the instruction does
45 std::map<std::string, uint16_t> registers; // Current register values
46 std::vector<std::string> call_stack; // Call stack leading to this point
47 std::vector<std::string> context_lines; // Surrounding disassembly for context
48 std::vector<std::string> suggestions; // Debugging suggestions
49 std::string memory_context; // Relevant memory state description
50 };
51
56 uint32_t address; // Memory address
57 size_t length; // Length of analyzed region
58 std::string data_type; // "sprite", "tile", "palette", "dma", "audio", etc.
59 std::string structure_name; // Specific structure name if known
60 std::string description; // Human-readable description
61 std::vector<uint8_t> data; // Raw data
62 std::map<std::string, uint32_t> fields; // Parsed fields (if structured data)
63 std::vector<std::string> anomalies; // Detected issues or unusual values
64 };
65
70 uint32_t address; // Patch location
71 size_t length; // Patch size
72 std::vector<uint8_t> original_code; // Original ROM code
73 std::vector<uint8_t> patched_code; // Patched code
74 std::string original_disassembly; // Disassembled original
75 std::string patched_disassembly; // Disassembled patch
76 std::vector<std::string> differences; // Key differences explained
77 std::vector<std::string> potential_issues; // Detected problems
78 bool is_safe; // Whether patch appears safe
79 };
80
84 enum class IssueType {
85 kBadJumpTarget, // Jump to invalid address
86 kStackImbalance, // Stack pointer corruption
87 kWramCorruption, // Writing to critical WRAM areas
88 kDmaConflict, // DMA during wrong time
89 kBankOverflow, // Code/data exceeds bank boundary
90 kInvalidOpcode, // Executing data as code
91 kInfiniteLoop, // Detected infinite loop
92 kNullPointer, // Dereferencing zero page incorrectly
93 kAudioDesync, // SPC700 communication issue
94 kPpuTimingViolation, // Writing to PPU at wrong time
95 };
96
102 uint32_t address;
103 std::string description;
104 std::string suggested_fix;
105 int severity; // 1-5, 5 being most severe
106 };
107
108 // Constructor
109 explicit RomDebugAgent(yaze::agent::EmulatorServiceImpl* emulator_service);
110
111 // --- Core Analysis Functions ---
112
116 absl::StatusOr<BreakpointAnalysis> AnalyzeBreakpoint(
117 const yaze::agent::BreakpointHitResponse& hit);
118
122 absl::StatusOr<MemoryAnalysis> AnalyzeMemory(
123 uint32_t address, size_t length);
124
128 absl::StatusOr<std::string> ExplainExecutionTrace(
129 const std::vector<ExecutionTraceBuffer::TraceEntry>& trace);
130
134 absl::StatusOr<PatchComparisonResult> ComparePatch(
135 uint32_t address, size_t length, const std::vector<uint8_t>& original);
136
137 // --- Pattern Detection ---
138
142 std::vector<DetectedIssue> ScanForIssues(
143 uint32_t start_address, uint32_t end_address);
144
148 bool IsValidJumpTarget(uint32_t address) const;
149
153 bool HasStackImbalance(uint32_t routine_start, uint32_t routine_end);
154
158 bool IsMemoryWriteSafe(uint32_t address, size_t length) const;
159
160 // --- Helper Functions ---
161
165 std::string DescribeMemoryLocation(uint32_t address) const;
166
170 std::string IdentifyDataType(uint32_t address) const;
171
175 std::string FormatRegisterState(const std::map<std::string, uint16_t>& regs) const;
176
180 absl::Status LoadSymbols(const std::string& symbol_file);
181
185 void SetOriginalRom(const std::vector<uint8_t>& rom_data);
186
187 private:
188 // --- ALTTP Memory Layout Constants ---
189
190 // WRAM regions ($7E0000-$7FFFFF)
191 static constexpr uint32_t WRAM_START = 0x7E0000;
192 static constexpr uint32_t WRAM_END = 0x7FFFFF;
193
194 // System variables
195 static constexpr uint32_t GAME_MODE = 0x7E0010;
196 static constexpr uint32_t SUBMODULE = 0x7E0011;
197 static constexpr uint32_t NMI_FLAG = 0x7E0012;
198 static constexpr uint32_t FRAME_COUNTER = 0x7E001A;
199
200 // Player/Link state
201 static constexpr uint32_t LINK_X_POS = 0x7E0022;
202 static constexpr uint32_t LINK_Y_POS = 0x7E0020;
203 static constexpr uint32_t LINK_STATE = 0x7E005D;
204 static constexpr uint32_t LINK_DIRECTION = 0x7E002F;
205
206 // Sprite tables
207 static constexpr uint32_t SPRITE_TABLE_START = 0x7E0D00;
208 static constexpr uint32_t SPRITE_TABLE_END = 0x7E0FFF;
209 static constexpr uint32_t SPRITE_STATE = 0x7E0D10;
210 static constexpr uint32_t SPRITE_X_LOW = 0x7E0D30;
211 static constexpr uint32_t SPRITE_X_HIGH = 0x7E0D20;
212 static constexpr uint32_t SPRITE_Y_LOW = 0x7E0D00;
213 static constexpr uint32_t SPRITE_Y_HIGH = 0x7E0D20;
214
215 // OAM (Object Attribute Memory) buffer
216 static constexpr uint32_t OAM_BUFFER = 0x7E0800;
217 static constexpr uint32_t OAM_BUFFER_END = 0x7E0A1F;
218
219 // DMA registers
220 static constexpr uint32_t DMA0_CONTROL = 0x004300;
221 static constexpr uint32_t DMA_ENABLE = 0x00420B;
222 static constexpr uint32_t HDMA_ENABLE = 0x00420C;
223
224 // PPU registers
225 static constexpr uint32_t PPU_INIDISP = 0x002100;
226 static constexpr uint32_t PPU_BGMODE = 0x002105;
227 static constexpr uint32_t PPU_CGADD = 0x002121;
228 static constexpr uint32_t PPU_CGDATA = 0x002122;
229
230 // Audio communication
231 static constexpr uint32_t APU_PORT0 = 0x002140;
232 static constexpr uint32_t APU_PORT1 = 0x002141;
233 static constexpr uint32_t APU_PORT2 = 0x002142;
234 static constexpr uint32_t APU_PORT3 = 0x002143;
235
236 // Save data
237 static constexpr uint32_t SRAM_START = 0x7EF000;
238 static constexpr uint32_t SRAM_END = 0x7EF4FF;
239 static constexpr uint32_t PLAYER_NAME = 0x7EF000;
240 static constexpr uint32_t PLAYER_HEALTH = 0x7EF36D;
241 static constexpr uint32_t PLAYER_MAX_HEALTH = 0x7EF36C;
242 static constexpr uint32_t INVENTORY_START = 0x7EF340;
243
244 // --- Helper Methods ---
245
249 absl::StatusOr<std::string> AnalyzeInstruction(
250 uint32_t address, const uint8_t* code, size_t max_length);
251
255 std::vector<std::string> GetDisassemblyContext(
256 uint32_t address, int before_lines, int after_lines);
257
261 std::vector<std::string> BuildCallStack(uint32_t current_pc);
262
266 std::optional<DetectedIssue> DetectIssuePattern(
267 uint32_t address, const uint8_t* code, size_t length);
268
272 bool IsCriticalMemoryArea(uint32_t address) const;
273
277 std::optional<std::string> GetStructureInfo(uint32_t address) const;
278
279 // Member variables
281 std::unique_ptr<Disassembler65816> disassembler_;
282 std::unique_ptr<yaze::emu::debug::SymbolProvider> symbol_provider_;
283 std::vector<uint8_t> original_rom_; // Original ROM for comparison
284
285 // Cache for performance
286 mutable std::map<uint32_t, std::string> address_description_cache_;
287 mutable std::map<uint32_t, std::string> data_type_cache_;
288};
289
290} // namespace agent
291} // namespace cli
292} // namespace yaze
293
294#endif // YAZE_CLI_SERVICE_AGENT_ROM_DEBUG_AGENT_H_
ROM Debugging Agent for AI-assisted ROM hacking.
static constexpr uint32_t FRAME_COUNTER
void SetOriginalRom(const std::vector< uint8_t > &rom_data)
Set the original ROM data for comparison.
static constexpr uint32_t OAM_BUFFER
static constexpr uint32_t PLAYER_HEALTH
static constexpr uint32_t SPRITE_X_LOW
static constexpr uint32_t LINK_Y_POS
static constexpr uint32_t OAM_BUFFER_END
static constexpr uint32_t DMA_ENABLE
static constexpr uint32_t SPRITE_X_HIGH
absl::StatusOr< std::string > ExplainExecutionTrace(const std::vector< ExecutionTraceBuffer::TraceEntry > &trace)
Analyze execution trace and explain program flow.
static constexpr uint32_t WRAM_END
absl::StatusOr< BreakpointAnalysis > AnalyzeBreakpoint(const yaze::agent::BreakpointHitResponse &hit)
Analyze a breakpoint hit with full context.
static constexpr uint32_t APU_PORT0
bool IsMemoryWriteSafe(uint32_t address, size_t length) const
Check if memory write is safe.
static constexpr uint32_t PPU_BGMODE
static constexpr uint32_t LINK_X_POS
std::string FormatRegisterState(const std::map< std::string, uint16_t > &regs) const
Format register state for debugging output.
static constexpr uint32_t SPRITE_TABLE_START
bool IsValidJumpTarget(uint32_t address) const
Check if an address is a valid jump target.
static constexpr uint32_t PPU_CGADD
static constexpr uint32_t INVENTORY_START
static constexpr uint32_t GAME_MODE
RomDebugAgent(yaze::agent::EmulatorServiceImpl *emulator_service)
static constexpr uint32_t APU_PORT2
yaze::agent::EmulatorServiceImpl * emulator_service_
static constexpr uint32_t SRAM_END
std::optional< std::string > GetStructureInfo(uint32_t address) const
Get structure information for a memory address.
std::map< uint32_t, std::string > address_description_cache_
std::vector< DetectedIssue > ScanForIssues(uint32_t start_address, uint32_t end_address)
Scan for common ROM hacking issues in a code region.
static constexpr uint32_t SRAM_START
std::vector< std::string > GetDisassemblyContext(uint32_t address, int before_lines, int after_lines)
Get surrounding context for an address.
std::vector< std::string > BuildCallStack(uint32_t current_pc)
Build call stack from execution trace.
bool HasStackImbalance(uint32_t routine_start, uint32_t routine_end)
Detect stack imbalance in a subroutine.
std::vector< uint8_t > original_rom_
std::map< uint32_t, std::string > data_type_cache_
static constexpr uint32_t SPRITE_STATE
static constexpr uint32_t APU_PORT3
absl::Status LoadSymbols(const std::string &symbol_file)
Load symbol table for better disassembly.
static constexpr uint32_t SPRITE_Y_LOW
absl::StatusOr< PatchComparisonResult > ComparePatch(uint32_t address, size_t length, const std::vector< uint8_t > &original)
Compare original ROM code with patched code.
std::unique_ptr< yaze::emu::debug::SymbolProvider > symbol_provider_
std::optional< DetectedIssue > DetectIssuePattern(uint32_t address, const uint8_t *code, size_t length)
Detect pattern of common issues.
static constexpr uint32_t SPRITE_Y_HIGH
static constexpr uint32_t SPRITE_TABLE_END
std::string DescribeMemoryLocation(uint32_t address) const
Get human-readable description of a memory address.
static constexpr uint32_t SUBMODULE
absl::StatusOr< std::string > AnalyzeInstruction(uint32_t address, const uint8_t *code, size_t max_length)
Analyze the instruction at an address.
static constexpr uint32_t PPU_INIDISP
bool IsCriticalMemoryArea(uint32_t address) const
Check if address is in a critical system area.
static constexpr uint32_t NMI_FLAG
absl::StatusOr< MemoryAnalysis > AnalyzeMemory(uint32_t address, size_t length)
Analyze a memory region and identify its purpose.
static constexpr uint32_t LINK_STATE
static constexpr uint32_t PLAYER_NAME
static constexpr uint32_t HDMA_ENABLE
static constexpr uint32_t WRAM_START
static constexpr uint32_t PPU_CGDATA
static constexpr uint32_t PLAYER_MAX_HEALTH
static constexpr uint32_t DMA0_CONTROL
static constexpr uint32_t APU_PORT1
std::unique_ptr< Disassembler65816 > disassembler_
IssueType
Common ROM hacking issue types.
static constexpr uint32_t LINK_DIRECTION
std::string IdentifyDataType(uint32_t address) const
Get the data type at a memory address.
Analysis result for a breakpoint hit.
std::map< std::string, uint32_t > fields