Rizin
unix-like reverse engineering framework and cli tools
test_tms320c64x.c
Go to the documentation of this file.
1 /* Capstone Disassembly Engine */
2 /* TMS320C64x Backend by Fotis Loukos <me@fotisl.com> 2016 */
3 
4 #include <stdio.h>
5 
6 #include <capstone/capstone.h>
7 
8 struct platform {
10  cs_mode mode;
11  unsigned char *code;
12  size_t size;
13  const char *comment;
14 };
15 
16 static csh handle;
17 
18 static void print_string_hex(const char *comment, unsigned char *str, size_t len)
19 {
20  unsigned char *c;
21 
22  printf("%s", comment);
23  for (c = str; c < str + len; c++) {
24  printf("0x%02x ", *c & 0xff);
25  }
26 
27  printf("\n");
28 }
29 
30 static void print_insn_detail(cs_insn *ins)
31 {
32  cs_tms320c64x *tms320c64x;
33  int i;
34 
35  // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
36  if (ins->detail == NULL)
37  return;
38 
39  tms320c64x = &(ins->detail->tms320c64x);
40  if (tms320c64x->op_count)
41  printf("\top_count: %u\n", tms320c64x->op_count);
42 
43  for (i = 0; i < tms320c64x->op_count; i++) {
44  cs_tms320c64x_op *op = &(tms320c64x->operands[i]);
45  switch((int)op->type) {
46  default:
47  break;
48  case TMS320C64X_OP_REG:
49  printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
50  break;
51  case TMS320C64X_OP_IMM:
52  printf("\t\toperands[%u].type: IMM = 0x%x\n", i, op->imm);
53  break;
54  case TMS320C64X_OP_MEM:
55  printf("\t\toperands[%u].type: MEM\n", i);
56  if (op->mem.base != TMS320C64X_REG_INVALID)
57  printf("\t\t\toperands[%u].mem.base: REG = %s\n",
58  i, cs_reg_name(handle, op->mem.base));
59  printf("\t\t\toperands[%u].mem.disptype: ", i);
60  if(op->mem.disptype == TMS320C64X_MEM_DISP_INVALID) {
61  printf("Invalid\n");
62  printf("\t\t\toperands[%u].mem.disp: %u\n", i, op->mem.disp);
63  }
64  if(op->mem.disptype == TMS320C64X_MEM_DISP_CONSTANT) {
65  printf("Constant\n");
66  printf("\t\t\toperands[%u].mem.disp: %u\n", i, op->mem.disp);
67  }
68  if(op->mem.disptype == TMS320C64X_MEM_DISP_REGISTER) {
69  printf("Register\n");
70  printf("\t\t\toperands[%u].mem.disp: %s\n", i, cs_reg_name(handle, op->mem.disp));
71  }
72  printf("\t\t\toperands[%u].mem.unit: %u\n", i, op->mem.unit);
73  printf("\t\t\toperands[%u].mem.direction: ", i);
74  if(op->mem.direction == TMS320C64X_MEM_DIR_INVALID)
75  printf("Invalid\n");
76  if(op->mem.direction == TMS320C64X_MEM_DIR_FW)
77  printf("Forward\n");
78  if(op->mem.direction == TMS320C64X_MEM_DIR_BW)
79  printf("Backward\n");
80  printf("\t\t\toperands[%u].mem.modify: ", i);
81  if(op->mem.modify == TMS320C64X_MEM_MOD_INVALID)
82  printf("Invalid\n");
83  if(op->mem.modify == TMS320C64X_MEM_MOD_NO)
84  printf("No\n");
85  if(op->mem.modify == TMS320C64X_MEM_MOD_PRE)
86  printf("Pre\n");
87  if(op->mem.modify == TMS320C64X_MEM_MOD_POST)
88  printf("Post\n");
89  printf("\t\t\toperands[%u].mem.scaled: %u\n", i, op->mem.scaled);
90 
91 
92  break;
94  printf("\t\toperands[%u].type: REGPAIR = %s:%s\n", i, cs_reg_name(handle, op->reg + 1), cs_reg_name(handle, op->reg));
95  break;
96  }
97  }
98 
99  printf("\tFunctional unit: ");
100  switch(tms320c64x->funit.unit) {
101  case TMS320C64X_FUNIT_D:
102  printf("D%u\n", tms320c64x->funit.side);
103  break;
104  case TMS320C64X_FUNIT_L:
105  printf("L%u\n", tms320c64x->funit.side);
106  break;
107  case TMS320C64X_FUNIT_M:
108  printf("M%u\n", tms320c64x->funit.side);
109  break;
110  case TMS320C64X_FUNIT_S:
111  printf("S%u\n", tms320c64x->funit.side);
112  break;
113  case TMS320C64X_FUNIT_NO:
114  printf("No Functional Unit\n");
115  break;
116  default:
117  printf("Unknown (Unit %u, Side %u)\n", tms320c64x->funit.unit, tms320c64x->funit.side);
118  break;
119  }
120  if(tms320c64x->funit.crosspath == 1)
121  printf("\tCrosspath: 1\n");
122 
123  if(tms320c64x->condition.reg != TMS320C64X_REG_INVALID)
124  printf("\tCondition: [%c%s]\n", (tms320c64x->condition.zero == 1) ? '!' : ' ', cs_reg_name(handle, tms320c64x->condition.reg));
125  printf("\tParallel: %s\n", (tms320c64x->parallel == 1) ? "true" : "false");
126 
127  printf("\n");
128 }
129 
130 static void test()
131 {
132 #define TMS320C64X_CODE "\x01\xac\x88\x40\x81\xac\x88\x43\x00\x00\x00\x00\x02\x90\x32\x96\x02\x80\x46\x9e\x05\x3c\x83\xe6\x0b\x0c\x8b\x24"
133 
134  struct platform platforms[] = {
135  {
138  (unsigned char*)TMS320C64X_CODE,
139  sizeof(TMS320C64X_CODE) - 1,
140  "TMS320C64x",
141  },
142  };
143 
144  uint64_t address = 0x1000;
145  cs_insn *insn;
146  int i;
147  size_t count;
148 
149  for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
150  cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
151  if (err) {
152  printf("Failed on cs_open() with error returned: %u\n", err);
153  continue;
154  }
155 
157 
158  count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
159  if (count) {
160  size_t j;
161 
162  printf("****************\n");
163  printf("Platform: %s\n", platforms[i].comment);
165  printf("Disasm:\n");
166 
167  for (j = 0; j < count; j++) {
168  printf("0x%"PRIx64":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
169  print_insn_detail(&insn[j]);
170  }
171  printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size);
172 
173  // free memory allocated by cs_disasm()
174  cs_free(insn, count);
175  } else {
176  printf("****************\n");
177  printf("Platform: %s\n", platforms[i].comment);
179  printf("ERROR: Failed to disasm given code!\n");
180  }
181 
182  printf("\n");
183 
184  cs_close(&handle);
185  }
186 }
187 
188 int main()
189 {
190  test();
191 
192  return 0;
193 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
static bool err
Definition: armass.c:435
cs_arch
Architecture type.
Definition: capstone.h:74
@ CS_ARCH_TMS320C64X
TMS320C64x architecture.
Definition: capstone.h:84
cs_mode
Mode type.
Definition: capstone.h:102
@ CS_MODE_BIG_ENDIAN
big-endian mode
Definition: capstone.h:123
@ CS_OPT_DETAIL
Break down instruction structure into details.
Definition: capstone.h:171
size_t csh
Definition: capstone.h:71
@ CS_OPT_ON
Turn ON an option (CS_OPT_DETAIL, CS_OPT_SKIPDATA).
Definition: capstone.h:183
#define NULL
Definition: cris-opc.c:27
CAPSTONE_EXPORT size_t CAPSTONE_API cs_disasm(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
Definition: cs.c:798
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Definition: cs.c:453
CAPSTONE_EXPORT void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Definition: cs.c:1017
CAPSTONE_EXPORT const char *CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Definition: cs.c:1154
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_close(csh *handle)
Definition: cs.c:501
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Definition: cs.c:646
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
cs_arch arch
Definition: cstool.c:13
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
struct platform platforms[]
Definition: fuzz_diff.c:18
voidpf void uLong size
Definition: ioapi.h:138
const char int mode
Definition: ioapi.h:137
unsigned long uint64_t
Definition: sftypes.h:28
#define c(i)
Definition: sha256.c:43
Definition: inftree9.h:24
cs_tms320c64x_op operands[8]
operands for this instruction.
Definition: tms320c64x.h:66
struct cs_tms320c64x::@364 funit
unsigned int unit
Definition: tms320c64x.h:72
unsigned int parallel
Definition: tms320c64x.h:76
unsigned int crosspath
Definition: tms320c64x.h:74
unsigned int reg
Definition: tms320c64x.h:68
uint8_t op_count
Definition: tms320c64x.h:65
struct cs_tms320c64x::@363 condition
unsigned int zero
Definition: tms320c64x.h:69
unsigned int side
Definition: tms320c64x.h:73
unsigned char * code
#define PRIx64
Definition: sysdefs.h:94
static csh handle
static void print_string_hex(const char *comment, unsigned char *str, size_t len)
static void print_insn_detail(cs_insn *ins)
static void test()
#define TMS320C64X_CODE
int main()
@ TMS320C64X_FUNIT_NO
Definition: tms320c64x.h:351
@ TMS320C64X_FUNIT_L
Definition: tms320c64x.h:348
@ TMS320C64X_FUNIT_D
Definition: tms320c64x.h:347
@ TMS320C64X_FUNIT_S
Definition: tms320c64x.h:350
@ TMS320C64X_FUNIT_M
Definition: tms320c64x.h:349
@ TMS320C64X_MEM_DISP_INVALID
Definition: tms320c64x.h:27
@ TMS320C64X_MEM_DISP_CONSTANT
Definition: tms320c64x.h:28
@ TMS320C64X_MEM_DISP_REGISTER
Definition: tms320c64x.h:29
@ TMS320C64X_REG_INVALID
Definition: tms320c64x.h:80
@ TMS320C64X_MEM_DIR_FW
Definition: tms320c64x.h:34
@ TMS320C64X_MEM_DIR_BW
Definition: tms320c64x.h:35
@ TMS320C64X_MEM_DIR_INVALID
Definition: tms320c64x.h:33
@ TMS320C64X_MEM_MOD_POST
Definition: tms320c64x.h:42
@ TMS320C64X_MEM_MOD_INVALID
Definition: tms320c64x.h:39
@ TMS320C64X_MEM_MOD_NO
Definition: tms320c64x.h:40
@ TMS320C64X_MEM_MOD_PRE
Definition: tms320c64x.h:41
@ TMS320C64X_OP_REG
= CS_OP_REG (Register operand).
Definition: tms320c64x.h:20
@ TMS320C64X_OP_REGPAIR
Register pair for double word ops.
Definition: tms320c64x.h:23
@ TMS320C64X_OP_IMM
= CS_OP_IMM (Immediate operand).
Definition: tms320c64x.h:21
@ TMS320C64X_OP_MEM
= CS_OP_MEM (Memory operand).
Definition: tms320c64x.h:22
Definition: dis.c:32
mnemonic
Definition: z80asm.h:48