Dr. Peter Andrew Jamieson — Academic in an information evolution

← Back to A Random Number Every Second

testbench.sv

· Download testbench.sv · Download both files (.zip)

`timescale 1ns/1ps
// ECE 287 - Challenge testbench. You should NOT need to change this file.
// Paste this into the LEFT pane (testbench.sv) on EDA Playground.
//
// It plays the part of the DE board and of you watching it:
//   1. holds KEY[2] (reset) down, then lets go
//   2. sets SW[7:0] to a seed and presses KEY[1] briefly
//   3. lets the design run for about 12 "seconds"
//   4. reads HEX2..HEX0 back into a number and, every time it changes, prints
//      it and draws the three displays in ASCII art
//   5. prints a summary of what it saw
//
// "One second" is shrunk to ONE_SECOND = 100 clocks so the run is quick.
// On the board your design uses the default of 50,000,000.
//
// In EPWave (it opens empty): Get Signals -> scope tb -> CLOCK_50, KEY, HEX0;
// scope tb.dut -> random_number, load_seed, counter_start, one_second, val;
// scope tb.dut.u_counter -> count -> Append Selected. Between ticks
// random_number should change every clock while val holds still.

module tb;

	localparam ONE_SECOND = 100;
	localparam SECONDS    = 12;
	localparam [7:0] SEED = 8'hA5;   // 165

	reg        CLOCK_50 = 1'b0;
	reg  [3:0] KEY = 4'b1111;        // nothing pressed
	reg  [9:0] SW  = 10'd0;
	wire [6:0] HEX0, HEX1, HEX2;
	wire [9:0] LEDR;

	random_display #(.ONE_SECOND(ONE_SECOND)) dut(
		.CLOCK_50(CLOCK_50), .KEY(KEY), .SW(SW),
		.HEX0(HEX0), .HEX1(HEX1), .HEX2(HEX2), .LEDR(LEDR));

	always #10 CLOCK_50 = ~CLOCK_50;   // 50 MHz

	// turn a seven-segment pattern back into a digit; 15 means "not a digit"
	function [3:0] seg_to_digit(input [6:0] s);
		case (s)
			7'b1000000: seg_to_digit = 4'd0;
			7'b1111001: seg_to_digit = 4'd1;
			7'b0100100: seg_to_digit = 4'd2;
			7'b0110000: seg_to_digit = 4'd3;
			7'b0011001: seg_to_digit = 4'd4;
			7'b0010010: seg_to_digit = 4'd5;
			7'b0000010: seg_to_digit = 4'd6;
			7'b1111000: seg_to_digit = 4'd7;
			7'b0000000: seg_to_digit = 4'd8;
			7'b0010000: seg_to_digit = 4'd9;
			default:    seg_to_digit = 4'd15;
		endcase
	endfunction

	// one row of a seven-segment digit as 3 characters of ASCII art.
	// The segments are active low: a 0 bit draws, a 1 bit is blank.
	//   row 0:  _       segment 0
	//   row 1: |_|      segments 5, 6, 1
	//   row 2: |_|      segments 4, 3, 2
	function [23:0] seg_row(input [6:0] s, input [1:0] row);
		case (row)
			2'd0:    seg_row = {" ", (s[0] ? " " : "_"), " "};
			2'd1:    seg_row = {(s[5] ? " " : "|"), (s[6] ? " " : "_"), (s[1] ? " " : "|")};
			2'd2:    seg_row = {(s[4] ? " " : "|"), (s[3] ? " " : "_"), (s[2] ? " " : "|")};
			default: seg_row = "   ";
		endcase
	endfunction

	// draw HEX2 HEX1 HEX0 side by side, the way they sit on the board
	task show_hex;
		begin
			$display("           %s %s %s", seg_row(HEX2, 2'd0), seg_row(HEX1, 2'd0), seg_row(HEX0, 2'd0));
			$display("           %s %s %s", seg_row(HEX2, 2'd1), seg_row(HEX1, 2'd1), seg_row(HEX0, 2'd1));
			$display("           %s %s %s", seg_row(HEX2, 2'd2), seg_row(HEX1, 2'd2), seg_row(HEX0, 2'd2));
		end
	endtask

	integer cyc;
	integer shown;
	integer last_shown;
	integer last_change;
	integer changes;
	integer printed;
	integer min_gap;
	integer max_gap;
	integer gap;
	integer bad_digits;
	reg [3:0] d2, d1, d0;

	initial
	begin
		$dumpfile("dump.vcd");
		$dumpvars(0, tb);

		// 1. reset
		KEY[2] = 1'b0;
		repeat (5) @(posedge CLOCK_50);
		#1 KEY[2] = 1'b1;

		// 2. seed: set the switches, press KEY[1] for 3 clocks, let go
		repeat (3) @(posedge CLOCK_50);
		#1 SW[7:0] = SEED;
		repeat (2) @(posedge CLOCK_50);
		#1 KEY[1] = 1'b0;
		repeat (3) @(posedge CLOCK_50);
		#1 KEY[1] = 1'b1;

		$display("Reset done, seed %0d loaded with KEY[1]. ONE_SECOND = %0d clocks.", SEED, ONE_SECOND);
		$display("Watching HEX2..HEX0 for about %0d seconds:", SECONDS);
		$display("   clock | shown | clocks since last change");

		// 3 + 4. run and watch the display
		last_shown  = -1;
		last_change = 0;
		changes     = 0;
		printed     = 0;
		min_gap     = 1000000000;
		max_gap     = 0;
		bad_digits  = 0;

		for (cyc = 1; cyc <= SECONDS * ONE_SECOND; cyc = cyc + 1)
		begin
			@(posedge CLOCK_50);
			#1;
			d2 = seg_to_digit(HEX2);
			d1 = seg_to_digit(HEX1);
			d0 = seg_to_digit(HEX0);
			if (d2 == 15 || d1 == 15 || d0 == 15)
			begin
				if (bad_digits == 0)
				begin
					$display("   %5d | HEX shows something that is not a digit: HEX2=%b HEX1=%b HEX0=%b",
					         cyc, HEX2, HEX1, HEX0);
					show_hex;
				end
				bad_digits = bad_digits + 1;
			end
			else
			begin
				shown = d2 * 100 + d1 * 10 + d0;
				if (shown != last_shown)
				begin
					if (last_shown == -1)
					begin
						if (printed < 40)
						begin
							$display("   %5d |  %3d  | (first value)", cyc, shown);
							show_hex;
						end
					end
					else
					begin
						gap = cyc - last_change;
						changes = changes + 1;
						if (gap < min_gap) min_gap = gap;
						if (gap > max_gap) max_gap = gap;
						if (printed < 40)
						begin
							$display("   %5d |  %3d  | %0d", cyc, shown, gap);
							show_hex;
						end
					end
					printed = printed + 1;
					if (printed == 40) $display("   ... (only the first 40 changes are printed)");
					last_shown  = shown;
					last_change = cyc;
				end
			end
		end

		// 5. summary
		$display("");
		$display("===== SUMMARY =====");
		if (bad_digits > 0)
			$display("* On %0d clocks the HEX outputs were not a 0-9 digit. Check your seven_segment wiring.", bad_digits);
		if (changes == 0)
		begin
			if (last_shown == -1)
				$display("* The display never showed a number.");
			else
				$display("* The display never changed. It stayed at %0d the whole time.", last_shown);
		end
		else
		begin
			$display("* The display changed %0d times in about %0d seconds.", changes, SECONDS);
			$display("* Clocks between changes: shortest %0d, longest %0d (one second here = %0d).",
			         min_gap, max_gap, ONE_SECOND);
			if (min_gap < ONE_SECOND / 2)
				$display("* It is changing much faster than once a second.");
			else if (changes < SECONDS / 2)
				$display("* It is changing much slower than once a second.");
			else
				$display("* That looks like about one new number per second. Now check that the numbers look random, then try it on the board!");
		end
		$finish;
	end

endmodule