lsnes rr0-β3
[lsnes.git] / memorymanip.hpp
blobed7b05ee4c0caf61b732b9f05af2dab14a42da3f
1 #ifndef _memorymanip__hpp__included__
2 #define _memorymanip__hpp__included__
4 #include <string>
5 #include <list>
6 #include <vector>
7 #include <cstdint>
8 #include <stdexcept>
10 /**
11 * \brief Information about region of memory
13 * This structure contains information about memory region.
15 struct memory_region
17 /**
18 * \brief Name of region.
20 * This is name of region, mainly for debugging and showing to the user.
22 std::string region_name;
23 /**
24 * \brief Base address of region.
26 uint32_t baseaddr;
27 /**
28 * \brief Size of region in bytes.
30 uint32_t size;
31 /**
32 * \brief Last valid address in this region.
34 uint32_t lastaddr;
35 /**
36 * \brief True for ROM, false for RAM.
38 bool readonly;
39 /**
40 * \brief Endianess of the region.
42 * If true, region uses host endian.
43 * If false, region uses SNES (big) endian.
45 bool native_endian;
48 /**
49 * \brief Refresh cart memory mappings
51 * This function rereads cartridge memory map. Call after loading a new cartridge.
53 * \throws std::bad_alloc Not enough memory.
55 void refresh_cart_mappings() throw(std::bad_alloc);
57 /**
58 * \brief Get listing of all regions
60 * This function returns a list of all known regions.
62 * \return All regions
63 * \throws std::bad_alloc Not enough memory.
65 std::vector<struct memory_region> get_regions() throw(std::bad_alloc);
67 /**
68 * \brief Read byte from memory
70 * This function reads one byte from memory.
72 * \param addr The address to read.
73 * \return The byte read.
75 uint8_t memory_read_byte(uint32_t addr) throw();
77 /**
78 * \brief Read word from memory
80 * This function reads two bytes from memory.
82 * \param addr The address to read.
83 * \return The word read.
85 uint16_t memory_read_word(uint32_t addr) throw();
87 /**
88 * \brief Read dword from memory
90 * This function reads four bytes from memory.
92 * \param addr The address to read.
93 * \return The dword read.
95 uint32_t memory_read_dword(uint32_t addr) throw();
97 /**
98 * \brief Read qword from memory
100 * This function reads eight bytes from memory.
102 * \param addr The address to read.
103 * \return The qword read.
105 uint64_t memory_read_qword(uint32_t addr) throw();
108 * \brief Write byte to memory
110 * This function writes one byte to memory.
112 * \param addr The address to write.
113 * \param data The value to write.
114 * \return true if the write succeeded.
116 bool memory_write_byte(uint32_t addr, uint8_t data) throw();
119 * \brief Write word to memory
121 * This function writes two bytes to memory.
123 * \param addr The address to write.
124 * \param data The value to write.
125 * \return true if the write succeeded.
127 bool memory_write_word(uint32_t addr, uint16_t data) throw();
130 * \brief Write dword to memory
132 * This function writes four bytes to memory.
134 * \param addr The address to write.
135 * \param data The value to write.
136 * \return true if the write succeeded.
138 bool memory_write_dword(uint32_t addr, uint32_t data) throw();
141 * \brief Write qword to memory
143 * This function writes eight bytes to memory.
145 * \param addr The address to write.
146 * \param data The value to write.
147 * \return true if the write succeeded.
149 bool memory_write_qword(uint32_t addr, uint64_t data) throw();
152 * \brief Memory search context
154 * Context for memory search. Each individual context is independent.
156 class memorysearch
158 public:
160 * \brief Create new memory search context.
162 * Creates a new memory search context with all addresses.
164 * \throws std::bad_alloc Not enough memory.
166 memorysearch() throw(std::bad_alloc);
169 * \brief Reset the context
171 * Reset the context so all addresses are candidates again.
173 * \throws std::bad_alloc Not enough memory.
175 void reset() throw(std::bad_alloc);
178 * \brief Search for address satisfying criteria
180 * This searches the memory space, leaving those addresses for which condition object returns true.
182 * \param obj The condition to search for.
184 template<class T>
185 void search(const T& obj) throw();
188 * \brief Search for byte with specified value
189 * \param value The value to search for
191 void byte_value(uint8_t value) throw();
193 * \brief Search for bytes that are signed less
195 void byte_slt() throw();
197 * \brief Search for bytes that are signed less or equal
199 void byte_sle() throw();
201 * \brief Search for bytes that are signed equal
203 void byte_seq() throw();
205 * \brief Search for bytes that are signed not equal
207 void byte_sne() throw();
209 * \brief Search for bytes that are signed greater or equal
211 void byte_sge() throw();
213 * \brief Search for bytes that are signed greater
215 void byte_sgt() throw();
217 * \brief Search for bytes that are unsigned less
219 void byte_ult() throw();
221 * \brief Search for bytes that are unsigned less or equal
223 void byte_ule() throw();
225 * \brief Search for bytes that are unsigned equal
227 void byte_ueq() throw();
229 * \brief Search for bytes that are unsigned not equal
231 void byte_une() throw();
233 * \brief Search for bytes that are unsigned greater or equal
235 void byte_uge() throw();
237 * \brief Search for bytes that are unsigned greater
239 void byte_ugt() throw();
242 * \brief Search for word with specified value
243 * \param value The value to search for
245 void word_value(uint16_t value) throw();
247 * \brief Search for words that are signed less
249 void word_slt() throw();
251 * \brief Search for words that are signed less or equal
253 void word_sle() throw();
255 * \brief Search for words that are signed equal
257 void word_seq() throw();
259 * \brief Search for words that are signed not equal
261 void word_sne() throw();
263 * \brief Search for words that are signed greater or equal
265 void word_sge() throw();
267 * \brief Search for words that are signed greater
269 void word_sgt() throw();
271 * \brief Search for words that are unsigned less
273 void word_ult() throw();
275 * \brief Search for words that are unsigned less or equal
277 void word_ule() throw();
279 * \brief Search for words that are unsigned equal
281 void word_ueq() throw();
283 * \brief Search for words that are unsigned not equal
285 void word_une() throw();
287 * \brief Search for words that are unsigned greater or equal
289 void word_uge() throw();
291 * \brief Search for words that are unsigned greater
293 void word_ugt() throw();
296 * \brief Search for dword with specified value
297 * \param value The value to search for
299 void dword_value(uint32_t value) throw();
301 * \brief Search for dwords that are signed less
303 void dword_slt() throw();
305 * \brief Search for dwords that are signed less or equal
307 void dword_sle() throw();
309 * \brief Search for dwords that are signed equal
311 void dword_seq() throw();
313 * \brief Search for dwords that are signed not equal
315 void dword_sne() throw();
317 * \brief Search for dwords that are signed greater or equal
319 void dword_sge() throw();
321 * \brief Search for dwords that are signed greater
323 void dword_sgt() throw();
325 * \brief Search for dwords that are unsigned less
327 void dword_ult() throw();
329 * \brief Search for dwords that are unsigned less or equal
331 void dword_ule() throw();
333 * \brief Search for dwords that are unsigned equal
335 void dword_ueq() throw();
337 * \brief Search for dwords that are unsigned not equal
339 void dword_une() throw();
341 * \brief Search for dwords that are unsigned greater or equal
343 void dword_uge() throw();
345 * \brief Search for dwords that are unsigned greater
347 void dword_ugt() throw();
350 * \brief Search for qword with specified value
351 * \param value The value to search for
353 void qword_value(uint64_t value) throw();
355 * \brief Search for qwords that are signed less
357 void qword_slt() throw();
359 * \brief Search for qwords that are signed less or equal
361 void qword_sle() throw();
363 * \brief Search for qwords that are signed equal
365 void qword_seq() throw();
367 * \brief Search for qwords that are signed not equal
369 void qword_sne() throw();
371 * \brief Search for qwords that are signed greater or equal
373 void qword_sge() throw();
375 * \brief Search for qwords that are signed greater
377 void qword_sgt() throw();
379 * \brief Search for qwords that are unsigned less
381 void qword_ult() throw();
383 * \brief Search for qwords that are unsigned less or equal
385 void qword_ule() throw();
387 * \brief Search for qwords that are unsigned equal
389 void qword_ueq() throw();
391 * \brief Search for qwords that are unsigned not equal
393 void qword_une() throw();
395 * \brief Search for qwords that are unsigned greater or equal
397 void qword_uge() throw();
399 * \brief Search for qwords that are unsigned greater
401 void qword_ugt() throw();
404 * \brief Get number of memory addresses that are still candidates
406 * This returns the number of memory addresses satisfying constraints so far.
408 * \return The number of candidates
410 uint32_t get_candidate_count() throw();
413 * \brief Get List of all candidate addresses
415 * Returns list of all candidates. This function isn't lazy, so be careful when calling with many candidates.
417 * \return Candidate address list
418 * \throws std::bad_alloc Not enough memory.
420 std::list<uint32_t> get_candidates() throw(std::bad_alloc);
421 private:
422 std::vector<uint8_t> previous_content;
423 std::vector<uint64_t> still_in;
424 uint32_t candidates;
427 #endif