diff --git a/CMakeLists.txt b/CMakeLists.txt index bbb0d09..05ad390 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,7 +63,7 @@ add_executable(unit_tests tests/CPU/testInternal.cpp sources/Ram/ExtendedRam.cpp sources/Ram/ExtendedRam.hpp - sources/Utility/Utility.hpp) + sources/Utility/Utility.hpp sources/Utility/Utility.cpp) # include criterion & coverage target_link_libraries(unit_tests criterion -lgcov) @@ -130,8 +130,12 @@ add_executable(ComSquare sources/Renderer/QtRenderer/QtWidgetSFML.cpp sources/Renderer/QtRenderer/QtWidgetSFML.hpp ui/cpu.ui + ui/ramView.ui resources/appResources.qrc - sources/Utility/Utility.hpp sources/Debugger/RamViewer.cpp sources/Debugger/RamViewer.hpp) + sources/Utility/Utility.hpp + sources/Debugger/MemoryViewer.cpp + sources/Debugger/MemoryViewer.hpp + sources/Utility/Utility.cpp) target_compile_definitions(ComSquare PUBLIC DEBUGGER_ENABLED) diff --git a/sources/Debugger/MemoryViewer.cpp b/sources/Debugger/MemoryViewer.cpp new file mode 100644 index 0000000..2b2d47a --- /dev/null +++ b/sources/Debugger/MemoryViewer.cpp @@ -0,0 +1,57 @@ +// +// Created by anonymus-raccoon on 2/17/20. +// + +#include +#include "MemoryViewer.hpp" +#include "../SNES.hpp" +#include "../Utility/Utility.hpp" + +MemoryViewerModel::MemoryViewerModel(std::shared_ptr memory, QObject *parent) : + QAbstractTableModel(parent), + _memory(std::move(memory)) +{ } + +int MemoryViewerModel::rowCount(const QModelIndex &) const +{ + return this->_memory->getSize() / 16u; +} + +int MemoryViewerModel::columnCount(const QModelIndex &parent) const +{ + if (parent.row() == this->rowCount(parent) - 1) + return this->_memory->getSize() - (parent.row() << 8u); + return 16; +} + +QVariant MemoryViewerModel::data(const QModelIndex &index, int role) const +{ + if (role != Qt::DisplayRole) + return QVariant(); + return QString(ComSquare::Utility::to_hex(this->_memory->read_internal((index.row() << 4u) + index.column())).c_str()); +} + +QVariant MemoryViewerModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (role != Qt::DisplayRole) + return QVariant(); + if (orientation == Qt::Horizontal) + return QString(ComSquare::Utility::to_hex(static_cast(section)).c_str()); + else + return QString(ComSquare::Utility::to_hex(static_cast(section)).c_str()); +} + + +namespace ComSquare::Debugger +{ + MemoryViewer::MemoryViewer(ComSquare::SNES &snes) : + QMainWindow(), _snes(snes), _ui(), _model(snes.wram) + { + this->setContextMenuPolicy(Qt::NoContextMenu); + this->setAttribute(Qt::WA_QuitOnClose, false); + + this->_ui.setupUi(this); + this->_ui.tableView->setModel(&this->_model); + this->show(); + } +} \ No newline at end of file diff --git a/sources/Debugger/MemoryViewer.hpp b/sources/Debugger/MemoryViewer.hpp new file mode 100644 index 0000000..04f52ba --- /dev/null +++ b/sources/Debugger/MemoryViewer.hpp @@ -0,0 +1,55 @@ +// +// Created by anonymus-raccoon on 2/17/20. +// + +#ifndef COMSQUARE_MEMORYVIEWER_HPP +#define COMSQUARE_MEMORYVIEWER_HPP + +#include +#include "../../ui/ui_ramView.h" +#include "../Ram/Ram.hpp" + +using ComSquare::Ram::Ram; + +//! @brief The qt model that bind the ram to the view. +class MemoryViewerModel : public QAbstractTableModel +{ +Q_OBJECT +private: + std::shared_ptr _memory; +public: + explicit MemoryViewerModel(std::shared_ptr memory, QObject *parent = nullptr); + MemoryViewerModel(const MemoryViewerModel &) = delete; + const MemoryViewerModel &operator=(const MemoryViewerModel &) = delete; + ~MemoryViewerModel() override = default; + int rowCount(const QModelIndex &parent) const override; + int columnCount(const QModelIndex &parent) const override; + QVariant data(const QModelIndex &index, int role) const override; + QVariant headerData(int section, Qt::Orientation orientation, int role) const override; +}; + + +namespace ComSquare +{ + class SNES; + namespace Debugger + { + //! @brief Class responsible of the Memory Viewer. + class MemoryViewer : public QMainWindow { + private: + //! @brief SNES containing all rams to view. + SNES &_snes; + //! @brief The layout of the viewer. + Ui::RamView _ui; + //! @brief The Ram visualizer model for QT. + MemoryViewerModel _model; + public: + explicit MemoryViewer(SNES &snes); + MemoryViewer(const MemoryViewer &) = delete; + MemoryViewer &operator=(const MemoryViewer &) = delete; + ~MemoryViewer() override = default; + }; + } +} + +#endif //COMSQUARE_MEMORYVIEWER_HPP diff --git a/sources/Debugger/RamViewer.cpp b/sources/Debugger/RamViewer.cpp deleted file mode 100644 index 6ead5b6..0000000 --- a/sources/Debugger/RamViewer.cpp +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by anonymus-raccoon on 2/17/20. -// - -#include "RamViewer.hpp" -#include "../SNES.hpp" - -namespace ComSquare::Debugger -{ - RamViewer::RamViewer(ComSquare::SNES &snes) : - QMainWindow(), _snes(snes) - { - this->setContextMenuPolicy(Qt::NoContextMenu); - this->setAttribute(Qt::WA_QuitOnClose, false); - -// this->_ui.setupUi(this); - this->show(); - } -} \ No newline at end of file diff --git a/sources/Debugger/RamViewer.hpp b/sources/Debugger/RamViewer.hpp deleted file mode 100644 index eaac6aa..0000000 --- a/sources/Debugger/RamViewer.hpp +++ /dev/null @@ -1,30 +0,0 @@ -// -// Created by anonymus-raccoon on 2/17/20. -// - -#ifndef COMSQUARE_RAMVIEWER_HPP -#define COMSQUARE_RAMVIEWER_HPP - -#include - -namespace ComSquare -{ - class SNES; - namespace Debugger - { - class RamViewer : public QMainWindow { - private: - //! @brief SNES containing all rams to view. - SNES &_snes; - //! @brief The layout of the viewer. -// Ui::RamView _ui; - public: - explicit RamViewer(SNES &snes); - RamViewer(const RamViewer &) = delete; - RamViewer &operator=(const RamViewer &) = delete; - ~RamViewer() override = default; - }; - } -} - -#endif //COMSQUARE_RAMVIEWER_HPP diff --git a/sources/Ram/Ram.cpp b/sources/Ram/Ram.cpp index 98a3498..0f37fe4 100644 --- a/sources/Ram/Ram.cpp +++ b/sources/Ram/Ram.cpp @@ -42,4 +42,9 @@ namespace ComSquare::Ram throw InvalidAddress("Ram memset start", start); std::memset(&this->_data[start], value, sizeof(uint8_t) * (end - start)); } + + size_t Ram::getSize() + { + return this->_size; + } } diff --git a/sources/Ram/Ram.hpp b/sources/Ram/Ram.hpp index b659b2a..5606e05 100644 --- a/sources/Ram/Ram.hpp +++ b/sources/Ram/Ram.hpp @@ -13,10 +13,10 @@ namespace ComSquare::Ram private: //! @brief The ram. (Can be used for WRam, SRam, VRam etc) uint8_t *_data; - //! @brief The size of the ram. + //! @brief The size of the ram (iny bytes). size_t _size; public: - //! @brief Load a rom from it's path. + //! @brief Create a ram of a given size in bytes. explicit Ram(size_t size); //! @brief The ram can't be copied. Ram(const Ram &) = delete; @@ -40,6 +40,9 @@ namespace ComSquare::Ram //! @param end end address to replace //! @param value replace value void memset(uint24_t start, uint24_t end, uint8_t value); + + //! @brief Get the size of the ram in bytes. + size_t getSize(); }; } diff --git a/sources/Renderer/QtRenderer/QtSFML.cpp b/sources/Renderer/QtRenderer/QtSFML.cpp index 9f6ca27..8ffeed4 100644 --- a/sources/Renderer/QtRenderer/QtSFML.cpp +++ b/sources/Renderer/QtRenderer/QtSFML.cpp @@ -43,7 +43,7 @@ namespace ComSquare::Renderer cpuDebugger->setShortcut(Qt::Key_F1); QMainWindow::connect(cpuDebugger, &QAction::triggered, this->_sfWidget.get(), &QtFullSFML::enableDebugCPU); debugger->addAction(cpuDebugger); - QAction *ramViewer = new QAction("Ram viewer", &this->_window); + QAction *ramViewer = new QAction("Memory viewer", &this->_window); ramViewer->setShortcut(Qt::Key_F2); QMainWindow::connect(ramViewer, &QAction::triggered, this->_sfWidget.get(), &QtFullSFML::enableRamViewer); debugger->addAction(ramViewer); diff --git a/sources/SNES.cpp b/sources/SNES.cpp index 9a0983e..6026a56 100644 --- a/sources/SNES.cpp +++ b/sources/SNES.cpp @@ -39,7 +39,7 @@ namespace ComSquare void SNES::enableRamViewer() { #ifdef DEBUGGER_ENABLED - this->_ramViewer = std::make_shared(*this); + this->_ramViewer = std::make_shared(*this); #endif } diff --git a/sources/SNES.hpp b/sources/SNES.hpp index 7364b81..9c7ef7f 100644 --- a/sources/SNES.hpp +++ b/sources/SNES.hpp @@ -13,7 +13,7 @@ #include "APU/APU.hpp" #include "Renderer/IRenderer.hpp" #ifdef DEBUGGER_ENABLED -#include "Debugger/RamViewer.hpp" +#include "Debugger/MemoryViewer.hpp" #endif namespace ComSquare @@ -22,7 +22,7 @@ namespace ComSquare class SNES { #ifdef DEBUGGER_ENABLED private: - std::shared_ptr _ramViewer; + std::shared_ptr _ramViewer; #endif public: //! @brief Cartridge containing instructions (ROM). diff --git a/sources/Utility/Utility.cpp b/sources/Utility/Utility.cpp new file mode 100644 index 0000000..2273741 --- /dev/null +++ b/sources/Utility/Utility.cpp @@ -0,0 +1,29 @@ +// +// Created by anonymus-raccoon on 2/17/20. +// + +#include "Utility.hpp" + +namespace ComSquare::Utility +{ + std::string to_hex(uint8_t i) + { + char buf[5]; + sprintf(buf, "0x%02X", i); + return buf; + } + + std::string to_hex(uint16_t i) + { + char buf[7]; + sprintf(buf, "0x%04X", i); + return buf; + } + + std::string to_hex(uint24_t i) + { + char buf[9]; + sprintf(buf, "0x%06X", i); + return buf; + } +} \ No newline at end of file diff --git a/sources/Utility/Utility.hpp b/sources/Utility/Utility.hpp index f0f28df..367995a 100644 --- a/sources/Utility/Utility.hpp +++ b/sources/Utility/Utility.hpp @@ -12,26 +12,11 @@ namespace ComSquare::Utility { - std::string to_hex(uint8_t i) - { - char buf[5]; - sprintf(buf, "0x%02X", i); - return buf; - } + std::string to_hex(uint8_t i); - std::string to_hex(uint16_t i) - { - char buf[7]; - sprintf(buf, "0x%04X", i); - return buf; - } + std::string to_hex(uint16_t i); - std::string to_hex(uint24_t i) - { - char buf[9]; - sprintf(buf, "0x%06X", i); - return buf; - } + std::string to_hex(uint24_t i); } #endif //COMSQUARE_UTILITY_HPP diff --git a/ui/ramView.ui b/ui/ramView.ui new file mode 100644 index 0000000..4a9f4d3 --- /dev/null +++ b/ui/ramView.ui @@ -0,0 +1,32 @@ + + + RamView + + + + 0 + 0 + 800 + 600 + + + + Memory Viewer + + + + :/resources/Logo.png:/resources/Logo.png + + + + + + + + + + + + + +