From 213cf83925ccd0e81d029f697385def8a0c5e352 Mon Sep 17 00:00:00 2001 From: AnonymusRaccoon Date: Tue, 28 Jan 2020 14:19:16 +0100 Subject: [PATCH] Adding memory shadow --- CMakeLists.txt | 2 +- sources/Cartridge/Cartridge.cpp | 26 ++++++++++++++++---------- sources/Cartridge/Cartridge.hpp | 2 ++ sources/Memory/MemoryShadow.cpp | 24 ++++++++++++++++++++++++ sources/Memory/MemoryShadow.hpp | 33 +++++++++++++++++++++++++++++++++ sources/Ram/Ram.cpp | 5 +++++ sources/Ram/Ram.hpp | 2 ++ 7 files changed, 83 insertions(+), 11 deletions(-) create mode 100644 sources/Memory/MemoryShadow.cpp create mode 100644 sources/Memory/MemoryShadow.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ab108fa..d63be30 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,4 +36,4 @@ add_executable(ComSquare sources/Exceptions/NotImplementedException.hpp sources/APU/APU.hpp sources/APU/APU.cpp - sources/Exceptions/InvalidAddress.hpp sources/Exceptions/InvalidRom.hpp sources/Models/Ints.hpp sources/Models/Ints.hpp sources/Ram/Ram.cpp sources/Ram/Ram.hpp) + sources/Exceptions/InvalidAddress.hpp sources/Exceptions/InvalidRom.hpp sources/Models/Ints.hpp sources/Models/Ints.hpp sources/Ram/Ram.cpp sources/Ram/Ram.hpp sources/Memory/MemoryShadow.cpp sources/Memory/MemoryShadow.hpp) diff --git a/sources/Cartridge/Cartridge.cpp b/sources/Cartridge/Cartridge.cpp index b39f728..13062d4 100644 --- a/sources/Cartridge/Cartridge.cpp +++ b/sources/Cartridge/Cartridge.cpp @@ -11,19 +11,10 @@ namespace ComSquare::Cartridge { - size_t Cartridge::getRomSize(const std::string &romPath) - { - struct stat info; - - if (stat(romPath.c_str(), &info) < 0) - throw InvalidRomException("Could not stat the rom file at " + romPath + ". " + strerror(errno)); - return info.st_size; - } - Cartridge::Cartridge(const std::string &romPath) { try { - size_t size = this->getRomSize(romPath); + size_t size = Cartridge::getRomSize(romPath); FILE *rom = fopen(romPath.c_str(), "rb"); if (!rom) @@ -37,6 +28,21 @@ namespace ComSquare::Cartridge } } + Cartridge::~Cartridge() + { + delete[] this->_data; + } + + size_t Cartridge::getRomSize(const std::string &romPath) + { + struct stat info; + + if (stat(romPath.c_str(), &info) < 0) + throw InvalidRomException("Could not stat the rom file at " + romPath + ". " + strerror(errno)); + return info.st_size; + } + + uint8_t Cartridge::read(uint24_t addr) { if (addr >= this->_size) diff --git a/sources/Cartridge/Cartridge.hpp b/sources/Cartridge/Cartridge.hpp index 59ad0f7..af35323 100644 --- a/sources/Cartridge/Cartridge.hpp +++ b/sources/Cartridge/Cartridge.hpp @@ -25,6 +25,8 @@ namespace ComSquare::Cartridge public: //! @brief Load a rom from it's path. explicit Cartridge(const std::string &romPath); + //! @brief Destructor that free the cartridge data. + ~Cartridge(); //! @brief Read from the rom. //! @param addr The address to read from. The address 0x0 should refer to the first byte of the rom's memory. //! @throw InvalidAddress will be thrown if the address is more than the size of the rom's memory. diff --git a/sources/Memory/MemoryShadow.cpp b/sources/Memory/MemoryShadow.cpp new file mode 100644 index 0000000..b98e513 --- /dev/null +++ b/sources/Memory/MemoryShadow.cpp @@ -0,0 +1,24 @@ +// +// Created by anonymus-raccoon on 1/28/20. +// + +#include "MemoryShadow.hpp" + +#include + +namespace ComSquare::Memory +{ + MemoryShadow::MemoryShadow(std::shared_ptr initial) + : _initial(std::move(initial)) + { } + + uint8_t MemoryShadow::read(uint24_t addr) + { + return this->_initial->read(addr); + } + + void MemoryShadow::write(uint24_t addr, uint8_t data) + { + return this->_initial->write(addr, data); + } +} \ No newline at end of file diff --git a/sources/Memory/MemoryShadow.hpp b/sources/Memory/MemoryShadow.hpp new file mode 100644 index 0000000..8bfb284 --- /dev/null +++ b/sources/Memory/MemoryShadow.hpp @@ -0,0 +1,33 @@ +// +// Created by anonymus-raccoon on 1/28/20. +// + +#ifndef COMSQUARE_MEMORYSHADOW_HPP +#define COMSQUARE_MEMORYSHADOW_HPP + +#include +#include "IMemory.hpp" + +namespace ComSquare::Memory +{ + class MemoryShadow : IMemory { + private: + //! @brief Memory to shadow from. + std::shared_ptr _initial; + public: + //! @brief Create a shadow for the memory given as parameter. + explicit MemoryShadow(std::shared_ptr initial); + //! @brief Read from the initial IMemory given. + //! @param addr The address to read from. The address 0x0 should refer to the first byte of the initial IMemory. + //! @throw InvalidAddress will be thrown if the address is more than the size of the initial IMemory. + //! @return Return the data at the address. + uint8_t read(uint24_t addr) override; + //! @brief Write data to the ram. + //! @param addr The address to write to. The address 0x0 should refer to the first byte of the initial IMemory. + //! @param data The data to write. + //! @throw InvalidAddress will be thrown if the address is more than the size of the initial IMemory. + void write(uint24_t addr, uint8_t data) override; + }; +} + +#endif //COMSQUARE_MEMORYSHADOW_HPP diff --git a/sources/Ram/Ram.cpp b/sources/Ram/Ram.cpp index 726e167..d29182e 100644 --- a/sources/Ram/Ram.cpp +++ b/sources/Ram/Ram.cpp @@ -13,6 +13,11 @@ namespace ComSquare::Ram this->_data = new uint8_t[size]; } + Ram::~Ram() + { + delete[] this->_data; + } + uint8_t Ram::read(uint24_t addr) { if (addr >= this->_size) diff --git a/sources/Ram/Ram.hpp b/sources/Ram/Ram.hpp index b884c08..93b2b43 100644 --- a/sources/Ram/Ram.hpp +++ b/sources/Ram/Ram.hpp @@ -18,6 +18,8 @@ namespace ComSquare::Ram public: //! @brief Load a rom from it's path. explicit Ram(size_t size); + //! @brief Destructor that free the ram. + ~Ram(); //! @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.