OpenCores
URL https://opencores.org/ocsvn/usb11/usb11/trunk

Subversion Repositories usb11

[/] [usb11/] [trunk/] [bench/] [systemc/] [usb_ocp_test.cpp] - Blame information for rev 15

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 alfoltran
/////////////////////////////////////////////////////////////////////
2
////                                                             ////
3
////  USB 1.1 Top Level Test Bench                               ////
4
////                                                             ////
5
////  SystemC Version: usb_test.cpp                              ////
6
////  Author: Alfredo Luiz Foltran Fialho                        ////
7
////          alfoltran@ig.com.br                                ////
8
////                                                             ////
9
////                                                             ////
10
/////////////////////////////////////////////////////////////////////
11
////                                                             ////
12
//// Verilog Version: test_bench_top.v + tests.v + tests_lib.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 <stdlib.h>
39
#include <time.h>
40
#include "systemc.h"
41
#include "usb_defines.h"
42
#include "usb_phy.h"
43
#include "usb_ocp.h"
44
 
45
#define VCD_OUTPUT_ENABLE
46
//#define WIF_OUTPUT_ENABLE
47
 
48
SC_MODULE(test) {
49
        sc_in<bool>                     clk, clk2;
50
        sc_out<bool>            rst;
51
        sc_in<bool>                     txdp;
52
        sc_in<bool>                     txdn;
53
        sc_out<bool>            rxdp;
54
        sc_out<bool>            rxdn;
55
        sc_out<sc_uint<8> >     dout;
56
        sc_out<bool>            tx_valid;
57
        sc_in<bool>                     tx_ready;
58
        sc_in<sc_uint<8> >      din;
59
        sc_in<bool>                     rx_valid;
60
        sc_in<bool>                     rx_active;
61
        sc_in<bool>                     rx_error;
62
 
63
        sc_in<bool>                     txdp2;
64
        sc_in<bool>                     txdn2;
65
        sc_out<bool>            rxdp2;
66
        sc_out<bool>            rxdn2;
67
        sc_in<bool>                     s_int;
68
        sc_in<sc_uint<8> >      s_flag;
69
        sc_in<bool>                     s_error;
70
        sc_out<sc_uint<32> >m_addr;
71
        sc_out<sc_uint<3> >     m_cmd;
72
        sc_out<sc_uint<8> >     m_data;
73
        sc_in<bool>                     s_cmd_accept;
74
        sc_in<sc_uint<8> >      s_data;
75
        sc_in<sc_uint<2> >      s_resp;
76
 
77
        // Signals
78
 
79
        sc_signal<bool>                 usb_reset;
80
        sc_signal<sc_uint<32> > wd_cnt;
81
        sc_signal<bool> setup_pid;
82
 
83
        // Local Vars
84
 
85
        sc_uint<8> txmem[2049];
86
        sc_uint<8> buffer0[16385];
87
        sc_uint<8> buffer1[16385];
88
        sc_uint<8> buffer1_last;
89
        int error_cnt;
90
        int i;
91
 
92
/////////////////////////////////////////////////////////////////////
93
////                                                             ////
94
////              Test Bench Library                             ////
95
////                                                             ////
96
/////////////////////////////////////////////////////////////////////
97
 
98
void show_errors(void) {
99
        cout << "+----------------------+" << endl;
100
        cout << "| TOTAL ERRORS: " << error_cnt << endl;
101
        cout << "+----------------------+" << endl << endl;
102
}
103
 
104
sc_uint<5> crc5(sc_uint<5> crc_in, sc_uint<11> din) {
105
        sc_uint<5> crc_out;
106
 
107
        crc_out[0] = din[10] ^ din[9] ^ din[6] ^ din[5] ^ din[3] ^
108
                        din[0] ^ crc_in[0] ^ crc_in[3] ^ crc_in[4];
109
        crc_out[1] = din[10] ^ din[7] ^ din[6] ^ din[4] ^ din[1] ^
110
                        crc_in[0] ^ crc_in[1] ^ crc_in[4];
111
        crc_out[2] = din[10] ^ din[9] ^ din[8] ^ din[7] ^ din[6] ^
112
                        din[3] ^ din[2] ^ din[0] ^ crc_in[0] ^
113
                        crc_in[1] ^ crc_in[2] ^ crc_in[3] ^ crc_in[4];
114
        crc_out[3] = din[10] ^ din[9] ^ din[8] ^ din[7] ^ din[4] ^
115
                        din[3] ^ din[1] ^ crc_in[1] ^ crc_in[2] ^
116
                        crc_in[3] ^ crc_in[4];
117
        crc_out[4] = din[10] ^ din[9] ^ din[8] ^ din[5] ^ din[4] ^
118
                        din[2] ^ crc_in[2] ^ crc_in[3] ^ crc_in[4];
119
 
120
        return crc_out;
121
}
122
 
123
sc_uint<16> crc16(sc_uint<16> crc_in, sc_uint<8> din) {
124
        sc_uint<16> crc_out;
125
 
126
        crc_out[0] = din[7] ^ din[6] ^ din[5] ^ din[4] ^ din[3] ^ din[2] ^
127
                        din[1] ^ din[0] ^ crc_in[8] ^ crc_in[9] ^ crc_in[10] ^
128
                        crc_in[11] ^ crc_in[12] ^ crc_in[13] ^ crc_in[14] ^
129
                        crc_in[15];
130
        crc_out[1] = din[7] ^ din[6] ^ din[5] ^ din[4] ^ din[3] ^ din[2] ^
131
                        din[1] ^ crc_in[9] ^ crc_in[10] ^ crc_in[11] ^ crc_in[12] ^
132
                        crc_in[13] ^ crc_in[14] ^ crc_in[15];
133
        crc_out[2] = din[1] ^ din[0] ^ crc_in[8] ^ crc_in[9];
134
        crc_out[3] = din[2] ^ din[1] ^ crc_in[9] ^ crc_in[10];
135
        crc_out[4] = din[3] ^ din[2] ^ crc_in[10] ^ crc_in[11];
136
        crc_out[5] = din[4] ^ din[3] ^ crc_in[11] ^ crc_in[12];
137
        crc_out[6] = din[5] ^ din[4] ^ crc_in[12] ^ crc_in[13];
138
        crc_out[7] = din[6] ^ din[5] ^ crc_in[13] ^ crc_in[14];
139
        crc_out[8] = din[7] ^ din[6] ^ crc_in[0] ^ crc_in[14] ^ crc_in[15];
140
        crc_out[9] = din[7] ^ crc_in[1] ^ crc_in[15];
141
        crc_out[10] = crc_in[2];
142
        crc_out[11] = crc_in[3];
143
        crc_out[12] = crc_in[4];
144
        crc_out[13] = crc_in[5];
145
        crc_out[14] = crc_in[6];
146
        crc_out[15] = din[7] ^ din[6] ^ din[5] ^ din[4] ^ din[3] ^
147
                        din[2] ^ din[1] ^ din[0] ^ crc_in[7] ^ crc_in[8] ^
148
                        crc_in[9] ^ crc_in[10] ^ crc_in[11] ^ crc_in[12] ^
149
                        crc_in[13] ^ crc_in[14] ^ crc_in[15];
150
 
151
        return crc_out;
152
}
153
 
154
void utmi_send_pack(int size) {
155
        int n;
156
 
157
        wait(clk.posedge_event());
158
        tx_valid.write(true);
159
        for (n = 0; n < size; n++) {
160
                dout.write(txmem[n]);
161
                wait(clk.posedge_event());
162
                while (!tx_ready.read())
163
                        wait(clk.posedge_event());
164
        }
165
        tx_valid.write(false);
166
        wait(clk.posedge_event());
167
}
168
 
169
void utmi_recv_pack(int *size) {
170
        *size = 0;
171
        while (!rx_active.read())
172
                wait(clk.posedge_event());
173
        while (rx_active.read()) {
174
                while (!rx_valid.read() && rx_active.read())
175
                        wait(clk.posedge_event());
176
                if (rx_valid.read() && rx_active.read()) {
177
                        txmem[*size] = din.read();
178
                        (*size)++;
179
                }
180
                wait(clk.posedge_event());
181
        }
182
}
183
 
184
void recv_packet(sc_uint<4> *pid, int *size) {
185
        int n;
186
        sc_uint<16> crc16r;
187
        sc_uint<8> x, y;
188
 
189
        crc16r = 0xffff;
190
        utmi_recv_pack(size);
191
 
192
        if (*size != 1) {
193
                for (n = 1; n < *size - 2; n++) {
194
                        y = txmem[n];
195
                        x = (   (sc_uint<1>)y[0],
196
                                        (sc_uint<1>)y[1],
197
                                        (sc_uint<1>)y[2],
198
                                        (sc_uint<1>)y[3],
199
                                                (sc_uint<1>)y[4],
200
                                        (sc_uint<1>)y[5],
201
                                        (sc_uint<1>)y[6],
202
                                        (sc_uint<1>)y[7]);
203
                        crc16r = crc16(crc16r, x);
204
                }
205
 
206
                crc16r = (      (sc_uint<1>)!crc16r[8],
207
                                        (sc_uint<1>)!crc16r[9],
208
                                        (sc_uint<1>)!crc16r[10],
209
                                        (sc_uint<1>)!crc16r[11],
210
                                        (sc_uint<1>)!crc16r[12],
211
                                        (sc_uint<1>)!crc16r[13],
212
                                        (sc_uint<1>)!crc16r[14],
213
                                        (sc_uint<1>)!crc16r[15],
214
                                        (sc_uint<1>)!crc16r[0],
215
                                        (sc_uint<1>)!crc16r[1],
216
                                        (sc_uint<1>)!crc16r[2],
217
                                        (sc_uint<1>)!crc16r[3],
218
                                        (sc_uint<1>)!crc16r[4],
219
                                        (sc_uint<1>)!crc16r[5],
220
                                        (sc_uint<1>)!crc16r[6],
221
                                        (sc_uint<1>)!crc16r[7]);
222
 
223
                if (crc16r != (sc_uint<16>)(txmem[n], txmem[n + 1]))
224
                        cout << "ERROR: CRC Mismatch: Expected: " << crc16r << ", Got: " <<
225
                                        txmem[n] << txmem[n + 1] << " (" << sc_simulation_time() << ")" << endl << endl;
226
 
227
                for (n = 0; n < *size - 3; n++)
228
                        buffer1[buffer1_last + n] = txmem[n + 1];
229
                buffer1_last = buffer1_last + n;
230
        } else {
231
                *size = 3;
232
        }
233
 
234
        x = txmem[0];
235
 
236
        if ((sc_uint<4>)x.range(7, 4) != (sc_uint<4>)~x.range(3, 0))
237
                cout << "ERROR: Pid Checksum mismatch: Top: " << (sc_uint<4>)x.range(7, 4) <<
238
                                " Bottom: " << (sc_uint<4>)x.range(3, 0) << " (" << sc_simulation_time() << ")" << endl << endl;
239
 
240
        *pid = (sc_uint<4>)x.range(3, 0);
241
        *size = *size - 3;
242
}
243
 
244
void send_token(sc_uint<7> fa, sc_uint<4> ep, sc_uint<4> pid) {
245
        sc_uint<16> tmp_data;
246
        sc_uint<11> x, y;
247
        int len;
248
 
249
        tmp_data = ((sc_uint<7>)fa, (sc_uint<4>)ep, (sc_uint<5>)0);
250
        if (pid == USBF_T_PID_ACK)
251
                len = 1;
252
        else
253
                len = 3;
254
 
255
        y = ((sc_uint<7>)fa, (sc_uint<4>)ep);
256
        x = (   (sc_uint<1>)y[4],
257
                        (sc_uint<1>)y[5],
258
                        (sc_uint<1>)y[6],
259
                        (sc_uint<1>)y[7],
260
                        (sc_uint<1>)y[8],
261
                        (sc_uint<1>)y[9],
262
                        (sc_uint<1>)y[10],
263
                        (sc_uint<1>)y[0],
264
                        (sc_uint<1>)y[1],
265
                        (sc_uint<1>)y[2],
266
                        (sc_uint<1>)y[3]);
267
 
268
        y = ((sc_uint<6>)0, (sc_uint<5>)crc5(0x1f, x));
269
        tmp_data = ((sc_uint<11>)x, (sc_uint<5>)~y.range(4, 0));
270
        txmem[0] = ((sc_uint<4>)~pid, (sc_uint<4>)pid);
271
        txmem[1] = (    (sc_uint<1>)tmp_data[8],
272
                                        (sc_uint<1>)tmp_data[9],
273
                                        (sc_uint<1>)tmp_data[10],
274
                                        (sc_uint<1>)tmp_data[11],
275
                                        (sc_uint<1>)tmp_data[12],
276
                                        (sc_uint<1>)tmp_data[13],
277
                                        (sc_uint<1>)tmp_data[14],
278
                                        (sc_uint<1>)tmp_data[15]);
279
        txmem[2] = (    (sc_uint<1>)tmp_data[0],
280
                                        (sc_uint<1>)tmp_data[1],
281
                                        (sc_uint<1>)tmp_data[2],
282
                                        (sc_uint<1>)tmp_data[3],
283
                                        (sc_uint<1>)tmp_data[4],
284
                                        (sc_uint<1>)tmp_data[5],
285
                                        (sc_uint<1>)tmp_data[6],
286
                                        (sc_uint<1>)tmp_data[7]);
287
 
288
        utmi_send_pack(len);
289
}
290
 
291
void send_sof(sc_uint<11> frmn) {
292
        sc_uint<16> tmp_data;
293
        sc_uint<11> x;
294
 
295
        x = (   (sc_uint<1>)frmn[0],
296
                        (sc_uint<1>)frmn[1],
297
                        (sc_uint<1>)frmn[2],
298
                        (sc_uint<1>)frmn[3],
299
                        (sc_uint<1>)frmn[4],
300
                        (sc_uint<1>)frmn[5],
301
                        (sc_uint<1>)frmn[6],
302
                        (sc_uint<1>)frmn[7],
303
                        (sc_uint<1>)frmn[8],
304
                        (sc_uint<1>)frmn[9],
305
                        (sc_uint<1>)frmn[10]);
306
 
307
        tmp_data = ((sc_uint<11>)x, (sc_uint<5>)~crc5(0x1f, x));
308
        txmem[0] = ((sc_uint<4>)~USBF_T_PID_SOF, (sc_uint<4>)USBF_T_PID_SOF);
309
//      txmem[1] = (    (sc_uint<1>)tmp_data[8],
310
//                                      (sc_uint<1>)tmp_data[9],
311
//                                      (sc_uint<1>)tmp_data[10],
312
//                                      (sc_uint<1>)tmp_data[11],
313
//                                      (sc_uint<1>)tmp_data[12],
314
//                                      (sc_uint<1>)tmp_data[13],
315
//                                      (sc_uint<1>)tmp_data[14],
316
//                                      (sc_uint<1>)tmp_data[15]);
317
//      txmem[2] = (    (sc_uint<1>)tmp_data[0],
318
//                                      (sc_uint<1>)tmp_data[1],
319
//                                      (sc_uint<1>)tmp_data[2],
320
//                                      (sc_uint<1>)tmp_data[3],
321
//                                      (sc_uint<1>)tmp_data[4],
322
//                                      (sc_uint<1>)tmp_data[5],
323
//                                      (sc_uint<1>)tmp_data[6],
324
//                                      (sc_uint<1>)tmp_data[7]);
325
        txmem[1] = (sc_uint<8>)frmn.range(7, 0);
326
        txmem[2] = (    (sc_uint<1>)tmp_data[0],
327
                                        (sc_uint<1>)tmp_data[1],
328
                                        (sc_uint<1>)tmp_data[2],
329
                                        (sc_uint<1>)tmp_data[3],
330
                                        (sc_uint<1>)tmp_data[4],
331
                                        (sc_uint<3>)frmn.range(10, 8));
332
 
333
        utmi_send_pack(3);
334
}
335
 
336
void send_data(sc_uint<4> pid, int len, int mode) {
337
        int n;
338
        sc_uint<16> crc16r;
339
        sc_uint<8> x, y;
340
 
341
        txmem[0] = ((sc_uint<4>)~pid, (sc_uint<4>)pid);
342
        crc16r = 0xffff;
343
        for (n = 0; n < len; n++) {
344
                if (mode == 1)
345
                        y = buffer1[buffer1_last + n];
346
                else
347
                        y = n;
348
                x = (   (sc_uint<1>)y[0],
349
                                (sc_uint<1>)y[1],
350
                                (sc_uint<1>)y[2],
351
                                (sc_uint<1>)y[3],
352
                                (sc_uint<1>)y[4],
353
                                (sc_uint<1>)y[5],
354
                                (sc_uint<1>)y[6],
355
                                (sc_uint<1>)y[7]);
356
                txmem[n + 1] = y;
357
                crc16r = crc16(crc16r, x);
358
        }
359
 
360
        buffer1_last = buffer1_last + n;
361
        y = (sc_uint<8>)crc16r.range(15, 8);
362
        txmem[n + 1] = (        (sc_uint<1>)!y[0],
363
                                                (sc_uint<1>)!y[1],
364
                                                (sc_uint<1>)!y[2],
365
                                                (sc_uint<1>)!y[3],
366
                                                (sc_uint<1>)!y[4],
367
                                                (sc_uint<1>)!y[5],
368
                                                (sc_uint<1>)!y[6],
369
                                                (sc_uint<1>)!y[7]);
370
        y = (sc_uint<8>)crc16r.range(7, 0);
371
        txmem[n + 2] = (        (sc_uint<1>)!y[0],
372
                                                (sc_uint<1>)!y[1],
373
                                                (sc_uint<1>)!y[2],
374
                                                (sc_uint<1>)!y[3],
375
                                                (sc_uint<1>)!y[4],
376
                                                (sc_uint<1>)!y[5],
377
                                                (sc_uint<1>)!y[6],
378
                                                (sc_uint<1>)!y[7]);
379
 
380
        utmi_send_pack(len + 3);
381
}
382
 
383
/////////////////////////////////////////////////////////////////////
384
 
385
/////////////////////////////////////////////////////////////////////
386
////                                                             ////
387
////              Test Case Collection                           ////
388
////                                                             ////
389
/////////////////////////////////////////////////////////////////////
390
 
391
void send_setup(        sc_uint<7> fa,
392
                                        sc_uint<8> req_type,
393
                                        sc_uint<8> request,
394
                                        sc_uint<16> wValue,
395
                                        sc_uint<16> wIndex,
396
                                        sc_uint<16> wLength) {
397
        int len;
398
 
399
        buffer1[0] = req_type;
400
        buffer1[1] = request;
401
        buffer1[3] = (sc_uint<8>)wValue.range(15, 8);
402
        buffer1[2] = (sc_uint<8>)wValue.range(7, 0);
403
        buffer1[5] = (sc_uint<8>)wIndex.range(15, 8);
404
        buffer1[4] = (sc_uint<8>)wIndex.range(7, 0);
405
        buffer1[7] = (sc_uint<8>)wLength.range(15, 8);
406
        buffer1[6] = (sc_uint<8>)wLength.range(7, 0);
407
 
408
        buffer1_last = 0;
409
 
410
        send_token(fa, 0, USBF_T_PID_SETUP);
411
 
412
        wait(clk.posedge_event());
413
 
414
        send_data(USBF_T_PID_DATA0, 8, 1);
415
 
416
        utmi_recv_pack(&len);
417
 
418
        if (txmem[0] != 0xd2) {
419
                cout << "ERROR: SETUP: ACK mismatch. Expected: 0xD2, Got: " << txmem[0] <<
420
                                " (" << sc_simulation_time() << ")" << endl << endl;
421
                error_cnt++;
422
        }
423
 
424
        if (len != 1) {
425
                cout << "ERROR: SETUP: ACK mismatch. Expected: 1, Got: " << len <<
426
                                " (" << sc_simulation_time() << ")" << endl << endl;
427
                error_cnt++;
428
        }
429
 
430
        wait(clk.posedge_event());
431
        setup_pid.write(true);
432
        wait(clk.posedge_event());
433
}
434
 
435
void data_in(sc_uint<7> fa, int pl_size) {
436
        int rlen;
437
        sc_uint<4> pid, expect_pid;
438
 
439
        buffer1_last = 0;
440
        for (i = 0; i < 5; i++)
441
                wait(clk.posedge_event());
442
        send_token(fa, 0, USBF_T_PID_IN);
443
 
444
        recv_packet(&pid, &rlen);
445
        if (setup_pid.read())
446
                expect_pid = 0xb;       // DATA 1
447
        else
448
                expect_pid = 0x3;       // DATA 0
449
 
450
        if (pid != expect_pid) {
451
                cout << "ERROR: Data IN PID mismatch. Expected: " << expect_pid << ", Got: " << pid <<
452
                                " (" << sc_simulation_time() << ")" << endl << endl;
453
                error_cnt++;
454
        }
455
 
456
        setup_pid.write(!setup_pid.read());
457
        if (rlen != pl_size) {
458
                cout << "ERROR: Data IN Size mismatch. Expected: " << pl_size << ", Got: " << rlen <<
459
                                " (" << sc_simulation_time() << ")" << endl << endl;
460
                error_cnt++;
461
        }
462
 
463
        for (i = 0; i < rlen; i++) {
464
                cout << "RCV Data[" << i << "]: 0x";
465
                printf("%02x", (unsigned int)buffer1[i]);
466
                cout << endl;
467
        }
468
        cout << endl;
469
 
470
        for (i = 0; i < 5; i++)
471
                wait(clk.posedge_event());
472
        send_token(fa, 0, USBF_T_PID_ACK);
473
 
474
        for (i = 0; i < 5; i++)
475
                wait(clk.posedge_event());
476
}
477
 
478
void data_out(sc_uint<7> fa, int pl_size) {
479
        int len;
480
 
481
        send_token(fa, 0, USBF_T_PID_OUT);
482
 
483
        wait(clk.posedge_event());
484
 
485
        if (!setup_pid.read())
486
                send_data(USBF_T_PID_DATA0, pl_size, 1);
487
        else
488
                send_data(USBF_T_PID_DATA1, pl_size, 1);
489
 
490
        setup_pid.write(!setup_pid.read());
491
 
492
        utmi_recv_pack(&len);
493
 
494
        if (txmem[0] != 0xd2) {
495
                cout << "ERROR: Ack mismatch. Expected: 0xd2, Got: " << txmem[0] <<
496
                                " (" << sc_simulation_time() << ")" << endl << endl;
497
                error_cnt++;
498
        }
499
 
500
        if (len != 1) {
501
                cout << "ERROR: SETUP: Length mismatch. Expected: 1, Got: " << len <<
502
                                " (" << sc_simulation_time() << ")" << endl << endl;
503
                error_cnt++;
504
        }
505
 
506
        for (i = 0; i < 5; i++)
507
                wait(clk.posedge_event());
508
}
509
 
510
// Enumeration Test -> Endpoint 0
511
void setup0(void) {
512
        cout << endl;
513
 
514
        cout << "The Default Time Unit is: " << sc_get_default_time_unit().to_string() << endl << endl;
515
 
516
        cout << "**************************************************" << endl;
517
        cout << "*** CONTROL EP TEST 0                          ***" << endl;
518
        cout << "**************************************************" << endl << endl;
519
 
520
        cout << "Setting Address ..." << endl << endl;
521
 
522
        send_setup(0x00, 0x00, SET_ADDRESS, 0x0012, 0x0000, 0x0000);
523
        data_in(0x00,0);
524
 
525
        cout << "Getting Descriptor ..." << endl << endl;
526
 
527
        send_setup(0x12, 0x80, GET_DESCRIPTOR, 0x0100, 0x0000, 0x0012);
528
        data_in(0x12, 18);
529
        data_out(0x12, 0);
530
 
531
        cout << "Getting Descriptor ..." << endl << endl;
532
 
533
        send_setup(0x12, 0x80, GET_DESCRIPTOR, 0x0200, 0x0000, 0x0009);
534
        data_in(0x12, 9);
535
        data_out(0x12, 0);
536
 
537
        cout << "Getting Descriptor ..." << endl << endl;
538
 
539
        send_setup(0x12, 0x80, GET_DESCRIPTOR, 0x0200, 0x0000, 0x003c);
540
        data_in(0x12, 60);
541
        data_out(0x12, 0);
542
 
543
        cout << "Getting Descriptor ..." << endl << endl;
544
 
545
        send_setup(0x12, 0x80, GET_DESCRIPTOR, 0x0300, 0x0000, 0x0008);
546
        data_in(0x12, 8);
547
        data_out(0x12, 0);
548
 
549
        cout << "Getting Descriptor ..." << endl << endl;
550
 
551
        send_setup(0x12, 0x80, GET_DESCRIPTOR, 0x0301, 0x0416, 0x001a);
552
        data_in(0x12, 26);
553
        data_out(0x12, 0);
554
 
555
        cout << "Getting Descriptor ..." << endl << endl;
556
 
557
        send_setup(0x12, 0x80, GET_DESCRIPTOR, 0x0302, 0x0416, 0x001c);
558
        data_in(0x12, 28);
559
        data_out(0x12, 0);
560
 
561
        cout << "Getting Descriptor ..." << endl << endl;
562
 
563
        send_setup(0x12, 0x80, GET_DESCRIPTOR, 0x0303, 0x0416, 0x0036);
564
        data_in(0x12, 54);
565
        data_out(0x12, 0);
566
 
567
        show_errors();
568
 
569
        cout << "**************************************************" << endl;
570
        cout << "*** TEST DONE ...                              ***" << endl;
571
        cout << "**************************************************" << endl << endl;
572
}
573
 
574
// ISO IN Test -> Endpoint 1
575
void in1(void) {
576
        sc_uint<7> my_fa;
577
        int rlen, fc;
578
        sc_uint<8> fd;
579
        int pack_cnt, pack_cnt_max, pack_sz, pack_sz_max;
580
        sc_uint<8> x;
581
        sc_uint<4> pid, expect_pid;
582
        sc_uint<32> data;
583
//      bool pid_cnt;
584
 
585
        cout << endl;
586
 
587
        cout << "**************************************************" << endl;
588
        cout << "*** ISOCHRONOUS IN EP TEST 1                   ***" << endl;
589
        cout << "**************************************************" << endl << endl;
590
 
591
        send_sof(0x000);
592
 
593
        pack_sz_max = 256;
594
        pack_cnt_max = 4;
595
 
596
//      pid_cnt = false;
597
        my_fa = 0x12;
598
 
599
        m_addr.write(0x00000001);
600
        m_cmd.write(0x0);
601
 
602
        for (pack_sz = 0; pack_sz <= pack_sz_max; pack_sz += 32) {
603
                cout << "PL Size: " << pack_sz << endl;
604
 
605
                for (pack_cnt = 0; pack_cnt < pack_cnt_max; pack_cnt++) {
606
                        buffer1_last = 0;
607
                        for (fc = 0; fc < pack_sz; fc++) {
608
                                while (s_flag.read()[1])
609
                                        wait(clk.posedge_event());
610
 
611
                                x = (sc_uint<8>)(255.0 * rand() / (RAND_MAX + 1.0));
612
                                m_data.write(x);
613
                                buffer0[fc] = x;
614
                                m_cmd.write(0x1);
615
                                wait(clk.posedge_event());
616
                                m_cmd.write(0x0);
617
                                wait(clk.posedge_event());
618
                        }
619
                        m_cmd.write(0x0);
620
                        wait(clk.posedge_event());
621
 
622
                        // Send Data
623
                        wait(clk.posedge_event());
624
                        send_sof(0x000);
625
                        wait(clk.posedge_event());
626
                        send_token(my_fa, 1, USBF_T_PID_IN);
627
                        wait(clk.posedge_event());
628
 
629
                        recv_packet(&pid, &rlen);
630
 
631
//                      if (pid_cnt)
632
//                              expect_pid = 0xb;
633
//                      else
634
//                              expect_pid = 0x3;
635
                        expect_pid = 0x3;
636
 
637
                        if (pid != expect_pid) {
638
                                cout << "ERROR: PID mismatch. Expected: " << expect_pid << ", Got: " << pid <<
639
                                                " (" << sc_simulation_time() << ")" << endl << endl;
640
                                error_cnt++;
641
                        }
642
//                      pid_cnt = !pid_cnt;
643
 
644
                        if (rlen != pack_sz) {
645
                                cout << "ERROR: Size mismatch. Expected: " << pack_sz << ", Got: " << rlen <<
646
                                                " (" << sc_simulation_time() << ")" << endl << endl;
647
                                error_cnt++;
648
                        }
649
 
650
                        for (i = 0; i < 4; i++)
651
                                wait(clk.posedge_event());
652
 
653
                        // Verify Data
654
                        for (fc = 0; fc < pack_sz; fc++) {
655
                                x = buffer0[fc];
656
                                if (buffer1[fc] != x) {
657
                                        cout << "ERROR: Data (" << fc << ") mismatch. Expected: " << x << ", Got: " << buffer1[fc] <<
658
                                                        " (" << sc_simulation_time() << ")" << endl << endl;
659
                                        error_cnt++;
660
                                }
661
                        }
662
                }
663
                for (i = 0; i < 50; i++)
664
                        wait(clk.posedge_event());
665
        }
666
        m_cmd.write(0x4);
667
        m_addr.write(0x00000000);
668
 
669
        cout << endl;
670
 
671
        show_errors();
672
 
673
        cout << "**************************************************" << endl;
674
        cout << "*** TEST DONE ...                              ***" << endl;
675
        cout << "**************************************************" << endl << endl;
676
}
677
 
678
// ISO OUT Test -> Endpoint 2
679
void out2(void) {
680
        sc_uint<7> my_fa;
681
        sc_uint<32> data;
682
        int n, no_pack, no_pack_max, pl_sz, pl_sz_max;
683
        bool pid;
684
        sc_uint<8> x;
685
 
686
        cout << endl;
687
 
688
        cout << "**************************************************" << endl;
689
        cout << "*** ISOCHRONOUS OUT EP TEST 2                  ***" << endl;
690
        cout << "**************************************************" << endl << endl;
691
 
692
        no_pack_max = 4;
693
        pl_sz_max = 256;
694
 
695
        my_fa = 0x12;
696
 
697
        m_addr.write(0x00000002);
698
        m_cmd.write(0x0);
699
 
700
        for (pl_sz = 0; pl_sz <= pl_sz_max; pl_sz += 32) {
701
                pid = false;
702
 
703
                cout << "PL Size: " << pl_sz << endl;
704
 
705
                for (n = 0; n < 4096; n++)
706
                        buffer1[n] = n;
707
 
708
                buffer1_last = 0;
709
 
710
                no_pack = 0;
711
                while (true) {
712
                        wait(clk.posedge_event());
713
                        send_sof(0x000);
714
                        wait(clk.posedge_event());
715
 
716
                        send_token(my_fa, 2, USBF_T_PID_OUT);
717
                        wait(clk.posedge_event());
718
 
719
                        if (!pid)
720
                                send_data(USBF_T_PID_DATA0, pl_sz, 1);
721
                        else
722
                                send_data(USBF_T_PID_DATA1, pl_sz, 1);
723
                        for (i = 0; i < 10; i++)
724
                                wait(clk2.posedge_event());
725
                        for (n = 0; n < pl_sz; n++) {
726
                                m_cmd.write(0x0);
727
                                wait(clk2.posedge_event());
728
                                wait(clk2.posedge_event());
729
 
730
                                while (s_flag.read()[0]) {
731
                                        m_cmd.write(0x0);
732
                                        wait(clk2.posedge_event());
733
                                        wait(clk2.posedge_event());
734
                                }
735
 
736
                                if (buffer1[n + (pl_sz * no_pack)] != s_data.read()) {
737 12 alfoltran
                                        cout << "ERROR: DATA mismatch. Expected: " << buffer1[n + (pl_sz * no_pack)] << ", Got: " << s_data.read() <<
738 2 alfoltran
                                                " (" << sc_simulation_time() << ")" << endl << endl;
739
                                        error_cnt++;
740
                                }
741
 
742
                                m_cmd.write(0x2);
743
                                wait(clk2.posedge_event());
744
                                wait(clk2.negedge_event());
745
                        }
746
                        m_cmd.write(0x0);
747
                        wait(clk2.posedge_event());
748
 
749
                        no_pack++;
750
                        if (no_pack == no_pack_max)
751
                                break;
752
                }
753
                wait(clk.posedge_event());
754
        }
755
 
756
        m_cmd.write(0x4);
757
        m_addr.write(0x00000000);
758
 
759
        cout << endl;
760
 
761
        show_errors();
762
 
763
        cout << "**************************************************" << endl;
764
        cout << "*** TEST DONE ...                              ***" << endl;
765
        cout << "**************************************************" << endl << endl;
766
}
767
 
768
// BULK IN Test -> Endpoint 3
769
void in3(void) {
770
        sc_uint<7> my_fa;
771
        int rlen, fc;
772
        sc_uint<8> fd;
773
        int pack_cnt, pack_cnt_max, pack_sz, pack_sz_max;
774
        sc_uint<8> x;
775
        sc_uint<4> pid, expect_pid;
776
        sc_uint<32> data;
777
        bool pid_cnt;
778
 
779
        cout << endl;
780
 
781
        cout << "**************************************************" << endl;
782
        cout << "*** BULK IN EP TEST 3                          ***" << endl;
783
        cout << "**************************************************" << endl << endl;
784
 
785
        send_sof(0x000);
786
 
787
        pack_sz_max = 64;
788
        pack_cnt_max = 4;
789
 
790
        pid_cnt = false;
791
        my_fa = 0x12;
792
 
793
        m_addr.write(0x00000003);
794
        m_cmd.write(0x0);
795
 
796
        for (pack_sz = 0; pack_sz <= pack_sz_max; pack_sz += 8) {
797
                cout << "PL Size: " << pack_sz << endl;
798
 
799
                for (pack_cnt = 0; pack_cnt < pack_cnt_max; pack_cnt++) {
800
                        buffer1_last = 0;
801
                        for (fc = 0; fc < pack_sz; fc++) {
802
                                while (s_flag.read()[1])
803
                                        wait(clk.posedge_event());
804
 
805
                                x = (sc_uint<8>)(255.0 * rand() / (RAND_MAX + 1.0));
806
                                m_data.write(x);
807
                                buffer0[fc] = x;
808
                                m_cmd.write(0x1);
809
                                wait(clk.posedge_event());
810
                                m_cmd.write(0x0);
811
                                wait(clk.posedge_event());
812
                        }
813
                        m_cmd.write(0x0);
814
                        wait(clk.posedge_event());
815
 
816
                        // Send Data
817
                        wait(clk.posedge_event());
818
                        send_sof(0x000);
819
                        wait(clk.posedge_event());
820
                        send_token(my_fa, 3, USBF_T_PID_IN);
821
                        wait(clk.posedge_event());
822
 
823
                        recv_packet(&pid, &rlen);
824
 
825
                        if (pid_cnt)
826
                                expect_pid = 0xb;
827
                        else
828
                                expect_pid = 0x3;
829
//                      expect_pid = 0x3;
830
 
831
                        if (pid != expect_pid) {
832
                                cout << "ERROR: PID mismatch. Expected: " << expect_pid << ", Got: " << pid <<
833
                                                " (" << sc_simulation_time() << ")" << endl << endl;
834
                                error_cnt++;
835
                        }
836
                        pid_cnt = !pid_cnt;
837
 
838
                        if (rlen != pack_sz) {
839
                                cout << "ERROR: Size mismatch. Expected: " << pack_sz << ", Got: " << rlen <<
840
                                                " (" << sc_simulation_time() << ")" << endl << endl;
841
                                error_cnt++;
842
                        }
843
 
844
                        for (i = 0; i < 4; i++)
845
                                wait(clk.posedge_event());
846
 
847
                        send_token(my_fa, 3, USBF_T_PID_ACK);
848
 
849
                        for (i = 0; i < 5; i++)
850
                                wait(clk.posedge_event());
851
 
852
                        // Verify Data
853
                        for (fc = 0; fc < pack_sz; fc++) {
854
                                x = buffer0[fc];
855
                                if (buffer1[fc] != x) {
856
                                        cout << "ERROR: Data (" << fc << ") mismatch. Expected: " << x << ", Got: " << buffer1[fc] <<
857
                                                        " (" << sc_simulation_time() << ")" << endl << endl;
858
                                        error_cnt++;
859
                                }
860
                        }
861
                }
862
                for (i = 0; i < 50; i++)
863
                        wait(clk.posedge_event());
864
        }
865
        m_cmd.write(0x4);
866
        m_addr.write(0x00000000);
867
 
868
        cout << endl;
869
 
870
        show_errors();
871
 
872
        cout << "**************************************************" << endl;
873
        cout << "*** TEST DONE ...                              ***" << endl;
874
        cout << "**************************************************" << endl << endl;
875
}
876
 
877
// BULK OUT Test -> Endpoint 4
878
void out4(void) {
879
        sc_uint<7> my_fa;
880
        sc_uint<32> data;
881
        int n, len, no_pack, no_pack_max, pl_sz, pl_sz_max;
882
        bool pid;
883
        sc_uint<8> x;
884
 
885
        cout << endl;
886
 
887
        cout << "**************************************************" << endl;
888
        cout << "*** BULK OUT EP TEST 4                         ***" << endl;
889
        cout << "**************************************************" << endl << endl;
890
 
891
        no_pack_max = 4;
892
        pl_sz_max = 64;
893
 
894
        my_fa = 0x12;
895
 
896
        m_addr.write(0x00000004);
897
        m_cmd.write(0x0);
898
 
899
        for (pl_sz = 0; pl_sz <= pl_sz_max; pl_sz += 8) {
900
                pid = false;
901
 
902
                cout << "PL Size: " << pl_sz << endl;
903
 
904
                for (n = 0; n < 4096; n++)
905
                        buffer1[n] = n;
906
 
907
                buffer1_last = 0;
908
 
909
                no_pack = 0;
910
                while (true) {
911
                        wait(clk.posedge_event());
912
                        send_sof(0x000);
913
                        wait(clk.posedge_event());
914
 
915
                        send_token(my_fa, 4, USBF_T_PID_OUT);
916
                        wait(clk.posedge_event());
917
 
918
                        if (!pid)
919
                                send_data(USBF_T_PID_DATA0, pl_sz, 1);
920
                        else
921
                                send_data(USBF_T_PID_DATA1, pl_sz, 1);
922
                        pid = !pid;
923
 
924
                        utmi_recv_pack(&len);
925
 
926
                        if (txmem[0] != 0xd2) {
927
                                cout << "ERROR: ACK mismatch. Expected: 0xd2, Got: " << txmem[0] <<
928
                                                " (" << sc_simulation_time() << ")" << endl << endl;
929
                                error_cnt++;
930
                        }
931
 
932
                        if (len != 1) {
933
                                cout << "ERROR: Size mismatch. Expected: 1, Got: " << len <<
934
                                                " (" << sc_simulation_time() << ")" << endl << endl;
935
                                error_cnt++;
936
                        }
937
 
938
                        wait(clk.posedge_event());
939
 
940
                        for (i = 0; i < 10; i++)
941
                                wait(clk2.posedge_event());
942
                        for (n = 0; n < pl_sz; n++) {
943
                                m_cmd.write(0x0);
944
                                wait(clk2.posedge_event());
945
                                wait(clk2.posedge_event());
946
 
947
                                while (s_flag.read()[0]) {
948
                                        m_cmd.write(0x0);
949
                                        wait(clk2.posedge_event());
950
                                        wait(clk2.posedge_event());
951
                                }
952
 
953
                                if (buffer1[n + (pl_sz * no_pack)] != s_data.read()) {
954 12 alfoltran
                                        cout << "ERROR: DATA mismatch. Expected: " << buffer1[n + (pl_sz * no_pack)] << ", Got: " << s_data.read() <<
955 2 alfoltran
                                                " (" << sc_simulation_time() << ")" << endl << endl;
956
                                        error_cnt++;
957
                                }
958
 
959
                                m_cmd.write(0x2);
960
                                wait(clk2.posedge_event());
961
                                wait(clk2.negedge_event());
962
                        }
963
                        m_cmd.write(0x0);
964
                        wait(clk2.posedge_event());
965
 
966
                        no_pack++;
967
                        if (no_pack == no_pack_max)
968
                                break;
969
                }
970
                wait(clk.posedge_event());
971
        }
972
 
973
        m_cmd.write(0x4);
974
        m_addr.write(0x00000000);
975
 
976
        cout << endl;
977
 
978
        show_errors();
979
 
980
        cout << "**************************************************" << endl;
981
        cout << "*** TEST DONE ...                              ***" << endl;
982
        cout << "**************************************************" << endl << endl;
983
}
984
 
985
// INT IN Test -> Endpoint 5
986
void in5(void) {
987
        sc_uint<7> my_fa;
988
        int rlen, fc;
989
        sc_uint<8> fd;
990
        int pack_cnt, pack_cnt_max, pack_sz, pack_sz_max;
991
        sc_uint<8> x;
992
        sc_uint<4> pid, expect_pid;
993
        sc_uint<32> data;
994
        bool pid_cnt;
995
 
996
        cout << endl;
997
 
998
        cout << "**************************************************" << endl;
999
        cout << "*** INTERRUPT IN EP TEST 5                     ***" << endl;
1000
        cout << "**************************************************" << endl << endl;
1001
 
1002
        send_sof(0x000);
1003
 
1004
        pack_sz_max = 64;
1005
        pack_cnt_max = 4;
1006
 
1007
        pid_cnt = false;
1008
        my_fa = 0x12;
1009
 
1010
        m_addr.write(0x00000005);
1011
                m_cmd.write(0x0);
1012
 
1013
        for (pack_sz = 0; pack_sz <= pack_sz_max; pack_sz += 8) {
1014
                cout << "PL Size: " << pack_sz << endl;
1015
 
1016
                for (pack_cnt = 0; pack_cnt < pack_cnt_max; pack_cnt++) {
1017
                        buffer1_last = 0;
1018
                        for (fc = 0; fc < pack_sz; fc++) {
1019
                                while (s_flag.read()[1])
1020
                                        wait(clk.posedge_event());
1021
 
1022
                                x = (sc_uint<8>)(255.0 * rand() / (RAND_MAX + 1.0));
1023
                                m_data.write(x);
1024
                                buffer0[fc] = x;
1025
                                m_cmd.write(0x1);
1026
                                wait(clk.posedge_event());
1027
                                m_cmd.write(0x0);
1028
                                wait(clk.posedge_event());
1029
                        }
1030
                        m_cmd.write(0x0);
1031
                        wait(clk.posedge_event());
1032
 
1033
                        // Send Data
1034
                        wait(clk.posedge_event());
1035
                        send_sof(0x000);
1036
                        wait(clk.posedge_event());
1037
                        send_token(my_fa, 5, USBF_T_PID_IN);
1038
                        wait(clk.posedge_event());
1039
 
1040
                        recv_packet(&pid, &rlen);
1041
 
1042
                        if (pack_sz == 0)
1043
                                expect_pid = 0xa;
1044
                        else if (pid_cnt)
1045
                                expect_pid = 0xb;
1046
                        else
1047
                                expect_pid = 0x3;
1048
//                      expect_pid = 0x3;
1049
 
1050
                        if (pid != expect_pid) {
1051
                                cout << "ERROR: PID mismatch. Expected: " << expect_pid << ", Got: " << pid <<
1052
                                                " (" << sc_simulation_time() << ")" << endl << endl;
1053
                                error_cnt++;
1054
                        }
1055
                        pid_cnt = !pid_cnt;
1056
 
1057
                        if (rlen != pack_sz) {
1058
                                cout << "ERROR: Size mismatch. Expected: " << pack_sz << ", Got: " << rlen <<
1059
                                                " (" << sc_simulation_time() << ")" << endl << endl;
1060
                                error_cnt++;
1061
                        }
1062
 
1063
                        for (i = 0; i < 4; i++)
1064
                                wait(clk.posedge_event());
1065
 
1066 5 alfoltran
                        if (pack_sz != 0) {
1067
                                send_token(my_fa, 5, USBF_T_PID_ACK);
1068 2 alfoltran
 
1069 5 alfoltran
                                for (i = 0; i < 5; i++)
1070
                                        wait(clk.posedge_event());
1071
                        }
1072 2 alfoltran
 
1073
                        // Verify Data
1074
                        for (fc = 0; fc < pack_sz; fc++) {
1075
                                x = buffer0[fc];
1076
                                if (buffer1[fc] != x) {
1077
                                        cout << "ERROR: Data (" << fc << ") mismatch. Expected: " << x << ", Got: " << buffer1[fc] <<
1078
                                                        " (" << sc_simulation_time() << ")" << endl << endl;
1079
                                        error_cnt++;
1080
                                }
1081
                        }
1082
                }
1083
                for (i = 0; i < 50; i++)
1084
                        wait(clk.posedge_event());
1085
        }
1086
        m_cmd.write(0x4);
1087
        m_addr.write(0x00000000);
1088
 
1089
        cout << endl;
1090
 
1091
        show_errors();
1092
 
1093
        cout << "**************************************************" << endl;
1094
        cout << "*** TEST DONE ...                              ***" << endl;
1095
        cout << "**************************************************" << endl << endl;
1096
}
1097
 
1098
// INT OUT Test -> Endpoint 6
1099
void out6(void) {
1100
        sc_uint<7> my_fa;
1101
        sc_uint<32> data;
1102
        int n, len, no_pack, no_pack_max, pl_sz, pl_sz_max;
1103
        bool pid;
1104
        sc_uint<8> x;
1105
 
1106
        cout << endl;
1107
 
1108
        cout << "**************************************************" << endl;
1109
        cout << "*** INTERRUPT OUT EP TEST 6                    ***" << endl;
1110
        cout << "**************************************************" << endl << endl;
1111
 
1112
        no_pack_max = 4;
1113
        pl_sz_max = 64;
1114
 
1115
        my_fa = 0x12;
1116
 
1117
        m_addr.write(0x00000006);
1118
        m_cmd.write(0x0);
1119
 
1120
        for (pl_sz = 0; pl_sz <= pl_sz_max; pl_sz += 8) {
1121
                pid = false;
1122
 
1123
                cout << "PL Size: " << pl_sz << endl;
1124
 
1125
                for (n = 0; n < 4096; n++)
1126
                        buffer1[n] = n;
1127
 
1128
                buffer1_last = 0;
1129
 
1130
                no_pack = 0;
1131
                while (true) {
1132
                        wait(clk.posedge_event());
1133
                        send_sof(0x000);
1134
                        wait(clk.posedge_event());
1135
 
1136
                        send_token(my_fa, 6, USBF_T_PID_OUT);
1137
                        wait(clk.posedge_event());
1138
 
1139
                        if (!pid)
1140
                                send_data(USBF_T_PID_DATA0, pl_sz, 1);
1141
                        else
1142
                                send_data(USBF_T_PID_DATA1, pl_sz, 1);
1143
                        pid = !pid;
1144
 
1145
                        utmi_recv_pack(&len);
1146
 
1147
                        if (txmem[0] != 0xd2) {
1148
                                cout << "ERROR: ACK mismatch. Expected: 0xd2, Got: " << txmem[0] <<
1149
                                                " (" << sc_simulation_time() << ")" << endl << endl;
1150
                                error_cnt++;
1151
                        }
1152
 
1153
                        if (len != 1) {
1154
                                cout << "ERROR: Size mismatch. Expected: 1, Got: " << len <<
1155
                                                " (" << sc_simulation_time() << ")" << endl << endl;
1156
                                error_cnt++;
1157
                        }
1158
 
1159
                        wait(clk.posedge_event());
1160
 
1161
                        for (i = 0; i < 10; i++)
1162
                                wait(clk2.posedge_event());
1163
                        for (n = 0; n < pl_sz; n++) {
1164
                                m_cmd.write(0x0);
1165
                                wait(clk2.posedge_event());
1166
                                wait(clk2.posedge_event());
1167
 
1168
                                while (s_flag.read()[0]) {
1169
                                        m_cmd.write(0x0);
1170
                                        wait(clk2.posedge_event());
1171
                                        wait(clk2.posedge_event());
1172
                                }
1173
 
1174
                                if (buffer1[n + (pl_sz * no_pack)] != s_data.read()) {
1175 12 alfoltran
                                        cout << "ERROR: DATA mismatch. Expected: " << buffer1[n + (pl_sz * no_pack)] << ", Got: " << s_data.read() <<
1176 2 alfoltran
                                                " (" << sc_simulation_time() << ")" << endl << endl;
1177
                                        error_cnt++;
1178
                                }
1179
 
1180
                                m_cmd.write(0x2);
1181
                                wait(clk2.posedge_event());
1182
                                wait(clk2.negedge_event());
1183
                        }
1184
                        m_cmd.write(0x0);
1185
                        wait(clk2.posedge_event());
1186
 
1187
                        no_pack++;
1188
                        if (no_pack == no_pack_max)
1189
                                break;
1190
                }
1191
                wait(clk.posedge_event());
1192
        }
1193
 
1194
        m_cmd.write(0x4);
1195
        m_addr.write(0x00000000);
1196
 
1197
        cout << endl;
1198
 
1199
        show_errors();
1200
 
1201
        cout << "**************************************************" << endl;
1202
        cout << "*** TEST DONE ...                              ***" << endl;
1203
        cout << "**************************************************" << endl << endl;
1204
}
1205
 
1206
/////////////////////////////////////////////////////////////////////
1207
 
1208
        void rx1_update(void) {
1209
                rxdp.write(!usb_reset.read() && txdp2.read());
1210
                rxdn.write(!usb_reset.read() && txdn2.read());
1211
        }
1212
 
1213
        void rx2_update(void) {
1214
                rxdp2.write(!usb_reset.read() && txdp.read());
1215
                rxdn2.write(!usb_reset.read() && txdn.read());
1216
        }
1217
 
1218
        void watchdog(void) {
1219
                if (txdp.read() || txdp2.read())
1220
                        wd_cnt.write(0);
1221
                else
1222
                        wd_cnt.write(wd_cnt.read() + 1);
1223
        }
1224
 
1225
        void wd_cnt_mon(void) {
1226
                if (wd_cnt.read() > 5000) {
1227
                        cout << "**********************************" << endl;
1228
                        cout << "ERROR: Watch Dog Counter Expired" << endl;
1229
                        cout << "**********************************" << endl << endl;
1230
                        sc_stop();
1231
                }
1232
        }
1233
 
1234
        void init(void) {
1235
                usb_reset.write(false);
1236
                tx_valid.write(false);
1237
                error_cnt = 0;
1238
                wd_cnt.write(0);
1239
                rst.write(false);
1240
                m_cmd.write(0x4);
1241
                m_addr.write(0x00000000);
1242
 
1243
                for (i = 0; i < 10; i++) wait(clk.posedge_event());
1244
                rst.write(true);
1245
 
1246
                for (i = 0; i < 50; i++) wait(clk.posedge_event());
1247
                usb_reset.write(true);
1248
 
1249
                for (i = 0; i < 300; i++) wait(clk.posedge_event());
1250
                usb_reset.write(false);
1251
 
1252
                for (i = 0; i < 10; i++) wait(clk.posedge_event());
1253
 
1254
                setup0();
1255
                in1();
1256
                out2();
1257
                in3();
1258
                out4();
1259
                in5();
1260
                out6();
1261
 
1262
                for (i = 0; i < 500; i++) wait(clk.posedge_event());
1263
                sc_stop();
1264
        }
1265
 
1266
        SC_CTOR(test) {
1267
                SC_METHOD(rx1_update);
1268
                sensitive << usb_reset << txdp2 << txdn2;
1269
                SC_METHOD(rx2_update);
1270
                sensitive << usb_reset << txdp << txdn;
1271
                SC_METHOD(watchdog);
1272
                sensitive << clk.pos();
1273
                SC_METHOD(wd_cnt_mon);
1274
                sensitive << wd_cnt;
1275
                SC_THREAD(init);
1276
                sensitive << clk.pos();
1277
        }
1278
};
1279
 
