Skip to content

Instantly share code, notes, and snippets.

@pedrovanzella
Created February 26, 2018 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pedrovanzella/808ba39310f3b21a7e099b18bf6c1bd5 to your computer and use it in GitHub Desktop.
Save pedrovanzella/808ba39310f3b21a7e099b18bf6c1bd5 to your computer and use it in GitHub Desktop.
operator[]
class ROM {
public:
ROM(std::array<uint8_t, 1024> buf) { _buffer = std::move(buf); }
const uint16_t operator[](const uint16_t offset) const {
if (offset > _buffer.size()) {
return 0x0;
}
// This is a big-endian machine, indexed by 16-bit words
return (_buffer[offset] << 8) + (_buffer[offset + 1]);
}
private:
std::array<uint8_t, 1024> _buffer;
}
class Memory {
public:
Memory(ROM rom) {
_ram.fill(0);
_rom = std::move(rom);
}
// operator[]??
// Say we have an instance called mem
// mem[0x200] should return _rom[0x0] and be read-only
// mem[0x602] or higher should return _ram[0x602] or higher and be read-write.
private:
ROM _rom;
std::array<uint8_t, 2560> _ram;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment