diff --git a/src/loader.cpp b/src/loader.cpp index 69a5a3d..7c8a823 100644 --- a/src/loader.cpp +++ b/src/loader.cpp @@ -4,11 +4,25 @@ * SPDX-License-Identifier: MIT */ -#include "loader.h" +#include // size_t + #include "cpu.h" #include "emu.h" +#include "loader.h" #include "ppu.h" #include "ruc/file.h" +#include "ruc/format/print.h" + +void Loader::loadRom(std::string_view rom_path) +{ + if (!rom_path.empty()) { + m_rom_data = ruc::File(rom_path).data(); + } + + init(); +} + +// ----------------------------------------- void Loader::init() { @@ -24,7 +38,8 @@ void Loader::init() // https://gbdev.io/pandocs/Memory_Map.html // https://gbdev.io/pandocs/Power_Up_Sequence.html - Emu::the().addMemorySpace("BOOTROM1", 0x0000, 0x00ff); // 256B + Emu::the().addMemorySpace("BOOTROM1", 0x0000, 0x00ff); // 256B + loadCartridgeHeader(); Emu::the().addMemorySpace("BOOTROM2", 0x0200, 0x08ff); // 1792B Emu::the().addMemorySpace("VRAM", 0x8000, 0x9fff, 2); // 8KiB * 2 banks Emu::the().addMemorySpace("CARDRAM", 0xa000, 0xbfff, 1); // 8KiB * ? banks, if any @@ -48,6 +63,11 @@ void Loader::init() Emu::the().writeMemory(i, bootrom[i]); } + update(); +} + +void Loader::update() +{ while (true) { Emu::the().update(); } @@ -57,3 +77,16 @@ void Loader::destroy() { Emu::destroy(); } + +void Loader::loadCartridgeHeader() +{ + if (m_rom_data.empty()) { + return; + } + + Emu::the().addMemorySpace("CARTHEADER", 0x100, 0x14f); + + for (size_t i = 0x100; i <= 0x14f; ++i) { + Emu::the().writeMemory(i, m_rom_data[i]); + } +} diff --git a/src/loader.h b/src/loader.h index 633388b..45213d0 100644 --- a/src/loader.h +++ b/src/loader.h @@ -6,18 +6,23 @@ #pragma once -#include "ruc/singleton.h" +#include #include +#include "ruc/singleton.h" + class Loader final : public ruc::Singleton { public: Loader(s) {} + void loadRom(std::string_view rom_path); + +private: void init(); + void update(); void destroy(); - void setRomPath(std::string_view rom_path) { m_rom_path = rom_path; } + void loadCartridgeHeader(); -private: - std::string_view m_rom_path; + std::string m_rom_data; }; diff --git a/src/main.cpp b/src/main.cpp index b409f12..3b5939f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,8 +19,7 @@ int main(int argc, const char* argv[]) argParser.addOption(rom_path, 'r', "rom", nullptr, nullptr, "", ruc::ArgParser::Required::Yes); argParser.parse(argc, argv); - Loader::the().setRomPath(rom_path); - Loader::the().init(); + Loader::the().loadRom(rom_path); return 0; }