src/platform/qt/MemoryView.cpp (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7#include "MemoryView.h"
8
9#include "GameController.h"
10
11#include <mgba/core/core.h>
12#ifdef M_CORE_GBA
13#include <mgba/internal/gba/memory.h>
14#endif
15#ifdef M_CORE_GB
16#include <mgba/internal/gb/memory.h>
17#endif
18
19using namespace QGBA;
20
21struct IndexInfo {
22 const char* name;
23 const char* longName;
24 uint32_t base;
25 uint32_t size;
26 int maxSegment;
27};
28#ifdef M_CORE_GBA
29const static struct IndexInfo indexInfoGBA[] = {
30 { "All", "All", 0, 0x10000000 },
31 { "BIOS", "BIOS (16kiB)", BASE_BIOS, SIZE_BIOS },
32 { "EWRAM", "Working RAM (256kiB)", BASE_WORKING_RAM, SIZE_WORKING_RAM },
33 { "IWRAM", "Internal Working RAM (32kiB)", BASE_WORKING_IRAM, SIZE_WORKING_IRAM },
34 { "MMIO", "Memory-Mapped I/O", BASE_IO, SIZE_IO },
35 { "Palette", "Palette RAM (1kiB)", BASE_PALETTE_RAM, SIZE_PALETTE_RAM },
36 { "VRAM", "Video RAM (96kiB)", BASE_VRAM, SIZE_VRAM },
37 { "OAM", "OBJ Attribute Memory (1kiB)", BASE_OAM, SIZE_OAM },
38 { "ROM", "Game Pak (32MiB)", BASE_CART0, SIZE_CART0 },
39 { "ROM WS1", "Game Pak (Waitstate 1)", BASE_CART1, SIZE_CART1 },
40 { "ROM WS2", "Game Pak (Waitstate 2)", BASE_CART2, SIZE_CART2 },
41 { "SRAM", "Static RAM (64kiB)", BASE_CART_SRAM, SIZE_CART_SRAM },
42 { nullptr, nullptr, 0, 0, 0 }
43};
44#endif
45#ifdef M_CORE_GB
46const static struct IndexInfo indexInfoGB[] = {
47 { "All", "All", 0, 0x10000 },
48 { "ROM", "Game Pak (32kiB)", GB_BASE_CART_BANK0, GB_SIZE_CART_BANK0 * 2, 511 },
49 { "VRAM", "Video RAM (8kiB)", GB_BASE_VRAM, GB_SIZE_VRAM, 1 },
50 { "SRAM", "External RAM (8kiB)", GB_BASE_EXTERNAL_RAM, GB_SIZE_EXTERNAL_RAM, 3 },
51 { "WRAM", "Working RAM (8kiB)", GB_BASE_WORKING_RAM_BANK0, GB_SIZE_WORKING_RAM_BANK0 * 2, 7 },
52 { "OAM", "OBJ Attribute Memory", GB_BASE_OAM, GB_SIZE_OAM },
53 { "IO", "Memory-Mapped I/O", GB_BASE_IO, GB_SIZE_IO },
54 { "HRAM", "High RAM", GB_BASE_HRAM, GB_SIZE_HRAM },
55 { nullptr, nullptr, 0, 0, 0 }
56};
57#endif
58
59MemoryView::MemoryView(GameController* controller, QWidget* parent)
60 : QWidget(parent)
61 , m_controller(controller)
62{
63 m_ui.setupUi(this);
64
65 m_ui.hexfield->setController(controller);
66
67 mCore* core = m_controller->thread()->core;
68 const IndexInfo* info = nullptr;
69 switch (core->platform(core)) {
70#ifdef M_CORE_GBA
71 case PLATFORM_GBA:
72 info = indexInfoGBA;
73 break;
74#endif
75#ifdef M_CORE_GB
76 case PLATFORM_GB:
77 info = indexInfoGB;
78 break;
79#endif
80 default:
81 break;
82 }
83
84 connect(m_ui.regions, SIGNAL(currentIndexChanged(int)), this, SLOT(setIndex(int)));
85 connect(m_ui.segments, SIGNAL(valueChanged(int)), this, SLOT(setSegment(int)));
86
87 if (info) {
88 for (size_t i = 0; info[i].name; ++i) {
89 m_ui.regions->addItem(tr(info[i].longName));
90 }
91 }
92
93 connect(m_ui.width8, &QAbstractButton::clicked, [this]() { m_ui.hexfield->setAlignment(1); });
94 connect(m_ui.width16, &QAbstractButton::clicked, [this]() { m_ui.hexfield->setAlignment(2); });
95 connect(m_ui.width32, &QAbstractButton::clicked, [this]() { m_ui.hexfield->setAlignment(4); });
96 connect(m_ui.setAddress, SIGNAL(valueChanged(const QString&)), m_ui.hexfield, SLOT(jumpToAddress(const QString&)));
97 connect(m_ui.hexfield, SIGNAL(selectionChanged(uint32_t, uint32_t)), this, SLOT(updateSelection(uint32_t, uint32_t)));
98
99 connect(controller, SIGNAL(gameStopped(mCoreThread*)), this, SLOT(close()));
100
101 connect(controller, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(update()));
102 connect(controller, SIGNAL(gamePaused(mCoreThread*)), this, SLOT(update()));
103 connect(controller, SIGNAL(stateLoaded(mCoreThread*)), this, SLOT(update()));
104 connect(controller, SIGNAL(rewound(mCoreThread*)), this, SLOT(update()));
105
106 connect(m_ui.copy, SIGNAL(clicked()), m_ui.hexfield, SLOT(copy()));
107 connect(m_ui.save, SIGNAL(clicked()), m_ui.hexfield, SLOT(save()));
108 connect(m_ui.paste, SIGNAL(clicked()), m_ui.hexfield, SLOT(paste()));
109 connect(m_ui.load, SIGNAL(clicked()), m_ui.hexfield, SLOT(load()));
110
111 connect(m_ui.loadTBL, SIGNAL(clicked()), m_ui.hexfield, SLOT(loadTBL()));
112}
113
114void MemoryView::setIndex(int index) {
115 mCore* core = m_controller->thread()->core;
116 IndexInfo info;
117 switch (core->platform(core)) {
118#ifdef M_CORE_GBA
119 case PLATFORM_GBA:
120 info = indexInfoGBA[index];
121 break;
122#endif
123#ifdef M_CORE_GB
124 case PLATFORM_GB:
125 info = indexInfoGB[index];
126 break;
127#endif
128 default:
129 return;
130 }
131 m_ui.segments->setValue(-1);
132 m_ui.segments->setVisible(info.maxSegment > 0);
133 m_ui.segments->setMaximum(info.maxSegment);
134 m_ui.hexfield->setRegion(info.base, info.size, info.name);
135}
136
137void MemoryView::setSegment(int segment) {
138 mCore* core = m_controller->thread()->core;
139 IndexInfo info;
140 switch (core->platform(core)) {
141#ifdef M_CORE_GBA
142 case PLATFORM_GBA:
143 info = indexInfoGBA[m_ui.regions->currentIndex()];
144 break;
145#endif
146#ifdef M_CORE_GB
147 case PLATFORM_GB:
148 info = indexInfoGB[m_ui.regions->currentIndex()];
149 break;
150#endif
151 default:
152 return;
153 }
154 m_ui.hexfield->setSegment(info.maxSegment < segment ? info.maxSegment : segment);
155}
156
157void MemoryView::update() {
158 m_ui.hexfield->viewport()->update();
159 updateStatus();
160}
161
162void MemoryView::updateSelection(uint32_t start, uint32_t end) {
163 m_selection.first = start;
164 m_selection.second = end;
165 updateStatus();
166}
167
168void MemoryView::updateStatus() {
169 int align = m_ui.hexfield->alignment();
170 if (!m_controller->isLoaded()) {
171 return;
172 }
173 mCore* core = m_controller->thread()->core;
174 QByteArray selection(m_ui.hexfield->serialize());
175 QString text(m_ui.hexfield->decodeText(selection));
176 m_ui.stringVal->setText(text);
177
178 if (m_selection.first & (align - 1) || m_selection.second - m_selection.first != align) {
179 m_ui.sintVal->clear();
180 m_ui.uintVal->clear();
181 return;
182 }
183 union {
184 uint32_t u32;
185 int32_t i32;
186 uint16_t u16;
187 int16_t i16;
188 uint8_t u8;
189 int8_t i8;
190 } value;
191 switch (align) {
192 case 1:
193 value.u8 = core->rawRead8(core, m_selection.first, m_ui.segments->value());
194 m_ui.sintVal->setText(QString::number(value.i8));
195 m_ui.uintVal->setText(QString::number(value.u8));
196 break;
197 case 2:
198 value.u16 = core->rawRead16(core, m_selection.first, m_ui.segments->value());
199 m_ui.sintVal->setText(QString::number(value.i16));
200 m_ui.uintVal->setText(QString::number(value.u16));
201 break;
202 case 4:
203 value.u32 = core->rawRead32(core, m_selection.first, m_ui.segments->value());
204 m_ui.sintVal->setText(QString::number(value.i32));
205 m_ui.uintVal->setText(QString::number(value.u32));
206 break;
207 }
208}