OpenCores
URL https://opencores.org/ocsvn/s80186/s80186/trunk

Subversion Repositories s80186

[/] [s80186/] [trunk/] [tests/] [instructions/] [TestIncDec.cpp] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jamieiles
// Copyright Jamie Iles, 2017
2
//
3
// This file is part of s80x86.
4
//
5
// s80x86 is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
//
10
// s80x86 is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with s80x86.  If not, see <http://www.gnu.org/licenses/>.
17
 
18
#include <stdint.h>
19
#include <vector>
20
#include <gtest/gtest.h>
21
 
22
#include "EmulateFixture.h"
23
#include "Flags.h"
24
#include "Arithmetic.h"
25
 
26
template <typename T>
27
struct IncDecTest {
28
    T v;
29
    T expected;
30
    uint16_t expected_flags;
31
    bool carry_set;
32
};
33
 
34
using IncDec8Params = std::pair<const std::vector<uint8_t>,
35
                                const std::vector<struct IncDecTest<uint8_t>>>;
36
using IncDec16Params =
37
    std::pair<const std::vector<uint8_t>,
38
              const std::vector<struct IncDecTest<uint16_t>>>;
39
 
40
static std::vector<IncDecTest<uint8_t>> inc8_tests = {
41
    {0, 1, 0, false},
42
    {0, 1, CF, true},
43
    {0xff, 0, PF | ZF | AF, false},
44
    {0xff, 0, PF | ZF | AF | CF, true},
45
};
46
 
47
static std::vector<IncDecTest<uint16_t>> inc16_tests = {
48
    {0, 1, 0, false},
49
    {0, 1, CF, true},
50
    {0x00ff, 0x0100, CF | AF | PF, true},
51
    {0xffff, 0, PF | ZF | AF, false},
52
    {0xffff, 0, PF | ZF | AF | CF, true},
53
};
54
 
55
static std::vector<IncDecTest<uint8_t>> dec8_tests = {
56
    {1, 0, ZF | PF, false},
57
    {1, 0, ZF | PF | CF, true},
58
    {0, 0xff, PF | AF | SF, false},
59
    {0, 0xff, PF | AF | CF | SF, true},
60
};
61
 
62
static std::vector<IncDecTest<uint16_t>> dec16_tests = {
63
    {1, 0, ZF | PF, false},
64
    {1, 0, ZF | PF | CF, true},
65
    {0x0100, 0x00ff, CF | AF | PF, true},
66
    {0, 0xffff, PF | AF | SF, false},
67
    {0, 0xffff, PF | AF | CF | SF, true},
68
};
69
 
70
class IncReg8Test : public EmulateFixture,
71
                    public ::testing::WithParamInterface<IncDec8Params>
72
{
73
};
74
TEST_P(IncReg8Test, ResultAndFlags)
75
{
76
    auto params = GetParam();
77
    for (auto &t : params.second) {
78
        reset();
79
 
80
        write_flags(0);
81
        if (t.carry_set)
82
            write_flags(CF);
83
        write_reg(AL, t.v);
84
        set_instruction(params.first);
85
 
86
        emulate();
87
 
88
        ASSERT_EQ(read_reg(AL), t.expected);
89
        ASSERT_PRED_FORMAT2(AssertFlagsEqual, read_flags(),
90
                            FLAGS_STUCK_BITS | t.expected_flags);
91
    }
92
};
93
INSTANTIATE_TEST_CASE_P(Inc,
94
                        IncReg8Test,
95
                        ::testing::Values(
96
                            // inc al
97
                            IncDec8Params({0xfe, 0xc0}, inc8_tests)));
98
INSTANTIATE_TEST_CASE_P(Dec,
99
                        IncReg8Test,
100
                        ::testing::Values(
101
                            // dec al
102
                            IncDec8Params({0xfe, 0xc8}, dec8_tests)));
103
 
104
class IncMem8Test : public EmulateFixture,
105
                    public ::testing::WithParamInterface<IncDec8Params>
106
{
107
};
108
TEST_P(IncMem8Test, ResultAndFlags)
109
{
110
    auto params = GetParam();
111
    for (auto &t : params.second) {
112
        reset();
113
 
114
        write_flags(0);
115
        if (t.carry_set)
116
            write_flags(CF);
117
        write_reg(BX, 0x0100);
118
        write_mem8(0x0100, t.v);
119
        set_instruction(params.first);
120
 
121
        emulate();
122
 
123
        ASSERT_EQ(read_mem8(0x0100), t.expected);
124
        ASSERT_PRED_FORMAT2(AssertFlagsEqual, read_flags(),
125
                            FLAGS_STUCK_BITS | t.expected_flags);
126
    }
127
};
128
INSTANTIATE_TEST_CASE_P(Inc,
129
                        IncMem8Test,
130
                        ::testing::Values(
131
                            // inc byte [bx]
132
                            IncDec8Params({0xfe, 0x07}, inc8_tests)));
