diff --git a/main.cpp b/main.cpp index 00241e4..6d5afbc 100644 --- a/main.cpp +++ b/main.cpp @@ -16,6 +16,5 @@ int main(int argc, char **argv) } Memory::MemoryBus bus; SNES snes(std::make_shared(bus), argv[1]); - bus.mapComponents(snes); return 0; } \ No newline at end of file diff --git a/sources/Cartridge/Cartridge.cpp b/sources/Cartridge/Cartridge.cpp index fcc39eb..7954177 100644 --- a/sources/Cartridge/Cartridge.cpp +++ b/sources/Cartridge/Cartridge.cpp @@ -49,7 +49,7 @@ namespace ComSquare::Cartridge { if (addr >= this->_size) throw InvalidAddress("Cartridge read", addr); - return this->_data[addr]; + return this->_data[addr + this->_romStart]; } void Cartridge::write_internal(uint24_t addr, uint8_t data) @@ -109,7 +109,7 @@ namespace ComSquare::Cartridge uint32_t Cartridge::_getHeaderAddress() { - std::vector address = {0x7FC0, 0xFFC0, 0x81C0, 0x101C0}; + const std::vector address = {0x7FC0, 0xFFC0, 0x81C0, 0x101C0}; int bestScore = -1; uint32_t bestAddress = 0; @@ -176,6 +176,11 @@ namespace ComSquare::Cartridge std::memcpy(name, &this->_data[headerAddress + 0xC0u], 21); name[21] = '\0'; this->header.gameName = std::string(name); - return headerAddress & 0x200u; + if (headerAddress & 0x200u) { + this->_romStart = 0x200u; + this->_size -= 0x200u; + return true; + } + return false; } } \ No newline at end of file diff --git a/sources/Cartridge/Cartridge.hpp b/sources/Cartridge/Cartridge.hpp index c9717ae..a48d513 100644 --- a/sources/Cartridge/Cartridge.hpp +++ b/sources/Cartridge/Cartridge.hpp @@ -60,12 +60,14 @@ namespace ComSquare::Cartridge }; //! @brief Contains the rom's memory/instructions. - class Cartridge : Memory::IRectangleMemory { + class Cartridge : public Memory::IRectangleMemory { private: //! @brief The rom data (contains all the instructions). uint8_t *_data; //! @brief The size of the rom data. size_t _size; + //! @brief Sometime the rom's data has an offset for a SMC header. This value indicate the start of the real rom discarding this header. + uint16_t _romStart; //! @brief Get the size of a rom from it's path. //! @param romPath The path of the rom to get info from. diff --git a/sources/Memory/MemoryBus.cpp b/sources/Memory/MemoryBus.cpp index 28e4245..804c906 100644 --- a/sources/Memory/MemoryBus.cpp +++ b/sources/Memory/MemoryBus.cpp @@ -79,5 +79,10 @@ namespace ComSquare::Memory this->_mirrorComponents(console, i); // TODO should map SRam, cartridge etc via the mapping mode of the cartridge. + + if (console.cartridge->header.mappingMode & Cartridge::LoRom) { + console.cartridge->setMemoryRegion(0x80, 0xFF, 0x8000, 0xFFFF); + this->_memoryAccessors.push_back(console.cartridge); + } } } \ No newline at end of file diff --git a/sources/SNES.cpp b/sources/SNES.cpp index 8e864dd..63bcb08 100644 --- a/sources/SNES.cpp +++ b/sources/SNES.cpp @@ -12,5 +12,7 @@ namespace ComSquare apu(new APU::APU()), cartridge(new Cartridge::Cartridge(romPath)), wram(new Ram::Ram(16384)) - { } + { + bus->mapComponents(*this); + } }