-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpCode.cpp
More file actions
528 lines (402 loc) · 12.8 KB
/
Copy pathOpCode.cpp
File metadata and controls
528 lines (402 loc) · 12.8 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//
// OpCode.cpp
// 65calc02
//
// Created by Kelvin Sherlock on 9/13/2011.
// Copyright 2011 Kelvin W Sherlock LLC. All rights reserved.
//
#include "OpCode.h"
#include "gen-o-table.h"
#include <unordered_map>
#include <tuple>
struct MMA {
Machine machine;
Mnemonic mnemonic;
AddressMode addressMode;
MMA(Machine a, Mnemonic b, AddressMode c) :
machine(a), mnemonic(b), addressMode(c)
{
}
MMA(OpCode op) :
machine(op.machine()), mnemonic(op.mnemonic()), addressMode(op.addressMode())
{
}
size_t hash() const
{
return machine | mnemonic << 8 | addressMode << 16;
}
bool operator==(const MMA& rhs) const
{
return machine == rhs.machine
&& mnemonic == rhs.mnemonic
&& addressMode == rhs.addressMode;
}
};
//typedef std::tr1::tuple<Machine, Mnemonic, AddressMode> MMA;
typedef std::pair<Machine, uint8_t> MO;
namespace std {
template<> struct hash<MMA> : public unary_function<MMA, size_t>
{
size_t operator()(MMA mma) const
{
return mma.hash();
//return get<0>(mma) | get<1>(mma) << 8 | get<2>(mma) << 16;
}
};
template<> struct hash<MO> : public unary_function<MO, size_t>
{
size_t operator()(MO mo) const
{
return mo.first << 8 | mo.second;
}
};
};
typedef std::unordered_map<MMA, OpCode> HashTable;
static HashTable hash;
void OpCode::InitHashTable()
{
// map tuple of machine + mnemonic + address mode -> opcode
if (hash.empty())
{
// 6502 includes empty entries but not extra nops.
for (unsigned i = 0; i < 256; ++i)
{
const OpCode &op = m6502_opcodes[i];
if (op)
{
hash[MMA(op)] = op;
}
}
// 65c02 -- includes extra nops.
for (unsigned i = 0; i < 256; ++i)
{
const OpCode &op = m65c02_opcodes[i];
if (op.mnemonic() == NOP && i != 0xea) continue;
hash[MMA(op)] = op;
}
// w65c02 -- includes extra nops.
for (unsigned i = 0; i < 256; ++i)
{
const OpCode &op = mw65c02_opcodes[i];
if (op.mnemonic() == NOP && i != 0xea) continue;
hash[MMA(op)] = op;
}
// rockwell -- includes extra nops.
for (unsigned i = 0; i < 256; ++i)
{
const OpCode &op = mr65c02_opcodes[i];
if (op.mnemonic() == NOP && i != 0xea) continue;
hash[MMA(op)] = op;
}
// 65816 -- all instructions valid.
for (unsigned i = 0; i < 256; ++i)
{
const OpCode &op = m65816_opcodes[i];
hash[MMA(op)] = op;
}
}
}
OpCode::OpCode(Instruction instr, AddressMode addressMode) : OpCode()
{
InitHashTable();
HashTable::iterator iter;
iter = hash.find(MMA(instr.machine(), instr.mnemonic(), addressMode));
if (iter == hash.end()) return;
*this = iter->second;
}
OpCode::OpCode(Machine machine, Mnemonic m, AddressMode addressMode) :
OpCode(Instruction(machine, m), addressMode)
{
}
OpCode::OpCode(Machine machine, uint8_t opcode) : OpCode()
{
switch (machine)
{
case kUndefinedMachine:
break;
case m6502:
*this = m6502_opcodes[opcode];
break;
case m65c02:
*this = m65c02_opcodes[opcode];
break;
case mw65c02:
*this = mw65c02_opcodes[opcode];
break;
case mr65c02:
*this = mr65c02_opcodes[opcode];
break;
case m65802:
case m65816:
case m65816_e:
*this = m65816_opcodes[opcode];
break;
case m65ce02:
*this = m65ce02_opcodes[opcode];
break;
}
}
const_vector<OpCode> OpCode::OpCodes(Machine machine)
{
#undef xxx
#define xxx(table) { return const_vector<OpCode>(table, 256); }
switch (machine)
{
default:
case kUndefinedMachine:
return const_vector<OpCode>();
break;
case m6502:
xxx(m6502_opcodes);
break;
case m65c02:
xxx(m65c02_opcodes);
break;
case mw65c02:
xxx(mw65c02_opcodes);
break;
case mr65c02:
xxx(mr65c02_opcodes);
break;
case m65802:
case m65816:
xxx(m65816_opcodes);
break;
}
}
int OpCode::opcode(Machine machine, Mnemonic mnemonic, AddressMode addressMode)
{
InitHashTable();
HashTable::iterator iter;
iter = hash.find(MMA(machine, mnemonic, addressMode));
if (iter == hash.end()) return -1;
return iter->second.opcode();
}
#ifdef __OBJC__
NSString *OpCode::formatOperand(uint32_t operand, uint16_t address, bool longM, bool longX) const
{
unsigned b;
b = bytes(longM, longX) - 1;
if (_addressMode == immediate && _machine == m65816)
{
if (b == 4)
return [NSString stringWithFormat: @"#%04X", operand];
}
switch (_addressMode)
{
case kUndefinedAddressMode:
return nil;
case implied:
return @"";
case immediate:
return [NSString stringWithFormat: @"#%02X", operand];
case interrupt:
case zp:
return [NSString stringWithFormat: @"%02X", operand];
case zp_x:
return [NSString stringWithFormat: @"%02X,X", operand];
case zp_y:
return [NSString stringWithFormat: @"%02X,Y", operand];
case zp_indirect:
return [NSString stringWithFormat: @"(%02X)", operand];
case zp_indirect_x:
return [NSString stringWithFormat: @"(%02X,X)", operand];
case zp_indirect_y:
return [NSString stringWithFormat: @"(%02X),Y", operand];
case zp_indirect_long:
return [NSString stringWithFormat: @"[%02X]", operand];
case zp_indirect_long_y:
return [NSString stringWithFormat: @"[%02X],Y", operand];
case absolute:
return [NSString stringWithFormat: @"%04X", operand];
case absolute_x:
return [NSString stringWithFormat: @"%04X,X", operand];
case absolute_y:
return [NSString stringWithFormat: @"%04X,Y", operand];
case absolute_indirect:
return [NSString stringWithFormat: @"(%04X)", operand];
case absolute_indirect_x:
return [NSString stringWithFormat: @"(%04X,X)", operand];
case absolute_long:
return [NSString stringWithFormat: @"%06X", operand];
case absolute_long_x:
return [NSString stringWithFormat: @"%06X,X", operand];
case absolute_indirect_long:
return [NSString stringWithFormat: @"[%04X]", operand];
case block:
return [NSString stringWithFormat: @"%02X,%02X", operand >> 8, operand & 0xff];
case stack_relative:
return [NSString stringWithFormat: @"%02X,S", operand];
case stack_relative_y:
return [NSString stringWithFormat: @"(%02X,S),Y", operand];
case relative_long:
if (true)
{
uint16_t tmp;
//bool n;
operand &= 0xffff;
//n = operand & 0x8000;
tmp = address + 3 + operand;
tmp &= 0xffff;
#if 0
// todo -- if operand == 0, "*"
if (n)
return [NSString stringWithFormat: @"%04X (*-%04X)",
tmp, 0x1000 - operand];
else
return [NSString stringWithFormat: @"%04X (*+%04X)",
tmp, operand];
#else
return [NSString stringWithFormat: @"%04X", tmp];
#endif
}
case relative:
if (true)
{
uint16_t tmp;
bool n;
operand &= 0xff;
n = operand & 0x80;
tmp = address + 2 + operand;
if (n) tmp += 0xff00;
tmp &= 0xffff;
#if 0
if (n)
{
tmp += 0xff00;
return [NSString stringWithFormat: @"%04X (*-%02X)",
tmp, 0x100 - operand];
}
else
return [NSString stringWithFormat: @"%04X (*+%02X)",
tmp, operand];
#else
return [NSString stringWithFormat: @"%04X", tmp];
#endif
}
case zp_relative:
if (true)
{
uint16_t tmp;
bool n;
unsigned zp;
zp = operand & 0xff;
operand >>= 8;
n = operand & 0x80;
tmp = address + 3 + (operand & 0xff);
if (n) tmp += 0xff00;
#if 0
if (n)
{
tmp += 0xff00;
return [NSString stringWithFormat: @"%02X,%04X (*-%02X)",
zp, tmp, 0x100 - operand];
}
else
return [NSString stringWithFormat: @"%02X,%04X (*+%02X)",
zp, tmp, operand];
#else
return [NSString stringWithFormat: @"%02X,%04X", zp, tmp];
#endif
}
}
return nil;
}
NSString *OpCode::formatHex(uint32_t operand, bool longM, bool longX) const
{
static const char *hex = "0123456789ABCDEF";
unsigned b;
unichar tmp[4 * 3];
unsigned length;
b = bytes(longM, longX);
//for (unsigned i = 0; i < sizeof(tmp); ++i) tmp[i] = ' ';
tmp[0] = hex[(_opcode >> 4) & 0x0f];
tmp[1] = hex[_opcode & 0x0f];
length = 2;
if (b >= 2 && b <= 4)
{
while (--b)
{
tmp[length++] = ' ';
tmp[length++] = hex[(operand >> 4) & 0x0f];
tmp[length++] = hex[operand & 0x0f];
operand >>= 8;
}
//length--;
}
return [NSString stringWithCharacters: tmp length: length];
}
#endif
unsigned OpCode::bytes(bool longM, bool longXY) const
{
if (_addressMode == immediate)
{
switch (_mnemonic)
{
case LDX:
case LDY:
case CPX:
case CPY:
return longXY ? 3 : 2;
case ADC:
case AND:
case BIT:
case CMP:
case EOR:
case LDA:
case ORA:
case SBC:
return longM ? 3 : 2;
case PHK:
return 3;
default:
break;
}
}
switch (_addressMode)
{
case implied:
return 1;
case immediate:
return 2; // should be caught above.
case zp:
case zp_x:
case zp_y:
case zp_indirect:
case zp_indirect_x:
case zp_indirect_y:
case zp_indirect_z:
case zp_indirect_long:
case zp_indirect_long_y:
case stack_relative:
case stack_relative_y:
return 2;
case zp_relative:
// bbr0 zp,offset
return 3;
case absolute:
case absolute_x:
case absolute_y:
case absolute_indirect:
case absolute_indirect_x:
return 3;
case relative:
return 2;
case relative_long:
return 3;
case interrupt:
// brk, cop, wdm
return 2;
case absolute_long:
case absolute_long_x:
return 4;
case absolute_indirect_long:
// jml [abs]
return 3;
case block:
// mvn, srcb, destb
return 3;
case kUndefinedAddressMode:
return 0;
}
}