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

Subversion Repositories uart_fifo_cpu_if_sv_testbench

[/] [uart_fifo_cpu_if_sv_testbench/] [trunk/] [bench/] [uart_tb.sv] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 andrewbrid
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  Testbench for uart.vhd                                      ////
4
////                                                              ////
5
////  This file is part of the XXX project                        ////
6
////  http://www.opencores.org/cores/xxx/                         ////
7
////                                                              ////
8
////  Description                                                 ////
9
////  Self checking testbench for uart.vhd. SV class implements a serial UART driver and monitor.
10
////  The driver accepts byte transactions and converts the byte to a serial stream.
11
////  The monitor converts serial UART bitstream to byte transactions.
12
////                                                              ////
13
////  To Do:                                                      ////
14
////   -                                                          ////
15
////                                                              ////
16
////  Author(s):                                                  ////
17
////      - Andrew Bridger, andrew.bridger@gmail.com              ////
18
////                                                              ////
19
//////////////////////////////////////////////////////////////////////
20
////                                                              ////
21
//// Copyright (C) 2001 Authors and OPENCORES.ORG                 ////
22
////                                                              ////
23
//// This source file may be used and distributed without         ////
24
//// restriction provided that this copyright statement is not    ////
25
//// removed from the file and that any derivative work contains  ////
26
//// the original copyright notice and the associated disclaimer. ////
27
////                                                              ////
28
//// This source file is free software; you can redistribute it   ////
29
//// and/or modify it under the terms of the GNU Lesser General   ////
30
//// Public License as published by the Free Software Foundation; ////
31
//// either version 2.1 of the License, or (at your option) any   ////
32
//// later version.                                               ////
33
////                                                              ////
34
//// This source is distributed in the hope that it will be       ////
35
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
36
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
37
//// PURPOSE.  See the GNU Lesser General Public License for more ////
38
//// details.                                                     ////
39
////                                                              ////
40
//// You should have received a copy of the GNU Lesser General    ////
41
//// Public License along with this source; if not, download it   ////
42
//// from http://www.opencores.org/lgpl.shtml                     ////
43
////                                                              ////
44
//////////////////////////////////////////////////////////////////////
45
//
46
// CVS Revision History
47
//
48
// $Log$
49
//
50
 
51
timeunit 1ns;
52
timeprecision 1ps;
53
 
54
import std_defs::*;
55
 
56
interface uart_if ();
57
  logic txd = 0; //from testbench driver
58 4 andrewbrid
  logic rxd;     //to testbench monitor
59 2 andrewbrid
endinterface : uart_if
60
 
61
typedef logic[11:0] cpu_addr_t;
62
typedef logic[7:0]  cpu_data_t;
63
 
64
 
65
 
66
module uart_tb();
67
 
68
`define BAUD_RATE      115200  //baud rate to test.
69
`define UART_FIFO_SIZE 32      //DUT FIFO size.
70
 
71
  //Instantiate the DUT interfaces;
72
  uart_if  uart_();
73
  //Keep Testbench simple for the CPU register interface - use tasks for bus functional model, so don't require SV i/f.
74
  cpu_addr_t cpu_addr;
75
  cpu_data_t cpu_data_in;
76
  cpu_data_t cpu_data_out;
77
  logic cpu_we;
78
  logic cpu_rd;
79
  logic clk, reset;
80
 
81
  //DUT
82
  \work.uart(rtl) vhdl_module (
83
                               .clk           (clk),
84
                               .reset         (reset),
85
                               //Serial UART
86
                               .i_rxd         (uart_.txd),
87
                               .o_txd         (uart_.rxd),
88
                               //Cpu register interface
89
                               .i_addr         (cpu_addr),
90
                               .i_write_enable (cpu_we),
91
                               .i_read_enable  (cpu_rd),
92
                               .i_data         (cpu_data_in),
93
                               .o_data         (cpu_data_out) );
94
 
95
  //Takes byte transactions and generates serial data on TXD. No parity, 8 data bits, 1 stop bit.
96
  class uart_driver;
97
    virtual uart_if  uart_;
98
    mailbox #(uint8) mbx;
99
    bit     verbose = false;
100
    uint16           baud_rate;
101
 
