From 265bf0984591a59906e20d0748a30fb63c6a3a5a Mon Sep 17 00:00:00 2001 From: Frank Date: Thu, 18 Aug 2022 00:43:41 +0200 Subject: [PATCH] Emu: Add clock divider support for PUs --- src/apu.cpp | 2 +- src/apu.h | 2 +- src/cpu.cpp | 7 ++++++- src/cpu.h | 4 +++- src/emu.cpp | 13 ++++++++++--- src/emu.h | 10 ++++++---- src/main.cpp | 16 +++++++++++++++- src/ppu.cpp | 7 ++++++- src/ppu.h | 3 ++- src/processing-unit.cpp | 5 +++-- src/processing-unit.h | 8 ++++---- 11 files changed, 57 insertions(+), 20 deletions(-) diff --git a/src/apu.cpp b/src/apu.cpp index 72027e0..ab6b181 100644 --- a/src/apu.cpp +++ b/src/apu.cpp @@ -7,7 +7,7 @@ #include "apu.h" -APU::APU() +APU::APU(unsigned int frequency) : ProcessingUnit(frequency) { } diff --git a/src/apu.h b/src/apu.h index 20ab650..a9fc899 100644 --- a/src/apu.h +++ b/src/apu.h @@ -11,6 +11,6 @@ class APU : public ProcessingUnit { public: - APU(); + APU(unsigned int frequency); virtual ~APU(); }; diff --git a/src/cpu.cpp b/src/cpu.cpp index 6ce2bc7..a8764d5 100644 --- a/src/cpu.cpp +++ b/src/cpu.cpp @@ -6,11 +6,16 @@ */ #include "cpu.h" +#include -CPU::CPU() +CPU::CPU(unsigned int frequency) : ProcessingUnit(frequency) { } CPU::~CPU() { } + +void CPU::update() { + printf("This is an update from the CPU\n"); +} \ No newline at end of file diff --git a/src/cpu.h b/src/cpu.h index 5241a9a..2244324 100644 --- a/src/cpu.h +++ b/src/cpu.h @@ -11,6 +11,8 @@ class CPU final : public ProcessingUnit { public: - CPU(); + CPU(unsigned int frequency); virtual ~CPU(); + + void update() override; }; diff --git a/src/emu.cpp b/src/emu.cpp index 610b192..a15e155 100644 --- a/src/emu.cpp +++ b/src/emu.cpp @@ -1,14 +1,21 @@ #include "emu.h" +#include "cpu.h" +#include + +void Emu::init(unsigned int frequency) { + m_frequency = frequency; +} void Emu::update() { for (auto unit : m_processing_units) { - if (m_cycle % int(m_frequency / unit.frequency()) == 0) { - unit.update(); + if (m_cycle % (m_frequency / unit->frequency()) == 0) { + unit->update(); } } + m_cycle++; } -void Emu::addProcessingUnit(ProcessingUnit processing_unit) { +void Emu::addProcessingUnit(ProcessingUnit* processing_unit) { m_processing_units.push_back(processing_unit); } diff --git a/src/emu.h b/src/emu.h index b20b0c8..551f381 100644 --- a/src/emu.h +++ b/src/emu.h @@ -11,9 +11,11 @@ class Emu final : public ruc::Singleton { public: Emu(s) {} + void init(unsigned int frequency); + void update(); - void addProcessingUnit(ProcessingUnit processing_unit); + void addProcessingUnit(ProcessingUnit* processing_unit); void addMemorySpace(const char* name, int size); void writeRAM(const char* memory_space, int location); @@ -25,9 +27,9 @@ public: uint8_t readROM(const char* memory_space, int location); private: - float m_frequency; - int m_cycle = 0; + unsigned int m_frequency; + unsigned int m_cycle = 0; - std::vector m_processing_units; + std::vector m_processing_units; std::unordered_map> m_memory_spaces; }; diff --git a/src/main.cpp b/src/main.cpp index 34c9cf2..c757ee4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,8 @@ #include #include "emu.h" +#include "cpu.h" +#include "ppu.h" #include "ruc/timer.h" int main(int argc, char* argv[]) @@ -9,7 +11,19 @@ int main(int argc, char* argv[]) printf("%fms\n", t.elapsedNanoseconds() / 1000000.0); - Emu::the().ReadRAM(0, 0); + Emu::the().init(4000000); + + CPU cpu(1000000); + PPU ppu(2000000); + + Emu::the().addProcessingUnit(&cpu); + Emu::the().addProcessingUnit(&ppu); + + Emu::the().addMemorySpace("RAM", 1024); + + for(int i = 0; i < 1000; i++) { + Emu::the().update(); + } return 0; } diff --git a/src/ppu.cpp b/src/ppu.cpp index 93b3398..4a87c94 100644 --- a/src/ppu.cpp +++ b/src/ppu.cpp @@ -6,11 +6,16 @@ */ #include "ppu.h" +#include -PPU ::PPU() +PPU ::PPU(unsigned int frequency) : ProcessingUnit(frequency) { } PPU ::~PPU() { } + +void PPU::update() { + printf("ppu update\n"); +} \ No newline at end of file diff --git a/src/ppu.h b/src/ppu.h index b236e8e..e051717 100644 --- a/src/ppu.h +++ b/src/ppu.h @@ -11,6 +11,7 @@ class PPU final : public ProcessingUnit { public: - PPU(); + PPU(unsigned int frequency); virtual ~PPU(); + virtual void update() override; }; diff --git a/src/processing-unit.cpp b/src/processing-unit.cpp index 4026a82..a8dcc55 100644 --- a/src/processing-unit.cpp +++ b/src/processing-unit.cpp @@ -6,8 +6,9 @@ */ #include "processing-unit.h" +#include -ProcessingUnit::ProcessingUnit(float frequency) : m_frequency(frequency) +ProcessingUnit::ProcessingUnit(unsigned int frequency) : m_frequency(frequency) { } @@ -15,6 +16,6 @@ ProcessingUnit::~ProcessingUnit() { } -float ProcessingUnit::frequency() { +unsigned int ProcessingUnit::frequency() { return m_frequency; } \ No newline at end of file diff --git a/src/processing-unit.h b/src/processing-unit.h index 97d6c11..ff00cd8 100644 --- a/src/processing-unit.h +++ b/src/processing-unit.h @@ -9,13 +9,13 @@ class ProcessingUnit { public: - ProcessingUnit(float frequency); + ProcessingUnit(unsigned int frequency); virtual ~ProcessingUnit(); - virtual void update(); + virtual void update() = 0; - float frequency(); + unsigned int frequency(); private: -float m_frequency; +unsigned int m_frequency; };