all repos — mgba @ fc905657adeba406a2c280bbb94b09533a89e3f8

mGBA Game Boy Advance Emulator

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	if (!m_controller->isLoaded()) {
 84		return;
 85	}
 86	ARMCore* cpu = m_controller->thread()->cpu;
 87	union {
 88		uint32_t u32;
 89		int32_t i32;
 90		uint16_t u16;
 91		int16_t i16;
 92		uint8_t u8;
 93		int8_t i8;
 94	} value;
 95	switch (align) {
 96	case 1:
 97		value.u8 = GBAView8(cpu, m_selection.first);
 98		m_ui.sintVal->setText(QString::number(value.i8));
 99		m_ui.uintVal->setText(QString::number(value.u8));
100		break;
101	case 2:
102		value.u16 = GBAView16(cpu, m_selection.first);
103		m_ui.sintVal->setText(QString::number(value.i16));
104		m_ui.uintVal->setText(QString::number(value.u16));
105		break;
106	case 4:
107		value.u32 = GBAView32(cpu, m_selection.first);
108		m_ui.sintVal->setText(QString::number(value.i32));
109		m_ui.uintVal->setText(QString::number(value.u32));
110		break;
111	}
112}