Making the RAM a template

This commit is contained in:
AnonymusRaccoon
2020-02-13 18:22:17 +01:00
parent 2735238329
commit 41355191aa
10 changed files with 70 additions and 83 deletions
+3 -8
View File
@@ -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
+3 -2
View File
@@ -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<Memory::MemoryBus> bus, Cartridge::Header &cartridgeHeader);
CPU(const CPU &) = default;
@@ -1,10 +0,0 @@
//
// Created by anonymus-raccoon on 2/5/20.
//
#include "CommonInstructions.hpp"
namespace ComSquare::CPU
{
}
@@ -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
@@ -0,0 +1,13 @@
//
// Created by anonymus-raccoon on 2/13/20.
//
#include "../CPU.hpp"
namespace ComSquare::CPU
{
void CPU::SEP(uint24_t addr)
{
}
}
-36
View File
@@ -1,36 +0,0 @@
//
// Created by anonymus-raccoon on 1/28/20.
//
#include <cstring>
#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;
}
}
+30 -7
View File
@@ -5,36 +5,59 @@
#ifndef COMSQUARE_RAM_HPP
#define COMSQUARE_RAM_HPP
#include <cstring>
#include "../Memory/IRectangleMemory.hpp"
#include "../Exceptions/InvalidAddress.hpp"
namespace ComSquare::Ram
{
template<typename T>
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<uint8_t> BasicRam;
}
#endif //COMSQUARE_RAM_HPP
+2 -2
View File
@@ -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);
+2 -2
View File
@@ -27,9 +27,9 @@ namespace ComSquare
//! @brief Audio Processing Unit if the SNES
std::shared_ptr<APU::APU> apu;
//! @brief Work Ram shared by all the components.
std::shared_ptr<Ram::Ram> wram;
std::shared_ptr<Ram::BasicRam> wram;
//! @brief Save Ram residing inside the Cartridge in a real SNES.
std::shared_ptr<Ram::Ram> sram;
std::shared_ptr<Ram::BasicRam> sram;
//! @brief Create all the components using a common memory bus for all of them.
SNES(const std::shared_ptr<Memory::MemoryBus> &bus, const std::string &ramPath, Renderer::IRenderer &renderer);
SNES(const SNES &) = default;
+17
View File
@@ -0,0 +1,17 @@
//
// Created by anonymus-raccoon on 2/13/20.
//
#include <criterion/criterion.h>
#include <iostream>
#include <bitset>
#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()
}