1 |
53 |
root |
//-------------------------------------------------------------------------------------
|
2 |
|
|
//
|
3 |
|
|
// Author: John Clayton
|
4 |
|
|
// Date : April 30, 2001
|
5 |
|
|
// Update: 6/06/01 copied this file from ps2.v (pared down).
|
6 |
|
|
// Update: 6/07/01 Finished initial coding efforts.
|
7 |
|
|
// Update: 6/09/01 Made minor changes to state machines during debugging.
|
8 |
|
|
// Fixed errors in state transitions. Added state to m2
|
9 |
|
|
// so that "reset" causes the mouse to be initialized.
|
10 |
|
|
// Removed debug port.
|
11 |
|
|
//
|
12 |
|
|
//
|
13 |
|
|
//
|
14 |
|
|
//
|
15 |
|
|
//
|
16 |
|
|
// Description
|
17 |
|
|
//-------------------------------------------------------------------------------------
|
18 |
|
|
// This is a state-machine driven serial-to-parallel and parallel-to-serial
|
19 |
|
|
// interface to the ps2 style mouse. The state diagram for part of the
|
20 |
|
|
// m2 state machine was obtained from the work of Rob Chapman, as published
|
21 |
|
|
// at:
|
22 |
|
|
// www.ee.ualberta.ca/~elliott/ee552/studentAppNotes/1998_w/mouse_notes.html
|
23 |
|
|
//
|
24 |
|
|
//
|
25 |
|
|
// Some aspects of the mouse interface are not implemented (e.g, verifying
|
26 |
|
|
// the FA response code from the mouse when enabling streaming mode.)
|
27 |
|
|
// However, the mouse interface was designed so that "hot plugging" a mouse
|
28 |
|
|
// into the connector should cause the interface to send the F4 code to the
|
29 |
|
|
// mouse in order to enable streaming. By this means, the mouse begins to
|
30 |
|
|
// operate, and no reset pulse should be needed.
|
31 |
|
|
//
|
32 |
|
|
// Similarly, there is a "watchdog" timer implemented, so that during periods
|
33 |
|
|
// of inactivity, the bit_count is cleared to zero. Therefore, the effects of
|
34 |
|
|
// a bad count value are corrected, and internal errors of that type are not
|
35 |
|
|
// propagated into subsequent packet receive operations.
|
36 |
|
|
//
|
37 |
|
|
// To enable the streaming mode, F4 is sent to the mouse.
|
38 |
|
|
// The mouse responds with FA to acknowledge the command, and then enters
|
39 |
|
|
// streaming mode at the default rate of 100 packets per second (transmission
|
40 |
|
|
// of packets ceases when the activity at the mouse is not longer sensed.)
|
41 |
|
|
//
|
42 |
|
|
// There are additional commands to change the sampling rate and resolution
|
43 |
|
|
// of the mouse reported data. Those commands are not implemented here.
|
44 |
|
|
// (E8,XX = set resolution 0,1,2,3)
|
45 |
|
|
// (E7 = set scaling 2:1)
|
46 |
|
|
// (E6 = reset scaling)
|
47 |
|
|
// (F3,XX = set sampling rate to XX packets per second.)
|
48 |
|
|
//
|
49 |
|
|
// At this time I do not know any of the command related to using the
|
50 |
|
|
// wheel of a "wheel mouse."
|
51 |
|
|
//
|
52 |
|
|
// The packets consists of three bytes transmitted in sequence. The interval
|
53 |
|
|
// between these bytes has been measured on two different mice, and found to
|
54 |
|
|
// be different. On the slower (older) mouse it was approximately 345
|
55 |
|
|
// microseconds, while on a newer "wheel" mouse it was approximately 125
|
56 |
|
|
// microseconds. The watchdog timer is designed to cause processing of a
|
57 |
|
|
// complete packet when it expires. Therefore, the watchdog timer must last
|
58 |
|
|
// for longer than the "inter-byte delay" between bytes of the packet.
|
59 |
|
|
// I have set the default timer value to 400 usec, for my 49.152 MHz clock.
|
60 |
|
|
// The timer value and size of the timer counter is settable by parameters,
|
61 |
|
|
// so that other clock frequencies and settings may be used. The setting for
|
62 |
|
|
// the watchdog timeout is not critical -- it only needs to be greater than
|
63 |
|
|
// the inter-byte delay as data is transmitted from the mouse, and no less
|
64 |
|
|
// than 60usec.
|
65 |
|
|
//
|
66 |
|
|
// Each "byte" of the packet is transmitted from the mouse as follows:
|
67 |
|
|
//
|
68 |
|
|
// 1 start bit, 8 data bits, 1 odd parity bit, 1 stop bit. == 11 bits total.
|
69 |
|
|
// (The data bits are sent LSB first)
|
70 |
|
|
//
|
71 |
|
|
// The data bits are formatted as follows:
|
72 |
|
|
//
|
73 |
|
|
// byte 0: YV, XV, YS, XS, 1, 0, R, L
|
74 |
|
|
// byte 1: X7..X0
|
75 |
|
|
// byte 2: Y7..Y0
|
76 |
|
|
//
|
77 |
|
|
// Where YV, XV are set to indicate overflow conditions.
|
78 |
|
|
// XS, YS are set to indicate negative quantities (sign bits).
|
79 |
|
|
// R, L are set to indicate buttons pressed, left and right.
|
80 |
|
|
//
|
81 |
|
|
//
|
82 |
|
|
//
|
83 |
|
|
// The interface to the ps2 mouse (like the keyboard) uses clock rates of
|
84 |
|
|
// 30-40 kHz, dependent upon the mouse itself. The mouse generates the
|
85 |
|
|
// clock.
|
86 |
|
|
// The rate at which the state machine runs should be at least twice the
|
87 |
|
|
// rate of the ps2_clk, so that the states can accurately follow the clock
|
88 |
|
|
// signal itself. Four times oversampling is better. Say 200kHz at least.
|
89 |
|
|
// In order to run the state machine extremely fast, synchronizing flip-flops
|
90 |
|
|
// have been added to the ps2_clk and ps2_data inputs of the state machine.
|
91 |
|
|
// This avoids poor performance related to slow transitions of the inputs.
|
92 |
|
|
//
|
93 |
|
|
// Because this is a bi-directional interface, while reading from the mouse
|
94 |
|
|
// the ps2_clk and ps2_data lines are used as inputs. While writing to the
|
95 |
|
|
// mouse, however (which is done when a "packet" of less than 33 bits is
|
96 |
|
|
// received), both the ps2_clk and ps2_data lines are sometime pulled low by
|
97 |
|
|
// this interface. As such, they are bidirectional, and pullups are used to
|
98 |
|
|
// return them to the "high" state, whenever the drivers are set to the
|
99 |
|
|
// high impedance state.
|
100 |
|
|
//
|
101 |
|
|
// Pullups MUST BE USED on the ps2_clk and ps2_data lines for this design,
|
102 |
|
|
// whether they be internal to an FPGA I/O pad, or externally placed.
|
103 |
|
|
// If internal pullups are used, they may be fairly weak, causing bounces
|
104 |
|
|
// due to crosstalk, etc. There is a "debounce timer" implemented in order
|
105 |
|
|
// to eliminate erroneous state transitions which would occur based on bounce.
|
106 |
|
|
// Parameters are provided to configure the debounce timer for different
|
107 |
|
|
// clock frequencies. 2 or 3 microseconds of debounce should be plenty.
|
108 |
|
|
// You may possibly use much less, if your pullups are strong.
|
109 |
|
|
//
|
110 |
|
|
// A parameters is provided to configure a 60 microsecond period used while
|
111 |
|
|
// transmitting to the mouse. The 60 microsecond period is guaranteed to be
|
112 |
|
|
// more than one period of the ps2_clk signal.
|
113 |
|
|
//
|
114 |
|
|
//
|
115 |
|
|
//-------------------------------------------------------------------------------------
|
116 |
|
|
|
117 |
|
|
|
118 |
|
|
`resetall
|
119 |
|
|
`timescale 1ns/100ps
|
120 |
|
|
|
121 |
|
|
`define TOTAL_BITS 33 // Number of bits in one full packet
|
122 |
|
|
|
123 |
|
|
|
124 |
|
|
module ps2_mouse_interface (
|
125 |
|
|
clk,
|
126 |
|
|
reset,
|
127 |
|
|
ps2_clk,
|
128 |
|
|
ps2_data,
|
129 |
|
|
left_button,
|
130 |
|
|
right_button,
|
131 |
|
|
x_increment,
|
132 |
|
|
y_increment,
|
133 |
|
|
data_ready, // rx_read_o
|
134 |
|
|
read, // rx_read_ack_i
|
135 |
|
|
error_no_ack
|
136 |
|
|
);
|
137 |
|
|
|
138 |
|
|
// Parameters
|
139 |
|
|
|
140 |
|
|
// The timer value can be up to (2^bits) inclusive.
|
141 |
|
|
parameter WATCHDOG_TIMER_VALUE_PP = 19660; // Number of sys_clks for 400usec.
|
142 |
|
|
parameter WATCHDOG_TIMER_BITS_PP = 15; // Number of bits needed for timer
|
143 |
|
|
parameter DEBOUNCE_TIMER_VALUE_PP = 186; // Number of sys_clks for debounce
|
144 |
|
|
parameter DEBOUNCE_TIMER_BITS_PP = 8; // Number of bits needed for timer
|
145 |
|
|
|
146 |
|
|
|
147 |
|
|
// State encodings, provided as parameters
|
148 |
|
|
// for flexibility to the one instantiating the module.
|
149 |
|
|
// In general, the default values need not be changed.
|
150 |
|
|
|
151 |
|
|
// There are three state machines: m1, m2 and m3.
|
152 |
|
|
// States chosen as "default" states upon power-up and configuration:
|
153 |
|
|
// "m1_clk_h"
|
154 |
|
|
// "m2_wait"
|
155 |
|
|
// "m3_data_ready_ack"
|
156 |
|
|
|
157 |
|
|
parameter m1_clk_h = 0;
|
158 |
|
|
parameter m1_falling_edge = 1;
|
159 |
|
|
parameter m1_falling_wait = 3;
|
160 |
|
|
parameter m1_clk_l = 2;
|
161 |
|
|
parameter m1_rising_edge = 6;
|
162 |
|
|
parameter m1_rising_wait = 4;
|
163 |
|
|
|
164 |
|
|
parameter m2_reset = 14;
|
165 |
|
|
parameter m2_wait = 0;
|
166 |
|
|
parameter m2_gather = 1;
|
167 |
|
|
parameter m2_verify = 3;
|
168 |
|
|
parameter m2_use = 2;
|
169 |
|
|
parameter m2_hold_clk_l = 6;
|
170 |
|
|
parameter m2_data_low_1 = 4;
|
171 |
|
|
parameter m2_data_high_1 = 5;
|
172 |
|
|
parameter m2_data_low_2 = 7;
|
173 |
|
|
parameter m2_data_high_2 = 8;
|
174 |
|
|
parameter m2_data_low_3 = 9;
|
175 |
|
|
parameter m2_data_high_3 = 11;
|
176 |
|
|
parameter m2_error_no_ack = 15;
|
177 |
|
|
parameter m2_await_response = 10;
|
178 |
|
|
|
179 |
|
|
parameter m3_data_ready = 1;
|
180 |
|
|
parameter m3_data_ready_ack = 0;
|
181 |
|
|
|
182 |
|
|
// I/O declarations
|
183 |
|
|
input clk;
|
184 |
|
|
input reset;
|
185 |
|
|
inout ps2_clk;
|
186 |
|
|
inout ps2_data;
|
187 |
|
|
output left_button;
|
188 |
|
|
output right_button;
|
189 |
|
|
output [8:0] x_increment;
|
190 |
|
|
output [8:0] y_increment;
|
191 |
|
|
output data_ready;
|
192 |
|
|
input read;
|
193 |
|
|
output error_no_ack;
|
194 |
|
|
|
195 |
|
|
reg left_button;
|
196 |
|
|
reg right_button;
|
197 |
|
|
reg [8:0] x_increment;
|
198 |
|
|
reg [8:0] y_increment;
|
199 |
|
|
reg data_ready;
|
200 |
|
|
reg error_no_ack;
|
201 |
|
|
|
202 |
|
|
// Internal signal declarations
|
203 |
|
|
wire watchdog_timer_done;
|
204 |
|
|
wire debounce_timer_done;
|
205 |
|
|
wire packet_good;
|
206 |
|
|
|
207 |
|
|
reg [`TOTAL_BITS-1:0] q; // Shift register
|
208 |
|
|
reg [2:0] m1_state;
|
209 |
|
|
reg [2:0] m1_next_state;
|
210 |
|
|
reg [3:0] m2_state;
|
211 |
|
|
reg [3:0] m2_next_state;
|
212 |
|
|
reg m3_state;
|
213 |
|
|
reg m3_next_state;
|
214 |
|
|
reg [5:0] bit_count; // Bit counter
|
215 |
|
|
reg [WATCHDOG_TIMER_BITS_PP-1:0] watchdog_timer_count;
|
216 |
|
|
reg [DEBOUNCE_TIMER_BITS_PP-1:0] debounce_timer_count;
|
217 |
|
|
reg ps2_clk_hi_z; // Without keyboard, high Z equals 1 due to pullups.
|
218 |
|
|
reg ps2_data_hi_z; // Without keyboard, high Z equals 1 due to pullups.
|
219 |
|
|
reg clean_clk; // Debounced output from m1, follows ps2_clk.
|
220 |
|
|
reg rising_edge; // Output from m1 state machine.
|
221 |
|
|
reg falling_edge; // Output from m1 state machine.
|
222 |
|
|
reg output_strobe; // Latches data data into the output registers
|
223 |
|
|
|
224 |
|
|
//--------------------------------------------------------------------------
|
225 |
|
|
// Module code
|
226 |
|
|
|
227 |
|
|
assign ps2_clk = ps2_clk_hi_z?1'bZ:1'b0;
|
228 |
|
|
assign ps2_data = ps2_data_hi_z?1'bZ:1'b0;
|
229 |
|
|
|
230 |
|
|
// State register
|
231 |
|
|
always @(posedge clk)
|
232 |
|
|
begin : m1_state_register
|
233 |
|
|
if (reset) m1_state <= m1_clk_h;
|
234 |
|
|
else m1_state <= m1_next_state;
|
235 |
|
|
end
|
236 |
|
|
|
237 |
|
|
// State transition logic
|
238 |
|
|
always @(m1_state
|
239 |
|
|
or ps2_clk
|
240 |
|
|
or debounce_timer_done
|
241 |
|
|
or watchdog_timer_done
|
242 |
|
|
)
|
243 |
|
|
begin : m1_state_logic
|
244 |
|
|
|
245 |
|
|
// Output signals default to this value, unless changed in a state condition.
|
246 |
|
|
clean_clk <= 0;
|
247 |
|
|
rising_edge <= 0;
|
248 |
|
|
falling_edge <= 0;
|
249 |
|
|
|
250 |
|
|
case (m1_state)
|
251 |
|
|
m1_clk_h :
|
252 |
|
|
begin
|
253 |
|
|
clean_clk <= 1;
|
254 |
|
|
if (~ps2_clk) m1_next_state <= m1_falling_edge;
|
255 |
|
|
else m1_next_state <= m1_clk_h;
|
256 |
|
|
end
|
257 |
|
|
|
258 |
|
|
m1_falling_edge :
|
259 |
|
|
begin
|
260 |
|
|
falling_edge <= 1;
|
261 |
|
|
m1_next_state <= m1_falling_wait;
|
262 |
|
|
end
|
263 |
|
|
|
264 |
|
|
m1_falling_wait :
|
265 |
|
|
begin
|
266 |
|
|
if (debounce_timer_done) m1_next_state <= m1_clk_l;
|
267 |
|
|
else m1_next_state <= m1_falling_wait;
|
268 |
|
|
end
|
269 |
|
|
|
270 |
|
|
m1_clk_l :
|
271 |
|
|
begin
|
272 |
|
|
if (ps2_clk) m1_next_state <= m1_rising_edge;
|
273 |
|
|
else m1_next_state <= m1_clk_l;
|
274 |
|
|
end
|
275 |
|
|
|
276 |
|
|
m1_rising_edge :
|
277 |
|
|
begin
|
278 |
|
|
rising_edge <= 1;
|
279 |
|
|
m1_next_state <= m1_rising_wait;
|
280 |
|
|
end
|
281 |
|
|
|
282 |
|
|
m1_rising_wait :
|
283 |
|
|
begin
|
284 |
|
|
clean_clk <= 1;
|
285 |
|
|
if (debounce_timer_done) m1_next_state <= m1_clk_h;
|
286 |
|
|
else m1_next_state <= m1_rising_wait;
|
287 |
|
|
end
|
288 |
|
|
default : m1_next_state <= m1_clk_h;
|
289 |
|
|
endcase
|
290 |
|
|
end
|
291 |
|
|
|
292 |
|
|
|
293 |
|
|
// State register
|
294 |
|
|
always @(posedge clk)
|
295 |
|
|
begin : m2_state_register
|
296 |
|
|
if (reset) m2_state <= m2_reset;
|
297 |
|
|
else m2_state <= m2_next_state;
|
298 |
|
|
end
|
299 |
|
|
|
300 |
|
|
// State transition logic
|
301 |
|
|
always @(m2_state
|
302 |
|
|
or q
|
303 |
|
|
or falling_edge
|
304 |
|
|
or rising_edge
|
305 |
|
|
or watchdog_timer_done
|
306 |
|
|
or bit_count
|
307 |
|
|
or packet_good
|
308 |
|
|
or ps2_data
|
309 |
|
|
or clean_clk
|
310 |
|
|
)
|
311 |
|
|
begin : m2_state_logic
|
312 |
|
|
|
313 |
|
|
// Output signals default to this value, unless changed in a state condition.
|
314 |
|
|
ps2_clk_hi_z <= 1;
|
315 |
|
|
ps2_data_hi_z <= 1;
|
316 |
|
|
error_no_ack <= 0;
|
317 |
|
|
output_strobe <= 0;
|
318 |
|
|
|
319 |
|
|
case (m2_state)
|
320 |
|
|
|
321 |
|
|
m2_reset : // After reset, sends command to mouse.
|
322 |
|
|
begin
|
323 |
|
|
m2_next_state <= m2_hold_clk_l;
|
324 |
|
|
end
|
325 |
|
|
|
326 |
|
|
m2_wait :
|
327 |
|
|
begin
|
328 |
|
|
if (falling_edge) m2_next_state <= m2_gather;
|
329 |
|
|
else m2_next_state <= m2_wait;
|
330 |
|
|
end
|
331 |
|
|
|
332 |
|
|
m2_gather :
|
333 |
|
|
begin
|
334 |
|
|
if (watchdog_timer_done && (bit_count == `TOTAL_BITS))
|
335 |
|
|
m2_next_state <= m2_verify;
|
336 |
|
|
else if (watchdog_timer_done && (bit_count < `TOTAL_BITS))
|
337 |
|
|
m2_next_state <= m2_hold_clk_l;
|
338 |
|
|
else m2_next_state <= m2_gather;
|
339 |
|
|
end
|
340 |
|
|
|
341 |
|
|
m2_verify :
|
342 |
|
|
begin
|
343 |
|
|
if (packet_good) m2_next_state <= m2_use;
|
344 |
|
|
else m2_next_state <= m2_wait;
|
345 |
|
|
end
|
346 |
|
|
|
347 |
|
|
m2_use :
|
348 |
|
|
begin
|
349 |
|
|
output_strobe <= 1;
|
350 |
|
|
m2_next_state <= m2_wait;
|
351 |
|
|
end
|
352 |
|
|
|
353 |
|
|
// The following sequence of 9 states is designed to transmit the
|
354 |
|
|
// "enable streaming mode" command to the mouse, and then await the
|
355 |
|
|
// response from the mouse. Upon completion of this operation, the
|
356 |
|
|
// receive shift register contains 22 bits of data which are "invalid"
|
357 |
|
|
// therefore, the m2_verify state will fail to validate the data, and
|
358 |
|
|
// control will be passed into the m2_wait state once again (but the
|
359 |
|
|
// mouse will then be enabled, and valid data packets will ensue whenever
|
360 |
|
|
// there is activity on the mouse.)
|
361 |
|
|
m2_hold_clk_l :
|
362 |
|
|
begin
|
363 |
|
|
ps2_clk_hi_z <= 0; // This starts the watchdog timer!
|
364 |
|
|
if (watchdog_timer_done && ~clean_clk) m2_next_state <= m2_data_low_1;
|
365 |
|
|
else m2_next_state <= m2_hold_clk_l;
|
366 |
|
|
end
|
367 |
|
|
|
368 |
|
|
m2_data_low_1 :
|
369 |
|
|
begin
|
370 |
|
|
ps2_data_hi_z <= 0; // Forms start bit, d[0] and d[1]
|
371 |
|
|
if (rising_edge && (bit_count == 3))
|
372 |
|
|
m2_next_state <= m2_data_high_1;
|
373 |
|
|
else m2_next_state <= m2_data_low_1;
|
374 |
|
|
end
|
375 |
|
|
|
376 |
|
|
m2_data_high_1 :
|
377 |
|
|
begin
|
378 |
|
|
ps2_data_hi_z <= 1; // Forms d[2]
|
379 |
|
|
if (rising_edge && (bit_count == 4))
|
380 |
|
|
m2_next_state <= m2_data_low_2;
|
381 |
|
|
else m2_next_state <= m2_data_high_1;
|
382 |
|
|
end
|
383 |
|
|
|
384 |
|
|
m2_data_low_2 :
|
385 |
|
|
begin
|
386 |
|
|
ps2_data_hi_z <= 0; // Forms d[3]
|
387 |
|
|
if (rising_edge && (bit_count == 5))
|
388 |
|
|
m2_next_state <= m2_data_high_2;
|
389 |
|
|
else m2_next_state <= m2_data_low_2;
|
390 |
|
|
end
|
391 |
|
|
|
392 |
|
|
m2_data_high_2 :
|
393 |
|
|
begin
|
394 |
|
|
ps2_data_hi_z <= 1; // Forms d[4],d[5],d[6],d[7]
|
395 |
|
|
if (rising_edge && (bit_count == 9))
|
396 |
|
|
m2_next_state <= m2_data_low_3;
|
397 |
|
|
else m2_next_state <= m2_data_high_2;
|
398 |
|
|
end
|
399 |
|
|
|
400 |
|
|
m2_data_low_3 :
|
401 |
|
|
begin
|
402 |
|
|
ps2_data_hi_z <= 0; // Forms parity bit
|
403 |
|
|
if (rising_edge) m2_next_state <= m2_data_high_3;
|
404 |
|
|
else m2_next_state <= m2_data_low_3;
|
405 |
|
|
end
|
406 |
|
|
|
407 |
|
|
m2_data_high_3 :
|
408 |
|
|
begin
|
409 |
|
|
ps2_data_hi_z <= 1; // Allow mouse to pull low (ack pulse)
|
410 |
|
|
if (falling_edge && ps2_data) m2_next_state <= m2_error_no_ack;
|
411 |
|
|
else if (falling_edge && ~ps2_data)
|
412 |
|
|
m2_next_state <= m2_await_response;
|
413 |
|
|
else m2_next_state <= m2_data_high_3;
|
414 |
|
|
end
|
415 |
|
|
|
416 |
|
|
m2_error_no_ack :
|
417 |
|
|
begin
|
418 |
|
|
error_no_ack <= 1;
|
419 |
|
|
m2_next_state <= m2_error_no_ack;
|
420 |
|
|
end
|
421 |
|
|
|
422 |
|
|
// In order to "cleanly" exit the setting of the mouse into "streaming"
|
423 |
|
|
// data mode, the state machine should wait for a long enough time to
|
424 |
|
|
// ensure the FA response is done being sent by the mouse. Unfortunately,
|
425 |
|
|
// this is tough to figure out, since the watchdog timeout might be longer
|
426 |
|
|
// or shorter depending upon the user. If the watchdog timeout is set to
|
427 |
|
|
// a small enough value (less than about 560 usec?) then the bit_count
|
428 |
|
|
// will get reset to zero by the watchdog before the FA response is
|
429 |
|
|
// received. In that case, bit_count will be 11.
|
430 |
|
|
// If the bit_count is not reset by the watchdog, then the
|
431 |
|
|
// total bit_count will be 22.
|
432 |
|
|
// In either case, when this state is reached, the watchdog timer is still
|
433 |
|
|
// running and it is best to let it expire before returning to normal
|
434 |
|
|
// operation. One easy way to do this is to check for the bit_count to
|
435 |
|
|
// reach 22 (which it will always do when receiving a normal packet) and
|
436 |
|
|
// then jump to "verify" which will always fail for that time.
|
437 |
|
|
m2_await_response :
|
438 |
|
|
begin
|
439 |
|
|
if (bit_count == 22) m2_next_state <= m2_verify;
|
440 |
|
|
else m2_next_state <= m2_await_response;
|
441 |
|
|
end
|
442 |
|
|
|
443 |
|
|
default : m2_next_state <= m2_wait;
|
444 |
|
|
endcase
|
445 |
|
|
end
|
446 |
|
|
|
447 |
|
|
|
448 |
|
|
|
449 |
|
|
// State register
|
450 |
|
|
always @(posedge clk)
|
451 |
|
|
begin : m3_state_register
|
452 |
|
|
if (reset) m3_state <= m3_data_ready_ack;
|
453 |
|
|
else m3_state <= m3_next_state;
|
454 |
|
|
end
|
455 |
|
|
|
456 |
|
|
// State transition logic
|
457 |
|
|
always @(m3_state or output_strobe or read)
|
458 |
|
|
begin : m3_state_logic
|
459 |
|
|
case (m3_state)
|
460 |
|
|
m3_data_ready_ack:
|
461 |
|
|
begin
|
462 |
|
|
data_ready <= 1'b0;
|
463 |
|
|
if (output_strobe) m3_next_state <= m3_data_ready;
|
464 |
|
|
else m3_next_state <= m3_data_ready_ack;
|
465 |
|
|
end
|
466 |
|
|
m3_data_ready:
|
467 |
|
|
begin
|
468 |
|
|
data_ready <= 1'b1;
|
469 |
|
|
if (read) m3_next_state <= m3_data_ready_ack;
|
470 |
|
|
else m3_next_state <= m3_data_ready;
|
471 |
|
|
end
|
472 |
|
|
default : m3_next_state <= m3_data_ready_ack;
|
473 |
|
|
endcase
|
474 |
|
|
end
|
475 |
|
|
|
476 |
|
|
// This is the bit counter
|
477 |
|
|
always @(posedge clk)
|
478 |
|
|
begin
|
479 |
|
|
if (reset) bit_count <= 0; // normal reset
|
480 |
|
|
else if (falling_edge) bit_count <= bit_count + 1;
|
481 |
|
|
else if (watchdog_timer_done) bit_count <= 0; // rx watchdog timer reset
|
482 |
|
|
end
|
483 |
|
|
|
484 |
|
|
// This is the shift register
|
485 |
|
|
always @(posedge clk)
|
486 |
|
|
begin
|
487 |
|
|
if (reset) q <= 0;
|
488 |
|
|
else if (falling_edge) q <= {ps2_data,q[`TOTAL_BITS-1:1]};
|
489 |
|
|
end
|
490 |
|
|
|
491 |
|
|
// This is the watchdog timer counter
|
492 |
|
|
// The watchdog timer is always "enabled" to operate.
|
493 |
|
|
always @(posedge clk)
|
494 |
|
|
begin
|
495 |
|
|
if (reset || rising_edge || falling_edge) watchdog_timer_count <= 0;
|
496 |
|
|
else if (~watchdog_timer_done)
|
497 |
|
|
watchdog_timer_count <= watchdog_timer_count + 1;
|
498 |
|
|
end
|
499 |
|
|
assign watchdog_timer_done = (watchdog_timer_count==WATCHDOG_TIMER_VALUE_PP-1);
|
500 |
|
|
|
501 |
|
|
// This is the debounce timer counter
|
502 |
|
|
always @(posedge clk)
|
503 |
|
|
begin
|
504 |
|
|
if (reset || falling_edge || rising_edge) debounce_timer_count <= 0;
|
505 |
|
|
// else if (~debounce_timer_done)
|
506 |
|
|
else debounce_timer_count <= debounce_timer_count + 1;
|
507 |
|
|
end
|
508 |
|
|
assign debounce_timer_done = (debounce_timer_count==DEBOUNCE_TIMER_VALUE_PP-1);
|
509 |
|
|
|
510 |
|
|
// This is the logic to verify that a received data packet is "valid"
|
511 |
|
|
// or good.
|
512 |
|
|
assign packet_good = (
|
513 |
|
|
(q[0] == 0)
|
514 |
|
|
&& (q[10] == 1)
|
515 |
|
|
&& (q[11] == 0)
|
516 |
|
|
&& (q[21] == 1)
|
517 |
|
|
&& (q[22] == 0)
|
518 |
|
|
&& (q[32] == 1)
|
519 |
|
|
&& (q[9] == ~^q[8:1]) // odd parity bit
|
520 |
|
|
&& (q[20] == ~^q[19:12]) // odd parity bit
|
521 |
|
|
&& (q[31] == ~^q[30:23]) // odd parity bit
|
522 |
|
|
);
|
523 |
|
|
|
524 |
|
|
// Output the special scan code flags, the scan code and the ascii
|
525 |
|
|
always @(posedge clk)
|
526 |
|
|
begin
|
527 |
|
|
if (reset)
|
528 |
|
|
begin
|
529 |
|
|
left_button <= 0;
|
530 |
|
|
right_button <= 0;
|
531 |
|
|
x_increment <= 0;
|
532 |
|
|
y_increment <= 0;
|
533 |
|
|
end
|
534 |
|
|
else if (output_strobe)
|
535 |
|
|
begin
|
536 |
|
|
left_button <= q[1];
|
537 |
|
|
right_button <= q[2];
|
538 |
|
|
x_increment <= {q[5],q[19:12]};
|
539 |
|
|
y_increment <= {q[6],q[30:23]};
|
540 |
|
|
end
|
541 |
|
|
end
|
542 |
|
|
|
543 |
|
|
|
544 |
|
|
endmodule
|
545 |
|
|
|
546 |
|
|
//`undefine TOTAL_BITS
|
547 |
|
|
|