📄 usb_ocp_test.cpp
字号:
for (i = 0; i < 5; i++) wait(clk.posedge_event());}void data_out(sc_uint<7> fa, int pl_size) { int len; send_token(fa, 0, USBF_T_PID_OUT); wait(clk.posedge_event()); if (!setup_pid.read()) send_data(USBF_T_PID_DATA0, pl_size, 1); else send_data(USBF_T_PID_DATA1, pl_size, 1); setup_pid.write(!setup_pid.read()); utmi_recv_pack(&len); if (txmem[0] != 0xd2) { cout << "ERROR: Ack mismatch. Expected: 0xd2, Got: " << txmem[0] << " (" << sc_simulation_time() << ")" << endl << endl; error_cnt++; } if (len != 1) { cout << "ERROR: SETUP: Length mismatch. Expected: 1, Got: " << len << " (" << sc_simulation_time() << ")" << endl << endl; error_cnt++; } for (i = 0; i < 5; i++) wait(clk.posedge_event());}// Enumeration Test -> Endpoint 0void setup0(void) { cout << endl; cout << "The Default Time Unit is: " << sc_get_default_time_unit().to_string() << endl << endl; cout << "**************************************************" << endl; cout << "*** CONTROL EP TEST 0 ***" << endl; cout << "**************************************************" << endl << endl; cout << "Setting Address ..." << endl << endl; send_setup(0x00, 0x00, SET_ADDRESS, 0x0012, 0x0000, 0x0000); data_in(0x00,0); cout << "Getting Descriptor ..." << endl << endl; send_setup(0x12, 0x80, GET_DESCRIPTOR, 0x0100, 0x0000, 0x0012); data_in(0x12, 18); data_out(0x12, 0); cout << "Getting Descriptor ..." << endl << endl; send_setup(0x12, 0x80, GET_DESCRIPTOR, 0x0200, 0x0000, 0x0009); data_in(0x12, 9); data_out(0x12, 0); cout << "Getting Descriptor ..." << endl << endl; send_setup(0x12, 0x80, GET_DESCRIPTOR, 0x0200, 0x0000, 0x003c); data_in(0x12, 60); data_out(0x12, 0); cout << "Getting Descriptor ..." << endl << endl; send_setup(0x12, 0x80, GET_DESCRIPTOR, 0x0300, 0x0000, 0x0008); data_in(0x12, 8); data_out(0x12, 0); cout << "Getting Descriptor ..." << endl << endl; send_setup(0x12, 0x80, GET_DESCRIPTOR, 0x0301, 0x0416, 0x001a); data_in(0x12, 26); data_out(0x12, 0); cout << "Getting Descriptor ..." << endl << endl; send_setup(0x12, 0x80, GET_DESCRIPTOR, 0x0302, 0x0416, 0x001c); data_in(0x12, 28); data_out(0x12, 0); cout << "Getting Descriptor ..." << endl << endl; send_setup(0x12, 0x80, GET_DESCRIPTOR, 0x0303, 0x0416, 0x0036); data_in(0x12, 54); data_out(0x12, 0); show_errors(); cout << "**************************************************" << endl; cout << "*** TEST DONE ... ***" << endl; cout << "**************************************************" << endl << endl;}// ISO IN Test -> Endpoint 1void in1(void) { sc_uint<7> my_fa; int rlen, fc; sc_uint<8> fd; int pack_cnt, pack_cnt_max, pack_sz, pack_sz_max; sc_uint<8> x; sc_uint<4> pid, expect_pid; sc_uint<32> data;// bool pid_cnt; cout << endl; cout << "**************************************************" << endl; cout << "*** ISOCHRONOUS IN EP TEST 1 ***" << endl; cout << "**************************************************" << endl << endl; send_sof(0x000); pack_sz_max = 256; pack_cnt_max = 4;// pid_cnt = false; my_fa = 0x12; m_addr.write(0x00000001); m_cmd.write(0x0); for (pack_sz = 0; pack_sz <= pack_sz_max; pack_sz += 32) { cout << "PL Size: " << pack_sz << endl; for (pack_cnt = 0; pack_cnt < pack_cnt_max; pack_cnt++) { buffer1_last = 0; for (fc = 0; fc < pack_sz; fc++) { while (s_flag.read()[1]) wait(clk.posedge_event()); x = (sc_uint<8>)(255.0 * rand() / (RAND_MAX + 1.0)); m_data.write(x); buffer0[fc] = x; m_cmd.write(0x1); wait(clk.posedge_event()); m_cmd.write(0x0); wait(clk.posedge_event()); } m_cmd.write(0x0); wait(clk.posedge_event()); // Send Data wait(clk.posedge_event()); send_sof(0x000); wait(clk.posedge_event()); send_token(my_fa, 1, USBF_T_PID_IN); wait(clk.posedge_event()); recv_packet(&pid, &rlen);// if (pid_cnt)// expect_pid = 0xb;// else// expect_pid = 0x3; expect_pid = 0x3; if (pid != expect_pid) { cout << "ERROR: PID mismatch. Expected: " << expect_pid << ", Got: " << pid << " (" << sc_simulation_time() << ")" << endl << endl; error_cnt++; }// pid_cnt = !pid_cnt; if (rlen != pack_sz) { cout << "ERROR: Size mismatch. Expected: " << pack_sz << ", Got: " << rlen << " (" << sc_simulation_time() << ")" << endl << endl; error_cnt++; } for (i = 0; i < 4; i++) wait(clk.posedge_event()); // Verify Data for (fc = 0; fc < pack_sz; fc++) { x = buffer0[fc]; if (buffer1[fc] != x) { cout << "ERROR: Data (" << fc << ") mismatch. Expected: " << x << ", Got: " << buffer1[fc] << " (" << sc_simulation_time() << ")" << endl << endl; error_cnt++; } } } for (i = 0; i < 50; i++) wait(clk.posedge_event()); } m_cmd.write(0x4); m_addr.write(0x00000000); cout << endl; show_errors(); cout << "**************************************************" << endl; cout << "*** TEST DONE ... ***" << endl; cout << "**************************************************" << endl << endl;}// ISO OUT Test -> Endpoint 2void out2(void) { sc_uint<7> my_fa; sc_uint<32> data; int n, no_pack, no_pack_max, pl_sz, pl_sz_max; bool pid; sc_uint<8> x; cout << endl; cout << "**************************************************" << endl; cout << "*** ISOCHRONOUS OUT EP TEST 2 ***" << endl; cout << "**************************************************" << endl << endl; no_pack_max = 4; pl_sz_max = 256; my_fa = 0x12; m_addr.write(0x00000002); m_cmd.write(0x0); for (pl_sz = 0; pl_sz <= pl_sz_max; pl_sz += 32) { pid = false; cout << "PL Size: " << pl_sz << endl; for (n = 0; n < 4096; n++) buffer1[n] = n; buffer1_last = 0; no_pack = 0; while (true) { wait(clk.posedge_event()); send_sof(0x000); wait(clk.posedge_event()); send_token(my_fa, 2, USBF_T_PID_OUT); wait(clk.posedge_event()); if (!pid) send_data(USBF_T_PID_DATA0, pl_sz, 1); else send_data(USBF_T_PID_DATA1, pl_sz, 1); for (i = 0; i < 10; i++) wait(clk2.posedge_event()); for (n = 0; n < pl_sz; n++) { m_cmd.write(0x0); wait(clk2.posedge_event()); wait(clk2.posedge_event()); while (s_flag.read()[0]) { m_cmd.write(0x0); wait(clk2.posedge_event()); wait(clk2.posedge_event()); } if (buffer1[n + (pl_sz * no_pack)] != s_data.read()) { cout << "ERROR: DATA mismatch. Expected: " << buffer1[n + (pl_sz * no_pack)] << ", Got: " << s_data.read() << " (" << sc_simulation_time() << ")" << endl << endl; error_cnt++; } m_cmd.write(0x2); wait(clk2.posedge_event()); wait(clk2.negedge_event()); } m_cmd.write(0x0); wait(clk2.posedge_event()); no_pack++; if (no_pack == no_pack_max) break; } wait(clk.posedge_event()); } m_cmd.write(0x4); m_addr.write(0x00000000); cout << endl; show_errors(); cout << "**************************************************" << endl; cout << "*** TEST DONE ... ***" << endl; cout << "**************************************************" << endl << endl;}// BULK IN Test -> Endpoint 3void in3(void) { sc_uint<7> my_fa; int rlen, fc; sc_uint<8> fd; int pack_cnt, pack_cnt_max, pack_sz, pack_sz_max; sc_uint<8> x; sc_uint<4> pid, expect_pid; sc_uint<32> data; bool pid_cnt; cout << endl; cout << "**************************************************" << endl; cout << "*** BULK IN EP TEST 3 ***" << endl; cout << "**************************************************" << endl << endl; send_sof(0x000); pack_sz_max = 64; pack_cnt_max = 4; pid_cnt = false; my_fa = 0x12; m_addr.write(0x00000003); m_cmd.write(0x0); for (pack_sz = 0; pack_sz <= pack_sz_max; pack_sz += 8) { cout << "PL Size: " << pack_sz << endl; for (pack_cnt = 0; pack_cnt < pack_cnt_max; pack_cnt++) { buffer1_last = 0; for (fc = 0; fc < pack_sz; fc++) { while (s_flag.read()[1]) wait(clk.posedge_event()); x = (sc_uint<8>)(255.0 * rand() / (RAND_MAX + 1.0)); m_data.write(x); buffer0[fc] = x; m_cmd.write(0x1); wait(clk.posedge_event()); m_cmd.write(0x0); wait(clk.posedge_event()); } m_cmd.write(0x0); wait(clk.posedge_event()); // Send Data wait(clk.posedge_event()); send_sof(0x000); wait(clk.posedge_event()); send_token(my_fa, 3, USBF_T_PID_IN); wait(clk.posedge_event()); recv_packet(&pid, &rlen); if (pid_cnt) expect_pid = 0xb; else expect_pid = 0x3;// expect_pid = 0x3; if (pid != expect_pid) { cout << "ERROR: PID mismatch. Expected: " << expect_pid << ", Got: " << pid << " (" << sc_simulation_time() << ")" << endl << endl; error_cnt++; } pid_cnt = !pid_cnt; if (rlen != pack_sz) { cout << "ERROR: Size mismatch. Expected: " << pack_sz << ", Got: " << rlen << " (" << sc_simulation_time() << ")" << endl << endl; error_cnt++; } for (i = 0; i < 4; i++) wait(clk.posedge_event()); send_token(my_fa, 3, USBF_T_PID_ACK); for (i = 0; i < 5; i++) wait(clk.posedge_event()); // Verify Data for (fc = 0; fc < pack_sz; fc++) { x = buffer0[fc]; if (buffer1[fc] != x) { cout << "ERROR: Data (" << fc << ") mismatch. Expected: " << x << ", Got: " << buffer1[fc] << " (" << sc_simulation_time() << ")" << endl << endl; error_cnt++; } } } for (i = 0; i < 50; i++) wait(clk.posedge_event()); } m_cmd.write(0x4); m_addr.write(0x00000000); cout << endl; show_errors(); cout << "**************************************************" << endl; cout << "*** TEST DONE ... ***" << endl; cout << "**************************************************" << endl << endl;}// BULK OUT Test -> Endpoint 4void out4(void) { sc_uint<7> my_fa; sc_uint<32> data; int n, len, no_pack, no_pack_max, pl_sz, pl_sz_max; bool pid; sc_uint<8> x; cout << endl; cout << "**************************************************" << endl; cout << "*** BULK OUT EP TEST 4 ***" << endl; cout << "**************************************************" << endl << endl; no_pack_max = 4; pl_sz_max = 64; my_fa = 0x12; m_addr.write(0x00000004); m_cmd.write(0x0); for (pl_sz = 0; pl_sz <= pl_sz_max; pl_sz += 8) { pid = false; cout << "PL Size: " << pl_sz << endl; for (n = 0; n < 4096; n++) buffer1[n] = n; buffer1_last = 0; no_pack = 0; while (true) { wait(clk.posedge_event()); send_sof(0x000); wait(clk.posedge_event()); send_token(my_fa, 4, USBF_T_PID_OUT); wait(clk.posedge_event()); if (!pid) send_data(USBF_T_PID_DATA0, pl_sz, 1); else send_data(USBF_T_PID_DATA1, pl_sz, 1); pid = !pid; utmi_recv_pack(&len); if (txmem[0] != 0xd2) { cout << "ERROR: ACK mismatch. Expected: 0xd2, Got: " << txmem[0] << " (" << sc_simulation_time() << ")" << endl << endl; error_cnt++; } if (len != 1) { cout << "ERROR: Size mismatch. Expected: 1, Got: " << len << " (" << sc_simulation_time() << ")" << endl << endl; error_cnt++; } wait(clk.posedge_event()); for (i = 0; i < 10; i++) wait(clk2.posedge_event()); for (n = 0; n < pl_sz; n++) { m_cmd.write(0x0); wait(clk2.posedge_event());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -