Save bitmaps as PNG
[lsnes.git] / include / library / memoryspace.hpp
blob403688054a039ef4871e98fb28f14c01530b5a5d
1 #ifndef _library__memoryspace__hpp__included__
2 #define _library__memoryspace__hpp__included__
4 #include <string>
5 #include <list>
6 #include <vector>
7 #include <cstdint>
8 #include <cstring>
9 #include "threadtypes.hpp"
10 #include "arch-detect.hpp"
12 /**
13 * Information about region of memory.
15 struct memory_region
17 /**
18 * Destructor.
20 virtual ~memory_region() throw();
21 /**
22 * Name of the region (mainly for debugging and showing to user).
24 std::string name;
25 /**
26 * Base address of the region.
28 uint64_t base;
29 /**
30 * Size of the region.
32 uint64_t size;
33 /**
34 * Last address in region.
36 uint64_t last_address() const throw() { return base + size - 1; }
37 /**
38 * Endianess of region (-1 => little, 0 => host, 1 => big).
40 int endian;
41 /**
42 * Read-only flag.
44 bool readonly;
45 /**
46 * Special flag.
48 * Signals that this region is not RAM/ROM-like, but is special I/O region where reads and writes might not be
49 * consistent.
51 bool special;
52 /**
53 * Direct mapping for the region. If not NULL, read/write will not be used, instead all operations directly
54 * manipulate this buffer (faster). Must be NULL for special regions.
56 unsigned char* direct_map;
57 /**
58 * Read from region.
60 * Parameter offset: Offset to start the read.
61 * Parameter buffer: Buffer to store the data to.
62 * Parameter tsize: Amount to read.
64 * Note: The default implementation reads from direct_map.
65 * Note: Needs to be overridden if direct_map is NULL.
66 * Note: Must be able to read the whole region at once.
68 virtual void read(uint64_t offset, void* buffer, size_t tsize);
69 /**
70 * Write to region (writes to readonly regions are ignored).
72 * Parameter offset: Offset to start the write.
73 * Parameter buffer: Buffer to read the data from.
74 * Parameter tsize: Amount to write.
75 * Returns: True on success, false on failure.
77 * Note: The default implementation writes to direct_map if available and readwrite. Otherwise fails.
78 * Note: Must be able to write the whole region at once.
80 virtual bool write(uint64_t offset, const void* buffer, size_t tsize);
83 /**
84 * Direct-mapped region.
86 struct memory_region_direct : public memory_region
88 /**
89 * Create new direct-mapped region.
91 * Parameter name: Name of the region.
92 * Parameter base: Base address of the region.
93 * Parameter endian: Endianess of the region.
94 * Parameter memory: Memory backing the region.
95 * Parameter size: Size of the region.
96 * Parameter _readonly: If true, region is readonly.
98 memory_region_direct(const std::string& name, uint64_t base, int endian, unsigned char* memory,
99 size_t size, bool _readonly = false);
101 * Destructor.
103 ~memory_region_direct() throw();
107 * A whole memory space.
109 class memory_space
111 public:
113 * Get system endianess.
115 #ifdef ARCH_IS_I386
116 static int get_system_endian() throw() { return -1; }
117 #else
118 static int get_system_endian() throw() { if(!sysendian) sysendian = _get_system_endian(); return sysendian; }
119 #endif
121 * Do native unaligned reads work?
123 #ifdef ARCH_IS_I386
124 static int can_read_unaligned() throw() { return true; }
125 #else
126 static int can_read_unaligned() throw() { return false; }
127 #endif
129 * Lookup region covering address.
131 * Parameter address: The address to look up.
132 * Returns: The region/offset pair, or NULL/0 if that address is unmapped.
134 std::pair<memory_region*, uint64_t> lookup(uint64_t address);
136 * Lookup region covering linear address.
138 * Parameter linear: The linear address to look up.
139 * Returns: The region/offset pair, or NULL/0 if that address is unmapped.
141 std::pair<memory_region*, uint64_t> lookup_linear(uint64_t linear);
143 * Lookup region with specified index..
145 * Parameter n: The index to look up.
146 * Returns: The region, or NULL if index is invalid.
148 memory_region* lookup_n(size_t n);
150 * Get number of regions.
152 size_t get_region_count() { return u_regions.size(); }
154 * Get linear RAM size.
156 * Returns: The linear RAM size in bytes.
158 uint64_t get_linear_size() { return linear_size; }
160 * Get list of all regions in memory space.
162 std::list<memory_region*> get_regions();
164 * Set list of all regions in memory space.
166 void set_regions(const std::list<memory_region*>& regions);
168 * Read an element (primitive type) from memory.
170 * Parameter address: The address to read.
171 * Returns: The read value.
173 template<typename T> T read(uint64_t address);
175 * Write an element (primitive type) to memory.
177 * Parameter address: The address to write.
178 * Parameter value: The value to write.
179 * Returns: True on success, false on failure.
181 template<typename T> bool write(uint64_t address, T value);
183 * Read a byte range (not across regions).
185 * Parameter address: Base address to start the read from.
186 * Parameter buffer: Buffer to store the data to.
187 * Parameter bsize: Size of buffer.
189 void read_range(uint64_t address, void* buffer, size_t bsize);
191 * Write a byte range (not across regions).
193 * Parameter address: Base address to start the write from.
194 * Parameter buffer: Buffer to read the data from.
195 * Parameter bsize: Size of buffer.
196 * Returns: True on success, false on failure.
198 bool write_range(uint64_t address, const void* buffer, size_t bsize);
200 * Read an element (primitive type) from memory.
202 * Parameter linear: The linear address to read.
203 * Returns: The read value.
205 template<typename T> T read_linear(uint64_t linear);
207 * Write an element (primitive type) to memory.
209 * Parameter linear: The linear address to write.
210 * Parameter value: The value to write.
211 * Returns: True on success, false on failure.
213 template<typename T> bool write_linear(uint64_t linear, T value);
215 * Read a byte range (not across regions).
217 * Parameter linear: Base linear address to start the read from.
218 * Parameter buffer: Buffer to store the data to.
219 * Parameter bsize: Size of buffer.
221 void read_range_linear(uint64_t linear, void* buffer, size_t bsize);
223 * Write a byte range (not across regions).
225 * Parameter linear: Base linear address to start the write from.
226 * Parameter buffer: Buffer to read the data from.
227 * Parameter bsize: Size of buffer.
228 * Returns: True on success, false on failure.
230 bool write_range_linear(uint64_t linear, const void* buffer, size_t bsize);
232 * Read complete linear memory.
234 * Parameter buffer: Buffer to store to (get_linear_size() bytes).
236 void read_all_linear_memory(uint8_t* buffer);
237 private:
238 mutex_class mutex;
239 std::vector<memory_region*> u_regions;
240 std::vector<memory_region*> u_lregions;
241 std::vector<uint64_t> linear_bases;
242 uint64_t linear_size;
243 static int _get_system_endian();
244 static int sysendian;
248 #endif