about summary refs log tree commit diff stats
path: root/test/arch/mep/ir/test_branchjump.py
blob: 828b172f0e9cf8a2761c5e703bda692b7fb18259 (plain) (blame)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Toshiba MeP-c4 - Branch/Jump instructions unit tests
# Guillaume Valadon <guillaume@valadon.net>

from ut_helpers_ir import exec_instruction

from miasm.expression.expression import ExprId, ExprInt


class TestBranchJump(object):

    def test_bra(self):
        """Test BRA execution"""

        # BRA disp12.align2
        exec_instruction("BRA 0x28",
                         [],
                         [(ExprId("PC", 32), ExprInt(0x28, 32))])

        exec_instruction("BRA 0x800",
                         [],
                         [(ExprId("PC", 32), ExprInt(0xFFFFF800, 32))])

        exec_instruction("BRA 0x28",
                         [],
                         [(ExprId("PC", 32), ExprInt(0x1028, 32))], offset=0x1000)

    def test_beqz(self):
        """Test BEQZ execution"""

        # BEQZ Rn,disp8.align2
        exec_instruction("BEQZ R1, 0x10",
                         [(ExprId("R1", 32), ExprInt(0, 32))],
                         [(ExprId("PC", 32), ExprInt(0x20, 32))], offset=0x10)

        exec_instruction("BEQZ R1, 0x10",
                         [(ExprId("R1", 32), ExprInt(1, 32))],
                         [(ExprId("PC", 32), ExprInt(0x2, 32))])

        exec_instruction("BEQZ R1, 0x80",
                         [(ExprId("R1", 32), ExprInt(0, 32))],
                         [(ExprId("PC", 32), ExprInt(0xFFFFFF90, 32))], offset=0x10)

    def test_bnez(self):
        """Test BNEZ execution"""

        # BNEZ Rn,disp8.align2
        exec_instruction("BNEZ R1, 0x10",
                         [(ExprId("R1", 32), ExprInt(0, 32))],
                         [(ExprId("PC", 32), ExprInt(0x2, 32))])

        exec_instruction("BNEZ R1, 0x10",
                         [(ExprId("R1", 32), ExprInt(1, 32))],
                         [(ExprId("PC", 32), ExprInt(0x20, 32))], offset=0x10)

        exec_instruction("BNEZ R1, 0x80",
                         [(ExprId("R1", 32), ExprInt(0, 32))],
                         [(ExprId("PC", 32), ExprInt(0x2, 32))])

    def test_beqi(self):
        """Test BEQI execution"""

        # BEQI Rn,imm4,disp17.align2
        exec_instruction("BEQI R1, 0x8, 0x28",
                         [(ExprId("R1", 32), ExprInt(0, 32))],
                         [(ExprId("PC", 32), ExprInt(0x4, 32))])

        exec_instruction("BEQI R1, 0x1, 0x28",
                         [(ExprId("R1", 32), ExprInt(1, 32))],
                         [(ExprId("PC", 32), ExprInt(0x38, 32))], offset=0x10)

        exec_instruction("BEQI R1, 0x6, 0x10000",
                         [(ExprId("R1", 32), ExprInt(6, 32))],
                         [(ExprId("PC", 32), ExprInt(0xFFFF0010, 32))], offset=0x10)

    def test_bnei(self):
        """Test BNEI execution"""

        # BNEI Rn,imm4,disp17.align2
        exec_instruction("BNEI R1, 0x5, 0x28",
                         [(ExprId("R1", 32), ExprInt(0, 32))],
                         [(ExprId("PC", 32), ExprInt(0x38, 32))], offset=0x10)

        exec_instruction("BNEI R1, 0x7, 0xFF00",
                         [(ExprId("R1", 32), ExprInt(7, 32)),
                          (ExprId("PC", 32), ExprInt(0x1, 32))],
                         [(ExprId("PC", 32), ExprInt(0x4, 32))])

    def test_blti(self):
        """Test BLTI execution"""

        # BLTI Rn,imm4,disp17.align2
        exec_instruction("BLTI R1, 0x5, 0x10000",
                         [(ExprId("R1", 32), ExprInt(0x10, 32))],
                         [(ExprId("PC", 32), ExprInt(0x14, 32))],
                         offset=0x10)

        exec_instruction("BLTI R1, 0x5, 0x10000",
                         [(ExprId("R1", 32), ExprInt(0x1, 32))],
                         [(ExprId("PC", 32), ExprInt(0xFFFF0010, 32))],
                         offset=0x10)

    def test_bgei(self):
        """Test BGEI execution"""

        # BGEI Rn,imm4,disp17.align2
        exec_instruction("BGEI R1, 0x5, 0x10000",
                         [(ExprId("R1", 32), ExprInt(0x10, 32))],
                         [(ExprId("PC", 32), ExprInt(0xFFFF0010, 32))],
                         offset=0x10)

        exec_instruction("BGEI R1, 0x5, 0x10000",
                         [(ExprId("R1", 32), ExprInt(0x01, 32))],
                         [(ExprId("PC", 32), ExprInt(0x14, 32))],
                         offset=0x10)

        exec_instruction("BGEI R1, 0x5, 0x10000",
                         [(ExprId("R1", 32), ExprInt(0x05, 32))],
                         [(ExprId("PC", 32), ExprInt(0xFFFF0010, 32))],
                         offset=0x10)

    def test_beq(self):
        """Test BEQ execution"""

        # BEQ Rn,Rm,disp17.align2
        exec_instruction("BEQ R1, R2, 0x10000",
                         [(ExprId("R1", 32), ExprInt(0x10, 32)),
                          (ExprId("R2", 32), ExprInt(0x10, 32))],
                         [(ExprId("PC", 32), ExprInt(0xFFFF0010, 32))], offset=0x10)

        exec_instruction("BEQ R1, R2, 0x8000",
                         [(ExprId("R1", 32), ExprInt(0x09, 32)),
                          (ExprId("R2", 32), ExprInt(0x10, 32)),
                          (ExprId("PC", 32), ExprInt(0x10, 32))],
                         [(ExprId("PC", 32), ExprInt(0x4, 32))])

    def test_bne(self):
        """Test BNE execution"""

        # BNE Rn,Rm,disp17.align2
        exec_instruction("BNE R1, R2, 0x8000",
                         [(ExprId("R1", 32), ExprInt(0x10, 32)),
                          (ExprId("R2", 32), ExprInt(0x10, 32))],
                         [(ExprId("PC", 32), ExprInt(0x4, 32))])

        exec_instruction("BNE R1, R2, 0x8000",
                         [(ExprId("R1", 32), ExprInt(0x09, 32)),
                          (ExprId("R2", 32), ExprInt(0x10, 32))],
                         [(ExprId("PC", 32), ExprInt(0x8010, 32))], offset=0x10)

        exec_instruction("BNE R1, R2, 0x10000",
                         [(ExprId("R1", 32), ExprInt(0x09, 32)),
                          (ExprId("R2", 32), ExprInt(0x10, 32))],
                         [(ExprId("PC", 32), ExprInt(0xFFFF0010, 32))], offset=0x10)

    def test_bsr(self):
        """Test BSR execution"""

        # BSR disp12.align2
        exec_instruction("BSR 0x800",
                         [(ExprId("PC", 32), ExprInt(2, 32))],
                         [(ExprId("PC", 32), ExprInt(0xFFFFF800, 32)),
                          (ExprId("LP", 32), ExprInt(2, 32))], index=0)

        # BSR disp24.align2
        exec_instruction("BSR 0x101015",
                         [(ExprId("PC", 32), ExprInt(4, 32))],
                         [(ExprId("PC", 32), ExprInt(0x101014, 32)),
                          (ExprId("LP", 32), ExprInt(4, 32))], index=1)

    def test_jmp(self):
        """Test JMP execution"""

        # JMP Rm
        exec_instruction("JMP R1",
                         [(ExprId("R1", 32), ExprInt(0x101015, 32))],
                         [(ExprId("PC", 32), ExprInt(0x101015, 32))])

        # JMP target24.align2
        exec_instruction("JMP 0x2807",
                         [(ExprId("PC", 32), ExprInt(0, 32))],
                         [(ExprId("PC", 32), ExprInt(0x2806, 32))], offset=0x42)
        exec_instruction("JMP 0x2807",
                         [(ExprId("PC", 32), ExprInt(0xB0000000, 32))],
                         [(ExprId("PC", 32), ExprInt(0xB0002806, 32))], offset=0xB0000000)

    def test_jsr(self):
        """Test JSR execution"""

        # JSR Rm
        exec_instruction("JSR R1",
                         [(ExprId("R1", 32), ExprInt(0x2807, 32))],
                         [(ExprId("PC", 32), ExprInt(0x2807, 32)),
                          (ExprId("LP", 32), ExprInt(0x2, 32))])

    def test_ret(self):
        """Test RET execution"""

        # RET
        exec_instruction("RET",
                         [(ExprId("LP", 32), ExprInt(0x28, 32))],
                         [(ExprId("PC", 32), ExprInt(0x28, 32))])