From c9eed502893e9456113393f15b1df17545b19b0a Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Thu, 4 Feb 2021 10:22:30 +0100 Subject: [PATCH] Adding const qualifiers to the IMemory and adding error messages in the memory viewer --- sources/APU/APU.cpp | 19 +++++++++----- sources/APU/APU.hpp | 39 +++++++++++++++++------------ sources/APU/DSP/DSP.cpp | 11 +++++--- sources/APU/DSP/DSP.hpp | 10 +++++--- sources/APU/IPL/IPL.cpp | 11 +++++--- sources/APU/IPL/IPL.hpp | 10 +++++--- sources/CPU/CPU.cpp | 13 +++++++--- sources/CPU/CPU.hpp | 12 ++++++--- sources/CPU/DMA/DMA.cpp | 2 +- sources/CPU/DMA/DMA.hpp | 2 +- sources/Cartridge/Cartridge.cpp | 2 +- sources/Cartridge/Cartridge.hpp | 2 +- sources/Debugger/APUDebug.cpp | 2 +- sources/Debugger/APUDebug.hpp | 2 +- sources/Debugger/CPU/CPUDebug.cpp | 7 ++++-- sources/Debugger/CPU/CPUDebug.hpp | 2 +- sources/Debugger/MemoryViewer.cpp | 18 +++++++++++-- sources/Memory/AMemory.cpp | 10 ++++---- sources/Memory/AMemory.hpp | 10 ++++---- sources/Memory/ARectangleMemory.cpp | 11 ++++---- sources/Memory/ARectangleMemory.hpp | 10 ++++---- sources/Memory/IMemory.hpp | 19 ++++++++------ sources/Memory/MemoryShadow.cpp | 15 +++++++---- sources/Memory/MemoryShadow.hpp | 13 ++++++---- sources/Memory/RectangleShadow.cpp | 17 ++++++++----- sources/Memory/RectangleShadow.hpp | 15 ++++++----- sources/PPU/PPU.cpp | 15 +++++++---- sources/PPU/PPU.hpp | 13 ++++++---- sources/Ram/Ram.cpp | 9 ++++--- sources/Ram/Ram.hpp | 10 ++++---- 30 files changed, 208 insertions(+), 123 deletions(-) diff --git a/sources/APU/APU.cpp b/sources/APU/APU.cpp index effc58d..ee93fba 100644 --- a/sources/APU/APU.cpp +++ b/sources/APU/APU.cpp @@ -17,22 +17,23 @@ namespace ComSquare::APU this->reset(); } - bool APU::isDebugger() + bool APU::isDebugger() const { return false; } - std::string APU::getName() + std::string APU::getName() const { return "APU"; } - Component APU::getComponent() + Component APU::getComponent() const { return Apu; } - uint8_t APU::_internalRead(uint24_t addr) { + uint8_t APU::_internalRead(uint24_t addr) const + { switch (addr) { case 0x0000 ... 0x00EF: return this->_map->Page0.read(addr); @@ -71,7 +72,8 @@ namespace ComSquare::APU } } - void APU::_internalWrite(uint24_t addr, uint8_t data) { + void APU::_internalWrite(uint24_t addr, uint8_t data) + { switch (addr) { case 0x0000 ... 0x00EF: this->_map->Page0.write(addr, data); @@ -129,7 +131,7 @@ namespace ComSquare::APU } } - uint8_t APU::read(uint24_t addr) + uint8_t APU::read(uint24_t addr) const { switch (addr) { case 0x00: @@ -165,6 +167,11 @@ namespace ComSquare::APU } } + uint24_t APU::getSize() const + { + return 0x3; + } + void APU::reset() { this->_registers.port0 = 0x00; diff --git a/sources/APU/APU.hpp b/sources/APU/APU.hpp index 6503207..1725859 100644 --- a/sources/APU/APU.hpp +++ b/sources/APU/APU.hpp @@ -149,19 +149,14 @@ namespace ComSquare::APU //! @param addr The address to read from. The address 0x0000 should refer to the first byte of the register. //! @throw InvalidAddress will be thrown if the address is more than $FFFF (the number of register). //! @return Return the data. - uint8_t _internalRead(uint24_t addr); + uint8_t _internalRead(uint24_t addr) const; + //! @brief Write data to the APU ram. //! @param addr The address to write to. The address 0x0000 should refer to the first byte of register. //! @param data The new value of the register. //! @throw InvalidAddress will be thrown if the address is more than $FFFF (the number of register). void _internalWrite(uint24_t addr, uint8_t data); - //! @brief Get the name of this accessor (used for debug purpose) - std::string getName() override; - - //! @brief Get the component of this accessor (used for debug purpose) - Component getComponent() override; - //! @brief Current state of APU CPU StateMode _state = Running; @@ -372,16 +367,28 @@ namespace ComSquare::APU APU &operator=(const APU &) = default; ~APU() override = default; - //! @brief Read from the internal APU register. - //! @param addr The address to read from. The address 0x00 should refer to the first byte of the register. - //! @throw InvalidAddress will be thrown if the address is more than $0F (the number of register). - //! @return Return the value of the register. - uint8_t read(uint24_t addr) override; - //! @brief Write data to the internal APU register. - //! @param addr The address to write to. The address 0x00 should refer to the first byte of register. + //! @brief Read from the APU ram. + //! @param addr The address to read from. The address 0x0000 should refer to the first byte of the register. + //! @throw InvalidAddress will be thrown if the address is more than $FFFF (the number of register). + //! @return Return the data. + uint8_t read(uint24_t addr) const override; + + //! @brief Write data to the APU ram. + //! @param addr The address to write to. The address 0x0000 should refer to the first byte of register. //! @param data The new value of the register. - //! @throw InvalidAddress will be thrown if the address is more than $0F (the number of register). + //! @throw InvalidAddress will be thrown if the address is more than $FFFF (the number of register). void write(uint24_t addr, uint8_t data) override; + + //! @brief Get the name of this accessor (used for debug purpose) + std::string getName() const override; + + //! @brief Get the component of this accessor (used for debug purpose) + Component getComponent() const override; + + //! @brief Get the size of the data. This size can be lower than the mapped data. + //! @return The number of bytes inside this memory. + uint24_t getSize() const override; + //! @brief This function execute the instructions received until the maximum number of cycles is reached. //! @return The number of cycles that elapsed. virtual void update(unsigned cycles); @@ -390,7 +397,7 @@ namespace ComSquare::APU void reset(); //! @brief Return true if the CPU is overloaded with debugging features. - virtual bool isDebugger(); + virtual bool isDebugger() const; }; } diff --git a/sources/APU/DSP/DSP.cpp b/sources/APU/DSP/DSP.cpp index 421826e..59dfad5 100644 --- a/sources/APU/DSP/DSP.cpp +++ b/sources/APU/DSP/DSP.cpp @@ -7,7 +7,7 @@ namespace ComSquare::APU::DSP { - uint8_t DSP::read(uint24_t addr) + uint8_t DSP::read(uint24_t addr) const { switch (addr) { case 0x00: @@ -579,6 +579,11 @@ namespace ComSquare::APU::DSP } } + uint24_t DSP::getSize() const + { + return 0x7F; + } + Registers DSP::getRegisters() { return this->_registers; @@ -589,12 +594,12 @@ namespace ComSquare::APU::DSP return this->_channels; } - std::string DSP::getName() + std::string DSP::getName() const { return "DSP"; } - Component DSP::getComponent() + Component DSP::getComponent() const { return Apu; } diff --git a/sources/APU/DSP/DSP.hpp b/sources/APU/DSP/DSP.hpp index 1676cd5..04d6265 100644 --- a/sources/APU/DSP/DSP.hpp +++ b/sources/APU/DSP/DSP.hpp @@ -134,7 +134,7 @@ namespace ComSquare::APU::DSP //! @param addr The address to read from. The address 0x0 should refer to the first byte of the register. //! @throw InvalidAddress will be thrown if the address is more than $7F (the number of register). //! @return Return the value of the register. - uint8_t read(uint24_t addr) override; + uint8_t read(uint24_t addr) const override; //! @brief Write data to the internal DSP register. //! @param addr The address to write to. The address 0x0 should refer to the first byte of register. //! @param data The new value of the register. @@ -142,10 +142,14 @@ namespace ComSquare::APU::DSP void write(uint24_t addr, uint8_t data) override; //! @brief Get the name of this accessor (used for debug purpose) - std::string getName() override; + std::string getName() const override; //! @brief Get the component of this accessor (used for debug purpose) - Component getComponent() override; + Component getComponent() const override; + + //! @brief Get the size of the data. This size can be lower than the mapped data. + //! @return The number of bytes inside this memory. + uint24_t getSize() const override; }; } diff --git a/sources/APU/IPL/IPL.cpp b/sources/APU/IPL/IPL.cpp index 4bbf989..0b90e6a 100644 --- a/sources/APU/IPL/IPL.cpp +++ b/sources/APU/IPL/IPL.cpp @@ -17,7 +17,7 @@ namespace ComSquare::APU::IPL IPL::~IPL() { } - uint8_t IPL::read(uint24_t addr) + uint8_t IPL::read(uint24_t addr) const { if (addr >= this->_size) throw InvalidAddress("IPL read", addr); @@ -31,12 +31,17 @@ namespace ComSquare::APU::IPL this->_data[addr] = data; } - std::string IPL::getName() + uint24_t IPL::getSize() const + { + return this->_size; + } + + std::string IPL::getName() const { return this->_iplName; } - Component IPL::getComponent() + Component IPL::getComponent() const { return this->_iplType; } diff --git a/sources/APU/IPL/IPL.hpp b/sources/APU/IPL/IPL.hpp index 54ba95d..0c0c5a3 100644 --- a/sources/APU/IPL/IPL.hpp +++ b/sources/APU/IPL/IPL.hpp @@ -45,7 +45,7 @@ namespace ComSquare::APU::IPL //! @param addr The global 24 bits address. This method is responsible of mapping to the component's read. //! @throw InvalidAddress if the address is not mapped to the component. //! @return Return the data at the address given as parameter. - uint8_t read(uint24_t addr) override; + uint8_t read(uint24_t addr) const override; //! @brief Write data to this component using the same method as the basic IMemory. //! @param addr The global 24 bits address. This method is responsible of mapping to the component's write. @@ -53,11 +53,15 @@ namespace ComSquare::APU::IPL //! @throw InvalidAddress if the address is not mapped to the component. void write(uint24_t addr, uint8_t data) override; + //! @brief Get the size of the data. This size can be lower than the mapped data. + //! @return The number of bytes inside this memory. + uint24_t getSize() const override; + //! @brief Get the name of this accessor (used for debug purpose) - std::string getName() override; + std::string getName() const override; //! @brief Get the component of this accessor (used for debug purpose) - Component getComponent() override; + Component getComponent() const override; }; } diff --git a/sources/CPU/CPU.cpp b/sources/CPU/CPU.cpp index 107a6a9..6ccfc60 100644 --- a/sources/CPU/CPU.cpp +++ b/sources/CPU/CPU.cpp @@ -20,7 +20,7 @@ namespace ComSquare::CPU channel.setBus(_bus); } - bool CPU::isDebugger() + bool CPU::isDebugger() const { return false; } @@ -31,7 +31,7 @@ namespace ComSquare::CPU } //! @bref The CPU's internal registers starts at $4200 and finish at $421F. - uint8_t CPU::read(uint24_t addr) + uint8_t CPU::read(uint24_t addr) const { uint8_t tmp = 0; @@ -207,6 +207,11 @@ namespace ComSquare::CPU } } + uint24_t CPU::getSize() const + { + return 0x180; + } + uint8_t CPU::readPC() { uint8_t ret = this->_bus->read(this->_registers.pac); @@ -349,12 +354,12 @@ namespace ComSquare::CPU return value; } - std::string CPU::getName() + std::string CPU::getName() const { return "CPU"; } - Component CPU::getComponent() + Component CPU::getComponent() const { return Cpu; } diff --git a/sources/CPU/CPU.hpp b/sources/CPU/CPU.hpp index 9db4b3f..7872bbf 100644 --- a/sources/CPU/CPU.hpp +++ b/sources/CPU/CPU.hpp @@ -741,18 +741,22 @@ namespace ComSquare::CPU //! @param addr The address to read from. The address 0x0 should refer to the first byte of the register. //! @throw InvalidAddress will be thrown if the address is more than $1F (the number of register). //! @return Return the value of the register. - uint8_t read(uint24_t addr) override; + uint8_t read(uint24_t addr) const override; //! @brief Write data to the internal CPU register. //! @param addr The address to write to. The address 0x0 should refer to the first byte of register. //! @param data The new value of the register. //! @throw InvalidAddress will be thrown if the address is more than $1F (the number of register). void write(uint24_t addr, uint8_t data) override; + //! @brief Get the size of the data. This size can be lower than the mapped data. + //! @return The number of bytes inside this memory. + uint24_t getSize() const override; + //! @brief Get the name of this accessor (used for debug purpose) - std::string getName() override; + std::string getName() const override; //! @brief Get the component of this accessor (used for debug purpose) - Component getComponent() override; + Component getComponent() const override; //! @brief Reset interrupt - Called on boot and when the reset button is pressed. virtual int RESB(); @@ -765,7 +769,7 @@ namespace ComSquare::CPU bool IsAbortRequested = false; //! @brief Return true if the CPU is overloaded with debugging features. - virtual bool isDebugger(); + virtual bool isDebugger() const; //! @brief Change the memory bus used by the CPU. virtual void setMemoryBus(std::shared_ptr bus); diff --git a/sources/CPU/DMA/DMA.cpp b/sources/CPU/DMA/DMA.cpp index 306ed9b..9c049ca 100644 --- a/sources/CPU/DMA/DMA.cpp +++ b/sources/CPU/DMA/DMA.cpp @@ -15,7 +15,7 @@ namespace ComSquare::CPU this->_bus = std::move(bus); } - uint8_t DMA::read(uint8_t addr) + uint8_t DMA::read(uint8_t addr) const { switch (addr) { case 0x0: diff --git a/sources/CPU/DMA/DMA.hpp b/sources/CPU/DMA/DMA.hpp index de6d232..eef46dc 100644 --- a/sources/CPU/DMA/DMA.hpp +++ b/sources/CPU/DMA/DMA.hpp @@ -94,7 +94,7 @@ namespace ComSquare::CPU void setBus(std::shared_ptr bus); //! @brief Bus helper to read from this channel. - uint8_t read(uint8_t addr); + uint8_t read(uint8_t addr) const; //! @brief Bus helper to write to this channel. void write(uint8_t addr, uint8_t data); diff --git a/sources/Cartridge/Cartridge.cpp b/sources/Cartridge/Cartridge.cpp index 8156c54..ead9363 100644 --- a/sources/Cartridge/Cartridge.cpp +++ b/sources/Cartridge/Cartridge.cpp @@ -38,7 +38,7 @@ namespace ComSquare::Cartridge } - uint8_t Cartridge::read(uint24_t addr) + uint8_t Cartridge::read(uint24_t addr) const { return Ram::read(addr + this->_romStart); } diff --git a/sources/Cartridge/Cartridge.hpp b/sources/Cartridge/Cartridge.hpp index 4b4ea0a..90562e8 100644 --- a/sources/Cartridge/Cartridge.hpp +++ b/sources/Cartridge/Cartridge.hpp @@ -98,7 +98,7 @@ namespace ComSquare::Cartridge //! @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. //! @return Return the data at the address. - uint8_t read(uint24_t addr) override; + uint8_t read(uint24_t addr) const override; //! @brief Write data to the rom. //! @param addr The address to write to. The address 0x0 should refer to the first byte of the rom's memory. //! @param data The data to write. diff --git a/sources/Debugger/APUDebug.cpp b/sources/Debugger/APUDebug.cpp index 6e11913..62e2519 100644 --- a/sources/Debugger/APUDebug.cpp +++ b/sources/Debugger/APUDebug.cpp @@ -522,7 +522,7 @@ namespace ComSquare::Debugger this->_snes.disableAPUDebugging(); } - bool APUDebug::isDebugger() + bool APUDebug::isDebugger() const { return true; } diff --git a/sources/Debugger/APUDebug.hpp b/sources/Debugger/APUDebug.hpp index db7628f..f907208 100644 --- a/sources/Debugger/APUDebug.hpp +++ b/sources/Debugger/APUDebug.hpp @@ -57,7 +57,7 @@ namespace ComSquare::Debugger //! @brief Return true if the CPU is overloaded with debugging features. - bool isDebugger() override; + bool isDebugger() const override; //! @brief Focus the debugger's window. void focus(); diff --git a/sources/Debugger/CPU/CPUDebug.cpp b/sources/Debugger/CPU/CPUDebug.cpp index c38be29..fda754d 100644 --- a/sources/Debugger/CPU/CPUDebug.cpp +++ b/sources/Debugger/CPU/CPUDebug.cpp @@ -8,6 +8,7 @@ #include #include #include +#include using namespace ComSquare::CPU; @@ -58,7 +59,7 @@ namespace ComSquare::Debugger this->_updateDisassembly(this->_registers.pac, 0); } - bool CPUDebug::isDebugger() + bool CPUDebug::isDebugger() const { return true; } @@ -387,7 +388,9 @@ QSize RowPainter::sizeHint(const QStyleOptionViewItem &, const QModelIndex &) co return QSize(); } -StackModel::StackModel(std::shared_ptr bus, ComSquare::Debugger::CPUDebug &cpu) : _bus(bus), _cpu(cpu) { } +StackModel::StackModel(std::shared_ptr bus, ComSquare::Debugger::CPUDebug &cpu) + : _bus(std::move(bus)), _cpu(cpu) +{ } void StackModel::setMemoryBus(std::shared_ptr bus) { diff --git a/sources/Debugger/CPU/CPUDebug.hpp b/sources/Debugger/CPU/CPUDebug.hpp index 69d3e0e..57f403b 100644 --- a/sources/Debugger/CPU/CPUDebug.hpp +++ b/sources/Debugger/CPU/CPUDebug.hpp @@ -281,7 +281,7 @@ namespace ComSquare::Debugger ~CPUDebug() override = default; //! @brief Return true if the CPU is overloaded with debugging features. - bool isDebugger() override; + bool isDebugger() const override; //! @brief Focus the debugger's window. void focus(); diff --git a/sources/Debugger/MemoryViewer.cpp b/sources/Debugger/MemoryViewer.cpp index 5cbd6bc..fa6e760 100644 --- a/sources/Debugger/MemoryViewer.cpp +++ b/sources/Debugger/MemoryViewer.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include "MemoryViewer.hpp" #include "../SNES.hpp" #include "../Memory/MemoryShadow.hpp" @@ -147,11 +148,17 @@ namespace ComSquare::Debugger if (dialogUI.checkBox->isChecked()) { try { value = this->switchToAddrTab(value); - } catch (const InvalidAddress &) {} + } catch (const InvalidAddress &) { + QMessageBox msgBox; + msgBox.setText("This address is not mapped. Reading it will result in OpenBus."); + msgBox.exec(); + return; + } } QModelIndex index = this->_ui.tableView->model()->index(value >> 4, value & 0x0F); this->_ui.tableView->scrollTo(index); this->_ui.tableView->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect); + this->_ui.tableView->setCurrentIndex(index); } unsigned MemoryViewer::switchToAddrTab(uint24_t addr) @@ -173,7 +180,14 @@ namespace ComSquare::Debugger default: throw InvalidAddress("Memory viewer switch to address", addr); } - return accessor->getRelativeAddress(addr); + addr = accessor->getRelativeAddress(addr); + if (addr > accessor->getSize()) { + QMessageBox msgBox; + msgBox.setText((std::string("The ") + accessor->getName() + " is too small to contain this address.").c_str()); + msgBox.exec(); + return 0; + } + return addr; } void MemoryViewer::focus() diff --git a/sources/Memory/AMemory.cpp b/sources/Memory/AMemory.cpp index 3132424..70ea7b5 100644 --- a/sources/Memory/AMemory.cpp +++ b/sources/Memory/AMemory.cpp @@ -6,7 +6,7 @@ namespace ComSquare::Memory { - uint24_t AMemory::getRelativeAddress(uint24_t addr) + uint24_t AMemory::getRelativeAddress(uint24_t addr) const { return addr - this->_start; } @@ -17,22 +17,22 @@ namespace ComSquare::Memory this->_end = end; } - bool AMemory::hasMemoryAt(uint24_t addr) + bool AMemory::hasMemoryAt(uint24_t addr) const { return this->_start <= addr && addr <= this->_end; } - bool AMemory::isMirror() + bool AMemory::isMirror() const { return false; } - std::shared_ptr AMemory::getMirrored() + std::shared_ptr AMemory::getMirrored() const { return nullptr; } - std::string AMemory::getValueName(uint24_t) + std::string AMemory::getValueName(uint24_t) const { return "???"; } diff --git a/sources/Memory/AMemory.hpp b/sources/Memory/AMemory.hpp index 210aa49..575226a 100644 --- a/sources/Memory/AMemory.hpp +++ b/sources/Memory/AMemory.hpp @@ -26,7 +26,7 @@ namespace ComSquare::Memory //! @param addr The absolute address (in the 24 bit bus) //! @return The local address (0 refers to the first byte of this component). //! @throw InvalidAddress is thrown if the address is not mapped by this component. - virtual uint24_t getRelativeAddress(uint24_t addr) override; + virtual uint24_t getRelativeAddress(uint24_t addr) const override; //! @brief Change starting and ending points of this mapped memory. //! @param start The first address mapped to this component. //! @param end The last address mapped to this component. @@ -35,16 +35,16 @@ namespace ComSquare::Memory //! @brief Return true if this component has mapped the address. //! @param addr The address to check. //! @return True if this address is mapped to the component. False otherwise. - virtual bool hasMemoryAt(uint24_t addr) override; + virtual bool hasMemoryAt(uint24_t addr) const override; //! @brief Check if this memory is a mirror or not. //! @return True if this memory is a mirror. False otherwise. - virtual bool isMirror() override; + virtual bool isMirror() const override; //! @brief Get the name of the data at the address //! @param addr The address (in local space) - virtual std::string getValueName(uint24_t addr) override; + virtual std::string getValueName(uint24_t addr) const override; //! @brief Return the memory accessor this accessor mirror if any //! @return nullptr if isMirror is false, the source otherwise. - virtual std::shared_ptr getMirrored() override; + virtual std::shared_ptr getMirrored() const override; virtual ~AMemory() override = default; }; } \ No newline at end of file diff --git a/sources/Memory/ARectangleMemory.cpp b/sources/Memory/ARectangleMemory.cpp index 5c6b392..e1be35b 100644 --- a/sources/Memory/ARectangleMemory.cpp +++ b/sources/Memory/ARectangleMemory.cpp @@ -5,11 +5,10 @@ #include #include "ARectangleMemory.hpp" #include "../Exceptions/InvalidAddress.hpp" -#include "../Utility/Utility.hpp" namespace ComSquare::Memory { - uint24_t ARectangleMemory::getRelativeAddress(uint24_t addr) + uint24_t ARectangleMemory::getRelativeAddress(uint24_t addr) const { uint8_t bank = addr >> 16u; uint16_t page = addr; @@ -24,7 +23,7 @@ namespace ComSquare::Memory return pageCount * bankCount + page; } - bool ARectangleMemory::hasMemoryAt(uint24_t addr) + bool ARectangleMemory::hasMemoryAt(uint24_t addr) const { uint8_t bank = addr >> 16u; uint16_t page = addr; @@ -43,17 +42,17 @@ namespace ComSquare::Memory this->_endPage = endPage; } - bool ARectangleMemory::isMirror() + bool ARectangleMemory::isMirror() const { return false; } - std::shared_ptr ARectangleMemory::getMirrored() + std::shared_ptr ARectangleMemory::getMirrored() const { return nullptr; } - std::string ARectangleMemory::getValueName(uint24_t) + std::string ARectangleMemory::getValueName(uint24_t) const { return "???"; } diff --git a/sources/Memory/ARectangleMemory.hpp b/sources/Memory/ARectangleMemory.hpp index e923bb3..347db58 100644 --- a/sources/Memory/ARectangleMemory.hpp +++ b/sources/Memory/ARectangleMemory.hpp @@ -24,11 +24,11 @@ namespace ComSquare::Memory //! @param addr The absolute address (in the 24 bit bus) //! @return The local address (0 refers to the first byte of this component). //! @throw InvalidAddress is thrown if the address is not mapped by this component. - virtual uint24_t getRelativeAddress(uint24_t addr) override; + virtual uint24_t getRelativeAddress(uint24_t addr) const override; //! @brief Return true if this component has mapped the address. //! @param addr The address to check. //! @return True if this address is mapped to the component. False otherwise. - bool hasMemoryAt(uint24_t addr) override; + bool hasMemoryAt(uint24_t addr) const override; //! @brief Change starting and ending points of this mapped memory. //! @param startBank The first bank mapped to this component. //! @param endBank The last bank mapped to this component. @@ -38,13 +38,13 @@ namespace ComSquare::Memory void setMemoryRegion(uint8_t startBank, uint8_t endBank, uint16_t startPage, uint16_t endPage); //! @brief Check if this memory is a mirror or not. //! @return True if this memory is a mirror. False otherwise. - virtual bool isMirror() override; + virtual bool isMirror() const override; //! @brief Get the name of the data at the address //! @param addr The address (in local space) - virtual std::string getValueName(uint24_t addr) override; + virtual std::string getValueName(uint24_t addr) const override; //! @brief Return the memory accessor this accessor mirror if any //! @return nullptr if isMirror is false, the source otherwise. - virtual std::shared_ptr getMirrored() override; + virtual std::shared_ptr getMirrored() const override; virtual ~ARectangleMemory() override = default; }; } \ No newline at end of file diff --git a/sources/Memory/IMemory.hpp b/sources/Memory/IMemory.hpp index 14370af..fb36045 100644 --- a/sources/Memory/IMemory.hpp +++ b/sources/Memory/IMemory.hpp @@ -20,7 +20,7 @@ namespace ComSquare::Memory //! @param addr The local address to read from (0x0 should refer to the first byte of this component). //! @throw This function should thrown an InvalidAddress for address that are not mapped to the component. //! @return Return the data at the address given as parameter. - virtual uint8_t read(uint24_t addr) = 0; + virtual uint8_t read(uint24_t addr) const = 0; //! @brief Write data to this component. //! @param addr The local address to write data (0x0 should refer to the first byte of this component). //! @param data The new data to write. @@ -29,25 +29,28 @@ namespace ComSquare::Memory //! @brief Return true if this component has mapped the address. //! @param addr The address to check. //! @return True if this address is mapped to the component. False otherwise. - virtual bool hasMemoryAt(uint24_t addr) = 0; + virtual bool hasMemoryAt(uint24_t addr) const = 0; //! @brief Translate an absolute address to a relative address //! @param addr The absolute address (in the 24 bit bus) //! @return The local address (0 refers to the first byte of this component). //! @throw InvalidAddress is thrown if the address is not mapped by this component. - virtual uint24_t getRelativeAddress(uint24_t addr) = 0; + virtual uint24_t getRelativeAddress(uint24_t addr) const = 0; + //! @brief Get the size of the data. This size can be lower than the mapped data. + //! @return The number of bytes inside this memory. + virtual uint24_t getSize() const = 0; //! @brief Check if this memory is a mirror or not. //! @return True if this memory is a mirror. False otherwise. - virtual bool isMirror() = 0; + virtual bool isMirror() const = 0; //! @brief Get the name of this accessor (used for debug purpose) - virtual std::string getName() = 0; + virtual std::string getName() const = 0; //! @brief Get the component of this accessor (used for debug purpose) - virtual Component getComponent() = 0; + virtual Component getComponent() const = 0; //! @brief Get the name of the data at the address //! @param addr The address (in local space) - virtual std::string getValueName(uint24_t addr) = 0; + virtual std::string getValueName(uint24_t addr) const = 0; //! @brief Return the memory accessor this accessor mirror if any //! @return nullptr if isMirror is false, the source otherwise. - virtual std::shared_ptr getMirrored() = 0; + virtual std::shared_ptr getMirrored() const = 0; virtual ~IMemory() = default; }; }; \ No newline at end of file diff --git a/sources/Memory/MemoryShadow.cpp b/sources/Memory/MemoryShadow.cpp index 4912fc5..40434db 100644 --- a/sources/Memory/MemoryShadow.cpp +++ b/sources/Memory/MemoryShadow.cpp @@ -14,7 +14,7 @@ namespace ComSquare::Memory this->setMemoryRegion(start, end); } - uint8_t MemoryShadow::read(uint24_t addr) + uint8_t MemoryShadow::read(uint24_t addr) const { return this->_initial->read(addr); } @@ -24,22 +24,27 @@ namespace ComSquare::Memory return this->_initial->write(addr, data); } - bool MemoryShadow::isMirror() + uint24_t MemoryShadow::getSize() const + { + return this->_initial->getSize(); + } + + bool MemoryShadow::isMirror() const { return true; } - std::shared_ptr MemoryShadow::getMirrored() + std::shared_ptr MemoryShadow::getMirrored() const { return this->_initial; } - std::string MemoryShadow::getName() + std::string MemoryShadow::getName() const { return this->_initial->getName(); } - Component MemoryShadow::getComponent() + Component MemoryShadow::getComponent() const { return this->_initial->getComponent(); } diff --git a/sources/Memory/MemoryShadow.hpp b/sources/Memory/MemoryShadow.hpp index 519a4c6..8b2c037 100644 --- a/sources/Memory/MemoryShadow.hpp +++ b/sources/Memory/MemoryShadow.hpp @@ -24,21 +24,24 @@ namespace ComSquare::Memory //! @param addr The address to read from. The address 0x0 should refer to the first byte of the initial AMemory. //! @throw InvalidAddress will be thrown if the address is more than the size of the initial AMemory. //! @return Return the data at the address. - uint8_t read(uint24_t addr) override; + uint8_t read(uint24_t addr) const 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 AMemory. //! @param data The data to write. //! @throw InvalidAddress will be thrown if the address is more than the size of the initial AMemory. void write(uint24_t addr, uint8_t data) override; + //! @brief Get the size of the data. This size can be lower than the mapped data. + //! @return The number of bytes inside this memory. + virtual uint24_t getSize() const override; //! @brief Check if this memory is a mirror or not. //! @return True if this memory is a mirror. False otherwise. - bool isMirror() override; + bool isMirror() const override; //! @brief Get the name of this accessor (used for debug purpose) - std::string getName() override; + std::string getName() const override; //! @brief Get the component of this accessor (used for debug purpose) - Component getComponent() override; + Component getComponent() const override; //! @brief Return the memory accessor this accessor mirror if any //! @return nullptr if isMirror is false, the source otherwise. - std::shared_ptr getMirrored() override; + std::shared_ptr getMirrored() const override; }; } \ No newline at end of file diff --git a/sources/Memory/RectangleShadow.cpp b/sources/Memory/RectangleShadow.cpp index 182a4fc..8a7ad5e 100644 --- a/sources/Memory/RectangleShadow.cpp +++ b/sources/Memory/RectangleShadow.cpp @@ -18,13 +18,13 @@ namespace ComSquare::Memory this->setMemoryRegion(startBank, endBank, startPage, endPage); } - uint24_t RectangleShadow::getRelativeAddress(uint24_t addr) + uint24_t RectangleShadow::getRelativeAddress(uint24_t addr) const { uint24_t base = ARectangleMemory::getRelativeAddress(addr); return base + this->_bankOffset * (this->_endPage - this->_startPage); } - uint8_t RectangleShadow::read(uint24_t addr) + uint8_t RectangleShadow::read(uint24_t addr) const { return this->_initial->read(addr); } @@ -40,22 +40,27 @@ namespace ComSquare::Memory return this; } - bool RectangleShadow::isMirror() + uint24_t RectangleShadow::getSize() const + { + return this->_initial->getSize(); + } + + bool RectangleShadow::isMirror() const { return true; } - std::shared_ptr RectangleShadow::getMirrored() + std::shared_ptr RectangleShadow::getMirrored() const { return this->_initial; } - std::string RectangleShadow::getName() + std::string RectangleShadow::getName() const { return this->_initial->getName(); } - Component RectangleShadow::getComponent() + Component RectangleShadow::getComponent() const { return this->_initial->getComponent(); } diff --git a/sources/Memory/RectangleShadow.hpp b/sources/Memory/RectangleShadow.hpp index 2c6cb24..2365550 100644 --- a/sources/Memory/RectangleShadow.hpp +++ b/sources/Memory/RectangleShadow.hpp @@ -27,7 +27,7 @@ namespace ComSquare::Memory //! @param addr The address to read from. The address 0x0 should refer to the first byte of the initial AMemory. //! @throw InvalidAddress will be thrown if the address is more than the size of the initial AMemory. //! @return Return the data at the address. - uint8_t read(uint24_t addr) override; + uint8_t read(uint24_t addr) const 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 AMemory. //! @param data The data to write. @@ -37,17 +37,20 @@ namespace ComSquare::Memory //! @param addr The absolute address (in the 24 bit bus) //! @return The local address (0 refers to the first byte of this component). //! @throw InvalidAddress is thrown if the address is not mapped by this component. - uint24_t getRelativeAddress(uint24_t addr) override; + uint24_t getRelativeAddress(uint24_t addr) const override; + //! @brief Get the size of the data. This size can be lower than the mapped data. + //! @return The number of bytes inside this memory. + virtual uint24_t getSize() const override; //! @brief Check if this memory is a mirror or not. //! @return True if this memory is a mirror. False otherwise. - bool isMirror() override; + bool isMirror() const override; //! @brief Get the name of this accessor (used for debug purpose) - std::string getName() override; + std::string getName() const override; //! @brief Get the component of this accessor (used for debug purpose) - Component getComponent() override; + Component getComponent() const override; //! @brief Return the memory accessor this accessor mirror if any //! @return nullptr if isMirror is false, the source otherwise. - std::shared_ptr getMirrored() override; + std::shared_ptr getMirrored() const override; RectangleShadow *setBankOffset(uint8_t bankOffset); }; diff --git a/sources/PPU/PPU.cpp b/sources/PPU/PPU.cpp index f931a8e..8844a24 100644 --- a/sources/PPU/PPU.cpp +++ b/sources/PPU/PPU.cpp @@ -22,7 +22,7 @@ namespace ComSquare::PPU } } - uint8_t PPU::read(uint24_t addr) + uint8_t PPU::read(uint24_t addr) const { switch (addr) { case ppuRegisters::mpyl: @@ -216,6 +216,11 @@ namespace ComSquare::PPU } } + uint24_t PPU::getSize() const + { + return 0x3F; + } + uint16_t PPU::getVramAddress() { uint16_t vanillaAddress = this->_registers._vmadd.vmadd * 2; @@ -262,12 +267,12 @@ namespace ComSquare::PPU this->_renderer.drawScreen(); } - std::string PPU::getName() + std::string PPU::getName() const { return "PPU"; } - std::string PPU::getValueName(uint24_t addr) + std::string PPU::getValueName(uint24_t addr) const { switch (addr) { case ppuRegisters::inidisp: @@ -403,12 +408,12 @@ namespace ComSquare::PPU } } - Component PPU::getComponent() + Component PPU::getComponent() const { return Ppu; } - bool PPU::isDebugger() + bool PPU::isDebugger() const { return false; } diff --git a/sources/PPU/PPU.hpp b/sources/PPU/PPU.hpp index c8cdf30..6436f74 100644 --- a/sources/PPU/PPU.hpp +++ b/sources/PPU/PPU.hpp @@ -562,16 +562,19 @@ namespace ComSquare::PPU //! @param addr The local address to read from (0x0 should refer to the first byte of this component). //! @throw This function should thrown an InvalidAddress for address that are not mapped to the component. //! @return Return the data at the address given as parameter. - uint8_t read(uint24_t addr) override; + uint8_t read(uint24_t addr) const override; //! @brief Write data to this component. //! @param addr The local address to write data (0x0 should refer to the first byte of this component). //! @param data The new data to write. //! @throw This function should thrown an InvalidAddress for address that are not mapped to the component. void write(uint24_t addr, uint8_t data) override; //! @brief Get the name of this accessor (used for debug purpose) - std::string getName() override; + std::string getName() const override; //! @brief Get the component of this accessor (used for debug purpose) - Component getComponent() override; + Component getComponent() const override; + //! @brief Get the size of the data. This size can be lower than the mapped data. + //! @return The number of bytes inside this memory. + uint24_t getSize() const override; //! @brief Update the PPU of n cycles. //! @param The number of cycles to update. @@ -579,9 +582,9 @@ namespace ComSquare::PPU //! @brief Give the Vram Address with the right Address remapping uint16_t getVramAddress(); //! @brief Give the name of the Address register (used for debug) - std::string getValueName(uint24_t addr); + std::string getValueName(uint24_t addr) const; //! @brief Return true if the CPU is overloaded with debugging features. - virtual bool isDebugger(); + virtual bool isDebugger() const; //! @brief Allow others components to read the CGRAM (Debuggers) uint16_t cgramRead(uint16_t addr); //! @brief Render a background on the screen diff --git a/sources/Ram/Ram.cpp b/sources/Ram/Ram.cpp index da6ab26..de8dc98 100644 --- a/sources/Ram/Ram.cpp +++ b/sources/Ram/Ram.cpp @@ -27,8 +27,9 @@ namespace ComSquare::Ram delete[] this->_data; } - uint8_t Ram::read(uint24_t addr) + uint8_t Ram::read(uint24_t addr) const { + // TODO read/write after the size of the rom should noop or behave like a mirror. I don't really know. if (addr >= this->_size) throw InvalidAddress(this->getName() + " read", addr); return this->_data[addr]; @@ -41,17 +42,17 @@ namespace ComSquare::Ram this->_data[addr] = data; } - size_t Ram::getSize() + uint24_t Ram::getSize() const { return this->_size; } - std::string Ram::getName() + std::string Ram::getName() const { return this->_ramName; } - Component Ram::getComponent() + Component Ram::getComponent() const { return this->_ramType; } diff --git a/sources/Ram/Ram.hpp b/sources/Ram/Ram.hpp index 4fe2a41..d3e4916 100644 --- a/sources/Ram/Ram.hpp +++ b/sources/Ram/Ram.hpp @@ -15,7 +15,7 @@ namespace ComSquare::Ram //! @brief The ram. (Can be used for WRam, SRam, VRam etc) uint8_t *_data; //! @brief The size of the ram (in bytes). - size_t _size; + uint24_t _size; //! @brief An id identifying the type of memory this is (for the debugger) Component _ramType; //! @brief The name of this ram. @@ -34,7 +34,7 @@ namespace ComSquare::Ram //! @param addr The local address to read from (0x0 should refer to the first byte of this component). //! @throw This function should thrown an InvalidAddress for address that are not mapped to the component. //! @return Return the data at the address given as parameter. - uint8_t read(uint24_t addr) override; + uint8_t read(uint24_t addr) const override; //! @brief Write data to this component. //! @param addr The local address to write data (0x0 should refer to the first byte of this component). //! @param data The new data to write. @@ -43,13 +43,13 @@ namespace ComSquare::Ram //! @brief Get the name of this accessor (used for debug purpose) - std::string getName() override; + std::string getName() const override; //! @brief Get the component of this accessor (used for debug purpose) - Component getComponent() override; + Component getComponent() const override; //! @brief Get the size of the ram in bytes. - size_t getSize(); + uint24_t getSize() const override; }; }