Browse Source

Emu: Add clock divider support for PUs

master
Frank 2 years ago
parent
commit
265bf09845
  1. 2
      src/apu.cpp
  2. 2
      src/apu.h
  3. 7
      src/cpu.cpp
  4. 4
      src/cpu.h
  5. 13
      src/emu.cpp
  6. 10
      src/emu.h
  7. 16
      src/main.cpp
  8. 7
      src/ppu.cpp
  9. 3
      src/ppu.h
  10. 5
      src/processing-unit.cpp
  11. 8
      src/processing-unit.h

2
src/apu.cpp

@ -7,7 +7,7 @@
#include "apu.h"
APU::APU()
APU::APU(unsigned int frequency) : ProcessingUnit(frequency)
{
}

2
src/apu.h

@ -11,6 +11,6 @@
class APU : public ProcessingUnit {
public:
APU();
APU(unsigned int frequency);
virtual ~APU();
};

7
src/cpu.cpp

@ -6,11 +6,16 @@
*/
#include "cpu.h"
#include <iostream>
CPU::CPU()
CPU::CPU(unsigned int frequency) : ProcessingUnit(frequency)
{
}
CPU::~CPU()
{
}
void CPU::update() {
printf("This is an update from the CPU\n");
}

4
src/cpu.h

@ -11,6 +11,8 @@
class CPU final : public ProcessingUnit {
public:
CPU();
CPU(unsigned int frequency);
virtual ~CPU();
void update() override;
};

13
src/emu.cpp

@ -1,14 +1,21 @@
#include "emu.h"
#include "cpu.h"
#include <iostream>
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);
}

10
src/emu.h

@ -11,9 +11,11 @@ class Emu final : public ruc::Singleton<Emu> {
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<ProcessingUnit> m_processing_units;
std::vector<ProcessingUnit*> m_processing_units;
std::unordered_map<const char*, std::vector<uint8_t>> m_memory_spaces;
};

16
src/main.cpp

@ -1,6 +1,8 @@
#include <cstdio>
#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;
}

7
src/ppu.cpp

@ -6,11 +6,16 @@
*/
#include "ppu.h"
#include <iostream>
PPU ::PPU()
PPU ::PPU(unsigned int frequency) : ProcessingUnit(frequency)
{
}
PPU ::~PPU()
{
}
void PPU::update() {
printf("ppu update\n");
}

3
src/ppu.h

@ -11,6 +11,7 @@
class PPU final : public ProcessingUnit {
public:
PPU();
PPU(unsigned int frequency);
virtual ~PPU();
virtual void update() override;
};

5
src/processing-unit.cpp

@ -6,8 +6,9 @@
*/
#include "processing-unit.h"
#include <iostream>
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;
}

8
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;
};

Loading…
Cancel
Save