Hardware Reference
In-Depth Information
In this kind of application, the “−1” term present in the dei nition of the total time
(lines 16, 39, 44, 49, . . .) does not make any difference, but it was maintained as a
reminder of the precise value. Also, in this application possible glitches during (posi-
tive) clock transitions are not a problem, so the optional output register shown in the
last part of the template was not employed.
The reader is invited to compile this code and play with the circuit in the FPGA
development board. Also, check whether the number of DFFs inferred by the compiler
matches the prediction made in section 8.11.2 for each encoding style.
1 //Module header:-----------------------------------
2 module light_rotator (
3
input logic stp, clk, rst,
4
output logic [6:0] ssd);
5
6 //Declarations:-------------------------------------
7
8
//FSM-related declarations:
9
typedef enum logic [3:0] {A, AB, B, BC, C, CD, D, DE, E, EF,
10
F, FA} state;
11
state pr_state, nx_state;
12
13
//Timer-related declarations:
14
const logic [22:0] T1 = 6_000_000; //120ms @fclk=50MHz
15
const logic [22:0] T2 = 1_750_000; //35ms @fclk=50MHz
16
const logic [22:0] tmax = T1-1; //tmax≥max(T1,T2)-1
17
logic [22:0] t;
18
19 //Statements:-----------------------------------------
20
21
//Timer (using strategy #1):
22
always_ff @(posedge clk, posedge rst)
23
if (rst) t <= 0;
24
else if (~stp) begin
25
if (pr_state != nx_state) t <= 0;
26
else if (t != tmax) t <= t + 1;
27
end
28
29
//FSM state register:
30
always_ff @(posedge clk, posedge rst)
31
if (rst) pr_state <= A;
32
else pr_state <= nx_state;
33
34
//FSM combinational logic:
35
always_comb
36
case (pr_state)
37
A: begin
38
ssd <= 7'b0111111;
39
if (t>=T1-1) nx_state <= AB; //or t==T1-1
40
else nx_state <= A;
41
end
42
AB: begin
43
ssd <= 7'b0011111;
44
if (t>=T2-1) nx_state <= B; //or t==T2-1
45
else nx_state <= AB;
Search WWH ::




Custom Search