-
Notifications
You must be signed in to change notification settings - Fork 0
/
keypress_state_machine.sv
86 lines (77 loc) · 1.83 KB
/
keypress_state_machine.sv
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// State Machine for KeyPress
module keypress_state_machine (
input Clk,
input Reset,
input touchdown,
input ResetShape,
input [15:0] keycode,
output [2:0] keypress );
enum logic [3:0] {State_A_1, State_A_2, State_S_1, State_D_1, State_D_2, State_SBar_1, State_SBar_2} Idle, State, Next_State;
always_ff @ (posedge Clk or posedge Reset)
begin //Begin Initilization Procedure
if (Reset)
begin
State <= Idle;
end
else
begin
State <= Next_State;
end
end
always_comb
begin // Transitions Happen Here
Next_State = State;
unique case (State)
Idle:
begin
if (keycode == 16'h0004) //'a'
Next_State <= State_A_1;
else if (keycode == 16'h0007) //'d'
Next_State <= State_D_1;
else if (keycode == 16'h0016) //'s'
Next_State <= State_S_1;
else if (keycode == 16'h002C)
Next_State <= State_SBar_1;
end
State_A_1:
Next_State <= State_A_2;
State_A_2:
begin
if (keycode == 16'h0000)
Next_State <= Idle;
end
State_D_1:
Next_State <= State_D_2;
State_D_2:
begin
if (keycode == 16'h0000)
Next_State <= Idle;
end
State_SBar_1:
if (touchdown||ResetShape)
Next_State <= State_SBar_2;
State_SBar_2:
if (keycode == 16'h0000)
Next_State <= Idle;
State_S_1:
Next_State <= Idle;
default : ;
endcase
end
always_comb
begin
//Default Zeros
keypress = 3'd0;
unique case (State)
State_A_1:
keypress = 3'd1;
State_D_1:
keypress = 3'd2;
State_S_1:
keypress = 3'd3;
State_SBar_1:
keypress = 3'd4;
default : ;
endcase
end
endmodule