Browse Source

Emulator+Test: Store ProcessingUnits as std::shared_ptr

master
Riyyi 2 years ago
parent
commit
ce46f5033a
  1. 2
      src/emu.cpp
  2. 7
      src/emu.h
  3. 9
      src/loader.cpp
  4. 57
      test/testcpu.cpp

2
src/emu.cpp

@ -39,7 +39,7 @@ void Emu::update()
}
}
void Emu::addProcessingUnit(std::string_view name, ProcessingUnit* processing_unit)
void Emu::addProcessingUnit(std::string_view name, std::shared_ptr<ProcessingUnit> processing_unit)
{
m_processing_units.emplace(name, processing_unit);
}

7
src/emu.h

@ -8,6 +8,7 @@
#pragma once
#include <cstdint> // uint32_t
#include <memory> // std::shared_ptr
#include <string>
#include <string_view>
#include <unordered_map>
@ -34,7 +35,7 @@ public:
void update();
void addProcessingUnit(std::string_view name, ProcessingUnit* processing_unit);
void addProcessingUnit(std::string_view name, std::shared_ptr<ProcessingUnit> processing_unit);
void addMemorySpace(std::string_view name, uint32_t start_address, uint32_t end_address, uint32_t amount_of_banks = 1);
void removeMemorySpace(std::string_view name);
@ -43,7 +44,7 @@ public:
// -------------------------------------
ProcessingUnit* processingUnit(std::string_view name) const { return m_processing_units.at(name); }
std::shared_ptr<ProcessingUnit> processingUnit(std::string_view name) const { return m_processing_units.at(name); }
MemorySpace memorySpace(std::string_view name) { return m_memory_spaces[name]; }
private:
@ -55,6 +56,6 @@ private:
ruc::Timer m_timer;
std::unordered_map<std::string_view, ProcessingUnit*> m_processing_units;
std::unordered_map<std::string_view, std::shared_ptr<ProcessingUnit>> m_processing_units;
std::unordered_map<std::string_view, MemorySpace> m_memory_spaces;
};

9
src/loader.cpp

@ -6,6 +6,7 @@
#include <cstddef> // size_t
#include <cstdint> // uint32_t
#include <memory> // std::make_shared
#include "cpu.h"
#include "emu.h"
@ -44,11 +45,11 @@ void Loader::init()
Emu::the().init(8000000);
CPU cpu(8000000);
PPU ppu(4000000);
auto cpu = std::make_shared<CPU>(8000000);
auto ppu = std::make_shared<PPU>(4000000);
Emu::the().addProcessingUnit("cpu", &cpu);
Emu::the().addProcessingUnit("ppu", &ppu);
Emu::the().addProcessingUnit("CPU", cpu);
Emu::the().addProcessingUnit("PPU", ppu);
// https://gbdev.io/pandocs/Memory_Map.html
// https://gbdev.io/pandocs/Power_Up_Sequence.html

57
test/testcpu.cpp