102
    function new( virtual uart_if uart_, uint16 baud_rate, mailbox #(uint8) mbx );
103
      uart_     = uart_;
104
      baud_rate = baud_rate;
105
      mbx       = mbx;
106
    endfunction
107
 
108
    //Main driver thread. Empty the mailbox by looking for transactions, and driving them out onto TXD.
109
    task automatic run();
110
      //spawn processes required for this driver.
111
      fork begin
112
        uint8      data;
113
        logic[7:0] data_bits;
114
 
115
        forever begin
116
          mbx.get( data ); //block if no transactions waiting.
117
          data_bits = data;
118
          //start bit
119
          uart_.txd = 0;
120 4 andrewbrid
          #(1s/baud_rate);
121 2 andrewbrid
          //8 bits of data
122
          for(uint8 i=0; i<8; i++) begin
123 3 andrewbrid
            uart_.txd = data_bits[i]; //least significant bit first.
124 4 andrewbrid
            #(1s/baud_rate);
125 2 andrewbrid
          end
126
          //1 stop bit
127
          uart_.txd = 1;
128 4 andrewbrid
          #(1s/baud_rate);
129 2 andrewbrid
        end
130
      end
131
      join_none
132
    endtask
133
  endclass
134
 
135
  //Looks for serial bytes on RXD, and converts them into byte transactions into a mailbox.
136
  class uart_monitor;
137
    virtual uart_if  uart_;
138
    mailbox #(uint8) mbx;
139
    bit     verbose = false;
140
    uint16           baud_rate;
141
 
142
    function new( virtual uart_if uart_, uint16 baud_rate, mailbox #(uint8) mbx );
143
      uart_     = uart_;
144
      baud_rate = baud_rate;
145
      mbx       = mbx;
146
    endfunction
147
 
148
    //Main monitor thread.
149
    task automatic run();
150
      fork begin
151
        forever begin
152
          //Look for a valid start bit. Must be at least 1/2 bit period duration.
153
          @(negedge uart_.rxd);
154 4 andrewbrid
          #(0.5 * 1s/baud_rate);
155 2 andrewbrid
          if ( uart_.rxd == 0 ) begin
156
            logic[7:0] data_bits;
157 3 andrewbrid
            //read in 8 data bits, LSBit first, sampling in the center of the bit period.
158
            for(uint8 i=0; i<8; i++) begin
159 4 andrewbrid
              #(1s/baud_rate);
160 3 andrewbrid
              data_bits[i] = uart_.rxd;
161 2 andrewbrid
            end
162
            //check stop bit.
163 4 andrewbrid
            #(1s/baud_rate);
164 2 andrewbrid
            if ( uart_.rxd != 1 ) begin
165
              $display("Monitor: Invalid stop bit.");
166
            end
167
            else begin
168
              //valid stop bit so generate transaction.
169
              uint8 data;
170
              data = data_bits;
171
              mbx.put( data );
172
            end
173
          end
174
        end
175
      end
176
      join_none
177
    endtask
178
  endclass
179
 
180
  task automatic cpu_init();
181
    cpu_addr    = 0;
182
    cpu_data_in = 0;
183
    cpu_we      = 0;
184
    cpu_rd      = 0;
185
  endtask
186
 
187
  //Read a register.
188
  task automatic cpu_read( cpu_addr_t addr, cpu_data_t data );
189
    @(negedge clk); //setup on falling edge. DUT reads on rising edge.
190
    cpu_addr = addr;
191
    cpu_rd   = 1;
192
    cpu_we   = 0;
193
    @(negedge clk);
194
    cpu_rd   = 0;
195
    @(negedge clk); //uart returns data maximum of 2 clocks after read enable.
196
    data = cpu_data_out;
197
  endtask
198
 
199
  //Write a register.
200
  task automatic cpu_write( cpu_addr_t addr, cpu_data_t data );
201
    @(negedge clk); //setup on falling edge. DUT reads on rising edge.
202
    cpu_addr    = addr;
203
    cpu_rd      = 0;
204
    cpu_we      = 1;
205
    cpu_data_in = data;
206
    @(negedge clk);
207
  endtask
208
 
209
  //System clock
210
  initial
211
    begin
212
      clk = 0;
213
      forever #20ns clk <= ~clk;
214
    end
215
 
216
  //Main sim process.
217
  initial
218
    begin
219
      $display("%t << Starting the simulation >>", $time);
220
      cpu_init();
221
 
222
      //ok, now run some tests.
223
      test_cpu_interface();
224
      test_serial_facing_loopback(); //This one is also a good test for the testbench UART monitor/driver classes.
225
      test_cpu_to_txd();
226 4 andrewbrid
      //test_rxd_to_cpu();
227 2 andrewbrid
 
228
      $display("%m: %t << Simulation ran to completion >>", $time);
229
      //     $stop(0); //stop the simulation
230
    end
231
 
232
  task automatic dut_reset();
233
    reset <= 1;
234
    repeat(16) @(negedge clk);
235
    reset <= 0;
236
  endtask
237
 
238
  uart_driver  Uart_driver;
239
  uart_monitor Uart_monitor;
240
 
241
  task automatic build_test_harness( mailbox #(uint8) uart_driver_mbx, mailbox #(uint8) uart_monitor_mbx);
242
    //transaction mailboxes for comms between driver/monitor and main testbench thread.
243
    uart_driver_mbx  = new(0); //unbounded
244
    uart_monitor_mbx = new(0); //unbounded
245
 
246
    //construct UART monitor and driver.
247
    Uart_driver  = new( uart_, `BAUD_RATE, uart_driver_mbx );
248
    Uart_monitor = new( uart_, `BAUD_RATE, uart_monitor_mbx );
249
 
250
    //start them up.
251
    Uart_driver.run();
252
    Uart_monitor.run();
253
  endtask
254
 
255
  task automatic test_cpu_interface();
256
    cpu_data_t readval;
257
    $display("Testing cpu register interface...");
258
 
259
    dut_reset();
260
    //test read/write baud rate register
261
    cpu_write( 'h002, 'h55 );
262
    cpu_write( 'h003, 'hAA );
263
    cpu_read(  'h002, readval );
264
    assert (readval == 'h55) else $display("Failed to readback register 2 correctly.");
265
    cpu_read(  'h003, readval );
266
    assert (readval == 'hAA) else $display("Failed to readback register 2 correctly.");
267
  endtask
268
 
269
  task automatic test_serial_facing_loopback();
270
    //This loopback is right at the UART txd/rxd pins.
271
    mailbox #(uint8) sent_data_mbx;
272
    mailbox #(uint8) uart_driver_mbx;
273
    mailbox #(uint8) uart_monitor_mbx;
274
    build_test_harness( uart_driver_mbx, uart_monitor_mbx);
275
    dut_reset();
276
 
277
    $display("Testing serial facing loopback...");
278
    cpu_write( 'h002, 'h80 ); //enable the loopback.
279
 
280
    //Send a bunch of serial UART bytes.
281
    repeat(1000) begin
282
      uint8 data;
283
      data = $random();
284
      uart_driver_mbx.put( data );
285
      sent_data_mbx.put( data );
286
    end
287
 
288
    //Gives some time for any remaining transactions to propagate through the DUT.
289 4 andrewbrid
    #( `UART_FIFO_SIZE * (12 * 1s/`BAUD_RATE)); //10 bits/ baud, but allow for 12.
290 2 andrewbrid
 
291
    //Check the sent data comes back error free.
292
    compare_mailbox_data( sent_data_mbx, uart_monitor_mbx );
293
  endtask
294
 
295
  task automatic test_cpu_to_txd();
296
    mailbox #(uint8) sent_data_mbx;
297
 
298
    mailbox #(uint8) uart_driver_mbx;
299
    mailbox #(uint8) uart_monitor_mbx;
300
    build_test_harness( uart_driver_mbx, uart_monitor_mbx);
301
    dut_reset();
302
 
303
    sent_data_mbx = new(0);
304
    $display("Testing cpu to UART serial TXD interface...");
305
 
306
    //Using the UART's cpu interface, write a bunch of data in. Make sure we don't overflow the uart tx FIFO by
307
    //always reading the FIFO full flag prior to writing. Check all data is correctly received by the monitor.
308
    repeat(1000) begin
309
      uint8 readval;
310
      uint8 data;
311
      data = $random();
312
      //check uart tx fifo is not full
313
      cpu_read( 'h001, readval );
314
      if ((readval & 'h08) == 0) begin
315
        cpu_write( 'h000, data );
316
        sent_data_mbx.put(data);
317
      end
318
    end
319
 
320
    //Gives some time for any remaining transactions to propagate through the DUT.
321 4 andrewbrid
    #( `UART_FIFO_SIZE * (12 * 1s/`BAUD_RATE)); //10 bits/ baud, but allow for 12.
322 2 andrewbrid
 
323
    //Check the sent and received data is the same.
324
    compare_mailbox_data( sent_data_mbx, uart_monitor_mbx );
325
  endtask
326
 
327
  //Check all reference mailbox items against dut mailbox items. Expected to be identical, report differences.
328
  function automatic void compare_mailbox_data( mailbox #(uint8) ref_mbx, mailbox #(uint8) dut_mbx );
329
    uint32 error = 0;
330
    uint32 good = 0;
331 4 andrewbrid
    uint32 ref_mbx_num;
332
    uint32 dut_mbx_num;
333
 
334
    ref_mbx_num = ref_mbx.num();
335
    dut_mbx_num = dut_mbx.num();
336
 
337
    repeat( ref_mbx_num ) begin
338 2 andrewbrid
      uint8 dut_data;
339
      uint8 ref_data;
340
      uint32 tryget_result;
341
 
342 4 andrewbrid
      ref_mbx.try_get( ref_data );
343 2 andrewbrid
      //try to get dut data, may not be there if dut swallowed it.
344
      tryget_result = dut_mbx.try_get( dut_data );
345
      if (tryget_result) begin
346
        if (ref_data != dut_data) begin
347
          error++;
348
        end
349
        else begin
350
          good++;
351
        end
352
        break; //no more DUT data
353
      end
354
    end
355
    $display("Good: %2d, Errored: %2d, Excess reference %2d, Excess DUT %2d",
356 4 andrewbrid
             good, error, ref_mbx_num, dut_mbx_num);
357 2 andrewbrid
  endfunction
358
 
359
endmodule
360
 
361
 
362
 
363
 

powered by: WebSVN 2.1.0

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