Skip to content

Instantly share code, notes, and snippets.

@wdevore
Created October 5, 2019 16:40
Show Gist options
  • Save wdevore/69029fde32d610e24968bdbbe5f6173b to your computer and use it in GitHub Desktop.
Save wdevore/69029fde32d610e24968bdbbe5f6173b to your computer and use it in GitHub Desktop.
Simple counter used for learning test bench features and workflow
// A simple comparator used for learning simulation workflows
// comparator - comparator_tb.v
//
// Description:
// Testbench for comparator module.
//
`timescale 1ns/10ps
module comparator #(
parameter WIDTH = 8 // data width
) (
input [WIDTH-1:0] a, b, // values to compare
output lt, // high when a < b
output eq, // high when a = b
output gt // high when a > b
);
assign lt = (a < b) ? 1'b1 : 1'b0;
assign eq = (a == b) ? 1'b1 : 1'b0;
assign gt = (a > b) ? 1'b1 : 1'b0;
endmodule // comparator
// --------------------------------------------------------------------------
// Test bench
// --------------------------------------------------------------------------
module comparator_tb;
parameter WIDTH = 2; // data width
integer i, j; // for-loop variables
reg [WIDTH-1:0] a, b; // input values to compare
wire lt, eq, gt; // output comparison status
initial begin
$dumpfile("comparator_tb.vcd"); // waveforms file
$dumpvars; // save waveforms
$display("%d %m: Starting testbench simulation...", $stime);
$monitor("%d %m: MONITOR - a = %d, b = %d, lt = %d, eq = %d, gt = %d.", $stime, a, b, lt, eq, gt);
#1;
for (i = 0; i < 2 ** WIDTH; i = i + 1) begin
for (j = 0; j < 2 ** WIDTH; j = j + 1) begin
#1 a = i; b = j;
#1;
if (a < b && (!lt || eq || gt)) begin
$display("%d %m: ERROR - Status flags lt (%d) eq (%d) gt (%d) are not correct for a (%d) less than b (%d).", $stime, lt, eq, gt, a, b);
$finish;
end
if (a == b && (lt || !eq || gt)) begin
$display("%d %m: ERROR - Status flags lt (%d) eq (%d) gt (%d) are not correct for a (%d) equal to b (%d).", $stime, lt, eq, gt, a, b);
$finish;
end
if (a > b && (lt || eq || !gt)) begin
$display("%d %m: ERROR - Status flags lt (%d) eq (%d) gt (%d) are not correct for a (%d) greater than b (%d). ", $stime, lt, eq, gt, a, b);
$finish;
end
end
end
#1 $display("%d %m: Testbench simulation PASSED.", $stime);
$finish; // end simulation
end
// Instances
comparator #(.WIDTH(WIDTH)) comparator_1(.a(a), .b(b), .lt(lt), .eq(eq), .gt(gt));
endmodule // comparator_tb
@wdevore
Copy link
Author

wdevore commented Oct 5, 2019

Make sure you have both files in the same folder otherwise you will need to add an include directive:

`include "folder/..."

If you are using APIO then you can use the following command to simulate the counter:

apio sim

You should get an output in your console:

0 comparator_tb: Starting testbench simulation...
0 comparator_tb: MONITOR - a = x, b = x, lt = x, eq = x, gt = x.
2 comparator_tb: MONITOR - a = 0, b = 0, lt = 0, eq = 1, gt = 0.
4 comparator_tb: MONITOR - a = 0, b = 1, lt = 1, eq = 0, gt = 0.
6 comparator_tb: MONITOR - a = 0, b = 2, lt = 1, eq = 0, gt = 0.
8 comparator_tb: MONITOR - a = 0, b = 3, lt = 1, eq = 0, gt = 0.
10 comparator_tb: MONITOR - a = 1, b = 0, lt = 0, eq = 0, gt = 1.
12 comparator_tb: MONITOR - a = 1, b = 1, lt = 0, eq = 1, gt = 0.
14 comparator_tb: MONITOR - a = 1, b = 2, lt = 1, eq = 0, gt = 0.
16 comparator_tb: MONITOR - a = 1, b = 3, lt = 1, eq = 0, gt = 0.
18 comparator_tb: MONITOR - a = 2, b = 0, lt = 0, eq = 0, gt = 1.
20 comparator_tb: MONITOR - a = 2, b = 1, lt = 0, eq = 0, gt = 1.
22 comparator_tb: MONITOR - a = 2, b = 2, lt = 0, eq = 1, gt = 0.
24 comparator_tb: MONITOR - a = 2, b = 3, lt = 1, eq = 0, gt = 0.
26 comparator_tb: MONITOR - a = 3, b = 0, lt = 0, eq = 0, gt = 1.
28 comparator_tb: MONITOR - a = 3, b = 1, lt = 0, eq = 0, gt = 1.
30 comparator_tb: MONITOR - a = 3, b = 2, lt = 0, eq = 0, gt = 1.
32 comparator_tb: MONITOR - a = 3, b = 3, lt = 0, eq = 1, gt = 0.
34 comparator_tb: Testbench simulation PASSED.

APIO will also attempt to launch gtkwave if you have it installed:
gtkwave

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment