1 |
27 |
smpickett |
//==================================================================//
|
2 |
|
|
// File: d_MouseDriver.v //
|
3 |
|
|
// Version: 0.0.0.1 //
|
4 |
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -//
|
5 |
|
|
// Copyright (C) Stephen Pickett //
|
6 |
|
|
// Apr 28, 2005 //
|
7 |
|
|
// //
|
8 |
|
|
// This program is free software; you can redistribute it and/or //
|
9 |
|
|
// modify it under the terms of the GNU General Public License //
|
10 |
|
|
// as published by the Free Software Foundation; either version 2 //
|
11 |
|
|
// of the License, or (at your option) any later version. //
|
12 |
|
|
// //
|
13 |
|
|
// This program is distributed in the hope that it will be useful, //
|
14 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
15 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
16 |
|
|
// GNU General Public License for more details. //
|
17 |
|
|
// //
|
18 |
|
|
// If you have not received a copy of the GNU General Public License//
|
19 |
|
|
// along with this program; write to: //
|
20 |
|
|
// Free Software Foundation, Inc., //
|
21 |
|
|
// 51 Franklin Street, Fifth Floor, //
|
22 |
|
|
// Boston, MA 02110-1301, USA. //
|
23 |
|
|
// //
|
24 |
|
|
//------------------------------------------------------------------//
|
25 |
|
|
// Revisions: //
|
26 |
|
|
// Ver 0.0.0.1 Apr 28, 2005 Under Development //
|
27 |
|
|
// //
|
28 |
|
|
//==================================================================//
|
29 |
2 |
smpickett |
|
30 |
|
|
module Driver_mouse(
|
31 |
|
|
CLK_50MHZ, MASTER_RST,
|
32 |
|
|
PS2C, PS2D,
|
33 |
|
|
XCOORD, YCOORD,
|
34 |
5 |
smpickett |
L_BUTTON, R_BUTTON, M_BUTTON
|
35 |
2 |
smpickett |
);
|
36 |
|
|
|
37 |
|
|
//==================================================================//
|
38 |
|
|
// DEFINITIONS //
|
39 |
|
|
//==================================================================//
|
40 |
|
|
parameter ss_CLK_LOW_100US = 4'b0000;
|
41 |
|
|
parameter ss_DATA_LOW = 4'b0001;
|
42 |
|
|
parameter ss_SET_BIT_0 = 4'b0011;
|
43 |
|
|
parameter ss_SET_BIT_1 = 4'b0010;
|
44 |
|
|
parameter ss_SET_BIT_2 = 4'b0110;
|
45 |
|
|
parameter ss_SET_BIT_3 = 4'b0111;
|
46 |
|
|
parameter ss_SET_BIT_4 = 4'b0101;
|
47 |
|
|
parameter ss_SET_BIT_5 = 4'b0100;
|
48 |
|
|
parameter ss_SET_BIT_6 = 4'b1100;
|
49 |
|
|
parameter ss_SET_BIT_7 = 4'b1101;
|
50 |
|
|
parameter ss_SET_BIT_PARITY = 4'b1111;
|
51 |
|
|
parameter ss_SET_BIT_STOP = 4'b1110;
|
52 |
|
|
parameter ss_WAIT_BIT_ACK = 4'b1010;
|
53 |
|
|
parameter ss_GET_MOVEMENT = 4'b1000;
|
54 |
|
|
|
55 |
|
|
parameter P_Lbut_index = 1;
|
56 |
|
|
parameter P_Mbut_index = 2;
|
57 |
|
|
parameter P_Rbut_index = 3;
|
58 |
|
|
|
59 |
|
|
|
60 |
|
|
//==================================================================//
|
61 |
|
|
// VARIABLE DEFINITIONS //
|
62 |
|
|
//==================================================================//
|
63 |
|
|
//----------------------//
|
64 |
|
|
// INPUTS //
|
65 |
|
|
//----------------------//
|
66 |
|
|
input CLK_50MHZ; // System wide clock
|
67 |
|
|
input MASTER_RST; // System wide reset
|
68 |
|
|
inout PS2C; // PS2 clock
|
69 |
|
|
inout PS2D; // PS2 data
|
70 |
|
|
|
71 |
|
|
//----------------------//
|
72 |
|
|
// OUTPUTS //
|
73 |
|
|
//----------------------//
|
74 |
|
|
output[11:0] XCOORD; // X coordinate of the cursor
|
75 |
|
|
output[11:0] YCOORD; // Y coordinate of the cursor
|
76 |
|
|
output L_BUTTON, R_BUTTON, M_BUTTON;
|
77 |
|
|
|
78 |
|
|
//----------------------//
|
79 |
|
|
// WIRES / NODES //
|
80 |
|
|
//----------------------//
|
81 |
|
|
wire CLK_50MHZ, MASTER_RST;
|
82 |
|
|
wire PS2C, PS2D;
|
83 |
|
|
reg[11:0] XCOORD;
|
84 |
|
|
reg[11:0] YCOORD;
|
85 |
|
|
reg L_BUTTON, R_BUTTON, M_BUTTON;
|
86 |
|
|
|
87 |
|
|
//----------------------//
|
88 |
|
|
// REGISTERS //
|
89 |
|
|
//----------------------//
|
90 |
|
|
reg[12:0] Counter_timer;
|
91 |
|
|
reg[5:0] Counter_bits;
|
92 |
|
|
reg[3:0] sm_ps2mouse;
|
93 |
|
|
reg[32:0] data_in_buf;
|
94 |
|
|
|
95 |
|
|
|
96 |
|
|
|
97 |
|
|
|
98 |
|
|
//==================================================================//
|
99 |
|
|
// FUNCTIONAL DEFINITIONS //
|
100 |
|
|
//==================================================================//
|
101 |
|
|
|
102 |
|
|
//------------------------------------------------------------------//
|
103 |
|
|
// INTERMEDIATE VALUES //
|
104 |
|
|
//------------------------------------------------------------------//
|
105 |
|
|
reg[7:0] Counter_PS2C;
|
106 |
|
|
reg CLK_ps2c_debounced;
|
107 |
|
|
|
108 |
|
|
// Debounce the PS2C line.
|
109 |
|
|
// The mouse is generally not outputting a nice rising clock edge.
|
110 |
|
|
// To eliminate the false edge detection, make sure it is high/low
|
111 |
27 |
smpickett |
// for at least 256 counts (5.12us off 50MHz) before triggering the CLK.
|
112 |
2 |
smpickett |
always @ (posedge CLK_50MHZ or posedge MASTER_RST) begin
|
113 |
|
|
if(MASTER_RST == 1'b1) begin
|
114 |
|
|
Counter_PS2C <= 8'b0;
|
115 |
|
|
end else begin
|
116 |
|
|
if(PS2C == 1'b1) begin
|
117 |
|
|
if(Counter_PS2C == 8'hFF)
|
118 |
|
|
Counter_PS2C <= Counter_PS2C;
|
119 |
|
|
else
|
120 |
|
|
Counter_PS2C <= Counter_PS2C + 1;
|
121 |
|
|
end else begin
|
122 |
|
|
if(Counter_PS2C == 8'b0)
|
123 |
|
|
Counter_PS2C <= Counter_PS2C;
|
124 |
|
|
else
|
125 |
|
|
Counter_PS2C <= Counter_PS2C - 1;
|
126 |
|
|
end
|
127 |
|
|
end
|
128 |
|
|
end
|
129 |
|
|
|
130 |
|
|
always @ (posedge CLK_50MHZ or posedge MASTER_RST) begin
|
131 |
|
|
if(MASTER_RST == 1'b1)
|
132 |
|
|
CLK_ps2c_debounced <= 1'b0;
|
133 |
|
|
else if(Counter_PS2C == 8'b0)
|
134 |
|
|
CLK_ps2c_debounced <= 1'b0;
|
135 |
|
|
else if(Counter_PS2C == 8'hFF)
|
136 |
|
|
CLK_ps2c_debounced <= 1'b1;
|
137 |
|
|
else
|
138 |
|
|
CLK_ps2c_debounced <= CLK_ps2c_debounced;
|
139 |
|
|
end
|
140 |
|
|
|
141 |
|
|
|
142 |
|
|
//------------------------------------------------------------------//
|
143 |
|
|
// INTERPRETING MOVEMENTS //
|
144 |
|
|
//------------------------------------------------------------------//
|
145 |
|
|
reg[7:0] xcoord_buf;
|
146 |
|
|
reg[7:0] ycoord_buf;
|
147 |
|
|
|
148 |
|
|
always @ (posedge CLK_50MHZ or posedge MASTER_RST) begin
|
149 |
|
|
if(MASTER_RST == 1'b1) begin
|
150 |
|
|
xcoord_buf <= 8'b0;
|
151 |
|
|
end else if(data_in_buf[5] == 1'b0) begin
|
152 |
|
|
xcoord_buf <= data_in_buf[19:12];
|
153 |
|
|
end else begin
|
154 |
|
|
xcoord_buf <= ((~(data_in_buf[19:12]))+1);
|
155 |
|
|
end
|
156 |
|
|
end
|
157 |
|
|
|
158 |
|
|
always @ (posedge CLK_50MHZ or posedge MASTER_RST) begin
|
159 |
|
|
if(MASTER_RST == 1'b1) begin
|
160 |
|
|
ycoord_buf <= 8'b0;
|
161 |
|
|
end else if(data_in_buf[6] == 1'b0) begin
|
162 |
|
|
ycoord_buf <= data_in_buf[30:23];
|
163 |
|
|
end else begin
|
164 |
|
|
ycoord_buf <= ((~(data_in_buf[30:23]))+1);
|
165 |
|
|
end
|
166 |
|
|
end
|
167 |
|
|
|
168 |
|
|
|
169 |
|
|
always @ (posedge CLK_ps2c_debounced or posedge MASTER_RST) begin
|
170 |
|
|
if(MASTER_RST == 1'b1) begin
|
171 |
|
|
XCOORD <= 12'd320;
|
172 |
|
|
end else if(Counter_bits == 6'd32 && (data_in_buf[7] == 1'b0)) begin
|
173 |
|
|
if(data_in_buf[5] == 1'b1) begin // NEGITIVE
|
174 |
|
|
if(XCOORD <= xcoord_buf)
|
175 |
|
|
XCOORD <= 12'b0;
|
176 |
|
|
else
|
177 |
|
|
XCOORD <= XCOORD - xcoord_buf;
|
178 |
|
|
end else begin // POSITIVE
|
179 |
27 |
smpickett |
if((XCOORD + xcoord_buf) >= 12'd639)
|
180 |
2 |
smpickett |
XCOORD <= 12'd639;
|
181 |
|
|
else
|
182 |
|
|
XCOORD <= XCOORD + xcoord_buf;
|
183 |
|
|
end
|
184 |
|
|
end else begin
|
185 |
|
|
XCOORD <= XCOORD;
|
186 |
|
|
end
|
187 |
|
|
end
|
188 |
|
|
|
189 |
|
|
always @ (posedge CLK_ps2c_debounced or posedge MASTER_RST) begin
|
190 |
|
|
if(MASTER_RST == 1'b1) begin
|
191 |
11 |
smpickett |
YCOORD <= 12'd199;
|
192 |
2 |
smpickett |
end else if(Counter_bits == 6'd32 && (data_in_buf[8] == 1'b0)) begin
|
193 |
11 |
smpickett |
if(data_in_buf[6] == 1'b0) begin
|
194 |
27 |
smpickett |
if( (YCOORD < 12'd401) && ((YCOORD + ycoord_buf) >= 12'd401) )
|
195 |
|
|
YCOORD <= 12'd400;
|
196 |
|
|
else if( ((YCOORD >= 12'd441) /*&& (YCOORD <= 12'd520)*/) && ((YCOORD + ycoord_buf) > 12'd520) )
|
197 |
|
|
YCOORD <= (YCOORD + ycoord_buf) - 12'd521;
|
198 |
2 |
smpickett |
else
|
199 |
|
|
YCOORD <= YCOORD + ycoord_buf;
|
200 |
11 |
smpickett |
end else begin
|
201 |
27 |
smpickett |
if( /*(YCOORD < 12'd401) &&*/ (YCOORD < ycoord_buf) )
|
202 |
|
|
YCOORD <= 12'd521 - ycoord_buf;
|
203 |
|
|
else if( (YCOORD >= 12'd441) && ((YCOORD-12'd441) < ycoord_buf) )
|
204 |
|
|
YCOORD <= 12'd441;
|
205 |
2 |
smpickett |
else
|
206 |
|
|
YCOORD <= YCOORD - ycoord_buf;
|
207 |
|
|
end
|
208 |
|
|
end else begin
|
209 |
|
|
YCOORD <= YCOORD;
|
210 |
|
|
end
|
211 |
|
|
end
|
212 |
|
|
|
213 |
|
|
//------------------------------------------------------------------//
|
214 |
|
|
// INTERPRETING BUTTONS //
|
215 |
|
|
//------------------------------------------------------------------//
|
216 |
|
|
always @ (posedge CLK_50MHZ or posedge MASTER_RST) begin
|
217 |
|
|
if(MASTER_RST == 1'b1) begin
|
218 |
|
|
L_BUTTON <= 1'b0;
|
219 |
|
|
M_BUTTON <= 1'b0;
|
220 |
|
|
R_BUTTON <= 1'b0;
|
221 |
|
|
end else if(Counter_bits == 6'd32) begin
|
222 |
|
|
L_BUTTON <= data_in_buf[P_Lbut_index];
|
223 |
|
|
M_BUTTON <= data_in_buf[P_Mbut_index];
|
224 |
|
|
R_BUTTON <= data_in_buf[P_Rbut_index];
|
225 |
|
|
end else begin
|
226 |
|
|
L_BUTTON <= L_BUTTON;
|
227 |
|
|
M_BUTTON <= M_BUTTON;
|
228 |
|
|
R_BUTTON <= R_BUTTON;
|
229 |
|
|
end
|
230 |
|
|
end
|
231 |
|
|
|
232 |
|
|
|
233 |
|
|
|
234 |
|
|
|
235 |
|
|
//------------------------------------------------------------------//
|
236 |
|
|
// SENDING DATA //
|
237 |
|
|
//------------------------------------------------------------------//
|
238 |
|
|
reg PS2C_out, PS2D_out;
|
239 |
|
|
|
240 |
|
|
assign PS2C = PS2C_out;
|
241 |
|
|
assign PS2D = PS2D_out;
|
242 |
|
|
|
243 |
|
|
|
244 |
|
|
always @ (Counter_timer or MASTER_RST) begin
|
245 |
|
|
if(MASTER_RST == 1'b1) begin
|
246 |
|
|
PS2C_out = 1'bZ;
|
247 |
|
|
end else if((Counter_timer <= 13'd5500) && (MASTER_RST == 1'b0))
|
248 |
|
|
PS2C_out = 1'b0;
|
249 |
|
|
else
|
250 |
|
|
PS2C_out = 1'bZ;
|
251 |
|
|
end
|
252 |
|
|
|
253 |
|
|
always @ (sm_ps2mouse or Counter_timer or MASTER_RST) begin
|
254 |
|
|
if(MASTER_RST == 1'b1) begin
|
255 |
|
|
PS2D_out = 1'bZ;
|
256 |
|
|
end else if(Counter_timer >= 13'd5000 && sm_ps2mouse == ss_DATA_LOW) begin
|
257 |
|
|
PS2D_out = 1'b0;
|
258 |
|
|
end else if(sm_ps2mouse == ss_SET_BIT_0) begin
|
259 |
|
|
PS2D_out = 1'b0;
|
260 |
|
|
end else if(sm_ps2mouse == ss_SET_BIT_1) begin
|
261 |
|
|
PS2D_out = 1'b0;
|
262 |
|
|
end else if(sm_ps2mouse == ss_SET_BIT_2) begin
|
263 |
|
|
PS2D_out = 1'b1;
|
264 |
|
|
end else if(sm_ps2mouse == ss_SET_BIT_3) begin
|
265 |
|
|
PS2D_out = 1'b0;
|
266 |
|
|
end else if(sm_ps2mouse == ss_SET_BIT_4) begin
|
267 |
|
|
PS2D_out = 1'b1;
|
268 |
|
|
end else if(sm_ps2mouse == ss_SET_BIT_5) begin
|
269 |
|
|
PS2D_out = 1'b1;
|
270 |
|
|
end else if(sm_ps2mouse == ss_SET_BIT_6) begin
|
271 |
|
|
PS2D_out = 1'b1;
|
272 |
|
|
end else if(sm_ps2mouse == ss_SET_BIT_7) begin
|
273 |
|
|
PS2D_out = 1'b1;
|
274 |
|
|
end else if(sm_ps2mouse == ss_SET_BIT_PARITY) begin
|
275 |
|
|
PS2D_out = 1'b0;
|
276 |
|
|
end else if(sm_ps2mouse == ss_SET_BIT_STOP) begin
|
277 |
|
|
PS2D_out = 1'b1;
|
278 |
|
|
end else begin
|
279 |
|
|
PS2D_out = 1'bZ;
|
280 |
|
|
end
|
281 |
|
|
end
|
282 |
|
|
|
283 |
|
|
//------------------------------------------------------------------//
|
284 |
|
|
// RECIEVING DATA //
|
285 |
|
|
//------------------------------------------------------------------//
|
286 |
|
|
always @ (negedge CLK_ps2c_debounced or posedge MASTER_RST) begin
|
287 |
|
|
if(MASTER_RST == 1'b1) begin
|
288 |
|
|
data_in_buf <= 33'b0;
|
289 |
|
|
end else if(sm_ps2mouse == ss_GET_MOVEMENT) begin
|
290 |
|
|
data_in_buf <= data_in_buf >> 1;
|
291 |
|
|
data_in_buf[32] <= PS2D;
|
292 |
|
|
end else
|
293 |
|
|
data_in_buf <= data_in_buf;
|
294 |
|
|
end
|
295 |
|
|
|
296 |
|
|
|
297 |
|
|
|
298 |
|
|
//------------------------------------------------------------------//
|
299 |
|
|
// COUNTERS FOR STATE MACHINE //
|
300 |
|
|
//------------------------------------------------------------------//
|
301 |
|
|
// COUNTER: timer
|
302 |
|
|
always @ (posedge CLK_50MHZ or posedge MASTER_RST) begin
|
303 |
|
|
if(MASTER_RST == 1'b1)
|
304 |
|
|
Counter_timer <= 13'b0;
|
305 |
|
|
else if(Counter_timer == 13'd6000)
|
306 |
|
|
Counter_timer <= Counter_timer;
|
307 |
|
|
else
|
308 |
|
|
Counter_timer <= Counter_timer + 1;
|
309 |
|
|
end
|
310 |
|
|
|
311 |
|
|
// COUNTER: rec_data_bit_cnt
|
312 |
|
|
always @ (negedge CLK_ps2c_debounced or posedge MASTER_RST) begin
|
313 |
|
|
if(MASTER_RST == 1'b1) begin
|
314 |
|
|
Counter_bits <= 6'd22;
|
315 |
|
|
end else if(sm_ps2mouse == ss_GET_MOVEMENT) begin
|
316 |
|
|
if(Counter_bits == 6'd32)
|
317 |
|
|
Counter_bits <= 6'd0;
|
318 |
|
|
else
|
319 |
|
|
Counter_bits <= Counter_bits + 1;
|
320 |
|
|
end else begin
|
321 |
|
|
Counter_bits <= Counter_bits;
|
322 |
|
|
end
|
323 |
|
|
end
|
324 |
|
|
|
325 |
|
|
|
326 |
|
|
//------------------------------------------------------------------//
|
327 |
|
|
// MOUSE STATE MACHINE //
|
328 |
|
|
//------------------------------------------------------------------//
|
329 |
|
|
always @ (negedge CLK_ps2c_debounced or posedge MASTER_RST) begin
|
330 |
|
|
if(MASTER_RST == 1'b1) begin
|
331 |
|
|
sm_ps2mouse <= ss_DATA_LOW;
|
332 |
|
|
end else if(sm_ps2mouse == ss_DATA_LOW) begin
|
333 |
|
|
sm_ps2mouse <= ss_SET_BIT_0;
|
334 |
|
|
end else if(sm_ps2mouse == ss_SET_BIT_0) begin
|
335 |
|
|
sm_ps2mouse <= ss_SET_BIT_1;
|
336 |
|
|
end else if(sm_ps2mouse == ss_SET_BIT_1) begin
|
337 |
|
|
sm_ps2mouse <= ss_SET_BIT_2;
|
338 |
|
|
end else if(sm_ps2mouse == ss_SET_BIT_2) begin
|
339 |
|
|
sm_ps2mouse <= ss_SET_BIT_3;
|
340 |
|
|
end else if(sm_ps2mouse == ss_SET_BIT_3) begin
|
341 |
|
|
sm_ps2mouse <= ss_SET_BIT_4;
|
342 |
|
|
end else if(sm_ps2mouse == ss_SET_BIT_4) begin
|
343 |
|
|
sm_ps2mouse <= ss_SET_BIT_5;
|
344 |
|
|
end else if(sm_ps2mouse == ss_SET_BIT_5) begin
|
345 |
|
|
sm_ps2mouse <= ss_SET_BIT_6;
|
346 |
|
|
end else if(sm_ps2mouse == ss_SET_BIT_6) begin
|
347 |
|
|
sm_ps2mouse <= ss_SET_BIT_7;
|
348 |
|
|
end else if(sm_ps2mouse == ss_SET_BIT_7) begin
|
349 |
|
|
sm_ps2mouse <= ss_SET_BIT_PARITY;
|
350 |
|
|
end else if(sm_ps2mouse == ss_SET_BIT_PARITY) begin
|
351 |
|
|
sm_ps2mouse <= ss_SET_BIT_STOP;
|
352 |
|
|
end else if(sm_ps2mouse == ss_SET_BIT_STOP) begin
|
353 |
|
|
sm_ps2mouse <= ss_WAIT_BIT_ACK;
|
354 |
|
|
end else if(sm_ps2mouse == ss_WAIT_BIT_ACK) begin
|
355 |
|
|
sm_ps2mouse <= ss_GET_MOVEMENT;
|
356 |
|
|
end else if(sm_ps2mouse == ss_GET_MOVEMENT) begin
|
357 |
|
|
sm_ps2mouse <= sm_ps2mouse;
|
358 |
|
|
end else begin
|
359 |
|
|
sm_ps2mouse <= ss_DATA_LOW;
|
360 |
|
|
end
|
361 |
|
|
end
|
362 |
|
|
|
363 |
|
|
|
364 |
|
|
|
365 |
|
|
|
366 |
|
|
|
367 |
|
|
|
368 |
|
|
|
369 |
|
|
|
370 |
|
|
|
371 |
|
|
|
372 |
|
|
|
373 |
|
|
|
374 |
|
|
|
375 |
|
|
|
376 |
|
|
|
377 |
|
|
|
378 |
|
|
|
379 |
|
|
endmodule
|
380 |
|
|
|