Update Java samples to match C samples.
Also add all of the samples as Java tests, referencing the output of the C samples.
This commit is contained in:
@@ -28,7 +28,7 @@ package samples;
|
||||
|
||||
import unicorn.*;
|
||||
|
||||
public class Sample_m68k {
|
||||
public class Sample_m68k implements UnicornConst, M68kConst {
|
||||
|
||||
// code to be emulated
|
||||
public static final byte[] M68K_CODE = { 118, -19 }; // movq #-19, %d3
|
||||
@@ -36,41 +36,21 @@ public class Sample_m68k {
|
||||
// memory address where emulation starts
|
||||
public static final int ADDRESS = 0x10000;
|
||||
|
||||
public static final long toInt(byte val[]) {
|
||||
long res = 0;
|
||||
for (int i = 0; i < val.length; i++) {
|
||||
long v = val[i] & 0xff;
|
||||
res = res + (v << (i * 8));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static final byte[] toBytes(long val) {
|
||||
byte[] res = new byte[8];
|
||||
for (int i = 0; i < 8; i++) {
|
||||
res[i] = (byte) (val & 0xff);
|
||||
val >>>= 8;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// callback for tracing basic blocks
|
||||
private static class MyBlockHook implements BlockHook {
|
||||
public void hook(Unicorn u, long address, int size, Object user_data) {
|
||||
System.out.print(String.format(
|
||||
">>> Tracing basic block at 0x%x, block size = 0x%x\n", address,
|
||||
size));
|
||||
}
|
||||
}
|
||||
private static final BlockHook hook_block =
|
||||
(uc, address, size, user_data) -> {
|
||||
System.out.format(
|
||||
">>> Tracing basic block at 0x%x, block size = 0x%x\n",
|
||||
address, size);
|
||||
};
|
||||
|
||||
// callback for tracing instruction
|
||||
private static class MyCodeHook implements CodeHook {
|
||||
public void hook(Unicorn u, long address, int size, Object user_data) {
|
||||
System.out.print(String.format(
|
||||
// callback for tracing instructions
|
||||
private static final CodeHook hook_code =
|
||||
(uc, address, size, user_data) -> {
|
||||
System.out.format(
|
||||
">>> Tracing instruction at 0x%x, instruction size = 0x%x\n",
|
||||
address, size));
|
||||
}
|
||||
}
|
||||
address, size);
|
||||
};
|
||||
|
||||
public static void test_m68k() {
|
||||
long d0 = 0x0000L; // d0 data register
|
||||
@@ -97,42 +77,41 @@ public class Sample_m68k {
|
||||
System.out.print("Emulate M68K code\n");
|
||||
|
||||
// Initialize emulator in M68K mode
|
||||
Unicorn u =
|
||||
new Unicorn(Unicorn.UC_ARCH_M68K, Unicorn.UC_MODE_BIG_ENDIAN);
|
||||
Unicorn u = new Unicorn(UC_ARCH_M68K, UC_MODE_BIG_ENDIAN);
|
||||
|
||||
// map 2MB memory for this emulation
|
||||
u.mem_map(ADDRESS, 2 * 1024 * 1024, Unicorn.UC_PROT_ALL);
|
||||
u.mem_map(ADDRESS, 2 * 1024 * 1024, UC_PROT_ALL);
|
||||
|
||||
// write machine code to be emulated to memory
|
||||
u.mem_write(ADDRESS, M68K_CODE);
|
||||
|
||||
// initialize machine registers
|
||||
u.reg_write(Unicorn.UC_M68K_REG_D0, d0);
|
||||
u.reg_write(Unicorn.UC_M68K_REG_D1, d1);
|
||||
u.reg_write(Unicorn.UC_M68K_REG_D2, d2);
|
||||
u.reg_write(Unicorn.UC_M68K_REG_D3, d3);
|
||||
u.reg_write(Unicorn.UC_M68K_REG_D4, d4);
|
||||
u.reg_write(Unicorn.UC_M68K_REG_D5, d5);
|
||||
u.reg_write(Unicorn.UC_M68K_REG_D6, d6);
|
||||
u.reg_write(Unicorn.UC_M68K_REG_D7, d7);
|
||||
u.reg_write(UC_M68K_REG_D0, d0);
|
||||
u.reg_write(UC_M68K_REG_D1, d1);
|
||||
u.reg_write(UC_M68K_REG_D2, d2);
|
||||
u.reg_write(UC_M68K_REG_D3, d3);
|
||||
u.reg_write(UC_M68K_REG_D4, d4);
|
||||
u.reg_write(UC_M68K_REG_D5, d5);
|
||||
u.reg_write(UC_M68K_REG_D6, d6);
|
||||
u.reg_write(UC_M68K_REG_D7, d7);
|
||||
|
||||
u.reg_write(Unicorn.UC_M68K_REG_A0, a0);
|
||||
u.reg_write(Unicorn.UC_M68K_REG_A1, a1);
|
||||
u.reg_write(Unicorn.UC_M68K_REG_A2, a2);
|
||||
u.reg_write(Unicorn.UC_M68K_REG_A3, a3);
|
||||
u.reg_write(Unicorn.UC_M68K_REG_A4, a4);
|
||||
u.reg_write(Unicorn.UC_M68K_REG_A5, a5);
|
||||
u.reg_write(Unicorn.UC_M68K_REG_A6, a6);
|
||||
u.reg_write(Unicorn.UC_M68K_REG_A7, a7);
|
||||
u.reg_write(UC_M68K_REG_A0, a0);
|
||||
u.reg_write(UC_M68K_REG_A1, a1);
|
||||
u.reg_write(UC_M68K_REG_A2, a2);
|
||||
u.reg_write(UC_M68K_REG_A3, a3);
|
||||
u.reg_write(UC_M68K_REG_A4, a4);
|
||||
u.reg_write(UC_M68K_REG_A5, a5);
|
||||
u.reg_write(UC_M68K_REG_A6, a6);
|
||||
u.reg_write(UC_M68K_REG_A7, a7);
|
||||
|
||||
u.reg_write(Unicorn.UC_M68K_REG_PC, pc);
|
||||
u.reg_write(Unicorn.UC_M68K_REG_SR, sr);
|
||||
u.reg_write(UC_M68K_REG_PC, pc);
|
||||
u.reg_write(UC_M68K_REG_SR, sr);
|
||||
|
||||
// tracing all basic blocks with customized callback
|
||||
u.hook_add(new MyBlockHook(), 1, 0, null);
|
||||
u.hook_add(hook_block, 1, 0, null);
|
||||
|
||||
// tracing all instruction
|
||||
u.hook_add(new MyCodeHook(), 1, 0, null);
|
||||
u.hook_add(hook_code, 1, 0, null);
|
||||
|
||||
// emulate machine code in infinite time (last param = 0), or when
|
||||
// finishing all the code.
|
||||
@@ -141,47 +120,37 @@ public class Sample_m68k {
|
||||
// now print out some registers
|
||||
System.out.print(">>> Emulation done. Below is the CPU context\n");
|
||||
|
||||
d0 = u.reg_read(Unicorn.UC_M68K_REG_D0);
|
||||
d1 = u.reg_read(Unicorn.UC_M68K_REG_D1);
|
||||
d2 = u.reg_read(Unicorn.UC_M68K_REG_D2);
|
||||
d3 = u.reg_read(Unicorn.UC_M68K_REG_D3);
|
||||
d4 = u.reg_read(Unicorn.UC_M68K_REG_D4);
|
||||
d5 = u.reg_read(Unicorn.UC_M68K_REG_D5);
|
||||
d6 = u.reg_read(Unicorn.UC_M68K_REG_D6);
|
||||
d7 = u.reg_read(Unicorn.UC_M68K_REG_D7);
|
||||
d0 = u.reg_read(UC_M68K_REG_D0);
|
||||
d1 = u.reg_read(UC_M68K_REG_D1);
|
||||
d2 = u.reg_read(UC_M68K_REG_D2);
|
||||
d3 = u.reg_read(UC_M68K_REG_D3);
|
||||
d4 = u.reg_read(UC_M68K_REG_D4);
|
||||
d5 = u.reg_read(UC_M68K_REG_D5);
|
||||
d6 = u.reg_read(UC_M68K_REG_D6);
|
||||
d7 = u.reg_read(UC_M68K_REG_D7);
|
||||
|
||||
a0 = u.reg_read(Unicorn.UC_M68K_REG_A0);
|
||||
a1 = u.reg_read(Unicorn.UC_M68K_REG_A1);
|
||||
a2 = u.reg_read(Unicorn.UC_M68K_REG_A2);
|
||||
a3 = u.reg_read(Unicorn.UC_M68K_REG_A3);
|
||||
a4 = u.reg_read(Unicorn.UC_M68K_REG_A4);
|
||||
a5 = u.reg_read(Unicorn.UC_M68K_REG_A5);
|
||||
a6 = u.reg_read(Unicorn.UC_M68K_REG_A6);
|
||||
a7 = u.reg_read(Unicorn.UC_M68K_REG_A7);
|
||||
a0 = u.reg_read(UC_M68K_REG_A0);
|
||||
a1 = u.reg_read(UC_M68K_REG_A1);
|
||||
a2 = u.reg_read(UC_M68K_REG_A2);
|
||||
a3 = u.reg_read(UC_M68K_REG_A3);
|
||||
a4 = u.reg_read(UC_M68K_REG_A4);
|
||||
a5 = u.reg_read(UC_M68K_REG_A5);
|
||||
a6 = u.reg_read(UC_M68K_REG_A6);
|
||||
a7 = u.reg_read(UC_M68K_REG_A7);
|
||||
|
||||
pc = u.reg_read(Unicorn.UC_M68K_REG_PC);
|
||||
sr = u.reg_read(Unicorn.UC_M68K_REG_SR);
|
||||
pc = u.reg_read(UC_M68K_REG_PC);
|
||||
sr = u.reg_read(UC_M68K_REG_SR);
|
||||
|
||||
System.out.print(String.format(">>> A0 = 0x%x\t\t>>> D0 = 0x%x\n",
|
||||
a0, d0));
|
||||
System.out.print(String.format(">>> A1 = 0x%x\t\t>>> D1 = 0x%x\n",
|
||||
a1, d1));
|
||||
System.out.print(String.format(">>> A2 = 0x%x\t\t>>> D2 = 0x%x\n",
|
||||
a2, d2));
|
||||
System.out.print(String.format(">>> A3 = 0x%x\t\t>>> D3 = 0x%x\n",
|
||||
a3, d3));
|
||||
System.out.print(String.format(">>> A4 = 0x%x\t\t>>> D4 = 0x%x\n",
|
||||
a4, d4));
|
||||
System.out.print(String.format(">>> A5 = 0x%x\t\t>>> D5 = 0x%x\n",
|
||||
a5, d5));
|
||||
System.out.print(String.format(">>> A6 = 0x%x\t\t>>> D6 = 0x%x\n",
|
||||
a6, d6));
|
||||
System.out.print(String.format(">>> A7 = 0x%x\t\t>>> D7 = 0x%x\n",
|
||||
a7, d7));
|
||||
System.out.print(String.format(">>> PC = 0x%x\n", pc));
|
||||
System.out.print(String.format(">>> SR = 0x%x\n", sr));
|
||||
|
||||
u.close();
|
||||
System.out.format(">>> A0 = 0x%x\t\t>>> D0 = 0x%x\n", a0, d0);
|
||||
System.out.format(">>> A1 = 0x%x\t\t>>> D1 = 0x%x\n", a1, d1);
|
||||
System.out.format(">>> A2 = 0x%x\t\t>>> D2 = 0x%x\n", a2, d2);
|
||||
System.out.format(">>> A3 = 0x%x\t\t>>> D3 = 0x%x\n", a3, d3);
|
||||
System.out.format(">>> A4 = 0x%x\t\t>>> D4 = 0x%x\n", a4, d4);
|
||||
System.out.format(">>> A5 = 0x%x\t\t>>> D5 = 0x%x\n", a5, d5);
|
||||
System.out.format(">>> A6 = 0x%x\t\t>>> D6 = 0x%x\n", a6, d6);
|
||||
System.out.format(">>> A7 = 0x%x\t\t>>> D7 = 0x%x\n", a7, d7);
|
||||
System.out.format(">>> PC = 0x%x\n", pc);
|
||||
System.out.format(">>> SR = 0x%x\n", sr);
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
|
||||
Reference in New Issue
Block a user