-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMSMouseWrapper.v
424 lines (390 loc) · 9.66 KB
/
MSMouseWrapper.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
`timescale 1ns / 1ps
`default_nettype none
//////////////////////////////////////////////////////////////////////////////////
/*
MIT License
Copyright (c) 2022 Antonio Sánchez (@TheSonders)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
PS2MOUSE -> MSMOUSE Conversion
REMOTE VERSION
References:
https://roborooter.com/post/serial-mice/
https://isdaman.com/alsos/hardware/mouse/ps2interface.htm
https://www.avrfreaks.net/sites/default/files/PS2%20Keyboard.pdf
*/
//////////////////////////////////////////////////////////////////////////////////
module MSMouseWrapper
#(parameter CLKFREQ=50_000_000)
(input wire clk,
inout wire ps2dta,
inout wire ps2clk,
input wire rts,
output reg rd=0
);
assign ps2dta=(ps2dta_reg==0)?0:1'bZ;
assign ps2clk=(ps2clk_reg==0)?0:1'bZ;
////////////////////////
localparam PS2BAUDRATE=9_000;
////////////////////////
localparam PS2PERIOD=(CLKFREQ/PS2BAUDRATE);
localparam HUNDRED=(CLKFREQ/10_000);
localparam SERIALBAUDRATE=1_200;
localparam SERIALPERIOD=(CLKFREQ/SERIALBAUDRATE);
localparam MILLIS=(CLKFREQ/1000);
`define PAR_ODD 0
`define PAR_EVEN 1
///////////////////////////////////////////
//////////////PS2 Reception////////////////
///////////////////////////////////////////
`define PS2CLKRISE (ps2clkbuf==4'b0011)
`define PS2CLKFALL (ps2clkbuf==4'b1100)
`define RTSRISE (rtsbuf==4'b0011)
`define RTSFALL (rtsbuf==4'b0000)
`define TXIDLE (PS2Tr_STM==1)
`define PS2R_Start 0
`define PS2R_Parity 9
`define PS2R_Stop 10
`define PS2R_Delay 11
reg [3:0]ps2clkbuf=0;
reg [3:0]rtsbuf=0;
reg [3:0]PS2R_STM=0;
reg PS2R_NewByte=0;
reg [7:0]PS2R_Byte=0;
reg PS2R_PAR=0;
reg [$clog2(PS2PERIOD)-1:0]PS2R_Counter=0;
always @(posedge clk)begin
ps2clkbuf<={ps2clkbuf[2:0],ps2clk};
rtsbuf<={rtsbuf[2:0],rts};
if (PS2R_NewByte==1)PS2R_NewByte<=0;
if (PS2R_STM==`PS2R_Delay)begin
if (PS2R_Counter==0)begin
PS2R_NewByte<=1;
PS2R_STM<=0;
end
else PS2R_Counter<=PS2R_Counter-1;
end
else begin
if (`PS2CLKFALL && `TXIDLE)begin
case (PS2R_STM)
`PS2R_Start:
begin
if (ps2dta==0)begin
PS2R_STM<=PS2R_STM+1;
PS2R_PAR<=`PAR_EVEN;
end
end
`PS2R_Parity:
begin
if (ps2dta==PS2R_PAR)begin
PS2R_STM<=PS2R_STM+1;
end
else begin
PS2R_STM<=0;
end
end
`PS2R_Stop:
begin
if (ps2dta==1)begin
PS2R_STM<=PS2R_STM+1;
PS2R_Counter<=PS2PERIOD;
end
else PS2R_STM<=0;
end
default:
begin
PS2R_Byte<={ps2dta,PS2R_Byte[7:1]};
PS2R_PAR<=PS2R_PAR^ps2dta;
PS2R_STM<=PS2R_STM+1;
end
endcase
end
end
end
///////////////////////////////////////////
//////////////PS2 Processing///////////////
///////////////////////////////////////////
`define PS2Pr_ResetDelay 0
`define PS2Pr_SendReset 1
`define PS2Pr_WaitResetACK 2
`define PS2Pr_WaitBAT 3
`define PS2Pr_WaitID 4
`define PS2Pr_WaitACK 5
`define PS2Pr_SendM 6
`define PS2Pr_Query 7
`define PS2Pr_Wait0 8
`define PS2Pr_Wait1 9
`define PS2Pr_Wait2 10
`define PS2Pr_Wait3 11
`define PS2Pr_BAT 8'hAA
`define PS2Pr_ID 8'h00
`define PS2Pr_RESET 8'hFF
`define PS2Pr_REMOTE 8'hF0
`define PS2Pr_ACK 8'hFA
`define PS2Pr_READ 8'hEB
`define PS2Pr_M 30'h39AFFFFF
`define PS2BitSYNC 3
`define MSMByte1 {2'b11,LeftBt,RightBt,YC[7:6],XC[7:6]}
`define MSMByte2 {2'b10,XC[5:0]}
`define MSMByte3 {2'b10,YC[5:0]}
`define Serial_Reset 0
`define TMR_END (Timer==0)
`define PS2Tr_Reset 0
`define PS2Tr_Idle 1
`define PS2Tr_WaitClock 2
`define PS2Tr_ClockLow 3
`define PS2Tr_Parity 12
`define PS2Tr_Stop 13
`define PS2Tr_ACK 14
`define PS2Tr_End 15
`define STARTBIT 0
`define STOPBIT 1
wire [7:0]YC= ~{PS2Byte1[5],PS2R_Byte[7:1]}+1;
wire [7:0]XC= {PS2Byte1[4],PS2Byte2[7:1]};
wire LeftBt=PS2Byte1[0];
wire RightBt=PS2Byte1[1];
reg LBut=0;
reg RBut=0;
reg PS2Detected=0;
reg [$clog2(MILLIS)-1:0]Timer=0;
reg SerialSendRequest=0;
reg [4:0]Serial_STM=0;
reg [3:0]PS2Pr_STM=0;
reg PS2SendRequest=0;
reg [7:0]PS2SendData=0;
reg [7:0]PS2Byte1=0;
reg [7:0]PS2Byte2=0;
reg [29:0]SerialSendData=0;
reg ps2dta_reg=0;
reg ps2clk_reg=0;
reg [3:0]PS2Tr_STM=0;
reg PS2Tr_PAR=0;
always @(posedge clk)begin
if (PS2SendRequest==1)PS2SendRequest<=0;
if (SerialSendRequest==1)SerialSendRequest<=0;
if (Timer!=0)Timer<=Timer-1;
case (PS2Pr_STM)
`PS2Pr_ResetDelay:begin
SetTimer(MILLIS);
PS2Pr_STM<=PS2Pr_STM+1;
end
`PS2Pr_SendReset:begin
if (`TMR_END)begin
SendPS2(`PS2Pr_RESET);
PS2Pr_STM<=PS2Pr_STM+1;
end
end
`PS2Pr_WaitResetACK:begin
if (PS2R_NewByte==1)begin
if (PS2R_Byte==`PS2Pr_ACK)begin
PS2Pr_STM<=PS2Pr_STM+1;
end
else begin
PS2Pr_STM<=0;
end
end
end
`PS2Pr_WaitBAT:begin
if (PS2R_NewByte==1)begin
if (PS2R_Byte==`PS2Pr_BAT)begin
PS2Pr_STM<=PS2Pr_STM+1;
end
else begin
PS2Pr_STM<=0;
end
end
end
`PS2Pr_WaitID:begin
if (PS2R_NewByte==1)begin
if (PS2R_Byte==`PS2Pr_ID)begin
PS2Pr_STM<=PS2Pr_STM+1;
SendPS2(`PS2Pr_REMOTE);
end
else begin
PS2Pr_STM<=0;
end
end
end
`PS2Pr_WaitACK:begin
if (PS2R_NewByte==1)begin
if (PS2R_Byte==`PS2Pr_ACK)begin
PS2Pr_STM<=PS2Pr_STM+1;
PS2Detected<=1;
end
else begin
PS2Pr_STM<=0;
end
end
end
`PS2Pr_SendM:begin
if(`RTSRISE)begin
PS2Pr_STM<=PS2Pr_STM+1;
SendSerial(`PS2Pr_M);
end
end
`PS2Pr_Query:begin
if (SerialSendRequest==0 && Serial_STM==0)begin
SendPS2(`PS2Pr_READ);
PS2Pr_STM<=PS2Pr_STM+1;
end
end
`PS2Pr_Wait0:begin
if (PS2R_NewByte==1)begin
if (PS2R_Byte==`PS2Pr_ACK)begin
PS2Pr_STM<=PS2Pr_STM+1;
end
else begin
PS2Pr_STM<=0;
end
end
end
`PS2Pr_Wait1:begin
if (PS2R_NewByte==1)begin
if (PS2R_Byte[`PS2BitSYNC]==1)begin
PS2Byte1<=PS2R_Byte;
PS2Pr_STM<=PS2Pr_STM+1;
end
end
end
`PS2Pr_Wait2:begin
if (PS2R_NewByte==1)begin
PS2Byte2<=PS2R_Byte;
PS2Pr_STM<=PS2Pr_STM+1;
end
end
`PS2Pr_Wait3:begin
if (PS2R_NewByte==1)begin
if (`RTSFALL)begin
PS2Pr_STM<=`PS2Pr_SendM;
end
else begin
PS2Pr_STM<=`PS2Pr_Query;
SerialSendRequest<=1;
if (XC!=0 || YC!=0 || LBut!=LeftBt || RBut!=RightBt) begin
SendSerial({1'b1,`MSMByte3,2'b01,`MSMByte2,2'b01,`MSMByte1,1'b0});
LBut<=LeftBt;
RBut<=RightBt;
end
end
end
end
endcase
///////////////////////////////////////////
/////////////Serial Transmision////////////
///////////////////////////////////////////
if (`RTSFALL)begin
Serial_STM<=0;
rd<=1;
end
else begin
case (Serial_STM)
`Serial_Reset:begin
if (SerialSendRequest==1)begin
Serial_STM<=Serial_STM+1;
{SerialSendData,rd}<={1'b1,SerialSendData};
SetTimer(SERIALPERIOD);
end
else begin
rd<=1;
end
end
default:begin
if (`TMR_END)begin
Serial_STM<=Serial_STM+1;
{SerialSendData,rd}<={1'b1,SerialSendData};
SetTimer(SERIALPERIOD);
end
end
endcase
end
///////////////////////////////////////////
//////////////PS2 Transmision//////////////
///////////////////////////////////////////
case (PS2Tr_STM)
`PS2Tr_Reset:begin
ps2dta_reg<=1;
ps2clk_reg<=1;
PS2Tr_STM<=PS2Tr_STM+1;
end
`PS2Tr_Idle:begin
if (PS2SendRequest==1)begin
PS2Tr_STM<=PS2Tr_STM+1;
end
end
`PS2Tr_WaitClock:begin
if (PS2R_STM==0)begin
ps2clk_reg<=0;
SetTimer(HUNDRED);
PS2Tr_STM<=PS2Tr_STM+1;
end
end
`PS2Tr_ClockLow:begin
if (`TMR_END)begin
ps2dta_reg<=`STARTBIT;
ps2clk_reg<=1;
PS2Tr_STM<=PS2Tr_STM+1;
PS2Tr_PAR<=`PAR_EVEN;
end
end
`PS2Tr_Parity:begin
if (`PS2CLKFALL)begin
ps2dta_reg<=PS2Tr_PAR;
PS2Tr_STM<=PS2Tr_STM+1;
end
end
`PS2Tr_Stop:begin
if (`PS2CLKFALL)begin
ps2dta_reg<=`STOPBIT;
PS2Tr_STM<=PS2Tr_STM+1;
end
end
`PS2Tr_ACK:begin
if (`PS2CLKFALL)begin
PS2Tr_STM<=PS2Tr_STM+1;
end
end
`PS2Tr_End:begin
if (`PS2CLKRISE)begin
PS2Tr_STM<=`PS2Tr_Idle;
end
end
default:begin
if (`PS2CLKFALL)begin
{PS2SendData,ps2dta_reg}<={1'b1,PS2SendData};
PS2Tr_PAR<=PS2Tr_PAR^PS2SendData[0];
PS2Tr_STM<=PS2Tr_STM+1;
end
end
endcase
end
task SendPS2 (input [7:0] ByteToSend);
begin
PS2SendRequest<=1;
PS2SendData<=ByteToSend;
end
endtask
task SendSerial (input [29:0] ByteToSend);
begin
SerialSendRequest<=1;
SerialSendData<=ByteToSend;
end
endtask
task SetTimer(input [31:0]TIME);
begin
Timer<=TIME;
end
endtask
endmodule