Browse Source

Emulator: Add opcode lookup table

master
Riyyi 2 years ago
parent
commit
d511ec8397
  1. 30
      src/cpu.cpp
  2. 8
      src/cpu.h

30
src/cpu.cpp

@ -8,6 +8,7 @@
#include <cstdint> // uint8_t, uint32_t #include <cstdint> // uint8_t, uint32_t
#include "cpu.h" #include "cpu.h"
#include "emu.h"
#include "ruc/format/print.h" #include "ruc/format/print.h"
#include "ruc/meta/assert.h" #include "ruc/meta/assert.h"
@ -22,7 +23,8 @@ 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(0x100)
, m_pc(0x0)
, m_sp(0xffe) , m_sp(0xffe)
// Flags // Flags
, m_zf(0x1) , m_zf(0x1)
@ -43,6 +45,12 @@ CPU::CPU(uint32_t frequency)
m_shared_registers.emplace("nf", &m_nf); m_shared_registers.emplace("nf", &m_nf);
m_shared_registers.emplace("hf", &m_hf); m_shared_registers.emplace("hf", &m_hf);
m_shared_registers.emplace("cf", &m_cf); m_shared_registers.emplace("cf", &m_cf);
// Add opcode functions to lookup table
m_opcode_lookup_table.emplace(0x08, std::bind(&CPU::ldStack, this));
m_opcode_lookup_table.emplace(0x31, std::bind(&CPU::ldStack, this));
m_opcode_lookup_table.emplace(0xf8, std::bind(&CPU::ldStack, this));
m_opcode_lookup_table.emplace(0xf9, std::bind(&CPU::ldStack, this));
} }
CPU::~CPU() CPU::~CPU()
@ -54,6 +62,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 = Emu::the().bootrom()[m_pc];
m_opcode_lookup_table[opcode]();
} }
// print("This is an update from the CPU\n"); // print("This is an update from the CPU\n");
@ -83,6 +93,24 @@ void CPU::add(uint8_t opcode, uint8_t immediate)
break; break;
default: default:
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
}
}
void CPU::ldStack()
{
printf("Calling stack LD\n");
uint8_t opcode = Emu::the().bootrom()[m_pc];
switch (opcode) {
case 0x08:
break;
case 0x31:
break;
case 0xf8:
break; break;
case 0xf9:
break;
default:
VERIFY_NOT_REACHED();
} }
} }

8
src/cpu.h

@ -7,7 +7,9 @@
#pragma once #pragma once
#include <cstdint> // int8_t, uint8_t, uint32_t #include <cstdint> // int8_t, uint8_t, uint32_t
#include <functional> // function
#include <unordered_map>
#include "processing-unit.h" #include "processing-unit.h"
@ -34,6 +36,8 @@ public:
// Stack Operations Instructions // Stack Operations Instructions
void ldStack();
// Miscellaneous Instructions // Miscellaneous Instructions
private: private:
@ -55,4 +59,6 @@ private:
uint32_t m_cf { 0 }; // Carry flag uint32_t m_cf { 0 }; // Carry flag
int8_t m_wait_cycles { 0 }; int8_t m_wait_cycles { 0 };
std::unordered_map<uint32_t, std::function<void()>> m_opcode_lookup_table;
}; };

Loading…
Cancel
Save