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

Subversion Repositories s80186

[/] [s80186/] [trunk/] [tests/] [rtl/] [TestImmediateReader.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 <gtest/gtest.h>
19
#include <VImmediateReader.h>
20
#include <deque>
21
#include <memory>
22
 
23
#include "VerilogTestbench.h"
24
 
25
class ImmediateReaderTestbench : public VerilogTestbench<VImmediateReader>,
26
                                 public ::testing::Test
27
{
28
public:
29
    ImmediateReaderTestbench();
30
 
31
    void add_bytes(const std::vector<uint8_t> bytes)
32
    {
33
        stream.insert(stream.end(), bytes.begin(), bytes.end());
34
        this->cycle();
35
    }
36
 
37
    void fetch()
38
    {
39
        after_n_cycles(0, [&] {
40
            this->dut.start = 1;
41
            after_n_cycles(1, [&] { this->dut.start = 0; });
42
        });
43
 
44
        for (auto i = 0; i < 1000; ++i) {
45
            cycle();
46
            if (complete)
47
                return;
48
        }
49
 
50
        FAIL() << "failed to complete immediate fetch" << std::endl;
51
    }
52
 
53
    bool is_complete() const
54
    {
55
        return complete;
56
    }
57
 
58
private:
59
    std::deque<uint8_t> stream;
60
    bool complete;
61
};
62
 
63
ImmediateReaderTestbench::ImmediateReaderTestbench() : complete(false)
64
{
65
    dut.reset = 0;
66
    reset();
67
 
68
    periodic(ClockSetup, [&] {
69
        this->dut.fifo_empty = this->stream.size() == 0;
70
 
71
        if (!this->dut.reset && this->dut.fifo_rd_en &&
72
            this->stream.size() == 0)
73
            FAIL() << "fifo underflow" << std::endl;
74
 
75
    });
76
 
77
    periodic(ClockCapture, [&] {
78
        this->complete = this->dut.complete;
79
        if (!this->dut.reset && this->dut.fifo_rd_en &&
80
            this->stream.size() > 0) {
81
            this->stream.pop_front();
82
        }
83
        after_n_cycles(1, [&] { this->dut.fifo_rd_data = this->stream[0]; });
84
    });
85
}
86
 
87
TEST_F(ImmediateReaderTestbench, Immed8PopsOne)
88
{
89
    add_bytes({0x80});
90
    dut.is_8bit = 1;
91
 
92
    fetch();
93
 
94
    EXPECT_EQ(dut.immediate, 0xff80LU);
95
}
96
 
97
TEST_F(ImmediateReaderTestbench, Immed8SignExtendPositive)
98
{
99
    add_bytes({0x7f});
100
    dut.is_8bit = 1;
101
 
102
    fetch();
103
 
104
    EXPECT_EQ(dut.immediate, 0x007f);
105
}
106
 
107
TEST_F(ImmediateReaderTestbench, Immed16PopsTwo)
108
{
109
    add_bytes({0xaa, 0x55});
110
    dut.is_8bit = 0;
111
 
112
    fetch();
113
 
114
    EXPECT_EQ(dut.immediate, 0x55aaLU);
115
}
116
 
117
TEST_F(ImmediateReaderTestbench, ImmedPersistsAfterCompletion)
118
{
119
    add_bytes({0xaa, 0x55});
120
    dut.is_8bit = 0;
121
 
122
    fetch();
123
    cycle(2);
124
    dut.fifo_rd_data = 0;
125
    cycle(2);
126
 
127
    EXPECT_EQ(dut.immediate, 0x55aaLU);
128
}

powered by: WebSVN 2.1.0

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