|
| 1 | +/*************************************************************************** |
| 2 | + * Copyright (C) 2026 PCSX-Redux authors * |
| 3 | + * * |
| 4 | + * This program is free software; you can redistribute it and/or modify * |
| 5 | + * it under the terms of the GNU General Public License as published by * |
| 6 | + * the Free Software Foundation; either version 2 of the License, or * |
| 7 | + * (at your option) any later version. * |
| 8 | + * * |
| 9 | + * This program is distributed in the hope that it will be useful, * |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 12 | + * GNU General Public License for more details. * |
| 13 | + * * |
| 14 | + * You should have received a copy of the GNU General Public License * |
| 15 | + * along with this program; if not, write to the * |
| 16 | + * Free Software Foundation, Inc., * |
| 17 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * |
| 18 | + ***************************************************************************/ |
| 19 | + |
| 20 | +#include "core/ramlogger.h" |
| 21 | + |
| 22 | +#include "core/psxemulator.h" |
| 23 | +#include "core/psxmem.h" |
| 24 | + |
| 25 | +void PCSX::RAMLogger::recordAccess(uint32_t physAddr, unsigned width, AccessType type, uint32_t cycle) { |
| 26 | + if (!m_enabled) return; |
| 27 | + |
| 28 | + uint32_t* timestamps = (type == AccessType::Read) ? m_readTimestamps |
| 29 | + : (type == AccessType::Write) ? m_writeTimestamps |
| 30 | + : m_execTimestamps; |
| 31 | + |
| 32 | + for (unsigned i = 0; i < width; i++) { |
| 33 | + uint32_t addr = physAddr + i; |
| 34 | + if (addr < c_maxBytes) { |
| 35 | + timestamps[addr] = cycle; |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +void PCSX::RAMLogger::enable() { |
| 41 | + if (m_hasResources) { |
| 42 | + m_enabled = true; |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + auto setupTex = [](OpenGL::Texture& tex) { |
| 47 | + tex.bind(); |
| 48 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 49 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 50 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 51 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 52 | + }; |
| 53 | + |
| 54 | + // Create heatmap textures (GL_R32UI - unsigned integer) |
| 55 | + m_readHeatmapTex.create(c_width, c_maxHeight, GL_R32UI); |
| 56 | + m_writeHeatmapTex.create(c_width, c_maxHeight, GL_R32UI); |
| 57 | + m_execHeatmapTex.create(c_width, c_maxHeight, GL_R32UI); |
| 58 | + if (!m_readHeatmapTex.exists() || !m_writeHeatmapTex.exists() || !m_execHeatmapTex.exists()) return; |
| 59 | + setupTex(m_readHeatmapTex); |
| 60 | + setupTex(m_writeHeatmapTex); |
| 61 | + setupTex(m_execHeatmapTex); |
| 62 | + |
| 63 | + // Create RAM data texture (GL_R8) |
| 64 | + m_ramTexture.create(c_width, c_maxHeight, GL_R8); |
| 65 | + if (!m_ramTexture.exists()) return; |
| 66 | + setupTex(m_ramTexture); |
| 67 | + |
| 68 | + // Clear timestamp arrays |
| 69 | + memset(m_readTimestamps, 0, sizeof(m_readTimestamps)); |
| 70 | + memset(m_writeTimestamps, 0, sizeof(m_writeTimestamps)); |
| 71 | + memset(m_execTimestamps, 0, sizeof(m_execTimestamps)); |
| 72 | + |
| 73 | + m_hasResources = true; |
| 74 | + m_enabled = true; |
| 75 | +} |
| 76 | + |
| 77 | +void PCSX::RAMLogger::disable() { |
| 78 | + m_enabled = false; |
| 79 | +} |
| 80 | + |
| 81 | +void PCSX::RAMLogger::uploadRAM() { |
| 82 | + if (!m_hasResources) return; |
| 83 | + bool is8MB = g_emulator->settings.get<Emulator::Setting8MB>(); |
| 84 | + int height = is8MB ? c_maxHeight : (c_maxHeight / 4); |
| 85 | + |
| 86 | + m_ramTexture.bind(); |
| 87 | + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 88 | + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, c_width, height, GL_RED, GL_UNSIGNED_BYTE, g_emulator->m_mem->m_wram); |
| 89 | +} |
| 90 | + |
| 91 | +void PCSX::RAMLogger::uploadHeatmaps() { |
| 92 | + if (!m_hasResources) return; |
| 93 | + bool is8MB = g_emulator->settings.get<Emulator::Setting8MB>(); |
| 94 | + int height = is8MB ? c_maxHeight : (c_maxHeight / 4); |
| 95 | + |
| 96 | + glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
| 97 | + |
| 98 | + m_readHeatmapTex.bind(); |
| 99 | + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, c_width, height, GL_RED_INTEGER, GL_UNSIGNED_INT, m_readTimestamps); |
| 100 | + |
| 101 | + m_writeHeatmapTex.bind(); |
| 102 | + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, c_width, height, GL_RED_INTEGER, GL_UNSIGNED_INT, m_writeTimestamps); |
| 103 | + |
| 104 | + m_execHeatmapTex.bind(); |
| 105 | + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, c_width, height, GL_RED_INTEGER, GL_UNSIGNED_INT, m_execTimestamps); |
| 106 | +} |
0 commit comments