From 7f9847dd2dbf8ceb8a2a7b9f24024b77d0ffeb75 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Thu, 18 Aug 2022 00:03:32 +0200 Subject: [PATCH 1/2] Emulator: Add register variables to CPU class --- src/cpu.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/cpu.h b/src/cpu.h index 5241a9a..403f0e5 100644 --- a/src/cpu.h +++ b/src/cpu.h @@ -7,10 +7,43 @@ #pragma once +#include // uint8_t, uint16_t + #include "processing-unit.h" class CPU final : public ProcessingUnit { public: CPU(); virtual ~CPU(); + + // 8-bit Arithmetic and Logic Instructions + + // 16-bit Arithmetic Instructions + + // Bit Operations Instructions + + // Bit Shift Instructions + + // Load Instructions + + // Jumps and Subroutines + + // Stack Operations Instructions + + // Miscellaneous Instructions + +private: + // 16-bit registers + uint16_t m_af { 0 }; // Accumulator & Flags + uint16_t m_bc { 0 }; // BC + uint16_t m_de { 0 }; // DE + uint16_t m_hl { 0 }; // HL + uint16_t m_sp { 0 }; // Stack Pointer + uint16_t m_pc { 0 }; // Program Counter + + // 8-bit registers (flags) + uint8_t m_z { 0 }; // Zero flag + uint8_t m_n { 0 }; // Subtraction flag (BCD) + uint8_t m_h { 0 }; // Half Carry flag (BCD) + uint8_t m_c { 0 }; // Carry flag }; From 9af75ad702521c1ab8219b601a2e9c9385cee2ef Mon Sep 17 00:00:00 2001 From: Frank Date: Wed, 17 Aug 2022 23:41:46 +0200 Subject: [PATCH 2/2] Emu: Add functionality to add Memory and PUs --- src/emu.cpp | 19 +++++++++++++++++++ src/emu.h | 23 +++++++++++++---------- src/processing-unit.cpp | 8 ++++++-- src/processing-unit.h | 9 ++++++++- 4 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 src/emu.cpp diff --git a/src/emu.cpp b/src/emu.cpp new file mode 100644 index 0000000..610b192 --- /dev/null +++ b/src/emu.cpp @@ -0,0 +1,19 @@ +#include "emu.h" + +void Emu::update() { + for (auto unit : m_processing_units) { + if (m_cycle % int(m_frequency / unit.frequency()) == 0) { + unit.update(); + } + } +} + +void Emu::addProcessingUnit(ProcessingUnit processing_unit) { + m_processing_units.push_back(processing_unit); +} + +void Emu::addMemorySpace(const char* name, int size) { + std::vector memory; + memory.reserve(size); + m_memory_spaces.emplace(name, memory); +} \ No newline at end of file diff --git a/src/emu.h b/src/emu.h index 799c5a5..b20b0c8 100644 --- a/src/emu.h +++ b/src/emu.h @@ -2,6 +2,7 @@ #include #include +#include #include "processing-unit.h" #include "ruc/singleton.h" @@ -10,21 +11,23 @@ class Emu final : public ruc::Singleton { public: Emu(s) {} - void WriteRAM(int location, int length); - void WriteVRAM(int location, int length); - void WriteROM(int location, int length); + void update(); - void ReadRAM(int location, int length); - void ReadVRAM(int location, int length); - void ReadROM(int location, int length); + void addProcessingUnit(ProcessingUnit processing_unit); + void addMemorySpace(const char* name, int size); + + void writeRAM(const char* memory_space, int location); + void writeVRAM(const char* memory_space, int location); + void writeROM(const char* memory_space, int location); + + uint8_t readRAM(const char* memory_space, int location); + uint8_t readVRAM(const char* memory_space, int location); + uint8_t readROM(const char* memory_space, int location); private: float m_frequency; int m_cycle = 0; - uint8_t m_ram[1024]; - uint8_t m_vram[1024]; - uint8_t m_rom[1024]; - std::vector m_processing_units; + std::unordered_map> m_memory_spaces; }; diff --git a/src/processing-unit.cpp b/src/processing-unit.cpp index 851c7b8..4026a82 100644 --- a/src/processing-unit.cpp +++ b/src/processing-unit.cpp @@ -5,12 +5,16 @@ * SPDX-License-Identifier: MIT */ -#include "processingunit.h" +#include "processing-unit.h" -ProcessingUnit::ProcessingUnit() +ProcessingUnit::ProcessingUnit(float frequency) : m_frequency(frequency) { } ProcessingUnit::~ProcessingUnit() { } + +float ProcessingUnit::frequency() { + return m_frequency; +} \ No newline at end of file diff --git a/src/processing-unit.h b/src/processing-unit.h index d43d5dd..97d6c11 100644 --- a/src/processing-unit.h +++ b/src/processing-unit.h @@ -9,6 +9,13 @@ class ProcessingUnit { public: - ProcessingUnit(); + ProcessingUnit(float frequency); virtual ~ProcessingUnit(); + + virtual void update(); + + float frequency(); + +private: +float m_frequency; };