Compare commits

...
2 Commits
Author SHA1 Message Date
Riyyi d511ec8397 Emulator: Add opcode lookup table 2022-08-20 03:50:04 +02:00
Riyyi a68171002c Emulator: Fix out of scope destruction of string_view 2022-08-20 03:43:00 +02:00
3 changed files with 41 additions and 4 deletions
+29 -1
View File
@@ -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; break;
case 0x31:
break;
case 0xf8:
break;
case 0xf9:
break;
default:
VERIFY_NOT_REACHED();
} }
} }
+6
View File
@@ -8,6 +8,8 @@
#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;
}; };
+4 -1
View File
@@ -1,6 +1,7 @@
#pragma once #pragma once
#include <cstdint> // uint32_t #include <cstdint> // uint32_t
#include <string>
#include <string_view> #include <string_view>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
@@ -27,6 +28,8 @@ 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 };
@@ -39,5 +42,5 @@ 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, std::vector<uint32_t>> m_memory_spaces; std::unordered_map<std::string_view, std::vector<uint32_t>> m_memory_spaces;
std::string_view m_bootrom; std::string m_bootrom;
}; };