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

from ut_helpers_ir import exec_instruction

from miasm2.expression.expression import ExprId, ExprInt, ExprCond, ExprOp
from miasm2.jitter.csts import EXCEPT_DIV_BY_ZERO


class TestDivide:

    def test_div(self):
        """Test DIV execution"""

        # DIV Rn,Rm
        exec_instruction("DIV R0, R1",
                         [(ExprId("R0", 32), ExprInt(0x80, 32)),
                          (ExprId("R1", 32), ExprInt(0x0, 32)),
                          (ExprId("HI", 32), ExprInt(0, 32)),
                          (ExprId("LO", 32), ExprInt(0, 32))],
                         [(ExprId("HI", 32), ExprInt(0, 32)),
                          (ExprId("LO", 32), ExprInt(0, 32)),
                          (ExprId("exception_flags", 32), ExprInt(EXCEPT_DIV_BY_ZERO, 32))])

        # Negative numbers
        exec_instruction("DIV R0, R1",
                         [(ExprId("R0", 32), ExprInt(-4, 32)),
                          (ExprId("R1", 32), ExprInt(-2, 32))],
                         [(ExprId("HI", 32), ExprInt(0, 32)),
                          (ExprId("LO", 32), ExprInt(2, 32))])

        exec_instruction("DIV R0, R1",
                         [(ExprId("R0", 32), ExprInt(-5, 32)),
                          (ExprId("R1", 32), ExprInt(-2, 32))],
                         [(ExprId("HI", 32), ExprInt(1, 32)),
                          (ExprId("LO", 32), ExprInt(2, 32))])

        # Positive numbers
        exec_instruction("DIV R0, R1",
                         [(ExprId("R0", 32), ExprInt(4, 32)),
                          (ExprId("R1", 32), ExprInt(2, 32))],
                         [(ExprId("HI", 32), ExprCond(ExprOp("==",
                                                             ExprInt(0, 32),
                                                             ExprInt(0x80000000, 32)),
                                                      ExprInt(0, 32),
                                                      ExprInt(0xFFFFFFFC, 32))),
                          (ExprId("LO", 32), ExprCond(ExprOp("==",
                                                             ExprInt(0, 32),
                                                             ExprInt(0x80000000, 32)),
                                                       ExprInt(2, 32),
                                                       ExprInt(0, 32)))])

        # Negative & positive numbers
        exec_instruction("DIV R0, R1",
                         [(ExprId("R0", 32), ExprInt(-5, 32)),
                          (ExprId("R1", 32), ExprInt(2, 32))],
                         [(ExprId("HI", 32), ExprCond(ExprOp("==", ExprInt(0, 32), ExprInt(0x80000000, 32)),
                                                      ExprInt(1, 32), ExprInt(0xFFFFFFFF, 32))),
                          (ExprId("LO", 32), ExprCond(ExprOp("==", ExprInt(0, 32), ExprInt(0x80000000, 32)),
                                                      ExprInt(0x7FFFFFFD, 32), ExprInt(0xFFFFFFFE, 32)))])

        exec_instruction("DIV R0, R1",
                         [(ExprId("R0", 32), ExprInt(5, 32)),
                          (ExprId("R1", 32), ExprInt(-2, 32))],
                         [(ExprId("HI", 32), ExprCond(ExprOp("==", ExprInt(0, 32), ExprInt(0x80000000, 32)),
                                                      ExprInt(5, 32), ExprInt(0xFFFFFFFF, 32))),
                          (ExprId("LO", 32), ExprCond(ExprOp("==", ExprInt(0, 32), ExprInt(0x80000000, 32)),
                                                      ExprInt(0, 32), ExprInt(0xFFFFFFFE, 32)))])

    def test_divu(self):
        """Test DIVU execution"""

        # DIVU Rn,Rm
        exec_instruction("DIVU R0, R1",
                         [(ExprId("R0", 32), ExprInt(0x80, 32)),
                          (ExprId("R1", 32), ExprInt(0x0, 32)),
                          (ExprId("HI", 32), ExprInt(0, 32)),
                          (ExprId("LO", 32), ExprInt(0, 32))],
                         [(ExprId("HI", 32), ExprInt(0, 32)),
                          (ExprId("LO", 32), ExprInt(0, 32)),
                          (ExprId("exception_flags", 32), ExprInt(EXCEPT_DIV_BY_ZERO, 32))])

        exec_instruction("DIVU R0, R1",
                         [(ExprId("R0", 32), ExprInt(0x80, 32)),
                          (ExprId("R1", 32), ExprInt(0x2, 32))],
                         [(ExprId("HI", 32), ExprInt(0x0, 32)),
                          (ExprId("LO", 32), ExprInt(0x40, 32))])

        exec_instruction("DIVU R0, R1",
                         [(ExprId("R0", 32), ExprInt(0x83, 32)),
                          (ExprId("R1", 32), ExprInt(0x2, 32))],
                         [(ExprId("HI", 32), ExprInt(0x1, 32)),
                          (ExprId("LO", 32), ExprInt(0x41, 32))])

        exec_instruction("DIVU R0, R1",
                         [(ExprId("R0", 32), ExprInt(0x80000000, 32)),
                          (ExprId("R1", 32), ExprInt(-1, 32))],
                         [(ExprId("HI", 32), ExprInt(0x80000000, 32)),
                          (ExprId("LO", 32), ExprInt(0x0, 32))])