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
|
# Toshiba MeP-c4 - Arithmetic instructions unit tests
# Guillaume Valadon <guillaume@valadon.net>
from ut_helpers_ir import exec_instruction
from miasm2.expression.expression import ExprId, ExprInt, ExprCond, ExprOp
class TestArithmetic:
def test_add3(self):
"""Test ADD3 execution"""
# ADD3 Rl,Rn,Rm
exec_instruction("ADD3 R1, R2, R3",
[(ExprId("R2", 32), ExprInt(0x40, 32)), (ExprId("R3", 32), ExprInt(0x2, 32))],
[(ExprId("R1", 32), ExprInt(0x42, 32))])
# ADD3 Rn,SP,imm7.align4
exec_instruction("ADD3 R1, SP, 0x8",
[(ExprId("SP", 32), ExprInt(0x20, 32))],
[(ExprId("R1", 32), ExprInt(0x28, 32))])
# ADD3 Rn,Rm,imm16
exec_instruction("ADD3 R7, R5, -31912",
[(ExprId("R5", 32), ExprInt(0x20, 32))],
[(ExprId("R7", 32), ExprInt(-31880, 32))])
def test_add(self):
"""Test ADD execution"""
# ADD Rn,imm6
exec_instruction("ADD R1, 0x10",
[(ExprId("R1", 32), ExprInt(0x32, 32))],
[(ExprId("R1", 32), ExprInt(0x42, 32))])
exec_instruction("ADD R1, -5",
[(ExprId("R1", 32), ExprInt(0x32, 32))],
[(ExprId("R1", 32), ExprInt(45, 32))])
exec_instruction("ADD R1, -16",
[(ExprId("R1", 32), ExprInt(0xFFFF, 32))],
[(ExprId("R1", 32), ExprInt(0xFFEF, 32))])
exec_instruction("ADD R1, -28",
[(ExprId("R1", 32), ExprInt(0, 32))],
[(ExprId("R1", 32), ExprInt(0xFFFFFFE4, 32))])
def test_advck3(self):
"""Test ADVCK3 execution"""
# ADVCK3 R0,Rn,Rm
exec_instruction("ADVCK3 R0, R1, R2",
[(ExprId("R1", 32), ExprInt(1, 32)),
(ExprId("R2", 32), ExprInt(2, 32))],
[(ExprId("R0", 32), ExprInt(0, 32))])
exec_instruction("ADVCK3 R0, R1, R2",
[(ExprId("R1", 32), ExprInt(1, 32)),
(ExprId("R2", 32), ExprInt(0xFFFFFFFF, 32))],
[(ExprId("R0", 32), ExprInt(1, 32))])
def test_sub(self):
"""Test SUB execution"""
# SUB Rn,Rm
exec_instruction("SUB R1, R2",
[(ExprId("R1", 32), ExprInt(0x28, 32)),
(ExprId("R2", 32), ExprInt(0x7, 32))],
[(ExprId("R1", 32), ExprInt(0x21, 32))])
def test_sbvck3(self):
"""Test SBVCK3 execution"""
# SBVCK3 R0,Rn,Rm
exec_instruction("SBVCK3 R0, R1, R2",
[(ExprId("R1", 32), ExprInt(2, 32)),
(ExprId("R2", 32), ExprInt(1, 32))],
[(ExprId("R0", 32), ExprCond(ExprOp(">",
ExprInt(3, 32),
ExprCond(ExprOp(">", ExprInt(0x2, 32), ExprInt(0x1, 32)),
ExprInt(0x2, 32),
ExprInt(0x1, 32))),
ExprInt(1, 32),
ExprInt(0, 32)))])
exec_instruction("SBVCK3 R0, R1, R2",
[(ExprId("R1", 32), ExprInt(0, 32)),
(ExprId("R2", 32), ExprInt(1, 32))],
[(ExprId("R0", 32), ExprCond(ExprOp(">",
ExprInt(1, 32),
ExprCond(ExprOp(">", ExprInt(0, 32), ExprInt(1, 32)),
ExprInt(0, 32),
ExprInt(1, 32))),
ExprInt(1, 32),
ExprInt(0, 32)))])
def test_neg(self):
"""Test NEG execution"""
# NEG Rn,Rm
exec_instruction("NEG R1, R2",
[(ExprId("R2", 32), ExprInt(1, 32))],
[(ExprId("R1", 32), ExprInt(0xFFFFFFFF, 32))])
exec_instruction("NEG R1, R2",
[(ExprId("R2", 32), ExprInt(0x42, 32))],
[(ExprId("R1", 32), ExprInt(0xFFFFFFBE, 32))])
def test_slt3(self):
"""Test SLT3 execution"""
# SLT3 R0,Rn,Rm
exec_instruction("SLT3 R0, R1, R2",
[(ExprId("R1", 32), ExprInt(0x2, 32)),
(ExprId("R2", 32), ExprInt(0x1, 32))],
[(ExprId("R0", 32), ExprInt(0, 32))])
r1 = 0x80000000
r2 = 0x80000001
exec_instruction("SLT3 R0, R1, R2",
[(ExprId("R1", 32), ExprInt(r1, 32)),
(ExprId("R2", 32), ExprInt(r2, 32))],
[(ExprId("R0", 32), ExprInt(1, 32))])
r1 = 0x80000000
r2 = 0x00000001
exec_instruction("SLT3 R0, R1, R2",
[(ExprId("R1", 32), ExprInt(r1, 32)),
(ExprId("R2", 32), ExprInt(r2, 32))],
[(ExprId("R0", 32), ExprInt(1, 32))])
r1 = 0x00000001
r2 = 0x80000000
exec_instruction("SLT3 R0, R1, R2",
[(ExprId("R1", 32), ExprInt(r1, 32)),
(ExprId("R2", 32), ExprInt(r2, 32))],
[(ExprId("R0", 32), ExprInt(0, 32))])
# SLT3 R0,Rn,imm5
exec_instruction("SLT3 R0, R1, 12",
[(ExprId("R1", 32), ExprInt(0x1, 32))],
[(ExprId("R0", 32), ExprInt(1, 32))])
r1 = 0x80000000
exec_instruction("SLT3 R0, R1, 12",
[(ExprId("R1", 32), ExprInt(0x80000000, 32))],
[(ExprId("R0", 32), ExprInt(1, 32))])
def test_sltu3(self):
"""Test SLTU3 execution"""
# SLTU3 R0,Rn,Rm
exec_instruction("SLTU3 R0, R1, R2",
[(ExprId("R1", 32), ExprInt(0x1, 32)),
(ExprId("R2", 32), ExprInt(0x2, 32))],
[(ExprId("R0", 32), ExprInt(1, 32))])
exec_instruction("SLTU3 R0, R1, R2",
[(ExprId("R1", 32), ExprInt(0x2, 32)),
(ExprId("R2", 32), ExprInt(0x1, 32))],
[(ExprId("R0", 32), ExprInt(0, 32))])
# SLTU3 R0,Rn,imm5
exec_instruction("SLTU3 R0, R1, 12",
[(ExprId("R1", 32), ExprInt(0x1, 32))],
[(ExprId("R0", 32), ExprInt(1, 32))])
def test_sl1ad3(self):
"""Test SL2AD3 execution"""
# SL1AD3 R0,Rn,Rm
exec_instruction("SL1AD3 R0, R1, R2",
[(ExprId("R1", 32), ExprInt(0x2, 32)),
(ExprId("R2", 32), ExprInt(0x20, 32))],
[(ExprId("R0", 32), ExprInt(0x24, 32))])
def test_sl2ad3(self):
"""Test SL2AD3 execution"""
# SL2AD3 R0,Rn,Rm
exec_instruction("SL2AD3 R0, R1, R2",
[(ExprId("R1", 32), ExprInt(0x2, 32)),
(ExprId("R2", 32), ExprInt(0x20, 32))],
[(ExprId("R0", 32), ExprInt(0x28, 32))])
|