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

Subversion Repositories s80186

[/] [s80186/] [trunk/] [tests/] [rtl/] [TestPrefetch.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 <VPrefetch.h>
20
#include <vector>
21
#include <map>
22
 
23
#include "VerilogTestbench.h"
24
 
25
class PrefetchTestFixture : public VerilogTestbench<VPrefetch>,
26
                            public ::testing::Test
27
{
28
public:
29
    PrefetchTestFixture();
30
    std::vector<uint8_t> fifo_bytes;
31
    std::map<uint32_t, uint16_t> memory;
32
    int mem_latency;
33
 
34
private:
35
    bool reading;
36
};
37
 
38
PrefetchTestFixture::PrefetchTestFixture() : mem_latency(0), reading(false)
39
{
40
    reset();
41
 
42
    dut.new_cs = 0;
43
    dut.new_ip = 0;
44
    dut.load_new_ip = 0;
45
    dut.fifo_full = 0;
46
    dut.mem_access = 0;
47
    dut.mem_ack = 0;
48
    dut.mem_data = 0;
49
 
50
    periodic(ClockCapture, [&] {
51
        if (!this->dut.reset && this->dut.fifo_wr_en)
52
            fifo_bytes.push_back(this->dut.fifo_wr_data & 0xff);
53
    });
54
    periodic(ClockSetup, [&] {
55
        if (!this->dut.reset && this->dut.mem_access && !reading) {
56
            reading = true;
57
            if (memory.find(this->dut.mem_address << 1) == memory.end())
58
                FAIL() << "no memory at 0x" << std::hex
59
                       << (this->dut.mem_address << 1);
60
            after_n_cycles(mem_latency, [&] {
61
                this->dut.mem_data = memory[this->dut.mem_address << 1];
62
                this->dut.mem_ack = 1;
63
                after_n_cycles(1, [&] {
64
                    this->dut.mem_ack = 0;
65
                    reading = false;
66
                });
67
            });
68
        }
69
    });
70
}
71
 
72
TEST_F(PrefetchTestFixture, full_fifo_no_fetch)
73
{
74
    dut.fifo_full = 1;
75
    reset();
76
    cycle();
77
    ASSERT_FALSE(dut.mem_access);
78
}
79
 
80
TEST_F(PrefetchTestFixture, empty_fifo_triggers_fetch)
81
{
82
    memory[0] = 0;
83
    dut.fifo_full = 0;
84
    cycle();
85
}
86
 
87
TEST_F(PrefetchTestFixture, fetch_writes_to_fifo)
88
{
89
    memory[0xffff0 + 0] = 0xaa55;
90
    memory[0xffff0 + 2] = 0xdead;
91
    dut.fifo_full = 0;
92
 
93
    cycle(3);
94
 
95
    auto expected = std::vector<uint8_t>{0x55, 0xaa};
96
    ASSERT_EQ(fifo_bytes, expected);
97
}
98
 
99
TEST_F(PrefetchTestFixture, update_ip_resets_fifo)
100
{
101
    memory[0x000] = 0xf00f;
102
    memory[0x100] = 0x1234;
103
    memory[0x102] = 0x0ff0;
104
    dut.new_ip = 0x101;
105
    dut.load_new_ip = 1;
106
    cycle();
107
    dut.load_new_ip = 0;
108
    ASSERT_TRUE(dut.fifo_reset);
109
    ASSERT_EQ(dut.mem_address << 1, 0x100LU);
110
    cycle(2);
111
    ASSERT_EQ(fifo_bytes, std::vector<uint8_t>{0x12});
112
}
113
 
114
TEST_F(PrefetchTestFixture, odd_fetch_address_writes_one)
115
{
116
    memory[0x000] = 0xbeef;
117
    memory[0x100] = 0x1234;
118
    memory[0x102] = 0xdead;
119
    dut.new_ip = 0x101;
120
    dut.load_new_ip = 1;
121
    cycle();
122
    dut.load_new_ip = 0;
123
    cycle(2);
124
 
125
    ASSERT_EQ(fifo_bytes, std::vector<uint8_t>{0x12});
126
}
127
 
128
TEST_F(PrefetchTestFixture, back_to_back_reads)
129
{
130
    memory[0xffff0 + 0x000] = 0xbeef;
131
    memory[0x100] = 0x12aa;
132
    memory[0x102] = 0x5634;
133
    memory[0x104] = 0xffff;
134
    dut.new_ip = 0x101;
135
    dut.load_new_ip = 1;
136
    cycle();
137
    dut.load_new_ip = 0;
138
    // Start fetching from the new address, this should do back to back reads,
139
    // so 6 cycles to write the 3 bytes.
140
    cycle(4);
141
 
142
    ASSERT_EQ(fifo_bytes, (std::vector<uint8_t>{0x12, 0x34, 0x56}));
143
}
144
 
145
TEST_F(PrefetchTestFixture, address_generation)
146
{
147
    memory[0xdead8] = 0xf00d;
148
    dut.new_cs = 0xdead;
149
    dut.new_ip = 0x8;
150
    dut.load_new_ip = 1;
151
    cycle();
152
    ASSERT_EQ(dut.mem_address << 1, 0xdead8LU);
153
}
154
 
155
TEST_F(PrefetchTestFixture, new_ip_discards_current_fetch)
156
{
157
    mem_latency = 5;
158
    memory[0xffff0 + 0x00000] = 0xffff;
159
    memory[0xdead8] = 0xf00d;
160
    memory[0xdeada] = 0xface;
161
    memory[0xdeadc] = 0x5555;
162
    dut.new_cs = 0xdead;
163
    dut.new_ip = 0x8;
164
    cycle(2);
165
    dut.load_new_ip = 1;
166
    cycle();
167
    dut.load_new_ip = 0;
168
    cycle(20);
169
    ASSERT_EQ(fifo_bytes, (std::vector<uint8_t>{0x0d, 0xf0, 0xce, 0xfa}));
170
}
171
 
172
TEST_F(PrefetchTestFixture, filled_fifo_doesnt_skip)
173
{
174
    for (uint32_t m = 0; m < 32; m += 2)
175
        memory[0xffff0 + m] = m;
176
 
177
    while (fifo_bytes.size() != 6)
178
        cycle();
179
    dut.fifo_full = 1;
180
    cycle(128);
181
    dut.fifo_full = 0;
182
    cycle();
183
 
184
    while (fifo_bytes.size() != 8)
185
        cycle();
186
 
187
    ASSERT_EQ(fifo_bytes, (std::vector<uint8_t>{0, 0, 2, 0, 4, 0, 6, 0}));
188
}

powered by: WebSVN 2.1.0

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