-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFifo_tb.vhd
324 lines (252 loc) · 10.8 KB
/
Fifo_tb.vhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
-- This file is part of VHDL-FIFO.
--
-- VHDL-FIFO is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Lesser General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- VHDL-FIFO is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU Lesser General Public License
-- along with VHDL-FIFO. If not, see <http://www.gnu.org/licenses/>.
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
use std.textio.all; -- Imports the standard textio package.
-- A testbench has no ports.
entity Fifo_tb is
end Fifo_tb;
architecture sim of Fifo_tb is
component Fifo is
generic (
DEPTH : integer := 32
);
port (
CLK : in std_logic;
RST : in std_logic;
W_DATA : in std_logic_vector;
W_EN : in std_logic;
R_DATA : out std_logic_vector;
R_EN : in std_logic;
HAS_DATA : out std_logic;
IS_FULL : out std_logic
);
end component;
signal RST, W_EN, R_EN, HAS_DATA, IS_FULL : std_logic;
signal W_DATA, R_DATA : std_logic_vector(31 downto 0);
constant Clk_period : time := 10 ns;
signal CLK : std_logic := '1';
signal runTest : std_logic := '1';
-- This function encapsulates the assertion checking code for standard logic vectors
function slvAssert (
expected : std_logic_vector;
actual : std_logic_vector;
testName : String)
return BOOLEAN is
variable myLine : line;
-- We have to choose an arbitariliy high value for the size of the String (really annoying)
variable errorMessage : String(1 to 4096);
begin
write (myLine, String'("Expecting: "));
hwrite (myLine, expected);
write (myLine, String'(", Got: "));
hwrite (myLine, actual);
write (myLine, testName);
assert myLine'length < errorMessage'length; -- make sure S is big enough
if myLine'length > 0 then
read(myLine, errorMessage(1 to myLine'length));
end if;
assert actual = expected report errorMessage severity error;
return (actual = expected);
end slvAssert;
begin
-- Instatiate the FIFO with a depth of 4
FifoInst : Fifo
generic map (DEPTH => 4)
port map (
CLK => CLK,
RST => RST,
W_DATA => W_DATA,
W_EN => W_EN,
R_DATA => R_DATA,
R_EN => R_EN,
HAS_DATA => HAS_DATA,
IS_FULL => IS_FULL);
-- Create the clock
clockProc : process
begin
if runTest = '1' then
wait for Clk_period/2;
CLK <= not CLK;
else
wait;
end if;
end process;
-- Provide Stimulus and assertions
testProc : process
variable myLine : line;
variable expectedValue : std_logic_vector(R_DATA'range) := x"00000000";
-- We have to create an arbitraily large number for a string (super annoying)
variable errorMessage : String(1 to 4096);
variable result : Boolean;
begin
-- Initialize important signals
RST <= '0';
W_EN <= '0';
R_EN <= '0';
wait until rising_edge(CLK);
-- Reset the FIFO
RST <= '1';
wait until rising_edge(CLK);
RST <= '0';
wait until rising_edge(CLK);
-- Test out a basic use case, fill up the FIFO then read all the
-- Values out, we expect to write only 4 values in and read those
-- same values out
assert HAS_DATA = '0' report "Has Data Check 1 failed" severity error;
assert IS_FULL = '0' report "Is FULL Check 1 failed" severity error;
W_DATA <= x"00000001";
W_EN <= '1';
wait until rising_edge(CLK);
W_EN <= '0';
wait until rising_edge(CLK);
assert HAS_DATA = '1' report "Has Data Check 2 failed" severity error;
assert IS_FULL = '0' report "Is FULL Check 2 failed" severity error;
expectedValue := x"00000000";
result := slvAssert(expectedValue, R_DATA, String'(" R_DATA check 1 failed"));
R_EN <= '1';
wait until rising_edge(CLK);
R_EN <= '0';
wait until rising_edge(CLK);
assert HAS_DATA = '0' report "Has Data Check 3 failed" severity error;
assert IS_FULL = '0' report "Is FULL Check 3 failed" severity error;
expectedValue := x"00000001";
result := slvAssert(expectedValue, R_DATA, String'(" R_DATA check 2 failed"));
wait until rising_edge(CLK);
W_DATA <= x"00000002";
W_EN <= '1';
wait until rising_edge(CLK);
W_DATA <= x"00000003";
W_EN <= '1';
wait until rising_edge(CLK);
W_DATA <= x"00000004";
W_EN <= '1';
wait until rising_edge(CLK);
W_DATA <= x"00000005";
W_EN <= '1';
wait until rising_edge(CLK);
W_EN <= '0';
wait until rising_edge(CLK);
assert HAS_DATA = '1' report "Has Data Check 4 failed" severity error;
assert IS_FULL = '1' report "Is FULL Check 4 failed" severity error;
W_DATA <= x"00000006";
W_EN <= '1';
wait until rising_edge(CLK);
W_EN <= '0';
wait until rising_edge(CLK);
assert HAS_DATA = '1' report "Has Data Check 5 failed" severity error;
assert IS_FULL = '1' report "Is FULL Check 5 failed" severity error;
wait until rising_edge(CLK);
R_EN <= '1';
wait until rising_edge(CLK);
wait until falling_edge(CLK);
assert HAS_DATA = '1' report "Has Data Check 6 failed" severity error;
assert IS_FULL = '0' report "Is FULL Check 6 failed" severity error;
expectedValue := x"00000002";
result := slvAssert(expectedValue, R_DATA, String'(" R_DATA check 3 failed"));
wait until falling_edge(CLK);
assert HAS_DATA = '1' report "Has Data Check 7 failed" severity error;
assert IS_FULL = '0' report "Is FULL Check 7 failed" severity error;
expectedValue := x"00000003";
result := slvAssert(expectedValue, R_DATA, String'(" R_DATA check 4 failed"));
wait until falling_edge(CLK);
assert HAS_DATA = '1' report "Has Data Check 8 failed" severity error;
assert IS_FULL = '0' report "Is FULL Check 8 failed" severity error;
expectedValue := x"00000004";
result := slvAssert(expectedValue, R_DATA, String'(" R_DATA check 5 failed"));
wait until rising_edge(CLK);
assert HAS_DATA = '1' report "Has Data Check 9 failed" severity error;
wait until falling_edge(CLK);
assert IS_FULL = '0' report "Is FULL Check 9 failed" severity error;
expectedValue := x"00000005";
result := slvAssert(expectedValue, R_DATA, String'(" R_DATA check 6 failed"));
wait until rising_edge(CLK);
R_EN <= '0';
wait until rising_edge(CLK);
assert HAS_DATA = '0' report "Has Data Check 10 failed" severity error;
assert IS_FULL = '0' report "Is FULL Check 10 failed" severity error;
-- A really wild edge case will be tested below
-- The FIFO will be completly filled up and written to when it is
-- also read from. We expect the FIFO will store a value and write out
-- a value when it is also full
wait until rising_edge(CLK);
W_DATA <= x"00000007";
W_EN <= '1';
wait until rising_edge(CLK);
W_DATA <= x"00000008";
W_EN <= '1';
wait until rising_edge(CLK);
assert HAS_DATA = '1' report "Has Data Check 11 failed" severity error;
assert IS_FULL = '0' report "Is FULL Check 11 failed" severity error;
W_DATA <= x"00000009";
W_EN <= '1';
wait until rising_edge(CLK);
W_DATA <= x"0000000A";
W_EN <= '1';
wait until rising_edge(CLK);
-- Now that the FIFO is full we stop writing values in
W_DATA <= x"00000000";
W_EN <= '0';
wait until rising_edge(CLK);
assert HAS_DATA = '1' report "Has Data Check 12 failed" severity error;
assert IS_FULL = '1' report "Is FULL Check 12 failed" severity error;
-- Now put in a value and expect to read out a
W_EN <= '1';
R_EN <= '1';
W_DATA <= x"0000000B";
wait until rising_edge(CLK);
W_EN <= '0';
R_EN <= '0';
W_DATA <= x"00000000";
wait until rising_edge(CLK);
-- Now that we have read one value off we expect to read 4 more off
assert HAS_DATA = '1' report "Has Data Check 13 failed" severity error;
assert IS_FULL = '1' report "Is FULL Check 13 failed" severity error;
expectedValue := x"00000007";
result := slvAssert(expectedValue, R_DATA, String'(" R_DATA check 7 failed"));
wait until rising_edge(CLK);
R_EN <= '1';
wait until rising_edge(CLK);
wait until falling_edge(CLK);
assert HAS_DATA = '1' report "Has Data Check 14 failed" severity error;
assert IS_FULL = '0' report "Is FULL Check 14 failed" severity error;
expectedValue := x"00000008";
result := slvAssert(expectedValue, R_DATA, String'(" R_DATA check 8 failed"));
wait until rising_edge(CLK);
wait until falling_edge(CLK);
assert HAS_DATA = '1' report "Has Data Check 15 failed" severity error;
assert IS_FULL = '0' report "Is FULL Check 15 failed" severity error;
expectedValue := x"00000009";
result := slvAssert(expectedValue, R_DATA, String'(" R_DATA check 9 failed"));
wait until rising_edge(CLK);
wait until falling_edge(CLK);
assert HAS_DATA = '1' report "Has Data Check 16 failed" severity error;
assert IS_FULL = '0' report "Is FULL Check 16 failed" severity error;
expectedValue := x"0000000A";
result := slvAssert(expectedValue, R_DATA, String'(" R_DATA check 10 failed"));
wait until rising_edge(CLK);
R_EN <= '0';
wait until falling_edge(CLK);
assert HAS_DATA = '0' report "Has Data Check 14 failed" severity error;
assert IS_FULL = '0' report "Is FULL Check 14 failed" severity error;
expectedValue := x"0000000B";
result := slvAssert(expectedValue, R_DATA, String'(" R_DATA check 11 failed"));
-- End the test
assert false report "end of test" severity note;
runTest <= '0';
-- Wait forever, this will finish the simulation
wait;
end process;
end sim;