← Back to A Random Number Every Second
testbench.sv
· Download testbench.sv · Download both files (.zip)
`timescale 1ns/1ps
// ECE 287 - Exercise 3 testbench: does "done" happen once a second?
// Paste this into the LEFT pane (testbench.sv) on EDA Playground.
//
// Here "one second" is shrunk to MAX_COUNT = 10 clocks so you can read
// every line of the log.
//
// In EPWave (it opens empty): Get Signals -> scope tb -> select clk, start,
// done; then scope tb.dut -> select count -> Append Selected. Count the clock
// periods between start rising and done rising.
//
// Questions to answer from the log:
// 1. How many clocks after start goes high does done go high?
// 2. While start stays high, does done ever go back to 0?
// 3. For the challenge you need something that happens ONCE EVERY
// second, over and over. Does this counter do that as written?
// What would have to change, here or in how you drive start?
module tb;
localparam MAX_COUNT = 10;
reg clk = 1'b0;
reg rst = 1'b0;
reg start = 1'b0;
wire done;
integer cyc;
integer rises;
integer falls;
reg last_done;
counter_50MHz #(.MAX_COUNT(MAX_COUNT)) dut(.clk(clk), .rst(rst), .start(start), .done(done));
// From Exercises 1 and 2, not instantiated here. design.sv still has both
// modules; the instances stay commented out until the challenge.
// lfsr dut_lfsr(.clk(clk), .rst(rst), .seed(seed), .en_seed(en_seed), .number(number));
// seven_segment dut_seg(.i(i), .o(o));
always #10 clk = ~clk; // 50 MHz
initial
begin
$dumpfile("dump.vcd");
$dumpvars(0, tb);
repeat (3) @(posedge clk);
#1 rst = 1'b1;
repeat (3) @(posedge clk);
#1 start = 1'b1;
rises = 0;
falls = 0;
last_done = done;
$display("start went high. MAX_COUNT = %0d", MAX_COUNT);
$display(" clock | count | done");
for (cyc = 1; cyc <= 4 * MAX_COUNT; cyc = cyc + 1)
begin
@(posedge clk);
#1;
$display(" %3d | %3d | %b", cyc, dut.count, done);
if (done == 1'b1 && last_done == 1'b0) rises = rises + 1;
if (done == 1'b0 && last_done == 1'b1) falls = falls + 1;
last_done = done;
end
$display("");
$display("In %0d clocks (about %0d 'seconds'), done went 0->1 %0d time(s) and 1->0 %0d time(s).",
4 * MAX_COUNT, 4, rises, falls);
$finish;
end
endmodule