Browse Source

Emulator: Remove bootrom as separate Emu member variable

master
Riyyi 2 years ago
parent
commit
8768daabfe
  1. 18
      src/cpu.cpp
  2. 6
      src/cpu.h
  3. 4
      src/emu.cpp
  4. 6
      src/emu.h
  5. 8
      src/main.cpp

18
src/cpu.cpp

@ -23,7 +23,6 @@ CPU::CPU(uint32_t frequency)
, m_e(0x56) , m_e(0x56)
, m_h(0x0) , m_h(0x0)
, m_l(0x0d) , m_l(0x0d)
// , m_pc(0x100)
, m_pc(0x0) , m_pc(0x0)
, m_sp(0xfffe) , m_sp(0xfffe)
// Flags // Flags
@ -65,7 +64,8 @@ void CPU::update()
m_wait_cycles--; m_wait_cycles--;
if (m_wait_cycles <= 0) { if (m_wait_cycles <= 0) {
// Read next opcode // 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](); m_opcode_lookup_table[opcode]();
} }
@ -74,8 +74,8 @@ void CPU::update()
void CPU::add() void CPU::add()
{ {
uint8_t opcode = consumeBootrom(); uint8_t opcode = consumeMemory();
uint8_t immediate = consumeBootrom(); uint8_t immediate = consumeMemory();
switch (opcode) { switch (opcode) {
case 0xc6: // ADD A,d8 case 0xc6: // ADD A,d8
// Program counter +2 // Program counter +2
@ -103,7 +103,7 @@ void CPU::add()
void CPU::ld16() void CPU::ld16()
{ {
uint8_t opcode = consumeBootrom(); uint8_t opcode = consumeMemory();
switch (opcode) { switch (opcode) {
case 0x01: { case 0x01: {
m_wait_cycles += 12; 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) void CPU::setBc(uint32_t value)

6
src/cpu.h

@ -52,10 +52,10 @@ public:
// Miscellaneous Instructions // Miscellaneous Instructions
private: private:
uint8_t peekBootrom(int32_t offset = 0) const; uint8_t peekMemory(int32_t offset = 0) const;
uint8_t consumeBootrom(); uint8_t consumeMemory();
uint32_t immediate16() { return (consumeBootrom() << 8) | consumeBootrom(); } uint32_t immediate16() { return (consumeMemory() << 8) | consumeMemory(); }
void setBc(uint32_t value); void setBc(uint32_t value);
void setDe(uint32_t value); void setDe(uint32_t value);

4
src/emu.cpp

@ -10,15 +10,13 @@
#include "cpu.h" #include "cpu.h"
#include "emu.h" #include "emu.h"
#include "ruc/file.h"
#include "ruc/format/print.h" #include "ruc/format/print.h"
#include "ruc/meta/assert.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_frequency = frequency;
m_timestep = 1.0 / m_frequency * 1000000; m_timestep = 1.0 / m_frequency * 1000000;
m_bootrom = ruc::File(bootrom.data()).data();
} }
void Emu::update() void Emu::update()

6
src/emu.h

@ -30,7 +30,7 @@ class Emu final : public ruc::Singleton<Emu> {
public: public:
Emu(s) {} Emu(s) {}
void init(uint32_t frequency, std::string_view bootrom); void init(uint32_t frequency);
void update(); void update();
@ -44,8 +44,6 @@ public:
ProcessingUnit* processingUnit(std::string_view name) const { return m_processing_units.at(name); } ProcessingUnit* processingUnit(std::string_view name) const { return m_processing_units.at(name); }
std::string_view bootrom() const { return m_bootrom; }
private: private:
uint32_t m_frequency { 0 }; uint32_t m_frequency { 0 };
double m_timestep { 0 }; double m_timestep { 0 };
@ -57,6 +55,4 @@ private:
std::unordered_map<std::string_view, ProcessingUnit*> m_processing_units; std::unordered_map<std::string_view, ProcessingUnit*> m_processing_units;
std::unordered_map<std::string_view, MemorySpace> m_memory_spaces; std::unordered_map<std::string_view, MemorySpace> m_memory_spaces;
std::string m_bootrom;
}; };

8
src/main.cpp

@ -20,7 +20,7 @@ int main(int argc, char* argv[])
print("{}ms\n", t.elapsedNanoseconds() / 1000000.0); print("{}ms\n", t.elapsedNanoseconds() / 1000000.0);
Emu::the().init(8000000, "gbc_bios.bin"); Emu::the().init(8000000);
CPU cpu(8000000); CPU cpu(8000000);
PPU ppu(4000000); PPU ppu(4000000);
@ -36,9 +36,9 @@ int main(int argc, char* argv[])
Emu::the().addMemorySpace("CARDRAM", 0xa000, 0xbfff); // 8KiB Emu::the().addMemorySpace("CARDRAM", 0xa000, 0xbfff); // 8KiB
Emu::the().addMemorySpace("WRAM1", 0xc000, 0xcfff, 1); // 4 KiB, Work RAM 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("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("ECHORAM", 0xe000, 0xfdff, 1); // 7680B, Mirror of 0xc000~0xddff
Emu::the().addMemorySpace("OAM", 0xfe00, 0xfe9f, 1); // 4KiB, Object Attribute Memory (VRAM Sprite Attribute Table) Emu::the().addMemorySpace("OAM", 0xfe00, 0xfe9f, 1); // 160B, Object Attribute Memory (VRAM Sprite Attribute Table)
Emu::the().addMemorySpace("HRAM", 0xff80, 0xfffe, 1); // 126B, High RAM (CPU cache) Emu::the().addMemorySpace("HRAM", 0xff80, 0xfffe, 1); // 127B, High RAM (CPU cache)
// Get shared register // Get shared register
print("{}\n", *Emu::the().processingUnit("cpu")->sharedRegister("a")); print("{}\n", *Emu::the().processingUnit("cpu")->sharedRegister("a"));

Loading…
Cancel
Save