yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
memory_inspector_tool.h
Go to the documentation of this file.
1#ifndef YAZE_SRC_CLI_SERVICE_AGENT_TOOLS_MEMORY_INSPECTOR_TOOL_H_
2#define YAZE_SRC_CLI_SERVICE_AGENT_TOOLS_MEMORY_INSPECTOR_TOOL_H_
3
4#include <cstdint>
5#include <map>
6#include <optional>
7#include <string>
8#include <vector>
9
10#include "absl/status/status.h"
11#include "absl/status/statusor.h"
13
14namespace yaze {
15namespace cli {
16namespace agent {
17namespace tools {
18
25 // WRAM regions ($7E0000-$7FFFFF)
26 static constexpr uint32_t kWRAMStart = 0x7E0000;
27 static constexpr uint32_t kWRAMEnd = 0x7FFFFF;
28
29 // System variables
30 static constexpr uint32_t kGameMode = 0x7E0010;
31 static constexpr uint32_t kSubmodule = 0x7E0011;
32 static constexpr uint32_t kNmiFlag = 0x7E0012;
33 static constexpr uint32_t kFrameCounter = 0x7E001A;
34
35 // Player/Link state
36 static constexpr uint32_t kLinkXLow = 0x7E0022;
37 static constexpr uint32_t kLinkXHigh = 0x7E0023;
38 static constexpr uint32_t kLinkYLow = 0x7E0020;
39 static constexpr uint32_t kLinkYHigh = 0x7E0021;
40 static constexpr uint32_t kLinkState = 0x7E005D;
41 static constexpr uint32_t kLinkDirection = 0x7E002F;
42 static constexpr uint32_t kLinkLayer = 0x7E00EE;
43
44 // Sprite tables (16 sprites max)
45 static constexpr uint32_t kSpriteYLow = 0x7E0D00;
46 static constexpr uint32_t kSpriteXLow = 0x7E0D10;
47 static constexpr uint32_t kSpriteYHigh = 0x7E0D20;
48 static constexpr uint32_t kSpriteXHigh = 0x7E0D30;
49 static constexpr uint32_t kSpriteState = 0x7E0DD0;
50 static constexpr uint32_t kSpriteType = 0x7E0E20;
51 static constexpr uint32_t kSpriteHealth = 0x7E0E50;
52 static constexpr int kMaxSprites = 16;
53
54 // OAM buffer
55 static constexpr uint32_t kOAMBuffer = 0x7E0800;
56 static constexpr uint32_t kOAMBufferEnd = 0x7E0A1F;
57
58 // Save data / Inventory
59 static constexpr uint32_t kSRAMStart = 0x7EF000;
60 static constexpr uint32_t kSRAMEnd = 0x7EF4FF;
61 static constexpr uint32_t kPlayerHealth = 0x7EF36D;
62 static constexpr uint32_t kPlayerMaxHealth = 0x7EF36C;
63 static constexpr uint32_t kPlayerRupees = 0x7EF360;
64 static constexpr uint32_t kInventoryStart = 0x7EF340;
65
66 // Current location
67 static constexpr uint32_t kOverworldArea = 0x7E008A;
68 static constexpr uint32_t kDungeonRoom = 0x7E00A0;
69 static constexpr uint32_t kIndoors = 0x7E001B;
70
71 // Check if address is in WRAM
72 static bool IsWRAM(uint32_t addr) {
73 return addr >= kWRAMStart && addr <= kWRAMEnd;
74 }
75
76 // Check if address is in sprite table
77 static bool IsSpriteTable(uint32_t addr) {
78 return addr >= 0x7E0D00 && addr <= 0x7E0FFF;
79 }
80
81 // Check if address is in save data
82 static bool IsSaveData(uint32_t addr) {
83 return addr >= kSRAMStart && addr <= kSRAMEnd;
84 }
85};
86
91 std::string name;
92 std::string description;
93 uint32_t start_address;
94 uint32_t end_address;
95 std::string data_type; // "byte", "word", "struct", "array"
96};
97
102 uint32_t address;
103 std::string type; // "out_of_bounds", "null_pointer", "corruption"
104 std::string description;
105 int severity; // 1-5, 5 being most severe
106};
107
112 uint32_t address;
113 std::vector<uint8_t> matched_bytes;
114 std::string context; // Memory region name
115};
116
121 protected:
125 std::string DescribeAddress(uint32_t address) const;
126
130 std::string IdentifyDataType(uint32_t address) const;
131
135 std::vector<MemoryRegionInfo> GetKnownRegions() const;
136
140 std::string FormatHex(const std::vector<uint8_t>& data,
141 int bytes_per_line = 16) const;
142
146 std::string FormatAscii(const std::vector<uint8_t>& data) const;
147
151 absl::StatusOr<uint32_t> ParseAddress(const std::string& addr_str) const;
152};
153
165 public:
166 std::string GetName() const override { return "memory-analyze"; }
167
168 std::string GetDescription() const {
169 return "Analyze a memory region with structure awareness";
170 }
171
172 std::string GetUsage() const override {
173 return "memory-analyze --address <addr> --length <len> [--format <json|text>]";
174 }
175
176 bool RequiresLabels() const override { return false; }
177
178 protected:
179 absl::Status ValidateArgs(const resources::ArgumentParser& parser) override;
180
181 absl::Status Execute(Rom* rom, const resources::ArgumentParser& parser,
182 resources::OutputFormatter& formatter) override;
183
184 private:
185 // Analyze sprite table entry
186 std::map<std::string, std::string> AnalyzeSpriteEntry(
187 int sprite_index, const std::vector<uint8_t>& wram) const;
188
189 // Analyze player state
190 std::map<std::string, std::string> AnalyzePlayerState(
191 const std::vector<uint8_t>& wram) const;
192
193 // Analyze game mode
194 std::map<std::string, std::string> AnalyzeGameMode(
195 const std::vector<uint8_t>& wram) const;
196};
197
209 public:
210 std::string GetName() const override { return "memory-search"; }
211
212 std::string GetDescription() const {
213 return "Search for byte patterns in memory";
214 }
215
216 std::string GetUsage() const override {
217 return "memory-search --pattern <hex> [--start <addr>] [--end <addr>] "
218 "[--max-results <n>] [--format <json|text>]";
219 }
220
221 bool RequiresLabels() const override { return false; }
222
223 protected:
224 absl::Status ValidateArgs(const resources::ArgumentParser& parser) override;
225
226 absl::Status Execute(Rom* rom, const resources::ArgumentParser& parser,
227 resources::OutputFormatter& formatter) override;
228
229 private:
230 // Parse pattern string (supports wildcards)
231 absl::StatusOr<std::pair<std::vector<uint8_t>, std::vector<bool>>>
232 ParsePattern(const std::string& pattern_str) const;
233
234 // Match pattern against memory
235 std::vector<PatternMatch> FindMatches(
236 const std::vector<uint8_t>& memory, uint32_t base_address,
237 const std::vector<uint8_t>& pattern,
238 const std::vector<bool>& mask, int max_results) const;
239};
240
253 public:
254 std::string GetName() const override { return "memory-compare"; }
255
256 std::string GetDescription() const {
257 return "Compare memory regions or against expected values";
258 }
259
260 std::string GetUsage() const override {
261 return "memory-compare --address <addr> --expected <hex> [--format <json|text>]";
262 }
263
264 bool RequiresLabels() const override { return false; }
265
266 protected:
267 absl::Status ValidateArgs(const resources::ArgumentParser& parser) override;
268
269 absl::Status Execute(Rom* rom, const resources::ArgumentParser& parser,
270 resources::OutputFormatter& formatter) override;
271};
272
285 public:
286 std::string GetName() const override { return "memory-check"; }
287
288 std::string GetDescription() const {
289 return "Check memory for anomalies and corruption";
290 }
291
292 std::string GetUsage() const override {
293 return "memory-check [--region <region_name>] [--format <json|text>]";
294 }
295
296 bool RequiresLabels() const override { return false; }
297
298 protected:
299 absl::Status ValidateArgs(const resources::ArgumentParser& parser) override;
300
301 absl::Status Execute(Rom* rom, const resources::ArgumentParser& parser,
302 resources::OutputFormatter& formatter) override;
303
304 private:
305 // Check sprite table for anomalies
306 std::vector<MemoryAnomaly> CheckSpriteTable(
307 const std::vector<uint8_t>& wram) const;
308
309 // Check player state for anomalies
310 std::vector<MemoryAnomaly> CheckPlayerState(
311 const std::vector<uint8_t>& wram) const;
312
313 // Check game mode for anomalies
314 std::vector<MemoryAnomaly> CheckGameMode(
315 const std::vector<uint8_t>& wram) const;
316};
317
326 public:
327 std::string GetName() const override { return "memory-regions"; }
328
329 std::string GetDescription() const {
330 return "List known memory regions and their descriptions";
331 }
332
333 std::string GetUsage() const override {
334 return "memory-regions [--filter <pattern>] [--format <json|text>]";
335 }
336
337 bool RequiresLabels() const override { return false; }
338
339 protected:
340 absl::Status ValidateArgs(const resources::ArgumentParser& parser) override;
341
342 absl::Status Execute(Rom* rom, const resources::ArgumentParser& parser,
343 resources::OutputFormatter& formatter) override;
344};
345
346} // namespace tools
347} // namespace agent
348} // namespace cli
349} // namespace yaze
350
351#endif // YAZE_SRC_CLI_SERVICE_AGENT_TOOLS_MEMORY_INSPECTOR_TOOL_H_
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
Definition rom.h:24
Analyze a memory region with structure awareness.
std::map< std::string, std::string > AnalyzePlayerState(const std::vector< uint8_t > &wram) const
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
std::map< std::string, std::string > AnalyzeSpriteEntry(int sprite_index, const std::vector< uint8_t > &wram) const
absl::Status ValidateArgs(const resources::ArgumentParser &parser) override
Validate command arguments.
std::string GetUsage() const override
Get the command usage string.
bool RequiresLabels() const override
Check if the command requires ROM labels.
std::string GetName() const override
Get the command name.
std::map< std::string, std::string > AnalyzeGameMode(const std::vector< uint8_t > &wram) const
Check memory for anomalies and corruption.
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
bool RequiresLabels() const override
Check if the command requires ROM labels.
std::string GetUsage() const override
Get the command usage string.
std::vector< MemoryAnomaly > CheckPlayerState(const std::vector< uint8_t > &wram) const
std::vector< MemoryAnomaly > CheckSpriteTable(const std::vector< uint8_t > &wram) const
absl::Status ValidateArgs(const resources::ArgumentParser &parser) override
Validate command arguments.
std::string GetName() const override
Get the command name.
std::vector< MemoryAnomaly > CheckGameMode(const std::vector< uint8_t > &wram) const
Compare memory regions or detect changes.
std::string GetName() const override
Get the command name.
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
bool RequiresLabels() const override
Check if the command requires ROM labels.
absl::Status ValidateArgs(const resources::ArgumentParser &parser) override
Validate command arguments.
std::string GetUsage() const override
Get the command usage string.
Base class for memory inspection tools.
std::vector< MemoryRegionInfo > GetKnownRegions() const
Get known memory regions.
std::string IdentifyDataType(uint32_t address) const
Identify data type at address.
std::string DescribeAddress(uint32_t address) const
Get description for a memory address.
std::string FormatAscii(const std::vector< uint8_t > &data) const
Format bytes as ASCII (printable chars only)
std::string FormatHex(const std::vector< uint8_t > &data, int bytes_per_line=16) const
Format bytes as hex string.
absl::StatusOr< uint32_t > ParseAddress(const std::string &addr_str) const
Parse address from string (supports hex with $ or 0x prefix)
List known memory regions and their descriptions.
std::string GetName() const override
Get the command name.
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
absl::Status ValidateArgs(const resources::ArgumentParser &parser) override
Validate command arguments.
std::string GetUsage() const override
Get the command usage string.
bool RequiresLabels() const override
Check if the command requires ROM labels.
Search for byte patterns in memory.
absl::StatusOr< std::pair< std::vector< uint8_t >, std::vector< bool > > > ParsePattern(const std::string &pattern_str) const
bool RequiresLabels() const override
Check if the command requires ROM labels.
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
std::vector< PatternMatch > FindMatches(const std::vector< uint8_t > &memory, uint32_t base_address, const std::vector< uint8_t > &pattern, const std::vector< bool > &mask, int max_results) const
absl::Status ValidateArgs(const resources::ArgumentParser &parser) override
Validate command arguments.
std::string GetUsage() const override
Get the command usage string.
std::string GetName() const override
Get the command name.
Utility for parsing common CLI argument patterns.
Base class for CLI command handlers.
Utility for consistent output formatting across commands.
Memory region descriptor for AI-friendly output.