Dr. Peter Andrew Jamieson — Academic in an information evolution

← Back to A Random Number Every Second

design.sv

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

`timescale 1ns/1ps
// ECE 287 - Simple Sequential Design
// THE CHALLENGE: build a design that displays a ~random number every ~second!
// Paste this into the RIGHT pane (design.sv) on EDA Playground.
//
// This file holds all three modules from Exercises 1-3, and the skeleton of
// the top-level module, random_display, at the bottom. The skeleton has every
// piece in place: the LFSR, the counter, a display register, and the three
// seven-segment decoders, all instantiated and wired up. What is missing is
// the logic that makes them work together. Look for TODO 1-3.
//
// This skeleton compiles and runs as-is. It shows 000 forever, because the
// TODOs hold placeholder values. That is your starting point, not a bug in
// the testbench.
//
// Board interface (the testbench drives these exactly like the DE board):
//   CLOCK_50   50 MHz clock
//   KEY[2]     reset, active low (pressed = 0)
//   KEY[1]     load the seed, active low (pressed = 0)
//   SW[7:0]    the seed value
//   HEX2..0    hundreds, tens, ones digit of the random number
//   LEDR       yours to use (optional)


// ---------------------------------------------------------------------------
// From Exercise 1: LFSR (unchanged)
// ---------------------------------------------------------------------------
module lfsr(	input clk,
		input rst,
		input [7:0]seed,
		input en_seed,
		output reg [7:0]number);

always @(posedge clk or negedge rst)
begin
	if (rst == 1'b0)
	begin
		number <= 8'd0;
	end
	else
	begin
		if (en_seed == 1'b1)
		   number <= seed;
		else
		   number <= {number[6], number[5]^number[4], number[4], number[3], number[2], number[1], number[0], number[7]^number[2]};
	end
end

endmodule


// ---------------------------------------------------------------------------
// From Exercise 2: seven-segment decoder (unchanged)
// ---------------------------------------------------------------------------
module seven_segment(
	input wire[3:0]i,
	output reg[6:0]o
);

always @(*)
begin
	case (i)	    // 6543210
		4'h0: o = 7'b1000000;
		4'h1: o = 7'b1111001;
		4'h2: o = 7'b0100100;
		4'h3: o = 7'b0110000;
		4'h4: o = 7'b0011001;
		4'h5: o = 7'b0010010;
		4'h6: o = 7'b0000010;
		4'h7: o = 7'b1111000;
		4'h8: o = 7'b0000000;
		4'h9: o = 7'b0010000;
		4'ha: o = 7'b0001000;
		4'hb: o = 7'b0000011;
		4'hc: o = 7'b1000110;
		4'hd: o = 7'b0100001;
		4'he: o = 7'b0000110;
		4'hf: o = 7'b0001110;
	endcase
end

endmodule


// ---------------------------------------------------------------------------
// From Exercise 3: one-second counter. The limit is a parameter so the
// testbench can shrink "one second".
// ---------------------------------------------------------------------------
module counter_50MHz #(parameter MAX_COUNT = 50000000)
	(input clk, input rst, input start, output reg done);

reg [26:0]count;

always @(posedge clk or negedge rst)
begin
	if (rst == 1'b0)
	begin
		count <= 27'd0;
		done <= 1'b0;
	end
	else
	begin
		if (start == 1'b0)
			count <= 27'd0;
		else
			count <= count + 1'b1;

		if (count == 7'd0)
			done <= 1'b0;
		else if (count >= MAX_COUNT)
			done <= 1'b1;
	end
end

endmodule


// ---------------------------------------------------------------------------
// YOUR TOP LEVEL
// ---------------------------------------------------------------------------
module random_display #(parameter ONE_SECOND = 50000000)
(
	input        CLOCK_50,
	input  [3:0] KEY,
	input  [9:0] SW,
	output [6:0] HEX0,
	output [6:0] HEX1,
	output [6:0] HEX2,
	output [9:0] LEDR
);

wire clk;
assign clk = CLOCK_50;
wire rst;
assign rst = KEY[2];

// ----- The pieces. Every wire and instance is here, but nothing is built yet.
// Each TODO is a placeholder value that keeps the file compiling. Replace it.

wire [7:0]random_number;   // the LFSR's output, a new value every clock
wire load_seed;            // 1 = the LFSR loads SW[7:0] on this clock
wire one_second;           // the counter's done
wire counter_start;        // the counter's start
reg  [7:0]shown;           // the number on the display, held between ticks

wire [7:0]val;             // what the HEX displays show (the "?" on the slide)
assign val = shown;


// TODO 1: load_seed. KEY[1] reads 0 while it is pressed, and en_seed loads
//         the seed when it is 1. What goes here?
assign load_seed = 1'b0;

lfsr u_lfsr(
	.clk(clk),
	.rst(rst),
	.seed(SW[7:0]),
	.en_seed(load_seed),
	.number(random_number)
);


// TODO 2: counter_start. The counter counts while start is 1 and clears to 0
//         while start is 0. done goes high once the count passes ONE_SECOND.
//         You need a tick EVERY second, not just once (see Exercise 3).
//         What should start be so the counter keeps starting over?
assign counter_start = 1'b0;

counter_50MHz #(.MAX_COUNT(ONE_SECOND)) u_counter(
	.clk(clk),
	.rst(rst),
	.start(counter_start),
	.done(one_second)
);


// TODO 3: shown. This register is what makes the display hold still for a
//         second. On which clocks should it take a new value, and from where?
always @(posedge clk or negedge rst)
begin
	if (rst == 1'b0)
	begin
		shown <= 8'd0;
	end
	else
	begin
		// TODO 3 goes here
	end
end


// TODO 4 (optional): use LEDR for something helpful, like showing one_second.
assign LEDR = 10'd0;


// ----- From the slides: binary to three decimal digits on HEX2..HEX0 -------
wire [6:0]seg7_1sb;
wire [6:0]seg7_10sb;
wire [6:0]seg7_100sb;

assign HEX2 = seg7_100sb;
assign HEX1 = seg7_10sb;
assign HEX0 = seg7_1sb;

reg [3:0] result_hundred_digit;
reg [3:0] result_one_digit;
reg [3:0] result_ten_digit;

/* convert the binary value into 3 decimal signals */
always @(*)
begin
	result_hundred_digit = val / 100 % 10;
	result_ten_digit = val / 10 % 10;
	result_one_digit = val % 10;
end

/* instantiate the modules for each of the seven seg decoders */
seven_segment m100sb(result_hundred_digit, seg7_100sb);
seven_segment m10sb(result_ten_digit, seg7_10sb);
seven_segment m1sb(result_one_digit, seg7_1sb);

endmodule