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

Subversion Repositories s80186

[/] [s80186/] [trunk/] [tests/] [rtl/] [TestRegisterFile.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 <VRegisterFile.h>
20
 
21
#include "VerilogTestbench.h"
22
#include "RTLCPU.h"
23
 
24
class RegisterFileTestFixture : public VerilogTestbench<VRegisterFile>,
25
                                public ::testing::Test
26
{
27
public:
28
    void write8(uint8_t reg, uint8_t v);
29
    void write16(uint8_t reg, uint16_t v);
30
    void trigger_read8(int port, uint8_t reg);
31
    void trigger_read16(int port, uint8_t reg);
32
    uint16_t get_value(int port);
33
};
34
 
35
void RegisterFileTestFixture::write8(uint8_t reg, uint8_t v)
36
{
37
    after_n_cycles(0, [&] {
38
        this->dut.wr_sel = reg;
39
        this->dut.wr_val = v;
40
        this->dut.wr_en = 1;
41
        this->dut.is_8_bit = 1;
42
        after_n_cycles(1, [&] { dut.wr_en = 0; });
43
    });
44
    cycle();
45
}
46
 
47
void RegisterFileTestFixture::write16(uint8_t reg, uint16_t v)
48
{
49
    after_n_cycles(0, [&] {
50
        this->dut.wr_sel = reg;
51
        this->dut.wr_val = v;
52
        this->dut.wr_en = 1;
53
        this->dut.is_8_bit = 0;
54
        after_n_cycles(1, [&] { dut.wr_en = 0; });
55
    });
56
    cycle();
57
}
58
 
59
void RegisterFileTestFixture::trigger_read8(int port, uint8_t reg)
60
{
61
    after_n_cycles(0, [&] {
62
        dut.rd_sel[port] = reg;
63
        dut.is_8_bit = 1;
64
    });
65
    cycle();
66
}
67
 
68
void RegisterFileTestFixture::trigger_read16(int port, uint8_t reg)
69
{
70
    after_n_cycles(0, [&] {
71
        dut.rd_sel[port] = reg;
72
        dut.is_8_bit = 0;
73
    });
74
    cycle();
75
}
76
 
77
uint16_t RegisterFileTestFixture::get_value(int port)
78
{
79
    cycle();
80
    return dut.rd_val[port];
81
}
82
 
83
TEST_F(RegisterFileTestFixture, read_write_8_bit)
84
{
85
    for (auto r = 0; r < 8; ++r)
86
        write8(r, r);
87
 
88
    for (auto r = 0; r < 8; ++r) {
89
        trigger_read8(0, r);
90
        EXPECT_EQ(r, get_value(0));
91
    }
92
}
93
 
94
TEST_F(RegisterFileTestFixture, read_write_16_bit)
95
{
96
    for (auto r = 0; r < 8; ++r)
97
        write16(r, r | ((r << 8) + 1));
98
 
99
    for (auto r = 0; r < 8; ++r) {
100
        trigger_read16(0, r);
101
        EXPECT_EQ(r | ((r << 8) + 1), get_value(0));
102
    }
103
}
104
 
105
TEST_F(RegisterFileTestFixture, read_during_write)
106
{
107
    write16(0, 0x1234);
108
    trigger_read16(0, 0);
109
    ASSERT_EQ(0x1234, get_value(0));
110
}
111
 
112
TEST_F(RegisterFileTestFixture, high_low_16)
113
{
114
    for (auto r = 0; r < 8; ++r)
115
        write16(r, r | (~r << 8));
116
 
117
    for (auto r = 0; r < 4; ++r) {
118
        trigger_read8(0, r);
119
        EXPECT_EQ(r, get_value(0));
120
        trigger_read8(0, r + 4);
121
        EXPECT_EQ(~r & 0xff, get_value(0));
122
    }
123
}
124
 
125
TEST_F(RegisterFileTestFixture, multiple_reads)
126
{
127
    write16(0, 0xdead);
128
    write16(1, 0xbeef);
129
 
130
    dut.rd_sel[0] = 1;
131
    dut.rd_sel[1] = 0;
132
    cycle();
133
 
134
    EXPECT_EQ(0xbeef, get_value(0));
135
    EXPECT_EQ(0xdead, get_value(1));
136
}
137
 
138
class CoreFixture : public RTLCPU<verilator_debug_enabled>,
139
                    public ::testing::Test
140
{
141
public:
142
    CoreFixture() : RTLCPU(current_test_name())
143
    {
144
    }
145
};
146
TEST_F(CoreFixture, RegisterFileReset)
147
{
148
    for (auto r = static_cast<int>(AX); r <= static_cast<int>(DI); ++r) {
149
        write_reg(static_cast<GPR>(r), ~r);
150
        EXPECT_EQ(read_reg(static_cast<GPR>(r)), static_cast<uint16_t>(~r));
151
    }
152
 
153
    reset();
154
 
155
    for (auto r = static_cast<int>(AX); r <= static_cast<int>(DI); ++r)
156
        EXPECT_EQ(read_reg(static_cast<GPR>(r)), 0);
157
}

powered by: WebSVN 2.1.0

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