diff --git a/src/emu.cpp b/src/emu.cpp index f3b6d85..24e6b9b 100644 --- a/src/emu.cpp +++ b/src/emu.cpp @@ -41,18 +41,38 @@ void Emu::addProcessingUnit(std::string_view name, ProcessingUnit* processing_un m_processing_units.emplace(name, processing_unit); } -void Emu::addMemorySpace(std::string_view name, uint32_t size) +void Emu::addMemorySpace(std::string_view name, uint32_t start_address, uint32_t end_adress, uint32_t amount_of_banks) { - std::vector memory(size); - m_memory_spaces.emplace(name, memory); + uint32_t bank_length = 1 + end_adress - start_address; + MemorySpace memory_space { + .memory = BankedMemory(amount_of_banks, std::vector(bank_length)), + .active_bank = 0, + .start_address = start_address, + .end_address = end_adress, + }; + + m_memory_spaces.emplace(name, memory_space); } -void Emu::writeMemory(std::string_view memory_space, uint32_t location, uint32_t value) +void Emu::writeMemory(uint32_t address, uint32_t value) { - m_memory_spaces[memory_space][location] = value; + for (auto& memory_space : m_memory_spaces) { + auto memory = memory_space.second; + if (address >= memory.start_address && address <= memory.end_address) { + memory.memory[memory.active_bank][address] = value; + } + } } -uint32_t Emu::readMemory(std::string_view memory_space, uint32_t location) +uint32_t Emu::readMemory(uint32_t address) { - return m_memory_spaces[memory_space][location]; + for (auto& memory_space : m_memory_spaces) { + auto memory = memory_space.second; + if (address >= memory.start_address && address <= memory.end_address) { + return memory.memory[memory.active_bank][address]; + break; + } + } + + return 0; } diff --git a/src/emu.h b/src/emu.h index d7623cc..de8ebbf 100644 --- a/src/emu.h +++ b/src/emu.h @@ -17,6 +17,15 @@ #include "ruc/singleton.h" #include "ruc/timer.h" +using BankedMemory = std::vector>; + +struct MemorySpace { + BankedMemory memory; + uint32_t active_bank { 0 }; + uint32_t start_address { 0 }; + uint32_t end_address { 0 }; +}; + class Emu final : public ruc::Singleton { public: Emu(s) {} @@ -26,10 +35,10 @@ public: void update(); void addProcessingUnit(std::string_view name, ProcessingUnit* processing_unit); - void addMemorySpace(std::string_view name, uint32_t size); + void addMemorySpace(std::string_view name, uint32_t start_address, uint32_t end_address, uint32_t amount_of_banks = 1); - void writeMemory(std::string_view memory_space, uint32_t location, uint32_t value); - uint32_t readMemory(std::string_view memory_space, uint32_t location); + void writeMemory(uint32_t address, uint32_t value); + uint32_t readMemory(uint32_t address); // ------------------------------------- @@ -47,7 +56,7 @@ private: ruc::Timer m_timer; std::unordered_map m_processing_units; - std::unordered_map> m_memory_spaces; + std::unordered_map m_memory_spaces; std::string m_bootrom; };