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
11extern "C" {
12#include "core/core.h"
13#ifdef M_CORE_GBA
14#include "gba/memory.h"
15#endif
16#ifdef M_CORE_GB
17#include "gb/memory.h"
18#endif
19}
20
21using namespace QGBA;
22
23struct IndexInfo {
24 const char* name;
25 const char* longName;
26 uint32_t base;
27 uint32_t size;
28 int maxSegment;
29};
30#ifdef M_CORE_GBA
31const static struct IndexInfo indexInfoGBA[] = {
32 { "All", "All", 0, 0x10000000 },
33 { "BIOS", "BIOS (16kiB)", BASE_BIOS, SIZE_BIOS },
34 { "EWRAM", "Working RAM (256kiB)", BASE_WORKING_RAM, SIZE_WORKING_RAM },
35 { "IWRAM", "Internal Working RAM (32kiB)", BASE_WORKING_IRAM, SIZE_WORKING_IRAM },
36 { "MMIO", "Memory-Mapped I/O", BASE_IO, SIZE_IO },
37 { "Palette", "Palette RAM (1kiB)", BASE_PALETTE_RAM, SIZE_PALETTE_RAM },
38 { "VRAM", "Video RAM (96kiB)", BASE_VRAM, SIZE_VRAM },
39 { "OAM", "OBJ Attribute Memory (1kiB)", BASE_OAM, SIZE_OAM },
40 { "ROM", "Game Pak (32MiB)", BASE_CART0, SIZE_CART0 },
41 { "ROM WS1", "Game Pak (Waitstate 1)", BASE_CART1, SIZE_CART1 },
42 { "ROM WS2", "Game Pak (Waitstate 2)", BASE_CART2, SIZE_CART2 },
43 { "SRAM", "Static RAM (64kiB)", BASE_CART_SRAM, SIZE_CART_SRAM },
44 { nullptr, nullptr, 0, 0, 0 }
45};
46#endif
47#ifdef M_CORE_GB
48const static struct IndexInfo indexInfoGB[] = {
49 { "All", "All", 0, 0x10000 },
50 { "ROM", "Game Pak (32kiB)", GB_BASE_CART_BANK0, GB_SIZE_CART_BANK0 * 2, 511 },
51 { "VRAM", "Video RAM (8kiB)", GB_BASE_VRAM, GB_SIZE_VRAM, 1 },
52 { "SRAM", "External RAM (8kiB)", GB_BASE_EXTERNAL_RAM, GB_SIZE_EXTERNAL_RAM, 3 },
53 { "WRAM", "Working RAM (8kiB)", GB_BASE_WORKING_RAM_BANK0, GB_SIZE_WORKING_RAM_BANK0 * 2, 7 },
54 { "OAM", "OBJ Attribute Memory", GB_BASE_OAM, GB_SIZE_OAM },
55 { "IO", "Memory-Mapped I/O", GB_BASE_IO, GB_SIZE_IO },
56 { "HRAM", "High RAM", GB_BASE_HRAM, GB_SIZE_HRAM },
57 { nullptr, nullptr, 0, 0, 0 }
58};
59#endif
60
61MemoryView::MemoryView(GameController* controller, QWidget* parent)
62 : QWidget(parent)
63 , m_controller(controller)
64{
65 m_ui.setupUi(this);
66
67 m_ui.hexfield->setController(controller);
68
69 mCore* core = m_controller->thread()->core;
70 const IndexInfo* info = nullptr;
71 switch (core->platform(core)) {
72#ifdef M_CORE_GBA
73 case PLATFORM_GBA:
74 info = indexInfoGBA;
75 break;
76#endif
77#ifdef M_CORE_GB
78 case PLATFORM_GB:
79 info = indexInfoGB;
80 break;
81#endif
82 default:
83 break;
84 }
85
86 connect(m_ui.regions, SIGNAL(currentIndexChanged(int)), this, SLOT(setIndex(int)));
87 connect(m_ui.segments, SIGNAL(valueChanged(int)), this, SLOT(setSegment(int)));
88
89 if (info) {
90 for (size_t i = 0; info[i].name; ++i) {
91 m_ui.regions->addItem(tr(info[i].longName));
92 }
93 }
94
95 connect(m_ui.width8, &QAbstractButton::clicked, [this]() { m_ui.hexfield->setAlignment(1); });
96 connect(m_ui.width16, &QAbstractButton::clicked, [this]() { m_ui.hexfield->setAlignment(2); });
97 connect(m_ui.width32, &QAbstractButton::clicked, [this]() { m_ui.hexfield->setAlignment(4); });
98 connect(m_ui.setAddress, SIGNAL(valueChanged(const QString&)), m_ui.hexfield, SLOT(jumpToAddress(const QString&)));
99 connect(m_ui.hexfield, SIGNAL(selectionChanged(uint32_t, uint32_t)), this, SLOT(updateSelection(uint32_t, uint32_t)));
100
101 connect(controller, SIGNAL(gameStopped(mCoreThread*)), this, SLOT(close()));
102
103 connect(controller, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(update()));
104 connect(controller, SIGNAL(gamePaused(mCoreThread*)), this, SLOT(update()));
105 connect(controller, SIGNAL(stateLoaded(mCoreThread*)), this, SLOT(update()));
106 connect(controller, SIGNAL(rewound(mCoreThread*)), this, SLOT(update()));
107
108 connect(m_ui.copy, SIGNAL(clicked()), m_ui.hexfield, SLOT(copy()));
109 connect(m_ui.save, SIGNAL(clicked()), m_ui.hexfield, SLOT(save()));
110 connect(m_ui.paste, SIGNAL(clicked()), m_ui.hexfield, SLOT(paste()));
111 connect(m_ui.load, SIGNAL(clicked()), m_ui.hexfield, SLOT(load()));
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_selection.first & (align - 1) || m_selection.second - m_selection.first != align) {
171 m_ui.sintVal->clear();
172 m_ui.uintVal->clear();
173 return;
174 }
175 if (!m_controller->isLoaded()) {
176 return;
177 }
178 mCore* core = m_controller->thread()->core;
179 union {
180 uint32_t u32;
181 int32_t i32;
182 uint16_t u16;
183 int16_t i16;
184 uint8_t u8;
185 int8_t i8;
186 } value;
187 switch (align) {
188 case 1:
189 value.u8 = core->rawRead8(core, m_selection.first, m_ui.segments->value());
190 m_ui.sintVal->setText(QString::number(value.i8));
191 m_ui.uintVal->setText(QString::number(value.u8));
192 break;
193 case 2:
194 value.u16 = core->rawRead16(core, m_selection.first, m_ui.segments->value());
195 m_ui.sintVal->setText(QString::number(value.i16));
196 m_ui.uintVal->setText(QString::number(value.u16));
197 break;
198 case 4:
199 value.u32 = core->rawRead32(core, m_selection.first, m_ui.segments->value());
200 m_ui.sintVal->setText(QString::number(value.i32));
201 m_ui.uintVal->setText(QString::number(value.u32));
202 break;
203 }
204}