From 41355191aa1d1feeaf20faa1b4d28dec073d4e50 Mon Sep 17 00:00:00 2001
From: AnonymusRaccoon
Date: Thu, 13 Feb 2020 18:22:17 +0100
Subject: [PATCH] Making the RAM a template
---
CMakeLists.txt | 11 ++----
sources/CPU/CPU.hpp | 5 ++-
.../CPU/Instructions/CommonInstructions.cpp | 10 -----
.../CPU/Instructions/CommonInstructions.hpp | 16 --------
.../CPU/Instructions/InternalInstruction.cpp | 13 +++++++
sources/Ram/Ram.cpp | 36 ------------------
sources/Ram/Ram.hpp | 37 +++++++++++++++----
sources/SNES.cpp | 4 +-
sources/SNES.hpp | 4 +-
tests/CPU/testInternal.cpp | 17 +++++++++
10 files changed, 70 insertions(+), 83 deletions(-)
delete mode 100644 sources/CPU/Instructions/CommonInstructions.cpp
delete mode 100644 sources/CPU/Instructions/CommonInstructions.hpp
create mode 100644 sources/CPU/Instructions/InternalInstruction.cpp
delete mode 100644 sources/Ram/Ram.cpp
create mode 100644 tests/CPU/testInternal.cpp
diff --git a/CMakeLists.txt b/CMakeLists.txt
index e0c5967..7533eb2 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -33,7 +33,6 @@ add_executable(unit_tests
sources/Exceptions/InvalidRom.hpp
sources/Models/Int24.hpp
sources/Models/Int24.hpp
- sources/Ram/Ram.cpp
sources/Ram/Ram.hpp
sources/Memory/MemoryShadow.cpp
sources/Memory/MemoryShadow.hpp
@@ -48,8 +47,6 @@ add_executable(unit_tests
sources/Cartridge/InterruptVectors.hpp
sources/Memory/RectangleShadow.cpp
sources/Memory/RectangleShadow.hpp
- sources/CPU/Instructions/CommonInstructions.cpp
- sources/CPU/Instructions/CommonInstructions.hpp
sources/Exceptions/InvalidOpcode.hpp
sources/CPU/Instructions/Interrupts.cpp
sources/CPU/Instructions/MathematicalOperations.cpp
@@ -61,7 +58,8 @@ add_executable(unit_tests
sources/CPU/Instructions/MemoryInstructions.cpp
tests/CPU/Math/testADC.cpp
tests/CPU/testStore.cpp
-)
+ sources/CPU/Instructions/InternalInstruction.cpp
+ tests/CPU/testInternal.cpp)
# include criterion & coverage
target_link_libraries(unit_tests criterion -lgcov)
@@ -95,7 +93,6 @@ add_executable(ComSquare
sources/Exceptions/InvalidRom.hpp
sources/Models/Int24.hpp
sources/Models/Int24.hpp
- sources/Ram/Ram.cpp
sources/Ram/Ram.hpp
sources/Memory/MemoryShadow.cpp
sources/Memory/MemoryShadow.hpp
@@ -110,12 +107,10 @@ add_executable(ComSquare
sources/Cartridge/InterruptVectors.hpp
sources/Memory/RectangleShadow.cpp
sources/Memory/RectangleShadow.hpp
- sources/CPU/Instructions/CommonInstructions.cpp
- sources/CPU/Instructions/CommonInstructions.hpp
sources/Exceptions/InvalidOpcode.hpp
sources/CPU/Instructions/Interrupts.cpp
sources/CPU/Instructions/MathematicalOperations.cpp
- sources/CPU/Instructions/MemoryInstructions.cpp)
+ sources/CPU/Instructions/MemoryInstructions.cpp sources/CPU/Instructions/InternalInstruction.cpp)
target_link_libraries(ComSquare
sfml-graphics
diff --git a/sources/CPU/CPU.hpp b/sources/CPU/CPU.hpp
index 6fa4d96..de2ecaa 100644
--- a/sources/CPU/CPU.hpp
+++ b/sources/CPU/CPU.hpp
@@ -8,7 +8,6 @@
#include "../Memory/IMemory.hpp"
#include "../Memory/MemoryBus.hpp"
#include "../Models/Int24.hpp"
-#include "Instructions/CommonInstructions.hpp"
#include "../Cartridge/Cartridge.hpp"
namespace ComSquare::CPU
@@ -263,7 +262,7 @@ namespace ComSquare::CPU
};
//! @brief The main CPU
- class CPU : public CommonInstructions, public Memory::IMemory {
+ class CPU : public Memory::IMemory {
private:
//! @brief All the registers of the CPU
Registers _registers{};
@@ -358,6 +357,8 @@ namespace ComSquare::CPU
void LDX(uint24_t addr);
//! @brief Load the Y index register from memory.
void LDY(uint24_t addr);
+ //! @brief Set status bits.
+ void SEP(uint24_t addr);
public:
explicit CPU(std::shared_ptr bus, Cartridge::Header &cartridgeHeader);
CPU(const CPU &) = default;
diff --git a/sources/CPU/Instructions/CommonInstructions.cpp b/sources/CPU/Instructions/CommonInstructions.cpp
deleted file mode 100644
index de8ee00..0000000
--- a/sources/CPU/Instructions/CommonInstructions.cpp
+++ /dev/null
@@ -1,10 +0,0 @@
-//
-// Created by anonymus-raccoon on 2/5/20.
-//
-
-#include "CommonInstructions.hpp"
-
-namespace ComSquare::CPU
-{
-
-}
\ No newline at end of file
diff --git a/sources/CPU/Instructions/CommonInstructions.hpp b/sources/CPU/Instructions/CommonInstructions.hpp
deleted file mode 100644
index a8e1e41..0000000
--- a/sources/CPU/Instructions/CommonInstructions.hpp
+++ /dev/null
@@ -1,16 +0,0 @@
-//
-// Created by anonymus-raccoon on 2/5/20.
-//
-
-#ifndef COMSQUARE_COMMONINSTRUCTIONS_HPP
-#define COMSQUARE_COMMONINSTRUCTIONS_HPP
-
-namespace ComSquare::CPU
-{
- //! @brief The shared states of the Main's CPU and the APU's CPU.
- class CommonInstructions {
-
- };
-}
-
-#endif //COMSQUARE_COMMONINSTRUCTIONS_HPP
diff --git a/sources/CPU/Instructions/InternalInstruction.cpp b/sources/CPU/Instructions/InternalInstruction.cpp
new file mode 100644
index 0000000..ab20ed9
--- /dev/null
+++ b/sources/CPU/Instructions/InternalInstruction.cpp
@@ -0,0 +1,13 @@
+//
+// Created by anonymus-raccoon on 2/13/20.
+//
+
+#include "../CPU.hpp"
+
+namespace ComSquare::CPU
+{
+ void CPU::SEP(uint24_t addr)
+ {
+
+ }
+}
\ No newline at end of file
diff --git a/sources/Ram/Ram.cpp b/sources/Ram/Ram.cpp
deleted file mode 100644
index a297705..0000000
--- a/sources/Ram/Ram.cpp
+++ /dev/null
@@ -1,36 +0,0 @@
-//
-// Created by anonymus-raccoon on 1/28/20.
-//
-
-#include
-#include "Ram.hpp"
-#include "../Exceptions/InvalidAddress.hpp"
-
-namespace ComSquare::Ram
-{
- Ram::Ram(size_t size)
- : _size(size)
- {
- this->_data = new uint8_t[size];
- std::memset(this->_data, 0, size * sizeof(uint8_t));
- }
-
- Ram::~Ram()
- {
- delete[] this->_data;
- }
-
- uint8_t Ram::read_internal(uint24_t addr)
- {
- if (addr >= this->_size)
- throw InvalidAddress("Ram read", addr);
- return this->_data[addr];
- }
-
- void Ram::write_internal(uint24_t addr, uint8_t data)
- {
- if (addr >= this->_size)
- throw InvalidAddress("Ram write", addr);
- this->_data[addr] = data;
- }
-}
diff --git a/sources/Ram/Ram.hpp b/sources/Ram/Ram.hpp
index 1164305..d2cbf97 100644
--- a/sources/Ram/Ram.hpp
+++ b/sources/Ram/Ram.hpp
@@ -5,36 +5,59 @@
#ifndef COMSQUARE_RAM_HPP
#define COMSQUARE_RAM_HPP
+#include
#include "../Memory/IRectangleMemory.hpp"
+#include "../Exceptions/InvalidAddress.hpp"
namespace ComSquare::Ram
{
+ template
class Ram : public Memory::IRectangleMemory {
- private:
+ private:
//! @brief The ram. (Can be used for WRam, SRam, VRam etc)
- uint8_t *_data;
+ T *_data;
//! @brief The size of the ram.
size_t _size;
public:
- //! @brief Load a rom from it's path.
- explicit Ram(size_t size);
+ //! @brief Create a ram of the given size (in bytes).
+ explicit Ram(size_t size)
+ : _size(size)
+ {
+ this->_data = new T[size];
+ std::memset(this->_data, 0, size * sizeof(T));
+ }
//! @brief The ram can't be copied.
Ram(const Ram &) = delete;
//! @brief The ram can't be assigned.
Ram &operator=(Ram &) = delete;
//! @brief Destructor that free the ram.
- ~Ram();
+ ~Ram()
+ {
+ delete[] this->_data;
+ }
//! @brief Read from the ram.
//! @param addr The address to read from. The address 0x0 should refer to the first byte of this ram.
//! @throw InvalidAddress will be thrown if the address is more than the size of the ram.
//! @return Return the data at the address.
- uint8_t read_internal(uint24_t addr) override;
+ T read_internal(uint24_t addr) override
+ {
+ if (addr >= this->_size)
+ throw InvalidAddress("Ram read", addr);
+ return this->_data[addr];
+ }
//! @brief Write data to the ram.
//! @param addr The address to write to. The address 0x0 should refer to the first byte of this ram.
//! @param data The data to write.
//! @throw InvalidAddress will be thrown if the address is more than the size of the ram.
- void write_internal(uint24_t addr, uint8_t data) override;
+ void write_internal(uint24_t addr, T data) override
+ {
+ if (addr >= this->_size)
+ throw InvalidAddress("Ram write", addr);
+ this->_data[addr] = data;
+ }
};
+
+ typedef Ram BasicRam;
}
#endif //COMSQUARE_RAM_HPP
diff --git a/sources/SNES.cpp b/sources/SNES.cpp
index 20646a2..effcfc5 100644
--- a/sources/SNES.cpp
+++ b/sources/SNES.cpp
@@ -13,8 +13,8 @@ namespace ComSquare
cpu(new CPU::CPU(bus, cartridge->header)),
ppu(new PPU::PPU(bus, renderer)),
apu(new APU::APU()),
- wram(new Ram::Ram(16384)),
- sram(new Ram::Ram(this->cartridge->header.sramSize))
+ wram(new Ram::BasicRam(16384)),
+ sram(new Ram::BasicRam(this->cartridge->header.sramSize))
{
bus->mapComponents(*this);
renderer.setWindowName(this->cartridge->header.gameName);
diff --git a/sources/SNES.hpp b/sources/SNES.hpp
index c863d88..9b3cc13 100644
--- a/sources/SNES.hpp
+++ b/sources/SNES.hpp
@@ -27,9 +27,9 @@ namespace ComSquare
//! @brief Audio Processing Unit if the SNES
std::shared_ptr apu;
//! @brief Work Ram shared by all the components.
- std::shared_ptr wram;
+ std::shared_ptr wram;
//! @brief Save Ram residing inside the Cartridge in a real SNES.
- std::shared_ptr sram;
+ std::shared_ptr sram;
//! @brief Create all the components using a common memory bus for all of them.
SNES(const std::shared_ptr &bus, const std::string &ramPath, Renderer::IRenderer &renderer);
SNES(const SNES &) = default;
diff --git a/tests/CPU/testInternal.cpp b/tests/CPU/testInternal.cpp
new file mode 100644
index 0000000..cb983b9
--- /dev/null
+++ b/tests/CPU/testInternal.cpp
@@ -0,0 +1,17 @@
+//
+// Created by anonymus-raccoon on 2/13/20.
+//
+
+#include
+#include
+#include
+#include "../tests.hpp"
+#include "../../sources/SNES.hpp"
+#include "../../sources/Memory/MemoryBus.hpp"
+using namespace ComSquare;
+
+Test(SEP, setall)
+{
+ auto pair = Init();
+ pair.second.cpu.SEP()
+}
\ No newline at end of file