-
Notifications
You must be signed in to change notification settings - Fork 2
/
disassembler.cpp
69 lines (60 loc) · 2.3 KB
/
disassembler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "disassembler.h"
#include "commondefs.h"
#include "commonformatters.h"
#include "instruction.h"
#include "instructiontable.h"
#include "mnemonics.h"
#include <QStringList>
#include <map>
QString Disassembler::formatOperand8() const {
return "$" + formatHexByte(memory[address + 1]);
}
QString Disassembler::formatOperand16() const {
return "$" + formatHexWord(memory.word(address + 1));
}
Disassembler::Disassembler(const Memory& memory, Address pc) : memory(memory) {
setOrigin(pc);
}
void Disassembler::setOrigin(Address addr) {
address = addr;
opcode = memory[addr];
instruction = InstructionTable[opcode];
}
void Disassembler::nextInstruction() {
setOrigin(address + instruction.size);
}
QString Disassembler::dumpBytes(uint16_t n) const {
QString str;
for (uint16_t i = 0; i < n; i++) { str.append(formatHexByte(memory[address + i])).append(" "); }
return str;
}
QString Disassembler::dumpWords(uint16_t n) const {
QString str;
for (uint16_t i = 0; i < n; i += 2) { str.append(formatHexWord(memory.word(address + i))).append(" "); }
return str;
}
QString Disassembler::disassemble() const {
QString str;
for (uint8_t i = 0; i < 3; i++) { str.append(i < instruction.size ? formatHexByte(memory[address + i]).append(" ") : " "); }
str.append(" ");
str.append(MnemonicTable.at(instruction.type)).append(" ");
switch (instruction.mode) {
case ImpliedOrAccumulator: break;
case Immediate: str.append("#").append(formatOperand8()); break;
case Absolute: str.append(formatOperand16()); break;
case AbsoluteX: str.append(formatOperand16()).append(",X"); break;
case AbsoluteY: str.append(formatOperand16()).append(",Y"); break;
case ZeroPage: str.append(formatOperand8()); break;
case ZeroPageX: str.append(formatOperand8()).append(",X"); break;
case ZeroPageY: str.append(formatOperand8()).append(",Y"); break;
case IndexedIndirectX: str.append("(").append(formatOperand8()).append(",X)"); break;
case IndirectIndexedY: str.append("(").append(formatOperand8()).append("),Y"); break;
case Indirect: str.append("(").append(formatOperand16()).append(")"); break;
case Branch:
const auto displacement = static_cast<int8_t>(memory[address + 1]);
if (displacement > 0) str.append("+");
str.append(QString::number(displacement));
break;
}
return str.toUpper();
}