-
Notifications
You must be signed in to change notification settings - Fork 8
/
inst_c16x.c
612 lines (524 loc) · 15.8 KB
/
inst_c16x.c
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
/*
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#include "inst_c16x.h"
/* C16x disassembler
*
* Work-In-Progress
*
* Note: Not all opcodes and modes yet supported!!!
*/
char *lookup16_regname(unsigned short regnum)
{
int i=0;
unsigned short hex_w;
char *name;
while(1) {
name = SFR_Entries[i].name;
hex_w = SFR_Entries[i].hex_w;
if(hex_w == regnum) { break; } // found name
if(name == 0) { break; } // found end of table
i++;
}
return(name);
}
char *lookup_regname(unsigned char regnum)
{
int i=0;
unsigned char hex_b;
char *name;
while(1) {
name = SFR8_Entries[i].name;
hex_b = SFR8_Entries[i].hex_b;
if(hex_b == regnum) { break; } // found name
if(name == 0) { break; } // found end of table
i++;
}
return(name);
}
char *lookup_rh_regname(unsigned char regnum)
{
int i=0,hex;
char *name;
while(1) {
name = RHNames[i].name;
hex = RHNames[i].hex;
if(hex == regnum) { break; } // found name
if(name == 0) { break; } // found end of table
i++;
}
return(name);
}
void translate_arg_type(unsigned char *rom_start, uint8_t *buf, int rval, int rdata16, INST *p, unsigned char type, unsigned char pos)
{
long int val;
int rval_l,num_opcodes;
char *name;
unsigned char nibble;
unsigned char data4;
unsigned char _byte;
unsigned char hi,lo;
int len = p->len;
// process different opcode modes
switch(type)
{
case RW_L: // 1 // Rn
nibble = get_lo_nibble(rval);
printf("r%d",nibble);
break;
case RW: // 1 // Rn
rval = *(buf+1);
if(pos == 2)
{
nibble = get_lo_nibble(rval);
printf("r%d",nibble);
} else if(pos == 1) {
nibble = get_hi_nibble(rval);
printf("r%d",nibble);
}
break;
case RW_IND: // 2 // [Rn]
printf("[r%d]",rval);
break;
case RW_IND_POST_INC: // 3 // [Rn]+
printf("[r%d+]",rval);
break;
case RW_IND_POST_DEC: // 4 // [Rn]-
printf("[r%d-]",rval);
break;
case RW_IND_PRE_INC: // 5 // +[Rn]
printf("[+r%d]",rval);
break;
case RW_IND_PRE_DEC: // 6 // -[Rn]
printf("[-r%d]",rval);
break;
case RW_IND_1_DATA16_2: // 7 // [Rn+Data16]
if(rdata16 < 10) {
printf("[r%d+%d]",rval,rdata16);
} else {
printf("[r%d+%02Xh]",rval,rdata16);
}
break;
case RB: // 8
rval = *(buf+1);
if(pos == 1)
{
// nibble = get_hi_nibble(rval);
// nibble = 0;
// printf("%.2Xh", nibble); // (if not found in list) show as a number
nibble = get_hi_nibble(rval);
// printf(".cRB=[%x]",nibble);
if((name = lookup_rh_regname(nibble)) == 0) { // try to identify number as a named register, DPP0, etc.
printf("^%.2Xh", rval); // (if not found in list) show as a number
} else {
printf("%s",name); // if found as a name
}
}
else if(pos == 2)
{
nibble = get_lo_nibble(rval);
// printf(".cRB=[%x]",nibble);
if((name = lookup_rh_regname(nibble)) == 0) { // try to identify number as a named register, DPP0, etc.
printf("^%.2Xh", rval); // (if not found in list) show as a number
} else {
printf("%s",name); // if found as a name
}
}
// rval = *(buf+1);
// printf(".cRB=%x",rval);
// if((name = lookup_regname(rval)) == 0) { // try to identify number as a named register, DPP0, etc.
// printf("%.2Xh", rval); // (if not found in list) show as a number
// } else
// printf("%s",name); // if found as a name
// }
break;
// cregb(RR) SFR/GPR byte context
case REGB2: // 10 [ SFR/GPR byte context ]
// printf("REGB[%x]", rval);
rval = *(buf+1);
rval = get_lo_nibble(rval);
// if((name = lookup_regname(rval)) == 0) { // try to identify number as a named register, DPP0, etc.
// printf("%.2Xh", rval); // (if not found in list) show as a number
// } else {
printf("r%d", rval); // if found as a name
// }
break;
case REGB3: // 10 [ SFR/GPR byte context ]
rval = *(buf+1);
if((name = lookup_regname(rval)) == 0) { // try to identify number as a named register, DPP0, etc.
printf("r%X", rval); // (if not found in list) show as a number
} else {
printf("%s", name); // if found as a name
}
break;
case REGB: // 10 [ SFR/GPR byte context ]
// printf(".cREGW");
rval = *(buf+1);
_byte = (unsigned char)rval;
if(len == 4) {
// printf(".cREGB[4]");
rval = *(buf+1);
if((name = lookup_regname(rval)) == 0) { // try to identify number as a named register, DPP0, etc.
printf("r%X", rval); // (if not found in list) show as a number
} else {
printf("%s", name); // if found as a name
}
} else if(len == 2) {
// printf(".cREGB[2]");
// nibble = get_hi_nibble(rval);
nibble = _byte;
// printf("<%-2.2X:",nibble);
// printBits(sizeof(nibble), &nibble);
// printf(">");
// nibble = (_byte & 0b00001111);
nibble = (_byte & 0b11110000) >> 4;
/// printf("<%-2.2X:",nibble);
/// printBits(sizeof(nibble), &nibble);
/// printf(">");
// 1111 set ?
if( (((_byte & 0b11110000) >> 4) == 1) )
{
// RL or RH?
if( (((_byte & 0b10000000) >> 7) == 0) ) {
nibble = (_byte & 0b01110000) >> 3;
printf("***RL%d", nibble);
} else {
nibble = (_byte & 0b01110000) >> 3;
printf("***RH%d", nibble);
}
break;
} else {
rval = (_byte & 0b01110000) >> 3;
rval_l = get_lo_nibble(_byte);
// printf("rXX"); // ... then show it as a R0-R15
if((name = lookup_rh_regname(rval_l)) == 0) { // try to identify number as a named register, DPP0, etc.
printf("%.2Xh", rval); // (if not found in list) show as a number
} else {
printf("%s",name); // if found as a name
}
break;
}
}
// printf("_%d",nibble); // ... then show it as a R0-R15
break;
case REGW: // 10 [ SFR/GPR byte context ]
// printf(".cREGW");
rval = *(buf+1);
nibble = get_hi_nibble(rval);
if(nibble == 0xf) { // if high nibble is 1111 (0xf)
printf("r%d",get_lo_nibble(rval)); // ... then show it as a R0-R15
} else {
if((name = lookup_regname(rval)) == 0) { // try to identify number as a named register, DPP0, etc.
printf("%.2Xh", rval); // (if not found in list) show as a number
} else {
printf("%s",name); // if found as a name
}
}
break;
case MEM: // 11
if( *(buf+0) == 0xc2 || *(buf+0) == 0xf7)
{
printf("byte_%X",rdata16); //+0x378000);
} else {
if((name = lookup16_regname(rdata16)) == 0) { // try to identify number as a named register, DPP0, etc.
printf("word_%X",rdata16); //+0x378000);
} else {
printf("%s",name); // if found as a name
}
}
// printf("MEM");
break;
case BITADR_W: // 12
rval = *(buf+3);
lo = get_lo_nibble(rval);
rval = *(buf+2);
printf("word_%X.%d",0xFD00+(rval*2), lo );
break;
case BITADR_W2: // 12
rval = *(buf+1);
printf("word_%X.%d",0xFD00+(rval*2), get_hi_nibble(*(buf+3)));
break;
case BITADR: // 12
#if 0
rval = *(buf+1);
nibble = get_hi_nibble(rval);
if(nibble == 0xf) { // if high nibble is 1111 (0xf)
printf("r%d",get_lo_nibble(rval)); // ... then show it as a R0-R15
} else {
if((name = lookup_regname(rval)) == 0) { // try to identify number as a named register, DPP0, etc.
printf("%.2Xh", rval); // (if not found in list) show as a number
} else {
printf("%s",name); // if found as a name
}
}
printf(".");
#endif
//exper
if(p->len == 2)
{
rval = *(buf+0);
hi = get_hi_nibble(rval);
rval = *(buf+1);
if((name = lookup_regname(rval)) == 0) { // try to identify number as a named register, DPP0, etc.
printf("%.2Xh ??????????", rval); // (if not found in list) show as a number
} else {
printf("%s",name); // if found as a name
}
// printf("word_%X.%d [***************]",0xFD00+(rval*2), hi);
} else {
if(pos == 1) {
rval = *(buf+1);
printf("word_%X.%d",0xFD00+(rval*2), get_hi_nibble(*(buf+3)));
} else {
// printf("BITADR");
num_opcodes = (*(buf+2))+2;
printf("loc_%X",rom_start+(num_opcodes << 1));
}
}
break;
case BITOFF: // 13
printf("BITOFF");
break;
case IRANG2: // 14
printf("IRANG2");
break;
case DATA3: // 15
printf("#%d",rval); //DATA3");
break;
case DATA4: // 16
data4 = ((rval & 0b11110000) >> 4);
if(data4 < 10) {
printf("#%x",data4 ); //DATA4");
} else {
printf("#%02Xh",data4 ); //DATA4");
}
break;
case DATA8: // 17
printf("DATA8");
break;
case DATA16: // 18
if(rdata16 < 10) {
printf("#%d",rdata16);
} else {
printf("#%-4.4Xh",rdata16);
}
// printf("DATA16");
break;
case MASK8: // 19
printf("MASK8");
break;
case ADR: // 20
rval = get16(buf+2);
printf("loc_%X",rval);
break;
case SEG: // 21
rval = *(buf+1);
printf("%Xh",rval);
// printf("SEG");
break;
case REG: // 22
printf("REG");
break;
case TRAP7: // 23
printf("TRAP7");
break;
case CC: // 24
val = strtol(p->fmt,&p->fmt+4,2);
printf("cc_%s",CCNames[val]);
break;
case REL: // 25
{
int num_opcodes, skip_bytes;
num_opcodes = (*(buf+1))+1;
skip_bytes = num_opcodes << 1; // jump offset is measured in words so x2
printf("loc_%X",rom_start+skip_bytes);
}
break;
default:
printf("????");
break;
}
}
void c167x_diss(unsigned char *rom_start, uint8_t *buf, int len)
{
unsigned char chA, chB, chC, chD, rval, nibble, _byte, hi, lo, pos;
int i=0,skip,ln,argc;
int rdata16;
int type, numbits, argnum, bitcount;
INST *ci;
INST *link;
char s_bin[8*8];
char s_tmp[8*8];
unsigned short val;
// len = len*10;
i=0;
while(1)
{
if(len > 4) {
chA = *(buf+0); chB = *(buf+1); chC = *(buf+2); chD = *(buf+3);
ci=&inst_set[chA];
// get length of opcode from lookup table
skip = ci->len;
printf("0x%-8.8X: (+%-3d) ",(int)rom_start+i, (int)i);
// show hex dump of the opcode
if(skip == 2) { // opcodes can either be 2 bytes
printf(" %02X %02X \t\t", chA, chB);
} else if(skip == 4) { // or 4 bytes long...
printf(" %02X %02X %02X %02X \t\t", chA, chB, chC, chD);
}
// show offsets and opcode name
link = ci->link;
if(link == 0)
{
printf(" %-8s", (char *)ci->name);
//
// lets process arguments for this opcode
//
if(ci->argcount > 0) // shows args for at least 1 argument
{
#if 0
for(argc=0;argc < ci->argcount; argc++ )
{
// argnum = find_argnum_by_offset(ci, argc, ci->argcount);
type = ci->args.list[argc].type;
pos = ci->args.list[argc].pos;
numbits = ci->bits.ops[argc];
printf("\n\t\t\t{ listpos+%d: bits=%2d , type=%2d, order=%2d }, ", argc, numbits, type, pos );
}
#endif
rdata16 = get16((buf+2));
rval = *(buf+1);
hi = get_hi_nibble(rval);
lo = get_lo_nibble(rval);
pos = ci->args.list[0].pos;
type = ci->args.list[0].type;
if(pos == 1) {
rval = get_hi_nibble(*(buf+1));
} else if(pos == 2){
rval = get_lo_nibble(*(buf+1));
}
translate_arg_type(rom_start+i, buf, rval, rdata16, ci, type, pos);
if(ci->argcount > 1)
{
printf(", ");
pos = ci->args.list[1].pos;
type = ci->args.list[1].type;
if(pos == 1) {
rval = *(buf+1);
} else if(pos == 2){
rval = get_lo_nibble(*(buf+1));
}
translate_arg_type(rom_start+i, buf, rval, rdata16, ci, type, pos);
}
}
} else {
switch(chA) {
case 0x08:case 0x09:
case 0x18:case 0x19:
case 0x28:case 0x29:
case 0x38:case 0x39:
case 0x48:case 0x49:
case 0x58:case 0x59:
case 0x68:case 0x69:
case 0x78:case 0x79:
{
char *name;
char regname[16];
unsigned char type;
//determine which instruction we are dealing with... 1 of 3
//AAAA0BBB
//AAAA10BB
//AAAA11BB
_byte = *(buf+1);
nibble = (_byte & 0b00001000) >> 3;
hi = get_hi_nibble(_byte);
lo = get_lo_nibble(_byte);
if(link[0].args.list[0].type == RB) {
type = RB; //printf("ARG1 is RB!!!");
if((name = lookup_rh_regname(hi)) == 0) { // try to identify number as a named register, DPP0, etc.
sprintf(regname,"%.2Xh", rval); // (if not found in list) show as a number
} else {
sprintf(regname,"%s",name); // if found as a name
}
}
else if(link[0].args.list[0].type == RW) {
type = RW; //]printf("ARG1 is RW!!!");
sprintf(regname,"r%d",hi); // if found as a name
}
if(nibble == 0)
{
printf(" %s%s, #%d",link[0].name, regname, lo);
}
else
{
nibble = (_byte & 0b00000100) >> 4;
if(nibble == 0) {
printf(" %s%s, #%d ; AAAA10BB {INCOMPLETE}",link[1].name, regname, lo);
} else {
printf(" %s%s, #%d ; AAAA11BB {INCOMPLETE}",link[2].name, regname, lo);
}
}
}
break;
case 0xd7: // opcode: 'extX'
{
int opcode;
_byte = *(buf+1);
opcode = (_byte & 0b11000000) >> 6; // instruction offset : 1st 2 bits
printf(" %s", link[opcode].name);
switch(opcode)
{
case 0b00: // 00AA0000BBBBBBBB00000000 : exts
case 0b10: // 10AA0000BBBBBBBB00000000 : extsr
printf("#%-2.2Xh", *(buf+2)); // segment : 8 bits
printf(", ");
nibble = (_byte & 0b00110000) >> 4; // irang : 2 bits
printf("#%d", nibble+1);
break;
case 0b01: // 01AA0000BBBBBBBBBBBBBBBB : extp
case 0b11: // 11AA0000BBBBBBBBBBBBBBBB : extpr
printf("#%-4.4Xh", get16(buf+2)); // segment : 16 bits
printf(", ");
nibble = (_byte & 0b00110000) >> 4; // irang : 2 bits
printf("#%d", nibble+1);
break;
}
}
break;
default:
printf(" %-8s", (char *)ci->name);
printf("Link for [%#2x]",chA);
break;
}
}
printf("\n");
// support for line feed after specific opcodes, rets, calls, jmps, etc.
if(ci->addLF > 0) {
printf("\n");
}
// to next opcode in buffer
buf += skip;
i += skip;
len -= skip;
} else {
break;
}
}
printf("***\n");
}