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

Subversion Repositories ethmac

[/] [ethmac/] [trunk/] [bench/] [verilog/] [tb_ethernet.v] - Blame information for rev 266

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

Line No. Rev Author Line
1 116 mohor
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  tb_ethernet.v                                               ////
4
////                                                              ////
5
////  This file is part of the Ethernet IP core project           ////
6
////  http://www.opencores.org/projects/ethmac/                   ////
7
////                                                              ////
8
////  Author(s):                                                  ////
9 169 mohor
////      - Tadej Markovic, tadej@opencores.org                   ////
10 243 tadejm
////      - Igor Mohor,     igorM@opencores.org                  ////
11 116 mohor
////                                                              ////
12 169 mohor
////  All additional information is available in the Readme.txt   ////
13 116 mohor
////  file.                                                       ////
14
////                                                              ////
15
//////////////////////////////////////////////////////////////////////
16
////                                                              ////
17
//// Copyright (C) 2001, 2002 Authors                             ////
18
////                                                              ////
19
//// This source file may be used and distributed without         ////
20
//// restriction provided that this copyright statement is not    ////
21
//// removed from the file and that any derivative work contains  ////
22
//// the original copyright notice and the associated disclaimer. ////
23
////                                                              ////
24
//// This source file is free software; you can redistribute it   ////
25
//// and/or modify it under the terms of the GNU Lesser General   ////
26
//// Public License as published by the Free Software Foundation; ////
27
//// either version 2.1 of the License, or (at your option) any   ////
28
//// later version.                                               ////
29
////                                                              ////
30
//// This source is distributed in the hope that it will be       ////
31
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
32
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
33
//// PURPOSE.  See the GNU Lesser General Public License for more ////
34
//// details.                                                     ////
35
////                                                              ////
36
//// You should have received a copy of the GNU Lesser General    ////
37
//// Public License along with this source; if not, download it   ////
38
//// from http://www.opencores.org/lgpl.shtml                     ////
39
////                                                              ////
40
//////////////////////////////////////////////////////////////////////
41
//
42
// CVS Revision History
43
//
44
// $Log: not supported by cvs2svn $
45 266 mohor
// Revision 1.23  2002/11/22 02:12:16  mohor
46
// test_mac_full_duplex_flow_control tests pretty much finished.
47
// TEST 0: INSERT CONTROL FRM. WHILE TRANSMITTING NORMAL
48
// FRM. AT 4 TX BD ( 10Mbps ) finished.
49
// TEST 2: RECEIVE CONTROL FRAMES WITH PASSALL OPTION
50
// TURNED OFF AT ONE RX BD ( 10Mbps ) finished.
51
//
52 263 mohor
// Revision 1.22  2002/11/21 13:56:50  mohor
53
// test_mac_full_duplex_flow test 0 finished. Sending the control (PAUSE) frame
54
// finished.
55
//
56 260 mohor
// Revision 1.21  2002/11/19 20:27:45  mohor
57
// Temp version.
58
//
59 254 mohor
// Revision 1.20  2002/11/19 17:41:19  tadejm
60
// Just some updates.
61
//
62 252 tadejm
// Revision 1.19  2002/11/14 13:12:47  tadejm
63
// Late collision is not reported any more.
64
//
65 243 tadejm
// Revision 1.18  2002/10/18 17:03:34  tadejm
66
// Changed BIST scan signals.
67
//
68 227 tadejm
// Revision 1.17  2002/10/18 13:58:22  tadejm
69
// Some code changed due to bug fixes.
70
//
71 223 tadejm
// Revision 1.16  2002/10/09 13:16:51  tadejm
72
// Just back-up; not completed testbench and some testcases are not
73
// wotking properly yet.
74
//
75 209 tadejm
// Revision 1.15  2002/09/20 14:29:12  tadej
76
// Full duplex tests modified and testbench bug repaired.
77
//
78 194 tadej
// Revision 1.14  2002/09/18 17:56:38  tadej
79
// Some additional reports added
80
//
81 192 tadej
// Revision 1.13  2002/09/16 17:53:49  tadej
82
// Full duplex test improved.
83
//
84 182 tadej
// Revision 1.12  2002/09/16 15:10:42  mohor
85
// MIIM test look better.
86
//
87 181 mohor
// Revision 1.11  2002/09/13 19:18:04  mohor
88
// Bench outputs data to display every 128 bytes.
89
//
90 180 mohor
// Revision 1.10  2002/09/13 18:44:29  mohor
91
// Beautiful tests merget together
92
//
93 179 mohor
// Revision 1.9  2002/09/13 18:41:45  mohor
94
// Rearanged testcases
95
//
96 178 mohor
// Revision 1.8  2002/09/13 14:50:15  mohor
97
// Bug in MIIM fixed.
98
//
99 177 mohor
// Revision 1.7  2002/09/13 12:29:14  mohor
100
// Headers changed.
101
//
102 170 mohor
// Revision 1.6  2002/09/13 11:57:20  mohor
103
// New testbench. Thanks to Tadej M - "The Spammer".
104
//
105 121 mohor
// Revision 1.2  2002/07/19 14:02:47  mohor
106
// Clock mrx_clk set to 2.5 MHz.
107
//
108 117 mohor
// Revision 1.1  2002/07/19 13:57:53  mohor
109
// Testing environment also includes traffic cop, memory interface and host
110
// interface.
111 116 mohor
//
112
//
113
//
114
//
115 117 mohor
//
116 116 mohor
 
117
 
118 169 mohor
`include "eth_phy_defines.v"
119
`include "wb_model_defines.v"
120 116 mohor
`include "tb_eth_defines.v"
121
`include "eth_defines.v"
122
`include "timescale.v"
123
 
124
module tb_ethernet();
125
 
126
 
127 169 mohor
reg           wb_clk;
128
reg           wb_rst;
129
wire          wb_int;
130 116 mohor
 
131 169 mohor
wire          mtx_clk;  // This goes to PHY
132
wire          mrx_clk;  // This goes to PHY
133 116 mohor
 
134
wire   [3:0]  MTxD;
135
wire          MTxEn;
136
wire          MTxErr;
137
 
138 169 mohor
wire   [3:0]  MRxD;     // This goes to PHY
139
wire          MRxDV;    // This goes to PHY
140
wire          MRxErr;   // This goes to PHY
141
wire          MColl;    // This goes to PHY
142
wire          MCrs;     // This goes to PHY
143 116 mohor
 
144
wire          Mdi_I;
145
wire          Mdo_O;
146
wire          Mdo_OE;
147 169 mohor
tri           Mdio_IO;
148 116 mohor
wire          Mdc_O;
149
 
150
 
151 169 mohor
parameter Tp = 1;
152 116 mohor
 
153 121 mohor
 
154 116 mohor
// Ethernet Slave Interface signals
155 169 mohor
wire [31:0] eth_sl_wb_adr;
156 116 mohor
wire [31:0] eth_sl_wb_adr_i, eth_sl_wb_dat_o, eth_sl_wb_dat_i;
157
wire  [3:0] eth_sl_wb_sel_i;
158
wire        eth_sl_wb_we_i, eth_sl_wb_cyc_i, eth_sl_wb_stb_i, eth_sl_wb_ack_o, eth_sl_wb_err_o;
159
 
160
// Ethernet Master Interface signals
161
wire [31:0] eth_ma_wb_adr_o, eth_ma_wb_dat_i, eth_ma_wb_dat_o;
162
wire  [3:0] eth_ma_wb_sel_o;
163
wire        eth_ma_wb_we_o, eth_ma_wb_cyc_o, eth_ma_wb_stb_o, eth_ma_wb_ack_i, eth_ma_wb_err_i;
164
 
165
 
166
 
167
 
168
// Connecting Ethernet top module
169 169 mohor
eth_top eth_top
170 116 mohor
(
171
  // WISHBONE common
172 169 mohor
  .wb_clk_i(wb_clk),              .wb_rst_i(wb_rst),
173 116 mohor
 
174
  // WISHBONE slave
175 169 mohor
  .wb_adr_i(eth_sl_wb_adr_i[11:2]), .wb_sel_i(eth_sl_wb_sel_i),   .wb_we_i(eth_sl_wb_we_i),
176
  .wb_cyc_i(eth_sl_wb_cyc_i),       .wb_stb_i(eth_sl_wb_stb_i),   .wb_ack_o(eth_sl_wb_ack_o),
177
  .wb_err_o(eth_sl_wb_err_o),       .wb_dat_i(eth_sl_wb_dat_i),   .wb_dat_o(eth_sl_wb_dat_o),
178 116 mohor
 
179
  // WISHBONE master
180
  .m_wb_adr_o(eth_ma_wb_adr_o),     .m_wb_sel_o(eth_ma_wb_sel_o), .m_wb_we_o(eth_ma_wb_we_o),
181
  .m_wb_dat_i(eth_ma_wb_dat_i),     .m_wb_dat_o(eth_ma_wb_dat_o), .m_wb_cyc_o(eth_ma_wb_cyc_o),
182
  .m_wb_stb_o(eth_ma_wb_stb_o),     .m_wb_ack_i(eth_ma_wb_ack_i), .m_wb_err_i(eth_ma_wb_err_i),
183
 
184
  //TX
185
  .mtx_clk_pad_i(mtx_clk), .mtxd_pad_o(MTxD), .mtxen_pad_o(MTxEn), .mtxerr_pad_o(MTxErr),
186
 
187
  //RX
188
  .mrx_clk_pad_i(mrx_clk), .mrxd_pad_i(MRxD), .mrxdv_pad_i(MRxDV), .mrxerr_pad_i(MRxErr),
189
  .mcoll_pad_i(MColl),    .mcrs_pad_i(MCrs),
190
 
191
  // MIIM
192
  .mdc_pad_o(Mdc_O), .md_pad_i(Mdi_I), .md_pad_o(Mdo_O), .md_padoe_o(Mdo_OE),
193
 
194 169 mohor
  .int_o(wb_int)
195 227 tadejm
 
196
  // Bist
197
`ifdef ETH_BIST
198
  ,
199
  .scanb_rst      (1'b0),
200
  .scanb_clk      (1'b0),
201
  .scanb_si       (1'b0),
202
  .scanb_so       (),
203
  .scanb_en       (1'b0)
204
`endif
205 116 mohor
);
206
 
207
 
208
 
209 169 mohor
// Connecting Ethernet PHY Module
210
assign Mdio_IO = Mdo_OE ? Mdo_O : 1'bz ;
211
assign Mdi_I   = Mdio_IO;
212
integer phy_log_file_desc;
213
 
214
eth_phy eth_phy
215 116 mohor
(
216 169 mohor
  // WISHBONE reset
217
  .m_rst_n_i(!wb_rst),
218 116 mohor
 
219 169 mohor
  // MAC TX
220
  .mtx_clk_o(mtx_clk),    .mtxd_i(MTxD),    .mtxen_i(MTxEn),    .mtxerr_i(MTxErr),
221
 
222
  // MAC RX
223
  .mrx_clk_o(mrx_clk),    .mrxd_o(MRxD),    .mrxdv_o(MRxDV),    .mrxerr_o(MRxErr),
224
  .mcoll_o(MColl),        .mcrs_o(MCrs),
225
 
226
  // MIIM
227
  .mdc_i(Mdc_O),          .md_io(Mdio_IO),
228
 
229
  // SYSTEM
230
  .phy_log(phy_log_file_desc)
231 116 mohor
);
232
 
233
 
234 169 mohor
 
235
// Connecting WB Master as Host Interface
236
integer host_log_file_desc;
237
 
238
WB_MASTER_BEHAVIORAL wb_master
239 116 mohor
(
240 169 mohor
    .CLK_I(wb_clk),
241
    .RST_I(wb_rst),
242
    .TAG_I({`WB_TAG_WIDTH{1'b0}}),
243
    .TAG_O(),
244
    .ACK_I(eth_sl_wb_ack_o),
245
    .ADR_O(eth_sl_wb_adr), // only eth_sl_wb_adr_i[11:2] used
246
    .CYC_O(eth_sl_wb_cyc_i),
247
    .DAT_I(eth_sl_wb_dat_o),
248
    .DAT_O(eth_sl_wb_dat_i),
249
    .ERR_I(eth_sl_wb_err_o),
250
    .RTY_I(1'b0),  // inactive (1'b0)
251
    .SEL_O(eth_sl_wb_sel_i),
252
    .STB_O(eth_sl_wb_stb_i),
253
    .WE_O (eth_sl_wb_we_i),
254
    .CAB_O()       // NOT USED for now!
255
);
256
 
257
assign eth_sl_wb_adr_i = {20'h0, eth_sl_wb_adr[11:2], 2'h0};
258
 
259
 
260
 
261
// Connecting WB Slave as Memory Interface Module
262
integer memory_log_file_desc;
263
 
264
WB_SLAVE_BEHAVIORAL wb_slave
265
(
266
    .CLK_I(wb_clk),
267
    .RST_I(wb_rst),
268
    .ACK_O(eth_ma_wb_ack_i),
269
    .ADR_I(eth_ma_wb_adr_o),
270
    .CYC_I(eth_ma_wb_cyc_o),
271
    .DAT_O(eth_ma_wb_dat_i),
272
    .DAT_I(eth_ma_wb_dat_o),
273
    .ERR_O(eth_ma_wb_err_i),
274
    .RTY_O(),      // NOT USED for now!
275
    .SEL_I(eth_ma_wb_sel_o),
276
    .STB_I(eth_ma_wb_stb_o),
277
    .WE_I (eth_ma_wb_we_o),
278
    .CAB_I(1'b0)   // inactive (1'b0)
279
);
280
 
281
 
282
 
283
// Connecting WISHBONE Bus Monitors to ethernet master and slave interfaces
284
integer wb_s_mon_log_file_desc ;
285
integer wb_m_mon_log_file_desc ;
286
 
287
WB_BUS_MON wb_eth_slave_bus_mon
288
(
289 116 mohor
  // WISHBONE common
290 169 mohor
  .CLK_I(wb_clk),
291
  .RST_I(wb_rst),
292 116 mohor
 
293 169 mohor
  // WISHBONE slave
294
  .ACK_I(eth_sl_wb_ack_o),
295
  .ADDR_O({20'h0, eth_sl_wb_adr_i[11:2], 2'b0}),
296
  .CYC_O(eth_sl_wb_cyc_i),
297
  .DAT_I(eth_sl_wb_dat_o),
298
  .DAT_O(eth_sl_wb_dat_i),
299
  .ERR_I(eth_sl_wb_err_o),
300
  .RTY_I(1'b0),
301
  .SEL_O(eth_sl_wb_sel_i),
302
  .STB_O(eth_sl_wb_stb_i),
303
  .WE_O (eth_sl_wb_we_i),
304
  .TAG_I({`WB_TAG_WIDTH{1'b0}}),
305
  .TAG_O(),
306
  .CAB_O(1'b0),
307
  .log_file_desc (wb_s_mon_log_file_desc)
308
);
309
 
310
WB_BUS_MON wb_eth_master_bus_mon
311
(
312
  // WISHBONE common
313
  .CLK_I(wb_clk),
314
  .RST_I(wb_rst),
315
 
316 116 mohor
  // WISHBONE master
317 169 mohor
  .ACK_I(eth_ma_wb_ack_i),
318
  .ADDR_O(eth_ma_wb_adr_o),
319
  .CYC_O(eth_ma_wb_cyc_o),
320
  .DAT_I(eth_ma_wb_dat_i),
321
  .DAT_O(eth_ma_wb_dat_o),
322
  .ERR_I(eth_ma_wb_err_i),
323
  .RTY_I(1'b0),
324
  .SEL_O(eth_ma_wb_sel_o),
325
  .STB_O(eth_ma_wb_stb_o),
326
  .WE_O (eth_ma_wb_we_o),
327
  .TAG_I({`WB_TAG_WIDTH{1'b0}}),
328
  .TAG_O(),
329
  .CAB_O(1'b0),
330
  .log_file_desc(wb_m_mon_log_file_desc)
331 116 mohor
);
332
 
333
 
334
 
335 169 mohor
reg         StartTB;
336
integer     tb_log_file;
337 116 mohor
 
338 169 mohor
initial
339
begin
340
  tb_log_file = $fopen("../log/eth_tb.log");
341
  if (tb_log_file < 2)
342
  begin
343
    $display("*E Could not open/create testbench log file in ../log/ directory!");
344
    $finish;
345
  end
346
  $fdisplay(tb_log_file, "========================== ETHERNET IP Core Testbench results ===========================");
347
  $fdisplay(tb_log_file, " ");
348 116 mohor
 
349 169 mohor
  phy_log_file_desc = $fopen("../log/eth_tb_phy.log");
350
  if (phy_log_file_desc < 2)
351
  begin
352
    $fdisplay(tb_log_file, "*E Could not open/create eth_tb_phy.log file in ../log/ directory!");
353
    $finish;
354
  end
355
  $fdisplay(phy_log_file_desc, "================ PHY Module  Testbench access log ================");
356
  $fdisplay(phy_log_file_desc, " ");
357
 
358
  memory_log_file_desc = $fopen("../log/eth_tb_memory.log");
359
  if (memory_log_file_desc < 2)
360
  begin
361
    $fdisplay(tb_log_file, "*E Could not open/create eth_tb_memory.log file in ../log/ directory!");
362
    $finish;
363
  end
364
  $fdisplay(memory_log_file_desc, "=============== MEMORY Module Testbench access log ===============");
365
  $fdisplay(memory_log_file_desc, " ");
366
 
367
  host_log_file_desc = $fopen("../log/eth_tb_host.log");
368
  if (host_log_file_desc < 2)
369
  begin
370
    $fdisplay(tb_log_file, "*E Could not open/create eth_tb_host.log file in ../log/ directory!");
371
    $finish;
372
  end
373
  $fdisplay(host_log_file_desc, "================ HOST Module Testbench access log ================");
374
  $fdisplay(host_log_file_desc, " ");
375
 
376
  wb_s_mon_log_file_desc = $fopen("../log/eth_tb_wb_s_mon.log");
377
  if (wb_s_mon_log_file_desc < 2)
378
  begin
379
    $fdisplay(tb_log_file, "*E Could not open/create eth_tb_wb_s_mon.log file in ../log/ directory!");
380
    $finish;
381
  end
382
  $fdisplay(wb_s_mon_log_file_desc, "============== WISHBONE Slave Bus Monitor error log ==============");
383
  $fdisplay(wb_s_mon_log_file_desc, " ");
384
  $fdisplay(wb_s_mon_log_file_desc, "   Only ERRONEOUS conditions are logged !");
385
  $fdisplay(wb_s_mon_log_file_desc, " ");
386
 
387
  wb_m_mon_log_file_desc = $fopen("../log/eth_tb_wb_m_mon.log");
388
  if (wb_m_mon_log_file_desc < 2)
389
  begin
390
    $fdisplay(tb_log_file, "*E Could not open/create eth_tb_wb_m_mon.log file in ../log/ directory!");
391
    $finish;
392
  end
393
  $fdisplay(wb_m_mon_log_file_desc, "============= WISHBONE Master Bus Monitor  error log =============");
394
  $fdisplay(wb_m_mon_log_file_desc, " ");
395
  $fdisplay(wb_m_mon_log_file_desc, "   Only ERRONEOUS conditions are logged !");
396
  $fdisplay(wb_m_mon_log_file_desc, " ");
397
 
398 243 tadejm
  // Reset pulse
399
  wb_rst =  1'b1;
400
  #423 wb_rst =  1'b0;
401
 
402 169 mohor
  // Clear memories
403
  clear_memories;
404 243 tadejm
  clear_buffer_descriptors;
405 169 mohor
 
406
  #423 StartTB  =  1'b1;
407
end
408
 
409
 
410
 
411
// Generating wb_clk clock
412 116 mohor
initial
413
begin
414 169 mohor
  wb_clk=0;
415
//  forever #2.5 wb_clk = ~wb_clk;  // 2*2.5 ns -> 200.0 MHz    
416 209 tadejm
  forever #5 wb_clk = ~wb_clk;  // 2*5 ns -> 100.0 MHz    
417 169 mohor
//  forever #10 wb_clk = ~wb_clk;  // 2*10 ns -> 50.0 MHz    
418
//  forever #12.5 wb_clk = ~wb_clk;  // 2*12.5 ns -> 40 MHz    
419
//  forever #15 wb_clk = ~wb_clk;  // 2*10 ns -> 33.3 MHz    
420 209 tadejm
//  forever #20 wb_clk = ~wb_clk;  // 2*20 ns -> 25 MHz    
421 169 mohor
//  forever #25 wb_clk = ~wb_clk;  // 2*25 ns -> 20.0 MHz
422
//  forever #31.25 wb_clk = ~wb_clk;  // 2*31.25 ns -> 16.0 MHz    
423
//  forever #50 wb_clk = ~wb_clk;  // 2*50 ns -> 10.0 MHz
424
//  forever #55 wb_clk = ~wb_clk;  // 2*55 ns ->  9.1 MHz    
425 116 mohor
end
426
 
427
 
428
 
429 169 mohor
integer      tests_successfull;
430
integer      tests_failed;
431
reg [799:0]  test_name; // used for tb_log_file
432 121 mohor
 
433 169 mohor
reg   [3:0]  wbm_init_waits; // initial wait cycles between CYC_O and STB_O of WB Master
434
reg   [3:0]  wbm_subseq_waits; // subsequent wait cycles between STB_Os of WB Master
435
reg   [2:0]  wbs_waits; // wait cycles befor WB Slave responds
436
reg   [7:0]  wbs_retries; // if RTY response, then this is the number of retries before ACK
437
 
438 116 mohor
initial
439
begin
440 169 mohor
  wait(StartTB);  // Start of testbench
441
 
442
  // Initial global values
443
  tests_successfull = 0;
444
  tests_failed = 0;
445
 
446
  wbm_init_waits = 4'h1;
447
  wbm_subseq_waits = 4'h3;
448
  wbs_waits = 4'h1;
449
  wbs_retries = 8'h2;
450
  wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
451
 
452
 
453
  //  Call tests
454
  //  ----------
455 194 tadej
//    test_access_to_mac_reg(0, 3);           // 0 - 3
456
//    test_mii(0, 17);                        // 0 - 17
457 169 mohor
  test_note("PHY generates ideal Carrier sense and Collision signals for following tests");
458
  eth_phy.carrier_sense_real_delay(0);
459 243 tadejm
//    test_mac_full_duplex_transmit(8, 9);    // 0 - (21)
460 254 mohor
//    test_mac_full_duplex_receive(8, 9);
461 266 mohor
    test_mac_full_duplex_flow_control(2, 2);
462 169 mohor
 
463 243 tadejm
  test_note("PHY generates 'real delayed' Carrier sense and Collision signals for following tests");
464 169 mohor
  eth_phy.carrier_sense_real_delay(1);
465
 
466
 
467
  // Finish test's logs
468
  test_summary;
469
  $display("\n\n END of SIMULATION");
470
  $fclose(tb_log_file | phy_log_file_desc | memory_log_file_desc | host_log_file_desc);
471
  $fclose(wb_s_mon_log_file_desc | wb_m_mon_log_file_desc);
472
 
473
  $stop;
474 116 mohor
end
475 169 mohor
 
476 116 mohor
 
477 169 mohor
 
478
//////////////////////////////////////////////////////////////
479
// Test tasks
480
//////////////////////////////////////////////////////////////
481
 
482
task test_access_to_mac_reg;
483
  input  [31:0]  start_task;
484
  input  [31:0]  end_task;
485
  integer        bit_start_1;
486
  integer        bit_end_1;
487
  integer        bit_start_2;
488
  integer        bit_end_2;
489
  integer        num_of_reg;
490
  integer        i_addr;
491
  integer        i_data;
492
  integer        i_length;
493
  integer        tmp_data;
494
  reg    [31:0]  tx_bd_num;
495
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_data;
496
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_tmp_data;
497
  integer        i;
498
  integer        i1;
499
  integer        i2;
500
  integer        i3;
501
  integer        fail;
502 178 mohor
  integer        test_num;
503 169 mohor
  reg    [31:0]  addr;
504
  reg    [31:0]  data;
505
  reg    [31:0]  data_max;
506 116 mohor
begin
507 169 mohor
// ACCESS TO MAC REGISTERS TEST
508
test_heading("ACCESS TO MAC REGISTERS TEST");
509
$display(" ");
510
$display("ACCESS TO MAC REGISTERS TEST");
511
fail = 0;
512
 
513 192 tadej
// reset MAC registers
514
hard_reset;
515
// reset MAC and MII LOGIC with soft reset
516
reset_mac;
517
reset_mii;
518 169 mohor
 
519 192 tadej
 
520 178 mohor
//////////////////////////////////////////////////////////////////////
521
////                                                              ////
522
////  test_access_to_mac_reg:                                     ////
523
////                                                              ////
524
////  0: Walking 1 with single cycles across MAC regs.            ////
525
////  1: Walking 1 with single cycles across MAC buffer descript. ////
526
////  2: Test max reg. values and reg. values after writing       ////
527
////     inverse reset values and hard reset of the MAC           ////
528
////  3: Test buffer desc. RAM preserving values after hard reset ////
529
////     of the MAC and resetting the logic                       ////
530
////                                                              ////
531
//////////////////////////////////////////////////////////////////////
532 194 tadej
for (test_num = start_task; test_num <= end_task; test_num = test_num + 1)
533 169 mohor
begin
534
 
535 178 mohor
  ////////////////////////////////////////////////////////////////////
536
  ////                                                            ////
537
  ////  Walking 1 with single cycles across MAC regs.             ////
538
  ////                                                            ////
539
  ////////////////////////////////////////////////////////////////////
540
  if (test_num == 0) // Walking 1 with single cycles across MAC regs.
541 194 tadej
  begin
542
    // TEST 0: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )
543
    test_name   = "TEST 0: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )";
544
    `TIME; $display("  TEST 0: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )");
545 178 mohor
 
546 194 tadej
    data = 0;
547
    for (i = 0; i <= 4; i = i + 1) // for initial wait cycles on WB bus
548
      begin
549
        wbm_init_waits = i;
550
        wbm_subseq_waits = {$random} % 5; // it is not important for single accesses
551
        for (i_addr = 0; i_addr <= 32'h4C; i_addr = i_addr + 4) // register address
552
          begin
553
            addr = `ETH_BASE + i_addr;
554
            // set ranges of R/W bits
555
            case (addr)
556
              `ETH_MODER:
557
                begin
558
                  bit_start_1 = 0;
559
                  bit_end_1   = 16;
560
                  bit_start_2 = 32; // not used
561
                  bit_end_2   = 32; // not used
562
                end
563
              `ETH_INT: // READONLY - tested within INT test
564
                begin
565
                  bit_start_1 = 32; // not used
566
                  bit_end_1   = 32; // not used
567
                  bit_start_2 = 32; // not used
568
                  bit_end_2   = 32; // not used
569
                end
570
              `ETH_INT_MASK:
571
                begin
572
                  bit_start_1 = 0;
573
                  bit_end_1   = 6;
574
                  bit_start_2 = 32; // not used
575
                  bit_end_2   = 32; // not used
576
                end
577
              `ETH_IPGT:
578
                begin
579
                  bit_start_1 = 0;
580
                  bit_end_1   = 6;
581
                  bit_start_2 = 32; // not used
582
                  bit_end_2   = 32; // not used
583
                end
584
              `ETH_IPGR1:
585
                begin
586
                  bit_start_1 = 0;
587
                  bit_end_1   = 6;
588
                  bit_start_2 = 32; // not used
589
                  bit_end_2   = 32; // not used
590
                end
591
              `ETH_IPGR2:
592
                begin
593
                  bit_start_1 = 0;
594
                  bit_end_1   = 6;
595
                  bit_start_2 = 32; // not used
596
                  bit_end_2   = 32; // not used
597
                end
598
              `ETH_PACKETLEN:
599
                begin
600
                  bit_start_1 = 0;
601
                  bit_end_1   = 31;
602
                  bit_start_2 = 32; // not used
603
                  bit_end_2   = 32; // not used
604
                end
605
              `ETH_COLLCONF:
606
                begin
607
                  bit_start_1 = 0;
608
                  bit_end_1   = 5;
609
                  bit_start_2 = 16;
610
                  bit_end_2   = 19;
611
                end
612
              `ETH_TX_BD_NUM:
613
                begin
614
                  bit_start_1 = 0;
615
                  bit_end_1   = 7;
616
                  bit_start_2 = 32; // not used
617
                  bit_end_2   = 32; // not used
618
                end
619
              `ETH_CTRLMODER:
620
                begin
621
                  bit_start_1 = 0;
622
                  bit_end_1   = 2;
623
                  bit_start_2 = 32; // not used
624
                  bit_end_2   = 32; // not used
625
                end
626
              `ETH_MIIMODER:
627
                begin
628
                  bit_start_1 = 0;
629
                  bit_end_1   = 9;
630
                  bit_start_2 = 32; // not used
631
                  bit_end_2   = 32; // not used
632
                end
633
              `ETH_MIICOMMAND: // "WRITEONLY" - tested within MIIM test - 3 LSBits are not written here!!!
634
                begin
635
                  bit_start_1 = 32; // not used
636
                  bit_end_1   = 32; // not used
637
                  bit_start_2 = 32; // not used
638
                  bit_end_2   = 32; // not used
639
                end
640
              `ETH_MIIADDRESS:
641
                begin
642
                  bit_start_1 = 0;
643
                  bit_end_1   = 4;
644
                  bit_start_2 = 8;
645
                  bit_end_2   = 12;
646
                end
647
              `ETH_MIITX_DATA:
648
                begin
649
                  bit_start_1 = 0;
650
                  bit_end_1   = 15;
651
                  bit_start_2 = 32; // not used
652
                  bit_end_2   = 32; // not used
653
                end
654
              `ETH_MIIRX_DATA: // READONLY - tested within MIIM test
655
                begin
656
                  bit_start_1 = 32; // not used
657
                  bit_end_1   = 32; // not used
658
                  bit_start_2 = 32; // not used
659
                  bit_end_2   = 32; // not used
660
                end
661
              `ETH_MIISTATUS: // READONLY - tested within MIIM test
662
                begin
663
                  bit_start_1 = 32; // not used
664
                  bit_end_1   = 32; // not used
665
                  bit_start_2 = 32; // not used
666
                  bit_end_2   = 32; // not used
667
                end
668
              `ETH_MAC_ADDR0:
669
                begin
670
                  bit_start_1 = 0;
671
                  bit_end_1   = 31;
672
                  bit_start_2 = 32; // not used
673
                  bit_end_2   = 32; // not used
674 178 mohor
                  end
675 194 tadej
              `ETH_MAC_ADDR1:
676
                begin
677
                  bit_start_1 = 0;
678
                  bit_end_1   = 15;
679
                  bit_start_2 = 32; // not used
680
                  bit_end_2   = 32; // not used
681
                end
682
              `ETH_HASH_ADDR0:
683
                begin
684
                  bit_start_1 = 0;
685
                  bit_end_1   = 31;
686
                  bit_start_2 = 32; // not used
687
                  bit_end_2   = 32; // not used
688
                end
689
              default: // `ETH_HASH_ADDR1:
690
                begin
691
                  bit_start_1 = 0;
692
                  bit_end_1   = 31;
693
                  bit_start_2 = 32; // not used
694
                  bit_end_2   = 32; // not used
695
                end
696
            endcase
697
 
698
            for (i_data = 0; i_data <= 31; i_data = i_data + 1) // the position of walking one
699
              begin
700
                data = 1'b1 << i_data;
701
                if ( (addr == `ETH_MIICOMMAND) && (i_data <= 2) ) // DO NOT WRITE to 3 LSBits of MIICOMMAND !!!
702
                  ;
703
                else
704 178 mohor
                  begin
705 194 tadej
                    wbm_write(addr, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
706
                    wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
707
                    if ( ((i_data >= bit_start_1) && (i_data <= bit_end_1)) ||
708
                         ((i_data >= bit_start_2) && (i_data <= bit_end_2)) ) // data should be equal to tmp_data
709
                      begin
710
                        if (tmp_data !== data)
711 178 mohor
                        begin
712 194 tadej
                          fail = fail + 1;
713
                          test_fail("RW bit of the MAC register was not written or not read");
714
                          `TIME;
715
                          $display("wbm_init_waits %d, addr %h, data %h, tmp_data %h",
716
                                    wbm_init_waits, addr, data, tmp_data);
717
                        end
718
                      end
719
                    else // data should not be equal to tmp_data
720
                      begin
721
                        if (tmp_data === data)
722 178 mohor
                          begin
723
                            fail = fail + 1;
724 194 tadej
                            test_fail("NON RW bit of the MAC register was written, but it shouldn't be");
725 178 mohor
                            `TIME;
726 194 tadej
                            $display("wbm_init_waits %d, addr %h, data %h, tmp_data %h",
727 178 mohor
                                      wbm_init_waits, addr, data, tmp_data);
728
                          end
729 194 tadej
                      end
730
                  end
731
              end
732
          end
733
      end
734 209 tadejm
    // INTERMEDIATE DISPLAYS (The only one)
735
    $display("    ->buffer descriptors tested with 0, 1, 2, 3 and 4 bus delay cycles");
736 194 tadej
    if(fail == 0)
737
      test_ok;
738
    else
739
      fail = 0;    // Errors were reported previously
740
  end
741 178 mohor
 
742
 
743
  ////////////////////////////////////////////////////////////////////
744
  ////                                                            ////
745
  ////  Walking 1 with single cycles across MAC buffer descript.  ////
746
  ////                                                            ////
747
  ////////////////////////////////////////////////////////////////////
748
  if (test_num == 1) // Start Walking 1 with single cycles across MAC buffer descript.
749 169 mohor
  begin
750 194 tadej
    // TEST 1: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC BUFFER DESC. ( VARIOUS BUS DELAYS )
751
    test_name   = "TEST 1: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC BUFFER DESC. ( VARIOUS BUS DELAYS )";
752
    `TIME; $display("  TEST 1: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC BUFFER DESC. ( VARIOUS BUS DELAYS )");
753 178 mohor
 
754
    data = 0;
755
    // set TX and RX buffer descriptors
756
    tx_bd_num = 32'h40;
757
    wbm_write(`ETH_TX_BD_NUM, tx_bd_num, 4'hF, 1, 0, 0);
758
    for (i = 0; i <= 4; i = i + 1) // for initial wait cycles on WB bus
759
    begin
760 169 mohor
      wbm_init_waits = i;
761
      wbm_subseq_waits = {$random} % 5; // it is not important for single accesses
762 178 mohor
      for (i_addr = 32'h400; i_addr <= 32'h7FC; i_addr = i_addr + 4) // buffer descriptor address
763 169 mohor
      begin
764
        addr = `ETH_BASE + i_addr;
765 178 mohor
        if (i_addr < (32'h400 + (tx_bd_num << 3))) // TX buffer descriptors
766
        begin
767
          // set ranges of R/W bits
768
          case (addr[3])
769
            1'b0: // buffer control bits
770
            begin
771
              bit_start_1 = 0;
772
              bit_end_1   = 31; // 8;
773
              bit_start_2 = 11;
774
              bit_end_2   = 31;
775
            end
776
            default: // 1'b1: // buffer pointer
777
            begin
778
              bit_start_1 = 0;
779
              bit_end_1   = 31;
780
              bit_start_2 = 32; // not used
781
              bit_end_2   = 32; // not used
782
            end
783
          endcase
784
        end
785
        else // RX buffer descriptors
786
        begin
787
          // set ranges of R/W bits
788
          case (addr[3])
789
            1'b0: // buffer control bits
790
            begin
791
              bit_start_1 = 0;
792
              bit_end_1   = 31; // 7;
793
              bit_start_2 = 13;
794
              bit_end_2   = 31;
795
            end
796
            default: // 1'b1: // buffer pointer
797
            begin
798
              bit_start_1 = 0;
799
              bit_end_1   = 31;
800
              bit_start_2 = 32; // not used
801
              bit_end_2   = 32; // not used
802
            end
803
          endcase
804
        end
805
 
806 169 mohor
        for (i_data = 0; i_data <= 31; i_data = i_data + 1) // the position of walking one
807
        begin
808
          data = 1'b1 << i_data;
809 178 mohor
          if ( (addr[3] == 0) && (i_data == 15) ) // DO NOT WRITE to this bit !!!
810
            ;
811 169 mohor
          else
812
          begin
813
            wbm_write(addr, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
814
            wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
815
            if ( ((i_data >= bit_start_1) && (i_data <= bit_end_1)) ||
816
                 ((i_data >= bit_start_2) && (i_data <= bit_end_2)) ) // data should be equal to tmp_data
817
            begin
818
              if (tmp_data !== data)
819
              begin
820
                fail = fail + 1;
821 178 mohor
                test_fail("RW bit of the MAC buffer descriptors was not written or not read");
822 169 mohor
                `TIME;
823
                $display("wbm_init_waits %d, addr %h, data %h, tmp_data %h",
824
                          wbm_init_waits, addr, data, tmp_data);
825
              end
826
            end
827
            else // data should not be equal to tmp_data
828
            begin
829
              if (tmp_data === data)
830
              begin
831
                fail = fail + 1;
832 178 mohor
                test_fail("NON RW bit of the MAC buffer descriptors was written, but it shouldn't be");
833 169 mohor
                `TIME;
834
                $display("wbm_init_waits %d, addr %h, data %h, tmp_data %h",
835
                          wbm_init_waits, addr, data, tmp_data);
836
              end
837
            end
838
          end
839
        end
840
      end
841 178 mohor
      // INTERMEDIATE DISPLAYS
842
      case (i)
843 209 tadejm
        0:       $display("    ->buffer descriptors tested with 0 bus delay");
844
        1:       $display("    ->buffer descriptors tested with 1 bus delay cycle");
845
        2:       $display("    ->buffer descriptors tested with 2 bus delay cycles");
846
        3:       $display("    ->buffer descriptors tested with 3 bus delay cycles");
847
        default: $display("    ->buffer descriptors tested with 4 bus delay cycles");
848 178 mohor
      endcase
849
    end
850
    if(fail == 0)
851
      test_ok;
852
    else
853
      fail = 0;
854 169 mohor
  end
855 178 mohor
 
856
 
857
  ////////////////////////////////////////////////////////////////////
858
  ////                                                            ////
859
  ////  Test max reg. values and reg. values after writing        ////
860
  ////  inverse reset values and hard reset of the MAC            ////
861
  ////                                                            ////
862
  ////////////////////////////////////////////////////////////////////
863
  if (test_num == 2) // Start this task
864 169 mohor
  begin
865 194 tadej
    // TEST 2: MAX REG. VALUES AND REG. VALUES AFTER WRITING INVERSE RESET VALUES AND HARD RESET OF THE MAC
866 178 mohor
    test_name   =
867 194 tadej
      "TEST 2: MAX REG. VALUES AND REG. VALUES AFTER WRITING INVERSE RESET VALUES AND HARD RESET OF THE MAC";
868 178 mohor
    `TIME; $display(
869 194 tadej
      "  TEST 2: MAX REG. VALUES AND REG. VALUES AFTER WRITING INVERSE RESET VALUES AND HARD RESET OF THE MAC");
870 178 mohor
 
871
    // reset MAC registers
872
    hard_reset;
873
    for (i = 0; i <= 4; i = i + 1) // 0, 2 - WRITE; 1, 3, 4 - READ
874 169 mohor
    begin
875 178 mohor
      for (i_addr = 0; i_addr <= 32'h4C; i_addr = i_addr + 4) // register address
876 169 mohor
      begin
877 178 mohor
        addr = `ETH_BASE + i_addr;
878
        // set ranges of R/W bits
879
        case (addr)
880
          `ETH_MODER:
881 169 mohor
          begin
882 178 mohor
            data = 32'h0000_A800;
883
            data_max = 32'h0001_FFFF;
884 169 mohor
          end
885 178 mohor
          `ETH_INT: // READONLY - tested within INT test
886 169 mohor
          begin
887 178 mohor
            data = 32'h0000_0000;
888
            data_max = 32'h0000_0000;
889 169 mohor
          end
890
          `ETH_INT_MASK:
891 178 mohor
          begin
892
            data = 32'h0000_0000;
893
            data_max = 32'h0000_007F;
894
          end
895 169 mohor
          `ETH_IPGT:
896 178 mohor
          begin
897
            data = 32'h0000_0012;
898
            data_max = 32'h0000_007F;
899
          end
900 169 mohor
          `ETH_IPGR1:
901 178 mohor
          begin
902
            data = 32'h0000_000C;
903
            data_max = 32'h0000_007F;
904
          end
905 169 mohor
          `ETH_IPGR2:
906 178 mohor
          begin
907
            data = 32'h0000_0012;
908
            data_max = 32'h0000_007F;
909
          end
910 169 mohor
          `ETH_PACKETLEN:
911 178 mohor
          begin
912
            data = 32'h0040_0600;
913
            data_max = 32'hFFFF_FFFF;
914
          end
915 169 mohor
          `ETH_COLLCONF:
916 178 mohor
          begin
917
            data = 32'h000F_003F;
918
            data_max = 32'h000F_003F;
919
          end
920 169 mohor
          `ETH_TX_BD_NUM:
921 178 mohor
          begin
922
            data = 32'h0000_0040;
923
            data_max = 32'h0000_0080;
924
          end
925 169 mohor
          `ETH_CTRLMODER:
926 178 mohor
          begin
927
            data = 32'h0000_0000;
928
            data_max = 32'h0000_0007;
929
          end
930 169 mohor
          `ETH_MIIMODER:
931 178 mohor
          begin
932
            data = 32'h0000_0064;
933
            data_max = 32'h0000_03FF;
934
          end
935 169 mohor
          `ETH_MIICOMMAND: // "WRITEONLY" - tested within MIIM test - 3 LSBits are not written here!!!
936 178 mohor
          begin
937
            data = 32'h0000_0000;
938
            data_max = 32'h0000_0007;
939
          end
940 169 mohor
          `ETH_MIIADDRESS:
941 178 mohor
          begin
942
            data = 32'h0000_0000;
943
            data_max = 32'h0000_1F1F;
944
          end
945 169 mohor
          `ETH_MIITX_DATA:
946 178 mohor
          begin
947
            data = 32'h0000_0000;
948
            data_max = 32'h0000_FFFF;
949
          end
950 169 mohor
          `ETH_MIIRX_DATA: // READONLY - tested within MIIM test
951
          begin
952 178 mohor
            data = 32'h0000_0000;
953
            data_max = 32'h0000_0000;
954 169 mohor
          end
955 178 mohor
          `ETH_MIISTATUS: // READONLY - tested within MIIM test
956 169 mohor
          begin
957 178 mohor
            data = 32'h0000_0000;
958
            data_max = 32'h0000_0000;
959 169 mohor
          end
960 178 mohor
          `ETH_MAC_ADDR0:
961 169 mohor
          begin
962 178 mohor
            data = 32'h0000_0000;
963
            data_max = 32'hFFFF_FFFF;
964 169 mohor
          end
965 178 mohor
          `ETH_MAC_ADDR1:
966 169 mohor
          begin
967 178 mohor
            data = 32'h0000_0000;
968
            data_max = 32'h0000_FFFF;
969 169 mohor
          end
970 178 mohor
          `ETH_HASH_ADDR0:
971 169 mohor
          begin
972 178 mohor
            data = 32'h0000_0000;
973
            data_max = 32'hFFFF_FFFF;
974 169 mohor
          end
975 178 mohor
          default: // `ETH_HASH_ADDR1:
976 169 mohor
          begin
977 178 mohor
            data = 32'h0000_0000;
978
            data_max = 32'hFFFF_FFFF;
979 169 mohor
          end
980
        endcase
981 178 mohor
 
982
        wbm_init_waits = {$random} % 3;
983
        wbm_subseq_waits = {$random} % 5; // it is not important for single accesses
984
        if (i == 0)
985
          wbm_write(addr, ~data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
986
        else if (i == 2)
987
          wbm_write(addr, 32'hFFFFFFFF, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
988
        else if ((i == 1) || (i == 4))
989 169 mohor
        begin
990 178 mohor
          wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
991
          if (tmp_data !== data)
992
          begin
993
            fail = fail + 1;
994
            test_fail("RESET value of the MAC register is not correct");
995
            `TIME;
996
            $display("  addr %h, data %h, tmp_data %h", addr, data, tmp_data);
997
          end
998 169 mohor
        end
999 178 mohor
        else // check maximum values
1000 169 mohor
        begin
1001
          wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1002 178 mohor
          if (addr == `ETH_TX_BD_NUM) // previous data should remain in this register
1003 169 mohor
          begin
1004
            if (tmp_data !== data)
1005
            begin
1006
              fail = fail + 1;
1007 178 mohor
              test_fail("Previous value of the TX_BD_NUM register did not remain");
1008 169 mohor
              `TIME;
1009 178 mohor
              $display("  addr %h, data_max %h, tmp_data %h", addr, data_max, tmp_data);
1010 169 mohor
            end
1011 178 mohor
            // try maximum (80)
1012
            wbm_write(addr, data_max, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1013
            wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1014
            if (tmp_data !== data_max)
1015
            begin
1016
              fail = fail + 1;
1017
              test_fail("MAX value of the TX_BD_NUM register is not correct");
1018
              `TIME;
1019
              $display("  addr %h, data_max %h, tmp_data %h", addr, data_max, tmp_data);
1020
            end
1021
            // try one less than maximum (80)
1022
            wbm_write(addr, (data_max - 1), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1023
            wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1024
            if (tmp_data !== (data_max - 1))
1025
            begin
1026
              fail = fail + 1;
1027
              test_fail("ONE less than MAX value of the TX_BD_NUM register is not correct");
1028
              `TIME;
1029
              $display("  addr %h, data_max %h, tmp_data %h", addr, data_max, tmp_data);
1030
            end
1031
            // try one more than maximum (80)
1032
            wbm_write(addr, (data_max + 1), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1033
            wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1034
            if (tmp_data !== (data_max - 1)) // previous data should remain in this register
1035
            begin
1036
              fail = fail + 1;
1037
              test_fail("Previous value of the TX_BD_NUM register did not remain");
1038
              `TIME;
1039
              $display("  addr %h, data_max %h, tmp_data %h", addr, data_max, tmp_data);
1040
            end
1041 169 mohor
          end
1042 178 mohor
          else
1043 169 mohor
          begin
1044 178 mohor
            if (tmp_data !== data_max)
1045 169 mohor
            begin
1046
              fail = fail + 1;
1047 178 mohor
              test_fail("MAX value of the MAC register is not correct");
1048 169 mohor
              `TIME;
1049 178 mohor
              $display("  addr %h, data_max %h, tmp_data %h", addr, data_max, tmp_data);
1050 169 mohor
            end
1051
          end
1052
        end
1053
      end
1054 178 mohor
      // reset MAC registers
1055
      if ((i == 0) || (i == 3))
1056
        hard_reset;
1057 169 mohor
    end
1058 178 mohor
    if(fail == 0)
1059
      test_ok;
1060
    else
1061
      fail = 0;
1062 169 mohor
  end
1063 116 mohor
 
1064 156 mohor
 
1065 181 mohor
  ////////////////////////////////////////////////////////////////////
1066
  ////                                                            ////
1067
  ////  Test buffer desc. ram preserving values after hard reset  ////
1068
  ////  of the mac and reseting the logic                         ////
1069
  ////                                                            ////
1070
  ////////////////////////////////////////////////////////////////////
1071 178 mohor
  if (test_num == 3) // Start this task
1072 169 mohor
  begin
1073 194 tadej
    // TEST 3: BUFFER DESC. RAM PRESERVING VALUES AFTER HARD RESET OF THE MAC AND RESETING THE LOGIC
1074
    test_name   = "TEST 3: BUFFER DESC. RAM PRESERVING VALUES AFTER HARD RESET OF THE MAC AND RESETING THE LOGIC";
1075 178 mohor
    `TIME;
1076 194 tadej
    $display("  TEST 3: BUFFER DESC. RAM PRESERVING VALUES AFTER HARD RESET OF THE MAC AND RESETING THE LOGIC");
1077 178 mohor
 
1078
    // reset MAC registers
1079
    hard_reset;
1080
    // reset LOGIC with soft reset
1081
    reset_mac;
1082
    reset_mii;
1083
    for (i = 0; i <= 3; i = i + 1) // 0, 2 - WRITE; 1, 3 - READ
1084 169 mohor
    begin
1085 178 mohor
      for (i_addr = 32'h400; i_addr <= 32'h7FC; i_addr = i_addr + 4) // buffer descriptor address
1086 169 mohor
      begin
1087 178 mohor
        addr = `ETH_BASE + i_addr;
1088
 
1089
        wbm_init_waits = {$random} % 3;
1090
        wbm_subseq_waits = {$random} % 5; // it is not important for single accesses
1091
        if (i == 0)
1092 169 mohor
        begin
1093 178 mohor
          data = 32'hFFFFFFFF;
1094
          wbm_write(addr, 32'hFFFFFFFF, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1095 169 mohor
        end
1096 178 mohor
        else if (i == 2)
1097 169 mohor
        begin
1098 178 mohor
          data = 32'h00000000;
1099
          wbm_write(addr, 32'h00000000, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1100 169 mohor
        end
1101
        else
1102
        begin
1103 178 mohor
          wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1104
          if (tmp_data !== data)
1105 169 mohor
          begin
1106
            fail = fail + 1;
1107 178 mohor
            test_fail("PRESERVED value of the MAC buffer descriptors is not correct");
1108 169 mohor
            `TIME;
1109 178 mohor
            $display("  addr %h, data %h, tmp_data %h", addr, data, tmp_data);
1110 169 mohor
          end
1111
        end
1112
      end
1113 178 mohor
      if ((i == 0) || (i == 2))
1114
      begin
1115
        // reset MAC registers
1116
        hard_reset;
1117
        // reset LOGIC with soft reset
1118
        reset_mac;
1119
        reset_mii;
1120
      end
1121 169 mohor
    end
1122 178 mohor
    if(fail == 0)
1123
      test_ok;
1124
    else
1125
    fail = 0;
1126 169 mohor
  end
1127 116 mohor
 
1128
 
1129 178 mohor
  if (test_num == 4) // Start this task
1130 169 mohor
  begin
1131 194 tadej
        /*  // TEST 4: 'WALKING ONE' WITH BURST CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )
1132
          test_name   = "TEST 4: 'WALKING ONE' WITH BURST CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )";
1133
          `TIME; $display("  TEST 4: 'WALKING ONE' WITH BURST CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )");
1134 178 mohor
 
1135
          data = 0;
1136
          burst_data = 0;
1137
          burst_tmp_data = 0;
1138
          i_length = 10; // two bursts for length 20
1139
          for (i = 0; i <= 4; i = i + 1) // for initial wait cycles on WB bus
1140
          begin
1141
            for (i1 = 0; i1 <= 4; i1 = i1 + 1) // for initial wait cycles on WB bus
1142
            begin
1143
              wbm_init_waits = i;
1144
              wbm_subseq_waits = i1;
1145
              #1;
1146
              for (i_data = 0; i_data <= 31; i_data = i_data + 1) // the position of walking one
1147
              begin
1148
                data = 1'b1 << i_data;
1149
                #1;
1150
                for (i2 = 32'h4C; i2 >= 0; i2 = i2 - 4)
1151
                begin
1152
                  burst_data = burst_data << 32;
1153
                  // DO NOT WRITE to 3 LSBits of MIICOMMAND !!!
1154
                  if ( ((`ETH_BASE + i2) == `ETH_MIICOMMAND) && (i_data <= 2) )
1155
                  begin
1156
                    #1 burst_data[31:0] = 0;
1157
                  end
1158
                  else
1159
                  begin
1160
                    #1 burst_data[31:0] = data;
1161
                  end
1162
                end
1163
                #1;
1164
                // 2 burst writes
1165
                addr = `ETH_BASE; // address of a first burst
1166
                wbm_write(addr, burst_data[(32 * 10 - 1):0], 4'hF, i_length, wbm_init_waits, wbm_subseq_waits);
1167
                burst_tmp_data = burst_data >> (32 * i_length);
1168
                addr = addr + 32'h28; // address of a second burst
1169
                wbm_write(addr, burst_tmp_data[(32 * 10 - 1):0], 4'hF, i_length, wbm_init_waits, wbm_subseq_waits);
1170
                #1;
1171
                // 2 burst reads
1172
                addr = `ETH_BASE; // address of a first burst
1173
                wbm_read(addr, burst_tmp_data[(32 * 10 - 1):0], 4'hF, i_length,
1174
                         wbm_init_waits, wbm_subseq_waits); // first burst
1175
                burst_tmp_data = burst_tmp_data << (32 * i_length);
1176
                addr = addr + 32'h28; // address of a second burst
1177
                wbm_read(addr, burst_tmp_data[(32 * 10 - 1):0], 4'hF, i_length,
1178
                         wbm_init_waits, wbm_subseq_waits); // second burst
1179
                #1;
1180
                for (i2 = 0; i2 <= 32'h4C; i2 = i2 + 4)
1181
                begin
1182
                  // set ranges of R/W bits
1183
                  case (`ETH_BASE + i2)
1184
                  `ETH_MODER:
1185
                    begin
1186
                      bit_start_1 = 0;
1187
                      bit_end_1   = 16;
1188
                      bit_start_2 = 32; // not used
1189
                      bit_end_2   = 32; // not used
1190
                    end
1191
                  `ETH_INT: // READONLY - tested within INT test
1192
                    begin
1193
                      bit_start_1 = 32; // not used
1194
                      bit_end_1   = 32; // not used
1195
                      bit_start_2 = 32; // not used
1196
                      bit_end_2   = 32; // not used
1197
                    end
1198
                  `ETH_INT_MASK:
1199
                    begin
1200
                      bit_start_1 = 0;
1201
                      bit_end_1   = 6;
1202
                      bit_start_2 = 32; // not used
1203
                      bit_end_2   = 32; // not used
1204
                    end
1205
                  `ETH_IPGT:
1206
                    begin
1207
                      bit_start_1 = 0;
1208
                      bit_end_1   = 6;
1209
                      bit_start_2 = 32; // not used
1210
                      bit_end_2   = 32; // not used
1211
                    end
1212
                  `ETH_IPGR1:
1213
                    begin
1214
                      bit_start_1 = 0;
1215
                      bit_end_1   = 6;
1216
                      bit_start_2 = 32; // not used
1217
                      bit_end_2   = 32; // not used
1218
                    end
1219
                  `ETH_IPGR2:
1220
                    begin
1221
                      bit_start_1 = 0;
1222
                      bit_end_1   = 6;
1223
                      bit_start_2 = 32; // not used
1224
                      bit_end_2   = 32; // not used
1225
                    end
1226
                  `ETH_PACKETLEN:
1227
                    begin
1228
                      bit_start_1 = 0;
1229
                      bit_end_1   = 31;
1230
                      bit_start_2 = 32; // not used
1231
                      bit_end_2   = 32; // not used
1232
                    end
1233
                  `ETH_COLLCONF:
1234
                    begin
1235
                      bit_start_1 = 0;
1236
                      bit_end_1   = 5;
1237
                      bit_start_2 = 16;
1238
                      bit_end_2   = 19;
1239
                    end
1240
                  `ETH_TX_BD_NUM:
1241
                    begin
1242
                      bit_start_1 = 0;
1243
                      bit_end_1   = 7;
1244
                      bit_start_2 = 32; // not used
1245
                      bit_end_2   = 32; // not used
1246
                    end
1247
                  `ETH_CTRLMODER:
1248
                    begin
1249
                      bit_start_1 = 0;
1250
                      bit_end_1   = 2;
1251
                      bit_start_2 = 32; // not used
1252
                      bit_end_2   = 32; // not used
1253
                    end
1254
                  `ETH_MIIMODER:
1255
                    begin
1256
                      bit_start_1 = 0;
1257
                      bit_end_1   = 9;
1258
                      bit_start_2 = 32; // not used
1259
                      bit_end_2   = 32; // not used
1260
                    end
1261
                  `ETH_MIICOMMAND: // "WRITEONLY" - tested within MIIM test - 3 LSBits are not written here!!!
1262
                    begin
1263
                      bit_start_1 = 32; // not used
1264
                      bit_end_1   = 32; // not used
1265
                      bit_start_2 = 32; // not used
1266
                      bit_end_2   = 32; // not used
1267
                    end
1268
                  `ETH_MIIADDRESS:
1269
                    begin
1270
                      bit_start_1 = 0;
1271
                      bit_end_1   = 4;
1272
                      bit_start_2 = 8;
1273
                      bit_end_2   = 12;
1274
                    end
1275
                  `ETH_MIITX_DATA:
1276
                    begin
1277
                      bit_start_1 = 0;
1278
                      bit_end_1   = 15;
1279
                      bit_start_2 = 32; // not used
1280
                      bit_end_2   = 32; // not used
1281
                    end
1282
                  `ETH_MIIRX_DATA: // READONLY - tested within MIIM test
1283
                    begin
1284
                      bit_start_1 = 32; // not used
1285
                      bit_end_1   = 32; // not used
1286
                      bit_start_2 = 32; // not used
1287
                      bit_end_2   = 32; // not used
1288
                    end
1289
                  `ETH_MIISTATUS: // READONLY - tested within MIIM test
1290
                    begin
1291
                      bit_start_1 = 32; // not used
1292
                      bit_end_1   = 32; // not used
1293
                      bit_start_2 = 32; // not used
1294
                      bit_end_2   = 32; // not used
1295
                    end
1296
                  `ETH_MAC_ADDR0:
1297
                    begin
1298
                      bit_start_1 = 0;
1299
                      bit_end_1   = 31;
1300
                      bit_start_2 = 32; // not used
1301
                      bit_end_2   = 32; // not used
1302
                    end
1303
                  `ETH_MAC_ADDR1:
1304
                    begin
1305
                      bit_start_1 = 0;
1306
                      bit_end_1   = 15;
1307
                      bit_start_2 = 32; // not used
1308
                      bit_end_2   = 32; // not used
1309
                    end
1310
                  `ETH_HASH_ADDR0:
1311
                    begin
1312
                      bit_start_1 = 0;
1313
                      bit_end_1   = 31;
1314
                      bit_start_2 = 32; // not used
1315
                      bit_end_2   = 32; // not used
1316
                    end
1317
                  default: // `ETH_HASH_ADDR1:
1318
                    begin
1319
                      bit_start_1 = 0;
1320
                      bit_end_1   = 31;
1321
                      bit_start_2 = 32; // not used
1322
                      bit_end_2   = 32; // not used
1323
                    end
1324
                  endcase
1325
                  #1;
1326
                  // 3 LSBits of MIICOMMAND are NOT written !!!
1327
                  if ( ((`ETH_BASE + i2) == `ETH_MIICOMMAND) && (i_data <= 2) )
1328
                  begin
1329
                    if (burst_tmp_data[31:0] !== burst_data[31:0])
1330
                    begin
1331
                      fail = fail + 1;
1332
                      test_fail("NON WR bit of the MAC MIICOMMAND register was wrong written or read");
1333
                      `TIME;
1334
                      $display("wbm_init_waits %d, wbm_subseq_waits %d, addr %h, data %h, tmp_data %h",
1335
                                wbm_init_waits, wbm_subseq_waits, i2, burst_data[31:0], burst_tmp_data[31:0]);
1336
                    end
1337
                  end
1338
                  else
1339
                  begin
1340
                    if ( ((i_data >= bit_start_1) && (i_data <= bit_end_1)) ||
1341
                         ((i_data >= bit_start_2) && (i_data <= bit_end_2)) ) // data should be equal to tmp_data
1342
                    begin
1343
                      if (burst_tmp_data[31:0] !== burst_data[31:0])
1344
                      begin
1345
                        fail = fail + 1;
1346
                        test_fail("RW bit of the MAC register was not written or not read");
1347
                        `TIME;
1348
                        $display("wbm_init_waits %d, wbm_subseq_waits %d, addr %h, data %h, tmp_data %h",
1349
                                  wbm_init_waits, wbm_subseq_waits, i2, burst_data[31:0], burst_tmp_data[31:0]);
1350
                      end
1351
                    end
1352
                    else // data should not be equal to tmp_data
1353
                    begin
1354
                      if (burst_tmp_data[31:0] === burst_data[31:0])
1355
                      begin
1356
                        fail = fail + 1;
1357
                        test_fail("NON RW bit of the MAC register was written, but it shouldn't be");
1358
                        `TIME;
1359
                        $display("wbm_init_waits %d, wbm_subseq_waits %d, addr %h, data %h, tmp_data %h",
1360
                                  wbm_init_waits, wbm_subseq_waits, i2, burst_data[31:0], burst_tmp_data[31:0]);
1361
                      end
1362
                    end
1363
                  end
1364
                  burst_tmp_data = burst_tmp_data >> 32;
1365
                  burst_data = burst_data >> 32;
1366
                end
1367
              end
1368
            end
1369
          end
1370
          if(fail == 0)
1371
            test_ok;
1372
          else
1373
            fail = 0;*/
1374
  end
1375 116 mohor
 
1376 169 mohor
end
1377 156 mohor
 
1378 169 mohor
end
1379
endtask // test_access_to_mac_reg
1380 156 mohor
 
1381
 
1382 169 mohor
task test_mii;
1383
  input  [31:0]  start_task;
1384
  input  [31:0]  end_task;
1385
  integer        i;
1386
  integer        i1;
1387
  integer        i2;
1388
  integer        i3;
1389
  integer        cnt;
1390
  integer        fail;
1391 181 mohor
  integer        test_num;
1392 169 mohor
  reg     [8:0]  clk_div; // only 8 bits are valid!
1393
  reg     [4:0]  phy_addr;
1394
  reg     [4:0]  reg_addr;
1395
  reg     [15:0] phy_data;
1396
  reg     [15:0] tmp_data;
1397
begin
1398
// MIIM MODULE TEST
1399
test_heading("MIIM MODULE TEST");
1400
$display(" ");
1401
$display("MIIM MODULE TEST");
1402
fail = 0;
1403 156 mohor
 
1404 192 tadej
// reset MAC registers
1405
hard_reset;
1406
// reset MAC and MII LOGIC with soft reset
1407
reset_mac;
1408 169 mohor
reset_mii;
1409 116 mohor
 
1410 194 tadej
 
1411 181 mohor
//////////////////////////////////////////////////////////////////////
1412
////                                                              ////
1413
////  test_mii:                                                   ////
1414
////                                                              ////
1415
////  0:  Test clock divider of mii management module with all    ////
1416
////      possible frequences.                                    ////
1417
////  1:  Test various readings from 'real' phy registers.        ////
1418
////  2:  Test various writings to 'real' phy registers (control  ////
1419
////      and non writable registers)                             ////
1420
////  3:  Test reset phy through mii management module            ////
1421
////  4:  Test 'walking one' across phy address (with and without ////
1422
////      preamble)                                               ////
1423
////  5:  Test 'walking one' across phy's register address (with  ////
1424
////      and without preamble)                                   ////
1425
////  6:  Test 'walking one' across phy's data (with and without  ////
1426
////      preamble)                                               ////
1427
////  7:  Test reading from phy with wrong phy address (host      ////
1428
////      reading high 'z' data)                                  ////
1429
////  8:  Test writing to phy with wrong phy address and reading  ////
1430
////      from correct one                                        ////
1431
////  9:  Test sliding stop scan command immediately after read   ////
1432
////      request (with and without preamble)                     ////
1433
//// 10:  Test sliding stop scan command immediately after write  ////
1434
////      request (with and without preamble)                     ////
1435
//// 11:  Test busy and nvalid status durations during write      ////
1436
////      (with and without preamble)                             ////
1437
//// 12:  Test busy and nvalid status durations during write      ////
1438
////      (with and without preamble)                             ////
1439
//// 13:  Test busy and nvalid status durations during scan (with ////
1440
////      and without preamble)                                   ////
1441
//// 14:  Test scan status from phy with detecting link-fail bit  ////
1442
////      (with and without preamble)                             ////
1443
//// 15:  Test scan status from phy with sliding link-fail bit    ////
1444
////      (with and without preamble)                             ////
1445
//// 16:  Test sliding stop scan command immediately after scan   ////
1446
////      request (with and without preamble)                     ////
1447
//// 17:  Test sliding stop scan command after 2. scan (with and  ////
1448
////      without preamble)                                       ////
1449
////                                                              ////
1450
//////////////////////////////////////////////////////////////////////
1451 194 tadej
for (test_num = start_task; test_num <= end_task; test_num = test_num + 1)
1452 169 mohor
begin
1453 194 tadej
 
1454 181 mohor
  ////////////////////////////////////////////////////////////////////
1455
  ////                                                            ////
1456
  ////  Test clock divider of mii management module with all      ////
1457
  ////  possible frequences.                                      ////
1458
  ////                                                            ////
1459
  ////////////////////////////////////////////////////////////////////
1460
  if (test_num == 0) // Test clock divider of mii management module with all possible frequences.
1461 169 mohor
  begin
1462 194 tadej
    // TEST 0: CLOCK DIVIDER OF MII MANAGEMENT MODULE WITH ALL POSSIBLE FREQUENCES
1463
    test_name   = "TEST 0: CLOCK DIVIDER OF MII MANAGEMENT MODULE WITH ALL POSSIBLE FREQUENCES";
1464
    `TIME; $display("  TEST 0: CLOCK DIVIDER OF MII MANAGEMENT MODULE WITH ALL POSSIBLE FREQUENCES");
1465 181 mohor
 
1466
    wait(Mdc_O); // wait for MII clock to be 1
1467
    for(clk_div = 0; clk_div <= 255; clk_div = clk_div + 1)
1468
    begin
1469
      i1 = 0;
1470
      i2 = 0;
1471
      #Tp mii_set_clk_div(clk_div[7:0]);
1472
      @(posedge Mdc_O);
1473
      #Tp;
1474
      fork
1475 169 mohor
        begin
1476 181 mohor
          @(posedge Mdc_O);
1477 169 mohor
          #Tp;
1478 181 mohor
          disable count_i1;
1479
          disable count_i2;
1480 169 mohor
        end
1481 181 mohor
        begin: count_i1
1482
          forever
1483
          begin
1484
            @(posedge wb_clk);
1485
            i1 = i1 + 1;
1486
            #Tp;
1487
          end
1488
        end
1489
        begin: count_i2
1490
          forever
1491
          begin
1492
            @(negedge wb_clk);
1493
            i2 = i2 + 1;
1494
            #Tp;
1495
          end
1496
        end
1497
      join
1498
      if((clk_div[7:0] == 0) || (clk_div[7:0] == 1) || (clk_div[7:0] == 2) || (clk_div[7:0] == 3))
1499
      begin
1500
        if((i1 == i2) && (i1 == 2))
1501 169 mohor
        begin
1502
        end
1503 181 mohor
        else
1504
        begin
1505
          fail = fail + 1;
1506 209 tadejm
          test_fail("Clock divider of MII module did'nt divide frequency corectly (it should divide by 2)");
1507 181 mohor
        end
1508 169 mohor
      end
1509
      else
1510
      begin
1511 181 mohor
        if((i1 == i2) && (i1 == {clk_div[7:1], 1'b0}))
1512
        begin
1513
        end
1514
        else
1515
        begin
1516
          fail = fail + 1;
1517
          test_fail("Clock divider of MII module did'nt divide frequency corectly");
1518
        end
1519 169 mohor
      end
1520
    end
1521 181 mohor
    if(fail == 0)
1522
      test_ok;
1523 169 mohor
    else
1524 181 mohor
      fail = 0;
1525
  end
1526
 
1527
 
1528
  ////////////////////////////////////////////////////////////////////
1529
  ////                                                            ////
1530
  ////  Test various readings from 'real' phy registers.          ////
1531
  ////                                                            ////
1532
  ////////////////////////////////////////////////////////////////////
1533
  if (test_num == 1) // Test various readings from 'real' phy registers.
1534
  begin
1535 194 tadej
    // TEST 1: VARIOUS READINGS FROM 'REAL' PHY REGISTERS
1536
    test_name   = "TEST 1: VARIOUS READINGS FROM 'REAL' PHY REGISTERS";
1537
    `TIME; $display("  TEST 1: VARIOUS READINGS FROM 'REAL' PHY REGISTERS");
1538 181 mohor
 
1539
    // set the fastest possible MII
1540
    clk_div = 0;
1541
    mii_set_clk_div(clk_div[7:0]);
1542
    // set address
1543
    reg_addr = 5'h1F;
1544
    phy_addr = 5'h1;
1545
    while(reg_addr >= 5'h4)
1546 169 mohor
    begin
1547 181 mohor
      // read request
1548
      #Tp mii_read_req(phy_addr, reg_addr);
1549
      check_mii_busy; // wait for read to finish
1550
      // read data
1551
      #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1552
      if (phy_data !== 16'hDEAD)
1553 169 mohor
      begin
1554 181 mohor
        test_fail("Wrong data was read from PHY from 'not used' address space");
1555
        fail = fail + 1;
1556 169 mohor
      end
1557 181 mohor
      if (reg_addr == 5'h4) // go out of for loop
1558
        reg_addr = 5'h3;
1559 169 mohor
      else
1560 181 mohor
        reg_addr = reg_addr - 5'h9;
1561 169 mohor
    end
1562 181 mohor
 
1563
    // set address
1564
    reg_addr = 5'h3;
1565
    // read request
1566
    #Tp mii_read_req(phy_addr, reg_addr);
1567
    check_mii_busy; // wait for read to finish
1568
    // read data
1569
    #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1570
    if (phy_data !== {`PHY_ID2, `MAN_MODEL_NUM, `MAN_REVISION_NUM})
1571
    begin
1572
      test_fail("Wrong data was read from PHY from ID register 2");
1573
      fail = fail + 1;
1574
    end
1575
    if(fail == 0)
1576
      test_ok;
1577
    else
1578
      fail = 0;
1579 169 mohor
  end
1580 116 mohor
 
1581
 
1582 181 mohor
  ////////////////////////////////////////////////////////////////////
1583
  ////                                                            ////
1584
  ////  Test various writings to 'real' phy registers (control    ////
1585
  ////  and non writable registers)                               ////
1586
  ////                                                            ////
1587
  ////////////////////////////////////////////////////////////////////
1588
  if (test_num == 2) // 
1589 169 mohor
  begin
1590 194 tadej
    // TEST 2: VARIOUS WRITINGS TO 'REAL' PHY REGISTERS ( CONTROL AND NON WRITABLE REGISTERS )
1591
    test_name   = "TEST 2: VARIOUS WRITINGS TO 'REAL' PHY REGISTERS ( CONTROL AND NON WRITABLE REGISTERS )";
1592
    `TIME; $display("  TEST 2: VARIOUS WRITINGS TO 'REAL' PHY REGISTERS ( CONTROL AND NON WRITABLE REGISTERS )");
1593 181 mohor
 
1594
    // negate data and try to write into unwritable register
1595
    tmp_data = ~phy_data;
1596
    // write request
1597
    #Tp mii_write_req(phy_addr, reg_addr, tmp_data);
1598
    check_mii_busy; // wait for write to finish
1599 169 mohor
    // read request
1600
    #Tp mii_read_req(phy_addr, reg_addr);
1601
    check_mii_busy; // wait for read to finish
1602
    // read data
1603 181 mohor
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1604
    if (tmp_data !== phy_data)
1605
    begin
1606
      test_fail("Data was written into unwritable PHY register - ID register 2");
1607
      fail = fail + 1;
1608
    end
1609
 
1610
    // set address
1611
    reg_addr = 5'h0; // control register
1612
    // read request
1613
    #Tp mii_read_req(phy_addr, reg_addr);
1614
    check_mii_busy; // wait for read to finish
1615
    // read data
1616
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1617
    // write request
1618
    phy_data = 16'h7DFF; // bit 15 (RESET bit) and bit 9 are self clearing bits
1619
    #Tp mii_write_req(phy_addr, reg_addr, phy_data);
1620
    check_mii_busy; // wait for write to finish
1621
    // read request
1622
    #Tp mii_read_req(phy_addr, reg_addr);
1623
    check_mii_busy; // wait for read to finish
1624
    // read data
1625 169 mohor
    #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1626 181 mohor
    if (phy_data !== 16'h7DFF)
1627 169 mohor
    begin
1628 181 mohor
      test_fail("Data was not correctly written into OR read from writable PHY register - control register");
1629 169 mohor
      fail = fail + 1;
1630
    end
1631 181 mohor
    // write request
1632
    #Tp mii_write_req(phy_addr, reg_addr, tmp_data);
1633
    check_mii_busy; // wait for write to finish
1634
    // read request
1635
    #Tp mii_read_req(phy_addr, reg_addr);
1636
    check_mii_busy; // wait for read to finish
1637
    // read data
1638
    #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1639
    if (phy_data !== tmp_data)
1640
    begin
1641
      test_fail("Data was not correctly written into OR read from writable PHY register - control register");
1642
      fail = fail + 1;
1643
    end
1644
    if(fail == 0)
1645
      test_ok;
1646 116 mohor
    else
1647 181 mohor
      fail = 0;
1648 169 mohor
  end
1649 116 mohor
 
1650
 
1651 181 mohor
  ////////////////////////////////////////////////////////////////////
1652
  ////                                                            ////
1653
  ////  Test reset phy through mii management module              ////
1654
  ////                                                            ////
1655
  ////////////////////////////////////////////////////////////////////
1656
  if (test_num == 3) // 
1657 169 mohor
  begin
1658 194 tadej
    // TEST 3: RESET PHY THROUGH MII MANAGEMENT MODULE
1659
    test_name   = "TEST 3: RESET PHY THROUGH MII MANAGEMENT MODULE";
1660
    `TIME; $display("  TEST 3: RESET PHY THROUGH MII MANAGEMENT MODULE");
1661 181 mohor
 
1662
    // set address
1663
    reg_addr = 5'h0; // control register
1664
    // write request
1665
    phy_data = 16'h7DFF; // bit 15 (RESET bit) and bit 9 are self clearing bits
1666
    #Tp mii_write_req(phy_addr, reg_addr, phy_data);
1667
    check_mii_busy; // wait for write to finish
1668
    // read request
1669
    #Tp mii_read_req(phy_addr, reg_addr);
1670
    check_mii_busy; // wait for read to finish
1671
    // read data
1672
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1673
    if (phy_data !== tmp_data)
1674
    begin
1675
      test_fail("Data was not correctly written into OR read from writable PHY register - control register");
1676
      fail = fail + 1;
1677
    end
1678
    // set reset bit - selfclearing bit in PHY
1679
    phy_data = phy_data | 16'h8000;
1680
    // write request
1681
    #Tp mii_write_req(phy_addr, reg_addr, phy_data);
1682
    check_mii_busy; // wait for write to finish
1683
    // read request
1684
    #Tp mii_read_req(phy_addr, reg_addr);
1685
    check_mii_busy; // wait for read to finish
1686
    // read data
1687
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1688
    // check self clearing of reset bit
1689
    if (tmp_data[15] !== 1'b0)
1690
    begin
1691
      test_fail("Reset bit should be self cleared - control register");
1692
      fail = fail + 1;
1693
    end
1694
    // check reset value of control register
1695
    if (tmp_data !== {2'h0, (`LED_CFG1 || `LED_CFG2), `LED_CFG1, 3'h0, `LED_CFG3, 8'h0})
1696
    begin
1697
      test_fail("PHY was not reset correctly AND/OR reset bit not self cleared");
1698
      fail = fail + 1;
1699
    end
1700
    if(fail == 0)
1701
      test_ok;
1702
    else
1703
      fail = 0;
1704 169 mohor
  end
1705
 
1706
 
1707 181 mohor
  ////////////////////////////////////////////////////////////////////
1708
  ////                                                            ////
1709
  ////  Test 'walking one' across phy address (with and without   ////
1710
  ////  preamble)                                                 ////
1711
  ////                                                            ////
1712
  ////////////////////////////////////////////////////////////////////
1713
  if (test_num == 4) // 
1714 169 mohor
  begin
1715 194 tadej
    // TEST 4: 'WALKING ONE' ACROSS PHY ADDRESS ( WITH AND WITHOUT PREAMBLE )
1716
    test_name   = "TEST 4: 'WALKING ONE' ACROSS PHY ADDRESS ( WITH AND WITHOUT PREAMBLE )";
1717
    `TIME; $display("  TEST 4: 'WALKING ONE' ACROSS PHY ADDRESS ( WITH AND WITHOUT PREAMBLE )");
1718 181 mohor
 
1719
    // set PHY to test mode
1720
    #Tp eth_phy.test_regs(1); // set test registers (wholy writable registers) and respond to all PHY addresses
1721
    for (i = 0; i <= 1; i = i + 1)
1722 169 mohor
    begin
1723 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
1724
      #Tp eth_phy.clear_test_regs;
1725
      // MII mode register
1726
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}), 4'hF, 1, wbm_init_waits,
1727
                wbm_subseq_waits);
1728
      // walk one across phy address
1729
      for (phy_addr = 5'h1; phy_addr > 5'h0; phy_addr = phy_addr << 1)
1730 169 mohor
      begin
1731 181 mohor
        reg_addr = $random;
1732
        tmp_data = $random;
1733
        // write request
1734
        #Tp mii_write_req(phy_addr, reg_addr, tmp_data);
1735
        check_mii_busy; // wait for write to finish
1736
        // read request
1737
        #Tp mii_read_req(phy_addr, reg_addr);
1738
        check_mii_busy; // wait for read to finish
1739
        // read data
1740
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1741
        #Tp;
1742
        if (phy_data !== tmp_data)
1743
        begin
1744
          if (i)
1745
            test_fail("Data was not correctly written into OR read from test registers (without preamble)");
1746
          else
1747
            test_fail("Data was not correctly written into OR read from test registers (with preamble)");
1748
          fail = fail + 1;
1749
        end
1750
        @(posedge wb_clk);
1751
        #Tp;
1752 169 mohor
      end
1753
    end
1754 181 mohor
    // set PHY to normal mode
1755
    #Tp eth_phy.test_regs(0);
1756
    #Tp eth_phy.preamble_suppresed(0);
1757
    // MII mode register
1758
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1759
    if(fail == 0)
1760
      test_ok;
1761
    else
1762
      fail = 0;
1763 169 mohor
  end
1764
 
1765
 
1766 181 mohor
  ////////////////////////////////////////////////////////////////////
1767
  ////                                                            ////
1768
  ////  Test 'walking one' across phy's register address (with    ////
1769
  ////  and without preamble)                                     ////
1770
  ////                                                            ////
1771
  ////////////////////////////////////////////////////////////////////
1772
  if (test_num == 5) // 
1773 169 mohor
  begin
1774 194 tadej
    // TEST 5: 'WALKING ONE' ACROSS PHY'S REGISTER ADDRESS ( WITH AND WITHOUT PREAMBLE )
1775
    test_name   = "TEST 5: 'WALKING ONE' ACROSS PHY'S REGISTER ADDRESS ( WITH AND WITHOUT PREAMBLE )";
1776
    `TIME; $display("  TEST 5: 'WALKING ONE' ACROSS PHY'S REGISTER ADDRESS ( WITH AND WITHOUT PREAMBLE )");
1777 181 mohor
 
1778
    // set PHY to test mode
1779
    #Tp eth_phy.test_regs(1); // set test registers (wholy writable registers) and respond to all PHY addresses
1780
    for (i = 0; i <= 1; i = i + 1)
1781 169 mohor
    begin
1782 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
1783
      #Tp eth_phy.clear_test_regs;
1784
      // MII mode register
1785
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}), 4'hF, 1, wbm_init_waits,
1786
                wbm_subseq_waits);
1787
      // walk one across reg address
1788
      for (reg_addr = 5'h1; reg_addr > 5'h0; reg_addr = reg_addr << 1)
1789 169 mohor
      begin
1790 181 mohor
        phy_addr = $random;
1791
        tmp_data = $random;
1792
        // write request
1793
        #Tp mii_write_req(phy_addr, reg_addr, tmp_data);
1794
        check_mii_busy; // wait for write to finish
1795
        // read request
1796
        #Tp mii_read_req(phy_addr, reg_addr);
1797
        check_mii_busy; // wait for read to finish
1798
        // read data
1799
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1800
        #Tp;
1801
        if (phy_data !== tmp_data)
1802
        begin
1803
          if (i)
1804
            test_fail("Data was not correctly written into OR read from test registers (without preamble)");
1805
          else
1806
            test_fail("Data was not correctly written into OR read from test registers (with preamble)");
1807
          fail = fail + 1;
1808
        end
1809
        @(posedge wb_clk);
1810
        #Tp;
1811 169 mohor
      end
1812
    end
1813 181 mohor
    // set PHY to normal mode
1814
    #Tp eth_phy.test_regs(0);
1815
    #Tp eth_phy.preamble_suppresed(0);
1816
    // MII mode register
1817
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1818
    if(fail == 0)
1819
      test_ok;
1820
    else
1821
      fail = 0;
1822 169 mohor
  end
1823
 
1824
 
1825 181 mohor
  ////////////////////////////////////////////////////////////////////
1826
  ////                                                            ////
1827
  ////  Test 'walking one' across phy's data (with and without    ////
1828
  ////  preamble)                                                 ////
1829
  ////                                                            ////
1830
  ////////////////////////////////////////////////////////////////////
1831
  if (test_num == 6) // 
1832 169 mohor
  begin
1833 194 tadej
    // TEST 6: 'WALKING ONE' ACROSS PHY'S DATA ( WITH AND WITHOUT PREAMBLE )
1834
    test_name   = "TEST 6: 'WALKING ONE' ACROSS PHY'S DATA ( WITH AND WITHOUT PREAMBLE )";
1835
    `TIME; $display("  TEST 6: 'WALKING ONE' ACROSS PHY'S DATA ( WITH AND WITHOUT PREAMBLE )");
1836 181 mohor
 
1837
    // set PHY to test mode
1838
    #Tp eth_phy.test_regs(1); // set test registers (wholy writable registers) and respond to all PHY addresses
1839
    for (i = 0; i <= 1; i = i + 1)
1840 169 mohor
    begin
1841 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
1842
      #Tp eth_phy.clear_test_regs;
1843
      // MII mode register
1844
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}), 4'hF, 1, wbm_init_waits,
1845
                wbm_subseq_waits);
1846
      // walk one across data
1847
      for (tmp_data = 16'h1; tmp_data > 16'h0; tmp_data = tmp_data << 1)
1848 169 mohor
      begin
1849 181 mohor
        phy_addr = $random;
1850
        reg_addr = $random;
1851
        // write request
1852
        #Tp mii_write_req(phy_addr, reg_addr, tmp_data);
1853
        check_mii_busy; // wait for write to finish
1854
        // read request
1855
        #Tp mii_read_req(phy_addr, reg_addr);
1856
        check_mii_busy; // wait for read to finish
1857
        // read data
1858
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1859
        #Tp;
1860
        if (phy_data !== tmp_data)
1861
        begin
1862
          if (i)
1863
            test_fail("Data was not correctly written into OR read from test registers (without preamble)");
1864
          else
1865
            test_fail("Data was not correctly written into OR read from test registers (with preamble)");
1866
          fail = fail + 1;
1867
        end
1868
        @(posedge wb_clk);
1869
        #Tp;
1870 169 mohor
      end
1871
    end
1872 181 mohor
    // set PHY to normal mode
1873
    #Tp eth_phy.test_regs(0);
1874
    #Tp eth_phy.preamble_suppresed(0);
1875
    // MII mode register
1876
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1877
    if(fail == 0)
1878
      test_ok;
1879
    else
1880
      fail = 0;
1881 169 mohor
  end
1882
 
1883
 
1884 181 mohor
  ////////////////////////////////////////////////////////////////////
1885
  ////                                                            ////
1886
  ////  Test reading from phy with wrong phy address (host        ////
1887
  ////  reading high 'z' data)                                    ////
1888
  ////                                                            ////
1889
  ////////////////////////////////////////////////////////////////////
1890
  if (test_num == 7) // 
1891 169 mohor
  begin
1892 194 tadej
    // TEST 7: READING FROM PHY WITH WRONG PHY ADDRESS ( HOST READING HIGH 'Z' DATA )
1893
    test_name   = "TEST 7: READING FROM PHY WITH WRONG PHY ADDRESS ( HOST READING HIGH 'Z' DATA )";
1894
    `TIME; $display("  TEST 7: READING FROM PHY WITH WRONG PHY ADDRESS ( HOST READING HIGH 'Z' DATA )");
1895 181 mohor
 
1896
    phy_addr = 5'h2; // wrong PHY address
1897
    // read request
1898
    #Tp mii_read_req(phy_addr, reg_addr);
1899
    check_mii_busy; // wait for read to finish
1900
    // read data
1901
    $display("  => Two errors will be displayed from WB Bus Monitor, because correct HIGH Z data was read");
1902
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1903
    if (tmp_data !== 16'hzzzz)
1904
    begin
1905
      test_fail("Data was read from PHY register with wrong PHY address - control register");
1906
      fail = fail + 1;
1907
    end
1908
    if(fail == 0)
1909
      test_ok;
1910
    else
1911
      fail = 0;
1912 169 mohor
  end
1913
 
1914
 
1915 181 mohor
  ////////////////////////////////////////////////////////////////////
1916
  ////                                                            ////
1917
  ////  Test writing to phy with wrong phy address and reading    ////
1918
  ////  from correct one                                          ////
1919
  ////                                                            ////
1920
  ////////////////////////////////////////////////////////////////////
1921
  if (test_num == 8) // 
1922 169 mohor
  begin
1923 194 tadej
    // TEST 8: WRITING TO PHY WITH WRONG PHY ADDRESS AND READING FROM CORRECT ONE
1924
    test_name   = "TEST 8: WRITING TO PHY WITH WRONG PHY ADDRESS AND READING FROM CORRECT ONE";
1925
    `TIME; $display("  TEST 8: WRITING TO PHY WITH WRONG PHY ADDRESS AND READING FROM CORRECT ONE");
1926 181 mohor
 
1927
    // set address
1928
    reg_addr = 5'h0; // control register
1929
    phy_addr = 5'h2; // wrong PHY address
1930
    // write request
1931
    phy_data = 16'h7DFF; // bit 15 (RESET bit) and bit 9 are self clearing bits
1932
    #Tp mii_write_req(phy_addr, reg_addr, phy_data);
1933
    check_mii_busy; // wait for write to finish
1934
 
1935
    phy_addr = 5'h1; // correct PHY address
1936
    // read request
1937
    #Tp mii_read_req(phy_addr, reg_addr);
1938
    check_mii_busy; // wait for read to finish
1939
    // read data
1940
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1941
    if (phy_data === tmp_data)
1942
    begin
1943
      test_fail("Data was written into PHY register with wrong PHY address - control register");
1944
      fail = fail + 1;
1945
    end
1946
    if(fail == 0)
1947
      test_ok;
1948
    else
1949
      fail = 0;
1950 169 mohor
  end
1951
 
1952
 
1953 181 mohor
  ////////////////////////////////////////////////////////////////////
1954
  ////                                                            ////
1955
  ////  Test sliding stop scan command immediately after read     ////
1956
  ////  request (with and without preamble)                       ////
1957
  ////                                                            ////
1958
  ////////////////////////////////////////////////////////////////////
1959
  if (test_num == 9) // 
1960 169 mohor
  begin
1961 194 tadej
    // TEST 9: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER READ REQUEST ( WITH AND WITHOUT PREAMBLE )
1962
    test_name = "TEST 9: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER READ REQUEST ( WITH AND WITHOUT PREAMBLE )";
1963 181 mohor
    `TIME;
1964 194 tadej
    $display("  TEST 9: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER READ REQUEST ( WITH AND WITHOUT PREAMBLE )");
1965 181 mohor
 
1966
    for (i2 = 0; i2 <= 1; i2 = i2 + 1) // choose preamble or not
1967 169 mohor
    begin
1968 181 mohor
      #Tp eth_phy.preamble_suppresed(i2);
1969
      // MII mode register
1970
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i2, 8'h0}), 4'hF, 1, wbm_init_waits,
1971
               wbm_subseq_waits);
1972
      i = 0;
1973
      cnt = 0;
1974
      while (i < 80) // delay for sliding of writing a STOP SCAN command
1975 169 mohor
      begin
1976 181 mohor
        for (i3 = 0; i3 <= 1; i3 = i3 + 1) // choose read or write after read will be finished
1977
        begin
1978
          // set address
1979
          reg_addr = 5'h0; // control register
1980
          phy_addr = 5'h1; // correct PHY address
1981
          cnt = 0;
1982
          // read request
1983
          #Tp mii_read_req(phy_addr, reg_addr);
1984
          fork
1985
            begin
1986
              repeat(i) @(posedge Mdc_O);
1987
              // write command 0x0 into MII command register
1988
              // MII command written while read in progress
1989
              wbm_write(`ETH_MIICOMMAND, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1990
              @(posedge wb_clk);
1991
              #Tp check_mii_busy; // wait for read to finish
1992
            end
1993
            begin
1994
              // wait for serial bus to become active
1995
              wait(Mdio_IO !== 1'bz);
1996
              // count transfer length
1997
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
1998
              begin
1999
                @(posedge Mdc_O);
2000
                #Tp cnt = cnt + 1;
2001
              end
2002
            end
2003
          join
2004
          // check transfer length
2005
          if (i2) // without preamble
2006 169 mohor
          begin
2007 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
2008
            begin
2009
              test_fail("Read request did not proceed correctly, while SCAN STOP command was written");
2010
              fail = fail + 1;
2011
            end
2012 169 mohor
          end
2013 181 mohor
          else // with preamble
2014 169 mohor
          begin
2015 181 mohor
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
2016
            begin
2017
              test_fail("Read request did not proceed correctly, while SCAN STOP command was written");
2018
              fail = fail + 1;
2019
            end
2020
          end
2021
          // check the BUSY signal to see if the bus is still IDLE
2022
          for (i1 = 0; i1 < 8; i1 = i1 + 1)
2023
            check_mii_busy; // wait for bus to become idle
2024
 
2025
          // try normal write or read after read was finished
2026
          #Tp phy_data = {8'h7D, (i[7:0] + 1)};
2027
          #Tp cnt = 0;
2028
          if (i3 == 0) // write after read
2029
          begin
2030
            // write request
2031
            #Tp mii_write_req(phy_addr, reg_addr, phy_data);
2032 169 mohor
            // wait for serial bus to become active
2033
            wait(Mdio_IO !== 1'bz);
2034
            // count transfer length
2035 181 mohor
            while(Mdio_IO !== 1'bz)
2036 169 mohor
            begin
2037
              @(posedge Mdc_O);
2038
              #Tp cnt = cnt + 1;
2039
            end
2040 181 mohor
            @(posedge Mdc_O);
2041
            // read request
2042
            #Tp mii_read_req(phy_addr, reg_addr);
2043
            check_mii_busy; // wait for read to finish
2044
            // read and check data
2045
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2046
            if (phy_data !== tmp_data)
2047
            begin
2048
              test_fail("Data was not correctly written into OR read from PHY register - control register");
2049
              fail = fail + 1;
2050
            end
2051 169 mohor
          end
2052 181 mohor
          else // read after read
2053 169 mohor
          begin
2054 181 mohor
            // read request
2055
            #Tp mii_read_req(phy_addr, reg_addr);
2056
            // wait for serial bus to become active
2057
            wait(Mdio_IO !== 1'bz);
2058
            // count transfer length
2059
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
2060
            begin
2061
              @(posedge Mdc_O);
2062
              #Tp cnt = cnt + 1;
2063
            end
2064 169 mohor
            @(posedge Mdc_O);
2065 181 mohor
            check_mii_busy; // wait for read to finish
2066
            // read and check data
2067
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2068
            if (phy_data !== tmp_data)
2069
            begin
2070
              test_fail("Data was not correctly written into OR read from PHY register - control register");
2071
              fail = fail + 1;
2072
            end
2073 169 mohor
          end
2074 181 mohor
          // check if transfer was a proper length
2075
          if (i2) // without preamble
2076 169 mohor
          begin
2077 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
2078
            begin
2079
              test_fail("New request did not proceed correctly, after read request");
2080
              fail = fail + 1;
2081
            end
2082 169 mohor
          end
2083 181 mohor
          else // with preamble
2084 169 mohor
          begin
2085 181 mohor
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
2086
            begin
2087
              test_fail("New request did not proceed correctly, after read request");
2088
              fail = fail + 1;
2089
            end
2090 169 mohor
          end
2091
        end
2092 181 mohor
        #Tp;
2093
        // set delay of writing the command
2094 169 mohor
        if (i2) // without preamble
2095
        begin
2096 181 mohor
          case(i)
2097
            0, 1:               i = i + 1;
2098
            18, 19, 20, 21, 22,
2099
            23, 24, 25, 26, 27,
2100
            28, 29, 30, 31, 32,
2101
            33, 34, 35:         i = i + 1;
2102
            36:                 i = 80;
2103
            default:            i = 18;
2104
          endcase
2105 169 mohor
        end
2106
        else // with preamble
2107
        begin
2108 181 mohor
          case(i)
2109
            0, 1:               i = i + 1;
2110
            50, 51, 52, 53, 54,
2111
            55, 56, 57, 58, 59,
2112
            60, 61, 62, 63, 64,
2113
            65, 66, 67:         i = i + 1;
2114
            68:                 i = 80;
2115
            default:            i = 50;
2116
          endcase
2117 169 mohor
        end
2118 181 mohor
        @(posedge wb_clk);
2119 169 mohor
      end
2120
    end
2121 181 mohor
    // set PHY to normal mode
2122
    #Tp eth_phy.preamble_suppresed(0);
2123
    // MII mode register
2124
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2125
    if(fail == 0)
2126
      test_ok;
2127
    else
2128
      fail = 0;
2129 169 mohor
  end
2130
 
2131
 
2132 181 mohor
  ////////////////////////////////////////////////////////////////////
2133
  ////                                                            ////
2134
  ////  Test sliding stop scan command immediately after write    ////
2135
  ////  request (with and without preamble)                       ////
2136
  ////                                                            ////
2137
  ////////////////////////////////////////////////////////////////////
2138
  if (test_num == 10) // 
2139 169 mohor
  begin
2140 194 tadej
    // TEST 10: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER WRITE REQUEST ( WITH AND WITHOUT PREAMBLE )
2141
    test_name = "TEST 10: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER WRITE REQUEST ( WITH AND WITHOUT PREAMBLE )";
2142 181 mohor
    `TIME;
2143 194 tadej
    $display("  TEST 10: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER WRITE REQUEST ( WITH AND WITHOUT PREAMBLE )");
2144 181 mohor
 
2145
    for (i2 = 0; i2 <= 1; i2 = i2 + 1) // choose preamble or not
2146 169 mohor
    begin
2147 181 mohor
      #Tp eth_phy.preamble_suppresed(i2);
2148
      // MII mode register
2149
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i2, 8'h0}), 4'hF, 1, wbm_init_waits,
2150
                wbm_subseq_waits);
2151
      i = 0;
2152
      cnt = 0;
2153
      while (i < 80) // delay for sliding of writing a STOP SCAN command
2154 169 mohor
      begin
2155 181 mohor
        for (i3 = 0; i3 <= 1; i3 = i3 + 1) // choose read or write after write will be finished
2156
        begin
2157
          // set address
2158
          reg_addr = 5'h0; // control register
2159
          phy_addr = 5'h1; // correct PHY address
2160
          cnt = 0;
2161
          // write request
2162
          phy_data = {8'h75, (i[7:0] + 1)};
2163
          #Tp mii_write_req(phy_addr, reg_addr, phy_data);
2164
          fork
2165
            begin
2166
              repeat(i) @(posedge Mdc_O);
2167
              // write command 0x0 into MII command register
2168
              // MII command written while read in progress
2169
              wbm_write(`ETH_MIICOMMAND, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2170
              @(posedge wb_clk);
2171
              #Tp check_mii_busy; // wait for write to finish
2172
            end
2173
            begin
2174
              // wait for serial bus to become active
2175
              wait(Mdio_IO !== 1'bz);
2176
              // count transfer length
2177
              while(Mdio_IO !== 1'bz)
2178
              begin
2179
                @(posedge Mdc_O);
2180
                #Tp cnt = cnt + 1;
2181
              end
2182
            end
2183
          join
2184
          // check transfer length
2185
          if (i2) // without preamble
2186 169 mohor
          begin
2187 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
2188
            begin
2189
              test_fail("Write request did not proceed correctly, while SCAN STOP command was written");
2190
              fail = fail + 1;
2191
            end
2192 169 mohor
          end
2193 181 mohor
          else // with preamble
2194 169 mohor
          begin
2195 181 mohor
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
2196
            begin
2197
              test_fail("Write request did not proceed correctly, while SCAN STOP command was written");
2198
              fail = fail + 1;
2199
            end
2200
          end
2201
          // check the BUSY signal to see if the bus is still IDLE
2202
          for (i1 = 0; i1 < 8; i1 = i1 + 1)
2203
            check_mii_busy; // wait for bus to become idle
2204
 
2205
          // try normal write or read after write was finished
2206
          #Tp cnt = 0;
2207
          if (i3 == 0) // write after write
2208
          begin
2209
            phy_data = {8'h7A, (i[7:0] + 1)};
2210
            // write request
2211
            #Tp mii_write_req(phy_addr, reg_addr, phy_data);
2212 169 mohor
            // wait for serial bus to become active
2213
            wait(Mdio_IO !== 1'bz);
2214
            // count transfer length
2215
            while(Mdio_IO !== 1'bz)
2216
            begin
2217
              @(posedge Mdc_O);
2218
              #Tp cnt = cnt + 1;
2219
            end
2220 181 mohor
            @(posedge Mdc_O);
2221
            // read request
2222
            #Tp mii_read_req(phy_addr, reg_addr);
2223
            check_mii_busy; // wait for read to finish
2224
            // read and check data
2225
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data , 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2226
            if (phy_data !== tmp_data)
2227
            begin
2228
              test_fail("Data was not correctly written into OR read from PHY register - control register");
2229
              fail = fail + 1;
2230
            end
2231 169 mohor
          end
2232 181 mohor
          else // read after write
2233 169 mohor
          begin
2234 181 mohor
            // read request
2235
            #Tp mii_read_req(phy_addr, reg_addr);
2236
            // wait for serial bus to become active
2237
            wait(Mdio_IO !== 1'bz);
2238
            // count transfer length
2239
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
2240
            begin
2241
              @(posedge Mdc_O);
2242
              #Tp cnt = cnt + 1;
2243
            end
2244 169 mohor
            @(posedge Mdc_O);
2245 181 mohor
            check_mii_busy; // wait for read to finish
2246
            // read and check data
2247
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data , 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2248
            if (phy_data !== tmp_data)
2249
            begin
2250
              test_fail("Data was not correctly written into OR read from PHY register - control register");
2251
              fail = fail + 1;
2252
            end
2253 169 mohor
          end
2254 181 mohor
          // check if transfer was a proper length
2255
          if (i2) // without preamble
2256 169 mohor
          begin
2257 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
2258
            begin
2259
              test_fail("New request did not proceed correctly, after write request");
2260
              fail = fail + 1;
2261
            end
2262 169 mohor
          end
2263 181 mohor
          else // with preamble
2264 169 mohor
          begin
2265 181 mohor
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
2266
            begin
2267
              test_fail("New request did not proceed correctly, after write request");
2268
              fail = fail + 1;
2269
            end
2270 169 mohor
          end
2271
        end
2272 181 mohor
        #Tp;
2273
        // set delay of writing the command
2274 169 mohor
        if (i2) // without preamble
2275
        begin
2276 181 mohor
          case(i)
2277
            0, 1:               i = i + 1;
2278
            18, 19, 20, 21, 22,
2279
            23, 24, 25, 26, 27,
2280
            28, 29, 30, 31, 32,
2281
            33, 34, 35:         i = i + 1;
2282
            36:                 i = 80;
2283
            default:            i = 18;
2284
          endcase
2285 169 mohor
        end
2286
        else // with preamble
2287
        begin
2288 181 mohor
          case(i)
2289
            0, 1:               i = i + 1;
2290
            50, 51, 52, 53, 54,
2291
            55, 56, 57, 58, 59,
2292
            60, 61, 62, 63, 64,
2293
            65, 66, 67:         i = i + 1;
2294
            68:                 i = 80;
2295
            default:            i = 50;
2296
          endcase
2297 169 mohor
        end
2298 181 mohor
        @(posedge wb_clk);
2299 169 mohor
      end
2300
    end
2301 181 mohor
    // set PHY to normal mode
2302
    #Tp eth_phy.preamble_suppresed(0);
2303
    // MII mode register
2304
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2305
    if(fail == 0)
2306
      test_ok;
2307
    else
2308
      fail = 0;
2309 169 mohor
  end
2310
 
2311
 
2312 181 mohor
  ////////////////////////////////////////////////////////////////////
2313
  ////                                                            ////
2314
  ////  Test busy and nvalid status durations during write (with  ////
2315
  ////  and without preamble)                                     ////
2316
  ////                                                            ////
2317
  ////////////////////////////////////////////////////////////////////
2318
  if (test_num == 11) // 
2319 169 mohor
  begin
2320 194 tadej
    // TEST 11: BUSY AND NVALID STATUS DURATIONS DURING WRITE ( WITH AND WITHOUT PREAMBLE )
2321
    test_name   = "TEST 11: BUSY AND NVALID STATUS DURATIONS DURING WRITE ( WITH AND WITHOUT PREAMBLE )";
2322
    `TIME; $display("  TEST 11: BUSY AND NVALID STATUS DURATIONS DURING WRITE ( WITH AND WITHOUT PREAMBLE )");
2323 181 mohor
 
2324
    reset_mii; // reset MII
2325
    // set link up, if it wasn't due to previous tests, since there weren't PHY registers
2326
    #Tp eth_phy.link_up_down(1);
2327
    // set the MII
2328
    clk_div = 64;
2329
    mii_set_clk_div(clk_div[7:0]);
2330
    // set address
2331
    reg_addr = 5'h1; // status register
2332
    phy_addr = 5'h1; // correct PHY address
2333
 
2334
    for (i = 0; i <= 1; i = i + 1)
2335 169 mohor
    begin
2336 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
2337
      // MII mode register
2338
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}) | (`ETH_MIIMODER_CLKDIV & clk_div),
2339
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2340
      @(posedge Mdc_O);
2341
      // write request
2342
      #Tp mii_write_req(phy_addr, reg_addr, 16'h5A5A);
2343
      // read data from MII status register - Busy and Nvalid bits
2344
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2345
 
2346
      // check MII IO signal and Busy and Nvalid bits
2347
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2348 169 mohor
      begin
2349 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is not HIGH Z - set higher clock divider");
2350
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2351
        begin
2352
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z anymore - 1. read");
2353
          fail = fail + 1;
2354
        end
2355
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2356
        begin
2357
          test_fail("Nvalid signal was set during write");
2358
          fail = fail + 1;
2359
        end
2360 169 mohor
      end
2361 181 mohor
      else // Busy bit should already be set to '1', due to reads from MII status register
2362 169 mohor
      begin
2363 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2364
        begin
2365
          test_fail("Busy signal should be set after write, due to reads from MII status register");
2366
          fail = fail + 1;
2367
        end
2368
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2369
        begin
2370
          test_fail("Nvalid signal was set during write");
2371
          fail = fail + 1;
2372
        end
2373 169 mohor
      end
2374 181 mohor
 
2375
      // wait for serial bus to become active
2376
      wait(Mdio_IO !== 1'bz);
2377
      // count transfer bits
2378
      if (i)
2379 169 mohor
      begin
2380 181 mohor
        repeat(32) @(posedge Mdc_O);
2381 169 mohor
      end
2382 181 mohor
      else
2383 169 mohor
      begin
2384 181 mohor
        repeat(64) @(posedge Mdc_O);
2385 169 mohor
      end
2386 181 mohor
      // read data from MII status register - Busy and Nvalid bits
2387
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2388
 
2389
      // check MII IO signal and Busy and Nvalid bits
2390
      if (Mdio_IO === 1'bz) // Mdio_IO should not be HIGH Z here - testbench selfcheck
2391 169 mohor
      begin
2392 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is HIGH Z - set higher clock divider");
2393
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2394
        begin
2395
          test_fail("Busy signal should be set while MII IO signal is not active anymore");
2396
          fail = fail + 1;
2397
        end
2398
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2399
        begin
2400
          test_fail("Nvalid signal was set during write");
2401
          fail = fail + 1;
2402
        end
2403 169 mohor
      end
2404 181 mohor
      else // Busy bit should still be set to '1'
2405 169 mohor
      begin
2406 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2407
        begin
2408
          test_fail("Busy signal should be set while MII IO signal not HIGH Z");
2409
          fail = fail + 1;
2410
        end
2411
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2412
        begin
2413
          test_fail("Nvalid signal was set during write");
2414
          fail = fail + 1;
2415
        end
2416 169 mohor
      end
2417 181 mohor
 
2418
      // wait for next negative clock edge
2419
      @(negedge Mdc_O);
2420 169 mohor
      // read data from MII status register - Busy and Nvalid bits
2421
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2422 181 mohor
 
2423 169 mohor
      // check MII IO signal and Busy and Nvalid bits
2424
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2425
      begin
2426
        test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2427
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2428
        begin
2429
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2430
          fail = fail + 1;
2431
        end
2432
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2433
        begin
2434
          test_fail("Nvalid signal was set during write");
2435
          fail = fail + 1;
2436
        end
2437
      end
2438 181 mohor
      else // Busy bit should still be set to '1'
2439 169 mohor
      begin
2440
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2441
        begin
2442 181 mohor
          test_fail("Busy signal should be set after MII IO signal become HIGH Z");
2443
          fail = fail + 1;
2444 169 mohor
        end
2445 181 mohor
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2446 169 mohor
        begin
2447 181 mohor
          test_fail("Nvalid signal was set during write");
2448
          fail = fail + 1;
2449
        end
2450
      end
2451
 
2452
      // wait for Busy to become inactive
2453
      i1 = 0;
2454
      while (i1 <= 2)
2455
      begin
2456
        // wait for next positive clock edge
2457
        @(posedge Mdc_O);
2458
        // read data from MII status register - Busy and Nvalid bits
2459
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2460
 
2461
        // check MII IO signal and Busy and Nvalid bits
2462
        if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2463
        begin
2464
          test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2465
          if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2466 169 mohor
          begin
2467 181 mohor
            test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2468 169 mohor
            fail = fail + 1;
2469
          end
2470 181 mohor
          if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2471
          begin
2472
            test_fail("Nvalid signal was set during write");
2473
            fail = fail + 1;
2474
          end
2475 169 mohor
        end
2476 181 mohor
        else // wait for Busy bit to be set to '0'
2477 169 mohor
        begin
2478 181 mohor
          if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2479
          begin
2480
            i1 = 3; // end of Busy checking
2481
          end
2482
          else
2483
          begin
2484
            if (i1 == 2)
2485
            begin
2486
              test_fail("Busy signal should be cleared after 2 periods after MII IO signal become HIGH Z");
2487
              fail = fail + 1;
2488
            end
2489
            #Tp i1 = i1 + 1;
2490
          end
2491
          if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2492
          begin
2493
            test_fail("Nvalid signal was set after write");
2494
            fail = fail + 1;
2495
          end
2496 169 mohor
        end
2497
      end
2498
    end
2499 181 mohor
    // set PHY to normal mode
2500
    #Tp eth_phy.preamble_suppresed(0);
2501
    // MII mode register
2502
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2503
    if(fail == 0)
2504
      test_ok;
2505
    else
2506
      fail = 0;
2507 116 mohor
  end
2508
 
2509
 
2510 181 mohor
  ////////////////////////////////////////////////////////////////////
2511
  ////                                                            ////
2512
  ////  Test busy and nvalid status durations during write (with  ////
2513
  ////  and without preamble)                                     ////
2514
  ////                                                            ////
2515
  ////////////////////////////////////////////////////////////////////
2516
  if (test_num == 12) // 
2517 169 mohor
  begin
2518 194 tadej
    // TEST 12: BUSY AND NVALID STATUS DURATIONS DURING READ ( WITH AND WITHOUT PREAMBLE )
2519
    test_name   = "TEST 12: BUSY AND NVALID STATUS DURATIONS DURING READ ( WITH AND WITHOUT PREAMBLE )";
2520
    `TIME; $display("  TEST 12: BUSY AND NVALID STATUS DURATIONS DURING READ ( WITH AND WITHOUT PREAMBLE )");
2521 181 mohor
 
2522
    reset_mii; // reset MII
2523
    // set link up, if it wasn't due to previous tests, since there weren't PHY registers
2524
    #Tp eth_phy.link_up_down(1);
2525
    // set the MII
2526
    clk_div = 64;
2527
    mii_set_clk_div(clk_div[7:0]);
2528
    // set address
2529
    reg_addr = 5'h1; // status register
2530
    phy_addr = 5'h1; // correct PHY address
2531
 
2532
    for (i = 0; i <= 1; i = i + 1)
2533 169 mohor
    begin
2534 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
2535
      // MII mode register
2536
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}) | (`ETH_MIIMODER_CLKDIV & clk_div),
2537
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2538 169 mohor
      @(posedge Mdc_O);
2539 181 mohor
      // read request
2540
      #Tp mii_read_req(phy_addr, reg_addr);
2541 169 mohor
      // read data from MII status register - Busy and Nvalid bits
2542
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2543 181 mohor
 
2544 169 mohor
      // check MII IO signal and Busy and Nvalid bits
2545
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2546
      begin
2547 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is not HIGH Z - set higher clock divider");
2548 169 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2549
        begin
2550 181 mohor
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z anymore - 1. read");
2551 169 mohor
          fail = fail + 1;
2552
        end
2553
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2554
        begin
2555
          test_fail("Nvalid signal was set during read");
2556
          fail = fail + 1;
2557
        end
2558
      end
2559 181 mohor
      else // Busy bit should already be set to '1', due to reads from MII status register
2560 169 mohor
      begin
2561
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2562
        begin
2563 181 mohor
          test_fail("Busy signal should be set after read, due to reads from MII status register");
2564
          fail = fail + 1;
2565 169 mohor
        end
2566
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2567
        begin
2568 181 mohor
          test_fail("Nvalid signal was set during read");
2569 169 mohor
          fail = fail + 1;
2570
        end
2571
      end
2572 181 mohor
 
2573
      // wait for serial bus to become active
2574
      wait(Mdio_IO !== 1'bz);
2575
      // count transfer bits
2576
      if (i)
2577 169 mohor
      begin
2578 181 mohor
        repeat(31) @(posedge Mdc_O);
2579 169 mohor
      end
2580 181 mohor
      else
2581 169 mohor
      begin
2582 181 mohor
        repeat(63) @(posedge Mdc_O);
2583 169 mohor
      end
2584 181 mohor
      // wait for next negative clock edge
2585
      @(negedge Mdc_O);
2586
      // read data from MII status register - Busy and Nvalid bits
2587
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2588
 
2589
      // check MII IO signal and Busy and Nvalid bits
2590
      if (Mdio_IO === 1'bz) // Mdio_IO should not be HIGH Z here - testbench selfcheck
2591 169 mohor
      begin
2592 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is HIGH Z - set higher clock divider");
2593
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2594
        begin
2595
          test_fail("Busy signal should be set while MII IO signal is not active anymore");
2596
          fail = fail + 1;
2597
        end
2598
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2599
        begin
2600
          test_fail("Nvalid signal was set during read");
2601
          fail = fail + 1;
2602
        end
2603 169 mohor
      end
2604 181 mohor
      else // Busy bit should still be set to '1'
2605 169 mohor
      begin
2606 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2607
        begin
2608
          test_fail("Busy signal should be set while MII IO signal not HIGH Z");
2609
          fail = fail + 1;
2610
        end
2611
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2612
        begin
2613
          test_fail("Nvalid signal was set during read");
2614
          fail = fail + 1;
2615
        end
2616 169 mohor
      end
2617 181 mohor
 
2618 169 mohor
      // wait for next positive clock edge
2619
      @(posedge Mdc_O);
2620
      // read data from MII status register - Busy and Nvalid bits
2621
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2622 181 mohor
 
2623 169 mohor
      // check MII IO signal and Busy and Nvalid bits
2624
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2625
      begin
2626
        test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2627 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2628 169 mohor
        begin
2629 181 mohor
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2630
          fail = fail + 1;
2631
        end
2632
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2633
        begin
2634
          test_fail("Nvalid signal was set during read");
2635
          fail = fail + 1;
2636
        end
2637
      end
2638
      else // Busy bit should still be set to '1'
2639
      begin
2640
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2641
        begin
2642
          test_fail("Busy signal should be set after MII IO signal become HIGH Z");
2643
          fail = fail + 1;
2644
        end
2645
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2646
        begin
2647
          test_fail("Nvalid signal was set during read");
2648
          fail = fail + 1;
2649
        end
2650
      end
2651
 
2652
      // wait for Busy to become inactive
2653
      i1 = 0;
2654
      while (i1 <= 2)
2655
      begin
2656
        // wait for next positive clock edge
2657
        @(posedge Mdc_O);
2658
        // read data from MII status register - Busy and Nvalid bits
2659
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2660
 
2661
        // check MII IO signal and Busy and Nvalid bits
2662
        if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2663
        begin
2664
          test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2665 169 mohor
          if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2666
          begin
2667
            test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2668
            fail = fail + 1;
2669
          end
2670 181 mohor
          if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2671 169 mohor
          begin
2672 181 mohor
            test_fail("Nvalid signal was set during read");
2673 169 mohor
            fail = fail + 1;
2674
          end
2675
        end
2676 181 mohor
        else // wait for Busy bit to be set to '0'
2677 169 mohor
        begin
2678
          if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2679
          begin
2680
            i1 = 3; // end of Busy checking
2681
          end
2682
          else
2683
          begin
2684
            if (i1 == 2)
2685
            begin
2686
              test_fail("Busy signal should be cleared after 2 periods after MII IO signal become HIGH Z");
2687
              fail = fail + 1;
2688
            end
2689
            #Tp i1 = i1 + 1;
2690
          end
2691 181 mohor
          if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2692 169 mohor
          begin
2693 181 mohor
            test_fail("Nvalid signal was set after read");
2694 169 mohor
            fail = fail + 1;
2695
          end
2696
        end
2697
      end
2698
    end
2699 181 mohor
    // set PHY to normal mode
2700
    #Tp eth_phy.preamble_suppresed(0);
2701
    // MII mode register
2702
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2703
    if(fail == 0)
2704
      test_ok;
2705
    else
2706
      fail = 0;
2707 169 mohor
  end
2708
 
2709
 
2710 181 mohor
  ////////////////////////////////////////////////////////////////////
2711
  ////                                                            ////
2712
  ////  Test busy and nvalid status durations during scan (with   ////
2713
  ////  and without preamble)                                     ////
2714
  ////                                                            ////
2715
  ////////////////////////////////////////////////////////////////////
2716
  if (test_num == 13) // 
2717 169 mohor
  begin
2718 194 tadej
    // TEST 13: BUSY AND NVALID STATUS DURATIONS DURING SCAN ( WITH AND WITHOUT PREAMBLE )
2719
    test_name   = "TEST 13: BUSY AND NVALID STATUS DURATIONS DURING SCAN ( WITH AND WITHOUT PREAMBLE )";
2720
    `TIME; $display("  TEST 13: BUSY AND NVALID STATUS DURATIONS DURING SCAN ( WITH AND WITHOUT PREAMBLE )");
2721 181 mohor
 
2722
    reset_mii; // reset MII
2723
    // set link up, if it wasn't due to previous tests, since there weren't PHY registers
2724
    #Tp eth_phy.link_up_down(1);
2725
    // set the MII
2726
    clk_div = 64;
2727
    mii_set_clk_div(clk_div[7:0]);
2728
    // set address
2729
    reg_addr = 5'h1; // status register
2730
    phy_addr = 5'h1; // correct PHY address
2731
 
2732
    for (i = 0; i <= 1; i = i + 1)
2733 169 mohor
    begin
2734 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
2735
      // MII mode register
2736
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}) | (`ETH_MIIMODER_CLKDIV & clk_div),
2737
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2738
      @(posedge Mdc_O);
2739
      // scan request
2740
      #Tp mii_scan_req(phy_addr, reg_addr);
2741
      // read data from MII status register - Busy and Nvalid bits
2742
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2743
 
2744
      // check MII IO signal and Busy and Nvalid bits
2745
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2746 169 mohor
      begin
2747 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is not HIGH Z - set higher clock divider");
2748
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2749
        begin
2750
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z anymore - 1. read");
2751
          fail = fail + 1;
2752
        end
2753
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2754
        begin
2755
          test_fail("Nvalid signal was not set while MII IO signal is not HIGH Z anymore - 1. read");
2756
          fail = fail + 1;
2757
        end
2758 169 mohor
      end
2759 181 mohor
      else // Busy bit should already be set to '1', due to reads from MII status register
2760 169 mohor
      begin
2761 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2762
        begin
2763
          test_fail("Busy signal should be set after scan, due to reads from MII status register");
2764
          fail = fail + 1;
2765
        end
2766
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2767
        begin
2768
          test_fail("Nvalid signal should be set after scan, due to reads from MII status register");
2769
          fail = fail + 1;
2770
        end
2771 169 mohor
      end
2772 181 mohor
 
2773
      // wait for serial bus to become active
2774 169 mohor
      wait(Mdio_IO !== 1'bz);
2775 181 mohor
      // count transfer bits
2776
      if (i)
2777 169 mohor
      begin
2778 181 mohor
        repeat(21) @(posedge Mdc_O);
2779 169 mohor
      end
2780 181 mohor
      else
2781 169 mohor
      begin
2782 181 mohor
        repeat(53) @(posedge Mdc_O);
2783 169 mohor
      end
2784 181 mohor
      // stop scan
2785
      #Tp mii_scan_finish; // finish scan operation
2786
 
2787
      // wait for next positive clock edge
2788
      repeat(10) @(posedge Mdc_O);
2789
      // read data from MII status register - Busy and Nvalid bits
2790
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2791
 
2792
      // check MII IO signal and Busy and Nvalid bits
2793
      if (Mdio_IO === 1'bz) // Mdio_IO should not be HIGH Z here - testbench selfcheck
2794 169 mohor
      begin
2795 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is HIGH Z - set higher clock divider");
2796
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2797 169 mohor
        begin
2798 181 mohor
          test_fail("Busy signal should be set while MII IO signal is not active anymore");
2799 169 mohor
          fail = fail + 1;
2800
        end
2801 181 mohor
        // Nvalid signal can be cleared here - it is still Testbench error
2802 169 mohor
      end
2803 181 mohor
      else // Busy bit should still be set to '1', Nvalid bit should still be set to '1'
2804 169 mohor
      begin
2805 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2806 169 mohor
        begin
2807 181 mohor
          test_fail("Busy signal should be set while MII IO signal not HIGH Z");
2808 169 mohor
          fail = fail + 1;
2809
        end
2810 181 mohor
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2811 169 mohor
        begin
2812 181 mohor
          test_fail("Nvalid signal should be set while MII IO signal not HIGH Z");
2813 169 mohor
          fail = fail + 1;
2814
        end
2815 181 mohor
      end
2816
 
2817
      // wait for next negative clock edge
2818
      @(negedge Mdc_O);
2819
      // read data from MII status register - Busy and Nvalid bits
2820
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2821
 
2822
      // check MII IO signal and Busy and Nvalid bits
2823
      if (Mdio_IO === 1'bz) // Mdio_IO should not be HIGH Z here - testbench selfcheck
2824
      begin
2825
        test_fail("Testbench error - read was to late, Mdio_IO is HIGH Z - set higher clock divider");
2826
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2827 169 mohor
        begin
2828 181 mohor
          test_fail("Busy signal should be set while MII IO signal is not active anymore");
2829
          fail = fail + 1;
2830 169 mohor
        end
2831 181 mohor
        // Nvalid signal can be cleared here - it is still Testbench error
2832 169 mohor
      end
2833 181 mohor
      else // Busy bit should still be set to '1', Nvalid bit should still be set to '1'
2834 169 mohor
      begin
2835 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2836 169 mohor
        begin
2837 181 mohor
          test_fail("Busy signal should be set while MII IO signal not HIGH Z");
2838
          fail = fail + 1;
2839 169 mohor
        end
2840 181 mohor
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2841 169 mohor
        begin
2842 181 mohor
          test_fail("Nvalid signal should be set while MII IO signal not HIGH Z");
2843
          fail = fail + 1;
2844 169 mohor
        end
2845
      end
2846 181 mohor
 
2847
      // wait for next negative clock edge
2848
      @(posedge Mdc_O);
2849
      // read data from MII status register - Busy and Nvalid bits
2850 169 mohor
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2851 181 mohor
 
2852
      // check MII IO signal and Busy and Nvalid bits
2853
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2854 169 mohor
      begin
2855 181 mohor
        test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2856
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2857 169 mohor
        begin
2858 181 mohor
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2859 169 mohor
          fail = fail + 1;
2860
        end
2861 181 mohor
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2862 169 mohor
        begin
2863 181 mohor
          test_fail("Nvalid signal was not set while MII IO signal is not HIGH Z");
2864 169 mohor
          fail = fail + 1;
2865
        end
2866
      end
2867 181 mohor
      else // Busy bit should still be set to '1', Nvalid bit can be set to '0'
2868 169 mohor
      begin
2869 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2870 169 mohor
        begin
2871 181 mohor
          test_fail("Busy signal should be set after MII IO signal become HIGH Z");
2872
          fail = fail + 1;
2873 169 mohor
        end
2874 181 mohor
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2875 169 mohor
        begin
2876 181 mohor
          i2 = 1; // check finished
2877 169 mohor
        end
2878 181 mohor
        else
2879 169 mohor
        begin
2880 181 mohor
          i2 = 0; // check must continue
2881 169 mohor
        end
2882
      end
2883 181 mohor
 
2884
      // wait for Busy to become inactive
2885
      i1 = 0;
2886
      while ((i1 <= 2) || (i2 == 0))
2887
      begin
2888
        // wait for next positive clock edge
2889
        @(posedge Mdc_O);
2890
        // read data from MII status register - Busy and Nvalid bits
2891
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2892
 
2893
        // check MII IO signal and Busy and Nvalid bits
2894
        if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2895 169 mohor
        begin
2896 181 mohor
          test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2897
          if (i1 <= 2)
2898 169 mohor
          begin
2899 181 mohor
            if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2900 169 mohor
            begin
2901 181 mohor
              test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2902 169 mohor
              fail = fail + 1;
2903
            end
2904
          end
2905 181 mohor
          if (i2 == 0)
2906 169 mohor
          begin
2907 181 mohor
            if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2908 169 mohor
            begin
2909 181 mohor
              test_fail("Nvalid signal was not set while MII IO signal is not HIGH Z");
2910 169 mohor
              fail = fail + 1;
2911
            end
2912
          end
2913
        end
2914 181 mohor
        else // wait for Busy bit to be set to '0'
2915 169 mohor
        begin
2916 181 mohor
          if (i1 <= 2)
2917 169 mohor
          begin
2918 181 mohor
            if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2919 169 mohor
            begin
2920 181 mohor
              i1 = 3; // end of Busy checking
2921 169 mohor
            end
2922 181 mohor
            else
2923 169 mohor
            begin
2924 181 mohor
              if (i1 == 2)
2925 169 mohor
              begin
2926 181 mohor
                test_fail("Busy signal should be cleared after 2 periods after MII IO signal become HIGH Z");
2927 169 mohor
                fail = fail + 1;
2928
              end
2929 181 mohor
              #Tp i1 = i1 + 1;
2930 169 mohor
            end
2931 181 mohor
          end
2932
          if (i2 == 0)
2933
          begin
2934
            if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2935 169 mohor
            begin
2936 181 mohor
              i2 = 1;
2937 169 mohor
            end
2938 181 mohor
            else
2939
            begin
2940
              test_fail("Nvalid signal should be cleared after MII IO signal become HIGH Z");
2941
              fail = fail + 1;
2942
            end
2943 169 mohor
          end
2944
        end
2945 181 mohor
      end
2946
    end
2947
    // set PHY to normal mode
2948
    #Tp eth_phy.preamble_suppresed(0);
2949
    // MII mode register
2950
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2951
    if(fail == 0)
2952
      test_ok;
2953
    else
2954
      fail = 0;
2955
  end
2956
 
2957
 
2958
  ////////////////////////////////////////////////////////////////////
2959
  ////                                                            ////
2960
  ////  Test scan status from phy with detecting link-fail bit    ////
2961
  ////  (with and without preamble)                               ////
2962
  ////                                                            ////
2963
  ////////////////////////////////////////////////////////////////////
2964
  if (test_num == 14) // 
2965
  begin
2966 194 tadej
    // TEST 14: SCAN STATUS FROM PHY WITH DETECTING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )
2967
    test_name   = "TEST 14: SCAN STATUS FROM PHY WITH DETECTING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )";
2968
    `TIME; $display("  TEST 14: SCAN STATUS FROM PHY WITH DETECTING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )");
2969 181 mohor
 
2970
    reset_mii; // reset MII
2971
    // set link up, if it wasn't due to previous tests, since there weren't PHY registers
2972
    #Tp eth_phy.link_up_down(1);
2973
    // set MII speed
2974
    clk_div = 6;
2975
    mii_set_clk_div(clk_div[7:0]);
2976
    // set address
2977
    reg_addr = 5'h1; // status register
2978
    phy_addr = 5'h1; // correct PHY address
2979
 
2980
    // read request
2981
    #Tp mii_read_req(phy_addr, reg_addr);
2982
    check_mii_busy; // wait for read to finish
2983
    // read data from PHY status register - remember LINK-UP status
2984
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2985
 
2986
    for (i = 0; i <= 1; i = i + 1)
2987
    begin
2988
      #Tp eth_phy.preamble_suppresed(i);
2989
      // MII mode register
2990
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}) | (`ETH_MIIMODER_CLKDIV & clk_div),
2991
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2992
      if (i)
2993
      begin
2994
        // change saved data when preamble is suppressed
2995
        #Tp tmp_data = tmp_data | 16'h0040; // put bit 6 to ONE
2996
      end
2997
 
2998
      // scan request
2999
      #Tp mii_scan_req(phy_addr, reg_addr);
3000
      check_mii_scan_valid; // wait for scan to make first data valid
3001
 
3002 169 mohor
      fork
3003 181 mohor
      begin
3004 169 mohor
        repeat(2) @(posedge Mdc_O);
3005
        // read data from PHY status register
3006
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3007 181 mohor
        if (phy_data !== tmp_data)
3008 169 mohor
        begin
3009 181 mohor
          test_fail("Data was not correctly scaned from status register");
3010 169 mohor
          fail = fail + 1;
3011
        end
3012
        // read data from MII status register
3013
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3014 181 mohor
        if (phy_data[0] !== 1'b0)
3015 169 mohor
        begin
3016 181 mohor
          test_fail("Link FAIL bit was set in the MII status register");
3017 169 mohor
          fail = fail + 1;
3018
        end
3019
      end
3020
      begin
3021 181 mohor
      // Completely check second scan
3022 169 mohor
        #Tp cnt = 0;
3023
        // wait for serial bus to become active - second scan
3024
        wait(Mdio_IO !== 1'bz);
3025
        // count transfer length
3026 181 mohor
        while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i == 0)) || ((cnt == 15) && (i == 1)) )
3027 169 mohor
        begin
3028
          @(posedge Mdc_O);
3029
          #Tp cnt = cnt + 1;
3030
        end
3031
        // check transfer length
3032 181 mohor
        if (i) // without preamble
3033 169 mohor
        begin
3034
          if (cnt != 33) // at this value Mdio_IO is HIGH Z
3035
          begin
3036 181 mohor
            test_fail("Second scan request did not proceed correctly");
3037 169 mohor
            fail = fail + 1;
3038
          end
3039
        end
3040
        else // with preamble
3041
        begin
3042
          if (cnt != 65) // at this value Mdio_IO is HIGH Z
3043
          begin
3044 181 mohor
            test_fail("Second scan request did not proceed correctly");
3045 169 mohor
            fail = fail + 1;
3046
          end
3047
        end
3048
      end
3049
      join
3050 181 mohor
      // check third to fifth scans
3051
      for (i3 = 0; i3 <= 2; i3 = i3 + 1)
3052
      begin
3053
        fork
3054 169 mohor
        begin
3055
          repeat(2) @(posedge Mdc_O);
3056
          // read data from PHY status register
3057
          #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3058 181 mohor
          if (phy_data !== tmp_data)
3059 169 mohor
          begin
3060 181 mohor
            test_fail("Data was not correctly scaned from status register");
3061
            fail = fail + 1;
3062 169 mohor
          end
3063
          // read data from MII status register
3064
          #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3065 181 mohor
          if (phy_data[0] !== 1'b0)
3066 169 mohor
          begin
3067 181 mohor
            test_fail("Link FAIL bit was set in the MII status register");
3068
            fail = fail + 1;
3069 169 mohor
          end
3070 181 mohor
          if (i3 == 2) // after fourth scan read
3071 169 mohor
          begin
3072 181 mohor
            @(posedge Mdc_O);
3073
            // change saved data
3074
            #Tp tmp_data = tmp_data & 16'hFFFB; // put bit 3 to ZERO
3075
            // set link down
3076
            #Tp eth_phy.link_up_down(0);
3077 169 mohor
          end
3078
        end
3079
        begin
3080 181 mohor
        // Completely check scans
3081
          #Tp cnt = 0;
3082
          // wait for serial bus to become active - second scan
3083
          wait(Mdio_IO !== 1'bz);
3084
          // count transfer length
3085
          while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i == 0)) || ((cnt == 15) && (i == 1)) )
3086 169 mohor
          begin
3087 181 mohor
            @(posedge Mdc_O);
3088
            #Tp cnt = cnt + 1;
3089
          end
3090
          // check transfer length
3091
          if (i) // without preamble
3092
          begin
3093
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
3094 169 mohor
            begin
3095 181 mohor
              test_fail("Fifth scan request did not proceed correctly");
3096
              fail = fail + 1;
3097 169 mohor
            end
3098 181 mohor
          end
3099
          else // with preamble
3100
          begin
3101
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
3102 169 mohor
            begin
3103 181 mohor
              test_fail("Fifth scan request did not proceed correctly");
3104
              fail = fail + 1;
3105 169 mohor
            end
3106
          end
3107
        end
3108 181 mohor
        join
3109
      end
3110
 
3111
      fork
3112
      begin
3113
        repeat(2) @(posedge Mdc_O);
3114
        // read data from PHY status register
3115
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3116
        if (phy_data !== tmp_data)
3117
        begin
3118
          test_fail("Data was not correctly scaned from status register");
3119
          fail = fail + 1;
3120
        end
3121
        // read data from MII status register
3122
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3123
        if (phy_data[0] === 1'b0)
3124
        begin
3125
          test_fail("Link FAIL bit was not set in the MII status register");
3126
          fail = fail + 1;
3127
        end
3128
        // wait to see if data stayed latched
3129
        repeat(4) @(posedge Mdc_O);
3130
        // read data from PHY status register
3131
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3132
        if (phy_data !== tmp_data)
3133
        begin
3134
          test_fail("Data was not latched correctly in status register");
3135
          fail = fail + 1;
3136
        end
3137
        // read data from MII status register
3138
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3139
        if (phy_data[0] === 1'b0)
3140
        begin
3141
          test_fail("Link FAIL bit was not set in the MII status register");
3142
          fail = fail + 1;
3143
        end
3144
        // change saved data
3145
        #Tp tmp_data = tmp_data | 16'h0004; // put bit 2 to ONE
3146
        // set link up
3147
        #Tp eth_phy.link_up_down(1);
3148
      end
3149
      begin
3150
      // Wait for sixth scan
3151
        // wait for serial bus to become active - sixth scan
3152
        wait(Mdio_IO !== 1'bz);
3153
        // wait for serial bus to become inactive - turn-around cycle in sixth scan
3154
        wait(Mdio_IO === 1'bz);
3155
        // wait for serial bus to become active - end of turn-around cycle in sixth scan
3156
        wait(Mdio_IO !== 1'bz);
3157
        // wait for serial bus to become inactive - end of sixth scan
3158
        wait(Mdio_IO === 1'bz);
3159
      end
3160 169 mohor
      join
3161 181 mohor
 
3162
      @(posedge Mdc_O);
3163 169 mohor
      // read data from PHY status register
3164
      #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3165
      if (phy_data !== tmp_data)
3166
      begin
3167 181 mohor
        test_fail("Data was not correctly scaned from status register");
3168 169 mohor
        fail = fail + 1;
3169
      end
3170
      // read data from MII status register
3171
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3172
      if (phy_data[0] !== 1'b0)
3173
      begin
3174
        test_fail("Link FAIL bit was set in the MII status register");
3175
        fail = fail + 1;
3176
      end
3177 181 mohor
      // wait to see if data stayed latched
3178
      repeat(4) @(posedge Mdc_O);
3179
      // read data from PHY status register
3180
      #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3181
      if (phy_data !== tmp_data)
3182 169 mohor
      begin
3183 181 mohor
        test_fail("Data was not correctly scaned from status register");
3184
        fail = fail + 1;
3185 169 mohor
      end
3186 181 mohor
      // read data from MII status register
3187
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3188
      if (phy_data[0] !== 1'b0)
3189 169 mohor
      begin
3190 181 mohor
        test_fail("Link FAIL bit was set in the MII status register");
3191
        fail = fail + 1;
3192 169 mohor
      end
3193 181 mohor
 
3194
      // STOP SCAN
3195
      #Tp mii_scan_finish; // finish scan operation
3196
      #Tp check_mii_busy; // wait for scan to finish
3197 169 mohor
    end
3198 181 mohor
    // set PHY to normal mode
3199
    #Tp eth_phy.preamble_suppresed(0);
3200
    // MII mode register
3201
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3202
    if(fail == 0)
3203
      test_ok;
3204
    else
3205
      fail = 0;
3206 169 mohor
  end
3207
 
3208
 
3209 181 mohor
  ////////////////////////////////////////////////////////////////////
3210
  ////                                                            ////
3211
  ////  Test scan status from phy with sliding link-fail bit      ////
3212
  ////  (with and without preamble)                               ////
3213
  ////                                                            ////
3214
  ////////////////////////////////////////////////////////////////////
3215
  if (test_num == 15) // 
3216 169 mohor
  begin
3217 194 tadej
    // TEST 15: SCAN STATUS FROM PHY WITH SLIDING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )
3218
    test_name   = "TEST 15: SCAN STATUS FROM PHY WITH SLIDING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )";
3219
    `TIME; $display("  TEST 15: SCAN STATUS FROM PHY WITH SLIDING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )");
3220 181 mohor
 
3221
    // set address
3222
    reg_addr = 5'h1; // status register
3223
    phy_addr = 5'h1; // correct PHY address
3224
 
3225
    // read request
3226
    #Tp mii_read_req(phy_addr, reg_addr);
3227
    check_mii_busy; // wait for read to finish
3228
    // read data from PHY status register - remember LINK-UP status
3229
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3230
 
3231
    for (i2 = 0; i2 <= 1; i2 = i2 + 1) // choose preamble or not
3232 169 mohor
    begin
3233 181 mohor
      #Tp eth_phy.preamble_suppresed(i2);
3234
      // MII mode register
3235
      #Tp wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i2, 8'h0}), 4'hF, 1, wbm_init_waits,
3236
                    wbm_subseq_waits);
3237
      if (i2)
3238 169 mohor
      begin
3239 181 mohor
        // change saved data when preamble is suppressed
3240
        #Tp tmp_data = tmp_data | 16'h0040; // put bit 6 to ONE
3241
      end
3242
 
3243
      i = 0;
3244
      while (i < 80) // delay for sliding of LinkFail bit
3245
      begin
3246
        // first there are two scans
3247
        #Tp cnt = 0;
3248 169 mohor
        // scan request
3249
        #Tp mii_scan_req(phy_addr, reg_addr);
3250 181 mohor
        #Tp check_mii_scan_valid; // wait for scan to make first data valid
3251
 
3252
        // check second scan
3253 169 mohor
        fork
3254 181 mohor
        begin
3255
          repeat(4) @(posedge Mdc_O);
3256
          // read data from PHY status register
3257
          #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3258
          if (phy_data !== tmp_data)
3259 169 mohor
          begin
3260 181 mohor
            test_fail("Second data was not correctly scaned from status register");
3261
            fail = fail + 1;
3262 169 mohor
          end
3263 181 mohor
          // read data from MII status register
3264
          #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3265
          if (phy_data[0] !== 1'b0)
3266
          begin
3267
            test_fail("Link FAIL bit was set in the MII status register");
3268
            fail = fail + 1;
3269
          end
3270
        end
3271
        begin
3272
        // Completely check scan
3273
          #Tp cnt = 0;
3274
          // wait for serial bus to become active - second scan
3275
          wait(Mdio_IO !== 1'bz);
3276
          // count transfer length
3277
          while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3278
          begin
3279
            @(posedge Mdc_O);
3280
            #Tp cnt = cnt + 1;
3281
          end
3282
          // check transfer length
3283
          if (i2) // without preamble
3284
          begin
3285
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
3286 169 mohor
            begin
3287 181 mohor
              test_fail("Second scan request did not proceed correctly");
3288
              fail = fail + 1;
3289 169 mohor
            end
3290 181 mohor
          end
3291
          else // with preamble
3292
          begin
3293
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
3294
            begin
3295
              test_fail("Second scan request did not proceed correctly");
3296
              fail = fail + 1;
3297
            end
3298
          end
3299
        end
3300
        join
3301
        // reset counter 
3302
        #Tp cnt = 0;
3303
        // SLIDING LINK DOWN and CHECK
3304
        fork
3305
          begin
3306
          // set link down
3307
            repeat(i) @(posedge Mdc_O);
3308
            // set link down
3309
            #Tp eth_phy.link_up_down(0);
3310
          end
3311
          begin
3312
          // check data in MII registers after each scan in this fork statement
3313 169 mohor
            if (i2) // without preamble
3314 181 mohor
              wait (cnt == 32);
3315
            else // with preamble
3316
              wait (cnt == 64);
3317
            repeat(3) @(posedge Mdc_O);
3318
            // read data from PHY status register
3319
            #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3320
            if ( ((i < 49) && !i2) || ((i < 17) && i2) )
3321 169 mohor
            begin
3322 181 mohor
              if (phy_data !== (tmp_data & 16'hFFFB)) // bit 3 is ZERO
3323 169 mohor
              begin
3324 181 mohor
                test_fail("Third data was not correctly scaned from status register");
3325 169 mohor
                fail = fail + 1;
3326
              end
3327
            end
3328 181 mohor
            else
3329 169 mohor
            begin
3330 181 mohor
              if (phy_data !== tmp_data)
3331 169 mohor
              begin
3332 181 mohor
                test_fail("Third data was not correctly scaned from status register");
3333 169 mohor
                fail = fail + 1;
3334
              end
3335
            end
3336 181 mohor
            // read data from MII status register
3337
            #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3338
            if ( ((i < 49) && !i2) || ((i < 17) && i2) )
3339 169 mohor
            begin
3340 181 mohor
              if (phy_data[0] === 1'b0)
3341
              begin
3342
                test_fail("Link FAIL bit was not set in the MII status register");
3343
                fail = fail + 1;
3344
              end
3345 169 mohor
            end
3346 181 mohor
            else
3347 169 mohor
            begin
3348 181 mohor
              if (phy_data[0] !== 1'b0)
3349 169 mohor
              begin
3350 181 mohor
                test_fail("Link FAIL bit was set in the MII status register");
3351 169 mohor
                fail = fail + 1;
3352
              end
3353
            end
3354 181 mohor
          end
3355
          begin
3356
          // check length
3357
            for (i3 = 0; i3 <= 1; i3 = i3 + 1) // two scans
3358 169 mohor
            begin
3359 181 mohor
              #Tp cnt = 0;
3360
              // wait for serial bus to become active if there is more than one scan
3361
              wait(Mdio_IO !== 1'bz);
3362
              // count transfer length
3363
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3364 169 mohor
              begin
3365 181 mohor
                @(posedge Mdc_O);
3366
                #Tp cnt = cnt + 1;
3367 169 mohor
              end
3368 181 mohor
              // check transfer length
3369
              if (i2) // without preamble
3370
              begin
3371
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3372
                begin
3373
                  test_fail("3. or 4. scan request did not proceed correctly, while SCAN STOP was written");
3374
                  fail = fail + 1;
3375
                end
3376
              end
3377
              else // with preamble
3378
              begin
3379
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3380
                begin
3381
                  test_fail("3. or 4. scan request did not proceed correctly, while SCAN STOP was written");
3382
                  fail = fail + 1;
3383
                end
3384
              end
3385 169 mohor
            end
3386
          end
3387
        join
3388 181 mohor
        // reset counter
3389
        #Tp cnt = 0;
3390
        // check fifth scan and data from fourth scan
3391
        fork
3392 169 mohor
        begin
3393 181 mohor
          repeat(2) @(posedge Mdc_O);
3394
          // read data from PHY status register
3395
          #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3396
          if (phy_data !== (tmp_data & 16'hFFFB)) // bit 3 is ZERO
3397 169 mohor
          begin
3398 181 mohor
            test_fail("4. data was not correctly scaned from status register");
3399
            fail = fail + 1;
3400 169 mohor
          end
3401 181 mohor
          // read data from MII status register
3402
          #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3403
          if (phy_data[0] === 1'b0)
3404 169 mohor
          begin
3405 181 mohor
            test_fail("Link FAIL bit was not set in the MII status register");
3406 169 mohor
            fail = fail + 1;
3407
          end
3408
        end
3409
        begin
3410 181 mohor
        // Completely check intermediate scan
3411
          #Tp cnt = 0;
3412
          // wait for serial bus to become active - second scan
3413 169 mohor
          wait(Mdio_IO !== 1'bz);
3414
          // count transfer length
3415
          while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3416
          begin
3417
            @(posedge Mdc_O);
3418
            #Tp cnt = cnt + 1;
3419
          end
3420 181 mohor
          // check transfer length
3421
          if (i2) // without preamble
3422 169 mohor
          begin
3423 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
3424
            begin
3425
              test_fail("Fifth scan request did not proceed correctly");
3426
              fail = fail + 1;
3427
            end
3428 169 mohor
          end
3429 181 mohor
          else // with preamble
3430
          begin
3431
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
3432
            begin
3433
              test_fail("Fifth scan request did not proceed correctly");
3434
              fail = fail + 1;
3435
            end
3436
          end
3437 169 mohor
        end
3438 181 mohor
        join
3439
        // reset counter 
3440
        #Tp cnt = 0;
3441
        // SLIDING LINK UP and CHECK
3442
        fork
3443 169 mohor
          begin
3444 181 mohor
          // set link up
3445
            repeat(i) @(posedge Mdc_O);
3446
            // set link up
3447
            #Tp eth_phy.link_up_down(1);
3448 169 mohor
          end
3449 181 mohor
          begin
3450
          // check data in MII registers after each scan in this fork statement
3451
            repeat(2) @(posedge Mdc_O);
3452
            if (i2) // without preamble
3453
              wait (cnt == 32);
3454
            else // with preamble
3455
              wait (cnt == 64);
3456
            repeat(3) @(posedge Mdc_O);
3457
            // read data from PHY status register
3458
            #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3459
            if ( ((i < 49) && !i2) || ((i < 17) && i2) )
3460
            begin
3461
              if (phy_data !== tmp_data)
3462
              begin
3463
                test_fail("6. data was not correctly scaned from status register");
3464
                fail = fail + 1;
3465
              end
3466
            end
3467
            else
3468
            begin
3469
              if (phy_data !== (tmp_data & 16'hFFFB)) // bit 3 is ZERO
3470
              begin
3471
                test_fail("6. data was not correctly scaned from status register");
3472
                fail = fail + 1;
3473
              end
3474
            end
3475
            // read data from MII status register
3476
            #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3477
            if ( ((i < 49) && !i2) || ((i < 17) && i2) )
3478
            begin
3479
              if (phy_data[0] !== 1'b0)
3480
              begin
3481
                test_fail("Link FAIL bit was set in the MII status register");
3482
                fail = fail + 1;
3483
              end
3484
            end
3485
            else
3486
            begin
3487
              if (phy_data[0] === 1'b0)
3488
              begin
3489
                test_fail("Link FAIL bit was not set in the MII status register");
3490
                fail = fail + 1;
3491
              end
3492
            end
3493
          end
3494
          begin
3495
          // check length
3496
            for (i3 = 0; i3 <= 1; i3 = i3 + 1) // two scans
3497
            begin
3498
              #Tp cnt = 0;
3499
              // wait for serial bus to become active if there is more than one scan
3500
              wait(Mdio_IO !== 1'bz);
3501
              // count transfer length
3502
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3503
              begin
3504
                @(posedge Mdc_O);
3505
                #Tp cnt = cnt + 1;
3506
              end
3507
              // check transfer length
3508
              if (i2) // without preamble
3509
              begin
3510
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3511
                begin
3512
                  test_fail("Scan request did not proceed correctly, while SCAN STOP was written");
3513
                  fail = fail + 1;
3514
                end
3515
              end
3516
              else // with preamble
3517
              begin
3518
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3519
                begin
3520
                  test_fail("Scan request did not proceed correctly, while SCAN STOP was written");
3521
                  fail = fail + 1;
3522
                end
3523
              end
3524
            end
3525
          end
3526
        join
3527
        // check last scan 
3528
        repeat(4) @(posedge Mdc_O);
3529
        // read data from PHY status register
3530
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3531
        if (phy_data !== tmp_data)
3532
        begin
3533
          test_fail("7. data was not correctly scaned from status register");
3534
          fail = fail + 1;
3535 169 mohor
        end
3536 181 mohor
        // read data from MII status register
3537
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3538
        if (phy_data[0] !== 1'b0)
3539
        begin
3540
          test_fail("Link FAIL bit was set in the MII status register");
3541
          fail = fail + 1;
3542
        end
3543
 
3544
        #Tp mii_scan_finish; // finish scan operation
3545
        #Tp check_mii_busy; // wait for scan to finish
3546
        #Tp;
3547
        // set delay of writing the command
3548
        if (i2) // without preamble
3549
        begin
3550
          case(i)
3551
            0,  1,  2,  3,  4:  i = i + 1;
3552
            13, 14, 15, 16, 17,
3553
            18, 19, 20, 21, 22,
3554
            23, 24, 25, 26, 27,
3555
            28, 29, 30, 31, 32,
3556
            33, 34, 35:         i = i + 1;
3557
            36:                 i = 80;
3558
            default:            i = 13;
3559
          endcase
3560
        end
3561 169 mohor
        else // with preamble
3562
        begin
3563 181 mohor
          case(i)
3564
            0,  1,  2,  3,  4:  i = i + 1;
3565
            45, 46, 47, 48, 49,
3566
            50, 51, 52, 53, 54,
3567
            55, 56, 57, 58, 59,
3568
            60, 61, 62, 63, 64,
3569
            65, 66, 67:         i = i + 1;
3570
            68:                 i = 80;
3571
            default:            i = 45;
3572
          endcase
3573 169 mohor
        end
3574 181 mohor
        @(posedge wb_clk);
3575
        #Tp;
3576 169 mohor
      end
3577
    end
3578 181 mohor
    // set PHY to normal mode
3579
    #Tp eth_phy.preamble_suppresed(0);
3580
    // MII mode register
3581
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3582
    if(fail == 0)
3583
      test_ok;
3584
    else
3585
      fail = 0;
3586 169 mohor
  end
3587
 
3588
 
3589 181 mohor
  ////////////////////////////////////////////////////////////////////
3590
  ////                                                            ////
3591
  ////  Test sliding stop scan command immediately after scan     ////
3592
  ////  request (with and without preamble)                       ////
3593
  ////                                                            ////
3594
  ////////////////////////////////////////////////////////////////////
3595
  if (test_num == 16) // 
3596 116 mohor
  begin
3597 194 tadej
    // TEST 16: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER SCAN REQUEST ( WITH AND WITHOUT PREAMBLE )
3598
    test_name = "TEST 16: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER SCAN REQUEST ( WITH AND WITHOUT PREAMBLE )";
3599 181 mohor
    `TIME;
3600 194 tadej
    $display("  TEST 16: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER SCAN REQUEST ( WITH AND WITHOUT PREAMBLE )");
3601 181 mohor
 
3602
    for (i2 = 0; i2 <= 1; i2 = i2 + 1) // choose preamble or not
3603 169 mohor
    begin
3604 181 mohor
      #Tp eth_phy.preamble_suppresed(i2);
3605
      // MII mode register
3606
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i2, 8'h0}), 4'hF, 1, wbm_init_waits,
3607
                wbm_subseq_waits);
3608
      i = 0;
3609
      cnt = 0;
3610
      while (i < 80) // delay for sliding of writing a STOP SCAN command
3611 169 mohor
      begin
3612 181 mohor
        for (i3 = 0; i3 <= 1; i3 = i3 + 1) // choose read or write after scan will be finished
3613 169 mohor
        begin
3614 181 mohor
          // set address
3615
          reg_addr = 5'h0; // control register
3616
          phy_addr = 5'h1; // correct PHY address
3617
          cnt = 0;
3618
          // scan request
3619
          #Tp mii_scan_req(phy_addr, reg_addr);
3620
          fork
3621
            begin
3622
              repeat(i) @(posedge Mdc_O);
3623
              // write command 0x0 into MII command register
3624
              // MII command written while scan in progress
3625
              wbm_write(`ETH_MIICOMMAND, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3626
              @(posedge wb_clk);
3627
              #Tp check_mii_busy; // wait for scan to finish
3628
              @(posedge wb_clk);
3629
              disable check;
3630
            end
3631
            begin: check
3632
              // wait for serial bus to become active
3633
              wait(Mdio_IO !== 1'bz);
3634
              // count transfer length
3635
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3636
              begin
3637
                @(posedge Mdc_O);
3638
                #Tp cnt = cnt + 1;
3639
              end
3640
              // check transfer length
3641
              if (i2) // without preamble
3642
              begin
3643
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3644
                begin
3645
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3646
                  fail = fail + 1;
3647
                end
3648
              end
3649
              else // with preamble
3650
              begin
3651
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3652
                begin
3653
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3654
                  fail = fail + 1;
3655
                end
3656
              end
3657
              cnt = 0;
3658
              // wait for serial bus to become active if there is more than one scan
3659
              wait(Mdio_IO !== 1'bz);
3660
              // count transfer length
3661
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3662
              begin
3663
                @(posedge Mdc_O);
3664
                #Tp cnt = cnt + 1;
3665
              end
3666
              // check transfer length
3667
              if (i2) // without preamble
3668
              begin
3669
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3670
                begin
3671
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3672
                  fail = fail + 1;
3673
                end
3674
              end
3675
              else // with preamble
3676
              begin
3677
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3678
                begin
3679
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3680
                  fail = fail + 1;
3681
                end
3682
              end
3683
            end
3684
          join
3685
          // check the BUSY signal to see if the bus is still IDLE
3686
          for (i1 = 0; i1 < 8; i1 = i1 + 1)
3687
            check_mii_busy; // wait for bus to become idle
3688
 
3689
          // try normal write or read after scan was finished
3690
          phy_data = {8'h7D, (i[7:0] + 1)};
3691
          cnt = 0;
3692
          if (i3 == 0) // write after scan
3693 169 mohor
          begin
3694 181 mohor
            // write request
3695
            #Tp mii_write_req(phy_addr, reg_addr, phy_data);
3696
            // wait for serial bus to become active
3697
            wait(Mdio_IO !== 1'bz);
3698
            // count transfer length
3699
            while(Mdio_IO !== 1'bz)
3700
            begin
3701
              @(posedge Mdc_O);
3702
              #Tp cnt = cnt + 1;
3703
            end
3704 169 mohor
            @(posedge Mdc_O);
3705 181 mohor
            // read request
3706
            #Tp mii_read_req(phy_addr, reg_addr);
3707
            check_mii_busy; // wait for read to finish
3708
            // read and check data
3709
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3710
            if (phy_data !== tmp_data)
3711 169 mohor
            begin
3712 181 mohor
              test_fail("Data was not correctly written into OR read from PHY register - control register");
3713 169 mohor
              fail = fail + 1;
3714
            end
3715
          end
3716 181 mohor
          else // read after scan
3717 169 mohor
          begin
3718 181 mohor
            // read request
3719
            #Tp mii_read_req(phy_addr, reg_addr);
3720
            // wait for serial bus to become active
3721
            wait(Mdio_IO !== 1'bz);
3722
            // count transfer length
3723
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3724 169 mohor
            begin
3725 181 mohor
              @(posedge Mdc_O);
3726
              #Tp cnt = cnt + 1;
3727
            end
3728
            @(posedge Mdc_O);
3729
            check_mii_busy; // wait for read to finish
3730
            // read and check data
3731
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3732
            if (phy_data !== tmp_data)
3733
            begin
3734
              test_fail("Data was not correctly written into OR read from PHY register - control register");
3735 169 mohor
              fail = fail + 1;
3736
            end
3737
          end
3738 181 mohor
          // check if transfer was a proper length
3739 169 mohor
          if (i2) // without preamble
3740
          begin
3741
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
3742
            begin
3743 181 mohor
              test_fail("New request did not proceed correctly, after scan request");
3744 169 mohor
              fail = fail + 1;
3745
            end
3746
          end
3747
          else // with preamble
3748
          begin
3749
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
3750
            begin
3751 181 mohor
              test_fail("New request did not proceed correctly, after scan request");
3752 169 mohor
              fail = fail + 1;
3753
            end
3754
          end
3755
        end
3756 181 mohor
        #Tp;
3757
        // set delay of writing the command
3758
        if (i2) // without preamble
3759
        begin
3760
          case(i)
3761
            0, 1:               i = i + 1;
3762
            18, 19, 20, 21, 22,
3763
            23, 24, 25, 26, 27,
3764
            28, 29, 30, 31, 32,
3765
            33, 34, 35:         i = i + 1;
3766
            36:                 i = 80;
3767
            default:            i = 18;
3768
          endcase
3769
        end
3770
        else // with preamble
3771
        begin
3772
          case(i)
3773
            0, 1:               i = i + 1;
3774
            50, 51, 52, 53, 54,
3775
            55, 56, 57, 58, 59,
3776
            60, 61, 62, 63, 64,
3777
            65, 66, 67:         i = i + 1;
3778
            68:                 i = 80;
3779
            default:            i = 50;
3780
          endcase
3781
        end
3782
        @(posedge wb_clk);
3783
      end
3784
    end
3785
    // set PHY to normal mode
3786
    #Tp eth_phy.preamble_suppresed(0);
3787
    // MII mode register
3788
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3789
    if(fail == 0)
3790
      test_ok;
3791
    else
3792
      fail = 0;
3793
  end
3794 169 mohor
 
3795 181 mohor
 
3796
  ////////////////////////////////////////////////////////////////////
3797
  ////                                                            ////
3798
  ////  Test sliding stop scan command after 2. scan (with and    ////
3799
  ////  without preamble)                                         ////
3800
  ////                                                            ////
3801
  ////////////////////////////////////////////////////////////////////
3802
  if (test_num == 17) // 
3803
  begin
3804 194 tadej
    // TEST 17: SLIDING STOP SCAN COMMAND AFTER 2. SCAN ( WITH AND WITHOUT PREAMBLE )
3805
    test_name = "TEST 17: SLIDING STOP SCAN COMMAND AFTER 2. SCAN ( WITH AND WITHOUT PREAMBLE )";
3806
    `TIME; $display("  TEST 17: SLIDING STOP SCAN COMMAND AFTER 2. SCAN ( WITH AND WITHOUT PREAMBLE )");
3807 181 mohor
 
3808
    for (i2 = 0; i2 <= 1; i2 = i2 + 1) // choose preamble or not
3809
    begin
3810
      #Tp eth_phy.preamble_suppresed(i2);
3811
      // MII mode register
3812
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i2, 8'h0}), 4'hF, 1, wbm_init_waits,
3813
                wbm_subseq_waits);
3814
 
3815
      i = 0;
3816
      cnt = 0;
3817
      while (i < 80) // delay for sliding of writing a STOP SCAN command
3818
      begin
3819
        for (i3 = 0; i3 <= 1; i3 = i3 + 1) // choose read or write after scan will be finished
3820
        begin
3821
          // first there are two scans
3822
          // set address
3823
          reg_addr = 5'h0; // control register
3824
          phy_addr = 5'h1; // correct PHY address
3825
          cnt = 0;
3826
          // scan request
3827
          #Tp mii_scan_req(phy_addr, reg_addr);
3828
          // wait and check first 2 scans
3829 169 mohor
          begin
3830
            // wait for serial bus to become active
3831
            wait(Mdio_IO !== 1'bz);
3832
            // count transfer length
3833
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3834
            begin
3835
              @(posedge Mdc_O);
3836
              #Tp cnt = cnt + 1;
3837
            end
3838
            // check transfer length
3839
            if (i2) // without preamble
3840
            begin
3841
              if (cnt != 33) // at this value Mdio_IO is HIGH Z
3842
              begin
3843
                test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3844
                fail = fail + 1;
3845
              end
3846
            end
3847
            else // with preamble
3848
            begin
3849
              if (cnt != 65) // at this value Mdio_IO is HIGH Z
3850
              begin
3851
                test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3852
                fail = fail + 1;
3853
              end
3854
            end
3855
            cnt = 0;
3856
            // wait for serial bus to become active if there is more than one scan
3857
            wait(Mdio_IO !== 1'bz);
3858
            // count transfer length
3859
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3860
            begin
3861
              @(posedge Mdc_O);
3862
              #Tp cnt = cnt + 1;
3863
            end
3864
            // check transfer length
3865
            if (i2) // without preamble
3866
            begin
3867
              if (cnt != 33) // at this value Mdio_IO is HIGH Z
3868
              begin
3869
                test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3870
                fail = fail + 1;
3871
              end
3872
            end
3873
            else // with preamble
3874
            begin
3875
              if (cnt != 65) // at this value Mdio_IO is HIGH Z
3876
              begin
3877
                test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3878
                fail = fail + 1;
3879
              end
3880
            end
3881
          end
3882
 
3883 181 mohor
          // reset counter 
3884
          cnt = 0;
3885
          fork
3886
            begin
3887
              repeat(i) @(posedge Mdc_O);
3888
              // write command 0x0 into MII command register
3889
              // MII command written while scan in progress
3890
              wbm_write(`ETH_MIICOMMAND, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3891
              @(posedge wb_clk);
3892
              #Tp check_mii_busy; // wait for scan to finish
3893
              @(posedge wb_clk);
3894
              disable check_3;
3895
            end
3896
            begin: check_3
3897
              // wait for serial bus to become active
3898
              wait(Mdio_IO !== 1'bz);
3899
              // count transfer length
3900
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3901
              begin
3902
                @(posedge Mdc_O);
3903
                #Tp cnt = cnt + 1;
3904
              end
3905
              // check transfer length
3906
              if (i2) // without preamble
3907
              begin
3908
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3909
                begin
3910
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3911
                  fail = fail + 1;
3912
                end
3913
              end
3914
              else // with preamble
3915
              begin
3916
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3917
                begin
3918
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3919
                  fail = fail + 1;
3920
                end
3921
              end
3922
              cnt = 0;
3923
              // wait for serial bus to become active if there is more than one scan
3924
              wait(Mdio_IO !== 1'bz);
3925
              // count transfer length
3926
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3927
              begin
3928
                @(posedge Mdc_O);
3929
                #Tp cnt = cnt + 1;
3930
              end
3931
              // check transfer length
3932
              if (i2) // without preamble
3933
              begin
3934
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3935
                begin
3936
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3937
                  fail = fail + 1;
3938
                end
3939
              end
3940
              else // with preamble
3941
              begin
3942
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3943
                begin
3944
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3945
                  fail = fail + 1;
3946
                end
3947
              end
3948
            end
3949
          join
3950
          // check the BUSY signal to see if the bus is still IDLE
3951
          for (i1 = 0; i1 < 8; i1 = i1 + 1)
3952
            check_mii_busy; // wait for bus to become idle
3953
 
3954
          // try normal write or read after scan was finished
3955
          phy_data = {8'h7D, (i[7:0] + 1)};
3956
          cnt = 0;
3957
          if (i3 == 0) // write after scan
3958 169 mohor
          begin
3959 181 mohor
            // write request
3960
            #Tp mii_write_req(phy_addr, reg_addr, phy_data);
3961
            // wait for serial bus to become active
3962
            wait(Mdio_IO !== 1'bz);
3963
            // count transfer length
3964
            while(Mdio_IO !== 1'bz)
3965
            begin
3966
              @(posedge Mdc_O);
3967
              #Tp cnt = cnt + 1;
3968
            end
3969 169 mohor
            @(posedge Mdc_O);
3970 181 mohor
            // read request
3971
            #Tp mii_read_req(phy_addr, reg_addr);
3972
            check_mii_busy; // wait for read to finish
3973
            // read and check data
3974
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3975
            if (phy_data !== tmp_data)
3976
            begin
3977
              test_fail("Data was not correctly written into OR read from PHY register - control register");
3978
              fail = fail + 1;
3979
            end
3980 169 mohor
          end
3981 181 mohor
          else // read after scan
3982 169 mohor
          begin
3983 181 mohor
            // read request
3984
            #Tp mii_read_req(phy_addr, reg_addr);
3985
            // wait for serial bus to become active
3986
            wait(Mdio_IO !== 1'bz);
3987
            // count transfer length
3988
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3989
            begin
3990
              @(posedge Mdc_O);
3991
              #Tp cnt = cnt + 1;
3992
            end
3993
            @(posedge Mdc_O);
3994
            check_mii_busy; // wait for read to finish
3995
            // read and check data
3996
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3997
            if (phy_data !== tmp_data)
3998
            begin
3999
              test_fail("Data was not correctly written into OR read from PHY register - control register");
4000
              fail = fail + 1;
4001
            end
4002 169 mohor
          end
4003 181 mohor
          // check if transfer was a proper length
4004
          if (i2) // without preamble
4005 169 mohor
          begin
4006 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
4007
            begin
4008
              test_fail("New request did not proceed correctly, after scan request");
4009
              fail = fail + 1;
4010
            end
4011 169 mohor
          end
4012 181 mohor
          else // with preamble
4013 169 mohor
          begin
4014 181 mohor
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
4015
            begin
4016
              test_fail("New request did not proceed correctly, after scan request");
4017
              fail = fail + 1;
4018
            end
4019 169 mohor
          end
4020
        end
4021 181 mohor
        #Tp;
4022
        // set delay of writing the command
4023 169 mohor
        if (i2) // without preamble
4024
        begin
4025 181 mohor
          case(i)
4026
            0, 1:               i = i + 1;
4027
            18, 19, 20, 21, 22,
4028
            23, 24, 25, 26, 27,
4029
            28, 29, 30, 31, 32,
4030
            33, 34, 35:         i = i + 1;
4031
            36:                 i = 80;
4032
            default:            i = 18;
4033
          endcase
4034 169 mohor
        end
4035
        else // with preamble
4036
        begin
4037 181 mohor
          case(i)
4038
            0, 1:               i = i + 1;
4039
            50, 51, 52, 53, 54,
4040
            55, 56, 57, 58, 59,
4041
            60, 61, 62, 63, 64,
4042
            65, 66, 67:         i = i + 1;
4043
            68:                 i = 80;
4044
            default:            i = 50;
4045
          endcase
4046 169 mohor
        end
4047 181 mohor
        @(posedge wb_clk);
4048 116 mohor
      end
4049
    end
4050 181 mohor
    // set PHY to normal mode
4051
    #Tp eth_phy.preamble_suppresed(0);
4052
    // MII mode register
4053
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4054
    if(fail == 0)
4055
      test_ok;
4056
    else
4057
      fail = 0;
4058 169 mohor
  end
4059 116 mohor
 
4060 181 mohor
end   //  for (test_num=start_task; test_num <= end_task; test_num=test_num+1)
4061
 
4062 169 mohor
end
4063
endtask // test_mii
4064
 
4065
 
4066
task test_mac_full_duplex_transmit;
4067
  input  [31:0]  start_task;
4068
  input  [31:0]  end_task;
4069
  integer        bit_start_1;
4070
  integer        bit_end_1;
4071
  integer        bit_start_2;
4072
  integer        bit_end_2;
4073
  integer        num_of_reg;
4074 209 tadejm
  integer        num_of_frames;
4075
  integer        num_of_bd;
4076 169 mohor
  integer        i_addr;
4077
  integer        i_data;
4078
  integer        i_length;
4079 209 tadejm
  integer        tmp_len;
4080
  integer        tmp_bd;
4081
  integer        tmp_bd_num;
4082 169 mohor
  integer        tmp_data;
4083 209 tadejm
  integer        tmp_ipgt;
4084 194 tadej
  integer        test_num;
4085 169 mohor
  reg    [31:0]  tx_bd_num;
4086
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_data;
4087
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_tmp_data;
4088
  integer        i;
4089
  integer        i1;
4090
  integer        i2;
4091
  integer        i3;
4092
  integer        fail;
4093
  integer        speed;
4094 209 tadejm
  reg            frame_started;
4095
  reg            frame_ended;
4096
  reg            wait_for_frame;
4097 169 mohor
  reg    [31:0]  addr;
4098
  reg    [31:0]  data;
4099
  reg    [31:0]  tmp;
4100
  reg    [ 7:0]  st_data;
4101
  reg    [15:0]  max_tmp;
4102
  reg    [15:0]  min_tmp;
4103
begin
4104
// MAC FULL DUPLEX TRANSMIT TEST
4105
test_heading("MAC FULL DUPLEX TRANSMIT TEST");
4106
$display(" ");
4107
$display("MAC FULL DUPLEX TRANSMIT TEST");
4108
fail = 0;
4109
 
4110
// reset MAC registers
4111
hard_reset;
4112
// reset MAC and MII LOGIC with soft reset
4113
reset_mac;
4114
reset_mii;
4115
// set wb slave response
4116
wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
4117
 
4118
  /*
4119
  TASKS for set and control TX buffer descriptors (also send packet - set_tx_bd_ready):
4120
  -------------------------------------------------------------------------------------
4121
  set_tx_bd
4122
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0], len[15:0], irq, pad, crc, txpnt[31:0]);
4123
  set_tx_bd_wrap
4124
    (tx_bd_num_end[6:0]);
4125
  set_tx_bd_ready
4126
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
4127
  check_tx_bd
4128
    (tx_bd_num_start[6:0], tx_bd_status[31:0]);
4129
  clear_tx_bd
4130
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
4131
 
4132
  TASKS for set and control RX buffer descriptors:
4133
  ------------------------------------------------
4134
  set_rx_bd
4135
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0], irq, rxpnt[31:0]);
4136
  set_rx_bd_wrap
4137
    (rx_bd_num_end[6:0]);
4138
  set_rx_bd_empty
4139
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
4140
  check_rx_bd
4141
    (rx_bd_num_end[6:0], rx_bd_status);
4142
  clear_rx_bd
4143
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
4144
 
4145
  TASKS for set and check TX packets:
4146
  -----------------------------------
4147
  set_tx_packet
4148
    (txpnt[31:0], len[15:0], eth_start_data[7:0]);
4149
  check_tx_packet
4150
    (txpnt_wb[31:0], txpnt_phy[31:0], len[15:0], failure[31:0]);
4151
 
4152
  TASKS for set and check RX packets:
4153
  -----------------------------------
4154
  set_rx_packet
4155
    (rxpnt[31:0], len[15:0], plus_nibble, d_addr[47:0], s_addr[47:0], type_len[15:0], start_data[7:0]);
4156
  check_rx_packet
4157
    (rxpnt_phy[31:0], rxpnt_wb[31:0], len[15:0], plus_nibble, successful_nibble, failure[31:0]);
4158
 
4159
  TASKS for append and check CRC to/of TX packet:
4160
  -----------------------------------------------
4161
  append_tx_crc
4162
    (txpnt_wb[31:0], len[15:0], negated_crc);
4163
  check_tx_crc
4164
    (txpnt_phy[31:0], len[15:0], negated_crc, failure[31:0]);
4165
 
4166
  TASK for append CRC to RX packet (CRC is checked together with check_rx_packet):
4167
  --------------------------------------------------------------------------------
4168
  append_rx_crc
4169
    (rxpnt_phy[31:0], len[15:0], plus_nibble, negated_crc);
4170
  */
4171
 
4172 194 tadej
//////////////////////////////////////////////////////////////////////
4173
////                                                              ////
4174
////  test_mac_full_duplex_transmit:                              ////
4175
////                                                              ////
4176
////  0: Test no transmit when all buffers are RX ( 10Mbps ).     ////
4177
////  1: Test no transmit when all buffers are RX ( 100Mbps ).    ////
4178
////  2: Test transmit packets form MINFL to MAXFL sizes at       ////
4179
////     one TX buffer decriptor ( 10Mbps ).                      ////
4180
////  3: Test transmit packets form MINFL to MAXFL sizes at       ////
4181
////     one TX buffer decriptor ( 100Mbps ).                     ////
4182
////                                                              ////
4183
//////////////////////////////////////////////////////////////////////
4184
for (test_num = start_task; test_num <= end_task; test_num = test_num + 1)
4185 169 mohor
begin
4186
 
4187 194 tadej
  ////////////////////////////////////////////////////////////////////
4188
  ////                                                            ////
4189
  ////  Test no transmit when all buffers are RX ( 10Mbps ).      ////
4190
  ////                                                            ////
4191
  ////////////////////////////////////////////////////////////////////
4192
  if (test_num == 0) // Test no transmit when all buffers are RX ( 10Mbps ).
4193 169 mohor
  begin
4194 194 tadej
    // TEST 0: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 10Mbps )
4195
    test_name   = "TEST 0: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 10Mbps )";
4196
    `TIME; $display("  TEST 0: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 10Mbps )");
4197
 
4198
    // unmask interrupts
4199 209 tadejm
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4200 194 tadej
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4201
    // set all buffer descriptors to RX - must be set before TX enable
4202
    wbm_write(`ETH_TX_BD_NUM, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4203
    // enable TX, set full-duplex mode, padding and CRC appending
4204
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4205
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4206
 
4207
    // write to phy's control register for 10Mbps
4208
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
4209
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
4210
    speed = 10;
4211
 
4212
    i = 0;
4213
    while (i < 128)
4214 169 mohor
    begin
4215 194 tadej
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
4216 169 mohor
      begin
4217 194 tadej
        set_tx_packet((`MEMORY_BASE + (i1 * 200)), 100, 0);
4218
        set_tx_bd(i1, i1, 100, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + (i1 * 200)));
4219 169 mohor
      end
4220 194 tadej
      set_tx_bd_wrap(i);
4221
      fork
4222
        begin
4223
          set_tx_bd_ready(0, i);
4224
          repeat(20) @(negedge mtx_clk);
4225
          #1 disable check_tx_en10;
4226
        end
4227
        begin: check_tx_en10
4228
          wait (MTxEn === 1'b1);
4229
          test_fail("Tramsmit should not start at all");
4230
          fail = fail + 1;
4231
          `TIME; $display("*E Transmit of %d packets should not start at all - active MTxEn", i);
4232
        end
4233
      join
4234
      for (i2 = 0; i2 < 20; i2 = i2 + 1)
4235 169 mohor
      begin
4236 194 tadej
        check_tx_bd(0, tmp);
4237
        #1;
4238
        if (tmp[15] === 1'b0)
4239
        begin
4240
          test_fail("Tramsmit should not start at all");
4241
          fail = fail + 1;
4242
          `TIME; $display("*E Transmit of %d packets should not start at all - ready is 0", i);
4243
        end
4244
        if (tmp[8:0] !== 0)
4245
        begin
4246
          test_fail("Tramsmit should not be finished since it should not start at all");
4247
          fail = fail + 1;
4248
          `TIME; $display("*E Transmit of should not be finished since it should not start at all");
4249
        end
4250
        @(posedge wb_clk);
4251 169 mohor
      end
4252 194 tadej
      wbm_read(`ETH_INT, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4253
      if (tmp[6:0] !== 0)
4254 169 mohor
      begin
4255 194 tadej
        test_fail("Tramsmit should not get INT since it should not start at all");
4256 169 mohor
        fail = fail + 1;
4257 194 tadej
        `TIME; $display("*E Transmit of should not get INT since it should not start at all");
4258 169 mohor
      end
4259 194 tadej
      clear_tx_bd(0, i);
4260
      if ((i < 5) || (i > 124))
4261
        i = i + 1;
4262
      else
4263
        i = i + 120;
4264 116 mohor
    end
4265 194 tadej
    // disable TX
4266
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4267
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4268
    if(fail == 0)
4269
      test_ok;
4270 116 mohor
    else
4271 194 tadej
      fail = 0;
4272 169 mohor
  end
4273 116 mohor
 
4274
 
4275 194 tadej
  ////////////////////////////////////////////////////////////////////
4276
  ////                                                            ////
4277
  ////  Test no transmit when all buffers are RX ( 100Mbps ).     ////
4278
  ////                                                            ////
4279
  ////////////////////////////////////////////////////////////////////
4280
  if (test_num == 1) // Test no transmit when all buffers are RX ( 100Mbps ).
4281 169 mohor
  begin
4282 194 tadej
    // TEST 1: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 100Mbps )
4283
    test_name   = "TEST 1: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 100Mbps )";
4284
    `TIME; $display("  TEST 1: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 100Mbps )");
4285
 
4286
    // unmask interrupts
4287 209 tadejm
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4288 194 tadej
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4289
    // set all buffer descriptors to RX - must be set before TX enable
4290
    wbm_write(`ETH_TX_BD_NUM, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4291
    // enable TX, set full-duplex mode, padding and CRC appending
4292
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4293
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4294
 
4295
    // write to phy's control register for 100Mbps
4296
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
4297
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
4298
    speed = 100;
4299
 
4300
    i = 0;
4301
    while (i < 128)
4302 169 mohor
    begin
4303 194 tadej
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
4304 169 mohor
      begin
4305 194 tadej
        set_tx_packet((`MEMORY_BASE + (i1 * 200)), 100, 0);
4306
        set_tx_bd(i1, i1, 100, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + (i1 * 200)));
4307 169 mohor
      end
4308 194 tadej
      set_tx_bd_wrap(i);
4309
      fork
4310
        begin
4311
          set_tx_bd_ready(0, i);
4312
          repeat(20) @(negedge mtx_clk);
4313
          #1 disable check_tx_en100;
4314
        end
4315
        begin: check_tx_en100
4316
          wait (MTxEn === 1'b1);
4317
          test_fail("Tramsmit should not start at all");
4318
          fail = fail + 1;
4319
          `TIME; $display("*E Transmit of %d packets should not start at all - active MTxEn", i);
4320
        end
4321
      join
4322
      for (i2 = 0; i2 < 20; i2 = i2 + 1)
4323 169 mohor
      begin
4324 194 tadej
        check_tx_bd(0, tmp);
4325
        #1;
4326
        if (tmp[15] === 1'b0)
4327
        begin
4328
          test_fail("Tramsmit should not start at all");
4329
          fail = fail + 1;
4330
          `TIME; $display("*E Transmit of %d packets should not start at all - ready is 0", i);
4331
        end
4332
        if (tmp[8:0] !== 0)
4333
        begin
4334
          test_fail("Tramsmit should not be finished since it should not start at all");
4335
          fail = fail + 1;
4336
          `TIME; $display("*E Transmit of should not be finished since it should not start at all");
4337
        end
4338
        @(posedge wb_clk);
4339 169 mohor
      end
4340 194 tadej
      wbm_read(`ETH_INT, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4341
      if (tmp[6:0] !== 0)
4342 169 mohor
      begin
4343 194 tadej
        test_fail("Tramsmit should not get INT since it should not start at all");
4344 169 mohor
        fail = fail + 1;
4345 194 tadej
        `TIME; $display("*E Transmit of should not get INT since it should not start at all");
4346 169 mohor
      end
4347 194 tadej
      clear_tx_bd(0, i);
4348
      if ((i < 5) || (i > 124))
4349
        i = i + 1;
4350
      else
4351
        i = i + 120;
4352 169 mohor
    end
4353 194 tadej
    // disable TX
4354
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4355
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4356
    if(fail == 0)
4357
      test_ok;
4358 169 mohor
    else
4359 194 tadej
      fail = 0;
4360 169 mohor
  end
4361
 
4362
 
4363 194 tadej
  ////////////////////////////////////////////////////////////////////
4364
  ////                                                            ////
4365
  ////  Test transmit packets form MINFL to MAXFL sizes at        ////
4366
  ////  one TX buffer decriptor ( 10Mbps ).                       ////
4367
  ////                                                            ////
4368
  ////////////////////////////////////////////////////////////////////
4369 209 tadejm
  if (test_num == 2) // without and with padding
4370 169 mohor
  begin
4371 194 tadej
    // TEST 2: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 10Mbps )
4372
    test_name = "TEST 2: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 10Mbps )";
4373
    `TIME; $display("  TEST 2: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 10Mbps )");
4374
 
4375
    max_tmp = 0;
4376
    min_tmp = 0;
4377
    // set one TX buffer descriptor - must be set before TX enable
4378
    wbm_write(`ETH_TX_BD_NUM, 32'h1, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4379 209 tadejm
    // enable TX, set full-duplex mode, NO padding and CRC appending
4380
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
4381 194 tadej
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4382
    // prepare two packets of MAXFL length
4383
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4384
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
4385
    min_tmp = tmp[31:16];
4386 209 tadejm
    st_data = 8'h01;
4387
    set_tx_packet(`MEMORY_BASE, (max_tmp), st_data); // length without CRC
4388 194 tadej
    st_data = 8'h10;
4389 209 tadejm
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp), st_data); // length without CRC
4390 194 tadej
    // check WB INT signal
4391
    if (wb_int !== 1'b0)
4392 169 mohor
    begin
4393 194 tadej
      test_fail("WB INT signal should not be set");
4394
      fail = fail + 1;
4395 169 mohor
    end
4396 194 tadej
 
4397
    // write to phy's control register for 10Mbps
4398
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
4399
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
4400
    speed = 10;
4401
 
4402
    i_length = (min_tmp - 4);
4403
    while (i_length <= (max_tmp - 4))
4404 169 mohor
    begin
4405 194 tadej
      // choose generating carrier sense and collision for first and last 64 lengths of frames
4406
      case (i_length[1:0])
4407
      2'h0: // Interrupt is generated
4408 169 mohor
      begin
4409 194 tadej
        // enable interrupt generation
4410 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
4411 194 tadej
        // unmask interrupts
4412 209 tadejm
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4413 194 tadej
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4414
        // not detect carrier sense in FD and no collision
4415
        eth_phy.carrier_sense_tx_fd_detect(0);
4416
        eth_phy.collision(0);
4417 169 mohor
      end
4418 194 tadej
      2'h1: // Interrupt is not generated
4419 169 mohor
      begin
4420 194 tadej
        // enable interrupt generation
4421 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
4422 194 tadej
        // mask interrupts
4423
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4424
        // detect carrier sense in FD and no collision
4425
        eth_phy.carrier_sense_tx_fd_detect(1);
4426
        eth_phy.collision(0);
4427 169 mohor
      end
4428 194 tadej
      2'h2: // Interrupt is not generated
4429
      begin
4430
        // disable interrupt generation
4431 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
4432 194 tadej
        // unmask interrupts
4433 209 tadejm
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4434 194 tadej
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4435
        // not detect carrier sense in FD and set collision
4436
        eth_phy.carrier_sense_tx_fd_detect(0);
4437
        eth_phy.collision(1);
4438
      end
4439
      default: // 2'h3: // Interrupt is not generated
4440
      begin
4441
        // disable interrupt generation
4442 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
4443 194 tadej
        // mask interrupts
4444
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4445
        // detect carrier sense in FD and set collision
4446
        eth_phy.carrier_sense_tx_fd_detect(1);
4447
        eth_phy.collision(1);
4448
      end
4449
      endcase
4450
      eth_phy.set_tx_mem_addr(max_tmp);
4451
      // set wrap bit
4452
      set_tx_bd_wrap(0);
4453
      set_tx_bd_ready(0, 0);
4454 169 mohor
      #1 check_tx_bd(0, data);
4455 194 tadej
      if (i_length < min_tmp) // just first four
4456 169 mohor
      begin
4457 194 tadej
        while (data[15] === 1)
4458
        begin
4459
          #1 check_tx_bd(0, data);
4460
          @(posedge wb_clk);
4461
        end
4462 209 tadejm
        repeat (1) @(posedge wb_clk);
4463 169 mohor
      end
4464 194 tadej
      else if (i_length > (max_tmp - 8)) // just last four
4465 192 tadej
      begin
4466 194 tadej
        tmp = 0;
4467
        wait (MTxEn === 1'b1); // start transmit
4468
        while (tmp < (i_length - 20))
4469
        begin
4470
          #1 tmp = tmp + 1;
4471
          @(posedge wb_clk);
4472
        end
4473
        #1 check_tx_bd(0, data);
4474
        while (data[15] === 1)
4475
        begin
4476
          #1 check_tx_bd(0, data);
4477
          @(posedge wb_clk);
4478
        end
4479 209 tadejm
        repeat (1) @(posedge wb_clk);
4480 192 tadej
      end
4481
      else
4482
      begin
4483 194 tadej
        wait (MTxEn === 1'b1); // start transmit
4484
        #1 check_tx_bd(0, data);
4485
        if (data[15] !== 1)
4486
        begin
4487
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
4488
          fail = fail + 1;
4489
        end
4490
        wait (MTxEn === 1'b0); // end transmit
4491
        while (data[15] === 1)
4492
        begin
4493
          #1 check_tx_bd(0, data);
4494
          @(posedge wb_clk);
4495
        end
4496
        repeat (1) @(posedge wb_clk);
4497 192 tadej
      end
4498 194 tadej
      // check length of a PACKET
4499
      if (eth_phy.tx_len != (i_length + 4))
4500 192 tadej
      begin
4501 194 tadej
        test_fail("Wrong length of the packet out from MAC");
4502 192 tadej
        fail = fail + 1;
4503
      end
4504 194 tadej
      // checking in the following if statement is performed only for first and last 64 lengths
4505
      if ( ((i_length + 4) <= (min_tmp + 64)) || ((i_length + 4) > (max_tmp - 64)) )
4506 192 tadej
      begin
4507 194 tadej
        // check transmitted TX packet data
4508
        if (i_length[0] == 0)
4509
        begin
4510 209 tadejm
          check_tx_packet((`MEMORY_BASE + i_length[1:0]), max_tmp, i_length, tmp);
4511 194 tadej
        end
4512
        else
4513
        begin
4514 209 tadejm
          check_tx_packet(((`MEMORY_BASE + i_length[1:0]) + max_tmp), max_tmp, i_length, tmp);
4515 194 tadej
        end
4516
        if (tmp > 0)
4517
        begin
4518
          test_fail("Wrong data of the transmitted packet");
4519
          fail = fail + 1;
4520
        end
4521
        // check transmited TX packet CRC
4522
        check_tx_crc(max_tmp, i_length, 1'b0, tmp); // length without CRC
4523
        if (tmp > 0)
4524
        begin
4525
          test_fail("Wrong CRC of the transmitted packet");
4526
          fail = fail + 1;
4527
        end
4528 192 tadej
      end
4529 194 tadej
      // check WB INT signal
4530
      if (i_length[1:0] == 2'h0)
4531 192 tadej
      begin
4532 194 tadej
        if (wb_int !== 1'b1)
4533
        begin
4534
          `TIME; $display("*E WB INT signal should be set");
4535
          test_fail("WB INT signal should be set");
4536
          fail = fail + 1;
4537
        end
4538 192 tadej
      end
4539 194 tadej
      else
4540 192 tadej
      begin
4541 194 tadej
        if (wb_int !== 1'b0)
4542
        begin
4543
          `TIME; $display("*E WB INT signal should not be set");
4544
          test_fail("WB INT signal should not be set");
4545
          fail = fail + 1;
4546
        end
4547 192 tadej
      end
4548 194 tadej
      // check TX buffer descriptor of a packet
4549
      check_tx_bd(0, data);
4550
      if (i_length[1] == 1'b0) // interrupt enabled
4551 192 tadej
      begin
4552 194 tadej
        if (data[15:0] !== 16'h7800)
4553
        begin
4554
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
4555
          test_fail("TX buffer descriptor status is not correct");
4556
          fail = fail + 1;
4557
        end
4558 192 tadej
      end
4559 194 tadej
      else // interrupt not enabled
4560 192 tadej
      begin
4561 194 tadej
        if (data[15:0] !== 16'h3800)
4562
        begin
4563
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
4564
          test_fail("TX buffer descriptor status is not correct");
4565
          fail = fail + 1;
4566
        end
4567 192 tadej
      end
4568 194 tadej
      // clear TX buffer descriptor
4569
      clear_tx_bd(0, 0);
4570
      // check interrupts
4571
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4572
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
4573 192 tadej
      begin
4574 194 tadej
        if ((data & `ETH_INT_TXB) !== 1'b1)
4575
        begin
4576
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
4577
          test_fail("Interrupt Transmit Buffer was not set");
4578
          fail = fail + 1;
4579
        end
4580
        if ((data & (~`ETH_INT_TXB)) !== 0)
4581
        begin
4582
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
4583
          test_fail("Other interrupts (except Transmit Buffer) were set");
4584
          fail = fail + 1;
4585
        end
4586 192 tadej
      end
4587 194 tadej
      else
4588 192 tadej
      begin
4589 194 tadej
        if (data !== 0)
4590
        begin
4591
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
4592
          test_fail("Any of interrupts (except Transmit Buffer) was set");
4593
          fail = fail + 1;
4594
        end
4595 192 tadej
      end
4596 194 tadej
      // clear interrupts
4597
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4598
      // check WB INT signal
4599
      if (wb_int !== 1'b0)
4600 192 tadej
      begin
4601 194 tadej
        test_fail("WB INT signal should not be set");
4602 192 tadej
        fail = fail + 1;
4603
      end
4604 194 tadej
      // INTERMEDIATE DISPLAYS
4605
      if ((i_length + 4) == (min_tmp + 64))
4606 209 tadejm
      begin
4607 194 tadej
        // starting length is min_tmp, ending length is (min_tmp + 64)
4608 209 tadejm
        $display("    pads appending to packets is NOT selected");
4609
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
4610 194 tadej
                 min_tmp, (min_tmp + 64));
4611 209 tadejm
        // set padding, remain the rest
4612
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4613
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4614
      end
4615 194 tadej
      else if ((i_length + 4) == (max_tmp - 16))
4616 209 tadejm
      begin
4617 194 tadej
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
4618 209 tadejm
        $display("    pads appending to packets is selected");
4619
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
4620 194 tadej
                 (min_tmp + 64 + 128), tmp_data);
4621 209 tadejm
        // reset padding, remain the rest
4622
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
4623
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4624
      end
4625 194 tadej
      else if ((i_length + 4) == max_tmp)
4626 209 tadejm
      begin
4627
        $display("    pads appending to packets is NOT selected");
4628
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
4629 194 tadej
                 (max_tmp - (4 + 16)), max_tmp);
4630 209 tadejm
      end
4631 194 tadej
      // set length (loop variable)
4632
      if ((i_length + 4) < (min_tmp + 64))
4633
        i_length = i_length + 1;
4634
      else if ( ((i_length + 4) >= (min_tmp + 64)) && ((i_length + 4) <= (max_tmp - 256)) )
4635
      begin
4636
        i_length = i_length + 128;
4637
        tmp_data = i_length + 4; // last tmp_data is ending length
4638
      end
4639
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
4640
        i_length = max_tmp - (4 + 16);
4641
      else if ((i_length + 4) >= (max_tmp - 16))
4642
        i_length = i_length + 1;
4643
      else
4644
      begin
4645
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
4646
        #10 $stop;
4647
      end
4648 192 tadej
    end
4649 194 tadej
    // disable TX
4650
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4651
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4652
    if(fail == 0)
4653
      test_ok;
4654
    else
4655
      fail = 0;
4656
  end
4657
 
4658
 
4659
  ////////////////////////////////////////////////////////////////////
4660
  ////                                                            ////
4661
  ////  Test transmit packets form MINFL to MAXFL sizes at        ////
4662
  ////  one TX buffer decriptor ( 100Mbps ).                      ////
4663
  ////                                                            ////
4664
  ////////////////////////////////////////////////////////////////////
4665 209 tadejm
  if (test_num == 3) // with and without padding
4666 194 tadej
  begin
4667
    // TEST 3: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 100Mbps )
4668
    test_name = "TEST 3: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 100Mbps )";
4669
    `TIME; $display("  TEST 3: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 100Mbps )");
4670
 
4671
    max_tmp = 0;
4672
    min_tmp = 0;
4673
    // set one TX buffer descriptor - must be set before TX enable
4674
    wbm_write(`ETH_TX_BD_NUM, 32'h1, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4675 209 tadejm
    // enable TX, set full-duplex mode, NO padding and CRC appending
4676
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
4677 194 tadej
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4678
    // prepare two packets of MAXFL length
4679
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4680
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
4681
    min_tmp = tmp[31:16];
4682
    st_data = 8'h5A;
4683 209 tadejm
    set_tx_packet(`MEMORY_BASE, (max_tmp), st_data); // length without CRC
4684 194 tadej
    st_data = 8'h10;
4685 209 tadejm
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp), st_data); // length without CRC
4686 169 mohor
    // check WB INT signal
4687 192 tadej
    if (wb_int !== 1'b0)
4688 169 mohor
    begin
4689
      test_fail("WB INT signal should not be set");
4690
      fail = fail + 1;
4691
    end
4692 194 tadej
 
4693
    // write to phy's control register for 100Mbps
4694
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
4695
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
4696
    speed = 100;
4697 192 tadej
 
4698 194 tadej
    i_length = (min_tmp - 4);
4699
    while (i_length <= (max_tmp - 4))
4700 192 tadej
    begin
4701 194 tadej
      // choose generating carrier sense and collision
4702
      case (i_length[1:0])
4703
      2'h0: // Interrupt is generated
4704 192 tadej
      begin
4705 194 tadej
        // enable interrupt generation
4706 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
4707 194 tadej
        // unmask interrupts
4708 209 tadejm
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4709 194 tadej
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4710
        // not detect carrier sense in FD and no collision
4711
        eth_phy.carrier_sense_tx_fd_detect(0);
4712
        eth_phy.collision(0);
4713 192 tadej
      end
4714 194 tadej
      2'h1: // Interrupt is not generated
4715 192 tadej
      begin
4716 194 tadej
        // enable interrupt generation
4717 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
4718 194 tadej
        // mask interrupts
4719
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4720
        // detect carrier sense in FD and no collision
4721
        eth_phy.carrier_sense_tx_fd_detect(1);
4722
        eth_phy.collision(0);
4723 192 tadej
      end
4724 194 tadej
      2'h2: // Interrupt is not generated
4725
      begin
4726
        // disable interrupt generation
4727 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
4728 194 tadej
        // unmask interrupts
4729 209 tadejm
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4730 194 tadej
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4731
        // not detect carrier sense in FD and set collision
4732
        eth_phy.carrier_sense_tx_fd_detect(0);
4733
        eth_phy.collision(1);
4734
      end
4735
      default: // 2'h3: // Interrupt is not generated
4736
      begin
4737
        // disable interrupt generation
4738 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
4739 194 tadej
        // mask interrupts
4740
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4741
        // detect carrier sense in FD and set collision
4742
        eth_phy.carrier_sense_tx_fd_detect(1);
4743
        eth_phy.collision(1);
4744
      end
4745
      endcase
4746
      eth_phy.set_tx_mem_addr(max_tmp);
4747
      // set wrap bit
4748
      set_tx_bd_wrap(0);
4749
      set_tx_bd_ready(0, 0);
4750 192 tadej
      #1 check_tx_bd(0, data);
4751 194 tadej
      if (i_length < min_tmp) // just first four
4752 192 tadej
      begin
4753 194 tadej
        while (data[15] === 1)
4754
        begin
4755
          #1 check_tx_bd(0, data);
4756
          @(posedge wb_clk);
4757
        end
4758 209 tadejm
        repeat (1) @(posedge wb_clk);
4759 194 tadej
      end
4760
      else if (i_length > (max_tmp - 8)) // just last four
4761
      begin
4762
        tmp = 0;
4763
        wait (MTxEn === 1'b1); // start transmit
4764
        while (tmp < (i_length - 20))
4765
        begin
4766
          #1 tmp = tmp + 1;
4767
          @(posedge wb_clk);
4768
        end
4769 192 tadej
        #1 check_tx_bd(0, data);
4770 194 tadej
        while (data[15] === 1)
4771
        begin
4772
          #1 check_tx_bd(0, data);
4773
          @(posedge wb_clk);
4774
        end
4775 209 tadejm
        repeat (1) @(posedge wb_clk);
4776 192 tadej
      end
4777 194 tadej
      else
4778
      begin
4779
        wait (MTxEn === 1'b1); // start transmit
4780
        #1 check_tx_bd(0, data);
4781
        if (data[15] !== 1)
4782
        begin
4783
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
4784
          fail = fail + 1;
4785
        end
4786
        wait (MTxEn === 1'b0); // end transmit
4787
        while (data[15] === 1)
4788
        begin
4789
          #1 check_tx_bd(0, data);
4790
          @(posedge wb_clk);
4791
        end
4792
        repeat (1) @(posedge wb_clk);
4793
      end
4794
      // check length of a PACKET
4795
      if (eth_phy.tx_len != (i_length + 4))
4796
      begin
4797
        test_fail("Wrong length of the packet out from MAC");
4798
        fail = fail + 1;
4799
      end
4800 192 tadej
      // check transmitted TX packet data
4801
      if (i_length[0] == 0)
4802
      begin
4803 209 tadejm
        check_tx_packet((`MEMORY_BASE + i_length[1:0]), max_tmp, i_length, tmp);
4804 192 tadej
      end
4805
      else
4806
      begin
4807 209 tadejm
        check_tx_packet(((`MEMORY_BASE + i_length[1:0]) + max_tmp), max_tmp, i_length, tmp);
4808 192 tadej
      end
4809
      if (tmp > 0)
4810
      begin
4811
        test_fail("Wrong data of the transmitted packet");
4812
        fail = fail + 1;
4813
      end
4814
      // check transmited TX packet CRC
4815
      check_tx_crc(max_tmp, i_length, 1'b0, tmp); // length without CRC
4816
      if (tmp > 0)
4817
      begin
4818
        test_fail("Wrong CRC of the transmitted packet");
4819
        fail = fail + 1;
4820
      end
4821 194 tadej
      // check WB INT signal
4822
      if (i_length[1:0] == 2'h0)
4823 192 tadej
      begin
4824 194 tadej
        if (wb_int !== 1'b1)
4825
        begin
4826
          `TIME; $display("*E WB INT signal should be set");
4827
          test_fail("WB INT signal should be set");
4828
          fail = fail + 1;
4829
        end
4830 192 tadej
      end
4831 194 tadej
      else
4832 192 tadej
      begin
4833 194 tadej
        if (wb_int !== 1'b0)
4834
        begin
4835
          `TIME; $display("*E WB INT signal should not be set");
4836
          test_fail("WB INT signal should not be set");
4837
          fail = fail + 1;
4838
        end
4839 192 tadej
      end
4840 194 tadej
      // check TX buffer descriptor of a packet
4841
      check_tx_bd(0, data);
4842
      if (i_length[1] == 1'b0) // interrupt enabled
4843 192 tadej
      begin
4844 194 tadej
        if (data[15:0] !== 16'h7800)
4845
        begin
4846
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
4847
          test_fail("TX buffer descriptor status is not correct");
4848
          fail = fail + 1;
4849
        end
4850 192 tadej
      end
4851 194 tadej
      else // interrupt not enabled
4852 192 tadej
      begin
4853 194 tadej
        if (data[15:0] !== 16'h3800)
4854
        begin
4855
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
4856
          test_fail("TX buffer descriptor status is not correct");
4857
          fail = fail + 1;
4858
        end
4859 192 tadej
      end
4860 194 tadej
      // clear TX buffer descriptor
4861
      clear_tx_bd(0, 0);
4862
      // check interrupts
4863
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4864
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
4865 192 tadej
      begin
4866 194 tadej
        if ((data & `ETH_INT_TXB) !== 1'b1)
4867
        begin
4868
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
4869
          test_fail("Interrupt Transmit Buffer was not set");
4870
          fail = fail + 1;
4871
        end
4872
        if ((data & (~`ETH_INT_TXB)) !== 0)
4873
        begin
4874
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
4875
          test_fail("Other interrupts (except Transmit Buffer) were set");
4876
          fail = fail + 1;
4877
        end
4878 192 tadej
      end
4879 194 tadej
      else
4880 192 tadej
      begin
4881 194 tadej
        if (data !== 0)
4882
        begin
4883
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h", data);
4884
          test_fail("Any of interrupts (except Transmit Buffer) was set");
4885
          fail = fail + 1;
4886
        end
4887 192 tadej
      end
4888 194 tadej
      // clear interrupts
4889
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4890
      // check WB INT signal
4891
      if (wb_int !== 1'b0)
4892 192 tadej
      begin
4893 194 tadej
        test_fail("WB INT signal should not be set");
4894 192 tadej
        fail = fail + 1;
4895
      end
4896 194 tadej
      // INTERMEDIATE DISPLAYS
4897
      if ((i_length + 4) == (min_tmp + 64))
4898 209 tadejm
      begin
4899 194 tadej
        // starting length is min_tmp, ending length is (min_tmp + 64)
4900 209 tadejm
        $display("    pads appending to packets is NOT selected");
4901
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
4902 194 tadej
                 min_tmp, (min_tmp + 64));
4903 209 tadejm
        // set padding, remain the rest
4904
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4905
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4906
      end
4907 194 tadej
      else if ((i_length + 4) == (max_tmp - 16))
4908 209 tadejm
      begin
4909 194 tadej
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
4910 209 tadejm
        $display("    pads appending to packets is selected");
4911
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
4912 194 tadej
                 (min_tmp + 64 + 128), tmp_data);
4913 209 tadejm
        // reset padding, remain the rest
4914
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
4915
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4916
      end
4917 194 tadej
      else if ((i_length + 4) == max_tmp)
4918 209 tadejm
      begin
4919
        $display("    pads appending to packets is NOT selected");
4920
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
4921 194 tadej
                 (max_tmp - (4 + 16)), max_tmp);
4922 209 tadejm
      end
4923 194 tadej
      // set length (loop variable)
4924
      if ((i_length + 4) < (min_tmp + 64))
4925
        i_length = i_length + 1;
4926
      else if ( ((i_length + 4) >= (min_tmp + 64)) && ((i_length + 4) <= (max_tmp - 256)) )
4927
      begin
4928
        i_length = i_length + 128;
4929
        tmp_data = i_length + 4; // last tmp_data is ending length
4930
      end
4931
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
4932
        i_length = max_tmp - (4 + 16);
4933
      else if ((i_length + 4) >= (max_tmp - 16))
4934
        i_length = i_length + 1;
4935 192 tadej
      else
4936 194 tadej
      begin
4937
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
4938
        #10 $stop;
4939
      end
4940 179 mohor
    end
4941 194 tadej
    // disable TX
4942
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4943
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4944
    if(fail == 0)
4945
      test_ok;
4946
    else
4947
      fail = 0;
4948 169 mohor
  end
4949
 
4950
 
4951 209 tadejm
  ////////////////////////////////////////////////////////////////////
4952
  ////                                                            ////
4953
  ////  Test transmit packets form MINFL to MAXFL sizes at        ////
4954
  ////  maximum TX buffer decriptors ( 10Mbps ).                  ////
4955
  ////                                                            ////
4956
  ////////////////////////////////////////////////////////////////////
4957
  if (test_num == 4) // without and with padding
4958
  begin
4959
    // TEST 4: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 10Mbps )
4960
    test_name = "TEST 4: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 10Mbps )";
4961
    `TIME; $display("  TEST 4: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 10Mbps )");
4962
 
4963
    // reset MAC registers
4964
    hard_reset;
4965
    // reset MAC and MII LOGIC with soft reset
4966
    reset_mac;
4967
    reset_mii;
4968
    // set wb slave response
4969
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
4970 192 tadej
 
4971 209 tadejm
    max_tmp = 0;
4972
    min_tmp = 0;
4973
    num_of_frames = 0;
4974
    num_of_bd = 0;
4975
    // set maximum TX buffer descriptors (128) - must be set before TX enable
4976
    wbm_write(`ETH_TX_BD_NUM, 32'h80, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4977
    // enable TX, set full-duplex mode, NO padding and CRC appending
4978
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
4979
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4980
    // prepare two packets of MAXFL length
4981
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4982
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
4983
    min_tmp = tmp[31:16];
4984
    st_data = 8'hA3;
4985
    set_tx_packet(`MEMORY_BASE, (max_tmp), st_data); // length without CRC
4986
    st_data = 8'h81;
4987
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp), st_data); // length without CRC
4988
    // check WB INT signal
4989
    if (wb_int !== 1'b0)
4990
    begin
4991
      test_fail("WB INT signal should not be set");
4992
      fail = fail + 1;
4993
    end
4994
 
4995
    // write to phy's control register for 10Mbps
4996
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
4997
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
4998
    speed = 10;
4999
 
5000
    i_length = (min_tmp - 4);
5001
    while (i_length <= (max_tmp - 4))
5002
    begin
5003
      // choose generating carrier sense and collision
5004
      case (i_length[1:0])
5005
      2'h0: // Interrupt is generated
5006
      begin
5007
        // Reset_tx_bd nable interrupt generation
5008
        // unmask interrupts
5009
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
5010
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5011
        // not detect carrier sense in FD and no collision
5012
        eth_phy.carrier_sense_tx_fd_detect(0);
5013
        eth_phy.collision(0);
5014
      end
5015
      2'h1: // Interrupt is not generated
5016
      begin
5017
        // set_tx_bd enable interrupt generation
5018
        // mask interrupts
5019
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5020
        // detect carrier sense in FD and no collision
5021
        eth_phy.carrier_sense_tx_fd_detect(1);
5022
        eth_phy.collision(0);
5023
      end
5024
      2'h2: // Interrupt is not generated
5025
      begin
5026
        // set_tx_bd disable the interrupt generation
5027
        // unmask interrupts
5028
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
5029
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5030
        // not detect carrier sense in FD and set collision
5031
        eth_phy.carrier_sense_tx_fd_detect(0);
5032
        eth_phy.collision(1);
5033
      end
5034
      default: // 2'h3: // Interrupt is not generated
5035
      begin
5036
        // set_tx_bd disable the interrupt generation
5037
        // mask interrupts
5038
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5039
        // detect carrier sense in FD and set collision
5040
        eth_phy.carrier_sense_tx_fd_detect(1);
5041
        eth_phy.collision(1);
5042
      end
5043
      endcase
5044
      // first destination address on ethernet PHY
5045
      if (i_length[0] == 0)
5046
        eth_phy.set_tx_mem_addr(0);
5047
      else
5048
        eth_phy.set_tx_mem_addr(max_tmp);
5049
      // first 8 frames are transmitted with TX BD 0 (wrap bit on TX BD 0)
5050
      // number of all frames is 154 (146 without first 8)
5051
      if (num_of_frames < 8)
5052
      begin
5053
        case (i_length[1:0])
5054
        2'h0: // Interrupt is generated
5055
        begin
5056
          // enable interrupt generation
5057
          set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
5058
          // interrupts are unmasked
5059
        end
5060
        2'h1: // Interrupt is not generated
5061
        begin
5062
          // enable interrupt generation
5063
          set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
5064
          // interrupts are masked
5065
        end
5066
        2'h2: // Interrupt is not generated
5067
        begin
5068
          // disable interrupt generation
5069
          set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
5070
          // interrupts are unmasked
5071
        end
5072
        default: // 2'h3: // Interrupt is not generated
5073
        begin
5074
          // disable interrupt generation
5075
          set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
5076
          // interrupts are masked
5077
        end
5078
        endcase
5079
        // set wrap bit
5080
        set_tx_bd_wrap(0);
5081
      end
5082
      // after first 8 number of frames, 128 frames form TX BD 0 to 127 will be transmitted
5083
      else if ((num_of_frames - 8) == 0)
5084
      begin
5085
        tmp_len = i_length; // length of frame
5086
        tmp_bd_num = 0; // TX BD number
5087
        while (tmp_bd_num < 128) // (tmp_len <= (max_tmp - 4)) - this is the last frame
5088
        begin
5089
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5090
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5091
          if (tmp_len[0] == 0)
5092
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
5093
          else
5094
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
5095
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
5096
          if ((tmp_len + 4) < (min_tmp + 128))
5097
            tmp_len = tmp_len + 1;
5098
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5099
            tmp_len = 256;
5100
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5101
            tmp_len = tmp_len + 128;
5102
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
5103
            tmp_len = max_tmp - (4 + 16);
5104
          else if ((tmp_len + 4) >= (max_tmp - 16))
5105
            tmp_len = tmp_len + 1;
5106
          // set TX BD number
5107
          tmp_bd_num = tmp_bd_num + 1;
5108
        end
5109
        // set wrap bit
5110
        set_tx_bd_wrap(127);
5111
      end
5112
      // after 128 + first 8 number of frames, 19 frames form TX BD 0 to 18 will be transmitted
5113
      else if ((num_of_frames - 8) == 20) // 128
5114
      begin
5115
        tmp_len = tmp_len; // length of frame remaines from previous settings
5116
        tmp_bd_num = 0; // TX BD number
5117
        while (tmp_bd_num < 19) // (tmp_len <= (max_tmp - 4)) - this is the last frame
5118
        begin
5119
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5120
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5121
          if (tmp_len[0] == 0)
5122
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
5123
          else
5124
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
5125
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
5126
          if ((tmp_len + 4) < (min_tmp + 128))
5127
            tmp_len = tmp_len + 1;
5128
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5129
            tmp_len = 256;
5130
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5131
            tmp_len = tmp_len + 128;
5132
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
5133
            tmp_len = max_tmp - (4 + 16);
5134
          else if ((tmp_len + 4) >= (max_tmp - 16))
5135
            tmp_len = tmp_len + 1;
5136
          // set TX BD number
5137
          tmp_bd_num = tmp_bd_num + 1;
5138
        end
5139
      end
5140
      // set ready bit
5141
      if (num_of_frames < 8)
5142
        set_tx_bd_ready(0, 0);
5143
      else if ((num_of_frames - 8) < 128)
5144
        set_tx_bd_ready((num_of_frames - 8), (num_of_frames - 8));
5145
      else if ((num_of_frames - 136) < 19)
5146
        set_tx_bd_ready((num_of_frames - 136), (num_of_frames - 136));
5147
      // CHECK END OF TRANSMITION
5148
      #1 check_tx_bd(num_of_bd, data);
5149
      if (i_length < min_tmp) // just first four
5150
      begin
5151
        while (data[15] === 1)
5152
        begin
5153
          #1 check_tx_bd(num_of_bd, data);
5154
          @(posedge wb_clk);
5155
        end
5156
        repeat (1) @(posedge wb_clk);
5157
      end
5158
      else if (i_length > (max_tmp - 8)) // just last four
5159
      begin
5160
        tmp = 0;
5161
        wait (MTxEn === 1'b1); // start transmit
5162
        while (tmp < (i_length - 20))
5163
        begin
5164
          #1 tmp = tmp + 1;
5165
          @(posedge wb_clk);
5166
        end
5167
        #1 check_tx_bd(num_of_bd, data);
5168
        while (data[15] === 1)
5169
        begin
5170
          #1 check_tx_bd(num_of_bd, data);
5171
          @(posedge wb_clk);
5172
        end
5173
        repeat (1) @(posedge wb_clk);
5174
      end
5175
      else
5176
      begin
5177
        wait (MTxEn === 1'b1); // start transmit
5178
        #1 check_tx_bd(num_of_bd, data);
5179
        if (data[15] !== 1)
5180
        begin
5181
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
5182
          fail = fail + 1;
5183
        end
5184
        wait (MTxEn === 1'b0); // end transmit
5185
        while (data[15] === 1)
5186
        begin
5187
          #1 check_tx_bd(num_of_bd, data);
5188
          @(posedge wb_clk);
5189
        end
5190
        repeat (1) @(posedge wb_clk);
5191
      end
5192
      // check length of a PACKET
5193
      if (eth_phy.tx_len != (i_length + 4))
5194
      begin
5195
        test_fail("Wrong length of the packet out from MAC");
5196
        fail = fail + 1;
5197
      end
5198
        // check transmitted TX packet data
5199
        if (i_length[0] == 0)
5200
        begin
5201
          check_tx_packet((`MEMORY_BASE + i_length[1:0]), 0, i_length, tmp);
5202
        end
5203
        else
5204
        begin
5205
          check_tx_packet(((`MEMORY_BASE + i_length[1:0]) + max_tmp), max_tmp, i_length, tmp);
5206
        end
5207
        if (tmp > 0)
5208
        begin
5209
          test_fail("Wrong data of the transmitted packet");
5210
          fail = fail + 1;
5211
        end
5212
        // check transmited TX packet CRC
5213
        if (i_length[0] == 0)
5214
          check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
5215
        else
5216
          check_tx_crc(max_tmp, i_length, 1'b0, tmp); // length without CRC
5217
        if (tmp > 0)
5218
        begin
5219
          test_fail("Wrong CRC of the transmitted packet");
5220
          fail = fail + 1;
5221
        end
5222
      // check WB INT signal
5223
      if (i_length[1:0] == 2'h0)
5224
      begin
5225
        if (wb_int !== 1'b1)
5226
        begin
5227
          `TIME; $display("*E WB INT signal should be set");
5228
          test_fail("WB INT signal should be set");
5229
          fail = fail + 1;
5230
        end
5231
      end
5232
      else
5233
      begin
5234
        if (wb_int !== 1'b0)
5235
        begin
5236
          `TIME; $display("*E WB INT signal should not be set");
5237
          test_fail("WB INT signal should not be set");
5238
          fail = fail + 1;
5239
        end
5240
      end
5241
      // check TX buffer descriptor of a packet
5242
      check_tx_bd(num_of_bd, data);
5243
      if (i_length[1] == 1'b0) // interrupt enabled
5244
      begin
5245
        if ( ((data[15:0] !== 16'h7800) && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
5246
             ((data[15:0] !== 16'h5800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
5247
        begin
5248
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
5249
          test_fail("TX buffer descriptor status is not correct");
5250
          fail = fail + 1;
5251
        end
5252
      end
5253
      else // interrupt not enabled
5254
      begin
5255
        if ( ((data[15:0] !== 16'h3800)  && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
5256
             ((data[15:0] !== 16'h1800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
5257
        begin
5258
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
5259
          test_fail("TX buffer descriptor status is not correct");
5260
          fail = fail + 1;
5261
        end
5262
      end
5263
      // clear first half of 8 frames from TX buffer descriptor 0
5264
      if (num_of_frames < 4)
5265
        clear_tx_bd(num_of_bd, num_of_bd);
5266
      // clear BD with wrap bit
5267
      if (num_of_frames == 140)
5268
        clear_tx_bd(127, 127);
5269
      // check interrupts
5270
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5271
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
5272
      begin
5273
        if ((data & `ETH_INT_TXB) !== 1'b1)
5274
        begin
5275
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
5276
          test_fail("Interrupt Transmit Buffer was not set");
5277
          fail = fail + 1;
5278
        end
5279
        if ((data & (~`ETH_INT_TXB)) !== 0)
5280
        begin
5281
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
5282
          test_fail("Other interrupts (except Transmit Buffer) were set");
5283
          fail = fail + 1;
5284
        end
5285
      end
5286
      else
5287
      begin
5288
        if (data !== 0)
5289
        begin
5290
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
5291
          test_fail("Any of interrupts (except Transmit Buffer) was set");
5292
          fail = fail + 1;
5293
        end
5294
      end
5295
      // clear interrupts
5296
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5297
      // check WB INT signal
5298
      if (wb_int !== 1'b0)
5299
      begin
5300
        test_fail("WB INT signal should not be set");
5301
        fail = fail + 1;
5302
      end
5303
      // INTERMEDIATE DISPLAYS
5304
      if ((i_length + 4) == (min_tmp + 7))
5305
      begin
5306
        // starting length is min_tmp, ending length is (min_tmp + 128)
5307
        $display("    pads appending to packets is NOT selected");
5308
        $display("    using only TX BD 0 out of 128 BDs assigned to TX (wrap at first BD - TX BD 0)");
5309
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
5310
                 min_tmp, (min_tmp + 7));
5311
        $display("    ->all packets were send from TX BD 0");
5312
        // set padding, remain the rest
5313
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5314
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5315
      end
5316
      else if ((i_length + 4) == (min_tmp + 128))
5317
      begin
5318
        // starting length is min_tmp, ending length is (min_tmp + 128)
5319
        $display("    pads appending to packets is NOT selected");
5320
        $display("    using all 128 BDs assigned to TX (wrap at 128th BD - TX BD 127)");
5321
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
5322
                 (min_tmp + 8), (min_tmp + 128));
5323
        $display("    ->packets were send from TX BD %0d to TX BD %0d respectively",
5324
                 1'b0, num_of_bd);
5325
        tmp_bd = num_of_bd + 1;
5326
        // set padding, remain the rest
5327
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5328
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5329
      end
5330
      else if ((i_length + 4) == (max_tmp - 16))
5331
      begin
5332
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
5333
        $display("    pads appending to packets is selected");
5334
        $display("    using all 128 BDs assigned to TX (wrap at 128th BD - TX BD 127)");
5335
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
5336
                 (min_tmp + 64 + 128), tmp_data);
5337
        if (tmp_bd > num_of_bd)
5338
          $display("    ->packets were send from TX BD %0d to TX BD 127 and from TX BD 0 to TX BD %0d respectively",
5339
                   tmp_bd, num_of_bd);
5340
        else
5341
          $display("    ->packets were send from TX BD %0d to TX BD %0d respectively",
5342
                   tmp_bd, num_of_bd);
5343
        tmp_bd = num_of_bd + 1;
5344
        // reset padding, remain the rest
5345
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
5346
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5347
      end
5348
      else if ((i_length + 4) == max_tmp)
5349
      begin
5350
        $display("    pads appending to packets is NOT selected");
5351
        $display("    using all 128 BDs assigned to TX (wrap at 128th BD - TX BD 127)");
5352
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
5353
                 (max_tmp - (4 + 16)), max_tmp);
5354
        if (tmp_bd > num_of_bd)
5355
          $display("    ->packets were send from TX BD %0d to TX BD 127 and from TX BD 0 to TX BD %0d respectively",
5356
                   tmp_bd, num_of_bd);
5357
        else
5358
          $display("    ->packets were send from TX BD %0d to TX BD %0d respectively",
5359
                   tmp_bd, num_of_bd);
5360
      end
5361
      // set length (loop variable)
5362
      if ((i_length + 4) < (min_tmp + 128))
5363
        i_length = i_length + 1;
5364
      else if ( ((i_length + 4) == (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
5365
        i_length = 256;
5366
      else if ( ((i_length + 4) > (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
5367
      begin
5368
        i_length = i_length + 128;
5369
        tmp_data = i_length + 4; // last tmp_data is ending length
5370
      end
5371
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
5372
        i_length = max_tmp - (4 + 16);
5373
      else if ((i_length + 4) >= (max_tmp - 16))
5374
        i_length = i_length + 1;
5375
      else
5376
      begin
5377
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
5378
        #10 $stop;
5379
      end
5380
      // the number of frame transmitted
5381
      num_of_frames = num_of_frames + 1;
5382
      if ((num_of_frames <= 8) || ((num_of_frames - 8) == 128))
5383
        num_of_bd = 0;
5384
      else
5385
        num_of_bd = num_of_bd + 1;
5386
    end
5387
    // disable TX
5388
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5389
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5390
    @(posedge wb_clk);
5391
    if(fail == 0)
5392
      test_ok;
5393
    else
5394
      fail = 0;
5395
  end
5396
 
5397
 
5398
  ////////////////////////////////////////////////////////////////////
5399
  ////                                                            ////
5400
  ////  Test transmit packets form MINFL to MAXFL sizes at        ////
5401
  ////  maximum TX buffer decriptors ( 100Mbps ).                 ////
5402
  ////                                                            ////
5403
  ////////////////////////////////////////////////////////////////////
5404
  if (test_num == 5) // with and without padding
5405
  begin
5406
    // TEST 5: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 100Mbps )
5407
    test_name = "TEST 5: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 100Mbps )";
5408
    `TIME; $display("  TEST 5: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 100Mbps )");
5409
 
5410
    // reset MAC registers
5411
    hard_reset;
5412
    // reset MAC and MII LOGIC with soft reset
5413
    reset_mac;
5414
    reset_mii;
5415
    // set wb slave response
5416
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
5417
 
5418
    max_tmp = 0;
5419
    min_tmp = 0;
5420
    num_of_frames = 0;
5421
    num_of_bd = 0;
5422
    // set maximum TX buffer descriptors (128) - must be set before TX enable
5423
    wbm_write(`ETH_TX_BD_NUM, 32'h80, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5424
    // enable TX, set full-duplex mode, NO padding and CRC appending
5425
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
5426
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5427
    // prepare two packets of MAXFL length
5428
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5429
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
5430
    min_tmp = tmp[31:16];
5431
    st_data = 8'hA5;
5432
    set_tx_packet(`MEMORY_BASE, (max_tmp), st_data); // length without CRC
5433
    st_data = 8'h71;
5434
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp), st_data); // length without CRC
5435
    // check WB INT signal
5436
    if (wb_int !== 1'b0)
5437
    begin
5438
      test_fail("WB INT signal should not be set");
5439
      fail = fail + 1;
5440
    end
5441
 
5442
    // write to phy's control register for 100Mbps
5443
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
5444
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
5445
    speed = 100;
5446
 
5447
    i_length = (min_tmp - 4);
5448
    while (i_length <= (max_tmp - 4))
5449
    begin
5450
      // choose generating carrier sense and collision
5451
      case (i_length[1:0])
5452
      2'h0: // Interrupt is generated
5453
      begin
5454
        // Reset_tx_bd nable interrupt generation
5455
        // unmask interrupts
5456
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
5457
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5458
        // not detect carrier sense in FD and no collision
5459
        eth_phy.carrier_sense_tx_fd_detect(0);
5460
        eth_phy.collision(0);
5461
      end
5462
      2'h1: // Interrupt is not generated
5463
      begin
5464
        // set_tx_bd enable interrupt generation
5465
        // mask interrupts
5466
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5467
        // detect carrier sense in FD and no collision
5468
        eth_phy.carrier_sense_tx_fd_detect(1);
5469
        eth_phy.collision(0);
5470
      end
5471
      2'h2: // Interrupt is not generated
5472
      begin
5473
        // set_tx_bd disable the interrupt generation
5474
        // unmask interrupts
5475
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
5476
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5477
        // not detect carrier sense in FD and set collision
5478
        eth_phy.carrier_sense_tx_fd_detect(0);
5479
        eth_phy.collision(1);
5480
      end
5481
      default: // 2'h3: // Interrupt is not generated
5482
      begin
5483
        // set_tx_bd disable the interrupt generation
5484
        // mask interrupts
5485
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5486
        // detect carrier sense in FD and set collision
5487
        eth_phy.carrier_sense_tx_fd_detect(1);
5488
        eth_phy.collision(1);
5489
      end
5490
      endcase
5491
      // first destination address on ethernet PHY
5492
      if (i_length[0] == 0)
5493
        eth_phy.set_tx_mem_addr(0);
5494
      else
5495
        eth_phy.set_tx_mem_addr(max_tmp);
5496
      // first 8 frames are transmitted with TX BD 0 (wrap bit on TX BD 0)
5497
      // number of all frames is 154 (146 without first 8)
5498
      if (num_of_frames < 8)
5499
      begin
5500
        case (i_length[1:0])
5501
        2'h0: // Interrupt is generated
5502
        begin
5503
          // enable interrupt generation
5504
          set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
5505
          // interrupts are unmasked
5506
        end
5507
        2'h1: // Interrupt is not generated
5508
        begin
5509
          // enable interrupt generation
5510
          set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
5511
          // interrupts are masked
5512
        end
5513
        2'h2: // Interrupt is not generated
5514
        begin
5515
          // disable interrupt generation
5516
          set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
5517
          // interrupts are unmasked
5518
        end
5519
        default: // 2'h3: // Interrupt is not generated
5520
        begin
5521
          // disable interrupt generation
5522
          set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
5523
          // interrupts are masked
5524
        end
5525
        endcase
5526
        // set wrap bit
5527
        set_tx_bd_wrap(0);
5528
      end
5529
      // after first 8 number of frames, 128 frames form TX BD 0 to 127 will be transmitted
5530
      else if ((num_of_frames - 8) == 0)
5531
      begin
5532
        tmp_len = i_length; // length of frame
5533
        tmp_bd_num = 0; // TX BD number
5534
        while (tmp_bd_num < 128) // (tmp_len <= (max_tmp - 4)) - this is the last frame
5535
        begin
5536
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5537
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5538
          if (tmp_len[0] == 0)
5539
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
5540
          else
5541
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
5542
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
5543
          if ((tmp_len + 4) < (min_tmp + 128))
5544
            tmp_len = tmp_len + 1;
5545
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5546
            tmp_len = 256;
5547
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5548
            tmp_len = tmp_len + 128;
5549
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
5550
            tmp_len = max_tmp - (4 + 16);
5551
          else if ((tmp_len + 4) >= (max_tmp - 16))
5552
            tmp_len = tmp_len + 1;
5553
          // set TX BD number
5554
          tmp_bd_num = tmp_bd_num + 1;
5555
        end
5556
        // set wrap bit
5557
        set_tx_bd_wrap(127);
5558
      end
5559
      // after 128 + first 8 number of frames, 19 frames form TX BD 0 to 18 will be transmitted
5560
      else if ((num_of_frames - 8) == 20) // 128
5561
      begin
5562
        tmp_len = tmp_len; // length of frame remaines from previous settings
5563
        tmp_bd_num = 0; // TX BD number
5564
        while (tmp_bd_num < 19) // (tmp_len <= (max_tmp - 4)) - this is the last frame
5565
        begin
5566
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5567
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5568
          if (tmp_len[0] == 0)
5569
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
5570
          else
5571
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
5572
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
5573
          if ((tmp_len + 4) < (min_tmp + 128))
5574
            tmp_len = tmp_len + 1;
5575
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5576
            tmp_len = 256;
5577
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5578
            tmp_len = tmp_len + 128;
5579
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
5580
            tmp_len = max_tmp - (4 + 16);
5581
          else if ((tmp_len + 4) >= (max_tmp - 16))
5582
            tmp_len = tmp_len + 1;
5583
          // set TX BD number
5584
          tmp_bd_num = tmp_bd_num + 1;
5585
        end
5586
      end
5587
      // set ready bit
5588
      if (num_of_frames < 8)
5589
        set_tx_bd_ready(0, 0);
5590
      else if ((num_of_frames - 8) < 128)
5591
        set_tx_bd_ready((num_of_frames - 8), (num_of_frames - 8));
5592
      else if ((num_of_frames - 136) < 19)
5593
        set_tx_bd_ready((num_of_frames - 136), (num_of_frames - 136));
5594
      // CHECK END OF TRANSMITION
5595
      #1 check_tx_bd(num_of_bd, data);
5596
      if (i_length < min_tmp) // just first four
5597
      begin
5598
        while (data[15] === 1)
5599
        begin
5600
          #1 check_tx_bd(num_of_bd, data);
5601
          @(posedge wb_clk);
5602
        end
5603
        repeat (1) @(posedge wb_clk);
5604
      end
5605
      else if (i_length > (max_tmp - 8)) // just last four
5606
      begin
5607
        tmp = 0;
5608
        wait (MTxEn === 1'b1); // start transmit
5609
        while (tmp < (i_length - 20))
5610
        begin
5611
          #1 tmp = tmp + 1;
5612
          @(posedge wb_clk);
5613
        end
5614
        #1 check_tx_bd(num_of_bd, data);
5615
        while (data[15] === 1)
5616
        begin
5617
          #1 check_tx_bd(num_of_bd, data);
5618
          @(posedge wb_clk);
5619
        end
5620
        repeat (1) @(posedge wb_clk);
5621
      end
5622
      else
5623
      begin
5624
        wait (MTxEn === 1'b1); // start transmit
5625
        #1 check_tx_bd(num_of_bd, data);
5626
        if (data[15] !== 1)
5627
        begin
5628
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
5629
          fail = fail + 1;
5630
        end
5631
        wait (MTxEn === 1'b0); // end transmit
5632
        while (data[15] === 1)
5633
        begin
5634
          #1 check_tx_bd(num_of_bd, data);
5635
          @(posedge wb_clk);
5636
        end
5637
        repeat (1) @(posedge wb_clk);
5638
      end
5639
      // check length of a PACKET
5640
      if (eth_phy.tx_len != (i_length + 4))
5641
      begin
5642
        test_fail("Wrong length of the packet out from MAC");
5643
        fail = fail + 1;
5644
      end
5645
      // checking in the following if statement is performed only for first and last 64 lengths
5646
        // check transmitted TX packet data
5647
        if (i_length[0] == 0)
5648
        begin
5649
          check_tx_packet((`MEMORY_BASE + i_length[1:0]), 0, i_length, tmp);
5650
        end
5651
        else
5652
        begin
5653
          check_tx_packet(((`MEMORY_BASE + i_length[1:0]) + max_tmp), max_tmp, i_length, tmp);
5654
        end
5655
        if (tmp > 0)
5656
        begin
5657
          test_fail("Wrong data of the transmitted packet");
5658
          fail = fail + 1;
5659
        end
5660
        // check transmited TX packet CRC
5661
        if (i_length[0] == 0)
5662
          check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
5663
        else
5664
          check_tx_crc(max_tmp, i_length, 1'b0, tmp); // length without CRC
5665
        if (tmp > 0)
5666
        begin
5667
          test_fail("Wrong CRC of the transmitted packet");
5668
          fail = fail + 1;
5669
        end
5670
      // check WB INT signal
5671
      if (i_length[1:0] == 2'h0)
5672
      begin
5673
        if (wb_int !== 1'b1)
5674
        begin
5675
          `TIME; $display("*E WB INT signal should be set");
5676
          test_fail("WB INT signal should be set");
5677
          fail = fail + 1;
5678
        end
5679
      end
5680
      else
5681
      begin
5682
        if (wb_int !== 1'b0)
5683
        begin
5684
          `TIME; $display("*E WB INT signal should not be set");
5685
          test_fail("WB INT signal should not be set");
5686
          fail = fail + 1;
5687
        end
5688
      end
5689
      // check TX buffer descriptor of a packet
5690
      check_tx_bd(num_of_bd, data);
5691
      if (i_length[1] == 1'b0) // interrupt enabled
5692
      begin
5693
        if ( ((data[15:0] !== 16'h7800) && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
5694
             ((data[15:0] !== 16'h5800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
5695
        begin
5696
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
5697
          test_fail("TX buffer descriptor status is not correct");
5698
          fail = fail + 1;
5699
        end
5700
      end
5701
      else // interrupt not enabled
5702
      begin
5703
        if ( ((data[15:0] !== 16'h3800)  && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
5704
             ((data[15:0] !== 16'h1800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
5705
        begin
5706
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
5707
          test_fail("TX buffer descriptor status is not correct");
5708
          fail = fail + 1;
5709
        end
5710
      end
5711
      // clear first half of 8 frames from TX buffer descriptor 0
5712
      if (num_of_frames < 4)
5713
        clear_tx_bd(num_of_bd, num_of_bd);
5714
      // clear BD with wrap bit
5715
      if (num_of_frames == 140)
5716
        clear_tx_bd(127, 127);
5717
      // check interrupts
5718
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5719
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
5720
      begin
5721
        if ((data & `ETH_INT_TXB) !== 1'b1)
5722
        begin
5723
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
5724
          test_fail("Interrupt Transmit Buffer was not set");
5725
          fail = fail + 1;
5726
        end
5727
        if ((data & (~`ETH_INT_TXB)) !== 0)
5728
        begin
5729
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
5730
          test_fail("Other interrupts (except Transmit Buffer) were set");
5731
          fail = fail + 1;
5732
        end
5733
      end
5734
      else
5735
      begin
5736
        if (data !== 0)
5737
        begin
5738
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
5739
          test_fail("Any of interrupts (except Transmit Buffer) was set");
5740
          fail = fail + 1;
5741
        end
5742
      end
5743
      // clear interrupts
5744
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5745
      // check WB INT signal
5746
      if (wb_int !== 1'b0)
5747
      begin
5748
        test_fail("WB INT signal should not be set");
5749
        fail = fail + 1;
5750
      end
5751
      // INTERMEDIATE DISPLAYS
5752
      if ((i_length + 4) == (min_tmp + 7))
5753
      begin
5754
        // starting length is min_tmp, ending length is (min_tmp + 128)
5755
        $display("    pads appending to packets is NOT selected");
5756
        $display("    using only TX BD 0 out of 128 BDs assigned to TX (wrap at first BD - TX BD 0)");
5757
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
5758
                 min_tmp, (min_tmp + 7));
5759
        $display("    ->all packets were send from TX BD 0");
5760
        // set padding, remain the rest
5761
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5762
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5763
      end
5764
      else if ((i_length + 4) == (min_tmp + 128))
5765
      begin
5766
        // starting length is min_tmp, ending length is (min_tmp + 128)
5767
        $display("    pads appending to packets is NOT selected");
5768
        $display("    using all 128 BDs assigned to TX (wrap at 128th BD - TX BD 127)");
5769
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
5770
                 (min_tmp + 8), (min_tmp + 128));
5771
        $display("    ->packets were send from TX BD %0d to TX BD %0d respectively",
5772
                 1'b0, num_of_bd);
5773
        tmp_bd = num_of_bd + 1;
5774
        // set padding, remain the rest
5775
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5776
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5777
      end
5778
      else if ((i_length + 4) == (max_tmp - 16))
5779
      begin
5780
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
5781
        $display("    pads appending to packets is selected");
5782
        $display("    using all 128 BDs assigned to TX (wrap at 128th BD - TX BD 127)");
5783
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
5784
                 (min_tmp + 64 + 128), tmp_data);
5785
        if (tmp_bd > num_of_bd)
5786
          $display("    ->packets were send from TX BD %0d to TX BD 127 and from TX BD 0 to TX BD %0d respectively",
5787
                   tmp_bd, num_of_bd);
5788
        else
5789
          $display("    ->packets were send from TX BD %0d to TX BD %0d respectively",
5790
                   tmp_bd, num_of_bd);
5791
        tmp_bd = num_of_bd + 1;
5792
        // reset padding, remain the rest
5793
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
5794
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5795
      end
5796
      else if ((i_length + 4) == max_tmp)
5797
      begin
5798
        $display("    pads appending to packets is NOT selected");
5799
        $display("    using all 128 BDs assigned to TX (wrap at 128th BD - TX BD 127)");
5800
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
5801
                 (max_tmp - (4 + 16)), max_tmp);
5802
        if (tmp_bd > num_of_bd)
5803
          $display("    ->packets were send from TX BD %0d to TX BD 127 and from TX BD 0 to TX BD %0d respectively",
5804
                   tmp_bd, num_of_bd);
5805
        else
5806
          $display("    ->packets were send from TX BD %0d to TX BD %0d respectively",
5807
                   tmp_bd, num_of_bd);
5808
      end
5809
      // set length (loop variable)
5810
      if ((i_length + 4) < (min_tmp + 128))
5811
        i_length = i_length + 1;
5812
      else if ( ((i_length + 4) == (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
5813
        i_length = 256;
5814
      else if ( ((i_length + 4) > (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
5815
      begin
5816
        i_length = i_length + 128;
5817
        tmp_data = i_length + 4; // last tmp_data is ending length
5818
      end
5819
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
5820
        i_length = max_tmp - (4 + 16);
5821
      else if ((i_length + 4) >= (max_tmp - 16))
5822
        i_length = i_length + 1;
5823
      else
5824
      begin
5825
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
5826
        #10 $stop;
5827
      end
5828
      // the number of frame transmitted
5829
      num_of_frames = num_of_frames + 1;
5830
      if ((num_of_frames <= 8) || ((num_of_frames - 8) == 128))
5831
        num_of_bd = 0;
5832
      else
5833
        num_of_bd = num_of_bd + 1;
5834
    end
5835
    // disable TX
5836
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5837
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5838
    @(posedge wb_clk);
5839
    if(fail == 0)
5840
      test_ok;
5841
    else
5842
      fail = 0;
5843
  end
5844
 
5845
 
5846
  ////////////////////////////////////////////////////////////////////
5847
  ////                                                            ////
5848
  ////  Test transmit packets form 0 to (MINFL - 1) sizes at      ////
5849
  ////  8 TX buffer decriptors ( 10Mbps ).                        ////
5850
  ////                                                            ////
5851
  ////////////////////////////////////////////////////////////////////
5852
  if (test_num == 6) // 
5853
  begin
5854
    // TEST 6: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )
5855
    test_name = "TEST 6: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )";
5856
    `TIME; $display("  TEST 6: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )");
5857
 
5858
    // reset MAC registers
5859
    hard_reset;
5860
    // reset MAC and MII LOGIC with soft reset
5861
    reset_mac;
5862
    reset_mii;
5863
    // set wb slave response
5864
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
5865
 
5866
    max_tmp = 0;
5867
    min_tmp = 0;
5868
    // set 8 TX buffer descriptors - must be set before TX enable
5869
    wbm_write(`ETH_TX_BD_NUM, 32'h8, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5870
    // enable TX, set full-duplex mode, padding and CRC appending
5871
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
5872
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5873
    // prepare two packets of MAXFL length
5874
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5875
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
5876
    min_tmp = tmp[31:16];
5877
    st_data = 8'h12;
5878
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
5879
    st_data = 8'h34;
5880
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp - 4), st_data); // length without CRC
5881
    // check WB INT signal
5882
    if (wb_int !== 1'b0)
5883
    begin
5884
      test_fail("WB INT signal should not be set");
5885
      fail = fail + 1;
5886
    end
5887
 
5888
    // write to phy's control register for 10Mbps
5889
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
5890
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
5891
    speed = 10;
5892
 
5893
    frame_started = 0;
5894
    num_of_frames = 0;
5895
    num_of_bd = 0;
5896
    i_length = 0; // 0;
5897
    while (i_length < 70) // (min_tmp - 4))
5898
    begin
5899
      #1;
5900
      // choose generating carrier sense and collision
5901
      case (i_length[1:0])
5902
      2'h0: // Interrupt is generated
5903
      begin
5904
        // Reset_tx_bd nable interrupt generation
5905
        // unmask interrupts
5906
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
5907
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5908
        // not detect carrier sense in FD and no collision
5909
        eth_phy.carrier_sense_tx_fd_detect(0);
5910
        eth_phy.collision(0);
5911
      end
5912
      2'h1: // Interrupt is not generated
5913
      begin
5914
        // set_tx_bd enable interrupt generation
5915
        // mask interrupts
5916
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5917
        // detect carrier sense in FD and no collision
5918
        eth_phy.carrier_sense_tx_fd_detect(1);
5919
        eth_phy.collision(0);
5920
      end
5921
      2'h2: // Interrupt is not generated
5922
      begin
5923
        // set_tx_bd disable the interrupt generation
5924
        // unmask interrupts
5925
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
5926
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5927
        // not detect carrier sense in FD and set collision
5928
        eth_phy.carrier_sense_tx_fd_detect(0);
5929
        eth_phy.collision(1);
5930
      end
5931
      default: // 2'h3: // Interrupt is not generated
5932
      begin
5933
        // set_tx_bd disable the interrupt generation
5934
        // mask interrupts
5935
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5936
        // detect carrier sense in FD and set collision
5937
        eth_phy.carrier_sense_tx_fd_detect(1);
5938
        eth_phy.collision(1);
5939
      end
5940
      endcase
5941
      #1;
5942
      // first destination address on ethernet PHY
5943
      eth_phy.set_tx_mem_addr(num_of_frames * 16);
5944
      // SET packets and wrap bit
5945
      // num_of_frames <= 9 => wrap set to TX BD 0
5946
      if (num_of_frames <= 9)
5947
      begin
5948
        tmp_len = i_length; // length of frame
5949
        tmp_bd_num = 0; // TX BD number
5950
        // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5951
        // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5952
        if (tmp_len[0] == 0)
5953
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
5954
        else
5955
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
5956
        // set wrap bit
5957
        set_tx_bd_wrap(0);
5958
      end
5959
      // 10 <= num_of_frames < 18 => wrap set to TX BD 3
5960
      else if ((num_of_frames == 10) || (num_of_frames == 14))
5961
      begin
5962
        tmp_len = i_length; // length of frame
5963
        tmp_bd_num = 0; // TX BD number
5964
        while (tmp_bd_num < 4) //
5965
        begin
5966
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5967
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5968
          if (tmp_len[0] == 0)
5969
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
5970
          else
5971
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
5972
          tmp_len = tmp_len + 1;
5973
          // set TX BD number
5974
          tmp_bd_num = tmp_bd_num + 1;
5975
        end
5976
        // set wrap bit
5977
        set_tx_bd_wrap(3);
5978
      end
5979
      // 18 <= num_of_frames < 28 => wrap set to TX BD 4
5980
      else if ((num_of_frames == 18) || (num_of_frames == 23))
5981
      begin
5982
        tmp_len = i_length; // length of frame
5983
        tmp_bd_num = 0; // TX BD number
5984
        while (tmp_bd_num < 5) //
5985
        begin
5986
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5987
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5988
          if (tmp_len[0] == 0)
5989
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
5990
          else
5991
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
5992
          tmp_len = tmp_len + 1;
5993
          // set TX BD number
5994
          tmp_bd_num = tmp_bd_num + 1;
5995
        end
5996
        // set wrap bit
5997
        set_tx_bd_wrap(4);
5998
      end
5999
      // 28 <= num_of_frames < 40 => wrap set to TX BD 5
6000
      else if ((num_of_frames == 28) || (num_of_frames == 34))
6001
      begin
6002
        tmp_len = i_length; // length of frame
6003
        tmp_bd_num = 0; // TX BD number
6004
        while (tmp_bd_num < 6) //
6005
        begin
6006
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6007
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6008
          if (tmp_len[0] == 0)
6009
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6010
          else
6011
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6012
          tmp_len = tmp_len + 1;
6013
          // set TX BD number
6014
          tmp_bd_num = tmp_bd_num + 1;
6015
        end
6016
        // set wrap bit
6017
        set_tx_bd_wrap(5);
6018
      end
6019
      // 40 <= num_of_frames < 54 => wrap set to TX BD 6
6020
      else if ((num_of_frames == 40) || (num_of_frames == 47))
6021
      begin
6022
        tmp_len = i_length; // length of frame
6023
        tmp_bd_num = 0; // TX BD number
6024
        while (tmp_bd_num < 7) //
6025
        begin
6026
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6027
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6028
          if (tmp_len[0] == 0)
6029
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6030
          else
6031
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6032
          tmp_len = tmp_len + 1;
6033
          // set TX BD number
6034
          tmp_bd_num = tmp_bd_num + 1;
6035
        end
6036
        // set wrap bit
6037
        set_tx_bd_wrap(6);
6038
      end
6039
      // 54 <= num_of_frames < 70 => wrap set to TX BD 7
6040
      else if ((num_of_frames == 54) || (num_of_frames == 62))
6041
      begin
6042
        tmp_len = i_length; // length of frame
6043
        tmp_bd_num = 0; // TX BD number
6044
        while (tmp_bd_num < 8) //
6045
        begin
6046
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6047
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6048
          if (tmp_len[0] == 0)
6049
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6050
          else
6051
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6052
          tmp_len = tmp_len + 1;
6053
          // set TX BD number
6054
          tmp_bd_num = tmp_bd_num + 1;
6055
        end
6056
        // set wrap bit
6057
        set_tx_bd_wrap(7);
6058
      end
6059
      #1;
6060
      // SET ready bit
6061
      if (num_of_frames < 10)
6062
        set_tx_bd_ready(0, 0);
6063
      else if (num_of_frames < 14)
6064
        set_tx_bd_ready((num_of_frames - 10), (num_of_frames - 10));
6065
      else if (num_of_frames < 18)
6066
        set_tx_bd_ready((num_of_frames - 14), (num_of_frames - 14));
6067
      else if (num_of_frames < 23)
6068
        set_tx_bd_ready((num_of_frames - 18), (num_of_frames - 18));
6069
      else if (num_of_frames < 28)
6070
        set_tx_bd_ready((num_of_frames - 23), (num_of_frames - 23));
6071
      else if (num_of_frames < 34)
6072
        set_tx_bd_ready((num_of_frames - 28), (num_of_frames - 28));
6073
      else if (num_of_frames < 40)
6074
        set_tx_bd_ready((num_of_frames - 34), (num_of_frames - 34));
6075
      else if (num_of_frames < 47)
6076
        set_tx_bd_ready((num_of_frames - 40), (num_of_frames - 40));
6077
      else if (num_of_frames < 54)
6078
        set_tx_bd_ready((num_of_frames - 47), (num_of_frames - 47));
6079
      else if (num_of_frames < 62)
6080
        set_tx_bd_ready((num_of_frames - 54), (num_of_frames - 54));
6081
      else if (num_of_frames < 70)
6082
        set_tx_bd_ready((num_of_frames - 62), (num_of_frames - 62));
6083
      // CHECK END OF TRANSMITION
6084
      frame_started = 0;
6085
      if (num_of_frames >= 5)
6086
        #1 check_tx_bd(num_of_bd, data);
6087
      fork
6088
      begin: fr_st
6089
        wait (MTxEn === 1'b1); // start transmit
6090
        frame_started = 1;
6091
      end
6092
      begin
6093
        repeat (30) @(posedge mtx_clk);
6094
        if (num_of_frames < 5)
6095
        begin
6096
          if (frame_started == 1)
6097
          begin
6098
            `TIME; $display("*E Frame should NOT start!");
6099
          end
6100
          disable fr_st;
6101
        end
6102
        else
6103
        begin
6104
          if (frame_started == 0)
6105
          begin
6106
            `TIME; $display("*W Frame should start!");
6107
            disable fr_st;
6108
          end
6109
        end
6110
      end
6111
      join
6112
      // check packets larger than 4 bytes
6113
      if (num_of_frames >= 5)
6114
      begin
6115
        wait (MTxEn === 1'b0); // end transmit
6116
        while (data[15] === 1)
6117
        begin
6118
          #1 check_tx_bd(num_of_bd, data);
6119
          @(posedge wb_clk);
6120
        end
6121
        repeat (1) @(posedge wb_clk);
6122
        // check length of a PACKET
6123
        if (i_length <= (min_tmp - 4))
6124
        begin
6125
          if (eth_phy.tx_len != min_tmp)
6126
          begin
6127
            test_fail("Wrong length of the packet out from MAC");
6128
            fail = fail + 1;
6129
          end
6130
        end
6131
        else
6132
        begin
6133
          if (eth_phy.tx_len != (i_length + 4))
6134
          begin
6135
            test_fail("Wrong length of the packet out from MAC");
6136
            fail = fail + 1;
6137
          end
6138
        end
6139
        // check transmitted TX packet data
6140
        if (i_length[0] == 0)
6141
        begin
6142
          #1 check_tx_packet(`MEMORY_BASE, (num_of_frames * 16), i_length, tmp);
6143
        end
6144
        else
6145
        begin
6146
          #1 check_tx_packet((`MEMORY_BASE + max_tmp), (num_of_frames * 16), i_length, tmp);
6147
        end
6148
        if (tmp > 0)
6149
        begin
6150
          test_fail("Wrong data of the transmitted packet");
6151
          fail = fail + 1;
6152
        end
6153
        // check transmited TX packet CRC
6154
        if (num_of_frames < (min_tmp - 4))
6155
          #1 check_tx_crc((num_of_frames * 16), (min_tmp - 4), 1'b0, tmp); // length without CRC
6156
        else
6157
          #1 check_tx_crc((num_of_frames * 16), i_length, 1'b0, tmp); // length without CRC
6158
        if (tmp > 0)
6159
        begin
6160
          test_fail("Wrong CRC of the transmitted packet");
6161
          fail = fail + 1;
6162
        end
6163
      end
6164
      // check WB INT signal
6165
      if ((i_length[1:0] == 2'h0) && (num_of_frames >= 5))
6166
      begin
6167
        if (wb_int !== 1'b1)
6168
        begin
6169
          `TIME; $display("*E WB INT signal should be set");
6170
          test_fail("WB INT signal should be set");
6171
          fail = fail + 1;
6172
        end
6173
      end
6174
      else
6175
      begin
6176
        if (wb_int !== 1'b0)
6177
        begin
6178
          `TIME; $display("*E WB INT signal should not be set");
6179
          test_fail("WB INT signal should not be set");
6180
          fail = fail + 1;
6181
        end
6182
      end
6183
      // check TX buffer descriptor of a packet
6184
      check_tx_bd(num_of_bd, data);
6185
      if (num_of_frames >= 5)
6186
      begin
6187
        if (i_length[1] == 1'b0) // interrupt enabled
6188
        begin
6189
          if ( (data[15:0] !== 16'h7800) && // wrap bit
6190
               (data[15:0] !== 16'h5800) ) // without wrap bit
6191
          begin
6192
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6193
            test_fail("TX buffer descriptor status is not correct");
6194
            fail = fail + 1;
6195
          end
6196
        end
6197
        else // interrupt not enabled
6198
        begin
6199
          if ( (data[15:0] !== 16'h3800) && // wrap bit
6200
               (data[15:0] !== 16'h1800) ) // without wrap bit
6201
          begin
6202
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6203
            test_fail("TX buffer descriptor status is not correct");
6204
            fail = fail + 1;
6205
          end
6206
        end
6207
      end
6208
      else
6209
      begin
6210
        if (data[15] !== 1'b1)
6211
        begin
6212
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6213
          test_fail("TX buffer descriptor status is not correct");
6214
          fail = fail + 1;
6215
        end
6216
      end
6217
      // clear TX BD with wrap bit
6218
      if (num_of_frames == 63)
6219
        clear_tx_bd(16, 16);
6220
      // check interrupts
6221
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6222
      if ( ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1)) && (num_of_frames >= 5) )
6223
      begin
6224
        if ((data & `ETH_INT_TXB) !== 1'b1)
6225
        begin
6226
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
6227
          test_fail("Interrupt Transmit Buffer was not set");
6228
          fail = fail + 1;
6229
        end
6230
        if ((data & (~`ETH_INT_TXB)) !== 0)
6231
        begin
6232
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
6233
          test_fail("Other interrupts (except Transmit Buffer) were set");
6234
          fail = fail + 1;
6235
        end
6236
      end
6237
      else
6238
      begin
6239
        if (data !== 0)
6240
        begin
6241
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
6242
          test_fail("Any of interrupts (except Transmit Buffer) was set");
6243
          fail = fail + 1;
6244
        end
6245
      end
6246
      // clear interrupts
6247
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6248
      // check WB INT signal
6249
      if (wb_int !== 1'b0)
6250
      begin
6251
        test_fail("WB INT signal should not be set");
6252
        fail = fail + 1;
6253
      end
6254
      // INTERMEDIATE DISPLAYS
6255
      if (i_length == 3)
6256
      begin
6257
        $display("    pads appending to packets is selected");
6258
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
6259
        $display("    ->packets with lengths from %0d to %0d are not transmitted (length increasing by 1 byte)",
6260
                 0, 3);
6261
      end
6262
      else if (i_length == 9)
6263
      begin
6264
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
6265
        $display("    ->packet with length 4 is not transmitted (length increasing by 1 byte)");
6266
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6267
                 5, 9);
6268
      end
6269
      else if (i_length == 17)
6270
      begin
6271
        $display("    using 4 BDs out of 8 BDs assigned to TX (wrap at 4th BD - TX BD 3)");
6272
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6273
                 10, 17);
6274
      end
6275
      else if (i_length == 27)
6276
      begin
6277
        $display("    using 5 BDs out of 8 BDs assigned to TX (wrap at 5th BD - TX BD 4)");
6278
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6279
                 18, 27);
6280
      end
6281
      else if (i_length == 40)
6282
      begin
6283
        $display("    using 6 BDs out of 8 BDs assigned to TX (wrap at 6th BD - TX BD 5)");
6284
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6285
                 28, 40);
6286
      end
6287
      else if (i_length == 54)
6288
      begin
6289
        $display("    using 7 BDs out of 8 BDs assigned to TX (wrap at 7th BD - TX BD 6)");
6290
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6291
                 41, 54);
6292
      end
6293
      else if (i_length == 69)
6294
      begin
6295
        $display("    using 8 BDs out of 8 BDs assigned to TX (wrap at 8th BD - TX BD 7)");
6296
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6297
                 55, 69);
6298
      end
6299
      // set length (loop variable)
6300
      i_length = i_length + 1;
6301
      // the number of frame transmitted
6302
      num_of_frames = num_of_frames + 1;
6303
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
6304
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
6305
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
6306
          (num_of_frames == 54) || (num_of_frames == 62))
6307
        num_of_bd = 0;
6308
      else
6309
        num_of_bd = num_of_bd + 1;
6310
    end
6311
    // disable TX
6312
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
6313
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6314
    @(posedge wb_clk);
6315
    if(fail == 0)
6316
      test_ok;
6317
    else
6318
      fail = 0;
6319
  end
6320
 
6321
 
6322
  ////////////////////////////////////////////////////////////////////
6323
  ////                                                            ////
6324
  ////  Test transmit packets form 0 to (MINFL - 1) sizes at      ////
6325
  ////  8 TX buffer decriptors ( 100Mbps ).                       ////
6326
  ////                                                            ////
6327
  ////////////////////////////////////////////////////////////////////
6328
  if (test_num == 7) // 
6329
  begin
6330
    // TEST 7: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )
6331
    test_name = "TEST 7: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )";
6332
    `TIME; $display("  TEST 7: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )");
6333
 
6334
    // reset MAC registers
6335
    hard_reset;
6336
    // reset MAC and MII LOGIC with soft reset
6337
    reset_mac;
6338
    reset_mii;
6339
    // set wb slave response
6340
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
6341
 
6342
    max_tmp = 0;
6343
    min_tmp = 0;
6344
    // set 8 TX buffer descriptors - must be set before TX enable
6345
    wbm_write(`ETH_TX_BD_NUM, 32'h8, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6346
    // enable TX, set full-duplex mode, padding and CRC appending
6347
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
6348
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6349
    // prepare two packets of MAXFL length
6350
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6351
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
6352
    min_tmp = tmp[31:16];
6353
    st_data = 8'h12;
6354
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
6355
    st_data = 8'h34;
6356
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp - 4), st_data); // length without CRC
6357
    // check WB INT signal
6358
    if (wb_int !== 1'b0)
6359
    begin
6360
      test_fail("WB INT signal should not be set");
6361
      fail = fail + 1;
6362
    end
6363
 
6364
    // write to phy's control register for 100Mbps
6365
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
6366
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
6367
    speed = 100;
6368
 
6369
    frame_started = 0;
6370
    num_of_frames = 0;
6371
    num_of_bd = 0;
6372
    i_length = 0; // 0;
6373
    while (i_length < 70) // (min_tmp - 4))
6374
    begin
6375
      #1;
6376
      // choose generating carrier sense and collision
6377
      case (i_length[1:0])
6378
      2'h0: // Interrupt is generated
6379
      begin
6380
        // Reset_tx_bd nable interrupt generation
6381
        // unmask interrupts
6382
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
6383
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6384
        // not detect carrier sense in FD and no collision
6385
        eth_phy.carrier_sense_tx_fd_detect(0);
6386
        eth_phy.collision(0);
6387
      end
6388
      2'h1: // Interrupt is not generated
6389
      begin
6390
        // set_tx_bd enable interrupt generation
6391
        // mask interrupts
6392
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6393
        // detect carrier sense in FD and no collision
6394
        eth_phy.carrier_sense_tx_fd_detect(1);
6395
        eth_phy.collision(0);
6396
      end
6397
      2'h2: // Interrupt is not generated
6398
      begin
6399
        // set_tx_bd disable the interrupt generation
6400
        // unmask interrupts
6401
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
6402
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6403
        // not detect carrier sense in FD and set collision
6404
        eth_phy.carrier_sense_tx_fd_detect(0);
6405
        eth_phy.collision(1);
6406
      end
6407
      default: // 2'h3: // Interrupt is not generated
6408
      begin
6409
        // set_tx_bd disable the interrupt generation
6410
        // mask interrupts
6411
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6412
        // detect carrier sense in FD and set collision
6413
        eth_phy.carrier_sense_tx_fd_detect(1);
6414
        eth_phy.collision(1);
6415
      end
6416
      endcase
6417
      #1;
6418
      // first destination address on ethernet PHY
6419
      eth_phy.set_tx_mem_addr(num_of_frames * 16);
6420
      // SET packets and wrap bit
6421
      // num_of_frames <= 9 => wrap set to TX BD 0
6422
      if (num_of_frames <= 9)
6423
      begin
6424
        tmp_len = i_length; // length of frame
6425
        tmp_bd_num = 0; // TX BD number
6426
        // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6427
        // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6428
        if (tmp_len[0] == 0)
6429
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6430
        else
6431
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6432
        // set wrap bit
6433
        set_tx_bd_wrap(0);
6434
      end
6435
      // 10 <= num_of_frames < 18 => wrap set to TX BD 3
6436
      else if ((num_of_frames == 10) || (num_of_frames == 14))
6437
      begin
6438
        tmp_len = i_length; // length of frame
6439
        tmp_bd_num = 0; // TX BD number
6440
        while (tmp_bd_num < 4) //
6441
        begin
6442
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6443
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6444
          if (tmp_len[0] == 0)
6445
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6446
          else
6447
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6448
          tmp_len = tmp_len + 1;
6449
          // set TX BD number
6450
          tmp_bd_num = tmp_bd_num + 1;
6451
        end
6452
        // set wrap bit
6453
        set_tx_bd_wrap(3);
6454
      end
6455
      // 18 <= num_of_frames < 28 => wrap set to TX BD 4
6456
      else if ((num_of_frames == 18) || (num_of_frames == 23))
6457
      begin
6458
        tmp_len = i_length; // length of frame
6459
        tmp_bd_num = 0; // TX BD number
6460
        while (tmp_bd_num < 5) //
6461
        begin
6462
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6463
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6464
          if (tmp_len[0] == 0)
6465
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6466
          else
6467
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6468
          tmp_len = tmp_len + 1;
6469
          // set TX BD number
6470
          tmp_bd_num = tmp_bd_num + 1;
6471
        end
6472
        // set wrap bit
6473
        set_tx_bd_wrap(4);
6474
      end
6475
      // 28 <= num_of_frames < 40 => wrap set to TX BD 5
6476
      else if ((num_of_frames == 28) || (num_of_frames == 34))
6477
      begin
6478
        tmp_len = i_length; // length of frame
6479
        tmp_bd_num = 0; // TX BD number
6480
        while (tmp_bd_num < 6) //
6481
        begin
6482
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6483
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6484
          if (tmp_len[0] == 0)
6485
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6486
          else
6487
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6488
          tmp_len = tmp_len + 1;
6489
          // set TX BD number
6490
          tmp_bd_num = tmp_bd_num + 1;
6491
        end
6492
        // set wrap bit
6493
        set_tx_bd_wrap(5);
6494
      end
6495
      // 40 <= num_of_frames < 54 => wrap set to TX BD 6
6496
      else if ((num_of_frames == 40) || (num_of_frames == 47))
6497
      begin
6498
        tmp_len = i_length; // length of frame
6499
        tmp_bd_num = 0; // TX BD number
6500
        while (tmp_bd_num < 7) //
6501
        begin
6502
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6503
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6504
          if (tmp_len[0] == 0)
6505
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6506
          else
6507
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6508
          tmp_len = tmp_len + 1;
6509
          // set TX BD number
6510
          tmp_bd_num = tmp_bd_num + 1;
6511
        end
6512
        // set wrap bit
6513
        set_tx_bd_wrap(6);
6514
      end
6515
      // 54 <= num_of_frames < 70 => wrap set to TX BD 7
6516
      else if ((num_of_frames == 54) || (num_of_frames == 62))
6517
      begin
6518
        tmp_len = i_length; // length of frame
6519
        tmp_bd_num = 0; // TX BD number
6520
        while (tmp_bd_num < 8) //
6521
        begin
6522
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6523
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6524
          if (tmp_len[0] == 0)
6525
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6526
          else
6527
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6528
          tmp_len = tmp_len + 1;
6529
          // set TX BD number
6530
          tmp_bd_num = tmp_bd_num + 1;
6531
        end
6532
        // set wrap bit
6533
        set_tx_bd_wrap(7);
6534
      end
6535
      #1;
6536
      // SET ready bit
6537
      if (num_of_frames < 10)
6538
        set_tx_bd_ready(0, 0);
6539
      else if (num_of_frames < 14)
6540
        set_tx_bd_ready((num_of_frames - 10), (num_of_frames - 10));
6541
      else if (num_of_frames < 18)
6542
        set_tx_bd_ready((num_of_frames - 14), (num_of_frames - 14));
6543
      else if (num_of_frames < 23)
6544
        set_tx_bd_ready((num_of_frames - 18), (num_of_frames - 18));
6545
      else if (num_of_frames < 28)
6546
        set_tx_bd_ready((num_of_frames - 23), (num_of_frames - 23));
6547
      else if (num_of_frames < 34)
6548
        set_tx_bd_ready((num_of_frames - 28), (num_of_frames - 28));
6549
      else if (num_of_frames < 40)
6550
        set_tx_bd_ready((num_of_frames - 34), (num_of_frames - 34));
6551
      else if (num_of_frames < 47)
6552
        set_tx_bd_ready((num_of_frames - 40), (num_of_frames - 40));
6553
      else if (num_of_frames < 54)
6554
        set_tx_bd_ready((num_of_frames - 47), (num_of_frames - 47));
6555
      else if (num_of_frames < 62)
6556
        set_tx_bd_ready((num_of_frames - 54), (num_of_frames - 54));
6557
      else if (num_of_frames < 70)
6558
        set_tx_bd_ready((num_of_frames - 62), (num_of_frames - 62));
6559
      // CHECK END OF TRANSMITION
6560
      frame_started = 0;
6561
      if (num_of_frames >= 5)
6562
        #1 check_tx_bd(num_of_bd, data);
6563
      fork
6564
      begin: fr_st1
6565
        wait (MTxEn === 1'b1); // start transmit
6566
        frame_started = 1;
6567
      end
6568
      begin
6569
        repeat (30) @(posedge mtx_clk);
6570
        if (num_of_frames < 5)
6571
        begin
6572
          if (frame_started == 1)
6573
          begin
6574
            `TIME; $display("*E Frame should NOT start!");
6575
          end
6576
          disable fr_st1;
6577
        end
6578
        else
6579
        begin
6580
          if (frame_started == 0)
6581
          begin
6582
            `TIME; $display("*W Frame should start!");
6583
            disable fr_st1;
6584
          end
6585
        end
6586
      end
6587
      join
6588
      // check packets larger than 4 bytes
6589
      if (num_of_frames >= 5)
6590
      begin
6591
        wait (MTxEn === 1'b0); // end transmit
6592
        while (data[15] === 1)
6593
        begin
6594
          #1 check_tx_bd(num_of_bd, data);
6595
          @(posedge wb_clk);
6596
        end
6597
        repeat (1) @(posedge wb_clk);
6598
        // check length of a PACKET
6599
        if (i_length <= (min_tmp - 4))
6600
        begin
6601
          if (eth_phy.tx_len != min_tmp)
6602
          begin
6603
            test_fail("Wrong length of the packet out from MAC");
6604
            fail = fail + 1;
6605
          end
6606
        end
6607
        else
6608
        begin
6609
          if (eth_phy.tx_len != (i_length + 4))
6610
          begin
6611
            test_fail("Wrong length of the packet out from MAC");
6612
            fail = fail + 1;
6613
          end
6614
        end
6615
        // check transmitted TX packet data
6616
        if (i_length[0] == 0)
6617
        begin
6618
          #1 check_tx_packet(`MEMORY_BASE, (num_of_frames * 16), i_length, tmp);
6619
        end
6620
        else
6621
        begin
6622
          #1 check_tx_packet((`MEMORY_BASE + max_tmp), (num_of_frames * 16), i_length, tmp);
6623
        end
6624
        if (tmp > 0)
6625
        begin
6626
          test_fail("Wrong data of the transmitted packet");
6627
          fail = fail + 1;
6628
        end
6629
        // check transmited TX packet CRC
6630
        if (num_of_frames < (min_tmp - 4))
6631
          #1 check_tx_crc((num_of_frames * 16), (min_tmp - 4), 1'b0, tmp); // length without CRC
6632
        else
6633
          #1 check_tx_crc((num_of_frames * 16), i_length, 1'b0, tmp); // length without CRC
6634
        if (tmp > 0)
6635
        begin
6636
          test_fail("Wrong CRC of the transmitted packet");
6637
          fail = fail + 1;
6638
        end
6639
      end
6640
      // check WB INT signal
6641
      if ((i_length[1:0] == 2'h0) && (num_of_frames >= 5))
6642
      begin
6643
        if (wb_int !== 1'b1)
6644
        begin
6645
          `TIME; $display("*E WB INT signal should be set");
6646
          test_fail("WB INT signal should be set");
6647
          fail = fail + 1;
6648
        end
6649
      end
6650
      else
6651
      begin
6652
        if (wb_int !== 1'b0)
6653
        begin
6654
          `TIME; $display("*E WB INT signal should not be set");
6655
          test_fail("WB INT signal should not be set");
6656
          fail = fail + 1;
6657
        end
6658
      end
6659
      // check TX buffer descriptor of a packet
6660
      check_tx_bd(num_of_bd, data);
6661
      if (num_of_frames >= 5)
6662
      begin
6663
        if (i_length[1] == 1'b0) // interrupt enabled
6664
        begin
6665
          if ( (data[15:0] !== 16'h7800) && // wrap bit
6666
               (data[15:0] !== 16'h5800) ) // without wrap bit
6667
          begin
6668
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6669
            test_fail("TX buffer descriptor status is not correct");
6670
            fail = fail + 1;
6671
          end
6672
        end
6673
        else // interrupt not enabled
6674
        begin
6675
          if ( (data[15:0] !== 16'h3800) && // wrap bit
6676
               (data[15:0] !== 16'h1800) ) // without wrap bit
6677
          begin
6678
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6679
            test_fail("TX buffer descriptor status is not correct");
6680
            fail = fail + 1;
6681
          end
6682
        end
6683
      end
6684
      else
6685
      begin
6686
        if (data[15] !== 1'b1)
6687
        begin
6688
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6689
          test_fail("TX buffer descriptor status is not correct");
6690
          fail = fail + 1;
6691
        end
6692
      end
6693
      // clear TX BD with wrap bit
6694
      if (num_of_frames == 63)
6695
        clear_tx_bd(16, 16);
6696
      // check interrupts
6697
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6698
      if ( ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1)) && (num_of_frames >= 5) )
6699
      begin
6700
        if ((data & `ETH_INT_TXB) !== 1'b1)
6701
        begin
6702
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
6703
          test_fail("Interrupt Transmit Buffer was not set");
6704
          fail = fail + 1;
6705
        end
6706
        if ((data & (~`ETH_INT_TXB)) !== 0)
6707
        begin
6708
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
6709
          test_fail("Other interrupts (except Transmit Buffer) were set");
6710
          fail = fail + 1;
6711
        end
6712
      end
6713
      else
6714
      begin
6715
        if (data !== 0)
6716
        begin
6717
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
6718
          test_fail("Any of interrupts (except Transmit Buffer) was set");
6719
          fail = fail + 1;
6720
        end
6721
      end
6722
      // clear interrupts
6723
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6724
      // check WB INT signal
6725
      if (wb_int !== 1'b0)
6726
      begin
6727
        test_fail("WB INT signal should not be set");
6728
        fail = fail + 1;
6729
      end
6730
      // INTERMEDIATE DISPLAYS
6731
      if (i_length == 3)
6732
      begin
6733
        $display("    pads appending to packets is selected");
6734
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
6735
        $display("    ->packets with lengths from %0d to %0d are not transmitted (length increasing by 1 byte)",
6736
                 0, 3);
6737
      end
6738
      else if (i_length == 9)
6739
      begin
6740
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
6741
        $display("    ->packet with length 4 is not transmitted (length increasing by 1 byte)");
6742
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6743
                 5, 9);
6744
      end
6745
      else if (i_length == 17)
6746
      begin
6747
        $display("    using 4 BDs out of 8 BDs assigned to TX (wrap at 4th BD - TX BD 3)");
6748
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6749
                 10, 17);
6750
      end
6751
      else if (i_length == 27)
6752
      begin
6753
        $display("    using 5 BDs out of 8 BDs assigned to TX (wrap at 5th BD - TX BD 4)");
6754
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6755
                 18, 27);
6756
      end
6757
      else if (i_length == 40)
6758
      begin
6759
        $display("    using 6 BDs out of 8 BDs assigned to TX (wrap at 6th BD - TX BD 5)");
6760
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6761
                 28, 40);
6762
      end
6763
      else if (i_length == 54)
6764
      begin
6765
        $display("    using 7 BDs out of 8 BDs assigned to TX (wrap at 7th BD - TX BD 6)");
6766
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6767
                 41, 54);
6768
      end
6769
      else if (i_length == 69)
6770
      begin
6771
        $display("    using 8 BDs out of 8 BDs assigned to TX (wrap at 8th BD - TX BD 7)");
6772
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6773
                 55, 69);
6774
      end
6775
      // set length (loop variable)
6776
      i_length = i_length + 1;
6777
      // the number of frame transmitted
6778
      num_of_frames = num_of_frames + 1;
6779
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
6780
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
6781
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
6782
          (num_of_frames == 54) || (num_of_frames == 62))
6783
        num_of_bd = 0;
6784
      else
6785
        num_of_bd = num_of_bd + 1;
6786
    end
6787
    // disable TX
6788
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
6789
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6790
    @(posedge wb_clk);
6791
    if(fail == 0)
6792
      test_ok;
6793
    else
6794
      fail = 0;
6795
  end
6796
 
6797
 
6798
  ////////////////////////////////////////////////////////////////////
6799
  ////                                                            ////
6800 243 tadejm
  ////  Test transmit packets (no pads) from 0 to (MINFL - 1)     ////
6801 209 tadejm
  ////  sizes at 8 TX buffer decriptors ( 10Mbps ).               ////
6802
  ////                                                            ////
6803
  ////////////////////////////////////////////////////////////////////
6804
  if (test_num == 8) // 
6805
  begin
6806 243 tadejm
    // TEST 8: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )
6807
    test_name = "TEST 8: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )";
6808
    `TIME; $display("  TEST 8: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )");
6809 209 tadejm
 
6810
    // reset MAC registers
6811
    hard_reset;
6812
    // reset MAC and MII LOGIC with soft reset
6813
    reset_mac;
6814
    reset_mii;
6815
    // set wb slave response
6816
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
6817
 
6818
    max_tmp = 0;
6819
    min_tmp = 0;
6820
    // set 8 TX buffer descriptors - must be set before TX enable
6821
    wbm_write(`ETH_TX_BD_NUM, 32'h8, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6822
    // enable TX, set full-duplex mode, padding and CRC appending
6823 243 tadejm
//    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
6824
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD,
6825 209 tadejm
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6826
    // prepare two packets of MAXFL length
6827
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6828
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
6829
    min_tmp = tmp[31:16];
6830
    st_data = 8'h12;
6831
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
6832
    st_data = 8'h34;
6833
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp - 4), st_data); // length without CRC
6834
    // check WB INT signal
6835
    if (wb_int !== 1'b0)
6836
    begin
6837
      test_fail("WB INT signal should not be set");
6838
      fail = fail + 1;
6839
    end
6840
 
6841
    // write to phy's control register for 10Mbps
6842
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
6843
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
6844
    speed = 10;
6845
 
6846
    frame_started = 0;
6847
    num_of_frames = 0;
6848
    num_of_bd = 0;
6849
    i_length = 0; // 0;
6850
    while (i_length < 70) // (min_tmp - 4))
6851
    begin
6852
      #1;
6853
      // choose generating carrier sense and collision
6854
      case (i_length[1:0])
6855
      2'h0: // Interrupt is generated
6856
      begin
6857
        // Reset_tx_bd nable interrupt generation
6858
        // unmask interrupts
6859
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
6860
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6861
        // not detect carrier sense in FD and no collision
6862
        eth_phy.carrier_sense_tx_fd_detect(0);
6863
        eth_phy.collision(0);
6864
      end
6865
      2'h1: // Interrupt is not generated
6866
      begin
6867
        // set_tx_bd enable interrupt generation
6868
        // mask interrupts
6869
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6870
        // detect carrier sense in FD and no collision
6871
        eth_phy.carrier_sense_tx_fd_detect(1);
6872
        eth_phy.collision(0);
6873
      end
6874
      2'h2: // Interrupt is not generated
6875
      begin
6876
        // set_tx_bd disable the interrupt generation
6877
        // unmask interrupts
6878
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
6879
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6880
        // not detect carrier sense in FD and set collision
6881
        eth_phy.carrier_sense_tx_fd_detect(0);
6882
        eth_phy.collision(1);
6883
      end
6884
      default: // 2'h3: // Interrupt is not generated
6885
      begin
6886
        // set_tx_bd disable the interrupt generation
6887
        // mask interrupts
6888
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6889
        // detect carrier sense in FD and set collision
6890
        eth_phy.carrier_sense_tx_fd_detect(1);
6891
        eth_phy.collision(1);
6892
      end
6893
      endcase
6894 243 tadejm
//      // append CRC
6895
//      if ((i_length[0] == 1'b0) && (num_of_frames >= 6))
6896
//      begin
6897
//        append_tx_crc(`MEMORY_BASE, i_length, 1'b0);
6898
//      end
6899 209 tadejm
      #1;
6900
      // first destination address on ethernet PHY
6901
      eth_phy.set_tx_mem_addr(num_of_frames * 16);
6902
      // SET packets and wrap bit
6903
      // num_of_frames <= 9 => wrap set to TX BD 0
6904 243 tadejm
      if (num_of_frames <= 5)
6905
        begin
6906
          tmp_len = i_length; // length of frame
6907
          tmp_bd_num = 0; // TX BD number
6908
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6909
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6910
          if (tmp_len[0] == 0) // CRC appended by 'HARDWARE'
6911
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, `MEMORY_BASE);
6912
          else
6913
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
6914
          // set wrap bit
6915
          set_tx_bd_wrap(0);
6916
        end
6917
        else if (num_of_frames <= 9)
6918
        begin
6919
          tmp_len = i_length; // length of frame
6920
          tmp_bd_num = 0; // TX BD number
6921
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6922
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6923
          if (tmp_len[0] == 0) // CRC appended by 'SOFTWARE'
6924
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
6925
          else
6926
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
6927
          // set wrap bit
6928
          set_tx_bd_wrap(0);
6929 209 tadejm
      end
6930
      // 10 <= num_of_frames < 18 => wrap set to TX BD 3
6931
      else if ((num_of_frames == 10) || (num_of_frames == 14))
6932
      begin
6933
        tmp_len = i_length; // length of frame
6934
        tmp_bd_num = 0; // TX BD number
6935
        while (tmp_bd_num < 4) //
6936
        begin
6937
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6938
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6939
          if (tmp_len[0] == 0)
6940 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
6941 209 tadejm
          else
6942 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
6943 209 tadejm
          tmp_len = tmp_len + 1;
6944
          // set TX BD number
6945
          tmp_bd_num = tmp_bd_num + 1;
6946
        end
6947
        // set wrap bit
6948
        set_tx_bd_wrap(3);
6949
      end
6950
      // 18 <= num_of_frames < 28 => wrap set to TX BD 4
6951
      else if ((num_of_frames == 18) || (num_of_frames == 23))
6952
      begin
6953
        tmp_len = i_length; // length of frame
6954
        tmp_bd_num = 0; // TX BD number
6955
        while (tmp_bd_num < 5) //
6956
        begin
6957
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6958
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6959
          if (tmp_len[0] == 0)
6960 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
6961
          else // when (num_of_frames == 23), (i_length == 23) and therefor i_length[0] == 1 !!!
6962
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1],
6963
                      ((num_of_frames == 23) && (tmp_bd_num == 0)), 1'b1, (`MEMORY_BASE + max_tmp));
6964 209 tadejm
          tmp_len = tmp_len + 1;
6965
          // set TX BD number
6966
          tmp_bd_num = tmp_bd_num + 1;
6967
        end
6968
        // set wrap bit
6969
        set_tx_bd_wrap(4);
6970
      end
6971
      // 28 <= num_of_frames < 40 => wrap set to TX BD 5
6972
      else if ((num_of_frames == 28) || (num_of_frames == 34))
6973
      begin
6974
        tmp_len = i_length; // length of frame
6975
        tmp_bd_num = 0; // TX BD number
6976
        while (tmp_bd_num < 6) //
6977
        begin
6978
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6979
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6980
          if (tmp_len[0] == 0)
6981 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
6982 209 tadejm
          else
6983 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
6984 209 tadejm
          tmp_len = tmp_len + 1;
6985
          // set TX BD number
6986
          tmp_bd_num = tmp_bd_num + 1;
6987
        end
6988
        // set wrap bit
6989
        set_tx_bd_wrap(5);
6990
      end
6991
      // 40 <= num_of_frames < 54 => wrap set to TX BD 6
6992
      else if ((num_of_frames == 40) || (num_of_frames == 47))
6993
      begin
6994
        tmp_len = i_length; // length of frame
6995
        tmp_bd_num = 0; // TX BD number
6996
        while (tmp_bd_num < 7) //
6997
        begin
6998
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6999
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7000
          if (tmp_len[0] == 0)
7001 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
7002 209 tadejm
          else
7003 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
7004 209 tadejm
          tmp_len = tmp_len + 1;
7005
          // set TX BD number
7006
          tmp_bd_num = tmp_bd_num + 1;
7007
        end
7008
        // set wrap bit
7009
        set_tx_bd_wrap(6);
7010
      end
7011
      // 54 <= num_of_frames < 70 => wrap set to TX BD 7
7012
      else if ((num_of_frames == 54) || (num_of_frames == 62))
7013
      begin
7014
        tmp_len = i_length; // length of frame
7015
        tmp_bd_num = 0; // TX BD number
7016
        while (tmp_bd_num < 8) //
7017
        begin
7018
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7019
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7020
          if (tmp_len[0] == 0)
7021 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
7022 209 tadejm
          else
7023 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
7024 209 tadejm
          tmp_len = tmp_len + 1;
7025
          // set TX BD number
7026
          tmp_bd_num = tmp_bd_num + 1;
7027
        end
7028
        // set wrap bit
7029
        set_tx_bd_wrap(7);
7030
      end
7031
      #1;
7032
      // SET ready bit
7033
      if (num_of_frames < 10)
7034
        set_tx_bd_ready(0, 0);
7035
      else if (num_of_frames < 14)
7036
        set_tx_bd_ready((num_of_frames - 10), (num_of_frames - 10));
7037
      else if (num_of_frames < 18)
7038
        set_tx_bd_ready((num_of_frames - 14), (num_of_frames - 14));
7039
      else if (num_of_frames < 23)
7040
        set_tx_bd_ready((num_of_frames - 18), (num_of_frames - 18));
7041
      else if (num_of_frames < 28)
7042
        set_tx_bd_ready((num_of_frames - 23), (num_of_frames - 23));
7043
      else if (num_of_frames < 34)
7044
        set_tx_bd_ready((num_of_frames - 28), (num_of_frames - 28));
7045
      else if (num_of_frames < 40)
7046
        set_tx_bd_ready((num_of_frames - 34), (num_of_frames - 34));
7047
      else if (num_of_frames < 47)
7048
        set_tx_bd_ready((num_of_frames - 40), (num_of_frames - 40));
7049
      else if (num_of_frames < 54)
7050
        set_tx_bd_ready((num_of_frames - 47), (num_of_frames - 47));
7051
      else if (num_of_frames < 62)
7052
        set_tx_bd_ready((num_of_frames - 54), (num_of_frames - 54));
7053
      else if (num_of_frames < 70)
7054
        set_tx_bd_ready((num_of_frames - 62), (num_of_frames - 62));
7055
      // CHECK END OF TRANSMITION
7056
      frame_started = 0;
7057
      if (num_of_frames >= 5)
7058
        #1 check_tx_bd(num_of_bd, data);
7059
      fork
7060
      begin: fr_st2
7061
        wait (MTxEn === 1'b1); // start transmit
7062
        frame_started = 1;
7063
      end
7064
      begin
7065
        repeat (30) @(posedge mtx_clk);
7066
        if (num_of_frames < 5)
7067
        begin
7068
          if (frame_started == 1)
7069
          begin
7070
            `TIME; $display("*E Frame should NOT start!");
7071
          end
7072
          disable fr_st2;
7073
        end
7074
        else
7075
        begin
7076
          if (frame_started == 0)
7077
          begin
7078
            `TIME; $display("*W Frame should start!");
7079
            disable fr_st2;
7080
          end
7081
        end
7082
      end
7083
      join
7084
      // check packets larger than 4 bytes
7085
      if (num_of_frames >= 5)
7086
      begin
7087
        wait (MTxEn === 1'b0); // end transmit
7088
        while (data[15] === 1)
7089
        begin
7090
          #1 check_tx_bd(num_of_bd, data);
7091
          @(posedge wb_clk);
7092
        end
7093
        repeat (1) @(posedge wb_clk);
7094 243 tadejm
        // check length of a PACKET 
7095
        if ((eth_phy.tx_len != i_length) && (i_length[0] == 1'b0) && (num_of_frames >= 6))
7096 209 tadejm
        begin
7097
          `TIME; $display("*E Wrong length of the packet out from MAC");
7098
          test_fail("Wrong length of the packet out from MAC");
7099
          fail = fail + 1;
7100
        end
7101 243 tadejm
        else if ((eth_phy.tx_len != (i_length + 4)) && (num_of_frames != 23))
7102
        begin
7103
          `TIME; $display("*E Wrong length of the packet out from MAC");
7104
          test_fail("Wrong length of the packet out from MAC");
7105
          fail = fail + 1;
7106
        end
7107
        else if ((eth_phy.tx_len != (min_tmp)) && (num_of_frames == 23))
7108
        begin
7109
          `TIME; $display("*E Wrong length of the packet out from MAC");
7110
          test_fail("Wrong length of the packet out from MAC");
7111
          fail = fail + 1;
7112
        end
7113 209 tadejm
        // check transmitted TX packet data
7114
        if (i_length[0] == 0)
7115
        begin
7116
          #1 check_tx_packet(`MEMORY_BASE, (num_of_frames * 16), i_length, tmp);
7117
        end
7118 243 tadejm
        else if (num_of_frames == 23) // i_length[0] == 1 here
7119
        begin
7120
          #1 check_tx_packet((`MEMORY_BASE + max_tmp), (num_of_frames * 16), (min_tmp - 4), tmp);
7121
        end
7122 209 tadejm
        else
7123
        begin
7124
          #1 check_tx_packet((`MEMORY_BASE + max_tmp), (num_of_frames * 16), i_length, tmp);
7125
        end
7126
        if (tmp > 0)
7127
        begin
7128
          test_fail("Wrong data of the transmitted packet");
7129
          fail = fail + 1;
7130
        end
7131
        // check transmited TX packet CRC
7132 243 tadejm
        #1;
7133
        if ((i_length[0] == 1'b0) && (num_of_frames >= 6))
7134
        begin
7135
        end
7136
        else
7137
          check_tx_crc((num_of_frames * 16), (eth_phy.tx_len - 4), 1'b0, tmp); // length without CRC
7138 209 tadejm
        if (tmp > 0)
7139
        begin
7140
          test_fail("Wrong CRC of the transmitted packet");
7141
          fail = fail + 1;
7142
        end
7143
      end
7144
      // check WB INT signal
7145
      if ((i_length[1:0] == 2'h0) && (num_of_frames >= 5))
7146
      begin
7147
        if (wb_int !== 1'b1)
7148
        begin
7149
          `TIME; $display("*E WB INT signal should be set");
7150
          test_fail("WB INT signal should be set");
7151
          fail = fail + 1;
7152
        end
7153
      end
7154
      else
7155
      begin
7156
        if (wb_int !== 1'b0)
7157
        begin
7158
          `TIME; $display("*E WB INT signal should not be set");
7159
          test_fail("WB INT signal should not be set");
7160
          fail = fail + 1;
7161
        end
7162
      end
7163
      // check TX buffer descriptor of a packet
7164
      check_tx_bd(num_of_bd, data);
7165
      if (num_of_frames >= 5)
7166
      begin
7167 243 tadejm
        if ((i_length[1] == 1'b0) && (i_length[0] == 1'b0)) // interrupt enabled
7168 209 tadejm
        begin
7169 243 tadejm
          if ( (data[15:0] !== 16'h6000) &&  // wrap bit
7170
               (data[15:0] !== 16'h4000) )  // without wrap bit
7171 209 tadejm
          begin
7172
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7173
            test_fail("TX buffer descriptor status is not correct");
7174
            fail = fail + 1;
7175
          end
7176
        end
7177 243 tadejm
        else if ((i_length[1] == 1'b1) && (i_length[0] == 1'b0)) // interrupt not enabled
7178 209 tadejm
        begin
7179 243 tadejm
          if ( (data[15:0] !== 16'h2000) && // wrap bit
7180
               (data[15:0] !== 16'h0000) ) // without wrap bit
7181
          begin
7182
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7183
            test_fail("TX buffer descriptor status is not correct");
7184
            fail = fail + 1;
7185
          end
7186
        end
7187
        else if ((i_length[1] == 1'b0) && (i_length[0] == 1'b1)) // interrupt enabled
7188
        begin
7189
          if ( (data[15:0] !== 16'h6800) && // wrap bit
7190
               (data[15:0] !== 16'h4800) ) // without wrap bit
7191
          begin
7192
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7193
            test_fail("TX buffer descriptor status is not correct");
7194
            fail = fail + 1;
7195
          end
7196
        end
7197
        else if (num_of_frames != 23) // ((i_length[1] == 1'b1) && (i_length[0] == 1'b1)) // interrupt not enabled
7198
        begin
7199
          if ( (data[15:0] !== 16'h2800) && // wrap bit
7200
               (data[15:0] !== 16'h0800) ) // without wrap bit
7201
          begin
7202
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7203
            test_fail("TX buffer descriptor status is not correct");
7204
            fail = fail + 1;
7205
          end
7206
        end
7207
        else // ((num_of_frames != 23) && (i_length[1] == 1'b1) && (i_length[0] == 1'b1)) // interrupt not enabled
7208
        begin
7209 209 tadejm
          if ( (data[15:0] !== 16'h3800) && // wrap bit
7210
               (data[15:0] !== 16'h1800) ) // without wrap bit
7211
          begin
7212
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7213
            test_fail("TX buffer descriptor status is not correct");
7214
            fail = fail + 1;
7215
          end
7216
        end
7217
      end
7218
      else
7219
      begin
7220
        if (data[15] !== 1'b1)
7221
        begin
7222
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7223
          test_fail("TX buffer descriptor status is not correct");
7224
          fail = fail + 1;
7225
        end
7226
      end
7227
      // clear TX BD with wrap bit
7228
      if (num_of_frames == 63)
7229
        clear_tx_bd(16, 16);
7230
      // check interrupts
7231
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7232
      if ( ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1)) && (num_of_frames >= 5) )
7233
      begin
7234
        if ((data & `ETH_INT_TXB) !== 1'b1)
7235
        begin
7236
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
7237
          test_fail("Interrupt Transmit Buffer was not set");
7238
          fail = fail + 1;
7239
        end
7240
        if ((data & (~`ETH_INT_TXB)) !== 0)
7241
        begin
7242
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
7243
          test_fail("Other interrupts (except Transmit Buffer) were set");
7244
          fail = fail + 1;
7245
        end
7246
      end
7247
      else
7248
      begin
7249
        if (data !== 0)
7250
        begin
7251
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
7252
          test_fail("Any of interrupts (except Transmit Buffer) was set");
7253
          fail = fail + 1;
7254
        end
7255
      end
7256
      // clear interrupts
7257
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7258
      // check WB INT signal
7259
      if (wb_int !== 1'b0)
7260
      begin
7261
        test_fail("WB INT signal should not be set");
7262
        fail = fail + 1;
7263
      end
7264
      // INTERMEDIATE DISPLAYS
7265
      if (i_length == 3)
7266
      begin
7267
        $display("    pads appending to packets is selected");
7268
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
7269
        $display("    ->packets with lengths from %0d to %0d are not transmitted (length increasing by 1 byte)",
7270
                 0, 3);
7271
      end
7272
      else if (i_length == 9)
7273
      begin
7274
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
7275
        $display("    ->packet with length 4 is not transmitted (length increasing by 1 byte)");
7276
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7277
                 5, 9);
7278
      end
7279
      else if (i_length == 17)
7280
      begin
7281
        $display("    using 4 BDs out of 8 BDs assigned to TX (wrap at 4th BD - TX BD 3)");
7282
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7283
                 10, 17);
7284
      end
7285
      else if (i_length == 27)
7286
      begin
7287
        $display("    using 5 BDs out of 8 BDs assigned to TX (wrap at 5th BD - TX BD 4)");
7288
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7289
                 18, 27);
7290
      end
7291
      else if (i_length == 40)
7292
      begin
7293
        $display("    using 6 BDs out of 8 BDs assigned to TX (wrap at 6th BD - TX BD 5)");
7294
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7295
                 28, 40);
7296
      end
7297
      else if (i_length == 54)
7298
      begin
7299
        $display("    using 7 BDs out of 8 BDs assigned to TX (wrap at 7th BD - TX BD 6)");
7300
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7301
                 41, 54);
7302
      end
7303
      else if (i_length == 69)
7304
      begin
7305
        $display("    using 8 BDs out of 8 BDs assigned to TX (wrap at 8th BD - TX BD 7)");
7306
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7307
                 55, 69);
7308
      end
7309
      // set length (loop variable)
7310
      i_length = i_length + 1;
7311
      // the number of frame transmitted
7312
      num_of_frames = num_of_frames + 1;
7313
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
7314
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
7315
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
7316
          (num_of_frames == 54) || (num_of_frames == 62))
7317
        num_of_bd = 0;
7318
      else
7319
        num_of_bd = num_of_bd + 1;
7320
    end
7321
    // disable TX
7322
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
7323
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7324
    @(posedge wb_clk);
7325
    if(fail == 0)
7326
      test_ok;
7327
    else
7328
      fail = 0;
7329
  end
7330
 
7331
 
7332
  ////////////////////////////////////////////////////////////////////
7333
  ////                                                            ////
7334 243 tadejm
  ////  Test transmit packets (no pads) form 0 to (MINFL - 1)     ////
7335 209 tadejm
  ////  sizes at 8 TX buffer decriptors ( 100Mbps ).              ////
7336
  ////                                                            ////
7337
  ////////////////////////////////////////////////////////////////////
7338
  if (test_num == 9) // 
7339
  begin
7340 243 tadejm
    // TEST 9: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )
7341
    test_name = "TEST 9: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )";
7342
    `TIME; $display("  TEST 9: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )");
7343 209 tadejm
 
7344
    // reset MAC registers
7345
    hard_reset;
7346
    // reset MAC and MII LOGIC with soft reset
7347
    reset_mac;
7348
    reset_mii;
7349
    // set wb slave response
7350
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
7351
 
7352
    max_tmp = 0;
7353
    min_tmp = 0;
7354
    // set 8 TX buffer descriptors - must be set before TX enable
7355
    wbm_write(`ETH_TX_BD_NUM, 32'h8, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7356
    // enable TX, set full-duplex mode, padding and CRC appending
7357
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
7358
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7359
    // prepare two packets of MAXFL length
7360
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7361
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
7362
    min_tmp = tmp[31:16];
7363
    st_data = 8'h12;
7364
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
7365
    st_data = 8'h34;
7366
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp - 4), st_data); // length without CRC
7367
    // check WB INT signal
7368
    if (wb_int !== 1'b0)
7369
    begin
7370
      test_fail("WB INT signal should not be set");
7371
      fail = fail + 1;
7372
    end
7373
 
7374
    // write to phy's control register for 100Mbps
7375
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
7376
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
7377
    speed = 100;
7378
 
7379
    frame_started = 0;
7380
    num_of_frames = 0;
7381
    num_of_bd = 0;
7382
    i_length = 0; // 0;
7383
    while (i_length < 70) // (min_tmp - 4))
7384
    begin
7385
      #1;
7386
      // choose generating carrier sense and collision
7387
      case (i_length[1:0])
7388
      2'h0: // Interrupt is generated
7389
      begin
7390
        // Reset_tx_bd nable interrupt generation
7391
        // unmask interrupts
7392
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
7393
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7394
        // not detect carrier sense in FD and no collision
7395
        eth_phy.carrier_sense_tx_fd_detect(0);
7396
        eth_phy.collision(0);
7397
      end
7398
      2'h1: // Interrupt is not generated
7399
      begin
7400
        // set_tx_bd enable interrupt generation
7401
        // mask interrupts
7402
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7403
        // detect carrier sense in FD and no collision
7404
        eth_phy.carrier_sense_tx_fd_detect(1);
7405
        eth_phy.collision(0);
7406
      end
7407
      2'h2: // Interrupt is not generated
7408
      begin
7409
        // set_tx_bd disable the interrupt generation
7410
        // unmask interrupts
7411
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
7412
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7413
        // not detect carrier sense in FD and set collision
7414
        eth_phy.carrier_sense_tx_fd_detect(0);
7415
        eth_phy.collision(1);
7416
      end
7417
      default: // 2'h3: // Interrupt is not generated
7418
      begin
7419
        // set_tx_bd disable the interrupt generation
7420
        // mask interrupts
7421
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7422
        // detect carrier sense in FD and set collision
7423
        eth_phy.carrier_sense_tx_fd_detect(1);
7424
        eth_phy.collision(1);
7425
      end
7426
      endcase
7427
      #1;
7428
      // first destination address on ethernet PHY
7429
      eth_phy.set_tx_mem_addr(num_of_frames * 16);
7430
      // SET packets and wrap bit
7431
      // num_of_frames <= 9 => wrap set to TX BD 0
7432
      if (num_of_frames <= 9)
7433
      begin
7434
        tmp_len = i_length; // length of frame
7435
        tmp_bd_num = 0; // TX BD number
7436
        // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7437
        // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7438
        if (tmp_len[0] == 0)
7439
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7440
        else
7441
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7442
        // set wrap bit
7443
        set_tx_bd_wrap(0);
7444
      end
7445
      // 10 <= num_of_frames < 18 => wrap set to TX BD 3
7446
      else if ((num_of_frames == 10) || (num_of_frames == 14))
7447
      begin
7448
        tmp_len = i_length; // length of frame
7449
        tmp_bd_num = 0; // TX BD number
7450
        while (tmp_bd_num < 4) //
7451
        begin
7452
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7453
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7454
          if (tmp_len[0] == 0)
7455
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7456
          else
7457
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7458
          tmp_len = tmp_len + 1;
7459
          // set TX BD number
7460
          tmp_bd_num = tmp_bd_num + 1;
7461
        end
7462
        // set wrap bit
7463
        set_tx_bd_wrap(3);
7464
      end
7465
      // 18 <= num_of_frames < 28 => wrap set to TX BD 4
7466
      else if ((num_of_frames == 18) || (num_of_frames == 23))
7467
      begin
7468
        tmp_len = i_length; // length of frame
7469
        tmp_bd_num = 0; // TX BD number
7470
        while (tmp_bd_num < 5) //
7471
        begin
7472
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7473
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7474
          if (tmp_len[0] == 0)
7475
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7476
          else
7477
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7478
          tmp_len = tmp_len + 1;
7479
          // set TX BD number
7480
          tmp_bd_num = tmp_bd_num + 1;
7481
        end
7482
        // set wrap bit
7483
        set_tx_bd_wrap(4);
7484
      end
7485
      // 28 <= num_of_frames < 40 => wrap set to TX BD 5
7486
      else if ((num_of_frames == 28) || (num_of_frames == 34))
7487
      begin
7488
        tmp_len = i_length; // length of frame
7489
        tmp_bd_num = 0; // TX BD number
7490
        while (tmp_bd_num < 6) //
7491
        begin
7492
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7493
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7494
          if (tmp_len[0] == 0)
7495
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7496
          else
7497
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7498
          tmp_len = tmp_len + 1;
7499
          // set TX BD number
7500
          tmp_bd_num = tmp_bd_num + 1;
7501
        end
7502
        // set wrap bit
7503
        set_tx_bd_wrap(5);
7504
      end
7505
      // 40 <= num_of_frames < 54 => wrap set to TX BD 6
7506
      else if ((num_of_frames == 40) || (num_of_frames == 47))
7507
      begin
7508
        tmp_len = i_length; // length of frame
7509
        tmp_bd_num = 0; // TX BD number
7510
        while (tmp_bd_num < 7) //
7511
        begin
7512
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7513
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7514
          if (tmp_len[0] == 0)
7515
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7516
          else
7517
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7518
          tmp_len = tmp_len + 1;
7519
          // set TX BD number
7520
          tmp_bd_num = tmp_bd_num + 1;
7521
        end
7522
        // set wrap bit
7523
        set_tx_bd_wrap(6);
7524
      end
7525
      // 54 <= num_of_frames < 70 => wrap set to TX BD 7
7526
      else if ((num_of_frames == 54) || (num_of_frames == 62))
7527
      begin
7528
        tmp_len = i_length; // length of frame
7529
        tmp_bd_num = 0; // TX BD number
7530
        while (tmp_bd_num < 8) //
7531
        begin
7532
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7533
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7534
          if (tmp_len[0] == 0)
7535
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7536
          else
7537
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7538
          tmp_len = tmp_len + 1;
7539
          // set TX BD number
7540
          tmp_bd_num = tmp_bd_num + 1;
7541
        end
7542
        // set wrap bit
7543
        set_tx_bd_wrap(7);
7544
      end
7545
      #1;
7546
      // SET ready bit
7547
      if (num_of_frames < 10)
7548
        set_tx_bd_ready(0, 0);
7549
      else if (num_of_frames < 14)
7550
        set_tx_bd_ready((num_of_frames - 10), (num_of_frames - 10));
7551
      else if (num_of_frames < 18)
7552
        set_tx_bd_ready((num_of_frames - 14), (num_of_frames - 14));
7553
      else if (num_of_frames < 23)
7554
        set_tx_bd_ready((num_of_frames - 18), (num_of_frames - 18));
7555
      else if (num_of_frames < 28)
7556
        set_tx_bd_ready((num_of_frames - 23), (num_of_frames - 23));
7557
      else if (num_of_frames < 34)
7558
        set_tx_bd_ready((num_of_frames - 28), (num_of_frames - 28));
7559
      else if (num_of_frames < 40)
7560
        set_tx_bd_ready((num_of_frames - 34), (num_of_frames - 34));
7561
      else if (num_of_frames < 47)
7562
        set_tx_bd_ready((num_of_frames - 40), (num_of_frames - 40));
7563
      else if (num_of_frames < 54)
7564
        set_tx_bd_ready((num_of_frames - 47), (num_of_frames - 47));
7565
      else if (num_of_frames < 62)
7566
        set_tx_bd_ready((num_of_frames - 54), (num_of_frames - 54));
7567
      else if (num_of_frames < 70)
7568
        set_tx_bd_ready((num_of_frames - 62), (num_of_frames - 62));
7569
      // CHECK END OF TRANSMITION
7570
      frame_started = 0;
7571
      if (num_of_frames >= 5)
7572
        #1 check_tx_bd(num_of_bd, data);
7573
      fork
7574
      begin: fr_st3
7575
        wait (MTxEn === 1'b1); // start transmit
7576
        frame_started = 1;
7577
      end
7578
      begin
7579
        repeat (30) @(posedge mtx_clk);
7580
        if (num_of_frames < 5)
7581
        begin
7582
          if (frame_started == 1)
7583
          begin
7584
            `TIME; $display("*E Frame should NOT start!");
7585
          end
7586
          disable fr_st3;
7587
        end
7588
        else
7589
        begin
7590
          if (frame_started == 0)
7591
          begin
7592
            `TIME; $display("*W Frame should start!");
7593
            disable fr_st3;
7594
          end
7595
        end
7596
      end
7597
      join
7598
      // check packets larger than 4 bytes
7599
      if (num_of_frames >= 5)
7600
      begin
7601
        wait (MTxEn === 1'b0); // end transmit
7602
        while (data[15] === 1)
7603
        begin
7604
          #1 check_tx_bd(num_of_bd, data);
7605
          @(posedge wb_clk);
7606
        end
7607
        repeat (1) @(posedge wb_clk);
7608
        // check length of a PACKET
7609
        if (eth_phy.tx_len != (i_length + 4))
7610
        begin
7611
          `TIME; $display("*E Wrong length of the packet out from MAC");
7612
          test_fail("Wrong length of the packet out from MAC");
7613
          fail = fail + 1;
7614
        end
7615
        // check transmitted TX packet data
7616
        if (i_length[0] == 0)
7617
        begin
7618
          #1 check_tx_packet(`MEMORY_BASE, (num_of_frames * 16), i_length, tmp);
7619
        end
7620
        else
7621
        begin
7622
          #1 check_tx_packet((`MEMORY_BASE + max_tmp), (num_of_frames * 16), i_length, tmp);
7623
        end
7624
        if (tmp > 0)
7625
        begin
7626
          test_fail("Wrong data of the transmitted packet");
7627
          fail = fail + 1;
7628
        end
7629
        // check transmited TX packet CRC
7630
        #1 check_tx_crc((num_of_frames * 16), (eth_phy.tx_len - 4), 1'b0, tmp); // length without CRC
7631
        if (tmp > 0)
7632
        begin
7633
          test_fail("Wrong CRC of the transmitted packet");
7634
          fail = fail + 1;
7635
        end
7636
      end
7637
      // check WB INT signal
7638
      if ((i_length[1:0] == 2'h0) && (num_of_frames >= 5))
7639
      begin
7640
        if (wb_int !== 1'b1)
7641
        begin
7642
          `TIME; $display("*E WB INT signal should be set");
7643
          test_fail("WB INT signal should be set");
7644
          fail = fail + 1;
7645
        end
7646
      end
7647
      else
7648
      begin
7649
        if (wb_int !== 1'b0)
7650
        begin
7651
          `TIME; $display("*E WB INT signal should not be set");
7652
          test_fail("WB INT signal should not be set");
7653
          fail = fail + 1;
7654
        end
7655
      end
7656
      // check TX buffer descriptor of a packet
7657
      check_tx_bd(num_of_bd, data);
7658
      if (num_of_frames >= 5)
7659
      begin
7660
        if (i_length[1] == 1'b0) // interrupt enabled
7661
        begin
7662
          if ( (data[15:0] !== 16'h7800) && // wrap bit
7663
               (data[15:0] !== 16'h5800) ) // without wrap bit
7664
          begin
7665
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7666
            test_fail("TX buffer descriptor status is not correct");
7667
            fail = fail + 1;
7668
          end
7669
        end
7670
        else // interrupt not enabled
7671
        begin
7672
          if ( (data[15:0] !== 16'h3800) && // wrap bit
7673
               (data[15:0] !== 16'h1800) ) // without wrap bit
7674
          begin
7675
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7676
            test_fail("TX buffer descriptor status is not correct");
7677
            fail = fail + 1;
7678
          end
7679
        end
7680
      end
7681
      else
7682
      begin
7683
        if (data[15] !== 1'b1)
7684
        begin
7685
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7686
          test_fail("TX buffer descriptor status is not correct");
7687
          fail = fail + 1;
7688
        end
7689
      end
7690
      // clear TX BD with wrap bit
7691
      if (num_of_frames == 63)
7692
        clear_tx_bd(16, 16);
7693
      // check interrupts
7694
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7695
      if ( ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1)) && (num_of_frames >= 5) )
7696
      begin
7697
        if ((data & `ETH_INT_TXB) !== 1'b1)
7698
        begin
7699
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
7700
          test_fail("Interrupt Transmit Buffer was not set");
7701
          fail = fail + 1;
7702
        end
7703
        if ((data & (~`ETH_INT_TXB)) !== 0)
7704
        begin
7705
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
7706
          test_fail("Other interrupts (except Transmit Buffer) were set");
7707
          fail = fail + 1;
7708
        end
7709
      end
7710
      else
7711
      begin
7712
        if (data !== 0)
7713
        begin
7714
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
7715
          test_fail("Any of interrupts (except Transmit Buffer) was set");
7716
          fail = fail + 1;
7717
        end
7718
      end
7719
      // clear interrupts
7720
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7721
      // check WB INT signal
7722
      if (wb_int !== 1'b0)
7723
      begin
7724
        test_fail("WB INT signal should not be set");
7725
        fail = fail + 1;
7726
      end
7727
      // INTERMEDIATE DISPLAYS
7728
      if (i_length == 3)
7729
      begin
7730
        $display("    pads appending to packets is selected");
7731
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
7732
        $display("    ->packets with lengths from %0d to %0d are not transmitted (length increasing by 1 byte)",
7733
                 0, 3);
7734
      end
7735
      else if (i_length == 9)
7736
      begin
7737
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
7738
        $display("    ->packet with length 4 is not transmitted (length increasing by 1 byte)");
7739
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7740
                 5, 9);
7741
      end
7742
      else if (i_length == 17)
7743
      begin
7744
        $display("    using 4 BDs out of 8 BDs assigned to TX (wrap at 4th BD - TX BD 3)");
7745
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7746
                 10, 17);
7747
      end
7748
      else if (i_length == 27)
7749
      begin
7750
        $display("    using 5 BDs out of 8 BDs assigned to TX (wrap at 5th BD - TX BD 4)");
7751
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7752
                 18, 27);
7753
      end
7754
      else if (i_length == 40)
7755
      begin
7756
        $display("    using 6 BDs out of 8 BDs assigned to TX (wrap at 6th BD - TX BD 5)");
7757
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7758
                 28, 40);
7759
      end
7760
      else if (i_length == 54)
7761
      begin
7762
        $display("    using 7 BDs out of 8 BDs assigned to TX (wrap at 7th BD - TX BD 6)");
7763
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7764
                 41, 54);
7765
      end
7766
      else if (i_length == 69)
7767
      begin
7768
        $display("    using 8 BDs out of 8 BDs assigned to TX (wrap at 8th BD - TX BD 7)");
7769
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7770
                 55, 69);
7771
      end
7772
      // set length (loop variable)
7773
      i_length = i_length + 1;
7774
      // the number of frame transmitted
7775
      num_of_frames = num_of_frames + 1;
7776
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
7777
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
7778
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
7779
          (num_of_frames == 54) || (num_of_frames == 62))
7780
        num_of_bd = 0;
7781
      else
7782
        num_of_bd = num_of_bd + 1;
7783
    end
7784
    // disable TX
7785
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
7786
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7787
    @(posedge wb_clk);
7788
    if(fail == 0)
7789
      test_ok;
7790
    else
7791
      fail = 0;
7792
  end
7793
 
7794
 
7795
  ////////////////////////////////////////////////////////////////////
7796
  ////                                                            ////
7797
  ////  Test transmit packets across MAXFL value at               ////
7798
  ////  13 TX buffer decriptors ( 10Mbps ).                       ////
7799
  ////                                                            ////
7800
  ////////////////////////////////////////////////////////////////////
7801
  if (test_num == 10) // without and with padding
7802
  begin
7803
    // TEST 10: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 10Mbps )
7804
    test_name = "TEST 10: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 10Mbps )";
7805
    `TIME; $display("  TEST 10: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 10Mbps )");
7806
 
7807
    // reset MAC registers
7808
    hard_reset;
7809
    // reset MAC and MII LOGIC with soft reset
7810
    reset_mac;
7811
    reset_mii;
7812
    // set wb slave response
7813
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
7814
 
7815
    max_tmp = 0;
7816
    min_tmp = 0;
7817
    num_of_frames = 0;
7818
    num_of_bd = 0;
7819
    // set 13 TX buffer descriptors - must be set before TX enable
7820
    wbm_write(`ETH_TX_BD_NUM, 32'hD, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7821
    // enable TX, set full-duplex mode, NO padding and CRC appending
7822
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
7823
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7824
    // prepare a packet of MAXFL + 10 length
7825
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7826
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
7827
    min_tmp = tmp[31:16];
7828
    st_data = 8'hA3;
7829
    set_tx_packet(`MEMORY_BASE, (max_tmp + 10), st_data); // length without CRC
7830
    // check WB INT signal
7831
    if (wb_int !== 1'b0)
7832
    begin
7833
      test_fail("WB INT signal should not be set");
7834
      fail = fail + 1;
7835
    end
7836
 
7837
    // write to phy's control register for 10Mbps
7838
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
7839
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
7840
    speed = 10;
7841
 
7842
    i_length = (max_tmp - 5);
7843
    while (i_length <= (max_tmp - 3)) // (max_tmp - 4) is the limit
7844
    begin
7845
$display("   i_length = %0d", i_length);
7846
      // choose generating carrier sense and collision
7847
//      case (i_length[1:0])
7848
//      2'h0: // Interrupt is generated
7849
//      begin
7850
        // Reset_tx_bd nable interrupt generation
7851
        // unmask interrupts
7852
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
7853
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7854
        // not detect carrier sense in FD and no collision
7855
        eth_phy.carrier_sense_tx_fd_detect(0);
7856
        eth_phy.collision(0);
7857
//      end
7858
//      2'h1: // Interrupt is not generated
7859
//      begin
7860
        // set_tx_bd enable interrupt generation
7861
        // mask interrupts
7862
//        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7863
        // detect carrier sense in FD and no collision
7864
//        eth_phy.carrier_sense_tx_fd_detect(1);
7865
//        eth_phy.collision(0);
7866
//      end
7867
//      2'h2: // Interrupt is not generated
7868
//      begin
7869
        // set_tx_bd disable the interrupt generation
7870
        // unmask interrupts
7871
//        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
7872
//                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7873
        // not detect carrier sense in FD and set collision
7874
//        eth_phy.carrier_sense_tx_fd_detect(0);
7875
//        eth_phy.collision(1);
7876
//      end
7877
//      default: // 2'h3: // Interrupt is not generated
7878
//      begin
7879
        // set_tx_bd disable the interrupt generation
7880
        // mask interrupts
7881
//        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7882
        // detect carrier sense in FD and set collision
7883
//        eth_phy.carrier_sense_tx_fd_detect(1);
7884
//        eth_phy.collision(1);
7885
//      end
7886
//      endcase
7887
      // first destination address on ethernet PHY
7888
      eth_phy.set_tx_mem_addr(0);
7889
      // 
7890
if (num_of_bd == 0)
7891
begin
7892
set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
7893
set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
7894
set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
7895
set_tx_bd_wrap(2);
7896
set_tx_bd_ready(0, 0);
7897
end
7898
else if (num_of_bd == 1)
7899
set_tx_bd_ready(1, 1);
7900
else if (num_of_bd == 2)
7901
set_tx_bd_ready(2, 2);
7902
 
7903
 
7904
//        tmp_len = i_length; // length of frame
7905
//        tmp_bd_num = 0; // TX BD number
7906
//        while (tmp_bd_num < 8) // 
7907
//        begin
7908
//          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7909
//          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7910
//          if (tmp_len[0] == 0)
7911
//            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7912
//          else
7913
//            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + 2*max_tmp));
7914
//          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
7915
//          tmp_len = tmp_len + 1;
7916
//          // set TX BD number
7917
//          tmp_bd_num = tmp_bd_num + 1;
7918
//        end
7919
//        // set wrap bit
7920
//        set_tx_bd_wrap(7);
7921
//      // set ready bit
7922
//      set_tx_bd_ready((i_length - (max_tmp - 8)), (i_length - (max_tmp - 8)));
7923
      // CHECK END OF TRANSMITION
7924
check_tx_bd(num_of_bd, data);
7925
//      #1 check_tx_bd((i_length - (max_tmp - 8)), data);
7926
        wait (MTxEn === 1'b1); // start transmit
7927
check_tx_bd(num_of_bd, data);
7928
//        #1 check_tx_bd((i_length - (max_tmp - 8)), data);
7929
        if (data[15] !== 1)
7930
        begin
7931
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
7932
          fail = fail + 1;
7933
        end
7934
        wait (MTxEn === 1'b0); // end transmit
7935
        while (data[15] === 1)
7936
        begin
7937
check_tx_bd(num_of_bd, data);
7938
//          #1 check_tx_bd((i_length - (max_tmp - 8)), data);
7939
          @(posedge wb_clk);
7940
        end
7941
        repeat (1) @(posedge wb_clk);
7942
      // check length of a PACKET
7943
$display("   eth_phy length = %0d", eth_phy.tx_len);
7944
tmp_len = eth_phy.tx_len;
7945
#1;
7946
if (tmp_len != (i_length + 4))
7947
//      if (eth_phy.tx_len != (i_length + 4))
7948
      begin
7949
        test_fail("Wrong length of the packet out from MAC");
7950
        fail = fail + 1;
7951
      end
7952
      // checking in the following if statement is performed only for first and last 64 lengths
7953
//      if ( ((i_length + 4) <= (min_tmp + 64)) || ((i_length + 4) > (max_tmp - 64)) )
7954
//      begin
7955
        // check transmitted TX packet data
7956
//        if (i_length[0] == 0)
7957
//        begin
7958
          check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
7959
//        end
7960
//        else
7961
//        begin
7962
//          check_tx_packet((`MEMORY_BASE + 2*max_tmp), 0, i_length, tmp);
7963
//        end
7964
        if (tmp > 0)
7965
        begin
7966
          test_fail("Wrong data of the transmitted packet");
7967
          fail = fail + 1;
7968
        end
7969
        // check transmited TX packet CRC
7970
//        if (i_length[0] == 0)
7971
          check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
7972
//        else
7973
//          check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
7974
        if (tmp > 0)
7975
        begin
7976
          test_fail("Wrong CRC of the transmitted packet");
7977
          fail = fail + 1;
7978
        end
7979
//      end
7980
      // check WB INT signal
7981
//      if (i_length[1:0] == 2'h0)
7982
//      begin
7983
        if (wb_int !== 1'b1)
7984
        begin
7985
          `TIME; $display("*E WB INT signal should be set");
7986
          test_fail("WB INT signal should be set");
7987
          fail = fail + 1;
7988
        end
7989
//      end
7990
//      else
7991
//      begin
7992
//        if (wb_int !== 1'b0)
7993
//        begin
7994
//          `TIME; $display("*E WB INT signal should not be set");
7995
//          test_fail("WB INT signal should not be set");
7996
//          fail = fail + 1;
7997
//        end
7998
//      end
7999
//      // check TX buffer descriptor of a packet
8000
//      check_tx_bd((i_length - (max_tmp - 8)), data);
8001
check_tx_bd(num_of_bd, data);
8002
if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
8003
     ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
8004
//      if (i_length[1] == 1'b0) // interrupt enabled
8005
//      begin
8006
//        if ( ((data[15:0] !== 16'h7800) && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
8007
//             ((data[15:0] !== 16'h5800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
8008
        begin
8009
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8010
          test_fail("TX buffer descriptor status is not correct");
8011
          fail = fail + 1;
8012
        end
8013
//      end
8014
//      else // interrupt not enabled
8015
//      begin
8016
//        if ( ((data[15:0] !== 16'h3800)  && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
8017
//             ((data[15:0] !== 16'h1800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
8018
//        begin
8019
//          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8020
//          test_fail("TX buffer descriptor status is not correct");
8021
//          fail = fail + 1;
8022
//        end
8023
//      end
8024
//      // clear first half of 8 frames from TX buffer descriptor 0
8025
//      if (num_of_frames < 4)
8026
//        clear_tx_bd((i_length - (max_tmp - 8)), (i_length - (max_tmp - 8)));
8027
      // check interrupts
8028
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8029
//      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
8030
//      begin
8031
        if ((data & `ETH_INT_TXB) !== 1'b1)
8032
        begin
8033
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8034
          test_fail("Interrupt Transmit Buffer was not set");
8035
          fail = fail + 1;
8036
        end
8037
        if ((data & (~`ETH_INT_TXB)) !== 0)
8038
        begin
8039
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8040
          test_fail("Other interrupts (except Transmit Buffer) were set");
8041
          fail = fail + 1;
8042
        end
8043
//      end
8044
//      else
8045
//      begin
8046
//        if (data !== 0)
8047
//        begin
8048
//          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
8049
//          test_fail("Any of interrupts (except Transmit Buffer) was set");
8050
//          fail = fail + 1;
8051
//        end
8052
//      end
8053
      // clear interrupts
8054
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8055
      // check WB INT signal
8056
      if (wb_int !== 1'b0)
8057
      begin
8058
        test_fail("WB INT signal should not be set");
8059
        fail = fail + 1;
8060
      end
8061
      // INTERMEDIATE DISPLAYS
8062
if (num_of_bd == 0)
8063
  $display("    ->packet with length %0d sent", (i_length + 4));
8064
else if (num_of_bd == 1)
8065
  $display("    ->packet with length %0d sent", (i_length + 4));
8066
else if (num_of_bd == 2)
8067
  $display("    ->packet with length %0d sent", (i_length + 4));
8068
      // set length (loop variable)
8069
      i_length = i_length + 1;
8070
      // the number of frame transmitted
8071
      num_of_frames = num_of_frames + 1;
8072
      num_of_bd = num_of_bd + 1;
8073
      @(posedge wb_clk);
8074
    end
8075
    // disable TX
8076
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8077
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8078
    @(posedge wb_clk);
8079
    if(fail == 0)
8080
      test_ok;
8081
    else
8082
      fail = 0;
8083
  end
8084
 
8085
 
8086
  ////////////////////////////////////////////////////////////////////
8087
  ////                                                            ////
8088
  ////  Test transmit packets across MAXFL value at               ////
8089
  ////  13 TX buffer decriptors ( 100Mbps ).                      ////
8090
  ////                                                            ////
8091
  ////////////////////////////////////////////////////////////////////
8092
  if (test_num == 11) // without and with padding
8093
  begin
8094
    // TEST 11: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 100Mbps )
8095
    test_name = "TEST 11: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 100Mbps )";
8096
    `TIME; $display("  TEST 11: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 100Mbps )");
8097
 
8098
    // reset MAC registers
8099
    hard_reset;
8100
    // reset MAC and MII LOGIC with soft reset
8101
    reset_mac;
8102
    reset_mii;
8103
    // set wb slave response
8104
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
8105
 
8106
    max_tmp = 0;
8107
    min_tmp = 0;
8108
    num_of_frames = 0;
8109
    num_of_bd = 0;
8110
    // set 13 TX buffer descriptors - must be set before TX enable
8111
    wbm_write(`ETH_TX_BD_NUM, 32'hD, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8112
    // enable TX, set full-duplex mode, NO padding and CRC appending
8113
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
8114
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8115
    // prepare a packet of MAXFL + 10 length
8116
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8117
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
8118
    min_tmp = tmp[31:16];
8119
    st_data = 8'hA3;
8120
    set_tx_packet(`MEMORY_BASE, (max_tmp + 10), st_data); // length without CRC
8121
    // check WB INT signal
8122
    if (wb_int !== 1'b0)
8123
    begin
8124
      test_fail("WB INT signal should not be set");
8125
      fail = fail + 1;
8126
    end
8127
 
8128
    // write to phy's control register for 100Mbps
8129
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
8130
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
8131
    speed = 100;
8132
 
8133
    i_length = (max_tmp - 5);
8134
    while (i_length <= (max_tmp - 3)) // (max_tmp - 4) is the limit
8135
    begin
8136
      $display("   i_length = %0d", i_length);
8137
      // Reset_tx_bd nable interrupt generation
8138
      // unmask interrupts
8139
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
8140
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8141
      // not detect carrier sense in FD and no collision
8142
      eth_phy.carrier_sense_tx_fd_detect(0);
8143
      eth_phy.collision(0);
8144
      // first destination address on ethernet PHY
8145
      eth_phy.set_tx_mem_addr(0);
8146
      // prepare BDs
8147
      if (num_of_bd == 0)
8148
      begin
8149
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8150
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8151
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8152
        set_tx_bd_wrap(2);
8153
        set_tx_bd_ready(0, 0);
8154
      end
8155
      else if (num_of_bd == 1)
8156
        set_tx_bd_ready(1, 1);
8157
      else if (num_of_bd == 2)
8158
        set_tx_bd_ready(2, 2);
8159
      // CHECK END OF TRANSMITION
8160
      check_tx_bd(num_of_bd, data);
8161
        wait (MTxEn === 1'b1); // start transmit
8162
      check_tx_bd(num_of_bd, data);
8163
        if (data[15] !== 1)
8164
        begin
8165
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
8166
          fail = fail + 1;
8167
        end
8168
        wait (MTxEn === 1'b0); // end transmit
8169
        while (data[15] === 1)
8170
        begin
8171
      check_tx_bd(num_of_bd, data);
8172
          @(posedge wb_clk);
8173
        end
8174
        repeat (1) @(posedge wb_clk);
8175
      // check length of a PACKET
8176
      $display("   eth_phy length = %0d", eth_phy.tx_len);
8177
      tmp_len = eth_phy.tx_len;
8178
      #1;
8179
      if (tmp_len != (i_length + 4))
8180
      begin
8181
        test_fail("Wrong length of the packet out from MAC");
8182
        fail = fail + 1;
8183
      end
8184
      // checking packet
8185
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
8186
      if (tmp > 0)
8187
      begin
8188
        test_fail("Wrong data of the transmitted packet");
8189
        fail = fail + 1;
8190
      end
8191
      // check transmited TX packet CRC
8192
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
8193
      if (tmp > 0)
8194
      begin
8195
        test_fail("Wrong CRC of the transmitted packet");
8196
        fail = fail + 1;
8197
      end
8198
      // check WB INT signal
8199
      if (wb_int !== 1'b1)
8200
      begin
8201
        `TIME; $display("*E WB INT signal should be set");
8202
        test_fail("WB INT signal should be set");
8203
        fail = fail + 1;
8204
      end
8205
      // check TX buffer descriptor of a packet
8206
      check_tx_bd(num_of_bd, data);
8207
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
8208
           ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
8209
      begin
8210
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8211
        test_fail("TX buffer descriptor status is not correct");
8212
        fail = fail + 1;
8213
      end
8214
      // check interrupts
8215
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8216
      if ((data & `ETH_INT_TXB) !== 1'b1)
8217
      begin
8218
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8219
        test_fail("Interrupt Transmit Buffer was not set");
8220
        fail = fail + 1;
8221
      end
8222
      if ((data & (~`ETH_INT_TXB)) !== 0)
8223
      begin
8224
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8225
        test_fail("Other interrupts (except Transmit Buffer) were set");
8226
        fail = fail + 1;
8227
      end
8228
      // clear interrupts
8229
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8230
      // check WB INT signal
8231
      if (wb_int !== 1'b0)
8232
      begin
8233
        test_fail("WB INT signal should not be set");
8234
        fail = fail + 1;
8235
      end
8236
      // INTERMEDIATE DISPLAYS
8237
      if (num_of_bd == 0)
8238
        $display("    ->packet with length %0d sent", (i_length + 4));
8239
      else if (num_of_bd == 1)
8240
        $display("    ->packet with length %0d sent", (i_length + 4));
8241
      else if (num_of_bd == 2)
8242
        $display("    ->packet with length %0d sent", (i_length + 4));
8243
      // set length (loop variable)
8244
      i_length = i_length + 1;
8245
      // the number of frame transmitted
8246
      num_of_frames = num_of_frames + 1;
8247
      num_of_bd = num_of_bd + 1;
8248
      @(posedge wb_clk);
8249
    end
8250
    // disable TX
8251
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8252
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8253
    @(posedge wb_clk);
8254
    if(fail == 0)
8255
      test_ok;
8256
    else
8257
      fail = 0;
8258
  end
8259
 
8260
 
8261
  ////////////////////////////////////////////////////////////////////
8262
  ////                                                            ////
8263
  ////  Test transmit packets across changed MAXFL value at       ////
8264
  ////  47 TX buffer decriptors ( 10Mbps ).                       ////
8265
  ////                                                            ////
8266
  ////////////////////////////////////////////////////////////////////
8267
  if (test_num == 12) // without and with padding
8268
  begin
8269
    // TEST 12: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 10Mbps )
8270
    test_name = "TEST 12: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 10Mbps )";
8271
    `TIME; $display("  TEST 12: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 10Mbps )");
8272
 
8273
    // reset MAC registers
8274
    hard_reset;
8275
    // reset MAC and MII LOGIC with soft reset
8276
    reset_mac;
8277
    reset_mii;
8278
    // set wb slave response
8279
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
8280
 
8281
    max_tmp = 0;
8282
    min_tmp = 0;
8283
    num_of_frames = 0;
8284
    num_of_bd = 0;
8285
    // set 47 TX buffer descriptors - must be set before TX enable
8286
    wbm_write(`ETH_TX_BD_NUM, 32'h2F, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8287
    // prepare a packet of MAXFL + 10 length
8288
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8289
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
8290
    min_tmp = tmp[31:16];
8291
    // change MAXFL value
8292
    max_tmp = min_tmp + 53;
8293
    wbm_write(`ETH_PACKETLEN, {min_tmp, max_tmp}, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8294
    st_data = 8'h62;
8295
    set_tx_packet(`MEMORY_BASE, max_tmp, st_data); // length with CRC
8296
    append_tx_crc(`MEMORY_BASE, (max_tmp - 5), 1'b0); // for first packet
8297
    // enable TX, set full-duplex mode, NO padding and NO CRC appending
8298
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD,
8299
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8300
    // check WB INT signal
8301
    if (wb_int !== 1'b0)
8302
    begin
8303
      test_fail("WB INT signal should not be set");
8304
      fail = fail + 1;
8305
    end
8306
 
8307
    // write to phy's control register for 10Mbps
8308
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
8309
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
8310
    speed = 10;
8311
 
8312
    i_length = (max_tmp - 5); // (max_tmp - 1); // not (max_tmp - 5) because NO automatic CRC appending
8313
    while (i_length <= (max_tmp - 3)) // (max_tmp + 1)) // (max_tmp) is the limit
8314
    begin
8315
      $display("   i_length = %0d", i_length);
8316
      // prepare packet's CRC
8317
      if (num_of_bd == 1)
8318
        append_tx_crc(`MEMORY_BASE, (max_tmp - 4), 1'b0); // for second and third packets
8319
      // Reset_tx_bd nable interrupt generation
8320
      // unmask interrupts
8321
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
8322
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8323
      // not detect carrier sense in FD and no collision
8324
      eth_phy.carrier_sense_tx_fd_detect(0);
8325
      eth_phy.collision(0);
8326
      // first destination address on ethernet PHY
8327
      eth_phy.set_tx_mem_addr(0);
8328
      // prepare BDs
8329
      if (num_of_bd == 0)
8330
      begin
8331
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8332
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8333
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8334
        set_tx_bd_wrap(2);
8335
        set_tx_bd_ready(0, 0);
8336
      end
8337
      else if (num_of_bd == 1)
8338
        set_tx_bd_ready(1, 1);
8339
      else if (num_of_bd == 2)
8340
        set_tx_bd_ready(2, 2);
8341
      // CHECK END OF TRANSMITION
8342
      check_tx_bd(num_of_bd, data);
8343
        wait (MTxEn === 1'b1); // start transmit
8344
      check_tx_bd(num_of_bd, data);
8345
        if (data[15] !== 1)
8346
        begin
8347
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
8348
          fail = fail + 1;
8349
        end
8350
        wait (MTxEn === 1'b0); // end transmit
8351
        while (data[15] === 1)
8352
        begin
8353
      check_tx_bd(num_of_bd, data);
8354
          @(posedge wb_clk);
8355
        end
8356
        repeat (1) @(posedge wb_clk);
8357
      // check length of a PACKET
8358
      $display("   eth_phy length = %0d", eth_phy.tx_len);
8359
      tmp_len = eth_phy.tx_len;
8360
      #1;
8361
      if (tmp_len != (i_length + 4))
8362
      begin
8363
        test_fail("Wrong length of the packet out from MAC");
8364
        fail = fail + 1;
8365
      end
8366
      // checking packet
8367
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
8368
      if (tmp > 0)
8369
      begin
8370
        test_fail("Wrong data of the transmitted packet");
8371
        fail = fail + 1;
8372
      end
8373
      // check transmited TX packet CRC
8374
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
8375
      if (tmp > 0)
8376
      begin
8377
        test_fail("Wrong CRC of the transmitted packet");
8378
        fail = fail + 1;
8379
      end
8380
      // check WB INT signal
8381
      if (wb_int !== 1'b1)
8382
      begin
8383
        `TIME; $display("*E WB INT signal should be set");
8384
        test_fail("WB INT signal should be set");
8385
        fail = fail + 1;
8386
      end
8387
      // check TX buffer descriptor of a packet
8388
      check_tx_bd(num_of_bd, data);
8389
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
8390
           ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
8391
      begin
8392
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8393
        test_fail("TX buffer descriptor status is not correct");
8394
        fail = fail + 1;
8395
      end
8396
      // check interrupts
8397
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8398
      if ((data & `ETH_INT_TXB) !== 1'b1)
8399
      begin
8400
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8401
        test_fail("Interrupt Transmit Buffer was not set");
8402
        fail = fail + 1;
8403
      end
8404
      if ((data & (~`ETH_INT_TXB)) !== 0)
8405
      begin
8406
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8407
        test_fail("Other interrupts (except Transmit Buffer) were set");
8408
        fail = fail + 1;
8409
      end
8410
      // clear interrupts
8411
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8412
      // check WB INT signal
8413
      if (wb_int !== 1'b0)
8414
      begin
8415
        test_fail("WB INT signal should not be set");
8416
        fail = fail + 1;
8417
      end
8418
      // INTERMEDIATE DISPLAYS
8419
      if (num_of_bd == 0)
8420
        $display("    ->packet with length %0d sent", (i_length + 4));
8421
      else if (num_of_bd == 1)
8422
        $display("    ->packet with length %0d sent", (i_length + 4));
8423
      else if (num_of_bd == 2)
8424
        $display("    ->packet with length %0d sent", (i_length + 4));
8425
      // set length (loop variable)
8426
      i_length = i_length + 1;
8427
      // the number of frame transmitted
8428
      num_of_frames = num_of_frames + 1;
8429
      num_of_bd = num_of_bd + 1;
8430
      @(posedge wb_clk);
8431
    end
8432
    // disable TX
8433
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8434
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8435
    @(posedge wb_clk);
8436
    if(fail == 0)
8437
      test_ok;
8438
    else
8439
      fail = 0;
8440
  end
8441
 
8442
 
8443
  ////////////////////////////////////////////////////////////////////
8444
  ////                                                            ////
8445
  ////  Test transmit packets across changed MAXFL value at       ////
8446
  ////  47 TX buffer decriptors ( 100Mbps ).                      ////
8447
  ////                                                            ////
8448
  ////////////////////////////////////////////////////////////////////
8449
  if (test_num == 13) // without and with padding
8450
  begin
8451
    // TEST 13: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 100Mbps )
8452
    test_name = "TEST 13: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 100Mbps )";
8453
    `TIME; $display("  TEST 13: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 100Mbps )");
8454
 
8455
    // reset MAC registers
8456
    hard_reset;
8457
    // reset MAC and MII LOGIC with soft reset
8458
    reset_mac;
8459
    reset_mii;
8460
    // set wb slave response
8461
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
8462
 
8463
    max_tmp = 0;
8464
    min_tmp = 0;
8465
    num_of_frames = 0;
8466
    num_of_bd = 0;
8467
    // set 47 TX buffer descriptors - must be set before TX enable
8468
    wbm_write(`ETH_TX_BD_NUM, 32'h2F, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8469
    // prepare a packet of MAXFL + 10 length
8470
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8471
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
8472
    min_tmp = tmp[31:16];
8473
    // change MAXFL value
8474
    max_tmp = min_tmp + 53;
8475
    wbm_write(`ETH_PACKETLEN, {min_tmp, max_tmp}, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8476
    st_data = 8'h62;
8477
    set_tx_packet(`MEMORY_BASE, max_tmp, st_data); // length with CRC
8478
    append_tx_crc(`MEMORY_BASE, (max_tmp - 5), 1'b0); // for first packet
8479
    // enable TX, set full-duplex mode, NO padding and NO CRC appending
8480
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD,
8481
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8482
    // check WB INT signal
8483
    if (wb_int !== 1'b0)
8484
    begin
8485
      test_fail("WB INT signal should not be set");
8486
      fail = fail + 1;
8487
    end
8488
 
8489
    // write to phy's control register for 100Mbps
8490
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
8491
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
8492
    speed = 100;
8493
 
8494
    i_length = (max_tmp - 5); // (max_tmp - 1); // not (max_tmp - 5) because NO automatic CRC appending
8495
    while (i_length <= (max_tmp - 3)) // (max_tmp + 1)) // (max_tmp) is the limit
8496
    begin
8497
      $display("   i_length = %0d", i_length);
8498
      // prepare packet's CRC
8499
      if (num_of_bd == 1)
8500
        append_tx_crc(`MEMORY_BASE, (max_tmp - 4), 1'b0); // for second and third packets
8501
      // Reset_tx_bd nable interrupt generation
8502
      // unmask interrupts
8503
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
8504
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8505
      // not detect carrier sense in FD and no collision
8506
      eth_phy.carrier_sense_tx_fd_detect(0);
8507
      eth_phy.collision(0);
8508
      // first destination address on ethernet PHY
8509
      eth_phy.set_tx_mem_addr(0);
8510
      // prepare BDs
8511
      if (num_of_bd == 0)
8512
      begin
8513
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8514
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8515
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8516
        set_tx_bd_wrap(2);
8517
        set_tx_bd_ready(0, 0);
8518
      end
8519
      else if (num_of_bd == 1)
8520
        set_tx_bd_ready(1, 1);
8521
      else if (num_of_bd == 2)
8522
        set_tx_bd_ready(2, 2);
8523
      // CHECK END OF TRANSMITION
8524
      check_tx_bd(num_of_bd, data);
8525
        wait (MTxEn === 1'b1); // start transmit
8526
      check_tx_bd(num_of_bd, data);
8527
        if (data[15] !== 1)
8528
        begin
8529
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
8530
          fail = fail + 1;
8531
        end
8532
        wait (MTxEn === 1'b0); // end transmit
8533
        while (data[15] === 1)
8534
        begin
8535
      check_tx_bd(num_of_bd, data);
8536
          @(posedge wb_clk);
8537
        end
8538
        repeat (1) @(posedge wb_clk);
8539
      // check length of a PACKET
8540
      $display("   eth_phy length = %0d", eth_phy.tx_len);
8541
      tmp_len = eth_phy.tx_len;
8542
      #1;
8543
      if (tmp_len != (i_length + 4))
8544
      begin
8545
        test_fail("Wrong length of the packet out from MAC");
8546
        fail = fail + 1;
8547
      end
8548
      // checking packet
8549
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
8550
      if (tmp > 0)
8551
      begin
8552
        test_fail("Wrong data of the transmitted packet");
8553
        fail = fail + 1;
8554
      end
8555
      // check transmited TX packet CRC
8556
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
8557
      if (tmp > 0)
8558
      begin
8559
        test_fail("Wrong CRC of the transmitted packet");
8560
        fail = fail + 1;
8561
      end
8562
      // check WB INT signal
8563
      if (wb_int !== 1'b1)
8564
      begin
8565
        `TIME; $display("*E WB INT signal should be set");
8566
        test_fail("WB INT signal should be set");
8567
        fail = fail + 1;
8568
      end
8569
      // check TX buffer descriptor of a packet
8570
      check_tx_bd(num_of_bd, data);
8571
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
8572
           ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
8573
      begin
8574
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8575
        test_fail("TX buffer descriptor status is not correct");
8576
        fail = fail + 1;
8577
      end
8578
      // check interrupts
8579
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8580
      if ((data & `ETH_INT_TXB) !== 1'b1)
8581
      begin
8582
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8583
        test_fail("Interrupt Transmit Buffer was not set");
8584
        fail = fail + 1;
8585
      end
8586
      if ((data & (~`ETH_INT_TXB)) !== 0)
8587
      begin
8588
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8589
        test_fail("Other interrupts (except Transmit Buffer) were set");
8590
        fail = fail + 1;
8591
      end
8592
      // clear interrupts
8593
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8594
      // check WB INT signal
8595
      if (wb_int !== 1'b0)
8596
      begin
8597
        test_fail("WB INT signal should not be set");
8598
        fail = fail + 1;
8599
      end
8600
      // INTERMEDIATE DISPLAYS
8601
      if (num_of_bd == 0)
8602
        $display("    ->packet with length %0d sent", (i_length + 4));
8603
      else if (num_of_bd == 1)
8604
        $display("    ->packet with length %0d sent", (i_length + 4));
8605
      else if (num_of_bd == 2)
8606
        $display("    ->packet with length %0d sent", (i_length + 4));
8607
      // set length (loop variable)
8608
      i_length = i_length + 1;
8609
      // the number of frame transmitted
8610
      num_of_frames = num_of_frames + 1;
8611
      num_of_bd = num_of_bd + 1;
8612
      @(posedge wb_clk);
8613
    end
8614
    // disable TX
8615
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8616
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8617
    @(posedge wb_clk);
8618
    if(fail == 0)
8619
      test_ok;
8620
    else
8621
      fail = 0;
8622
  end
8623
 
8624
 
8625
  ////////////////////////////////////////////////////////////////////
8626
  ////                                                            ////
8627
  ////  Test transmit packets across changed MINFL value at       ////
8628
  ////  7 TX buffer decriptors ( 10Mbps ).                        ////
8629
  ////                                                            ////
8630
  ////////////////////////////////////////////////////////////////////
8631
  if (test_num == 14) // without and with padding
8632
  begin
8633
    // TEST 14: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 10Mbps )
8634
    test_name = "TEST 14: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 10Mbps )";
8635
    `TIME; $display("  TEST 14: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 10Mbps )");
8636
 
8637
    // reset MAC registers
8638
    hard_reset;
8639
    // reset MAC and MII LOGIC with soft reset
8640
    reset_mac;
8641
    reset_mii;
8642
    // set wb slave response
8643
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
8644
 
8645
    max_tmp = 0;
8646
    min_tmp = 0;
8647
    num_of_frames = 0;
8648
    num_of_bd = 0;
8649
    // set 7 TX buffer descriptors - must be set before TX enable
8650
    wbm_write(`ETH_TX_BD_NUM, 32'h7, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8651
    // prepare a packet of MAXFL + 10 length
8652
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8653
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
8654
    min_tmp = tmp[31:16];
8655
    // change MINFL value
8656
    min_tmp = max_tmp - 177;
8657
    wbm_write(`ETH_PACKETLEN, {min_tmp, max_tmp}, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8658
    st_data = 8'h62;
8659
    set_tx_packet(`MEMORY_BASE, min_tmp, st_data); // length without CRC
8660
    // enable TX, set full-duplex mode, padding and CRC appending
8661
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
8662
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8663
    // check WB INT signal
8664
    if (wb_int !== 1'b0)
8665
    begin
8666
      test_fail("WB INT signal should not be set");
8667
      fail = fail + 1;
8668
    end
8669
 
8670
    // write to phy's control register for 10Mbps
8671
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
8672
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
8673
    speed = 10;
8674
 
8675
    i_length = (min_tmp - 5);
8676
    while (i_length <= (min_tmp - 3)) // (min_tmp - 4) is the limit
8677
    begin
8678
      $display("   i_length = %0d", i_length);
8679
      // Reset_tx_bd nable interrupt generation
8680
      // unmask interrupts
8681
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
8682
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8683
      // not detect carrier sense in FD and no collision
8684
      eth_phy.carrier_sense_tx_fd_detect(0);
8685
      eth_phy.collision(0);
8686
      // first destination address on ethernet PHY
8687
      eth_phy.set_tx_mem_addr(0);
8688
      // prepare BDs
8689
      if (num_of_bd == 0)
8690
      begin
8691
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8692
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8693
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8694
        set_tx_bd_wrap(2);
8695
        set_tx_bd_ready(0, 0);
8696
      end
8697
      else if (num_of_bd == 1)
8698
        set_tx_bd_ready(1, 1);
8699
      else if (num_of_bd == 2)
8700
        set_tx_bd_ready(2, 2);
8701
      // CHECK END OF TRANSMITION
8702
      check_tx_bd(num_of_bd, data);
8703
        wait (MTxEn === 1'b1); // start transmit
8704
      check_tx_bd(num_of_bd, data);
8705
        if (data[15] !== 1)
8706
        begin
8707
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
8708
          fail = fail + 1;
8709
        end
8710
        wait (MTxEn === 1'b0); // end transmit
8711
        while (data[15] === 1)
8712
        begin
8713
      check_tx_bd(num_of_bd, data);
8714
          @(posedge wb_clk);
8715
        end
8716
        repeat (1) @(posedge wb_clk);
8717
      // check length of a PACKET
8718
      $display("   eth_phy length = %0d", eth_phy.tx_len);
8719
      tmp_len = eth_phy.tx_len;
8720
      #1;
8721
      if (tmp_len != (i_length + 4))
8722
      begin
8723
        test_fail("Wrong length of the packet out from MAC");
8724
        fail = fail + 1;
8725
      end
8726
      // checking packet
8727
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
8728
      if (tmp > 0)
8729
      begin
8730
        test_fail("Wrong data of the transmitted packet");
8731
        fail = fail + 1;
8732
      end
8733
      // check transmited TX packet CRC
8734
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
8735
      if (tmp > 0)
8736
      begin
8737
        test_fail("Wrong CRC of the transmitted packet");
8738
        fail = fail + 1;
8739
      end
8740
      // check WB INT signal
8741
      if (wb_int !== 1'b1)
8742
      begin
8743
        `TIME; $display("*E WB INT signal should be set");
8744
        test_fail("WB INT signal should be set");
8745
        fail = fail + 1;
8746
      end
8747
      // check TX buffer descriptor of a packet
8748
      check_tx_bd(num_of_bd, data);
8749
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
8750
           ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
8751
      begin
8752
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8753
        test_fail("TX buffer descriptor status is not correct");
8754
        fail = fail + 1;
8755
      end
8756
      // check interrupts
8757
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8758
      if ((data & `ETH_INT_TXB) !== 1'b1)
8759
      begin
8760
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8761
        test_fail("Interrupt Transmit Buffer was not set");
8762
        fail = fail + 1;
8763
      end
8764
      if ((data & (~`ETH_INT_TXB)) !== 0)
8765
      begin
8766
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8767
        test_fail("Other interrupts (except Transmit Buffer) were set");
8768
        fail = fail + 1;
8769
      end
8770
      // clear interrupts
8771
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8772
      // check WB INT signal
8773
      if (wb_int !== 1'b0)
8774
      begin
8775
        test_fail("WB INT signal should not be set");
8776
        fail = fail + 1;
8777
      end
8778
      // INTERMEDIATE DISPLAYS
8779
      if (num_of_bd == 0)
8780
        $display("    ->packet with length %0d sent", (i_length + 4));
8781
      else if (num_of_bd == 1)
8782
        $display("    ->packet with length %0d sent", (i_length + 4));
8783
      else if (num_of_bd == 2)
8784
        $display("    ->packet with length %0d sent", (i_length + 4));
8785
      // set length (loop variable)
8786
      i_length = i_length + 1;
8787
      // the number of frame transmitted
8788
      num_of_frames = num_of_frames + 1;
8789
      num_of_bd = num_of_bd + 1;
8790
      @(posedge wb_clk);
8791
    end
8792
    // disable TX
8793
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8794
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8795
    @(posedge wb_clk);
8796
    if(fail == 0)
8797
      test_ok;
8798
    else
8799
      fail = 0;
8800
  end
8801
 
8802
 
8803
  ////////////////////////////////////////////////////////////////////
8804
  ////                                                            ////
8805
  ////  Test transmit packets across changed MINFL value at       ////
8806
  ////  7 TX buffer decriptors ( 100Mbps ).                       ////
8807
  ////                                                            ////
8808
  ////////////////////////////////////////////////////////////////////
8809
  if (test_num == 15) // without and with padding
8810
  begin
8811
    // TEST 15: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 100Mbps )
8812
    test_name = "TEST 15: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 100Mbps )";
8813
    `TIME; $display("  TEST 15: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 100Mbps )");
8814
 
8815
    // reset MAC registers
8816
    hard_reset;
8817
    // reset MAC and MII LOGIC with soft reset
8818
    reset_mac;
8819
    reset_mii;
8820
    // set wb slave response
8821
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
8822
 
8823
    max_tmp = 0;
8824
    min_tmp = 0;
8825
    num_of_frames = 0;
8826
    num_of_bd = 0;
8827
    // set 7 TX buffer descriptors - must be set before TX enable
8828
    wbm_write(`ETH_TX_BD_NUM, 32'h7, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8829
    // prepare a packet of MAXFL + 10 length
8830
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8831
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
8832
    min_tmp = tmp[31:16];
8833
    // change MINFL value
8834
    min_tmp = max_tmp - 177;
8835
    wbm_write(`ETH_PACKETLEN, {min_tmp, max_tmp}, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8836
    st_data = 8'h62;
8837
    set_tx_packet(`MEMORY_BASE, min_tmp, st_data); // length without CRC
8838
    // enable TX, set full-duplex mode, padding and CRC appending
8839
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
8840
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8841
    // check WB INT signal
8842
    if (wb_int !== 1'b0)
8843
    begin
8844
      test_fail("WB INT signal should not be set");
8845
      fail = fail + 1;
8846
    end
8847
 
8848
    // write to phy's control register for 100Mbps
8849
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
8850
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
8851
    speed = 100;
8852
 
8853
    i_length = (min_tmp - 5);
8854
    while (i_length <= (min_tmp - 3)) // (min_tmp - 4) is the limit
8855
    begin
8856
      $display("   i_length = %0d", i_length);
8857
      // Reset_tx_bd nable interrupt generation
8858
      // unmask interrupts
8859
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
8860
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8861
      // not detect carrier sense in FD and no collision
8862
      eth_phy.carrier_sense_tx_fd_detect(0);
8863
      eth_phy.collision(0);
8864
      // first destination address on ethernet PHY
8865
      eth_phy.set_tx_mem_addr(0);
8866
      // prepare BDs
8867
      if (num_of_bd == 0)
8868
      begin
8869
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8870
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8871
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8872
        set_tx_bd_wrap(2);
8873
        set_tx_bd_ready(0, 0);
8874
      end
8875
      else if (num_of_bd == 1)
8876
        set_tx_bd_ready(1, 1);
8877
      else if (num_of_bd == 2)
8878
        set_tx_bd_ready(2, 2);
8879
      // CHECK END OF TRANSMITION
8880
      check_tx_bd(num_of_bd, data);
8881
        wait (MTxEn === 1'b1); // start transmit
8882
      check_tx_bd(num_of_bd, data);
8883
        if (data[15] !== 1)
8884
        begin
8885
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
8886
          fail = fail + 1;
8887
        end
8888
        wait (MTxEn === 1'b0); // end transmit
8889
        while (data[15] === 1)
8890
        begin
8891
      check_tx_bd(num_of_bd, data);
8892
          @(posedge wb_clk);
8893
        end
8894
        repeat (1) @(posedge wb_clk);
8895
      // check length of a PACKET
8896
      $display("   eth_phy length = %0d", eth_phy.tx_len);
8897
      tmp_len = eth_phy.tx_len;
8898
      #1;
8899
      if (tmp_len != (i_length + 4))
8900
      begin
8901
        test_fail("Wrong length of the packet out from MAC");
8902
        fail = fail + 1;
8903
      end
8904
      // checking packet
8905
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
8906
      if (tmp > 0)
8907
      begin
8908
        test_fail("Wrong data of the transmitted packet");
8909
        fail = fail + 1;
8910
      end
8911
      // check transmited TX packet CRC
8912
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
8913
      if (tmp > 0)
8914
      begin
8915
        test_fail("Wrong CRC of the transmitted packet");
8916
        fail = fail + 1;
8917
      end
8918
      // check WB INT signal
8919
      if (wb_int !== 1'b1)
8920
      begin
8921
        `TIME; $display("*E WB INT signal should be set");
8922
        test_fail("WB INT signal should be set");
8923
        fail = fail + 1;
8924
      end
8925
      // check TX buffer descriptor of a packet
8926
      check_tx_bd(num_of_bd, data);
8927
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
8928
           ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
8929
      begin
8930
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8931
        test_fail("TX buffer descriptor status is not correct");
8932
        fail = fail + 1;
8933
      end
8934
      // check interrupts
8935
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8936
      if ((data & `ETH_INT_TXB) !== 1'b1)
8937
      begin
8938
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8939
        test_fail("Interrupt Transmit Buffer was not set");
8940
        fail = fail + 1;
8941
      end
8942
      if ((data & (~`ETH_INT_TXB)) !== 0)
8943
      begin
8944
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8945
        test_fail("Other interrupts (except Transmit Buffer) were set");
8946
        fail = fail + 1;
8947
      end
8948
      // clear interrupts
8949
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8950
      // check WB INT signal
8951
      if (wb_int !== 1'b0)
8952
      begin
8953
        test_fail("WB INT signal should not be set");
8954
        fail = fail + 1;
8955
      end
8956
      // INTERMEDIATE DISPLAYS
8957
      if (num_of_bd == 0)
8958
        $display("    ->packet with length %0d sent", (i_length + 4));
8959
      else if (num_of_bd == 1)
8960
        $display("    ->packet with length %0d sent", (i_length + 4));
8961
      else if (num_of_bd == 2)
8962
        $display("    ->packet with length %0d sent", (i_length + 4));
8963
      // set length (loop variable)
8964
      i_length = i_length + 1;
8965
      // the number of frame transmitted
8966
      num_of_frames = num_of_frames + 1;
8967
      num_of_bd = num_of_bd + 1;
8968
      @(posedge wb_clk);
8969
    end
8970
    // disable TX
8971
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8972
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8973
    @(posedge wb_clk);
8974
    if(fail == 0)
8975
      test_ok;
8976
    else
8977
      fail = 0;
8978
  end
8979
 
8980
 
8981
  ////////////////////////////////////////////////////////////////////
8982
  ////                                                            ////
8983
  ////  Test transmit packets across MAXFL with HUGEN at          ////
8984
  ////  19 TX buffer decriptors ( 10Mbps ).                       ////
8985
  ////                                                            ////
8986
  ////////////////////////////////////////////////////////////////////
8987
  if (test_num == 16) // without and with padding
8988
  begin
8989
    // TEST 16: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 10Mbps )
8990
    test_name = "TEST 16: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 10Mbps )";
8991
    `TIME; $display("  TEST 16: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 10Mbps )");
8992
 
8993
    // reset MAC registers
8994
    hard_reset;
8995
    // reset MAC and MII LOGIC with soft reset
8996
    reset_mac;
8997
    reset_mii;
8998
    // set wb slave response
8999
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
9000
 
9001
    max_tmp = 0;
9002
    min_tmp = 0;
9003
    num_of_frames = 0;
9004
    num_of_bd = 0;
9005
    // set 19 TX buffer descriptors - must be set before TX enable
9006
    wbm_write(`ETH_TX_BD_NUM, 32'h13, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9007
    // prepare a packet of 64k - 1 length (16'hFFFF)
9008
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9009
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
9010
    min_tmp = tmp[31:16];
9011
    st_data = 8'h8D;
9012
    set_tx_packet(`MEMORY_BASE, 16'hFFFF, st_data); // length with CRC
9013
    // enable TX, set full-duplex mode, NO padding, CRC appending and huge enabled
9014
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN | `ETH_MODER_HUGEN,
9015
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9016
    // check WB INT signal
9017
    if (wb_int !== 1'b0)
9018
    begin
9019
      test_fail("WB INT signal should not be set");
9020
      fail = fail + 1;
9021
    end
9022
 
9023
    // write to phy's control register for 10Mbps
9024
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
9025
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
9026
    speed = 10;
9027
 
9028
    i_length = (max_tmp - 5); // (max_tmp - 4) is the MAXFL limit
9029
    while (i_length <= (16'hFFFF - 4)) // (16'hFFFF - 4) is the limit
9030
    begin
9031
      $display("   i_length = %0d", i_length);
9032
      // Reset_tx_bd nable interrupt generation
9033
      // unmask interrupts
9034
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
9035
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9036
      // not detect carrier sense in FD and no collision
9037
      eth_phy.carrier_sense_tx_fd_detect(0);
9038
      eth_phy.collision(0);
9039
      // first destination address on ethernet PHY
9040
      eth_phy.set_tx_mem_addr(0);
9041
      // prepare BDs
9042
      if (num_of_bd == 0)
9043
      begin
9044
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9045
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9046
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9047
        set_tx_bd(3, 3, (16'hFFFF - 5), 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9048
        set_tx_bd(4, 4, (16'hFFFF - 4), 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9049
        set_tx_bd_wrap(4);
9050
        set_tx_bd_ready(0, 0);
9051
      end
9052
      else if (num_of_bd == 1)
9053
        set_tx_bd_ready(1, 1);
9054
      else if (num_of_bd == 2)
9055
        set_tx_bd_ready(2, 2);
9056
      else if (num_of_bd == 3)
9057
        set_tx_bd_ready(3, 3);
9058
      else if (num_of_bd == 4)
9059
        set_tx_bd_ready(4, 4);
9060
      // CHECK END OF TRANSMITION
9061
      check_tx_bd(num_of_bd, data);
9062
        wait (MTxEn === 1'b1); // start transmit
9063
      check_tx_bd(num_of_bd, data);
9064
        if (data[15] !== 1)
9065
        begin
9066
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
9067
          fail = fail + 1;
9068
        end
9069
        wait (MTxEn === 1'b0); // end transmit
9070
        while (data[15] === 1)
9071
        begin
9072
      check_tx_bd(num_of_bd, data);
9073
          @(posedge wb_clk);
9074
        end
9075
        repeat (1) @(posedge wb_clk);
9076
      // check length of a PACKET
9077
      $display("   eth_phy length = %0d", eth_phy.tx_len);
9078
      tmp_len = eth_phy.tx_len;
9079
      #1;
9080
      if (tmp_len != (i_length + 4))
9081
      begin
9082
        test_fail("Wrong length of the packet out from MAC");
9083
        fail = fail + 1;
9084
      end
9085
      // checking packet
9086
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
9087
      if (tmp > 0)
9088
      begin
9089
        test_fail("Wrong data of the transmitted packet");
9090
        fail = fail + 1;
9091
      end
9092
      // check transmited TX packet CRC
9093
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
9094
      if (tmp > 0)
9095
      begin
9096
        test_fail("Wrong CRC of the transmitted packet");
9097
        fail = fail + 1;
9098
      end
9099
      // check WB INT signal
9100
      if (wb_int !== 1'b1)
9101
      begin
9102
        `TIME; $display("*E WB INT signal should be set");
9103
        test_fail("WB INT signal should be set");
9104
        fail = fail + 1;
9105
      end
9106
      // check TX buffer descriptor of a packet
9107
      check_tx_bd(num_of_bd, data);
9108
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 4)) || // wrap bit
9109
           ((data[15:0] !== 16'h5800) && (num_of_bd < 4)) )   // without wrap bit
9110
      begin
9111
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
9112
        test_fail("TX buffer descriptor status is not correct");
9113
        fail = fail + 1;
9114
      end
9115
      // check interrupts
9116
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9117
      if ((data & `ETH_INT_TXB) !== 1'b1)
9118
      begin
9119
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
9120
        test_fail("Interrupt Transmit Buffer was not set");
9121
        fail = fail + 1;
9122
      end
9123
      if ((data & (~`ETH_INT_TXB)) !== 0)
9124
      begin
9125
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
9126
        test_fail("Other interrupts (except Transmit Buffer) were set");
9127
        fail = fail + 1;
9128
      end
9129
      // clear interrupts
9130
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9131
      // check WB INT signal
9132
      if (wb_int !== 1'b0)
9133
      begin
9134
        test_fail("WB INT signal should not be set");
9135
        fail = fail + 1;
9136
      end
9137
      // INTERMEDIATE DISPLAYS
9138
      $display("    ->packet with length %0d sent", (i_length + 4));
9139
      // set length (loop variable)
9140
      if ((num_of_bd < 2) || (num_of_bd >= 3))
9141
        i_length = i_length + 1;
9142
      else if (num_of_bd == 2)
9143
        i_length = (16'hFFFF - 5);
9144
      // the number of frame transmitted
9145
      num_of_frames = num_of_frames + 1;
9146
      num_of_bd = num_of_bd + 1;
9147
      @(posedge wb_clk);
9148
    end
9149
    // disable TX
9150
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
9151
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9152
    @(posedge wb_clk);
9153
    if(fail == 0)
9154
      test_ok;
9155
    else
9156
      fail = 0;
9157
  end
9158
 
9159
 
9160
  ////////////////////////////////////////////////////////////////////
9161
  ////                                                            ////
9162
  ////  Test transmit packets across MAXFL with HUGEN at          ////
9163
  ////  19 TX buffer decriptors ( 100Mbps ).                      ////
9164
  ////                                                            ////
9165
  ////////////////////////////////////////////////////////////////////
9166
  if (test_num == 17) // without and with padding
9167
  begin
9168
    // TEST 17: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 100Mbps )
9169
    test_name = "TEST 17: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 100Mbps )";
9170
    `TIME; $display("  TEST 17: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 100Mbps )");
9171
 
9172
    // reset MAC registers
9173
    hard_reset;
9174
    // reset MAC and MII LOGIC with soft reset
9175
    reset_mac;
9176
    reset_mii;
9177
    // set wb slave response
9178
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
9179
 
9180
    max_tmp = 0;
9181
    min_tmp = 0;
9182
    num_of_frames = 0;
9183
    num_of_bd = 0;
9184
    // set 19 TX buffer descriptors - must be set before TX enable
9185
    wbm_write(`ETH_TX_BD_NUM, 32'h13, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9186
    // prepare a packet of 64k - 1 length (16'hFFFF)
9187
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9188
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
9189
    min_tmp = tmp[31:16];
9190
    st_data = 8'h8D;
9191
    set_tx_packet(`MEMORY_BASE, 16'hFFFF, st_data); // length with CRC
9192
    // enable TX, set full-duplex mode, NO padding, CRC appending and huge enabled
9193
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN | `ETH_MODER_HUGEN,
9194
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9195
    // check WB INT signal
9196
    if (wb_int !== 1'b0)
9197
    begin
9198
      test_fail("WB INT signal should not be set");
9199
      fail = fail + 1;
9200
    end
9201
 
9202
    // write to phy's control register for 100Mbps
9203
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
9204
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
9205
    speed = 100;
9206
 
9207
    i_length = (max_tmp - 5); // (max_tmp - 4) is the MAXFL limit
9208
    while (i_length <= (16'hFFFF - 4)) // (16'hFFFF - 4) is the limit
9209
    begin
9210
      $display("   i_length = %0d", i_length);
9211
      // Reset_tx_bd nable interrupt generation
9212
      // unmask interrupts
9213
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
9214
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9215
      // not detect carrier sense in FD and no collision
9216
      eth_phy.carrier_sense_tx_fd_detect(0);
9217
      eth_phy.collision(0);
9218
      // first destination address on ethernet PHY
9219
      eth_phy.set_tx_mem_addr(0);
9220
      // prepare BDs
9221
      if (num_of_bd == 0)
9222
      begin
9223
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9224
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9225
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9226
        set_tx_bd(3, 3, (16'hFFFF - 5), 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9227
        set_tx_bd(4, 4, (16'hFFFF - 4), 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9228
        set_tx_bd_wrap(4);
9229
        set_tx_bd_ready(0, 0);
9230
      end
9231
      else if (num_of_bd == 1)
9232
        set_tx_bd_ready(1, 1);
9233
      else if (num_of_bd == 2)
9234
        set_tx_bd_ready(2, 2);
9235
      else if (num_of_bd == 3)
9236
        set_tx_bd_ready(3, 3);
9237
      else if (num_of_bd == 4)
9238
        set_tx_bd_ready(4, 4);
9239
      // CHECK END OF TRANSMITION
9240
      check_tx_bd(num_of_bd, data);
9241
        wait (MTxEn === 1'b1); // start transmit
9242
      check_tx_bd(num_of_bd, data);
9243
        if (data[15] !== 1)
9244
        begin
9245
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
9246
          fail = fail + 1;
9247
        end
9248
        wait (MTxEn === 1'b0); // end transmit
9249
        while (data[15] === 1)
9250
        begin
9251
      check_tx_bd(num_of_bd, data);
9252
          @(posedge wb_clk);
9253
        end
9254
        repeat (1) @(posedge wb_clk);
9255
      // check length of a PACKET
9256
      $display("   eth_phy length = %0d", eth_phy.tx_len);
9257
      tmp_len = eth_phy.tx_len;
9258
      #1;
9259
      if (tmp_len != (i_length + 4))
9260
      begin
9261
        test_fail("Wrong length of the packet out from MAC");
9262
        fail = fail + 1;
9263
      end
9264
      // checking packet
9265
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
9266
      if (tmp > 0)
9267
      begin
9268
        test_fail("Wrong data of the transmitted packet");
9269
        fail = fail + 1;
9270
      end
9271
      // check transmited TX packet CRC
9272
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
9273
      if (tmp > 0)
9274
      begin
9275
        test_fail("Wrong CRC of the transmitted packet");
9276
        fail = fail + 1;
9277
      end
9278
      // check WB INT signal
9279
      if (wb_int !== 1'b1)
9280
      begin
9281
        `TIME; $display("*E WB INT signal should be set");
9282
        test_fail("WB INT signal should be set");
9283
        fail = fail + 1;
9284
      end
9285
      // check TX buffer descriptor of a packet
9286
      check_tx_bd(num_of_bd, data);
9287
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 4)) || // wrap bit
9288
           ((data[15:0] !== 16'h5800) && (num_of_bd < 4)) )   // without wrap bit
9289
      begin
9290
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
9291
        test_fail("TX buffer descriptor status is not correct");
9292
        fail = fail + 1;
9293
      end
9294
      // check interrupts
9295
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9296
      if ((data & `ETH_INT_TXB) !== 1'b1)
9297
      begin
9298
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
9299
        test_fail("Interrupt Transmit Buffer was not set");
9300
        fail = fail + 1;
9301
      end
9302
      if ((data & (~`ETH_INT_TXB)) !== 0)
9303
      begin
9304
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
9305
        test_fail("Other interrupts (except Transmit Buffer) were set");
9306
        fail = fail + 1;
9307
      end
9308
      // clear interrupts
9309
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9310
      // check WB INT signal
9311
      if (wb_int !== 1'b0)
9312
      begin
9313
        test_fail("WB INT signal should not be set");
9314
        fail = fail + 1;
9315
      end
9316
      // INTERMEDIATE DISPLAYS
9317
      $display("    ->packet with length %0d sent", (i_length + 4));
9318
      // set length (loop variable)
9319
      if ((num_of_bd < 2) || (num_of_bd >= 3))
9320
        i_length = i_length + 1;
9321
      else if (num_of_bd == 2)
9322
        i_length = (16'hFFFF - 5);
9323
      // the number of frame transmitted
9324
      num_of_frames = num_of_frames + 1;
9325
      num_of_bd = num_of_bd + 1;
9326
      @(posedge wb_clk);
9327
    end
9328
    // disable TX
9329
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
9330
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9331
    @(posedge wb_clk);
9332
    if(fail == 0)
9333
      test_ok;
9334
    else
9335
      fail = 0;
9336
  end
9337
 
9338
 
9339
  ////////////////////////////////////////////////////////////////////
9340
  ////                                                            ////
9341
  ////  Test IPG during Back-to-Back transmit at                  ////
9342
  ////  88 TX buffer decriptors ( 10Mbps ).                       ////
9343
  ////                                                            ////
9344
  ////////////////////////////////////////////////////////////////////
9345
  if (test_num == 18) // without and with padding
9346
  begin
9347
    // TEST 18: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 10Mbps )
9348
    test_name = "TEST 18: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 10Mbps )";
9349
    `TIME; $display("  TEST 18: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 10Mbps )");
9350
 
9351
    // reset MAC registers
9352
    hard_reset;
9353
    // reset MAC and MII LOGIC with soft reset
9354
    reset_mac;
9355
    reset_mii;
9356
    // set wb slave response
9357
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
9358
 
9359
    max_tmp = 0;
9360
    min_tmp = 0;
9361
    num_of_frames = 0;
9362
    num_of_bd = 0;
9363
    tmp_ipgt = 0;
9364
    // set 88 TX buffer descriptors - must be set before TX enable
9365
    wbm_write(`ETH_TX_BD_NUM, 32'h58, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9366
    // enable TX, set full-duplex mode, NO padding and CRC appending
9367
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9368
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9369
    // prepare two packets of MAXFL length
9370
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9371
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
9372
    min_tmp = tmp[31:16];
9373
    st_data = 8'h29;
9374
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
9375
    // check WB INT signal
9376
    if (wb_int !== 1'b0)
9377
    begin
9378
      test_fail("WB INT signal should not be set");
9379
      fail = fail + 1;
9380
    end
9381
 
9382
    // write to phy's control register for 10Mbps
9383
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
9384
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
9385
    speed = 10;
9386
 
9387
    i_length = (min_tmp - 4);
9388
    while (i_length < (max_tmp - 4))
9389
    begin
9390
      // disable TX, set full-duplex mode, NO padding and CRC appending
9391
      wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9392
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9393
      // set IPGT register
9394
      wbm_write(`ETH_IPGT, tmp_ipgt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9395
      // enable TX, set full-duplex mode, NO padding and CRC appending
9396
      wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9397
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9398
      // Reset_tx_bd enable interrupt generation
9399
      // unmask interrupts
9400
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
9401
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9402
      // not detect carrier sense in FD and no collision
9403
      eth_phy.carrier_sense_tx_fd_detect(0);
9404
      eth_phy.collision(0);
9405
      // first destination address on ethernet PHY
9406
      eth_phy.set_tx_mem_addr(0);
9407
      // prepare BDs
9408
      if (num_of_bd == 0)
9409
      begin
9410
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9411
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9412
        set_tx_bd_wrap(1);
9413
        set_tx_bd_ready(0, 0);
9414
        set_tx_bd_ready(1, 1);
9415
      end
9416
      // CHECK END OF TWO TRANSMITIONs
9417
      // wait for first transmit to end
9418
      check_tx_bd(num_of_bd, data);
9419
      wait (MTxEn === 1'b1); // start transmit
9420
      if (data[15] !== 1)
9421
      begin
9422
        test_fail("Wrong buffer descriptor's ready bit read out from MAC");
9423
        fail = fail + 1;
9424
      end
9425
      wait (MTxEn === 1'b0); // end transmit
9426
      num_of_frames = num_of_frames + 1;
9427
      num_of_bd = num_of_bd + 1;
9428
      #Tp;
9429
      // destination address on ethernet PHY
9430
      eth_phy.set_tx_mem_addr(0);
9431
      i1 = 0;
9432
      i2 = 0;
9433
      // count IPG clock periods
9434
      fork
9435
        begin
9436
          wait (MTxEn === 1'b1); // start second transmit
9437
          #Tp;
9438
          disable count_rising;
9439
          disable count_falling;
9440
        end
9441
        begin: count_rising
9442
          forever
9443
          begin
9444
            @(posedge mtx_clk);
9445
            i1 = i1 + 1;
9446
            #Tp;
9447
          end
9448
        end
9449
        begin: count_falling
9450
          forever
9451
          begin
9452
            @(negedge mtx_clk);
9453
            i2 = i2 + 1;
9454
            #Tp;
9455
          end
9456
        end
9457
      join
9458
      // check IPG length - INTERMEDIATE DISPLAYS
9459
      if((i1 == i2) && (i1 >= (tmp_ipgt + 3)))
9460
      begin
9461
        $display("    ->IPG with %0d mtx_clk periods (min %0d) between packets with lengths %0d and %0d checked",
9462
                  i1, (tmp_ipgt + 3), (i_length + 4), (i_length + 4 + 1));
9463
      end
9464
      else
9465
      begin
9466
        `TIME; $display("*E IPG is not correct: (%0d + %0d) / 2, requested: %d", i1, i2, (tmp_ipgt + 3));
9467
        fail = fail + 1;
9468
        test_fail("IPG is not correct");
9469
      end
9470
      // wait for second transmit to end
9471
      wait (MTxEn === 1'b0); // end second transmit
9472
      while (data[15] === 1)
9473
      begin
9474
        check_tx_bd(num_of_bd, data);
9475
        @(posedge wb_clk);
9476
      end
9477
      repeat (1) @(posedge wb_clk);
9478
      // check length of a second PACKET
9479
      tmp_len = eth_phy.tx_len;
9480
      #1;
9481
      if (tmp_len != (i_length + 4 + 1))
9482
      begin
9483
        test_fail("Wrong length of second packet out from MAC");
9484
        fail = fail + 1;
9485
      end
9486
      // checking second packet
9487
      check_tx_packet(`MEMORY_BASE, 0, (i_length + 1), tmp);
9488
      if (tmp > 0)
9489
      begin
9490
        test_fail("Wrong data of second transmitted packet");
9491
        fail = fail + 1;
9492
      end
9493
      // check second transmited TX packet CRC
9494
      check_tx_crc(0, (i_length + 1), 1'b0, tmp); // length without CRC
9495
      if (tmp > 0)
9496
      begin
9497
        test_fail("Wrong CRC of second transmitted packet");
9498
        fail = fail + 1;
9499
      end
9500
      // check WB INT signal
9501
      if (wb_int !== 1'b1)
9502
      begin
9503
        `TIME; $display("*E WB INT signal should be set");
9504
        test_fail("WB INT signal should be set");
9505
        fail = fail + 1;
9506
      end
9507
      // check TX buffer descriptor of a packet
9508
      check_tx_bd(num_of_bd, data);
9509
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
9510
           ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
9511
      begin
9512
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
9513
        test_fail("TX buffer descriptor status is not correct");
9514
        fail = fail + 1;
9515
      end
9516
      // check interrupts
9517
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9518
      if ((data & `ETH_INT_TXB) !== 1'b1)
9519
      begin
9520
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
9521
        test_fail("Interrupt Transmit Buffer was not set");
9522
        fail = fail + 1;
9523
      end
9524
      if ((data & (~`ETH_INT_TXB)) !== 0)
9525
      begin
9526
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
9527
        test_fail("Other interrupts (except Transmit Buffer) were set");
9528
        fail = fail + 1;
9529
      end
9530
      // clear interrupts
9531
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9532
      // check WB INT signal
9533
      if (wb_int !== 1'b0)
9534
      begin
9535
        test_fail("WB INT signal should not be set");
9536
        fail = fail + 1;
9537
      end
9538
      // set length (LOOP variable)
9539
      if ((tmp_ipgt + 3) < 130) // tmp_ipgt < 124
9540
        i_length = i_length + 2;
9541
      else
9542
        i_length = (max_tmp - 4);
9543
      // set IPGT
9544
      if ((tmp_ipgt + 3) < 10)
9545
        tmp_ipgt = tmp_ipgt + 1;
9546
      else if ((tmp_ipgt + 3) < 24)
9547
        tmp_ipgt = tmp_ipgt + 7;
9548
      else if ((tmp_ipgt + 3) == 24)
9549
        tmp_ipgt = 38 - 3;
9550
      else if ((tmp_ipgt + 3) == 38)
9551
        tmp_ipgt = 72 - 3;
9552
      else if ((tmp_ipgt + 3) == 72)
9553
        tmp_ipgt = 130 - 3; // 124 - 3
9554
      // the number of frame transmitted
9555
      num_of_frames = num_of_frames + 1;
9556
      num_of_bd = 0;
9557
      @(posedge wb_clk);
9558
    end
9559
    // disable TX
9560
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
9561
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9562
    @(posedge wb_clk);
9563
    if(fail == 0)
9564
      test_ok;
9565
    else
9566
      fail = 0;
9567
  end
9568
 
9569
 
9570
  ////////////////////////////////////////////////////////////////////
9571
  ////                                                            ////
9572
  ////  Test IPG during Back-to-Back transmit at                  ////
9573
  ////  88 TX buffer decriptors ( 100Mbps ).                      ////
9574
  ////                                                            ////
9575
  ////////////////////////////////////////////////////////////////////
9576
  if (test_num == 19) // without and with padding
9577
  begin
9578
    // TEST 19: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 100Mbps )
9579
    test_name = "TEST 19: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 100Mbps )";
9580
    `TIME; $display("  TEST 19: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 100Mbps )");
9581
 
9582
    // reset MAC registers
9583
    hard_reset;
9584
    // reset MAC and MII LOGIC with soft reset
9585
    reset_mac;
9586
    reset_mii;
9587
    // set wb slave response
9588
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
9589
 
9590
    max_tmp = 0;
9591
    min_tmp = 0;
9592
    num_of_frames = 0;
9593
    num_of_bd = 0;
9594
    tmp_ipgt = 0;
9595
    // set 88 TX buffer descriptors - must be set before TX enable
9596
    wbm_write(`ETH_TX_BD_NUM, 32'h58, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9597
    // enable TX, set full-duplex mode, NO padding and CRC appending
9598
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9599
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9600
    // prepare two packets of MAXFL length
9601
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9602
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
9603
    min_tmp = tmp[31:16];
9604
    st_data = 8'h29;
9605
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
9606
    // check WB INT signal
9607
    if (wb_int !== 1'b0)
9608
    begin
9609
      test_fail("WB INT signal should not be set");
9610
      fail = fail + 1;
9611
    end
9612
 
9613
    // write to phy's control register for 100Mbps
9614
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
9615
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
9616
    speed = 100;
9617
 
9618
    i_length = (min_tmp - 4);
9619
    while (i_length < (max_tmp - 4))
9620
    begin
9621
      // disable TX, set full-duplex mode, NO padding and CRC appending
9622
      wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9623
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9624
      // set IPGT register
9625
      wbm_write(`ETH_IPGT, tmp_ipgt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9626
      // enable TX, set full-duplex mode, NO padding and CRC appending
9627
      wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9628
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9629
      // Reset_tx_bd enable interrupt generation
9630
      // unmask interrupts
9631
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
9632
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9633
      // not detect carrier sense in FD and no collision
9634
      eth_phy.carrier_sense_tx_fd_detect(0);
9635
      eth_phy.collision(0);
9636
      // first destination address on ethernet PHY
9637
      eth_phy.set_tx_mem_addr(0);
9638
      // prepare BDs
9639
      if (num_of_bd == 0)
9640
      begin
9641
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9642
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9643
        set_tx_bd_wrap(1);
9644
        set_tx_bd_ready(0, 0);
9645
        set_tx_bd_ready(1, 1);
9646
      end
9647
      // CHECK END OF TWO TRANSMITIONs
9648
      // wait for first transmit to end
9649
      check_tx_bd(num_of_bd, data);
9650
      wait (MTxEn === 1'b1); // start transmit
9651
      if (data[15] !== 1)
9652
      begin
9653
        test_fail("Wrong buffer descriptor's ready bit read out from MAC");
9654
        fail = fail + 1;
9655
      end
9656
      wait (MTxEn === 1'b0); // end transmit
9657
      num_of_frames = num_of_frames + 1;
9658
      num_of_bd = num_of_bd + 1;
9659
      #Tp;
9660
      // destination address on ethernet PHY
9661
      eth_phy.set_tx_mem_addr(0);
9662
      i1 = 0;
9663
      i2 = 0;
9664
      // count IPG clock periods
9665
      fork
9666
        begin
9667
          wait (MTxEn === 1'b1); // start second transmit
9668
          #Tp;
9669
          disable count_rising1;
9670
          disable count_falling1;
9671
        end
9672
        begin: count_rising1
9673
          forever
9674
          begin
9675
            @(posedge mtx_clk);
9676
            i1 = i1 + 1;
9677
            #Tp;
9678
          end
9679
        end
9680
        begin: count_falling1
9681
          forever
9682
          begin
9683
            @(negedge mtx_clk);
9684
            i2 = i2 + 1;
9685
            #Tp;
9686
          end
9687
        end
9688
      join
9689
      // check IPG length - INTERMEDIATE DISPLAYS
9690
      if((i1 == i2) && (i1 >= (tmp_ipgt + 3)))
9691
      begin
9692
        $display("    ->IPG with %0d mtx_clk periods (min %0d) between packets with lengths %0d and %0d checked",
9693
                  i1, (tmp_ipgt + 3), (i_length + 4), (i_length + 4 + 1));
9694
      end
9695
      else
9696
      begin
9697
        `TIME; $display("*E IPG is not correct: (%0d + %0d) / 2, requested: %d", i1, i2, (tmp_ipgt + 3));
9698
        fail = fail + 1;
9699
        test_fail("IPG is not correct");
9700
      end
9701
      // wait for second transmit to end
9702
      wait (MTxEn === 1'b0); // end second transmit
9703
      while (data[15] === 1)
9704
      begin
9705
        check_tx_bd(num_of_bd, data);
9706
        @(posedge wb_clk);
9707
      end
9708
      repeat (1) @(posedge wb_clk);
9709
      // check length of a second PACKET
9710
      tmp_len = eth_phy.tx_len;
9711
      #1;
9712
      if (tmp_len != (i_length + 4 + 1))
9713
      begin
9714
        test_fail("Wrong length of second packet out from MAC");
9715
        fail = fail + 1;
9716
      end
9717
      // checking second packet
9718
      check_tx_packet(`MEMORY_BASE, 0, (i_length + 1), tmp);
9719
      if (tmp > 0)
9720
      begin
9721
        test_fail("Wrong data of second transmitted packet");
9722
        fail = fail + 1;
9723
      end
9724
      // check second transmited TX packet CRC
9725
      check_tx_crc(0, (i_length + 1), 1'b0, tmp); // length without CRC
9726
      if (tmp > 0)
9727
      begin
9728
        test_fail("Wrong CRC of second transmitted packet");
9729
        fail = fail + 1;
9730
      end
9731
      // check WB INT signal
9732
      if (wb_int !== 1'b1)
9733
      begin
9734
        `TIME; $display("*E WB INT signal should be set");
9735
        test_fail("WB INT signal should be set");
9736
        fail = fail + 1;
9737
      end
9738
      // check TX buffer descriptor of a packet
9739
      check_tx_bd(num_of_bd, data);
9740
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
9741
           ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
9742
      begin
9743
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
9744
        test_fail("TX buffer descriptor status is not correct");
9745
        fail = fail + 1;
9746
      end
9747
      // check interrupts
9748
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9749
      if ((data & `ETH_INT_TXB) !== 1'b1)
9750
      begin
9751
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
9752
        test_fail("Interrupt Transmit Buffer was not set");
9753
        fail = fail + 1;
9754
      end
9755
      if ((data & (~`ETH_INT_TXB)) !== 0)
9756
      begin
9757
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
9758
        test_fail("Other interrupts (except Transmit Buffer) were set");
9759
        fail = fail + 1;
9760
      end
9761
      // clear interrupts
9762
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9763
      // check WB INT signal
9764
      if (wb_int !== 1'b0)
9765
      begin
9766
        test_fail("WB INT signal should not be set");
9767
        fail = fail + 1;
9768
      end
9769
      // set length (LOOP variable)
9770
      if ((tmp_ipgt + 3) < 130) // tmp_ipgt < 124
9771
        i_length = i_length + 2;
9772
      else
9773
        i_length = (max_tmp - 4);
9774
      // set IPGT
9775
      if ((tmp_ipgt + 3) < 10)
9776
        tmp_ipgt = tmp_ipgt + 1;
9777
      else if ((tmp_ipgt + 3) < 24)
9778
        tmp_ipgt = tmp_ipgt + 7;
9779
      else if ((tmp_ipgt + 3) == 24)
9780
        tmp_ipgt = 38 - 3;
9781
      else if ((tmp_ipgt + 3) == 38)
9782
        tmp_ipgt = 72 - 3;
9783
      else if ((tmp_ipgt + 3) == 72)
9784
        tmp_ipgt = 130 - 3; // 124 - 3
9785
      // the number of frame transmitted
9786
      num_of_frames = num_of_frames + 1;
9787
      num_of_bd = 0;
9788
      @(posedge wb_clk);
9789
    end
9790
    // disable TX
9791
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
9792
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9793
    @(posedge wb_clk);
9794
    if(fail == 0)
9795
      test_ok;
9796
    else
9797
      fail = 0;
9798
  end
9799
 
9800
 
9801
  ////////////////////////////////////////////////////////////////////
9802
  ////                                                            ////
9803
  ////  Test transmit packets after TX under-run on each packet's ////
9804
  ////  byte at 2 TX buffer decriptors ( 10Mbps ).                ////
9805
  ////                                                            ////
9806
  ////////////////////////////////////////////////////////////////////
9807
  if (test_num == 20) // without padding
9808
  begin
9809
    // TEST 20: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 10Mbps )
9810
    test_name = "TEST 20: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 10Mbps )";
9811
    `TIME;
9812
    $display("  TEST 20: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 10Mbps )");
9813
 
9814
    // reset MAC registers
9815
    hard_reset;
9816
    // reset MAC and MII LOGIC with soft reset
9817
    reset_mac;
9818
    reset_mii;
9819
    // set wb slave response
9820
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
9821
 
9822
    max_tmp = 0;
9823
    min_tmp = 0;
9824
    // set 2 TX buffer descriptors - must be set before TX enable
9825
    wbm_write(`ETH_TX_BD_NUM, 32'h2, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9826
    // enable TX, set full-duplex mode, NO padding and CRC appending
9827
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN |/* `ETH_MODER_PAD |*/ `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9828
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9829
    // prepare a packet of MAXFL length
9830
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9831
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
9832
    min_tmp = tmp[31:16];
9833
    st_data = 8'h99;
9834
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
9835
    // read IPG value
9836
    wbm_read(`ETH_IPGT, tmp_ipgt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9837
    // check WB INT signal
9838
    if (wb_int !== 1'b0)
9839
    begin
9840
      test_fail("WB INT signal should not be set");
9841
      fail = fail + 1;
9842
    end
9843
 
9844
    // write to phy's control register for 10Mbps
9845
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
9846
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
9847
    speed = 10;
9848
 
9849 223 tadejm
    num_of_frames = 40; // (0..3) => start under-run on first word
9850 209 tadejm
    num_of_bd = 0;
9851
    i_data = 3; // (3) => one BYTE read in first word - FIRST byte
9852
    i_length = (min_tmp + 4);
9853
    while (i_length < (max_tmp - 4))
9854
    begin
9855
      // Reset_tx_bd enable interrupt generation
9856
      // unmask interrupts
9857
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
9858
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9859
      // not detect carrier sense in FD and no collision
9860
      eth_phy.carrier_sense_tx_fd_detect(0);
9861
      eth_phy.collision(0);
9862
      // first destination address on ethernet PHY
9863
      eth_phy.set_tx_mem_addr(0);
9864
      // prepare BDs
9865
      if (num_of_bd == 0)
9866
      begin
9867
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_data[1:0]));
9868
        set_tx_bd(1, 1, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9869
        set_tx_bd_wrap(1);
9870
        // set wb slave response: ACK (response), wbs_waits[2:0] (waits before response), 
9871
        //                       wbs_retries[7:0] (RTYs before ACK if RTY response selected)
9872 223 tadejm
        #1 wb_slave.cycle_response(`ACK_RESPONSE, 3'h0, 8'h0);
9873 209 tadejm
        set_tx_bd_ready(1, 1);
9874
        set_tx_bd_ready(0, 0);
9875
      end
9876
      // frame under-run checking
9877
      frame_started = 0;
9878
      frame_ended = 0;
9879
      wait_for_frame = 0;
9880
      fork
9881
        begin
9882
          // for every 4 frames bytes 1, 2, 3 and 4 respectively are read in first word => 1 ACK
9883
          // in other words 4 bytes are read, since length is MINFL => num_of_frames[31:2] ACKs
9884
          repeat ((num_of_frames[31:2] + 1'b1)) @(posedge eth_ma_wb_ack_i);
9885
          @(negedge eth_ma_wb_ack_i); // wait for last ACK to finish
9886
          // set wb slave response: ACK (response), wbs_waits[2:0] (waits before response), 
9887
          //                       wbs_retries[7:0] (RTYs before ACK if RTY response selected)
9888 223 tadejm
          #1 wb_slave.cycle_response(`NO_RESPONSE, 3'h0, 8'hFF);
9889 209 tadejm
          // wait for synchronization and some additional clocks
9890
          wait_for_frame = 1;
9891
          // wait for frame
9892
          wait ((wait_for_frame == 0) || (frame_started == 1))
9893
          if ((wait_for_frame == 0) && (frame_started == 0)) // frame didn't start
9894
          begin
9895
            disable check_fr;
9896
          end
9897
          else if ((wait_for_frame == 1) && (frame_started == 1)) // frame started
9898
          begin
9899
            disable wait_fr;
9900
            wait (frame_ended == 1);
9901
          end
9902
          repeat (2) @(posedge wb_clk);
9903
          // set wb slave response: ACK (response), wbs_waits[2:0] (waits before response), 
9904
          //                       wbs_retries[7:0] (RTYs before ACK if RTY response selected)
9905 223 tadejm
          wb_slave.cycle_response(`ACK_RESPONSE, 3'h0, 8'h0);
9906 209 tadejm
        end
9907
        begin: wait_fr
9908
          wait (wait_for_frame == 1)
9909
          begin
9910
            // wait for synchronization and some additional clocks
9911
            repeat (3) @(posedge wb_clk);
9912
            repeat (2 * tmp_ipgt) @(posedge mtx_clk);
9913
            repeat (2) @(posedge wb_clk);
9914
            repeat (2) @(posedge mtx_clk);
9915
            wait_for_frame = 0;
9916
          end
9917
        end
9918
        begin: check_fr
9919
          // wait for frame to start
9920
          @(posedge MTxEn);
9921
          frame_started = 1;
9922 223 tadejm
`TIME; $display("  Under-run (on %0d. byte) frame started", (num_of_frames + 1));
9923 209 tadejm
          // wait for frame to end due to under-run
9924
          @(negedge MTxEn);
9925
          frame_ended = 1;
9926 223 tadejm
`TIME; $display("  Under-run frame ended");
9927 209 tadejm
        end
9928
      join
9929
      // wait for first transmit to end, if under-run didn't happen
9930
      if (frame_ended == 0)
9931
      begin
9932
        // WAIT FOR FIRST TRANSMIT
9933
        check_tx_bd(num_of_bd, data);
9934
        wait (MTxEn === 1'b1); // start first transmit
9935
        if (data[15] !== 1)
9936
        begin
9937
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
9938
          fail = fail + 1;
9939
        end
9940
        wait (MTxEn === 1'b0); // end first transmit
9941
        while (data[15] === 1)
9942
        begin
9943
          check_tx_bd(num_of_bd, data);
9944
          @(posedge wb_clk);
9945
        end
9946
        repeat (1) @(posedge wb_clk);
9947
        // CHECK FIRST FRAME
9948
        // check length of a first PACKET
9949
        tmp_len = eth_phy.tx_len;
9950
        #1;
9951
        if (tmp_len != (i_length + 4))
9952
        begin
9953 223 tadejm
          `TIME; $display("*E Wrong length of first packet out from MAC");
9954
          test_fail("Wrong length of first packet out from MAC");
9955 209 tadejm
          fail = fail + 1;
9956
        end
9957
        // checking first packet
9958
        check_tx_packet((`MEMORY_BASE + i_data[1:0]), 0, (i_length), tmp);
9959
        if (tmp > 0)
9960
        begin
9961 223 tadejm
          `TIME; $display("*E Wrong data of first transmitted packet");
9962
          test_fail("Wrong data of first transmitted packet");
9963 209 tadejm
          fail = fail + 1;
9964
        end
9965
        // check first transmited TX packet CRC
9966
        check_tx_crc(0, (i_length), 1'b0, tmp); // length without CRC
9967
        if (tmp > 0)
9968
        begin
9969 223 tadejm
          `TIME; $display("*E Wrong CRC of first transmitted packet");
9970
          test_fail("Wrong CRC of first transmitted packet");
9971 209 tadejm
          fail = fail + 1;
9972
        end
9973
        // check WB INT signal
9974
        if (wb_int !== 1'b1)
9975
        begin
9976
          `TIME; $display("*E WB INT signal should be set");
9977
          test_fail("WB INT signal should be set");
9978
          fail = fail + 1;
9979
        end
9980
        // check TX buffer descriptor of a packet
9981
        check_tx_bd(num_of_bd, data);
9982
        if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
9983
             ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
9984
        begin
9985
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
9986
          test_fail("TX buffer descriptor status is not correct");
9987
          fail = fail + 1;
9988
        end
9989
        // check interrupts
9990
        wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9991
        if ((data & `ETH_INT_TXB) !== 1'b1)
9992
        begin
9993
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
9994
          test_fail("Interrupt Transmit Buffer was not set");
9995
          fail = fail + 1;
9996
        end
9997
        if ((data & (~`ETH_INT_TXB)) !== 0)
9998
        begin
9999
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
10000
          test_fail("Other interrupts (except Transmit Buffer) were set");
10001
          fail = fail + 1;
10002
        end
10003
        // clear interrupts
10004
        wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10005
        // check WB INT signal
10006
        if (wb_int !== 1'b0)
10007
        begin
10008
          test_fail("WB INT signal should not be set");
10009
          fail = fail + 1;
10010
        end
10011
      end
10012 223 tadejm
      else
10013
      begin
10014
        // CHECK FIRST FRAME
10015
        // check length of a first PACKET
10016
        tmp_len = eth_phy.tx_len_err;
10017
        #1;
10018
        if (tmp_len != (num_of_frames + (4 - i_data)))
10019
        begin
10020
          `TIME; $display("*E Wrong length of first packet out from MAC");
10021
          test_fail("Wrong length of first packet out from MAC");
10022
          fail = fail + 1;
10023
        end
10024
        // checking first packet
10025
        check_tx_packet((`MEMORY_BASE + i_data[1:0]), 0, (num_of_frames), tmp);
10026
        if (tmp > 0)
10027
        begin
10028
          `TIME; $display("*E Wrong data of first transmitted packet");
10029
          test_fail("Wrong data of first transmitted packet");
10030
          fail = fail + 1;
10031
        end
10032
        // check WB INT signal
10033
        if (wb_int !== 1'b1)
10034
        begin
10035
          `TIME; $display("*E WB INT signal should be set");
10036
          test_fail("WB INT signal should be set");
10037
          fail = fail + 1;
10038
        end
10039
        // check TX buffer descriptor of a packet
10040
        check_tx_bd(num_of_bd, data);
10041
        if ( ((data[15:0] !== 16'h7900) && (num_of_bd == 1)) || // under-run, wrap bit
10042
             ((data[15:0] !== 16'h5900) && (num_of_bd < 1)) )   // under-run, without wrap bit
10043
        begin
10044
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
10045
          test_fail("TX buffer descriptor status is not correct");
10046
          fail = fail + 1;
10047
        end
10048
        // check interrupts
10049
        wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10050
        if ((data & `ETH_INT_TXE) !== 2'b10)
10051
        begin
10052
          `TIME; $display("*E Interrupt Transmit Error was not set, interrupt reg: %0h", data);
10053
          test_fail("Interrupt Transmit Buffer was not set");
10054
          fail = fail + 1;
10055
        end
10056
        if ((data & (~`ETH_INT_TXE)) !== 0)
10057
        begin
10058
          `TIME; $display("*E Other interrupts (except Transmit Error) were set, interrupt reg: %0h", data);
10059
          test_fail("Other interrupts (except Transmit Buffer) were set");
10060
          fail = fail + 1;
10061
        end
10062
        // clear interrupts
10063
        wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10064
        // check WB INT signal
10065
        if (wb_int !== 1'b0)
10066
        begin
10067
          test_fail("WB INT signal should not be set");
10068
          fail = fail + 1;
10069
        end
10070
      end
10071 209 tadejm
      num_of_bd = num_of_bd + 1;
10072
      // destination address on ethernet PHY
10073
      eth_phy.set_tx_mem_addr(0);
10074 223 tadejm
      // WAIT FOR SECOND TRANSMIT
10075 209 tadejm
      check_tx_bd(num_of_bd, data);
10076
      wait (MTxEn === 1'b1); // start first transmit
10077
      if (data[15] !== 1)
10078
      begin
10079
        test_fail("Wrong buffer descriptor's ready bit read out from MAC");
10080
        fail = fail + 1;
10081
      end
10082
      wait (MTxEn === 1'b0); // end first transmit
10083
      while (data[15] === 1)
10084
      begin
10085
        check_tx_bd(num_of_bd, data);
10086
        @(posedge wb_clk);
10087
      end
10088
      repeat (1) @(posedge wb_clk);
10089
      // CHECK SECOND FRAME
10090
      // check length of a second PACKET
10091 223 tadejm
if (frame_ended == 1'b1)
10092
begin
10093
`TIME; $display("  Second frame after under-run ended");
10094
end
10095 209 tadejm
      tmp_len = eth_phy.tx_len;
10096
      #1;
10097
      if (tmp_len != (i_length + 4))
10098
      begin
10099 223 tadejm
        `TIME; $display("*E Wrong length of second packet out from MAC");
10100 209 tadejm
        test_fail("Wrong length of second packet out from MAC");
10101
        fail = fail + 1;
10102
      end
10103
      // checking second packet
10104
      check_tx_packet(`MEMORY_BASE, 0, (i_length), tmp);
10105
      if (tmp > 0)
10106
      begin
10107 223 tadejm
        `TIME; $display("*E Wrong data of second transmitted packet");
10108 209 tadejm
        test_fail("Wrong data of second transmitted packet");
10109
        fail = fail + 1;
10110
      end
10111
      // check second transmited TX packet CRC
10112
      check_tx_crc(0, (i_length), 1'b0, tmp); // length without CRC
10113
      if (tmp > 0)
10114
      begin
10115 223 tadejm
        `TIME; $display("*E Wrong CRC of second transmitted packet");
10116 209 tadejm
        test_fail("Wrong CRC of second transmitted packet");
10117
        fail = fail + 1;
10118
      end
10119
      // check WB INT signal
10120
      if (wb_int !== 1'b1)
10121
      begin
10122
        `TIME; $display("*E WB INT signal should be set");
10123
        test_fail("WB INT signal should be set");
10124
        fail = fail + 1;
10125
      end
10126
      // check TX buffer descriptor of a packet
10127
      check_tx_bd(num_of_bd, data);
10128
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
10129
           ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
10130
      begin
10131
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
10132
        test_fail("TX buffer descriptor status is not correct");
10133
        fail = fail + 1;
10134
      end
10135
      // check interrupts
10136
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10137
      if ((data & `ETH_INT_TXB) !== 1'b1)
10138
      begin
10139
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
10140
        test_fail("Interrupt Transmit Buffer was not set");
10141
        fail = fail + 1;
10142
      end
10143
      if ((data & (~`ETH_INT_TXB)) !== 0)
10144
      begin
10145
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
10146
        test_fail("Other interrupts (except Transmit Buffer) were set");
10147
        fail = fail + 1;
10148
      end
10149
      // clear interrupts
10150
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10151
      // check WB INT signal
10152
      if (wb_int !== 1'b0)
10153
      begin
10154
        test_fail("WB INT signal should not be set");
10155
        fail = fail + 1;
10156
      end
10157
      // set initial value
10158
      i_data = i_data - 1;
10159
      // the number of frame transmitted
10160
      num_of_frames = num_of_frames + 1;
10161
      num_of_bd = 0;
10162
      // set length (LOOP variable)
10163 223 tadejm
      if (num_of_frames == i_length + 4) // 64 => this was last Byte (1st .. 64th) when i_length = min_tmp - 4
10164 209 tadejm
        i_length = (max_tmp - 4);
10165
      @(posedge wb_clk);
10166
    end
10167
    // disable TX
10168
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
10169
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10170
    @(posedge wb_clk);
10171
    if(fail == 0)
10172
      test_ok;
10173
    else
10174
      fail = 0;
10175
  end
10176
 
10177
 
10178
  ////////////////////////////////////////////////////////////////////
10179
  ////                                                            ////
10180
  ////  Test transmit packets after TX under-run on each packet's ////
10181
  ////  byte at 2 TX buffer decriptors ( 100Mbps ).               ////
10182
  ////                                                            ////
10183
  ////////////////////////////////////////////////////////////////////
10184
  if (test_num == 21) // without padding
10185
  begin
10186
    // TEST 21: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 100Mbps )
10187
    test_name = "TEST 21: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 100Mbps )";
10188
    `TIME;
10189
    $display("  TEST 21: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 100Mbps )");
10190
 
10191
    // reset MAC registers
10192
    hard_reset;
10193
    // reset MAC and MII LOGIC with soft reset
10194
    reset_mac;
10195
    reset_mii;
10196
    // set wb slave response
10197
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
10198
 
10199
    max_tmp = 0;
10200
    min_tmp = 0;
10201
    // set 2 TX buffer descriptors - must be set before TX enable
10202
    wbm_write(`ETH_TX_BD_NUM, 32'h2, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10203
    // enable TX, set full-duplex mode, NO padding and CRC appending
10204
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN |/* `ETH_MODER_PAD |*/ `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
10205
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10206
    // prepare a packet of MAXFL length
10207
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10208
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
10209
    min_tmp = tmp[31:16];
10210
    st_data = 8'h99;
10211
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
10212
    // read IPG value
10213
    wbm_read(`ETH_IPGT, tmp_ipgt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10214
    // check WB INT signal
10215
    if (wb_int !== 1'b0)
10216
    begin
10217
      test_fail("WB INT signal should not be set");
10218
      fail = fail + 1;
10219
    end
10220
 
10221
    // write to phy's control register for 100Mbps
10222
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
10223
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
10224
    speed = 100;
10225
 
10226
    num_of_frames = 0; // (0..3) => start under-run on first word
10227
    num_of_bd = 0;
10228
    i_data = 3; // (3) => one BYTE read in first word - FIRST byte
10229
    i_length = (min_tmp + 4);
10230
    while (i_length < (max_tmp - 4))
10231
    begin
10232
      // Reset_tx_bd enable interrupt generation
10233
      // unmask interrupts
10234
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
10235
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10236
      // not detect carrier sense in FD and no collision
10237
      eth_phy.carrier_sense_tx_fd_detect(0);
10238
      eth_phy.collision(0);
10239
      // first destination address on ethernet PHY
10240
      eth_phy.set_tx_mem_addr(0);
10241
      // prepare BDs
10242
      if (num_of_bd == 0)
10243
      begin
10244
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_data[1:0]));
10245
        set_tx_bd(1, 1, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
10246
        set_tx_bd_wrap(1);
10247
        // set wb slave response: ACK (response), wbs_waits[2:0] (waits before response), 
10248
        //                       wbs_retries[7:0] (RTYs before ACK if RTY response selected)
10249
        #1 wb_slave.cycle_response(`ACK_RESPONSE, 3'h2, 8'h0);
10250
        set_tx_bd_ready(1, 1);
10251
        set_tx_bd_ready(0, 0);
10252
      end
10253
      // frame under-run checking
10254
      frame_started = 0;
10255
      frame_ended = 0;
10256
      wait_for_frame = 0;
10257
      fork
10258
        begin
10259
          // for every 4 frames bytes 1, 2, 3 and 4 respectively are read in first word => 1 ACK
10260
          // in other words 4 bytes are read, since length is MINFL => num_of_frames[31:2] ACKs
10261
          repeat ((num_of_frames[31:2] + 1'b1)) @(posedge eth_ma_wb_ack_i);
10262
          @(negedge eth_ma_wb_ack_i); // wait for last ACK to finish
10263
          // set wb slave response: ACK (response), wbs_waits[2:0] (waits before response), 
10264
          //                       wbs_retries[7:0] (RTYs before ACK if RTY response selected)
10265
          #1 wb_slave.cycle_response(`NO_RESPONSE, 3'h7, 8'hFF);
10266
          // wait for synchronization and some additional clocks
10267
          wait_for_frame = 1;
10268
          // wait for frame
10269
          wait ((wait_for_frame == 0) || (frame_started == 1))
10270
          if ((wait_for_frame == 0) && (frame_started == 0)) // frame didn't start
10271
          begin
10272
            disable check_fr1;
10273
          end
10274
          else if ((wait_for_frame == 1) && (frame_started == 1)) // frame started
10275
          begin
10276
            disable wait_fr1;
10277
            wait (frame_ended == 1);
10278
          end
10279
          repeat (2) @(posedge wb_clk);
10280
          // set wb slave response: ACK (response), wbs_waits[2:0] (waits before response), 
10281
          //                       wbs_retries[7:0] (RTYs before ACK if RTY response selected)
10282
          wb_slave.cycle_response(`ACK_RESPONSE, 3'h2, 8'h0);
10283
        end
10284
        begin: wait_fr1
10285
          wait (wait_for_frame == 1)
10286
          begin
10287
            // wait for synchronization and some additional clocks
10288
            repeat (3) @(posedge wb_clk);
10289
            repeat (2 * tmp_ipgt) @(posedge mtx_clk);
10290
            repeat (2) @(posedge wb_clk);
10291
            repeat (2) @(posedge mtx_clk);
10292
            wait_for_frame = 0;
10293
          end
10294
        end
10295
        begin: check_fr1
10296
          // wait for frame to start
10297
          @(posedge MTxEn);
10298
          frame_started = 1;
10299
$display("  Under-run (on %0d. byte) frame started", (num_of_frames + 1));
10300
          // wait for frame to end due to under-run
10301
          @(negedge MTxEn);
10302
          frame_ended = 1;
10303
$display("  Under-run frame ended");
10304
        end
10305
      join
10306
      // wait for first transmit to end, if under-run didn't happen
10307
      if (frame_ended == 0)
10308
      begin
10309
        // WAIT FOR FIRST TRANSMIT
10310
        check_tx_bd(num_of_bd, data);
10311
        wait (MTxEn === 1'b1); // start first transmit
10312
        if (data[15] !== 1)
10313
        begin
10314
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
10315
          fail = fail + 1;
10316
        end
10317
        wait (MTxEn === 1'b0); // end first transmit
10318
        while (data[15] === 1)
10319
        begin
10320
          check_tx_bd(num_of_bd, data);
10321
          @(posedge wb_clk);
10322
        end
10323
        repeat (1) @(posedge wb_clk);
10324
        // CHECK FIRST FRAME
10325
        // check length of a first PACKET
10326
        tmp_len = eth_phy.tx_len;
10327
        #1;
10328
        if (tmp_len != (i_length + 4))
10329
        begin
10330
          test_fail("Wrong length of second packet out from MAC");
10331
          fail = fail + 1;
10332
        end
10333
        // checking first packet
10334
        check_tx_packet((`MEMORY_BASE + i_data[1:0]), 0, (i_length), tmp);
10335
        if (tmp > 0)
10336
        begin
10337
          test_fail("Wrong data of second transmitted packet");
10338
          fail = fail + 1;
10339
        end
10340
        // check first transmited TX packet CRC
10341
        check_tx_crc(0, (i_length), 1'b0, tmp); // length without CRC
10342
        if (tmp > 0)
10343
        begin
10344
          test_fail("Wrong CRC of second transmitted packet");
10345
          fail = fail + 1;
10346
        end
10347
        // check WB INT signal
10348
        if (wb_int !== 1'b1)
10349
        begin
10350
          `TIME; $display("*E WB INT signal should be set");
10351
          test_fail("WB INT signal should be set");
10352
          fail = fail + 1;
10353
        end
10354
        // check TX buffer descriptor of a packet
10355
        check_tx_bd(num_of_bd, data);
10356
        if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
10357
             ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
10358
        begin
10359
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
10360
          test_fail("TX buffer descriptor status is not correct");
10361
          fail = fail + 1;
10362
        end
10363
        // check interrupts
10364
        wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10365
        if ((data & `ETH_INT_TXB) !== 1'b1)
10366
        begin
10367
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
10368
          test_fail("Interrupt Transmit Buffer was not set");
10369
          fail = fail + 1;
10370
        end
10371
        if ((data & (~`ETH_INT_TXB)) !== 0)
10372
        begin
10373
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
10374
          test_fail("Other interrupts (except Transmit Buffer) were set");
10375
          fail = fail + 1;
10376
        end
10377
        // clear interrupts
10378
        wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10379
        // check WB INT signal
10380
        if (wb_int !== 1'b0)
10381
        begin
10382
          test_fail("WB INT signal should not be set");
10383
          fail = fail + 1;
10384
        end
10385
      end
10386
      num_of_bd = num_of_bd + 1;
10387
      // destination address on ethernet PHY
10388
      eth_phy.set_tx_mem_addr(0);
10389
      // WAIT FOR FIRST TRANSMIT
10390
      check_tx_bd(num_of_bd, data);
10391
      wait (MTxEn === 1'b1); // start first transmit
10392
      if (data[15] !== 1)
10393
      begin
10394
        test_fail("Wrong buffer descriptor's ready bit read out from MAC");
10395
        fail = fail + 1;
10396
      end
10397
      wait (MTxEn === 1'b0); // end first transmit
10398
      while (data[15] === 1)
10399
      begin
10400
        check_tx_bd(num_of_bd, data);
10401
        @(posedge wb_clk);
10402
      end
10403
      repeat (1) @(posedge wb_clk);
10404
      // CHECK SECOND FRAME
10405
      // check length of a second PACKET
10406
      tmp_len = eth_phy.tx_len;
10407
      #1;
10408
      if (tmp_len != (i_length + 4))
10409
      begin
10410
        test_fail("Wrong length of second packet out from MAC");
10411
        fail = fail + 1;
10412
      end
10413
      // checking second packet
10414
      check_tx_packet(`MEMORY_BASE, 0, (i_length), tmp);
10415
      if (tmp > 0)
10416
      begin
10417
        test_fail("Wrong data of second transmitted packet");
10418
        fail = fail + 1;
10419
      end
10420
      // check second transmited TX packet CRC
10421
      check_tx_crc(0, (i_length), 1'b0, tmp); // length without CRC
10422
      if (tmp > 0)
10423
      begin
10424
        test_fail("Wrong CRC of second transmitted packet");
10425
        fail = fail + 1;
10426
      end
10427
      // check WB INT signal
10428
      if (wb_int !== 1'b1)
10429
      begin
10430
        `TIME; $display("*E WB INT signal should be set");
10431
        test_fail("WB INT signal should be set");
10432
        fail = fail + 1;
10433
      end
10434
      // check TX buffer descriptor of a packet
10435
      check_tx_bd(num_of_bd, data);
10436
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
10437
           ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
10438
      begin
10439
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
10440
        test_fail("TX buffer descriptor status is not correct");
10441
        fail = fail + 1;
10442
      end
10443
      // check interrupts
10444
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10445
      if ((data & `ETH_INT_TXB) !== 1'b1)
10446
      begin
10447
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
10448
        test_fail("Interrupt Transmit Buffer was not set");
10449
        fail = fail + 1;
10450
      end
10451
      if ((data & (~`ETH_INT_TXB)) !== 0)
10452
      begin
10453
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
10454
        test_fail("Other interrupts (except Transmit Buffer) were set");
10455
        fail = fail + 1;
10456
      end
10457
      // clear interrupts
10458
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10459
      // check WB INT signal
10460
      if (wb_int !== 1'b0)
10461
      begin
10462
        test_fail("WB INT signal should not be set");
10463
        fail = fail + 1;
10464
      end
10465
      // set initial value
10466
      i_data = i_data - 1;
10467
      // the number of frame transmitted
10468
      num_of_frames = num_of_frames + 1;
10469
      num_of_bd = 0;
10470
      // set length (LOOP variable)
10471
      if (num_of_frames == i_length + 4) // 64 => this vas last Byte (1st .. 64th) when i_length = min_tmp - 4
10472
        i_length = (max_tmp - 4);
10473
      @(posedge wb_clk);
10474
    end
10475
    // disable TX
10476
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
10477
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10478
    @(posedge wb_clk);
10479
    if(fail == 0)
10480
      test_ok;
10481
    else
10482
      fail = 0;
10483
  end
10484
 
10485 194 tadej
end   //  for (test_num=start_task; test_num <= end_task; test_num=test_num+1)
10486 169 mohor
 
10487
end
10488
endtask // test_mac_full_duplex_transmit
10489
 
10490
 
10491 209 tadejm
task test_mac_full_duplex_receive;
10492
  input  [31:0]  start_task;
10493
  input  [31:0]  end_task;
10494
  integer        bit_start_1;
10495
  integer        bit_end_1;
10496
  integer        bit_start_2;
10497
  integer        bit_end_2;
10498
  integer        num_of_reg;
10499
  integer        num_of_frames;
10500
  integer        num_of_bd;
10501
  integer        i_addr;
10502
  integer        i_data;
10503
  integer        i_length;
10504
  integer        tmp_len;
10505
  integer        tmp_bd;
10506
  integer        tmp_bd_num;
10507
  integer        tmp_data;
10508
  integer        tmp_ipgt;
10509
  integer        test_num;
10510
  reg    [31:0]  tx_bd_num;
10511
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_data;
10512
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_tmp_data;
10513
  integer        i;
10514
  integer        i1;
10515
  integer        i2;
10516
  integer        i3;
10517
  integer        fail;
10518
  integer        speed;
10519
  reg            frame_started;
10520
  reg            frame_ended;
10521
  reg            wait_for_frame;
10522 243 tadejm
  reg            check_frame;
10523
  reg            stop_checking_frame;
10524
  reg            first_fr_received;
10525 209 tadejm
  reg    [31:0]  addr;
10526
  reg    [31:0]  data;
10527
  reg    [31:0]  tmp;
10528
  reg    [ 7:0]  st_data;
10529
  reg    [15:0]  max_tmp;
10530
  reg    [15:0]  min_tmp;
10531
begin
10532
// MAC FULL DUPLEX RECEIVE TEST
10533
test_heading("MAC FULL DUPLEX RECEIVE TEST");
10534
$display(" ");
10535
$display("MAC FULL DUPLEX RECEIVE TEST");
10536
fail = 0;
10537
 
10538
// reset MAC registers
10539
hard_reset;
10540
// reset MAC and MII LOGIC with soft reset
10541
reset_mac;
10542
reset_mii;
10543
// set wb slave response
10544
wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
10545
 
10546
  /*
10547
  TASKS for set and control TX buffer descriptors (also send packet - set_tx_bd_ready):
10548
  -------------------------------------------------------------------------------------
10549
  set_tx_bd
10550
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0], len[15:0], irq, pad, crc, txpnt[31:0]);
10551
  set_tx_bd_wrap
10552
    (tx_bd_num_end[6:0]);
10553
  set_tx_bd_ready
10554
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
10555
  check_tx_bd
10556
    (tx_bd_num_start[6:0], tx_bd_status[31:0]);
10557
  clear_tx_bd
10558
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
10559
 
10560
  TASKS for set and control RX buffer descriptors:
10561
  ------------------------------------------------
10562
  set_rx_bd
10563
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0], irq, rxpnt[31:0]);
10564
  set_rx_bd_wrap
10565
    (rx_bd_num_end[6:0]);
10566
  set_rx_bd_empty
10567
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
10568
  check_rx_bd
10569
    (rx_bd_num_end[6:0], rx_bd_status);
10570
  clear_rx_bd
10571
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
10572
 
10573
  TASKS for set and check TX packets:
10574
  -----------------------------------
10575
  set_tx_packet
10576
    (txpnt[31:0], len[15:0], eth_start_data[7:0]);
10577
  check_tx_packet
10578
    (txpnt_wb[31:0], txpnt_phy[31:0], len[15:0], failure[31:0]);
10579
 
10580
  TASKS for set and check RX packets:
10581
  -----------------------------------
10582
  set_rx_packet
10583
    (rxpnt[31:0], len[15:0], plus_nibble, d_addr[47:0], s_addr[47:0], type_len[15:0], start_data[7:0]);
10584
  check_rx_packet
10585
    (rxpnt_phy[31:0], rxpnt_wb[31:0], len[15:0], plus_nibble, successful_nibble, failure[31:0]);
10586
 
10587
  TASKS for append and check CRC to/of TX packet:
10588
  -----------------------------------------------
10589
  append_tx_crc
10590
    (txpnt_wb[31:0], len[15:0], negated_crc);
10591
  check_tx_crc
10592
    (txpnt_phy[31:0], len[15:0], negated_crc, failure[31:0]);
10593
 
10594
  TASK for append CRC to RX packet (CRC is checked together with check_rx_packet):
10595
  --------------------------------------------------------------------------------
10596
  append_rx_crc
10597
    (rxpnt_phy[31:0], len[15:0], plus_nibble, negated_crc);
10598
  */
10599
 
10600
//////////////////////////////////////////////////////////////////////
10601
////                                                              ////
10602
////  test_mac_full_duplex_receive:                               ////
10603
////                                                              ////
10604
////  0: Test no receive when all buffers are TX ( 10Mbps ).      ////
10605
////  1: Test no receive when all buffers are TX ( 100Mbps ).     ////
10606 243 tadejm
////  2: Test receive packet synchronization with receive         ////
10607
////     disable/enable ( 10Mbps ).                               ////
10608
////  3: Test receive packet synchronization with receive         ////
10609
////     disable/enable ( 100Mbps ).                              ////
10610
////  4: Test receive packets form MINFL to MAXFL sizes at        ////
10611 209 tadejm
////     one RX buffer decriptor ( 10Mbps ).                      ////
10612 243 tadejm
////  5: Test receive packets form MINFL to MAXFL sizes at        ////
10613 209 tadejm
////     one RX buffer decriptor ( 100Mbps ).                     ////
10614
////                                                              ////
10615
//////////////////////////////////////////////////////////////////////
10616
for (test_num = start_task; test_num <= end_task; test_num = test_num + 1)
10617
begin
10618
 
10619
  ////////////////////////////////////////////////////////////////////
10620
  ////                                                            ////
10621
  ////  Test no receive when all buffers are TX ( 10Mbps ).       ////
10622
  ////                                                            ////
10623
  ////////////////////////////////////////////////////////////////////
10624
  if (test_num == 0) // Test no receive when all buffers are TX ( 10Mbps ).
10625
  begin
10626
    // TEST 0: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 10Mbps )
10627
    test_name   = "TEST 0: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 10Mbps )";
10628
    `TIME; $display("  TEST 0: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 10Mbps )");
10629
 
10630
    // unmask interrupts
10631
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
10632
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10633
    // set all buffer descriptors to TX - must be set before RX enable
10634
    wbm_write(`ETH_TX_BD_NUM, 32'h80, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10635 223 tadejm
    // enable RX, set full-duplex mode, receive small, NO correct IFG
10636 209 tadejm
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
10637
              `ETH_MODER_PRO | `ETH_MODER_BRO,
10638
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10639
 
10640
    // write to phy's control register for 10Mbps
10641
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
10642
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
10643
    speed = 10;
10644
 
10645
    i = 0;
10646
    while (i < 128)
10647
    begin
10648
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
10649
      begin
10650
        set_rx_packet((i1 * 50), 10, 1'b0, 48'h1234_5678_8765, 48'h0011_2233_4455, 16'h0101, 8'h0);
10651
        append_rx_crc((i1 * 50), 10, 1'b0, 1'b0);
10652
        set_rx_bd(i1, i1, 1'b1, (`MEMORY_BASE + (i1 * 50)));
10653
      end
10654
      set_rx_bd_wrap(i);
10655
      set_rx_bd_empty(0, i);
10656
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
10657
      begin
10658
        #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, (i1 * 50), 14, 1'b0);
10659
        repeat(10) @(posedge mrx_clk);
10660
      end
10661
      @(posedge mrx_clk);
10662
      for (i2 = 0; i2 < 20; i2 = i2 + 1)
10663
      begin
10664
        check_rx_bd(0, tmp);
10665
        #1;
10666
        if (tmp[15] === 1'b0)
10667
        begin
10668
          test_fail("Receive should not start at all");
10669
          fail = fail + 1;
10670
          `TIME; $display("*E Receive of %d packets should not start at all - empty is 0", i);
10671
        end
10672
        if (tmp[7:0] !== 0)
10673
        begin
10674
          test_fail("Receive should not be finished since it should not start at all");
10675
          fail = fail + 1;
10676
          `TIME; $display("*E Receive of should not be finished since it should not start at all");
10677
        end
10678
        @(posedge wb_clk);
10679
      end
10680
      wbm_read(`ETH_INT, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10681
      if (tmp[6:0] !== 0)
10682
      begin
10683
        test_fail("Receive should not get INT since it should not start at all");
10684
        fail = fail + 1;
10685
        `TIME; $display("*E Receive of should not get INT since it should not start at all");
10686
      end
10687
      clear_rx_bd(0, i);
10688
      if ((i < 5) || (i > 124))
10689
        i = i + 1;
10690
      else
10691
        i = i + 120;
10692
    end
10693
    // disable RX
10694
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
10695
              `ETH_MODER_PRO | `ETH_MODER_BRO,
10696
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10697
    if(fail == 0)
10698
      test_ok;
10699
    else
10700
      fail = 0;
10701
  end
10702
 
10703
 
10704
  ////////////////////////////////////////////////////////////////////
10705
  ////                                                            ////
10706
  ////  Test no receive when all buffers are TX ( 100Mbps ).      ////
10707
  ////                                                            ////
10708
  ////////////////////////////////////////////////////////////////////
10709
  if (test_num == 1) // Test no receive when all buffers are TX ( 100Mbps ).
10710
  begin
10711
    // TEST 1: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 100Mbps )
10712
    test_name   = "TEST 1: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 100Mbps )";
10713
    `TIME; $display("  TEST 1: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 100Mbps )");
10714
 
10715
    // unmask interrupts
10716
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
10717
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10718
    // set all buffer descriptors to TX - must be set before RX enable
10719
    wbm_write(`ETH_TX_BD_NUM, 32'h80, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10720 223 tadejm
    // enable RX, set full-duplex mode, receive small, NO correct IFG
10721 209 tadejm
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
10722
              `ETH_MODER_PRO | `ETH_MODER_BRO,
10723
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10724
 
10725
    // write to phy's control register for 100Mbps
10726
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
10727
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
10728
    speed = 100;
10729
 
10730
    i = 0;
10731
    while (i < 128)
10732
    begin
10733
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
10734
      begin
10735
        set_rx_packet((i1 * 50), 10, 1'b0, 48'h1234_5678_8765, 48'h0011_2233_4455, 16'h0101, 8'h0);
10736
        append_rx_crc((i1 * 50), 10, 1'b0, 1'b0);
10737
        set_rx_bd(i1, i1, 1'b1, (`MEMORY_BASE + (i1 * 50)));
10738
      end
10739
      set_rx_bd_wrap(i);
10740
      set_rx_bd_empty(0, i);
10741
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
10742
      begin
10743
        #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, (i1 * 50), 14, 1'b0);
10744
        repeat(10) @(posedge mrx_clk);
10745
      end
10746
      @(posedge mrx_clk);
10747
      for (i2 = 0; i2 < 20; i2 = i2 + 1)
10748
      begin
10749
        check_rx_bd(0, tmp);
10750
        #1;
10751
        if (tmp[15] === 1'b0)
10752
        begin
10753
          test_fail("Receive should not start at all");
10754
          fail = fail + 1;
10755
          `TIME; $display("*E Receive of %d packets should not start at all - empty is 0", i);
10756
        end
10757
        if (tmp[7:0] !== 0)
10758
        begin
10759
          test_fail("Receive should not be finished since it should not start at all");
10760
          fail = fail + 1;
10761
          `TIME; $display("*E Receive of should not be finished since it should not start at all");
10762
        end
10763
        @(posedge wb_clk);
10764
      end
10765
      wbm_read(`ETH_INT, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10766
      if (tmp[6:0] !== 0)
10767
      begin
10768
        test_fail("Receive should not get INT since it should not start at all");
10769
        fail = fail + 1;
10770
        `TIME; $display("*E Receive of should not get INT since it should not start at all");
10771
      end
10772
      clear_rx_bd(0, i);
10773
      if ((i < 5) || (i > 124))
10774
        i = i + 1;
10775
      else
10776
        i = i + 120;
10777
    end
10778
    // disable RX
10779
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
10780
              `ETH_MODER_PRO | `ETH_MODER_BRO,
10781
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10782
    if(fail == 0)
10783
      test_ok;
10784
    else
10785
      fail = 0;
10786
  end
10787
 
10788
 
10789
  ////////////////////////////////////////////////////////////////////
10790
  ////                                                            ////
10791 243 tadejm
  ////  Test receive packet synchronization with receive          ////
10792
  ////  disable/enable ( 10Mbps ).                                ////
10793
  ////                                                            ////
10794
  ////////////////////////////////////////////////////////////////////
10795
  if (test_num == 2) // Test no receive when all buffers are TX ( 10Mbps ).
10796
  begin
10797
    // TEST 2: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )
10798
    test_name   = "TEST 2: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )";
10799
    `TIME; $display("  TEST 2: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )");
10800
 
10801
    // unmask interrupts
10802
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
10803
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10804
    // set 10 RX buffer descriptor (8'h80 - 8'hA) - must be set before RX enable
10805
    wbm_write(`ETH_TX_BD_NUM, 32'h76, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10806
    // disable RX, set full-duplex mode, NO receive small, NO correct IFG
10807
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_IFG |
10808
              `ETH_MODER_PRO | `ETH_MODER_BRO,
10809
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10810
    // prepare two packets of MAXFL length
10811
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10812
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
10813
    min_tmp = tmp[31:16];
10814
    st_data = 8'h0F;
10815
    set_rx_packet(0, (min_tmp + 1), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
10816
    append_rx_crc(0, (min_tmp + 1), 1'b0, 1'b0);
10817
    st_data = 8'h1A;
10818
    set_rx_packet(max_tmp, (min_tmp + 1), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
10819
    append_rx_crc(max_tmp, (min_tmp + 1), 1'b0, 1'b0);
10820
    // check WB INT signal
10821
    if (wb_int !== 1'b0)
10822
    begin
10823
      test_fail("WB INT signal should not be set");
10824
      fail = fail + 1;
10825
    end
10826
 
10827
    // write to phy's control register for 10Mbps
10828
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
10829
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
10830
    speed = 10;
10831
 
10832
    frame_started = 0;
10833
    frame_ended = 0;
10834
    wait_for_frame = 0;
10835
    check_frame = 0;
10836
    stop_checking_frame = 0;
10837
    first_fr_received = 1; // at the beginning the first frame of each two will be received!
10838
 
10839
    num_of_frames = 0; // 
10840
    num_of_bd = 0;
10841
    i_length = (min_tmp + 1); // 5 bytes longer than MINFL
10842
    while (i_length < (max_tmp - 4))
10843
    begin
10844
      // choose generating carrier sense and collision 
10845
      case (num_of_frames[1:0])
10846
      2'h0: // Interrupt is generated
10847
      begin
10848
        // enable interrupt generation
10849
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
10850
        // not detect carrier sense in FD and no collision
10851
        eth_phy.no_carrier_sense_rx_fd_detect(0);
10852
        eth_phy.collision(0);
10853
      end
10854
      2'h1: // Interrupt is generated
10855
      begin
10856
        // enable interrupt generation
10857
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
10858
        // detect carrier sense in FD and no collision
10859
        eth_phy.no_carrier_sense_rx_fd_detect(1);
10860
        eth_phy.collision(0);
10861
      end
10862
      2'h2: // Interrupt is generated
10863
      begin
10864
        // disable interrupt generation
10865
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
10866
        // not detect carrier sense in FD and set collision
10867
        eth_phy.no_carrier_sense_rx_fd_detect(0);
10868
        eth_phy.collision(1);
10869
      end
10870
      default: // 2'h3: // Interrupt is generated
10871
      begin
10872
        // disable interrupt generation
10873
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
10874
        // detect carrier sense in FD and set collision
10875
        eth_phy.no_carrier_sense_rx_fd_detect(1);
10876
        eth_phy.collision(1);
10877
      end
10878
      endcase
10879
      // set wrap bit
10880
      set_rx_bd_wrap(118);
10881
      set_rx_bd_empty(118, 118);
10882
      check_frame = 0;
10883
      stop_checking_frame = 0;
10884
      tmp_data = 0;
10885
      fork
10886
        begin // enable RX MAC on first of each two packets - every second should be recived normaly
10887
          if (num_of_frames[0] == 1'b0)
10888
          begin
10889
            repeat(1) @(posedge wb_clk);
10890
            if (num_of_frames[1] == 1'b0)
10891
            begin
10892
              repeat (num_of_frames[31:2]) @(posedge mrx_clk); // for every (second) frame enable receiver one clock later
10893
            end
10894
            else
10895
            begin
10896
              @(posedge mrx_clk);
10897
              repeat (num_of_frames[31:2]) @(negedge mrx_clk); // for every (second) frame enable receiver one clock later
10898
            end
10899
            // enable RX, set full-duplex mode, NO receive small, NO correct IFG
10900
            wbm_init_waits = 4'h0;
10901
            wbm_subseq_waits = 4'h0;
10902
            #1 wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
10903
                      `ETH_MODER_PRO | `ETH_MODER_BRO,
10904
                      4'hF, 1, wbm_init_waits, wbm_subseq_waits); // write ASAP
10905
          end
10906
        end
10907
        begin // send a packet from PHY RX
10908
          repeat(1) @(posedge wb_clk); // wait for WB write when it is without delays
10909
          if (num_of_frames[1] == 1'b0)
10910
          begin
10911
            set_rx_addr_type(0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E);
10912
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
10913
          end
10914
          else
10915
          begin
10916
            set_rx_addr_type((max_tmp), 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E);
10917
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
10918
          end
10919
        end
10920
        begin: send_packet0
10921
          wait (MRxDV === 1'b1); // start transmit
10922
          wait (MRxDV === 1'b0); // end transmit
10923
          check_frame = 1;
10924
          repeat(10) @(posedge mrx_clk);
10925
          repeat(15) @(posedge wb_clk);
10926
          stop_checking_frame = 1;
10927
        end
10928
        begin // count WB clocks between ACK (negedge) and RX_DV (posedge) or vice-versa
10929
          @(posedge eth_sl_wb_ack_o or posedge MRxDV);
10930
          if ((eth_sl_wb_ack_o === 1'b1) && (MRxDV === 1'b1))
10931
          begin
10932
            tmp_data = 32'h8000_0001; // bit[31]==1 => 'posedge MRxDV' was before 'negedge eth_sl_wb_ack_o'
10933
          end
10934
          else if (MRxDV === 1'b1)
10935
          begin
10936
            while (eth_sl_wb_ack_o === 1'b0)
10937
            begin
10938
              @(posedge wb_clk);
10939
              tmp_data = tmp_data + 1;
10940
            end
10941
            tmp_data = tmp_data | 32'h8000_0000; // bit[31]==1 => 'posedge MRxDV' was before 'negedge eth_sl_wb_ack_o'
10942
          end
10943
          else if (eth_sl_wb_ack_o === 1'b1)
10944
          begin
10945
            @(posedge wb_clk); // wait for one clock => tmp_data 'becomes' 0
10946
            while (MRxDV === 1'b0)
10947
            begin
10948
              @(posedge wb_clk);
10949
              tmp_data = tmp_data + 1; // bit[31]==0 => 'negedge eth_sl_wb_ack_o' was equal or before 'posedge MRxDV'
10950
            end
10951
          end
10952
        end
10953
        begin // check packet
10954
          wait (check_frame == 1);
10955
          check_rx_bd(118, tmp_bd);
10956
          while ((tmp_bd[15] === 1) && (stop_checking_frame == 0))
10957
          begin
10958
            #1 check_rx_bd(118, tmp_bd);
10959
            @(posedge wb_clk);
10960
          end
10961
          if (num_of_frames[0] == 1'b0)
10962
          begin
10963
            if (tmp_bd[15] === 1)
10964
            begin
10965
              if (first_fr_received == 1)
10966
              begin
10967
                first_fr_received = 0;
10968
                $display("    %0d packets (without this one) are checked - packets are received by two in a set",
10969
                         num_of_frames); // +1 due to start with 0 AND -1 because this packet is excluded
10970
                $display("    From this moment:");
10971
                $display("    first one of two packets (including this one) is not accepted due to late RX enable");
10972
                if (tmp_data[31])
10973
                  $display("    ->RX enable set %0d WB clks after RX_DV", tmp_data[30:0]);
10974
                else
10975
                  $display("    ->RX enable set %0d WB clks before RX_DV", tmp_data[30:0]);
10976
              end
10977
            end
10978
          end
10979
          if (stop_checking_frame == 0)
10980
            disable send_packet0;
10981
        end
10982
      join
10983
      // ONLY IF packet was received!
10984
      if (tmp_bd[15] === 0)
10985
      begin
10986
        // check length of a PACKET
10987
        if (tmp_bd[31:16] != (i_length + 4))
10988
        begin
10989
          `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
10990
                          tmp_bd[31:16], (i_length + 4));
10991
          test_fail("Wrong length of the packet out from PHY");
10992
          fail = fail + 1;
10993
        end
10994
        // check received RX packet data and CRC
10995
        if (first_fr_received == 0) // if PREVIOUS RX buffer descriptor was not ready, pointer address is -1
10996
        begin
10997
          if (num_of_frames[1] == 1'b0)
10998
          begin
10999
            check_rx_packet(0, (`MEMORY_BASE + num_of_frames[1:0] - 1'b1), (i_length + 4), 1'b0, 1'b0, tmp);
11000
          end
11001
          else
11002
          begin
11003
            check_rx_packet(max_tmp, (`MEMORY_BASE + num_of_frames[1:0] - 1'b1), (i_length + 4), 1'b0, 1'b0, tmp);
11004
          end
11005
          if (tmp > 0)
11006
          begin
11007
            `TIME; $display("*E Wrong data of the received packet");
11008
            test_fail("Wrong data of the received packet");
11009
            fail = fail + 1;
11010
          end
11011
        end
11012
        else // if PREVIOUS RX buffer descriptor was ready
11013
        begin
11014
          if (num_of_frames[1] == 1'b0)
11015
          begin
11016
            check_rx_packet(0, (`MEMORY_BASE + num_of_frames[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
11017
          end
11018
          else
11019
          begin
11020
            check_rx_packet(max_tmp, (`MEMORY_BASE + num_of_frames[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
11021
          end
11022
          if (tmp > 0)
11023
          begin
11024
            `TIME; $display("*E Wrong data of the received packet");
11025
            test_fail("Wrong data of the received packet");
11026
            fail = fail + 1;
11027
          end
11028
        end
11029
      end
11030
      // check WB INT signal
11031
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11032
      begin
11033
        if (wb_int !== 1'b0)
11034
        begin
11035
          `TIME; $display("*E WB INT signal should not be set");
11036
          test_fail("WB INT signal should not be set");
11037
          fail = fail + 1;
11038
        end
11039
      end
11040
      else
11041
      begin
11042
        if (wb_int !== 1'b1)
11043
        begin
11044
          `TIME; $display("*E WB INT signal should be set");
11045
          test_fail("WB INT signal should be set");
11046
          fail = fail + 1;
11047
        end
11048
      end
11049
      // check RX buffer descriptor of a packet - only 15 LSBits
11050
      check_rx_bd(118, data);
11051
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11052
      begin
11053
        if (data[15:0] !== 16'hE000)
11054
        begin
11055
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11056
          test_fail("RX buffer descriptor status is not correct");
11057
          fail = fail + 1;
11058
        end
11059
      end
11060
      else // interrupt enabled
11061
      begin
11062
        if (data[15:0] !== 16'h6000)
11063
        begin
11064
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11065
          test_fail("RX buffer descriptor status is not correct");
11066
          fail = fail + 1;
11067
        end
11068
      end
11069
      // check interrupts
11070
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11071
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11072
      begin
11073
        if (data !== 0)
11074
        begin
11075
          `TIME; $display("*E Any of interrupts was set, interrupt reg: %0h, len: %0h", data, num_of_frames[1:0]);
11076
          test_fail("Any of interrupts was set");
11077
          fail = fail + 1;
11078
        end
11079
      end
11080
      else
11081
      begin
11082
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
11083
        begin
11084
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
11085
          test_fail("Interrupt Receive Buffer was not set");
11086
          fail = fail + 1;
11087
        end
11088
        if ((data & (~`ETH_INT_RXB)) !== 0)
11089
        begin
11090
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
11091
          test_fail("Other interrupts (except Receive Buffer) were set");
11092
          fail = fail + 1;
11093
        end
11094
      end
11095
      // clear interrupts
11096
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11097
      // check WB INT signal
11098
      if (wb_int !== 1'b0)
11099
      begin
11100
        test_fail("WB INT signal should not be set");
11101
        fail = fail + 1;
11102
      end
11103
      // disable RX after two packets
11104
      if (num_of_frames[0] == 1'b1)
11105
      begin
11106
        // disable RX, set full-duplex mode, NO receive small, NO correct IFG
11107
        wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_IFG |
11108
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
11109
                  4'hF, 1, 4'h0, 4'h0); // write ASAP
11110
      end
11111
      // the number of frame transmitted
11112
      num_of_frames = num_of_frames + 1;
11113
      num_of_bd = 0;
11114
      // set length (LOOP variable)
11115
      if (num_of_frames[31:2] == (i_length * 2 + 16)) // 64 => this vas last Byte (1st .. 64th) when i_length = min_tmp - 4
11116
        i_length = (max_tmp - 4);
11117
      @(posedge wb_clk);
11118
    end
11119
    // disable RX
11120
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
11121
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11122
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11123
    if(fail == 0)
11124
      test_ok;
11125
    else
11126
      fail = 0;
11127
  end
11128
 
11129
 
11130
  ////////////////////////////////////////////////////////////////////
11131
  ////                                                            ////
11132
  ////  Test receive packet synchronization with receive          ////
11133
  ////  disable/enable ( 100Mbps ).                               ////
11134
  ////                                                            ////
11135
  ////////////////////////////////////////////////////////////////////
11136
  if (test_num == 3) // Test no receive when all buffers are TX ( 100Mbps ).
11137
  begin
11138
    // TEST 3: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 100Mbps )
11139
    test_name   = "TEST 3: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 100Mbps )";
11140
    `TIME; $display("  TEST 3: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 100Mbps )");
11141
 
11142
    // unmask interrupts
11143
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11144
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11145
    // set 10 RX buffer descriptor (8'h80 - 8'hA) - must be set before RX enable
11146
    wbm_write(`ETH_TX_BD_NUM, 32'h76, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11147
    // disable RX, set full-duplex mode, NO receive small, NO correct IFG
11148
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_IFG |
11149
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11150
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11151
    // prepare two packets of MAXFL length
11152
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11153
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
11154
    min_tmp = tmp[31:16];
11155
    st_data = 8'h0F;
11156
    set_rx_packet(0, (min_tmp + 1), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
11157
    append_rx_crc(0, (min_tmp + 1), 1'b0, 1'b0);
11158
    st_data = 8'h1A;
11159
    set_rx_packet(max_tmp, (min_tmp + 1), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
11160
    append_rx_crc(max_tmp, (min_tmp + 1), 1'b0, 1'b0);
11161
    // check WB INT signal
11162
    if (wb_int !== 1'b0)
11163
    begin
11164
      test_fail("WB INT signal should not be set");
11165
      fail = fail + 1;
11166
    end
11167
 
11168
    // write to phy's control register for 100Mbps
11169
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
11170
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
11171
    speed = 100;
11172
 
11173
    frame_started = 0;
11174
    frame_ended = 0;
11175
    wait_for_frame = 0;
11176
    check_frame = 0;
11177
    stop_checking_frame = 0;
11178
    first_fr_received = 1; // at the beginning the first frame of each two will be received!
11179
 
11180
    num_of_frames = 0; // 
11181
    num_of_bd = 0;
11182
    i_length = (min_tmp + 1); // 5 bytes longer than MINFL
11183
    while (i_length < (max_tmp - 4))
11184
    begin
11185
      // choose generating carrier sense and collision 
11186
      case (num_of_frames[1:0])
11187
      2'h0: // Interrupt is generated
11188
      begin
11189
        // enable interrupt generation
11190
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
11191
        // not detect carrier sense in FD and no collision
11192
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11193
        eth_phy.collision(0);
11194
      end
11195
      2'h1: // Interrupt is generated
11196
      begin
11197
        // enable interrupt generation
11198
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
11199
        // detect carrier sense in FD and no collision
11200
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11201
        eth_phy.collision(0);
11202
      end
11203
      2'h2: // Interrupt is generated
11204
      begin
11205
        // disable interrupt generation
11206
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
11207
        // not detect carrier sense in FD and set collision
11208
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11209
        eth_phy.collision(1);
11210
      end
11211
      default: // 2'h3: // Interrupt is generated
11212
      begin
11213
        // disable interrupt generation
11214
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
11215
        // detect carrier sense in FD and set collision
11216
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11217
        eth_phy.collision(1);
11218
      end
11219
      endcase
11220
//if (first_fr_received == 0)
11221
//begin
11222
//  check_rx_bd(118, data);
11223
//  wbm_read((`TX_BD_BASE + (118 * 8) + 4), tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11224
//  $display("RX BD set : %h, %h", data, tmp);
11225
//end
11226
      // set wrap bit
11227
      set_rx_bd_wrap(118);
11228
      set_rx_bd_empty(118, 118);
11229
      check_frame = 0;
11230
      stop_checking_frame = 0;
11231
      tmp_data = 0;
11232
$display("mama 1");
11233
      fork
11234
        begin // enable RX MAC on first of each two packets - every second should be recived normaly
11235
          if (num_of_frames[0] == 1'b0)
11236
          begin
11237
            repeat(1) @(posedge wb_clk);
11238
            if (num_of_frames[1] == 1'b0)
11239
            begin
11240
              repeat (num_of_frames[31:2]) @(posedge mrx_clk); // for every (second) frame enable receiver one clock later
11241
            end
11242
            else
11243
            begin
11244
              @(posedge mrx_clk);
11245
              repeat (num_of_frames[31:2]) @(negedge mrx_clk); // for every (second) frame enable receiver one clock later
11246
            end
11247
            // enable RX, set full-duplex mode, NO receive small, NO correct IFG
11248
            wbm_init_waits = 4'h0;
11249
            wbm_subseq_waits = 4'h0;
11250
            #1 wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
11251
                      `ETH_MODER_PRO | `ETH_MODER_BRO,
11252
                      4'hF, 1, wbm_init_waits, wbm_subseq_waits); // write ASAP
11253
$display("mama 2, num_of_frames=%0h", num_of_frames);
11254
          end
11255
        end
11256
        begin // send a packet from PHY RX
11257
          repeat(1) @(posedge wb_clk); // wait for WB write when it is without delays
11258
          if (num_of_frames[1] == 1'b0)
11259
          begin
11260
            set_rx_addr_type(0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E);
11261
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
11262
          end
11263
          else
11264
          begin
11265
            set_rx_addr_type((max_tmp), 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E);
11266
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
11267
          end
11268
        end
11269
        begin: send_packet1
11270
          wait (MRxDV === 1'b1); // start transmit
11271
          wait (MRxDV === 1'b0); // end transmit
11272
          check_frame = 1;
11273
$display("mama 3");
11274
          repeat(10) @(posedge mrx_clk);
11275
          repeat(15) @(posedge wb_clk);
11276
          stop_checking_frame = 1;
11277
        end
11278
        begin // count WB clocks between ACK (negedge) and RX_DV (posedge) or vice-versa
11279
          @(posedge eth_sl_wb_ack_o or posedge MRxDV);
11280
$display("mama 4");
11281
          if ((eth_sl_wb_ack_o === 1'b1) && (MRxDV === 1'b1))
11282
          begin
11283
            tmp_data = 32'h8000_0001; // bit[31]==1 => 'posedge MRxDV' was before 'negedge eth_sl_wb_ack_o'
11284
$display("mama 4_1");
11285
          end
11286
          else if (MRxDV === 1'b1)
11287
          begin
11288
            while (eth_sl_wb_ack_o === 1'b0)
11289
            begin
11290
              @(posedge wb_clk);
11291
              tmp_data = tmp_data + 1;
11292
            end
11293
            tmp_data = tmp_data | 32'h8000_0000; // bit[31]==1 => 'posedge MRxDV' was before 'negedge eth_sl_wb_ack_o'
11294
$display("mama 4_2");
11295
          end
11296
          else if (eth_sl_wb_ack_o === 1'b1)
11297
          begin
11298
            @(posedge wb_clk); // wait for one clock => tmp_data 'becomes' 0
11299
            while (MRxDV === 1'b0)
11300
            begin
11301
              @(posedge wb_clk);
11302
              tmp_data = tmp_data + 1; // bit[31]==0 => 'negedge eth_sl_wb_ack_o' was equal or before 'posedge MRxDV'
11303
            end
11304
$display("mama 4_3");
11305
          end
11306
        end
11307
        begin // check packet
11308
          wait (check_frame == 1);
11309
          check_rx_bd(118, tmp_bd);
11310
          while ((tmp_bd[15] === 1) && (stop_checking_frame == 0))
11311
          begin
11312
            #1 check_rx_bd(118, tmp_bd);
11313
            @(posedge wb_clk);
11314
          end
11315
$display("mama 5, tmp_bd=%0h", tmp_bd);
11316
          if (num_of_frames[0] == 1'b0)
11317
          begin
11318
            if (tmp_bd[15] === 1)
11319
            begin
11320
              if (first_fr_received == 1)
11321
              begin
11322
                first_fr_received = 0;
11323
                $display("    %0d packets (without this one) are checked - packets are received by two in a set",
11324
                         num_of_frames); // +1 due to start with 0 AND -1 because this packet is excluded
11325
                $display("    From this moment:");
11326
                $display("    first one of two packets (including this one) is not accepted due to late RX enable");
11327
                if (tmp_data[31])
11328
                  $display("    ->RX enable set %0d WB clks after RX_DV", tmp_data[30:0]);
11329
                else
11330
                  $display("    ->RX enable set %0d WB clks before RX_DV", tmp_data[30:0]);
11331
              end
11332
              // check FB, etc.
11333
 
11334
            end
11335
            else // (tmp_bd[15] === 0)
11336
            begin // check FB, packet, etc.
11337
 
11338
            end
11339
$display("mama 5_1");
11340
          end
11341
          else // (num_of_frames[0] == 1'b1)
11342
          begin
11343
            if (tmp_bd[15] === 1) // ERROR, because second packet of each two frames should be received
11344
            begin // check NOTHING
11345
 
11346
            end
11347
            else // (tmp_bd[15] === 0)
11348
            begin // check FB, packet, etc.
11349
 
11350
            end
11351
$display("mama 5_2");
11352
          end
11353
          if (stop_checking_frame == 0)
11354
            disable send_packet1;
11355
        end
11356
      join
11357
      // ONLY IF packet was received!
11358
$display("mama 6");
11359
      if (tmp_bd[15] === 0)
11360
      begin
11361
        // check length of a PACKET
11362
        if (tmp_bd[31:16] != (i_length + 4))
11363
        begin
11364
          `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
11365
                          tmp_bd[31:16], (i_length + 4));
11366
          test_fail("Wrong length of the packet out from PHY");
11367
          fail = fail + 1;
11368
        end
11369
        // check received RX packet data and CRC
11370
        if (first_fr_received == 0) // if PREVIOUS RX buffer descriptor was not ready, pointer address is -1
11371
        begin
11372
          if (num_of_frames[1] == 1'b0)
11373
          begin
11374
            check_rx_packet(0, (`MEMORY_BASE + num_of_frames[1:0] - 1'b1), (i_length + 4), 1'b0, 1'b0, tmp);
11375
          end
11376
          else
11377
          begin
11378
            check_rx_packet(max_tmp, (`MEMORY_BASE + num_of_frames[1:0] - 1'b1), (i_length + 4), 1'b0, 1'b0, tmp);
11379
          end
11380
          if (tmp > 0)
11381
          begin
11382
            `TIME; $display("*E Wrong data of the received packet");
11383
            test_fail("Wrong data of the received packet");
11384
            fail = fail + 1;
11385
          end
11386
        end
11387
        else // if PREVIOUS RX buffer descriptor was ready
11388
        begin
11389
          if (num_of_frames[1] == 1'b0)
11390
          begin
11391
            check_rx_packet(0, (`MEMORY_BASE + num_of_frames[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
11392
          end
11393
          else
11394
          begin
11395
            check_rx_packet(max_tmp, (`MEMORY_BASE + num_of_frames[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
11396
          end
11397
          if (tmp > 0)
11398
          begin
11399
            `TIME; $display("*E Wrong data of the received packet");
11400
            test_fail("Wrong data of the received packet");
11401
            fail = fail + 1;
11402
          end
11403
        end
11404
      end
11405
      // check WB INT signal
11406
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11407
      begin
11408
        if (wb_int !== 1'b0)
11409
        begin
11410
          `TIME; $display("*E WB INT signal should not be set");
11411
          test_fail("WB INT signal should not be set");
11412
          fail = fail + 1;
11413
        end
11414
      end
11415
      else
11416
      begin
11417
        if (wb_int !== 1'b1)
11418
        begin
11419
          `TIME; $display("*E WB INT signal should be set");
11420
          test_fail("WB INT signal should be set");
11421
          fail = fail + 1;
11422
        end
11423
      end
11424
      // check RX buffer descriptor of a packet - only 15 LSBits
11425
      check_rx_bd(118, data);
11426
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11427
      begin
11428
        if (data[15:0] !== 16'hE000)
11429
        begin
11430
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11431
          test_fail("RX buffer descriptor status is not correct");
11432
          fail = fail + 1;
11433
        end
11434
      end
11435
      else // interrupt enabled
11436
      begin
11437
        if (data[15:0] !== 16'h6000)
11438
        begin
11439
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11440
          test_fail("RX buffer descriptor status is not correct");
11441
          fail = fail + 1;
11442
        end
11443
      end
11444
      // check interrupts
11445
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11446
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11447
      begin
11448
        if (data !== 0)
11449
        begin
11450
          `TIME; $display("*E Any of interrupts was set, interrupt reg: %0h, len: %0h", data, num_of_frames[1:0]);
11451
          test_fail("Any of interrupts was set");
11452
          fail = fail + 1;
11453
        end
11454
      end
11455
      else
11456
      begin
11457
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
11458
        begin
11459
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
11460
          test_fail("Interrupt Receive Buffer was not set");
11461
          fail = fail + 1;
11462
        end
11463
        if ((data & (~`ETH_INT_RXB)) !== 0)
11464
        begin
11465
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
11466
          test_fail("Other interrupts (except Receive Buffer) were set");
11467
          fail = fail + 1;
11468
        end
11469
      end
11470
      // clear interrupts
11471
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11472
      // check WB INT signal
11473
      if (wb_int !== 1'b0)
11474
      begin
11475
        test_fail("WB INT signal should not be set");
11476
        fail = fail + 1;
11477
      end
11478
      // disable RX after two packets
11479
      if (num_of_frames[0] == 1'b1)
11480
      begin
11481
        // disable RX, set full-duplex mode, NO receive small, NO correct IFG
11482
        wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_IFG |
11483
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
11484
                  4'hF, 1, 4'h0, 4'h0); // write ASAP
11485
      end
11486
      // the number of frame transmitted
11487
      num_of_frames = num_of_frames + 1;
11488
      num_of_bd = 0;
11489
      // set length (LOOP variable)
11490
      if (num_of_frames[31:2] == (i_length * 2 + 16)) // 64 => this vas last Byte (1st .. 64th) when i_length = min_tmp - 4
11491
        i_length = (max_tmp - 4);
11492
      @(posedge wb_clk);
11493
    end
11494
    // disable RX
11495
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
11496
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11497
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11498
    if(fail == 0)
11499
      test_ok;
11500
    else
11501
      fail = 0;
11502
  end
11503
 
11504
 
11505
  ////////////////////////////////////////////////////////////////////
11506
  ////                                                            ////
11507 209 tadejm
  ////  Test receive packets form MINFL to MAXFL sizes at         ////
11508
  ////  one RX buffer decriptor ( 10Mbps ).                       ////
11509
  ////                                                            ////
11510
  ////////////////////////////////////////////////////////////////////
11511 243 tadejm
  if (test_num == 4) // 
11512 209 tadejm
  begin
11513 243 tadejm
    // TEST 4: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 10Mbps )
11514
    test_name   = "TEST 4: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 10Mbps )";
11515
    `TIME; $display("  TEST 4: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 10Mbps )");
11516 209 tadejm
 
11517
    // unmask interrupts
11518
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11519
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11520
    // set 1 RX buffer descriptor (8'h80 - 1) - must be set before RX enable
11521
    wbm_write(`ETH_TX_BD_NUM, 32'h7F, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11522
    // enable RX, set full-duplex mode, NO receive small, NO correct IFG
11523
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
11524
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11525
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11526
    // prepare two packets of MAXFL length
11527
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11528
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
11529
    min_tmp = tmp[31:16];
11530
    st_data = 8'h0F;
11531
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
11532
    st_data = 8'h1A;
11533
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
11534
    // check WB INT signal
11535
    if (wb_int !== 1'b0)
11536
    begin
11537
      test_fail("WB INT signal should not be set");
11538
      fail = fail + 1;
11539
    end
11540
 
11541
    // write to phy's control register for 10Mbps
11542
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
11543
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
11544
    speed = 10;
11545
 
11546
    i_length = (min_tmp - 4);
11547
    while (i_length <= (max_tmp - 4))
11548
    begin
11549
      // choose generating carrier sense and collision for first and last 64 lengths of frames
11550
      case (i_length[1:0])
11551
      2'h0: // Interrupt is generated
11552
      begin
11553
        // enable interrupt generation
11554
        set_rx_bd(127, 127, 1'b1, (`MEMORY_BASE + i_length[1:0]));
11555
        // unmask interrupts
11556
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11557
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11558
        // not detect carrier sense in FD and no collision
11559
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11560
        eth_phy.collision(0);
11561
      end
11562
      2'h1: // Interrupt is not generated
11563
      begin
11564
        // enable interrupt generation
11565
        set_rx_bd(127, 127, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
11566
        // mask interrupts
11567
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11568
        // detect carrier sense in FD and no collision
11569
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11570
        eth_phy.collision(0);
11571
      end
11572
      2'h2: // Interrupt is not generated
11573
      begin
11574
        // disable interrupt generation
11575
        set_rx_bd(127, 127, 1'b0, (`MEMORY_BASE + i_length[1:0]));
11576
        // unmask interrupts
11577
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11578
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11579
        // not detect carrier sense in FD and set collision
11580
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11581
        eth_phy.collision(1);
11582
      end
11583
      default: // 2'h3: // Interrupt is not generated
11584
      begin
11585
        // disable interrupt generation
11586
        set_rx_bd(127, 127, 1'b0, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
11587
        // mask interrupts
11588
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11589
        // detect carrier sense in FD and set collision
11590
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11591
        eth_phy.collision(1);
11592
      end
11593
      endcase
11594
      if (i_length[0] == 1'b0)
11595
        append_rx_crc (0, i_length, 1'b0, 1'b0);
11596
      else
11597
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
11598
      // set wrap bit
11599
      set_rx_bd_wrap(127);
11600
      set_rx_bd_empty(127, 127);
11601
      fork
11602
        begin
11603
          if (i_length[0] == 1'b0)
11604
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
11605
          else
11606
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
11607
          repeat(10) @(posedge mrx_clk);
11608
        end
11609
        begin
11610
          #1 check_rx_bd(127, data);
11611
          if (i_length < min_tmp) // just first four
11612
          begin
11613
            while (data[15] === 1)
11614
            begin
11615
              #1 check_rx_bd(127, data);
11616
              @(posedge wb_clk);
11617
            end
11618
            repeat (1) @(posedge wb_clk);
11619
          end
11620
          else
11621
          begin
11622
            wait (MRxDV === 1'b1); // start transmit
11623
            #1 check_rx_bd(127, data);
11624
            if (data[15] !== 1)
11625
            begin
11626
              test_fail("Wrong buffer descriptor's ready bit read out from MAC");
11627
              fail = fail + 1;
11628
            end
11629
            wait (MRxDV === 1'b0); // end transmit
11630
            while (data[15] === 1)
11631
            begin
11632
              #1 check_rx_bd(127, data);
11633
              @(posedge wb_clk);
11634
            end
11635
            repeat (1) @(posedge wb_clk);
11636
          end
11637
        end
11638
      join
11639
      // check length of a PACKET
11640
      if (data[31:16] != (i_length + 4))
11641
      begin
11642
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
11643
                        data[31:16], (i_length + 4));
11644
        test_fail("Wrong length of the packet out from PHY");
11645
        fail = fail + 1;
11646
      end
11647
      // checking in the following if statement is performed only for first and last 64 lengths
11648
      // check received RX packet data and CRC
11649
      if (i_length[0] == 1'b0)
11650
      begin
11651
        check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
11652
      end
11653
      else
11654
      begin
11655
        check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
11656
      end
11657
      if (tmp > 0)
11658
      begin
11659
        `TIME; $display("*E Wrong data of the received packet");
11660
        test_fail("Wrong data of the received packet");
11661
        fail = fail + 1;
11662
      end
11663
      // check WB INT signal
11664
      if (i_length[1:0] == 2'h0)
11665
      begin
11666
        if (wb_int !== 1'b1)
11667
        begin
11668
          `TIME; $display("*E WB INT signal should be set");
11669
          test_fail("WB INT signal should be set");
11670
          fail = fail + 1;
11671
        end
11672
      end
11673
      else
11674
      begin
11675
        if (wb_int !== 1'b0)
11676
        begin
11677
          `TIME; $display("*E WB INT signal should not be set");
11678
          test_fail("WB INT signal should not be set");
11679
          fail = fail + 1;
11680
        end
11681
      end
11682
      // check RX buffer descriptor of a packet
11683
      check_rx_bd(127, data);
11684
      if (i_length[1] == 1'b0) // interrupt enabled no_carrier_sense_rx_fd_detect
11685
      begin
11686
        if ( ((data[15:0] !== 16'h6000) && (i_length[0] == 1'b0)) ||
11687
             ((data[15:0] !== 16'h6000) && (i_length[0] == 1'b1)) )
11688
        begin
11689
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11690
          test_fail("RX buffer descriptor status is not correct");
11691
          fail = fail + 1;
11692
        end
11693
      end
11694
      else // interrupt not enabled
11695
      begin
11696
        if ( ((data[15:0] !== 16'h2000) && (i_length[0] == 1'b0)) ||
11697
             ((data[15:0] !== 16'h2000) && (i_length[0] == 1'b1)) )
11698
        begin
11699
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11700
          test_fail("RX buffer descriptor status is not correct");
11701
          fail = fail + 1;
11702
        end
11703
      end
11704
      // clear RX buffer descriptor for first 4 frames
11705
      if (i_length < min_tmp)
11706
        clear_rx_bd(127, 127);
11707
      // check interrupts
11708
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11709
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
11710
      begin
11711
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
11712
        begin
11713
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
11714
          test_fail("Interrupt Receive Buffer was not set");
11715
          fail = fail + 1;
11716
        end
11717
        if ((data & (~`ETH_INT_RXB)) !== 0)
11718
        begin
11719
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
11720
          test_fail("Other interrupts (except Receive Buffer) were set");
11721
          fail = fail + 1;
11722
        end
11723
      end
11724
      else
11725
      begin
11726
        if (data !== 0)
11727
        begin
11728
          `TIME; $display("*E Any of interrupts (except Receive Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
11729
          test_fail("Any of interrupts (except Receive Buffer) was set");
11730
          fail = fail + 1;
11731
        end
11732
      end
11733
      // clear interrupts
11734
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11735
      // check WB INT signal
11736
      if (wb_int !== 1'b0)
11737
      begin
11738
        test_fail("WB INT signal should not be set");
11739
        fail = fail + 1;
11740
      end
11741
      // INTERMEDIATE DISPLAYS
11742
      if ((i_length + 4) == (min_tmp + 64))
11743
      begin
11744
        // starting length is min_tmp, ending length is (min_tmp + 64)
11745
        $display("    receive small packets is NOT selected");
11746
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
11747
                 min_tmp, (min_tmp + 64));
11748
        // set receive small, remain the rest
11749
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
11750
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
11751
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11752
      end
11753
      else if ((i_length + 4) == (max_tmp - 16))
11754
      begin
11755
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
11756
        $display("    receive small packets is selected");
11757
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
11758
                 (min_tmp + 64 + 128), tmp_data);
11759
        // reset receive small, remain the rest
11760
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
11761
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
11762
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11763
      end
11764
      else if ((i_length + 4) == max_tmp)
11765
      begin
11766
        $display("    receive small packets is NOT selected");
11767
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
11768
                 (max_tmp - (4 + 16)), max_tmp);
11769
      end
11770
      // set length (loop variable)
11771
      if ((i_length + 4) < (min_tmp + 64))
11772
        i_length = i_length + 1;
11773
      else if ( ((i_length + 4) >= (min_tmp + 64)) && ((i_length + 4) <= (max_tmp - 256)) )
11774
      begin
11775
        i_length = i_length + 128;
11776
        tmp_data = i_length + 4; // last tmp_data is ending length
11777
      end
11778
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
11779
        i_length = max_tmp - (4 + 16);
11780
      else if ((i_length + 4) >= (max_tmp - 16))
11781
        i_length = i_length + 1;
11782
      else
11783
      begin
11784
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
11785
        #10 $stop;
11786
      end
11787
    end
11788
    // disable RX
11789
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
11790
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11791
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11792
    if(fail == 0)
11793
      test_ok;
11794
    else
11795
      fail = 0;
11796
  end
11797
 
11798
 
11799
  ////////////////////////////////////////////////////////////////////
11800
  ////                                                            ////
11801
  ////  Test receive packets form MINFL to MAXFL sizes at         ////
11802
  ////  one RX buffer decriptor ( 100Mbps ).                      ////
11803
  ////                                                            ////
11804
  ////////////////////////////////////////////////////////////////////
11805 243 tadejm
  if (test_num == 5) // Test no receive when all buffers are TX ( 100Mbps ).
11806 209 tadejm
  begin
11807 243 tadejm
    // TEST 5: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 100Mbps )
11808
    test_name   = "TEST 5: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 100Mbps )";
11809
    `TIME; $display("  TEST 5: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 100Mbps )");
11810 209 tadejm
 
11811
    // unmask interrupts
11812
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11813
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11814
    // set 1 RX buffer descriptor (8'h80 - 1) - must be set before RX enable
11815
    wbm_write(`ETH_TX_BD_NUM, 32'h7F, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11816
    // enable RX, set full-duplex mode, NO receive small, NO correct IFG
11817
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
11818
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11819
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11820
    // prepare two packets of MAXFL length
11821
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11822
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
11823
    min_tmp = tmp[31:16];
11824
    st_data = 8'h0F;
11825
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
11826
    st_data = 8'h1A;
11827
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
11828
    // check WB INT signal
11829
    if (wb_int !== 1'b0)
11830
    begin
11831
      test_fail("WB INT signal should not be set");
11832
      fail = fail + 1;
11833
    end
11834
 
11835
    // write to phy's control register for 100Mbps
11836
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
11837
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
11838
    speed = 100;
11839
 
11840
    i_length = (min_tmp - 4);
11841
    while (i_length <= (max_tmp - 4))
11842
    begin
11843
      // choose generating carrier sense and collision for first and last 64 lengths of frames
11844
      case (i_length[1:0])
11845
      2'h0: // Interrupt is generated
11846
      begin
11847
        // enable interrupt generation
11848
        set_rx_bd(127, 127, 1'b1, (`MEMORY_BASE + i_length[1:0]));
11849
        // unmask interrupts
11850
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11851
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11852
        // not detect carrier sense in FD and no collision
11853
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11854
        eth_phy.collision(0);
11855
      end
11856
      2'h1: // Interrupt is not generated
11857
      begin
11858
        // enable interrupt generation
11859
        set_rx_bd(127, 127, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
11860
        // mask interrupts
11861
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11862
        // detect carrier sense in FD and no collision
11863
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11864
        eth_phy.collision(0);
11865
      end
11866
      2'h2: // Interrupt is not generated
11867
      begin
11868
        // disable interrupt generation
11869
        set_rx_bd(127, 127, 1'b0, (`MEMORY_BASE + i_length[1:0]));
11870
        // unmask interrupts
11871
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11872
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11873
        // not detect carrier sense in FD and set collision
11874
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11875
        eth_phy.collision(1);
11876
      end
11877
      default: // 2'h3: // Interrupt is not generated
11878
      begin
11879
        // disable interrupt generation
11880
        set_rx_bd(127, 127, 1'b0, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
11881
        // mask interrupts
11882
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11883
        // detect carrier sense in FD and set collision
11884
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11885
        eth_phy.collision(1);
11886
      end
11887
      endcase
11888
      if (i_length[0] == 1'b0)
11889
        append_rx_crc (0, i_length, 1'b0, 1'b0);
11890
      else
11891
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
11892
      // set wrap bit
11893
      set_rx_bd_wrap(127);
11894
      set_rx_bd_empty(127, 127);
11895
      fork
11896
        begin
11897
          if (i_length[0] == 1'b0)
11898
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
11899
          else
11900
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
11901
          repeat(10) @(posedge mrx_clk);
11902
        end
11903
        begin
11904
          #1 check_rx_bd(127, data);
11905
          if (i_length < min_tmp) // just first four
11906
          begin
11907
            while (data[15] === 1)
11908
            begin
11909
              #1 check_rx_bd(127, data);
11910
              @(posedge wb_clk);
11911
            end
11912
            repeat (1) @(posedge wb_clk);
11913
          end
11914
          else
11915
          begin
11916
            wait (MRxDV === 1'b1); // start transmit
11917
            #1 check_rx_bd(127, data);
11918
            if (data[15] !== 1)
11919
            begin
11920
              test_fail("Wrong buffer descriptor's ready bit read out from MAC");
11921
              fail = fail + 1;
11922
            end
11923
            wait (MRxDV === 1'b0); // end transmit
11924
            while (data[15] === 1)
11925
            begin
11926
              #1 check_rx_bd(127, data);
11927
              @(posedge wb_clk);
11928
            end
11929
            repeat (1) @(posedge wb_clk);
11930
          end
11931
        end
11932
      join
11933
      // check length of a PACKET
11934
      if (data[31:16] != (i_length + 4))
11935
      begin
11936
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
11937
                        data[31:16], (i_length + 4));
11938
        test_fail("Wrong length of the packet out from PHY");
11939
        fail = fail + 1;
11940
      end
11941
      // checking in the following if statement is performed only for first and last 64 lengths
11942
      // check received RX packet data and CRC
11943
      if (i_length[0] == 1'b0)
11944
      begin
11945
        check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
11946
      end
11947
      else
11948
      begin
11949
        check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
11950
      end
11951
      if (tmp > 0)
11952
      begin
11953
        `TIME; $display("*E Wrong data of the received packet");
11954
        test_fail("Wrong data of the received packet");
11955
        fail = fail + 1;
11956
      end
11957
      // check WB INT signal
11958
      if (i_length[1:0] == 2'h0)
11959
      begin
11960
        if (wb_int !== 1'b1)
11961
        begin
11962
          `TIME; $display("*E WB INT signal should be set");
11963
          test_fail("WB INT signal should be set");
11964
          fail = fail + 1;
11965
        end
11966
      end
11967
      else
11968
      begin
11969
        if (wb_int !== 1'b0)
11970
        begin
11971
          `TIME; $display("*E WB INT signal should not be set");
11972
          test_fail("WB INT signal should not be set");
11973
          fail = fail + 1;
11974
        end
11975
      end
11976
      // check RX buffer descriptor of a packet
11977
      check_rx_bd(127, data);
11978
      if (i_length[1] == 1'b0) // interrupt enabled 
11979
      begin
11980
        if ( ((data[15:0] !== 16'h6000) && (i_length[0] == 1'b0)) ||
11981
             ((data[15:0] !== 16'h6000) && (i_length[0] == 1'b1)) )
11982
        begin
11983
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11984
          test_fail("RX buffer descriptor status is not correct");
11985
          fail = fail + 1;
11986
        end
11987
      end
11988
      else // interrupt not enabled
11989
      begin
11990
        if ( ((data[15:0] !== 16'h2000) && (i_length[0] == 1'b0)) ||
11991
             ((data[15:0] !== 16'h2000) && (i_length[0] == 1'b1)) )
11992
        begin
11993
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11994
          test_fail("RX buffer descriptor status is not correct");
11995
          fail = fail + 1;
11996
        end
11997
      end
11998
      // clear RX buffer descriptor for first 4 frames
11999
      if (i_length < min_tmp)
12000
        clear_rx_bd(127, 127);
12001
      // check interrupts
12002
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12003
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
12004
      begin
12005
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
12006
        begin
12007
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
12008
          test_fail("Interrupt Receive Buffer was not set");
12009
          fail = fail + 1;
12010
        end
12011
        if ((data & (~`ETH_INT_RXB)) !== 0)
12012
        begin
12013
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
12014
          test_fail("Other interrupts (except Receive Buffer) were set");
12015
          fail = fail + 1;
12016
        end
12017
      end
12018
      else
12019
      begin
12020
        if (data !== 0)
12021
        begin
12022
          `TIME; $display("*E Any of interrupts (except Receive Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
12023
          test_fail("Any of interrupts (except Receive Buffer) was set");
12024
          fail = fail + 1;
12025
        end
12026
      end
12027
      // clear interrupts
12028
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12029
      // check WB INT signal
12030
      if (wb_int !== 1'b0)
12031
      begin
12032
        test_fail("WB INT signal should not be set");
12033
        fail = fail + 1;
12034
      end
12035
      // INTERMEDIATE DISPLAYS
12036
      if ((i_length + 4) == (min_tmp + 64))
12037
      begin
12038
        // starting length is min_tmp, ending length is (min_tmp + 64)
12039
        $display("    receive small packets is NOT selected");
12040
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
12041
                 min_tmp, (min_tmp + 64));
12042
        // set receive small, remain the rest
12043
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12044
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12045
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12046
      end
12047
      else if ((i_length + 4) == (max_tmp - 16))
12048
      begin
12049
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
12050
        $display("    receive small packets is selected");
12051
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
12052
                 (min_tmp + 64 + 128), tmp_data);
12053
        // reset receive small, remain the rest
12054
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12055
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12056
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12057
      end
12058
      else if ((i_length + 4) == max_tmp)
12059
      begin
12060
        $display("    receive small packets is NOT selected");
12061
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
12062
                 (max_tmp - (4 + 16)), max_tmp);
12063
      end
12064
      // set length (loop variable)
12065
      if ((i_length + 4) < (min_tmp + 64))
12066
        i_length = i_length + 1;
12067
      else if ( ((i_length + 4) >= (min_tmp + 64)) && ((i_length + 4) <= (max_tmp - 256)) )
12068
      begin
12069
        i_length = i_length + 128;
12070
        tmp_data = i_length + 4; // last tmp_data is ending length
12071
      end
12072
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
12073
        i_length = max_tmp - (4 + 16);
12074
      else if ((i_length + 4) >= (max_tmp - 16))
12075
        i_length = i_length + 1;
12076
      else
12077
      begin
12078
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
12079
        #10 $stop;
12080
      end
12081
    end
12082
    // disable RX
12083
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12084
              `ETH_MODER_PRO | `ETH_MODER_BRO,
12085
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12086
    if(fail == 0)
12087
      test_ok;
12088
    else
12089
      fail = 0;
12090
  end
12091
 
12092
 
12093
  ////////////////////////////////////////////////////////////////////
12094
  ////                                                            ////
12095
  ////  Test receive packets form MINFL to MAXFL sizes at         ////
12096
  ////  maximum RX buffer decriptors ( 10Mbps ).                  ////
12097
  ////                                                            ////
12098
  ////////////////////////////////////////////////////////////////////
12099 243 tadejm
  if (test_num == 6) // 
12100 209 tadejm
  begin
12101 243 tadejm
    // TEST 6: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 10Mbps )
12102
    test_name = "TEST 6: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 10Mbps )";
12103
    `TIME; $display("  TEST 6: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 10Mbps )");
12104 209 tadejm
 
12105
    // reset MAC registers
12106
    hard_reset;
12107
    // reset MAC and MII LOGIC with soft reset
12108
    reset_mac;
12109
    reset_mii;
12110
    // set wb slave response
12111
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
12112
 
12113
    max_tmp = 0;
12114
    min_tmp = 0;
12115
    num_of_frames = 0;
12116
    num_of_bd = 0;
12117
    // set maximum RX buffer descriptors (128) - must be set before RX enable
12118
    wbm_write(`ETH_TX_BD_NUM, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12119
    // enable RX, set full-duplex mode, NO receive small, NO correct IFG
12120
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12121
              `ETH_MODER_PRO | `ETH_MODER_BRO,
12122
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12123
    // prepare two packets of MAXFL length
12124
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12125
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
12126
    min_tmp = tmp[31:16];
12127
    st_data = 8'hAC;
12128
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
12129
    st_data = 8'h35;
12130
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
12131
    // check WB INT signal
12132
    if (wb_int !== 1'b0)
12133
    begin
12134
      test_fail("WB INT signal should not be set");
12135
      fail = fail + 1;
12136
    end
12137
 
12138
    // write to phy's control register for 10Mbps
12139
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
12140
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
12141
    speed = 10;
12142
 
12143
    i_length = (min_tmp - 4);
12144
    while (i_length <= (max_tmp - 4))
12145
    begin
12146
      // append CRC to packet
12147
      if (i_length[0] == 1'b0)
12148
        append_rx_crc (0, i_length, 1'b0, 1'b0);
12149
      else
12150
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
12151
      // choose generating carrier sense and collision
12152
      case (i_length[1:0])
12153
      2'h0: // Interrupt is generated
12154
      begin
12155
        // Reset_tx_bd nable interrupt generation
12156
        // unmask interrupts
12157
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
12158
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12159
        // not detect carrier sense in FD and no collision
12160
        eth_phy.no_carrier_sense_rx_fd_detect(0);
12161
        eth_phy.collision(0);
12162
      end
12163
      2'h1: // Interrupt is not generated
12164
      begin
12165
        // set_tx_bd enable interrupt generation
12166
        // mask interrupts
12167
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12168
        // detect carrier sense in FD and no collision
12169
        eth_phy.no_carrier_sense_rx_fd_detect(1);
12170
        eth_phy.collision(0);
12171
      end
12172
      2'h2: // Interrupt is not generated
12173
      begin
12174
        // set_tx_bd disable the interrupt generation
12175
        // unmask interrupts
12176
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
12177
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12178
        // not detect carrier sense in FD and set collision
12179
        eth_phy.no_carrier_sense_rx_fd_detect(0);
12180
        eth_phy.collision(1);
12181
      end
12182
      default: // 2'h3: // Interrupt is not generated
12183
      begin
12184
        // set_tx_bd disable the interrupt generation
12185
        // mask interrupts
12186
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12187
        // detect carrier sense in FD and set collision
12188
        eth_phy.no_carrier_sense_rx_fd_detect(1);
12189
        eth_phy.collision(1);
12190
      end
12191
      endcase
12192
      // first 8 frames are received with RX BD 0 (wrap bit on RX BD 0)
12193
      // number of all frames is 154 (146 without first 8)
12194
      if (num_of_frames < 8)
12195
      begin
12196
        case (i_length[1:0])
12197
        2'h0: // Interrupt is generated
12198
        begin
12199
          // enable interrupt generation
12200
          set_rx_bd(0, 0, 1'b1, (`MEMORY_BASE + i_length[1:0]));
12201
          // interrupts are unmasked
12202
        end
12203
        2'h1: // Interrupt is not generated
12204
        begin
12205
          // enable interrupt generation
12206
          set_rx_bd(0, 0, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
12207
          // interrupts are masked
12208
        end
12209
        2'h2: // Interrupt is not generated
12210
        begin
12211
          // disable interrupt generation
12212
          set_rx_bd(0, 0, 1'b0, (`MEMORY_BASE + i_length[1:0]));
12213
          // interrupts are unmasked
12214
        end
12215
        default: // 2'h3: // Interrupt is not generated
12216
        begin
12217
          // disable interrupt generation
12218
          set_rx_bd(0, 0, 1'b0, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
12219
          // interrupts are masked
12220
        end
12221
        endcase
12222
        // set wrap bit
12223
        set_rx_bd_wrap(0);
12224
      end
12225
      // after first 8 number of frames, 128 frames form RX BD 0 to 127 will be received
12226
      else if ((num_of_frames - 8) == 0)
12227
      begin
12228
        tmp_len = i_length; // length of frame
12229
        tmp_bd_num = 0; // RX BD number
12230
        while (tmp_bd_num < 128) // (tmp_len <= (max_tmp - 4)) - this is the last frame
12231
        begin
12232
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
12233
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
12234
          if (tmp_len[0] == 0)
12235
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], (`MEMORY_BASE + tmp_len[1:0]));
12236
          else
12237
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
12238
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
12239
          if ((tmp_len + 4) < (min_tmp + 128))
12240
            tmp_len = tmp_len + 1;
12241
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12242
            tmp_len = 256;
12243
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12244
            tmp_len = tmp_len + 128;
12245
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
12246
            tmp_len = max_tmp - (4 + 16);
12247
          else if ((tmp_len + 4) >= (max_tmp - 16))
12248
            tmp_len = tmp_len + 1;
12249
          // set RX BD number
12250
          tmp_bd_num = tmp_bd_num + 1;
12251
        end
12252
        // set wrap bit
12253
        set_rx_bd_wrap(127);
12254
      end
12255
      // after 128 + first 8 number of frames, 19 frames form RX BD 0 to 18 will be received
12256
      else if ((num_of_frames - 8) == 20) // 128
12257
      begin
12258
        tmp_len = tmp_len; // length of frame remaines from previous settings
12259
        tmp_bd_num = 0; // TX BD number
12260
        while (tmp_bd_num < 19) // (tmp_len <= (max_tmp - 4)) - this is the last frame
12261
        begin
12262
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
12263
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
12264
          if (tmp_len[0] == 0)
12265
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], (`MEMORY_BASE + tmp_len[1:0]));
12266
          else
12267
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
12268
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
12269
          if ((tmp_len + 4) < (min_tmp + 128))
12270
            tmp_len = tmp_len + 1;
12271
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12272
            tmp_len = 256;
12273
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12274
            tmp_len = tmp_len + 128;
12275
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
12276
            tmp_len = max_tmp - (4 + 16);
12277
          else if ((tmp_len + 4) >= (max_tmp - 16))
12278
            tmp_len = tmp_len + 1;
12279
          // set TX BD number
12280
          tmp_bd_num = tmp_bd_num + 1;
12281
        end
12282
      end
12283
      // set empty bit
12284
      if (num_of_frames < 8)
12285
        set_rx_bd_empty(0, 0);
12286
      else if ((num_of_frames - 8) < 128)
12287
        set_rx_bd_empty((num_of_frames - 8), (num_of_frames - 8));
12288
      else if ((num_of_frames - 136) < 19)
12289
        set_rx_bd_empty((num_of_frames - 136), (num_of_frames - 136));
12290
      // CHECK END OF RECEIVE
12291
      fork
12292
        begin
12293
          if (i_length[0] == 1'b0)
12294
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
12295
          else
12296
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
12297
          repeat(10) @(posedge mrx_clk);
12298
        end
12299
        begin
12300
          #1 check_rx_bd(num_of_bd, data);
12301
          if (i_length < min_tmp) // just first four
12302
          begin
12303
            while (data[15] === 1)
12304
            begin
12305
              #1 check_rx_bd(num_of_bd, data);
12306
              @(posedge wb_clk);
12307
            end
12308
            repeat (1) @(posedge wb_clk);
12309
          end
12310
          else
12311
          begin
12312
            wait (MRxDV === 1'b1); // start transmit
12313
            #1 check_rx_bd(num_of_bd, data);
12314
            if (data[15] !== 1)
12315
            begin
12316
              test_fail("Wrong buffer descriptor's ready bit read out from MAC");
12317
              fail = fail + 1;
12318
            end
12319
            wait (MRxDV === 1'b0); // end transmit
12320
            while (data[15] === 1)
12321
            begin
12322
              #1 check_rx_bd(num_of_bd, data);
12323
              @(posedge wb_clk);
12324
            end
12325
            repeat (1) @(posedge wb_clk);
12326
          end
12327
        end
12328
      join
12329
      // check length of a PACKET
12330
      if (data[31:16] != (i_length + 4))
12331
      begin
12332
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
12333
                        data[31:16], (i_length + 4));
12334
        test_fail("Wrong length of the packet out from PHY");
12335
        fail = fail + 1;
12336
      end
12337
      // checking in the following if statement is performed only for first and last 64 lengths
12338
      // check received RX packet data and CRC
12339
      if (i_length[0] == 1'b0)
12340
      begin
12341
        check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
12342
      end
12343
      else
12344
      begin
12345
        check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
12346
      end
12347
      if (tmp > 0)
12348
      begin
12349
        `TIME; $display("*E Wrong data of the received packet");
12350
        test_fail("Wrong data of the received packet");
12351
        fail = fail + 1;
12352
      end
12353
      // check WB INT signal
12354
      if (i_length[1:0] == 2'h0)
12355
      begin
12356
        if (wb_int !== 1'b1)
12357
        begin
12358
          `TIME; $display("*E WB INT signal should be set");
12359
          test_fail("WB INT signal should be set");
12360
          fail = fail + 1;
12361
        end
12362
      end
12363
      else
12364
      begin
12365
        if (wb_int !== 1'b0)
12366
        begin
12367
          `TIME; $display("*E WB INT signal should not be set");
12368
          test_fail("WB INT signal should not be set");
12369
          fail = fail + 1;
12370
        end
12371
      end
12372
      // check RX buffer descriptor of a packet
12373
      check_rx_bd(num_of_bd, data);
12374
      if (i_length[1] == 1'b0) // interrupt enabled
12375
      begin
12376
        if ( ((data[15:0] !== 16'h6000) && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
12377
             ((data[15:0] !== 16'h4000) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
12378
        begin
12379
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
12380
          test_fail("RX buffer descriptor status is not correct");
12381
          fail = fail + 1;
12382
        end
12383
      end
12384
      else // interrupt not enabled
12385
      begin
12386
        if ( ((data[15:0] !== 16'h2000)  && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
12387
             ((data[15:0] !== 16'h0000) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
12388
        begin
12389
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
12390
          test_fail("RX buffer descriptor status is not correct");
12391
          fail = fail + 1;
12392
        end
12393
      end
12394
      // clear first half of 8 frames from RX buffer descriptor 0
12395
      if (num_of_frames < 4)
12396
        clear_rx_bd(num_of_bd, num_of_bd);
12397
      // clear BD with wrap bit
12398
      if (num_of_frames == 140)
12399
        clear_rx_bd(127, 127);
12400
      // check interrupts
12401
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12402
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
12403
      begin
12404
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
12405
        begin
12406
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
12407
          test_fail("Interrupt Receive Buffer was not set");
12408
          fail = fail + 1;
12409
        end
12410
        if ((data & (~`ETH_INT_RXB)) !== 0)
12411
        begin
12412
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
12413
          test_fail("Other interrupts (except Receive Buffer) were set");
12414
          fail = fail + 1;
12415
        end
12416
      end
12417
      else
12418
      begin
12419
        if (data !== 0)
12420
        begin
12421
          `TIME; $display("*E Any of interrupts (except Receive Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
12422
          test_fail("Any of interrupts (except Receive Buffer) was set");
12423
          fail = fail + 1;
12424
        end
12425
      end
12426
      // clear interrupts
12427
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12428
      // check WB INT signal
12429
      if (wb_int !== 1'b0)
12430
      begin
12431
        test_fail("WB INT signal should not be set");
12432
        fail = fail + 1;
12433
      end
12434
      // INTERMEDIATE DISPLAYS
12435
      if ((i_length + 4) == (min_tmp + 7))
12436
      begin
12437
        // starting length is min_tmp, ending length is (min_tmp + 128)
12438
        $display("    receive small packets is NOT selected");
12439
        $display("    using only RX BD 0 out of 128 BDs assigned to RX (wrap at first BD - RX BD 0)");
12440
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
12441
                 min_tmp, (min_tmp + 7));
12442
        $display("    ->all packets were received on RX BD 0");
12443
        // reset receive small, remain the rest
12444
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12445
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12446
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12447
      end
12448
      else if ((i_length + 4) == (min_tmp + 128))
12449
      begin
12450
        // starting length is min_tmp, ending length is (min_tmp + 128)
12451
        $display("    receive small packets is NOT selected");
12452
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12453
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
12454
                 (min_tmp + 8), (min_tmp + 128));
12455
        $display("    ->packets were received on RX BD %0d to RX BD %0d respectively",
12456
                 1'b0, num_of_bd);
12457
        tmp_bd = num_of_bd + 1;
12458
        // set receive small, remain the rest
12459
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12460
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12461
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12462
      end
12463
      else if ((i_length + 4) == (max_tmp - 16))
12464
      begin
12465
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
12466
        $display("    receive small packets is selected");
12467
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12468
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
12469
                 (min_tmp + 64 + 128), tmp_data);
12470
        if (tmp_bd > num_of_bd)
12471
          $display("    ->packets were received from RX BD %0d to RX BD 127 and from RX BD 0 to RX BD %0d respectively",
12472
                   tmp_bd, num_of_bd);
12473
        else
12474
          $display("    ->packets were received from RX BD %0d to RX BD %0d respectively",
12475
                   tmp_bd, num_of_bd);
12476
        tmp_bd = num_of_bd + 1;
12477
        // reset receive small, remain the rest
12478
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12479
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12480
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12481
      end
12482
      else if ((i_length + 4) == max_tmp)
12483
      begin
12484
        $display("    receive small packets is NOT selected");
12485
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12486
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
12487
                 (max_tmp - (4 + 16)), max_tmp);
12488
        if (tmp_bd > num_of_bd)
12489
          $display("    ->packets were received from RX BD %0d to RX BD 127 and from RX BD 0 to RX BD %0d respectively",
12490
                   tmp_bd, num_of_bd);
12491
        else
12492
          $display("    ->packets were received from RX BD %0d to RX BD %0d respectively",
12493
                   tmp_bd, num_of_bd);
12494
      end
12495
      // set length (loop variable)
12496
      if ((i_length + 4) < (min_tmp + 128))
12497
        i_length = i_length + 1;
12498
      else if ( ((i_length + 4) == (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
12499
        i_length = 256;
12500
      else if ( ((i_length + 4) > (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
12501
      begin
12502
        i_length = i_length + 128;
12503
        tmp_data = i_length + 4; // last tmp_data is ending length
12504
      end
12505
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
12506
        i_length = max_tmp - (4 + 16);
12507
      else if ((i_length + 4) >= (max_tmp - 16))
12508
        i_length = i_length + 1;
12509
      else
12510
      begin
12511
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
12512
        #10 $stop;
12513
      end
12514
      // the number of frame transmitted
12515
      num_of_frames = num_of_frames + 1;
12516
      if ((num_of_frames <= 8) || ((num_of_frames - 8) == 128))
12517
        num_of_bd = 0;
12518
      else
12519
        num_of_bd = num_of_bd + 1;
12520
    end
12521
    // disable RX
12522
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12523
              `ETH_MODER_PRO | `ETH_MODER_BRO,
12524
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12525
    @(posedge wb_clk);
12526
    if(fail == 0)
12527
      test_ok;
12528
    else
12529
      fail = 0;
12530
  end
12531
 
12532
 
12533
  ////////////////////////////////////////////////////////////////////
12534
  ////                                                            ////
12535
  ////  Test receive packets form MINFL to MAXFL sizes at         ////
12536
  ////  maximum RX buffer decriptors ( 100Mbps ).                 ////
12537
  ////                                                            ////
12538
  ////////////////////////////////////////////////////////////////////
12539 243 tadejm
  if (test_num == 7) // 
12540 209 tadejm
  begin
12541 243 tadejm
    // TEST 7: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 100Mbps )
12542
    test_name = "TEST 7: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 100Mbps )";
12543
    `TIME; $display("  TEST 7: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 100Mbps )");
12544 209 tadejm
 
12545
    // reset MAC registers
12546
    hard_reset;
12547
    // reset MAC and MII LOGIC with soft reset
12548
    reset_mac;
12549
    reset_mii;
12550
    // set wb slave response
12551
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
12552
 
12553
    max_tmp = 0;
12554
    min_tmp = 0;
12555
    num_of_frames = 0;
12556
    num_of_bd = 0;
12557
    // set maximum RX buffer descriptors (128) - must be set before RX enable
12558
    wbm_write(`ETH_TX_BD_NUM, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12559
    // enable RX, set full-duplex mode, NO receive small, NO correct IFG
12560
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12561
              `ETH_MODER_PRO | `ETH_MODER_BRO,
12562
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12563
    // prepare two packets of MAXFL length
12564
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12565
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
12566
    min_tmp = tmp[31:16];
12567
    st_data = 8'hAC;
12568
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
12569
    st_data = 8'h35;
12570
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
12571
    // check WB INT signal
12572
    if (wb_int !== 1'b0)
12573
    begin
12574
      test_fail("WB INT signal should not be set");
12575
      fail = fail + 1;
12576
    end
12577
 
12578
    // write to phy's control register for 100Mbps
12579
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
12580
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
12581
    speed = 100;
12582
 
12583
    i_length = (min_tmp - 4);
12584
    while (i_length <= (max_tmp - 4))
12585
    begin
12586
      // append CRC to packet
12587
      if (i_length[0] == 1'b0)
12588
        append_rx_crc (0, i_length, 1'b0, 1'b0);
12589
      else
12590
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
12591
      // choose generating carrier sense and collision
12592
      case (i_length[1:0])
12593
      2'h0: // Interrupt is generated
12594
      begin
12595
        // Reset_tx_bd nable interrupt generation
12596
        // unmask interrupts
12597
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
12598
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12599
        // not detect carrier sense in FD and no collision
12600
        eth_phy.no_carrier_sense_rx_fd_detect(0);
12601
        eth_phy.collision(0);
12602
      end
12603
      2'h1: // Interrupt is not generated
12604
      begin
12605
        // set_tx_bd enable interrupt generation
12606
        // mask interrupts
12607
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12608
        // detect carrier sense in FD and no collision
12609
        eth_phy.no_carrier_sense_rx_fd_detect(1);
12610
        eth_phy.collision(0);
12611
      end
12612
      2'h2: // Interrupt is not generated
12613
      begin
12614
        // set_tx_bd disable the interrupt generation
12615
        // unmask interrupts
12616
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
12617
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12618
        // not detect carrier sense in FD and set collision
12619
        eth_phy.no_carrier_sense_rx_fd_detect(0);
12620
        eth_phy.collision(1);
12621
      end
12622
      default: // 2'h3: // Interrupt is not generated
12623
      begin
12624
        // set_tx_bd disable the interrupt generation
12625
        // mask interrupts
12626
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12627
        // detect carrier sense in FD and set collision
12628
        eth_phy.no_carrier_sense_rx_fd_detect(1);
12629
        eth_phy.collision(1);
12630
      end
12631
      endcase
12632
      // first 8 frames are received with RX BD 0 (wrap bit on RX BD 0)
12633
      // number of all frames is 154 (146 without first 8)
12634
      if (num_of_frames < 8)
12635
      begin
12636
        case (i_length[1:0])
12637
        2'h0: // Interrupt is generated
12638
        begin
12639
          // enable interrupt generation
12640
          set_rx_bd(0, 0, 1'b1, (`MEMORY_BASE + i_length[1:0]));
12641
          // interrupts are unmasked
12642
        end
12643
        2'h1: // Interrupt is not generated
12644
        begin
12645
          // enable interrupt generation
12646
          set_rx_bd(0, 0, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
12647
          // interrupts are masked
12648
        end
12649
        2'h2: // Interrupt is not generated
12650
        begin
12651
          // disable interrupt generation
12652
          set_rx_bd(0, 0, 1'b0, (`MEMORY_BASE + i_length[1:0]));
12653
          // interrupts are unmasked
12654
        end
12655
        default: // 2'h3: // Interrupt is not generated
12656
        begin
12657
          // disable interrupt generation
12658
          set_rx_bd(0, 0, 1'b0, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
12659
          // interrupts are masked
12660
        end
12661
        endcase
12662
        // set wrap bit
12663
        set_rx_bd_wrap(0);
12664
      end
12665
      // after first 8 number of frames, 128 frames form RX BD 0 to 127 will be received
12666
      else if ((num_of_frames - 8) == 0)
12667
      begin
12668
        tmp_len = i_length; // length of frame
12669
        tmp_bd_num = 0; // RX BD number
12670
        while (tmp_bd_num < 128) // (tmp_len <= (max_tmp - 4)) - this is the last frame
12671
        begin
12672
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
12673
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
12674
          if (tmp_len[0] == 0)
12675
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], (`MEMORY_BASE + tmp_len[1:0]));
12676
          else
12677
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
12678
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
12679
          if ((tmp_len + 4) < (min_tmp + 128))
12680
            tmp_len = tmp_len + 1;
12681
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12682
            tmp_len = 256;
12683
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12684
            tmp_len = tmp_len + 128;
12685
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
12686
            tmp_len = max_tmp - (4 + 16);
12687
          else if ((tmp_len + 4) >= (max_tmp - 16))
12688
            tmp_len = tmp_len + 1;
12689
          // set RX BD number
12690
          tmp_bd_num = tmp_bd_num + 1;
12691
        end
12692
        // set wrap bit
12693
        set_rx_bd_wrap(127);
12694
      end
12695
      // after 128 + first 8 number of frames, 19 frames form RX BD 0 to 18 will be received
12696
      else if ((num_of_frames - 8) == 20) // 128
12697
      begin
12698
        tmp_len = tmp_len; // length of frame remaines from previous settings
12699
        tmp_bd_num = 0; // TX BD number
12700
        while (tmp_bd_num < 19) // (tmp_len <= (max_tmp - 4)) - this is the last frame
12701
        begin
12702
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
12703
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
12704
          if (tmp_len[0] == 0)
12705
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], (`MEMORY_BASE + tmp_len[1:0]));
12706
          else
12707
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
12708
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
12709
          if ((tmp_len + 4) < (min_tmp + 128))
12710
            tmp_len = tmp_len + 1;
12711
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12712
            tmp_len = 256;
12713
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12714
            tmp_len = tmp_len + 128;
12715
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
12716
            tmp_len = max_tmp - (4 + 16);
12717
          else if ((tmp_len + 4) >= (max_tmp - 16))
12718
            tmp_len = tmp_len + 1;
12719
          // set TX BD number
12720
          tmp_bd_num = tmp_bd_num + 1;
12721
        end
12722
      end
12723
      // set empty bit
12724
      if (num_of_frames < 8)
12725
        set_rx_bd_empty(0, 0);
12726
      else if ((num_of_frames - 8) < 128)
12727
        set_rx_bd_empty((num_of_frames - 8), (num_of_frames - 8));
12728
      else if ((num_of_frames - 136) < 19)
12729
        set_rx_bd_empty((num_of_frames - 136), (num_of_frames - 136));
12730
      // CHECK END OF RECEIVE
12731
      fork
12732
        begin
12733
          if (i_length[0] == 1'b0)
12734
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
12735
          else
12736
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
12737
          repeat(10) @(posedge mrx_clk);
12738
        end
12739
        begin
12740
          #1 check_rx_bd(num_of_bd, data);
12741
          if (i_length < min_tmp) // just first four
12742
          begin
12743
            while (data[15] === 1)
12744
            begin
12745
              #1 check_rx_bd(num_of_bd, data);
12746
              @(posedge wb_clk);
12747
            end
12748
            repeat (1) @(posedge wb_clk);
12749
          end
12750
          else
12751
          begin
12752
            wait (MRxDV === 1'b1); // start transmit
12753
            #1 check_rx_bd(num_of_bd, data);
12754
            if (data[15] !== 1)
12755
            begin
12756
              test_fail("Wrong buffer descriptor's ready bit read out from MAC");
12757
              fail = fail + 1;
12758
            end
12759
            wait (MRxDV === 1'b0); // end transmit
12760
            while (data[15] === 1)
12761
            begin
12762
              #1 check_rx_bd(num_of_bd, data);
12763
              @(posedge wb_clk);
12764
            end
12765
            repeat (1) @(posedge wb_clk);
12766
          end
12767
        end
12768
      join
12769
      // check length of a PACKET
12770
      if (data[31:16] != (i_length + 4))
12771
      begin
12772
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
12773
                        data[31:16], (i_length + 4));
12774
        test_fail("Wrong length of the packet out from PHY");
12775
        fail = fail + 1;
12776
      end
12777
      // check received RX packet data and CRC
12778
      if (i_length[0] == 1'b0)
12779
      begin
12780
        check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
12781
      end
12782
      else
12783
      begin
12784
        check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
12785
      end
12786
      if (tmp > 0)
12787
      begin
12788
        `TIME; $display("*E Wrong data of the received packet");
12789
        test_fail("Wrong data of the received packet");
12790
        fail = fail + 1;
12791
      end
12792
      // check WB INT signal
12793
      if (i_length[1:0] == 2'h0)
12794
      begin
12795
        if (wb_int !== 1'b1)
12796
        begin
12797
          `TIME; $display("*E WB INT signal should be set");
12798
          test_fail("WB INT signal should be set");
12799
          fail = fail + 1;
12800
        end
12801
      end
12802
      else
12803
      begin
12804
        if (wb_int !== 1'b0)
12805
        begin
12806
          `TIME; $display("*E WB INT signal should not be set");
12807
          test_fail("WB INT signal should not be set");
12808
          fail = fail + 1;
12809
        end
12810
      end
12811
      // check RX buffer descriptor of a packet
12812
      check_rx_bd(num_of_bd, data);
12813
      if (i_length[1] == 1'b0) // interrupt enabled
12814
      begin
12815
        if ( ((data[15:0] !== 16'h6000) && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
12816
             ((data[15:0] !== 16'h4000) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
12817
        begin
12818
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
12819
          test_fail("RX buffer descriptor status is not correct");
12820
          fail = fail + 1;
12821
        end
12822
      end
12823
      else // interrupt not enabled
12824
      begin
12825
        if ( ((data[15:0] !== 16'h2000)  && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
12826
             ((data[15:0] !== 16'h0000) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
12827
        begin
12828
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
12829
          test_fail("RX buffer descriptor status is not correct");
12830
          fail = fail + 1;
12831
        end
12832
      end
12833
      // clear first half of 8 frames from RX buffer descriptor 0
12834
      if (num_of_frames < 4)
12835
        clear_rx_bd(num_of_bd, num_of_bd);
12836
      // clear BD with wrap bit
12837
      if (num_of_frames == 140)
12838
        clear_rx_bd(127, 127);
12839
      // check interrupts
12840
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12841
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
12842
      begin
12843
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
12844
        begin
12845
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
12846
          test_fail("Interrupt Receive Buffer was not set");
12847
          fail = fail + 1;
12848
        end
12849
        if ((data & (~`ETH_INT_RXB)) !== 0)
12850
        begin
12851
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
12852
          test_fail("Other interrupts (except Receive Buffer) were set");
12853
          fail = fail + 1;
12854
        end
12855
      end
12856
      else
12857
      begin
12858
        if (data !== 0)
12859
        begin
12860
          `TIME; $display("*E Any of interrupts (except Receive Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
12861
          test_fail("Any of interrupts (except Receive Buffer) was set");
12862
          fail = fail + 1;
12863
        end
12864
      end
12865
      // clear interrupts
12866
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12867
      // check WB INT signal
12868
      if (wb_int !== 1'b0)
12869
      begin
12870
        test_fail("WB INT signal should not be set");
12871
        fail = fail + 1;
12872
      end
12873
      // INTERMEDIATE DISPLAYS
12874
      if ((i_length + 4) == (min_tmp + 7))
12875
      begin
12876
        // starting length is min_tmp, ending length is (min_tmp + 128)
12877
        $display("    receive small packets is NOT selected");
12878
        $display("    using only RX BD 0 out of 128 BDs assigned to RX (wrap at first BD - RX BD 0)");
12879
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
12880
                 min_tmp, (min_tmp + 7));
12881
        $display("    ->all packets were received on RX BD 0");
12882
        // reset receive small, remain the rest
12883
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12884
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12885
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12886
      end
12887
      else if ((i_length + 4) == (min_tmp + 128))
12888
      begin
12889
        // starting length is min_tmp, ending length is (min_tmp + 128)
12890
        $display("    receive small packets is NOT selected");
12891
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12892
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
12893
                 (min_tmp + 8), (min_tmp + 128));
12894
        $display("    ->packets were received on RX BD %0d to RX BD %0d respectively",
12895
                 1'b0, num_of_bd);
12896
        tmp_bd = num_of_bd + 1;
12897
        // set receive small, remain the rest
12898
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12899
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12900
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12901
      end
12902
      else if ((i_length + 4) == (max_tmp - 16))
12903
      begin
12904
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
12905
        $display("    receive small packets is selected");
12906
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12907
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
12908
                 (min_tmp + 64 + 128), tmp_data);
12909
        if (tmp_bd > num_of_bd)
12910
          $display("    ->packets were received from RX BD %0d to RX BD 127 and from RX BD 0 to RX BD %0d respectively",
12911
                   tmp_bd, num_of_bd);
12912
        else
12913
          $display("    ->packets were received from RX BD %0d to RX BD %0d respectively",
12914
                   tmp_bd, num_of_bd);
12915
        tmp_bd = num_of_bd + 1;
12916
        // reset receive small, remain the rest
12917
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12918
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12919
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12920
      end
12921
      else if ((i_length + 4) == max_tmp)
12922
      begin
12923
        $display("    receive small packets is NOT selected");
12924
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12925
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
12926
                 (max_tmp - (4 + 16)), max_tmp);
12927
        if (tmp_bd > num_of_bd)
12928
          $display("    ->packets were received from RX BD %0d to RX BD 127 and from RX BD 0 to RX BD %0d respectively",
12929
                   tmp_bd, num_of_bd);
12930
        else
12931
          $display("    ->packets were received from RX BD %0d to RX BD %0d respectively",
12932
                   tmp_bd, num_of_bd);
12933
      end
12934
      // set length (loop variable)
12935
      if ((i_length + 4) < (min_tmp + 128))
12936
        i_length = i_length + 1;
12937
      else if ( ((i_length + 4) == (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
12938
        i_length = 256;
12939
      else if ( ((i_length + 4) > (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
12940
      begin
12941
        i_length = i_length + 128;
12942
        tmp_data = i_length + 4; // last tmp_data is ending length
12943
      end
12944
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
12945
        i_length = max_tmp - (4 + 16);
12946
      else if ((i_length + 4) >= (max_tmp - 16))
12947
        i_length = i_length + 1;
12948
      else
12949
      begin
12950
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
12951
        #10 $stop;
12952
      end
12953
      // the number of frame transmitted
12954
      num_of_frames = num_of_frames + 1;
12955
      if ((num_of_frames <= 8) || ((num_of_frames - 8) == 128))
12956
        num_of_bd = 0;
12957
      else
12958
        num_of_bd = num_of_bd + 1;
12959
    end
12960
    // disable RX
12961
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12962
              `ETH_MODER_PRO | `ETH_MODER_BRO,
12963
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12964
    @(posedge wb_clk);
12965
    if(fail == 0)
12966
      test_ok;
12967
    else
12968
      fail = 0;
12969
  end
12970
 
12971
 
12972
  ////////////////////////////////////////////////////////////////////
12973
  ////                                                            ////
12974 243 tadejm
  ////  Test receive packets form 0 to (MINFL + 12) sizes at       ////
12975 209 tadejm
  ////  8 RX buffer decriptors ( 10Mbps ).                        ////
12976
  ////                                                            ////
12977
  ////////////////////////////////////////////////////////////////////
12978 243 tadejm
  if (test_num == 8) // 
12979 209 tadejm
  begin
12980 243 tadejm
    // TEST 8: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 10Mbps )
12981
    test_name = "TEST 8: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 10Mbps )";
12982
    `TIME; $display("  TEST 8: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 10Mbps )");
12983 209 tadejm
 
12984
    // reset MAC registers
12985
    hard_reset;
12986
    // reset MAC and MII LOGIC with soft reset
12987
    reset_mac;
12988
    reset_mii;
12989
    // set wb slave response
12990
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
12991
 
12992
    max_tmp = 0;
12993
    min_tmp = 0;
12994
    // set 8 RX buffer descriptors (120 - 127) - must be set before RX enable
12995
    wbm_write(`ETH_TX_BD_NUM, 32'h78, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12996
    // enable RX, set full-duplex mode, receive small, NO correct IFG
12997
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12998
              `ETH_MODER_PRO | `ETH_MODER_BRO,
12999
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13000
    // prepare two packets of MAXFL length
13001
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13002
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
13003
    min_tmp = tmp[31:16];
13004
    st_data = 8'hAC;
13005
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
13006
    st_data = 8'h35;
13007
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
13008
    // check WB INT signal
13009
    if (wb_int !== 1'b0)
13010
    begin
13011
      test_fail("WB INT signal should not be set");
13012
      fail = fail + 1;
13013
    end
13014 243 tadejm
    // unmask interrupts
13015
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
13016
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13017 209 tadejm
 
13018
    // write to phy's control register for 10Mbps
13019
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
13020
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
13021
    speed = 10;
13022
 
13023 243 tadejm
    frame_ended = 0;
13024
    num_of_frames = 0;// 0; // 10;
13025
    num_of_bd = 120;
13026
    i_length = 0 - 4;// (0 - 4); // 6; // 4 less due to CRC
13027
    while ((i_length + 4) < 78) // (min_tmp - 4))
13028 209 tadejm
    begin
13029 243 tadejm
      // append CRC to packet
13030
      if ((i_length[0] == 1'b0) && (num_of_frames > 4))
13031
        append_rx_crc (0, i_length, 1'b0, 1'b0);
13032
      else if (num_of_frames > 4)
13033
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
13034 209 tadejm
      // choose generating carrier sense and collision
13035
      case (i_length[1:0])
13036 243 tadejm
      2'h0:
13037 209 tadejm
      begin
13038
        // not detect carrier sense in FD and no collision
13039 243 tadejm
        eth_phy.no_carrier_sense_rx_fd_detect(0);
13040 209 tadejm
        eth_phy.collision(0);
13041
      end
13042 243 tadejm
      2'h1:
13043 209 tadejm
      begin
13044
        // detect carrier sense in FD and no collision
13045 243 tadejm
        eth_phy.no_carrier_sense_rx_fd_detect(1);
13046 209 tadejm
        eth_phy.collision(0);
13047
      end
13048 243 tadejm
      2'h2:
13049 209 tadejm
      begin
13050
        // not detect carrier sense in FD and set collision
13051 243 tadejm
        eth_phy.no_carrier_sense_rx_fd_detect(0);
13052 209 tadejm
        eth_phy.collision(1);
13053
      end
13054 243 tadejm
      default: // 2'h3:
13055 209 tadejm
      begin
13056
        // detect carrier sense in FD and set collision
13057 243 tadejm
        eth_phy.no_carrier_sense_rx_fd_detect(1);
13058 209 tadejm
        eth_phy.collision(1);
13059
      end
13060
      endcase
13061
      #1;
13062 243 tadejm
      // first 10 frames are received with RX BD 120 (wrap bit on RX BD 120)
13063 209 tadejm
      if (num_of_frames <= 9)
13064
      begin
13065 243 tadejm
        case (i_length[1:0])
13066
        2'h0: // Interrupt is generated
13067
        begin
13068
          // enable interrupt generation
13069
          set_rx_bd(120, 120, 1'b1, (`MEMORY_BASE + i_length[1:0]));
13070
        end
13071
        2'h1: // Interrupt is generated
13072
        begin
13073
          // enable interrupt generation
13074
          set_rx_bd(120, 120, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
13075
        end
13076
        2'h2: // Interrupt is generated
13077
        begin
13078
          // enable interrupt generation
13079
          set_rx_bd(120, 120, 1'b1, (`MEMORY_BASE + i_length[1:0]));
13080
        end
13081
        default: // 2'h3: // Interrupt is generated
13082
        begin
13083
          // enable interrupt generation
13084
          set_rx_bd(120, 120, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
13085
        end
13086
        endcase
13087 209 tadejm
        // set wrap bit
13088 243 tadejm
        set_rx_bd_wrap(120);
13089 209 tadejm
      end
13090 243 tadejm
      // 10 <= num_of_frames < 18 => wrap set to TX BD 123
13091 209 tadejm
      else if ((num_of_frames == 10) || (num_of_frames == 14))
13092
      begin
13093
        tmp_len = i_length; // length of frame
13094 243 tadejm
        tmp_bd_num = 120; // RX BD number
13095
        while (tmp_bd_num < 124) // 
13096 209 tadejm
        begin
13097
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13098
          if (tmp_len[0] == 0)
13099 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13100 209 tadejm
          else
13101 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13102
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13103 209 tadejm
          tmp_len = tmp_len + 1;
13104 243 tadejm
          // set RX BD number
13105 209 tadejm
          tmp_bd_num = tmp_bd_num + 1;
13106
        end
13107
        // set wrap bit
13108 243 tadejm
        set_rx_bd_wrap(123);
13109 209 tadejm
      end
13110 243 tadejm
      // 18 <= num_of_frames < 28 => wrap set to RX BD 124
13111 209 tadejm
      else if ((num_of_frames == 18) || (num_of_frames == 23))
13112
      begin
13113
        tmp_len = i_length; // length of frame
13114 243 tadejm
        tmp_bd_num = 120; // RX BD number
13115
        while (tmp_bd_num < 125) // 
13116 209 tadejm
        begin
13117
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13118
          if (tmp_len[0] == 0)
13119 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13120 209 tadejm
          else
13121 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13122
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13123 209 tadejm
          tmp_len = tmp_len + 1;
13124 243 tadejm
          // set RX BD number
13125 209 tadejm
          tmp_bd_num = tmp_bd_num + 1;
13126
        end
13127
        // set wrap bit
13128 243 tadejm
        set_rx_bd_wrap(124);
13129 209 tadejm
      end
13130 243 tadejm
      // 28 <= num_of_frames < 40 => wrap set to RX BD 125
13131 209 tadejm
      else if ((num_of_frames == 28) || (num_of_frames == 34))
13132
      begin
13133
        tmp_len = i_length; // length of frame
13134 243 tadejm
        tmp_bd_num = 120; // RX BD number
13135
        while (tmp_bd_num < 126) // 
13136 209 tadejm
        begin
13137
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13138
          if (tmp_len[0] == 0)
13139 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13140 209 tadejm
          else
13141 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13142
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13143 209 tadejm
          tmp_len = tmp_len + 1;
13144 243 tadejm
          // set RX BD number
13145 209 tadejm
          tmp_bd_num = tmp_bd_num + 1;
13146
        end
13147
        // set wrap bit
13148 243 tadejm
        set_rx_bd_wrap(125);
13149 209 tadejm
      end
13150 243 tadejm
      // 40 <= num_of_frames < 54 => wrap set to RX BD 126
13151 209 tadejm
      else if ((num_of_frames == 40) || (num_of_frames == 47))
13152
      begin
13153
        tmp_len = i_length; // length of frame
13154 243 tadejm
        tmp_bd_num = 120; // RX BD number
13155
        while (tmp_bd_num < 127) // 
13156 209 tadejm
        begin
13157
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13158
          if (tmp_len[0] == 0)
13159 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13160 209 tadejm
          else
13161 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13162
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13163 209 tadejm
          tmp_len = tmp_len + 1;
13164 243 tadejm
          // set RX BD number
13165 209 tadejm
          tmp_bd_num = tmp_bd_num + 1;
13166
        end
13167
        // set wrap bit
13168 243 tadejm
        set_rx_bd_wrap(126);
13169 209 tadejm
      end
13170 243 tadejm
      // 54 <= num_of_frames < 70 => wrap set to RX BD 127
13171 209 tadejm
      else if ((num_of_frames == 54) || (num_of_frames == 62))
13172
      begin
13173
        tmp_len = i_length; // length of frame
13174 243 tadejm
        tmp_bd_num = 120; // RX BD number
13175
        while (tmp_bd_num < 128) // 
13176 209 tadejm
        begin
13177
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13178
          if (tmp_len[0] == 0)
13179 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13180 209 tadejm
          else
13181 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13182
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13183 209 tadejm
          tmp_len = tmp_len + 1;
13184 243 tadejm
          // set RX BD number
13185 209 tadejm
          tmp_bd_num = tmp_bd_num + 1;
13186
        end
13187
        // set wrap bit
13188 243 tadejm
        set_rx_bd_wrap(127);
13189 209 tadejm
      end
13190 243 tadejm
      // 70 <= num_of_frames < 78 => wrap set to RX BD 127
13191
      else if (num_of_frames == 70)
13192
      begin
13193
        tmp_len = i_length; // length of frame
13194
        tmp_bd_num = 120; // RX BD number
13195
        while (tmp_bd_num < 128) // 
13196
        begin
13197
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13198
          if (tmp_len[0] == 0)
13199
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13200
          else
13201
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13202
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13203
          tmp_len = tmp_len + 1;
13204
          // set RX BD number
13205
          tmp_bd_num = tmp_bd_num + 1;
13206
        end
13207
        // set wrap bit
13208
        set_rx_bd_wrap(127);
13209
      end
13210 209 tadejm
      #1;
13211 243 tadejm
      // SET empty bit
13212 209 tadejm
      if (num_of_frames < 10)
13213 243 tadejm
        set_rx_bd_empty(120, 120);
13214 209 tadejm
      else if (num_of_frames < 14)
13215 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 10), (120 + num_of_frames - 10));
13216 209 tadejm
      else if (num_of_frames < 18)
13217 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 14), (120 + num_of_frames - 14));
13218 209 tadejm
      else if (num_of_frames < 23)
13219 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 18), (120 + num_of_frames - 18));
13220 209 tadejm
      else if (num_of_frames < 28)
13221 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 23), (120 + num_of_frames - 23));
13222 209 tadejm
      else if (num_of_frames < 34)
13223 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 28), (120 + num_of_frames - 28));
13224 209 tadejm
      else if (num_of_frames < 40)
13225 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 34), (120 + num_of_frames - 34));
13226 209 tadejm
      else if (num_of_frames < 47)
13227 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 40), (120 + num_of_frames - 40));
13228 209 tadejm
      else if (num_of_frames < 54)
13229 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 47), (120 + num_of_frames - 47));
13230 209 tadejm
      else if (num_of_frames < 62)
13231 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 54), (120 + num_of_frames - 54));
13232 209 tadejm
      else if (num_of_frames < 70)
13233 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 62), (120 + num_of_frames - 62));
13234
      else if (num_of_frames < 78)
13235
        set_rx_bd_empty((120 + num_of_frames - 70), (120 + num_of_frames - 70));
13236
      // CHECK END OF RECEIVE
13237 252 tadejm
      // receive just preamble between some packets
13238
      if ((num_of_frames == 0) || (num_of_frames == 4) || (num_of_frames == 9))
13239
      begin
13240
        #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h6, 8'h55, 0, 0, 1'b0);
13241
        @(posedge mrx_clk);
13242
        wait (MRxDV === 1'b0); // end receive
13243
        repeat(10) @(posedge mrx_clk);
13244
        repeat(15) @(posedge wb_clk);
13245
      end
13246
      // receiving frames and checking end of them
13247 243 tadejm
      frame_ended = 0;
13248
      check_frame = 0;
13249 209 tadejm
      fork
13250
        begin
13251 243 tadejm
          if (i_length[0] == 1'b0)
13252
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
13253
          else
13254
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
13255
          repeat(10) @(posedge mrx_clk);
13256
        end
13257
        begin: fr_end1
13258
          wait (MRxDV === 1'b1); // start receive
13259
          #1 check_rx_bd(num_of_bd, data);
13260
          if (data[15] !== 1)
13261 209 tadejm
          begin
13262 243 tadejm
            test_fail("Wrong buffer descriptor's empty bit read out from MAC");
13263
            fail = fail + 1;
13264 209 tadejm
          end
13265 243 tadejm
          wait (MRxDV === 1'b0); // end receive
13266
          while ((data[15] === 1) && (check_frame == 0))
13267
          begin
13268
            #1 check_rx_bd(num_of_bd, data);
13269
            @(posedge wb_clk);
13270
          end
13271
          if (data[15] === 0)
13272
            frame_ended = 1;
13273
          repeat (1) @(posedge wb_clk);
13274 209 tadejm
        end
13275 243 tadejm
        begin
13276
          wait (MRxDV === 1'b1); // start receive
13277
          wait (MRxDV === 1'b0); // end receive
13278
          repeat(10) @(posedge mrx_clk);
13279
          repeat(15) @(posedge wb_clk);
13280
          check_frame = 1;
13281
        end
13282
      join
13283
      // check length of a PACKET
13284
      if ( ((data[31:16] != (i_length + 4)) && (num_of_frames >= 3)) ||
13285
           ((data[31:16] != 0) && (num_of_frames < 3)) )
13286
      begin
13287
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
13288
                        data[31:16], (i_length + 4));
13289
        test_fail("Wrong length of the packet out from PHY");
13290
        fail = fail + 1;
13291
      end
13292
      // check received RX packet data and CRC
13293
      if ((frame_ended == 1) && (num_of_frames >= 5)) // 5 bytes is minimum size without CRC error, since
13294
      begin                                           // CRC has 4 bytes for itself
13295
        if (i_length[0] == 1'b0)
13296
        begin
13297
          check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
13298
        end
13299 209 tadejm
        else
13300
        begin
13301 243 tadejm
          check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
13302 209 tadejm
        end
13303 243 tadejm
        if (tmp > 0)
13304
        begin
13305
          `TIME; $display("*E Wrong data of the received packet");
13306
          test_fail("Wrong data of the received packet");
13307
          fail = fail + 1;
13308
        end
13309 209 tadejm
      end
13310 243 tadejm
      // check WB INT signal
13311
      if (num_of_frames >= 3) // Frames smaller than 3 are not received.
13312
      begin                   // Frames greater then 5 always cause an interrupt (Frame received)
13313
        if (wb_int !== 1'b1)  // Frames with length 3 or 4 always cause an interrupt (CRC error)
13314
        begin
13315
          `TIME; $display("*E WB INT signal should be set");
13316
          test_fail("WB INT signal should be set");
13317
          fail = fail + 1;
13318
        end
13319
      end
13320
      else
13321
      begin
13322
        if (wb_int !== 1'b0)
13323
        begin
13324
          `TIME; $display("*E WB INT signal should not be set");
13325
          test_fail("WB INT signal should not be set");
13326
          fail = fail + 1;
13327
        end
13328
      end
13329
      // check RX buffer descriptor of a packet
13330 254 mohor
      // check RX buffer descriptor of a packet
13331 243 tadejm
      if (num_of_frames >= min_tmp)
13332
      begin
13333 254 mohor
        if ( (data[15:0] !== 16'h6080) && // wrap bit
13334
             (data[15:0] !== 16'h4080) ) // without wrap bit
13335 243 tadejm
        begin
13336
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13337
          test_fail("RX buffer descriptor status is not correct");
13338
          fail = fail + 1;
13339
        end
13340
      end
13341 254 mohor
      else if (num_of_frames > 6)
13342 243 tadejm
      begin
13343 254 mohor
        if ( (data[15:0] !== 16'h6084) && // wrap bit
13344
             (data[15:0] !== 16'h4084) ) // without wrap bit
13345
        begin
13346
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13347
          test_fail("RX buffer descriptor status is not correct");
13348
          fail = fail + 1;
13349
        end
13350
      end
13351
      else if (num_of_frames > 4) // MAC does not recognize Dest. ADDR. for lengths 5, 6 => no MISS
13352
      begin
13353 243 tadejm
        if ( (data[15:0] !== 16'h6004) && // wrap bit
13354
             (data[15:0] !== 16'h4004) ) // without wrap bit
13355
        begin
13356
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13357
          test_fail("RX buffer descriptor status is not correct");
13358
          fail = fail + 1;
13359
        end
13360
      end
13361 254 mohor
      else if (num_of_frames > 2) // MAC does not recognize Dest. ADDR. for length 3, 4 => no MISS, CRC ERROR
13362 243 tadejm
      begin
13363
        if ( (data[15:0] !== 16'h6006) && // wrap bit
13364
             (data[15:0] !== 16'h4006) ) // without wrap bit
13365
        begin
13366
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13367
          test_fail("RX buffer descriptor status is not correct");
13368
          fail = fail + 1;
13369
        end
13370
      end
13371
      else
13372
      begin
13373
        if (data[15] !== 1'b1)
13374
        begin
13375
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13376
          test_fail("RX buffer descriptor status is not correct");
13377
          fail = fail + 1;
13378
        end
13379
      end
13380
      // check interrupts
13381
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13382 209 tadejm
      if (num_of_frames >= 5)
13383
      begin
13384 243 tadejm
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
13385 209 tadejm
        begin
13386 243 tadejm
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
13387
          test_fail("Interrupt Receive Buffer was not set");
13388
          fail = fail + 1;
13389 209 tadejm
        end
13390 243 tadejm
        if ((data & (~`ETH_INT_RXB)) !== 0)
13391 209 tadejm
        begin
13392 243 tadejm
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
13393
          test_fail("Other interrupts (except Receive Buffer) were set");
13394
          fail = fail + 1;
13395 209 tadejm
        end
13396 243 tadejm
      end
13397
      else if ((num_of_frames < 3)) // Frames smaller than 3 are not received.
13398
      begin
13399
        if (data) // Checking if any interrupt is pending)
13400 209 tadejm
        begin
13401 243 tadejm
          `TIME; $display("*E Interrupt(s) is(are) pending although frame was ignored, interrupt reg: %0h", data);
13402
          test_fail("Interrupts were set");
13403
          fail = fail + 1;
13404
        end
13405
      end
13406
      else
13407
      begin
13408
        if ((data & `ETH_INT_RXE) !== `ETH_INT_RXE)
13409
        begin
13410
          `TIME; $display("*E Interrupt Receive Buffer Error was not set, interrupt reg: %0h", data);
13411
          test_fail("Interrupt Receive Buffer Error was not set");
13412
          fail = fail + 1;
13413
        end
13414
        if ((data & (~`ETH_INT_RXE)) !== 0)
13415
        begin
13416
          `TIME; $display("*E Other interrupts (except Receive Buffer Error) were set, interrupt reg: %0h", data);
13417
          test_fail("Other interrupts (except Receive Buffer Error) were set");
13418
          fail = fail + 1;
13419
        end
13420
      end
13421
      // clear interrupts
13422
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13423
      // check WB INT signal
13424
      if (wb_int !== 1'b0)
13425
      begin
13426
        test_fail("WB INT signal should not be set");
13427
        fail = fail + 1;
13428
      end
13429
      // INTERMEDIATE DISPLAYS
13430
      if (num_of_frames == 3)
13431
      begin
13432
        $display("    using 1 BD out of 8 BDs (120..127) assigned to RX (wrap at 1st BD - RX BD 120)");
13433
        $display("    ->packets with lengths from %0d to %0d are not received (length increasing by 1 byte)",
13434
                 0, 3);
13435
      end
13436
      else if (num_of_frames == 9)
13437
      begin
13438
        $display("    using 1 BD out of 8 BDs (120..127) assigned to RX (wrap at 1st BD - RX BD 120)");
13439
        $display("    ->packet with length 4 is not received (length increasing by 1 byte)");
13440
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13441
                 5, 9);
13442
      end
13443
      else if (num_of_frames == 17)
13444
      begin
13445
        $display("    using 4 BDs out of 8 BDs (120..127) assigned to RX (wrap at 4th BD - RX BD 123)");
13446
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13447
                 10, 17);
13448
      end
13449
      else if (num_of_frames == 27)
13450
      begin
13451
        $display("    using 5 BDs out of 8 BDs (120..127) assigned to RX (wrap at 5th BD - RX BD 124)");
13452
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13453
                 18, 27);
13454
      end
13455
      else if (num_of_frames == 40)
13456
      begin
13457
        $display("    using 6 BDs out of 8 BDs (120..127) assigned to RX (wrap at 6th BD - RX BD 125)");
13458
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13459
                 28, 40);
13460
      end
13461
      else if (num_of_frames == 54)
13462
      begin
13463
        $display("    using 7 BDs out of 8 BDs (120..127) assigned to RX (wrap at 7th BD - RX BD 126)");
13464
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13465
                 41, 54);
13466
      end
13467
      else if (num_of_frames == 69)
13468
      begin
13469
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
13470
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13471
                 55, 69);
13472
      end
13473
      else if (num_of_frames == 69)
13474
      begin
13475
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
13476
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13477
                 55, 69);
13478
      end
13479
      else if (num_of_frames == 77)
13480
      begin
13481
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
13482
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13483
                 70, 77);
13484
      end
13485
      // set length (loop variable)
13486
      i_length = i_length + 1;
13487
      // the number of frame transmitted
13488
      num_of_frames = num_of_frames + 1;
13489
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
13490
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
13491
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
13492
          (num_of_frames == 54) || (num_of_frames == 62) || (num_of_frames == 70))
13493
        num_of_bd = 120;
13494
      else
13495
        num_of_bd = num_of_bd + 1;
13496
    end
13497
    // disable RX
13498
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
13499
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13500
    @(posedge wb_clk);
13501
    if(fail == 0)
13502
      test_ok;
13503
    else
13504
      fail = 0;
13505
  end
13506
 
13507
 
13508
  ////////////////////////////////////////////////////////////////////
13509
  ////                                                            ////
13510
  ////  Test receive packets form 0 to (MINFL + 12) sizes at      ////
13511
  ////  8 RX buffer decriptors ( 100Mbps ).                       ////
13512
  ////                                                            ////
13513
  ////////////////////////////////////////////////////////////////////
13514
  if (test_num == 9) // 
13515
  begin
13516
    // TEST 9: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 100Mbps )
13517
    test_name = "TEST 9: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 100Mbps )";
13518
    `TIME; $display("  TEST 9: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 100Mbps )");
13519
 
13520
    // reset MAC registers
13521
    hard_reset;
13522
    // reset MAC and MII LOGIC with soft reset
13523
    reset_mac;
13524
    reset_mii;
13525
    // set wb slave response
13526
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
13527
 
13528
    max_tmp = 0;
13529
    min_tmp = 0;
13530
    // set 8 RX buffer descriptors (120 - 127) - must be set before RX enable
13531
    wbm_write(`ETH_TX_BD_NUM, 32'h78, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13532
    // enable RX, set full-duplex mode, receive small, NO correct IFG
13533
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
13534
              `ETH_MODER_PRO | `ETH_MODER_BRO,
13535
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13536
    // prepare two packets of MAXFL length
13537
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13538
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
13539
    min_tmp = tmp[31:16];
13540
    st_data = 8'hAC;
13541
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
13542
    st_data = 8'h35;
13543
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
13544
    // check WB INT signal
13545
    if (wb_int !== 1'b0)
13546
    begin
13547
      test_fail("WB INT signal should not be set");
13548
      fail = fail + 1;
13549
    end
13550
    // unmask interrupts
13551
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
13552
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13553
 
13554
    // write to phy's control register for 100Mbps
13555
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
13556
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
13557
    speed = 100;
13558
 
13559
    frame_ended = 0;
13560 252 tadejm
    num_of_frames = 0;// 0; // 10;
13561 243 tadejm
    num_of_bd = 120;
13562
    i_length = 0 - 4;// (0 - 4); // 6; // 4 less due to CRC
13563
    while ((i_length + 4) < 78) // (min_tmp - 4))
13564
    begin
13565
      // append CRC to packet
13566 252 tadejm
      if ((i_length[0] == 1'b0) && (num_of_frames > 4))
13567 243 tadejm
        append_rx_crc (0, i_length, 1'b0, 1'b0);
13568 252 tadejm
      else if (num_of_frames > 4)
13569 243 tadejm
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
13570
      // choose generating carrier sense and collision
13571
      case (i_length[1:0])
13572
      2'h0:
13573
      begin
13574
        // not detect carrier sense in FD and no collision
13575
        eth_phy.no_carrier_sense_rx_fd_detect(0);
13576
        eth_phy.collision(0);
13577
      end
13578
      2'h1:
13579
      begin
13580
        // detect carrier sense in FD and no collision
13581
        eth_phy.no_carrier_sense_rx_fd_detect(1);
13582
        eth_phy.collision(0);
13583
      end
13584
      2'h2:
13585
      begin
13586
        // not detect carrier sense in FD and set collision
13587
        eth_phy.no_carrier_sense_rx_fd_detect(0);
13588
        eth_phy.collision(1);
13589
      end
13590
      default: // 2'h3:
13591
      begin
13592
        // detect carrier sense in FD and set collision
13593
        eth_phy.no_carrier_sense_rx_fd_detect(1);
13594
        eth_phy.collision(1);
13595
      end
13596
      endcase
13597
      #1;
13598
      // first 10 frames are received with RX BD 120 (wrap bit on RX BD 120)
13599
      if (num_of_frames <= 9)
13600
      begin
13601
        case (i_length[1:0])
13602
        2'h0: // Interrupt is generated
13603
        begin
13604
          // enable interrupt generation
13605
          set_rx_bd(120, 120, 1'b1, (`MEMORY_BASE + i_length[1:0]));
13606
        end
13607
        2'h1: // Interrupt is generated
13608
        begin
13609
          // enable interrupt generation
13610
          set_rx_bd(120, 120, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
13611
        end
13612
        2'h2: // Interrupt is generated
13613
        begin
13614
          // enable interrupt generation
13615
          set_rx_bd(120, 120, 1'b1, (`MEMORY_BASE + i_length[1:0]));
13616
        end
13617
        default: // 2'h3: // Interrupt is generated
13618
        begin
13619
          // enable interrupt generation
13620
          set_rx_bd(120, 120, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
13621
        end
13622
        endcase
13623
        // set wrap bit
13624
        set_rx_bd_wrap(120);
13625
      end
13626
      // 10 <= num_of_frames < 18 => wrap set to TX BD 123
13627
      else if ((num_of_frames == 10) || (num_of_frames == 14))
13628
      begin
13629
        tmp_len = i_length; // length of frame
13630
        tmp_bd_num = 120; // RX BD number
13631
        while (tmp_bd_num < 124) // 
13632
        begin
13633
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13634
          if (tmp_len[0] == 0)
13635
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13636
          else
13637
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13638
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13639
          tmp_len = tmp_len + 1;
13640
          // set RX BD number
13641
          tmp_bd_num = tmp_bd_num + 1;
13642
        end
13643
        // set wrap bit
13644
        set_rx_bd_wrap(123);
13645
      end
13646
      // 18 <= num_of_frames < 28 => wrap set to RX BD 124
13647
      else if ((num_of_frames == 18) || (num_of_frames == 23))
13648
      begin
13649
        tmp_len = i_length; // length of frame
13650
        tmp_bd_num = 120; // RX BD number
13651
        while (tmp_bd_num < 125) // 
13652
        begin
13653
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13654
          if (tmp_len[0] == 0)
13655
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13656
          else
13657
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13658
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13659
          tmp_len = tmp_len + 1;
13660
          // set RX BD number
13661
          tmp_bd_num = tmp_bd_num + 1;
13662
        end
13663
        // set wrap bit
13664
        set_rx_bd_wrap(124);
13665
      end
13666
      // 28 <= num_of_frames < 40 => wrap set to RX BD 125
13667
      else if ((num_of_frames == 28) || (num_of_frames == 34))
13668
      begin
13669
        tmp_len = i_length; // length of frame
13670
        tmp_bd_num = 120; // RX BD number
13671
        while (tmp_bd_num < 126) // 
13672
        begin
13673
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13674
          if (tmp_len[0] == 0)
13675
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13676
          else
13677
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13678
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13679
          tmp_len = tmp_len + 1;
13680
          // set RX BD number
13681
          tmp_bd_num = tmp_bd_num + 1;
13682
        end
13683
        // set wrap bit
13684
        set_rx_bd_wrap(125);
13685
      end
13686
      // 40 <= num_of_frames < 54 => wrap set to RX BD 126
13687
      else if ((num_of_frames == 40) || (num_of_frames == 47))
13688
      begin
13689
        tmp_len = i_length; // length of frame
13690
        tmp_bd_num = 120; // RX BD number
13691
        while (tmp_bd_num < 127) // 
13692
        begin
13693
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13694
          if (tmp_len[0] == 0)
13695
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13696
          else
13697
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13698
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13699
          tmp_len = tmp_len + 1;
13700
          // set RX BD number
13701
          tmp_bd_num = tmp_bd_num + 1;
13702
        end
13703
        // set wrap bit
13704
        set_rx_bd_wrap(126);
13705
      end
13706
      // 54 <= num_of_frames < 70 => wrap set to RX BD 127
13707
      else if ((num_of_frames == 54) || (num_of_frames == 62))
13708
      begin
13709
        tmp_len = i_length; // length of frame
13710
        tmp_bd_num = 120; // RX BD number
13711
        while (tmp_bd_num < 128) // 
13712
        begin
13713
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13714
          if (tmp_len[0] == 0)
13715
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13716
          else
13717
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13718
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13719
          tmp_len = tmp_len + 1;
13720
          // set RX BD number
13721
          tmp_bd_num = tmp_bd_num + 1;
13722
        end
13723
        // set wrap bit
13724
        set_rx_bd_wrap(127);
13725
      end
13726
      // 70 <= num_of_frames < 78 => wrap set to RX BD 127
13727
      else if (num_of_frames == 70)
13728
      begin
13729
        tmp_len = i_length; // length of frame
13730
        tmp_bd_num = 120; // RX BD number
13731
        while (tmp_bd_num < 128) // 
13732
        begin
13733
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13734
          if (tmp_len[0] == 0)
13735
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13736
          else
13737
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13738
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13739
          tmp_len = tmp_len + 1;
13740
          // set RX BD number
13741
          tmp_bd_num = tmp_bd_num + 1;
13742
        end
13743
        // set wrap bit
13744
        set_rx_bd_wrap(127);
13745
      end
13746
      #1;
13747
      // SET empty bit
13748
      if (num_of_frames < 10)
13749
        set_rx_bd_empty(120, 120);
13750
      else if (num_of_frames < 14)
13751
        set_rx_bd_empty((120 + num_of_frames - 10), (120 + num_of_frames - 10));
13752
      else if (num_of_frames < 18)
13753
        set_rx_bd_empty((120 + num_of_frames - 14), (120 + num_of_frames - 14));
13754
      else if (num_of_frames < 23)
13755
        set_rx_bd_empty((120 + num_of_frames - 18), (120 + num_of_frames - 18));
13756
      else if (num_of_frames < 28)
13757
        set_rx_bd_empty((120 + num_of_frames - 23), (120 + num_of_frames - 23));
13758
      else if (num_of_frames < 34)
13759
        set_rx_bd_empty((120 + num_of_frames - 28), (120 + num_of_frames - 28));
13760
      else if (num_of_frames < 40)
13761
        set_rx_bd_empty((120 + num_of_frames - 34), (120 + num_of_frames - 34));
13762
      else if (num_of_frames < 47)
13763
        set_rx_bd_empty((120 + num_of_frames - 40), (120 + num_of_frames - 40));
13764
      else if (num_of_frames < 54)
13765
        set_rx_bd_empty((120 + num_of_frames - 47), (120 + num_of_frames - 47));
13766
      else if (num_of_frames < 62)
13767
        set_rx_bd_empty((120 + num_of_frames - 54), (120 + num_of_frames - 54));
13768
      else if (num_of_frames < 70)
13769
        set_rx_bd_empty((120 + num_of_frames - 62), (120 + num_of_frames - 62));
13770
      else if (num_of_frames < 78)
13771
        set_rx_bd_empty((120 + num_of_frames - 70), (120 + num_of_frames - 70));
13772
      // CHECK END OF RECEIVE
13773 252 tadejm
      // receive just preamble between some packets
13774
      if ((num_of_frames == 0) || (num_of_frames == 4) || (num_of_frames == 9))
13775
      begin
13776
        #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h6, 8'h55, 0, 0, 1'b0);
13777
        @(posedge mrx_clk);
13778
        wait (MRxDV === 1'b0); // end receive
13779
        repeat(10) @(posedge mrx_clk);
13780
        repeat(15) @(posedge wb_clk);
13781
      end
13782
      // receiving frames and checking end of them
13783 243 tadejm
      frame_ended = 0;
13784
      check_frame = 0;
13785
      fork
13786
        begin
13787
          if (i_length[0] == 1'b0)
13788
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
13789
          else
13790
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
13791
          repeat(10) @(posedge mrx_clk);
13792
        end
13793 254 mohor
        begin: fr_end2
13794 243 tadejm
          wait (MRxDV === 1'b1); // start receive
13795
          #1 check_rx_bd(num_of_bd, data);
13796
          if (data[15] !== 1)
13797 209 tadejm
          begin
13798 243 tadejm
            test_fail("Wrong buffer descriptor's empty bit read out from MAC");
13799 209 tadejm
            fail = fail + 1;
13800
          end
13801 243 tadejm
          wait (MRxDV === 1'b0); // end receive
13802
          while ((data[15] === 1) && (check_frame == 0))
13803
          begin
13804
            #1 check_rx_bd(num_of_bd, data);
13805
            @(posedge wb_clk);
13806
          end
13807
          if (data[15] === 0)
13808
            frame_ended = 1;
13809
          repeat (1) @(posedge wb_clk);
13810 209 tadejm
        end
13811
        begin
13812 243 tadejm
          wait (MRxDV === 1'b1); // start receive
13813
          wait (MRxDV === 1'b0); // end receive
13814
          repeat(10) @(posedge mrx_clk);
13815
          repeat(15) @(posedge wb_clk);
13816
          check_frame = 1;
13817 209 tadejm
        end
13818 243 tadejm
      join
13819
      // check length of a PACKET
13820 252 tadejm
      if ( ((data[31:16] != (i_length + 4)) && (num_of_frames >= 3)) ||
13821
           ((data[31:16] != 0) && (num_of_frames < 3)) )
13822 243 tadejm
      begin
13823
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
13824
                        data[31:16], (i_length + 4));
13825
        test_fail("Wrong length of the packet out from PHY");
13826
        fail = fail + 1;
13827
      end
13828
      // check received RX packet data and CRC
13829 252 tadejm
      if ((frame_ended == 1) && (num_of_frames >= 5)) // 5 bytes is minimum size without CRC error, since
13830
      begin                                           // CRC has 4 bytes for itself
13831 243 tadejm
        if (i_length[0] == 1'b0)
13832 209 tadejm
        begin
13833 243 tadejm
          check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
13834 209 tadejm
        end
13835 243 tadejm
        else
13836 209 tadejm
        begin
13837 243 tadejm
          check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
13838 209 tadejm
        end
13839
        if (tmp > 0)
13840
        begin
13841 243 tadejm
          `TIME; $display("*E Wrong data of the received packet");
13842
          test_fail("Wrong data of the received packet");
13843 209 tadejm
          fail = fail + 1;
13844
        end
13845
      end
13846
      // check WB INT signal
13847 252 tadejm
      if (num_of_frames >= 3) // Frames smaller than 3 are not received.
13848
      begin                   // Frames greater then 5 always cause an interrupt (Frame received)
13849
        if (wb_int !== 1'b1)  // Frames with length 3 or 4 always cause an interrupt (CRC error)
13850 209 tadejm
        begin
13851
          `TIME; $display("*E WB INT signal should be set");
13852
          test_fail("WB INT signal should be set");
13853
          fail = fail + 1;
13854
        end
13855
      end
13856 252 tadejm
      else
13857 209 tadejm
      begin
13858
        if (wb_int !== 1'b0)
13859
        begin
13860
          `TIME; $display("*E WB INT signal should not be set");
13861
          test_fail("WB INT signal should not be set");
13862
          fail = fail + 1;
13863
        end
13864
      end
13865 243 tadejm
      // check RX buffer descriptor of a packet
13866
      if (num_of_frames >= min_tmp)
13867
      begin
13868 254 mohor
        if ( (data[15:0] !== 16'h6080) && // wrap bit
13869
             (data[15:0] !== 16'h4080) ) // without wrap bit
13870 209 tadejm
        begin
13871 243 tadejm
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13872
          test_fail("RX buffer descriptor status is not correct");
13873
          fail = fail + 1;
13874 209 tadejm
        end
13875 243 tadejm
      end
13876 254 mohor
      else if (num_of_frames > 6)
13877 243 tadejm
      begin
13878 254 mohor
        if ( (data[15:0] !== 16'h6084) && // wrap bit
13879
             (data[15:0] !== 16'h4084) ) // without wrap bit
13880
        begin
13881
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13882
          test_fail("RX buffer descriptor status is not correct");
13883
          fail = fail + 1;
13884
        end
13885
      end
13886
      else if (num_of_frames > 4) // MAC does not recognize Dest. ADDR. for lengths 5, 6 => no MISS
13887
      begin
13888 243 tadejm
        if ( (data[15:0] !== 16'h6004) && // wrap bit
13889
             (data[15:0] !== 16'h4004) ) // without wrap bit
13890 209 tadejm
        begin
13891 243 tadejm
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13892
          test_fail("RX buffer descriptor status is not correct");
13893
          fail = fail + 1;
13894 209 tadejm
        end
13895
      end
13896 254 mohor
      else if (num_of_frames > 2) // MAC does not recognize Dest. ADDR. for length 3, 4 => no MISS, CRC ERROR
13897 243 tadejm
      begin
13898
        if ( (data[15:0] !== 16'h6006) && // wrap bit
13899
             (data[15:0] !== 16'h4006) ) // without wrap bit
13900
        begin
13901
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13902
          test_fail("RX buffer descriptor status is not correct");
13903
          fail = fail + 1;
13904
        end
13905
      end
13906 209 tadejm
      else
13907
      begin
13908
        if (data[15] !== 1'b1)
13909
        begin
13910 243 tadejm
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13911
          test_fail("RX buffer descriptor status is not correct");
13912 209 tadejm
          fail = fail + 1;
13913
        end
13914
      end
13915
      // check interrupts
13916
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13917 252 tadejm
      if (num_of_frames >= 5)
13918 209 tadejm
      begin
13919 252 tadejm
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
13920 209 tadejm
        begin
13921 243 tadejm
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
13922
          test_fail("Interrupt Receive Buffer was not set");
13923 209 tadejm
          fail = fail + 1;
13924
        end
13925 243 tadejm
        if ((data & (~`ETH_INT_RXB)) !== 0)
13926 209 tadejm
        begin
13927 243 tadejm
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
13928
          test_fail("Other interrupts (except Receive Buffer) were set");
13929 209 tadejm
          fail = fail + 1;
13930
        end
13931
      end
13932 252 tadejm
      else if ((num_of_frames < 3)) // Frames smaller than 3 are not received.
13933
      begin
13934
        if (data) // Checking if any interrupt is pending)
13935
        begin
13936
          `TIME; $display("*E Interrupt(s) is(are) pending although frame was ignored, interrupt reg: %0h", data);
13937
          test_fail("Interrupts were set");
13938
          fail = fail + 1;
13939
        end
13940
      end
13941 209 tadejm
      else
13942
      begin
13943 252 tadejm
        if ((data & `ETH_INT_RXE) !== `ETH_INT_RXE)
13944 209 tadejm
        begin
13945 243 tadejm
          `TIME; $display("*E Interrupt Receive Buffer Error was not set, interrupt reg: %0h", data);
13946
          test_fail("Interrupt Receive Buffer Error was not set");
13947 209 tadejm
          fail = fail + 1;
13948
        end
13949 243 tadejm
        if ((data & (~`ETH_INT_RXE)) !== 0)
13950
        begin
13951
          `TIME; $display("*E Other interrupts (except Receive Buffer Error) were set, interrupt reg: %0h", data);
13952
          test_fail("Other interrupts (except Receive Buffer Error) were set");
13953
          fail = fail + 1;
13954
        end
13955 209 tadejm
      end
13956
      // clear interrupts
13957
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13958
      // check WB INT signal
13959
      if (wb_int !== 1'b0)
13960
      begin
13961
        test_fail("WB INT signal should not be set");
13962
        fail = fail + 1;
13963
      end
13964
      // INTERMEDIATE DISPLAYS
13965 243 tadejm
      if (num_of_frames == 3)
13966 209 tadejm
      begin
13967 243 tadejm
        $display("    using 1 BD out of 8 BDs (120..127) assigned to RX (wrap at 1st BD - RX BD 120)");
13968
        $display("    ->packets with lengths from %0d to %0d are not received (length increasing by 1 byte)",
13969 209 tadejm
                 0, 3);
13970
      end
13971 243 tadejm
      else if (num_of_frames == 9)
13972 209 tadejm
      begin
13973 243 tadejm
        $display("    using 1 BD out of 8 BDs (120..127) assigned to RX (wrap at 1st BD - RX BD 120)");
13974
        $display("    ->packet with length 4 is not received (length increasing by 1 byte)");
13975 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13976
                 5, 9);
13977
      end
13978 243 tadejm
      else if (num_of_frames == 17)
13979 209 tadejm
      begin
13980 243 tadejm
        $display("    using 4 BDs out of 8 BDs (120..127) assigned to RX (wrap at 4th BD - RX BD 123)");
13981 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13982
                 10, 17);
13983
      end
13984 243 tadejm
      else if (num_of_frames == 27)
13985 209 tadejm
      begin
13986 243 tadejm
        $display("    using 5 BDs out of 8 BDs (120..127) assigned to RX (wrap at 5th BD - RX BD 124)");
13987 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13988
                 18, 27);
13989
      end
13990 243 tadejm
      else if (num_of_frames == 40)
13991 209 tadejm
      begin
13992 243 tadejm
        $display("    using 6 BDs out of 8 BDs (120..127) assigned to RX (wrap at 6th BD - RX BD 125)");
13993 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13994
                 28, 40);
13995
      end
13996 243 tadejm
      else if (num_of_frames == 54)
13997 209 tadejm
      begin
13998 243 tadejm
        $display("    using 7 BDs out of 8 BDs (120..127) assigned to RX (wrap at 7th BD - RX BD 126)");
13999 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
14000
                 41, 54);
14001
      end
14002 243 tadejm
      else if (num_of_frames == 69)
14003 209 tadejm
      begin
14004 243 tadejm
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
14005 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
14006
                 55, 69);
14007
      end
14008 243 tadejm
      else if (num_of_frames == 69)
14009
      begin
14010
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
14011
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
14012
                 55, 69);
14013
      end
14014
      else if (num_of_frames == 77)
14015
      begin
14016
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
14017
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
14018
                 70, 77);
14019
      end
14020 209 tadejm
      // set length (loop variable)
14021
      i_length = i_length + 1;
14022
      // the number of frame transmitted
14023
      num_of_frames = num_of_frames + 1;
14024
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
14025
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
14026
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
14027 243 tadejm
          (num_of_frames == 54) || (num_of_frames == 62) || (num_of_frames == 70))
14028
        num_of_bd = 120;
14029 209 tadejm
      else
14030
        num_of_bd = num_of_bd + 1;
14031
    end
14032 243 tadejm
    // disable RX
14033 209 tadejm
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
14034
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14035
    @(posedge wb_clk);
14036
    if(fail == 0)
14037
      test_ok;
14038
    else
14039
      fail = 0;
14040
  end
14041
 
14042
 
14043 243 tadejm
  ////////////////////////////////////////////////////////////////////
14044
  ////                                                            ////
14045
  ////  Test receive packet synchronization with receive          ////
14046
  ////  disable/enable ( 10Mbps ).                                ////
14047
  ////                                                            ////
14048
  ////////////////////////////////////////////////////////////////////
14049
  if (test_num == 10) // Test no receive when all buffers are TX ( 10Mbps ).
14050
  begin
14051
    // TEST 10: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )
14052
    test_name   = "TEST 10: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )";
14053
    `TIME; $display("  TEST 10: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )");
14054 209 tadejm
 
14055 243 tadejm
 
14056
 
14057
 
14058
 
14059
 
14060
  end
14061
 
14062
 
14063
  ////////////////////////////////////////////////////////////////////
14064
  ////                                                            ////
14065
  ////  Test receive packet synchronization with receive          ////
14066
  ////  disable/enable ( 10Mbps ).                                ////
14067
  ////                                                            ////
14068
  ////////////////////////////////////////////////////////////////////
14069
  if (test_num == 12) // Test no receive when all buffers are TX ( 10Mbps ).
14070
  begin
14071
    // TEST 12: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )
14072
    test_name   = "TEST 12: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )";
14073
    `TIME; $display("  TEST 12: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )");
14074
 
14075
 
14076
 
14077
 
14078
 
14079
 
14080
  end
14081
 
14082
 
14083
  ////////////////////////////////////////////////////////////////////
14084
  ////                                                            ////
14085
  ////  Test receive packet synchronization with receive          ////
14086
  ////  disable/enable ( 10Mbps ).                                ////
14087
  ////                                                            ////
14088
  ////////////////////////////////////////////////////////////////////
14089
  if (test_num == 14) // Test no receive when all buffers are TX ( 10Mbps ).
14090
  begin
14091
    // TEST 14: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )
14092
    test_name   = "TEST 14: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )";
14093
    `TIME; $display("  TEST 14: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )");
14094
 
14095
 
14096
 
14097
 
14098
 
14099
 
14100
  end
14101
 
14102
 
14103
  ////////////////////////////////////////////////////////////////////
14104
  ////                                                            ////
14105
  ////  Test receive packet synchronization with receive          ////
14106
  ////  disable/enable ( 10Mbps ).                                ////
14107
  ////                                                            ////
14108
  ////////////////////////////////////////////////////////////////////
14109
  if (test_num == 16) // Test no receive when all buffers are TX ( 10Mbps ).
14110
  begin
14111
    // TEST 16: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )
14112
    test_name   = "TEST 16: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )";
14113
    `TIME; $display("  TEST 16: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )");
14114
 
14115
 
14116
 
14117
 
14118
 
14119
 
14120
  end
14121
 
14122
 
14123
 
14124 209 tadejm
end   //  for (test_num=start_task; test_num <= end_task; test_num=test_num+1)
14125
 
14126
end
14127
endtask // test_mac_full_duplex_receive
14128
 
14129
 
14130 263 mohor
task test_mac_full_duplex_flow_control;
14131 209 tadejm
  input  [31:0]  start_task;
14132
  input  [31:0]  end_task;
14133
  integer        bit_start_1;
14134
  integer        bit_end_1;
14135
  integer        bit_start_2;
14136
  integer        bit_end_2;
14137
  integer        num_of_reg;
14138
  integer        num_of_frames;
14139
  integer        num_of_bd;
14140
  integer        i_addr;
14141
  integer        i_data;
14142
  integer        i_length;
14143
  integer        tmp_len;
14144
  integer        tmp_bd;
14145
  integer        tmp_bd_num;
14146
  integer        tmp_data;
14147
  integer        tmp_ipgt;
14148
  integer        test_num;
14149
  reg    [31:0]  tx_bd_num;
14150
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_data;
14151
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_tmp_data;
14152
  integer        i;
14153
  integer        i1;
14154
  integer        i2;
14155
  integer        i3;
14156
  integer        fail;
14157
  integer        speed;
14158 260 mohor
  integer        mac_hi_addr;
14159
  integer        mac_lo_addr;
14160 209 tadejm
  reg            frame_started;
14161
  reg            frame_ended;
14162
  reg            wait_for_frame;
14163
  reg    [31:0]  addr;
14164
  reg    [31:0]  data;
14165
  reg    [31:0]  tmp;
14166
  reg    [ 7:0]  st_data;
14167
  reg    [15:0]  max_tmp;
14168
  reg    [15:0]  min_tmp;
14169 263 mohor
  reg            PassAll;
14170
  reg            RxFlow;
14171 266 mohor
  reg            enable_irq_in_rxbd;
14172 209 tadejm
begin
14173 263 mohor
// MAC FULL DUPLEX FLOW CONTROL TEST
14174
test_heading("MAC FULL DUPLEX FLOW CONTROL TEST");
14175 209 tadejm
$display(" ");
14176 263 mohor
$display("MAC FULL DUPLEX FLOW CONTROL TEST");
14177 209 tadejm
fail = 0;
14178
 
14179
// reset MAC registers
14180
hard_reset;
14181
// reset MAC and MII LOGIC with soft reset
14182
reset_mac;
14183
reset_mii;
14184
// set wb slave response
14185
wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
14186
 
14187
  /*
14188
  TASKS for set and control TX buffer descriptors (also send packet - set_tx_bd_ready):
14189
  -------------------------------------------------------------------------------------
14190
  set_tx_bd
14191
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0], len[15:0], irq, pad, crc, txpnt[31:0]);
14192
  set_tx_bd_wrap
14193
    (tx_bd_num_end[6:0]);
14194
  set_tx_bd_ready
14195
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
14196
  check_tx_bd
14197
    (tx_bd_num_start[6:0], tx_bd_status[31:0]);
14198
  clear_tx_bd
14199
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
14200
 
14201
  TASKS for set and control RX buffer descriptors:
14202
  ------------------------------------------------
14203
  set_rx_bd
14204
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0], irq, rxpnt[31:0]);
14205
  set_rx_bd_wrap
14206
    (rx_bd_num_end[6:0]);
14207
  set_rx_bd_empty
14208
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
14209
  check_rx_bd
14210
    (rx_bd_num_end[6:0], rx_bd_status);
14211
  clear_rx_bd
14212
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
14213
 
14214
  TASKS for set and check TX packets:
14215
  -----------------------------------
14216
  set_tx_packet
14217
    (txpnt[31:0], len[15:0], eth_start_data[7:0]);
14218
  check_tx_packet
14219
    (txpnt_wb[31:0], txpnt_phy[31:0], len[15:0], failure[31:0]);
14220
 
14221
  TASKS for set and check RX packets:
14222
  -----------------------------------
14223
  set_rx_packet
14224
    (rxpnt[31:0], len[15:0], plus_nibble, d_addr[47:0], s_addr[47:0], type_len[15:0], start_data[7:0]);
14225
  check_rx_packet
14226
    (rxpnt_phy[31:0], rxpnt_wb[31:0], len[15:0], plus_nibble, successful_nibble, failure[31:0]);
14227
 
14228
  TASKS for append and check CRC to/of TX packet:
14229
  -----------------------------------------------
14230
  append_tx_crc
14231
    (txpnt_wb[31:0], len[15:0], negated_crc);
14232
  check_tx_crc
14233
    (txpnt_phy[31:0], len[15:0], negated_crc, failure[31:0]);
14234
 
14235
  TASK for append CRC to RX packet (CRC is checked together with check_rx_packet):
14236
  --------------------------------------------------------------------------------
14237
  append_rx_crc
14238
    (rxpnt_phy[31:0], len[15:0], plus_nibble, negated_crc);
14239
  */
14240
 
14241
//////////////////////////////////////////////////////////////////////
14242
////                                                              ////
14243 263 mohor
////  test_mac_full_duplex_flow_control:                          ////
14244 209 tadejm
////                                                              ////
14245
////  0: Test                                                     ////
14246
////                                                              ////
14247
//////////////////////////////////////////////////////////////////////
14248 260 mohor
 
14249 209 tadejm
for (test_num = start_task; test_num <= end_task; test_num = test_num + 1)
14250
begin
14251
 
14252
  ////////////////////////////////////////////////////////////////////
14253
  ////                                                            ////
14254 254 mohor
  ////  Test inserts control frames while transmitting normal     ////
14255
  ////  frames. Using 4 TX buffer decriptors ( 10Mbps ).          ////
14256 209 tadejm
  ////                                                            ////
14257
  ////////////////////////////////////////////////////////////////////
14258 254 mohor
  if (test_num == 0) // 
14259 209 tadejm
  begin
14260 254 mohor
    // TEST 0: INSERT CONTROL FRM. WHILE TRANSMITTING NORMAL FRM. AT 4 TX BD ( 10Mbps )
14261
    test_name = "TEST 0: INSERT CONTROL FRM. WHILE TRANSMITTING NORMAL FRM. AT 4 TX BD ( 10Mbps )";
14262
    `TIME; $display("  TEST 0: INSERT CONTROL FRM. WHILE TRANSMITTING NORMAL FRM. AT 4 TX BD ( 10Mbps )");
14263 260 mohor
 
14264 254 mohor
    // reset MAC completely
14265
    hard_reset;
14266
    // set wb slave response
14267
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
14268 209 tadejm
 
14269 254 mohor
    max_tmp = 0;
14270
    min_tmp = 0;
14271
    // set 4 TX buffer descriptors - must be set before TX enable
14272
    wbm_write(`ETH_TX_BD_NUM, 32'h4, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14273
    // enable TX, set full-duplex mode, padding and CRC appending
14274
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
14275
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14276
    // enable TX flow control
14277
    wbm_write(`ETH_CTRLMODER, `ETH_CTRLMODER_TXFLOW, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14278 260 mohor
 
14279
    // Set MAC address
14280
    mac_hi_addr = 32'h00000001;
14281
    mac_lo_addr = 32'h02030405;
14282
    wbm_write(`ETH_MAC_ADDR1, mac_hi_addr, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14283
    wbm_write(`ETH_MAC_ADDR0, mac_lo_addr, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14284
 
14285 254 mohor
    // prepare two packets of MAXFL length
14286
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14287
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
14288
    min_tmp = tmp[31:16];
14289 260 mohor
    st_data = 8'h34;
14290 254 mohor
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC. Writing data to the memory
14291 260 mohor
    st_data = 8'h56;
14292 254 mohor
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp - 4), st_data); // length without CRC. Writing data to the memory
14293
    // check WB INT signal
14294
    if (wb_int !== 1'b0)
14295
    begin
14296
      test_fail("WB INT signal should not be set");
14297
      fail = fail + 1;
14298
    end
14299
 
14300
    // write to phy's control register for 10Mbps
14301
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
14302
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
14303
    speed = 10;
14304
 
14305
    frame_started = 0;
14306
    num_of_frames = 0;
14307
    num_of_bd = 0;
14308
    i_length = 0; // 0;
14309 209 tadejm
 
14310 254 mohor
 
14311 260 mohor
    // Initialize one part of memory with data of control packet
14312
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h0), 32'h0180c200, 4'hF);
14313
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h4), {16'h0001, mac_hi_addr[15:0]}, 4'hF);
14314
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h8), mac_lo_addr, 4'hF);
14315
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'hc), 32'h88080001, 4'hF);
14316
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h10), 32'h11110000, 4'hF);
14317
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h14), 32'h00000000, 4'hF);
14318
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h18), 32'h00000000, 4'hF);
14319
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h1c), 32'h00000000, 4'hF);
14320
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h20), 32'h00000000, 4'hF);
14321
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h24), 32'h00000000, 4'hF);
14322
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h28), 32'h00000000, 4'hF);
14323
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h2c), 32'h00000000, 4'hF);
14324
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h30), 32'h00000000, 4'hF);
14325
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h34), 32'h00000000, 4'hF);
14326
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h38), 32'h00000000, 4'hF);
14327
//    append_tx_crc(`MEMORY_BASE + 2 * max_tmp, 60, 0);       // CRC is appended after the data
14328
 
14329
 
14330 254 mohor
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14331
    // In the following section, control frame will be sent while no other transmission is in progress.//
14332
    // TXC interrupt won't be unmasked.                                                                //
14333
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14334
    wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14335
    if(data)
14336
      begin
14337
        test_fail("IRQ already pending!");
14338
        fail = fail + 1;
14339
        `TIME; $display("*E IRQ already pending!");
14340
      end
14341
 
14342
    if (wb_int)
14343
    begin
14344
      test_fail("WB INT signal should not be set!");
14345
      fail = fail + 1;
14346
      `TIME; $display("*E WB INT signal should not be set!");
14347
    end
14348
 
14349 260 mohor
    // first destination address on ethernet PHY
14350
    eth_phy.set_tx_mem_addr(0);
14351
    // Request sending the control frame with pause value = 0x1111
14352
    wbm_write(`ETH_TX_CTRL, `ETH_TX_CTRL_TXPAUSERQ | 32'h1111, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14353 254 mohor
 
14354
    wait (MTxEn === 1'b1); // start transmit
14355
    wait (MTxEn === 1'b0); // end transmit
14356
    repeat(10) @ (posedge wb_clk);  // wait some time
14357
    repeat(10) @ (posedge mtx_clk); // wait some time
14358
 
14359
    wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14360
    if(data !== `ETH_INT_TXC)
14361
      begin
14362
        test_fail("TXC IRQ should be set!");
14363
        fail = fail + 1;
14364
        `TIME; $display("*E TXC IRQ should be set!");
14365
      end
14366
 
14367
    if (wb_int)
14368
    begin
14369
      test_fail("WB INT signal should not be set because TXC irq is masked!");
14370
      fail = fail + 1;
14371
      `TIME; $display("*E WB INT signal should not be set because TXC irq is masked!");
14372
    end
14373
 
14374
    // Clear TXC interrupt
14375
    wbm_write(`ETH_INT, `ETH_INT_TXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14376
 
14377
    if (wb_int)
14378
    begin
14379
      test_fail("WB INT signal should not be set!");
14380
      fail = fail + 1;
14381
      `TIME; $display("*E WB INT signal should not be set!");
14382
    end
14383
 
14384 260 mohor
    check_tx_packet((`MEMORY_BASE + 2 * max_tmp), 0, 60, tmp);
14385
    if (tmp > 0)
14386
    begin
14387
      $display("Wrong data of the transmitted packet");
14388
      test_fail("Wrong data of the transmitted packet");
14389
      fail = fail + 1;
14390
    end
14391
    // check transmited TX packet CRC
14392
    #1 check_tx_crc(0, 60, 1'b0, tmp); // length without CRC
14393
    if (tmp > 0)
14394
    begin
14395
      $display("Wrong CRC of the transmitted packet");
14396
      test_fail("Wrong CRC of the transmitted packet");
14397
      fail = fail + 1;
14398
    end
14399 254 mohor
 
14400
 
14401
 
14402
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14403
    // In the following section, control frame will be sent while no other transmission is in progress.//
14404
    // TXC interrupt is unmasked.                                                                      //
14405
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14406
 
14407
    // unmask all interrupts
14408
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
14409
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14410
 
14411
    wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14412
    if(data)
14413
      begin
14414
        test_fail("IRQ already pending!");
14415
        fail = fail + 1;
14416
        `TIME; $display("*E IRQ already pending!");
14417
      end
14418
 
14419
    if (wb_int)
14420
    begin
14421
      test_fail("WB INT signal should not be set!");
14422
      fail = fail + 1;
14423
      `TIME; $display("*E WB INT signal should not be set!");
14424
    end
14425
 
14426
    // unmask only TXC interrupts
14427
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14428
 
14429 260 mohor
    // first destination address on ethernet PHY
14430
    eth_phy.set_tx_mem_addr(0);
14431
    // Request sending the control frame with pause value = 0x2222
14432
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h10), 32'h22220000, 4'hF);  // Just for data test
14433
    wbm_write(`ETH_TX_CTRL, `ETH_TX_CTRL_TXPAUSERQ | 32'h2222, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14434 254 mohor
 
14435
    wait (MTxEn === 1'b1); // start transmit
14436
    wait (MTxEn === 1'b0); // end transmit
14437
    repeat(10) @ (posedge wb_clk);  // wait some time
14438
    repeat(10) @ (posedge mtx_clk); // wait some time
14439
 
14440
    wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14441
    if(data !== `ETH_INT_TXC)
14442
      begin
14443
        test_fail("TXC IRQ should be set!");
14444
        fail = fail + 1;
14445
        `TIME; $display("*E TXC IRQ should be set!");
14446
      end
14447
 
14448
    if (!wb_int)
14449
    begin
14450
      test_fail("WB INT signal should be set!");
14451
      fail = fail + 1;
14452
      `TIME; $display("*E WB INT signal should be set!");
14453
    end
14454
 
14455
    // Clear TXC interrupt
14456
    wbm_write(`ETH_INT, `ETH_INT_TXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14457
 
14458
    if (wb_int)
14459
    begin
14460
      test_fail("WB INT signal should not be set!");
14461
      fail = fail + 1;
14462
      `TIME; $display("*E WB INT signal should not be set!");
14463
    end
14464
 
14465
 
14466 260 mohor
    check_tx_packet((`MEMORY_BASE + 2 * max_tmp), 0, 60, tmp);
14467
    if (tmp > 0)
14468
    begin
14469
      $display("Wrong data of the transmitted packet");
14470
      test_fail("Wrong data of the transmitted packet");
14471
      fail = fail + 1;
14472
    end
14473
    // check transmited TX packet CRC
14474
    #1 check_tx_crc(0, 60, 1'b0, tmp); // length without CRC
14475
    if (tmp > 0)
14476
    begin
14477
      $display("Wrong CRC of the transmitted packet");
14478
      test_fail("Wrong CRC of the transmitted packet");
14479
      fail = fail + 1;
14480
    end
14481 254 mohor
 
14482 260 mohor
 
14483 254 mohor
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14484 260 mohor
    // In the following section, control frame sending is requested while no other transmission        //
14485
    // is in progress. TXC interrupt is unmasked.                                                      //
14486 254 mohor
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14487
 
14488
    // unmask all interrupts
14489
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
14490
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14491
 
14492
    wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14493
    if(data)
14494
      begin
14495
        test_fail("IRQ already pending!");
14496
        fail = fail + 1;
14497
        `TIME; $display("*E IRQ already pending!");
14498
      end
14499
 
14500
    if (wb_int)
14501
    begin
14502
      test_fail("WB INT signal should not be set!");
14503
      fail = fail + 1;
14504
      `TIME; $display("*E WB INT signal should not be set!");
14505
    end
14506
 
14507
 
14508 260 mohor
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14509
    // In the following section, control frame request and data send request are both set. At the      //
14510
    // beginning control frame request will be faster than data send request, later the opposite.      //
14511
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14512
    for (i=0; i<32; i=i+1)
14513
    begin
14514
      // Request sending the control frame with pause value = 0x5678
14515
      set_tx_bd(0, 0, 16'h100, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE)); // irq, pad, crc
14516
      set_tx_bd_wrap(0);
14517
      // first destination address on ethernet PHY
14518
      eth_phy.set_tx_mem_addr(0);
14519
      set_tx_bd_ready(0, 0);
14520
 
14521
      wait (MTxEn === 1'b1); // start transmit
14522
 
14523
      repeat(i) @ (posedge mtx_clk);  // We need to wait some time until TX module starts using the data (preamble stage is over)
14524
 
14525
      // Send control frame request
14526
      wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h10), {i[3:0], i[3:0], i[3:0], i[3:0], 16'h0}, 4'hF);  // Just for data test
14527
      wbm_write(`ETH_TX_CTRL, `ETH_TX_CTRL_TXPAUSERQ | {16'h0, i[3:0], i[3:0], i[3:0], i[3:0]}, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14528
 
14529
      wait (MTxEn === 1'b0); // Wait until data frame transmission is over
14530
      repeat(10) @ (posedge mtx_clk); // wait some time so status is written
14531
      tmp_len = eth_phy.tx_len; // the length of a packet which was sent out first!!!
14532
      repeat(10) @ (posedge wb_clk);  // wait some time so status is written
14533
 
14534
      // first destination address on ethernet PHY
14535
      eth_phy.set_tx_mem_addr(0);
14536
 
14537
      if(tmp_len == 64)  // Control frame
14538 254 mohor
      begin
14539 260 mohor
        wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14540
        if(data !== `ETH_INT_TXC)
14541
        begin
14542
          test_fail("TXC IRQ should be set!");
14543
          fail = fail + 1;
14544
          `TIME; $display("*E TXC IRQ should be set!");
14545
          `TIME; $display("ETH_INT = 0x%0x", data);
14546
        end
14547
      end
14548
      else
14549
      begin
14550
        wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14551
        if(data !== `ETH_INT_TXB)
14552
        begin
14553
          test_fail("TXB IRQ should be set!");
14554
          fail = fail + 1;
14555
          `TIME; $display("*E TXB IRQ should be set!");
14556
          `TIME; $display("ETH_INT = 0x%0x", data);
14557
        end
14558
      end
14559
 
14560
      if(tmp_len == 64)  // Control frame
14561
        check_tx_packet((`MEMORY_BASE + 2 * max_tmp), 0, 60, tmp);
14562
      else
14563
        check_tx_packet((`MEMORY_BASE), 0, 32'h100, tmp);
14564
 
14565
      if (tmp > 0)
14566
      begin
14567
        $display("Wrong data of the transmitted packet");
14568
        test_fail("Wrong data of the transmitted packet");
14569 254 mohor
        fail = fail + 1;
14570
      end
14571 260 mohor
 
14572
      // check transmited TX packet CRC
14573
      if(tmp_len == 64)  // Control frame
14574
        #1 check_tx_crc(0, 60, 1'b0, tmp); // length without CRC
14575
      else
14576
        #1 check_tx_crc(0, 32'h100, 1'b0, tmp); // length without CRC
14577
 
14578
      if (tmp > 0)
14579 254 mohor
      begin
14580 260 mohor
        $display("Wrong CRC of the transmitted packet");
14581
        test_fail("Wrong CRC of the transmitted packet");
14582
        fail = fail + 1;
14583
      end
14584
 
14585
 
14586
      wait (MTxEn === 1'b1); // start transmit of the control frame
14587
      wait (MTxEn === 1'b0); // end transmit of the control frame
14588
      repeat(10) @ (posedge wb_clk);  // wait some time
14589
      repeat(10) @ (posedge mtx_clk); // wait some time so status is written
14590
 
14591
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14592
      if(data !== (`ETH_INT_TXC | `ETH_INT_TXB))
14593
      begin
14594 254 mohor
        test_fail("TXC and TXB IRQ should be set!");
14595
        fail = fail + 1;
14596 260 mohor
        `TIME; $display("*E TXC and TXB IRQ should be set! (ETH_INT=0x%0x)", data);
14597 254 mohor
      end
14598 260 mohor
 
14599
      if (!wb_int)
14600
      begin
14601
        test_fail("WB INT signal should be set!");
14602
        fail = fail + 1;
14603
        `TIME; $display("*E WB INT signal should be set!");
14604
      end
14605
 
14606
      // Clear TXC and TXB interrupt
14607
      wbm_write(`ETH_INT, `ETH_INT_TXC | `ETH_INT_TXB, 4'hF, 1, 4'h0, 4'h0);
14608
 
14609
      if (wb_int)
14610
      begin
14611
        test_fail("WB INT signal should not be set!");
14612
        fail = fail + 1;
14613
        `TIME; $display("*E WB INT signal should not be set!");
14614
      end
14615
 
14616
      if(tmp_len == 64)  // Control frame
14617
        check_tx_packet((`MEMORY_BASE), 0, 32'h100, tmp);
14618
      else
14619
        check_tx_packet((`MEMORY_BASE + 2 * max_tmp), 0, 60, tmp);
14620
 
14621
      if (tmp > 0)
14622
      begin
14623
        $display("Wrong data of the transmitted packet");
14624
        test_fail("Wrong data of the transmitted packet");
14625
        fail = fail + 1;
14626
      end
14627
 
14628
      // check transmited TX packet CRC
14629
      if(tmp_len == 64)  // Control frame
14630
        #1 check_tx_crc(0, 32'h100, 1'b0, tmp); // length without CRC
14631
      else
14632
        #1 check_tx_crc(0, 60, 1'b0, tmp); // length without CRC
14633
 
14634
      if (tmp > 0)
14635
      begin
14636
        $display("Wrong CRC of the transmitted packet");
14637
        test_fail("Wrong CRC of the transmitted packet");
14638
        fail = fail + 1;
14639
      end
14640
    end // for loop
14641 254 mohor
 
14642 260 mohor
    if(fail)
14643 254 mohor
    begin
14644 260 mohor
      test_name = "TEST 0: FINISHED WITH ERRORS";
14645
      `TIME; $display("  TEST 0: FINISHED WITH ERRORS");
14646 254 mohor
    end
14647 260 mohor
    else
14648 254 mohor
    begin
14649 260 mohor
      test_name = "TEST 0: SUCCESSFULLY FINISHED";
14650
      `TIME; $display("  TEST 0: SUCCESSFULLY FINISHED");
14651 254 mohor
    end
14652
 
14653 260 mohor
    if(fail == 0)
14654
      test_ok;
14655
    else
14656
      fail = 0;
14657 254 mohor
 
14658 260 mohor
  end
14659 254 mohor
 
14660
 
14661 263 mohor
  if (test_num == 1) // 
14662
  begin
14663
    #1;
14664
    // TEST 1: INSERT CONTROL FRM. WHILE TRANSMITTING NORMAL FRM. AT 4 TX BD ( 100Mbps )
14665
    test_name = "TEST 1: INSERT CONTROL FRM. WHILE TRANSMITTING NORMAL FRM. AT 4 TX BD ( 100Mbps )";
14666
    `TIME; $display("  TEST 1: INSERT CONTROL FRM. WHILE TRANSMITTING NORMAL FRM. AT 4 TX BD ( 100Mbps )");
14667
  end
14668 254 mohor
 
14669 260 mohor
  ////////////////////////////////////////////////////////////////////
14670
  ////                                                            ////
14671
  ////  Receive control frames with PASSALL option turned off     ////
14672
  ////  Using only one RX buffer decriptor ( 10Mbps ).            ////
14673
  ////                                                            ////
14674
  ////////////////////////////////////////////////////////////////////
14675 263 mohor
  if (test_num == 2) // 
14676 260 mohor
  begin
14677 263 mohor
    // TEST 2: RECEIVE CONTROL FRAMES WITH PASSALL OPTION TURNED OFF AT ONE RX BD ( 10Mbps )
14678
    test_name   = "TEST 2: RECEIVE CONTROL FRAMES WITH PASSALL OPTION TURNED OFF AT ONE RX BD ( 10Mbps )";
14679
    `TIME; $display("  TEST 2: RECEIVE CONTROL FRAMES WITH PASSALL OPTION TURNED OFF AT ONE RX BD ( 10Mbps )");
14680 254 mohor
 
14681 260 mohor
    // unmask interrupts
14682
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
14683
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14684
    // set 1 RX buffer descriptor (8'h80 - 1) - must be set before RX enable
14685
    wbm_write(`ETH_TX_BD_NUM, 32'h7F, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14686
    // enable RX, set full-duplex mode, NO receive small, NO correct IFG
14687
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
14688
              `ETH_MODER_PRO | `ETH_MODER_BRO,
14689
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14690 263 mohor
    // enable RX_FLOW control
14691
    wbm_write(`ETH_CTRLMODER, `ETH_CTRLMODER_RXFLOW, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14692 260 mohor
    // prepare one control (PAUSE)packet
14693
    st_data = 8'h00;
14694
    set_rx_packet(0, 60, 1'b0, 48'h0180_c200_0001, 48'h0708_090A_0B0C, 16'h8808, st_data); // length without CRC
14695 263 mohor
    append_rx_crc (0, 60, 1'b0, 1'b0); // CRC for control packet
14696 260 mohor
    // prepare one packet of 100 bytes long
14697
    st_data = 8'h1A;
14698
    set_rx_packet(64, 100, 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
14699 263 mohor
    append_rx_crc (64, 100, 1'b0, 1'b0); // CRC for data packet
14700 260 mohor
    // check WB INT signal
14701
    if (wb_int !== 1'b0)
14702
    begin
14703
      test_fail("WB INT signal should not be set");
14704
      fail = fail + 1;
14705
    end
14706
 
14707
    // write to phy's control register for 10Mbps
14708
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
14709
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
14710
    speed = 10;
14711 254 mohor
 
14712 266 mohor
    // RXB and RXC interrupts masked
14713
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXE | `ETH_INT_BUSY |
14714
                             `ETH_INT_TXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14715
 
14716
    // Test irq logic while RXB and RXC interrupts are masked. IRQ in RxBD is cleared
14717
    for (i=0; i<3; i=i+1)
14718
    begin
14719
      // choose generating carrier sense and collision for first and last 64 lengths of frames
14720
      case (i)
14721
      0: // PASSALL = 0, RXFLOW = 1, IRQ in RxBD = 1
14722
      begin
14723
        PassAll=0; RxFlow=1; enable_irq_in_rxbd=1;
14724
        // enable interrupt generation
14725
        set_rx_bd(127, 127, 1'b1, `MEMORY_BASE);
14726
        // Set PASSALL = 0 and RXFLOW = 0
14727
        wbm_write(`ETH_CTRLMODER, `ETH_CTRLMODER_RXFLOW, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14728
      end
14729
      1: // PASSALL = 1, RXFLOW = 0, IRQ in RxBD = 1
14730
      begin
14731
        PassAll=1; RxFlow=0; enable_irq_in_rxbd=1;
14732
        // enable interrupt generation
14733
        set_rx_bd(127, 127, 1'b1, `MEMORY_BASE);
14734
        // Set PASSALL = 0 and RXFLOW = 0
14735
        wbm_write(`ETH_CTRLMODER, `ETH_CTRLMODER_PASSALL, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14736
      end
14737
      2: // PASSALL = 1, RXFLOW = 0, IRQ in RxBD = 0
14738
      begin
14739
        PassAll=1; RxFlow=0; enable_irq_in_rxbd=0;
14740
        // enable interrupt generation
14741
        set_rx_bd(127, 127, 1'b0, `MEMORY_BASE);
14742
        // Set PASSALL = 0 and RXFLOW = 0
14743
        wbm_write(`ETH_CTRLMODER, `ETH_CTRLMODER_PASSALL, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14744
      end
14745
      default:
14746
      begin
14747
        $display("*E We should never get here !!!");
14748
        test_fail("We should never get here !!!");
14749
        fail = fail + 1;
14750
      end
14751
      endcase
14752
 
14753
      // not detect carrier sense in FD and no collision
14754
      eth_phy.no_carrier_sense_rx_fd_detect(0);
14755
      eth_phy.collision(0);
14756
 
14757
      // set wrap bit and empty bit
14758
      set_rx_bd_wrap(127);
14759
      set_rx_bd_empty(127, 127);
14760
 
14761
      fork
14762
        begin
14763
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, 64, 1'b0);
14764
          repeat(10) @(posedge mrx_clk);
14765
        end
14766
        begin
14767
          #1 check_rx_bd(127, data);
14768
          wait (MRxDV === 1'b1); // start transmit
14769
          #1 check_rx_bd(127, data);
14770
          if (data[15] !== 1)
14771
          begin
14772
            $display("*E Wrong buffer descriptor's ready bit read out from MAC");
14773
            test_fail("Wrong buffer descriptor's ready bit read out from MAC");
14774
            fail = fail + 1;
14775
          end
14776
 
14777
          wait (MRxDV === 1'b0); // end transmit
14778
          repeat(50) @(posedge mrx_clk);  // Wait some time so frame is received and
14779
          repeat (100) @(posedge wb_clk); // status/irq is written.
14780
        end
14781
      join
14782
 
14783
      #1 check_rx_bd(127, data);
14784
      // Checking buffer descriptor
14785
      if(PassAll)
14786
      begin
14787
        if(enable_irq_in_rxbd)
14788
        begin
14789
          if(data !== 32'h406100)    // Rx BD must not be marked as EMPTY (control frame is received)
14790
          begin
14791
            $display("*E Rx BD is not OK. Control frame should be received because PASSALL bit is 1");
14792
            $display("RxBD = 0x%0x", data);
14793
            test_fail("Rx BD is not OK. Control frame should be received because PASSALL bit is 1");
14794
            fail = fail + 1;
14795
          end
14796
        end
14797
        else
14798
        begin
14799
          if(data !== 32'h402100)    // Rx BD must not be marked as EMPTY (control frame is received)
14800
          begin
14801
            $display("*E Rx BD is not OK. Control frame should be received because PASSALL bit is 1");
14802
            $display("RxBD = 0x%0x", data);
14803
            test_fail("Rx BD is not OK. Control frame should be received because PASSALL bit is 1");
14804
            fail = fail + 1;
14805
          end
14806
        end
14807
      end
14808
      else
14809
      begin
14810
        if(data !== 32'he000)    // Rx BD must be marked as EMPTY (no packet received)
14811
        begin
14812
          $display("*E Rx BD should be marked as EMPTY because a control frame was received while PASSALL bit is 0");
14813
          $display("RxBD = 0x%0x", data);
14814
          test_fail("Rx BD should be marked as EMPTY because a control frame was received while PASSALL bit is 0");
14815
          fail = fail + 1;
14816
        end
14817
      end
14818
 
14819
      // Checking if interrupt was generated
14820
      if (wb_int)
14821
      begin
14822
        `TIME; $display("*E WB INT signal should not be set because both RXB and RXC interrupts are masked");
14823
        test_fail("WB INT signal should not be set because both RXB and RXC interrupts are masked");
14824
        fail = fail + 1;
14825
      end
14826
 
14827
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14828
      if(RxFlow)
14829
      begin
14830
        if(data !== (`ETH_INT_RXC))
14831
        begin
14832
          test_fail("RXC is not set or multiple IRQs active!");
14833
          fail = fail + 1;
14834
          `TIME; $display("*E RXC is not set or multiple IRQs active! (ETH_INT=0x%0x)", data);
14835
        end
14836
        // Clear RXC interrupt
14837
        wbm_write(`ETH_INT, `ETH_INT_RXC, 4'hF, 1, 4'h0, 4'h0);
14838
      end
14839
      else if(enable_irq_in_rxbd)
14840
      begin
14841
        if(data !== (`ETH_INT_RXB))
14842
        begin
14843
          test_fail("RXB is not set or multiple IRQs active!");
14844
          fail = fail + 1;
14845
          `TIME; $display("*E RXB is not set or multiple IRQs active! (ETH_INT=0x%0x)", data);
14846
        end
14847
        // Clear RXC interrupt
14848
        wbm_write(`ETH_INT, `ETH_INT_RXB, 4'hF, 1, 4'h0, 4'h0);
14849
      end
14850
      else
14851
      begin
14852
        if(data !== 0)
14853
        begin
14854
          test_fail("Some IRQs is active!");
14855
          fail = fail + 1;
14856
          `TIME; $display("*E Some IRQs is active! (ETH_INT=0x%0x)", data);
14857
        end
14858
      end
14859
    end
14860
    // End: Test is irq is set while RXB and RXC interrupts are masked.
14861
 
14862
 
14863
 
14864
    // Now all interrupts are unmasked. Performing tests again.
14865 263 mohor
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
14866
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14867
 
14868 260 mohor
    for (i=0; i<4; i=i+1)
14869 254 mohor
    begin
14870 260 mohor
      // choose generating carrier sense and collision for first and last 64 lengths of frames
14871
      case (i)
14872 263 mohor
      0: // PASSALL = 0, RXFLOW = 0
14873 254 mohor
      begin
14874 263 mohor
        PassAll=0; RxFlow=0;
14875 260 mohor
        // enable interrupt generation
14876 263 mohor
        set_rx_bd(127, 127, 1'b1, `MEMORY_BASE);
14877
        // Set PASSALL = 0 and RXFLOW = 0
14878
        wbm_write(`ETH_CTRLMODER, 0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14879 254 mohor
      end
14880 263 mohor
      1: // PASSALL = 0, RXFLOW = 1
14881 254 mohor
      begin
14882 263 mohor
        PassAll=0; RxFlow=1;
14883 260 mohor
        // enable interrupt generation
14884 263 mohor
        set_rx_bd(127, 127, 1'b1, `MEMORY_BASE);
14885
        // Set PASSALL = 0 and RXFLOW = 0
14886
        wbm_write(`ETH_CTRLMODER, `ETH_CTRLMODER_RXFLOW, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14887 254 mohor
      end
14888 263 mohor
      2: // PASSALL = 1, RXFLOW = 0
14889 254 mohor
      begin
14890 263 mohor
        PassAll=1; RxFlow=0;
14891
        // enable interrupt generation
14892
        set_rx_bd(127, 127, 1'b1, `MEMORY_BASE);
14893
        // Set PASSALL = 0 and RXFLOW = 0
14894
        wbm_write(`ETH_CTRLMODER, `ETH_CTRLMODER_PASSALL, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14895 254 mohor
      end
14896 263 mohor
      default: // 3: PASSALL = 1, RXFLOW = 1
14897 254 mohor
      begin
14898 263 mohor
        PassAll=1; RxFlow=1;
14899
        // enable interrupt generation
14900
        set_rx_bd(127, 127, 1'b1, `MEMORY_BASE);
14901
        // Set PASSALL = 1 and RXFLOW = 1
14902
        wbm_write(`ETH_CTRLMODER, `ETH_CTRLMODER_PASSALL | `ETH_CTRLMODER_RXFLOW, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14903 254 mohor
      end
14904
      endcase
14905 260 mohor
 
14906 263 mohor
      // not detect carrier sense in FD and no collision
14907
      eth_phy.no_carrier_sense_rx_fd_detect(0);
14908
      eth_phy.collision(0);
14909
 
14910
      // set wrap bit and empty bit
14911 260 mohor
      set_rx_bd_wrap(127);
14912
      set_rx_bd_empty(127, 127);
14913 263 mohor
 
14914 260 mohor
      fork
14915 254 mohor
        begin
14916 260 mohor
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, 64, 1'b0);
14917
          repeat(10) @(posedge mrx_clk);
14918 254 mohor
        end
14919
        begin
14920 260 mohor
          #1 check_rx_bd(127, data);
14921
          wait (MRxDV === 1'b1); // start transmit
14922 263 mohor
          #1 check_rx_bd(127, data);
14923 260 mohor
          if (data[15] !== 1)
14924
          begin
14925 263 mohor
            $display("*E Wrong buffer descriptor's ready bit read out from MAC");
14926 260 mohor
            test_fail("Wrong buffer descriptor's ready bit read out from MAC");
14927
            fail = fail + 1;
14928
          end
14929 263 mohor
 
14930 260 mohor
          wait (MRxDV === 1'b0); // end transmit
14931 263 mohor
          repeat(50) @(posedge mrx_clk);  // Wait some time so frame is received and
14932
          repeat (100) @(posedge wb_clk); // status/irq is written.
14933 254 mohor
        end
14934 260 mohor
      join
14935 263 mohor
 
14936
      #1 check_rx_bd(127, data);
14937
 
14938
      // Checking buffer descriptor
14939
      if(PassAll)
14940 254 mohor
      begin
14941 263 mohor
        if(data !== 32'h406100)    // Rx BD must not be marked as EMPTY (control frame is received)
14942
        begin
14943
          $display("*E Rx BD is not OK. Control frame should be received because PASSALL bit is 1");
14944
          $display("RxBD = 0x%0x", data);
14945
          test_fail("Rx BD is not OK. Control frame should be received because PASSALL bit is 1");
14946
          fail = fail + 1;
14947
        end
14948 254 mohor
      end
14949 260 mohor
      else
14950 254 mohor
      begin
14951 263 mohor
        if(data !== 32'he000)    // Rx BD must be marked as EMPTY (no packet received)
14952
        begin
14953
          $display("*E Rx BD should be marked as EMPTY because a control frame was received while PASSALL bit is 0");
14954
          $display("RxBD = 0x%0x", data);
14955
          test_fail("Rx BD should be marked as EMPTY because a control frame was received while PASSALL bit is 0");
14956
          fail = fail + 1;
14957
        end
14958 254 mohor
      end
14959 263 mohor
 
14960
      // Checking if interrupt was generated
14961
      if(RxFlow || PassAll)
14962 254 mohor
      begin
14963 263 mohor
        if (!wb_int)
14964 254 mohor
        begin
14965
          `TIME; $display("*E WB INT signal should be set");
14966
          test_fail("WB INT signal should be set");
14967
          fail = fail + 1;
14968
        end
14969
      end
14970
      else
14971
      begin
14972 263 mohor
        if (wb_int)
14973 254 mohor
        begin
14974
          `TIME; $display("*E WB INT signal should not be set");
14975
          test_fail("WB INT signal should not be set");
14976
          fail = fail + 1;
14977
        end
14978
      end
14979 263 mohor
 
14980
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14981
      if(RxFlow)
14982 254 mohor
      begin
14983 263 mohor
        if(data !== (`ETH_INT_RXC))
14984 254 mohor
        begin
14985 263 mohor
          test_fail("RXC is not set or multiple IRQs active!");
14986 260 mohor
          fail = fail + 1;
14987 263 mohor
          `TIME; $display("*E RXC is not set or multiple IRQs active! (ETH_INT=0x%0x)", data);
14988 254 mohor
        end
14989 263 mohor
        // Clear RXC interrupt
14990
        wbm_write(`ETH_INT, `ETH_INT_RXC, 4'hF, 1, 4'h0, 4'h0);
14991 254 mohor
      end
14992 263 mohor
      else if(PassAll)
14993 254 mohor
      begin
14994 263 mohor
        if(data !== (`ETH_INT_RXB))
14995 254 mohor
        begin
14996 263 mohor
          test_fail("RXB is not set or multiple IRQs active!");
14997 254 mohor
          fail = fail + 1;
14998 263 mohor
          `TIME; $display("*E RXB is not set or multiple IRQs active! (ETH_INT=0x%0x)", data);
14999 254 mohor
        end
15000 263 mohor
        // Clear RXB interrupt
15001
        wbm_write(`ETH_INT, `ETH_INT_RXB, 4'hF, 1, 4'h0, 4'h0);
15002 254 mohor
      end
15003
      else
15004
      begin
15005 263 mohor
        if(data !== 0)
15006 254 mohor
        begin
15007 263 mohor
          test_fail("No interrupt should be set!");
15008 254 mohor
          fail = fail + 1;
15009 263 mohor
          `TIME; $display("*E No interrupt should be set! (ETH_INT=0x%0x)", data);
15010 254 mohor
        end
15011
      end
15012
    end
15013 266 mohor
 
15014 263 mohor
 
15015 260 mohor
    // disable RX
15016
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
15017
              `ETH_MODER_PRO | `ETH_MODER_BRO,
15018 254 mohor
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15019
    if(fail == 0)
15020
      test_ok;
15021
    else
15022
      fail = 0;
15023 209 tadejm
  end
15024
 
15025
 
15026
end   //  for (test_num=start_task; test_num <= end_task; test_num=test_num+1)
15027
 
15028 260 mohor
 
15029 209 tadejm
end
15030 263 mohor
endtask // test_mac_full_duplex_flow_control
15031 209 tadejm
 
15032
 
15033 169 mohor
//////////////////////////////////////////////////////////////
15034
// WB Behavioral Models Basic tasks
15035
//////////////////////////////////////////////////////////////
15036
 
15037
task wbm_write;
15038
  input  [31:0] address_i;
15039
  input  [((`MAX_BLK_SIZE * 32) - 1):0] data_i;
15040
  input  [3:0]  sel_i;
15041
  input  [31:0] size_i;
15042
  input  [3:0]  init_waits_i;
15043
  input  [3:0]  subseq_waits_i;
15044
 
15045
  reg `WRITE_STIM_TYPE write_data;
15046
  reg `WB_TRANSFER_FLAGS flags;
15047
  reg `WRITE_RETURN_TYPE write_status;
15048
  integer i;
15049
begin
15050
  write_status = 0;
15051
 
15052
  flags                    = 0;
15053
  flags`WB_TRANSFER_SIZE   = size_i;
15054
  flags`INIT_WAITS         = init_waits_i;
15055
  flags`SUBSEQ_WAITS       = subseq_waits_i;
15056
 
15057
  write_data               = 0;
15058
  write_data`WRITE_DATA    = data_i[31:0];
15059
  write_data`WRITE_ADDRESS = address_i;
15060
  write_data`WRITE_SEL     = sel_i;
15061
 
15062
  for (i = 0; i < size_i; i = i + 1)
15063
  begin
15064
    wb_master.blk_write_data[i] = write_data;
15065
    data_i                      = data_i >> 32;
15066
    write_data`WRITE_DATA       = data_i[31:0];
15067
    write_data`WRITE_ADDRESS    = write_data`WRITE_ADDRESS + 4;
15068 116 mohor
  end
15069
 
15070 169 mohor
  wb_master.wb_block_write(flags, write_status);
15071 116 mohor
 
15072 169 mohor
  if (write_status`CYC_ACTUAL_TRANSFER !== size_i)
15073
  begin
15074
    `TIME;
15075
    $display("*E WISHBONE Master was unable to complete the requested write operation to MAC!");
15076
  end
15077
end
15078
endtask // wbm_write
15079 116 mohor
 
15080 169 mohor
task wbm_read;
15081
  input  [31:0] address_i;
15082
  output [((`MAX_BLK_SIZE * 32) - 1):0] data_o;
15083
  input  [3:0]  sel_i;
15084
  input  [31:0] size_i;
15085
  input  [3:0]  init_waits_i;
15086
  input  [3:0]  subseq_waits_i;
15087
 
15088
  reg `READ_RETURN_TYPE read_data;
15089
  reg `WB_TRANSFER_FLAGS flags;
15090
  reg `READ_RETURN_TYPE read_status;
15091
  integer i;
15092
begin
15093
  read_status = 0;
15094
  data_o      = 0;
15095
 
15096
  flags                  = 0;
15097
  flags`WB_TRANSFER_SIZE = size_i;
15098
  flags`INIT_WAITS       = init_waits_i;
15099
  flags`SUBSEQ_WAITS     = subseq_waits_i;
15100
 
15101
  read_data              = 0;
15102
  read_data`READ_ADDRESS = address_i;
15103
  read_data`READ_SEL     = sel_i;
15104
 
15105
  for (i = 0; i < size_i; i = i + 1)
15106 116 mohor
  begin
15107 169 mohor
    wb_master.blk_read_data_in[i] = read_data;
15108
    read_data`READ_ADDRESS        = read_data`READ_ADDRESS + 4;
15109
  end
15110
 
15111
  wb_master.wb_block_read(flags, read_status);
15112
 
15113
  if (read_status`CYC_ACTUAL_TRANSFER !== size_i)
15114
  begin
15115
    `TIME;
15116
    $display("*E WISHBONE Master was unable to complete the requested read operation from MAC!");
15117
  end
15118
 
15119
  for (i = 0; i < size_i; i = i + 1)
15120
  begin
15121
    data_o       = data_o << 32;
15122
    read_data    = wb_master.blk_read_data_out[(size_i - 1) - i]; // [31 - i];
15123
    data_o[31:0] = read_data`READ_DATA;
15124
  end
15125
end
15126
endtask // wbm_read
15127
 
15128
 
15129
//////////////////////////////////////////////////////////////
15130
// Ethernet Basic tasks
15131
//////////////////////////////////////////////////////////////
15132
 
15133
task hard_reset; //  MAC registers
15134
begin
15135
  // reset MAC registers
15136
  @(posedge wb_clk);
15137
  #2 wb_rst = 1'b1;
15138
  repeat(2) @(posedge wb_clk);
15139
  #2 wb_rst = 1'b0;
15140
end
15141
endtask // hard_reset
15142
 
15143
task reset_mac; //  MAC module
15144
  reg [31:0] tmp;
15145
  reg [31:0] tmp_no_rst;
15146
begin
15147
  // read MODER register first
15148
  wbm_read(`ETH_MODER, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15149
  // set reset bit - write back to MODER register with RESET bit
15150
  wbm_write(`ETH_MODER, (`ETH_MODER_RST | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15151
  // clear reset bit - write back to MODER register without RESET bit
15152
  tmp_no_rst = `ETH_MODER_RST;
15153
  tmp_no_rst = ~tmp_no_rst;
15154
  wbm_write(`ETH_MODER, (tmp_no_rst & tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15155
end
15156
endtask // reset_mac
15157
 
15158
task set_tx_bd;
15159
  input  [6:0]  tx_bd_num_start;
15160
  input  [6:0]  tx_bd_num_end;
15161
  input  [15:0] len;
15162
  input         irq;
15163
  input         pad;
15164
  input         crc;
15165
  input  [31:0] txpnt;
15166
 
15167
  integer       i;
15168
  integer       bd_status_addr, bd_ptr_addr;
15169
//  integer       buf_addr;
15170
begin
15171
  for(i = tx_bd_num_start; i <= tx_bd_num_end; i = i + 1)
15172
  begin
15173
//    buf_addr = `TX_BUF_BASE + i * 32'h600;
15174
    bd_status_addr = `TX_BD_BASE + i * 8;
15175
    bd_ptr_addr = bd_status_addr + 4;
15176
    // initialize BD - status
15177
//    wbm_write(bd_status_addr, 32'h00005800, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // IRQ + PAD + CRC
15178
    wbm_write(bd_status_addr, {len, 1'b0, irq, 1'b0, pad, crc, 11'h0},
15179
              4'hF, 1, wbm_init_waits, wbm_subseq_waits); // IRQ + PAD + CRC
15180
    // initialize BD - pointer
15181
//    wbm_write(bd_ptr_addr, buf_addr, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // Initializing BD-pointer
15182
    wbm_write(bd_ptr_addr, txpnt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // Initializing BD-pointer
15183
  end
15184
end
15185
endtask // set_tx_bd
15186
 
15187
task set_tx_bd_wrap;
15188
  input  [6:0]  tx_bd_num_end;
15189
  integer       bd_status_addr, tmp;
15190
begin
15191
  bd_status_addr = `TX_BD_BASE + tx_bd_num_end * 8;
15192
  wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15193
  // set wrap bit to this BD - this BD should be last-one
15194
  wbm_write(bd_status_addr, (`ETH_TX_BD_WRAP | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15195
end
15196
endtask // set_tx_bd_wrap
15197
 
15198
task set_tx_bd_ready;
15199
  input  [6:0]  tx_nd_num_strat;
15200
  input  [6:0]  tx_bd_num_end;
15201
  integer       i;
15202
  integer       bd_status_addr, tmp;
15203
begin
15204
  for(i = tx_nd_num_strat; i <= tx_bd_num_end; i = i + 1)
15205
  begin
15206
    bd_status_addr = `TX_BD_BASE + i * 8;
15207
    wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15208
    // set empty bit to this BD - this BD should be ready
15209
    wbm_write(bd_status_addr, (`ETH_TX_BD_READY | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15210
  end
15211
end
15212
endtask // set_tx_bd_ready
15213
 
15214
task check_tx_bd;
15215
  input  [6:0]  tx_bd_num_end;
15216
  output [31:0] tx_bd_status;
15217
  integer       bd_status_addr, tmp;
15218
begin
15219
  bd_status_addr = `TX_BD_BASE + tx_bd_num_end * 8;
15220
  wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15221
  #1 tx_bd_status = tmp;
15222
  #1;
15223
end
15224
endtask // check_tx_bd
15225
 
15226
task clear_tx_bd;
15227
  input  [6:0]  tx_nd_num_strat;
15228
  input  [6:0]  tx_bd_num_end;
15229
  integer       i;
15230
  integer       bd_status_addr, bd_ptr_addr;
15231
begin
15232
  for(i = tx_nd_num_strat; i <= tx_bd_num_end; i = i + 1)
15233
  begin
15234
    bd_status_addr = `TX_BD_BASE + i * 8;
15235
    bd_ptr_addr = bd_status_addr + 4;
15236
    // clear BD - status
15237
    wbm_write(bd_status_addr, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15238
    // clear BD - pointer
15239
    wbm_write(bd_ptr_addr, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15240
  end
15241
end
15242
endtask // clear_tx_bd
15243
 
15244
task set_rx_bd;
15245
  input  [6:0]  rx_bd_num_strat;
15246
  input  [6:0]  rx_bd_num_end;
15247
  input         irq;
15248
  input  [31:0] rxpnt;
15249
//  input  [6:0]  rxbd_num;
15250
  integer       i;
15251
  integer       bd_status_addr, bd_ptr_addr;
15252
//  integer       buf_addr;
15253
begin
15254
  for(i = rx_bd_num_strat; i <= rx_bd_num_end; i = i + 1)
15255
  begin
15256
//    buf_addr = `RX_BUF_BASE + i * 32'h600;
15257 209 tadejm
//    bd_status_addr = `RX_BD_BASE + i * 8;
15258
//    bd_ptr_addr = bd_status_addr + 4; 
15259
    bd_status_addr = `TX_BD_BASE + i * 8;
15260
    bd_ptr_addr = bd_status_addr + 4;
15261 116 mohor
 
15262 169 mohor
    // initialize BD - status
15263
//    wbm_write(bd_status_addr, 32'h0000c000, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // IRQ + PAD + CRC
15264
    wbm_write(bd_status_addr, {17'h0, irq, 14'h0},
15265
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15266
    // initialize BD - pointer
15267
//    wbm_write(bd_ptr_addr, buf_addr, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // Initializing BD-pointer
15268
    wbm_write(bd_ptr_addr, rxpnt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // Initializing BD-pointer
15269
  end
15270
end
15271
endtask // set_rx_bd
15272 116 mohor
 
15273 169 mohor
task set_rx_bd_wrap;
15274
  input  [6:0]  rx_bd_num_end;
15275
  integer       bd_status_addr, tmp;
15276
begin
15277 209 tadejm
//  bd_status_addr = `RX_BD_BASE + rx_bd_num_end * 8;
15278
  bd_status_addr = `TX_BD_BASE + rx_bd_num_end * 8;
15279 169 mohor
  wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15280
  // set wrap bit to this BD - this BD should be last-one
15281
  wbm_write(bd_status_addr, (`ETH_RX_BD_WRAP | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15282
end
15283
endtask // set_rx_bd_wrap
15284 116 mohor
 
15285 169 mohor
task set_rx_bd_empty;
15286
  input  [6:0]  rx_bd_num_strat;
15287
  input  [6:0]  rx_bd_num_end;
15288
  integer       i;
15289
  integer       bd_status_addr, tmp;
15290
begin
15291
  for(i = rx_bd_num_strat; i <= rx_bd_num_end; i = i + 1)
15292
  begin
15293 209 tadejm
//    bd_status_addr = `RX_BD_BASE + i * 8;
15294
    bd_status_addr = `TX_BD_BASE + i * 8;
15295 169 mohor
    wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15296
    // set empty bit to this BD - this BD should be ready
15297
    wbm_write(bd_status_addr, (`ETH_RX_BD_EMPTY | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15298 116 mohor
  end
15299 169 mohor
end
15300
endtask // set_rx_bd_empty
15301 116 mohor
 
15302 169 mohor
task check_rx_bd;
15303
  input  [6:0]  rx_bd_num_end;
15304
  output [31:0] rx_bd_status;
15305
  integer       bd_status_addr, tmp;
15306
begin
15307 209 tadejm
//  bd_status_addr = `RX_BD_BASE + rx_bd_num_end * 8;
15308
  bd_status_addr = `TX_BD_BASE + rx_bd_num_end * 8;
15309 169 mohor
  wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15310
  #1 rx_bd_status = tmp;
15311
  #1;
15312
end
15313
endtask // check_rx_bd
15314 116 mohor
 
15315 169 mohor
task clear_rx_bd;
15316
  input  [6:0]  rx_bd_num_strat;
15317
  input  [6:0]  rx_bd_num_end;
15318
  integer       i;
15319
  integer       bd_status_addr, bd_ptr_addr;
15320
begin
15321
  for(i = rx_bd_num_strat; i <= rx_bd_num_end; i = i + 1)
15322
  begin
15323 209 tadejm
//    bd_status_addr = `RX_BD_BASE + i * 8;
15324
    bd_status_addr = `TX_BD_BASE + i * 8;
15325 169 mohor
    bd_ptr_addr = bd_status_addr + 4;
15326
    // clear BD - status
15327
    wbm_write(bd_status_addr, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15328
    // clear BD - pointer
15329
    wbm_write(bd_ptr_addr, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15330
  end
15331
end
15332
endtask // clear_rx_bd
15333 116 mohor
 
15334 169 mohor
task set_tx_packet;
15335
  input  [31:0] txpnt;
15336
  input  [15:0] len;
15337
  input  [7:0]  eth_start_data;
15338
  integer       i, sd;
15339
  integer       buffer;
15340
  reg           delta_t;
15341
begin
15342
  buffer = txpnt;
15343
  sd = eth_start_data;
15344
  delta_t = 0;
15345 116 mohor
 
15346 169 mohor
  // First write might not be word allign.
15347
  if(buffer[1:0] == 1)
15348
  begin
15349
    wb_slave.wr_mem(buffer - 1, {8'h0, sd[7:0], sd[7:0] + 3'h1, sd[7:0] + 3'h2}, 4'h7);
15350
    sd = sd + 3;
15351
    i = 3;
15352
  end
15353
  else if(buffer[1:0] == 2)
15354
  begin
15355
    wb_slave.wr_mem(buffer - 2, {16'h0, sd[7:0], sd[7:0] + 3'h1}, 4'h3);
15356
    sd = sd + 2;
15357
    i = 2;
15358
  end
15359
  else if(buffer[1:0] == 3)
15360
  begin
15361
    wb_slave.wr_mem(buffer - 3, {24'h0, sd[7:0]}, 4'h1);
15362
    sd = sd + 1;
15363
    i = 1;
15364
  end
15365
  else
15366
    i = 0;
15367
  delta_t = !delta_t;
15368 116 mohor
 
15369 169 mohor
  for(i = i; i < (len - 4); i = i + 4) // Last 0-3 bytes are not written
15370
  begin
15371
    wb_slave.wr_mem(buffer + i, {sd[7:0], sd[7:0] + 3'h1, sd[7:0] + 3'h2, sd[7:0] + 3'h3}, 4'hF);
15372
    sd = sd + 4;
15373
  end
15374
  delta_t = !delta_t;
15375
 
15376
  // Last word
15377
  if((len - i) == 3)
15378 116 mohor
  begin
15379 169 mohor
    wb_slave.wr_mem(buffer + i, {sd[7:0], sd[7:0] + 3'h1, sd[7:0] + 3'h2, 8'h0}, 4'hE);
15380
  end
15381
  else if((len - i) == 2)
15382
  begin
15383
    wb_slave.wr_mem(buffer + i, {sd[7:0], sd[7:0] + 3'h1, 16'h0}, 4'hC);
15384
  end
15385
  else if((len - i) == 1)
15386
  begin
15387
    wb_slave.wr_mem(buffer + i, {sd[7:0], 24'h0}, 4'h8);
15388
  end
15389
  else if((len - i) == 4)
15390
  begin
15391
    wb_slave.wr_mem(buffer + i, {sd[7:0], sd[7:0] + 3'h1, sd[7:0] + 3'h2, sd[7:0] + 3'h3}, 4'hF);
15392
  end
15393
  else
15394
    $display("(%0t)(%m) ERROR", $time);
15395
  delta_t = !delta_t;
15396
end
15397
endtask // set_tx_packet
15398
 
15399
task check_tx_packet;
15400
  input  [31:0] txpnt_wb;  // source
15401
  input  [31:0] txpnt_phy; // destination
15402
  input  [15:0] len;
15403
  output [31:0] failure;
15404
  integer       i, data_wb, data_phy;
15405
  reg    [31:0] addr_wb, addr_phy;
15406
  reg    [31:0] failure;
15407
  reg           delta_t;
15408
begin
15409
  addr_wb = txpnt_wb;
15410
  addr_phy = txpnt_phy;
15411
  delta_t = 0;
15412
  failure = 0;
15413 209 tadejm
  #1;
15414 169 mohor
  // First write might not be word allign.
15415
  if(addr_wb[1:0] == 1)
15416
  begin
15417
    wb_slave.rd_mem(addr_wb - 1, data_wb, 4'h7);
15418
    data_phy[31:24] = 0;
15419
    data_phy[23:16] = eth_phy.tx_mem[addr_phy[21:0]];
15420
    data_phy[15: 8] = eth_phy.tx_mem[addr_phy[21:0] + 1];
15421
    data_phy[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0] + 2];
15422
    i = 3;
15423
    if (data_phy[23:0] !== data_wb[23:0])
15424
    begin
15425
      `TIME;
15426 209 tadejm
      $display("*E Wrong 1. word (3 bytes) of TX packet! phy: %0h, wb: %0h", data_phy[23:0], data_wb[23:0]);
15427
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15428 169 mohor
      failure = 1;
15429
    end
15430
  end
15431
  else if (addr_wb[1:0] == 2)
15432
  begin
15433
    wb_slave.rd_mem(addr_wb - 2, data_wb, 4'h3);
15434
    data_phy[31:16] = 0;
15435
    data_phy[15: 8] = eth_phy.tx_mem[addr_phy[21:0]];
15436
    data_phy[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0] + 1];
15437
    i = 2;
15438
    if (data_phy[15:0] !== data_wb[15:0])
15439
    begin
15440
      `TIME;
15441 209 tadejm
      $display("*E Wrong 1. word (2 bytes) of TX packet! phy: %0h, wb: %0h", data_phy[15:0], data_wb[15:0]);
15442
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15443 169 mohor
      failure = 1;
15444
    end
15445
  end
15446
  else if (addr_wb[1:0] == 3)
15447
  begin
15448
    wb_slave.rd_mem(addr_wb - 3, data_wb, 4'h1);
15449
    data_phy[31: 8] = 0;
15450
    data_phy[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0]];
15451
    i = 1;
15452
    if (data_phy[7:0] !== data_wb[7:0])
15453
    begin
15454
      `TIME;
15455 209 tadejm
      $display("*E Wrong 1. word (1 byte) of TX packet! phy: %0h, wb: %0h", data_phy[7:0], data_wb[7:0]);
15456
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15457 169 mohor
      failure = 1;
15458
    end
15459
  end
15460
  else
15461
    i = 0;
15462
  delta_t = !delta_t;
15463 209 tadejm
  #1;
15464 169 mohor
  for(i = i; i < (len - 4); i = i + 4) // Last 0-3 bytes are not checked
15465
  begin
15466
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hF);
15467
    data_phy[31:24] = eth_phy.tx_mem[addr_phy[21:0] + i];
15468
    data_phy[23:16] = eth_phy.tx_mem[addr_phy[21:0] + i + 1];
15469
    data_phy[15: 8] = eth_phy.tx_mem[addr_phy[21:0] + i + 2];
15470
    data_phy[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0] + i + 3];
15471
    if (data_phy[31:0] !== data_wb[31:0])
15472
    begin
15473
      `TIME;
15474 209 tadejm
      $display("*E Wrong %d. word (4 bytes) of TX packet! phy: %0h, wb: %0h", ((i/4)+1), data_phy[31:0], data_wb[31:0]);
15475
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15476 169 mohor
      failure = failure + 1;
15477
    end
15478
  end
15479
  delta_t = !delta_t;
15480 209 tadejm
  #1;
15481 169 mohor
  // Last word
15482
  if((len - i) == 3)
15483
  begin
15484
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hE);
15485
    data_phy[31:24] = eth_phy.tx_mem[addr_phy[21:0] + i];
15486
    data_phy[23:16] = eth_phy.tx_mem[addr_phy[21:0] + i + 1];
15487
    data_phy[15: 8] = eth_phy.tx_mem[addr_phy[21:0] + i + 2];
15488
    data_phy[ 7: 0] = 0;
15489
    if (data_phy[31:8] !== data_wb[31:8])
15490
    begin
15491
      `TIME;
15492 209 tadejm
      $display("*E Wrong %d. word (3 bytes) of TX packet! phy: %0h, wb: %0h", ((i/4)+1), data_phy[31:8], data_wb[31:8]);
15493
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15494 169 mohor
      failure = failure + 1;
15495
    end
15496
  end
15497
  else if((len - i) == 2)
15498
  begin
15499
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hC);
15500
    data_phy[31:24] = eth_phy.tx_mem[addr_phy[21:0] + i];
15501
    data_phy[23:16] = eth_phy.tx_mem[addr_phy[21:0] + i + 1];
15502
    data_phy[15: 8] = 0;
15503
    data_phy[ 7: 0] = 0;
15504
    if (data_phy[31:16] !== data_wb[31:16])
15505
    begin
15506
      `TIME;
15507 209 tadejm
      $display("*E Wrong %d. word (2 bytes) of TX packet! phy: %0h, wb: %0h", ((i/4)+1), data_phy[31:16], data_wb[31:16]);
15508
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15509 169 mohor
      failure = failure + 1;
15510
    end
15511
  end
15512
  else if((len - i) == 1)
15513
  begin
15514
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'h8);
15515
    data_phy[31:24] = eth_phy.tx_mem[addr_phy[21:0] + i];
15516
    data_phy[23:16] = 0;
15517
    data_phy[15: 8] = 0;
15518
    data_phy[ 7: 0] = 0;
15519
    if (data_phy[31:24] !== data_wb[31:24])
15520
    begin
15521
      `TIME;
15522 209 tadejm
      $display("*E Wrong %d. word (1 byte) of TX packet! phy: %0h, wb: %0h", ((i/4)+1), data_phy[31:24], data_wb[31:24]);
15523
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15524 169 mohor
      failure = failure + 1;
15525
    end
15526
  end
15527
  else if((len - i) == 4)
15528
  begin
15529
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hF);
15530
    data_phy[31:24] = eth_phy.tx_mem[addr_phy[21:0] + i];
15531
    data_phy[23:16] = eth_phy.tx_mem[addr_phy[21:0] + i + 1];
15532
    data_phy[15: 8] = eth_phy.tx_mem[addr_phy[21:0] + i + 2];
15533
    data_phy[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0] + i + 3];
15534
    if (data_phy[31:0] !== data_wb[31:0])
15535
    begin
15536
      `TIME;
15537 209 tadejm
      $display("*E Wrong %d. word (4 bytes) of TX packet! phy: %0h, wb: %0h", ((i/4)+1), data_phy[31:0], data_wb[31:0]);
15538
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15539 169 mohor
      failure = failure + 1;
15540
    end
15541
  end
15542
  else
15543
    $display("(%0t)(%m) ERROR", $time);
15544
  delta_t = !delta_t;
15545
end
15546
endtask // check_tx_packet
15547
 
15548
task set_rx_packet;
15549
  input  [31:0] rxpnt;
15550
  input  [15:0] len;
15551
  input         plus_dribble_nibble; // if length is longer for one nibble
15552
  input  [47:0] eth_dest_addr;
15553
  input  [47:0] eth_source_addr;
15554
  input  [15:0] eth_type_len;
15555
  input  [7:0]  eth_start_data;
15556
  integer       i, sd;
15557
  reg    [47:0] dest_addr;
15558
  reg    [47:0] source_addr;
15559
  reg    [15:0] type_len;
15560
  reg    [21:0] buffer;
15561
  reg           delta_t;
15562
begin
15563
  buffer = rxpnt[21:0];
15564
  dest_addr = eth_dest_addr;
15565
  source_addr = eth_source_addr;
15566
  type_len = eth_type_len;
15567
  sd = eth_start_data;
15568
  delta_t = 0;
15569
  for(i = 0; i < len; i = i + 1)
15570
  begin
15571
    if (i < 6)
15572
    begin
15573
      eth_phy.rx_mem[buffer] = dest_addr[47:40];
15574
      dest_addr = dest_addr << 8;
15575
    end
15576
    else if (i < 12)
15577
    begin
15578
      eth_phy.rx_mem[buffer] = source_addr[47:40];
15579
      source_addr = source_addr << 8;
15580
    end
15581
    else if (i < 14)
15582
    begin
15583
      eth_phy.rx_mem[buffer] = type_len[15:8];
15584
      type_len = type_len << 8;
15585
    end
15586
    else
15587
    begin
15588
      eth_phy.rx_mem[buffer] = sd[7:0];
15589
      sd = sd + 1;
15590
    end
15591
    buffer = buffer + 1;
15592
  end
15593
  delta_t = !delta_t;
15594
  if (plus_dribble_nibble)
15595
    eth_phy.rx_mem[buffer] = {4'h0, 4'hD /*sd[3:0]*/};
15596
  delta_t = !delta_t;
15597
end
15598
endtask // set_rx_packet
15599
 
15600 243 tadejm
task set_rx_addr_type;
15601
  input  [31:0] rxpnt;
15602
  input  [47:0] eth_dest_addr;
15603
  input  [47:0] eth_source_addr;
15604
  input  [15:0] eth_type_len;
15605
  integer       i;
15606
  reg    [47:0] dest_addr;
15607
  reg    [47:0] source_addr;
15608
  reg    [15:0] type_len;
15609
  reg    [21:0] buffer;
15610
  reg           delta_t;
15611
begin
15612
  buffer = rxpnt[21:0];
15613
  dest_addr = eth_dest_addr;
15614
  source_addr = eth_source_addr;
15615
  type_len = eth_type_len;
15616
  delta_t = 0;
15617
  for(i = 0; i < 14; i = i + 1)
15618
  begin
15619
    if (i < 6)
15620
    begin
15621
      eth_phy.rx_mem[buffer] = dest_addr[47:40];
15622
      dest_addr = dest_addr << 8;
15623
    end
15624
    else if (i < 12)
15625
    begin
15626
      eth_phy.rx_mem[buffer] = source_addr[47:40];
15627
      source_addr = source_addr << 8;
15628
    end
15629
    else // if (i < 14)
15630
    begin
15631
      eth_phy.rx_mem[buffer] = type_len[15:8];
15632
      type_len = type_len << 8;
15633
    end
15634
    buffer = buffer + 1;
15635
  end
15636
  delta_t = !delta_t;
15637
end
15638
endtask // set_rx_addr_type
15639
 
15640 169 mohor
task check_rx_packet;
15641
  input  [31:0] rxpnt_phy; // source
15642
  input  [31:0] rxpnt_wb;  // destination
15643
  input  [15:0] len;
15644
  input         plus_dribble_nibble; // if length is longer for one nibble
15645
  input         successful_dribble_nibble; // if additional nibble is stored into memory
15646
  output [31:0] failure;
15647
  integer       i, data_wb, data_phy;
15648
  reg    [31:0] addr_wb, addr_phy;
15649
  reg    [31:0] failure;
15650
  reg    [21:0] buffer;
15651
  reg           delta_t;
15652
begin
15653
  addr_phy = rxpnt_phy;
15654
  addr_wb = rxpnt_wb;
15655
  delta_t = 0;
15656
  failure = 0;
15657
 
15658
  // First write might not be word allign.
15659
  if(addr_wb[1:0] == 1)
15660
  begin
15661
    wb_slave.rd_mem(addr_wb - 1, data_wb, 4'h7);
15662
    data_phy[31:24] = 0;
15663
    data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0]];
15664
    data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0] + 1];
15665
    data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0] + 2];
15666
    i = 3;
15667
    if (data_phy[23:0] !== data_wb[23:0])
15668
    begin
15669
      `TIME;
15670 243 tadejm
      $display("   addr_phy = %h, addr_wb = %h", rxpnt_phy, rxpnt_wb);
15671
      $display("*E Wrong 1. word (3 bytes) of RX packet! phy = %h, wb = %h", data_phy[23:0], data_wb[23:0]);
15672 169 mohor
      failure = 1;
15673
    end
15674
  end
15675
  else if (addr_wb[1:0] == 2)
15676
  begin
15677
    wb_slave.rd_mem(addr_wb - 2, data_wb, 4'h3);
15678
    data_phy[31:16] = 0;
15679
    data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0]];
15680
    data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0] + 1];
15681
    i = 2;
15682
    if (data_phy[15:0] !== data_wb[15:0])
15683
    begin
15684
      `TIME;
15685 243 tadejm
      $display("   addr_phy = %h, addr_wb = %h", rxpnt_phy, rxpnt_wb);
15686
      $display("*E Wrong 1. word (2 bytes) of RX packet! phy = %h, wb = %h", data_phy[15:0], data_wb[15:0]);
15687 169 mohor
      failure = 1;
15688
    end
15689
  end
15690
  else if (addr_wb[1:0] == 3)
15691
  begin
15692
    wb_slave.rd_mem(addr_wb - 3, data_wb, 4'h1);
15693
    data_phy[31: 8] = 0;
15694
    data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0]];
15695
    i = 1;
15696
    if (data_phy[7:0] !== data_wb[7:0])
15697
    begin
15698
      `TIME;
15699 243 tadejm
      $display("   addr_phy = %h, addr_wb = %h", rxpnt_phy, rxpnt_wb);
15700
      $display("*E Wrong 1. word (1 byte) of RX packet! phy = %h, wb = %h", data_phy[7:0], data_wb[7:0]);
15701 169 mohor
      failure = 1;
15702
    end
15703
  end
15704
  else
15705
    i = 0;
15706
  delta_t = !delta_t;
15707
 
15708
  for(i = i; i < (len - 4); i = i + 4) // Last 0-3 bytes are not checked
15709
  begin
15710
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hF);
15711
    data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i];
15712
    data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0] + i + 1];
15713
    data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0] + i + 2];
15714
    data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0] + i + 3];
15715
    if (data_phy[31:0] !== data_wb[31:0])
15716
    begin
15717
      `TIME;
15718 243 tadejm
      if (i == 0)
15719
        $display("   addr_phy = %h, addr_wb = %h", rxpnt_phy, rxpnt_wb);
15720
      $display("*E Wrong %0d. word (4 bytes) of RX packet! phy = %h, wb = %h", ((i/4)+1), data_phy[31:0], data_wb[31:0]);
15721 169 mohor
      failure = failure + 1;
15722
    end
15723
  end
15724
  delta_t = !delta_t;
15725
 
15726
  // Last word
15727
  if((len - i) == 3)
15728
  begin
15729
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hF);
15730
    data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i];
15731
    data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0] + i + 1];
15732
    data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0] + i + 2];
15733
    if (plus_dribble_nibble)
15734
      data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0] + i + 3];
15735
    else
15736
      data_phy[ 7: 0] = 0;
15737
    if (data_phy[31:8] !== data_wb[31:8])
15738
    begin
15739
      `TIME;
15740 243 tadejm
      $display("*E Wrong %0d. word (3 bytes) of RX packet! phy = %h, wb = %h", ((i/4)+1), data_phy[31:8], data_wb[31:8]);
15741 169 mohor
      failure = failure + 1;
15742
    end
15743
    if (plus_dribble_nibble && successful_dribble_nibble)
15744
    begin
15745
      if (data_phy[3:0] !== data_wb[3:0])
15746 116 mohor
      begin
15747 169 mohor
        `TIME;
15748 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (3 bytes) of RX packet!", ((i/4)+1));
15749 169 mohor
        failure = failure + 1;
15750 116 mohor
      end
15751 169 mohor
    end
15752
    else if (plus_dribble_nibble && !successful_dribble_nibble)
15753
    begin
15754
      if (data_phy[3:0] === data_wb[3:0])
15755 116 mohor
      begin
15756 169 mohor
        `TIME;
15757 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (3 bytes) of RX packet!", ((i/4)+1));
15758 169 mohor
        failure = failure + 1;
15759 116 mohor
      end
15760 169 mohor
    end
15761
  end
15762
  else if((len - i) == 2)
15763
  begin
15764
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hE);
15765
    data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i];
15766
    data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0] + i + 1];
15767
    if (plus_dribble_nibble)
15768
      data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0] + i + 2];
15769
    else
15770
      data_phy[15: 8] = 0;
15771
    data_phy[ 7: 0] = 0;
15772
    if (data_phy[31:16] !== data_wb[31:16])
15773
    begin
15774
      `TIME;
15775 243 tadejm
      $display("*E Wrong %0d. word (2 bytes) of RX packet! phy = %h, wb = %h", ((i/4)+1), data_phy[31:16], data_wb[31:16]);
15776 169 mohor
      failure = failure + 1;
15777
    end
15778
    if (plus_dribble_nibble && successful_dribble_nibble)
15779
    begin
15780
      if (data_phy[11:8] !== data_wb[11:8])
15781
      begin
15782
        `TIME;
15783 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (2 bytes) of RX packet!", ((i/4)+1));
15784 169 mohor
        failure = failure + 1;
15785
      end
15786
    end
15787
    else if (plus_dribble_nibble && !successful_dribble_nibble)
15788
    begin
15789
      if (data_phy[11:8] === data_wb[11:8])
15790
      begin
15791
        `TIME;
15792 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (2 bytes) of RX packet!", ((i/4)+1));
15793 169 mohor
        failure = failure + 1;
15794
      end
15795
    end
15796
  end
15797
  else if((len - i) == 1)
15798
  begin
15799
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hC);
15800
    data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i];
15801
    if (plus_dribble_nibble)
15802
      data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0] + i + 1];
15803
    else
15804
      data_phy[23:16] = 0;
15805
    data_phy[15: 8] = 0;
15806
    data_phy[ 7: 0] = 0;
15807
    if (data_phy[31:24] !== data_wb[31:24])
15808
    begin
15809
      `TIME;
15810 243 tadejm
      $display("*E Wrong %0d. word (1 byte) of RX packet! phy = %h, wb = %h", ((i/4)+1), data_phy[31:24], data_wb[31:24]);
15811 169 mohor
      failure = failure + 1;
15812
    end
15813
    if (plus_dribble_nibble && successful_dribble_nibble)
15814
    begin
15815
      if (data_phy[19:16] !== data_wb[19:16])
15816
      begin
15817
        `TIME;
15818 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (1 byte) of RX packet!", ((i/4)+1));
15819 169 mohor
        failure = failure + 1;
15820
      end
15821
    end
15822
    else if (plus_dribble_nibble && !successful_dribble_nibble)
15823
    begin
15824
      if (data_phy[19:16] === data_wb[19:16])
15825
      begin
15826
        `TIME;
15827 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (1 byte) of RX packet!", ((i/4)+1));
15828 169 mohor
        failure = failure + 1;
15829
      end
15830
    end
15831
  end
15832
  else if((len - i) == 4)
15833
  begin
15834
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hF);
15835
    data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i];
15836
    data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0] + i + 1];
15837
    data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0] + i + 2];
15838
    data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0] + i + 3];
15839
    if (data_phy[31:0] !== data_wb[31:0])
15840
    begin
15841
      `TIME;
15842 243 tadejm
      $display("*E Wrong %0d. word (4 bytes) of RX packet! phy = %h, wb = %h", ((i/4)+1), data_phy[31:0], data_wb[31:0]);
15843 169 mohor
      failure = failure + 1;
15844
    end
15845
    if (plus_dribble_nibble)
15846
    begin
15847
      wb_slave.rd_mem(addr_wb + i + 4, data_wb, 4'h8);
15848
      data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i + 4];
15849
      if (successful_dribble_nibble)
15850
      begin
15851
        if (data_phy[27:24] !== data_wb[27:24])
15852
        begin
15853
          `TIME;
15854 243 tadejm
          $display("*E Wrong dribble nibble in %0d. word (0 bytes) of RX packet!", ((i/4)+2));
15855 169 mohor
          failure = failure + 1;
15856
        end
15857
      end
15858
      else
15859
      begin
15860
        if (data_phy[27:24] === data_wb[27:24])
15861
        begin
15862
          `TIME;
15863 243 tadejm
          $display("*E Wrong dribble nibble in %0d. word (0 bytes) of RX packet!", ((i/4)+2));
15864 169 mohor
          failure = failure + 1;
15865
        end
15866
      end
15867
    end
15868
  end
15869
  else
15870
    $display("(%0t)(%m) ERROR", $time);
15871
  delta_t = !delta_t;
15872
end
15873
endtask // check_rx_packet
15874 116 mohor
 
15875 169 mohor
//////////////////////////////////////////////////////////////
15876
// Ethernet CRC Basic tasks
15877
//////////////////////////////////////////////////////////////
15878
 
15879
task append_tx_crc;
15880
  input  [31:0] txpnt_wb;  // source
15881
  input  [15:0] len; // length in bytes without CRC
15882
  input         negated_crc; // if appended CRC is correct or not
15883
  reg    [31:0] crc;
15884
  reg    [31:0] addr_wb;
15885
  reg           delta_t;
15886
begin
15887 243 tadejm
  addr_wb = txpnt_wb + {16'h0, len};
15888 169 mohor
  delta_t = 0;
15889
  // calculate CRC from prepared packet
15890
  paralel_crc_mac(txpnt_wb, {16'h0, len}, 1'b0, crc);
15891
  if (negated_crc)
15892
    crc = ~crc;
15893
  delta_t = !delta_t;
15894
 
15895
  // Write might not be word allign.
15896
  if (addr_wb[1:0] == 1)
15897
  begin
15898
    wb_slave.wr_mem(addr_wb - 1, {8'h0, crc[7:0], crc[15:8], crc[23:16]}, 4'h7);
15899
    wb_slave.wr_mem(addr_wb + 3, {crc[31:24], 24'h0}, 4'h8);
15900 116 mohor
  end
15901 169 mohor
  else if (addr_wb[1:0] == 2)
15902
  begin
15903
    wb_slave.wr_mem(addr_wb - 2, {16'h0, crc[7:0], crc[15:8]}, 4'h3);
15904
    wb_slave.wr_mem(addr_wb + 2, {crc[23:16], crc[31:24], 16'h0}, 4'hC);
15905
  end
15906
  else if (addr_wb[1:0] == 3)
15907
  begin
15908
    wb_slave.wr_mem(addr_wb - 3, {24'h0, crc[7:0]}, 4'h1);
15909
    wb_slave.wr_mem(addr_wb + 1, {crc[15:8], crc[23:16], crc[31:24], 8'h0}, 4'hE);
15910
  end
15911
  else
15912
  begin
15913
    wb_slave.wr_mem(addr_wb, {crc[7:0], crc[15:8], crc[23:16], crc[31:24]}, 4'hF);
15914
  end
15915
  delta_t = !delta_t;
15916
end
15917
endtask // append_tx_crc
15918 116 mohor
 
15919 169 mohor
task check_tx_crc; // used to check crc added to TX packets by MAC
15920
  input  [31:0] txpnt_phy; // destination
15921
  input  [15:0] len; // length in bytes without CRC
15922
  input         negated_crc; // if appended CRC is correct or not
15923
  output [31:0] failure;
15924
  reg    [31:0] failure;
15925
  reg    [31:0] crc_calc;
15926
  reg    [31:0] crc;
15927
  reg    [31:0] addr_phy;
15928
  reg           delta_t;
15929
begin
15930
  addr_phy = txpnt_phy;
15931
  failure = 0;
15932
  // calculate CRC from sent packet
15933
//  serial_crc_phy_tx(addr_phy, {16'h0, len}, 1'b0, crc_calc);
15934
//#10;
15935
  paralel_crc_phy_tx(addr_phy, {16'h0, len}, 1'b0, crc_calc);
15936 209 tadejm
  #1;
15937 169 mohor
  addr_phy = addr_phy + len;
15938
  // Read CRC - BIG endian
15939
  crc[31:24] = eth_phy.tx_mem[addr_phy[21:0]];
15940
  crc[23:16] = eth_phy.tx_mem[addr_phy[21:0] + 1];
15941
  crc[15: 8] = eth_phy.tx_mem[addr_phy[21:0] + 2];
15942
  crc[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0] + 3];
15943
 
15944
  delta_t = !delta_t;
15945
  if (negated_crc)
15946
  begin
15947
    if ((~crc_calc) !== crc)
15948
    begin
15949
      `TIME;
15950
      $display("*E Negated CRC was not successfuly transmitted!");
15951
      failure = failure + 1;
15952
    end
15953
  end
15954
  else
15955
  begin
15956
    if (crc_calc !== crc)
15957
    begin
15958
      `TIME;
15959 209 tadejm
      $display("*E Transmitted CRC was not correct; crc_calc: %0h, crc_mem: %0h", crc_calc, crc);
15960 169 mohor
      failure = failure + 1;
15961
    end
15962
  end
15963
  delta_t = !delta_t;
15964
end
15965
endtask // check_tx_crc
15966
 
15967
task append_rx_crc;
15968
  input  [31:0] rxpnt_phy; // source
15969
  input  [15:0] len; // length in bytes without CRC
15970
  input         plus_dribble_nibble; // if length is longer for one nibble
15971
  input         negated_crc; // if appended CRC is correct or not
15972
  reg    [31:0] crc;
15973
  reg    [7:0]  tmp;
15974
  reg    [31:0] addr_phy;
15975
  reg           delta_t;
15976
begin
15977
  addr_phy = rxpnt_phy + len;
15978
  delta_t = 0;
15979
  // calculate CRC from prepared packet
15980
  paralel_crc_phy_rx(rxpnt_phy, {16'h0, len}, plus_dribble_nibble, crc);
15981
  if (negated_crc)
15982
    crc = ~crc;
15983
  delta_t = !delta_t;
15984
 
15985
  if (plus_dribble_nibble)
15986
  begin
15987
    tmp = eth_phy.rx_mem[addr_phy];
15988 209 tadejm
    eth_phy.rx_mem[addr_phy]     = {crc[27:24], tmp[3:0]};
15989
    eth_phy.rx_mem[addr_phy + 1] = {crc[19:16], crc[31:28]};
15990
    eth_phy.rx_mem[addr_phy + 2] = {crc[11:8], crc[23:20]};
15991
    eth_phy.rx_mem[addr_phy + 3] = {crc[3:0], crc[15:12]};
15992
    eth_phy.rx_mem[addr_phy + 4] = {4'h0, crc[7:4]};
15993 169 mohor
  end
15994
  else
15995
  begin
15996 209 tadejm
    eth_phy.rx_mem[addr_phy]     = crc[31:24];
15997
    eth_phy.rx_mem[addr_phy + 1] = crc[23:16];
15998
    eth_phy.rx_mem[addr_phy + 2] = crc[15:8];
15999
    eth_phy.rx_mem[addr_phy + 3] = crc[7:0];
16000 169 mohor
  end
16001
end
16002
endtask // append_rx_crc
16003
 
16004
// paralel CRC checking for PHY TX
16005
task paralel_crc_phy_tx;
16006
  input  [31:0] start_addr; // start address
16007
  input  [31:0] len; // length of frame in Bytes without CRC length
16008
  input         plus_dribble_nibble; // if length is longer for one nibble
16009
  output [31:0] crc_out;
16010
  reg    [21:0] addr_cnt; // only 22 address lines
16011
  integer       word_cnt;
16012
  integer       nibble_cnt;
16013
  reg    [31:0] load_reg;
16014
  reg           delta_t;
16015
  reg    [31:0] crc_next;
16016
  reg    [31:0] crc;
16017
  reg           crc_error;
16018
  reg     [3:0] data_in;
16019
  integer       i;
16020
begin
16021
  #1 addr_cnt = start_addr[21:0];
16022
  word_cnt = 24; // 27; // start of the frame - nibble granularity (MSbit first)
16023
  crc = 32'hFFFF_FFFF; // INITIAL value
16024
  delta_t = 0;
16025
  // length must include 4 bytes of ZEROs, to generate CRC
16026
  // get number of nibbles from Byte length (2^1 = 2)
16027
  if (plus_dribble_nibble)
16028
    nibble_cnt = ((len + 4) << 1) + 1'b1; // one nibble longer
16029
  else
16030
    nibble_cnt = ((len + 4) << 1);
16031
  // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16032
  load_reg[31:24] = eth_phy.tx_mem[addr_cnt];
16033
  addr_cnt = addr_cnt + 1;
16034
  load_reg[23:16] = eth_phy.tx_mem[addr_cnt];
16035
  addr_cnt = addr_cnt + 1;
16036
  load_reg[15: 8] = eth_phy.tx_mem[addr_cnt];
16037
  addr_cnt = addr_cnt + 1;
16038
  load_reg[ 7: 0] = eth_phy.tx_mem[addr_cnt];
16039
  addr_cnt = addr_cnt + 1;
16040
  while (nibble_cnt > 0)
16041
  begin
16042
    // wait for delta time
16043
    delta_t = !delta_t;
16044
    // shift data in
16045
 
16046
    if(nibble_cnt <= 8) // for additional 8 nibbles shift ZEROs in!
16047
      data_in[3:0] = 4'h0;
16048
    else
16049
 
16050
      data_in[3:0] = {load_reg[word_cnt], load_reg[word_cnt+1], load_reg[word_cnt+2], load_reg[word_cnt+3]};
16051
    crc_next[0]  = (data_in[0] ^ crc[28]);
16052
    crc_next[1]  = (data_in[1] ^ data_in[0] ^ crc[28]    ^ crc[29]);
16053
    crc_next[2]  = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]);
16054
    crc_next[3]  = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]);
16055
    crc_next[4]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[0];
16056
    crc_next[5]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[1];
16057
    crc_next[6]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[ 2];
16058
    crc_next[7]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[3];
16059
    crc_next[8]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[4];
16060
    crc_next[9]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[5];
16061
    crc_next[10] = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[6];
16062
    crc_next[11] = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[7];
16063
    crc_next[12] = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]) ^ crc[8];
16064
    crc_next[13] = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]) ^ crc[9];
16065
    crc_next[14] = (data_in[3] ^ data_in[2] ^ crc[30]    ^ crc[31]) ^ crc[10];
16066
    crc_next[15] = (data_in[3] ^ crc[31])   ^ crc[11];
16067
    crc_next[16] = (data_in[0] ^ crc[28])   ^ crc[12];
16068
    crc_next[17] = (data_in[1] ^ crc[29])   ^ crc[13];
16069
    crc_next[18] = (data_in[2] ^ crc[30])   ^ crc[14];
16070
    crc_next[19] = (data_in[3] ^ crc[31])   ^ crc[15];
16071
    crc_next[20] =  crc[16];
16072
    crc_next[21] =  crc[17];
16073
    crc_next[22] = (data_in[0] ^ crc[28])   ^ crc[18];
16074
    crc_next[23] = (data_in[1] ^ data_in[0] ^ crc[29]    ^ crc[28]) ^ crc[19];
16075
    crc_next[24] = (data_in[2] ^ data_in[1] ^ crc[30]    ^ crc[29]) ^ crc[20];
16076
    crc_next[25] = (data_in[3] ^ data_in[2] ^ crc[31]    ^ crc[30]) ^ crc[21];
16077
    crc_next[26] = (data_in[3] ^ data_in[0] ^ crc[31]    ^ crc[28]) ^ crc[22];
16078
    crc_next[27] = (data_in[1] ^ crc[29])   ^ crc[23];
16079
    crc_next[28] = (data_in[2] ^ crc[30])   ^ crc[24];
16080
    crc_next[29] = (data_in[3] ^ crc[31])   ^ crc[25];
16081
    crc_next[30] =  crc[26];
16082
    crc_next[31] =  crc[27];
16083
 
16084
    crc = crc_next;
16085
    crc_error = crc[31:0] != 32'hc704dd7b;  // CRC not equal to magic number
16086
    case (nibble_cnt)
16087
    9: crc_out = {!crc[24], !crc[25], !crc[26], !crc[27], !crc[28], !crc[29], !crc[30], !crc[31],
16088
                  !crc[16], !crc[17], !crc[18], !crc[19], !crc[20], !crc[21], !crc[22], !crc[23],
16089
                  !crc[ 8], !crc[ 9], !crc[10], !crc[11], !crc[12], !crc[13], !crc[14], !crc[15],
16090
                  !crc[ 0], !crc[ 1], !crc[ 2], !crc[ 3], !crc[ 4], !crc[ 5], !crc[ 6], !crc[ 7]};
16091
    default: crc_out = crc_out;
16092
    endcase
16093
    // wait for delta time
16094
    delta_t = !delta_t;
16095
    // increment address and load new data
16096
    if ((word_cnt+3) == 7)//4)
16097
    begin
16098
      // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16099
      load_reg[31:24] = eth_phy.tx_mem[addr_cnt];
16100
      addr_cnt = addr_cnt + 1;
16101
      load_reg[23:16] = eth_phy.tx_mem[addr_cnt];
16102
      addr_cnt = addr_cnt + 1;
16103
      load_reg[15: 8] = eth_phy.tx_mem[addr_cnt];
16104
      addr_cnt = addr_cnt + 1;
16105
      load_reg[ 7: 0] = eth_phy.tx_mem[addr_cnt];
16106
      addr_cnt = addr_cnt + 1;
16107
    end
16108
    // set new load bit position
16109
    if((word_cnt+3) == 31)
16110
      word_cnt = 16;
16111
    else if ((word_cnt+3) == 23)
16112
      word_cnt = 8;
16113
    else if ((word_cnt+3) == 15)
16114
      word_cnt = 0;
16115
    else if ((word_cnt+3) == 7)
16116
      word_cnt = 24;
16117
    else
16118
      word_cnt = word_cnt + 4;// - 4;
16119
    // decrement nibble counter
16120
    nibble_cnt = nibble_cnt - 1;
16121
    // wait for delta time
16122
    delta_t = !delta_t;
16123
  end // while
16124
  #1;
16125
end
16126
endtask // paralel_crc_phy_tx
16127
 
16128
// paralel CRC calculating for PHY RX
16129
task paralel_crc_phy_rx;
16130
  input  [31:0] start_addr; // start address
16131
  input  [31:0] len; // length of frame in Bytes without CRC length
16132
  input         plus_dribble_nibble; // if length is longer for one nibble
16133 209 tadejm
  output [31:0] crc_out;
16134 169 mohor
  reg    [21:0] addr_cnt; // only 22 address lines
16135
  integer       word_cnt;
16136 209 tadejm
  integer       nibble_cnt;
16137 169 mohor
  reg    [31:0] load_reg;
16138
  reg           delta_t;
16139 209 tadejm
  reg    [31:0] crc_next;
16140
  reg    [31:0] crc;
16141
  reg           crc_error;
16142
  reg     [3:0] data_in;
16143
  integer       i;
16144 169 mohor
begin
16145
  #1 addr_cnt = start_addr[21:0];
16146 209 tadejm
  word_cnt = 24; // 27; // start of the frame - nibble granularity (MSbit first)
16147
  crc = 32'hFFFF_FFFF; // INITIAL value
16148 169 mohor
  delta_t = 0;
16149
  // length must include 4 bytes of ZEROs, to generate CRC
16150 209 tadejm
  // get number of nibbles from Byte length (2^1 = 2)
16151 169 mohor
  if (plus_dribble_nibble)
16152 209 tadejm
    nibble_cnt = ((len + 4) << 1) + 1'b1; // one nibble longer
16153 169 mohor
  else
16154 209 tadejm
    nibble_cnt = ((len + 4) << 1);
16155
  // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16156 169 mohor
  load_reg[31:24] = eth_phy.rx_mem[addr_cnt];
16157
  addr_cnt = addr_cnt + 1;
16158
  load_reg[23:16] = eth_phy.rx_mem[addr_cnt];
16159
  addr_cnt = addr_cnt + 1;
16160 209 tadejm
  load_reg[15: 8] = eth_phy.rx_mem[addr_cnt];
16161 169 mohor
  addr_cnt = addr_cnt + 1;
16162 209 tadejm
  load_reg[ 7: 0] = eth_phy.rx_mem[addr_cnt];
16163
  addr_cnt = addr_cnt + 1;
16164
  while (nibble_cnt > 0)
16165 169 mohor
  begin
16166
    // wait for delta time
16167
    delta_t = !delta_t;
16168
    // shift data in
16169 209 tadejm
 
16170
    if(nibble_cnt <= 8) // for additional 8 nibbles shift ZEROs in!
16171
      data_in[3:0] = 4'h0;
16172 169 mohor
    else
16173 209 tadejm
 
16174
      data_in[3:0] = {load_reg[word_cnt], load_reg[word_cnt+1], load_reg[word_cnt+2], load_reg[word_cnt+3]};
16175
    crc_next[0]  = (data_in[0] ^ crc[28]);
16176
    crc_next[1]  = (data_in[1] ^ data_in[0] ^ crc[28]    ^ crc[29]);
16177
    crc_next[2]  = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]);
16178
    crc_next[3]  = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]);
16179
    crc_next[4]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[0];
16180
    crc_next[5]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[1];
16181
    crc_next[6]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[ 2];
16182
    crc_next[7]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[3];
16183
    crc_next[8]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[4];
16184
    crc_next[9]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[5];
16185
    crc_next[10] = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[6];
16186
    crc_next[11] = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[7];
16187
    crc_next[12] = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]) ^ crc[8];
16188
    crc_next[13] = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]) ^ crc[9];
16189
    crc_next[14] = (data_in[3] ^ data_in[2] ^ crc[30]    ^ crc[31]) ^ crc[10];
16190
    crc_next[15] = (data_in[3] ^ crc[31])   ^ crc[11];
16191
    crc_next[16] = (data_in[0] ^ crc[28])   ^ crc[12];
16192
    crc_next[17] = (data_in[1] ^ crc[29])   ^ crc[13];
16193
    crc_next[18] = (data_in[2] ^ crc[30])   ^ crc[14];
16194
    crc_next[19] = (data_in[3] ^ crc[31])   ^ crc[15];
16195
    crc_next[20] =  crc[16];
16196
    crc_next[21] =  crc[17];
16197
    crc_next[22] = (data_in[0] ^ crc[28])   ^ crc[18];
16198
    crc_next[23] = (data_in[1] ^ data_in[0] ^ crc[29]    ^ crc[28]) ^ crc[19];
16199
    crc_next[24] = (data_in[2] ^ data_in[1] ^ crc[30]    ^ crc[29]) ^ crc[20];
16200
    crc_next[25] = (data_in[3] ^ data_in[2] ^ crc[31]    ^ crc[30]) ^ crc[21];
16201
    crc_next[26] = (data_in[3] ^ data_in[0] ^ crc[31]    ^ crc[28]) ^ crc[22];
16202
    crc_next[27] = (data_in[1] ^ crc[29])   ^ crc[23];
16203
    crc_next[28] = (data_in[2] ^ crc[30])   ^ crc[24];
16204
    crc_next[29] = (data_in[3] ^ crc[31])   ^ crc[25];
16205
    crc_next[30] =  crc[26];
16206
    crc_next[31] =  crc[27];
16207
 
16208
    crc = crc_next;
16209
    crc_error = crc[31:0] != 32'hc704dd7b;  // CRC not equal to magic number
16210
    case (nibble_cnt)
16211
    9: crc_out = {!crc[24], !crc[25], !crc[26], !crc[27], !crc[28], !crc[29], !crc[30], !crc[31],
16212
                  !crc[16], !crc[17], !crc[18], !crc[19], !crc[20], !crc[21], !crc[22], !crc[23],
16213
                  !crc[ 8], !crc[ 9], !crc[10], !crc[11], !crc[12], !crc[13], !crc[14], !crc[15],
16214
                  !crc[ 0], !crc[ 1], !crc[ 2], !crc[ 3], !crc[ 4], !crc[ 5], !crc[ 6], !crc[ 7]};
16215
    default: crc_out = crc_out;
16216
    endcase
16217 169 mohor
    // wait for delta time
16218
    delta_t = !delta_t;
16219
    // increment address and load new data
16220 209 tadejm
    if ((word_cnt+3) == 7)//4)
16221 169 mohor
    begin
16222 209 tadejm
      // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16223 169 mohor
      load_reg[31:24] = eth_phy.rx_mem[addr_cnt];
16224
      addr_cnt = addr_cnt + 1;
16225
      load_reg[23:16] = eth_phy.rx_mem[addr_cnt];
16226
      addr_cnt = addr_cnt + 1;
16227 209 tadejm
      load_reg[15: 8] = eth_phy.rx_mem[addr_cnt];
16228 169 mohor
      addr_cnt = addr_cnt + 1;
16229 209 tadejm
      load_reg[ 7: 0] = eth_phy.rx_mem[addr_cnt];
16230
      addr_cnt = addr_cnt + 1;
16231 169 mohor
    end
16232
    // set new load bit position
16233 209 tadejm
    if((word_cnt+3) == 31)
16234 169 mohor
      word_cnt = 16;
16235 209 tadejm
    else if ((word_cnt+3) == 23)
16236 169 mohor
      word_cnt = 8;
16237 209 tadejm
    else if ((word_cnt+3) == 15)
16238 169 mohor
      word_cnt = 0;
16239 209 tadejm
    else if ((word_cnt+3) == 7)
16240 169 mohor
      word_cnt = 24;
16241
    else
16242 209 tadejm
      word_cnt = word_cnt + 4;// - 4;
16243
    // decrement nibble counter
16244
    nibble_cnt = nibble_cnt - 1;
16245 169 mohor
    // wait for delta time
16246
    delta_t = !delta_t;
16247
  end // while
16248
  #1;
16249
end
16250
endtask // paralel_crc_phy_rx
16251
 
16252
// paralel CRC checking for MAC
16253
task paralel_crc_mac;
16254
  input  [31:0] start_addr; // start address
16255
  input  [31:0] len; // length of frame in Bytes without CRC length
16256
  input         plus_dribble_nibble; // if length is longer for one nibble
16257 209 tadejm
  output [31:0] crc_out;
16258
 
16259
  reg    [21:0] addr_cnt; // only 22 address lines
16260 169 mohor
  integer       word_cnt;
16261 209 tadejm
  integer       nibble_cnt;
16262 169 mohor
  reg    [31:0] load_reg;
16263
  reg           delta_t;
16264 209 tadejm
  reg    [31:0] crc_next;
16265
  reg    [31:0] crc;
16266
  reg           crc_error;
16267
  reg     [3:0] data_in;
16268
  integer       i;
16269 169 mohor
begin
16270
  #1 addr_cnt = start_addr[19:0];
16271
  // set starting point depending with which byte frame starts (e.g. if addr_cnt[1:0] == 0, then
16272
  //   MSB of the packet must be written to the LSB of Big ENDIAN Word [31:24])
16273
  if (addr_cnt[1:0] == 2'h1)
16274
    word_cnt = 16; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16275
  else if (addr_cnt[1:0] == 2'h2)
16276
    word_cnt = 8; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16277
  else if (addr_cnt[1:0] == 2'h3)
16278
    word_cnt = 0; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16279
  else
16280
    word_cnt = 24; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16281 209 tadejm
  crc = 32'hFFFF_FFFF; // INITIAL value
16282 169 mohor
  delta_t = 0;
16283
  // length must include 4 bytes of ZEROs, to generate CRC
16284 209 tadejm
  // get number of nibbles from Byte length (2^1 = 2)
16285 169 mohor
  if (plus_dribble_nibble)
16286 209 tadejm
    nibble_cnt = ((len + 4) << 1) + 1'b1; // one nibble longer
16287 169 mohor
  else
16288 209 tadejm
    nibble_cnt = ((len + 4) << 1);
16289 169 mohor
  load_reg = wb_slave.wb_memory[{12'h0, addr_cnt}];
16290 209 tadejm
  addr_cnt = addr_cnt + 4;
16291
  while (nibble_cnt > 0)
16292 169 mohor
  begin
16293
    // wait for delta time
16294
    delta_t = !delta_t;
16295
    // shift data in
16296 209 tadejm
 
16297
    if(nibble_cnt <= 8) // for additional 8 nibbles shift ZEROs in!
16298
      data_in[3:0] = 4'h0;
16299 169 mohor
    else
16300 209 tadejm
 
16301
      data_in[3:0] = {load_reg[word_cnt], load_reg[word_cnt+1], load_reg[word_cnt+2], load_reg[word_cnt+3]};
16302
    crc_next[0]  = (data_in[0] ^ crc[28]);
16303
    crc_next[1]  = (data_in[1] ^ data_in[0] ^ crc[28]    ^ crc[29]);
16304
    crc_next[2]  = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]);
16305
    crc_next[3]  = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]);
16306
    crc_next[4]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[0];
16307
    crc_next[5]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[1];
16308
    crc_next[6]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[ 2];
16309
    crc_next[7]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[3];
16310
    crc_next[8]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[4];
16311
    crc_next[9]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[5];
16312
    crc_next[10] = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[6];
16313
    crc_next[11] = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[7];
16314
    crc_next[12] = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]) ^ crc[8];
16315
    crc_next[13] = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]) ^ crc[9];
16316
    crc_next[14] = (data_in[3] ^ data_in[2] ^ crc[30]    ^ crc[31]) ^ crc[10];
16317
    crc_next[15] = (data_in[3] ^ crc[31])   ^ crc[11];
16318
    crc_next[16] = (data_in[0] ^ crc[28])   ^ crc[12];
16319
    crc_next[17] = (data_in[1] ^ crc[29])   ^ crc[13];
16320
    crc_next[18] = (data_in[2] ^ crc[30])   ^ crc[14];
16321
    crc_next[19] = (data_in[3] ^ crc[31])   ^ crc[15];
16322
    crc_next[20] =  crc[16];
16323
    crc_next[21] =  crc[17];
16324
    crc_next[22] = (data_in[0] ^ crc[28])   ^ crc[18];
16325
    crc_next[23] = (data_in[1] ^ data_in[0] ^ crc[29]    ^ crc[28]) ^ crc[19];
16326
    crc_next[24] = (data_in[2] ^ data_in[1] ^ crc[30]    ^ crc[29]) ^ crc[20];
16327
    crc_next[25] = (data_in[3] ^ data_in[2] ^ crc[31]    ^ crc[30]) ^ crc[21];
16328
    crc_next[26] = (data_in[3] ^ data_in[0] ^ crc[31]    ^ crc[28]) ^ crc[22];
16329
    crc_next[27] = (data_in[1] ^ crc[29])   ^ crc[23];
16330
    crc_next[28] = (data_in[2] ^ crc[30])   ^ crc[24];
16331
    crc_next[29] = (data_in[3] ^ crc[31])   ^ crc[25];
16332
    crc_next[30] =  crc[26];
16333
    crc_next[31] =  crc[27];
16334
 
16335
    crc = crc_next;
16336
    crc_error = crc[31:0] != 32'hc704dd7b;  // CRC not equal to magic number
16337
    case (nibble_cnt)
16338
    9: crc_out = {!crc[24], !crc[25], !crc[26], !crc[27], !crc[28], !crc[29], !crc[30], !crc[31],
16339
                  !crc[16], !crc[17], !crc[18], !crc[19], !crc[20], !crc[21], !crc[22], !crc[23],
16340
                  !crc[ 8], !crc[ 9], !crc[10], !crc[11], !crc[12], !crc[13], !crc[14], !crc[15],
16341
                  !crc[ 0], !crc[ 1], !crc[ 2], !crc[ 3], !crc[ 4], !crc[ 5], !crc[ 6], !crc[ 7]};
16342
    default: crc_out = crc_out;
16343
    endcase
16344 169 mohor
    // wait for delta time
16345
    delta_t = !delta_t;
16346 209 tadejm
    // increment address and load new data
16347
    if ((word_cnt+3) == 7)//4)
16348 169 mohor
    begin
16349 209 tadejm
      // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16350
      load_reg = wb_slave.wb_memory[{12'h0, addr_cnt}];
16351 169 mohor
      addr_cnt = addr_cnt + 4;
16352
    end
16353 209 tadejm
    // set new load bit position
16354
    if((word_cnt+3) == 31)
16355 169 mohor
      word_cnt = 16;
16356 209 tadejm
    else if ((word_cnt+3) == 23)
16357 169 mohor
      word_cnt = 8;
16358 209 tadejm
    else if ((word_cnt+3) == 15)
16359 169 mohor
      word_cnt = 0;
16360 209 tadejm
    else if ((word_cnt+3) == 7)
16361 169 mohor
      word_cnt = 24;
16362
    else
16363 209 tadejm
      word_cnt = word_cnt + 4;// - 4;
16364
    // decrement nibble counter
16365
    nibble_cnt = nibble_cnt - 1;
16366 169 mohor
    // wait for delta time
16367
    delta_t = !delta_t;
16368
  end // while
16369
  #1;
16370
end
16371
endtask // paralel_crc_mac
16372
 
16373
// serial CRC checking for PHY TX
16374
task serial_crc_phy_tx;
16375
  input  [31:0] start_addr; // start address
16376
  input  [31:0] len; // length of frame in Bytes without CRC length
16377
  input         plus_dribble_nibble; // if length is longer for one nibble
16378
  output [31:0] crc;
16379
  reg    [21:0] addr_cnt; // only 22 address lines
16380
  integer       word_cnt;
16381
  integer       bit_cnt;
16382
  reg    [31:0] load_reg;
16383
  reg    [31:0] crc_shift_reg;
16384
  reg    [31:0] crc_store_reg;
16385
  reg           delta_t;
16386
begin
16387
  #1 addr_cnt = start_addr[21:0];
16388
  word_cnt = 24; // 27; // start of the frame - nibble granularity (MSbit first)
16389
  crc_store_reg = 32'hFFFF_FFFF; // INITIAL value
16390
  delta_t = 0;
16391
  // length must include 4 bytes of ZEROs, to generate CRC
16392
  // get number of bits from Byte length (2^3 = 8)
16393
  if (plus_dribble_nibble)
16394
    bit_cnt = ((len + 4) << 3) + 3'h4; // one nibble longer
16395
  else
16396
    bit_cnt = ((len + 4) << 3);
16397
  // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16398
  load_reg[31:24] = eth_phy.tx_mem[addr_cnt];
16399
  addr_cnt = addr_cnt + 1;
16400
  load_reg[23:16] = eth_phy.tx_mem[addr_cnt];
16401
  addr_cnt = addr_cnt + 1;
16402
  load_reg[15: 8] = eth_phy.tx_mem[addr_cnt];
16403
  addr_cnt = addr_cnt + 1;
16404
  load_reg[ 7: 0] = eth_phy.tx_mem[addr_cnt];
16405
  addr_cnt = addr_cnt + 1;
16406
#1;
16407
  while (bit_cnt > 0)
16408
  begin
16409
    // wait for delta time
16410
    delta_t = !delta_t;
16411
#1;
16412
    // shift data in
16413
 
16414
    if(bit_cnt <= 32) // for additional 32 bits shift ZEROs in!
16415
     crc_shift_reg[0] = 1'b0               ^ crc_store_reg[31];
16416
    else
16417
 
16418
     crc_shift_reg[0] = load_reg[word_cnt] ^ crc_store_reg[31];
16419
    crc_shift_reg[1]  = crc_store_reg[0]   ^ crc_store_reg[31];
16420
    crc_shift_reg[2]  = crc_store_reg[1]   ^ crc_store_reg[31];
16421
    crc_shift_reg[3]  = crc_store_reg[2];
16422
    crc_shift_reg[4]  = crc_store_reg[3]   ^ crc_store_reg[31];
16423
    crc_shift_reg[5]  = crc_store_reg[4]   ^ crc_store_reg[31];
16424
    crc_shift_reg[6]  = crc_store_reg[5];
16425
    crc_shift_reg[7]  = crc_store_reg[6]   ^ crc_store_reg[31];
16426
    crc_shift_reg[8]  = crc_store_reg[7]   ^ crc_store_reg[31];
16427
    crc_shift_reg[9]  = crc_store_reg[8];
16428
    crc_shift_reg[10] = crc_store_reg[9]   ^ crc_store_reg[31];
16429
    crc_shift_reg[11] = crc_store_reg[10]  ^ crc_store_reg[31];
16430
    crc_shift_reg[12] = crc_store_reg[11]  ^ crc_store_reg[31];
16431
    crc_shift_reg[13] = crc_store_reg[12];
16432
    crc_shift_reg[14] = crc_store_reg[13];
16433
    crc_shift_reg[15] = crc_store_reg[14];
16434
    crc_shift_reg[16] = crc_store_reg[15]  ^ crc_store_reg[31];
16435
    crc_shift_reg[17] = crc_store_reg[16];
16436
    crc_shift_reg[18] = crc_store_reg[17];
16437
    crc_shift_reg[19] = crc_store_reg[18];
16438
    crc_shift_reg[20] = crc_store_reg[19];
16439
    crc_shift_reg[21] = crc_store_reg[20];
16440
    crc_shift_reg[22] = crc_store_reg[21]  ^ crc_store_reg[31];
16441
    crc_shift_reg[23] = crc_store_reg[22]  ^ crc_store_reg[31];
16442
    crc_shift_reg[24] = crc_store_reg[23];
16443
    crc_shift_reg[25] = crc_store_reg[24];
16444
    crc_shift_reg[26] = crc_store_reg[25]  ^ crc_store_reg[31];
16445
    crc_shift_reg[27] = crc_store_reg[26];
16446
    crc_shift_reg[28] = crc_store_reg[27];
16447
    crc_shift_reg[29] = crc_store_reg[28];
16448
    crc_shift_reg[30] = crc_store_reg[29];
16449
    crc_shift_reg[31] = crc_store_reg[30];
16450
    // wait for delta time
16451
    delta_t = !delta_t;
16452
 
16453
    // store previous data
16454
    crc_store_reg = crc_shift_reg;
16455
 
16456
    // put CRC out
16457
    case (bit_cnt)
16458
    33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 1:
16459
    begin
16460
      crc = crc_store_reg;
16461
      crc = {!crc[24], !crc[25], !crc[26], !crc[27], !crc[28], !crc[29], !crc[30], !crc[31],
16462
             !crc[16], !crc[17], !crc[18], !crc[19], !crc[20], !crc[21], !crc[22], !crc[23],
16463
             !crc[ 8], !crc[ 9], !crc[10], !crc[11], !crc[12], !crc[13], !crc[14], !crc[15],
16464
             !crc[ 0], !crc[ 1], !crc[ 2], !crc[ 3], !crc[ 4], !crc[ 5], !crc[ 6], !crc[ 7]};
16465
    end
16466
    default: crc = crc;
16467
    endcase
16468
 
16469
    // increment address and load new data
16470
#1;
16471
    if (word_cnt == 7)//4)
16472
    begin
16473
      // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16474
      load_reg[31:24] = eth_phy.tx_mem[addr_cnt];
16475
//      load_reg[31:24] = {load_reg[28], load_reg[29], load_reg[30], load_reg[31], 
16476
//                         load_reg[24], load_reg[25], load_reg[26], load_reg[27]};
16477
      addr_cnt = addr_cnt + 1;
16478
      load_reg[23:16] = eth_phy.tx_mem[addr_cnt];
16479
//      load_reg[23:16] = {load_reg[20], load_reg[21], load_reg[22], load_reg[23], 
16480
//                         load_reg[16], load_reg[17], load_reg[18], load_reg[19]};
16481
      addr_cnt = addr_cnt + 1;
16482
      load_reg[15: 8] = eth_phy.tx_mem[addr_cnt];
16483
//      load_reg[15: 8] = {load_reg[12], load_reg[13], load_reg[14], load_reg[15], 
16484
//                         load_reg[ 8], load_reg[ 9], load_reg[10], load_reg[11]};
16485
      addr_cnt = addr_cnt + 1;
16486
      load_reg[ 7: 0] = eth_phy.tx_mem[addr_cnt];
16487
//      load_reg[ 7: 0] = {load_reg[ 4], load_reg[ 5], load_reg[ 6], load_reg[ 7], 
16488
//                         load_reg[ 0], load_reg[ 1], load_reg[ 2], load_reg[ 3]};
16489
      addr_cnt = addr_cnt + 1;
16490
    end
16491
#1;
16492
    // set new load bit position
16493
    if(word_cnt == 31)
16494
      word_cnt = 16;
16495
    else if (word_cnt == 23)
16496
      word_cnt = 8;
16497
    else if (word_cnt == 15)
16498
      word_cnt = 0;
16499
    else if (word_cnt == 7)
16500
      word_cnt = 24;
16501
 
16502
//   if(word_cnt == 24)
16503
//     word_cnt = 31;
16504
//   else if (word_cnt == 28)
16505
//     word_cnt = 19;
16506
//   else if (word_cnt == 16)
16507
//     word_cnt = 23;
16508
//   else if (word_cnt == 20)
16509
//     word_cnt = 11;
16510
//   else if(word_cnt == 8)
16511
//     word_cnt = 15;
16512
//   else if (word_cnt == 12)
16513
//     word_cnt = 3;
16514
//   else if (word_cnt == 0)
16515
//     word_cnt = 7;
16516
//   else if (word_cnt == 4)
16517
//     word_cnt = 27;
16518
    else
16519
      word_cnt = word_cnt + 1;// - 1;
16520
#1;
16521
    // decrement bit counter
16522
    bit_cnt = bit_cnt - 1;
16523
#1;
16524
    // wait for delta time
16525
    delta_t = !delta_t;
16526
  end // while
16527
 
16528
  #1;
16529
end
16530
endtask // serial_crc_phy_tx
16531
 
16532
// serial CRC calculating for PHY RX
16533
task serial_crc_phy_rx;
16534
  input  [31:0] start_addr; // start address
16535
  input  [31:0] len; // length of frame in Bytes without CRC length
16536
  input         plus_dribble_nibble; // if length is longer for one nibble
16537
  output [31:0] crc;
16538
  reg    [21:0] addr_cnt; // only 22 address lines
16539
  integer       word_cnt;
16540
  integer       bit_cnt;
16541
  reg    [31:0] load_reg;
16542
  reg    [31:0] crc_shift_reg;
16543
  reg    [31:0] crc_store_reg;
16544
  reg           delta_t;
16545
begin
16546
  #1 addr_cnt = start_addr[21:0];
16547
  word_cnt = 24; // start of the frame
16548
  crc_shift_reg = 0;
16549
  delta_t = 0;
16550
  // length must include 4 bytes of ZEROs, to generate CRC
16551
  // get number of bits from Byte length (2^3 = 8)
16552
  if (plus_dribble_nibble)
16553
    bit_cnt = ((len + 4) << 3) + 3'h4; // one nibble longer
16554
  else
16555
    bit_cnt = ((len + 4) << 3);
16556
  load_reg[31:24] = eth_phy.rx_mem[addr_cnt];
16557
  addr_cnt = addr_cnt + 1;
16558
  load_reg[23:16] = eth_phy.rx_mem[addr_cnt];
16559
  addr_cnt = addr_cnt + 1;
16560
  load_reg[15:8]  = eth_phy.rx_mem[addr_cnt];
16561
  addr_cnt = addr_cnt + 1;
16562
  load_reg[7:0]   = eth_phy.rx_mem[addr_cnt];
16563
 
16564
  while (bit_cnt > 0)
16565
  begin
16566
    // wait for delta time
16567
    delta_t = !delta_t;
16568
    // store previous data
16569
    crc_store_reg = crc_shift_reg;
16570
    // shift data in
16571
    if(bit_cnt <= 32) // for additional 32 bits shift ZEROs in!
16572
     crc_shift_reg[0] = 1'b0               ^ crc_store_reg[31];
16573
    else
16574
     crc_shift_reg[0] = load_reg[word_cnt] ^ crc_store_reg[31];
16575
    crc_shift_reg[1]  = crc_store_reg[0]   ^ crc_store_reg[31];
16576
    crc_shift_reg[2]  = crc_store_reg[1]   ^ crc_store_reg[31];
16577
    crc_shift_reg[3]  = crc_store_reg[2];
16578
    crc_shift_reg[4]  = crc_store_reg[3]   ^ crc_store_reg[31];
16579
    crc_shift_reg[5]  = crc_store_reg[4]   ^ crc_store_reg[31];
16580
    crc_shift_reg[6]  = crc_store_reg[5];
16581
    crc_shift_reg[7]  = crc_store_reg[6]   ^ crc_store_reg[31];
16582
    crc_shift_reg[8]  = crc_store_reg[7]   ^ crc_store_reg[31];
16583
    crc_shift_reg[9]  = crc_store_reg[8];
16584
    crc_shift_reg[10] = crc_store_reg[9]   ^ crc_store_reg[31];
16585
    crc_shift_reg[11] = crc_store_reg[10]  ^ crc_store_reg[31];
16586
    crc_shift_reg[12] = crc_store_reg[11]  ^ crc_store_reg[31];
16587
    crc_shift_reg[13] = crc_store_reg[12];
16588
    crc_shift_reg[14] = crc_store_reg[13];
16589
    crc_shift_reg[15] = crc_store_reg[14];
16590
    crc_shift_reg[16] = crc_store_reg[15]  ^ crc_store_reg[31];
16591
    crc_shift_reg[17] = crc_store_reg[16];
16592
    crc_shift_reg[18] = crc_store_reg[17];
16593
    crc_shift_reg[19] = crc_store_reg[18];
16594
    crc_shift_reg[20] = crc_store_reg[19];
16595
    crc_shift_reg[21] = crc_store_reg[20];
16596
    crc_shift_reg[22] = crc_store_reg[21]  ^ crc_store_reg[31];
16597
    crc_shift_reg[23] = crc_store_reg[22]  ^ crc_store_reg[31];
16598
    crc_shift_reg[24] = crc_store_reg[23];
16599
    crc_shift_reg[25] = crc_store_reg[24];
16600
    crc_shift_reg[26] = crc_store_reg[25]  ^ crc_store_reg[31];
16601
    crc_shift_reg[27] = crc_store_reg[26];
16602
    crc_shift_reg[28] = crc_store_reg[27];
16603
    crc_shift_reg[29] = crc_store_reg[28];
16604
    crc_shift_reg[30] = crc_store_reg[29];
16605
    crc_shift_reg[31] = crc_store_reg[30];
16606
    // wait for delta time
16607
    delta_t = !delta_t;
16608
    // increment address and load new data
16609
    if (word_cnt == 7)
16610
    begin
16611
      addr_cnt = addr_cnt + 1;
16612
      load_reg[31:24] = eth_phy.rx_mem[addr_cnt];
16613
      addr_cnt = addr_cnt + 1;
16614
      load_reg[23:16] = eth_phy.rx_mem[addr_cnt];
16615
      addr_cnt = addr_cnt + 1;
16616
      load_reg[15:8]  = eth_phy.rx_mem[addr_cnt];
16617
      addr_cnt = addr_cnt + 1;
16618
      load_reg[7:0]   = eth_phy.rx_mem[addr_cnt];
16619
    end
16620
    // set new load bit position
16621
    if(word_cnt == 31)
16622
      word_cnt = 16;
16623
    else if (word_cnt == 23)
16624
      word_cnt = 8;
16625
    else if (word_cnt == 15)
16626
      word_cnt = 0;
16627
    else if (word_cnt == 7)
16628
      word_cnt = 24;
16629
    else
16630
      word_cnt = word_cnt + 1;
16631
    // decrement bit counter
16632
    bit_cnt = bit_cnt - 1;
16633
    // wait for delta time
16634
    delta_t = !delta_t;
16635
  end // while
16636
 
16637
  // put CRC out
16638
  crc = crc_shift_reg;
16639
  #1;
16640
end
16641
endtask // serial_crc_phy_rx
16642
 
16643
// serial CRC checking for MAC
16644
task serial_crc_mac;
16645
  input  [31:0] start_addr; // start address
16646
  input  [31:0] len; // length of frame in Bytes without CRC length
16647
  input         plus_dribble_nibble; // if length is longer for one nibble
16648
  output [31:0] crc;
16649
  reg    [19:0] addr_cnt; // only 20 address lines
16650
  integer       word_cnt;
16651
  integer       bit_cnt;
16652
  reg    [31:0] load_reg;
16653
  reg    [31:0] crc_shift_reg;
16654
  reg    [31:0] crc_store_reg;
16655
  reg           delta_t;
16656
begin
16657
  #1 addr_cnt = start_addr[19:0];
16658
  // set starting point depending with which byte frame starts (e.g. if addr_cnt[1:0] == 0, then
16659
  //   MSB of the packet must be written to the LSB of Big ENDIAN Word [31:24])
16660
  if (addr_cnt[1:0] == 2'h1)
16661
    word_cnt = 16; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16662
  else if (addr_cnt[1:0] == 2'h2)
16663
    word_cnt = 8; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16664
  else if (addr_cnt[1:0] == 2'h3)
16665
    word_cnt = 0; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16666
  else
16667
    word_cnt = 24; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16668
 
16669
  crc_shift_reg = 0;
16670
  delta_t = 0;
16671
  // length must include 4 bytes of ZEROs, to generate CRC
16672
  // get number of bits from Byte length (2^3 = 8)
16673
  if (plus_dribble_nibble)
16674
    bit_cnt = ((len + 4) << 3) + 3'h4; // one nibble longer
16675
  else
16676
    bit_cnt = ((len + 4) << 3);
16677
  load_reg = wb_slave.wb_memory[{12'h0, addr_cnt}];
16678
 
16679
  while (bit_cnt > 0)
16680
  begin
16681
    // wait for delta time
16682
    delta_t = !delta_t;
16683
    // store previous data
16684
    crc_store_reg = crc_shift_reg;
16685
    // shift data in
16686
    if(bit_cnt <= 32) // for additional 32 bits shift ZEROs in!
16687
     crc_shift_reg[0] = 1'b0               ^ crc_store_reg[31];
16688
    else
16689
     crc_shift_reg[0] = load_reg[word_cnt] ^ crc_store_reg[31];
16690
    crc_shift_reg[1]  = crc_store_reg[0]   ^ crc_store_reg[31];
16691
    crc_shift_reg[2]  = crc_store_reg[1]   ^ crc_store_reg[31];
16692
    crc_shift_reg[3]  = crc_store_reg[2];
16693
    crc_shift_reg[4]  = crc_store_reg[3]   ^ crc_store_reg[31];
16694
    crc_shift_reg[5]  = crc_store_reg[4]   ^ crc_store_reg[31];
16695
    crc_shift_reg[6]  = crc_store_reg[5];
16696
    crc_shift_reg[7]  = crc_store_reg[6]   ^ crc_store_reg[31];
16697
    crc_shift_reg[8]  = crc_store_reg[7]   ^ crc_store_reg[31];
16698
    crc_shift_reg[9]  = crc_store_reg[8];
16699
    crc_shift_reg[10] = crc_store_reg[9]   ^ crc_store_reg[31];
16700
    crc_shift_reg[11] = crc_store_reg[10]  ^ crc_store_reg[31];
16701
    crc_shift_reg[12] = crc_store_reg[11]  ^ crc_store_reg[31];
16702
    crc_shift_reg[13] = crc_store_reg[12];
16703
    crc_shift_reg[14] = crc_store_reg[13];
16704
    crc_shift_reg[15] = crc_store_reg[14];
16705
    crc_shift_reg[16] = crc_store_reg[15]  ^ crc_store_reg[31];
16706
    crc_shift_reg[17] = crc_store_reg[16];
16707
    crc_shift_reg[18] = crc_store_reg[17];
16708
    crc_shift_reg[19] = crc_store_reg[18];
16709
    crc_shift_reg[20] = crc_store_reg[19];
16710
    crc_shift_reg[21] = crc_store_reg[20];
16711
    crc_shift_reg[22] = crc_store_reg[21]  ^ crc_store_reg[31];
16712
    crc_shift_reg[23] = crc_store_reg[22]  ^ crc_store_reg[31];
16713
    crc_shift_reg[24] = crc_store_reg[23];
16714
    crc_shift_reg[25] = crc_store_reg[24];
16715
    crc_shift_reg[26] = crc_store_reg[25]  ^ crc_store_reg[31];
16716
    crc_shift_reg[27] = crc_store_reg[26];
16717
    crc_shift_reg[28] = crc_store_reg[27];
16718
    crc_shift_reg[29] = crc_store_reg[28];
16719
    crc_shift_reg[30] = crc_store_reg[29];
16720
    crc_shift_reg[31] = crc_store_reg[30];
16721
    // wait for delta time
16722
    delta_t = !delta_t;
16723
    // increment address and load new data for Big ENDIAN Bytes (Litle ENDIAN bits)
16724
    if (word_cnt == 7)
16725
    begin
16726
      addr_cnt = addr_cnt + 4;
16727
      load_reg = wb_slave.wb_memory[{12'h0, addr_cnt}];
16728
    end
16729
    // set new load bit position for Big ENDIAN Bytes (Litle ENDIAN bits)
16730
    if(word_cnt == 31)
16731
      word_cnt = 16;
16732
    else if (word_cnt == 23)
16733
      word_cnt = 8;
16734
    else if (word_cnt == 15)
16735
      word_cnt = 0;
16736
    else if (word_cnt == 7)
16737
      word_cnt = 24;
16738
    else
16739
      word_cnt = word_cnt + 1;
16740
    // decrement bit counter
16741
    bit_cnt = bit_cnt - 1;
16742
    // wait for delta time
16743
    delta_t = !delta_t;
16744
  end // while
16745
 
16746
  // put CRC out
16747
  crc = crc_shift_reg;
16748
  #1;
16749
end
16750
endtask // serial_crc_mac
16751
 
16752
//////////////////////////////////////////////////////////////
16753
// MIIM Basic tasks
16754
//////////////////////////////////////////////////////////////
16755
 
16756
task reset_mii; //  MII module
16757
  reg [31:0] tmp;
16758
  reg [31:0] tmp_no_rst;
16759
begin
16760
  // read MII mode register first
16761
  wbm_read(`ETH_MIIMODER, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16762
  // set reset bit - write back to MII mode register with RESET bit
16763
  wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_RST | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16764
  // clear reset bit - write back to MII mode register without RESET bit
16765
  tmp_no_rst = `ETH_MIIMODER_RST;
16766
  tmp_no_rst = ~tmp_no_rst;
16767
  wbm_write(`ETH_MIIMODER, (tmp_no_rst & tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16768
end
16769
endtask // reset_mii
16770
 
16771
task mii_set_clk_div; // set clock divider for MII clock
16772
  input [7:0]  clk_div;
16773
begin
16774
  // MII mode register
16775
  wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_CLKDIV & clk_div), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16776
end
16777
endtask // mii_set_clk_div
16778
 
16779
 
16780
task check_mii_busy; // MII - check if BUSY
16781
  reg [31:0] tmp;
16782
begin
16783
  @(posedge wb_clk);
16784
  // MII read status register
16785
  wbm_read(`ETH_MIISTATUS, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16786
  while(tmp[`ETH_MIISTATUS_BUSY] !== 1'b0) //`ETH_MIISTATUS_BUSY
16787
  begin
16788
    @(posedge wb_clk);
16789
    wbm_read(`ETH_MIISTATUS, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16790
  end
16791
end
16792
endtask // check_mii_busy
16793
 
16794
 
16795
task check_mii_scan_valid; // MII - check if SCAN data are valid
16796
  reg [31:0] tmp;
16797
begin
16798
  @(posedge wb_clk);
16799
  // MII read status register
16800
  wbm_read(`ETH_MIISTATUS, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16801
  while(tmp[`ETH_MIISTATUS_NVALID] !== 1'b0) //`ETH_MIISTATUS_NVALID
16802
  begin
16803
    @(posedge wb_clk);
16804
    wbm_read(`ETH_MIISTATUS, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16805
  end
16806
end
16807
endtask // check_mii_scan_valid
16808
 
16809
 
16810
task mii_write_req; // requests write to MII
16811
  input [4:0]  phy_addr;
16812
  input [4:0]  reg_addr;
16813
  input [15:0] data_in;
16814
begin
16815
  // MII address, PHY address = 1, command register address = 0
16816
  wbm_write(`ETH_MIIADDRESS, (`ETH_MIIADDRESS_FIAD & phy_addr) | (`ETH_MIIADDRESS_RGAD & (reg_addr << 8)),
16817
            4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16818
  // MII TX data
16819
  wbm_write(`ETH_MIITX_DATA, {16'h0000, data_in}, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16820
  // MII command
16821
  wbm_write(`ETH_MIICOMMAND, `ETH_MIICOMMAND_WCTRLDATA, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16822
  @(posedge wb_clk);
16823
end
16824
endtask // mii_write_req
16825
 
16826
 
16827
task mii_read_req; // requests read from MII
16828
  input [4:0]  phy_addr;
16829
  input [4:0]  reg_addr;
16830
begin
16831
  // MII address, PHY address = 1, command register address = 0
16832
  wbm_write(`ETH_MIIADDRESS, (`ETH_MIIADDRESS_FIAD & phy_addr) | (`ETH_MIIADDRESS_RGAD & (reg_addr << 8)),
16833
            4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16834
  // MII command
16835
  wbm_write(`ETH_MIICOMMAND, `ETH_MIICOMMAND_RSTAT, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16836
  @(posedge wb_clk);
16837
end
16838
endtask // mii_read_req
16839
 
16840
 
16841
task mii_scan_req; // requests scan from MII
16842
  input [4:0]  phy_addr;
16843
  input [4:0]  reg_addr;
16844
begin
16845
  // MII address, PHY address = 1, command register address = 0
16846
  wbm_write(`ETH_MIIADDRESS, (`ETH_MIIADDRESS_FIAD & phy_addr) | (`ETH_MIIADDRESS_RGAD & (reg_addr << 8)),
16847
            4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16848
  // MII command
16849
  wbm_write(`ETH_MIICOMMAND, `ETH_MIICOMMAND_SCANSTAT, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16850
  @(posedge wb_clk);
16851
end
16852
endtask // mii_scan_req
16853
 
16854
 
16855
task mii_scan_finish; // finish scan from MII
16856
begin
16857
  // MII command
16858
  wbm_write(`ETH_MIICOMMAND, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16859
  @(posedge wb_clk);
16860
end
16861
endtask // mii_scan_finish
16862
 
16863
//////////////////////////////////////////////////////////////
16864
// Log files and memory tasks
16865
//////////////////////////////////////////////////////////////
16866
 
16867
task clear_memories;
16868
  reg    [22:0]  adr_i;
16869
  reg            delta_t;
16870
begin
16871
  delta_t = 0;
16872
  for (adr_i = 0; adr_i < 4194304; adr_i = adr_i + 1)
16873
  begin
16874
    eth_phy.rx_mem[adr_i[21:0]] = 0;
16875
    eth_phy.tx_mem[adr_i[21:0]] = 0;
16876
    wb_slave.wb_memory[adr_i[21:2]] = 0;
16877
    delta_t = !delta_t;
16878
  end
16879
end
16880
endtask // clear_memories
16881
 
16882 243 tadejm
task clear_buffer_descriptors;
16883
  reg    [8:0]  adr_i;
16884
  reg            delta_t;
16885
begin
16886
  delta_t = 0;
16887
  for (adr_i = 0; adr_i < 256; adr_i = adr_i + 1)
16888
  begin
16889
    wbm_write((`TX_BD_BASE + {adr_i[7:0], 2'b0}), 32'h0, 4'hF, 1, 4'h1, 4'h1);
16890
    delta_t = !delta_t;
16891
  end
16892
end
16893
endtask // clear_buffer_descriptors
16894
 
16895 169 mohor
task test_note;
16896
  input [799:0] test_note ;
16897
  reg   [799:0] display_note ;
16898
begin
16899
  display_note = test_note;
16900
  while ( display_note[799:792] == 0 )
16901
    display_note = display_note << 8 ;
16902
  $fdisplay( tb_log_file, " " ) ;
16903
  $fdisplay( tb_log_file, "NOTE: %s", display_note ) ;
16904
  $fdisplay( tb_log_file, " " ) ;
16905
end
16906
endtask // test_note
16907
 
16908
task test_heading;
16909
  input [799:0] test_heading ;
16910
  reg   [799:0] display_test ;
16911
begin
16912
  display_test = test_heading;
16913
  while ( display_test[799:792] == 0 )
16914
    display_test = display_test << 8 ;
16915
  $fdisplay( tb_log_file, "  ***************************************************************************************" ) ;
16916
  $fdisplay( tb_log_file, "  ***************************************************************************************" ) ;
16917
  $fdisplay( tb_log_file, "  Heading: %s", display_test ) ;
16918
  $fdisplay( tb_log_file, "  ***************************************************************************************" ) ;
16919
  $fdisplay( tb_log_file, "  ***************************************************************************************" ) ;
16920
  $fdisplay( tb_log_file, " " ) ;
16921
end
16922
endtask // test_heading
16923
 
16924
 
16925
task test_fail ;
16926
  input [7999:0] failure_reason ;
16927
//  reg   [8007:0] display_failure ;
16928
  reg   [7999:0] display_failure ;
16929
  reg   [799:0] display_test ;
16930
begin
16931
  tests_failed = tests_failed + 1 ;
16932
 
16933
  display_failure = failure_reason; // {failure_reason, "!"} ;
16934
  while ( display_failure[7999:7992] == 0 )
16935
    display_failure = display_failure << 8 ;
16936
 
16937
  display_test = test_name ;
16938
  while ( display_test[799:792] == 0 )
16939
    display_test = display_test << 8 ;
16940
 
16941
  $fdisplay( tb_log_file, "    *************************************************************************************" ) ;
16942
  $fdisplay( tb_log_file, "    At time: %t ", $time ) ;
16943
  $fdisplay( tb_log_file, "    Test: %s", display_test ) ;
16944
  $fdisplay( tb_log_file, "    *FAILED* because") ;
16945
  $fdisplay( tb_log_file, "    %s", display_failure ) ;
16946
  $fdisplay( tb_log_file, "    *************************************************************************************" ) ;
16947
  $fdisplay( tb_log_file, " " ) ;
16948
 
16949
 `ifdef STOP_ON_FAILURE
16950
    #20 $stop ;
16951
 `endif
16952
end
16953
endtask // test_fail
16954
 
16955
 
16956
task test_ok ;
16957
  reg [799:0] display_test ;
16958
begin
16959
  tests_successfull = tests_successfull + 1 ;
16960
 
16961
  display_test = test_name ;
16962
  while ( display_test[799:792] == 0 )
16963
    display_test = display_test << 8 ;
16964
 
16965
  $fdisplay( tb_log_file, "    *************************************************************************************" ) ;
16966
  $fdisplay( tb_log_file, "    At time: %t ", $time ) ;
16967
  $fdisplay( tb_log_file, "    Test: %s", display_test ) ;
16968
  $fdisplay( tb_log_file, "    reported *SUCCESSFULL*! ") ;
16969
  $fdisplay( tb_log_file, "    *************************************************************************************" ) ;
16970
  $fdisplay( tb_log_file, " " ) ;
16971
end
16972
endtask // test_ok
16973
 
16974
 
16975
task test_summary;
16976
begin
16977
  $fdisplay(tb_log_file, "**************************** Ethernet MAC test summary **********************************") ;
16978
  $fdisplay(tb_log_file, "Tests performed:   %d", tests_successfull + tests_failed) ;
16979
  $fdisplay(tb_log_file, "Failed tests   :   %d", tests_failed) ;
16980
  $fdisplay(tb_log_file, "Successfull tests: %d", tests_successfull) ;
16981
  $fdisplay(tb_log_file, "**************************** Ethernet MAC test summary **********************************") ;
16982
  $fclose(tb_log_file) ;
16983
end
16984
endtask // test_summary
16985
 
16986
 
16987 116 mohor
endmodule

powered by: WebSVN 2.1.0

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