1280
int sc_main(int argc, char *argv[]) {
1281
 
1282
        sc_set_time_resolution(1.0, SC_NS);
1283
 
1284
        sc_clock clk("clock", 20.84, SC_NS);
1285
        sc_clock clk2("clock2", 20.84, SC_NS);
1286
 
1287
        sc_signal<bool> rst, vcc;
1288
 
1289
        sc_signal<bool> rx_dp1, rx_dn1, tx_dp1, tx_dn1;
1290
        sc_signal<bool> tb_rx_valid, tb_rx_active, tb_rx_error;
1291
        sc_signal<bool> tb_tx_valid, tb_tx_ready;
1292
        sc_signal<sc_uint<8> > tb_rx_data, tb_tx_data;
1293
 
1294
        sc_signal<bool> rx_dp2, rx_dn2, tx_dp2, tx_dn2;
1295
        sc_signal<sc_uint<8> >  SData, MData, SFlag;
1296
        sc_signal<sc_uint<32> > MAddr;
1297
        sc_signal<sc_uint<3> > MCmd;
1298
        sc_signal<sc_uint<2> > SResp;
1299
        sc_signal<bool> SInterrupt, SError, SCmdAccept;
1300
 
1301
        sc_signal<bool> usb_rst_nc, txoe_nc, tx_oe_nc;
1302
        sc_signal<sc_uint<2> > line_nc;
1303
 
1304
        usb_phy                 i_phy("HOST_PHY");
1305
        usb_ocp                 i_ocp("USB_OCP");
1306
        test                    i_test("TEST");
1307
 
1308
        i_phy.clk(clk);
1309
        i_phy.rst(rst);
1310
        i_phy.phy_tx_mode(vcc);
1311
        i_phy.usb_rst(usb_rst_nc);
1312
        i_phy.txdp(tx_dp1);
1313
        i_phy.txdn(tx_dn1);
1314
        i_phy.txoe(txoe_nc);
1315
        i_phy.rxd(rx_dp1);
1316
        i_phy.rxdp(rx_dp1);
1317
        i_phy.rxdn(rx_dn1);
1318
        i_phy.DataOut_i(tb_tx_data);
1319
        i_phy.TxValid_i(tb_tx_valid);
1320
        i_phy.TxReady_o(tb_tx_ready);
1321
        i_phy.DataIn_o(tb_rx_data);
1322
        i_phy.RxValid_o(tb_rx_valid);
1323
        i_phy.RxActive_o(tb_rx_active);
1324
        i_phy.RxError_o(tb_rx_error);
1325
        i_phy.LineState_o(line_nc);
1326
 
1327
        i_ocp.Clk(clk2);
1328
        i_ocp.Reset_n(rst);
1329
        i_ocp.tx_dp(tx_dp2);
1330
        i_ocp.tx_dn(tx_dn2);
1331
        i_ocp.tx_oe(tx_oe_nc);
1332
        i_ocp.rx_dp(rx_dp2);
1333
        i_ocp.rx_dn(rx_dn2);
1334
        i_ocp.rx_d(rx_dp2);
1335
        i_ocp.SInterrupt(SInterrupt);
1336
        i_ocp.SFlag(SFlag);
1337
        i_ocp.SError(SError);
1338
        i_ocp.MAddr(MAddr);
1339
        i_ocp.MCmd(MCmd);
1340
        i_ocp.MData(MData);
1341
        i_ocp.SCmdAccept(SCmdAccept);
1342
        i_ocp.SData(SData);
1343
        i_ocp.SResp(SResp);
1344
 
1345
        i_test.clk(clk);
1346
        i_test.rst(rst);
1347
        i_test.txdp(tx_dp1);
1348
        i_test.txdn(tx_dn1);
1349
        i_test.rxdp(rx_dp1);
1350
        i_test.rxdn(rx_dn1);
1351
        i_test.dout(tb_tx_data);
1352
        i_test.tx_valid(tb_tx_valid);
1353
        i_test.tx_ready(tb_tx_ready);
1354
        i_test.din(tb_rx_data);
1355
        i_test.rx_valid(tb_rx_valid);
1356
        i_test.rx_active(tb_rx_active);
1357
        i_test.rx_error(tb_rx_error);
1358
 
1359
        i_test.clk2(clk2);
1360
        i_test.txdp2(tx_dp2);
1361
        i_test.txdn2(tx_dn2);
1362
        i_test.rxdp2(rx_dp2);
1363
        i_test.rxdn2(rx_dn2);
1364
        i_test.s_int(SInterrupt);
1365
        i_test.s_flag(SFlag);
1366
        i_test.s_error(SError);
1367
        i_test.m_addr(MAddr);
1368
        i_test.m_cmd(MCmd);
1369
        i_test.m_data(MData);
1370
        i_test.s_cmd_accept(SCmdAccept);
1371
        i_test.s_data(SData);
1372
        i_test.s_resp(SResp);
1373
 
1374
        vcc.write(true);
1375
 
1376
#ifdef VCD_OUTPUT_ENABLE
1377
        sc_trace_file *vcd_log = sc_create_vcd_trace_file("USB_TEST");
1378
        sc_trace(vcd_log, clk2, "Clk");
1379
        sc_trace(vcd_log, rst, "Reset_n");
1380
        sc_trace(vcd_log, MAddr, "MAddr");
1381
        sc_trace(vcd_log, MCmd, "MCmd");
1382
        sc_trace(vcd_log, MData, "MData");
1383
        sc_trace(vcd_log, SData, "SData");
1384
        sc_trace(vcd_log, SCmdAccept, "SCmdAccept");
1385
        sc_trace(vcd_log, SResp, "SResp");
1386
#endif
1387
 
1388
#ifdef WIF_OUTPUT_ENABLE
1389
        sc_trace_file *wif_log = sc_create_wif_trace_file("USB_TEST");
1390
        sc_trace(wif_log, clk2, "Clk");
1391
        sc_trace(wif_log, rst, "Reset_n");
1392
        sc_trace(wif_log, MAddr, "MAddr");
1393
        sc_trace(wif_log, MCmd, "MCmd");
1394
        sc_trace(wif_log, MData, "MData");
1395
        sc_trace(wif_log, SData, "SData");
1396
        sc_trace(wif_log, SCmdAccept, "SCmdAccept");
1397
        sc_trace(wif_log, SResp, "SResp");
1398
#endif
1399
 
1400
        srand((unsigned int)(time(NULL) & 0xffffffff));
1401
        sc_start();
1402
 
1403
#ifdef VCD_OUTPUT_ENABLE
1404
        sc_close_vcd_trace_file(vcd_log);
1405
#endif
1406
 
1407
#ifdef WIF_OUTPUT_ENABLE
1408
        sc_close_wif_trace_file(wif_log);
1409
#endif
1410
 
1411
        return 0;
1412
}
1413
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.