From 95b1fb3b43034cca36ccb1c822c420ccf3f8e2f0 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 27 Jun 2025 00:58:05 -0400 Subject: [PATCH] UI Para visor de memoria --- src/CMakeLists.txt | 2 + src/UI/MemoryViewer.cpp | 99 +++++++++++++++++++++++++++++++++++++++++ src/UI/MemoryViewer.h | 29 ++++++++++++ src/UI/RomInfo.cpp | 2 +- src/UI/UIManager.cpp | 5 ++- src/UI/UIManager.h | 2 + 6 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 src/UI/MemoryViewer.cpp create mode 100644 src/UI/MemoryViewer.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bbaec45..bee3a69 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -22,6 +22,8 @@ target_sources( UI/ControlPanel.h UI/Display.cpp UI/Display.h + UI/MemoryViewer.cpp + UI/MemoryViewer.h UI/RomInfo.cpp UI/RomInfo.h UI/UIManager.cpp diff --git a/src/UI/MemoryViewer.cpp b/src/UI/MemoryViewer.cpp new file mode 100644 index 0000000..e4653a3 --- /dev/null +++ b/src/UI/MemoryViewer.cpp @@ -0,0 +1,99 @@ +#include "MemoryViewer.h" + +#include +#include +#include + +#include "imgui.h" + +MemoryViewer::MemoryViewer(std::shared_ptr graphics, + std::shared_ptr machine_state, + std::shared_ptr callback_manager +) : graphics{std::move(graphics)}, + machine_state{std::move(machine_state)}, + callback_manager{std::move(callback_manager)}, + width{16} +{ +} + +void MemoryViewer::render() +{ + if (ImGui::Begin("CHIP-8 - CPU Memory")) + { + ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x); + ImGui::SliderInt("##memory_view_width", &width, 1, 32, "%d", ImGuiSliderFlags_AlwaysClamp); + ImGui::PopItemWidth(); + + + auto dump_lines = this->get_memory_dump(); + for (auto dump_line : dump_lines) + { + ImGui::TextUnformatted(dump_line.c_str()); + } + } + ImGui::End(); +} + +std::vector MemoryViewer::get_memory_dump() const +{ + auto& memory = this->machine_state->memory; + const auto size = std::size(memory); + + std::vector dump_lines; + + for (auto i = 0; i < size; i += this->width) + { + std::stringstream text_builder; + + text_builder << "0x"; + text_builder << std::hex + << std::setw(4) + << std::setfill('0') + << std::uppercase + << static_cast(i) + << " | "; + + for (auto j = 0; j < this->width; j++) + { + if (i + j >= size) + { + text_builder << " "; + } + else + { + text_builder << std::hex + << std::setw(2) + << std::setfill('0') + << std::uppercase + << static_cast(memory[i + j]) + << " "; + } + } + + text_builder << "| "; + + for (auto j = 0; j < this->width; j++) + { + if (i + j >= size) + { + text_builder << "."; + } + else + { + auto byte = memory[i + j]; + if (byte >= 32 && byte <= 126) + { + text_builder << static_cast(memory[i + j]); + } + else + { + text_builder << "."; + } + } + } + + dump_lines.push_back(text_builder.str()); + } + + return dump_lines; +} diff --git a/src/UI/MemoryViewer.h b/src/UI/MemoryViewer.h new file mode 100644 index 0000000..951ad61 --- /dev/null +++ b/src/UI/MemoryViewer.h @@ -0,0 +1,29 @@ +#ifndef MEMORYVIEWER_H +#define MEMORYVIEWER_H +#include + +#include "CallbackManager.h" +#include "../Graphics/Graphics.h" +#include "../Interpreter/MachineState.h" + +class MemoryViewer +{ + std::shared_ptr graphics; + std::shared_ptr machine_state; + std::shared_ptr callback_manager; + + int width; + +public: + MemoryViewer( + std::shared_ptr graphics, + std::shared_ptr machine_state, + std::shared_ptr callback_manager + ); + + void render(); + std::vector get_memory_dump() const; +}; + + +#endif //MEMORYVIEWER_H diff --git a/src/UI/RomInfo.cpp b/src/UI/RomInfo.cpp index d2abf4c..6441404 100644 --- a/src/UI/RomInfo.cpp +++ b/src/UI/RomInfo.cpp @@ -40,5 +40,5 @@ void RomInfo::on_rom_load(const std::string& rom_path) std::filesystem::path home = SDL_GetUserFolder(SDL_FOLDER_HOME); std::filesystem::path path = rom_path; - this->rom_path = std::filesystem::relative(path, home).string(); + this->rom_path = "~/" + std::filesystem::relative(path, home).string(); } diff --git a/src/UI/UIManager.cpp b/src/UI/UIManager.cpp index f443f5c..90ecf1f 100644 --- a/src/UI/UIManager.cpp +++ b/src/UI/UIManager.cpp @@ -1,5 +1,6 @@ #include "UIManager.h" +#include "MemoryViewer.h" #include "RomInfo.h" UIManager::UIManager( @@ -13,7 +14,8 @@ UIManager::UIManager( display{std::make_unique(this->graphics, this->machine_state)}, control_panel{std::make_unique(this->graphics, this->machine_state, this->callback_manager)}, - rom_info{std::make_unique(this->graphics, this->machine_state, this->callback_manager)} + rom_info{std::make_unique(this->graphics, this->machine_state, this->callback_manager)}, + memory_viewer{std::make_unique(this->graphics, this->machine_state, this->callback_manager)} { } @@ -22,4 +24,5 @@ void UIManager::render() this->display->render(); this->control_panel->render(); this->rom_info->render(); + this->memory_viewer->render(); } diff --git a/src/UI/UIManager.h b/src/UI/UIManager.h index 092acd6..5c1582d 100644 --- a/src/UI/UIManager.h +++ b/src/UI/UIManager.h @@ -5,6 +5,7 @@ #include "CallbackManager.h" #include "ControlPanel.h" #include "Display.h" +#include "MemoryViewer.h" #include "RomInfo.h" #include "../Graphics/Graphics.h" @@ -16,6 +17,7 @@ class UIManager std::unique_ptr display; std::unique_ptr control_panel; std::unique_ptr rom_info; + std::unique_ptr memory_viewer; public: UIManager(