← Back to A Random Number Every Second
design.sv
· Download design.sv · Download both files (.zip)
`timescale 1ns/1ps
// ECE 287 - Simple Sequential Design
// Exercise 3: the one-second counter from the slides.
// Paste this into the RIGHT pane (design.sv) on EDA Playground.
//
// This file builds up the big design one piece at a time. Modules from
// earlier exercises are kept here, complete, but nothing instantiates them
// yet (their instantiations are commented out in testbench.sv). In the
// challenge, your top level instantiates all of them.
// ---------------------------------------------------------------------------
// From Exercise 1: LFSR (not instantiated in this exercise)
// ---------------------------------------------------------------------------
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 (not instantiated in this exercise)
// ---------------------------------------------------------------------------
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
// ---------------------------------------------------------------------------
// NEW in Exercise 3: one-second counter
//
// One change from the slide: the 50,000,000 limit is now a parameter,
// MAX_COUNT, so a testbench can shrink "one second" to a few clocks.
// Simulating 50 million clocks would take far too long.
//
// Question: on the slide the comparison was written as count >= 7'd50000000.
// How many bits does 50,000,000 need? What would 7'd do to it?
// ---------------------------------------------------------------------------
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