aboutsummaryrefslogtreecommitdiffstats
path: root/fpga/usrp3/lib/fifo/axi_fifo_tb.v
blob: 69529c7af5c8c8d37c6b2730d1525a8294c51d4b (plain)
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
325
326
327
328
//
// Copyright 2015 Ettus Research LLC
// Copyright 2018 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: LGPL-3.0-or-later
//

module axi_fifo_tb();

  /*********************************************
  ** User variables
  *********************************************/
  localparam FIFO_SIZE = 1;
  localparam TEST_VECTOR_SIZE = 10;

  /*********************************************
  ** Clocks & Reset
  *********************************************/
  `define CLOCK_FREQ    200e6
  `define RESET_TIME    100

  reg clk;
  initial clk = 1'b0;
  localparam CLOCK_PERIOD = 1e9/`CLOCK_FREQ;
  always
    #(CLOCK_PERIOD) clk = ~clk;

  reg reset;
  initial begin
    reset = 1'b1;
    #(`RESET_TIME);
    @(posedge clk);
    reset = 1'b0;
  end

  /*********************************************
  ** DUT
  *********************************************/
  reg [31:0] i_tdata;
  reg i_tvalid, o_tready;
  wire i_tready, o_tvalid;
  wire [31:0] o_tdata;
  reg clear;

  axi_fifo #(
    .SIZE(FIFO_SIZE),
    .WIDTH(32))
  dut_axi_fifo (
    .clk(clk), .reset(reset), .clear(clear),
    .i_tdata(i_tdata), .i_tvalid(i_tvalid), .i_tready(i_tready),
    .o_tdata(o_tdata), .o_tvalid(o_tvalid), .o_tready(o_tready),
    .space(), .occupied());

  /*********************************************
  ** Testbench
  *********************************************/
  reg [TEST_VECTOR_SIZE-1:0] i_tvalid_sequence;
  reg [TEST_VECTOR_SIZE-1:0] o_tready_sequence;
  integer i,k,n,i_tready_timeout;
  reg [31:0] o_tdata_check;

  initial begin
    i_tdata = {32{1'b1}};
    i_tvalid = 1'b0;
    o_tready = 1'b0;
    i_tready_timeout = 0;
    clear = 1'b0;
    @(negedge reset);
    #(10*CLOCK_PERIOD)
    @(posedge clk);
    $display("*****************************************************");
    $display("**              Begin Assertion Tests              **");
    $display("*****************************************************");
    $display("Test 1 -- Check filling FIFO");
    // Note, if REG_OUTPUT is enabled, the FIFO has space for 1 extra entry
    for (i = 0; i < 2**FIFO_SIZE; i = i + 1) begin
      if (~i_tready) begin
          $display("Test 1 FAILED!");
          $error("FIFO size should be %d entries, but detected %d!",2**FIFO_SIZE,i);
          $stop;
      end
      i_tvalid = 1'b1;
      i_tdata = i_tdata + 32'd1;
      @(posedge clk);
    end
    i_tvalid = 1'b0;
    @(posedge clk);
    if (i_tready) begin
      $display("Test 1 warning!");
      $warning("i_tready still asserted after filling FIFO with %d entries! Might be due to output registering.",i);
      //$stop;
    end
    $display("Test 1 Passed!");
    //////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////
    $display("Test 2 -- Check emptying FIFO");
    for (i = 0; i < 2**FIFO_SIZE; i = i + 1) begin
      if (~o_tvalid) begin
          $display("Test 2 FAILED!");
          $error("FIFO o_tvalid not asserted! Occured at entry %d",2**FIFO_SIZE-i+1);
          $stop;
      end
      o_tready = 1'b1;
      @(posedge clk);
    end
    o_tready = 1'b0;
    @(posedge clk);
    if (o_tvalid) begin
      $display("Test 1 FAILED!");
      $error("o_tvalid still asserted after emptying FIFO!");
      $stop;
    end
    $display("Test 2 Passed!");
    //////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////
    $display("Test 3 -- Check for o_tready / i_tready dropping unexpectantly");
    clear = 1'b1;
    i_tdata = {32{1'b1}};
    i_tvalid = 1'b0;
    o_tready = 1'b0;
    @(posedge clk);
    clear = 1'b0;
    @(posedge clk);
    for (i = 0; i < 2**FIFO_SIZE-1; i = i + 1) begin
      i_tvalid = 1'b1;
      i_tdata = i_tdata + 32'd1;
      @(posedge clk);
      i_tvalid = 1'b0;
      // Give some time to propogate
      @(posedge clk);
      @(posedge clk);
      @(posedge clk);
      if (~i_tready) begin
          $display("Test 3 FAILED!");
          $error("i_tready deasserted unexpectantly after writing %d entries!",i+1);
          $stop;
      end
      if (~o_tvalid) begin
          $display("Test 3 FAILED!");
          $error("o_tvalid deasserted unexpectantly after writing %d entries!",i+1);
          $stop;
      end
    end
    // Write final entry
    i_tvalid = 1'b1;
    i_tdata = i_tdata + 32'd1;
    @(posedge clk);
    i_tvalid = 1'b0;
    @(posedge clk);
    if (i_tready) begin
        $display("Test 3 warning!");
        $warning("i_tready still asserted after writing %d entries! Might be due to output registering.",i+1);
        //$stop;
    end
    @(posedge clk);
    for (i = 0; i < 2**FIFO_SIZE-1; i = i + 1) begin
      o_tready = 1'b1;
      @(posedge clk);
      o_tready = 1'b0;
      // Give some time to propogate
      @(posedge clk);
      @(posedge clk);
      @(posedge clk);
      if (~i_tready) begin
          $display("Test 3 FAILED!");
          $error("i_tready deasserted unexpectantly after reading %d entries!",i+1);
          $stop;
      end
      if (~o_tvalid) begin
          $display("Test 3 FAILED!");
          $error("o_tvalid deasserted unexpectantly after reading %d entries!",i+1);
          $stop;
      end
    end
    // Read final entry
    o_tready = 1'b1;
    @(posedge clk);
    o_tready = 1'b0;
    @(posedge clk);
    if (o_tvalid) begin
        $display("Test 3 FAILED!");
        $error("o_tvalid still asserted after reading %d entries!",i+1);
        $stop;
    end
    @(posedge clk);
    //////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////
    $display("Test 4 -- Check for bubble states");
    clear = 1'b1;
    i_tdata = {32{1'b1}};
    i_tvalid = 1'b0;
    o_tready = 1'b0;
    @(posedge clk);
    clear = 1'b0;
    @(posedge clk);
    // Fill up half way
    for (i = 0; i < (2**FIFO_SIZE + 1)/2; i = i + 1) begin
      i_tvalid = 1'b1;
      i_tdata = i_tdata + 32'd1;
      @(posedge clk);
    end
    // Start reading
    o_tready = 1'b1;
    i_tdata = i_tdata + 32'd1;
    @(posedge clk);
    // Give a clock cycle for latency, but no bubbles should occur after this
    i_tdata = i_tdata + 32'd1;
    @(posedge clk);
    // Continue to write and read at full rate
    for (i = 0; i < (2**FIFO_SIZE + 1)/2; i = i + 1) begin
      if (~i_tready) begin
          $display("Test 4 FAILED!");
          $error("FIFO bubble state detected when writing & reading at full rate!");
          $stop;
      end
      i_tvalid = 1'b1;
      i_tdata = i_tdata + 32'd1;
      @(posedge clk);
    end
    // Read at full, write at half rate
    for (i = 0; i < (2**FIFO_SIZE + 1)/2; i = i + 1) begin
      if (~i_tready | ~o_tvalid) begin
          $display("Test 4 FAILED!");
          $error("FIFO bubble state detected when write at half rate, reading at full rate!");
          $stop;
      end
      i_tvalid = ~i_tvalid;
      if (i_tvalid) i_tdata = i_tdata + 32'd1;
      @(posedge clk);
    end
    // Read at half rate, write at full rate
    for (i = 0; i < (2**FIFO_SIZE + 1)/2; i = i + 1) begin
      if (~i_tready | ~o_tvalid) begin
          $display("Test 4 FAILED!");
          $error("FIFO bubble state detected when write at half rate, reading at full rate!");
          $stop;
      end
      o_tready = ~o_tready;
      i_tvalid = 1'b1;
      i_tdata = i_tdata + 32'd1;
      @(posedge clk);
    end
    $display("Test 4 Passed!");
    //////////////////////////////////////////////////////////////////////////////
    // Tests combinations of i_tvalid / o_tready sequences.
    // Test space depends on TEST_VECTOR_SIZE.
    // Example: TEST_VECTOR_SIZE = 10 => 1024*1024 number of test sequences,
    //          which is every possible 10 bit sequence of i_tvalid / o_tready.
    $display("Test 5 -- Check combinations of i_tvalid / o_tready");
    clear = 1'b1;
    i_tdata = {32{1'b1}};
    i_tvalid = 1'b0;
    o_tready = 1'b0;
    i_tready_timeout = 0;
    i_tvalid_sequence = {TEST_VECTOR_SIZE{1'd0}};
    o_tready_sequence = {TEST_VECTOR_SIZE{1'd0}};
    @(posedge clk);
    clear = 1'b0;
    @(posedge clk);
    for (i = 0; i < 2**TEST_VECTOR_SIZE; i = i + 1) begin
      i_tvalid_sequence = i_tvalid_sequence + 1;
      for (k = 0; k < 2**TEST_VECTOR_SIZE; k = k + 1) begin
        o_tready_sequence = o_tready_sequence + 1;
        for (n = 0; n < TEST_VECTOR_SIZE; n = n + 1) begin
          if (o_tready_sequence[n]) begin
            o_tready = 1'b1;
          end else begin
            o_tready = 1'b0;
          end
          // Special Case: If i_tready timed out, then i_tvalid is still asserted and we cannot
          //               deassert i_tvalid until we see a corresponding i_tready. This is a basic
          //               AXI stream requirement, so we will continue to assert i_tvalid regardless
          //               of what i_tvalid_sequence would have set i_tvalid for this loop.
          if (i_tvalid_sequence[n] | (i_tready_timeout == TEST_VECTOR_SIZE)) begin
            i_tvalid = 1'b1;
            if (i_tready_timeout < TEST_VECTOR_SIZE) begin
              i_tdata = i_tdata + 32'd1;
            end
            @(posedge clk);
            i_tready_timeout = 0;
            // Wait for i_tready until timeout. Timeouts may occur when o_tready_sequence
            // has o_tready not asserted for several clock cycles.
            while(~i_tready & (i_tready_timeout < TEST_VECTOR_SIZE)) begin
              @(posedge clk)
              i_tready_timeout = i_tready_timeout + 1;
            end
          end else begin
            i_tvalid = 1'b0;
            @(posedge clk);
          end
        end
      end
      // Reset starting conditions for the test sequences
      clear = 1'b1;
      i_tdata = {32{1'b1}};
      i_tvalid = 1'b0;
      o_tready = 1'b0;
      i_tready_timeout = 0;
      @(posedge clk);
      clear = 1'b0;
      @(posedge clk);
    end
    $display("Test 5 Passed!");
    $display("All tests PASSED!");
    $stop;
  end

  // Check the input counting sequence independent of
  // i_tvalid / o_tready sequences.
  always @(posedge clk) begin
    if (reset) begin
      o_tdata_check <= 32'd0;
    end else begin
      if (clear) begin
        o_tdata_check <= 32'd0;
      end
      if (o_tready & o_tvalid) begin
        o_tdata_check <= o_tdata_check + 32'd1;
        if (o_tdata != o_tdata_check) begin
          $display("Test FAILED!");
          $error("Incorrect output!");
          $stop;
        end
      end
    end
  end

endmodule