@ -4,6 +4,7 @@
* SPDX-License-Identifier: MIT
*/
#include <memory> // std::make_shared, std::shared_ptr
#include <vector>
#include "cpu.h"
@ -26,13 +27,13 @@ struct CPUTest {
}
};
CPU runCPUTest(std::vector<uint8_t> test)
std::shared_ptr<CPU> runCPUTest(std::vector<uint8_t> test)
{
CPU cpu(4000000);
auto cpu = std::make_shared<CPU>(4000000);
Emu::the().destroy();
Emu::the().init(8000000);
Emu::the().addProcessingUnit("cpu", &cpu);
Emu::the().addProcessingUnit("cpu", cpu);
Emu::the().addMemorySpace("FULL", 0x0000, 0xffff);
// Load the test
@ -41,7 +42,7 @@ CPU runCPUTest(std::vector<uint8_t> test)
}
// Run the test
while (cpu.pc() < test.size()) {
while (cpu->pc() < test.size()) {
Emu::the().update();
}
@ -83,7 +84,7 @@ TEST_CASE(CPUIsCarry)
TEST_CASE(CPUAddPlusCarry)
{
CPU cpu(0);
std::shared_ptr<CPU> cpu(0);
// ADC A,E
@ -96,11 +97,11 @@ TEST_CASE(CPUAddPlusCarry)
// clang-format on
};
cpu = runCPUTest(adc_r8);
EXPECT_EQ(cpu.a(), 0xf1);
EXPECT_EQ(cpu.zf(), 0x0);
EXPECT_EQ(cpu.nf(), 0x0);
EXPECT_EQ(cpu.hf(), 0x1);
EXPECT_EQ(cpu.cf(), 0x0);
EXPECT_EQ(cpu->a(), 0xf1);
EXPECT_EQ(cpu->zf(), 0x0);
EXPECT_EQ(cpu->nf(), 0x0);
EXPECT_EQ(cpu->hf(), 0x1);
EXPECT_EQ(cpu->cf(), 0x0);
// ADC A,i8
@ -112,11 +113,11 @@ TEST_CASE(CPUAddPlusCarry)
// clang-format on
};
cpu = runCPUTest(adc_i8);
EXPECT_EQ(cpu.a(), 0x1d);
EXPECT_EQ(cpu.zf(), 0x0);
EXPECT_EQ(cpu.nf(), 0x0);
EXPECT_EQ(cpu.hf(), 0x0);
EXPECT_EQ(cpu.cf(), 0x1);
EXPECT_EQ(cpu->a(), 0x1d);
EXPECT_EQ(cpu->zf(), 0x0);
EXPECT_EQ(cpu->nf(), 0x0);
EXPECT_EQ(cpu->hf(), 0x0);
EXPECT_EQ(cpu->cf(), 0x1);
// ADC A,(HL)
@ -129,18 +130,18 @@ TEST_CASE(CPUAddPlusCarry)
// clang-format on
};
cpu = runCPUTest(adc_hl);
EXPECT_EQ(cpu.a(), 0x0);
EXPECT_EQ(cpu.zf(), 0x1);
EXPECT_EQ(cpu.nf(), 0x0);
EXPECT_EQ(cpu.hf(), 0x1);
EXPECT_EQ(cpu.cf(), 0x1);
EXPECT_EQ(cpu->a(), 0x0);
EXPECT_EQ(cpu->zf(), 0x1);
EXPECT_EQ(cpu->nf(), 0x0);
EXPECT_EQ(cpu->hf(), 0x1);
EXPECT_EQ(cpu->cf(), 0x1);
}
TEST_CASE(CPUSetStackPointer)
{
std::vector<uint8_t> test = { 0x31, 0xfe, 0xff }; // LD SP,i16
CPU cpu = runCPUTest(test);
EXPECT_EQ(cpu.sp(), 0xfffe);
auto cpu = runCPUTest(test);
EXPECT_EQ(cpu->sp(), 0xfffe);
}
TEST_CASE(CPUPushToStack)
@ -154,9 +155,9 @@ TEST_CASE(CPUPushToStack)
0xc5, // PUSH BC
// clang-format on
};
CPU cpu = runCPUTest(push_bc);
EXPECT_EQ(cpu.bc(), 0xfffc);
EXPECT_EQ(cpu.sp(), 0xfffc);
auto cpu = runCPUTest(push_bc);
EXPECT_EQ(cpu->bc(), 0xfffc);
EXPECT_EQ(cpu->sp(), 0xfffc);
EXPECT_EQ(Emu::the().readMemory(0xfffd), 0xff);
EXPECT_EQ(Emu::the().readMemory(0xfffc), 0xfc);
}
@ -173,9 +174,9 @@ TEST_CASE(CPUPopFromStack)
0xc1, // POP BC
// clang-format on
};
CPU cpu = runCPUTest(pop_bc);
EXPECT_EQ(cpu.bc(), 0x3c5f);
EXPECT_EQ(cpu.sp(), 0xfffe);
auto cpu = runCPUTest(pop_bc);
EXPECT_EQ(cpu->bc(), 0x3c5f);
EXPECT_EQ(cpu->sp(), 0xfffe);
EXPECT_EQ(Emu::the().readMemory(0xfffd), 0x3c);
EXPECT_EQ(Emu::the().readMemory(0xfffc), 0x5f);
}

Loading…
Cancel
Save