-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdifficulty_fsm.sv
More file actions
49 lines (41 loc) · 1.25 KB
/
difficulty_fsm.sv
File metadata and controls
49 lines (41 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
module difficulty_fsm(
input clk,
input button_edge,
output logic [1:0] level
);
// intial level = 2'b00;
// State teypedef enum used here
// Note that we specify the exact encoding that we want to use for each state
typedef enum logic [1:0] {
LVL1 = 2'b00,
LVL2 = 2'b01,
LVL3 = 2'b10,
LVL4 = 2'b11
} state_type;
state_type current_state = LVL1, next_state;
// always_comb block for next state logic
always_comb begin
next_state = current_state;
case(current_state)
LVL1: begin
next_state = (button_edge == 1'b1) ? LVL2 : LVL1;
end
LVL2: begin
next_state = (button_edge == 1'b1) ? LVL3 : LVL2;
end
LVL3: begin
next_state = (button_edge == 1'b1) ? LVL4 : LVL3;
end
LVL4: begin
next_state = (button_edge == 1'b1) ? LVL1: LVL4;
end
default: next_state = LVL1;
endcase
end
// always_ff for FSM state variable flip_flops
always_ff @(posedge clk) begin
current_state <= next_state;
end
// assign outputs
assign level = current_state;
endmodule