about summary refs log tree commit diff stats
path: root/test/arch/mep/ir/test_shift.py
blob: cac486601c728f30129defd74aff707c91d63e6d (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
# Toshiba MeP-c4 - Shift instructions unit tests
# Guillaume Valadon <guillaume@valadon.net>

from ut_helpers_ir import exec_instruction

from miasm.expression.expression import ExprId, ExprInt, ExprCond, ExprOp
from miasm.core.cpu import sign_ext


class TestShift(object):

    def test_sra(self):
        """Test SRA execution"""

        # SRA Rn, Rm
        exec_instruction("SRA R1, R2",
                         [(ExprId("R1", 32), ExprInt(4, 32)), (ExprId("R2", 32), ExprInt(1, 32))],
                         [(ExprId("R1", 32), ExprInt(2, 32))])

        exec_instruction("SRA R1, R2",
                         [(ExprId("R1", 32), ExprInt(sign_ext(4, 3, 32), 32)), (ExprId("R2", 32), ExprInt(1, 32))],
                         [(ExprId("R1", 32), ExprInt(0xFFFFFFFE, 32))])

        exec_instruction("SRA R1, R2",
                         [(ExprId("R1", 32), ExprInt(0xF0000000, 32)), (ExprId("R2", 32), ExprInt(4, 32))],
                         [(ExprId("R1", 32), ExprInt(0xFF000000, 32))])

        # SRA Rn,imm5
        exec_instruction("SRA R1, 1",
                         [(ExprId("R1", 32), ExprInt(4, 32))],
                         [(ExprId("R1", 32), ExprInt(2, 32))])

        # SRA Rn,imm5
        exec_instruction("SRA R1, 1",
                         [(ExprId("R1", 32), ExprInt(0x80000000, 32))],
                         [(ExprId("R1", 32), ExprInt(0xC0000000, 32))])

        exec_instruction("SRA R1, 1",
                         [(ExprId("R1", 32), ExprInt(1, 32))],
                         [(ExprId("R1", 32), ExprInt(0, 32))])

    def test_srl(self):
        """Test SRL execution"""

        # SRL Rn, Rm
        exec_instruction("SRL R1, R2",
                         [(ExprId("R1", 32), ExprInt(4, 32)), (ExprId("R2", 32), ExprInt(1, 32))],
                         [(ExprId("R1", 32), ExprInt(2, 32))])

        # SRL Rn,imm5
        exec_instruction("SRL R1, 1",
                         [(ExprId("R1", 32), ExprInt(4, 32))],
                         [(ExprId("R1", 32), ExprInt(2, 32))])

        exec_instruction("SRL R1, 1",
                         [(ExprId("R1", 32), ExprInt(1, 32))],
                         [(ExprId("R1", 32), ExprInt(0, 32))])

    def test_sll(self):
        """Test SLL execution"""

        # SLL Rn, Rm
        exec_instruction("SLL R1, R2",
                         [(ExprId("R1", 32), ExprInt(4, 32)), (ExprId("R2", 32), ExprInt(1, 32))],
                         [(ExprId("R1", 32), ExprInt(8, 32))])

        exec_instruction("SLL R1, R2",
                         [(ExprId("R1", 32), ExprInt(0x80000000, 32)), (ExprId("R2", 32), ExprInt(1, 32))],
                         [(ExprId("R1", 32), ExprInt(0, 32))])

        # SLL Rn,imm5
        exec_instruction("SLL R1, 1",
                         [(ExprId("R1", 32), ExprInt(4, 32))],
                         [(ExprId("R1", 32), ExprInt(8, 32))])

    def test_sll3(self):
        """Test SLL3 execution"""

        # SLL3 R0,Rn,imm5
        exec_instruction("SLL3 R0, R1, 2",
                         [(ExprId("R1", 32), ExprInt(4, 32))],
                         [(ExprId("R0", 32), ExprInt(16, 32))])

        exec_instruction("SLL3 R0, R1, 2",
                         [(ExprId("R1", 32), ExprInt(0xC0000000, 32))],
                         [(ExprId("R0", 32), ExprInt(0, 32))])

    def test_fsft(self):
        """Test FSFT execution"""

        # FSFT Rn,Rm
        exec_instruction("FSFT R0, R1",
                         [(ExprId("SAR", 32), ExprInt(0x00000001, 32)),
                          (ExprId("R0", 32), ExprInt(0x00000001, 32)),
                          (ExprId("R1", 32), ExprInt(0x80000000, 32))],
                         [(ExprId("R0", 32), ExprInt(0x00000003, 32))])

        exec_instruction("FSFT R0, R1",
                         [(ExprId("SAR", 32), ExprInt(0x00000004, 32)),
                          (ExprId("R0", 32), ExprInt(0xFFFFFFFF, 32)),
                          (ExprId("R1", 32), ExprInt(0xF0000000, 32))],
                         [(ExprId("R0", 32), ExprInt(0xFFFFFFFF, 32))])

        exec_instruction("FSFT R0, R1",
                         [(ExprId("SAR", 32), ExprInt(0x00000004, 32)),
                          (ExprId("R0", 32), ExprInt(0xF0000000, 32)),
                          (ExprId("R1", 32), ExprInt(0x0F000000, 32))],
                         [(ExprId("R0", 32), ExprInt(0, 32))])