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 "gba/memory.h"
13}
14
15using namespace QGBA;
16
17MemoryView::MemoryView(GameController* controller, QWidget* parent)
18 : QWidget(parent)
19 , m_controller(controller)
20{
21 m_ui.setupUi(this);
22
23 m_ui.hexfield->setController(controller);
24
25 connect(m_ui.regions, SIGNAL(currentIndexChanged(int)), this, SLOT(setIndex(int)));
26
27 connect(m_ui.width8, &QAbstractButton::clicked, [this]() { m_ui.hexfield->setAlignment(1); });
28 connect(m_ui.width16, &QAbstractButton::clicked, [this]() { m_ui.hexfield->setAlignment(2); });
29 connect(m_ui.width32, &QAbstractButton::clicked, [this]() { m_ui.hexfield->setAlignment(4); });
30 connect(m_ui.setAddress, SIGNAL(valueChanged(const QString&)), m_ui.hexfield, SLOT(jumpToAddress(const QString&)));
31
32 connect(m_ui.hexfield, SIGNAL(selectionChanged(uint32_t, uint32_t)), this, SLOT(updateSelection(uint32_t, uint32_t)));
33
34 connect(controller, SIGNAL(gameStopped(GBAThread*)), this, SLOT(close()));
35
36 connect(controller, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(update()));
37 connect(controller, SIGNAL(gamePaused(GBAThread*)), this, SLOT(update()));
38 connect(controller, SIGNAL(stateLoaded(GBAThread*)), this, SLOT(update()));
39 connect(controller, SIGNAL(rewound(GBAThread*)), this, SLOT(update()));
40}
41
42void MemoryView::setIndex(int index) {
43 static struct {
44 const char* name;
45 uint32_t base;
46 uint32_t size;
47 } indexInfo[] = {
48 { "All", 0, 0x10000000 },
49 { "BIOS", BASE_BIOS, SIZE_BIOS },
50 { "EWRAM", BASE_WORKING_RAM, SIZE_WORKING_RAM },
51 { "IWRAM", BASE_WORKING_IRAM, SIZE_WORKING_IRAM },
52 { "MMIO", BASE_IO, SIZE_IO },
53 { "Palette", BASE_PALETTE_RAM, SIZE_PALETTE_RAM },
54 { "VRAM", BASE_VRAM, SIZE_VRAM },
55 { "OAM", BASE_OAM, SIZE_OAM },
56 { "ROM", BASE_CART0, SIZE_CART0 },
57 { "ROM WS1", BASE_CART1, SIZE_CART1 },
58 { "ROM WS2", BASE_CART2, SIZE_CART2 },
59 { "SRAM", BASE_CART_SRAM, SIZE_CART_SRAM },
60 };
61 const auto& info = indexInfo[index];
62 m_ui.hexfield->setRegion(info.base, info.size, info.name);
63}
64
65void MemoryView::update() {
66 m_ui.hexfield->viewport()->update();
67 updateStatus();
68}
69
70void MemoryView::updateSelection(uint32_t start, uint32_t end) {
71 m_selection.first = start;
72 m_selection.second = end;
73 updateStatus();
74}
75
76void MemoryView::updateStatus() {
77 int align = m_ui.hexfield->alignment();
78 if (m_selection.first & (align - 1) || m_selection.second - m_selection.first != align) {
79 m_ui.sintVal->clear();
80 m_ui.uintVal->clear();
81 return;
82 }
83 ARMCore* cpu = m_controller->thread()->cpu;
84 union {
85 uint32_t u32;
86 int32_t i32;
87 uint16_t u16;
88 int16_t i16;
89 uint8_t u8;
90 int8_t i8;
91 } value;
92 switch (align) {
93 case 1:
94 value.u8 = cpu->memory.load8(cpu, m_selection.first, nullptr);
95 m_ui.sintVal->setText(QString::number(value.i8));
96 m_ui.uintVal->setText(QString::number(value.u8));
97 break;
98 case 2:
99 value.u16 = cpu->memory.load16(cpu, m_selection.first, nullptr);
100 m_ui.sintVal->setText(QString::number(value.i16));
101 m_ui.uintVal->setText(QString::number(value.u16));
102 break;
103 case 4:
104 value.u32 = cpu->memory.load32(cpu, m_selection.first, nullptr);
105 m_ui.sintVal->setText(QString::number(value.i32));
106 m_ui.uintVal->setText(QString::number(value.u32));
107 break;
108 }
109}