Hardware Reference
In-Depth Information
following: 1) all states are included; 2) the list of outputs (only ctr in this case) is
exactly the same in all states, and the corresponding values are always included; 3)
the specii cations for nx_state are always i nalized with an else statement, so no condi-
tion is left unchecked.
1 //Module header:-----------------------------------------
2 module garage_door_controller (
3
input logic remt, sen1, sen2, clk, rst,
4
output logic [1:0] ctr);
5
6 //Declarations:------------------------------------------
7
//FSM states type:
8
typedef enum logic [2:0] {closed1, closed2, opening1,
9
opening2, open1, open2, closing1, closing2} state;
10
state pr_state, nx_state;
11
12 //Statements:--------------------------------------------
13
//FSM state register:
14
always_ff @(posedge clk, posedge rst)
15
if (rst) pr_state <= closed1;
16
else pr_state <= nx_state;
17
18
//FSM combinational logic:
19
always_comb
20
case (pr_state)
21
closed1: begin
22
ctr <= 2'b0x;
23
if (~remt) nx_state <= closed2;
24
else nx_state <= closed1;
25
end
26
closed2: begin
27
ctr <= 2'b0x;
28
if (remt) nx_state <= opening1;
29
else nx_state <= closed2;
30
end
31
opening1: begin
32
ctr <= 2'b10;
33
if (sen1) nx_state <= open1;
34
else if (~remt) nx_state <= opening2;
35
else nx_state <= opening1;
36
end
37
opening2: begin
38
ctr <= 2'b10;
39
if (remt | sen1) nx_state <= open1;
40
else nx_state <= opening2;
41
end
42
open1: begin
43
ctr <= 2'b0x;
44
if (~remt) nx_state <= open2;
45
else nx_state <= open1;
46
end
47
open2: begin
48
ctr <= 2'b0x;
49
if (remt) nx_state <= closing1;
Search WWH ::




Custom Search