-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.cpp
633 lines (615 loc) · 18.1 KB
/
decode.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
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
#include <iostream>
#include <iomanip>
#include "thumbsim.hpp"
using namespace std;
/* Decode to find what type it is */
Thumb_Types decode (const ALL_Types);
/* Decodes for each type */
ALU_Ops decode (const ALU_Type);
DP_Ops decode (const DP_Type);
SP_Ops decode (const SP_Type);
LD_ST_Ops decode (const LD_ST_Type);
MISC_Ops decode (const MISC_Type);
BL_Ops decode (const BL_Type);
int decode (const COND_Type);
int decode (const UNCOND_Type);
int decode (const LDM_Type);
int decode (const STM_Type);
int decode (const LDRL_Type);
int decode (const ADD_SP_Type);
/* This function is complete, no modifications needed */
Thumb_Types decode (const ALL_Types data) {
if (data.type.alu.instr.class_type.type_check == ALU_TYPE) {
return ALU;
}
else if (data.type.dp.instr.class_type.type_check == DP_TYPE) {
return DP;
}
else if (data.type.sp.instr.class_type.type_check == SP_TYPE) {
return SPECIAL;
}
else if (data.type.uncond.instr.class_type.type_check == UNCOND_TYPE) {
return UNCOND;
}
else if (data.type.bl.instr.class_type.type_check == BL_TYPE) {
return BL;
}
else if (data.type.misc.instr.class_type.type_check == MISC_TYPE) {
return MISC;
}
else if (data.type.cond.instr.class_type.type_check == COND_TYPE) {
return COND;
}
else if (data.type.ldm.instr.class_type.type_check == LDM_TYPE) {
return LDM;
}
else if (data.type.stm.instr.class_type.type_check == STM_TYPE) {
return STM;
}
else if (data.type.ldrl.instr.class_type.type_check == LDRL_TYPE) {
return LDRL;
}
else if (data.type.addsp.instr.class_type.type_check == ADD_SP_TYPE) {
return ADD_SP;
}
else {
if (data.type.ld_st.instr.class_type.opA == LD_ST_REG_OPA) {
}
else if (data.type.ld_st.instr.class_type.opA == LD_ST_IMM_OPA) {
}
else if (data.type.ld_st.instr.class_type.opA == LD_ST_IMMB_OPA) {
}
else if (data.type.ld_st.instr.class_type.opA == LD_ST_IMMH_OPA) {
}
else if (data.type.ld_st.instr.class_type.opA == LD_ST_IMMSP_OPA) {
}
else {
cout << "NO TYPE FOUND" << endl;
return ERROR_TYPE;
}
return LD_ST;
}
}
ALU_Ops decode (const ALU_Type data) {
if (data.instr.lsli.op == ALU_LSLI_OP) {
// 315: insert code here to print lsls instruction
if (opts.instrs) {
cout << "lsls r" << data.instr.lsli.rd << ", r" << data.instr.lsli.rm << ", #" << data.instr.lsli.imm << endl;
}
return ALU_LSLI;
}
else if (data.instr.addr.op == ALU_ADDR_OP) {
if (opts.instrs) {
cout << "adds r" << data.instr.addr.rd << ", r" << data.instr.addr.rn << ", r" << data.instr.addr.rm << endl;
}
return ALU_ADDR;
}
else if (data.instr.subr.op == ALU_SUBR_OP) {
// 315: insert code here to print subs instruction
if (opts.instrs) {
cout << "subs r" << data.instr.subr.rd << ", r" << data.instr.subr.rn << ", r" << data.instr.subr.rm << endl;
}
return ALU_SUBR;
}
else if (data.instr.add3i.op == ALU_ADD3I_OP) {
// complete
if (opts.instrs) {
cout << "adds r" << data.instr.add3i.rd << ", r" << data.instr.add3i.rn << ", #" << data.instr.add3i.imm << endl;
}
return ALU_ADD3I;
}
else if (data.instr.sub3i.op == ALU_SUB3I_OP) {
// 315: insert code here to print subs instruction
if (opts.instrs) {
cout << "subs r" << data.instr.sub3i.rd << ", r" << data.instr.sub3i.rn << ", #" << data.instr.sub3i.imm << endl;
}
return ALU_SUB3I;
}
else if (data.instr.add8i.op == ALU_ADD8I_OP) {
// complete
if (opts.instrs) {
cout << "adds r" << data.instr.add8i.rdn << ", #" << setbase(10) << data.instr.add8i.imm << endl;
}
return ALU_ADD8I;
}
else if (data.instr.sub8i.op == ALU_SUB8I_OP) {
// 315: insert code here to print subs instruction
if (opts.instrs) {
cout << "subs r" << data.instr.sub8i.rdn << ", #" << setbase(10) << data.instr.sub8i.imm << endl;
}
return ALU_SUB8I;
}
else if (data.instr.cmp.op == ALU_CMP_OP) {
// complete
if (opts.instrs) {
cout << "cmp r" << data.instr.cmp.rdn << ", #" << setbase(10) << data.instr.cmp.imm << endl;
}
return ALU_CMP;
}
else if (data.instr.mov.op == ALU_MOV_OP) {
// complete
if (opts.instrs) {
cout << "movs r" << data.instr.mov.rdn << ", #" << setbase(10) << (data.instr.mov.imm) << endl;
}
return ALU_MOV;
}
cout << "[ERROR] Unknown Instruction to be decoded" << endl;
exit(1);
}
DP_Ops decode (const DP_Type data) {
if (data.instr.DP_Instr.op == DP_CMP) {
// 315: insert code here to print cmp instruction
if (opts.instrs) {
cout << "cmp r" << data.instr.DP_Instr.rdn << ", r" << data.instr.DP_Instr.rm << endl;
}
return DP_CMP;
}
else {
cerr << "UNKNOWN DP TYPE -- Exiting" << endl;
exit(1);
}
}
SP_Ops decode (const SP_Type data) {
if (data.instr.mov.op == 2) {
if (opts.instrs) {
cout << "mov";
if (data.instr.mov.d) {
// These two cases handle stack pointer printing
if (data.instr.mov.rd == 5) {
cout << " sp, r" << setbase(10) << data.instr.mov.rm << endl;
}
else if (data.instr.mov.rm == 13) {
cout << " r" << setbase(10) << (8+data.instr.mov.rd) << ", sp" << endl;
}
// this case is for registers greater than r7 that aren't sp
else {
cout << " r" << setbase(10) << (8+data.instr.mov.rd) << ", r" << setbase(10) << data.instr.mov.rm << endl;
}
}
// another stack pointer case
else if (data.instr.mov.rm == 13) {
cout << " r" << data.instr.mov.rd << ", sp" << endl;
}
else {
cout << " r" << setbase(10) << data.instr.mov.rd << ", r" << data.instr.mov.rm << endl;
}
}
return SP_MOV;
}
else if (data.instr.add.op == 0) {
// Here you'll need to SP_ADD similar to above
if (opts.instrs) {
cout << "add";
if (data.instr.add.d) {
// These two cases handle stack pointer printing
if (data.instr.add.rd == 5) {
cout << " sp, sp, r" << setbase(10) << data.instr.add.rm << endl;
}
else if (data.instr.add.rm == 13) {
cout << " r" << setbase(10) << (8+data.instr.add.rd)<< ", r" << setbase(10) << (8+data.instr.add.rd) << ", sp" << endl;
}
// this case is for registers greater than r7 that aren't sp
else {
cout << " r" << setbase(10) << (8+data.instr.add.rd) << ", r" << setbase(10) << (8+data.instr.add.rd) << ", r"<< setbase(10) << data.instr.add.rm << endl;
}
}
// another stack pointer case
else if (data.instr.add.rm == 13) {
cout << " r" << data.instr.add.rd << ", sp, sp" << endl;
}
else {
cout << " r" << setbase(10) << data.instr.add.rd << ", r" << setbase(10) << data.instr.add.rd << ", r" << data.instr.add.rm << endl;
}
}
return SP_ADD;
}
else if (data.instr.cmp.op == 1) {
// Here you'll need to SP_CMP similar to above
if (opts.instrs) {
cout << "cmp";
if (data.instr.cmp.d) {
// These two cases handle stack pointer printing
if (data.instr.cmp.rd == 5) {
cout << " sp, r" << setbase(10) << data.instr.cmp.rm << endl;
}
else if (data.instr.cmp.rm == 13) {
cout << " r" << setbase(10) << (8+data.instr.cmp.rd) << ", sp" << endl;
}
// this case is for registers greater than r7 that aren't sp
else {
cout << " r" << setbase(10) << (8+data.instr.cmp.rd) << ", r" << setbase(10) << data.instr.cmp.rm << endl;
}
}
// another stack pointer case
else if (data.instr.cmp.rm == 13) {
cout << " r" << data.instr.cmp.rd << ", sp" << endl;
}
else {
cout << " r" << setbase(10) << data.instr.cmp.rd << ", r" << data.instr.cmp.rm << endl;
}
}
return SP_CMP;
}
else {
cerr << "UNKNOWN SP TYPE -- Exiting" << endl;
exit(1);
}
}
LD_ST_Ops decode (const LD_ST_Type data) {
if (data.instr.class_type.opA == LD_ST_REG_OPA) {
if (data.instr.class_type.opB == LD_ST_OPB_LDRB) {
// 315: write code to print ldrb
if (opts.instrs) {
cout << "ldrb r" << data.instr.ld_st_reg.rt << ", [r" << data.instr.ld_st_reg.rn << ", r" << data.instr.ld_st_reg.rm << "]" << endl;
}
return LDRBR;
}
else if (data.instr.class_type.opB == LD_ST_OPB_STRB) {
// 315: write code to print strb
if (opts.instrs) {
cout << "strb r" << data.instr.ld_st_reg.rt << ", [r" << data.instr.ld_st_reg.rn << ", r" << data.instr.ld_st_reg.rm << "]" << endl;
}
return STRBR;
}
else if (data.instr.class_type.opB == LD_ST_OPB_LDR) {
// 315: write code to print ldr
if (opts.instrs) {
cout << "ldr r" << data.instr.ld_st_reg.rt << ", [r" << data.instr.ld_st_reg.rn << ", r" << data.instr.ld_st_reg.rm << "]" << endl;
}
return LDRR;
}
else if (data.instr.class_type.opB == LD_ST_OPB_STR) {
// 315: write code to print str
if (opts.instrs) {
cout << "str r" << data.instr.ld_st_reg.rt << ", [r" << data.instr.ld_st_reg.rn << ", r" << data.instr.ld_st_reg.rm << "]" << endl;
}
return STRR;
}
}
else if (data.instr.class_type.opA == LD_ST_IMM_OPA) {
if (data.instr.class_type.opB == LD_ST_OPB_STR) {
if (opts.instrs) {
cout << "str r" << data.instr.ld_st_imm.rt << ", [r" << data.instr.ld_st_imm.rn << ", #" << setbase(10) << (data.instr.ld_st_imm.imm*4) << "]" << endl;
}
return STRI;
}
else if (data.instr.class_type.opB == LD_ST_OPB_LDR) {
if (opts.instrs) {
cout << "ldr r" << data.instr.ld_st_imm.rt << ", [r" << data.instr.ld_st_imm.rn << ", #" << setbase(10) << (data.instr.ld_st_imm.imm*4) << "]" << endl;
}
return LDRI;
}
}
else if (data.instr.class_type.opA == LD_ST_IMMB_OPA) {
if (data.instr.ld_st_imm.op == LD_ST_LDB) {
// 315: write code to print ldrb
if (opts.instrs) {
cout << "ldrb r" << data.instr.ld_st_imm.rt << ", [r" << data.instr.ld_st_imm.rn << ", #" << setbase(10) << (data.instr.ld_st_imm.imm*4) << "]" << endl;
}
return LDRBI;
}
else if (data.instr.ld_st_imm.op == LD_ST_STB) {
// 315: write code to print strb
if (opts.instrs) {
cout << "strb r" << data.instr.ld_st_imm.rt << ", [r" << data.instr.ld_st_imm.rn << ", #" << setbase(10) << (data.instr.ld_st_imm.imm*4) << "]" << endl;
}
return STRBI;
}
}
else if (data.instr.class_type.opA == LD_ST_IMMB_OPA) {
}
else if (data.instr.class_type.opA == LD_ST_IMMH_OPA) {
}
else if (data.instr.class_type.opA == LD_ST_IMMSP_OPA) {
if (data.instr.ld_st_sp_imm.opB) {
if (opts.instrs) {
cout << "ldr r" << data.instr.ld_st_sp_imm.rt << ", [sp, #" << setbase(10) << data.instr.ld_st_sp_imm.imm << "]" << endl;
return LDRSP;
}
}
else {
if (opts.instrs) {
cout << "str r" << data.instr.ld_st_sp_imm.rt << ", [sp, #" << setbase(10) << data.instr.ld_st_sp_imm.imm << "]" << endl;
return STRSP;
}
}
}
cout << "[ERROR] Unknown Instruction to be decoded" << endl;
exit(1);
}
MISC_Ops decode (const MISC_Type data) {
if (data.instr.push.op == MISC_PUSH_OP) {
// Push is complete
if (opts.instrs) {
bool multiple = FALSE;
cout << "push ";
cout << "{";
if (data.instr.push.reg_list & 1) {
cout << "r0";
multiple = TRUE;
}
if (data.instr.push.reg_list & 2) {
if (multiple)
cout << ", ";
cout << "r1";
multiple = TRUE;
}
if (data.instr.push.reg_list & 4) {
if (multiple)
cout << ", ";
cout << "r2";
multiple = TRUE;
}
if (data.instr.push.reg_list & 8) {
if (multiple)
cout << ", ";
cout << "r3";
multiple = TRUE;
}
if (data.instr.push.reg_list & 16) {
if (multiple)
cout << ", ";
cout << "r4";
multiple = TRUE;
}
if (data.instr.push.reg_list & 32) {
if (multiple)
cout << ", ";
cout << "r5";
multiple = TRUE;
}
if (data.instr.push.reg_list & 64) {
if (multiple)
cout << ", ";
cout << "r6";
multiple = TRUE;
}
if (data.instr.push.reg_list & 128) {
if (multiple)
cout << ", ";
cout << "r7";
multiple = TRUE;
}
if (data.instr.push.m) {
if (multiple)
cout << ", ";
cout << "lr";
}
cout << "}" << endl;
}
return MISC_PUSH;
}
else if (data.instr.pop.op == MISC_POP_OP) {
// Pop is complete
if (opts.instrs) {
bool multiple = FALSE;
cout << "pop ";
cout << "{";
if (data.instr.pop.reg_list & 1) {
cout << "r0";
multiple = TRUE;
}
if (data.instr.pop.reg_list & 2) {
if (multiple)
cout << ", ";
cout << "r1";
multiple = TRUE;
}
if (data.instr.pop.reg_list & 4) {
if (multiple)
cout << ", ";
cout << "r2";
multiple = TRUE;
}
if (data.instr.pop.reg_list & 8) {
if (multiple)
cout << ", ";
cout << "r3";
multiple = TRUE;
}
if (data.instr.pop.reg_list & 16) {
if (multiple)
cout << ", ";
cout << "r4";
multiple = TRUE;
}
if (data.instr.pop.reg_list & 32) {
if (multiple)
cout << ", ";
cout << "r5";
multiple = TRUE;
}
if (data.instr.pop.reg_list & 64) {
if (multiple)
cout << ", ";
cout << "r6";
multiple = TRUE;
}
if (data.instr.pop.reg_list & 128) {
if (multiple)
cout << ", ";
cout << "r7";
multiple = TRUE;
}
if (data.instr.pop.m) {
if (multiple)
cout << ", ";
cout << "pc";
}
cout << "}" << endl;
}
return MISC_POP;
}
else if (data.instr.sub.op == MISC_SUB_OP) {
// complete
if (opts.instrs) {
cout << "sub sp, #";
cout << setbase(10) << (static_cast<unsigned int>(data.instr.sub.imm)<< 2) << endl;
}
return MISC_SUB;
}
else if (data.instr.add.op == MISC_ADD_OP) {
// complete
if (opts.instrs) {
cout << "add sp, #";
cout << setbase(10) << (static_cast<unsigned int>(data.instr.add.imm)<< 2) << endl;
}
return MISC_ADD;
}
else if (data.instr.class_type.type_check == ADD_SP8I_TYPE) {
// complete
if (opts.instrs) {
cout << "add sp, #" << setbase(10) << (data.instr.add.imm<<2)<< endl;
}
return MISC_ADD;
}
cout << "[ERROR] Unknown Instruction to be decoded" << endl;
exit(1);
}
int decode (const COND_Type data) {
// complete
if (opts.instrs) {
cout << "b";
printCond(data.instr.b.cond);
cout << " 0x" << hex << rf[15] + 2*(int)((char)(data.instr.b.imm))+2 << endl;
}
return COND;
}
int decode (const UNCOND_Type data) {
// complete
if (opts.instrs) {
cout << "b 0x" << hex << rf[15] + 2*(int)((char)(data.instr.b.imm))+2 << endl;
}
return UNCOND;
}
BL_Ops decode (const BL_Type data) {
// complete
if (opts.instrs) {
cout << "bl 0x" << hex << data.instr.bl_upper.imm10 << endl;
}
return BL_UPPER;
}
int decode (const LDM_Type data) {
// 315: add code to print ldm
if (opts.instrs) {
bool multiple = FALSE;
cout << "ldm r" << data.instr.ldm.rn << "!, {";
if (data.instr.ldm.reg_list & 1) {
cout << "r0";
multiple = TRUE;
}
if (data.instr.ldm.reg_list & 2) {
if (multiple)
cout << ", ";
cout << "r1";
multiple = TRUE;
}
if (data.instr.ldm.reg_list & 4) {
if (multiple)
cout << ", ";
cout << "r2";
multiple = TRUE;
}
if (data.instr.ldm.reg_list & 8) {
if (multiple)
cout << ", ";
cout << "r3";
multiple = TRUE;
}
if (data.instr.ldm.reg_list & 16) {
if (multiple)
cout << ", ";
cout << "r4";
multiple = TRUE;
}
if (data.instr.ldm.reg_list & 32) {
if (multiple)
cout << ", ";
cout << "r5";
multiple = TRUE;
}
if (data.instr.ldm.reg_list & 64) {
if (multiple)
cout << ", ";
cout << "r6";
multiple = TRUE;
}
if (data.instr.ldm.reg_list & 128) {
if (multiple)
cout << ", ";
cout << "r7";
multiple = TRUE;
}
cout << "}" << endl;
}
return LDM;
}
int decode (const STM_Type data) {
// 315: add code to print ldm
if (opts.instrs) {
bool multiple = FALSE;
cout << "stm r" << data.instr.stm.rn << "!, {";
if (data.instr.stm.reg_list & 1) {
cout << "r0";
multiple = TRUE;
}
if (data.instr.stm.reg_list & 2) {
if (multiple)
cout << ", ";
cout << "r1";
multiple = TRUE;
}
if (data.instr.stm.reg_list & 4) {
if (multiple)
cout << ", ";
cout << "r2";
multiple = TRUE;
}
if (data.instr.stm.reg_list & 8) {
if (multiple)
cout << ", ";
cout << "r3";
multiple = TRUE;
}
if (data.instr.stm.reg_list & 16) {
if (multiple)
cout << ", ";
cout << "r4";
multiple = TRUE;
}
if (data.instr.stm.reg_list & 32) {
if (multiple)
cout << ", ";
cout << "r5";
multiple = TRUE;
}
if (data.instr.stm.reg_list & 64) {
if (multiple)
cout << ", ";
cout << "r6";
multiple = TRUE;
}
if (data.instr.stm.reg_list & 128) {
if (multiple)
cout << ", ";
cout << "r7";
multiple = TRUE;
}
cout << "}" << endl;
}
return STM;
}
int decode (const LDRL_Type data) {
// 315: add code to print ldr
if (opts.instrs) {
cout << "ldr r" << data.instr.ldrl.rt << ", [pc, #" << setbase(10) << data.instr.ldrl.imm*4 << "]" << endl;
}
return LDRL;
}
int decode (const ADD_SP_Type data) {
// complete
if (opts.instrs) {
cout << "add r" << data.instr.add.rd << ", sp, #" << setbase(10) << data.instr.add.imm*4 << endl;
}
return ADD_SP;
}