-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjoy_interface.v
executable file
·65 lines (56 loc) · 982 Bytes
/
joy_interface.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
// When sndRec is 1, it begins reading bits from the joystick and to output a single byte
module joy_interface(
input CLK, //66.67kHz
input sndRec,
input MISO,
output SCLK,
output reg BUSY,
output [7:0] DOUT
);
reg [4:0] bitCount;
reg [7:0] rSR = 0;
reg [7:0] wSR = 0;
reg [1:0] pState = 0;
reg CE = 0;
assign SCLK = (CE == 1'b1) ? CLK : 1'b0;
assign DOUT = rSR;
//Write to read-shift-register
always @(posedge CLK)
begin
if (pState == 2 && CE)
rSR <= {rSR[6:0], MISO};
end
always @(negedge CLK)
begin
case (pState)
// Idle
0 : begin
CE <= 0;
BUSY <= 0;
bitCount <= 0;
pState <= sndRec ? 1 : 0;
end
// Init
1 : begin
BUSY <= 1;
bitCount <= 0;
CE <= 0;
pState <= 2;
end
// RxTx
2 : begin
BUSY <= 1;
bitCount <= bitCount + 1;
CE <= (bitCount < 8);
pState <= bitCount == 8 ? 3 : 2;
end
// Done
3 : begin
CE <= 0;
BUSY <= 1;
bitCount <= 0;
pState <= 0;
end
endcase
end
endmodule