Browse Source

Completed memory mapper and added ROM support.

master
Ian Burgmyer 4 years ago
parent
commit
9e4992d1c8
  1. 1
      CMakeLists.txt
  2. 47
      libplip/PlipMemoryMap.cpp
  3. 29
      libplip/PlipMemoryRom.cpp
  4. 24
      libplip/PlipMemoryRom.h
  5. 27
      plip-sdl/main.cpp

1
CMakeLists.txt

@ -37,6 +37,7 @@ add_library(${lib_name}
libplip/PlipInputDefinition.cpp
libplip/PlipMemoryMap.cpp
libplip/PlipMemoryRam.cpp
libplip/PlipMemoryRom.cpp
)
add_dependencies(${lib_name} GENERATE_LIB_VERSION_HEADER)

47
libplip/PlipMemoryMap.cpp

@ -34,7 +34,20 @@ namespace Plip {
}
void PlipMemoryMap::AssignBlockDirect(PlipMemory *memory, uint32_t address, uint32_t offset, uint32_t length) {
// TODO: Search for the appropriate spot and insert an entry.
auto it = m_range.begin();
auto end = m_range.end();
while(it != end) {
if(it->startAddress > address) break;
it++;
}
m_range.insert(it, {
.startAddress = address,
.memory = memory,
.offset = offset,
.length = length
});
}
std::tuple<PlipMemory*, uint32_t> PlipMemoryMap::FindAddress(uint32_t address) {
@ -119,8 +132,36 @@ namespace Plip {
case PartiallyInRange:
// Remove the portion of the block that overlaps.
foundBlock = true;
// TODO: Implement me.
auto blockEnd = it->startAddress + it->length - 1;
if(address <= it->startAddress) {
// Unassign range overlaps the beginning of the memory
// block. Move the starting address and offset forward,
// and bump the length back.
auto adjust = endAddress - it->startAddress + 1;
it->startAddress += adjust;
it->offset += adjust;
it->length -= adjust;
} else if(address > it->startAddress && endAddress >= blockEnd) {
// Unassign range overlaps the end of the memory block.
// Pull the block length back.
it->length -= endAddress - address + 1;
} else {
// Unassign range falls in the middle of this memory
// block. Truncate the block, then push in a new block
// with an adjusted offset/length.
it->length = address - it->startAddress;
auto newStart = endAddress + 1;
auto newLength = blockEnd - endAddress;
auto offset = endAddress - it->startAddress + 1;
AssignBlockDirect(it->memory, newStart, offset, newLength);
// There's no way there are any block beyond this, so
// break here.
it = end;
break;
}
it++;
continue;

29
libplip/PlipMemoryRom.cpp

@ -0,0 +1,29 @@
/* PlipMemoryRom.cpp
*
* A read-only memory implementation.
*/
#include <cstring>
#include "PlipMemoryRom.h"
namespace Plip {
PlipMemoryRom::PlipMemoryRom(void *data, uint32_t length) {
m_length = length;
m_data = new uint8_t[m_length];
std::memcpy(m_data, data, m_length);
}
PlipMemoryRom::~PlipMemoryRom() {
delete m_data;
}
uint8_t PlipMemoryRom::GetByte(uint32_t address) {
return m_data[address];
}
uint32_t PlipMemoryRom::GetLength() {
return m_length;
}
}

24
libplip/PlipMemoryRom.h

@ -0,0 +1,24 @@
/* PlipMemoryRom.h
*
* A read-only memory implementation.
*/
#pragma once
#include "PlipMemory.h"
namespace Plip {
class PlipMemoryRom : public PlipMemory {
public:
explicit PlipMemoryRom(void *data, uint32_t length);
~PlipMemoryRom();
uint8_t GetByte(uint32_t address) override;
uint32_t GetLength() override;
void SetByte(uint32_t address, uint8_t value) override {};
private:
uint32_t m_length;
uint8_t *m_data;
};
}

27
plip-sdl/main.cpp

@ -63,8 +63,6 @@ cxxopts::ParseResult parseCmdLine(int argc, char **argv) {
}
}
#include "PlipMemoryMap.h"
#include "PlipMemoryRam.h"
int main(int argc, char **argv) {
auto opts = parseCmdLine(argc, argv);
auto version = Plip::Plip::GetVersion();
@ -80,7 +78,6 @@ int main(int argc, char **argv) {
return 1;
}
/*
auto wnd = new PlipSdl::SdlWindow(opts["scale"].as<int>(), version);
auto event = new PlipSdl::SdlEvent();
auto plip = new Plip::Plip(event, wnd);
@ -92,30 +89,6 @@ int main(int argc, char **argv) {
#endif
gameLoop(plip, timer);
*/
auto memMap = new Plip::PlipMemoryMap();
auto ram1 = new Plip::PlipMemoryRam(0x2000);
auto ram2 = new Plip::PlipMemoryRam(0x2000);
auto ram3 = new Plip::PlipMemoryRam(0x1000);
auto ram4 = new Plip::PlipMemoryRam(0x1000);
auto ram5 = new Plip::PlipMemoryRam(0x2000);
auto ram6 = new Plip::PlipMemoryRam(0x2000);
auto ram7 = new Plip::PlipMemoryRam(0x2000);
auto ram8 = new Plip::PlipMemoryRam(0x4000);
memMap->AddBlock(ram1); // 0x0000 - 0x1FFF
memMap->AddBlock(ram2); // 0x2000 - 0x3FFF
memMap->AddBlock(ram3); // 0x4000 - 0x4FFF
memMap->AddBlock(ram4); // 0x5000 - 0x5FFF
memMap->AddBlock(ram5); // 0x6000 - 0x7FFF
memMap->AddBlock(ram6); // 0x8000 - 0x9FFF
memMap->AddBlock(ram7); // 0xA000 - 0xBFFF
memMap->AddBlock(ram8); // 0xC000 - 0xFFFF
memMap->AssignBlock(ram4, 0x1000, 0x0000, 0x1000);
memMap->UnassignBlock(0x4000, 0x800);
std::cout << "Map length: " << memMap->GetLength() << std::endl;
return 0;
}

Loading…
Cancel
Save