Advertisement
Sidsh

Untitled

Dec 4th, 2024
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. `timescale 1ns/1ps
  2. `include "uart_pkg.sv"
  3. import uart_pkg::*;
  4.  
  5. module tb_uart;
  6.  
  7.     // Testbench parameters
  8.     localparam CLK_PERIOD = 20; // 50 MHz clock
  9.     localparam integer DATA_WIDTH = 8;
  10.     parameter int CLK_FREQ = 50000000;
  11.     parameter int BAUD_RATE = 19200;
  12.     // DUT I/O
  13.     logic clk, rst;
  14.     logic rx, tx;
  15.     logic [DATA_WIDTH-1:0] tx_data_in;
  16.     logic [DATA_WIDTH-1:0] rx_data_out;
  17.     logic start, done_tx, tx_active;
  18.  
  19.     // Test variables
  20.     logic [DATA_WIDTH-1:0] test_data;
  21.  
  22.     // Instantiate DUT
  23.     UART #(
  24.         .CLK_FREQ(50000000), // 50 MHz
  25.         .BAUD_RATE(19200)    // 19200 baud rate
  26.     ) dut (
  27.         .clk(clk),
  28.         .rst(rst),
  29.         .rx(rx),
  30.         .tx_data_in(tx_data_in),
  31.         .start(start),
  32.         .rx_data_out(rx_data_out),
  33.         .tx(tx),
  34.         .tx_active(tx_active),
  35.         .done_tx(done_tx)
  36.     );
  37.  
  38.     // Clock generation
  39.     always #(CLK_PERIOD / 2) clk = ~clk;
  40.  
  41.     // UART RX/TX signal connection (loopback)
  42.     assign rx = tx;
  43.  
  44.     // Testbench stimulus
  45.     initial begin
  46.         $display("Starting UART Testbench...");
  47.        
  48.         // Initialize signals
  49.         clk = 0;
  50.         rst = 1;
  51.         start = 0;
  52.         tx_data_in = 0;
  53.         test_data = 8'hA5; // Test data (example: 0xA5)
  54.  
  55.         // Apply reset
  56.         #100;
  57.         rst = 0;
  58.  
  59.         // Test UART TX and RX
  60.         #100;
  61.         $display("Sending data: 0x%0h", test_data);
  62.         tx_data_in = test_data;
  63.         start = 1; // Trigger TX
  64.         #CLK_PERIOD;
  65.         start = 0;
  66.  
  67.         // Wait for transmission to complete
  68.         wait (done_tx);
  69.         #CLK_PERIOD;
  70.  
  71.         // Check received data
  72.         #100; // Allow some time for RX to process
  73.         if (rx_data_out == test_data) begin
  74.             $display("Test Passed: Received data matches transmitted data (0x%0h)", rx_data_out);
  75.         end else begin
  76.             $display("Test Failed: Received data (0x%0h) does not match transmitted data (0x%0h)", rx_data_out, test_data);
  77.         end
  78.  
  79.         // End simulation
  80.         $finish;
  81.     end
  82.  
  83.     // Monitor signals
  84.     initial begin
  85.         $monitor("Time=%0t | TX=0x%0h | RX=0x%0h | TX_Active=%b | Done_TX=%b",
  86.                  $time, tx_data_in, rx_data_out, tx_active, done_tx);
  87.     end
  88.  
  89. endmodule
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement