diff --git a/sources/Cartridge/Cartridge.cpp b/sources/Cartridge/Cartridge.cpp index b442fd1..58e7348 100644 --- a/sources/Cartridge/Cartridge.cpp +++ b/sources/Cartridge/Cartridge.cpp @@ -13,6 +13,7 @@ namespace ComSquare::Cartridge { Cartridge::Cartridge(const std::string &romPath) + : Ram::Ram(0) { try { if (romPath.empty()) diff --git a/sources/Cartridge/Cartridge.hpp b/sources/Cartridge/Cartridge.hpp index 113619b..9612ea8 100644 --- a/sources/Cartridge/Cartridge.hpp +++ b/sources/Cartridge/Cartridge.hpp @@ -10,6 +10,7 @@ #include "../Models/Int24.hpp" #include "../Memory/IRectangleMemory.hpp" #include "InterruptVectors.hpp" +#include "../Ram/Ram.hpp" namespace ComSquare::Cartridge { @@ -63,12 +64,8 @@ namespace ComSquare::Cartridge }; //! @brief Contains the rom's memory/instructions. - class Cartridge : public Memory::IRectangleMemory { + class Cartridge : public Ram::Ram { private: - //! @brief The rom data (contains all the instructions). - uint8_t *_data; - //! @brief The size of the rom data. - size_t _size; //! @brief Sometime the rom's data has an offset for a SMC header. This value indicate the start of the real rom discarding this header. uint16_t _romStart = 0; diff --git a/sources/Debugger/MemoryViewer.cpp b/sources/Debugger/MemoryViewer.cpp index b84e53d..2280a55 100644 --- a/sources/Debugger/MemoryViewer.cpp +++ b/sources/Debugger/MemoryViewer.cpp @@ -26,26 +26,44 @@ int MemoryViewerModel::columnCount(const QModelIndex &parent) const QVariant MemoryViewerModel::data(const QModelIndex &index, int role) const { + if (role == Qt::TextAlignmentRole) + return Qt::AlignCenter; if (role != Qt::DisplayRole) return QVariant(); - return QString(ComSquare::Utility::to_hex(this->_memory->read_internal((index.row() << 4u) + index.column())).c_str()); + char buf[3]; + snprintf(buf, 3, "%02X", this->_memory->read_internal((index.row() << 4u) + index.column())); + return QString(buf); } 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()); + if (orientation == Qt::Horizontal) { + char buf[2]; + snprintf(buf, 2, "%1X", section); + return QString(buf); + } else { + char buf[5]; + snprintf(buf, 5, "%03Xx", section); + return QString(buf); + } +} + +void MemoryViewerModel::setMemory(std::shared_ptr memory) +{ + this->_memory = std::move(memory); + emit this->layoutChanged(); } namespace ComSquare::Debugger { MemoryViewer::MemoryViewer(ComSquare::SNES &snes) : - QMainWindow(), _snes(snes), _ui(), _model(snes.wram) + QMainWindow(), + _snes(snes), + _ui(), + _model(snes.wram) { this->setContextMenuPolicy(Qt::NoContextMenu); this->setAttribute(Qt::WA_QuitOnClose, false); @@ -53,6 +71,30 @@ namespace ComSquare::Debugger this->_ui.setupUi(this); this->_ui.tableView->setModel(&this->_model); this->_ui.tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); + this->_ui.tabs->addTab("&WRam"); + this->_ui.tabs->addTab("&SRam"); + this->_ui.tabs->addTab("&Rom"); +// this->_ui.tabs->addTab("&VRam"); + QObject::connect(this->_ui.tabs, &QTabBar::currentChanged, this, &MemoryViewer::changeRam); this->show(); } + + void MemoryViewer::changeRam(int id) + { + switch (id) { + default: + case 0: + this->_model.setMemory(this->_snes.wram); + break; + case 1: + this->_model.setMemory(this->_snes.sram); + break; + case 2: + this->_model.setMemory(this->_snes.cartridge); + break; +// case 3: +// this->_model.setMemory(this->_snes.vram); +// break; + } + } } \ No newline at end of file diff --git a/sources/Debugger/MemoryViewer.hpp b/sources/Debugger/MemoryViewer.hpp index 04f52ba..82eac79 100644 --- a/sources/Debugger/MemoryViewer.hpp +++ b/sources/Debugger/MemoryViewer.hpp @@ -16,15 +16,24 @@ class MemoryViewerModel : public QAbstractTableModel { Q_OBJECT private: + //! @brief The ram to watch. std::shared_ptr _memory; public: + //! @brief Change the ram currently watched. + void setMemory(std::shared_ptr memory); + explicit MemoryViewerModel(std::shared_ptr memory, QObject *parent = nullptr); MemoryViewerModel(const MemoryViewerModel &) = delete; const MemoryViewerModel &operator=(const MemoryViewerModel &) = delete; ~MemoryViewerModel() override = default; + + //! @brief The number of row the table has. int rowCount(const QModelIndex &parent) const override; + //! @brief The number of column the table has. int columnCount(const QModelIndex &parent) const override; + //! @brief Return a data represneting the table cell. QVariant data(const QModelIndex &index, int role) const override; + //! @brief Override the headers to use hex values. QVariant headerData(int section, Qt::Orientation orientation, int role) const override; }; @@ -44,6 +53,9 @@ namespace ComSquare //! @brief The Ram visualizer model for QT. MemoryViewerModel _model; public: + //! @brief Callback called when a memory tab is selected. + void changeRam(int id); + explicit MemoryViewer(SNES &snes); MemoryViewer(const MemoryViewer &) = delete; MemoryViewer &operator=(const MemoryViewer &) = delete; diff --git a/sources/Ram/Ram.cpp b/sources/Ram/Ram.cpp index 0f37fe4..4209e8d 100644 --- a/sources/Ram/Ram.cpp +++ b/sources/Ram/Ram.cpp @@ -11,8 +11,12 @@ 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)); + if (size == 0) + this->_data = nullptr; + else { + this->_data = new uint8_t[size]; + std::memset(this->_data, 0, size * sizeof(uint8_t)); + } } Ram::~Ram() diff --git a/sources/Ram/Ram.hpp b/sources/Ram/Ram.hpp index 5606e05..26a3871 100644 --- a/sources/Ram/Ram.hpp +++ b/sources/Ram/Ram.hpp @@ -10,7 +10,7 @@ namespace ComSquare::Ram { class Ram : public Memory::IRectangleMemory { - private: + protected: //! @brief The ram. (Can be used for WRam, SRam, VRam etc) uint8_t *_data; //! @brief The size of the ram (iny bytes). diff --git a/ui/ramView.ui b/ui/ramView.ui index 4a9f4d3..9e88f07 100644 --- a/ui/ramView.ui +++ b/ui/ramView.ui @@ -20,6 +20,16 @@ + + + + 0 + 35 + + + + +