Make MainMemory and extension of std::unordered_map
DELETE
src/mainmemory.cpp
-#include "mainmemory.h"
-
-MainMemory::MainMemory() {}
-
-void MainMemory::write(uint32_t address, uint32_t value, int size) {
- // writes value to from the given address start, and up to $size bytes of
- // $value
- // Using the hashtable, new allocations will automatically be handled
- for (int i = 0; i < size; i++) {
- m_memory[address + i] = value & 0xff;
- value >>= 8;
- }
-}
-
-uint32_t MainMemory::read(uint32_t address) {
- // Note: If address is not found in memory map, a default constructed object
- // will be created, and read. in our case uint8_t() = 0
- uint32_t read = (m_memory[address] | (m_memory[address + 1] << 8) | (m_memory[address + 2] << 16) |
- (m_memory[address + 3] << 24));
- return read;
-}

MODIFY
src/mainmemory.h
src/mainmemory.h
#include "defines.h"
-class MainMemory {
+template <typename key_T, typename value_T>
+class MainMemoryTemplate : public std::unordered_map<key_T, value_T> {
public:
- explicit MainMemory();
-
- void write(uint32_t address, uint32_t value, int size);
- uint32_t read(uint32_t addess);
- void clear() { m_memory.clear(); }
-
-private:
- memory m_memory;
+ void write(uint32_t address, uint32_t value, int size) {
+ // writes value to from the given address start, and up to $size bytes of
+ // $value
+ // Using the hashtable, new allocations will automatically be handled
+ for (int i = 0; i < size; i++) {
+ (*this)[address + i] = value & 0xff;
+ value >>= 8;
+ }
+ }
+ uint32_t read(uint32_t address) {
+ // Note: If address is not found in memory map, a default constructed object
+ // will be created, and read. in our case uint8_t() = 0
+ uint32_t read = ((*this)[address] | ((*this)[address + 1] << 8) | ((*this)[address + 2] << 16) |
+ ((*this)[address + 3] << 24));
+ return read;
+ }
+ void reset(int textSize) {
+ // Reset memory - erase all elements which is not part of the textsegment of the program
+ for (auto it = this->begin(); it != this->end();) {
+ // Use standard associative-container erasing. If erased, set iterator to iterator after the erased value
+ // (default return value from .erase), else, increment iterator;
+ if (it->first > textSize) {
+ it = this->erase(it);
+ } else {
+ ++it;
+ }
+ }
+ }
};
+typedef MainMemoryTemplate<uint32_t, uint8_t> MainMemory;
+
#endif // MAiNMEMORY_H

Make MainMemory and extension of std::unordered_map