diff --git a/src/cpu.cpp b/src/cpu.cpp index f511501..f626793 100644 --- a/src/cpu.cpp +++ b/src/cpu.cpp @@ -23,7 +23,6 @@ CPU::CPU(uint32_t frequency) , m_e(0x56) , m_h(0x0) , m_l(0x0d) - // , m_pc(0x100) , m_pc(0x0) , m_sp(0xfffe) // Flags @@ -65,7 +64,8 @@ void CPU::update() m_wait_cycles--; if (m_wait_cycles <= 0) { // Read next opcode - uint8_t opcode = peekBootrom(); + uint8_t opcode = peekMemory(); + VERIFY(m_opcode_lookup_table.find(opcode) != m_opcode_lookup_table.end(), "opcode {:#x} not implemented", opcode); m_opcode_lookup_table[opcode](); } @@ -74,8 +74,8 @@ void CPU::update() void CPU::add() { - uint8_t opcode = consumeBootrom(); - uint8_t immediate = consumeBootrom(); + uint8_t opcode = consumeMemory(); + uint8_t immediate = consumeMemory(); switch (opcode) { case 0xc6: // ADD A,d8 // Program counter +2 @@ -103,7 +103,7 @@ void CPU::add() void CPU::ld16() { - uint8_t opcode = consumeBootrom(); + uint8_t opcode = consumeMemory(); switch (opcode) { case 0x01: { m_wait_cycles += 12; @@ -130,14 +130,14 @@ void CPU::ld16() // ----------------------------------------- -uint8_t CPU::peekBootrom(int32_t offset) const +uint8_t CPU::peekMemory(int32_t offset) const { - return Emu::the().bootrom()[m_pc + offset]; + return Emu::the().readMemory(m_pc + offset); } -uint8_t CPU::consumeBootrom() +uint8_t CPU::consumeMemory() { - return Emu::the().bootrom()[m_pc++]; + return Emu::the().readMemory(m_pc++); } void CPU::setBc(uint32_t value) diff --git a/src/cpu.h b/src/cpu.h index b5a5a45..c10bde5 100644 --- a/src/cpu.h +++ b/src/cpu.h @@ -52,10 +52,10 @@ public: // Miscellaneous Instructions private: - uint8_t peekBootrom(int32_t offset = 0) const; - uint8_t consumeBootrom(); + uint8_t peekMemory(int32_t offset = 0) const; + uint8_t consumeMemory(); - uint32_t immediate16() { return (consumeBootrom() << 8) | consumeBootrom(); } + uint32_t immediate16() { return (consumeMemory() << 8) | consumeMemory(); } void setBc(uint32_t value); void setDe(uint32_t value); diff --git a/src/emu.cpp b/src/emu.cpp index eb6d614..f32f475 100644 --- a/src/emu.cpp +++ b/src/emu.cpp @@ -10,15 +10,13 @@ #include "cpu.h" #include "emu.h" -#include "ruc/file.h" #include "ruc/format/print.h" #include "ruc/meta/assert.h" -void Emu::init(uint32_t frequency, std::string_view bootrom) +void Emu::init(uint32_t frequency) { m_frequency = frequency; m_timestep = 1.0 / m_frequency * 1000000; - m_bootrom = ruc::File(bootrom.data()).data(); } void Emu::update() diff --git a/src/emu.h b/src/emu.h index b6dab9d..5a5f5d4 100644 --- a/src/emu.h +++ b/src/emu.h @@ -30,7 +30,7 @@ class Emu final : public ruc::Singleton { public: Emu(s) {} - void init(uint32_t frequency, std::string_view bootrom); + void init(uint32_t frequency); void update(); @@ -44,8 +44,6 @@ public: ProcessingUnit* processingUnit(std::string_view name) const { return m_processing_units.at(name); } - std::string_view bootrom() const { return m_bootrom; } - private: uint32_t m_frequency { 0 }; double m_timestep { 0 }; @@ -57,6 +55,4 @@ private: std::unordered_map m_processing_units; std::unordered_map m_memory_spaces; - - std::string m_bootrom; }; diff --git a/src/main.cpp b/src/main.cpp index f381970..36bc351 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,7 +20,7 @@ int main(int argc, char* argv[]) print("{}ms\n", t.elapsedNanoseconds() / 1000000.0); - Emu::the().init(8000000, "gbc_bios.bin"); + Emu::the().init(8000000); CPU cpu(8000000); PPU ppu(4000000); @@ -36,9 +36,9 @@ int main(int argc, char* argv[]) Emu::the().addMemorySpace("CARDRAM", 0xa000, 0xbfff); // 8KiB Emu::the().addMemorySpace("WRAM1", 0xc000, 0xcfff, 1); // 4 KiB, Work RAM Emu::the().addMemorySpace("WRAM2", 0xd000, 0xdfff, 7); // 4 KiB * 7 banks, Work RAM - Emu::the().addMemorySpace("ECHORAM", 0xe000, 0xfdff, 1); // 7679B, Mirror of 0xc000~0xddff - Emu::the().addMemorySpace("OAM", 0xfe00, 0xfe9f, 1); // 4KiB, Object Attribute Memory (VRAM Sprite Attribute Table) - Emu::the().addMemorySpace("HRAM", 0xff80, 0xfffe, 1); // 126B, High RAM (CPU cache) + Emu::the().addMemorySpace("ECHORAM", 0xe000, 0xfdff, 1); // 7680B, Mirror of 0xc000~0xddff + Emu::the().addMemorySpace("OAM", 0xfe00, 0xfe9f, 1); // 160B, Object Attribute Memory (VRAM Sprite Attribute Table) + Emu::the().addMemorySpace("HRAM", 0xff80, 0xfffe, 1); // 127B, High RAM (CPU cache) // Get shared register print("{}\n", *Emu::the().processingUnit("cpu")->sharedRegister("a"));