Skip to content

Commit 9a0ff92

Browse files
committed
Debugger: Merge redundant memory interfaces
1 parent 2c608b4 commit 9a0ff92

File tree

14 files changed

+350
-372
lines changed

14 files changed

+350
-372
lines changed

common/MemoryInterface.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include "MemoryInterface.h"
55

6+
#include <bit>
7+
68
template <MemoryAccessType Value>
79
Value MemoryInterface::Read(u32 address, bool* valid)
810
{
@@ -24,6 +26,10 @@ Value MemoryInterface::Read(u32 address, bool* valid)
2426
.hi = static_cast<s64>(value.hi),
2527
};
2628
}
29+
else if constexpr (std::is_same_v<Value, float>)
30+
return std::bit_cast<float>(Read32(address, valid));
31+
else if constexpr (std::is_same_v<Value, double>)
32+
return std::bit_cast<double>(Read64(address, valid));
2733
else
2834
return Value(0);
2935
}
@@ -48,6 +54,10 @@ bool MemoryInterface::Write(u32 address, Value value)
4854
unsigned_value.hi = static_cast<u64>(value.hi);
4955
return Write128(address, unsigned_value);
5056
}
57+
else if constexpr (std::is_same_v<Value, float>)
58+
return Write32(address, std::bit_cast<u32>(value));
59+
else if constexpr (std::is_same_v<Value, double>)
60+
return Write64(address, std::bit_cast<u64>(value));
5161
else
5262
return false;
5363
}
@@ -135,4 +145,6 @@ EXPLICITLY_INSTANTIATE_TEMPLATES(u64)
135145
EXPLICITLY_INSTANTIATE_TEMPLATES(s64)
136146
EXPLICITLY_INSTANTIATE_TEMPLATES(u128)
137147
EXPLICITLY_INSTANTIATE_TEMPLATES(s128)
148+
EXPLICITLY_INSTANTIATE_TEMPLATES(float)
149+
EXPLICITLY_INSTANTIATE_TEMPLATES(double)
138150
#undef EXPLICITLY_INSTANTIATE_TEMPLATES

common/MemoryInterface.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ concept MemoryAccessType = std::is_same_v<Value, u8> || std::is_same_v<Value, s8
1212
std::is_same_v<Value, u16> || std::is_same_v<Value, s16> ||
1313
std::is_same_v<Value, u32> || std::is_same_v<Value, s32> ||
1414
std::is_same_v<Value, u64> || std::is_same_v<Value, s64> ||
15-
std::is_same_v<Value, u128> || std::is_same_v<Value, s128>;
15+
std::is_same_v<Value, u128> || std::is_same_v<Value, s128> ||
16+
std::is_same_v<Value, float> || std::is_same_v<Value, double>;
1617

1718
/// Interface for reading/writing guest memory.
1819
class MemoryInterface
1920
{
2021
public:
22+
virtual ~MemoryInterface() {}
23+
2124
virtual u8 Read8(u32 address, bool* valid = nullptr) = 0;
2225
virtual u16 Read16(u32 address, bool* valid = nullptr) = 0;
2326
virtual u32 Read32(u32 address, bool* valid = nullptr) = 0;

pcsx2-qt/Debugger/DisassemblyView.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ void DisassemblyView::contextRestoreInstruction()
204204
{
205205
u32 address = start + i * 4;
206206
if (original_instructions[i].has_value())
207-
cpu->write32(address, *original_instructions[i]);
207+
cpu->Write32(address, *original_instructions[i]);
208208
}
209209
DebuggerView::broadcastEvent(DebuggerEvents::VMUpdate());
210210
});
@@ -332,11 +332,11 @@ void DisassemblyView::contextStubFunction()
332332

