1 |
2 |
alfoltran |
/////////////////////////////////////////////////////////////////////
|
2 |
|
|
//// ////
|
3 |
|
|
//// USB Packet Disassembler ////
|
4 |
|
|
//// ////
|
5 |
|
|
//// SystemC Version: usb_pd_sie.cpp ////
|
6 |
|
|
//// Author: Alfredo Luiz Foltran Fialho ////
|
7 |
|
|
//// alfoltran@ig.com.br ////
|
8 |
|
|
//// ////
|
9 |
|
|
//// ////
|
10 |
|
|
/////////////////////////////////////////////////////////////////////
|
11 |
|
|
//// ////
|
12 |
|
|
//// Verilog Version: usb1_pd.v ////
|
13 |
|
|
//// Copyright (C) 2000-2002 Rudolf Usselmann ////
|
14 |
|
|
//// www.asics.ws ////
|
15 |
|
|
//// rudi@asics.ws ////
|
16 |
|
|
//// ////
|
17 |
|
|
//// This source file may be used and distributed without ////
|
18 |
|
|
//// restriction provided that this copyright statement is not ////
|
19 |
|
|
//// removed from the file and that any derivative work contains ////
|
20 |
|
|
//// the original copyright notice and the associated disclaimer.////
|
21 |
|
|
//// ////
|
22 |
|
|
//// THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY ////
|
23 |
|
|
//// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED ////
|
24 |
|
|
//// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS ////
|
25 |
|
|
//// FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR ////
|
26 |
|
|
//// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, ////
|
27 |
|
|
//// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ////
|
28 |
|
|
//// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ////
|
29 |
|
|
//// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR ////
|
30 |
|
|
//// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ////
|
31 |
|
|
//// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ////
|
32 |
|
|
//// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT ////
|
33 |
|
|
//// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE ////
|
34 |
|
|
//// POSSIBILITY OF SUCH DAMAGE. ////
|
35 |
|
|
//// ////
|
36 |
|
|
/////////////////////////////////////////////////////////////////////
|
37 |
|
|
|
38 |
|
|
#include "systemc.h"
|
39 |
|
|
#include "usb_pd_sie.h"
|
40 |
|
|
|
41 |
|
|
void usb_pd_sie::rx_busy_up1(void) {
|
42 |
|
|
if (!rst.read())
|
43 |
|
|
rx_busy_d.write(false);
|
44 |
|
|
else if (rx_valid.read() && (state.read() == PD_DATA))
|
45 |
|
|
rx_busy_d.write(true);
|
46 |
|
|
else if (state.read() != PD_DATA)
|
47 |
|
|
rx_busy_d.write(false);
|
48 |
|
|
}
|
49 |
|
|
|
50 |
|
|
void usb_pd_sie::rx_busy_up2(void) {
|
51 |
|
|
rx_busy.write(rx_busy_d.read());
|
52 |
|
|
}
|
53 |
|
|
|
54 |
|
|
void usb_pd_sie::pid_ld_up(void) {
|
55 |
|
|
pid_ld_en.write(pid_le_sm.read() && rx_active.read() && rx_valid.read());
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
void usb_pd_sie::pid_up(void) {
|
59 |
|
|
if (!rst.read())
|
60 |
|
|
pid.write(0xf0);
|
61 |
|
|
else if (pid_ld_en.read())
|
62 |
|
|
pid.write(rx_data.read());
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
void usb_pd_sie::pid_cks_err_up(void) {
|
66 |
|
|
pid_cks_err.write(pid.read().range(3, 0) != ~pid.read().range(7, 4));
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
void usb_pd_sie::pid_decoder(void) {
|
70 |
|
|
pid_OUT.write(pid.read().range(3, 0) == USBF_T_PID_OUT);
|
71 |
|
|
pid_IN.write(pid.read().range(3, 0) == USBF_T_PID_IN);
|
72 |
|
|
pid_SOF.write(pid.read().range(3, 0) == USBF_T_PID_SOF);
|
73 |
|
|
pid_SETUP.write(pid.read().range(3, 0) == USBF_T_PID_SETUP);
|
74 |
|
|
pid_DATA0.write(pid.read().range(3, 0) == USBF_T_PID_DATA0);
|
75 |
|
|
pid_DATA1.write(pid.read().range(3, 0) == USBF_T_PID_DATA1);
|
76 |
|
|
pid_DATA2.write(pid.read().range(3, 0) == USBF_T_PID_DATA2);
|
77 |
|
|
pid_MDATA.write(pid.read().range(3, 0) == USBF_T_PID_MDATA);
|
78 |
|
|
pid_ACK.write(pid.read().range(3, 0) == USBF_T_PID_ACK);
|
79 |
|
|
pid_NACK.write(pid.read().range(3, 0) == USBF_T_PID_NACK);
|
80 |
|
|
pid_STALL.write(pid.read().range(3, 0) == USBF_T_PID_STALL);
|
81 |
|
|
pid_NYET.write(pid.read().range(3, 0) == USBF_T_PID_NYET);
|
82 |
|
|
pid_PRE.write(pid.read().range(3, 0) == USBF_T_PID_PRE);
|
83 |
|
|
pid_ERR.write(pid.read().range(3, 0) == USBF_T_PID_ERR);
|
84 |
|
|
pid_SPLIT.write(pid.read().range(3, 0) == USBF_T_PID_SPLIT);
|
85 |
|
|
pid_PING.write(pid.read().range(3, 0) == USBF_T_PID_PING);
|
86 |
|
|
pid_RES.write(pid.read().range(3, 0) == USBF_T_PID_RES);
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
void usb_pd_sie::pid_token_up(void) {
|
90 |
|
|
pid_TOKEN.write(pid_OUT.read() || pid_IN.read() || pid_SOF.read() || pid_SETUP.read() || pid_PING.read());
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
void usb_pd_sie::pid_data_up(void) {
|
94 |
|
|
pid_DATA.write(pid_DATA0.read() || pid_DATA1.read() || pid_DATA2.read() || pid_MDATA.read());
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
void usb_pd_sie::token_decoder(void) {
|
98 |
|
|
if (token_le_1.read())
|
99 |
|
|
token0.write(rx_data.read());
|
100 |
|
|
|
101 |
|
|
if (token_le_2.read())
|
102 |
|
|
token1.write(rx_data.read());
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
void usb_pd_sie::token_valid_up1(void) {
|
106 |
|
|
token_valid_r1.write(token_le_2.read());
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
void usb_pd_sie::token_valid_up2(void) {
|
110 |
|
|
token_valid_str1.write(token_valid_r1.read() || pid_ack.read());
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
void usb_pd_sie::token_valid_up3(void) {
|
114 |
|
|
token_valid.write(token_valid_str1.read());
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
void usb_pd_sie::token_up(void) {
|
118 |
|
|
frame_no.write(((sc_uint<3>)token1.read().range(2, 0), token0.read()));
|
119 |
|
|
token_fadr.write((sc_uint<7>)token0.read().range(6, 0));
|
120 |
|
|
token_endp.write(((sc_uint<3>)token1.read().range(2, 0), (sc_uint<1>)token0.read()[7]));
|
121 |
|
|
token_crc5.write((sc_uint<5>)token1.read().range(7, 3));
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
// CRC5 should perform the check in one cycle (flow through logic)
|
125 |
|
|
// 11 bits and crc5 input, 1 bit output
|
126 |
|
|
void usb_pd_sie::crc5_din_up(void) {
|
127 |
|
|
#ifdef USB_SIMULATION
|
128 |
|
|
crc5_din.write(( (sc_uint<1>)token_fadr.read()[0],
|
129 |
|
|
(sc_uint<1>)token_fadr.read()[1],
|
130 |
|
|
(sc_uint<1>)token_fadr.read()[2],
|
131 |
|
|
(sc_uint<1>)token_fadr.read()[3],
|
132 |
|
|
(sc_uint<1>)token_fadr.read()[4],
|
133 |
|
|
(sc_uint<1>)token_fadr.read()[5],
|
134 |
|
|
(sc_uint<1>)token_fadr.read()[6],
|
135 |
|
|
(sc_uint<1>)token_endp.read()[0],
|
136 |
|
|
(sc_uint<1>)token_endp.read()[1],
|
137 |
|
|
(sc_uint<1>)token_endp.read()[2],
|
138 |
|
|
(sc_uint<1>)token_endp.read()[3]));
|
139 |
|
|
#else
|
140 |
|
|
crc5_din.write(((sc_uint<7>)token_fadr.read().range(0, 6), (sc_uint<4>)token_endp.read().range(0, 3)));
|
141 |
|
|
#endif
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
void usb_pd_sie::crc5_err_up(void) {
|
145 |
|
|
crc5_err.write(token_valid.read() && (crc5_out2.read() != token_crc5.read()));
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
// Invert and reverse result bits
|
149 |
|
|
void usb_pd_sie::crc5_out2_up(void) {
|
150 |
|
|
#ifdef USB_SIMULATION
|
151 |
|
|
crc5_out2.write(( (sc_uint<1>)!crc5_out.read()[0],
|
152 |
|
|
(sc_uint<1>)!crc5_out.read()[1],
|
153 |
|
|
(sc_uint<1>)!crc5_out.read()[2],
|
154 |
|
|
(sc_uint<1>)!crc5_out.read()[3],
|
155 |
|
|
(sc_uint<1>)!crc5_out.read()[4]));
|
156 |
|
|
#else
|
157 |
|
|
crc5_out2.write((sc_uint<5>)~crc5_out.read().range(0, 4));
|
158 |
|
|
#endif
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
// Data receiving logic
|
162 |
|
|
// Build a delay line and stop when we are about to get crc
|
163 |
|
|
void usb_pd_sie::rxv1_up(void) {
|
164 |
|
|
if (!rst.read())
|
165 |
|
|
rxv1.write(false);
|
166 |
|
|
else if (data_valid_d.read())
|
167 |
|
|
rxv1.write(true);
|
168 |
|
|
else if (data_done.read())
|
169 |
|
|
rxv1.write(false);
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
void usb_pd_sie::rxv2_up(void) {
|
173 |
|
|
if (!rst.read())
|
174 |
|
|
rxv2.write(false);
|
175 |
|
|
else if (rxv1.read() && data_valid_d.read())
|
176 |
|
|
rxv2.write(true);
|
177 |
|
|
else if (data_done.read())
|
178 |
|
|
rxv2.write(false);
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
void usb_pd_sie::data_valid0_up(void) {
|
182 |
|
|
data_valid0.write(rxv2.read() && data_valid_d.read());
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
void usb_pd_sie::d_up(void) {
|
186 |
|
|
if (data_valid_d.read())
|
187 |
|
|
d0.write(rx_data.read());
|
188 |
|
|
|
189 |
|
|
if (data_valid_d.read())
|
190 |
|
|
d1.write(d0.read());
|
191 |
|
|
|
192 |
|
|
if (data_valid_d.read())
|
193 |
|
|
d2.write(d1.read());
|
194 |
|
|
}
|
195 |
|
|
|
196 |
|
|
void usb_pd_sie::rx_data_st_up(void) {
|
197 |
|
|
rx_data_st.write(d2.read());
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
void usb_pd_sie::rx_data_valid_up(void) {
|
201 |
|
|
rx_data_valid.write(data_valid0.read());
|
202 |
|
|
}
|
203 |
|
|
|
204 |
|
|
void usb_pd_sie::rx_data_done_up(void) {
|
205 |
|
|
rx_data_done.write(data_done.read());
|
206 |
|
|
}
|
207 |
|
|
|
208 |
|
|
// CRC16 accumulates rx_data as long as data_valid_d is asserted.
|
209 |
|
|
// When data_done is asserted, CRC16 reports status, and resets itself
|
210 |
|
|
// next cycle
|
211 |
|
|
void usb_pd_sie::rx_active_r_up(void) {
|
212 |
|
|
rx_active_r.write(rx_active.read());
|
213 |
|
|
}
|
214 |
|
|
|
215 |
|
|
void usb_pd_sie::crc16_din_up(void) {
|
216 |
|
|
#ifdef USB_SIMULATION
|
217 |
|
|
crc16_din.write(( (sc_uint<1>)rx_data.read()[0],
|
218 |
|
|
(sc_uint<1>)rx_data.read()[1],
|
219 |
|
|
(sc_uint<1>)rx_data.read()[2],
|
220 |
|
|
(sc_uint<1>)rx_data.read()[3],
|
221 |
|
|
(sc_uint<1>)rx_data.read()[4],
|
222 |
|
|
(sc_uint<1>)rx_data.read()[5],
|
223 |
|
|
(sc_uint<1>)rx_data.read()[6],
|
224 |
|
|
(sc_uint<1>)rx_data.read()[7]));
|
225 |
|
|
#else
|
226 |
|
|
crc16_din.write((sc_uint<8>)rx_data.read().range(0, 7));
|
227 |
|
|
#endif
|
228 |
|
|
}
|
229 |
|
|
|
230 |
|
|
void usb_pd_sie::crc16_clr_up(void) {
|
231 |
|
|
crc16_clr.write(rx_active.read() && !rx_active_r.read());
|
232 |
|
|
}
|
233 |
|
|
|
234 |
|
|
void usb_pd_sie::crc16_sum_up(void) {
|
235 |
|
|
if (crc16_clr.read())
|
236 |
|
|
crc16_sum.write(0xffff);
|
237 |
|
|
else if (data_valid_d.read())
|
238 |
|
|
crc16_sum.write(crc16_out.read());
|
239 |
|
|
}
|
240 |
|
|
|
241 |
|
|
void usb_pd_sie::crc16_err_up(void) {
|
242 |
|
|
crc16_err.write(data_done.read() && (crc16_sum.read() != 0x800d));
|
243 |
|
|
}
|
244 |
|
|
|
245 |
|
|
// Receive and Decode FSM
|
246 |
|
|
void usb_pd_sie::state_up(void) {
|
247 |
|
|
if (!rst.read())
|
248 |
|
|
state.write(PD_IDLE);
|
249 |
|
|
else
|
250 |
|
|
state.write(next_state.read());
|
251 |
|
|
}
|
252 |
|
|
|
253 |
|
|
void usb_pd_sie::pd_statemachine(void) {
|
254 |
|
|
next_state.write(state.read()); // Default don't change current state
|
255 |
|
|
pid_le_sm.write(false);
|
256 |
|
|
token_le_1.write(false);
|
257 |
|
|
token_le_2.write(false);
|
258 |
|
|
data_valid_d.write(false);
|
259 |
|
|
data_done.write(false);
|
260 |
|
|
seq_err.write(false);
|
261 |
|
|
pid_ack.write(false);
|
262 |
|
|
|
263 |
|
|
switch (state.read()) {// synopsys full_case parallel_case
|
264 |
|
|
case PD_IDLE: pid_le_sm.write(true);
|
265 |
|
|
if (rx_valid.read() && rx_active.read())
|
266 |
|
|
next_state.write(PD_ACTIVE);
|
267 |
|
|
break;
|
268 |
|
|
case PD_ACTIVE: // Received a ACK from host
|
269 |
|
|
if (pid_ACK.read() && !rx_err.read()) {
|
270 |
|
|
pid_ack.write(true);
|
271 |
|
|
if (!rx_active.read())
|
272 |
|
|
next_state.write(PD_IDLE);
|
273 |
|
|
|
274 |
|
|
// Receiving a TOKEN
|
275 |
|
|
} else if (pid_TOKEN.read() && rx_valid.read() && rx_active.read() &&
|
276 |
|
|
!rx_err.read()) {
|
277 |
|
|
token_le_1.write(true);
|
278 |
|
|
next_state.write(PD_TOKEN);
|
279 |
|
|
|
280 |
|
|
// Receiving DATA
|
281 |
|
|
} else if (pid_DATA.read() && rx_valid.read() && rx_active.read() &&
|
282 |
|
|
!rx_err.read()) {
|
283 |
|
|
data_valid_d.write(true);
|
284 |
|
|
next_state.write(PD_DATA);
|
285 |
|
|
|
286 |
|
|
// ERROR
|
287 |
|
|
} else if (!rx_active.read() || rx_err.read() ||
|
288 |
|
|
(rx_valid.read() && !(pid_TOKEN.read() || pid_DATA.read()))) {
|
289 |
|
|
seq_err.write(!rx_err.read());
|
290 |
|
|
if (!rx_active.read())
|
291 |
|
|
next_state.write(PD_IDLE);
|
292 |
|
|
}
|
293 |
|
|
break;
|
294 |
|
|
case PD_TOKEN: if (rx_valid.read() && rx_active.read() && !rx_err.read()) {
|
295 |
|
|
token_le_2.write(true);
|
296 |
|
|
next_state.write(PD_IDLE);
|
297 |
|
|
|
298 |
|
|
// ERROR
|
299 |
|
|
} else if (!rx_active.read() || rx_err.read()) {
|
300 |
|
|
seq_err.write(!rx_err.read());
|
301 |
|
|
if (!rx_active.read())
|
302 |
|
|
next_state.write(PD_IDLE);
|
303 |
|
|
}
|
304 |
|
|
break;
|
305 |
|
|
case PD_DATA: if (rx_valid.read() && rx_active.read() && !rx_err.read())
|
306 |
|
|
data_valid_d.write(true);
|
307 |
|
|
if (!rx_active.read() || rx_err.read()) {
|
308 |
|
|
data_done.write(true);
|
309 |
|
|
if (!rx_active.read())
|
310 |
|
|
next_state.write(PD_IDLE);
|
311 |
|
|
}
|
312 |
|
|
break;
|
313 |
|
|
}
|
314 |
|
|
}
|
315 |
|
|
/*
|
316 |
|
|
usb_pd_sie::~usb_pd_sie(void) {
|
317 |
|
|
if (i_crc5)
|
318 |
|
|
delete i_crc5;
|
319 |
|
|
if (i_crc16)
|
320 |
|
|
delete i_crc16;
|
321 |
|
|
}
|
322 |
|
|
*/
|