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 "
174 "<json|text>]";
175 }
176
177 bool RequiresLabels() const override { return false; }
178
179 protected:
180 absl::Status ValidateArgs(const resources::ArgumentParser& parser) override;
181
182 absl::Status Execute(Rom* rom, const resources::ArgumentParser& parser,
183 resources::OutputFormatter& formatter) override;
184
185 private:
186 // Analyze sprite table entry
187 std::map<std::string, std::string> AnalyzeSpriteEntry(
188 int sprite_index, const std::vector<uint8_t>& wram) const;
189
190 // Analyze player state
191 std::map<std::string, std::string> AnalyzePlayerState(
192 const std::vector<uint8_t>& wram) const;
193
194 // Analyze game mode
195 std::map<std::string, std::string> AnalyzeGameMode(
196 const std::vector<uint8_t>& wram) const;
197};
198
210 public:
211 std::string GetName() const override { return "memory-search"; }
212
213 std::string GetDescription() const {
214 return "Search for byte patterns in memory";
215 }
216
217 std::string GetUsage() const override {
218 return "memory-search --pattern <hex> [--start <addr>] [--end <addr>] "
219 "[--max-results <n>] [--format <json|text>]";
220 }
221
222 bool RequiresLabels() const override { return false; }
223
224 protected:
225 absl::Status ValidateArgs(const resources::ArgumentParser& parser) override;
226
227 absl::Status Execute(Rom* rom, const resources::ArgumentParser& parser,
228 resources::OutputFormatter& formatter) override;
229
230 private:
231 // Parse pattern string (supports wildcards)
232 absl::StatusOr<std::pair<std::vector<uint8_t>, std::vector<bool>>>
233 ParsePattern(const std::string& pattern_str) const;
234
235 // Match pattern against memory
236 std::vector<PatternMatch> FindMatches(const std::vector<uint8_t>& memory,
237 uint32_t base_address,
238 const std::vector<uint8_t>& pattern,
239 const std::vector<bool>& mask,
240 int max_results) const;
241};
242
255 public:
256 std::string GetName() const override { return "memory-compare"; }
257
258 std::string GetDescription() const {
259 return "Compare memory regions or against expected values";
260 }
261
262 std::string GetUsage() const override {
263 return "memory-compare --address <addr> [--expected <hex>] [--format "
264 "<json|text>]";
265 }
266
267 bool RequiresLabels() const override { return false; }
268
269 protected:
270 absl::Status ValidateArgs(const resources::ArgumentParser& parser) override;
271
272 absl::Status Execute(Rom* rom, const resources::ArgumentParser& parser,
273 resources::OutputFormatter& formatter) override;
274};
275
288 public:
289 std::string GetName() const override { return "memory-check"; }
290
291 std::string GetDescription() const {
292 return "Check memory for anomalies and corruption";
293 }
294
295 std::string GetUsage() const override {
296 return "memory-check [--region <region_name>] [--format <json|text>]";
297 }
298
299 bool RequiresLabels() const override { return false; }
300
301 protected:
302 absl::Status ValidateArgs(const resources::ArgumentParser& parser) override;
303
304 absl::Status Execute(Rom* rom, const resources::ArgumentParser& parser,
305 resources::OutputFormatter& formatter) override;
306
307 private:
308 // Check sprite table for anomalies
309 std::vector<MemoryAnomaly> CheckSpriteTable(
310 const std::vector<uint8_t>& wram) const;
311
312 // Check player state for anomalies
313 std::vector<MemoryAnomaly> CheckPlayerState(
314 const std::vector<uint8_t>& wram) const;
315
316 // Check game mode for anomalies
317 std::vector<MemoryAnomaly> CheckGameMode(
318 const std::vector<uint8_t>& wram) const;
319};
320
329 public:
330 std::string GetName() const override { return "memory-regions"; }
331
332 std::string GetDescription() const {
333 return "List known memory regions and their descriptions";
334 }
335
336 std::string GetUsage() const override {
337 return "memory-regions [--filter <pattern>] [--format <json|text>]";
338 }
339
340 bool RequiresLabels() const override { return false; }
341
342 protected:
343 absl::Status ValidateArgs(const resources::ArgumentParser& parser) override;
344
345 absl::Status Execute(Rom* rom, const resources::ArgumentParser& parser,
346 resources::OutputFormatter& formatter) override;
347};
348
349} // namespace tools
350} // namespace agent
351} // namespace cli
352} // namespace yaze
353
354#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:52
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.