Rizin
unix-like reverse engineering framework and cli tools
TestM680x.java
Go to the documentation of this file.
1 // Capstone Java binding
2 /* M680X Backend by Wolfgang Schwotzer <wolfgang.schwotzer@gmx.net> 2017 */
3 
4 import java.lang.*;
5 import capstone.Capstone;
6 import capstone.M680x;
7 
8 import static capstone.M680x_const.*;
9 
10 public class TestM680x {
11 
12  static final String sAccess[] = {
13  "UNCHANGED", "READ", "WRITE", "READ | WRITE",
14  };
15 
16  static final String M6800_CODE = "010936647f7410009010A410b6100039";
17  static final String M6801_CODE = "04053c3d389310ec10ed1039";
18  static final String M6805_CODE = "047f00172228002e0040425a708e979ca015ad00c31000da1234e57ffe";
19  static final String M6808_CODE = "31220035224510004b005110525e226265123472848586878a8b8c9495a710af109e607f9e6b7f009ed610009ee67f";
20  static final String HD6301_CODE = "6b100071100072101039";
21  static final String M6809_CODE = "0610191a551e0123e931063455a681a7897fffa69d1000a791a69f100011ac99100039A607A627A647A667A60FA610A680A681A682A683A684A685A686A6887FA68880A6897FFFA6898000A68BA68C10A68D1000A691A693A694A695A696A6987FA69880A6997FFFA6998000A69BA69C10A69D1000A69F1000";
22  static final String M6811_CODE = "0203127f100013990800147f02157f011e7f20008fcf18081830183c1867188c1000188f18ce100018ff10001aa37f1aac1aee7f1aef7fcdac7f";
23  static final String CPU12_CODE = "000401000c00800e008000111e100080003b4a1000044b01044f7f80008f1000b752b7b1a667a6fea6f71802e23039e21000180c30391000181118121000181900181e00183e183f00";
24  static final String HD6309_CODE = "0110106210107b101000cd499602d21030231038103b1053105d1130431011372510113812113923113b34118e100011af1011ab1011f68000";
25  static final String HCS08_CODE = "3210009eae9ece7f9ebe10009efe7f3e10009ef37f9610009eff7f82";
26 
27  static byte[] hexString2Byte(String s) {
28  // from http://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java
29  int len = s.length();
30  byte[] data = new byte[len / 2];
31  for (int i = 0; i < len; i += 2) {
32  data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
33  + Character.digit(s.charAt(i+1), 16));
34  }
35  return data;
36  }
37 
38  static public String stringToHexUc(byte[] code) {
39  StringBuilder buf = new StringBuilder(800);
40  for (byte ch: code) {
41  buf.append(String.format(" 0x%02x", ch));
42  }
43  return buf.toString();
44  }
45 
46  static public String stringToHexShortUc(byte[] code) {
47  StringBuilder buf = new StringBuilder(800);
48  for (byte ch: code) {
49  buf.append(String.format("%02x", ch));
50  }
51  return buf.toString();
52  }
53 
54  public static Capstone cs;
55 /*
56  private static String hex(int i) {
57  return Integer.toString(i, 16);
58  }
59 
60  private static String hex(long i) {
61  return Long.toString(i, 16);
62  }
63 */
64  public static void print_ins_detail(Capstone.CsInsn ins) {
65  String bytes = stringToHexShortUc(ins.bytes);
66  System.out.printf("0x%04x:\t%s\t%s\t%s\n", ins.address, bytes, ins.mnemonic, ins.opStr);
67 
68  M680x.OpInfo operands = (M680x.OpInfo) ins.operands;
69 
70  if (operands.op.length != 0) {
71  System.out.printf("\top_count: %d\n", operands.op.length);
72  for (int c = 0; c < operands.op.length; c++) {
74  if (i.type == M680X_OP_REGISTER) {
75  String comment = "";
76  if ((c == 0 && ((operands.flags & M680X_FIRST_OP_IN_MNEM) != 0)) ||
77  (c == 1 && ((operands.flags & M680X_SECOND_OP_IN_MNEM) != 0)))
78  comment = " (in mnemonic)";
79  System.out.printf("\t\toperands[%d].type: REGISTER = %s%s\n", c, ins.regName(i.value.reg), comment);
80  }
81  if (i.type == M680X_OP_CONSTANT)
82  System.out.printf("\t\toperands[%d].type: CONSTANT = %d\n", c, i.value.const_val);
83  if (i.type == M680X_OP_IMMEDIATE)
84  System.out.printf("\t\toperands[%d].type: IMMEDIATE = #%d\n", c, i.value.imm);
85  if (i.type == M680X_OP_DIRECT)
86  System.out.printf("\t\toperands[%d].type: DIRECT = 0x%02x\n", c, i.value.direct_addr);
87  if (i.type == M680X_OP_EXTENDED)
88  System.out.printf("\t\toperands[%d].type: EXTENDED %s = 0x%04x\n", c,
89  i.value.ext.indirect != 0 ? "INDIRECT" : "", i.value.ext.address);
90  if (i.type == M680X_OP_RELATIVE)
91  System.out.printf("\t\toperands[%d].type: RELATIVE = 0x%04x\n", c, i.value.rel.address );
92  if (i.type == M680X_OP_INDEXED) {
93  System.out.printf("\t\toperands[%d].type: INDEXED%s\n", c,
94  (i.value.idx.flags & M680X_IDX_INDIRECT) != 0 ? " INDIRECT" : "");
95  if (i.value.idx.base_reg != M680X_REG_INVALID) {
96  String regName = ins.regName(i.value.idx.base_reg);
97  if (regName != null)
98  System.out.printf("\t\t\tbase register: %s\n", regName);
99  }
100  if (i.value.idx.offset_reg != M680X_REG_INVALID) {
101  String regName = ins.regName(i.value.idx.offset_reg);
102  if (regName != null)
103  System.out.printf("\t\t\toffset register: %s\n", regName);
104  }
105  if ((i.value.idx.offset_bits != 0) &&
106  (i.value.idx.offset_reg == M680X_REG_INVALID) &&
107  (i.value.idx.inc_dec == 0)) {
108  System.out.printf("\t\t\toffset: %d\n", i.value.idx.offset);
109  if (i.value.idx.base_reg == M680X_REG_PC)
110  System.out.printf("\t\t\toffset address: 0x%04x\n", i.value.idx.offset_addr);
111  System.out.printf("\t\t\toffset bits: %d\n", i.value.idx.offset_bits);
112  }
113  if (i.value.idx.inc_dec != 0) {
114  String post_pre =
115  (i.value.idx.flags & M680X_IDX_POST_INC_DEC) != 0 ?
116  "post" : "pre";
117  String inc_dec =
118  i.value.idx.inc_dec > 0 ? "increment" : "decrement";
119 
120  System.out.printf("\t\t\t%s %s: %d\n", post_pre, inc_dec,
121  Math.abs(i.value.idx.inc_dec));
122  }
123  }
124  if (i.size != 0)
125  System.out.printf("\t\t\tsize: %d\n", i.size);
126  if (i.access != Capstone.CS_AC_INVALID)
127  System.out.printf("\t\t\taccess: %s\n", sAccess[i.access]);
128  }
129  }
130 
131  if (ins.regsRead.length > 0) {
132  System.out.printf("\tRegisters read:");
133  for (int c = 0; c < ins.regsRead.length; c++) {
134  System.out.printf(" %s", ins.regName(ins.regsRead[c]));
135  }
136  System.out.printf("\n");
137  }
138 
139  if (ins.regsWrite.length > 0) {
140  System.out.printf("\tRegisters modified:");
141  for (int c = 0; c < ins.regsWrite.length; c++) {
142  System.out.printf(" %s", ins.regName(ins.regsWrite[c]));
143  }
144  System.out.printf("\n");
145  }
146 
147  if (ins.groups.length > 0)
148  System.out.printf("\tgroups_count: %d\n", ins.groups.length);
149  }
150 
151  public static void main(String argv[]) {
152 
153  final TestBasic.platform[] all_tests = {
154  new TestBasic.platform(Capstone.CS_ARCH_M680X,
155  Capstone.CS_MODE_M680X_6301,
156  hexString2Byte(HD6301_CODE), "M680X_HD6301"),
157  new TestBasic.platform(Capstone.CS_ARCH_M680X,
158  Capstone.CS_MODE_M680X_6309,
159  hexString2Byte(HD6309_CODE), "M680X_HD6309"),
160  new TestBasic.platform(Capstone.CS_ARCH_M680X,
161  Capstone.CS_MODE_M680X_6800,
162  hexString2Byte(M6800_CODE), "M680X_M6800"),
163  new TestBasic.platform(Capstone.CS_ARCH_M680X,
164  Capstone.CS_MODE_M680X_6801,
165  hexString2Byte(M6801_CODE), "M680X_M6801"),
166  new TestBasic.platform(Capstone.CS_ARCH_M680X,
167  Capstone.CS_MODE_M680X_6805,
168  hexString2Byte(M6805_CODE), "M680X_M68HC05"),
169  new TestBasic.platform(Capstone.CS_ARCH_M680X,
170  Capstone.CS_MODE_M680X_6808,
171  hexString2Byte(M6808_CODE), "M680X_M68HC08"),
172  new TestBasic.platform(Capstone.CS_ARCH_M680X,
173  Capstone.CS_MODE_M680X_6809,
174  hexString2Byte(M6809_CODE), "M680X_M6809"),
175  new TestBasic.platform(Capstone.CS_ARCH_M680X,
176  Capstone.CS_MODE_M680X_6811,
177  hexString2Byte(M6811_CODE), "M680X_M68HC11"),
178  new TestBasic.platform(Capstone.CS_ARCH_M680X,
179  Capstone.CS_MODE_M680X_CPU12,
180  hexString2Byte(CPU12_CODE), "M680X_CPU12"),
181  new TestBasic.platform(Capstone.CS_ARCH_M680X,
182  Capstone.CS_MODE_M680X_HCS08,
183  hexString2Byte(HCS08_CODE), "M680X_HCS08"),
184  };
185 
186  for (int i=0; i<all_tests.length; i++) {
188  System.out.println(new String(new char[20]).replace("\0", "*"));
189  System.out.println("Platform: " + test.comment);
190  System.out.println("Code: " + stringToHexUc(test.code));
191  System.out.println("Disasm:");
192 
193  cs = new Capstone(test.arch, test.mode);
194  cs.setDetail(Capstone.CS_OPT_ON);
195  Capstone.CsInsn[] all_ins = cs.disasm(test.code, 0x1000);
196 
197  for (int j = 0; j < all_ins.length; j++) {
198  print_ins_detail(all_ins[j]);
199  System.out.println();
200  }
201 
202  // Close when done
203  cs.close();
204  }
205  }
206 
207 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
static ut8 bytes[32]
Definition: asm_arc.c:23
static void print_ins_detail(Capstone.CsInsn ins)
Definition: TestM680x.java:64
static final String M6809_CODE
Definition: TestM680x.java:21
static String stringToHexShortUc(byte[] code)
Definition: TestM680x.java:46
static Capstone cs
Definition: TestM680x.java:54
static final String HCS08_CODE
Definition: TestM680x.java:25
static final String M6811_CODE
Definition: TestM680x.java:22
static final String M6801_CODE
Definition: TestM680x.java:17
static final String HD6301_CODE
Definition: TestM680x.java:20
static byte[] hexString2Byte(String s)
Definition: TestM680x.java:27
static void main(String argv[])
Definition: TestM680x.java:151
static final String CPU12_CODE
Definition: TestM680x.java:23
static final String sAccess[]
Definition: TestM680x.java:12
static final String M6800_CODE
Definition: TestM680x.java:16
static String stringToHexUc(byte[] code)
Definition: TestM680x.java:38
static final String M6808_CODE
Definition: TestM680x.java:19
static final String HD6309_CODE
Definition: TestM680x.java:24
static final String M6805_CODE
Definition: TestM680x.java:18
voidpf void * buf
Definition: ioapi.h:138
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
#define M680X_IDX_INDIRECT
Definition: m680x.h:76
#define M680X_FIRST_OP_IN_MNEM
Definition: m680x.h:159
@ M680X_REG_INVALID
Definition: m680x.h:21
@ M680X_REG_PC
M6800/1/2/3/9, M6301/9.
Definition: m680x.h:46
#define M680X_IDX_POST_INC_DEC
Definition: m680x.h:78
@ M680X_OP_EXTENDED
= Extended addressing operand.
Definition: m680x.h:60
@ M680X_OP_INDEXED
= Indexed addressing operand.
Definition: m680x.h:59
@ M680X_OP_CONSTANT
Used e.g. for a bit index or page number.
Definition: m680x.h:63
@ M680X_OP_IMMEDIATE
= Immediate operand.
Definition: m680x.h:58
@ M680X_OP_REGISTER
= Register operand.
Definition: m680x.h:57
@ M680X_OP_RELATIVE
= Relative addressing operand.
Definition: m680x.h:62
@ M680X_OP_DIRECT
= Direct addressing operand.
Definition: m680x.h:61
#define M680X_SECOND_OP_IN_MNEM
Definition: m680x.h:162
Definition: Arm.java:4
tuple all_tests
Definition: test_arm.py:18
-lz4-versions
static RzSocket * s
Definition: rtr.c:28
#define c(i)
Definition: sha256.c:43
Definition: inftree9.h:24
int replace(char *string, const char *token, const char *fmt,...)
Definition: tms320_dasm.c:325
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4