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

Subversion Repositories s80186

[/] [s80186/] [trunk/] [tests/] [instructions/] [TestNeg.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 <sstream>
19
#include <vector>
20
#include <gtest/gtest.h>
21
 
22
#include "EmulateFixture.h"
23
#include "Flags.h"
24
#include "Arithmetic.h"
25
 
26
// neg of numbers, 0 clears carry flag, others set it
27
// set AF CF OF PF SF ZF
28
// byte of -128 or word of -32768 causes no change but sets OF
29
 
30
template <typename T>
31
struct NegTest {
32
    T val;
33
    T expected;
34
    uint16_t expected_flags;
35
    bool carry_set;
36
};
37
 
38
using Neg8Params =
39
    std::pair<const std::vector<uint8_t>, const struct NegTest<uint8_t>>;
40
using Neg16Params =
41
    std::pair<const std::vector<uint8_t>, const struct NegTest<uint16_t>>;
42
 
43
static const std::vector<struct NegTest<uint8_t>> neg8_tests = {
44
    {0, 0, ZF | PF, false}, {0, 0, ZF | PF, true},
45
        {64, 0xc0, SF | CF | PF, false}, {0x80, 0x80, CF | SF | OF, false},
46
};
47
 
48
static const std::vector<struct NegTest<uint16_t>> neg16_tests = {
49
    {0, 0, ZF | PF, false}, {0, 0, ZF | PF, true},
50
        {256, 0xff00, SF | CF | PF, false},
51
};
52
 
53
TEST_F(EmulateFixture, Neg8Reg)
54
{
55
    // neg al
56
    for (auto &t : neg8_tests) {
57
        reset();
58
 
59
        SCOPED_TRACE("neg " + std::to_string(static_cast<int>(t.val)) +
60
                     " cf=" + std::to_string(static_cast<int>(!!t.carry_set)));
61
        write_reg(AL, t.val);
62
        write_flags(t.carry_set ? CF : 0);
63
        set_instruction({0xf6, 0xd8});
64
 
65
        emulate();
66
 
67
        ASSERT_PRED_FORMAT2(AssertFlagsEqual, read_flags(),
68
                            FLAGS_STUCK_BITS | t.expected_flags);
69
        ASSERT_EQ(read_reg(AL), t.expected);
70
    }
71
}
72
 
73
TEST_F(EmulateFixture, Neg8Mem)
74
{
75
    // neg byte [bx]
76
    for (auto &t : neg8_tests) {
77
        reset();
78
 
79
        SCOPED_TRACE("neg " + std::to_string(static_cast<int>(t.val)) +
80
                     " cf=" + std::to_string(static_cast<int>(!!t.carry_set)));
81
        write_reg(BX, 0x0100);
82
        write_mem8(0x0100, t.val);
83
        write_flags(t.carry_set ? CF : 0);
84
        set_instruction({0xf6, 0x1f});
85
 
86
        emulate();
87
 
88
        ASSERT_PRED_FORMAT2(AssertFlagsEqual, read_flags(),
89
                            FLAGS_STUCK_BITS | t.expected_flags);
90
        ASSERT_EQ(read_mem8(0x0100), t.expected);
91
    }
92
}
93
 
94
TEST_F(EmulateFixture, Neg16Reg)
95
{
96
    // neg ax
97
    for (auto &t : neg16_tests) {
98
        reset();
99
 
100
        SCOPED_TRACE("neg " + std::to_string(static_cast<int>(t.val)) +
101
                     " cf=" + std::to_string(static_cast<int>(!!t.carry_set)));
102
        write_reg(AX, t.val);
103
        write_flags(t.carry_set ? CF : 0);
104
        set_instruction({0xf7, 0xd8});
105
 
106
        emulate();
107
 
108
        ASSERT_PRED_FORMAT2(AssertFlagsEqual, read_flags(),
109
                            FLAGS_STUCK_BITS | t.expected_flags);
110
        ASSERT_EQ(read_reg(AX), t.expected);
111
    }
112
}
113
 
114
TEST_F(EmulateFixture, Neg16Mem)
115
{
116
    // neg byte [bx]
117
    for (auto &t : neg16_tests) {
118
        reset();
119
 
120
        SCOPED_TRACE("neg " + std::to_string(static_cast<int>(t.val)) +
121
                     " cf=" + std::to_string(static_cast<int>(!!t.carry_set)));
122
        write_reg(BX, 0x0100);
123
        write_mem16(0x0100, t.val);
124
        write_flags(t.carry_set ? CF : 0);
125
        set_instruction({0xf7, 0x1f});
126
 
127
        emulate();
128
 
129
        ASSERT_PRED_FORMAT2(AssertFlagsEqual, read_flags(),
130
                            FLAGS_STUCK_BITS | t.expected_flags);
131
        ASSERT_EQ(read_mem16(0x0100), t.expected);
132
    }
133
}

powered by: WebSVN 2.1.0

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