333333
const QPointer<DisassemblyView> view(this);
334334
Host::RunOnCPUThread([view, address, cpu = &cpu()] {
335-
const u32 first_instruction = cpu->read32(address);
336-
const u32 second_instruction = cpu->read32(address + 4);
335+
const u32 first_instruction = cpu->Read32(address);
336+
const u32 second_instruction = cpu->Read32(address + 4);
337337

338-
cpu->write32(address, 0x03E00008); // jr ra
339-
cpu->write32(address + 4, 0x00000000); // nop
338+
cpu->Write32(address, 0x03E00008); // jr ra
339+
cpu->Write32(address + 4, 0x00000000); // nop
340340

341341
QtHost::RunOnUIThread([view, address, first_instruction, second_instruction]() {
342342
if (!view)
@@ -364,8 +364,8 @@ void DisassemblyView::contextRestoreFunction()
364364
m_stubbedFunctions.erase(stub);
365365

366366
Host::RunOnCPUThread([address, cpu = &cpu(), first_instruction, second_instruction] {
367-
cpu->write32(address, first_instruction);
368-
cpu->write32(address + 4, second_instruction);
367+
cpu->Write32(address, first_instruction);
368+
cpu->Write32(address + 4, second_instruction);
369369
DebuggerView::broadcastEvent(DebuggerEvents::VMUpdate());
370370
});
371371
}
@@ -919,7 +919,7 @@ inline QString DisassemblyView::DisassemblyStringFromAddress(u32 address, QFont
919919

920920
if (showOpcode)
921921
{
922-
const u32 opcode = cpu().read32(address);
922+
const u32 opcode = cpu().Read32(address);
923923
lineString = lineString.arg(QtUtils::FilledQStringFromValue(opcode, 16));
924924
}
925925

@@ -989,7 +989,7 @@ QString DisassemblyView::FetchSelectionInfo(SelectionInfo selInfo)
989989
}
990990
else // INSTRUCTIONHEX
991991
{
992-
infoBlock += FilledQStringFromValue(cpu().read32(i), 16);
992+
infoBlock += FilledQStringFromValue(cpu().Read32(i), 16);
993993
}
994994
}
995995
return infoBlock;
@@ -1050,8 +1050,8 @@ void DisassemblyView::setInstructions(u32 start, u32 end, u32 value)
10501050
for (u32 i = 0; i < count; i++)
10511051
{
10521052
const u32 address = start + i * 4;
1053-
original_instructions.emplace_back(cpu->read32(address));
1054-
cpu->write32(address, value);
1053+
original_instructions.emplace_back(cpu->Read32(address));
1054+
cpu->Write32(address, value);
10551055
}
10561056

10571057
QtHost::RunOnUIThread([view, start, count, original_instructions = std::move(original_instructions)]() {

pcsx2-qt/Debugger/Memory/MemorySearchView.cpp

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -112,46 +112,6 @@ void MemorySearchView::onListSearchResultsContextMenu(QPoint pos)
112112
menu->popup(m_ui.listSearchResults->viewport()->mapToGlobal(pos));
113113
}
114114

115-
template <typename T>
116-
T readValueAtAddress(DebugInterface* cpu, u32 addr);
117-
template <>
118-
float readValueAtAddress<float>(DebugInterface* cpu, u32 addr)
119-
{
120-
return std::bit_cast<float>(cpu->read32(addr));
121-
}
122-
123-
template <>
124-
double readValueAtAddress<double>(DebugInterface* cpu, u32 addr)
125-
{
126-
return std::bit_cast<double>(cpu->read64(addr));
127-
}
128-
129-
template <typename T>
130-
T readValueAtAddress(DebugInterface* cpu, u32 addr)
131-
{
132-
T val = 0;
133-
switch (sizeof(T))
134-
{
135-
case sizeof(u8):
136-
val = cpu->read8(addr);
137-
break;
138-
case sizeof(u16):
139-
val = cpu->read16(addr);
140-
break;
141-
case sizeof(u32):
142-
{
143-
val = cpu->read32(addr);
144-
break;
145-
}
146-
case sizeof(u64):
147-
{
148-
val = cpu->read64(addr);
149-
break;
150-
}
151-
}
152-
return val;
153-
}
154-
155115
template <typename T>
156116
static bool memoryValueComparator(SearchComparison searchComparison, T searchValue, T readValue)
157117
{
@@ -294,7 +254,7 @@ void searchWorker(DebugInterface* cpu, std::vector<SearchResult>& searchResults,
294254
if (!cpu->isValidAddress(addr))
295255
continue;
296256

297-
T readValue = readValueAtAddress<T>(cpu, addr);
257+
T readValue = cpu->Read<T>(addr);
298258
if (handleSearchComparison(searchComparison, addr, nullptr, searchValue, readValue))
299259
{
300260
searchResults.push_back(MemorySearchView::SearchResult(addr, QVariant::fromValue(readValue), searchType));
@@ -308,7 +268,7 @@ void searchWorker(DebugInterface* cpu, std::vector<SearchResult>& searchResults,
308268
if (!cpu->isValidAddress(addr))
309269
return true;
310270

311-
const auto readValue = readValueAtAddress<T>(cpu, addr);
271+
const auto readValue = cpu->Read<T>(addr);
312272

313273
const bool doesMatch = handleSearchComparison(searchComparison, addr, &searchResult, searchValue, readValue);
314274
if (doesMatch)
@@ -325,7 +285,7 @@ static bool compareByteArrayAtAddress(DebugInterface* cpu, SearchComparison sear
325285
const bool isNotOperator = searchComparison == SearchComparison::NotEquals;
326286
for (qsizetype i = 0; i < value.length(); i++)
327287
{
328-
const char nextByte = cpu->read8(addr + i);
288+
const char nextByte = cpu->Read8(addr + i);
329289
switch (searchComparison)
330290
{
331291
case SearchComparison::Equals:
@@ -383,7 +343,7 @@ static QByteArray readArrayAtAddress(DebugInterface* cpu, u32 address, u32 lengt
383343
QByteArray readArray;
384344
for (u32 i = address; i < address + length; i++)
385345
{
386-
readArray.append(cpu->read8(i));
346+
readArray.append(cpu->Read8(i));
387347
}
388348
return readArray;
389349
}

pcsx2-qt/Debugger/Memory/MemoryView.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -119,39 +119,39 @@ void MemoryViewTable::DrawTable(QPainter& painter, const QPalette& palette, s32
119119
{
120120
case MemoryViewType::BYTE:
121121
{
122-
const u8 val = static_cast<u8>(cpu.read8(thisSegmentsStart, valid));
122+
const u8 val = cpu.Read8(thisSegmentsStart, &valid);
123123
if (penDefault && val == 0)
124124
painter.setPen(QColor::fromRgb(145, 145, 155)); // ZERO BYTE COLOUR
125125
painter.drawText(valX, y + (rowHeight * i), valid ? FilledQStringFromValue(val, 16) : "??");
126126
break;
127127
}
128128
case MemoryViewType::BYTEHW:
129129
{
130-
const u16 val = convertEndian<u16>(static_cast<u16>(cpu.read16(thisSegmentsStart, valid)));
130+
const u16 val = convertEndian<u16>(cpu.Read16(thisSegmentsStart, &valid));
131131
if (penDefault && val == 0)
132132
painter.setPen(QColor::fromRgb(145, 145, 155)); // ZERO BYTE COLOUR
133133
painter.drawText(valX, y + (rowHeight * i), valid ? FilledQStringFromValue(val, 16) : "????");
134134
break;
135135
}
136136
case MemoryViewType::WORD:
137137
{
138-
const u32 val = convertEndian<u32>(cpu.read32(thisSegmentsStart, valid));
138+
const u32 val = convertEndian<u32>(cpu.Read32(thisSegmentsStart, &valid));
139139
if (penDefault && val == 0)
140140
painter.setPen(QColor::fromRgb(145, 145, 155)); // ZERO BYTE COLOUR
141141
painter.drawText(valX, y + (rowHeight * i), valid ? FilledQStringFromValue(val, 16) : "????????");
142142
break;
143143
}
144144
case MemoryViewType::DWORD:
145145
{
146-
const u64 val = convertEndian<u64>(cpu.read64(thisSegmentsStart, valid));
146+
const u64 val = convertEndian<u64>(cpu.Read64(thisSegmentsStart, &valid));
147147
if (penDefault && val == 0)
148148
painter.setPen(QColor::fromRgb(145, 145, 155)); // ZERO BYTE COLOUR
149149
painter.drawText(valX, y + (rowHeight * i), valid ? FilledQStringFromValue(val, 16) : "????????????????");
150150
break;
151151
}
152152
case MemoryViewType::FLOAT:
153153
{
154-
const u32 intVal = convertEndian<u32>(cpu.read32(thisSegmentsStart, valid));
154+
const u32 intVal = convertEndian<u32>(cpu.Read32(thisSegmentsStart, &valid));
155155
float val = 0.0;
156156
std::memcpy(&val, &intVal, sizeof(val));
157157
if (penDefault && val == 0.0)
@@ -177,7 +177,7 @@ void MemoryViewTable::DrawTable(QPainter& painter, const QPalette& palette, s32
177177
painter.setPen(palette.text().color());
178178

179179
bool valid;
180-
const u8 value = cpu.read8(currentRowAddress + j, valid);
180+
const u8 value = cpu.Read8(currentRowAddress + j, &valid);
181181
if (valid)
182182
{
183183
QChar curChar = QChar::fromLatin1(value);
@@ -258,19 +258,19 @@ u128 MemoryViewTable::GetSelectedSegment(DebugInterface& cpu)
258258
switch (displayType)
259259
{
260260
case MemoryViewType::BYTE:
261-
val.lo = cpu.read8(selectedAddress);
261+
val.lo = cpu.Read8(selectedAddress);
262262
break;
263263
case MemoryViewType::BYTEHW:
264-
val.lo = convertEndian(static_cast<u16>(cpu.read16(selectedAddress & ~1)));
264+
val.lo = convertEndian(static_cast<u16>(cpu.Read16(selectedAddress & ~1)));
265265
break;
266266
case MemoryViewType::WORD:
267-
val.lo = convertEndian(cpu.read32(selectedAddress & ~3));
267+
val.lo = convertEndian(cpu.Read32(selectedAddress & ~3));
268268
break;
269269
case MemoryViewType::DWORD:
270-
val._u64[0] = convertEndian(cpu.read64(selectedAddress & ~7));
270+
val._u64[0] = convertEndian(cpu.Read64(selectedAddress & ~7));
271271
break;
272272
case MemoryViewType::FLOAT:
273-
val.lo = convertEndian(cpu.read32(selectedAddress & ~3));
273+
val.lo = convertEndian(cpu.Read32(selectedAddress & ~3));
274274
break;
275275
}
276276
return val;
@@ -279,13 +279,13 @@ u128 MemoryViewTable::GetSelectedSegment(DebugInterface& cpu)
279279
void MemoryViewTable::InsertIntoSelectedHexView(u8 value, DebugInterface& cpu)
280280
{
281281
const u8 mask = selectedNibbleHI ? 0x0f : 0xf0;
282-
u8 curVal = cpu.read8(selectedAddress) & mask;
282+
u8 curVal = cpu.Read8(selectedAddress) & mask;
283283
u8 newVal = value << (selectedNibbleHI ? 4 : 0);
284284
curVal |= newVal;
285285

286286
const QPointer<MemoryViewTable> table(this);
287287
Host::RunOnCPUThread([table, address = selectedAddress, &cpu, val = curVal] {
288-
cpu.write8(address, val);
288+
cpu.Write8(address, val);
289289

290290
QtHost::RunOnUIThread([table] {
291291
if (!table)
@@ -323,7 +323,7 @@ bool MemoryViewTable::InsertFloatIntoSelectedHexView(DebugInterface& cpu)
323323

324324
const QPointer<MemoryViewTable> table(this);
325325
Host::RunOnCPUThread([table, address = selectedAddress, &cpu, val = newIntVal] {
326-
cpu.write32(address, val);
326+
cpu.Write32(address, val);
327327

328328
QtHost::RunOnUIThread([table] {
329329
if (!table)
@@ -360,7 +360,7 @@ void MemoryViewTable::InsertAtCurrentSelection(const QString& text, DebugInterfa
360360

361361
const QPointer<MemoryViewTable> table(this);
362362
Host::RunOnCPUThread([table, address = selectedAddress, &cpu, val = newIntVal] {
363-
cpu.write32(address, val);
363+
cpu.Write32(address, val);
364364

365365
QtHost::RunOnUIThread([table] {
366366
if (!table)
@@ -384,7 +384,7 @@ void MemoryViewTable::InsertAtCurrentSelection(const QString& text, DebugInterfa
384384
u32 currAddr = address;
385385
for (int i = 0; i < input.size(); i++)
386386
{
387-
cpu.write8(currAddr, input[i]);
387+
cpu.Write8(currAddr, input[i]);
388388
currAddr = nextAddress(currAddr, address, display_type, little_endian);
389389
}
390390

@@ -552,7 +552,7 @@ bool MemoryViewTable::KeyPress(int key, QChar keychar, DebugInterface& cpu)
552552
{
553553
const QPointer<MemoryViewTable> table(this);
554554
Host::RunOnCPUThread([table, address = selectedAddress, &cpu, val = keychar.toLatin1()] {
555-
cpu.write8(address, val);
555+
cpu.Write8(address, val);
556556

557557
QtHost::RunOnUIThread([table, address]() {
558558
if (!table)
@@ -572,7 +572,7 @@ bool MemoryViewTable::KeyPress(int key, QChar keychar, DebugInterface& cpu)
572572
{
573573
const QPointer<MemoryViewTable> table(this);
574574
Host::RunOnCPUThread([table, address = selectedAddress, &cpu] {
575-
cpu.write8(address, 0);
575+
cpu.Write8(address, 0);
576576

577577
QtHost::RunOnUIThread([table] {
578578
if (!table)
@@ -865,7 +865,7 @@ void MemoryView::openContextMenu(QPoint pos)
865865

866866
void MemoryView::contextCopyByte()
867867
{
868-
QApplication::clipboard()->setText(QString::number(cpu().read8(m_table.selectedAddress), 16).toUpper());
868+
QApplication::clipboard()->setText(QString::number(cpu().Read8(m_table.selectedAddress), 16).toUpper());
869869
}
870870

871871
void MemoryView::contextCopySegment()
@@ -885,7 +885,7 @@ void MemoryView::contextCopySegment()
885885

886886
void MemoryView::contextCopyCharacter()
887887
{
888-
QApplication::clipboard()->setText(QChar::fromLatin1(cpu().read8(m_table.selectedAddress)).toUpper());
888+
QApplication::clipboard()->setText(QChar::fromLatin1(cpu().Read8(m_table.selectedAddress)).toUpper());
889889
}
890890

891891
void MemoryView::contextPaste()
@@ -913,7 +913,7 @@ void MemoryView::contextGoToAddress()
913913
void MemoryView::contextFollowAddress()
914914
{
915915
bool valid;
916-
u32 address = cpu().read32(m_table.selectedAddress & ~3, valid);
916+
u32 address = cpu().Read32(m_table.selectedAddress & ~3, &valid);
917917
if (!valid)
918918
return;
919919

0 commit comments

Comments
 (0)