-
Notifications
You must be signed in to change notification settings - Fork 0
/
MouseTransmitter.v
211 lines (195 loc) · 5.49 KB
/
MouseTransmitter.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
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 17:14:37 18/02/2014
// Design Name:
// Module Name: MouseTransmitter
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments: Large ammounts of code were already provided, however
// that also greatly restricts my coding options
//
//////////////////////////////////////////////////////////////////////////////////
module MouseTransmitter(
//Standard Inputs
input RESET,
input CLK,
//Mouse IO - CLK
input CLK_MOUSE_IN,
output CLK_MOUSE_OUT_EN, // Allows for the control of the Clock line
//Mouse IO - DATA
input DATA_MOUSE_IN,
output DATA_MOUSE_OUT,
output DATA_MOUSE_OUT_EN,
//Control
input SEND_BYTE,
input [7:0] BYTE_TO_SEND,
output BYTE_SENT
);
//////////////////////////////////////////////////////////
// Clk Mouse delayed to detect clock edges
reg ClkMouseInDly;
always@(posedge CLK)
ClkMouseInDly <= CLK_MOUSE_IN;
//////////////////////////////////////////////////////////
//Now a state machine to control the flow of write data
reg [3:0] Curr_State, Next_State;
reg Curr_MouseClkOutWE, Next_MouseClkOutWE;
reg Curr_MouseDataOut, Next_MouseDataOut;
reg Curr_MouseDataOutWE, Next_MouseDataOutWE;
reg [15:0] Curr_SendCounter, Next_SendCounter;
reg Curr_ByteSent, Next_ByteSent;
reg [7:0] Curr_ByteToSend, Next_ByteToSend;
//Sequential
always@(posedge CLK)
begin
if(RESET)
begin
Curr_State <= 0;
Curr_MouseClkOutWE <= 1'b0;
Curr_MouseDataOut <= 1'b0;
Curr_MouseDataOutWE <= 1'b0;
Curr_SendCounter <= 0;
Curr_ByteSent <= 1'b0;
Curr_ByteToSend <= 0;
end
else
begin
Curr_State <= Next_State;
Curr_MouseClkOutWE <= Next_MouseClkOutWE;
Curr_MouseDataOut <= Next_MouseDataOut;
Curr_MouseDataOutWE <= Next_MouseDataOutWE;
Curr_SendCounter <= Next_SendCounter;
Curr_ByteSent <= Next_ByteSent;
Curr_ByteToSend <= Next_ByteToSend;
end
end
//Combinatorial
always@*
begin
//default values
Next_State = Curr_State;
Next_MouseClkOutWE = 1'b0;
Next_MouseDataOut = 1'b0;
Next_MouseDataOutWE = Curr_MouseDataOutWE;
Next_SendCounter = Curr_SendCounter;
Next_ByteSent = 1'b0;
Next_ByteToSend = Curr_ByteToSend;
case(Curr_State)
//IDLE
0:
begin
if(SEND_BYTE)
begin
Next_State = 1;
Next_ByteToSend = BYTE_TO_SEND;
end
Next_MouseDataOutWE = 1'b0;
end
//Bring Clock line low for at least 100 microsecs i.e. 5000 clock cycles @ 50MHz
1:
begin
if(Curr_SendCounter == 6000)
begin
Next_State = 2;
Next_SendCounter = 0;
end
else
Next_SendCounter = Curr_SendCounter + 1'b1;
Next_MouseClkOutWE = 1'b1;
end
//Bring the Data Line Low and release the Clock line
2:
begin
Next_State = 3;
Next_MouseDataOutWE = 1'b1;
end
//Start Sending
3:
begin // change data at falling edge of clock, start bit = 0
if(ClkMouseInDly & ~CLK_MOUSE_IN)
Next_State = 4;
end
//Send Bits 0 to 7 - We need to send the byte
4:
begin // change data at falling edge of clock
if(ClkMouseInDly & ~CLK_MOUSE_IN)
begin
if(Curr_SendCounter == 7)
begin
Next_State = 5;
Next_SendCounter = 0;
end
else
Next_SendCounter = Curr_SendCounter + 1'b1;
end
Next_MouseDataOut = Curr_ByteToSend[Curr_SendCounter];
end
//Send the parity bit
5:
begin // change data at falling edge of clock
if(ClkMouseInDly & ~CLK_MOUSE_IN)
Next_State = 6;
Next_MouseDataOut = ~^Curr_ByteToSend[7:0];
end
//Send the stop bit... this state was forgotten in the original partial code
6:
begin
if(ClkMouseInDly & ~CLK_MOUSE_IN)
Next_State = 7;
Next_MouseDataOut = 1'b1;
end
//Release Data line
7:
begin
Next_State = 8;
Next_MouseDataOutWE = 1'b0;
end
/*
Wait for Device to bring Data line low, then wait for Device to bring Clock line low, and finally wait for
Device to release both Data and Clock.
*/
/*
………………..
FILL IN THIS AREA
……………….
*/
8:
begin
if(!DATA_MOUSE_IN) // returns to S7 if data line not pulled low by mouse
Next_State = 9;
end
9:
begin
if(!CLK_MOUSE_IN) // returns to S8 if CLK not pulled low by mouse
Next_State = 10;
end
10:
begin
if(DATA_MOUSE_IN & CLK_MOUSE_IN) // returns to S9 if data line not released (let rise high) by mouse
begin
Next_State = 0; // returns to S9 if CLK not released (let rise high) by mouse
Next_ByteSent = 1'b1; // confirms byte was sent out
end
end
endcase
end
///////////////////////////////////////////////////////////////
//Assign OUTPUTs
//Mouse IO - CLK
assign CLK_MOUSE_OUT_EN = Curr_MouseClkOutWE;
//Mouse IO - DATA
assign DATA_MOUSE_OUT = Curr_MouseDataOut;
assign DATA_MOUSE_OUT_EN = Curr_MouseDataOutWE;
//Control
assign BYTE_SENT = Curr_ByteSent;
endmodule