133
INSTANTIATE_TEST_CASE_P(Dec,
134
                        IncMem8Test,
135
                        ::testing::Values(
136
                            // dec byte [bx]
137
                            IncDec8Params({0xfe, 0x0f}, dec8_tests)));
138
 
139
TEST_F(EmulateFixture, IncFEInvalidReg)
140
{
141
    set_instruction({0xfe, 0xcc});
142
 
143
    emulate();
144
 
145
    ASSERT_EQ(0x0, read_reg(AL));
146
 
147
    ASSERT_FALSE(instruction_had_side_effects());
148
}
149
 
150
class IncReg16Test : public EmulateFixture,
151
                     public ::testing::WithParamInterface<IncDec16Params>
152
{
153
};
154
TEST_P(IncReg16Test, ResultAndFlags)
155
{
156
    auto params = GetParam();
157
    for (auto &t : params.second) {
158
        reset();
159
 
160
        write_flags(0);
161
        if (t.carry_set)
162
            write_flags(CF);
163
        write_reg(AX, t.v);
164
        set_instruction(params.first);
165
 
166
        emulate();
167
 
168
        ASSERT_EQ(read_reg(AX), t.expected);
169
        ASSERT_PRED_FORMAT2(AssertFlagsEqual, read_flags(),
170
                            FLAGS_STUCK_BITS | t.expected_flags);
171
    }
172
};
173
INSTANTIATE_TEST_CASE_P(Inc,
174
                        IncReg16Test,
175
                        ::testing::Values(
176
                            // inc ax
177
                            IncDec16Params({0xff, 0xc0}, inc16_tests)));
178
INSTANTIATE_TEST_CASE_P(Dec,
179
                        IncReg16Test,
180
                        ::testing::Values(
181
                            // dec ax
182
                            IncDec16Params({0xff, 0xc8}, dec16_tests)));
183
 
184
class IncMem16Test : public EmulateFixture,
185
                     public ::testing::WithParamInterface<IncDec16Params>
186
{
187
};
188
TEST_P(IncMem16Test, ResultAndFlags)
189
{
190
    auto params = GetParam();
191
    for (auto &t : params.second) {
192
        reset();
193
 
194
        write_flags(0);
195
        if (t.carry_set)
196
            write_flags(CF);
197
        write_reg(BX, 0x0100);
198
        write_mem16(0x0100, t.v);
199
        set_instruction(params.first);
200
 
201
        emulate();
202
 
203
        ASSERT_EQ(read_mem16(0x0100), t.expected);
204
        ASSERT_PRED_FORMAT2(AssertFlagsEqual, read_flags(),
205
                            FLAGS_STUCK_BITS | t.expected_flags);
206
    }
207
};
208
INSTANTIATE_TEST_CASE_P(Inc,
209
                        IncMem16Test,
210
                        ::testing::Values(
211
                            // inc word [bx]
212
                            IncDec16Params({0xff, 0x07}, inc16_tests)));
213
INSTANTIATE_TEST_CASE_P(Dec,
214
                        IncMem16Test,
215
                        ::testing::Values(
216
                            // dec word [bx]
217
                            IncDec16Params({0xff, 0x0f}, dec16_tests)));
218
 
219
TEST_F(EmulateFixture, IncReg)
220
{
221
    // inc REG
222
    for (uint8_t i = 0; i < 8; ++i) {
223
        reset();
224
 
225
        auto reg = static_cast<GPR>(static_cast<int>(AX) + i);
226
        write_reg(reg, 0x00ff);
227
 
228
        set_instruction({static_cast<uint8_t>(0x40 + i)});
229
        emulate();
230
 
231
        ASSERT_EQ(0x0100, read_reg(reg));
232
    }
233
}
234
 
235
TEST_F(EmulateFixture, DecReg)
236
{
237
    // dec REG
238
    for (uint8_t i = 0; i < 8; ++i) {
239
        reset();
240
 
241
        auto reg = static_cast<GPR>(static_cast<int>(AX) + i);
242
        write_reg(reg, 0x0100);
243
 
244
        set_instruction({static_cast<uint8_t>(0x48 + i)});
245
        emulate();
246
 
247
        ASSERT_EQ(0x00ff, read_reg(reg));
248
    }
249
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.