1# Capstone Python bindings, by Nicolas PLANEL <nplanel@gmail.com>
2
3import ctypes
4from . import copy_ctypes_list
5from .m68k_const import *
6
7# define the API
8class M68KOpMem(ctypes.Structure):
9    _fields_ = (
10        ('base_reg', ctypes.c_uint),
11        ('index_reg', ctypes.c_uint),
12        ('in_base_reg', ctypes.c_uint),
13        ('in_disp', ctypes.c_uint),
14        ('out_disp', ctypes.c_uint),
15        ('disp', ctypes.c_short),
16        ('scale', ctypes.c_ubyte),
17        ('bitfield', ctypes.c_ubyte),
18        ('width', ctypes.c_ubyte),
19        ('offset', ctypes.c_ubyte),
20        ('index_size', ctypes.c_ubyte),
21    )
22
23class M68KOpRegPair(ctypes.Structure):
24    _fields_ = (
25        ('reg_0', ctypes.c_uint),
26        ('reg_1', ctypes.c_uint),
27    )
28
29class M68KOpValue(ctypes.Union):
30    _fields_ = (
31        ('imm', ctypes.c_int64),
32        ('dimm', ctypes.c_double),
33        ('simm', ctypes.c_float),
34        ('reg', ctypes.c_uint),
35        ('reg_pair', M68KOpRegPair),
36    )
37
38class M68KOpBrDisp(ctypes.Structure):
39    _fields_ = (
40        ('disp', ctypes.c_int),
41        ('disp_size', ctypes.c_ubyte),
42    )
43
44class M68KOp(ctypes.Structure):
45    _fields_ = (
46        ('value', M68KOpValue),
47        ('mem', M68KOpMem),
48        ('br_disp', M68KOpBrDisp),
49        ('register_bits', ctypes.c_uint),
50        ('type', ctypes.c_uint),
51        ('address_mode', ctypes.c_uint),
52    )
53
54    @property
55    def imm(self):
56        return self.value.imm
57
58    @property
59    def dimm(self):
60        return self.value.dimm
61
62    @property
63    def simm(self):
64        return self.value.simm
65
66    @property
67    def reg(self):
68        return self.value.reg
69
70    @property
71    def mem(self):
72        return self.mem
73
74    @property
75    def register_bits(self):
76        return self.register_bits
77
78class M68KOpSize(ctypes.Structure):
79    _fields_ = (
80        ('type', ctypes.c_uint),
81        ('size', ctypes.c_uint),
82    )
83
84    def get(a):
85        return copy_ctypes_list(type, size)
86
87class CsM68K(ctypes.Structure):
88    M68K_OPERAND_COUNT = 4
89    _fields_ = (
90        ('operands', M68KOp * M68K_OPERAND_COUNT),
91        ('op_size', M68KOpSize),
92        ('op_count', ctypes.c_uint8),
93    )
94
95def get_arch_info(a):
96    return (copy_ctypes_list(a.operands[:a.op_count]), a.op_size)
97