This repository has been archived by the owner on Dec 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Decoder.v
234 lines (214 loc) · 5.63 KB
/
Decoder.v
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Engineer: Michelle Yu
// Josh Sackos
// Mavl Pond
//
//
// Create Date: 07/23/2012
// Last modified: 17/04/2018
//
// Module Name: Decoder
// Project Name: VGAStackCalculator
// Target Devices: Max10
// Description: This file defines a component Decoder. The Decoder scans
// each column by asserting a low to the pin corresponding to the column at 1KHz. After a
// column is asserted low, each row pin is checked. When a row pin is detected to be low,
// the key that was pressed could be determined.
//
// Revision History:
// Revision 0.01 - File Created (Michelle Yu)
// Revision 0.02 - Converted from VHDL to Verilog (Josh Sackos)
// Revision 0.03 - Added DecoderState flag and locking
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// ==============================================================================================
// Define Module
// ==============================================================================================
module Decoder(
clk,
Row,
Col,
DecodeOut,
DecoderState
);
// ==============================================================================================
// Port Declarations
// ==============================================================================================
input clk; // 100MHz onboard clock
input [3:0] Row; // Rows on KYPD
output [3:0] Col; // Columns on KYPD
output [3:0] DecodeOut; // Output data
output DecoderState; // 0 - no change, 1 - has new token
// ==============================================================================================
// Parameters, Regsiters, and Wires
// ==============================================================================================
reg NewToken = 0;
reg Probe = 0;
reg [3:0] OutBuffer;
// Output wires and registers
reg [3:0] Col;
reg [3:0] DecodeOut;
reg lock = 0; // Locks the input until signal goes away
// Count register
reg [19:0] sclk = 0;
assign DecoderState = NewToken && ~lock;
// ==============================================================================================
// Implementation
// ==============================================================================================
always @(posedge clk) begin
if (sclk == 20'b00000000000000000000)
if (NewToken && Probe)
lock <= 1;
else if (!NewToken && Probe) begin
DecodeOut <= OutBuffer;
NewToken <= 1;
if (lock)
lock <= 0;
end else if (NewToken && !Probe) begin
NewToken <= 0;
lock <= 0;
end
if (sclk == 20'b00000000000000000001)
lock <= 1;
// 1ms
if (sclk == 20'b11000011010100000) begin
//C1
Col <= 4'b0111;
sclk <= sclk + 1'b1;
Probe <= 0;
end
// check row pins
else if(sclk == 20'b11000011010101000) begin
//R1
if (Row == 4'b0111) begin
OutBuffer <= 4'b0001; //1
Probe <= 1;
lock <= 1;
end
//R2
else if(Row == 4'b1011) begin
OutBuffer <= 4'b0100; //4
Probe <= 1;
lock <= 1;
end
//R3
else if(Row == 4'b1101) begin
OutBuffer <= 4'b0111; //7
Probe <= 1;
lock <= 1;
end
//R4
else if(Row == 4'b1110) begin
OutBuffer <= 4'b0000; //0
Probe <= 1;
lock <= 1;
end
sclk <= sclk + 1'b1;
end
// 2ms
else if(sclk == 20'b110000110101000000) begin
//C2
Col<= 4'b1011;
sclk <= sclk + 1'b1;
end
// check row pins
else if(sclk == 20'b110000110101001000) begin
//R1
if (Row == 4'b0111) begin
OutBuffer <= 4'b0010; //2
Probe <= 1;
lock <= 1;
end
//R2
else if(Row == 4'b1011) begin
OutBuffer <= 4'b0101; //5
Probe <= 1;
lock <= 1;
end
//R3
else if(Row == 4'b1101) begin
OutBuffer <= 4'b1000; //8
Probe <= 1;
lock <= 1;
end
//R4
else if(Row == 4'b1110) begin
OutBuffer <= 4'b1111; //F
Probe <= 1;
lock <= 1;
end
sclk <= sclk + 1'b1;
end
//3ms
else if(sclk == 20'b1001001001111100000) begin
//C3
Col<= 4'b1101;
sclk <= sclk + 1'b1;
end
// check row pins
else if(sclk == 20'b1001001001111101000) begin
//R1
if(Row == 4'b0111) begin
OutBuffer <= 4'b0011; //3
Probe <= 1;
lock <= 1;
end
//R2
else if(Row == 4'b1011) begin
OutBuffer <= 4'b0110; //6
Probe <= 1;
lock <= 1;
end
//R3
else if(Row == 4'b1101) begin
OutBuffer <= 4'b1001; //9
Probe <= 1;
lock <= 1;
end
//R4
else if(Row == 4'b1110) begin
OutBuffer <= 4'b1110; //E
Probe <= 1;
lock <= 1;
end
sclk <= sclk + 1'b1;
end
//4ms
else if(sclk == 20'b11000011010100000000) begin
//C4
Col<= 4'b1110;
sclk <= sclk + 1'b1;
end
// Check row pins
else if(sclk == 20'b11000011010100001000) begin
//R1
if(Row == 4'b0111) begin
OutBuffer <= 4'b1010; //A
Probe <= 1;
lock <= 1;
end
//R2
else if(Row == 4'b1011) begin
OutBuffer <= 4'b1011; //B
Probe <= 1;
lock <= 1;
end
//R3
else if(Row == 4'b1101) begin
OutBuffer <= 4'b1100; //C
Probe <= 1;
lock <= 1;
end
//R4
else if(Row == 4'b1110) begin
OutBuffer <= 4'b1101; //D
Probe <= 1;
lock <= 1;
end
sclk <= 20'b00000000000000000000;
end
// Otherwise increment
else begin
sclk <= sclk + 1'b1;
end
end
endmodule