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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
#include "disasm.h"
#include "data_buffer.h"
#include "common.h"
#include <cassert>
#include <cstdio>
#include <cstdlib>
static void disasm_verbatim(
DisasmNode& node, uint16_t instr, const DataBuffer &, const Settings &)
{
node.size = kInstructionSizeStepBytes;
snprintf(node.mnemonic, kMnemonicBufferSize, ".short");
snprintf(node.arguments, kArgsBufferSize, "0x%04x", instr);
}
static void disasm_mfff0_v4e70(
DisasmNode& node, uint16_t instr, const DataBuffer &code, const Settings &s)
{
node.size = kInstructionSizeStepBytes;
if (instr == 0x4e70) {
snprintf(node.mnemonic, kMnemonicBufferSize, "reset");
} else if (instr == 0x4e71) {
snprintf(node.mnemonic, kMnemonicBufferSize, "nop");
} else if (instr == 0x4e73) {
snprintf(node.mnemonic, kMnemonicBufferSize, "rte");
} else if (instr == 0x4e75) {
snprintf(node.mnemonic, kMnemonicBufferSize, "rts");
} else if (instr == 0x4e76) {
snprintf(node.mnemonic, kMnemonicBufferSize, "trapv");
} else if (instr == 0x4e77) {
snprintf(node.mnemonic, kMnemonicBufferSize, "rtr");
} else {
disasm_verbatim(node, instr, code, s);
}
}
enum class JsrJmp {
kJsr,
kJmp,
};
static void disasm_jsr_jmp(
DisasmNode& node, uint16_t instr, const DataBuffer &code, const Settings &s, JsrJmp jsrjmp)
{
const char *mnemonic = (jsrjmp == JsrJmp::kJsr) ? "jsr" : "jmp";
const int addrmode = instr & 0x3f;
const int m = (addrmode >> 3) & 0x7;
const int xn = addrmode & 0x7;
switch (m) {
case 0: // 4e80 .. 4e87
case 1: // 4e88 .. 4e8f
break;
case 2: // 4e90 .. 4e97
node.size = kInstructionSizeStepBytes;
snprintf(node.mnemonic, kMnemonicBufferSize, "%s", mnemonic);
snprintf(node.arguments, kArgsBufferSize, "%%a%d@", xn);
return;
case 3: // 4e98 .. 4e9f
case 4: // 4ea0 .. 4ea7
break;
case 5: // 4ea8 .. 4eaf, Displacement
{
node.size = kInstructionSizeStepBytes * 2;
const int16_t dispmt = GetI16BE(code.buffer + node.offset + kInstructionSizeStepBytes);
snprintf(node.mnemonic, kMnemonicBufferSize, "%s", mnemonic);
snprintf(node.arguments, kArgsBufferSize, "%%a%d@(%d:w)", xn, dispmt);
return;
}
case 6: // 4eb0 .. 4eb7, Brief Extension Word
{
node.size = kInstructionSizeStepBytes * 2;
const uint16_t briefext = GetU16BE(code.buffer + node.offset + kInstructionSizeStepBytes);
const char reg = ((briefext >> 15) & 1) ? 'a' : 'd';
const int xn2 = (briefext >> 12) & 7;
const char size_spec = ((briefext >> 11) & 1) ? 'l' : 'w';
const int8_t dispmt = briefext & 0xff;
snprintf(node.mnemonic, kMnemonicBufferSize, "%s", mnemonic);
snprintf(node.arguments, kArgsBufferSize,
"%%a%d@(%d,%%%c%d:%c)", xn, dispmt, reg, xn2, size_spec);
return;
}
case 7: // 4eb8 .. 4ebf, some are with Brief Extension Word
switch (xn) {
case 0: // 4eb8 (xxx).W
{
// TODO set has_branch_addr and branch_addr
node.size = kInstructionSizeStepBytes * 2;
const int32_t dispmt = GetI16BE(code.buffer + node.offset + kInstructionSizeStepBytes);
snprintf(node.mnemonic, kMnemonicBufferSize, "%s", mnemonic);
snprintf(node.arguments, kArgsBufferSize, "0x%x:w", dispmt);
return;
}
case 1: // 4eb9 (xxx).L
{
node.size = kInstructionSizeStepBytes * 3;
const int32_t dispmt = GetI32BE(code.buffer + node.offset + kInstructionSizeStepBytes);
snprintf(node.mnemonic, kMnemonicBufferSize, "%s", mnemonic);
snprintf(node.arguments, kArgsBufferSize, "0x%x:l", dispmt);
return;
}
case 2: // 4eba, Displacement
{
node.size = kInstructionSizeStepBytes * 2;
const int16_t dispmt = GetI16BE(code.buffer + node.offset + kInstructionSizeStepBytes);
snprintf(node.mnemonic, kMnemonicBufferSize, "%s", mnemonic);
snprintf(node.arguments, kArgsBufferSize, "%%pc@(%d:w)", dispmt);
return;
}
case 3: // 4ebb
{
node.size = kInstructionSizeStepBytes * 2;
const uint16_t briefext = GetU16BE(
code.buffer + node.offset + kInstructionSizeStepBytes);
const char reg = ((briefext >> 15) & 1) ? 'a' : 'd';
const int xn2 = (briefext >> 12) & 7;
const char size_spec = ((briefext >> 11) & 1) ? 'l' : 'w';
const int8_t dispmt = briefext & 0xff;
snprintf(node.mnemonic, kMnemonicBufferSize, "%s", mnemonic);
snprintf(node.arguments, kArgsBufferSize,
"%%pc@(%d,%%%c%d:%c)", dispmt, reg, xn2, size_spec);
return;
}
case 4: // 4ebc
case 5: // 4ebd
case 6: // 4ebe
break;
}
break;
}
return disasm_verbatim(node, instr, code, s);
}
static void disasm_jsr(
DisasmNode& node, uint16_t instr, const DataBuffer &code, const Settings &s)
{
return disasm_jsr_jmp(node, instr, code, s, JsrJmp::kJsr);
}
static void disasm_jmp(
DisasmNode& node, uint16_t instr, const DataBuffer &code, const Settings &s)
{
return disasm_jsr_jmp(node, instr, code, s, JsrJmp::kJmp);
}
enum class Condition {
kT = 0,
kF = 1,
kHI = 2,
kLS = 3,
kCC = 4,
kCS = 5,
kNE = 6,
kEQ = 7,
kVC = 8,
kVS = 9,
kPL = 10,
kMI = 11,
kGE = 12,
kLT = 13,
kGT = 14,
kLE = 15,
};
static inline const char *branch_instr_name_by_cond(Condition condition)
{
switch (condition) {
case Condition::kT: return "bra"; // 60xx
case Condition::kF: return "bsr"; // 61xx
case Condition::kHI: return "bhi"; // 62xx
case Condition::kLS: return "bls"; // 63xx
case Condition::kCC: return "bcc"; // 64xx
case Condition::kCS: return "bcs"; // 65xx
case Condition::kNE: return "bne"; // 66xx
case Condition::kEQ: return "beq"; // 67xx
case Condition::kVC: return "bvc"; // 68xx
case Condition::kVS: return "bvs"; // 69xx
case Condition::kPL: return "bpl"; // 6axx
case Condition::kMI: return "bmi"; // 6bxx
case Condition::kGE: return "bge"; // 6cxx
case Condition::kLT: return "blt"; // 6dxx
case Condition::kGT: return "bgt"; // 6exx
case Condition::kLE: return "ble"; // 6fxx
}
return "?";
}
static void disasm_bra_bsr_bcc(
DisasmNode& node, uint16_t instr, const DataBuffer &code, const Settings &)
{
const char *mnemonic = branch_instr_name_by_cond(static_cast<Condition>((instr >> 8) & 0xf));
int dispmt = static_cast<int8_t>(instr & 0xff);
const char *size_spec = "s";
if (dispmt == 0) {
dispmt = GetI16BE(code.buffer + node.offset + kInstructionSizeStepBytes);
node.size = kInstructionSizeStepBytes * 2;
size_spec = "w";
} else {
node.size = kInstructionSizeStepBytes;
}
dispmt += kInstructionSizeStepBytes;
node.branch_addr = node.offset + dispmt;
node.has_branch_addr = true;
const char * const sign = dispmt >= 0 ? "+" : "";
snprintf(node.mnemonic, kMnemonicBufferSize, "%s%s", mnemonic, size_spec);
snprintf(node.arguments, kArgsBufferSize, ".%s%d", sign, dispmt);
return;
}
static void m68k_disasm(
DisasmNode& node, uint16_t instr, const DataBuffer &code, const Settings &s)
{
if ((instr & 0xfff0) == 0x4e70) {
return disasm_mfff0_v4e70(node, instr, code, s);
} else if ((instr & 0xffc0) == 0x4e80) {
return disasm_jsr(node, instr, code, s);
} else if ((instr & 0xffc0) == 0x4ec0) {
return disasm_jmp(node, instr, code, s);
} else if ((instr & 0xf000) == 0x6000) {
return disasm_bra_bsr_bcc(node, instr, code, s);
}
return disasm_verbatim(node, instr, code, s);
}
void DisasmNode::Disasm(const DataBuffer &code, const Settings &s)
{
// We assume that no MMU and ROM is always starts with 0
assert(this->offset < code.occupied_size);
// It is possible to have multiple DisasmNode::Disasm() calls, and there is
// no point to disassemble it again if it already has mnemonic determined
if (this->mnemonic[0] != '\0') {
return;
}
const uint16_t instr = GetU16BE(code.buffer + this->offset);
m68k_disasm(*this, instr, code, s);
}
void DisasmNode::AddReferencedBy(uint32_t offset)
{
ReferenceNode *node{};
if (this->last_ref_by) {
node = this->last_ref_by;
} else {
node = new ReferenceNode{};
assert(node);
this->ref_by = this->last_ref_by = node;
}
node->refs[node->refs_count] = offset;
node->refs_count++;
if (node->refs_count >= kRefsCountPerBuffer) {
ReferenceNode *new_node = new ReferenceNode{};
assert(new_node);
node->next = new_node;
this->last_ref_by = new_node;
}
}
DisasmNode::~DisasmNode()
{
ReferenceNode *ref{this->ref_by};
while (ref) {
ReferenceNode *prev = ref;
ref = ref->next;
delete prev;
}
}
|