Browse Source

Emulator: Add support for memory banks to memory spaces

master
Riyyi 2 years ago
parent
commit
42e54f1344
  1. 34
      src/emu.cpp
  2. 17
      src/emu.h

34
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<uint32_t> 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<uint32_t>(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;
}

17
src/emu.h

@ -17,6 +17,15 @@
#include "ruc/singleton.h"
#include "ruc/timer.h"
using BankedMemory = std::vector<std::vector<uint32_t>>;
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<Emu> {
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<std::string_view, ProcessingUnit*> m_processing_units;
std::unordered_map<std::string_view, std::vector<uint32_t>> m_memory_spaces;
std::unordered_map<std::string_view, MemorySpace> m_memory_spaces;
std::string m_bootrom;
};

Loading…
Cancel
Save