-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathusb_utils.v
165 lines (135 loc) · 3.02 KB
/
usb_utils.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
module usb_crc5(
input rst_n,
input clk,
input clken,
input d,
output valid
);
reg[4:0] r;
reg[4:0] next;
wire top = r[4];
assign valid = (next == 5'b01100);
always @(*) begin
if (top == d)
next = { r[3], r[2], r[1], r[0], 1'b0 };
else
next = { r[3], r[2], !r[1], r[0], 1'b1 };
end
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
r <= 5'b11111;
end else if (clken) begin
r <= next;
end
end
endmodule
//---------------------------------------------------------------------
module usb_crc16(
input rst_n,
input clk,
input clken,
input d,
input dump,
output out,
output valid
);
reg[15:0] r;
reg[15:0] next;
assign out = r[15];
assign valid = (next == 16'b1000000000001101);
always @(*) begin
if (dump || out == d)
next = { r[14:0], 1'b0 };
else
next = { !r[14], r[13:2], !r[1], r[0], 1'b1 };
end
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
r <= 16'hffff;
end else if (clken) begin
r <= next;
end
end
endmodule
//---------------------------------------------------------------------
module usb_clk_recovery(
input rst_n,
input clk,
input i,
output strobe
);
reg[1:0] cntr;
reg prev_i;
assign strobe = cntr == 1'b0;
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
cntr <= 1'b0;
prev_i <= 1'b0;
end else begin
if (i == prev_i) begin
cntr <= cntr - 1'b1;
end else begin
cntr <= 1'b1;
end
prev_i <= i;
end
end
endmodule
//---------------------------------------------------------------------
module usb_bit_destuff(
input rst_n,
input clk,
input clken,
input d,
output strobe);
reg[6:0] data;
assign strobe = clken && (data != 7'b0111111);
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
data <= 7'b0000000;
end else if (clken) begin
data <= { data[5:0], d };
end
end
endmodule
//---------------------------------------------------------------------
module usb_sync_detect(
input rst_n,
input clk,
input clken,
input j,
input se0,
output sync);
// 3KJ's followed by 2K's
reg[6:0] data;
assign sync = (data == 7'b0101010 && !j && !se0);
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
data <= 1'd0;
end else if (clken) begin
data <= { data[5:0], j || se0 };
end
end
endmodule
//---------------------------------------------------------------------
module usb_reset_detect(
input rst_n,
input clk,
input se0,
output usb_rst);
localparam cntr_rst_val = 19'd480000;
reg[18:0] cntr;
assign usb_rst = cntr == 1'b0;
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
cntr <= cntr_rst_val;
end else begin
if (se0) begin
if (!usb_rst)
cntr <= cntr - 1'b1;
end else begin
cntr <= cntr_rst_val;
end
end
end
endmodule