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

Subversion Repositories ethmac

[/] [ethmac/] [tags/] [rel_14/] [bench/] [verilog/] [tb_ethernet.v] - Blame information for rev 338

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 267 mohor
// Revision 1.24  2002/11/22 17:29:42  mohor
46
// Flow control test almost finished.
47
//
48 266 mohor
// Revision 1.23  2002/11/22 02:12:16  mohor
49
// test_mac_full_duplex_flow_control tests pretty much finished.
50
// TEST 0: INSERT CONTROL FRM. WHILE TRANSMITTING NORMAL
51
// FRM. AT 4 TX BD ( 10Mbps ) finished.
52
// TEST 2: RECEIVE CONTROL FRAMES WITH PASSALL OPTION
53
// TURNED OFF AT ONE RX BD ( 10Mbps ) finished.
54
//
55 263 mohor
// Revision 1.22  2002/11/21 13:56:50  mohor
56
// test_mac_full_duplex_flow test 0 finished. Sending the control (PAUSE) frame
57
// finished.
58
//
59 260 mohor
// Revision 1.21  2002/11/19 20:27:45  mohor
60
// Temp version.
61
//
62 254 mohor
// Revision 1.20  2002/11/19 17:41:19  tadejm
63
// Just some updates.
64
//
65 252 tadejm
// Revision 1.19  2002/11/14 13:12:47  tadejm
66
// Late collision is not reported any more.
67
//
68 243 tadejm
// Revision 1.18  2002/10/18 17:03:34  tadejm
69
// Changed BIST scan signals.
70
//
71 227 tadejm
// Revision 1.17  2002/10/18 13:58:22  tadejm
72
// Some code changed due to bug fixes.
73
//
74 223 tadejm
// Revision 1.16  2002/10/09 13:16:51  tadejm
75
// Just back-up; not completed testbench and some testcases are not
76
// wotking properly yet.
77
//
78 209 tadejm
// Revision 1.15  2002/09/20 14:29:12  tadej
79
// Full duplex tests modified and testbench bug repaired.
80
//
81 194 tadej
// Revision 1.14  2002/09/18 17:56:38  tadej
82
// Some additional reports added
83
//
84 192 tadej
// Revision 1.13  2002/09/16 17:53:49  tadej
85
// Full duplex test improved.
86
//
87 182 tadej
// Revision 1.12  2002/09/16 15:10:42  mohor
88
// MIIM test look better.
89
//
90 181 mohor
// Revision 1.11  2002/09/13 19:18:04  mohor
91
// Bench outputs data to display every 128 bytes.
92
//
93 180 mohor
// Revision 1.10  2002/09/13 18:44:29  mohor
94
// Beautiful tests merget together
95
//
96 179 mohor
// Revision 1.9  2002/09/13 18:41:45  mohor
97
// Rearanged testcases
98
//
99 178 mohor
// Revision 1.8  2002/09/13 14:50:15  mohor
100
// Bug in MIIM fixed.
101
//
102 177 mohor
// Revision 1.7  2002/09/13 12:29:14  mohor
103
// Headers changed.
104
//
105 170 mohor
// Revision 1.6  2002/09/13 11:57:20  mohor
106
// New testbench. Thanks to Tadej M - "The Spammer".
107
//
108 121 mohor
// Revision 1.2  2002/07/19 14:02:47  mohor
109
// Clock mrx_clk set to 2.5 MHz.
110
//
111 117 mohor
// Revision 1.1  2002/07/19 13:57:53  mohor
112
// Testing environment also includes traffic cop, memory interface and host
113
// interface.
114 116 mohor
//
115
//
116
//
117
//
118 117 mohor
//
119 116 mohor
 
120
 
121 169 mohor
`include "eth_phy_defines.v"
122
`include "wb_model_defines.v"
123 116 mohor
`include "tb_eth_defines.v"
124
`include "eth_defines.v"
125
`include "timescale.v"
126
 
127
module tb_ethernet();
128
 
129
 
130 169 mohor
reg           wb_clk;
131
reg           wb_rst;
132
wire          wb_int;
133 116 mohor
 
134 169 mohor
wire          mtx_clk;  // This goes to PHY
135
wire          mrx_clk;  // This goes to PHY
136 116 mohor
 
137
wire   [3:0]  MTxD;
138
wire          MTxEn;
139
wire          MTxErr;
140
 
141 169 mohor
wire   [3:0]  MRxD;     // This goes to PHY
142
wire          MRxDV;    // This goes to PHY
143
wire          MRxErr;   // This goes to PHY
144
wire          MColl;    // This goes to PHY
145
wire          MCrs;     // This goes to PHY
146 116 mohor
 
147
wire          Mdi_I;
148
wire          Mdo_O;
149
wire          Mdo_OE;
150 169 mohor
tri           Mdio_IO;
151 116 mohor
wire          Mdc_O;
152
 
153
 
154 169 mohor
parameter Tp = 1;
155 116 mohor
 
156 121 mohor
 
157 116 mohor
// Ethernet Slave Interface signals
158 169 mohor
wire [31:0] eth_sl_wb_adr;
159 116 mohor
wire [31:0] eth_sl_wb_adr_i, eth_sl_wb_dat_o, eth_sl_wb_dat_i;
160
wire  [3:0] eth_sl_wb_sel_i;
161
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;
162
 
163
// Ethernet Master Interface signals
164
wire [31:0] eth_ma_wb_adr_o, eth_ma_wb_dat_i, eth_ma_wb_dat_o;
165
wire  [3:0] eth_ma_wb_sel_o;
166
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;
167
 
168
 
169
 
170
 
171
// Connecting Ethernet top module
172 169 mohor
eth_top eth_top
173 116 mohor
(
174
  // WISHBONE common
175 169 mohor
  .wb_clk_i(wb_clk),              .wb_rst_i(wb_rst),
176 116 mohor
 
177
  // WISHBONE slave
178 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),
179
  .wb_cyc_i(eth_sl_wb_cyc_i),       .wb_stb_i(eth_sl_wb_stb_i),   .wb_ack_o(eth_sl_wb_ack_o),
180
  .wb_err_o(eth_sl_wb_err_o),       .wb_dat_i(eth_sl_wb_dat_i),   .wb_dat_o(eth_sl_wb_dat_o),
181 116 mohor
 
182
  // WISHBONE master
183
  .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),
184
  .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),
185
  .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),
186
 
187
  //TX
188
  .mtx_clk_pad_i(mtx_clk), .mtxd_pad_o(MTxD), .mtxen_pad_o(MTxEn), .mtxerr_pad_o(MTxErr),
189
 
190
  //RX
191
  .mrx_clk_pad_i(mrx_clk), .mrxd_pad_i(MRxD), .mrxdv_pad_i(MRxDV), .mrxerr_pad_i(MRxErr),
192
  .mcoll_pad_i(MColl),    .mcrs_pad_i(MCrs),
193
 
194
  // MIIM
195
  .mdc_pad_o(Mdc_O), .md_pad_i(Mdi_I), .md_pad_o(Mdo_O), .md_padoe_o(Mdo_OE),
196
 
197 169 mohor
  .int_o(wb_int)
198 227 tadejm
 
199
  // Bist
200
`ifdef ETH_BIST
201
  ,
202
  .scanb_rst      (1'b0),
203
  .scanb_clk      (1'b0),
204
  .scanb_si       (1'b0),
205
  .scanb_so       (),
206
  .scanb_en       (1'b0)
207
`endif
208 116 mohor
);
209
 
210
 
211
 
212 169 mohor
// Connecting Ethernet PHY Module
213
assign Mdio_IO = Mdo_OE ? Mdo_O : 1'bz ;
214
assign Mdi_I   = Mdio_IO;
215
integer phy_log_file_desc;
216
 
217
eth_phy eth_phy
218 116 mohor
(
219 169 mohor
  // WISHBONE reset
220
  .m_rst_n_i(!wb_rst),
221 116 mohor
 
222 169 mohor
  // MAC TX
223
  .mtx_clk_o(mtx_clk),    .mtxd_i(MTxD),    .mtxen_i(MTxEn),    .mtxerr_i(MTxErr),
224
 
225
  // MAC RX
226
  .mrx_clk_o(mrx_clk),    .mrxd_o(MRxD),    .mrxdv_o(MRxDV),    .mrxerr_o(MRxErr),
227
  .mcoll_o(MColl),        .mcrs_o(MCrs),
228
 
229
  // MIIM
230
  .mdc_i(Mdc_O),          .md_io(Mdio_IO),
231
 
232
  // SYSTEM
233
  .phy_log(phy_log_file_desc)
234 116 mohor
);
235
 
236
 
237 169 mohor
 
238
// Connecting WB Master as Host Interface
239
integer host_log_file_desc;
240
 
241
WB_MASTER_BEHAVIORAL wb_master
242 116 mohor
(
243 169 mohor
    .CLK_I(wb_clk),
244
    .RST_I(wb_rst),
245
    .TAG_I({`WB_TAG_WIDTH{1'b0}}),
246
    .TAG_O(),
247
    .ACK_I(eth_sl_wb_ack_o),
248
    .ADR_O(eth_sl_wb_adr), // only eth_sl_wb_adr_i[11:2] used
249
    .CYC_O(eth_sl_wb_cyc_i),
250
    .DAT_I(eth_sl_wb_dat_o),
251
    .DAT_O(eth_sl_wb_dat_i),
252
    .ERR_I(eth_sl_wb_err_o),
253
    .RTY_I(1'b0),  // inactive (1'b0)
254
    .SEL_O(eth_sl_wb_sel_i),
255
    .STB_O(eth_sl_wb_stb_i),
256
    .WE_O (eth_sl_wb_we_i),
257
    .CAB_O()       // NOT USED for now!
258
);
259
 
260
assign eth_sl_wb_adr_i = {20'h0, eth_sl_wb_adr[11:2], 2'h0};
261
 
262
 
263
 
264
// Connecting WB Slave as Memory Interface Module
265
integer memory_log_file_desc;
266
 
267
WB_SLAVE_BEHAVIORAL wb_slave
268
(
269
    .CLK_I(wb_clk),
270
    .RST_I(wb_rst),
271
    .ACK_O(eth_ma_wb_ack_i),
272
    .ADR_I(eth_ma_wb_adr_o),
273
    .CYC_I(eth_ma_wb_cyc_o),
274
    .DAT_O(eth_ma_wb_dat_i),
275
    .DAT_I(eth_ma_wb_dat_o),
276
    .ERR_O(eth_ma_wb_err_i),
277
    .RTY_O(),      // NOT USED for now!
278
    .SEL_I(eth_ma_wb_sel_o),
279
    .STB_I(eth_ma_wb_stb_o),
280
    .WE_I (eth_ma_wb_we_o),
281
    .CAB_I(1'b0)   // inactive (1'b0)
282
);
283
 
284
 
285
 
286
// Connecting WISHBONE Bus Monitors to ethernet master and slave interfaces
287
integer wb_s_mon_log_file_desc ;
288
integer wb_m_mon_log_file_desc ;
289
 
290
WB_BUS_MON wb_eth_slave_bus_mon
291
(
292 116 mohor
  // WISHBONE common
293 169 mohor
  .CLK_I(wb_clk),
294
  .RST_I(wb_rst),
295 116 mohor
 
296 169 mohor
  // WISHBONE slave
297
  .ACK_I(eth_sl_wb_ack_o),
298
  .ADDR_O({20'h0, eth_sl_wb_adr_i[11:2], 2'b0}),
299
  .CYC_O(eth_sl_wb_cyc_i),
300
  .DAT_I(eth_sl_wb_dat_o),
301
  .DAT_O(eth_sl_wb_dat_i),
302
  .ERR_I(eth_sl_wb_err_o),
303
  .RTY_I(1'b0),
304
  .SEL_O(eth_sl_wb_sel_i),
305
  .STB_O(eth_sl_wb_stb_i),
306
  .WE_O (eth_sl_wb_we_i),
307
  .TAG_I({`WB_TAG_WIDTH{1'b0}}),
308
  .TAG_O(),
309
  .CAB_O(1'b0),
310
  .log_file_desc (wb_s_mon_log_file_desc)
311
);
312
 
313
WB_BUS_MON wb_eth_master_bus_mon
314
(
315
  // WISHBONE common
316
  .CLK_I(wb_clk),
317
  .RST_I(wb_rst),
318
 
319 116 mohor
  // WISHBONE master
320 169 mohor
  .ACK_I(eth_ma_wb_ack_i),
321
  .ADDR_O(eth_ma_wb_adr_o),
322
  .CYC_O(eth_ma_wb_cyc_o),
323
  .DAT_I(eth_ma_wb_dat_i),
324
  .DAT_O(eth_ma_wb_dat_o),
325
  .ERR_I(eth_ma_wb_err_i),
326
  .RTY_I(1'b0),
327
  .SEL_O(eth_ma_wb_sel_o),
328
  .STB_O(eth_ma_wb_stb_o),
329
  .WE_O (eth_ma_wb_we_o),
330
  .TAG_I({`WB_TAG_WIDTH{1'b0}}),
331
  .TAG_O(),
332
  .CAB_O(1'b0),
333
  .log_file_desc(wb_m_mon_log_file_desc)
334 116 mohor
);
335
 
336
 
337
 
338 169 mohor
reg         StartTB;
339
integer     tb_log_file;
340 116 mohor
 
341 169 mohor
initial
342
begin
343
  tb_log_file = $fopen("../log/eth_tb.log");
344
  if (tb_log_file < 2)
345
  begin
346
    $display("*E Could not open/create testbench log file in ../log/ directory!");
347
    $finish;
348
  end
349
  $fdisplay(tb_log_file, "========================== ETHERNET IP Core Testbench results ===========================");
350
  $fdisplay(tb_log_file, " ");
351 116 mohor
 
352 169 mohor
  phy_log_file_desc = $fopen("../log/eth_tb_phy.log");
353
  if (phy_log_file_desc < 2)
354
  begin
355
    $fdisplay(tb_log_file, "*E Could not open/create eth_tb_phy.log file in ../log/ directory!");
356
    $finish;
357
  end
358
  $fdisplay(phy_log_file_desc, "================ PHY Module  Testbench access log ================");
359
  $fdisplay(phy_log_file_desc, " ");
360
 
361
  memory_log_file_desc = $fopen("../log/eth_tb_memory.log");
362
  if (memory_log_file_desc < 2)
363
  begin
364
    $fdisplay(tb_log_file, "*E Could not open/create eth_tb_memory.log file in ../log/ directory!");
365
    $finish;
366
  end
367
  $fdisplay(memory_log_file_desc, "=============== MEMORY Module Testbench access log ===============");
368
  $fdisplay(memory_log_file_desc, " ");
369
 
370
  host_log_file_desc = $fopen("../log/eth_tb_host.log");
371
  if (host_log_file_desc < 2)
372
  begin
373
    $fdisplay(tb_log_file, "*E Could not open/create eth_tb_host.log file in ../log/ directory!");
374
    $finish;
375
  end
376
  $fdisplay(host_log_file_desc, "================ HOST Module Testbench access log ================");
377
  $fdisplay(host_log_file_desc, " ");
378
 
379
  wb_s_mon_log_file_desc = $fopen("../log/eth_tb_wb_s_mon.log");
380
  if (wb_s_mon_log_file_desc < 2)
381
  begin
382
    $fdisplay(tb_log_file, "*E Could not open/create eth_tb_wb_s_mon.log file in ../log/ directory!");
383
    $finish;
384
  end
385
  $fdisplay(wb_s_mon_log_file_desc, "============== WISHBONE Slave Bus Monitor error log ==============");
386
  $fdisplay(wb_s_mon_log_file_desc, " ");
387
  $fdisplay(wb_s_mon_log_file_desc, "   Only ERRONEOUS conditions are logged !");
388
  $fdisplay(wb_s_mon_log_file_desc, " ");
389
 
390
  wb_m_mon_log_file_desc = $fopen("../log/eth_tb_wb_m_mon.log");
391
  if (wb_m_mon_log_file_desc < 2)
392
  begin
393
    $fdisplay(tb_log_file, "*E Could not open/create eth_tb_wb_m_mon.log file in ../log/ directory!");
394
    $finish;
395
  end
396
  $fdisplay(wb_m_mon_log_file_desc, "============= WISHBONE Master Bus Monitor  error log =============");
397
  $fdisplay(wb_m_mon_log_file_desc, " ");
398
  $fdisplay(wb_m_mon_log_file_desc, "   Only ERRONEOUS conditions are logged !");
399
  $fdisplay(wb_m_mon_log_file_desc, " ");
400
 
401 243 tadejm
  // Reset pulse
402
  wb_rst =  1'b1;
403
  #423 wb_rst =  1'b0;
404
 
405 169 mohor
  // Clear memories
406
  clear_memories;
407 243 tadejm
  clear_buffer_descriptors;
408 169 mohor
 
409
  #423 StartTB  =  1'b1;
410
end
411
 
412
 
413
 
414
// Generating wb_clk clock
415 116 mohor
initial
416
begin
417 169 mohor
  wb_clk=0;
418
//  forever #2.5 wb_clk = ~wb_clk;  // 2*2.5 ns -> 200.0 MHz    
419 209 tadejm
  forever #5 wb_clk = ~wb_clk;  // 2*5 ns -> 100.0 MHz    
420 169 mohor
//  forever #10 wb_clk = ~wb_clk;  // 2*10 ns -> 50.0 MHz    
421
//  forever #12.5 wb_clk = ~wb_clk;  // 2*12.5 ns -> 40 MHz    
422
//  forever #15 wb_clk = ~wb_clk;  // 2*10 ns -> 33.3 MHz    
423 209 tadejm
//  forever #20 wb_clk = ~wb_clk;  // 2*20 ns -> 25 MHz    
424 169 mohor
//  forever #25 wb_clk = ~wb_clk;  // 2*25 ns -> 20.0 MHz
425
//  forever #31.25 wb_clk = ~wb_clk;  // 2*31.25 ns -> 16.0 MHz    
426
//  forever #50 wb_clk = ~wb_clk;  // 2*50 ns -> 10.0 MHz
427
//  forever #55 wb_clk = ~wb_clk;  // 2*55 ns ->  9.1 MHz    
428 116 mohor
end
429
 
430
 
431
 
432 169 mohor
integer      tests_successfull;
433
integer      tests_failed;
434
reg [799:0]  test_name; // used for tb_log_file
435 121 mohor
 
436 169 mohor
reg   [3:0]  wbm_init_waits; // initial wait cycles between CYC_O and STB_O of WB Master
437
reg   [3:0]  wbm_subseq_waits; // subsequent wait cycles between STB_Os of WB Master
438
reg   [2:0]  wbs_waits; // wait cycles befor WB Slave responds
439
reg   [7:0]  wbs_retries; // if RTY response, then this is the number of retries before ACK
440
 
441 116 mohor
initial
442
begin
443 169 mohor
  wait(StartTB);  // Start of testbench
444
 
445
  // Initial global values
446
  tests_successfull = 0;
447
  tests_failed = 0;
448
 
449
  wbm_init_waits = 4'h1;
450
  wbm_subseq_waits = 4'h3;
451
  wbs_waits = 4'h1;
452
  wbs_retries = 8'h2;
453
  wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
454
 
455
 
456
  //  Call tests
457
  //  ----------
458 194 tadej
//    test_access_to_mac_reg(0, 3);           // 0 - 3
459
//    test_mii(0, 17);                        // 0 - 17
460 169 mohor
  test_note("PHY generates ideal Carrier sense and Collision signals for following tests");
461
  eth_phy.carrier_sense_real_delay(0);
462 243 tadejm
//    test_mac_full_duplex_transmit(8, 9);    // 0 - (21)
463 254 mohor
//    test_mac_full_duplex_receive(8, 9);
464 267 mohor
    test_mac_full_duplex_flow_control(0, 2);  // 0 - 2
465 169 mohor
 
466 243 tadejm
  test_note("PHY generates 'real delayed' Carrier sense and Collision signals for following tests");
467 169 mohor
  eth_phy.carrier_sense_real_delay(1);
468
 
469
 
470
  // Finish test's logs
471
  test_summary;
472
  $display("\n\n END of SIMULATION");
473
  $fclose(tb_log_file | phy_log_file_desc | memory_log_file_desc | host_log_file_desc);
474
  $fclose(wb_s_mon_log_file_desc | wb_m_mon_log_file_desc);
475
 
476
  $stop;
477 116 mohor
end
478 169 mohor
 
479 116 mohor
 
480 169 mohor
 
481
//////////////////////////////////////////////////////////////
482
// Test tasks
483
//////////////////////////////////////////////////////////////
484
 
485
task test_access_to_mac_reg;
486
  input  [31:0]  start_task;
487
  input  [31:0]  end_task;
488
  integer        bit_start_1;
489
  integer        bit_end_1;
490
  integer        bit_start_2;
491
  integer        bit_end_2;
492
  integer        num_of_reg;
493
  integer        i_addr;
494
  integer        i_data;
495
  integer        i_length;
496
  integer        tmp_data;
497
  reg    [31:0]  tx_bd_num;
498
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_data;
499
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_tmp_data;
500
  integer        i;
501
  integer        i1;
502
  integer        i2;
503
  integer        i3;
504
  integer        fail;
505 178 mohor
  integer        test_num;
506 169 mohor
  reg    [31:0]  addr;
507
  reg    [31:0]  data;
508
  reg    [31:0]  data_max;
509 116 mohor
begin
510 169 mohor
// ACCESS TO MAC REGISTERS TEST
511
test_heading("ACCESS TO MAC REGISTERS TEST");
512
$display(" ");
513
$display("ACCESS TO MAC REGISTERS TEST");
514
fail = 0;
515
 
516 192 tadej
// reset MAC registers
517
hard_reset;
518
// reset MAC and MII LOGIC with soft reset
519
reset_mac;
520
reset_mii;
521 169 mohor
 
522 192 tadej
 
523 178 mohor
//////////////////////////////////////////////////////////////////////
524
////                                                              ////
525
////  test_access_to_mac_reg:                                     ////
526
////                                                              ////
527
////  0: Walking 1 with single cycles across MAC regs.            ////
528
////  1: Walking 1 with single cycles across MAC buffer descript. ////
529
////  2: Test max reg. values and reg. values after writing       ////
530
////     inverse reset values and hard reset of the MAC           ////
531
////  3: Test buffer desc. RAM preserving values after hard reset ////
532
////     of the MAC and resetting the logic                       ////
533
////                                                              ////
534
//////////////////////////////////////////////////////////////////////
535 194 tadej
for (test_num = start_task; test_num <= end_task; test_num = test_num + 1)
536 169 mohor
begin
537
 
538 178 mohor
  ////////////////////////////////////////////////////////////////////
539
  ////                                                            ////
540
  ////  Walking 1 with single cycles across MAC regs.             ////
541
  ////                                                            ////
542
  ////////////////////////////////////////////////////////////////////
543
  if (test_num == 0) // Walking 1 with single cycles across MAC regs.
544 194 tadej
  begin
545
    // TEST 0: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )
546
    test_name   = "TEST 0: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )";
547
    `TIME; $display("  TEST 0: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )");
548 178 mohor
 
549 194 tadej
    data = 0;
550
    for (i = 0; i <= 4; i = i + 1) // for initial wait cycles on WB bus
551
      begin
552
        wbm_init_waits = i;
553
        wbm_subseq_waits = {$random} % 5; // it is not important for single accesses
554
        for (i_addr = 0; i_addr <= 32'h4C; i_addr = i_addr + 4) // register address
555
          begin
556
            addr = `ETH_BASE + i_addr;
557
            // set ranges of R/W bits
558
            case (addr)
559
              `ETH_MODER:
560
                begin
561
                  bit_start_1 = 0;
562
                  bit_end_1   = 16;
563
                  bit_start_2 = 32; // not used
564
                  bit_end_2   = 32; // not used
565
                end
566
              `ETH_INT: // READONLY - tested within INT test
567
                begin
568
                  bit_start_1 = 32; // not used
569
                  bit_end_1   = 32; // not used
570
                  bit_start_2 = 32; // not used
571
                  bit_end_2   = 32; // not used
572
                end
573
              `ETH_INT_MASK:
574
                begin
575
                  bit_start_1 = 0;
576
                  bit_end_1   = 6;
577
                  bit_start_2 = 32; // not used
578
                  bit_end_2   = 32; // not used
579
                end
580
              `ETH_IPGT:
581
                begin
582
                  bit_start_1 = 0;
583
                  bit_end_1   = 6;
584
                  bit_start_2 = 32; // not used
585
                  bit_end_2   = 32; // not used
586
                end
587
              `ETH_IPGR1:
588
                begin
589
                  bit_start_1 = 0;
590
                  bit_end_1   = 6;
591
                  bit_start_2 = 32; // not used
592
                  bit_end_2   = 32; // not used
593
                end
594
              `ETH_IPGR2:
595
                begin
596
                  bit_start_1 = 0;
597
                  bit_end_1   = 6;
598
                  bit_start_2 = 32; // not used
599
                  bit_end_2   = 32; // not used
600
                end
601
              `ETH_PACKETLEN:
602
                begin
603
                  bit_start_1 = 0;
604
                  bit_end_1   = 31;
605
                  bit_start_2 = 32; // not used
606
                  bit_end_2   = 32; // not used
607
                end
608
              `ETH_COLLCONF:
609
                begin
610
                  bit_start_1 = 0;
611
                  bit_end_1   = 5;
612
                  bit_start_2 = 16;
613
                  bit_end_2   = 19;
614
                end
615
              `ETH_TX_BD_NUM:
616
                begin
617
                  bit_start_1 = 0;
618
                  bit_end_1   = 7;
619
                  bit_start_2 = 32; // not used
620
                  bit_end_2   = 32; // not used
621
                end
622
              `ETH_CTRLMODER:
623
                begin
624
                  bit_start_1 = 0;
625
                  bit_end_1   = 2;
626
                  bit_start_2 = 32; // not used
627
                  bit_end_2   = 32; // not used
628
                end
629
              `ETH_MIIMODER:
630
                begin
631
                  bit_start_1 = 0;
632
                  bit_end_1   = 9;
633
                  bit_start_2 = 32; // not used
634
                  bit_end_2   = 32; // not used
635
                end
636
              `ETH_MIICOMMAND: // "WRITEONLY" - tested within MIIM test - 3 LSBits are not written here!!!
637
                begin
638
                  bit_start_1 = 32; // not used
639
                  bit_end_1   = 32; // not used
640
                  bit_start_2 = 32; // not used
641
                  bit_end_2   = 32; // not used
642
                end
643
              `ETH_MIIADDRESS:
644
                begin
645
                  bit_start_1 = 0;
646
                  bit_end_1   = 4;
647
                  bit_start_2 = 8;
648
                  bit_end_2   = 12;
649
                end
650
              `ETH_MIITX_DATA:
651
                begin
652
                  bit_start_1 = 0;
653
                  bit_end_1   = 15;
654
                  bit_start_2 = 32; // not used
655
                  bit_end_2   = 32; // not used
656
                end
657
              `ETH_MIIRX_DATA: // READONLY - tested within MIIM test
658
                begin
659
                  bit_start_1 = 32; // not used
660
                  bit_end_1   = 32; // not used
661
                  bit_start_2 = 32; // not used
662
                  bit_end_2   = 32; // not used
663
                end
664
              `ETH_MIISTATUS: // READONLY - tested within MIIM test
665
                begin
666
                  bit_start_1 = 32; // not used
667
                  bit_end_1   = 32; // not used
668
                  bit_start_2 = 32; // not used
669
                  bit_end_2   = 32; // not used
670
                end
671
              `ETH_MAC_ADDR0:
672
                begin
673
                  bit_start_1 = 0;
674
                  bit_end_1   = 31;
675
                  bit_start_2 = 32; // not used
676
                  bit_end_2   = 32; // not used
677 178 mohor
                  end
678 194 tadej
              `ETH_MAC_ADDR1:
679
                begin
680
                  bit_start_1 = 0;
681
                  bit_end_1   = 15;
682
                  bit_start_2 = 32; // not used
683
                  bit_end_2   = 32; // not used
684
                end
685
              `ETH_HASH_ADDR0:
686
                begin
687
                  bit_start_1 = 0;
688
                  bit_end_1   = 31;
689
                  bit_start_2 = 32; // not used
690
                  bit_end_2   = 32; // not used
691
                end
692
              default: // `ETH_HASH_ADDR1:
693
                begin
694
                  bit_start_1 = 0;
695
                  bit_end_1   = 31;
696
                  bit_start_2 = 32; // not used
697
                  bit_end_2   = 32; // not used
698
                end
699
            endcase
700
 
701
            for (i_data = 0; i_data <= 31; i_data = i_data + 1) // the position of walking one
702
              begin
703
                data = 1'b1 << i_data;
704
                if ( (addr == `ETH_MIICOMMAND) && (i_data <= 2) ) // DO NOT WRITE to 3 LSBits of MIICOMMAND !!!
705
                  ;
706
                else
707 178 mohor
                  begin
708 194 tadej
                    wbm_write(addr, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
709
                    wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
710
                    if ( ((i_data >= bit_start_1) && (i_data <= bit_end_1)) ||
711
                         ((i_data >= bit_start_2) && (i_data <= bit_end_2)) ) // data should be equal to tmp_data
712
                      begin
713
                        if (tmp_data !== data)
714 178 mohor
                        begin
715 194 tadej
                          fail = fail + 1;
716
                          test_fail("RW bit of the MAC register was not written or not read");
717
                          `TIME;
718
                          $display("wbm_init_waits %d, addr %h, data %h, tmp_data %h",
719
                                    wbm_init_waits, addr, data, tmp_data);
720
                        end
721
                      end
722
                    else // data should not be equal to tmp_data
723
                      begin
724
                        if (tmp_data === data)
725 178 mohor
                          begin
726
                            fail = fail + 1;
727 194 tadej
                            test_fail("NON RW bit of the MAC register was written, but it shouldn't be");
728 178 mohor
                            `TIME;
729 194 tadej
                            $display("wbm_init_waits %d, addr %h, data %h, tmp_data %h",
730 178 mohor
                                      wbm_init_waits, addr, data, tmp_data);
731
                          end
732 194 tadej
                      end
733
                  end
734
              end
735
          end
736
      end
737 209 tadejm
    // INTERMEDIATE DISPLAYS (The only one)
738
    $display("    ->buffer descriptors tested with 0, 1, 2, 3 and 4 bus delay cycles");
739 194 tadej
    if(fail == 0)
740
      test_ok;
741
    else
742
      fail = 0;    // Errors were reported previously
743
  end
744 178 mohor
 
745
 
746
  ////////////////////////////////////////////////////////////////////
747
  ////                                                            ////
748
  ////  Walking 1 with single cycles across MAC buffer descript.  ////
749
  ////                                                            ////
750
  ////////////////////////////////////////////////////////////////////
751
  if (test_num == 1) // Start Walking 1 with single cycles across MAC buffer descript.
752 169 mohor
  begin
753 194 tadej
    // TEST 1: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC BUFFER DESC. ( VARIOUS BUS DELAYS )
754
    test_name   = "TEST 1: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC BUFFER DESC. ( VARIOUS BUS DELAYS )";
755
    `TIME; $display("  TEST 1: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC BUFFER DESC. ( VARIOUS BUS DELAYS )");
756 178 mohor
 
757
    data = 0;
758
    // set TX and RX buffer descriptors
759
    tx_bd_num = 32'h40;
760
    wbm_write(`ETH_TX_BD_NUM, tx_bd_num, 4'hF, 1, 0, 0);
761
    for (i = 0; i <= 4; i = i + 1) // for initial wait cycles on WB bus
762
    begin
763 169 mohor
      wbm_init_waits = i;
764
      wbm_subseq_waits = {$random} % 5; // it is not important for single accesses
765 178 mohor
      for (i_addr = 32'h400; i_addr <= 32'h7FC; i_addr = i_addr + 4) // buffer descriptor address
766 169 mohor
      begin
767
        addr = `ETH_BASE + i_addr;
768 178 mohor
        if (i_addr < (32'h400 + (tx_bd_num << 3))) // TX buffer descriptors
769
        begin
770
          // set ranges of R/W bits
771
          case (addr[3])
772
            1'b0: // buffer control bits
773
            begin
774
              bit_start_1 = 0;
775
              bit_end_1   = 31; // 8;
776
              bit_start_2 = 11;
777
              bit_end_2   = 31;
778
            end
779
            default: // 1'b1: // buffer pointer
780
            begin
781
              bit_start_1 = 0;
782
              bit_end_1   = 31;
783
              bit_start_2 = 32; // not used
784
              bit_end_2   = 32; // not used
785
            end
786
          endcase
787
        end
788
        else // RX buffer descriptors
789
        begin
790
          // set ranges of R/W bits
791
          case (addr[3])
792
            1'b0: // buffer control bits
793
            begin
794
              bit_start_1 = 0;
795
              bit_end_1   = 31; // 7;
796
              bit_start_2 = 13;
797
              bit_end_2   = 31;
798
            end
799
            default: // 1'b1: // buffer pointer
800
            begin
801
              bit_start_1 = 0;
802
              bit_end_1   = 31;
803
              bit_start_2 = 32; // not used
804
              bit_end_2   = 32; // not used
805
            end
806
          endcase
807
        end
808
 
809 169 mohor
        for (i_data = 0; i_data <= 31; i_data = i_data + 1) // the position of walking one
810
        begin
811
          data = 1'b1 << i_data;
812 178 mohor
          if ( (addr[3] == 0) && (i_data == 15) ) // DO NOT WRITE to this bit !!!
813
            ;
814 169 mohor
          else
815
          begin
816
            wbm_write(addr, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
817
            wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
818
            if ( ((i_data >= bit_start_1) && (i_data <= bit_end_1)) ||
819
                 ((i_data >= bit_start_2) && (i_data <= bit_end_2)) ) // data should be equal to tmp_data
820
            begin
821
              if (tmp_data !== data)
822
              begin
823
                fail = fail + 1;
824 178 mohor
                test_fail("RW bit of the MAC buffer descriptors was not written or not read");
825 169 mohor
                `TIME;
826
                $display("wbm_init_waits %d, addr %h, data %h, tmp_data %h",
827
                          wbm_init_waits, addr, data, tmp_data);
828
              end
829
            end
830
            else // data should not be equal to tmp_data
831
            begin
832
              if (tmp_data === data)
833
              begin
834
                fail = fail + 1;
835 178 mohor
                test_fail("NON RW bit of the MAC buffer descriptors was written, but it shouldn't be");
836 169 mohor
                `TIME;
837
                $display("wbm_init_waits %d, addr %h, data %h, tmp_data %h",
838
                          wbm_init_waits, addr, data, tmp_data);
839
              end
840
            end
841
          end
842
        end
843
      end
844 178 mohor
      // INTERMEDIATE DISPLAYS
845
      case (i)
846 209 tadejm
        0:       $display("    ->buffer descriptors tested with 0 bus delay");
847
        1:       $display("    ->buffer descriptors tested with 1 bus delay cycle");
848
        2:       $display("    ->buffer descriptors tested with 2 bus delay cycles");
849
        3:       $display("    ->buffer descriptors tested with 3 bus delay cycles");
850
        default: $display("    ->buffer descriptors tested with 4 bus delay cycles");
851 178 mohor
      endcase
852
    end
853
    if(fail == 0)
854
      test_ok;
855
    else
856
      fail = 0;
857 169 mohor
  end
858 178 mohor
 
859
 
860
  ////////////////////////////////////////////////////////////////////
861
  ////                                                            ////
862
  ////  Test max reg. values and reg. values after writing        ////
863
  ////  inverse reset values and hard reset of the MAC            ////
864
  ////                                                            ////
865
  ////////////////////////////////////////////////////////////////////
866
  if (test_num == 2) // Start this task
867 169 mohor
  begin
868 194 tadej
    // TEST 2: MAX REG. VALUES AND REG. VALUES AFTER WRITING INVERSE RESET VALUES AND HARD RESET OF THE MAC
869 178 mohor
    test_name   =
870 194 tadej
      "TEST 2: MAX REG. VALUES AND REG. VALUES AFTER WRITING INVERSE RESET VALUES AND HARD RESET OF THE MAC";
871 178 mohor
    `TIME; $display(
872 194 tadej
      "  TEST 2: MAX REG. VALUES AND REG. VALUES AFTER WRITING INVERSE RESET VALUES AND HARD RESET OF THE MAC");
873 178 mohor
 
874
    // reset MAC registers
875
    hard_reset;
876
    for (i = 0; i <= 4; i = i + 1) // 0, 2 - WRITE; 1, 3, 4 - READ
877 169 mohor
    begin
878 178 mohor
      for (i_addr = 0; i_addr <= 32'h4C; i_addr = i_addr + 4) // register address
879 169 mohor
      begin
880 178 mohor
        addr = `ETH_BASE + i_addr;
881
        // set ranges of R/W bits
882
        case (addr)
883
          `ETH_MODER:
884 169 mohor
          begin
885 178 mohor
            data = 32'h0000_A800;
886
            data_max = 32'h0001_FFFF;
887 169 mohor
          end
888 178 mohor
          `ETH_INT: // READONLY - tested within INT test
889 169 mohor
          begin
890 178 mohor
            data = 32'h0000_0000;
891
            data_max = 32'h0000_0000;
892 169 mohor
          end
893
          `ETH_INT_MASK:
894 178 mohor
          begin
895
            data = 32'h0000_0000;
896
            data_max = 32'h0000_007F;
897
          end
898 169 mohor
          `ETH_IPGT:
899 178 mohor
          begin
900
            data = 32'h0000_0012;
901
            data_max = 32'h0000_007F;
902
          end
903 169 mohor
          `ETH_IPGR1:
904 178 mohor
          begin
905
            data = 32'h0000_000C;
906
            data_max = 32'h0000_007F;
907
          end
908 169 mohor
          `ETH_IPGR2:
909 178 mohor
          begin
910
            data = 32'h0000_0012;
911
            data_max = 32'h0000_007F;
912
          end
913 169 mohor
          `ETH_PACKETLEN:
914 178 mohor
          begin
915
            data = 32'h0040_0600;
916
            data_max = 32'hFFFF_FFFF;
917
          end
918 169 mohor
          `ETH_COLLCONF:
919 178 mohor
          begin
920
            data = 32'h000F_003F;
921
            data_max = 32'h000F_003F;
922
          end
923 169 mohor
          `ETH_TX_BD_NUM:
924 178 mohor
          begin
925
            data = 32'h0000_0040;
926
            data_max = 32'h0000_0080;
927
          end
928 169 mohor
          `ETH_CTRLMODER:
929 178 mohor
          begin
930
            data = 32'h0000_0000;
931
            data_max = 32'h0000_0007;
932
          end
933 169 mohor
          `ETH_MIIMODER:
934 178 mohor
          begin
935
            data = 32'h0000_0064;
936
            data_max = 32'h0000_03FF;
937
          end
938 169 mohor
          `ETH_MIICOMMAND: // "WRITEONLY" - tested within MIIM test - 3 LSBits are not written here!!!
939 178 mohor
          begin
940
            data = 32'h0000_0000;
941
            data_max = 32'h0000_0007;
942
          end
943 169 mohor
          `ETH_MIIADDRESS:
944 178 mohor
          begin
945
            data = 32'h0000_0000;
946
            data_max = 32'h0000_1F1F;
947
          end
948 169 mohor
          `ETH_MIITX_DATA:
949 178 mohor
          begin
950
            data = 32'h0000_0000;
951
            data_max = 32'h0000_FFFF;
952
          end
953 169 mohor
          `ETH_MIIRX_DATA: // READONLY - tested within MIIM test
954
          begin
955 178 mohor
            data = 32'h0000_0000;
956
            data_max = 32'h0000_0000;
957 169 mohor
          end
958 178 mohor
          `ETH_MIISTATUS: // READONLY - tested within MIIM test
959 169 mohor
          begin
960 178 mohor
            data = 32'h0000_0000;
961
            data_max = 32'h0000_0000;
962 169 mohor
          end
963 178 mohor
          `ETH_MAC_ADDR0:
964 169 mohor
          begin
965 178 mohor
            data = 32'h0000_0000;
966
            data_max = 32'hFFFF_FFFF;
967 169 mohor
          end
968 178 mohor
          `ETH_MAC_ADDR1:
969 169 mohor
          begin
970 178 mohor
            data = 32'h0000_0000;
971
            data_max = 32'h0000_FFFF;
972 169 mohor
          end
973 178 mohor
          `ETH_HASH_ADDR0:
974 169 mohor
          begin
975 178 mohor
            data = 32'h0000_0000;
976
            data_max = 32'hFFFF_FFFF;
977 169 mohor
          end
978 178 mohor
          default: // `ETH_HASH_ADDR1:
979 169 mohor
          begin
980 178 mohor
            data = 32'h0000_0000;
981
            data_max = 32'hFFFF_FFFF;
982 169 mohor
          end
983
        endcase
984 178 mohor
 
985
        wbm_init_waits = {$random} % 3;
986
        wbm_subseq_waits = {$random} % 5; // it is not important for single accesses
987
        if (i == 0)
988
          wbm_write(addr, ~data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
989
        else if (i == 2)
990
          wbm_write(addr, 32'hFFFFFFFF, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
991
        else if ((i == 1) || (i == 4))
992 169 mohor
        begin
993 178 mohor
          wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
994
          if (tmp_data !== data)
995
          begin
996
            fail = fail + 1;
997
            test_fail("RESET value of the MAC register is not correct");
998
            `TIME;
999
            $display("  addr %h, data %h, tmp_data %h", addr, data, tmp_data);
1000
          end
1001 169 mohor
        end
1002 178 mohor
        else // check maximum values
1003 169 mohor
        begin
1004
          wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1005 178 mohor
          if (addr == `ETH_TX_BD_NUM) // previous data should remain in this register
1006 169 mohor
          begin
1007
            if (tmp_data !== data)
1008
            begin
1009
              fail = fail + 1;
1010 178 mohor
              test_fail("Previous value of the TX_BD_NUM register did not remain");
1011 169 mohor
              `TIME;
1012 178 mohor
              $display("  addr %h, data_max %h, tmp_data %h", addr, data_max, tmp_data);
1013 169 mohor
            end
1014 178 mohor
            // try maximum (80)
1015
            wbm_write(addr, data_max, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1016
            wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1017
            if (tmp_data !== data_max)
1018
            begin
1019
              fail = fail + 1;
1020
              test_fail("MAX value of the TX_BD_NUM register is not correct");
1021
              `TIME;
1022
              $display("  addr %h, data_max %h, tmp_data %h", addr, data_max, tmp_data);
1023
            end
1024
            // try one less than maximum (80)
1025
            wbm_write(addr, (data_max - 1), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1026
            wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1027
            if (tmp_data !== (data_max - 1))
1028
            begin
1029
              fail = fail + 1;
1030
              test_fail("ONE less than MAX value of the TX_BD_NUM register is not correct");
1031
              `TIME;
1032
              $display("  addr %h, data_max %h, tmp_data %h", addr, data_max, tmp_data);
1033
            end
1034
            // try one more than maximum (80)
1035
            wbm_write(addr, (data_max + 1), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1036
            wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1037
            if (tmp_data !== (data_max - 1)) // previous data should remain in this register
1038
            begin
1039
              fail = fail + 1;
1040
              test_fail("Previous value of the TX_BD_NUM register did not remain");
1041
              `TIME;
1042
              $display("  addr %h, data_max %h, tmp_data %h", addr, data_max, tmp_data);
1043
            end
1044 169 mohor
          end
1045 178 mohor
          else
1046 169 mohor
          begin
1047 178 mohor
            if (tmp_data !== data_max)
1048 169 mohor
            begin
1049
              fail = fail + 1;
1050 178 mohor
              test_fail("MAX value of the MAC register is not correct");
1051 169 mohor
              `TIME;
1052 178 mohor
              $display("  addr %h, data_max %h, tmp_data %h", addr, data_max, tmp_data);
1053 169 mohor
            end
1054
          end
1055
        end
1056
      end
1057 178 mohor
      // reset MAC registers
1058
      if ((i == 0) || (i == 3))
1059
        hard_reset;
1060 169 mohor
    end
1061 178 mohor
    if(fail == 0)
1062
      test_ok;
1063
    else
1064
      fail = 0;
1065 169 mohor
  end
1066 116 mohor
 
1067 156 mohor
 
1068 181 mohor
  ////////////////////////////////////////////////////////////////////
1069
  ////                                                            ////
1070
  ////  Test buffer desc. ram preserving values after hard reset  ////
1071
  ////  of the mac and reseting the logic                         ////
1072
  ////                                                            ////
1073
  ////////////////////////////////////////////////////////////////////
1074 178 mohor
  if (test_num == 3) // Start this task
1075 169 mohor
  begin
1076 194 tadej
    // TEST 3: BUFFER DESC. RAM PRESERVING VALUES AFTER HARD RESET OF THE MAC AND RESETING THE LOGIC
1077
    test_name   = "TEST 3: BUFFER DESC. RAM PRESERVING VALUES AFTER HARD RESET OF THE MAC AND RESETING THE LOGIC";
1078 178 mohor
    `TIME;
1079 194 tadej
    $display("  TEST 3: BUFFER DESC. RAM PRESERVING VALUES AFTER HARD RESET OF THE MAC AND RESETING THE LOGIC");
1080 178 mohor
 
1081
    // reset MAC registers
1082
    hard_reset;
1083
    // reset LOGIC with soft reset
1084
    reset_mac;
1085
    reset_mii;
1086
    for (i = 0; i <= 3; i = i + 1) // 0, 2 - WRITE; 1, 3 - READ
1087 169 mohor
    begin
1088 178 mohor
      for (i_addr = 32'h400; i_addr <= 32'h7FC; i_addr = i_addr + 4) // buffer descriptor address
1089 169 mohor
      begin
1090 178 mohor
        addr = `ETH_BASE + i_addr;
1091
 
1092
        wbm_init_waits = {$random} % 3;
1093
        wbm_subseq_waits = {$random} % 5; // it is not important for single accesses
1094
        if (i == 0)
1095 169 mohor
        begin
1096 178 mohor
          data = 32'hFFFFFFFF;
1097
          wbm_write(addr, 32'hFFFFFFFF, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1098 169 mohor
        end
1099 178 mohor
        else if (i == 2)
1100 169 mohor
        begin
1101 178 mohor
          data = 32'h00000000;
1102
          wbm_write(addr, 32'h00000000, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1103 169 mohor
        end
1104
        else
1105
        begin
1106 178 mohor
          wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1107
          if (tmp_data !== data)
1108 169 mohor
          begin
1109
            fail = fail + 1;
1110 178 mohor
            test_fail("PRESERVED value of the MAC buffer descriptors is not correct");
1111 169 mohor
            `TIME;
1112 178 mohor
            $display("  addr %h, data %h, tmp_data %h", addr, data, tmp_data);
1113 169 mohor
          end
1114
        end
1115
      end
1116 178 mohor
      if ((i == 0) || (i == 2))
1117
      begin
1118
        // reset MAC registers
1119
        hard_reset;
1120
        // reset LOGIC with soft reset
1121
        reset_mac;
1122
        reset_mii;
1123
      end
1124 169 mohor
    end
1125 178 mohor
    if(fail == 0)
1126
      test_ok;
1127
    else
1128
    fail = 0;
1129 169 mohor
  end
1130 116 mohor
 
1131
 
1132 178 mohor
  if (test_num == 4) // Start this task
1133 169 mohor
  begin
1134 194 tadej
        /*  // TEST 4: 'WALKING ONE' WITH BURST CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )
1135
          test_name   = "TEST 4: 'WALKING ONE' WITH BURST CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )";
1136
          `TIME; $display("  TEST 4: 'WALKING ONE' WITH BURST CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )");
1137 178 mohor
 
1138
          data = 0;
1139
          burst_data = 0;
1140
          burst_tmp_data = 0;
1141
          i_length = 10; // two bursts for length 20
1142
          for (i = 0; i <= 4; i = i + 1) // for initial wait cycles on WB bus
1143
          begin
1144
            for (i1 = 0; i1 <= 4; i1 = i1 + 1) // for initial wait cycles on WB bus
1145
            begin
1146
              wbm_init_waits = i;
1147
              wbm_subseq_waits = i1;
1148
              #1;
1149
              for (i_data = 0; i_data <= 31; i_data = i_data + 1) // the position of walking one
1150
              begin
1151
                data = 1'b1 << i_data;
1152
                #1;
1153
                for (i2 = 32'h4C; i2 >= 0; i2 = i2 - 4)
1154
                begin
1155
                  burst_data = burst_data << 32;
1156
                  // DO NOT WRITE to 3 LSBits of MIICOMMAND !!!
1157
                  if ( ((`ETH_BASE + i2) == `ETH_MIICOMMAND) && (i_data <= 2) )
1158
                  begin
1159
                    #1 burst_data[31:0] = 0;
1160
                  end
1161
                  else
1162
                  begin
1163
                    #1 burst_data[31:0] = data;
1164
                  end
1165
                end
1166
                #1;
1167
                // 2 burst writes
1168
                addr = `ETH_BASE; // address of a first burst
1169
                wbm_write(addr, burst_data[(32 * 10 - 1):0], 4'hF, i_length, wbm_init_waits, wbm_subseq_waits);
1170
                burst_tmp_data = burst_data >> (32 * i_length);
1171
                addr = addr + 32'h28; // address of a second burst
1172
                wbm_write(addr, burst_tmp_data[(32 * 10 - 1):0], 4'hF, i_length, wbm_init_waits, wbm_subseq_waits);
1173
                #1;
1174
                // 2 burst reads
1175
                addr = `ETH_BASE; // address of a first burst
1176
                wbm_read(addr, burst_tmp_data[(32 * 10 - 1):0], 4'hF, i_length,
1177
                         wbm_init_waits, wbm_subseq_waits); // first burst
1178
                burst_tmp_data = burst_tmp_data << (32 * i_length);
1179
                addr = addr + 32'h28; // address of a second burst
1180
                wbm_read(addr, burst_tmp_data[(32 * 10 - 1):0], 4'hF, i_length,
1181
                         wbm_init_waits, wbm_subseq_waits); // second burst
1182
                #1;
1183
                for (i2 = 0; i2 <= 32'h4C; i2 = i2 + 4)
1184
                begin
1185
                  // set ranges of R/W bits
1186
                  case (`ETH_BASE + i2)
1187
                  `ETH_MODER:
1188
                    begin
1189
                      bit_start_1 = 0;
1190
                      bit_end_1   = 16;
1191
                      bit_start_2 = 32; // not used
1192
                      bit_end_2   = 32; // not used
1193
                    end
1194
                  `ETH_INT: // READONLY - tested within INT test
1195
                    begin
1196
                      bit_start_1 = 32; // not used
1197
                      bit_end_1   = 32; // not used
1198
                      bit_start_2 = 32; // not used
1199
                      bit_end_2   = 32; // not used
1200
                    end
1201
                  `ETH_INT_MASK:
1202
                    begin
1203
                      bit_start_1 = 0;
1204
                      bit_end_1   = 6;
1205
                      bit_start_2 = 32; // not used
1206
                      bit_end_2   = 32; // not used
1207
                    end
1208
                  `ETH_IPGT:
1209
                    begin
1210
                      bit_start_1 = 0;
1211
                      bit_end_1   = 6;
1212
                      bit_start_2 = 32; // not used
1213
                      bit_end_2   = 32; // not used
1214
                    end
1215
                  `ETH_IPGR1:
1216
                    begin
1217
                      bit_start_1 = 0;
1218
                      bit_end_1   = 6;
1219
                      bit_start_2 = 32; // not used
1220
                      bit_end_2   = 32; // not used
1221
                    end
1222
                  `ETH_IPGR2:
1223
                    begin
1224
                      bit_start_1 = 0;
1225
                      bit_end_1   = 6;
1226
                      bit_start_2 = 32; // not used
1227
                      bit_end_2   = 32; // not used
1228
                    end
1229
                  `ETH_PACKETLEN:
1230
                    begin
1231
                      bit_start_1 = 0;
1232
                      bit_end_1   = 31;
1233
                      bit_start_2 = 32; // not used
1234
                      bit_end_2   = 32; // not used
1235
                    end
1236
                  `ETH_COLLCONF:
1237
                    begin
1238
                      bit_start_1 = 0;
1239
                      bit_end_1   = 5;
1240
                      bit_start_2 = 16;
1241
                      bit_end_2   = 19;
1242
                    end
1243
                  `ETH_TX_BD_NUM:
1244
                    begin
1245
                      bit_start_1 = 0;
1246
                      bit_end_1   = 7;
1247
                      bit_start_2 = 32; // not used
1248
                      bit_end_2   = 32; // not used
1249
                    end
1250
                  `ETH_CTRLMODER:
1251
                    begin
1252
                      bit_start_1 = 0;
1253
                      bit_end_1   = 2;
1254
                      bit_start_2 = 32; // not used
1255
                      bit_end_2   = 32; // not used
1256
                    end
1257
                  `ETH_MIIMODER:
1258
                    begin
1259
                      bit_start_1 = 0;
1260
                      bit_end_1   = 9;
1261
                      bit_start_2 = 32; // not used
1262
                      bit_end_2   = 32; // not used
1263
                    end
1264
                  `ETH_MIICOMMAND: // "WRITEONLY" - tested within MIIM test - 3 LSBits are not written here!!!
1265
                    begin
1266
                      bit_start_1 = 32; // not used
1267
                      bit_end_1   = 32; // not used
1268
                      bit_start_2 = 32; // not used
1269
                      bit_end_2   = 32; // not used
1270
                    end
1271
                  `ETH_MIIADDRESS:
1272
                    begin
1273
                      bit_start_1 = 0;
1274
                      bit_end_1   = 4;
1275
                      bit_start_2 = 8;
1276
                      bit_end_2   = 12;
1277
                    end
1278
                  `ETH_MIITX_DATA:
1279
                    begin
1280
                      bit_start_1 = 0;
1281
                      bit_end_1   = 15;
1282
                      bit_start_2 = 32; // not used
1283
                      bit_end_2   = 32; // not used
1284
                    end
1285
                  `ETH_MIIRX_DATA: // READONLY - tested within MIIM test
1286
                    begin
1287
                      bit_start_1 = 32; // not used
1288
                      bit_end_1   = 32; // not used
1289
                      bit_start_2 = 32; // not used
1290
                      bit_end_2   = 32; // not used
1291
                    end
1292
                  `ETH_MIISTATUS: // READONLY - tested within MIIM test
1293
                    begin
1294
                      bit_start_1 = 32; // not used
1295
                      bit_end_1   = 32; // not used
1296
                      bit_start_2 = 32; // not used
1297
                      bit_end_2   = 32; // not used
1298
                    end
1299
                  `ETH_MAC_ADDR0:
1300
                    begin
1301
                      bit_start_1 = 0;
1302
                      bit_end_1   = 31;
1303
                      bit_start_2 = 32; // not used
1304
                      bit_end_2   = 32; // not used
1305
                    end
1306
                  `ETH_MAC_ADDR1:
1307
                    begin
1308
                      bit_start_1 = 0;
1309
                      bit_end_1   = 15;
1310
                      bit_start_2 = 32; // not used
1311
                      bit_end_2   = 32; // not used
1312
                    end
1313
                  `ETH_HASH_ADDR0:
1314
                    begin
1315
                      bit_start_1 = 0;
1316
                      bit_end_1   = 31;
1317
                      bit_start_2 = 32; // not used
1318
                      bit_end_2   = 32; // not used
1319
                    end
1320
                  default: // `ETH_HASH_ADDR1:
1321
                    begin
1322
                      bit_start_1 = 0;
1323
                      bit_end_1   = 31;
1324
                      bit_start_2 = 32; // not used
1325
                      bit_end_2   = 32; // not used
1326
                    end
1327
                  endcase
1328
                  #1;
1329
                  // 3 LSBits of MIICOMMAND are NOT written !!!
1330
                  if ( ((`ETH_BASE + i2) == `ETH_MIICOMMAND) && (i_data <= 2) )
1331
                  begin
1332
                    if (burst_tmp_data[31:0] !== burst_data[31:0])
1333
                    begin
1334
                      fail = fail + 1;
1335
                      test_fail("NON WR bit of the MAC MIICOMMAND register was wrong written or read");
1336
                      `TIME;
1337
                      $display("wbm_init_waits %d, wbm_subseq_waits %d, addr %h, data %h, tmp_data %h",
1338
                                wbm_init_waits, wbm_subseq_waits, i2, burst_data[31:0], burst_tmp_data[31:0]);
1339
                    end
1340
                  end
1341
                  else
1342
                  begin
1343
                    if ( ((i_data >= bit_start_1) && (i_data <= bit_end_1)) ||
1344
                         ((i_data >= bit_start_2) && (i_data <= bit_end_2)) ) // data should be equal to tmp_data
1345
                    begin
1346
                      if (burst_tmp_data[31:0] !== burst_data[31:0])
1347
                      begin
1348
                        fail = fail + 1;
1349
                        test_fail("RW bit of the MAC register was not written or not read");
1350
                        `TIME;
1351
                        $display("wbm_init_waits %d, wbm_subseq_waits %d, addr %h, data %h, tmp_data %h",
1352
                                  wbm_init_waits, wbm_subseq_waits, i2, burst_data[31:0], burst_tmp_data[31:0]);
1353
                      end
1354
                    end
1355
                    else // data should not be equal to tmp_data
1356
                    begin
1357
                      if (burst_tmp_data[31:0] === burst_data[31:0])
1358
                      begin
1359
                        fail = fail + 1;
1360
                        test_fail("NON RW bit of the MAC register was written, but it shouldn't be");
1361
                        `TIME;
1362
                        $display("wbm_init_waits %d, wbm_subseq_waits %d, addr %h, data %h, tmp_data %h",
1363
                                  wbm_init_waits, wbm_subseq_waits, i2, burst_data[31:0], burst_tmp_data[31:0]);
1364
                      end
1365
                    end
1366
                  end
1367
                  burst_tmp_data = burst_tmp_data >> 32;
1368
                  burst_data = burst_data >> 32;
1369
                end
1370
              end
1371
            end
1372
          end
1373
          if(fail == 0)
1374
            test_ok;
1375
          else
1376
            fail = 0;*/
1377
  end
1378 116 mohor
 
1379 169 mohor
end
1380 156 mohor
 
1381 169 mohor
end
1382
endtask // test_access_to_mac_reg
1383 156 mohor
 
1384
 
1385 169 mohor
task test_mii;
1386
  input  [31:0]  start_task;
1387
  input  [31:0]  end_task;
1388
  integer        i;
1389
  integer        i1;
1390
  integer        i2;
1391
  integer        i3;
1392
  integer        cnt;
1393
  integer        fail;
1394 181 mohor
  integer        test_num;
1395 169 mohor
  reg     [8:0]  clk_div; // only 8 bits are valid!
1396
  reg     [4:0]  phy_addr;
1397
  reg     [4:0]  reg_addr;
1398
  reg     [15:0] phy_data;
1399
  reg     [15:0] tmp_data;
1400
begin
1401
// MIIM MODULE TEST
1402
test_heading("MIIM MODULE TEST");
1403
$display(" ");
1404
$display("MIIM MODULE TEST");
1405
fail = 0;
1406 156 mohor
 
1407 192 tadej
// reset MAC registers
1408
hard_reset;
1409
// reset MAC and MII LOGIC with soft reset
1410
reset_mac;
1411 169 mohor
reset_mii;
1412 116 mohor
 
1413 194 tadej
 
1414 181 mohor
//////////////////////////////////////////////////////////////////////
1415
////                                                              ////
1416
////  test_mii:                                                   ////
1417
////                                                              ////
1418
////  0:  Test clock divider of mii management module with all    ////
1419
////      possible frequences.                                    ////
1420
////  1:  Test various readings from 'real' phy registers.        ////
1421
////  2:  Test various writings to 'real' phy registers (control  ////
1422
////      and non writable registers)                             ////
1423
////  3:  Test reset phy through mii management module            ////
1424
////  4:  Test 'walking one' across phy address (with and without ////
1425
////      preamble)                                               ////
1426
////  5:  Test 'walking one' across phy's register address (with  ////
1427
////      and without preamble)                                   ////
1428
////  6:  Test 'walking one' across phy's data (with and without  ////
1429
////      preamble)                                               ////
1430
////  7:  Test reading from phy with wrong phy address (host      ////
1431
////      reading high 'z' data)                                  ////
1432
////  8:  Test writing to phy with wrong phy address and reading  ////
1433
////      from correct one                                        ////
1434
////  9:  Test sliding stop scan command immediately after read   ////
1435
////      request (with and without preamble)                     ////
1436
//// 10:  Test sliding stop scan command immediately after write  ////
1437
////      request (with and without preamble)                     ////
1438
//// 11:  Test busy and nvalid status durations during write      ////
1439
////      (with and without preamble)                             ////
1440
//// 12:  Test busy and nvalid status durations during write      ////
1441
////      (with and without preamble)                             ////
1442
//// 13:  Test busy and nvalid status durations during scan (with ////
1443
////      and without preamble)                                   ////
1444
//// 14:  Test scan status from phy with detecting link-fail bit  ////
1445
////      (with and without preamble)                             ////
1446
//// 15:  Test scan status from phy with sliding link-fail bit    ////
1447
////      (with and without preamble)                             ////
1448
//// 16:  Test sliding stop scan command immediately after scan   ////
1449
////      request (with and without preamble)                     ////
1450
//// 17:  Test sliding stop scan command after 2. scan (with and  ////
1451
////      without preamble)                                       ////
1452
////                                                              ////
1453
//////////////////////////////////////////////////////////////////////
1454 194 tadej
for (test_num = start_task; test_num <= end_task; test_num = test_num + 1)
1455 169 mohor
begin
1456 194 tadej
 
1457 181 mohor
  ////////////////////////////////////////////////////////////////////
1458
  ////                                                            ////
1459
  ////  Test clock divider of mii management module with all      ////
1460
  ////  possible frequences.                                      ////
1461
  ////                                                            ////
1462
  ////////////////////////////////////////////////////////////////////
1463
  if (test_num == 0) // Test clock divider of mii management module with all possible frequences.
1464 169 mohor
  begin
1465 194 tadej
    // TEST 0: CLOCK DIVIDER OF MII MANAGEMENT MODULE WITH ALL POSSIBLE FREQUENCES
1466
    test_name   = "TEST 0: CLOCK DIVIDER OF MII MANAGEMENT MODULE WITH ALL POSSIBLE FREQUENCES";
1467
    `TIME; $display("  TEST 0: CLOCK DIVIDER OF MII MANAGEMENT MODULE WITH ALL POSSIBLE FREQUENCES");
1468 181 mohor
 
1469
    wait(Mdc_O); // wait for MII clock to be 1
1470
    for(clk_div = 0; clk_div <= 255; clk_div = clk_div + 1)
1471
    begin
1472
      i1 = 0;
1473
      i2 = 0;
1474
      #Tp mii_set_clk_div(clk_div[7:0]);
1475
      @(posedge Mdc_O);
1476
      #Tp;
1477
      fork
1478 169 mohor
        begin
1479 181 mohor
          @(posedge Mdc_O);
1480 169 mohor
          #Tp;
1481 181 mohor
          disable count_i1;
1482
          disable count_i2;
1483 169 mohor
        end
1484 181 mohor
        begin: count_i1
1485
          forever
1486
          begin
1487
            @(posedge wb_clk);
1488
            i1 = i1 + 1;
1489
            #Tp;
1490
          end
1491
        end
1492
        begin: count_i2
1493
          forever
1494
          begin
1495
            @(negedge wb_clk);
1496
            i2 = i2 + 1;
1497
            #Tp;
1498
          end
1499
        end
1500
      join
1501
      if((clk_div[7:0] == 0) || (clk_div[7:0] == 1) || (clk_div[7:0] == 2) || (clk_div[7:0] == 3))
1502
      begin
1503
        if((i1 == i2) && (i1 == 2))
1504 169 mohor
        begin
1505
        end
1506 181 mohor
        else
1507
        begin
1508
          fail = fail + 1;
1509 209 tadejm
          test_fail("Clock divider of MII module did'nt divide frequency corectly (it should divide by 2)");
1510 181 mohor
        end
1511 169 mohor
      end
1512
      else
1513
      begin
1514 181 mohor
        if((i1 == i2) && (i1 == {clk_div[7:1], 1'b0}))
1515
        begin
1516
        end
1517
        else
1518
        begin
1519
          fail = fail + 1;
1520
          test_fail("Clock divider of MII module did'nt divide frequency corectly");
1521
        end
1522 169 mohor
      end
1523
    end
1524 181 mohor
    if(fail == 0)
1525
      test_ok;
1526 169 mohor
    else
1527 181 mohor
      fail = 0;
1528
  end
1529
 
1530
 
1531
  ////////////////////////////////////////////////////////////////////
1532
  ////                                                            ////
1533
  ////  Test various readings from 'real' phy registers.          ////
1534
  ////                                                            ////
1535
  ////////////////////////////////////////////////////////////////////
1536
  if (test_num == 1) // Test various readings from 'real' phy registers.
1537
  begin
1538 194 tadej
    // TEST 1: VARIOUS READINGS FROM 'REAL' PHY REGISTERS
1539
    test_name   = "TEST 1: VARIOUS READINGS FROM 'REAL' PHY REGISTERS";
1540
    `TIME; $display("  TEST 1: VARIOUS READINGS FROM 'REAL' PHY REGISTERS");
1541 181 mohor
 
1542
    // set the fastest possible MII
1543
    clk_div = 0;
1544
    mii_set_clk_div(clk_div[7:0]);
1545
    // set address
1546
    reg_addr = 5'h1F;
1547
    phy_addr = 5'h1;
1548
    while(reg_addr >= 5'h4)
1549 169 mohor
    begin
1550 181 mohor
      // read request
1551
      #Tp mii_read_req(phy_addr, reg_addr);
1552
      check_mii_busy; // wait for read to finish
1553
      // read data
1554
      #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1555
      if (phy_data !== 16'hDEAD)
1556 169 mohor
      begin
1557 181 mohor
        test_fail("Wrong data was read from PHY from 'not used' address space");
1558
        fail = fail + 1;
1559 169 mohor
      end
1560 181 mohor
      if (reg_addr == 5'h4) // go out of for loop
1561
        reg_addr = 5'h3;
1562 169 mohor
      else
1563 181 mohor
        reg_addr = reg_addr - 5'h9;
1564 169 mohor
    end
1565 181 mohor
 
1566
    // set address
1567
    reg_addr = 5'h3;
1568
    // read request
1569
    #Tp mii_read_req(phy_addr, reg_addr);
1570
    check_mii_busy; // wait for read to finish
1571
    // read data
1572
    #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1573
    if (phy_data !== {`PHY_ID2, `MAN_MODEL_NUM, `MAN_REVISION_NUM})
1574
    begin
1575
      test_fail("Wrong data was read from PHY from ID register 2");
1576
      fail = fail + 1;
1577
    end
1578
    if(fail == 0)
1579
      test_ok;
1580
    else
1581
      fail = 0;
1582 169 mohor
  end
1583 116 mohor
 
1584
 
1585 181 mohor
  ////////////////////////////////////////////////////////////////////
1586
  ////                                                            ////
1587
  ////  Test various writings to 'real' phy registers (control    ////
1588
  ////  and non writable registers)                               ////
1589
  ////                                                            ////
1590
  ////////////////////////////////////////////////////////////////////
1591
  if (test_num == 2) // 
1592 169 mohor
  begin
1593 194 tadej
    // TEST 2: VARIOUS WRITINGS TO 'REAL' PHY REGISTERS ( CONTROL AND NON WRITABLE REGISTERS )
1594
    test_name   = "TEST 2: VARIOUS WRITINGS TO 'REAL' PHY REGISTERS ( CONTROL AND NON WRITABLE REGISTERS )";
1595
    `TIME; $display("  TEST 2: VARIOUS WRITINGS TO 'REAL' PHY REGISTERS ( CONTROL AND NON WRITABLE REGISTERS )");
1596 181 mohor
 
1597
    // negate data and try to write into unwritable register
1598
    tmp_data = ~phy_data;
1599
    // write request
1600
    #Tp mii_write_req(phy_addr, reg_addr, tmp_data);
1601
    check_mii_busy; // wait for write to finish
1602 169 mohor
    // read request
1603
    #Tp mii_read_req(phy_addr, reg_addr);
1604
    check_mii_busy; // wait for read to finish
1605
    // read data
1606 181 mohor
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1607
    if (tmp_data !== phy_data)
1608
    begin
1609
      test_fail("Data was written into unwritable PHY register - ID register 2");
1610
      fail = fail + 1;
1611
    end
1612
 
1613
    // set address
1614
    reg_addr = 5'h0; // control register
1615
    // read request
1616
    #Tp mii_read_req(phy_addr, reg_addr);
1617
    check_mii_busy; // wait for read to finish
1618
    // read data
1619
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1620
    // write request
1621
    phy_data = 16'h7DFF; // bit 15 (RESET bit) and bit 9 are self clearing bits
1622
    #Tp mii_write_req(phy_addr, reg_addr, phy_data);
1623
    check_mii_busy; // wait for write to finish
1624
    // read request
1625
    #Tp mii_read_req(phy_addr, reg_addr);
1626
    check_mii_busy; // wait for read to finish
1627
    // read data
1628 169 mohor
    #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1629 181 mohor
    if (phy_data !== 16'h7DFF)
1630 169 mohor
    begin
1631 181 mohor
      test_fail("Data was not correctly written into OR read from writable PHY register - control register");
1632 169 mohor
      fail = fail + 1;
1633
    end
1634 181 mohor
    // write request
1635
    #Tp mii_write_req(phy_addr, reg_addr, tmp_data);
1636
    check_mii_busy; // wait for write to finish
1637
    // read request
1638
    #Tp mii_read_req(phy_addr, reg_addr);
1639
    check_mii_busy; // wait for read to finish
1640
    // read data
1641
    #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1642
    if (phy_data !== tmp_data)
1643
    begin
1644
      test_fail("Data was not correctly written into OR read from writable PHY register - control register");
1645
      fail = fail + 1;
1646
    end
1647
    if(fail == 0)
1648
      test_ok;
1649 116 mohor
    else
1650 181 mohor
      fail = 0;
1651 169 mohor
  end
1652 116 mohor
 
1653
 
1654 181 mohor
  ////////////////////////////////////////////////////////////////////
1655
  ////                                                            ////
1656
  ////  Test reset phy through mii management module              ////
1657
  ////                                                            ////
1658
  ////////////////////////////////////////////////////////////////////
1659
  if (test_num == 3) // 
1660 169 mohor
  begin
1661 194 tadej
    // TEST 3: RESET PHY THROUGH MII MANAGEMENT MODULE
1662
    test_name   = "TEST 3: RESET PHY THROUGH MII MANAGEMENT MODULE";
1663
    `TIME; $display("  TEST 3: RESET PHY THROUGH MII MANAGEMENT MODULE");
1664 181 mohor
 
1665
    // set address
1666
    reg_addr = 5'h0; // control register
1667
    // write request
1668
    phy_data = 16'h7DFF; // bit 15 (RESET bit) and bit 9 are self clearing bits
1669
    #Tp mii_write_req(phy_addr, reg_addr, phy_data);
1670
    check_mii_busy; // wait for write to finish
1671
    // read request
1672
    #Tp mii_read_req(phy_addr, reg_addr);
1673
    check_mii_busy; // wait for read to finish
1674
    // read data
1675
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1676
    if (phy_data !== tmp_data)
1677
    begin
1678
      test_fail("Data was not correctly written into OR read from writable PHY register - control register");
1679
      fail = fail + 1;
1680
    end
1681
    // set reset bit - selfclearing bit in PHY
1682
    phy_data = phy_data | 16'h8000;
1683
    // write request
1684
    #Tp mii_write_req(phy_addr, reg_addr, phy_data);
1685
    check_mii_busy; // wait for write to finish
1686
    // read request
1687
    #Tp mii_read_req(phy_addr, reg_addr);
1688
    check_mii_busy; // wait for read to finish
1689
    // read data
1690
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1691
    // check self clearing of reset bit
1692
    if (tmp_data[15] !== 1'b0)
1693
    begin
1694
      test_fail("Reset bit should be self cleared - control register");
1695
      fail = fail + 1;
1696
    end
1697
    // check reset value of control register
1698
    if (tmp_data !== {2'h0, (`LED_CFG1 || `LED_CFG2), `LED_CFG1, 3'h0, `LED_CFG3, 8'h0})
1699
    begin
1700
      test_fail("PHY was not reset correctly AND/OR reset bit not self cleared");
1701
      fail = fail + 1;
1702
    end
1703
    if(fail == 0)
1704
      test_ok;
1705
    else
1706
      fail = 0;
1707 169 mohor
  end
1708
 
1709
 
1710 181 mohor
  ////////////////////////////////////////////////////////////////////
1711
  ////                                                            ////
1712
  ////  Test 'walking one' across phy address (with and without   ////
1713
  ////  preamble)                                                 ////
1714
  ////                                                            ////
1715
  ////////////////////////////////////////////////////////////////////
1716
  if (test_num == 4) // 
1717 169 mohor
  begin
1718 194 tadej
    // TEST 4: 'WALKING ONE' ACROSS PHY ADDRESS ( WITH AND WITHOUT PREAMBLE )
1719
    test_name   = "TEST 4: 'WALKING ONE' ACROSS PHY ADDRESS ( WITH AND WITHOUT PREAMBLE )";
1720
    `TIME; $display("  TEST 4: 'WALKING ONE' ACROSS PHY ADDRESS ( WITH AND WITHOUT PREAMBLE )");
1721 181 mohor
 
1722
    // set PHY to test mode
1723
    #Tp eth_phy.test_regs(1); // set test registers (wholy writable registers) and respond to all PHY addresses
1724
    for (i = 0; i <= 1; i = i + 1)
1725 169 mohor
    begin
1726 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
1727
      #Tp eth_phy.clear_test_regs;
1728
      // MII mode register
1729
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}), 4'hF, 1, wbm_init_waits,
1730
                wbm_subseq_waits);
1731
      // walk one across phy address
1732
      for (phy_addr = 5'h1; phy_addr > 5'h0; phy_addr = phy_addr << 1)
1733 169 mohor
      begin
1734 181 mohor
        reg_addr = $random;
1735
        tmp_data = $random;
1736
        // write request
1737
        #Tp mii_write_req(phy_addr, reg_addr, tmp_data);
1738
        check_mii_busy; // wait for write to finish
1739
        // read request
1740
        #Tp mii_read_req(phy_addr, reg_addr);
1741
        check_mii_busy; // wait for read to finish
1742
        // read data
1743
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1744
        #Tp;
1745
        if (phy_data !== tmp_data)
1746
        begin
1747
          if (i)
1748
            test_fail("Data was not correctly written into OR read from test registers (without preamble)");
1749
          else
1750
            test_fail("Data was not correctly written into OR read from test registers (with preamble)");
1751
          fail = fail + 1;
1752
        end
1753
        @(posedge wb_clk);
1754
        #Tp;
1755 169 mohor
      end
1756
    end
1757 181 mohor
    // set PHY to normal mode
1758
    #Tp eth_phy.test_regs(0);
1759
    #Tp eth_phy.preamble_suppresed(0);
1760
    // MII mode register
1761
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1762
    if(fail == 0)
1763
      test_ok;
1764
    else
1765
      fail = 0;
1766 169 mohor
  end
1767
 
1768
 
1769 181 mohor
  ////////////////////////////////////////////////////////////////////
1770
  ////                                                            ////
1771
  ////  Test 'walking one' across phy's register address (with    ////
1772
  ////  and without preamble)                                     ////
1773
  ////                                                            ////
1774
  ////////////////////////////////////////////////////////////////////
1775
  if (test_num == 5) // 
1776 169 mohor
  begin
1777 194 tadej
    // TEST 5: 'WALKING ONE' ACROSS PHY'S REGISTER ADDRESS ( WITH AND WITHOUT PREAMBLE )
1778
    test_name   = "TEST 5: 'WALKING ONE' ACROSS PHY'S REGISTER ADDRESS ( WITH AND WITHOUT PREAMBLE )";
1779
    `TIME; $display("  TEST 5: 'WALKING ONE' ACROSS PHY'S REGISTER ADDRESS ( WITH AND WITHOUT PREAMBLE )");
1780 181 mohor
 
1781
    // set PHY to test mode
1782
    #Tp eth_phy.test_regs(1); // set test registers (wholy writable registers) and respond to all PHY addresses
1783
    for (i = 0; i <= 1; i = i + 1)
1784 169 mohor
    begin
1785 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
1786
      #Tp eth_phy.clear_test_regs;
1787
      // MII mode register
1788
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}), 4'hF, 1, wbm_init_waits,
1789
                wbm_subseq_waits);
1790
      // walk one across reg address
1791
      for (reg_addr = 5'h1; reg_addr > 5'h0; reg_addr = reg_addr << 1)
1792 169 mohor
      begin
1793 181 mohor
        phy_addr = $random;
1794
        tmp_data = $random;
1795
        // write request
1796
        #Tp mii_write_req(phy_addr, reg_addr, tmp_data);
1797
        check_mii_busy; // wait for write to finish
1798
        // read request
1799
        #Tp mii_read_req(phy_addr, reg_addr);
1800
        check_mii_busy; // wait for read to finish
1801
        // read data
1802
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1803
        #Tp;
1804
        if (phy_data !== tmp_data)
1805
        begin
1806
          if (i)
1807
            test_fail("Data was not correctly written into OR read from test registers (without preamble)");
1808
          else
1809
            test_fail("Data was not correctly written into OR read from test registers (with preamble)");
1810
          fail = fail + 1;
1811
        end
1812
        @(posedge wb_clk);
1813
        #Tp;
1814 169 mohor
      end
1815
    end
1816 181 mohor
    // set PHY to normal mode
1817
    #Tp eth_phy.test_regs(0);
1818
    #Tp eth_phy.preamble_suppresed(0);
1819
    // MII mode register
1820
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1821
    if(fail == 0)
1822
      test_ok;
1823
    else
1824
      fail = 0;
1825 169 mohor
  end
1826
 
1827
 
1828 181 mohor
  ////////////////////////////////////////////////////////////////////
1829
  ////                                                            ////
1830
  ////  Test 'walking one' across phy's data (with and without    ////
1831
  ////  preamble)                                                 ////
1832
  ////                                                            ////
1833
  ////////////////////////////////////////////////////////////////////
1834
  if (test_num == 6) // 
1835 169 mohor
  begin
1836 194 tadej
    // TEST 6: 'WALKING ONE' ACROSS PHY'S DATA ( WITH AND WITHOUT PREAMBLE )
1837
    test_name   = "TEST 6: 'WALKING ONE' ACROSS PHY'S DATA ( WITH AND WITHOUT PREAMBLE )";
1838
    `TIME; $display("  TEST 6: 'WALKING ONE' ACROSS PHY'S DATA ( WITH AND WITHOUT PREAMBLE )");
1839 181 mohor
 
1840
    // set PHY to test mode
1841
    #Tp eth_phy.test_regs(1); // set test registers (wholy writable registers) and respond to all PHY addresses
1842
    for (i = 0; i <= 1; i = i + 1)
1843 169 mohor
    begin
1844 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
1845
      #Tp eth_phy.clear_test_regs;
1846
      // MII mode register
1847
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}), 4'hF, 1, wbm_init_waits,
1848
                wbm_subseq_waits);
1849
      // walk one across data
1850
      for (tmp_data = 16'h1; tmp_data > 16'h0; tmp_data = tmp_data << 1)
1851 169 mohor
      begin
1852 181 mohor
        phy_addr = $random;
1853
        reg_addr = $random;
1854
        // write request
1855
        #Tp mii_write_req(phy_addr, reg_addr, tmp_data);
1856
        check_mii_busy; // wait for write to finish
1857
        // read request
1858
        #Tp mii_read_req(phy_addr, reg_addr);
1859
        check_mii_busy; // wait for read to finish
1860
        // read data
1861
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1862
        #Tp;
1863
        if (phy_data !== tmp_data)
1864
        begin
1865
          if (i)
1866
            test_fail("Data was not correctly written into OR read from test registers (without preamble)");
1867
          else
1868
            test_fail("Data was not correctly written into OR read from test registers (with preamble)");
1869
          fail = fail + 1;
1870
        end
1871
        @(posedge wb_clk);
1872
        #Tp;
1873 169 mohor
      end
1874
    end
1875 181 mohor
    // set PHY to normal mode
1876
    #Tp eth_phy.test_regs(0);
1877
    #Tp eth_phy.preamble_suppresed(0);
1878
    // MII mode register
1879
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1880
    if(fail == 0)
1881
      test_ok;
1882
    else
1883
      fail = 0;
1884 169 mohor
  end
1885
 
1886
 
1887 181 mohor
  ////////////////////////////////////////////////////////////////////
1888
  ////                                                            ////
1889
  ////  Test reading from phy with wrong phy address (host        ////
1890
  ////  reading high 'z' data)                                    ////
1891
  ////                                                            ////
1892
  ////////////////////////////////////////////////////////////////////
1893
  if (test_num == 7) // 
1894 169 mohor
  begin
1895 194 tadej
    // TEST 7: READING FROM PHY WITH WRONG PHY ADDRESS ( HOST READING HIGH 'Z' DATA )
1896
    test_name   = "TEST 7: READING FROM PHY WITH WRONG PHY ADDRESS ( HOST READING HIGH 'Z' DATA )";
1897
    `TIME; $display("  TEST 7: READING FROM PHY WITH WRONG PHY ADDRESS ( HOST READING HIGH 'Z' DATA )");
1898 181 mohor
 
1899
    phy_addr = 5'h2; // wrong PHY address
1900
    // read request
1901
    #Tp mii_read_req(phy_addr, reg_addr);
1902
    check_mii_busy; // wait for read to finish
1903
    // read data
1904
    $display("  => Two errors will be displayed from WB Bus Monitor, because correct HIGH Z data was read");
1905
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1906
    if (tmp_data !== 16'hzzzz)
1907
    begin
1908
      test_fail("Data was read from PHY register with wrong PHY address - control register");
1909
      fail = fail + 1;
1910
    end
1911
    if(fail == 0)
1912
      test_ok;
1913
    else
1914
      fail = 0;
1915 169 mohor
  end
1916
 
1917
 
1918 181 mohor
  ////////////////////////////////////////////////////////////////////
1919
  ////                                                            ////
1920
  ////  Test writing to phy with wrong phy address and reading    ////
1921
  ////  from correct one                                          ////
1922
  ////                                                            ////
1923
  ////////////////////////////////////////////////////////////////////
1924
  if (test_num == 8) // 
1925 169 mohor
  begin
1926 194 tadej
    // TEST 8: WRITING TO PHY WITH WRONG PHY ADDRESS AND READING FROM CORRECT ONE
1927
    test_name   = "TEST 8: WRITING TO PHY WITH WRONG PHY ADDRESS AND READING FROM CORRECT ONE";
1928
    `TIME; $display("  TEST 8: WRITING TO PHY WITH WRONG PHY ADDRESS AND READING FROM CORRECT ONE");
1929 181 mohor
 
1930
    // set address
1931
    reg_addr = 5'h0; // control register
1932
    phy_addr = 5'h2; // wrong PHY address
1933
    // write request
1934
    phy_data = 16'h7DFF; // bit 15 (RESET bit) and bit 9 are self clearing bits
1935
    #Tp mii_write_req(phy_addr, reg_addr, phy_data);
1936
    check_mii_busy; // wait for write to finish
1937
 
1938
    phy_addr = 5'h1; // correct PHY address
1939
    // read request
1940
    #Tp mii_read_req(phy_addr, reg_addr);
1941
    check_mii_busy; // wait for read to finish
1942
    // read data
1943
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1944
    if (phy_data === tmp_data)
1945
    begin
1946
      test_fail("Data was written into PHY register with wrong PHY address - control register");
1947
      fail = fail + 1;
1948
    end
1949
    if(fail == 0)
1950
      test_ok;
1951
    else
1952
      fail = 0;
1953 169 mohor
  end
1954
 
1955
 
1956 181 mohor
  ////////////////////////////////////////////////////////////////////
1957
  ////                                                            ////
1958
  ////  Test sliding stop scan command immediately after read     ////
1959
  ////  request (with and without preamble)                       ////
1960
  ////                                                            ////
1961
  ////////////////////////////////////////////////////////////////////
1962
  if (test_num == 9) // 
1963 169 mohor
  begin
1964 194 tadej
    // TEST 9: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER READ REQUEST ( WITH AND WITHOUT PREAMBLE )
1965
    test_name = "TEST 9: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER READ REQUEST ( WITH AND WITHOUT PREAMBLE )";
1966 181 mohor
    `TIME;
1967 194 tadej
    $display("  TEST 9: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER READ REQUEST ( WITH AND WITHOUT PREAMBLE )");
1968 181 mohor
 
1969
    for (i2 = 0; i2 <= 1; i2 = i2 + 1) // choose preamble or not
1970 169 mohor
    begin
1971 181 mohor
      #Tp eth_phy.preamble_suppresed(i2);
1972
      // MII mode register
1973
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i2, 8'h0}), 4'hF, 1, wbm_init_waits,
1974
               wbm_subseq_waits);
1975
      i = 0;
1976
      cnt = 0;
1977
      while (i < 80) // delay for sliding of writing a STOP SCAN command
1978 169 mohor
      begin
1979 181 mohor
        for (i3 = 0; i3 <= 1; i3 = i3 + 1) // choose read or write after read will be finished
1980
        begin
1981
          // set address
1982
          reg_addr = 5'h0; // control register
1983
          phy_addr = 5'h1; // correct PHY address
1984
          cnt = 0;
1985
          // read request
1986
          #Tp mii_read_req(phy_addr, reg_addr);
1987
          fork
1988
            begin
1989
              repeat(i) @(posedge Mdc_O);
1990
              // write command 0x0 into MII command register
1991
              // MII command written while read in progress
1992
              wbm_write(`ETH_MIICOMMAND, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1993
              @(posedge wb_clk);
1994
              #Tp check_mii_busy; // wait for read to finish
1995
            end
1996
            begin
1997
              // wait for serial bus to become active
1998
              wait(Mdio_IO !== 1'bz);
1999
              // count transfer length
2000
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
2001
              begin
2002
                @(posedge Mdc_O);
2003
                #Tp cnt = cnt + 1;
2004
              end
2005
            end
2006
          join
2007
          // check transfer length
2008
          if (i2) // without preamble
2009 169 mohor
          begin
2010 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
2011
            begin
2012
              test_fail("Read request did not proceed correctly, while SCAN STOP command was written");
2013
              fail = fail + 1;
2014
            end
2015 169 mohor
          end
2016 181 mohor
          else // with preamble
2017 169 mohor
          begin
2018 181 mohor
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
2019
            begin
2020
              test_fail("Read request did not proceed correctly, while SCAN STOP command was written");
2021
              fail = fail + 1;
2022
            end
2023
          end
2024
          // check the BUSY signal to see if the bus is still IDLE
2025
          for (i1 = 0; i1 < 8; i1 = i1 + 1)
2026
            check_mii_busy; // wait for bus to become idle
2027
 
2028
          // try normal write or read after read was finished
2029
          #Tp phy_data = {8'h7D, (i[7:0] + 1)};
2030
          #Tp cnt = 0;
2031
          if (i3 == 0) // write after read
2032
          begin
2033
            // write request
2034
            #Tp mii_write_req(phy_addr, reg_addr, phy_data);
2035 169 mohor
            // wait for serial bus to become active
2036
            wait(Mdio_IO !== 1'bz);
2037
            // count transfer length
2038 181 mohor
            while(Mdio_IO !== 1'bz)
2039 169 mohor
            begin
2040
              @(posedge Mdc_O);
2041
              #Tp cnt = cnt + 1;
2042
            end
2043 181 mohor
            @(posedge Mdc_O);
2044
            // read request
2045
            #Tp mii_read_req(phy_addr, reg_addr);
2046
            check_mii_busy; // wait for read to finish
2047
            // read and check data
2048
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2049
            if (phy_data !== tmp_data)
2050
            begin
2051
              test_fail("Data was not correctly written into OR read from PHY register - control register");
2052
              fail = fail + 1;
2053
            end
2054 169 mohor
          end
2055 181 mohor
          else // read after read
2056 169 mohor
          begin
2057 181 mohor
            // read request
2058
            #Tp mii_read_req(phy_addr, reg_addr);
2059
            // wait for serial bus to become active
2060
            wait(Mdio_IO !== 1'bz);
2061
            // count transfer length
2062
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
2063
            begin
2064
              @(posedge Mdc_O);
2065
              #Tp cnt = cnt + 1;
2066
            end
2067 169 mohor
            @(posedge Mdc_O);
2068 181 mohor
            check_mii_busy; // wait for read to finish
2069
            // read and check data
2070
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2071
            if (phy_data !== tmp_data)
2072
            begin
2073
              test_fail("Data was not correctly written into OR read from PHY register - control register");
2074
              fail = fail + 1;
2075
            end
2076 169 mohor
          end
2077 181 mohor
          // check if transfer was a proper length
2078
          if (i2) // without preamble
2079 169 mohor
          begin
2080 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
2081
            begin
2082
              test_fail("New request did not proceed correctly, after read request");
2083
              fail = fail + 1;
2084
            end
2085 169 mohor
          end
2086 181 mohor
          else // with preamble
2087 169 mohor
          begin
2088 181 mohor
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
2089
            begin
2090
              test_fail("New request did not proceed correctly, after read request");
2091
              fail = fail + 1;
2092
            end
2093 169 mohor
          end
2094
        end
2095 181 mohor
        #Tp;
2096
        // set delay of writing the command
2097 169 mohor
        if (i2) // without preamble
2098
        begin
2099 181 mohor
          case(i)
2100
            0, 1:               i = i + 1;
2101
            18, 19, 20, 21, 22,
2102
            23, 24, 25, 26, 27,
2103
            28, 29, 30, 31, 32,
2104
            33, 34, 35:         i = i + 1;
2105
            36:                 i = 80;
2106
            default:            i = 18;
2107
          endcase
2108 169 mohor
        end
2109
        else // with preamble
2110
        begin
2111 181 mohor
          case(i)
2112
            0, 1:               i = i + 1;
2113
            50, 51, 52, 53, 54,
2114
            55, 56, 57, 58, 59,
2115
            60, 61, 62, 63, 64,
2116
            65, 66, 67:         i = i + 1;
2117
            68:                 i = 80;
2118
            default:            i = 50;
2119
          endcase
2120 169 mohor
        end
2121 181 mohor
        @(posedge wb_clk);
2122 169 mohor
      end
2123
    end
2124 181 mohor
    // set PHY to normal mode
2125
    #Tp eth_phy.preamble_suppresed(0);
2126
    // MII mode register
2127
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2128
    if(fail == 0)
2129
      test_ok;
2130
    else
2131
      fail = 0;
2132 169 mohor
  end
2133
 
2134
 
2135 181 mohor
  ////////////////////////////////////////////////////////////////////
2136
  ////                                                            ////
2137
  ////  Test sliding stop scan command immediately after write    ////
2138
  ////  request (with and without preamble)                       ////
2139
  ////                                                            ////
2140
  ////////////////////////////////////////////////////////////////////
2141
  if (test_num == 10) // 
2142 169 mohor
  begin
2143 194 tadej
    // TEST 10: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER WRITE REQUEST ( WITH AND WITHOUT PREAMBLE )
2144
    test_name = "TEST 10: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER WRITE REQUEST ( WITH AND WITHOUT PREAMBLE )";
2145 181 mohor
    `TIME;
2146 194 tadej
    $display("  TEST 10: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER WRITE REQUEST ( WITH AND WITHOUT PREAMBLE )");
2147 181 mohor
 
2148
    for (i2 = 0; i2 <= 1; i2 = i2 + 1) // choose preamble or not
2149 169 mohor
    begin
2150 181 mohor
      #Tp eth_phy.preamble_suppresed(i2);
2151
      // MII mode register
2152
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i2, 8'h0}), 4'hF, 1, wbm_init_waits,
2153
                wbm_subseq_waits);
2154
      i = 0;
2155
      cnt = 0;
2156
      while (i < 80) // delay for sliding of writing a STOP SCAN command
2157 169 mohor
      begin
2158 181 mohor
        for (i3 = 0; i3 <= 1; i3 = i3 + 1) // choose read or write after write will be finished
2159
        begin
2160
          // set address
2161
          reg_addr = 5'h0; // control register
2162
          phy_addr = 5'h1; // correct PHY address
2163
          cnt = 0;
2164
          // write request
2165
          phy_data = {8'h75, (i[7:0] + 1)};
2166
          #Tp mii_write_req(phy_addr, reg_addr, phy_data);
2167
          fork
2168
            begin
2169
              repeat(i) @(posedge Mdc_O);
2170
              // write command 0x0 into MII command register
2171
              // MII command written while read in progress
2172
              wbm_write(`ETH_MIICOMMAND, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2173
              @(posedge wb_clk);
2174
              #Tp check_mii_busy; // wait for write to finish
2175
            end
2176
            begin
2177
              // wait for serial bus to become active
2178
              wait(Mdio_IO !== 1'bz);
2179
              // count transfer length
2180
              while(Mdio_IO !== 1'bz)
2181
              begin
2182
                @(posedge Mdc_O);
2183
                #Tp cnt = cnt + 1;
2184
              end
2185
            end
2186
          join
2187
          // check transfer length
2188
          if (i2) // without preamble
2189 169 mohor
          begin
2190 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
2191
            begin
2192
              test_fail("Write request did not proceed correctly, while SCAN STOP command was written");
2193
              fail = fail + 1;
2194
            end
2195 169 mohor
          end
2196 181 mohor
          else // with preamble
2197 169 mohor
          begin
2198 181 mohor
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
2199
            begin
2200
              test_fail("Write request did not proceed correctly, while SCAN STOP command was written");
2201
              fail = fail + 1;
2202
            end
2203
          end
2204
          // check the BUSY signal to see if the bus is still IDLE
2205
          for (i1 = 0; i1 < 8; i1 = i1 + 1)
2206
            check_mii_busy; // wait for bus to become idle
2207
 
2208
          // try normal write or read after write was finished
2209
          #Tp cnt = 0;
2210
          if (i3 == 0) // write after write
2211
          begin
2212
            phy_data = {8'h7A, (i[7:0] + 1)};
2213
            // write request
2214
            #Tp mii_write_req(phy_addr, reg_addr, phy_data);
2215 169 mohor
            // wait for serial bus to become active
2216
            wait(Mdio_IO !== 1'bz);
2217
            // count transfer length
2218
            while(Mdio_IO !== 1'bz)
2219
            begin
2220
              @(posedge Mdc_O);
2221
              #Tp cnt = cnt + 1;
2222
            end
2223 181 mohor
            @(posedge Mdc_O);
2224
            // read request
2225
            #Tp mii_read_req(phy_addr, reg_addr);
2226
            check_mii_busy; // wait for read to finish
2227
            // read and check data
2228
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data , 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2229
            if (phy_data !== tmp_data)
2230
            begin
2231
              test_fail("Data was not correctly written into OR read from PHY register - control register");
2232
              fail = fail + 1;
2233
            end
2234 169 mohor
          end
2235 181 mohor
          else // read after write
2236 169 mohor
          begin
2237 181 mohor
            // read request
2238
            #Tp mii_read_req(phy_addr, reg_addr);
2239
            // wait for serial bus to become active
2240
            wait(Mdio_IO !== 1'bz);
2241
            // count transfer length
2242
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
2243
            begin
2244
              @(posedge Mdc_O);
2245
              #Tp cnt = cnt + 1;
2246
            end
2247 169 mohor
            @(posedge Mdc_O);
2248 181 mohor
            check_mii_busy; // wait for read to finish
2249
            // read and check data
2250
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data , 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2251
            if (phy_data !== tmp_data)
2252
            begin
2253
              test_fail("Data was not correctly written into OR read from PHY register - control register");
2254
              fail = fail + 1;
2255
            end
2256 169 mohor
          end
2257 181 mohor
          // check if transfer was a proper length
2258
          if (i2) // without preamble
2259 169 mohor
          begin
2260 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
2261
            begin
2262
              test_fail("New request did not proceed correctly, after write request");
2263
              fail = fail + 1;
2264
            end
2265 169 mohor
          end
2266 181 mohor
          else // with preamble
2267 169 mohor
          begin
2268 181 mohor
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
2269
            begin
2270
              test_fail("New request did not proceed correctly, after write request");
2271
              fail = fail + 1;
2272
            end
2273 169 mohor
          end
2274
        end
2275 181 mohor
        #Tp;
2276
        // set delay of writing the command
2277 169 mohor
        if (i2) // without preamble
2278
        begin
2279 181 mohor
          case(i)
2280
            0, 1:               i = i + 1;
2281
            18, 19, 20, 21, 22,
2282
            23, 24, 25, 26, 27,
2283
            28, 29, 30, 31, 32,
2284
            33, 34, 35:         i = i + 1;
2285
            36:                 i = 80;
2286
            default:            i = 18;
2287
          endcase
2288 169 mohor
        end
2289
        else // with preamble
2290
        begin
2291 181 mohor
          case(i)
2292
            0, 1:               i = i + 1;
2293
            50, 51, 52, 53, 54,
2294
            55, 56, 57, 58, 59,
2295
            60, 61, 62, 63, 64,
2296
            65, 66, 67:         i = i + 1;
2297
            68:                 i = 80;
2298
            default:            i = 50;
2299
          endcase
2300 169 mohor
        end
2301 181 mohor
        @(posedge wb_clk);
2302 169 mohor
      end
2303
    end
2304 181 mohor
    // set PHY to normal mode
2305
    #Tp eth_phy.preamble_suppresed(0);
2306
    // MII mode register
2307
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2308
    if(fail == 0)
2309
      test_ok;
2310
    else
2311
      fail = 0;
2312 169 mohor
  end
2313
 
2314
 
2315 181 mohor
  ////////////////////////////////////////////////////////////////////
2316
  ////                                                            ////
2317
  ////  Test busy and nvalid status durations during write (with  ////
2318
  ////  and without preamble)                                     ////
2319
  ////                                                            ////
2320
  ////////////////////////////////////////////////////////////////////
2321
  if (test_num == 11) // 
2322 169 mohor
  begin
2323 194 tadej
    // TEST 11: BUSY AND NVALID STATUS DURATIONS DURING WRITE ( WITH AND WITHOUT PREAMBLE )
2324
    test_name   = "TEST 11: BUSY AND NVALID STATUS DURATIONS DURING WRITE ( WITH AND WITHOUT PREAMBLE )";
2325
    `TIME; $display("  TEST 11: BUSY AND NVALID STATUS DURATIONS DURING WRITE ( WITH AND WITHOUT PREAMBLE )");
2326 181 mohor
 
2327
    reset_mii; // reset MII
2328
    // set link up, if it wasn't due to previous tests, since there weren't PHY registers
2329
    #Tp eth_phy.link_up_down(1);
2330
    // set the MII
2331
    clk_div = 64;
2332
    mii_set_clk_div(clk_div[7:0]);
2333
    // set address
2334
    reg_addr = 5'h1; // status register
2335
    phy_addr = 5'h1; // correct PHY address
2336
 
2337
    for (i = 0; i <= 1; i = i + 1)
2338 169 mohor
    begin
2339 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
2340
      // MII mode register
2341
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}) | (`ETH_MIIMODER_CLKDIV & clk_div),
2342
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2343
      @(posedge Mdc_O);
2344
      // write request
2345
      #Tp mii_write_req(phy_addr, reg_addr, 16'h5A5A);
2346
      // read data from MII status register - Busy and Nvalid bits
2347
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2348
 
2349
      // check MII IO signal and Busy and Nvalid bits
2350
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2351 169 mohor
      begin
2352 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is not HIGH Z - set higher clock divider");
2353
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2354
        begin
2355
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z anymore - 1. read");
2356
          fail = fail + 1;
2357
        end
2358
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2359
        begin
2360
          test_fail("Nvalid signal was set during write");
2361
          fail = fail + 1;
2362
        end
2363 169 mohor
      end
2364 181 mohor
      else // Busy bit should already be set to '1', due to reads from MII status register
2365 169 mohor
      begin
2366 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2367
        begin
2368
          test_fail("Busy signal should be set after write, due to reads from MII status register");
2369
          fail = fail + 1;
2370
        end
2371
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2372
        begin
2373
          test_fail("Nvalid signal was set during write");
2374
          fail = fail + 1;
2375
        end
2376 169 mohor
      end
2377 181 mohor
 
2378
      // wait for serial bus to become active
2379
      wait(Mdio_IO !== 1'bz);
2380
      // count transfer bits
2381
      if (i)
2382 169 mohor
      begin
2383 181 mohor
        repeat(32) @(posedge Mdc_O);
2384 169 mohor
      end
2385 181 mohor
      else
2386 169 mohor
      begin
2387 181 mohor
        repeat(64) @(posedge Mdc_O);
2388 169 mohor
      end
2389 181 mohor
      // read data from MII status register - Busy and Nvalid bits
2390
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2391
 
2392
      // check MII IO signal and Busy and Nvalid bits
2393
      if (Mdio_IO === 1'bz) // Mdio_IO should not be HIGH Z here - testbench selfcheck
2394 169 mohor
      begin
2395 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is HIGH Z - set higher clock divider");
2396
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2397
        begin
2398
          test_fail("Busy signal should be set while MII IO signal is not active anymore");
2399
          fail = fail + 1;
2400
        end
2401
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2402
        begin
2403
          test_fail("Nvalid signal was set during write");
2404
          fail = fail + 1;
2405
        end
2406 169 mohor
      end
2407 181 mohor
      else // Busy bit should still be set to '1'
2408 169 mohor
      begin
2409 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2410
        begin
2411
          test_fail("Busy signal should be set while MII IO signal not HIGH Z");
2412
          fail = fail + 1;
2413
        end
2414
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2415
        begin
2416
          test_fail("Nvalid signal was set during write");
2417
          fail = fail + 1;
2418
        end
2419 169 mohor
      end
2420 181 mohor
 
2421
      // wait for next negative clock edge
2422
      @(negedge Mdc_O);
2423 169 mohor
      // read data from MII status register - Busy and Nvalid bits
2424
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2425 181 mohor
 
2426 169 mohor
      // check MII IO signal and Busy and Nvalid bits
2427
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2428
      begin
2429
        test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2430
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2431
        begin
2432
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2433
          fail = fail + 1;
2434
        end
2435
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2436
        begin
2437
          test_fail("Nvalid signal was set during write");
2438
          fail = fail + 1;
2439
        end
2440
      end
2441 181 mohor
      else // Busy bit should still be set to '1'
2442 169 mohor
      begin
2443
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2444
        begin
2445 181 mohor
          test_fail("Busy signal should be set after MII IO signal become HIGH Z");
2446
          fail = fail + 1;
2447 169 mohor
        end
2448 181 mohor
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2449 169 mohor
        begin
2450 181 mohor
          test_fail("Nvalid signal was set during write");
2451
          fail = fail + 1;
2452
        end
2453
      end
2454
 
2455
      // wait for Busy to become inactive
2456
      i1 = 0;
2457
      while (i1 <= 2)
2458
      begin
2459
        // wait for next positive clock edge
2460
        @(posedge Mdc_O);
2461
        // read data from MII status register - Busy and Nvalid bits
2462
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2463
 
2464
        // check MII IO signal and Busy and Nvalid bits
2465
        if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2466
        begin
2467
          test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2468
          if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2469 169 mohor
          begin
2470 181 mohor
            test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2471 169 mohor
            fail = fail + 1;
2472
          end
2473 181 mohor
          if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2474
          begin
2475
            test_fail("Nvalid signal was set during write");
2476
            fail = fail + 1;
2477
          end
2478 169 mohor
        end
2479 181 mohor
        else // wait for Busy bit to be set to '0'
2480 169 mohor
        begin
2481 181 mohor
          if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2482
          begin
2483
            i1 = 3; // end of Busy checking
2484
          end
2485
          else
2486
          begin
2487
            if (i1 == 2)
2488
            begin
2489
              test_fail("Busy signal should be cleared after 2 periods after MII IO signal become HIGH Z");
2490
              fail = fail + 1;
2491
            end
2492
            #Tp i1 = i1 + 1;
2493
          end
2494
          if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2495
          begin
2496
            test_fail("Nvalid signal was set after write");
2497
            fail = fail + 1;
2498
          end
2499 169 mohor
        end
2500
      end
2501
    end
2502 181 mohor
    // set PHY to normal mode
2503
    #Tp eth_phy.preamble_suppresed(0);
2504
    // MII mode register
2505
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2506
    if(fail == 0)
2507
      test_ok;
2508
    else
2509
      fail = 0;
2510 116 mohor
  end
2511
 
2512
 
2513 181 mohor
  ////////////////////////////////////////////////////////////////////
2514
  ////                                                            ////
2515
  ////  Test busy and nvalid status durations during write (with  ////
2516
  ////  and without preamble)                                     ////
2517
  ////                                                            ////
2518
  ////////////////////////////////////////////////////////////////////
2519
  if (test_num == 12) // 
2520 169 mohor
  begin
2521 194 tadej
    // TEST 12: BUSY AND NVALID STATUS DURATIONS DURING READ ( WITH AND WITHOUT PREAMBLE )
2522
    test_name   = "TEST 12: BUSY AND NVALID STATUS DURATIONS DURING READ ( WITH AND WITHOUT PREAMBLE )";
2523
    `TIME; $display("  TEST 12: BUSY AND NVALID STATUS DURATIONS DURING READ ( WITH AND WITHOUT PREAMBLE )");
2524 181 mohor
 
2525
    reset_mii; // reset MII
2526
    // set link up, if it wasn't due to previous tests, since there weren't PHY registers
2527
    #Tp eth_phy.link_up_down(1);
2528
    // set the MII
2529
    clk_div = 64;
2530
    mii_set_clk_div(clk_div[7:0]);
2531
    // set address
2532
    reg_addr = 5'h1; // status register
2533
    phy_addr = 5'h1; // correct PHY address
2534
 
2535
    for (i = 0; i <= 1; i = i + 1)
2536 169 mohor
    begin
2537 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
2538
      // MII mode register
2539
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}) | (`ETH_MIIMODER_CLKDIV & clk_div),
2540
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2541 169 mohor
      @(posedge Mdc_O);
2542 181 mohor
      // read request
2543
      #Tp mii_read_req(phy_addr, reg_addr);
2544 169 mohor
      // read data from MII status register - Busy and Nvalid bits
2545
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2546 181 mohor
 
2547 169 mohor
      // check MII IO signal and Busy and Nvalid bits
2548
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2549
      begin
2550 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is not HIGH Z - set higher clock divider");
2551 169 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2552
        begin
2553 181 mohor
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z anymore - 1. read");
2554 169 mohor
          fail = fail + 1;
2555
        end
2556
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2557
        begin
2558
          test_fail("Nvalid signal was set during read");
2559
          fail = fail + 1;
2560
        end
2561
      end
2562 181 mohor
      else // Busy bit should already be set to '1', due to reads from MII status register
2563 169 mohor
      begin
2564
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2565
        begin
2566 181 mohor
          test_fail("Busy signal should be set after read, due to reads from MII status register");
2567
          fail = fail + 1;
2568 169 mohor
        end
2569
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2570
        begin
2571 181 mohor
          test_fail("Nvalid signal was set during read");
2572 169 mohor
          fail = fail + 1;
2573
        end
2574
      end
2575 181 mohor
 
2576
      // wait for serial bus to become active
2577
      wait(Mdio_IO !== 1'bz);
2578
      // count transfer bits
2579
      if (i)
2580 169 mohor
      begin
2581 181 mohor
        repeat(31) @(posedge Mdc_O);
2582 169 mohor
      end
2583 181 mohor
      else
2584 169 mohor
      begin
2585 181 mohor
        repeat(63) @(posedge Mdc_O);
2586 169 mohor
      end
2587 181 mohor
      // wait for next negative clock edge
2588
      @(negedge Mdc_O);
2589
      // read data from MII status register - Busy and Nvalid bits
2590
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2591
 
2592
      // check MII IO signal and Busy and Nvalid bits
2593
      if (Mdio_IO === 1'bz) // Mdio_IO should not be HIGH Z here - testbench selfcheck
2594 169 mohor
      begin
2595 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is HIGH Z - set higher clock divider");
2596
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2597
        begin
2598
          test_fail("Busy signal should be set while MII IO signal is not active anymore");
2599
          fail = fail + 1;
2600
        end
2601
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2602
        begin
2603
          test_fail("Nvalid signal was set during read");
2604
          fail = fail + 1;
2605
        end
2606 169 mohor
      end
2607 181 mohor
      else // Busy bit should still be set to '1'
2608 169 mohor
      begin
2609 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2610
        begin
2611
          test_fail("Busy signal should be set while MII IO signal not HIGH Z");
2612
          fail = fail + 1;
2613
        end
2614
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2615
        begin
2616
          test_fail("Nvalid signal was set during read");
2617
          fail = fail + 1;
2618
        end
2619 169 mohor
      end
2620 181 mohor
 
2621 169 mohor
      // wait for next positive clock edge
2622
      @(posedge Mdc_O);
2623
      // read data from MII status register - Busy and Nvalid bits
2624
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2625 181 mohor
 
2626 169 mohor
      // check MII IO signal and Busy and Nvalid bits
2627
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2628
      begin
2629
        test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2630 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2631 169 mohor
        begin
2632 181 mohor
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2633
          fail = fail + 1;
2634
        end
2635
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2636
        begin
2637
          test_fail("Nvalid signal was set during read");
2638
          fail = fail + 1;
2639
        end
2640
      end
2641
      else // Busy bit should still be set to '1'
2642
      begin
2643
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2644
        begin
2645
          test_fail("Busy signal should be set after MII IO signal become HIGH Z");
2646
          fail = fail + 1;
2647
        end
2648
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2649
        begin
2650
          test_fail("Nvalid signal was set during read");
2651
          fail = fail + 1;
2652
        end
2653
      end
2654
 
2655
      // wait for Busy to become inactive
2656
      i1 = 0;
2657
      while (i1 <= 2)
2658
      begin
2659
        // wait for next positive clock edge
2660
        @(posedge Mdc_O);
2661
        // read data from MII status register - Busy and Nvalid bits
2662
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2663
 
2664
        // check MII IO signal and Busy and Nvalid bits
2665
        if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2666
        begin
2667
          test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2668 169 mohor
          if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2669
          begin
2670
            test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2671
            fail = fail + 1;
2672
          end
2673 181 mohor
          if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2674 169 mohor
          begin
2675 181 mohor
            test_fail("Nvalid signal was set during read");
2676 169 mohor
            fail = fail + 1;
2677
          end
2678
        end
2679 181 mohor
        else // wait for Busy bit to be set to '0'
2680 169 mohor
        begin
2681
          if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2682
          begin
2683
            i1 = 3; // end of Busy checking
2684
          end
2685
          else
2686
          begin
2687
            if (i1 == 2)
2688
            begin
2689
              test_fail("Busy signal should be cleared after 2 periods after MII IO signal become HIGH Z");
2690
              fail = fail + 1;
2691
            end
2692
            #Tp i1 = i1 + 1;
2693
          end
2694 181 mohor
          if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2695 169 mohor
          begin
2696 181 mohor
            test_fail("Nvalid signal was set after read");
2697 169 mohor
            fail = fail + 1;
2698
          end
2699
        end
2700
      end
2701
    end
2702 181 mohor
    // set PHY to normal mode
2703
    #Tp eth_phy.preamble_suppresed(0);
2704
    // MII mode register
2705
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2706
    if(fail == 0)
2707
      test_ok;
2708
    else
2709
      fail = 0;
2710 169 mohor
  end
2711
 
2712
 
2713 181 mohor
  ////////////////////////////////////////////////////////////////////
2714
  ////                                                            ////
2715
  ////  Test busy and nvalid status durations during scan (with   ////
2716
  ////  and without preamble)                                     ////
2717
  ////                                                            ////
2718
  ////////////////////////////////////////////////////////////////////
2719
  if (test_num == 13) // 
2720 169 mohor
  begin
2721 194 tadej
    // TEST 13: BUSY AND NVALID STATUS DURATIONS DURING SCAN ( WITH AND WITHOUT PREAMBLE )
2722
    test_name   = "TEST 13: BUSY AND NVALID STATUS DURATIONS DURING SCAN ( WITH AND WITHOUT PREAMBLE )";
2723
    `TIME; $display("  TEST 13: BUSY AND NVALID STATUS DURATIONS DURING SCAN ( WITH AND WITHOUT PREAMBLE )");
2724 181 mohor
 
2725
    reset_mii; // reset MII
2726
    // set link up, if it wasn't due to previous tests, since there weren't PHY registers
2727
    #Tp eth_phy.link_up_down(1);
2728
    // set the MII
2729
    clk_div = 64;
2730
    mii_set_clk_div(clk_div[7:0]);
2731
    // set address
2732
    reg_addr = 5'h1; // status register
2733
    phy_addr = 5'h1; // correct PHY address
2734
 
2735
    for (i = 0; i <= 1; i = i + 1)
2736 169 mohor
    begin
2737 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
2738
      // MII mode register
2739
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}) | (`ETH_MIIMODER_CLKDIV & clk_div),
2740
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2741
      @(posedge Mdc_O);
2742
      // scan request
2743
      #Tp mii_scan_req(phy_addr, reg_addr);
2744
      // read data from MII status register - Busy and Nvalid bits
2745
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2746
 
2747
      // check MII IO signal and Busy and Nvalid bits
2748
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2749 169 mohor
      begin
2750 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is not HIGH Z - set higher clock divider");
2751
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2752
        begin
2753
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z anymore - 1. read");
2754
          fail = fail + 1;
2755
        end
2756
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2757
        begin
2758
          test_fail("Nvalid signal was not set while MII IO signal is not HIGH Z anymore - 1. read");
2759
          fail = fail + 1;
2760
        end
2761 169 mohor
      end
2762 181 mohor
      else // Busy bit should already be set to '1', due to reads from MII status register
2763 169 mohor
      begin
2764 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2765
        begin
2766
          test_fail("Busy signal should be set after scan, due to reads from MII status register");
2767
          fail = fail + 1;
2768
        end
2769
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2770
        begin
2771
          test_fail("Nvalid signal should be set after scan, due to reads from MII status register");
2772
          fail = fail + 1;
2773
        end
2774 169 mohor
      end
2775 181 mohor
 
2776
      // wait for serial bus to become active
2777 169 mohor
      wait(Mdio_IO !== 1'bz);
2778 181 mohor
      // count transfer bits
2779
      if (i)
2780 169 mohor
      begin
2781 181 mohor
        repeat(21) @(posedge Mdc_O);
2782 169 mohor
      end
2783 181 mohor
      else
2784 169 mohor
      begin
2785 181 mohor
        repeat(53) @(posedge Mdc_O);
2786 169 mohor
      end
2787 181 mohor
      // stop scan
2788
      #Tp mii_scan_finish; // finish scan operation
2789
 
2790
      // wait for next positive clock edge
2791
      repeat(10) @(posedge Mdc_O);
2792
      // read data from MII status register - Busy and Nvalid bits
2793
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2794
 
2795
      // check MII IO signal and Busy and Nvalid bits
2796
      if (Mdio_IO === 1'bz) // Mdio_IO should not be HIGH Z here - testbench selfcheck
2797 169 mohor
      begin
2798 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is HIGH Z - set higher clock divider");
2799
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2800 169 mohor
        begin
2801 181 mohor
          test_fail("Busy signal should be set while MII IO signal is not active anymore");
2802 169 mohor
          fail = fail + 1;
2803
        end
2804 181 mohor
        // Nvalid signal can be cleared here - it is still Testbench error
2805 169 mohor
      end
2806 181 mohor
      else // Busy bit should still be set to '1', Nvalid bit should still be set to '1'
2807 169 mohor
      begin
2808 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2809 169 mohor
        begin
2810 181 mohor
          test_fail("Busy signal should be set while MII IO signal not HIGH Z");
2811 169 mohor
          fail = fail + 1;
2812
        end
2813 181 mohor
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2814 169 mohor
        begin
2815 181 mohor
          test_fail("Nvalid signal should be set while MII IO signal not HIGH Z");
2816 169 mohor
          fail = fail + 1;
2817
        end
2818 181 mohor
      end
2819
 
2820
      // wait for next negative clock edge
2821
      @(negedge Mdc_O);
2822
      // read data from MII status register - Busy and Nvalid bits
2823
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2824
 
2825
      // check MII IO signal and Busy and Nvalid bits
2826
      if (Mdio_IO === 1'bz) // Mdio_IO should not be HIGH Z here - testbench selfcheck
2827
      begin
2828
        test_fail("Testbench error - read was to late, Mdio_IO is HIGH Z - set higher clock divider");
2829
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2830 169 mohor
        begin
2831 181 mohor
          test_fail("Busy signal should be set while MII IO signal is not active anymore");
2832
          fail = fail + 1;
2833 169 mohor
        end
2834 181 mohor
        // Nvalid signal can be cleared here - it is still Testbench error
2835 169 mohor
      end
2836 181 mohor
      else // Busy bit should still be set to '1', Nvalid bit should still be set to '1'
2837 169 mohor
      begin
2838 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2839 169 mohor
        begin
2840 181 mohor
          test_fail("Busy signal should be set while MII IO signal not HIGH Z");
2841
          fail = fail + 1;
2842 169 mohor
        end
2843 181 mohor
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2844 169 mohor
        begin
2845 181 mohor
          test_fail("Nvalid signal should be set while MII IO signal not HIGH Z");
2846
          fail = fail + 1;
2847 169 mohor
        end
2848
      end
2849 181 mohor
 
2850
      // wait for next negative clock edge
2851
      @(posedge Mdc_O);
2852
      // read data from MII status register - Busy and Nvalid bits
2853 169 mohor
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2854 181 mohor
 
2855
      // check MII IO signal and Busy and Nvalid bits
2856
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2857 169 mohor
      begin
2858 181 mohor
        test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2859
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2860 169 mohor
        begin
2861 181 mohor
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2862 169 mohor
          fail = fail + 1;
2863
        end
2864 181 mohor
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2865 169 mohor
        begin
2866 181 mohor
          test_fail("Nvalid signal was not set while MII IO signal is not HIGH Z");
2867 169 mohor
          fail = fail + 1;
2868
        end
2869
      end
2870 181 mohor
      else // Busy bit should still be set to '1', Nvalid bit can be set to '0'
2871 169 mohor
      begin
2872 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2873 169 mohor
        begin
2874 181 mohor
          test_fail("Busy signal should be set after MII IO signal become HIGH Z");
2875
          fail = fail + 1;
2876 169 mohor
        end
2877 181 mohor
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2878 169 mohor
        begin
2879 181 mohor
          i2 = 1; // check finished
2880 169 mohor
        end
2881 181 mohor
        else
2882 169 mohor
        begin
2883 181 mohor
          i2 = 0; // check must continue
2884 169 mohor
        end
2885
      end
2886 181 mohor
 
2887
      // wait for Busy to become inactive
2888
      i1 = 0;
2889
      while ((i1 <= 2) || (i2 == 0))
2890
      begin
2891
        // wait for next positive clock edge
2892
        @(posedge Mdc_O);
2893
        // read data from MII status register - Busy and Nvalid bits
2894
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2895
 
2896
        // check MII IO signal and Busy and Nvalid bits
2897
        if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2898 169 mohor
        begin
2899 181 mohor
          test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2900
          if (i1 <= 2)
2901 169 mohor
          begin
2902 181 mohor
            if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2903 169 mohor
            begin
2904 181 mohor
              test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2905 169 mohor
              fail = fail + 1;
2906
            end
2907
          end
2908 181 mohor
          if (i2 == 0)
2909 169 mohor
          begin
2910 181 mohor
            if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2911 169 mohor
            begin
2912 181 mohor
              test_fail("Nvalid signal was not set while MII IO signal is not HIGH Z");
2913 169 mohor
              fail = fail + 1;
2914
            end
2915
          end
2916
        end
2917 181 mohor
        else // wait for Busy bit to be set to '0'
2918 169 mohor
        begin
2919 181 mohor
          if (i1 <= 2)
2920 169 mohor
          begin
2921 181 mohor
            if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2922 169 mohor
            begin
2923 181 mohor
              i1 = 3; // end of Busy checking
2924 169 mohor
            end
2925 181 mohor
            else
2926 169 mohor
            begin
2927 181 mohor
              if (i1 == 2)
2928 169 mohor
              begin
2929 181 mohor
                test_fail("Busy signal should be cleared after 2 periods after MII IO signal become HIGH Z");
2930 169 mohor
                fail = fail + 1;
2931
              end
2932 181 mohor
              #Tp i1 = i1 + 1;
2933 169 mohor
            end
2934 181 mohor
          end
2935
          if (i2 == 0)
2936
          begin
2937
            if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2938 169 mohor
            begin
2939 181 mohor
              i2 = 1;
2940 169 mohor
            end
2941 181 mohor
            else
2942
            begin
2943
              test_fail("Nvalid signal should be cleared after MII IO signal become HIGH Z");
2944
              fail = fail + 1;
2945
            end
2946 169 mohor
          end
2947
        end
2948 181 mohor
      end
2949
    end
2950
    // set PHY to normal mode
2951
    #Tp eth_phy.preamble_suppresed(0);
2952
    // MII mode register
2953
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2954
    if(fail == 0)
2955
      test_ok;
2956
    else
2957
      fail = 0;
2958
  end
2959
 
2960
 
2961
  ////////////////////////////////////////////////////////////////////
2962
  ////                                                            ////
2963
  ////  Test scan status from phy with detecting link-fail bit    ////
2964
  ////  (with and without preamble)                               ////
2965
  ////                                                            ////
2966
  ////////////////////////////////////////////////////////////////////
2967
  if (test_num == 14) // 
2968
  begin
2969 194 tadej
    // TEST 14: SCAN STATUS FROM PHY WITH DETECTING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )
2970
    test_name   = "TEST 14: SCAN STATUS FROM PHY WITH DETECTING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )";
2971
    `TIME; $display("  TEST 14: SCAN STATUS FROM PHY WITH DETECTING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )");
2972 181 mohor
 
2973
    reset_mii; // reset MII
2974
    // set link up, if it wasn't due to previous tests, since there weren't PHY registers
2975
    #Tp eth_phy.link_up_down(1);
2976
    // set MII speed
2977
    clk_div = 6;
2978
    mii_set_clk_div(clk_div[7:0]);
2979
    // set address
2980
    reg_addr = 5'h1; // status register
2981
    phy_addr = 5'h1; // correct PHY address
2982
 
2983
    // read request
2984
    #Tp mii_read_req(phy_addr, reg_addr);
2985
    check_mii_busy; // wait for read to finish
2986
    // read data from PHY status register - remember LINK-UP status
2987
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2988
 
2989
    for (i = 0; i <= 1; i = i + 1)
2990
    begin
2991
      #Tp eth_phy.preamble_suppresed(i);
2992
      // MII mode register
2993
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}) | (`ETH_MIIMODER_CLKDIV & clk_div),
2994
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2995
      if (i)
2996
      begin
2997
        // change saved data when preamble is suppressed
2998
        #Tp tmp_data = tmp_data | 16'h0040; // put bit 6 to ONE
2999
      end
3000
 
3001
      // scan request
3002
      #Tp mii_scan_req(phy_addr, reg_addr);
3003
      check_mii_scan_valid; // wait for scan to make first data valid
3004
 
3005 169 mohor
      fork
3006 181 mohor
      begin
3007 169 mohor
        repeat(2) @(posedge Mdc_O);
3008
        // read data from PHY status register
3009
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3010 181 mohor
        if (phy_data !== tmp_data)
3011 169 mohor
        begin
3012 181 mohor
          test_fail("Data was not correctly scaned from status register");
3013 169 mohor
          fail = fail + 1;
3014
        end
3015
        // read data from MII status register
3016
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3017 181 mohor
        if (phy_data[0] !== 1'b0)
3018 169 mohor
        begin
3019 181 mohor
          test_fail("Link FAIL bit was set in the MII status register");
3020 169 mohor
          fail = fail + 1;
3021
        end
3022
      end
3023
      begin
3024 181 mohor
      // Completely check second scan
3025 169 mohor
        #Tp cnt = 0;
3026
        // wait for serial bus to become active - second scan
3027
        wait(Mdio_IO !== 1'bz);
3028
        // count transfer length
3029 181 mohor
        while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i == 0)) || ((cnt == 15) && (i == 1)) )
3030 169 mohor
        begin
3031
          @(posedge Mdc_O);
3032
          #Tp cnt = cnt + 1;
3033
        end
3034
        // check transfer length
3035 181 mohor
        if (i) // without preamble
3036 169 mohor
        begin
3037
          if (cnt != 33) // at this value Mdio_IO is HIGH Z
3038
          begin
3039 181 mohor
            test_fail("Second scan request did not proceed correctly");
3040 169 mohor
            fail = fail + 1;
3041
          end
3042
        end
3043
        else // with preamble
3044
        begin
3045
          if (cnt != 65) // at this value Mdio_IO is HIGH Z
3046
          begin
3047 181 mohor
            test_fail("Second scan request did not proceed correctly");
3048 169 mohor
            fail = fail + 1;
3049
          end
3050
        end
3051
      end
3052
      join
3053 181 mohor
      // check third to fifth scans
3054
      for (i3 = 0; i3 <= 2; i3 = i3 + 1)
3055
      begin
3056
        fork
3057 169 mohor
        begin
3058
          repeat(2) @(posedge Mdc_O);
3059
          // read data from PHY status register
3060
          #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3061 181 mohor
          if (phy_data !== tmp_data)
3062 169 mohor
          begin
3063 181 mohor
            test_fail("Data was not correctly scaned from status register");
3064
            fail = fail + 1;
3065 169 mohor
          end
3066
          // read data from MII status register
3067
          #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3068 181 mohor
          if (phy_data[0] !== 1'b0)
3069 169 mohor
          begin
3070 181 mohor
            test_fail("Link FAIL bit was set in the MII status register");
3071
            fail = fail + 1;
3072 169 mohor
          end
3073 181 mohor
          if (i3 == 2) // after fourth scan read
3074 169 mohor
          begin
3075 181 mohor
            @(posedge Mdc_O);
3076
            // change saved data
3077
            #Tp tmp_data = tmp_data & 16'hFFFB; // put bit 3 to ZERO
3078
            // set link down
3079
            #Tp eth_phy.link_up_down(0);
3080 169 mohor
          end
3081
        end
3082
        begin
3083 181 mohor
        // Completely check scans
3084
          #Tp cnt = 0;
3085
          // wait for serial bus to become active - second scan
3086
          wait(Mdio_IO !== 1'bz);
3087
          // count transfer length
3088
          while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i == 0)) || ((cnt == 15) && (i == 1)) )
3089 169 mohor
          begin
3090 181 mohor
            @(posedge Mdc_O);
3091
            #Tp cnt = cnt + 1;
3092
          end
3093
          // check transfer length
3094
          if (i) // without preamble
3095
          begin
3096
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
3097 169 mohor
            begin
3098 181 mohor
              test_fail("Fifth scan request did not proceed correctly");
3099
              fail = fail + 1;
3100 169 mohor
            end
3101 181 mohor
          end
3102
          else // with preamble
3103
          begin
3104
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
3105 169 mohor
            begin
3106 181 mohor
              test_fail("Fifth scan request did not proceed correctly");
3107
              fail = fail + 1;
3108 169 mohor
            end
3109
          end
3110
        end
3111 181 mohor
        join
3112
      end
3113
 
3114
      fork
3115
      begin
3116
        repeat(2) @(posedge Mdc_O);
3117
        // read data from PHY status register
3118
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3119
        if (phy_data !== tmp_data)
3120
        begin
3121
          test_fail("Data was not correctly scaned from status register");
3122
          fail = fail + 1;
3123
        end
3124
        // read data from MII status register
3125
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3126
        if (phy_data[0] === 1'b0)
3127
        begin
3128
          test_fail("Link FAIL bit was not set in the MII status register");
3129
          fail = fail + 1;
3130
        end
3131
        // wait to see if data stayed latched
3132
        repeat(4) @(posedge Mdc_O);
3133
        // read data from PHY status register
3134
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3135
        if (phy_data !== tmp_data)
3136
        begin
3137
          test_fail("Data was not latched correctly in status register");
3138
          fail = fail + 1;
3139
        end
3140
        // read data from MII status register
3141
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3142
        if (phy_data[0] === 1'b0)
3143
        begin
3144
          test_fail("Link FAIL bit was not set in the MII status register");
3145
          fail = fail + 1;
3146
        end
3147
        // change saved data
3148
        #Tp tmp_data = tmp_data | 16'h0004; // put bit 2 to ONE
3149
        // set link up
3150
        #Tp eth_phy.link_up_down(1);
3151
      end
3152
      begin
3153
      // Wait for sixth scan
3154
        // wait for serial bus to become active - sixth scan
3155
        wait(Mdio_IO !== 1'bz);
3156
        // wait for serial bus to become inactive - turn-around cycle in sixth scan
3157
        wait(Mdio_IO === 1'bz);
3158
        // wait for serial bus to become active - end of turn-around cycle in sixth scan
3159
        wait(Mdio_IO !== 1'bz);
3160
        // wait for serial bus to become inactive - end of sixth scan
3161
        wait(Mdio_IO === 1'bz);
3162
      end
3163 169 mohor
      join
3164 181 mohor
 
3165
      @(posedge Mdc_O);
3166 169 mohor
      // read data from PHY status register
3167
      #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3168
      if (phy_data !== tmp_data)
3169
      begin
3170 181 mohor
        test_fail("Data was not correctly scaned from status register");
3171 169 mohor
        fail = fail + 1;
3172
      end
3173
      // read data from MII status register
3174
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3175
      if (phy_data[0] !== 1'b0)
3176
      begin
3177
        test_fail("Link FAIL bit was set in the MII status register");
3178
        fail = fail + 1;
3179
      end
3180 181 mohor
      // wait to see if data stayed latched
3181
      repeat(4) @(posedge Mdc_O);
3182
      // read data from PHY status register
3183
      #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3184
      if (phy_data !== tmp_data)
3185 169 mohor
      begin
3186 181 mohor
        test_fail("Data was not correctly scaned from status register");
3187
        fail = fail + 1;
3188 169 mohor
      end
3189 181 mohor
      // read data from MII status register
3190
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3191
      if (phy_data[0] !== 1'b0)
3192 169 mohor
      begin
3193 181 mohor
        test_fail("Link FAIL bit was set in the MII status register");
3194
        fail = fail + 1;
3195 169 mohor
      end
3196 181 mohor
 
3197
      // STOP SCAN
3198
      #Tp mii_scan_finish; // finish scan operation
3199
      #Tp check_mii_busy; // wait for scan to finish
3200 169 mohor
    end
3201 181 mohor
    // set PHY to normal mode
3202
    #Tp eth_phy.preamble_suppresed(0);
3203
    // MII mode register
3204
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3205
    if(fail == 0)
3206
      test_ok;
3207
    else
3208
      fail = 0;
3209 169 mohor
  end
3210
 
3211
 
3212 181 mohor
  ////////////////////////////////////////////////////////////////////
3213
  ////                                                            ////
3214
  ////  Test scan status from phy with sliding link-fail bit      ////
3215
  ////  (with and without preamble)                               ////
3216
  ////                                                            ////
3217
  ////////////////////////////////////////////////////////////////////
3218
  if (test_num == 15) // 
3219 169 mohor
  begin
3220 194 tadej
    // TEST 15: SCAN STATUS FROM PHY WITH SLIDING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )
3221
    test_name   = "TEST 15: SCAN STATUS FROM PHY WITH SLIDING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )";
3222
    `TIME; $display("  TEST 15: SCAN STATUS FROM PHY WITH SLIDING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )");
3223 181 mohor
 
3224
    // set address
3225
    reg_addr = 5'h1; // status register
3226
    phy_addr = 5'h1; // correct PHY address
3227
 
3228
    // read request
3229
    #Tp mii_read_req(phy_addr, reg_addr);
3230
    check_mii_busy; // wait for read to finish
3231
    // read data from PHY status register - remember LINK-UP status
3232
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3233
 
3234
    for (i2 = 0; i2 <= 1; i2 = i2 + 1) // choose preamble or not
3235 169 mohor
    begin
3236 181 mohor
      #Tp eth_phy.preamble_suppresed(i2);
3237
      // MII mode register
3238
      #Tp wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i2, 8'h0}), 4'hF, 1, wbm_init_waits,
3239
                    wbm_subseq_waits);
3240
      if (i2)
3241 169 mohor
      begin
3242 181 mohor
        // change saved data when preamble is suppressed
3243
        #Tp tmp_data = tmp_data | 16'h0040; // put bit 6 to ONE
3244
      end
3245
 
3246
      i = 0;
3247
      while (i < 80) // delay for sliding of LinkFail bit
3248
      begin
3249
        // first there are two scans
3250
        #Tp cnt = 0;
3251 169 mohor
        // scan request
3252
        #Tp mii_scan_req(phy_addr, reg_addr);
3253 181 mohor
        #Tp check_mii_scan_valid; // wait for scan to make first data valid
3254
 
3255
        // check second scan
3256 169 mohor
        fork
3257 181 mohor
        begin
3258
          repeat(4) @(posedge Mdc_O);
3259
          // read data from PHY status register
3260
          #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3261
          if (phy_data !== tmp_data)
3262 169 mohor
          begin
3263 181 mohor
            test_fail("Second data was not correctly scaned from status register");
3264
            fail = fail + 1;
3265 169 mohor
          end
3266 181 mohor
          // read data from MII status register
3267
          #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3268
          if (phy_data[0] !== 1'b0)
3269
          begin
3270
            test_fail("Link FAIL bit was set in the MII status register");
3271
            fail = fail + 1;
3272
          end
3273
        end
3274
        begin
3275
        // Completely check scan
3276
          #Tp cnt = 0;
3277
          // wait for serial bus to become active - second scan
3278
          wait(Mdio_IO !== 1'bz);
3279
          // count transfer length
3280
          while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3281
          begin
3282
            @(posedge Mdc_O);
3283
            #Tp cnt = cnt + 1;
3284
          end
3285
          // check transfer length
3286
          if (i2) // without preamble
3287
          begin
3288
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
3289 169 mohor
            begin
3290 181 mohor
              test_fail("Second scan request did not proceed correctly");
3291
              fail = fail + 1;
3292 169 mohor
            end
3293 181 mohor
          end
3294
          else // with preamble
3295
          begin
3296
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
3297
            begin
3298
              test_fail("Second scan request did not proceed correctly");
3299
              fail = fail + 1;
3300
            end
3301
          end
3302
        end
3303
        join
3304
        // reset counter 
3305
        #Tp cnt = 0;
3306
        // SLIDING LINK DOWN and CHECK
3307
        fork
3308
          begin
3309
          // set link down
3310
            repeat(i) @(posedge Mdc_O);
3311
            // set link down
3312
            #Tp eth_phy.link_up_down(0);
3313
          end
3314
          begin
3315
          // check data in MII registers after each scan in this fork statement
3316 169 mohor
            if (i2) // without preamble
3317 181 mohor
              wait (cnt == 32);
3318
            else // with preamble
3319
              wait (cnt == 64);
3320
            repeat(3) @(posedge Mdc_O);
3321
            // read data from PHY status register
3322
            #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3323
            if ( ((i < 49) && !i2) || ((i < 17) && i2) )
3324 169 mohor
            begin
3325 181 mohor
              if (phy_data !== (tmp_data & 16'hFFFB)) // bit 3 is ZERO
3326 169 mohor
              begin
3327 181 mohor
                test_fail("Third data was not correctly scaned from status register");
3328 169 mohor
                fail = fail + 1;
3329
              end
3330
            end
3331 181 mohor
            else
3332 169 mohor
            begin
3333 181 mohor
              if (phy_data !== tmp_data)
3334 169 mohor
              begin
3335 181 mohor
                test_fail("Third data was not correctly scaned from status register");
3336 169 mohor
                fail = fail + 1;
3337
              end
3338
            end
3339 181 mohor
            // read data from MII status register
3340
            #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3341
            if ( ((i < 49) && !i2) || ((i < 17) && i2) )
3342 169 mohor
            begin
3343 181 mohor
              if (phy_data[0] === 1'b0)
3344
              begin
3345
                test_fail("Link FAIL bit was not set in the MII status register");
3346
                fail = fail + 1;
3347
              end
3348 169 mohor
            end
3349 181 mohor
            else
3350 169 mohor
            begin
3351 181 mohor
              if (phy_data[0] !== 1'b0)
3352 169 mohor
              begin
3353 181 mohor
                test_fail("Link FAIL bit was set in the MII status register");
3354 169 mohor
                fail = fail + 1;
3355
              end
3356
            end
3357 181 mohor
          end
3358
          begin
3359
          // check length
3360
            for (i3 = 0; i3 <= 1; i3 = i3 + 1) // two scans
3361 169 mohor
            begin
3362 181 mohor
              #Tp cnt = 0;
3363
              // wait for serial bus to become active if there is more than one scan
3364
              wait(Mdio_IO !== 1'bz);
3365
              // count transfer length
3366
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3367 169 mohor
              begin
3368 181 mohor
                @(posedge Mdc_O);
3369
                #Tp cnt = cnt + 1;
3370 169 mohor
              end
3371 181 mohor
              // check transfer length
3372
              if (i2) // without preamble
3373
              begin
3374
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3375
                begin
3376
                  test_fail("3. or 4. scan request did not proceed correctly, while SCAN STOP was written");
3377
                  fail = fail + 1;
3378
                end
3379
              end
3380
              else // with preamble
3381
              begin
3382
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3383
                begin
3384
                  test_fail("3. or 4. scan request did not proceed correctly, while SCAN STOP was written");
3385
                  fail = fail + 1;
3386
                end
3387
              end
3388 169 mohor
            end
3389
          end
3390
        join
3391 181 mohor
        // reset counter
3392
        #Tp cnt = 0;
3393
        // check fifth scan and data from fourth scan
3394
        fork
3395 169 mohor
        begin
3396 181 mohor
          repeat(2) @(posedge Mdc_O);
3397
          // read data from PHY status register
3398
          #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3399
          if (phy_data !== (tmp_data & 16'hFFFB)) // bit 3 is ZERO
3400 169 mohor
          begin
3401 181 mohor
            test_fail("4. data was not correctly scaned from status register");
3402
            fail = fail + 1;
3403 169 mohor
          end
3404 181 mohor
          // read data from MII status register
3405
          #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3406
          if (phy_data[0] === 1'b0)
3407 169 mohor
          begin
3408 181 mohor
            test_fail("Link FAIL bit was not set in the MII status register");
3409 169 mohor
            fail = fail + 1;
3410
          end
3411
        end
3412
        begin
3413 181 mohor
        // Completely check intermediate scan
3414
          #Tp cnt = 0;
3415
          // wait for serial bus to become active - second scan
3416 169 mohor
          wait(Mdio_IO !== 1'bz);
3417
          // count transfer length
3418
          while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3419
          begin
3420
            @(posedge Mdc_O);
3421
            #Tp cnt = cnt + 1;
3422
          end
3423 181 mohor
          // check transfer length
3424
          if (i2) // without preamble
3425 169 mohor
          begin
3426 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
3427
            begin
3428
              test_fail("Fifth scan request did not proceed correctly");
3429
              fail = fail + 1;
3430
            end
3431 169 mohor
          end
3432 181 mohor
          else // with preamble
3433
          begin
3434
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
3435
            begin
3436
              test_fail("Fifth scan request did not proceed correctly");
3437
              fail = fail + 1;
3438
            end
3439
          end
3440 169 mohor
        end
3441 181 mohor
        join
3442
        // reset counter 
3443
        #Tp cnt = 0;
3444
        // SLIDING LINK UP and CHECK
3445
        fork
3446 169 mohor
          begin
3447 181 mohor
          // set link up
3448
            repeat(i) @(posedge Mdc_O);
3449
            // set link up
3450
            #Tp eth_phy.link_up_down(1);
3451 169 mohor
          end
3452 181 mohor
          begin
3453
          // check data in MII registers after each scan in this fork statement
3454
            repeat(2) @(posedge Mdc_O);
3455
            if (i2) // without preamble
3456
              wait (cnt == 32);
3457
            else // with preamble
3458
              wait (cnt == 64);
3459
            repeat(3) @(posedge Mdc_O);
3460
            // read data from PHY status register
3461
            #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3462
            if ( ((i < 49) && !i2) || ((i < 17) && i2) )
3463
            begin
3464
              if (phy_data !== tmp_data)
3465
              begin
3466
                test_fail("6. data was not correctly scaned from status register");
3467
                fail = fail + 1;
3468
              end
3469
            end
3470
            else
3471
            begin
3472
              if (phy_data !== (tmp_data & 16'hFFFB)) // bit 3 is ZERO
3473
              begin
3474
                test_fail("6. data was not correctly scaned from status register");
3475
                fail = fail + 1;
3476
              end
3477
            end
3478
            // read data from MII status register
3479
            #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3480
            if ( ((i < 49) && !i2) || ((i < 17) && i2) )
3481
            begin
3482
              if (phy_data[0] !== 1'b0)
3483
              begin
3484
                test_fail("Link FAIL bit was set in the MII status register");
3485
                fail = fail + 1;
3486
              end
3487
            end
3488
            else
3489
            begin
3490
              if (phy_data[0] === 1'b0)
3491
              begin
3492
                test_fail("Link FAIL bit was not set in the MII status register");
3493
                fail = fail + 1;
3494
              end
3495
            end
3496
          end
3497
          begin
3498
          // check length
3499
            for (i3 = 0; i3 <= 1; i3 = i3 + 1) // two scans
3500
            begin
3501
              #Tp cnt = 0;
3502
              // wait for serial bus to become active if there is more than one scan
3503
              wait(Mdio_IO !== 1'bz);
3504
              // count transfer length
3505
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3506
              begin
3507
                @(posedge Mdc_O);
3508
                #Tp cnt = cnt + 1;
3509
              end
3510
              // check transfer length
3511
              if (i2) // without preamble
3512
              begin
3513
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3514
                begin
3515
                  test_fail("Scan request did not proceed correctly, while SCAN STOP was written");
3516
                  fail = fail + 1;
3517
                end
3518
              end
3519
              else // with preamble
3520
              begin
3521
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3522
                begin
3523
                  test_fail("Scan request did not proceed correctly, while SCAN STOP was written");
3524
                  fail = fail + 1;
3525
                end
3526
              end
3527
            end
3528
          end
3529
        join
3530
        // check last scan 
3531
        repeat(4) @(posedge Mdc_O);
3532
        // read data from PHY status register
3533
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3534
        if (phy_data !== tmp_data)
3535
        begin
3536
          test_fail("7. data was not correctly scaned from status register");
3537
          fail = fail + 1;
3538 169 mohor
        end
3539 181 mohor
        // read data from MII status register
3540
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3541
        if (phy_data[0] !== 1'b0)
3542
        begin
3543
          test_fail("Link FAIL bit was set in the MII status register");
3544
          fail = fail + 1;
3545
        end
3546
 
3547
        #Tp mii_scan_finish; // finish scan operation
3548
        #Tp check_mii_busy; // wait for scan to finish
3549
        #Tp;
3550
        // set delay of writing the command
3551
        if (i2) // without preamble
3552
        begin
3553
          case(i)
3554
            0,  1,  2,  3,  4:  i = i + 1;
3555
            13, 14, 15, 16, 17,
3556
            18, 19, 20, 21, 22,
3557
            23, 24, 25, 26, 27,
3558
            28, 29, 30, 31, 32,
3559
            33, 34, 35:         i = i + 1;
3560
            36:                 i = 80;
3561
            default:            i = 13;
3562
          endcase
3563
        end
3564 169 mohor
        else // with preamble
3565
        begin
3566 181 mohor
          case(i)
3567
            0,  1,  2,  3,  4:  i = i + 1;
3568
            45, 46, 47, 48, 49,
3569
            50, 51, 52, 53, 54,
3570
            55, 56, 57, 58, 59,
3571
            60, 61, 62, 63, 64,
3572
            65, 66, 67:         i = i + 1;
3573
            68:                 i = 80;
3574
            default:            i = 45;
3575
          endcase
3576 169 mohor
        end
3577 181 mohor
        @(posedge wb_clk);
3578
        #Tp;
3579 169 mohor
      end
3580
    end
3581 181 mohor
    // set PHY to normal mode
3582
    #Tp eth_phy.preamble_suppresed(0);
3583
    // MII mode register
3584
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3585
    if(fail == 0)
3586
      test_ok;
3587
    else
3588
      fail = 0;
3589 169 mohor
  end
3590
 
3591
 
3592 181 mohor
  ////////////////////////////////////////////////////////////////////
3593
  ////                                                            ////
3594
  ////  Test sliding stop scan command immediately after scan     ////
3595
  ////  request (with and without preamble)                       ////
3596
  ////                                                            ////
3597
  ////////////////////////////////////////////////////////////////////
3598
  if (test_num == 16) // 
3599 116 mohor
  begin
3600 194 tadej
    // TEST 16: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER SCAN REQUEST ( WITH AND WITHOUT PREAMBLE )
3601
    test_name = "TEST 16: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER SCAN REQUEST ( WITH AND WITHOUT PREAMBLE )";
3602 181 mohor
    `TIME;
3603 194 tadej
    $display("  TEST 16: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER SCAN REQUEST ( WITH AND WITHOUT PREAMBLE )");
3604 181 mohor
 
3605
    for (i2 = 0; i2 <= 1; i2 = i2 + 1) // choose preamble or not
3606 169 mohor
    begin
3607 181 mohor
      #Tp eth_phy.preamble_suppresed(i2);
3608
      // MII mode register
3609
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i2, 8'h0}), 4'hF, 1, wbm_init_waits,
3610
                wbm_subseq_waits);
3611
      i = 0;
3612
      cnt = 0;
3613
      while (i < 80) // delay for sliding of writing a STOP SCAN command
3614 169 mohor
      begin
3615 181 mohor
        for (i3 = 0; i3 <= 1; i3 = i3 + 1) // choose read or write after scan will be finished
3616 169 mohor
        begin
3617 181 mohor
          // set address
3618
          reg_addr = 5'h0; // control register
3619
          phy_addr = 5'h1; // correct PHY address
3620
          cnt = 0;
3621
          // scan request
3622
          #Tp mii_scan_req(phy_addr, reg_addr);
3623
          fork
3624
            begin
3625
              repeat(i) @(posedge Mdc_O);
3626
              // write command 0x0 into MII command register
3627
              // MII command written while scan in progress
3628
              wbm_write(`ETH_MIICOMMAND, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3629
              @(posedge wb_clk);
3630
              #Tp check_mii_busy; // wait for scan to finish
3631
              @(posedge wb_clk);
3632
              disable check;
3633
            end
3634
            begin: check
3635
              // wait for serial bus to become active
3636
              wait(Mdio_IO !== 1'bz);
3637
              // count transfer length
3638
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3639
              begin
3640
                @(posedge Mdc_O);
3641
                #Tp cnt = cnt + 1;
3642
              end
3643
              // check transfer length
3644
              if (i2) // without preamble
3645
              begin
3646
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3647
                begin
3648
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3649
                  fail = fail + 1;
3650
                end
3651
              end
3652
              else // with preamble
3653
              begin
3654
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3655
                begin
3656
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3657
                  fail = fail + 1;
3658
                end
3659
              end
3660
              cnt = 0;
3661
              // wait for serial bus to become active if there is more than one scan
3662
              wait(Mdio_IO !== 1'bz);
3663
              // count transfer length
3664
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3665
              begin
3666
                @(posedge Mdc_O);
3667
                #Tp cnt = cnt + 1;
3668
              end
3669
              // check transfer length
3670
              if (i2) // without preamble
3671
              begin
3672
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3673
                begin
3674
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3675
                  fail = fail + 1;
3676
                end
3677
              end
3678
              else // with preamble
3679
              begin
3680
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3681
                begin
3682
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3683
                  fail = fail + 1;
3684
                end
3685
              end
3686
            end
3687
          join
3688
          // check the BUSY signal to see if the bus is still IDLE
3689
          for (i1 = 0; i1 < 8; i1 = i1 + 1)
3690
            check_mii_busy; // wait for bus to become idle
3691
 
3692
          // try normal write or read after scan was finished
3693
          phy_data = {8'h7D, (i[7:0] + 1)};
3694
          cnt = 0;
3695
          if (i3 == 0) // write after scan
3696 169 mohor
          begin
3697 181 mohor
            // write request
3698
            #Tp mii_write_req(phy_addr, reg_addr, phy_data);
3699
            // wait for serial bus to become active
3700
            wait(Mdio_IO !== 1'bz);
3701
            // count transfer length
3702
            while(Mdio_IO !== 1'bz)
3703
            begin
3704
              @(posedge Mdc_O);
3705
              #Tp cnt = cnt + 1;
3706
            end
3707 169 mohor
            @(posedge Mdc_O);
3708 181 mohor
            // read request
3709
            #Tp mii_read_req(phy_addr, reg_addr);
3710
            check_mii_busy; // wait for read to finish
3711
            // read and check data
3712
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3713
            if (phy_data !== tmp_data)
3714 169 mohor
            begin
3715 181 mohor
              test_fail("Data was not correctly written into OR read from PHY register - control register");
3716 169 mohor
              fail = fail + 1;
3717
            end
3718
          end
3719 181 mohor
          else // read after scan
3720 169 mohor
          begin
3721 181 mohor
            // read request
3722
            #Tp mii_read_req(phy_addr, reg_addr);
3723
            // wait for serial bus to become active
3724
            wait(Mdio_IO !== 1'bz);
3725
            // count transfer length
3726
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3727 169 mohor
            begin
3728 181 mohor
              @(posedge Mdc_O);
3729
              #Tp cnt = cnt + 1;
3730
            end
3731
            @(posedge Mdc_O);
3732
            check_mii_busy; // wait for read to finish
3733
            // read and check data
3734
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3735
            if (phy_data !== tmp_data)
3736
            begin
3737
              test_fail("Data was not correctly written into OR read from PHY register - control register");
3738 169 mohor
              fail = fail + 1;
3739
            end
3740
          end
3741 181 mohor
          // check if transfer was a proper length
3742 169 mohor
          if (i2) // without preamble
3743
          begin
3744
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
3745
            begin
3746 181 mohor
              test_fail("New request did not proceed correctly, after scan request");
3747 169 mohor
              fail = fail + 1;
3748
            end
3749
          end
3750
          else // with preamble
3751
          begin
3752
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
3753
            begin
3754 181 mohor
              test_fail("New request did not proceed correctly, after scan request");
3755 169 mohor
              fail = fail + 1;
3756
            end
3757
          end
3758
        end
3759 181 mohor
        #Tp;
3760
        // set delay of writing the command
3761
        if (i2) // without preamble
3762
        begin
3763
          case(i)
3764
            0, 1:               i = i + 1;
3765
            18, 19, 20, 21, 22,
3766
            23, 24, 25, 26, 27,
3767
            28, 29, 30, 31, 32,
3768
            33, 34, 35:         i = i + 1;
3769
            36:                 i = 80;
3770
            default:            i = 18;
3771
          endcase
3772
        end
3773
        else // with preamble
3774
        begin
3775
          case(i)
3776
            0, 1:               i = i + 1;
3777
            50, 51, 52, 53, 54,
3778
            55, 56, 57, 58, 59,
3779
            60, 61, 62, 63, 64,
3780
            65, 66, 67:         i = i + 1;
3781
            68:                 i = 80;
3782
            default:            i = 50;
3783
          endcase
3784
        end
3785
        @(posedge wb_clk);
3786
      end
3787
    end
3788
    // set PHY to normal mode
3789
    #Tp eth_phy.preamble_suppresed(0);
3790
    // MII mode register
3791
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3792
    if(fail == 0)
3793
      test_ok;
3794
    else
3795
      fail = 0;
3796
  end
3797 169 mohor
 
3798 181 mohor
 
3799
  ////////////////////////////////////////////////////////////////////
3800
  ////                                                            ////
3801
  ////  Test sliding stop scan command after 2. scan (with and    ////
3802
  ////  without preamble)                                         ////
3803
  ////                                                            ////
3804
  ////////////////////////////////////////////////////////////////////
3805
  if (test_num == 17) // 
3806
  begin
3807 194 tadej
    // TEST 17: SLIDING STOP SCAN COMMAND AFTER 2. SCAN ( WITH AND WITHOUT PREAMBLE )
3808
    test_name = "TEST 17: SLIDING STOP SCAN COMMAND AFTER 2. SCAN ( WITH AND WITHOUT PREAMBLE )";
3809
    `TIME; $display("  TEST 17: SLIDING STOP SCAN COMMAND AFTER 2. SCAN ( WITH AND WITHOUT PREAMBLE )");
3810 181 mohor
 
3811
    for (i2 = 0; i2 <= 1; i2 = i2 + 1) // choose preamble or not
3812
    begin
3813
      #Tp eth_phy.preamble_suppresed(i2);
3814
      // MII mode register
3815
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i2, 8'h0}), 4'hF, 1, wbm_init_waits,
3816
                wbm_subseq_waits);
3817
 
3818
      i = 0;
3819
      cnt = 0;
3820
      while (i < 80) // delay for sliding of writing a STOP SCAN command
3821
      begin
3822
        for (i3 = 0; i3 <= 1; i3 = i3 + 1) // choose read or write after scan will be finished
3823
        begin
3824
          // first there are two scans
3825
          // set address
3826
          reg_addr = 5'h0; // control register
3827
          phy_addr = 5'h1; // correct PHY address
3828
          cnt = 0;
3829
          // scan request
3830
          #Tp mii_scan_req(phy_addr, reg_addr);
3831
          // wait and check first 2 scans
3832 169 mohor
          begin
3833
            // wait for serial bus to become active
3834
            wait(Mdio_IO !== 1'bz);
3835
            // count transfer length
3836
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3837
            begin
3838
              @(posedge Mdc_O);
3839
              #Tp cnt = cnt + 1;
3840
            end
3841
            // check transfer length
3842
            if (i2) // without preamble
3843
            begin
3844
              if (cnt != 33) // at this value Mdio_IO is HIGH Z
3845
              begin
3846
                test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3847
                fail = fail + 1;
3848
              end
3849
            end
3850
            else // with preamble
3851
            begin
3852
              if (cnt != 65) // at this value Mdio_IO is HIGH Z
3853
              begin
3854
                test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3855
                fail = fail + 1;
3856
              end
3857
            end
3858
            cnt = 0;
3859
            // wait for serial bus to become active if there is more than one scan
3860
            wait(Mdio_IO !== 1'bz);
3861
            // count transfer length
3862
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3863
            begin
3864
              @(posedge Mdc_O);
3865
              #Tp cnt = cnt + 1;
3866
            end
3867
            // check transfer length
3868
            if (i2) // without preamble
3869
            begin
3870
              if (cnt != 33) // at this value Mdio_IO is HIGH Z
3871
              begin
3872
                test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3873
                fail = fail + 1;
3874
              end
3875
            end
3876
            else // with preamble
3877
            begin
3878
              if (cnt != 65) // at this value Mdio_IO is HIGH Z
3879
              begin
3880
                test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3881
                fail = fail + 1;
3882
              end
3883
            end
3884
          end
3885
 
3886 181 mohor
          // reset counter 
3887
          cnt = 0;
3888
          fork
3889
            begin
3890
              repeat(i) @(posedge Mdc_O);
3891
              // write command 0x0 into MII command register
3892
              // MII command written while scan in progress
3893
              wbm_write(`ETH_MIICOMMAND, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3894
              @(posedge wb_clk);
3895
              #Tp check_mii_busy; // wait for scan to finish
3896
              @(posedge wb_clk);
3897
              disable check_3;
3898
            end
3899
            begin: check_3
3900
              // wait for serial bus to become active
3901
              wait(Mdio_IO !== 1'bz);
3902
              // count transfer length
3903
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3904
              begin
3905
                @(posedge Mdc_O);
3906
                #Tp cnt = cnt + 1;
3907
              end
3908
              // check transfer length
3909
              if (i2) // without preamble
3910
              begin
3911
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3912
                begin
3913
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3914
                  fail = fail + 1;
3915
                end
3916
              end
3917
              else // with preamble
3918
              begin
3919
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3920
                begin
3921
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3922
                  fail = fail + 1;
3923
                end
3924
              end
3925
              cnt = 0;
3926
              // wait for serial bus to become active if there is more than one scan
3927
              wait(Mdio_IO !== 1'bz);
3928
              // count transfer length
3929
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3930
              begin
3931
                @(posedge Mdc_O);
3932
                #Tp cnt = cnt + 1;
3933
              end
3934
              // check transfer length
3935
              if (i2) // without preamble
3936
              begin
3937
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3938
                begin
3939
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3940
                  fail = fail + 1;
3941
                end
3942
              end
3943
              else // with preamble
3944
              begin
3945
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3946
                begin
3947
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3948
                  fail = fail + 1;
3949
                end
3950
              end
3951
            end
3952
          join
3953
          // check the BUSY signal to see if the bus is still IDLE
3954
          for (i1 = 0; i1 < 8; i1 = i1 + 1)
3955
            check_mii_busy; // wait for bus to become idle
3956
 
3957
          // try normal write or read after scan was finished
3958
          phy_data = {8'h7D, (i[7:0] + 1)};
3959
          cnt = 0;
3960
          if (i3 == 0) // write after scan
3961 169 mohor
          begin
3962 181 mohor
            // write request
3963
            #Tp mii_write_req(phy_addr, reg_addr, phy_data);
3964
            // wait for serial bus to become active
3965
            wait(Mdio_IO !== 1'bz);
3966
            // count transfer length
3967
            while(Mdio_IO !== 1'bz)
3968
            begin
3969
              @(posedge Mdc_O);
3970
              #Tp cnt = cnt + 1;
3971
            end
3972 169 mohor
            @(posedge Mdc_O);
3973 181 mohor
            // read request
3974
            #Tp mii_read_req(phy_addr, reg_addr);
3975
            check_mii_busy; // wait for read to finish
3976
            // read and check data
3977
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3978
            if (phy_data !== tmp_data)
3979
            begin
3980
              test_fail("Data was not correctly written into OR read from PHY register - control register");
3981
              fail = fail + 1;
3982
            end
3983 169 mohor
          end
3984 181 mohor
          else // read after scan
3985 169 mohor
          begin
3986 181 mohor
            // read request
3987
            #Tp mii_read_req(phy_addr, reg_addr);
3988
            // wait for serial bus to become active
3989
            wait(Mdio_IO !== 1'bz);
3990
            // count transfer length
3991
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3992
            begin
3993
              @(posedge Mdc_O);
3994
              #Tp cnt = cnt + 1;
3995
            end
3996
            @(posedge Mdc_O);
3997
            check_mii_busy; // wait for read to finish
3998
            // read and check data
3999
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4000
            if (phy_data !== tmp_data)
4001
            begin
4002
              test_fail("Data was not correctly written into OR read from PHY register - control register");
4003
              fail = fail + 1;
4004
            end
4005 169 mohor
          end
4006 181 mohor
          // check if transfer was a proper length
4007
          if (i2) // without preamble
4008 169 mohor
          begin
4009 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
4010
            begin
4011
              test_fail("New request did not proceed correctly, after scan request");
4012
              fail = fail + 1;
4013
            end
4014 169 mohor
          end
4015 181 mohor
          else // with preamble
4016 169 mohor
          begin
4017 181 mohor
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
4018
            begin
4019
              test_fail("New request did not proceed correctly, after scan request");
4020
              fail = fail + 1;
4021
            end
4022 169 mohor
          end
4023
        end
4024 181 mohor
        #Tp;
4025
        // set delay of writing the command
4026 169 mohor
        if (i2) // without preamble
4027
        begin
4028 181 mohor
          case(i)
4029
            0, 1:               i = i + 1;
4030
            18, 19, 20, 21, 22,
4031
            23, 24, 25, 26, 27,
4032
            28, 29, 30, 31, 32,
4033
            33, 34, 35:         i = i + 1;
4034
            36:                 i = 80;
4035
            default:            i = 18;
4036
          endcase
4037 169 mohor
        end
4038
        else // with preamble
4039
        begin
4040 181 mohor
          case(i)
4041
            0, 1:               i = i + 1;
4042
            50, 51, 52, 53, 54,
4043
            55, 56, 57, 58, 59,
4044
            60, 61, 62, 63, 64,
4045
            65, 66, 67:         i = i + 1;
4046
            68:                 i = 80;
4047
            default:            i = 50;
4048
          endcase
4049 169 mohor
        end
4050 181 mohor
        @(posedge wb_clk);
4051 116 mohor
      end
4052
    end
4053 181 mohor
    // set PHY to normal mode
4054
    #Tp eth_phy.preamble_suppresed(0);
4055
    // MII mode register
4056
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4057
    if(fail == 0)
4058
      test_ok;
4059
    else
4060
      fail = 0;
4061 169 mohor
  end
4062 116 mohor
 
4063 181 mohor
end   //  for (test_num=start_task; test_num <= end_task; test_num=test_num+1)
4064
 
4065 169 mohor
end
4066
endtask // test_mii
4067
 
4068
 
4069
task test_mac_full_duplex_transmit;
4070
  input  [31:0]  start_task;
4071
  input  [31:0]  end_task;
4072
  integer        bit_start_1;
4073
  integer        bit_end_1;
4074
  integer        bit_start_2;
4075
  integer        bit_end_2;
4076
  integer        num_of_reg;
4077 209 tadejm
  integer        num_of_frames;
4078
  integer        num_of_bd;
4079 169 mohor
  integer        i_addr;
4080
  integer        i_data;
4081
  integer        i_length;
4082 209 tadejm
  integer        tmp_len;
4083
  integer        tmp_bd;
4084
  integer        tmp_bd_num;
4085 169 mohor
  integer        tmp_data;
4086 209 tadejm
  integer        tmp_ipgt;
4087 194 tadej
  integer        test_num;
4088 169 mohor
  reg    [31:0]  tx_bd_num;
4089
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_data;
4090
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_tmp_data;
4091
  integer        i;
4092
  integer        i1;
4093
  integer        i2;
4094
  integer        i3;
4095
  integer        fail;
4096
  integer        speed;
4097 209 tadejm
  reg            frame_started;
4098
  reg            frame_ended;
4099
  reg            wait_for_frame;
4100 169 mohor
  reg    [31:0]  addr;
4101
  reg    [31:0]  data;
4102
  reg    [31:0]  tmp;
4103
  reg    [ 7:0]  st_data;
4104
  reg    [15:0]  max_tmp;
4105
  reg    [15:0]  min_tmp;
4106
begin
4107
// MAC FULL DUPLEX TRANSMIT TEST
4108
test_heading("MAC FULL DUPLEX TRANSMIT TEST");
4109
$display(" ");
4110
$display("MAC FULL DUPLEX TRANSMIT TEST");
4111
fail = 0;
4112
 
4113
// reset MAC registers
4114
hard_reset;
4115
// reset MAC and MII LOGIC with soft reset
4116
reset_mac;
4117
reset_mii;
4118
// set wb slave response
4119
wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
4120
 
4121
  /*
4122
  TASKS for set and control TX buffer descriptors (also send packet - set_tx_bd_ready):
4123
  -------------------------------------------------------------------------------------
4124
  set_tx_bd
4125
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0], len[15:0], irq, pad, crc, txpnt[31:0]);
4126
  set_tx_bd_wrap
4127
    (tx_bd_num_end[6:0]);
4128
  set_tx_bd_ready
4129
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
4130
  check_tx_bd
4131
    (tx_bd_num_start[6:0], tx_bd_status[31:0]);
4132
  clear_tx_bd
4133
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
4134
 
4135
  TASKS for set and control RX buffer descriptors:
4136
  ------------------------------------------------
4137
  set_rx_bd
4138
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0], irq, rxpnt[31:0]);
4139
  set_rx_bd_wrap
4140
    (rx_bd_num_end[6:0]);
4141
  set_rx_bd_empty
4142
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
4143
  check_rx_bd
4144
    (rx_bd_num_end[6:0], rx_bd_status);
4145
  clear_rx_bd
4146
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
4147
 
4148
  TASKS for set and check TX packets:
4149
  -----------------------------------
4150
  set_tx_packet
4151
    (txpnt[31:0], len[15:0], eth_start_data[7:0]);
4152
  check_tx_packet
4153
    (txpnt_wb[31:0], txpnt_phy[31:0], len[15:0], failure[31:0]);
4154
 
4155
  TASKS for set and check RX packets:
4156
  -----------------------------------
4157
  set_rx_packet
4158
    (rxpnt[31:0], len[15:0], plus_nibble, d_addr[47:0], s_addr[47:0], type_len[15:0], start_data[7:0]);
4159
  check_rx_packet
4160
    (rxpnt_phy[31:0], rxpnt_wb[31:0], len[15:0], plus_nibble, successful_nibble, failure[31:0]);
4161
 
4162
  TASKS for append and check CRC to/of TX packet:
4163
  -----------------------------------------------
4164
  append_tx_crc
4165
    (txpnt_wb[31:0], len[15:0], negated_crc);
4166
  check_tx_crc
4167
    (txpnt_phy[31:0], len[15:0], negated_crc, failure[31:0]);
4168
 
4169
  TASK for append CRC to RX packet (CRC is checked together with check_rx_packet):
4170
  --------------------------------------------------------------------------------
4171
  append_rx_crc
4172
    (rxpnt_phy[31:0], len[15:0], plus_nibble, negated_crc);
4173
  */
4174
 
4175 194 tadej
//////////////////////////////////////////////////////////////////////
4176
////                                                              ////
4177
////  test_mac_full_duplex_transmit:                              ////
4178
////                                                              ////
4179
////  0: Test no transmit when all buffers are RX ( 10Mbps ).     ////
4180
////  1: Test no transmit when all buffers are RX ( 100Mbps ).    ////
4181
////  2: Test transmit packets form MINFL to MAXFL sizes at       ////
4182
////     one TX buffer decriptor ( 10Mbps ).                      ////
4183
////  3: Test transmit packets form MINFL to MAXFL sizes at       ////
4184
////     one TX buffer decriptor ( 100Mbps ).                     ////
4185
////                                                              ////
4186
//////////////////////////////////////////////////////////////////////
4187
for (test_num = start_task; test_num <= end_task; test_num = test_num + 1)
4188 169 mohor
begin
4189
 
4190 194 tadej
  ////////////////////////////////////////////////////////////////////
4191
  ////                                                            ////
4192
  ////  Test no transmit when all buffers are RX ( 10Mbps ).      ////
4193
  ////                                                            ////
4194
  ////////////////////////////////////////////////////////////////////
4195
  if (test_num == 0) // Test no transmit when all buffers are RX ( 10Mbps ).
4196 169 mohor
  begin
4197 194 tadej
    // TEST 0: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 10Mbps )
4198
    test_name   = "TEST 0: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 10Mbps )";
4199
    `TIME; $display("  TEST 0: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 10Mbps )");
4200
 
4201
    // unmask interrupts
4202 209 tadejm
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4203 194 tadej
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4204
    // set all buffer descriptors to RX - must be set before TX enable
4205
    wbm_write(`ETH_TX_BD_NUM, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4206
    // enable TX, set full-duplex mode, padding and CRC appending
4207
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4208
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4209
 
4210
    // write to phy's control register for 10Mbps
4211
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
4212
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
4213
    speed = 10;
4214
 
4215
    i = 0;
4216
    while (i < 128)
4217 169 mohor
    begin
4218 194 tadej
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
4219 169 mohor
      begin
4220 194 tadej
        set_tx_packet((`MEMORY_BASE + (i1 * 200)), 100, 0);
4221
        set_tx_bd(i1, i1, 100, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + (i1 * 200)));
4222 169 mohor
      end
4223 194 tadej
      set_tx_bd_wrap(i);
4224
      fork
4225
        begin
4226
          set_tx_bd_ready(0, i);
4227
          repeat(20) @(negedge mtx_clk);
4228
          #1 disable check_tx_en10;
4229
        end
4230
        begin: check_tx_en10
4231
          wait (MTxEn === 1'b1);
4232
          test_fail("Tramsmit should not start at all");
4233
          fail = fail + 1;
4234
          `TIME; $display("*E Transmit of %d packets should not start at all - active MTxEn", i);
4235
        end
4236
      join
4237
      for (i2 = 0; i2 < 20; i2 = i2 + 1)
4238 169 mohor
      begin
4239 194 tadej
        check_tx_bd(0, tmp);
4240
        #1;
4241
        if (tmp[15] === 1'b0)
4242
        begin
4243
          test_fail("Tramsmit should not start at all");
4244
          fail = fail + 1;
4245
          `TIME; $display("*E Transmit of %d packets should not start at all - ready is 0", i);
4246
        end
4247
        if (tmp[8:0] !== 0)
4248
        begin
4249
          test_fail("Tramsmit should not be finished since it should not start at all");
4250
          fail = fail + 1;
4251
          `TIME; $display("*E Transmit of should not be finished since it should not start at all");
4252
        end
4253
        @(posedge wb_clk);
4254 169 mohor
      end
4255 194 tadej
      wbm_read(`ETH_INT, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4256
      if (tmp[6:0] !== 0)
4257 169 mohor
      begin
4258 194 tadej
        test_fail("Tramsmit should not get INT since it should not start at all");
4259 169 mohor
        fail = fail + 1;
4260 194 tadej
        `TIME; $display("*E Transmit of should not get INT since it should not start at all");
4261 169 mohor
      end
4262 194 tadej
      clear_tx_bd(0, i);
4263
      if ((i < 5) || (i > 124))
4264
        i = i + 1;
4265
      else
4266
        i = i + 120;
4267 116 mohor
    end
4268 194 tadej
    // disable TX
4269
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4270
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4271
    if(fail == 0)
4272
      test_ok;
4273 116 mohor
    else
4274 194 tadej
      fail = 0;
4275 169 mohor
  end
4276 116 mohor
 
4277
 
4278 194 tadej
  ////////////////////////////////////////////////////////////////////
4279
  ////                                                            ////
4280
  ////  Test no transmit when all buffers are RX ( 100Mbps ).     ////
4281
  ////                                                            ////
4282
  ////////////////////////////////////////////////////////////////////
4283
  if (test_num == 1) // Test no transmit when all buffers are RX ( 100Mbps ).
4284 169 mohor
  begin
4285 194 tadej
    // TEST 1: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 100Mbps )
4286
    test_name   = "TEST 1: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 100Mbps )";
4287
    `TIME; $display("  TEST 1: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 100Mbps )");
4288
 
4289
    // unmask interrupts
4290 209 tadejm
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4291 194 tadej
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4292
    // set all buffer descriptors to RX - must be set before TX enable
4293
    wbm_write(`ETH_TX_BD_NUM, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4294
    // enable TX, set full-duplex mode, padding and CRC appending
4295
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4296
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4297
 
4298
    // write to phy's control register for 100Mbps
4299
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
4300
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
4301
    speed = 100;
4302
 
4303
    i = 0;
4304
    while (i < 128)
4305 169 mohor
    begin
4306 194 tadej
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
4307 169 mohor
      begin
4308 194 tadej
        set_tx_packet((`MEMORY_BASE + (i1 * 200)), 100, 0);
4309
        set_tx_bd(i1, i1, 100, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + (i1 * 200)));
4310 169 mohor
      end
4311 194 tadej
      set_tx_bd_wrap(i);
4312
      fork
4313
        begin
4314
          set_tx_bd_ready(0, i);
4315
          repeat(20) @(negedge mtx_clk);
4316
          #1 disable check_tx_en100;
4317
        end
4318
        begin: check_tx_en100
4319
          wait (MTxEn === 1'b1);
4320
          test_fail("Tramsmit should not start at all");
4321
          fail = fail + 1;
4322
          `TIME; $display("*E Transmit of %d packets should not start at all - active MTxEn", i);
4323
        end
4324
      join
4325
      for (i2 = 0; i2 < 20; i2 = i2 + 1)
4326 169 mohor
      begin
4327 194 tadej
        check_tx_bd(0, tmp);
4328
        #1;
4329
        if (tmp[15] === 1'b0)
4330
        begin
4331
          test_fail("Tramsmit should not start at all");
4332
          fail = fail + 1;
4333
          `TIME; $display("*E Transmit of %d packets should not start at all - ready is 0", i);
4334
        end
4335
        if (tmp[8:0] !== 0)
4336
        begin
4337
          test_fail("Tramsmit should not be finished since it should not start at all");
4338
          fail = fail + 1;
4339
          `TIME; $display("*E Transmit of should not be finished since it should not start at all");
4340
        end
4341
        @(posedge wb_clk);
4342 169 mohor
      end
4343 194 tadej
      wbm_read(`ETH_INT, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4344
      if (tmp[6:0] !== 0)
4345 169 mohor
      begin
4346 194 tadej
        test_fail("Tramsmit should not get INT since it should not start at all");
4347 169 mohor
        fail = fail + 1;
4348 194 tadej
        `TIME; $display("*E Transmit of should not get INT since it should not start at all");
4349 169 mohor
      end
4350 194 tadej
      clear_tx_bd(0, i);
4351
      if ((i < 5) || (i > 124))
4352
        i = i + 1;
4353
      else
4354
        i = i + 120;
4355 169 mohor
    end
4356 194 tadej
    // disable TX
4357
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4358
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4359
    if(fail == 0)
4360
      test_ok;
4361 169 mohor
    else
4362 194 tadej
      fail = 0;
4363 169 mohor
  end
4364
 
4365
 
4366 194 tadej
  ////////////////////////////////////////////////////////////////////
4367
  ////                                                            ////
4368
  ////  Test transmit packets form MINFL to MAXFL sizes at        ////
4369
  ////  one TX buffer decriptor ( 10Mbps ).                       ////
4370
  ////                                                            ////
4371
  ////////////////////////////////////////////////////////////////////
4372 209 tadejm
  if (test_num == 2) // without and with padding
4373 169 mohor
  begin
4374 194 tadej
    // TEST 2: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 10Mbps )
4375
    test_name = "TEST 2: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 10Mbps )";
4376
    `TIME; $display("  TEST 2: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 10Mbps )");
4377
 
4378
    max_tmp = 0;
4379
    min_tmp = 0;
4380
    // set one TX buffer descriptor - must be set before TX enable
4381
    wbm_write(`ETH_TX_BD_NUM, 32'h1, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4382 209 tadejm
    // enable TX, set full-duplex mode, NO padding and CRC appending
4383
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
4384 194 tadej
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4385
    // prepare two packets of MAXFL length
4386
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4387
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
4388
    min_tmp = tmp[31:16];
4389 209 tadejm
    st_data = 8'h01;
4390
    set_tx_packet(`MEMORY_BASE, (max_tmp), st_data); // length without CRC
4391 194 tadej
    st_data = 8'h10;
4392 209 tadejm
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp), st_data); // length without CRC
4393 194 tadej
    // check WB INT signal
4394
    if (wb_int !== 1'b0)
4395 169 mohor
    begin
4396 194 tadej
      test_fail("WB INT signal should not be set");
4397
      fail = fail + 1;
4398 169 mohor
    end
4399 194 tadej
 
4400
    // write to phy's control register for 10Mbps
4401
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
4402
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
4403
    speed = 10;
4404
 
4405
    i_length = (min_tmp - 4);
4406
    while (i_length <= (max_tmp - 4))
4407 169 mohor
    begin
4408 194 tadej
      // choose generating carrier sense and collision for first and last 64 lengths of frames
4409
      case (i_length[1:0])
4410
      2'h0: // Interrupt is generated
4411 169 mohor
      begin
4412 194 tadej
        // enable interrupt generation
4413 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
4414 194 tadej
        // unmask interrupts
4415 209 tadejm
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4416 194 tadej
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4417
        // not detect carrier sense in FD and no collision
4418
        eth_phy.carrier_sense_tx_fd_detect(0);
4419
        eth_phy.collision(0);
4420 169 mohor
      end
4421 194 tadej
      2'h1: // Interrupt is not generated
4422 169 mohor
      begin
4423 194 tadej
        // enable interrupt generation
4424 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
4425 194 tadej
        // mask interrupts
4426
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4427
        // detect carrier sense in FD and no collision
4428
        eth_phy.carrier_sense_tx_fd_detect(1);
4429
        eth_phy.collision(0);
4430 169 mohor
      end
4431 194 tadej
      2'h2: // Interrupt is not generated
4432
      begin
4433
        // disable interrupt generation
4434 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
4435 194 tadej
        // unmask interrupts
4436 209 tadejm
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4437 194 tadej
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4438
        // not detect carrier sense in FD and set collision
4439
        eth_phy.carrier_sense_tx_fd_detect(0);
4440
        eth_phy.collision(1);
4441
      end
4442
      default: // 2'h3: // Interrupt is not generated
4443
      begin
4444
        // disable interrupt generation
4445 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
4446 194 tadej
        // mask interrupts
4447
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4448
        // detect carrier sense in FD and set collision
4449
        eth_phy.carrier_sense_tx_fd_detect(1);
4450
        eth_phy.collision(1);
4451
      end
4452
      endcase
4453
      eth_phy.set_tx_mem_addr(max_tmp);
4454
      // set wrap bit
4455
      set_tx_bd_wrap(0);
4456
      set_tx_bd_ready(0, 0);
4457 169 mohor
      #1 check_tx_bd(0, data);
4458 194 tadej
      if (i_length < min_tmp) // just first four
4459 169 mohor
      begin
4460 194 tadej
        while (data[15] === 1)
4461
        begin
4462
          #1 check_tx_bd(0, data);
4463
          @(posedge wb_clk);
4464
        end
4465 209 tadejm
        repeat (1) @(posedge wb_clk);
4466 169 mohor
      end
4467 194 tadej
      else if (i_length > (max_tmp - 8)) // just last four
4468 192 tadej
      begin
4469 194 tadej
        tmp = 0;
4470
        wait (MTxEn === 1'b1); // start transmit
4471
        while (tmp < (i_length - 20))
4472
        begin
4473
          #1 tmp = tmp + 1;
4474
          @(posedge wb_clk);
4475
        end
4476
        #1 check_tx_bd(0, data);
4477
        while (data[15] === 1)
4478
        begin
4479
          #1 check_tx_bd(0, data);
4480
          @(posedge wb_clk);
4481
        end
4482 209 tadejm
        repeat (1) @(posedge wb_clk);
4483 192 tadej
      end
4484
      else
4485
      begin
4486 194 tadej
        wait (MTxEn === 1'b1); // start transmit
4487
        #1 check_tx_bd(0, data);
4488
        if (data[15] !== 1)
4489
        begin
4490
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
4491
          fail = fail + 1;
4492
        end
4493
        wait (MTxEn === 1'b0); // end transmit
4494
        while (data[15] === 1)
4495
        begin
4496
          #1 check_tx_bd(0, data);
4497
          @(posedge wb_clk);
4498
        end
4499
        repeat (1) @(posedge wb_clk);
4500 192 tadej
      end
4501 194 tadej
      // check length of a PACKET
4502
      if (eth_phy.tx_len != (i_length + 4))
4503 192 tadej
      begin
4504 194 tadej
        test_fail("Wrong length of the packet out from MAC");
4505 192 tadej
        fail = fail + 1;
4506
      end
4507 194 tadej
      // checking in the following if statement is performed only for first and last 64 lengths
4508
      if ( ((i_length + 4) <= (min_tmp + 64)) || ((i_length + 4) > (max_tmp - 64)) )
4509 192 tadej
      begin
4510 194 tadej
        // check transmitted TX packet data
4511
        if (i_length[0] == 0)
4512
        begin
4513 209 tadejm
          check_tx_packet((`MEMORY_BASE + i_length[1:0]), max_tmp, i_length, tmp);
4514 194 tadej
        end
4515
        else
4516
        begin
4517 209 tadejm
          check_tx_packet(((`MEMORY_BASE + i_length[1:0]) + max_tmp), max_tmp, i_length, tmp);
4518 194 tadej
        end
4519
        if (tmp > 0)
4520
        begin
4521
          test_fail("Wrong data of the transmitted packet");
4522
          fail = fail + 1;
4523
        end
4524
        // check transmited TX packet CRC
4525
        check_tx_crc(max_tmp, i_length, 1'b0, tmp); // length without CRC
4526
        if (tmp > 0)
4527
        begin
4528
          test_fail("Wrong CRC of the transmitted packet");
4529
          fail = fail + 1;
4530
        end
4531 192 tadej
      end
4532 194 tadej
      // check WB INT signal
4533
      if (i_length[1:0] == 2'h0)
4534 192 tadej
      begin
4535 194 tadej
        if (wb_int !== 1'b1)
4536
        begin
4537
          `TIME; $display("*E WB INT signal should be set");
4538
          test_fail("WB INT signal should be set");
4539
          fail = fail + 1;
4540
        end
4541 192 tadej
      end
4542 194 tadej
      else
4543 192 tadej
      begin
4544 194 tadej
        if (wb_int !== 1'b0)
4545
        begin
4546
          `TIME; $display("*E WB INT signal should not be set");
4547
          test_fail("WB INT signal should not be set");
4548
          fail = fail + 1;
4549
        end
4550 192 tadej
      end
4551 194 tadej
      // check TX buffer descriptor of a packet
4552
      check_tx_bd(0, data);
4553
      if (i_length[1] == 1'b0) // interrupt enabled
4554 192 tadej
      begin
4555 194 tadej
        if (data[15:0] !== 16'h7800)
4556
        begin
4557
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
4558
          test_fail("TX buffer descriptor status is not correct");
4559
          fail = fail + 1;
4560
        end
4561 192 tadej
      end
4562 194 tadej
      else // interrupt not enabled
4563 192 tadej
      begin
4564 194 tadej
        if (data[15:0] !== 16'h3800)
4565
        begin
4566
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
4567
          test_fail("TX buffer descriptor status is not correct");
4568
          fail = fail + 1;
4569
        end
4570 192 tadej
      end
4571 194 tadej
      // clear TX buffer descriptor
4572
      clear_tx_bd(0, 0);
4573
      // check interrupts
4574
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4575
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
4576 192 tadej
      begin
4577 194 tadej
        if ((data & `ETH_INT_TXB) !== 1'b1)
4578
        begin
4579
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
4580
          test_fail("Interrupt Transmit Buffer was not set");
4581
          fail = fail + 1;
4582
        end
4583
        if ((data & (~`ETH_INT_TXB)) !== 0)
4584
        begin
4585
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
4586
          test_fail("Other interrupts (except Transmit Buffer) were set");
4587
          fail = fail + 1;
4588
        end
4589 192 tadej
      end
4590 194 tadej
      else
4591 192 tadej
      begin
4592 194 tadej
        if (data !== 0)
4593
        begin
4594
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
4595
          test_fail("Any of interrupts (except Transmit Buffer) was set");
4596
          fail = fail + 1;
4597
        end
4598 192 tadej
      end
4599 194 tadej
      // clear interrupts
4600
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4601
      // check WB INT signal
4602
      if (wb_int !== 1'b0)
4603 192 tadej
      begin
4604 194 tadej
        test_fail("WB INT signal should not be set");
4605 192 tadej
        fail = fail + 1;
4606
      end
4607 194 tadej
      // INTERMEDIATE DISPLAYS
4608
      if ((i_length + 4) == (min_tmp + 64))
4609 209 tadejm
      begin
4610 194 tadej
        // starting length is min_tmp, ending length is (min_tmp + 64)
4611 209 tadejm
        $display("    pads appending to packets is NOT selected");
4612
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
4613 194 tadej
                 min_tmp, (min_tmp + 64));
4614 209 tadejm
        // set padding, remain the rest
4615
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4616
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4617
      end
4618 194 tadej
      else if ((i_length + 4) == (max_tmp - 16))
4619 209 tadejm
      begin
4620 194 tadej
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
4621 209 tadejm
        $display("    pads appending to packets is selected");
4622
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
4623 194 tadej
                 (min_tmp + 64 + 128), tmp_data);
4624 209 tadejm
        // reset padding, remain the rest
4625
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
4626
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4627
      end
4628 194 tadej
      else if ((i_length + 4) == max_tmp)
4629 209 tadejm
      begin
4630
        $display("    pads appending to packets is NOT selected");
4631
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
4632 194 tadej
                 (max_tmp - (4 + 16)), max_tmp);
4633 209 tadejm
      end
4634 194 tadej
      // set length (loop variable)
4635
      if ((i_length + 4) < (min_tmp + 64))
4636
        i_length = i_length + 1;
4637
      else if ( ((i_length + 4) >= (min_tmp + 64)) && ((i_length + 4) <= (max_tmp - 256)) )
4638
      begin
4639
        i_length = i_length + 128;
4640
        tmp_data = i_length + 4; // last tmp_data is ending length
4641
      end
4642
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
4643
        i_length = max_tmp - (4 + 16);
4644
      else if ((i_length + 4) >= (max_tmp - 16))
4645
        i_length = i_length + 1;
4646
      else
4647
      begin
4648
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
4649
        #10 $stop;
4650
      end
4651 192 tadej
    end
4652 194 tadej
    // disable TX
4653
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4654
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4655
    if(fail == 0)
4656
      test_ok;
4657
    else
4658
      fail = 0;
4659
  end
4660
 
4661
 
4662
  ////////////////////////////////////////////////////////////////////
4663
  ////                                                            ////
4664
  ////  Test transmit packets form MINFL to MAXFL sizes at        ////
4665
  ////  one TX buffer decriptor ( 100Mbps ).                      ////
4666
  ////                                                            ////
4667
  ////////////////////////////////////////////////////////////////////
4668 209 tadejm
  if (test_num == 3) // with and without padding
4669 194 tadej
  begin
4670
    // TEST 3: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 100Mbps )
4671
    test_name = "TEST 3: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 100Mbps )";
4672
    `TIME; $display("  TEST 3: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 100Mbps )");
4673
 
4674
    max_tmp = 0;
4675
    min_tmp = 0;
4676
    // set one TX buffer descriptor - must be set before TX enable
4677
    wbm_write(`ETH_TX_BD_NUM, 32'h1, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4678 209 tadejm
    // enable TX, set full-duplex mode, NO padding and CRC appending
4679
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
4680 194 tadej
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4681
    // prepare two packets of MAXFL length
4682
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4683
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
4684
    min_tmp = tmp[31:16];
4685
    st_data = 8'h5A;
4686 209 tadejm
    set_tx_packet(`MEMORY_BASE, (max_tmp), st_data); // length without CRC
4687 194 tadej
    st_data = 8'h10;
4688 209 tadejm
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp), st_data); // length without CRC
4689 169 mohor
    // check WB INT signal
4690 192 tadej
    if (wb_int !== 1'b0)
4691 169 mohor
    begin
4692
      test_fail("WB INT signal should not be set");
4693
      fail = fail + 1;
4694
    end
4695 194 tadej
 
4696
    // write to phy's control register for 100Mbps
4697
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
4698
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
4699
    speed = 100;
4700 192 tadej
 
4701 194 tadej
    i_length = (min_tmp - 4);
4702
    while (i_length <= (max_tmp - 4))
4703 192 tadej
    begin
4704 194 tadej
      // choose generating carrier sense and collision
4705
      case (i_length[1:0])
4706
      2'h0: // Interrupt is generated
4707 192 tadej
      begin
4708 194 tadej
        // enable interrupt generation
4709 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
4710 194 tadej
        // unmask interrupts
4711 209 tadejm
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4712 194 tadej
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4713
        // not detect carrier sense in FD and no collision
4714
        eth_phy.carrier_sense_tx_fd_detect(0);
4715
        eth_phy.collision(0);
4716 192 tadej
      end
4717 194 tadej
      2'h1: // Interrupt is not generated
4718 192 tadej
      begin
4719 194 tadej
        // enable interrupt generation
4720 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
4721 194 tadej
        // mask interrupts
4722
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4723
        // detect carrier sense in FD and no collision
4724
        eth_phy.carrier_sense_tx_fd_detect(1);
4725
        eth_phy.collision(0);
4726 192 tadej
      end
4727 194 tadej
      2'h2: // Interrupt is not generated
4728
      begin
4729
        // disable interrupt generation
4730 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
4731 194 tadej
        // unmask interrupts
4732 209 tadejm
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4733 194 tadej
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4734
        // not detect carrier sense in FD and set collision
4735
        eth_phy.carrier_sense_tx_fd_detect(0);
4736
        eth_phy.collision(1);
4737
      end
4738
      default: // 2'h3: // Interrupt is not generated
4739
      begin
4740
        // disable interrupt generation
4741 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
4742 194 tadej
        // mask interrupts
4743
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4744
        // detect carrier sense in FD and set collision
4745
        eth_phy.carrier_sense_tx_fd_detect(1);
4746
        eth_phy.collision(1);
4747
      end
4748
      endcase
4749
      eth_phy.set_tx_mem_addr(max_tmp);
4750
      // set wrap bit
4751
      set_tx_bd_wrap(0);
4752
      set_tx_bd_ready(0, 0);
4753 192 tadej
      #1 check_tx_bd(0, data);
4754 194 tadej
      if (i_length < min_tmp) // just first four
4755 192 tadej
      begin
4756 194 tadej
        while (data[15] === 1)
4757
        begin
4758
          #1 check_tx_bd(0, data);
4759
          @(posedge wb_clk);
4760
        end
4761 209 tadejm
        repeat (1) @(posedge wb_clk);
4762 194 tadej
      end
4763
      else if (i_length > (max_tmp - 8)) // just last four
4764
      begin
4765
        tmp = 0;
4766
        wait (MTxEn === 1'b1); // start transmit
4767
        while (tmp < (i_length - 20))
4768
        begin
4769
          #1 tmp = tmp + 1;
4770
          @(posedge wb_clk);
4771
        end
4772 192 tadej
        #1 check_tx_bd(0, data);
4773 194 tadej
        while (data[15] === 1)
4774
        begin
4775
          #1 check_tx_bd(0, data);
4776
          @(posedge wb_clk);
4777
        end
4778 209 tadejm
        repeat (1) @(posedge wb_clk);
4779 192 tadej
      end
4780 194 tadej
      else
4781
      begin
4782
        wait (MTxEn === 1'b1); // start transmit
4783
        #1 check_tx_bd(0, data);
4784
        if (data[15] !== 1)
4785
        begin
4786
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
4787
          fail = fail + 1;
4788
        end
4789
        wait (MTxEn === 1'b0); // end transmit
4790
        while (data[15] === 1)
4791
        begin
4792
          #1 check_tx_bd(0, data);
4793
          @(posedge wb_clk);
4794
        end
4795
        repeat (1) @(posedge wb_clk);
4796
      end
4797
      // check length of a PACKET
4798
      if (eth_phy.tx_len != (i_length + 4))
4799
      begin
4800
        test_fail("Wrong length of the packet out from MAC");
4801
        fail = fail + 1;
4802
      end
4803 192 tadej
      // check transmitted TX packet data
4804
      if (i_length[0] == 0)
4805
      begin
4806 209 tadejm
        check_tx_packet((`MEMORY_BASE + i_length[1:0]), max_tmp, i_length, tmp);
4807 192 tadej
      end
4808
      else
4809
      begin
4810 209 tadejm
        check_tx_packet(((`MEMORY_BASE + i_length[1:0]) + max_tmp), max_tmp, i_length, tmp);
4811 192 tadej
      end
4812
      if (tmp > 0)
4813
      begin
4814
        test_fail("Wrong data of the transmitted packet");
4815
        fail = fail + 1;
4816
      end
4817
      // check transmited TX packet CRC
4818
      check_tx_crc(max_tmp, i_length, 1'b0, tmp); // length without CRC
4819
      if (tmp > 0)
4820
      begin
4821
        test_fail("Wrong CRC of the transmitted packet");
4822
        fail = fail + 1;
4823
      end
4824 194 tadej
      // check WB INT signal
4825
      if (i_length[1:0] == 2'h0)
4826 192 tadej
      begin
4827 194 tadej
        if (wb_int !== 1'b1)
4828
        begin
4829
          `TIME; $display("*E WB INT signal should be set");
4830
          test_fail("WB INT signal should be set");
4831
          fail = fail + 1;
4832
        end
4833 192 tadej
      end
4834 194 tadej
      else
4835 192 tadej
      begin
4836 194 tadej
        if (wb_int !== 1'b0)
4837
        begin
4838
          `TIME; $display("*E WB INT signal should not be set");
4839
          test_fail("WB INT signal should not be set");
4840
          fail = fail + 1;
4841
        end
4842 192 tadej
      end
4843 194 tadej
      // check TX buffer descriptor of a packet
4844
      check_tx_bd(0, data);
4845
      if (i_length[1] == 1'b0) // interrupt enabled
4846 192 tadej
      begin
4847 194 tadej
        if (data[15:0] !== 16'h7800)
4848
        begin
4849
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
4850
          test_fail("TX buffer descriptor status is not correct");
4851
          fail = fail + 1;
4852
        end
4853 192 tadej
      end
4854 194 tadej
      else // interrupt not enabled
4855 192 tadej
      begin
4856 194 tadej
        if (data[15:0] !== 16'h3800)
4857
        begin
4858
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
4859
          test_fail("TX buffer descriptor status is not correct");
4860
          fail = fail + 1;
4861
        end
4862 192 tadej
      end
4863 194 tadej
      // clear TX buffer descriptor
4864
      clear_tx_bd(0, 0);
4865
      // check interrupts
4866
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4867
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
4868 192 tadej
      begin
4869 194 tadej
        if ((data & `ETH_INT_TXB) !== 1'b1)
4870
        begin
4871
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
4872
          test_fail("Interrupt Transmit Buffer was not set");
4873
          fail = fail + 1;
4874
        end
4875
        if ((data & (~`ETH_INT_TXB)) !== 0)
4876
        begin
4877
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
4878
          test_fail("Other interrupts (except Transmit Buffer) were set");
4879
          fail = fail + 1;
4880
        end
4881 192 tadej
      end
4882 194 tadej
      else
4883 192 tadej
      begin
4884 194 tadej
        if (data !== 0)
4885
        begin
4886
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h", data);
4887
          test_fail("Any of interrupts (except Transmit Buffer) was set");
4888
          fail = fail + 1;
4889
        end
4890 192 tadej
      end
4891 194 tadej
      // clear interrupts
4892
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4893
      // check WB INT signal
4894
      if (wb_int !== 1'b0)
4895 192 tadej
      begin
4896 194 tadej
        test_fail("WB INT signal should not be set");
4897 192 tadej
        fail = fail + 1;
4898
      end
4899 194 tadej
      // INTERMEDIATE DISPLAYS
4900
      if ((i_length + 4) == (min_tmp + 64))
4901 209 tadejm
      begin
4902 194 tadej
        // starting length is min_tmp, ending length is (min_tmp + 64)
4903 209 tadejm
        $display("    pads appending to packets is NOT selected");
4904
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
4905 194 tadej
                 min_tmp, (min_tmp + 64));
4906 209 tadejm
        // set padding, remain the rest
4907
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4908
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4909
      end
4910 194 tadej
      else if ((i_length + 4) == (max_tmp - 16))
4911 209 tadejm
      begin
4912 194 tadej
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
4913 209 tadejm
        $display("    pads appending to packets is selected");
4914
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
4915 194 tadej
                 (min_tmp + 64 + 128), tmp_data);
4916 209 tadejm
        // reset padding, remain the rest
4917
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
4918
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4919
      end
4920 194 tadej
      else if ((i_length + 4) == max_tmp)
4921 209 tadejm
      begin
4922
        $display("    pads appending to packets is NOT selected");
4923
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
4924 194 tadej
                 (max_tmp - (4 + 16)), max_tmp);
4925 209 tadejm
      end
4926 194 tadej
      // set length (loop variable)
4927
      if ((i_length + 4) < (min_tmp + 64))
4928
        i_length = i_length + 1;
4929
      else if ( ((i_length + 4) >= (min_tmp + 64)) && ((i_length + 4) <= (max_tmp - 256)) )
4930
      begin
4931
        i_length = i_length + 128;
4932
        tmp_data = i_length + 4; // last tmp_data is ending length
4933
      end
4934
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
4935
        i_length = max_tmp - (4 + 16);
4936
      else if ((i_length + 4) >= (max_tmp - 16))
4937
        i_length = i_length + 1;
4938 192 tadej
      else
4939 194 tadej
      begin
4940
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
4941
        #10 $stop;
4942
      end
4943 179 mohor
    end
4944 194 tadej
    // disable TX
4945
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4946
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4947
    if(fail == 0)
4948
      test_ok;
4949
    else
4950
      fail = 0;
4951 169 mohor
  end
4952
 
4953
 
4954 209 tadejm
  ////////////////////////////////////////////////////////////////////
4955
  ////                                                            ////
4956
  ////  Test transmit packets form MINFL to MAXFL sizes at        ////
4957
  ////  maximum TX buffer decriptors ( 10Mbps ).                  ////
4958
  ////                                                            ////
4959
  ////////////////////////////////////////////////////////////////////
4960
  if (test_num == 4) // without and with padding
4961
  begin
4962
    // TEST 4: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 10Mbps )
4963
    test_name = "TEST 4: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 10Mbps )";
4964
    `TIME; $display("  TEST 4: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 10Mbps )");
4965
 
4966
    // reset MAC registers
4967
    hard_reset;
4968
    // reset MAC and MII LOGIC with soft reset
4969
    reset_mac;
4970
    reset_mii;
4971
    // set wb slave response
4972
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
4973 192 tadej
 
4974 209 tadejm
    max_tmp = 0;
4975
    min_tmp = 0;
4976
    num_of_frames = 0;
4977
    num_of_bd = 0;
4978
    // set maximum TX buffer descriptors (128) - must be set before TX enable
4979
    wbm_write(`ETH_TX_BD_NUM, 32'h80, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4980
    // enable TX, set full-duplex mode, NO padding and CRC appending
4981
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
4982
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4983
    // prepare two packets of MAXFL length
4984
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4985
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
4986
    min_tmp = tmp[31:16];
4987
    st_data = 8'hA3;
4988
    set_tx_packet(`MEMORY_BASE, (max_tmp), st_data); // length without CRC
4989
    st_data = 8'h81;
4990
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp), st_data); // length without CRC
4991
    // check WB INT signal
4992
    if (wb_int !== 1'b0)
4993
    begin
4994
      test_fail("WB INT signal should not be set");
4995
      fail = fail + 1;
4996
    end
4997
 
4998
    // write to phy's control register for 10Mbps
4999
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
5000
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
5001
    speed = 10;
5002
 
5003
    i_length = (min_tmp - 4);
5004
    while (i_length <= (max_tmp - 4))
5005
    begin
5006
      // choose generating carrier sense and collision
5007
      case (i_length[1:0])
5008
      2'h0: // Interrupt is generated
5009
      begin
5010
        // Reset_tx_bd nable interrupt generation
5011
        // unmask interrupts
5012
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
5013
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5014
        // not detect carrier sense in FD and no collision
5015
        eth_phy.carrier_sense_tx_fd_detect(0);
5016
        eth_phy.collision(0);
5017
      end
5018
      2'h1: // Interrupt is not generated
5019
      begin
5020
        // set_tx_bd enable interrupt generation
5021
        // mask interrupts
5022
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5023
        // detect carrier sense in FD and no collision
5024
        eth_phy.carrier_sense_tx_fd_detect(1);
5025
        eth_phy.collision(0);
5026
      end
5027
      2'h2: // Interrupt is not generated
5028
      begin
5029
        // set_tx_bd disable the interrupt generation
5030
        // unmask interrupts
5031
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
5032
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5033
        // not detect carrier sense in FD and set collision
5034
        eth_phy.carrier_sense_tx_fd_detect(0);
5035
        eth_phy.collision(1);
5036
      end
5037
      default: // 2'h3: // Interrupt is not generated
5038
      begin
5039
        // set_tx_bd disable the interrupt generation
5040
        // mask interrupts
5041
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5042
        // detect carrier sense in FD and set collision
5043
        eth_phy.carrier_sense_tx_fd_detect(1);
5044
        eth_phy.collision(1);
5045
      end
5046
      endcase
5047
      // first destination address on ethernet PHY
5048
      if (i_length[0] == 0)
5049
        eth_phy.set_tx_mem_addr(0);
5050
      else
5051
        eth_phy.set_tx_mem_addr(max_tmp);
5052
      // first 8 frames are transmitted with TX BD 0 (wrap bit on TX BD 0)
5053
      // number of all frames is 154 (146 without first 8)
5054
      if (num_of_frames < 8)
5055
      begin
5056
        case (i_length[1:0])
5057
        2'h0: // Interrupt is generated
5058
        begin
5059
          // enable interrupt generation
5060
          set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
5061
          // interrupts are unmasked
5062
        end
5063
        2'h1: // Interrupt is not generated
5064
        begin
5065
          // enable interrupt generation
5066
          set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
5067
          // interrupts are masked
5068
        end
5069
        2'h2: // Interrupt is not generated
5070
        begin
5071
          // disable interrupt generation
5072
          set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
5073
          // interrupts are unmasked
5074
        end
5075
        default: // 2'h3: // Interrupt is not generated
5076
        begin
5077
          // disable interrupt generation
5078
          set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
5079
          // interrupts are masked
5080
        end
5081
        endcase
5082
        // set wrap bit
5083
        set_tx_bd_wrap(0);
5084
      end
5085
      // after first 8 number of frames, 128 frames form TX BD 0 to 127 will be transmitted
5086
      else if ((num_of_frames - 8) == 0)
5087
      begin
5088
        tmp_len = i_length; // length of frame
5089
        tmp_bd_num = 0; // TX BD number
5090
        while (tmp_bd_num < 128) // (tmp_len <= (max_tmp - 4)) - this is the last frame
5091
        begin
5092
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5093
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5094
          if (tmp_len[0] == 0)
5095
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
5096
          else
5097
            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));
5098
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
5099
          if ((tmp_len + 4) < (min_tmp + 128))
5100
            tmp_len = tmp_len + 1;
5101
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5102
            tmp_len = 256;
5103
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5104
            tmp_len = tmp_len + 128;
5105
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
5106
            tmp_len = max_tmp - (4 + 16);
5107
          else if ((tmp_len + 4) >= (max_tmp - 16))
5108
            tmp_len = tmp_len + 1;
5109
          // set TX BD number
5110
          tmp_bd_num = tmp_bd_num + 1;
5111
        end
5112
        // set wrap bit
5113
        set_tx_bd_wrap(127);
5114
      end
5115
      // after 128 + first 8 number of frames, 19 frames form TX BD 0 to 18 will be transmitted
5116
      else if ((num_of_frames - 8) == 20) // 128
5117
      begin
5118
        tmp_len = tmp_len; // length of frame remaines from previous settings
5119
        tmp_bd_num = 0; // TX BD number
5120
        while (tmp_bd_num < 19) // (tmp_len <= (max_tmp - 4)) - this is the last frame
5121
        begin
5122
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5123
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5124
          if (tmp_len[0] == 0)
5125
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
5126
          else
5127
            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));
5128
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
5129
          if ((tmp_len + 4) < (min_tmp + 128))
5130
            tmp_len = tmp_len + 1;
5131
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5132
            tmp_len = 256;
5133
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5134
            tmp_len = tmp_len + 128;
5135
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
5136
            tmp_len = max_tmp - (4 + 16);
5137
          else if ((tmp_len + 4) >= (max_tmp - 16))
5138
            tmp_len = tmp_len + 1;
5139
          // set TX BD number
5140
          tmp_bd_num = tmp_bd_num + 1;
5141
        end
5142
      end
5143
      // set ready bit
5144
      if (num_of_frames < 8)
5145
        set_tx_bd_ready(0, 0);
5146
      else if ((num_of_frames - 8) < 128)
5147
        set_tx_bd_ready((num_of_frames - 8), (num_of_frames - 8));
5148
      else if ((num_of_frames - 136) < 19)
5149
        set_tx_bd_ready((num_of_frames - 136), (num_of_frames - 136));
5150
      // CHECK END OF TRANSMITION
5151
      #1 check_tx_bd(num_of_bd, data);
5152
      if (i_length < min_tmp) // just first four
5153
      begin
5154
        while (data[15] === 1)
5155
        begin
5156
          #1 check_tx_bd(num_of_bd, data);
5157
          @(posedge wb_clk);
5158
        end
5159
        repeat (1) @(posedge wb_clk);
5160
      end
5161
      else if (i_length > (max_tmp - 8)) // just last four
5162
      begin
5163
        tmp = 0;
5164
        wait (MTxEn === 1'b1); // start transmit
5165
        while (tmp < (i_length - 20))
5166
        begin
5167
          #1 tmp = tmp + 1;
5168
          @(posedge wb_clk);
5169
        end
5170
        #1 check_tx_bd(num_of_bd, data);
5171
        while (data[15] === 1)
5172
        begin
5173
          #1 check_tx_bd(num_of_bd, data);
5174
          @(posedge wb_clk);
5175
        end
5176
        repeat (1) @(posedge wb_clk);
5177
      end
5178
      else
5179
      begin
5180
        wait (MTxEn === 1'b1); // start transmit
5181
        #1 check_tx_bd(num_of_bd, data);
5182
        if (data[15] !== 1)
5183
        begin
5184
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
5185
          fail = fail + 1;
5186
        end
5187
        wait (MTxEn === 1'b0); // end transmit
5188
        while (data[15] === 1)
5189
        begin
5190
          #1 check_tx_bd(num_of_bd, data);
5191
          @(posedge wb_clk);
5192
        end
5193
        repeat (1) @(posedge wb_clk);
5194
      end
5195
      // check length of a PACKET
5196
      if (eth_phy.tx_len != (i_length + 4))
5197
      begin
5198
        test_fail("Wrong length of the packet out from MAC");
5199
        fail = fail + 1;
5200
      end
5201
        // check transmitted TX packet data
5202
        if (i_length[0] == 0)
5203
        begin
5204
          check_tx_packet((`MEMORY_BASE + i_length[1:0]), 0, i_length, tmp);
5205
        end
5206
        else
5207
        begin
5208
          check_tx_packet(((`MEMORY_BASE + i_length[1:0]) + max_tmp), max_tmp, i_length, tmp);
5209
        end
5210
        if (tmp > 0)
5211
        begin
5212
          test_fail("Wrong data of the transmitted packet");
5213
          fail = fail + 1;
5214
        end
5215
        // check transmited TX packet CRC
5216
        if (i_length[0] == 0)
5217
          check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
5218
        else
5219
          check_tx_crc(max_tmp, i_length, 1'b0, tmp); // length without CRC
5220
        if (tmp > 0)
5221
        begin
5222
          test_fail("Wrong CRC of the transmitted packet");
5223
          fail = fail + 1;
5224
        end
5225
      // check WB INT signal
5226
      if (i_length[1:0] == 2'h0)
5227
      begin
5228
        if (wb_int !== 1'b1)
5229
        begin
5230
          `TIME; $display("*E WB INT signal should be set");
5231
          test_fail("WB INT signal should be set");
5232
          fail = fail + 1;
5233
        end
5234
      end
5235
      else
5236
      begin
5237
        if (wb_int !== 1'b0)
5238
        begin
5239
          `TIME; $display("*E WB INT signal should not be set");
5240
          test_fail("WB INT signal should not be set");
5241
          fail = fail + 1;
5242
        end
5243
      end
5244
      // check TX buffer descriptor of a packet
5245
      check_tx_bd(num_of_bd, data);
5246
      if (i_length[1] == 1'b0) // interrupt enabled
5247
      begin
5248
        if ( ((data[15:0] !== 16'h7800) && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
5249
             ((data[15:0] !== 16'h5800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
5250
        begin
5251
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
5252
          test_fail("TX buffer descriptor status is not correct");
5253
          fail = fail + 1;
5254
        end
5255
      end
5256
      else // interrupt not enabled
5257
      begin
5258
        if ( ((data[15:0] !== 16'h3800)  && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
5259
             ((data[15:0] !== 16'h1800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
5260
        begin
5261
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
5262
          test_fail("TX buffer descriptor status is not correct");
5263
          fail = fail + 1;
5264
        end
5265
      end
5266
      // clear first half of 8 frames from TX buffer descriptor 0
5267
      if (num_of_frames < 4)
5268
        clear_tx_bd(num_of_bd, num_of_bd);
5269
      // clear BD with wrap bit
5270
      if (num_of_frames == 140)
5271
        clear_tx_bd(127, 127);
5272
      // check interrupts
5273
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5274
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
5275
      begin
5276
        if ((data & `ETH_INT_TXB) !== 1'b1)
5277
        begin
5278
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
5279
          test_fail("Interrupt Transmit Buffer was not set");
5280
          fail = fail + 1;
5281
        end
5282
        if ((data & (~`ETH_INT_TXB)) !== 0)
5283
        begin
5284
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
5285
          test_fail("Other interrupts (except Transmit Buffer) were set");
5286
          fail = fail + 1;
5287
        end
5288
      end
5289
      else
5290
      begin
5291
        if (data !== 0)
5292
        begin
5293
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
5294
          test_fail("Any of interrupts (except Transmit Buffer) was set");
5295
          fail = fail + 1;
5296
        end
5297
      end
5298
      // clear interrupts
5299
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5300
      // check WB INT signal
5301
      if (wb_int !== 1'b0)
5302
      begin
5303
        test_fail("WB INT signal should not be set");
5304
        fail = fail + 1;
5305
      end
5306
      // INTERMEDIATE DISPLAYS
5307
      if ((i_length + 4) == (min_tmp + 7))
5308
      begin
5309
        // starting length is min_tmp, ending length is (min_tmp + 128)
5310
        $display("    pads appending to packets is NOT selected");
5311
        $display("    using only TX BD 0 out of 128 BDs assigned to TX (wrap at first BD - TX BD 0)");
5312
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
5313
                 min_tmp, (min_tmp + 7));
5314
        $display("    ->all packets were send from TX BD 0");
5315
        // set padding, remain the rest
5316
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5317
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5318
      end
5319
      else if ((i_length + 4) == (min_tmp + 128))
5320
      begin
5321
        // starting length is min_tmp, ending length is (min_tmp + 128)
5322
        $display("    pads appending to packets is NOT selected");
5323
        $display("    using all 128 BDs assigned to TX (wrap at 128th BD - TX BD 127)");
5324
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
5325
                 (min_tmp + 8), (min_tmp + 128));
5326
        $display("    ->packets were send from TX BD %0d to TX BD %0d respectively",
5327
                 1'b0, num_of_bd);
5328
        tmp_bd = num_of_bd + 1;
5329
        // set padding, remain the rest
5330
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5331
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5332
      end
5333
      else if ((i_length + 4) == (max_tmp - 16))
5334
      begin
5335
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
5336
        $display("    pads appending to packets is selected");
5337
        $display("    using all 128 BDs assigned to TX (wrap at 128th BD - TX BD 127)");
5338
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
5339
                 (min_tmp + 64 + 128), tmp_data);
5340
        if (tmp_bd > num_of_bd)
5341
          $display("    ->packets were send from TX BD %0d to TX BD 127 and from TX BD 0 to TX BD %0d respectively",
5342
                   tmp_bd, num_of_bd);
5343
        else
5344
          $display("    ->packets were send from TX BD %0d to TX BD %0d respectively",
5345
                   tmp_bd, num_of_bd);
5346
        tmp_bd = num_of_bd + 1;
5347
        // reset padding, remain the rest
5348
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
5349
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5350
      end
5351
      else if ((i_length + 4) == max_tmp)
5352
      begin
5353
        $display("    pads appending to packets is NOT selected");
5354
        $display("    using all 128 BDs assigned to TX (wrap at 128th BD - TX BD 127)");
5355
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
5356
                 (max_tmp - (4 + 16)), max_tmp);
5357
        if (tmp_bd > num_of_bd)
5358
          $display("    ->packets were send from TX BD %0d to TX BD 127 and from TX BD 0 to TX BD %0d respectively",
5359
                   tmp_bd, num_of_bd);
5360
        else
5361
          $display("    ->packets were send from TX BD %0d to TX BD %0d respectively",
5362
                   tmp_bd, num_of_bd);
5363
      end
5364
      // set length (loop variable)
5365
      if ((i_length + 4) < (min_tmp + 128))
5366
        i_length = i_length + 1;
5367
      else if ( ((i_length + 4) == (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
5368
        i_length = 256;
5369
      else if ( ((i_length + 4) > (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
5370
      begin
5371
        i_length = i_length + 128;
5372
        tmp_data = i_length + 4; // last tmp_data is ending length
5373
      end
5374
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
5375
        i_length = max_tmp - (4 + 16);
5376
      else if ((i_length + 4) >= (max_tmp - 16))
5377
        i_length = i_length + 1;
5378
      else
5379
      begin
5380
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
5381
        #10 $stop;
5382
      end
5383
      // the number of frame transmitted
5384
      num_of_frames = num_of_frames + 1;
5385
      if ((num_of_frames <= 8) || ((num_of_frames - 8) == 128))
5386
        num_of_bd = 0;
5387
      else
5388
        num_of_bd = num_of_bd + 1;
5389
    end
5390
    // disable TX
5391
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5392
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5393
    @(posedge wb_clk);
5394
    if(fail == 0)
5395
      test_ok;
5396
    else
5397
      fail = 0;
5398
  end
5399
 
5400
 
5401
  ////////////////////////////////////////////////////////////////////
5402
  ////                                                            ////
5403
  ////  Test transmit packets form MINFL to MAXFL sizes at        ////
5404
  ////  maximum TX buffer decriptors ( 100Mbps ).                 ////
5405
  ////                                                            ////
5406
  ////////////////////////////////////////////////////////////////////
5407
  if (test_num == 5) // with and without padding
5408
  begin
5409
    // TEST 5: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 100Mbps )
5410
    test_name = "TEST 5: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 100Mbps )";
5411
    `TIME; $display("  TEST 5: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 100Mbps )");
5412
 
5413
    // reset MAC registers
5414
    hard_reset;
5415
    // reset MAC and MII LOGIC with soft reset
5416
    reset_mac;
5417
    reset_mii;
5418
    // set wb slave response
5419
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
5420
 
5421
    max_tmp = 0;
5422
    min_tmp = 0;
5423
    num_of_frames = 0;
5424
    num_of_bd = 0;
5425
    // set maximum TX buffer descriptors (128) - must be set before TX enable
5426
    wbm_write(`ETH_TX_BD_NUM, 32'h80, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5427
    // enable TX, set full-duplex mode, NO padding and CRC appending
5428
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
5429
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5430
    // prepare two packets of MAXFL length
5431
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5432
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
5433
    min_tmp = tmp[31:16];
5434
    st_data = 8'hA5;
5435
    set_tx_packet(`MEMORY_BASE, (max_tmp), st_data); // length without CRC
5436
    st_data = 8'h71;
5437
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp), st_data); // length without CRC
5438
    // check WB INT signal
5439
    if (wb_int !== 1'b0)
5440
    begin
5441
      test_fail("WB INT signal should not be set");
5442
      fail = fail + 1;
5443
    end
5444
 
5445
    // write to phy's control register for 100Mbps
5446
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
5447
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
5448
    speed = 100;
5449
 
5450
    i_length = (min_tmp - 4);
5451
    while (i_length <= (max_tmp - 4))
5452
    begin
5453
      // choose generating carrier sense and collision
5454
      case (i_length[1:0])
5455
      2'h0: // Interrupt is generated
5456
      begin
5457
        // Reset_tx_bd nable interrupt generation
5458
        // unmask interrupts
5459
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
5460
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5461
        // not detect carrier sense in FD and no collision
5462
        eth_phy.carrier_sense_tx_fd_detect(0);
5463
        eth_phy.collision(0);
5464
      end
5465
      2'h1: // Interrupt is not generated
5466
      begin
5467
        // set_tx_bd enable interrupt generation
5468
        // mask interrupts
5469
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5470
        // detect carrier sense in FD and no collision
5471
        eth_phy.carrier_sense_tx_fd_detect(1);
5472
        eth_phy.collision(0);
5473
      end
5474
      2'h2: // Interrupt is not generated
5475
      begin
5476
        // set_tx_bd disable the interrupt generation
5477
        // unmask interrupts
5478
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
5479
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5480
        // not detect carrier sense in FD and set collision
5481
        eth_phy.carrier_sense_tx_fd_detect(0);
5482
        eth_phy.collision(1);
5483
      end
5484
      default: // 2'h3: // Interrupt is not generated
5485
      begin
5486
        // set_tx_bd disable the interrupt generation
5487
        // mask interrupts
5488
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5489
        // detect carrier sense in FD and set collision
5490
        eth_phy.carrier_sense_tx_fd_detect(1);
5491
        eth_phy.collision(1);
5492
      end
5493
      endcase
5494
      // first destination address on ethernet PHY
5495
      if (i_length[0] == 0)
5496
        eth_phy.set_tx_mem_addr(0);
5497
      else
5498
        eth_phy.set_tx_mem_addr(max_tmp);
5499
      // first 8 frames are transmitted with TX BD 0 (wrap bit on TX BD 0)
5500
      // number of all frames is 154 (146 without first 8)
5501
      if (num_of_frames < 8)
5502
      begin
5503
        case (i_length[1:0])
5504
        2'h0: // Interrupt is generated
5505
        begin
5506
          // enable interrupt generation
5507
          set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
5508
          // interrupts are unmasked
5509
        end
5510
        2'h1: // Interrupt is not generated
5511
        begin
5512
          // enable interrupt generation
5513
          set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
5514
          // interrupts are masked
5515
        end
5516
        2'h2: // Interrupt is not generated
5517
        begin
5518
          // disable interrupt generation
5519
          set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
5520
          // interrupts are unmasked
5521
        end
5522
        default: // 2'h3: // Interrupt is not generated
5523
        begin
5524
          // disable interrupt generation
5525
          set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
5526
          // interrupts are masked
5527
        end
5528
        endcase
5529
        // set wrap bit
5530
        set_tx_bd_wrap(0);
5531
      end
5532
      // after first 8 number of frames, 128 frames form TX BD 0 to 127 will be transmitted
5533
      else if ((num_of_frames - 8) == 0)
5534
      begin
5535
        tmp_len = i_length; // length of frame
5536
        tmp_bd_num = 0; // TX BD number
5537
        while (tmp_bd_num < 128) // (tmp_len <= (max_tmp - 4)) - this is the last frame
5538
        begin
5539
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5540
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5541
          if (tmp_len[0] == 0)
5542
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
5543
          else
5544
            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));
5545
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
5546
          if ((tmp_len + 4) < (min_tmp + 128))
5547
            tmp_len = tmp_len + 1;
5548
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5549
            tmp_len = 256;
5550
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5551
            tmp_len = tmp_len + 128;
5552
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
5553
            tmp_len = max_tmp - (4 + 16);
5554
          else if ((tmp_len + 4) >= (max_tmp - 16))
5555
            tmp_len = tmp_len + 1;
5556
          // set TX BD number
5557
          tmp_bd_num = tmp_bd_num + 1;
5558
        end
5559
        // set wrap bit
5560
        set_tx_bd_wrap(127);
5561
      end
5562
      // after 128 + first 8 number of frames, 19 frames form TX BD 0 to 18 will be transmitted
5563
      else if ((num_of_frames - 8) == 20) // 128
5564
      begin
5565
        tmp_len = tmp_len; // length of frame remaines from previous settings
5566
        tmp_bd_num = 0; // TX BD number
5567
        while (tmp_bd_num < 19) // (tmp_len <= (max_tmp - 4)) - this is the last frame
5568
        begin
5569
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5570
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5571
          if (tmp_len[0] == 0)
5572
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
5573
          else
5574
            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));
5575
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
5576
          if ((tmp_len + 4) < (min_tmp + 128))
5577
            tmp_len = tmp_len + 1;
5578
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5579
            tmp_len = 256;
5580
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5581
            tmp_len = tmp_len + 128;
5582
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
5583
            tmp_len = max_tmp - (4 + 16);
5584
          else if ((tmp_len + 4) >= (max_tmp - 16))
5585
            tmp_len = tmp_len + 1;
5586
          // set TX BD number
5587
          tmp_bd_num = tmp_bd_num + 1;
5588
        end
5589
      end
5590
      // set ready bit
5591
      if (num_of_frames < 8)
5592
        set_tx_bd_ready(0, 0);
5593
      else if ((num_of_frames - 8) < 128)
5594
        set_tx_bd_ready((num_of_frames - 8), (num_of_frames - 8));
5595
      else if ((num_of_frames - 136) < 19)
5596
        set_tx_bd_ready((num_of_frames - 136), (num_of_frames - 136));
5597
      // CHECK END OF TRANSMITION
5598
      #1 check_tx_bd(num_of_bd, data);
5599
      if (i_length < min_tmp) // just first four
5600
      begin
5601
        while (data[15] === 1)
5602
        begin
5603
          #1 check_tx_bd(num_of_bd, data);
5604
          @(posedge wb_clk);
5605
        end
5606
        repeat (1) @(posedge wb_clk);
5607
      end
5608
      else if (i_length > (max_tmp - 8)) // just last four
5609
      begin
5610
        tmp = 0;
5611
        wait (MTxEn === 1'b1); // start transmit
5612
        while (tmp < (i_length - 20))
5613
        begin
5614
          #1 tmp = tmp + 1;
5615
          @(posedge wb_clk);
5616
        end
5617
        #1 check_tx_bd(num_of_bd, data);
5618
        while (data[15] === 1)
5619
        begin
5620
          #1 check_tx_bd(num_of_bd, data);
5621
          @(posedge wb_clk);
5622
        end
5623
        repeat (1) @(posedge wb_clk);
5624
      end
5625
      else
5626
      begin
5627
        wait (MTxEn === 1'b1); // start transmit
5628
        #1 check_tx_bd(num_of_bd, data);
5629
        if (data[15] !== 1)
5630
        begin
5631
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
5632
          fail = fail + 1;
5633
        end
5634
        wait (MTxEn === 1'b0); // end transmit
5635
        while (data[15] === 1)
5636
        begin
5637
          #1 check_tx_bd(num_of_bd, data);
5638
          @(posedge wb_clk);
5639
        end
5640
        repeat (1) @(posedge wb_clk);
5641
      end
5642
      // check length of a PACKET
5643
      if (eth_phy.tx_len != (i_length + 4))
5644
      begin
5645
        test_fail("Wrong length of the packet out from MAC");
5646
        fail = fail + 1;
5647
      end
5648
      // checking in the following if statement is performed only for first and last 64 lengths
5649
        // check transmitted TX packet data
5650
        if (i_length[0] == 0)
5651
        begin
5652
          check_tx_packet((`MEMORY_BASE + i_length[1:0]), 0, i_length, tmp);
5653
        end
5654
        else
5655
        begin
5656
          check_tx_packet(((`MEMORY_BASE + i_length[1:0]) + max_tmp), max_tmp, i_length, tmp);
5657
        end
5658
        if (tmp > 0)
5659
        begin
5660
          test_fail("Wrong data of the transmitted packet");
5661
          fail = fail + 1;
5662
        end
5663
        // check transmited TX packet CRC
5664
        if (i_length[0] == 0)
5665
          check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
5666
        else
5667
          check_tx_crc(max_tmp, i_length, 1'b0, tmp); // length without CRC
5668
        if (tmp > 0)
5669
        begin
5670
          test_fail("Wrong CRC of the transmitted packet");
5671
          fail = fail + 1;
5672
        end
5673
      // check WB INT signal
5674
      if (i_length[1:0] == 2'h0)
5675
      begin
5676
        if (wb_int !== 1'b1)
5677
        begin
5678
          `TIME; $display("*E WB INT signal should be set");
5679
          test_fail("WB INT signal should be set");
5680
          fail = fail + 1;
5681
        end
5682
      end
5683
      else
5684
      begin
5685
        if (wb_int !== 1'b0)
5686
        begin
5687
          `TIME; $display("*E WB INT signal should not be set");
5688
          test_fail("WB INT signal should not be set");
5689
          fail = fail + 1;
5690
        end
5691
      end
5692
      // check TX buffer descriptor of a packet
5693
      check_tx_bd(num_of_bd, data);
5694
      if (i_length[1] == 1'b0) // interrupt enabled
5695
      begin
5696
        if ( ((data[15:0] !== 16'h7800) && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
5697
             ((data[15:0] !== 16'h5800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
5698
        begin
5699
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
5700
          test_fail("TX buffer descriptor status is not correct");
5701
          fail = fail + 1;
5702
        end
5703
      end
5704
      else // interrupt not enabled
5705
      begin
5706
        if ( ((data[15:0] !== 16'h3800)  && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
5707
             ((data[15:0] !== 16'h1800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
5708
        begin
5709
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
5710
          test_fail("TX buffer descriptor status is not correct");
5711
          fail = fail + 1;
5712
        end
5713
      end
5714
      // clear first half of 8 frames from TX buffer descriptor 0
5715
      if (num_of_frames < 4)
5716
        clear_tx_bd(num_of_bd, num_of_bd);
5717
      // clear BD with wrap bit
5718
      if (num_of_frames == 140)
5719
        clear_tx_bd(127, 127);
5720
      // check interrupts
5721
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5722
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
5723
      begin
5724
        if ((data & `ETH_INT_TXB) !== 1'b1)
5725
        begin
5726
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
5727
          test_fail("Interrupt Transmit Buffer was not set");
5728
          fail = fail + 1;
5729
        end
5730
        if ((data & (~`ETH_INT_TXB)) !== 0)
5731
        begin
5732
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
5733
          test_fail("Other interrupts (except Transmit Buffer) were set");
5734
          fail = fail + 1;
5735
        end
5736
      end
5737
      else
5738
      begin
5739
        if (data !== 0)
5740
        begin
5741
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
5742
          test_fail("Any of interrupts (except Transmit Buffer) was set");
5743
          fail = fail + 1;
5744
        end
5745
      end
5746
      // clear interrupts
5747
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5748
      // check WB INT signal
5749
      if (wb_int !== 1'b0)
5750
      begin
5751
        test_fail("WB INT signal should not be set");
5752
        fail = fail + 1;
5753
      end
5754
      // INTERMEDIATE DISPLAYS
5755
      if ((i_length + 4) == (min_tmp + 7))
5756
      begin
5757
        // starting length is min_tmp, ending length is (min_tmp + 128)
5758
        $display("    pads appending to packets is NOT selected");
5759
        $display("    using only TX BD 0 out of 128 BDs assigned to TX (wrap at first BD - TX BD 0)");
5760
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
5761
                 min_tmp, (min_tmp + 7));
5762
        $display("    ->all packets were send from TX BD 0");
5763
        // set padding, remain the rest
5764
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5765
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5766
      end
5767
      else if ((i_length + 4) == (min_tmp + 128))
5768
      begin
5769
        // starting length is min_tmp, ending length is (min_tmp + 128)
5770
        $display("    pads appending to packets is NOT selected");
5771
        $display("    using all 128 BDs assigned to TX (wrap at 128th BD - TX BD 127)");
5772
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
5773
                 (min_tmp + 8), (min_tmp + 128));
5774
        $display("    ->packets were send from TX BD %0d to TX BD %0d respectively",
5775
                 1'b0, num_of_bd);
5776
        tmp_bd = num_of_bd + 1;
5777
        // set padding, remain the rest
5778
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5779
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5780
      end
5781
      else if ((i_length + 4) == (max_tmp - 16))
5782
      begin
5783
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
5784
        $display("    pads appending to packets is selected");
5785
        $display("    using all 128 BDs assigned to TX (wrap at 128th BD - TX BD 127)");
5786
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
5787
                 (min_tmp + 64 + 128), tmp_data);
5788
        if (tmp_bd > num_of_bd)
5789
          $display("    ->packets were send from TX BD %0d to TX BD 127 and from TX BD 0 to TX BD %0d respectively",
5790
                   tmp_bd, num_of_bd);
5791
        else
5792
          $display("    ->packets were send from TX BD %0d to TX BD %0d respectively",
5793
                   tmp_bd, num_of_bd);
5794
        tmp_bd = num_of_bd + 1;
5795
        // reset padding, remain the rest
5796
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
5797
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5798
      end
5799
      else if ((i_length + 4) == max_tmp)
5800
      begin
5801
        $display("    pads appending to packets is NOT selected");
5802
        $display("    using all 128 BDs assigned to TX (wrap at 128th BD - TX BD 127)");
5803
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
5804
                 (max_tmp - (4 + 16)), max_tmp);
5805
        if (tmp_bd > num_of_bd)
5806
          $display("    ->packets were send from TX BD %0d to TX BD 127 and from TX BD 0 to TX BD %0d respectively",
5807
                   tmp_bd, num_of_bd);
5808
        else
5809
          $display("    ->packets were send from TX BD %0d to TX BD %0d respectively",
5810
                   tmp_bd, num_of_bd);
5811
      end
5812
      // set length (loop variable)
5813
      if ((i_length + 4) < (min_tmp + 128))
5814
        i_length = i_length + 1;
5815
      else if ( ((i_length + 4) == (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
5816
        i_length = 256;
5817
      else if ( ((i_length + 4) > (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
5818
      begin
5819
        i_length = i_length + 128;
5820
        tmp_data = i_length + 4; // last tmp_data is ending length
5821
      end
5822
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
5823
        i_length = max_tmp - (4 + 16);
5824
      else if ((i_length + 4) >= (max_tmp - 16))
5825
        i_length = i_length + 1;
5826
      else
5827
      begin
5828
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
5829
        #10 $stop;
5830
      end
5831
      // the number of frame transmitted
5832
      num_of_frames = num_of_frames + 1;
5833
      if ((num_of_frames <= 8) || ((num_of_frames - 8) == 128))
5834
        num_of_bd = 0;
5835
      else
5836
        num_of_bd = num_of_bd + 1;
5837
    end
5838
    // disable TX
5839
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5840
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5841
    @(posedge wb_clk);
5842
    if(fail == 0)
5843
      test_ok;
5844
    else
5845
      fail = 0;
5846
  end
5847
 
5848
 
5849
  ////////////////////////////////////////////////////////////////////
5850
  ////                                                            ////
5851
  ////  Test transmit packets form 0 to (MINFL - 1) sizes at      ////
5852
  ////  8 TX buffer decriptors ( 10Mbps ).                        ////
5853
  ////                                                            ////
5854
  ////////////////////////////////////////////////////////////////////
5855
  if (test_num == 6) // 
5856
  begin
5857
    // TEST 6: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )
5858
    test_name = "TEST 6: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )";
5859
    `TIME; $display("  TEST 6: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )");
5860
 
5861
    // reset MAC registers
5862
    hard_reset;
5863
    // reset MAC and MII LOGIC with soft reset
5864
    reset_mac;
5865
    reset_mii;
5866
    // set wb slave response
5867
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
5868
 
5869
    max_tmp = 0;
5870
    min_tmp = 0;
5871
    // set 8 TX buffer descriptors - must be set before TX enable
5872
    wbm_write(`ETH_TX_BD_NUM, 32'h8, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5873
    // enable TX, set full-duplex mode, padding and CRC appending
5874
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
5875
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5876
    // prepare two packets of MAXFL length
5877
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5878
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
5879
    min_tmp = tmp[31:16];
5880
    st_data = 8'h12;
5881
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
5882
    st_data = 8'h34;
5883
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp - 4), st_data); // length without CRC
5884
    // check WB INT signal
5885
    if (wb_int !== 1'b0)
5886
    begin
5887
      test_fail("WB INT signal should not be set");
5888
      fail = fail + 1;
5889
    end
5890
 
5891
    // write to phy's control register for 10Mbps
5892
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
5893
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
5894
    speed = 10;
5895
 
5896
    frame_started = 0;
5897
    num_of_frames = 0;
5898
    num_of_bd = 0;
5899
    i_length = 0; // 0;
5900
    while (i_length < 70) // (min_tmp - 4))
5901
    begin
5902
      #1;
5903
      // choose generating carrier sense and collision
5904
      case (i_length[1:0])
5905
      2'h0: // Interrupt is generated
5906
      begin
5907
        // Reset_tx_bd nable interrupt generation
5908
        // unmask interrupts
5909
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
5910
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5911
        // not detect carrier sense in FD and no collision
5912
        eth_phy.carrier_sense_tx_fd_detect(0);
5913
        eth_phy.collision(0);
5914
      end
5915
      2'h1: // Interrupt is not generated
5916
      begin
5917
        // set_tx_bd enable interrupt generation
5918
        // mask interrupts
5919
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5920
        // detect carrier sense in FD and no collision
5921
        eth_phy.carrier_sense_tx_fd_detect(1);
5922
        eth_phy.collision(0);
5923
      end
5924
      2'h2: // Interrupt is not generated
5925
      begin
5926
        // set_tx_bd disable the interrupt generation
5927
        // unmask interrupts
5928
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
5929
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5930
        // not detect carrier sense in FD and set collision
5931
        eth_phy.carrier_sense_tx_fd_detect(0);
5932
        eth_phy.collision(1);
5933
      end
5934
      default: // 2'h3: // Interrupt is not generated
5935
      begin
5936
        // set_tx_bd disable the interrupt generation
5937
        // mask interrupts
5938
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5939
        // detect carrier sense in FD and set collision
5940
        eth_phy.carrier_sense_tx_fd_detect(1);
5941
        eth_phy.collision(1);
5942
      end
5943
      endcase
5944
      #1;
5945
      // first destination address on ethernet PHY
5946
      eth_phy.set_tx_mem_addr(num_of_frames * 16);
5947
      // SET packets and wrap bit
5948
      // num_of_frames <= 9 => wrap set to TX BD 0
5949
      if (num_of_frames <= 9)
5950
      begin
5951
        tmp_len = i_length; // length of frame
5952
        tmp_bd_num = 0; // TX BD number
5953
        // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5954
        // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5955
        if (tmp_len[0] == 0)
5956
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
5957
        else
5958
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
5959
        // set wrap bit
5960
        set_tx_bd_wrap(0);
5961
      end
5962
      // 10 <= num_of_frames < 18 => wrap set to TX BD 3
5963
      else if ((num_of_frames == 10) || (num_of_frames == 14))
5964
      begin
5965
        tmp_len = i_length; // length of frame
5966
        tmp_bd_num = 0; // TX BD number
5967
        while (tmp_bd_num < 4) //
5968
        begin
5969
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5970
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5971
          if (tmp_len[0] == 0)
5972
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
5973
          else
5974
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
5975
          tmp_len = tmp_len + 1;
5976
          // set TX BD number
5977
          tmp_bd_num = tmp_bd_num + 1;
5978
        end
5979
        // set wrap bit
5980
        set_tx_bd_wrap(3);
5981
      end
5982
      // 18 <= num_of_frames < 28 => wrap set to TX BD 4
5983
      else if ((num_of_frames == 18) || (num_of_frames == 23))
5984
      begin
5985
        tmp_len = i_length; // length of frame
5986
        tmp_bd_num = 0; // TX BD number
5987
        while (tmp_bd_num < 5) //
5988
        begin
5989
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5990
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5991
          if (tmp_len[0] == 0)
5992
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
5993
          else
5994
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
5995
          tmp_len = tmp_len + 1;
5996
          // set TX BD number
5997
          tmp_bd_num = tmp_bd_num + 1;
5998
        end
5999
        // set wrap bit
6000
        set_tx_bd_wrap(4);
6001
      end
6002
      // 28 <= num_of_frames < 40 => wrap set to TX BD 5
6003
      else if ((num_of_frames == 28) || (num_of_frames == 34))
6004
      begin
6005
        tmp_len = i_length; // length of frame
6006
        tmp_bd_num = 0; // TX BD number
6007
        while (tmp_bd_num < 6) //
6008
        begin
6009
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6010
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6011
          if (tmp_len[0] == 0)
6012
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6013
          else
6014
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6015
          tmp_len = tmp_len + 1;
6016
          // set TX BD number
6017
          tmp_bd_num = tmp_bd_num + 1;
6018
        end
6019
        // set wrap bit
6020
        set_tx_bd_wrap(5);
6021
      end
6022
      // 40 <= num_of_frames < 54 => wrap set to TX BD 6
6023
      else if ((num_of_frames == 40) || (num_of_frames == 47))
6024
      begin
6025
        tmp_len = i_length; // length of frame
6026
        tmp_bd_num = 0; // TX BD number
6027
        while (tmp_bd_num < 7) //
6028
        begin
6029
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6030
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6031
          if (tmp_len[0] == 0)
6032
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6033
          else
6034
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6035
          tmp_len = tmp_len + 1;
6036
          // set TX BD number
6037
          tmp_bd_num = tmp_bd_num + 1;
6038
        end
6039
        // set wrap bit
6040
        set_tx_bd_wrap(6);
6041
      end
6042
      // 54 <= num_of_frames < 70 => wrap set to TX BD 7
6043
      else if ((num_of_frames == 54) || (num_of_frames == 62))
6044
      begin
6045
        tmp_len = i_length; // length of frame
6046
        tmp_bd_num = 0; // TX BD number
6047
        while (tmp_bd_num < 8) //
6048
        begin
6049
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6050
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6051
          if (tmp_len[0] == 0)
6052
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6053
          else
6054
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6055
          tmp_len = tmp_len + 1;
6056
          // set TX BD number
6057
          tmp_bd_num = tmp_bd_num + 1;
6058
        end
6059
        // set wrap bit
6060
        set_tx_bd_wrap(7);
6061
      end
6062
      #1;
6063
      // SET ready bit
6064
      if (num_of_frames < 10)
6065
        set_tx_bd_ready(0, 0);
6066
      else if (num_of_frames < 14)
6067
        set_tx_bd_ready((num_of_frames - 10), (num_of_frames - 10));
6068
      else if (num_of_frames < 18)
6069
        set_tx_bd_ready((num_of_frames - 14), (num_of_frames - 14));
6070
      else if (num_of_frames < 23)
6071
        set_tx_bd_ready((num_of_frames - 18), (num_of_frames - 18));
6072
      else if (num_of_frames < 28)
6073
        set_tx_bd_ready((num_of_frames - 23), (num_of_frames - 23));
6074
      else if (num_of_frames < 34)
6075
        set_tx_bd_ready((num_of_frames - 28), (num_of_frames - 28));
6076
      else if (num_of_frames < 40)
6077
        set_tx_bd_ready((num_of_frames - 34), (num_of_frames - 34));
6078
      else if (num_of_frames < 47)
6079
        set_tx_bd_ready((num_of_frames - 40), (num_of_frames - 40));
6080
      else if (num_of_frames < 54)
6081
        set_tx_bd_ready((num_of_frames - 47), (num_of_frames - 47));
6082
      else if (num_of_frames < 62)
6083
        set_tx_bd_ready((num_of_frames - 54), (num_of_frames - 54));
6084
      else if (num_of_frames < 70)
6085
        set_tx_bd_ready((num_of_frames - 62), (num_of_frames - 62));
6086
      // CHECK END OF TRANSMITION
6087
      frame_started = 0;
6088
      if (num_of_frames >= 5)
6089
        #1 check_tx_bd(num_of_bd, data);
6090
      fork
6091
      begin: fr_st
6092
        wait (MTxEn === 1'b1); // start transmit
6093
        frame_started = 1;
6094
      end
6095
      begin
6096
        repeat (30) @(posedge mtx_clk);
6097
        if (num_of_frames < 5)
6098
        begin
6099
          if (frame_started == 1)
6100
          begin
6101
            `TIME; $display("*E Frame should NOT start!");
6102
          end
6103
          disable fr_st;
6104
        end
6105
        else
6106
        begin
6107
          if (frame_started == 0)
6108
          begin
6109
            `TIME; $display("*W Frame should start!");
6110
            disable fr_st;
6111
          end
6112
        end
6113
      end
6114
      join
6115
      // check packets larger than 4 bytes
6116
      if (num_of_frames >= 5)
6117
      begin
6118
        wait (MTxEn === 1'b0); // end transmit
6119
        while (data[15] === 1)
6120
        begin
6121
          #1 check_tx_bd(num_of_bd, data);
6122
          @(posedge wb_clk);
6123
        end
6124
        repeat (1) @(posedge wb_clk);
6125
        // check length of a PACKET
6126
        if (i_length <= (min_tmp - 4))
6127
        begin
6128
          if (eth_phy.tx_len != min_tmp)
6129
          begin
6130
            test_fail("Wrong length of the packet out from MAC");
6131
            fail = fail + 1;
6132
          end
6133
        end
6134
        else
6135
        begin
6136
          if (eth_phy.tx_len != (i_length + 4))
6137
          begin
6138
            test_fail("Wrong length of the packet out from MAC");
6139
            fail = fail + 1;
6140
          end
6141
        end
6142
        // check transmitted TX packet data
6143
        if (i_length[0] == 0)
6144
        begin
6145
          #1 check_tx_packet(`MEMORY_BASE, (num_of_frames * 16), i_length, tmp);
6146
        end
6147
        else
6148
        begin
6149
          #1 check_tx_packet((`MEMORY_BASE + max_tmp), (num_of_frames * 16), i_length, tmp);
6150
        end
6151
        if (tmp > 0)
6152
        begin
6153
          test_fail("Wrong data of the transmitted packet");
6154
          fail = fail + 1;
6155
        end
6156
        // check transmited TX packet CRC
6157
        if (num_of_frames < (min_tmp - 4))
6158
          #1 check_tx_crc((num_of_frames * 16), (min_tmp - 4), 1'b0, tmp); // length without CRC
6159
        else
6160
          #1 check_tx_crc((num_of_frames * 16), i_length, 1'b0, tmp); // length without CRC
6161
        if (tmp > 0)
6162
        begin
6163
          test_fail("Wrong CRC of the transmitted packet");
6164
          fail = fail + 1;
6165
        end
6166
      end
6167
      // check WB INT signal
6168
      if ((i_length[1:0] == 2'h0) && (num_of_frames >= 5))
6169
      begin
6170
        if (wb_int !== 1'b1)
6171
        begin
6172
          `TIME; $display("*E WB INT signal should be set");
6173
          test_fail("WB INT signal should be set");
6174
          fail = fail + 1;
6175
        end
6176
      end
6177
      else
6178
      begin
6179
        if (wb_int !== 1'b0)
6180
        begin
6181
          `TIME; $display("*E WB INT signal should not be set");
6182
          test_fail("WB INT signal should not be set");
6183
          fail = fail + 1;
6184
        end
6185
      end
6186
      // check TX buffer descriptor of a packet
6187
      check_tx_bd(num_of_bd, data);
6188
      if (num_of_frames >= 5)
6189
      begin
6190
        if (i_length[1] == 1'b0) // interrupt enabled
6191
        begin
6192
          if ( (data[15:0] !== 16'h7800) && // wrap bit
6193
               (data[15:0] !== 16'h5800) ) // without wrap bit
6194
          begin
6195
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6196
            test_fail("TX buffer descriptor status is not correct");
6197
            fail = fail + 1;
6198
          end
6199
        end
6200
        else // interrupt not enabled
6201
        begin
6202
          if ( (data[15:0] !== 16'h3800) && // wrap bit
6203
               (data[15:0] !== 16'h1800) ) // without wrap bit
6204
          begin
6205
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6206
            test_fail("TX buffer descriptor status is not correct");
6207
            fail = fail + 1;
6208
          end
6209
        end
6210
      end
6211
      else
6212
      begin
6213
        if (data[15] !== 1'b1)
6214
        begin
6215
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6216
          test_fail("TX buffer descriptor status is not correct");
6217
          fail = fail + 1;
6218
        end
6219
      end
6220
      // clear TX BD with wrap bit
6221
      if (num_of_frames == 63)
6222
        clear_tx_bd(16, 16);
6223
      // check interrupts
6224
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6225
      if ( ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1)) && (num_of_frames >= 5) )
6226
      begin
6227
        if ((data & `ETH_INT_TXB) !== 1'b1)
6228
        begin
6229
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
6230
          test_fail("Interrupt Transmit Buffer was not set");
6231
          fail = fail + 1;
6232
        end
6233
        if ((data & (~`ETH_INT_TXB)) !== 0)
6234
        begin
6235
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
6236
          test_fail("Other interrupts (except Transmit Buffer) were set");
6237
          fail = fail + 1;
6238
        end
6239
      end
6240
      else
6241
      begin
6242
        if (data !== 0)
6243
        begin
6244
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
6245
          test_fail("Any of interrupts (except Transmit Buffer) was set");
6246
          fail = fail + 1;
6247
        end
6248
      end
6249
      // clear interrupts
6250
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6251
      // check WB INT signal
6252
      if (wb_int !== 1'b0)
6253
      begin
6254
        test_fail("WB INT signal should not be set");
6255
        fail = fail + 1;
6256
      end
6257
      // INTERMEDIATE DISPLAYS
6258
      if (i_length == 3)
6259
      begin
6260
        $display("    pads appending to packets is selected");
6261
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
6262
        $display("    ->packets with lengths from %0d to %0d are not transmitted (length increasing by 1 byte)",
6263
                 0, 3);
6264
      end
6265
      else if (i_length == 9)
6266
      begin
6267
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
6268
        $display("    ->packet with length 4 is not transmitted (length increasing by 1 byte)");
6269
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6270
                 5, 9);
6271
      end
6272
      else if (i_length == 17)
6273
      begin
6274
        $display("    using 4 BDs out of 8 BDs assigned to TX (wrap at 4th BD - TX BD 3)");
6275
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6276
                 10, 17);
6277
      end
6278
      else if (i_length == 27)
6279
      begin
6280
        $display("    using 5 BDs out of 8 BDs assigned to TX (wrap at 5th BD - TX BD 4)");
6281
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6282
                 18, 27);
6283
      end
6284
      else if (i_length == 40)
6285
      begin
6286
        $display("    using 6 BDs out of 8 BDs assigned to TX (wrap at 6th BD - TX BD 5)");
6287
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6288
                 28, 40);
6289
      end
6290
      else if (i_length == 54)
6291
      begin
6292
        $display("    using 7 BDs out of 8 BDs assigned to TX (wrap at 7th BD - TX BD 6)");
6293
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6294
                 41, 54);
6295
      end
6296
      else if (i_length == 69)
6297
      begin
6298
        $display("    using 8 BDs out of 8 BDs assigned to TX (wrap at 8th BD - TX BD 7)");
6299
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6300
                 55, 69);
6301
      end
6302
      // set length (loop variable)
6303
      i_length = i_length + 1;
6304
      // the number of frame transmitted
6305
      num_of_frames = num_of_frames + 1;
6306
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
6307
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
6308
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
6309
          (num_of_frames == 54) || (num_of_frames == 62))
6310
        num_of_bd = 0;
6311
      else
6312
        num_of_bd = num_of_bd + 1;
6313
    end
6314
    // disable TX
6315
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
6316
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6317
    @(posedge wb_clk);
6318
    if(fail == 0)
6319
      test_ok;
6320
    else
6321
      fail = 0;
6322
  end
6323
 
6324
 
6325
  ////////////////////////////////////////////////////////////////////
6326
  ////                                                            ////
6327
  ////  Test transmit packets form 0 to (MINFL - 1) sizes at      ////
6328
  ////  8 TX buffer decriptors ( 100Mbps ).                       ////
6329
  ////                                                            ////
6330
  ////////////////////////////////////////////////////////////////////
6331
  if (test_num == 7) // 
6332
  begin
6333
    // TEST 7: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )
6334
    test_name = "TEST 7: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )";
6335
    `TIME; $display("  TEST 7: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )");
6336
 
6337
    // reset MAC registers
6338
    hard_reset;
6339
    // reset MAC and MII LOGIC with soft reset
6340
    reset_mac;
6341
    reset_mii;
6342
    // set wb slave response
6343
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
6344
 
6345
    max_tmp = 0;
6346
    min_tmp = 0;
6347
    // set 8 TX buffer descriptors - must be set before TX enable
6348
    wbm_write(`ETH_TX_BD_NUM, 32'h8, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6349
    // enable TX, set full-duplex mode, padding and CRC appending
6350
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
6351
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6352
    // prepare two packets of MAXFL length
6353
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6354
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
6355
    min_tmp = tmp[31:16];
6356
    st_data = 8'h12;
6357
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
6358
    st_data = 8'h34;
6359
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp - 4), st_data); // length without CRC
6360
    // check WB INT signal
6361
    if (wb_int !== 1'b0)
6362
    begin
6363
      test_fail("WB INT signal should not be set");
6364
      fail = fail + 1;
6365
    end
6366
 
6367
    // write to phy's control register for 100Mbps
6368
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
6369
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
6370
    speed = 100;
6371
 
6372
    frame_started = 0;
6373
    num_of_frames = 0;
6374
    num_of_bd = 0;
6375
    i_length = 0; // 0;
6376
    while (i_length < 70) // (min_tmp - 4))
6377
    begin
6378
      #1;
6379
      // choose generating carrier sense and collision
6380
      case (i_length[1:0])
6381
      2'h0: // Interrupt is generated
6382
      begin
6383
        // Reset_tx_bd nable interrupt generation
6384
        // unmask interrupts
6385
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
6386
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6387
        // not detect carrier sense in FD and no collision
6388
        eth_phy.carrier_sense_tx_fd_detect(0);
6389
        eth_phy.collision(0);
6390
      end
6391
      2'h1: // Interrupt is not generated
6392
      begin
6393
        // set_tx_bd enable interrupt generation
6394
        // mask interrupts
6395
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6396
        // detect carrier sense in FD and no collision
6397
        eth_phy.carrier_sense_tx_fd_detect(1);
6398
        eth_phy.collision(0);
6399
      end
6400
      2'h2: // Interrupt is not generated
6401
      begin
6402
        // set_tx_bd disable the interrupt generation
6403
        // unmask interrupts
6404
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
6405
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6406
        // not detect carrier sense in FD and set collision
6407
        eth_phy.carrier_sense_tx_fd_detect(0);
6408
        eth_phy.collision(1);
6409
      end
6410
      default: // 2'h3: // Interrupt is not generated
6411
      begin
6412
        // set_tx_bd disable the interrupt generation
6413
        // mask interrupts
6414
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6415
        // detect carrier sense in FD and set collision
6416
        eth_phy.carrier_sense_tx_fd_detect(1);
6417
        eth_phy.collision(1);
6418
      end
6419
      endcase
6420
      #1;
6421
      // first destination address on ethernet PHY
6422
      eth_phy.set_tx_mem_addr(num_of_frames * 16);
6423
      // SET packets and wrap bit
6424
      // num_of_frames <= 9 => wrap set to TX BD 0
6425
      if (num_of_frames <= 9)
6426
      begin
6427
        tmp_len = i_length; // length of frame
6428
        tmp_bd_num = 0; // TX BD number
6429
        // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6430
        // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6431
        if (tmp_len[0] == 0)
6432
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6433
        else
6434
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6435
        // set wrap bit
6436
        set_tx_bd_wrap(0);
6437
      end
6438
      // 10 <= num_of_frames < 18 => wrap set to TX BD 3
6439
      else if ((num_of_frames == 10) || (num_of_frames == 14))
6440
      begin
6441
        tmp_len = i_length; // length of frame
6442
        tmp_bd_num = 0; // TX BD number
6443
        while (tmp_bd_num < 4) //
6444
        begin
6445
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6446
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6447
          if (tmp_len[0] == 0)
6448
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6449
          else
6450
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6451
          tmp_len = tmp_len + 1;
6452
          // set TX BD number
6453
          tmp_bd_num = tmp_bd_num + 1;
6454
        end
6455
        // set wrap bit
6456
        set_tx_bd_wrap(3);
6457
      end
6458
      // 18 <= num_of_frames < 28 => wrap set to TX BD 4
6459
      else if ((num_of_frames == 18) || (num_of_frames == 23))
6460
      begin
6461
        tmp_len = i_length; // length of frame
6462
        tmp_bd_num = 0; // TX BD number
6463
        while (tmp_bd_num < 5) //
6464
        begin
6465
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6466
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6467
          if (tmp_len[0] == 0)
6468
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6469
          else
6470
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6471
          tmp_len = tmp_len + 1;
6472
          // set TX BD number
6473
          tmp_bd_num = tmp_bd_num + 1;
6474
        end
6475
        // set wrap bit
6476
        set_tx_bd_wrap(4);
6477
      end
6478
      // 28 <= num_of_frames < 40 => wrap set to TX BD 5
6479
      else if ((num_of_frames == 28) || (num_of_frames == 34))
6480
      begin
6481
        tmp_len = i_length; // length of frame
6482
        tmp_bd_num = 0; // TX BD number
6483
        while (tmp_bd_num < 6) //
6484
        begin
6485
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6486
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6487
          if (tmp_len[0] == 0)
6488
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6489
          else
6490
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6491
          tmp_len = tmp_len + 1;
6492
          // set TX BD number
6493
          tmp_bd_num = tmp_bd_num + 1;
6494
        end
6495
        // set wrap bit
6496
        set_tx_bd_wrap(5);
6497
      end
6498
      // 40 <= num_of_frames < 54 => wrap set to TX BD 6
6499
      else if ((num_of_frames == 40) || (num_of_frames == 47))
6500
      begin
6501
        tmp_len = i_length; // length of frame
6502
        tmp_bd_num = 0; // TX BD number
6503
        while (tmp_bd_num < 7) //
6504
        begin
6505
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6506
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6507
          if (tmp_len[0] == 0)
6508
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6509
          else
6510
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6511
          tmp_len = tmp_len + 1;
6512
          // set TX BD number
6513
          tmp_bd_num = tmp_bd_num + 1;
6514
        end
6515
        // set wrap bit
6516
        set_tx_bd_wrap(6);
6517
      end
6518
      // 54 <= num_of_frames < 70 => wrap set to TX BD 7
6519
      else if ((num_of_frames == 54) || (num_of_frames == 62))
6520
      begin
6521
        tmp_len = i_length; // length of frame
6522
        tmp_bd_num = 0; // TX BD number
6523
        while (tmp_bd_num < 8) //
6524
        begin
6525
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6526
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6527
          if (tmp_len[0] == 0)
6528
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6529
          else
6530
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6531
          tmp_len = tmp_len + 1;
6532
          // set TX BD number
6533
          tmp_bd_num = tmp_bd_num + 1;
6534
        end
6535
        // set wrap bit
6536
        set_tx_bd_wrap(7);
6537
      end
6538
      #1;
6539
      // SET ready bit
6540
      if (num_of_frames < 10)
6541
        set_tx_bd_ready(0, 0);
6542
      else if (num_of_frames < 14)
6543
        set_tx_bd_ready((num_of_frames - 10), (num_of_frames - 10));
6544
      else if (num_of_frames < 18)
6545
        set_tx_bd_ready((num_of_frames - 14), (num_of_frames - 14));
6546
      else if (num_of_frames < 23)
6547
        set_tx_bd_ready((num_of_frames - 18), (num_of_frames - 18));
6548
      else if (num_of_frames < 28)
6549
        set_tx_bd_ready((num_of_frames - 23), (num_of_frames - 23));
6550
      else if (num_of_frames < 34)
6551
        set_tx_bd_ready((num_of_frames - 28), (num_of_frames - 28));
6552
      else if (num_of_frames < 40)
6553
        set_tx_bd_ready((num_of_frames - 34), (num_of_frames - 34));
6554
      else if (num_of_frames < 47)
6555
        set_tx_bd_ready((num_of_frames - 40), (num_of_frames - 40));
6556
      else if (num_of_frames < 54)
6557
        set_tx_bd_ready((num_of_frames - 47), (num_of_frames - 47));
6558
      else if (num_of_frames < 62)
6559
        set_tx_bd_ready((num_of_frames - 54), (num_of_frames - 54));
6560
      else if (num_of_frames < 70)
6561
        set_tx_bd_ready((num_of_frames - 62), (num_of_frames - 62));
6562
      // CHECK END OF TRANSMITION
6563
      frame_started = 0;
6564
      if (num_of_frames >= 5)
6565
        #1 check_tx_bd(num_of_bd, data);
6566
      fork
6567
      begin: fr_st1
6568
        wait (MTxEn === 1'b1); // start transmit
6569
        frame_started = 1;
6570
      end
6571
      begin
6572
        repeat (30) @(posedge mtx_clk);
6573
        if (num_of_frames < 5)
6574
        begin
6575
          if (frame_started == 1)
6576
          begin
6577
            `TIME; $display("*E Frame should NOT start!");
6578
          end
6579
          disable fr_st1;
6580
        end
6581
        else
6582
        begin
6583
          if (frame_started == 0)
6584
          begin
6585
            `TIME; $display("*W Frame should start!");
6586
            disable fr_st1;
6587
          end
6588
        end
6589
      end
6590
      join
6591
      // check packets larger than 4 bytes
6592
      if (num_of_frames >= 5)
6593
      begin
6594
        wait (MTxEn === 1'b0); // end transmit
6595
        while (data[15] === 1)
6596
        begin
6597
          #1 check_tx_bd(num_of_bd, data);
6598
          @(posedge wb_clk);
6599
        end
6600
        repeat (1) @(posedge wb_clk);
6601
        // check length of a PACKET
6602
        if (i_length <= (min_tmp - 4))
6603
        begin
6604
          if (eth_phy.tx_len != min_tmp)
6605
          begin
6606
            test_fail("Wrong length of the packet out from MAC");
6607
            fail = fail + 1;
6608
          end
6609
        end
6610
        else
6611
        begin
6612
          if (eth_phy.tx_len != (i_length + 4))
6613
          begin
6614
            test_fail("Wrong length of the packet out from MAC");
6615
            fail = fail + 1;
6616
          end
6617
        end
6618
        // check transmitted TX packet data
6619
        if (i_length[0] == 0)
6620
        begin
6621
          #1 check_tx_packet(`MEMORY_BASE, (num_of_frames * 16), i_length, tmp);
6622
        end
6623
        else
6624
        begin
6625
          #1 check_tx_packet((`MEMORY_BASE + max_tmp), (num_of_frames * 16), i_length, tmp);
6626
        end
6627
        if (tmp > 0)
6628
        begin
6629
          test_fail("Wrong data of the transmitted packet");
6630
          fail = fail + 1;
6631
        end
6632
        // check transmited TX packet CRC
6633
        if (num_of_frames < (min_tmp - 4))
6634
          #1 check_tx_crc((num_of_frames * 16), (min_tmp - 4), 1'b0, tmp); // length without CRC
6635
        else
6636
          #1 check_tx_crc((num_of_frames * 16), i_length, 1'b0, tmp); // length without CRC
6637
        if (tmp > 0)
6638
        begin
6639
          test_fail("Wrong CRC of the transmitted packet");
6640
          fail = fail + 1;
6641
        end
6642
      end
6643
      // check WB INT signal
6644
      if ((i_length[1:0] == 2'h0) && (num_of_frames >= 5))
6645
      begin
6646
        if (wb_int !== 1'b1)
6647
        begin
6648
          `TIME; $display("*E WB INT signal should be set");
6649
          test_fail("WB INT signal should be set");
6650
          fail = fail + 1;
6651
        end
6652
      end
6653
      else
6654
      begin
6655
        if (wb_int !== 1'b0)
6656
        begin
6657
          `TIME; $display("*E WB INT signal should not be set");
6658
          test_fail("WB INT signal should not be set");
6659
          fail = fail + 1;
6660
        end
6661
      end
6662
      // check TX buffer descriptor of a packet
6663
      check_tx_bd(num_of_bd, data);
6664
      if (num_of_frames >= 5)
6665
      begin
6666
        if (i_length[1] == 1'b0) // interrupt enabled
6667
        begin
6668
          if ( (data[15:0] !== 16'h7800) && // wrap bit
6669
               (data[15:0] !== 16'h5800) ) // without wrap bit
6670
          begin
6671
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6672
            test_fail("TX buffer descriptor status is not correct");
6673
            fail = fail + 1;
6674
          end
6675
        end
6676
        else // interrupt not enabled
6677
        begin
6678
          if ( (data[15:0] !== 16'h3800) && // wrap bit
6679
               (data[15:0] !== 16'h1800) ) // without wrap bit
6680
          begin
6681
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6682
            test_fail("TX buffer descriptor status is not correct");
6683
            fail = fail + 1;
6684
          end
6685
        end
6686
      end
6687
      else
6688
      begin
6689
        if (data[15] !== 1'b1)
6690
        begin
6691
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6692
          test_fail("TX buffer descriptor status is not correct");
6693
          fail = fail + 1;
6694
        end
6695
      end
6696
      // clear TX BD with wrap bit
6697
      if (num_of_frames == 63)
6698
        clear_tx_bd(16, 16);
6699
      // check interrupts
6700
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6701
      if ( ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1)) && (num_of_frames >= 5) )
6702
      begin
6703
        if ((data & `ETH_INT_TXB) !== 1'b1)
6704
        begin
6705
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
6706
          test_fail("Interrupt Transmit Buffer was not set");
6707
          fail = fail + 1;
6708
        end
6709
        if ((data & (~`ETH_INT_TXB)) !== 0)
6710
        begin
6711
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
6712
          test_fail("Other interrupts (except Transmit Buffer) were set");
6713
          fail = fail + 1;
6714
        end
6715
      end
6716
      else
6717
      begin
6718
        if (data !== 0)
6719
        begin
6720
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
6721
          test_fail("Any of interrupts (except Transmit Buffer) was set");
6722
          fail = fail + 1;
6723
        end
6724
      end
6725
      // clear interrupts
6726
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6727
      // check WB INT signal
6728
      if (wb_int !== 1'b0)
6729
      begin
6730
        test_fail("WB INT signal should not be set");
6731
        fail = fail + 1;
6732
      end
6733
      // INTERMEDIATE DISPLAYS
6734
      if (i_length == 3)
6735
      begin
6736
        $display("    pads appending to packets is selected");
6737
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
6738
        $display("    ->packets with lengths from %0d to %0d are not transmitted (length increasing by 1 byte)",
6739
                 0, 3);
6740
      end
6741
      else if (i_length == 9)
6742
      begin
6743
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
6744
        $display("    ->packet with length 4 is not transmitted (length increasing by 1 byte)");
6745
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6746
                 5, 9);
6747
      end
6748
      else if (i_length == 17)
6749
      begin
6750
        $display("    using 4 BDs out of 8 BDs assigned to TX (wrap at 4th BD - TX BD 3)");
6751
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6752
                 10, 17);
6753
      end
6754
      else if (i_length == 27)
6755
      begin
6756
        $display("    using 5 BDs out of 8 BDs assigned to TX (wrap at 5th BD - TX BD 4)");
6757
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6758
                 18, 27);
6759
      end
6760
      else if (i_length == 40)
6761
      begin
6762
        $display("    using 6 BDs out of 8 BDs assigned to TX (wrap at 6th BD - TX BD 5)");
6763
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6764
                 28, 40);
6765
      end
6766
      else if (i_length == 54)
6767
      begin
6768
        $display("    using 7 BDs out of 8 BDs assigned to TX (wrap at 7th BD - TX BD 6)");
6769
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6770
                 41, 54);
6771
      end
6772
      else if (i_length == 69)
6773
      begin
6774
        $display("    using 8 BDs out of 8 BDs assigned to TX (wrap at 8th BD - TX BD 7)");
6775
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6776
                 55, 69);
6777
      end
6778
      // set length (loop variable)
6779
      i_length = i_length + 1;
6780
      // the number of frame transmitted
6781
      num_of_frames = num_of_frames + 1;
6782
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
6783
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
6784
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
6785
          (num_of_frames == 54) || (num_of_frames == 62))
6786
        num_of_bd = 0;
6787
      else
6788
        num_of_bd = num_of_bd + 1;
6789
    end
6790
    // disable TX
6791
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
6792
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6793
    @(posedge wb_clk);
6794
    if(fail == 0)
6795
      test_ok;
6796
    else
6797
      fail = 0;
6798
  end
6799
 
6800
 
6801
  ////////////////////////////////////////////////////////////////////
6802
  ////                                                            ////
6803 243 tadejm
  ////  Test transmit packets (no pads) from 0 to (MINFL - 1)     ////
6804 209 tadejm
  ////  sizes at 8 TX buffer decriptors ( 10Mbps ).               ////
6805
  ////                                                            ////
6806
  ////////////////////////////////////////////////////////////////////
6807
  if (test_num == 8) // 
6808
  begin
6809 243 tadejm
    // TEST 8: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )
6810
    test_name = "TEST 8: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )";
6811
    `TIME; $display("  TEST 8: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )");
6812 209 tadejm
 
6813
    // reset MAC registers
6814
    hard_reset;
6815
    // reset MAC and MII LOGIC with soft reset
6816
    reset_mac;
6817
    reset_mii;
6818
    // set wb slave response
6819
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
6820
 
6821
    max_tmp = 0;
6822
    min_tmp = 0;
6823
    // set 8 TX buffer descriptors - must be set before TX enable
6824
    wbm_write(`ETH_TX_BD_NUM, 32'h8, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6825
    // enable TX, set full-duplex mode, padding and CRC appending
6826 243 tadejm
//    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
6827
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD,
6828 209 tadejm
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6829
    // prepare two packets of MAXFL length
6830
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6831
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
6832
    min_tmp = tmp[31:16];
6833
    st_data = 8'h12;
6834
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
6835
    st_data = 8'h34;
6836
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp - 4), st_data); // length without CRC
6837
    // check WB INT signal
6838
    if (wb_int !== 1'b0)
6839
    begin
6840
      test_fail("WB INT signal should not be set");
6841
      fail = fail + 1;
6842
    end
6843
 
6844
    // write to phy's control register for 10Mbps
6845
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
6846
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
6847
    speed = 10;
6848
 
6849
    frame_started = 0;
6850
    num_of_frames = 0;
6851
    num_of_bd = 0;
6852
    i_length = 0; // 0;
6853
    while (i_length < 70) // (min_tmp - 4))
6854
    begin
6855
      #1;
6856
      // choose generating carrier sense and collision
6857
      case (i_length[1:0])
6858
      2'h0: // Interrupt is generated
6859
      begin
6860
        // Reset_tx_bd nable interrupt generation
6861
        // unmask interrupts
6862
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
6863
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6864
        // not detect carrier sense in FD and no collision
6865
        eth_phy.carrier_sense_tx_fd_detect(0);
6866
        eth_phy.collision(0);
6867
      end
6868
      2'h1: // Interrupt is not generated
6869
      begin
6870
        // set_tx_bd enable interrupt generation
6871
        // mask interrupts
6872
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6873
        // detect carrier sense in FD and no collision
6874
        eth_phy.carrier_sense_tx_fd_detect(1);
6875
        eth_phy.collision(0);
6876
      end
6877
      2'h2: // Interrupt is not generated
6878
      begin
6879
        // set_tx_bd disable the interrupt generation
6880
        // unmask interrupts
6881
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
6882
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6883
        // not detect carrier sense in FD and set collision
6884
        eth_phy.carrier_sense_tx_fd_detect(0);
6885
        eth_phy.collision(1);
6886
      end
6887
      default: // 2'h3: // Interrupt is not generated
6888
      begin
6889
        // set_tx_bd disable the interrupt generation
6890
        // mask interrupts
6891
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6892
        // detect carrier sense in FD and set collision
6893
        eth_phy.carrier_sense_tx_fd_detect(1);
6894
        eth_phy.collision(1);
6895
      end
6896
      endcase
6897 243 tadejm
//      // append CRC
6898
//      if ((i_length[0] == 1'b0) && (num_of_frames >= 6))
6899
//      begin
6900
//        append_tx_crc(`MEMORY_BASE, i_length, 1'b0);
6901
//      end
6902 209 tadejm
      #1;
6903
      // first destination address on ethernet PHY
6904
      eth_phy.set_tx_mem_addr(num_of_frames * 16);
6905
      // SET packets and wrap bit
6906
      // num_of_frames <= 9 => wrap set to TX BD 0
6907 243 tadejm
      if (num_of_frames <= 5)
6908
        begin
6909
          tmp_len = i_length; // length of frame
6910
          tmp_bd_num = 0; // TX BD number
6911
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6912
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6913
          if (tmp_len[0] == 0) // CRC appended by 'HARDWARE'
6914
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, `MEMORY_BASE);
6915
          else
6916
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
6917
          // set wrap bit
6918
          set_tx_bd_wrap(0);
6919
        end
6920
        else if (num_of_frames <= 9)
6921
        begin
6922
          tmp_len = i_length; // length of frame
6923
          tmp_bd_num = 0; // TX BD number
6924
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6925
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6926
          if (tmp_len[0] == 0) // CRC appended by 'SOFTWARE'
6927
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
6928
          else
6929
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
6930
          // set wrap bit
6931
          set_tx_bd_wrap(0);
6932 209 tadejm
      end
6933
      // 10 <= num_of_frames < 18 => wrap set to TX BD 3
6934
      else if ((num_of_frames == 10) || (num_of_frames == 14))
6935
      begin
6936
        tmp_len = i_length; // length of frame
6937
        tmp_bd_num = 0; // TX BD number
6938
        while (tmp_bd_num < 4) //
6939
        begin
6940
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6941
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6942
          if (tmp_len[0] == 0)
6943 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
6944 209 tadejm
          else
6945 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
6946 209 tadejm
          tmp_len = tmp_len + 1;
6947
          // set TX BD number
6948
          tmp_bd_num = tmp_bd_num + 1;
6949
        end
6950
        // set wrap bit
6951
        set_tx_bd_wrap(3);
6952
      end
6953
      // 18 <= num_of_frames < 28 => wrap set to TX BD 4
6954
      else if ((num_of_frames == 18) || (num_of_frames == 23))
6955
      begin
6956
        tmp_len = i_length; // length of frame
6957
        tmp_bd_num = 0; // TX BD number
6958
        while (tmp_bd_num < 5) //
6959
        begin
6960
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6961
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6962
          if (tmp_len[0] == 0)
6963 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
6964
          else // when (num_of_frames == 23), (i_length == 23) and therefor i_length[0] == 1 !!!
6965
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1],
6966
                      ((num_of_frames == 23) && (tmp_bd_num == 0)), 1'b1, (`MEMORY_BASE + max_tmp));
6967 209 tadejm
          tmp_len = tmp_len + 1;
6968
          // set TX BD number
6969
          tmp_bd_num = tmp_bd_num + 1;
6970
        end
6971
        // set wrap bit
6972
        set_tx_bd_wrap(4);
6973
      end
6974
      // 28 <= num_of_frames < 40 => wrap set to TX BD 5
6975
      else if ((num_of_frames == 28) || (num_of_frames == 34))
6976
      begin
6977
        tmp_len = i_length; // length of frame
6978
        tmp_bd_num = 0; // TX BD number
6979
        while (tmp_bd_num < 6) //
6980
        begin
6981
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6982
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6983
          if (tmp_len[0] == 0)
6984 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
6985 209 tadejm
          else
6986 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
6987 209 tadejm
          tmp_len = tmp_len + 1;
6988
          // set TX BD number
6989
          tmp_bd_num = tmp_bd_num + 1;
6990
        end
6991
        // set wrap bit
6992
        set_tx_bd_wrap(5);
6993
      end
6994
      // 40 <= num_of_frames < 54 => wrap set to TX BD 6
6995
      else if ((num_of_frames == 40) || (num_of_frames == 47))
6996
      begin
6997
        tmp_len = i_length; // length of frame
6998
        tmp_bd_num = 0; // TX BD number
6999
        while (tmp_bd_num < 7) //
7000
        begin
7001
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7002
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7003
          if (tmp_len[0] == 0)
7004 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
7005 209 tadejm
          else
7006 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
7007 209 tadejm
          tmp_len = tmp_len + 1;
7008
          // set TX BD number
7009
          tmp_bd_num = tmp_bd_num + 1;
7010
        end
7011
        // set wrap bit
7012
        set_tx_bd_wrap(6);
7013
      end
7014
      // 54 <= num_of_frames < 70 => wrap set to TX BD 7
7015
      else if ((num_of_frames == 54) || (num_of_frames == 62))
7016
      begin
7017
        tmp_len = i_length; // length of frame
7018
        tmp_bd_num = 0; // TX BD number
7019
        while (tmp_bd_num < 8) //
7020
        begin
7021
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7022
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7023
          if (tmp_len[0] == 0)
7024 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
7025 209 tadejm
          else
7026 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
7027 209 tadejm
          tmp_len = tmp_len + 1;
7028
          // set TX BD number
7029
          tmp_bd_num = tmp_bd_num + 1;
7030
        end
7031
        // set wrap bit
7032
        set_tx_bd_wrap(7);
7033
      end
7034
      #1;
7035
      // SET ready bit
7036
      if (num_of_frames < 10)
7037
        set_tx_bd_ready(0, 0);
7038
      else if (num_of_frames < 14)
7039
        set_tx_bd_ready((num_of_frames - 10), (num_of_frames - 10));
7040
      else if (num_of_frames < 18)
7041
        set_tx_bd_ready((num_of_frames - 14), (num_of_frames - 14));
7042
      else if (num_of_frames < 23)
7043
        set_tx_bd_ready((num_of_frames - 18), (num_of_frames - 18));
7044
      else if (num_of_frames < 28)
7045
        set_tx_bd_ready((num_of_frames - 23), (num_of_frames - 23));
7046
      else if (num_of_frames < 34)
7047
        set_tx_bd_ready((num_of_frames - 28), (num_of_frames - 28));
7048
      else if (num_of_frames < 40)
7049
        set_tx_bd_ready((num_of_frames - 34), (num_of_frames - 34));
7050
      else if (num_of_frames < 47)
7051
        set_tx_bd_ready((num_of_frames - 40), (num_of_frames - 40));
7052
      else if (num_of_frames < 54)
7053
        set_tx_bd_ready((num_of_frames - 47), (num_of_frames - 47));
7054
      else if (num_of_frames < 62)
7055
        set_tx_bd_ready((num_of_frames - 54), (num_of_frames - 54));
7056
      else if (num_of_frames < 70)
7057
        set_tx_bd_ready((num_of_frames - 62), (num_of_frames - 62));
7058
      // CHECK END OF TRANSMITION
7059
      frame_started = 0;
7060
      if (num_of_frames >= 5)
7061
        #1 check_tx_bd(num_of_bd, data);
7062
      fork
7063
      begin: fr_st2
7064
        wait (MTxEn === 1'b1); // start transmit
7065
        frame_started = 1;
7066
      end
7067
      begin
7068
        repeat (30) @(posedge mtx_clk);
7069
        if (num_of_frames < 5)
7070
        begin
7071
          if (frame_started == 1)
7072
          begin
7073
            `TIME; $display("*E Frame should NOT start!");
7074
          end
7075
          disable fr_st2;
7076
        end
7077
        else
7078
        begin
7079
          if (frame_started == 0)
7080
          begin
7081
            `TIME; $display("*W Frame should start!");
7082
            disable fr_st2;
7083
          end
7084
        end
7085
      end
7086
      join
7087
      // check packets larger than 4 bytes
7088
      if (num_of_frames >= 5)
7089
      begin
7090
        wait (MTxEn === 1'b0); // end transmit
7091
        while (data[15] === 1)
7092
        begin
7093
          #1 check_tx_bd(num_of_bd, data);
7094
          @(posedge wb_clk);
7095
        end
7096
        repeat (1) @(posedge wb_clk);
7097 243 tadejm
        // check length of a PACKET 
7098
        if ((eth_phy.tx_len != i_length) && (i_length[0] == 1'b0) && (num_of_frames >= 6))
7099 209 tadejm
        begin
7100
          `TIME; $display("*E Wrong length of the packet out from MAC");
7101
          test_fail("Wrong length of the packet out from MAC");
7102
          fail = fail + 1;
7103
        end
7104 243 tadejm
        else if ((eth_phy.tx_len != (i_length + 4)) && (num_of_frames != 23))
7105
        begin
7106
          `TIME; $display("*E Wrong length of the packet out from MAC");
7107
          test_fail("Wrong length of the packet out from MAC");
7108
          fail = fail + 1;
7109
        end
7110
        else if ((eth_phy.tx_len != (min_tmp)) && (num_of_frames == 23))
7111
        begin
7112
          `TIME; $display("*E Wrong length of the packet out from MAC");
7113
          test_fail("Wrong length of the packet out from MAC");
7114
          fail = fail + 1;
7115
        end
7116 209 tadejm
        // check transmitted TX packet data
7117
        if (i_length[0] == 0)
7118
        begin
7119
          #1 check_tx_packet(`MEMORY_BASE, (num_of_frames * 16), i_length, tmp);
7120
        end
7121 243 tadejm
        else if (num_of_frames == 23) // i_length[0] == 1 here
7122
        begin
7123
          #1 check_tx_packet((`MEMORY_BASE + max_tmp), (num_of_frames * 16), (min_tmp - 4), tmp);
7124
        end
7125 209 tadejm
        else
7126
        begin
7127
          #1 check_tx_packet((`MEMORY_BASE + max_tmp), (num_of_frames * 16), i_length, tmp);
7128
        end
7129
        if (tmp > 0)
7130
        begin
7131
          test_fail("Wrong data of the transmitted packet");
7132
          fail = fail + 1;
7133
        end
7134
        // check transmited TX packet CRC
7135 243 tadejm
        #1;
7136
        if ((i_length[0] == 1'b0) && (num_of_frames >= 6))
7137
        begin
7138
        end
7139
        else
7140
          check_tx_crc((num_of_frames * 16), (eth_phy.tx_len - 4), 1'b0, tmp); // length without CRC
7141 209 tadejm
        if (tmp > 0)
7142
        begin
7143
          test_fail("Wrong CRC of the transmitted packet");
7144
          fail = fail + 1;
7145
        end
7146
      end
7147
      // check WB INT signal
7148
      if ((i_length[1:0] == 2'h0) && (num_of_frames >= 5))
7149
      begin
7150
        if (wb_int !== 1'b1)
7151
        begin
7152
          `TIME; $display("*E WB INT signal should be set");
7153
          test_fail("WB INT signal should be set");
7154
          fail = fail + 1;
7155
        end
7156
      end
7157
      else
7158
      begin
7159
        if (wb_int !== 1'b0)
7160
        begin
7161
          `TIME; $display("*E WB INT signal should not be set");
7162
          test_fail("WB INT signal should not be set");
7163
          fail = fail + 1;
7164
        end
7165
      end
7166
      // check TX buffer descriptor of a packet
7167
      check_tx_bd(num_of_bd, data);
7168
      if (num_of_frames >= 5)
7169
      begin
7170 243 tadejm
        if ((i_length[1] == 1'b0) && (i_length[0] == 1'b0)) // interrupt enabled
7171 209 tadejm
        begin
7172 243 tadejm
          if ( (data[15:0] !== 16'h6000) &&  // wrap bit
7173
               (data[15:0] !== 16'h4000) )  // without wrap bit
7174 209 tadejm
          begin
7175
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7176
            test_fail("TX buffer descriptor status is not correct");
7177
            fail = fail + 1;
7178
          end
7179
        end
7180 243 tadejm
        else if ((i_length[1] == 1'b1) && (i_length[0] == 1'b0)) // interrupt not enabled
7181 209 tadejm
        begin
7182 243 tadejm
          if ( (data[15:0] !== 16'h2000) && // wrap bit
7183
               (data[15:0] !== 16'h0000) ) // without wrap bit
7184
          begin
7185
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7186
            test_fail("TX buffer descriptor status is not correct");
7187
            fail = fail + 1;
7188
          end
7189
        end
7190
        else if ((i_length[1] == 1'b0) && (i_length[0] == 1'b1)) // interrupt enabled
7191
        begin
7192
          if ( (data[15:0] !== 16'h6800) && // wrap bit
7193
               (data[15:0] !== 16'h4800) ) // without wrap bit
7194
          begin
7195
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7196
            test_fail("TX buffer descriptor status is not correct");
7197
            fail = fail + 1;
7198
          end
7199
        end
7200
        else if (num_of_frames != 23) // ((i_length[1] == 1'b1) && (i_length[0] == 1'b1)) // interrupt not enabled
7201
        begin
7202
          if ( (data[15:0] !== 16'h2800) && // wrap bit
7203
               (data[15:0] !== 16'h0800) ) // without wrap bit
7204
          begin
7205
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7206
            test_fail("TX buffer descriptor status is not correct");
7207
            fail = fail + 1;
7208
          end
7209
        end
7210
        else // ((num_of_frames != 23) && (i_length[1] == 1'b1) && (i_length[0] == 1'b1)) // interrupt not enabled
7211
        begin
7212 209 tadejm
          if ( (data[15:0] !== 16'h3800) && // wrap bit
7213
               (data[15:0] !== 16'h1800) ) // without wrap bit
7214
          begin
7215
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7216
            test_fail("TX buffer descriptor status is not correct");
7217
            fail = fail + 1;
7218
          end
7219
        end
7220
      end
7221
      else
7222
      begin
7223
        if (data[15] !== 1'b1)
7224
        begin
7225
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7226
          test_fail("TX buffer descriptor status is not correct");
7227
          fail = fail + 1;
7228
        end
7229
      end
7230
      // clear TX BD with wrap bit
7231
      if (num_of_frames == 63)
7232
        clear_tx_bd(16, 16);
7233
      // check interrupts
7234
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7235
      if ( ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1)) && (num_of_frames >= 5) )
7236
      begin
7237
        if ((data & `ETH_INT_TXB) !== 1'b1)
7238
        begin
7239
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
7240
          test_fail("Interrupt Transmit Buffer was not set");
7241
          fail = fail + 1;
7242
        end
7243
        if ((data & (~`ETH_INT_TXB)) !== 0)
7244
        begin
7245
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
7246
          test_fail("Other interrupts (except Transmit Buffer) were set");
7247
          fail = fail + 1;
7248
        end
7249
      end
7250
      else
7251
      begin
7252
        if (data !== 0)
7253
        begin
7254
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
7255
          test_fail("Any of interrupts (except Transmit Buffer) was set");
7256
          fail = fail + 1;
7257
        end
7258
      end
7259
      // clear interrupts
7260
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7261
      // check WB INT signal
7262
      if (wb_int !== 1'b0)
7263
      begin
7264
        test_fail("WB INT signal should not be set");
7265
        fail = fail + 1;
7266
      end
7267
      // INTERMEDIATE DISPLAYS
7268
      if (i_length == 3)
7269
      begin
7270
        $display("    pads appending to packets is selected");
7271
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
7272
        $display("    ->packets with lengths from %0d to %0d are not transmitted (length increasing by 1 byte)",
7273
                 0, 3);
7274
      end
7275
      else if (i_length == 9)
7276
      begin
7277
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
7278
        $display("    ->packet with length 4 is not transmitted (length increasing by 1 byte)");
7279
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7280
                 5, 9);
7281
      end
7282
      else if (i_length == 17)
7283
      begin
7284
        $display("    using 4 BDs out of 8 BDs assigned to TX (wrap at 4th BD - TX BD 3)");
7285
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7286
                 10, 17);
7287
      end
7288
      else if (i_length == 27)
7289
      begin
7290
        $display("    using 5 BDs out of 8 BDs assigned to TX (wrap at 5th BD - TX BD 4)");
7291
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7292
                 18, 27);
7293
      end
7294
      else if (i_length == 40)
7295
      begin
7296
        $display("    using 6 BDs out of 8 BDs assigned to TX (wrap at 6th BD - TX BD 5)");
7297
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7298
                 28, 40);
7299
      end
7300
      else if (i_length == 54)
7301
      begin
7302
        $display("    using 7 BDs out of 8 BDs assigned to TX (wrap at 7th BD - TX BD 6)");
7303
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7304
                 41, 54);
7305
      end
7306
      else if (i_length == 69)
7307
      begin
7308
        $display("    using 8 BDs out of 8 BDs assigned to TX (wrap at 8th BD - TX BD 7)");
7309
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7310
                 55, 69);
7311
      end
7312
      // set length (loop variable)
7313
      i_length = i_length + 1;
7314
      // the number of frame transmitted
7315
      num_of_frames = num_of_frames + 1;
7316
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
7317
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
7318
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
7319
          (num_of_frames == 54) || (num_of_frames == 62))
7320
        num_of_bd = 0;
7321
      else
7322
        num_of_bd = num_of_bd + 1;
7323
    end
7324
    // disable TX
7325
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
7326
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7327
    @(posedge wb_clk);
7328
    if(fail == 0)
7329
      test_ok;
7330
    else
7331
      fail = 0;
7332
  end
7333
 
7334
 
7335
  ////////////////////////////////////////////////////////////////////
7336
  ////                                                            ////
7337 243 tadejm
  ////  Test transmit packets (no pads) form 0 to (MINFL - 1)     ////
7338 209 tadejm
  ////  sizes at 8 TX buffer decriptors ( 100Mbps ).              ////
7339
  ////                                                            ////
7340
  ////////////////////////////////////////////////////////////////////
7341
  if (test_num == 9) // 
7342
  begin
7343 243 tadejm
    // TEST 9: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )
7344
    test_name = "TEST 9: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )";
7345
    `TIME; $display("  TEST 9: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )");
7346 209 tadejm
 
7347
    // reset MAC registers
7348
    hard_reset;
7349
    // reset MAC and MII LOGIC with soft reset
7350
    reset_mac;
7351
    reset_mii;
7352
    // set wb slave response
7353
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
7354
 
7355
    max_tmp = 0;
7356
    min_tmp = 0;
7357
    // set 8 TX buffer descriptors - must be set before TX enable
7358
    wbm_write(`ETH_TX_BD_NUM, 32'h8, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7359
    // enable TX, set full-duplex mode, padding and CRC appending
7360
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
7361
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7362
    // prepare two packets of MAXFL length
7363
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7364
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
7365
    min_tmp = tmp[31:16];
7366
    st_data = 8'h12;
7367
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
7368
    st_data = 8'h34;
7369
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp - 4), st_data); // length without CRC
7370
    // check WB INT signal
7371
    if (wb_int !== 1'b0)
7372
    begin
7373
      test_fail("WB INT signal should not be set");
7374
      fail = fail + 1;
7375
    end
7376
 
7377
    // write to phy's control register for 100Mbps
7378
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
7379
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
7380
    speed = 100;
7381
 
7382
    frame_started = 0;
7383
    num_of_frames = 0;
7384
    num_of_bd = 0;
7385
    i_length = 0; // 0;
7386
    while (i_length < 70) // (min_tmp - 4))
7387
    begin
7388
      #1;
7389
      // choose generating carrier sense and collision
7390
      case (i_length[1:0])
7391
      2'h0: // Interrupt is generated
7392
      begin
7393
        // Reset_tx_bd nable interrupt generation
7394
        // unmask interrupts
7395
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
7396
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7397
        // not detect carrier sense in FD and no collision
7398
        eth_phy.carrier_sense_tx_fd_detect(0);
7399
        eth_phy.collision(0);
7400
      end
7401
      2'h1: // Interrupt is not generated
7402
      begin
7403
        // set_tx_bd enable interrupt generation
7404
        // mask interrupts
7405
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7406
        // detect carrier sense in FD and no collision
7407
        eth_phy.carrier_sense_tx_fd_detect(1);
7408
        eth_phy.collision(0);
7409
      end
7410
      2'h2: // Interrupt is not generated
7411
      begin
7412
        // set_tx_bd disable the interrupt generation
7413
        // unmask interrupts
7414
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
7415
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7416
        // not detect carrier sense in FD and set collision
7417
        eth_phy.carrier_sense_tx_fd_detect(0);
7418
        eth_phy.collision(1);
7419
      end
7420
      default: // 2'h3: // Interrupt is not generated
7421
      begin
7422
        // set_tx_bd disable the interrupt generation
7423
        // mask interrupts
7424
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7425
        // detect carrier sense in FD and set collision
7426
        eth_phy.carrier_sense_tx_fd_detect(1);
7427
        eth_phy.collision(1);
7428
      end
7429
      endcase
7430
      #1;
7431
      // first destination address on ethernet PHY
7432
      eth_phy.set_tx_mem_addr(num_of_frames * 16);
7433
      // SET packets and wrap bit
7434
      // num_of_frames <= 9 => wrap set to TX BD 0
7435
      if (num_of_frames <= 9)
7436
      begin
7437
        tmp_len = i_length; // length of frame
7438
        tmp_bd_num = 0; // TX BD number
7439
        // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7440
        // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7441
        if (tmp_len[0] == 0)
7442
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7443
        else
7444
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7445
        // set wrap bit
7446
        set_tx_bd_wrap(0);
7447
      end
7448
      // 10 <= num_of_frames < 18 => wrap set to TX BD 3
7449
      else if ((num_of_frames == 10) || (num_of_frames == 14))
7450
      begin
7451
        tmp_len = i_length; // length of frame
7452
        tmp_bd_num = 0; // TX BD number
7453
        while (tmp_bd_num < 4) //
7454
        begin
7455
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7456
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7457
          if (tmp_len[0] == 0)
7458
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7459
          else
7460
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7461
          tmp_len = tmp_len + 1;
7462
          // set TX BD number
7463
          tmp_bd_num = tmp_bd_num + 1;
7464
        end
7465
        // set wrap bit
7466
        set_tx_bd_wrap(3);
7467
      end
7468
      // 18 <= num_of_frames < 28 => wrap set to TX BD 4
7469
      else if ((num_of_frames == 18) || (num_of_frames == 23))
7470
      begin
7471
        tmp_len = i_length; // length of frame
7472
        tmp_bd_num = 0; // TX BD number
7473
        while (tmp_bd_num < 5) //
7474
        begin
7475
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7476
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7477
          if (tmp_len[0] == 0)
7478
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7479
          else
7480
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7481
          tmp_len = tmp_len + 1;
7482
          // set TX BD number
7483
          tmp_bd_num = tmp_bd_num + 1;
7484
        end
7485
        // set wrap bit
7486
        set_tx_bd_wrap(4);
7487
      end
7488
      // 28 <= num_of_frames < 40 => wrap set to TX BD 5
7489
      else if ((num_of_frames == 28) || (num_of_frames == 34))
7490
      begin
7491
        tmp_len = i_length; // length of frame
7492
        tmp_bd_num = 0; // TX BD number
7493
        while (tmp_bd_num < 6) //
7494
        begin
7495
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7496
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7497
          if (tmp_len[0] == 0)
7498
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7499
          else
7500
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7501
          tmp_len = tmp_len + 1;
7502
          // set TX BD number
7503
          tmp_bd_num = tmp_bd_num + 1;
7504
        end
7505
        // set wrap bit
7506
        set_tx_bd_wrap(5);
7507
      end
7508
      // 40 <= num_of_frames < 54 => wrap set to TX BD 6
7509
      else if ((num_of_frames == 40) || (num_of_frames == 47))
7510
      begin
7511
        tmp_len = i_length; // length of frame
7512
        tmp_bd_num = 0; // TX BD number
7513
        while (tmp_bd_num < 7) //
7514
        begin
7515
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7516
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7517
          if (tmp_len[0] == 0)
7518
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7519
          else
7520
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7521
          tmp_len = tmp_len + 1;
7522
          // set TX BD number
7523
          tmp_bd_num = tmp_bd_num + 1;
7524
        end
7525
        // set wrap bit
7526
        set_tx_bd_wrap(6);
7527
      end
7528
      // 54 <= num_of_frames < 70 => wrap set to TX BD 7
7529
      else if ((num_of_frames == 54) || (num_of_frames == 62))
7530
      begin
7531
        tmp_len = i_length; // length of frame
7532
        tmp_bd_num = 0; // TX BD number
7533
        while (tmp_bd_num < 8) //
7534
        begin
7535
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7536
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7537
          if (tmp_len[0] == 0)
7538
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7539
          else
7540
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7541
          tmp_len = tmp_len + 1;
7542
          // set TX BD number
7543
          tmp_bd_num = tmp_bd_num + 1;
7544
        end
7545
        // set wrap bit
7546
        set_tx_bd_wrap(7);
7547
      end
7548
      #1;
7549
      // SET ready bit
7550
      if (num_of_frames < 10)
7551
        set_tx_bd_ready(0, 0);
7552
      else if (num_of_frames < 14)
7553
        set_tx_bd_ready((num_of_frames - 10), (num_of_frames - 10));
7554
      else if (num_of_frames < 18)
7555
        set_tx_bd_ready((num_of_frames - 14), (num_of_frames - 14));
7556
      else if (num_of_frames < 23)
7557
        set_tx_bd_ready((num_of_frames - 18), (num_of_frames - 18));
7558
      else if (num_of_frames < 28)
7559
        set_tx_bd_ready((num_of_frames - 23), (num_of_frames - 23));
7560
      else if (num_of_frames < 34)
7561
        set_tx_bd_ready((num_of_frames - 28), (num_of_frames - 28));
7562
      else if (num_of_frames < 40)
7563
        set_tx_bd_ready((num_of_frames - 34), (num_of_frames - 34));
7564
      else if (num_of_frames < 47)
7565
        set_tx_bd_ready((num_of_frames - 40), (num_of_frames - 40));
7566
      else if (num_of_frames < 54)
7567
        set_tx_bd_ready((num_of_frames - 47), (num_of_frames - 47));
7568
      else if (num_of_frames < 62)
7569
        set_tx_bd_ready((num_of_frames - 54), (num_of_frames - 54));
7570
      else if (num_of_frames < 70)
7571
        set_tx_bd_ready((num_of_frames - 62), (num_of_frames - 62));
7572
      // CHECK END OF TRANSMITION
7573
      frame_started = 0;
7574
      if (num_of_frames >= 5)
7575
        #1 check_tx_bd(num_of_bd, data);
7576
      fork
7577
      begin: fr_st3
7578
        wait (MTxEn === 1'b1); // start transmit
7579
        frame_started = 1;
7580
      end
7581
      begin
7582
        repeat (30) @(posedge mtx_clk);
7583
        if (num_of_frames < 5)
7584
        begin
7585
          if (frame_started == 1)
7586
          begin
7587
            `TIME; $display("*E Frame should NOT start!");
7588
          end
7589
          disable fr_st3;
7590
        end
7591
        else
7592
        begin
7593
          if (frame_started == 0)
7594
          begin
7595
            `TIME; $display("*W Frame should start!");
7596
            disable fr_st3;
7597
          end
7598
        end
7599
      end
7600
      join
7601
      // check packets larger than 4 bytes
7602
      if (num_of_frames >= 5)
7603
      begin
7604
        wait (MTxEn === 1'b0); // end transmit
7605
        while (data[15] === 1)
7606
        begin
7607
          #1 check_tx_bd(num_of_bd, data);
7608
          @(posedge wb_clk);
7609
        end
7610
        repeat (1) @(posedge wb_clk);
7611
        // check length of a PACKET
7612
        if (eth_phy.tx_len != (i_length + 4))
7613
        begin
7614
          `TIME; $display("*E Wrong length of the packet out from MAC");
7615
          test_fail("Wrong length of the packet out from MAC");
7616
          fail = fail + 1;
7617
        end
7618
        // check transmitted TX packet data
7619
        if (i_length[0] == 0)
7620
        begin
7621
          #1 check_tx_packet(`MEMORY_BASE, (num_of_frames * 16), i_length, tmp);
7622
        end
7623
        else
7624
        begin
7625
          #1 check_tx_packet((`MEMORY_BASE + max_tmp), (num_of_frames * 16), i_length, tmp);
7626
        end
7627
        if (tmp > 0)
7628
        begin
7629
          test_fail("Wrong data of the transmitted packet");
7630
          fail = fail + 1;
7631
        end
7632
        // check transmited TX packet CRC
7633
        #1 check_tx_crc((num_of_frames * 16), (eth_phy.tx_len - 4), 1'b0, tmp); // length without CRC
7634
        if (tmp > 0)
7635
        begin
7636
          test_fail("Wrong CRC of the transmitted packet");
7637
          fail = fail + 1;
7638
        end
7639
      end
7640
      // check WB INT signal
7641
      if ((i_length[1:0] == 2'h0) && (num_of_frames >= 5))
7642
      begin
7643
        if (wb_int !== 1'b1)
7644
        begin
7645
          `TIME; $display("*E WB INT signal should be set");
7646
          test_fail("WB INT signal should be set");
7647
          fail = fail + 1;
7648
        end
7649
      end
7650
      else
7651
      begin
7652
        if (wb_int !== 1'b0)
7653
        begin
7654
          `TIME; $display("*E WB INT signal should not be set");
7655
          test_fail("WB INT signal should not be set");
7656
          fail = fail + 1;
7657
        end
7658
      end
7659
      // check TX buffer descriptor of a packet
7660
      check_tx_bd(num_of_bd, data);
7661
      if (num_of_frames >= 5)
7662
      begin
7663
        if (i_length[1] == 1'b0) // interrupt enabled
7664
        begin
7665
          if ( (data[15:0] !== 16'h7800) && // wrap bit
7666
               (data[15:0] !== 16'h5800) ) // without wrap bit
7667
          begin
7668
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7669
            test_fail("TX buffer descriptor status is not correct");
7670
            fail = fail + 1;
7671
          end
7672
        end
7673
        else // interrupt not enabled
7674
        begin
7675
          if ( (data[15:0] !== 16'h3800) && // wrap bit
7676
               (data[15:0] !== 16'h1800) ) // without wrap bit
7677
          begin
7678
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7679
            test_fail("TX buffer descriptor status is not correct");
7680
            fail = fail + 1;
7681
          end
7682
        end
7683
      end
7684
      else
7685
      begin
7686
        if (data[15] !== 1'b1)
7687
        begin
7688
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7689
          test_fail("TX buffer descriptor status is not correct");
7690
          fail = fail + 1;
7691
        end
7692
      end
7693
      // clear TX BD with wrap bit
7694
      if (num_of_frames == 63)
7695
        clear_tx_bd(16, 16);
7696
      // check interrupts
7697
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7698
      if ( ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1)) && (num_of_frames >= 5) )
7699
      begin
7700
        if ((data & `ETH_INT_TXB) !== 1'b1)
7701
        begin
7702
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
7703
          test_fail("Interrupt Transmit Buffer was not set");
7704
          fail = fail + 1;
7705
        end
7706
        if ((data & (~`ETH_INT_TXB)) !== 0)
7707
        begin
7708
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
7709
          test_fail("Other interrupts (except Transmit Buffer) were set");
7710
          fail = fail + 1;
7711
        end
7712
      end
7713
      else
7714
      begin
7715
        if (data !== 0)
7716
        begin
7717
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
7718
          test_fail("Any of interrupts (except Transmit Buffer) was set");
7719
          fail = fail + 1;
7720
        end
7721
      end
7722
      // clear interrupts
7723
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7724
      // check WB INT signal
7725
      if (wb_int !== 1'b0)
7726
      begin
7727
        test_fail("WB INT signal should not be set");
7728
        fail = fail + 1;
7729
      end
7730
      // INTERMEDIATE DISPLAYS
7731
      if (i_length == 3)
7732
      begin
7733
        $display("    pads appending to packets is selected");
7734
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
7735
        $display("    ->packets with lengths from %0d to %0d are not transmitted (length increasing by 1 byte)",
7736
                 0, 3);
7737
      end
7738
      else if (i_length == 9)
7739
      begin
7740
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
7741
        $display("    ->packet with length 4 is not transmitted (length increasing by 1 byte)");
7742
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7743
                 5, 9);
7744
      end
7745
      else if (i_length == 17)
7746
      begin
7747
        $display("    using 4 BDs out of 8 BDs assigned to TX (wrap at 4th BD - TX BD 3)");
7748
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7749
                 10, 17);
7750
      end
7751
      else if (i_length == 27)
7752
      begin
7753
        $display("    using 5 BDs out of 8 BDs assigned to TX (wrap at 5th BD - TX BD 4)");
7754
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7755
                 18, 27);
7756
      end
7757
      else if (i_length == 40)
7758
      begin
7759
        $display("    using 6 BDs out of 8 BDs assigned to TX (wrap at 6th BD - TX BD 5)");
7760
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7761
                 28, 40);
7762
      end
7763
      else if (i_length == 54)
7764
      begin
7765
        $display("    using 7 BDs out of 8 BDs assigned to TX (wrap at 7th BD - TX BD 6)");
7766
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7767
                 41, 54);
7768
      end
7769
      else if (i_length == 69)
7770
      begin
7771
        $display("    using 8 BDs out of 8 BDs assigned to TX (wrap at 8th BD - TX BD 7)");
7772
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7773
                 55, 69);
7774
      end
7775
      // set length (loop variable)
7776
      i_length = i_length + 1;
7777
      // the number of frame transmitted
7778
      num_of_frames = num_of_frames + 1;
7779
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
7780
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
7781
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
7782
          (num_of_frames == 54) || (num_of_frames == 62))
7783
        num_of_bd = 0;
7784
      else
7785
        num_of_bd = num_of_bd + 1;
7786
    end
7787
    // disable TX
7788
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
7789
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7790
    @(posedge wb_clk);
7791
    if(fail == 0)
7792
      test_ok;
7793
    else
7794
      fail = 0;
7795
  end
7796
 
7797
 
7798
  ////////////////////////////////////////////////////////////////////
7799
  ////                                                            ////
7800
  ////  Test transmit packets across MAXFL value at               ////
7801
  ////  13 TX buffer decriptors ( 10Mbps ).                       ////
7802
  ////                                                            ////
7803
  ////////////////////////////////////////////////////////////////////
7804
  if (test_num == 10) // without and with padding
7805
  begin
7806
    // TEST 10: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 10Mbps )
7807
    test_name = "TEST 10: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 10Mbps )";
7808
    `TIME; $display("  TEST 10: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 10Mbps )");
7809
 
7810
    // reset MAC registers
7811
    hard_reset;
7812
    // reset MAC and MII LOGIC with soft reset
7813
    reset_mac;
7814
    reset_mii;
7815
    // set wb slave response
7816
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
7817
 
7818
    max_tmp = 0;
7819
    min_tmp = 0;
7820
    num_of_frames = 0;
7821
    num_of_bd = 0;
7822
    // set 13 TX buffer descriptors - must be set before TX enable
7823
    wbm_write(`ETH_TX_BD_NUM, 32'hD, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7824
    // enable TX, set full-duplex mode, NO padding and CRC appending
7825
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
7826
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7827
    // prepare a packet of MAXFL + 10 length
7828
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7829
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
7830
    min_tmp = tmp[31:16];
7831
    st_data = 8'hA3;
7832
    set_tx_packet(`MEMORY_BASE, (max_tmp + 10), st_data); // length without CRC
7833
    // check WB INT signal
7834
    if (wb_int !== 1'b0)
7835
    begin
7836
      test_fail("WB INT signal should not be set");
7837
      fail = fail + 1;
7838
    end
7839
 
7840
    // write to phy's control register for 10Mbps
7841
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
7842
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
7843
    speed = 10;
7844
 
7845
    i_length = (max_tmp - 5);
7846
    while (i_length <= (max_tmp - 3)) // (max_tmp - 4) is the limit
7847
    begin
7848
$display("   i_length = %0d", i_length);
7849
      // choose generating carrier sense and collision
7850
//      case (i_length[1:0])
7851
//      2'h0: // Interrupt is generated
7852
//      begin
7853
        // Reset_tx_bd nable interrupt generation
7854
        // unmask interrupts
7855
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
7856
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7857
        // not detect carrier sense in FD and no collision
7858
        eth_phy.carrier_sense_tx_fd_detect(0);
7859
        eth_phy.collision(0);
7860
//      end
7861
//      2'h1: // Interrupt is not generated
7862
//      begin
7863
        // set_tx_bd enable interrupt generation
7864
        // mask interrupts
7865
//        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7866
        // detect carrier sense in FD and no collision
7867
//        eth_phy.carrier_sense_tx_fd_detect(1);
7868
//        eth_phy.collision(0);
7869
//      end
7870
//      2'h2: // Interrupt is not generated
7871
//      begin
7872
        // set_tx_bd disable the interrupt generation
7873
        // unmask interrupts
7874
//        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
7875
//                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7876
        // not detect carrier sense in FD and set collision
7877
//        eth_phy.carrier_sense_tx_fd_detect(0);
7878
//        eth_phy.collision(1);
7879
//      end
7880
//      default: // 2'h3: // Interrupt is not generated
7881
//      begin
7882
        // set_tx_bd disable the interrupt generation
7883
        // mask interrupts
7884
//        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7885
        // detect carrier sense in FD and set collision
7886
//        eth_phy.carrier_sense_tx_fd_detect(1);
7887
//        eth_phy.collision(1);
7888
//      end
7889
//      endcase
7890
      // first destination address on ethernet PHY
7891
      eth_phy.set_tx_mem_addr(0);
7892
      // 
7893
if (num_of_bd == 0)
7894
begin
7895
set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
7896
set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
7897
set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
7898
set_tx_bd_wrap(2);
7899
set_tx_bd_ready(0, 0);
7900
end
7901
else if (num_of_bd == 1)
7902
set_tx_bd_ready(1, 1);
7903
else if (num_of_bd == 2)
7904
set_tx_bd_ready(2, 2);
7905
 
7906
 
7907
//        tmp_len = i_length; // length of frame
7908
//        tmp_bd_num = 0; // TX BD number
7909
//        while (tmp_bd_num < 8) // 
7910
//        begin
7911
//          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7912
//          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7913
//          if (tmp_len[0] == 0)
7914
//            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7915
//          else
7916
//            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + 2*max_tmp));
7917
//          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
7918
//          tmp_len = tmp_len + 1;
7919
//          // set TX BD number
7920
//          tmp_bd_num = tmp_bd_num + 1;
7921
//        end
7922
//        // set wrap bit
7923
//        set_tx_bd_wrap(7);
7924
//      // set ready bit
7925
//      set_tx_bd_ready((i_length - (max_tmp - 8)), (i_length - (max_tmp - 8)));
7926
      // CHECK END OF TRANSMITION
7927
check_tx_bd(num_of_bd, data);
7928
//      #1 check_tx_bd((i_length - (max_tmp - 8)), data);
7929
        wait (MTxEn === 1'b1); // start transmit
7930
check_tx_bd(num_of_bd, data);
7931
//        #1 check_tx_bd((i_length - (max_tmp - 8)), data);
7932
        if (data[15] !== 1)
7933
        begin
7934
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
7935
          fail = fail + 1;
7936
        end
7937
        wait (MTxEn === 1'b0); // end transmit
7938
        while (data[15] === 1)
7939
        begin
7940
check_tx_bd(num_of_bd, data);
7941
//          #1 check_tx_bd((i_length - (max_tmp - 8)), data);
7942
          @(posedge wb_clk);
7943
        end
7944
        repeat (1) @(posedge wb_clk);
7945
      // check length of a PACKET
7946
$display("   eth_phy length = %0d", eth_phy.tx_len);
7947
tmp_len = eth_phy.tx_len;
7948
#1;
7949
if (tmp_len != (i_length + 4))
7950
//      if (eth_phy.tx_len != (i_length + 4))
7951
      begin
7952
        test_fail("Wrong length of the packet out from MAC");
7953
        fail = fail + 1;
7954
      end
7955
      // checking in the following if statement is performed only for first and last 64 lengths
7956
//      if ( ((i_length + 4) <= (min_tmp + 64)) || ((i_length + 4) > (max_tmp - 64)) )
7957
//      begin
7958
        // check transmitted TX packet data
7959
//        if (i_length[0] == 0)
7960
//        begin
7961
          check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
7962
//        end
7963
//        else
7964
//        begin
7965
//          check_tx_packet((`MEMORY_BASE + 2*max_tmp), 0, i_length, tmp);
7966
//        end
7967
        if (tmp > 0)
7968
        begin
7969
          test_fail("Wrong data of the transmitted packet");
7970
          fail = fail + 1;
7971
        end
7972
        // check transmited TX packet CRC
7973
//        if (i_length[0] == 0)
7974
          check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
7975
//        else
7976
//          check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
7977
        if (tmp > 0)
7978
        begin
7979
          test_fail("Wrong CRC of the transmitted packet");
7980
          fail = fail + 1;
7981
        end
7982
//      end
7983
      // check WB INT signal
7984
//      if (i_length[1:0] == 2'h0)
7985
//      begin
7986
        if (wb_int !== 1'b1)
7987
        begin
7988
          `TIME; $display("*E WB INT signal should be set");
7989
          test_fail("WB INT signal should be set");
7990
          fail = fail + 1;
7991
        end
7992
//      end
7993
//      else
7994
//      begin
7995
//        if (wb_int !== 1'b0)
7996
//        begin
7997
//          `TIME; $display("*E WB INT signal should not be set");
7998
//          test_fail("WB INT signal should not be set");
7999
//          fail = fail + 1;
8000
//        end
8001
//      end
8002
//      // check TX buffer descriptor of a packet
8003
//      check_tx_bd((i_length - (max_tmp - 8)), data);
8004
check_tx_bd(num_of_bd, data);
8005
if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
8006
     ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
8007
//      if (i_length[1] == 1'b0) // interrupt enabled
8008
//      begin
8009
//        if ( ((data[15:0] !== 16'h7800) && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
8010
//             ((data[15:0] !== 16'h5800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
8011
        begin
8012
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8013
          test_fail("TX buffer descriptor status is not correct");
8014
          fail = fail + 1;
8015
        end
8016
//      end
8017
//      else // interrupt not enabled
8018
//      begin
8019
//        if ( ((data[15:0] !== 16'h3800)  && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
8020
//             ((data[15:0] !== 16'h1800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
8021
//        begin
8022
//          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8023
//          test_fail("TX buffer descriptor status is not correct");
8024
//          fail = fail + 1;
8025
//        end
8026
//      end
8027
//      // clear first half of 8 frames from TX buffer descriptor 0
8028
//      if (num_of_frames < 4)
8029
//        clear_tx_bd((i_length - (max_tmp - 8)), (i_length - (max_tmp - 8)));
8030
      // check interrupts
8031
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8032
//      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
8033
//      begin
8034
        if ((data & `ETH_INT_TXB) !== 1'b1)
8035
        begin
8036
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8037
          test_fail("Interrupt Transmit Buffer was not set");
8038
          fail = fail + 1;
8039
        end
8040
        if ((data & (~`ETH_INT_TXB)) !== 0)
8041
        begin
8042
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8043
          test_fail("Other interrupts (except Transmit Buffer) were set");
8044
          fail = fail + 1;
8045
        end
8046
//      end
8047
//      else
8048
//      begin
8049
//        if (data !== 0)
8050
//        begin
8051
//          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
8052
//          test_fail("Any of interrupts (except Transmit Buffer) was set");
8053
//          fail = fail + 1;
8054
//        end
8055
//      end
8056
      // clear interrupts
8057
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8058
      // check WB INT signal
8059
      if (wb_int !== 1'b0)
8060
      begin
8061
        test_fail("WB INT signal should not be set");
8062
        fail = fail + 1;
8063
      end
8064
      // INTERMEDIATE DISPLAYS
8065
if (num_of_bd == 0)
8066
  $display("    ->packet with length %0d sent", (i_length + 4));
8067
else if (num_of_bd == 1)
8068
  $display("    ->packet with length %0d sent", (i_length + 4));
8069
else if (num_of_bd == 2)
8070
  $display("    ->packet with length %0d sent", (i_length + 4));
8071
      // set length (loop variable)
8072
      i_length = i_length + 1;
8073
      // the number of frame transmitted
8074
      num_of_frames = num_of_frames + 1;
8075
      num_of_bd = num_of_bd + 1;
8076
      @(posedge wb_clk);
8077
    end
8078
    // disable TX
8079
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8080
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8081
    @(posedge wb_clk);
8082
    if(fail == 0)
8083
      test_ok;
8084
    else
8085
      fail = 0;
8086
  end
8087
 
8088
 
8089
  ////////////////////////////////////////////////////////////////////
8090
  ////                                                            ////
8091
  ////  Test transmit packets across MAXFL value at               ////
8092
  ////  13 TX buffer decriptors ( 100Mbps ).                      ////
8093
  ////                                                            ////
8094
  ////////////////////////////////////////////////////////////////////
8095
  if (test_num == 11) // without and with padding
8096
  begin
8097
    // TEST 11: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 100Mbps )
8098
    test_name = "TEST 11: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 100Mbps )";
8099
    `TIME; $display("  TEST 11: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 100Mbps )");
8100
 
8101
    // reset MAC registers
8102
    hard_reset;
8103
    // reset MAC and MII LOGIC with soft reset
8104
    reset_mac;
8105
    reset_mii;
8106
    // set wb slave response
8107
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
8108
 
8109
    max_tmp = 0;
8110
    min_tmp = 0;
8111
    num_of_frames = 0;
8112
    num_of_bd = 0;
8113
    // set 13 TX buffer descriptors - must be set before TX enable
8114
    wbm_write(`ETH_TX_BD_NUM, 32'hD, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8115
    // enable TX, set full-duplex mode, NO padding and CRC appending
8116
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
8117
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8118
    // prepare a packet of MAXFL + 10 length
8119
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8120
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
8121
    min_tmp = tmp[31:16];
8122
    st_data = 8'hA3;
8123
    set_tx_packet(`MEMORY_BASE, (max_tmp + 10), st_data); // length without CRC
8124
    // check WB INT signal
8125
    if (wb_int !== 1'b0)
8126
    begin
8127
      test_fail("WB INT signal should not be set");
8128
      fail = fail + 1;
8129
    end
8130
 
8131
    // write to phy's control register for 100Mbps
8132
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
8133
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
8134
    speed = 100;
8135
 
8136
    i_length = (max_tmp - 5);
8137
    while (i_length <= (max_tmp - 3)) // (max_tmp - 4) is the limit
8138
    begin
8139
      $display("   i_length = %0d", i_length);
8140
      // Reset_tx_bd nable interrupt generation
8141
      // unmask interrupts
8142
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
8143
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8144
      // not detect carrier sense in FD and no collision
8145
      eth_phy.carrier_sense_tx_fd_detect(0);
8146
      eth_phy.collision(0);
8147
      // first destination address on ethernet PHY
8148
      eth_phy.set_tx_mem_addr(0);
8149
      // prepare BDs
8150
      if (num_of_bd == 0)
8151
      begin
8152
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8153
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8154
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8155
        set_tx_bd_wrap(2);
8156
        set_tx_bd_ready(0, 0);
8157
      end
8158
      else if (num_of_bd == 1)
8159
        set_tx_bd_ready(1, 1);
8160
      else if (num_of_bd == 2)
8161
        set_tx_bd_ready(2, 2);
8162
      // CHECK END OF TRANSMITION
8163
      check_tx_bd(num_of_bd, data);
8164
        wait (MTxEn === 1'b1); // start transmit
8165
      check_tx_bd(num_of_bd, data);
8166
        if (data[15] !== 1)
8167
        begin
8168
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
8169
          fail = fail + 1;
8170
        end
8171
        wait (MTxEn === 1'b0); // end transmit
8172
        while (data[15] === 1)
8173
        begin
8174
      check_tx_bd(num_of_bd, data);
8175
          @(posedge wb_clk);
8176
        end
8177
        repeat (1) @(posedge wb_clk);
8178
      // check length of a PACKET
8179
      $display("   eth_phy length = %0d", eth_phy.tx_len);
8180
      tmp_len = eth_phy.tx_len;
8181
      #1;
8182
      if (tmp_len != (i_length + 4))
8183
      begin
8184
        test_fail("Wrong length of the packet out from MAC");
8185
        fail = fail + 1;
8186
      end
8187
      // checking packet
8188
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
8189
      if (tmp > 0)
8190
      begin
8191
        test_fail("Wrong data of the transmitted packet");
8192
        fail = fail + 1;
8193
      end
8194
      // check transmited TX packet CRC
8195
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
8196
      if (tmp > 0)
8197
      begin
8198
        test_fail("Wrong CRC of the transmitted packet");
8199
        fail = fail + 1;
8200
      end
8201
      // check WB INT signal
8202
      if (wb_int !== 1'b1)
8203
      begin
8204
        `TIME; $display("*E WB INT signal should be set");
8205
        test_fail("WB INT signal should be set");
8206
        fail = fail + 1;
8207
      end
8208
      // check TX buffer descriptor of a packet
8209
      check_tx_bd(num_of_bd, data);
8210
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
8211
           ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
8212
      begin
8213
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8214
        test_fail("TX buffer descriptor status is not correct");
8215
        fail = fail + 1;
8216
      end
8217
      // check interrupts
8218
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8219
      if ((data & `ETH_INT_TXB) !== 1'b1)
8220
      begin
8221
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8222
        test_fail("Interrupt Transmit Buffer was not set");
8223
        fail = fail + 1;
8224
      end
8225
      if ((data & (~`ETH_INT_TXB)) !== 0)
8226
      begin
8227
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8228
        test_fail("Other interrupts (except Transmit Buffer) were set");
8229
        fail = fail + 1;
8230
      end
8231
      // clear interrupts
8232
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8233
      // check WB INT signal
8234
      if (wb_int !== 1'b0)
8235
      begin
8236
        test_fail("WB INT signal should not be set");
8237
        fail = fail + 1;
8238
      end
8239
      // INTERMEDIATE DISPLAYS
8240
      if (num_of_bd == 0)
8241
        $display("    ->packet with length %0d sent", (i_length + 4));
8242
      else if (num_of_bd == 1)
8243
        $display("    ->packet with length %0d sent", (i_length + 4));
8244
      else if (num_of_bd == 2)
8245
        $display("    ->packet with length %0d sent", (i_length + 4));
8246
      // set length (loop variable)
8247
      i_length = i_length + 1;
8248
      // the number of frame transmitted
8249
      num_of_frames = num_of_frames + 1;
8250
      num_of_bd = num_of_bd + 1;
8251
      @(posedge wb_clk);
8252
    end
8253
    // disable TX
8254
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8255
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8256
    @(posedge wb_clk);
8257
    if(fail == 0)
8258
      test_ok;
8259
    else
8260
      fail = 0;
8261
  end
8262
 
8263
 
8264
  ////////////////////////////////////////////////////////////////////
8265
  ////                                                            ////
8266
  ////  Test transmit packets across changed MAXFL value at       ////
8267
  ////  47 TX buffer decriptors ( 10Mbps ).                       ////
8268
  ////                                                            ////
8269
  ////////////////////////////////////////////////////////////////////
8270
  if (test_num == 12) // without and with padding
8271
  begin
8272
    // TEST 12: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 10Mbps )
8273
    test_name = "TEST 12: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 10Mbps )";
8274
    `TIME; $display("  TEST 12: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 10Mbps )");
8275
 
8276
    // reset MAC registers
8277
    hard_reset;
8278
    // reset MAC and MII LOGIC with soft reset
8279
    reset_mac;
8280
    reset_mii;
8281
    // set wb slave response
8282
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
8283
 
8284
    max_tmp = 0;
8285
    min_tmp = 0;
8286
    num_of_frames = 0;
8287
    num_of_bd = 0;
8288
    // set 47 TX buffer descriptors - must be set before TX enable
8289
    wbm_write(`ETH_TX_BD_NUM, 32'h2F, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8290
    // prepare a packet of MAXFL + 10 length
8291
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8292
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
8293
    min_tmp = tmp[31:16];
8294
    // change MAXFL value
8295
    max_tmp = min_tmp + 53;
8296
    wbm_write(`ETH_PACKETLEN, {min_tmp, max_tmp}, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8297
    st_data = 8'h62;
8298
    set_tx_packet(`MEMORY_BASE, max_tmp, st_data); // length with CRC
8299
    append_tx_crc(`MEMORY_BASE, (max_tmp - 5), 1'b0); // for first packet
8300
    // enable TX, set full-duplex mode, NO padding and NO CRC appending
8301
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD,
8302
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8303
    // check WB INT signal
8304
    if (wb_int !== 1'b0)
8305
    begin
8306
      test_fail("WB INT signal should not be set");
8307
      fail = fail + 1;
8308
    end
8309
 
8310
    // write to phy's control register for 10Mbps
8311
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
8312
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
8313
    speed = 10;
8314
 
8315
    i_length = (max_tmp - 5); // (max_tmp - 1); // not (max_tmp - 5) because NO automatic CRC appending
8316
    while (i_length <= (max_tmp - 3)) // (max_tmp + 1)) // (max_tmp) is the limit
8317
    begin
8318
      $display("   i_length = %0d", i_length);
8319
      // prepare packet's CRC
8320
      if (num_of_bd == 1)
8321
        append_tx_crc(`MEMORY_BASE, (max_tmp - 4), 1'b0); // for second and third packets
8322
      // Reset_tx_bd nable interrupt generation
8323
      // unmask interrupts
8324
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
8325
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8326
      // not detect carrier sense in FD and no collision
8327
      eth_phy.carrier_sense_tx_fd_detect(0);
8328
      eth_phy.collision(0);
8329
      // first destination address on ethernet PHY
8330
      eth_phy.set_tx_mem_addr(0);
8331
      // prepare BDs
8332
      if (num_of_bd == 0)
8333
      begin
8334
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8335
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8336
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8337
        set_tx_bd_wrap(2);
8338
        set_tx_bd_ready(0, 0);
8339
      end
8340
      else if (num_of_bd == 1)
8341
        set_tx_bd_ready(1, 1);
8342
      else if (num_of_bd == 2)
8343
        set_tx_bd_ready(2, 2);
8344
      // CHECK END OF TRANSMITION
8345
      check_tx_bd(num_of_bd, data);
8346
        wait (MTxEn === 1'b1); // start transmit
8347
      check_tx_bd(num_of_bd, data);
8348
        if (data[15] !== 1)
8349
        begin
8350
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
8351
          fail = fail + 1;
8352
        end
8353
        wait (MTxEn === 1'b0); // end transmit
8354
        while (data[15] === 1)
8355
        begin
8356
      check_tx_bd(num_of_bd, data);
8357
          @(posedge wb_clk);
8358
        end
8359
        repeat (1) @(posedge wb_clk);
8360
      // check length of a PACKET
8361
      $display("   eth_phy length = %0d", eth_phy.tx_len);
8362
      tmp_len = eth_phy.tx_len;
8363
      #1;
8364
      if (tmp_len != (i_length + 4))
8365
      begin
8366
        test_fail("Wrong length of the packet out from MAC");
8367
        fail = fail + 1;
8368
      end
8369
      // checking packet
8370
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
8371
      if (tmp > 0)
8372
      begin
8373
        test_fail("Wrong data of the transmitted packet");
8374
        fail = fail + 1;
8375
      end
8376
      // check transmited TX packet CRC
8377
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
8378
      if (tmp > 0)
8379
      begin
8380
        test_fail("Wrong CRC of the transmitted packet");
8381
        fail = fail + 1;
8382
      end
8383
      // check WB INT signal
8384
      if (wb_int !== 1'b1)
8385
      begin
8386
        `TIME; $display("*E WB INT signal should be set");
8387
        test_fail("WB INT signal should be set");
8388
        fail = fail + 1;
8389
      end
8390
      // check TX buffer descriptor of a packet
8391
      check_tx_bd(num_of_bd, data);
8392
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
8393
           ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
8394
      begin
8395
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8396
        test_fail("TX buffer descriptor status is not correct");
8397
        fail = fail + 1;
8398
      end
8399
      // check interrupts
8400
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8401
      if ((data & `ETH_INT_TXB) !== 1'b1)
8402
      begin
8403
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8404
        test_fail("Interrupt Transmit Buffer was not set");
8405
        fail = fail + 1;
8406
      end
8407
      if ((data & (~`ETH_INT_TXB)) !== 0)
8408
      begin
8409
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8410
        test_fail("Other interrupts (except Transmit Buffer) were set");
8411
        fail = fail + 1;
8412
      end
8413
      // clear interrupts
8414
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8415
      // check WB INT signal
8416
      if (wb_int !== 1'b0)
8417
      begin
8418
        test_fail("WB INT signal should not be set");
8419
        fail = fail + 1;
8420
      end
8421
      // INTERMEDIATE DISPLAYS
8422
      if (num_of_bd == 0)
8423
        $display("    ->packet with length %0d sent", (i_length + 4));
8424
      else if (num_of_bd == 1)
8425
        $display("    ->packet with length %0d sent", (i_length + 4));
8426
      else if (num_of_bd == 2)
8427
        $display("    ->packet with length %0d sent", (i_length + 4));
8428
      // set length (loop variable)
8429
      i_length = i_length + 1;
8430
      // the number of frame transmitted
8431
      num_of_frames = num_of_frames + 1;
8432
      num_of_bd = num_of_bd + 1;
8433
      @(posedge wb_clk);
8434
    end
8435
    // disable TX
8436
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8437
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8438
    @(posedge wb_clk);
8439
    if(fail == 0)
8440
      test_ok;
8441
    else
8442
      fail = 0;
8443
  end
8444
 
8445
 
8446
  ////////////////////////////////////////////////////////////////////
8447
  ////                                                            ////
8448
  ////  Test transmit packets across changed MAXFL value at       ////
8449
  ////  47 TX buffer decriptors ( 100Mbps ).                      ////
8450
  ////                                                            ////
8451
  ////////////////////////////////////////////////////////////////////
8452
  if (test_num == 13) // without and with padding
8453
  begin
8454
    // TEST 13: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 100Mbps )
8455
    test_name = "TEST 13: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 100Mbps )";
8456
    `TIME; $display("  TEST 13: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 100Mbps )");
8457
 
8458
    // reset MAC registers
8459
    hard_reset;
8460
    // reset MAC and MII LOGIC with soft reset
8461
    reset_mac;
8462
    reset_mii;
8463
    // set wb slave response
8464
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
8465
 
8466
    max_tmp = 0;
8467
    min_tmp = 0;
8468
    num_of_frames = 0;
8469
    num_of_bd = 0;
8470
    // set 47 TX buffer descriptors - must be set before TX enable
8471
    wbm_write(`ETH_TX_BD_NUM, 32'h2F, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8472
    // prepare a packet of MAXFL + 10 length
8473
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8474
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
8475
    min_tmp = tmp[31:16];
8476
    // change MAXFL value
8477
    max_tmp = min_tmp + 53;
8478
    wbm_write(`ETH_PACKETLEN, {min_tmp, max_tmp}, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8479
    st_data = 8'h62;
8480
    set_tx_packet(`MEMORY_BASE, max_tmp, st_data); // length with CRC
8481
    append_tx_crc(`MEMORY_BASE, (max_tmp - 5), 1'b0); // for first packet
8482
    // enable TX, set full-duplex mode, NO padding and NO CRC appending
8483
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD,
8484
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8485
    // check WB INT signal
8486
    if (wb_int !== 1'b0)
8487
    begin
8488
      test_fail("WB INT signal should not be set");
8489
      fail = fail + 1;
8490
    end
8491
 
8492
    // write to phy's control register for 100Mbps
8493
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
8494
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
8495
    speed = 100;
8496
 
8497
    i_length = (max_tmp - 5); // (max_tmp - 1); // not (max_tmp - 5) because NO automatic CRC appending
8498
    while (i_length <= (max_tmp - 3)) // (max_tmp + 1)) // (max_tmp) is the limit
8499
    begin
8500
      $display("   i_length = %0d", i_length);
8501
      // prepare packet's CRC
8502
      if (num_of_bd == 1)
8503
        append_tx_crc(`MEMORY_BASE, (max_tmp - 4), 1'b0); // for second and third packets
8504
      // Reset_tx_bd nable interrupt generation
8505
      // unmask interrupts
8506
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
8507
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8508
      // not detect carrier sense in FD and no collision
8509
      eth_phy.carrier_sense_tx_fd_detect(0);
8510
      eth_phy.collision(0);
8511
      // first destination address on ethernet PHY
8512
      eth_phy.set_tx_mem_addr(0);
8513
      // prepare BDs
8514
      if (num_of_bd == 0)
8515
      begin
8516
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8517
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8518
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8519
        set_tx_bd_wrap(2);
8520
        set_tx_bd_ready(0, 0);
8521
      end
8522
      else if (num_of_bd == 1)
8523
        set_tx_bd_ready(1, 1);
8524
      else if (num_of_bd == 2)
8525
        set_tx_bd_ready(2, 2);
8526
      // CHECK END OF TRANSMITION
8527
      check_tx_bd(num_of_bd, data);
8528
        wait (MTxEn === 1'b1); // start transmit
8529
      check_tx_bd(num_of_bd, data);
8530
        if (data[15] !== 1)
8531
        begin
8532
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
8533
          fail = fail + 1;
8534
        end
8535
        wait (MTxEn === 1'b0); // end transmit
8536
        while (data[15] === 1)
8537
        begin
8538
      check_tx_bd(num_of_bd, data);
8539
          @(posedge wb_clk);
8540
        end
8541
        repeat (1) @(posedge wb_clk);
8542
      // check length of a PACKET
8543
      $display("   eth_phy length = %0d", eth_phy.tx_len);
8544
      tmp_len = eth_phy.tx_len;
8545
      #1;
8546
      if (tmp_len != (i_length + 4))
8547
      begin
8548
        test_fail("Wrong length of the packet out from MAC");
8549
        fail = fail + 1;
8550
      end
8551
      // checking packet
8552
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
8553
      if (tmp > 0)
8554
      begin
8555
        test_fail("Wrong data of the transmitted packet");
8556
        fail = fail + 1;
8557
      end
8558
      // check transmited TX packet CRC
8559
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
8560
      if (tmp > 0)
8561
      begin
8562
        test_fail("Wrong CRC of the transmitted packet");
8563
        fail = fail + 1;
8564
      end
8565
      // check WB INT signal
8566
      if (wb_int !== 1'b1)
8567
      begin
8568
        `TIME; $display("*E WB INT signal should be set");
8569
        test_fail("WB INT signal should be set");
8570
        fail = fail + 1;
8571
      end
8572
      // check TX buffer descriptor of a packet
8573
      check_tx_bd(num_of_bd, data);
8574
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
8575
           ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
8576
      begin
8577
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8578
        test_fail("TX buffer descriptor status is not correct");
8579
        fail = fail + 1;
8580
      end
8581
      // check interrupts
8582
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8583
      if ((data & `ETH_INT_TXB) !== 1'b1)
8584
      begin
8585
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8586
        test_fail("Interrupt Transmit Buffer was not set");
8587
        fail = fail + 1;
8588
      end
8589
      if ((data & (~`ETH_INT_TXB)) !== 0)
8590
      begin
8591
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8592
        test_fail("Other interrupts (except Transmit Buffer) were set");
8593
        fail = fail + 1;
8594
      end
8595
      // clear interrupts
8596
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8597
      // check WB INT signal
8598
      if (wb_int !== 1'b0)
8599
      begin
8600
        test_fail("WB INT signal should not be set");
8601
        fail = fail + 1;
8602
      end
8603
      // INTERMEDIATE DISPLAYS
8604
      if (num_of_bd == 0)
8605
        $display("    ->packet with length %0d sent", (i_length + 4));
8606
      else if (num_of_bd == 1)
8607
        $display("    ->packet with length %0d sent", (i_length + 4));
8608
      else if (num_of_bd == 2)
8609
        $display("    ->packet with length %0d sent", (i_length + 4));
8610
      // set length (loop variable)
8611
      i_length = i_length + 1;
8612
      // the number of frame transmitted
8613
      num_of_frames = num_of_frames + 1;
8614
      num_of_bd = num_of_bd + 1;
8615
      @(posedge wb_clk);
8616
    end
8617
    // disable TX
8618
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8619
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8620
    @(posedge wb_clk);
8621
    if(fail == 0)
8622
      test_ok;
8623
    else
8624
      fail = 0;
8625
  end
8626
 
8627
 
8628
  ////////////////////////////////////////////////////////////////////
8629
  ////                                                            ////
8630
  ////  Test transmit packets across changed MINFL value at       ////
8631
  ////  7 TX buffer decriptors ( 10Mbps ).                        ////
8632
  ////                                                            ////
8633
  ////////////////////////////////////////////////////////////////////
8634
  if (test_num == 14) // without and with padding
8635
  begin
8636
    // TEST 14: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 10Mbps )
8637
    test_name = "TEST 14: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 10Mbps )";
8638
    `TIME; $display("  TEST 14: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 10Mbps )");
8639
 
8640
    // reset MAC registers
8641
    hard_reset;
8642
    // reset MAC and MII LOGIC with soft reset
8643
    reset_mac;
8644
    reset_mii;
8645
    // set wb slave response
8646
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
8647
 
8648
    max_tmp = 0;
8649
    min_tmp = 0;
8650
    num_of_frames = 0;
8651
    num_of_bd = 0;
8652
    // set 7 TX buffer descriptors - must be set before TX enable
8653
    wbm_write(`ETH_TX_BD_NUM, 32'h7, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8654
    // prepare a packet of MAXFL + 10 length
8655
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8656
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
8657
    min_tmp = tmp[31:16];
8658
    // change MINFL value
8659
    min_tmp = max_tmp - 177;
8660
    wbm_write(`ETH_PACKETLEN, {min_tmp, max_tmp}, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8661
    st_data = 8'h62;
8662
    set_tx_packet(`MEMORY_BASE, min_tmp, st_data); // length without CRC
8663
    // enable TX, set full-duplex mode, padding and CRC appending
8664
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
8665
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8666
    // check WB INT signal
8667
    if (wb_int !== 1'b0)
8668
    begin
8669
      test_fail("WB INT signal should not be set");
8670
      fail = fail + 1;
8671
    end
8672
 
8673
    // write to phy's control register for 10Mbps
8674
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
8675
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
8676
    speed = 10;
8677
 
8678
    i_length = (min_tmp - 5);
8679
    while (i_length <= (min_tmp - 3)) // (min_tmp - 4) is the limit
8680
    begin
8681
      $display("   i_length = %0d", i_length);
8682
      // Reset_tx_bd nable interrupt generation
8683
      // unmask interrupts
8684
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
8685
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8686
      // not detect carrier sense in FD and no collision
8687
      eth_phy.carrier_sense_tx_fd_detect(0);
8688
      eth_phy.collision(0);
8689
      // first destination address on ethernet PHY
8690
      eth_phy.set_tx_mem_addr(0);
8691
      // prepare BDs
8692
      if (num_of_bd == 0)
8693
      begin
8694
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8695
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8696
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8697
        set_tx_bd_wrap(2);
8698
        set_tx_bd_ready(0, 0);
8699
      end
8700
      else if (num_of_bd == 1)
8701
        set_tx_bd_ready(1, 1);
8702
      else if (num_of_bd == 2)
8703
        set_tx_bd_ready(2, 2);
8704
      // CHECK END OF TRANSMITION
8705
      check_tx_bd(num_of_bd, data);
8706
        wait (MTxEn === 1'b1); // start transmit
8707
      check_tx_bd(num_of_bd, data);
8708
        if (data[15] !== 1)
8709
        begin
8710
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
8711
          fail = fail + 1;
8712
        end
8713
        wait (MTxEn === 1'b0); // end transmit
8714
        while (data[15] === 1)
8715
        begin
8716
      check_tx_bd(num_of_bd, data);
8717
          @(posedge wb_clk);
8718
        end
8719
        repeat (1) @(posedge wb_clk);
8720
      // check length of a PACKET
8721
      $display("   eth_phy length = %0d", eth_phy.tx_len);
8722
      tmp_len = eth_phy.tx_len;
8723
      #1;
8724
      if (tmp_len != (i_length + 4))
8725
      begin
8726
        test_fail("Wrong length of the packet out from MAC");
8727
        fail = fail + 1;
8728
      end
8729
      // checking packet
8730
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
8731
      if (tmp > 0)
8732
      begin
8733
        test_fail("Wrong data of the transmitted packet");
8734
        fail = fail + 1;
8735
      end
8736
      // check transmited TX packet CRC
8737
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
8738
      if (tmp > 0)
8739
      begin
8740
        test_fail("Wrong CRC of the transmitted packet");
8741
        fail = fail + 1;
8742
      end
8743
      // check WB INT signal
8744
      if (wb_int !== 1'b1)
8745
      begin
8746
        `TIME; $display("*E WB INT signal should be set");
8747
        test_fail("WB INT signal should be set");
8748
        fail = fail + 1;
8749
      end
8750
      // check TX buffer descriptor of a packet
8751
      check_tx_bd(num_of_bd, data);
8752
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
8753
           ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
8754
      begin
8755
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8756
        test_fail("TX buffer descriptor status is not correct");
8757
        fail = fail + 1;
8758
      end
8759
      // check interrupts
8760
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8761
      if ((data & `ETH_INT_TXB) !== 1'b1)
8762
      begin
8763
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8764
        test_fail("Interrupt Transmit Buffer was not set");
8765
        fail = fail + 1;
8766
      end
8767
      if ((data & (~`ETH_INT_TXB)) !== 0)
8768
      begin
8769
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8770
        test_fail("Other interrupts (except Transmit Buffer) were set");
8771
        fail = fail + 1;
8772
      end
8773
      // clear interrupts
8774
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8775
      // check WB INT signal
8776
      if (wb_int !== 1'b0)
8777
      begin
8778
        test_fail("WB INT signal should not be set");
8779
        fail = fail + 1;
8780
      end
8781
      // INTERMEDIATE DISPLAYS
8782
      if (num_of_bd == 0)
8783
        $display("    ->packet with length %0d sent", (i_length + 4));
8784
      else if (num_of_bd == 1)
8785
        $display("    ->packet with length %0d sent", (i_length + 4));
8786
      else if (num_of_bd == 2)
8787
        $display("    ->packet with length %0d sent", (i_length + 4));
8788
      // set length (loop variable)
8789
      i_length = i_length + 1;
8790
      // the number of frame transmitted
8791
      num_of_frames = num_of_frames + 1;
8792
      num_of_bd = num_of_bd + 1;
8793
      @(posedge wb_clk);
8794
    end
8795
    // disable TX
8796
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8797
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8798
    @(posedge wb_clk);
8799
    if(fail == 0)
8800
      test_ok;
8801
    else
8802
      fail = 0;
8803
  end
8804
 
8805
 
8806
  ////////////////////////////////////////////////////////////////////
8807
  ////                                                            ////
8808
  ////  Test transmit packets across changed MINFL value at       ////
8809
  ////  7 TX buffer decriptors ( 100Mbps ).                       ////
8810
  ////                                                            ////
8811
  ////////////////////////////////////////////////////////////////////
8812
  if (test_num == 15) // without and with padding
8813
  begin
8814
    // TEST 15: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 100Mbps )
8815
    test_name = "TEST 15: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 100Mbps )";
8816
    `TIME; $display("  TEST 15: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 100Mbps )");
8817
 
8818
    // reset MAC registers
8819
    hard_reset;
8820
    // reset MAC and MII LOGIC with soft reset
8821
    reset_mac;
8822
    reset_mii;
8823
    // set wb slave response
8824
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
8825
 
8826
    max_tmp = 0;
8827
    min_tmp = 0;
8828
    num_of_frames = 0;
8829
    num_of_bd = 0;
8830
    // set 7 TX buffer descriptors - must be set before TX enable
8831
    wbm_write(`ETH_TX_BD_NUM, 32'h7, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8832
    // prepare a packet of MAXFL + 10 length
8833
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8834
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
8835
    min_tmp = tmp[31:16];
8836
    // change MINFL value
8837
    min_tmp = max_tmp - 177;
8838
    wbm_write(`ETH_PACKETLEN, {min_tmp, max_tmp}, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8839
    st_data = 8'h62;
8840
    set_tx_packet(`MEMORY_BASE, min_tmp, st_data); // length without CRC
8841
    // enable TX, set full-duplex mode, padding and CRC appending
8842
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
8843
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8844
    // check WB INT signal
8845
    if (wb_int !== 1'b0)
8846
    begin
8847
      test_fail("WB INT signal should not be set");
8848
      fail = fail + 1;
8849
    end
8850
 
8851
    // write to phy's control register for 100Mbps
8852
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
8853
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
8854
    speed = 100;
8855
 
8856
    i_length = (min_tmp - 5);
8857
    while (i_length <= (min_tmp - 3)) // (min_tmp - 4) is the limit
8858
    begin
8859
      $display("   i_length = %0d", i_length);
8860
      // Reset_tx_bd nable interrupt generation
8861
      // unmask interrupts
8862
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
8863
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8864
      // not detect carrier sense in FD and no collision
8865
      eth_phy.carrier_sense_tx_fd_detect(0);
8866
      eth_phy.collision(0);
8867
      // first destination address on ethernet PHY
8868
      eth_phy.set_tx_mem_addr(0);
8869
      // prepare BDs
8870
      if (num_of_bd == 0)
8871
      begin
8872
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8873
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8874
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8875
        set_tx_bd_wrap(2);
8876
        set_tx_bd_ready(0, 0);
8877
      end
8878
      else if (num_of_bd == 1)
8879
        set_tx_bd_ready(1, 1);
8880
      else if (num_of_bd == 2)
8881
        set_tx_bd_ready(2, 2);
8882
      // CHECK END OF TRANSMITION
8883
      check_tx_bd(num_of_bd, data);
8884
        wait (MTxEn === 1'b1); // start transmit
8885
      check_tx_bd(num_of_bd, data);
8886
        if (data[15] !== 1)
8887
        begin
8888
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
8889
          fail = fail + 1;
8890
        end
8891
        wait (MTxEn === 1'b0); // end transmit
8892
        while (data[15] === 1)
8893
        begin
8894
      check_tx_bd(num_of_bd, data);
8895
          @(posedge wb_clk);
8896
        end
8897
        repeat (1) @(posedge wb_clk);
8898
      // check length of a PACKET
8899
      $display("   eth_phy length = %0d", eth_phy.tx_len);
8900
      tmp_len = eth_phy.tx_len;
8901
      #1;
8902
      if (tmp_len != (i_length + 4))
8903
      begin
8904
        test_fail("Wrong length of the packet out from MAC");
8905
        fail = fail + 1;
8906
      end
8907
      // checking packet
8908
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
8909
      if (tmp > 0)
8910
      begin
8911
        test_fail("Wrong data of the transmitted packet");
8912
        fail = fail + 1;
8913
      end
8914
      // check transmited TX packet CRC
8915
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
8916
      if (tmp > 0)
8917
      begin
8918
        test_fail("Wrong CRC of the transmitted packet");
8919
        fail = fail + 1;
8920
      end
8921
      // check WB INT signal
8922
      if (wb_int !== 1'b1)
8923
      begin
8924
        `TIME; $display("*E WB INT signal should be set");
8925
        test_fail("WB INT signal should be set");
8926
        fail = fail + 1;
8927
      end
8928
      // check TX buffer descriptor of a packet
8929
      check_tx_bd(num_of_bd, data);
8930
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
8931
           ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
8932
      begin
8933
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8934
        test_fail("TX buffer descriptor status is not correct");
8935
        fail = fail + 1;
8936
      end
8937
      // check interrupts
8938
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8939
      if ((data & `ETH_INT_TXB) !== 1'b1)
8940
      begin
8941
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8942
        test_fail("Interrupt Transmit Buffer was not set");
8943
        fail = fail + 1;
8944
      end
8945
      if ((data & (~`ETH_INT_TXB)) !== 0)
8946
      begin
8947
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8948
        test_fail("Other interrupts (except Transmit Buffer) were set");
8949
        fail = fail + 1;
8950
      end
8951
      // clear interrupts
8952
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8953
      // check WB INT signal
8954
      if (wb_int !== 1'b0)
8955
      begin
8956
        test_fail("WB INT signal should not be set");
8957
        fail = fail + 1;
8958
      end
8959
      // INTERMEDIATE DISPLAYS
8960
      if (num_of_bd == 0)
8961
        $display("    ->packet with length %0d sent", (i_length + 4));
8962
      else if (num_of_bd == 1)
8963
        $display("    ->packet with length %0d sent", (i_length + 4));
8964
      else if (num_of_bd == 2)
8965
        $display("    ->packet with length %0d sent", (i_length + 4));
8966
      // set length (loop variable)
8967
      i_length = i_length + 1;
8968
      // the number of frame transmitted
8969
      num_of_frames = num_of_frames + 1;
8970
      num_of_bd = num_of_bd + 1;
8971
      @(posedge wb_clk);
8972
    end
8973
    // disable TX
8974
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8975
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8976
    @(posedge wb_clk);
8977
    if(fail == 0)
8978
      test_ok;
8979
    else
8980
      fail = 0;
8981
  end
8982
 
8983
 
8984
  ////////////////////////////////////////////////////////////////////
8985
  ////                                                            ////
8986
  ////  Test transmit packets across MAXFL with HUGEN at          ////
8987
  ////  19 TX buffer decriptors ( 10Mbps ).                       ////
8988
  ////                                                            ////
8989
  ////////////////////////////////////////////////////////////////////
8990
  if (test_num == 16) // without and with padding
8991
  begin
8992
    // TEST 16: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 10Mbps )
8993
    test_name = "TEST 16: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 10Mbps )";
8994
    `TIME; $display("  TEST 16: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 10Mbps )");
8995
 
8996
    // reset MAC registers
8997
    hard_reset;
8998
    // reset MAC and MII LOGIC with soft reset
8999
    reset_mac;
9000
    reset_mii;
9001
    // set wb slave response
9002
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
9003
 
9004
    max_tmp = 0;
9005
    min_tmp = 0;
9006
    num_of_frames = 0;
9007
    num_of_bd = 0;
9008
    // set 19 TX buffer descriptors - must be set before TX enable
9009
    wbm_write(`ETH_TX_BD_NUM, 32'h13, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9010
    // prepare a packet of 64k - 1 length (16'hFFFF)
9011
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9012
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
9013
    min_tmp = tmp[31:16];
9014
    st_data = 8'h8D;
9015
    set_tx_packet(`MEMORY_BASE, 16'hFFFF, st_data); // length with CRC
9016
    // enable TX, set full-duplex mode, NO padding, CRC appending and huge enabled
9017
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN | `ETH_MODER_HUGEN,
9018
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9019
    // check WB INT signal
9020
    if (wb_int !== 1'b0)
9021
    begin
9022
      test_fail("WB INT signal should not be set");
9023
      fail = fail + 1;
9024
    end
9025
 
9026
    // write to phy's control register for 10Mbps
9027
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
9028
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
9029
    speed = 10;
9030
 
9031
    i_length = (max_tmp - 5); // (max_tmp - 4) is the MAXFL limit
9032
    while (i_length <= (16'hFFFF - 4)) // (16'hFFFF - 4) is the limit
9033
    begin
9034
      $display("   i_length = %0d", i_length);
9035
      // Reset_tx_bd nable interrupt generation
9036
      // unmask interrupts
9037
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
9038
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9039
      // not detect carrier sense in FD and no collision
9040
      eth_phy.carrier_sense_tx_fd_detect(0);
9041
      eth_phy.collision(0);
9042
      // first destination address on ethernet PHY
9043
      eth_phy.set_tx_mem_addr(0);
9044
      // prepare BDs
9045
      if (num_of_bd == 0)
9046
      begin
9047
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9048
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9049
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9050
        set_tx_bd(3, 3, (16'hFFFF - 5), 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9051
        set_tx_bd(4, 4, (16'hFFFF - 4), 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9052
        set_tx_bd_wrap(4);
9053
        set_tx_bd_ready(0, 0);
9054
      end
9055
      else if (num_of_bd == 1)
9056
        set_tx_bd_ready(1, 1);
9057
      else if (num_of_bd == 2)
9058
        set_tx_bd_ready(2, 2);
9059
      else if (num_of_bd == 3)
9060
        set_tx_bd_ready(3, 3);
9061
      else if (num_of_bd == 4)
9062
        set_tx_bd_ready(4, 4);
9063
      // CHECK END OF TRANSMITION
9064
      check_tx_bd(num_of_bd, data);
9065
        wait (MTxEn === 1'b1); // start transmit
9066
      check_tx_bd(num_of_bd, data);
9067
        if (data[15] !== 1)
9068
        begin
9069
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
9070
          fail = fail + 1;
9071
        end
9072
        wait (MTxEn === 1'b0); // end transmit
9073
        while (data[15] === 1)
9074
        begin
9075
      check_tx_bd(num_of_bd, data);
9076
          @(posedge wb_clk);
9077
        end
9078
        repeat (1) @(posedge wb_clk);
9079
      // check length of a PACKET
9080
      $display("   eth_phy length = %0d", eth_phy.tx_len);
9081
      tmp_len = eth_phy.tx_len;
9082
      #1;
9083
      if (tmp_len != (i_length + 4))
9084
      begin
9085
        test_fail("Wrong length of the packet out from MAC");
9086
        fail = fail + 1;
9087
      end
9088
      // checking packet
9089
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
9090
      if (tmp > 0)
9091
      begin
9092
        test_fail("Wrong data of the transmitted packet");
9093
        fail = fail + 1;
9094
      end
9095
      // check transmited TX packet CRC
9096
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
9097
      if (tmp > 0)
9098
      begin
9099
        test_fail("Wrong CRC of the transmitted packet");
9100
        fail = fail + 1;
9101
      end
9102
      // check WB INT signal
9103
      if (wb_int !== 1'b1)
9104
      begin
9105
        `TIME; $display("*E WB INT signal should be set");
9106
        test_fail("WB INT signal should be set");
9107
        fail = fail + 1;
9108
      end
9109
      // check TX buffer descriptor of a packet
9110
      check_tx_bd(num_of_bd, data);
9111
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 4)) || // wrap bit
9112
           ((data[15:0] !== 16'h5800) && (num_of_bd < 4)) )   // without wrap bit
9113
      begin
9114
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
9115
        test_fail("TX buffer descriptor status is not correct");
9116
        fail = fail + 1;
9117
      end
9118
      // check interrupts
9119
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9120
      if ((data & `ETH_INT_TXB) !== 1'b1)
9121
      begin
9122
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
9123
        test_fail("Interrupt Transmit Buffer was not set");
9124
        fail = fail + 1;
9125
      end
9126
      if ((data & (~`ETH_INT_TXB)) !== 0)
9127
      begin
9128
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
9129
        test_fail("Other interrupts (except Transmit Buffer) were set");
9130
        fail = fail + 1;
9131
      end
9132
      // clear interrupts
9133
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9134
      // check WB INT signal
9135
      if (wb_int !== 1'b0)
9136
      begin
9137
        test_fail("WB INT signal should not be set");
9138
        fail = fail + 1;
9139
      end
9140
      // INTERMEDIATE DISPLAYS
9141
      $display("    ->packet with length %0d sent", (i_length + 4));
9142
      // set length (loop variable)
9143
      if ((num_of_bd < 2) || (num_of_bd >= 3))
9144
        i_length = i_length + 1;
9145
      else if (num_of_bd == 2)
9146
        i_length = (16'hFFFF - 5);
9147
      // the number of frame transmitted
9148
      num_of_frames = num_of_frames + 1;
9149
      num_of_bd = num_of_bd + 1;
9150
      @(posedge wb_clk);
9151
    end
9152
    // disable TX
9153
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
9154
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9155
    @(posedge wb_clk);
9156
    if(fail == 0)
9157
      test_ok;
9158
    else
9159
      fail = 0;
9160
  end
9161
 
9162
 
9163
  ////////////////////////////////////////////////////////////////////
9164
  ////                                                            ////
9165
  ////  Test transmit packets across MAXFL with HUGEN at          ////
9166
  ////  19 TX buffer decriptors ( 100Mbps ).                      ////
9167
  ////                                                            ////
9168
  ////////////////////////////////////////////////////////////////////
9169
  if (test_num == 17) // without and with padding
9170
  begin
9171
    // TEST 17: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 100Mbps )
9172
    test_name = "TEST 17: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 100Mbps )";
9173
    `TIME; $display("  TEST 17: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 100Mbps )");
9174
 
9175
    // reset MAC registers
9176
    hard_reset;
9177
    // reset MAC and MII LOGIC with soft reset
9178
    reset_mac;
9179
    reset_mii;
9180
    // set wb slave response
9181
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
9182
 
9183
    max_tmp = 0;
9184
    min_tmp = 0;
9185
    num_of_frames = 0;
9186
    num_of_bd = 0;
9187
    // set 19 TX buffer descriptors - must be set before TX enable
9188
    wbm_write(`ETH_TX_BD_NUM, 32'h13, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9189
    // prepare a packet of 64k - 1 length (16'hFFFF)
9190
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9191
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
9192
    min_tmp = tmp[31:16];
9193
    st_data = 8'h8D;
9194
    set_tx_packet(`MEMORY_BASE, 16'hFFFF, st_data); // length with CRC
9195
    // enable TX, set full-duplex mode, NO padding, CRC appending and huge enabled
9196
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN | `ETH_MODER_HUGEN,
9197
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9198
    // check WB INT signal
9199
    if (wb_int !== 1'b0)
9200
    begin
9201
      test_fail("WB INT signal should not be set");
9202
      fail = fail + 1;
9203
    end
9204
 
9205
    // write to phy's control register for 100Mbps
9206
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
9207
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
9208
    speed = 100;
9209
 
9210
    i_length = (max_tmp - 5); // (max_tmp - 4) is the MAXFL limit
9211
    while (i_length <= (16'hFFFF - 4)) // (16'hFFFF - 4) is the limit
9212
    begin
9213
      $display("   i_length = %0d", i_length);
9214
      // Reset_tx_bd nable interrupt generation
9215
      // unmask interrupts
9216
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
9217
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9218
      // not detect carrier sense in FD and no collision
9219
      eth_phy.carrier_sense_tx_fd_detect(0);
9220
      eth_phy.collision(0);
9221
      // first destination address on ethernet PHY
9222
      eth_phy.set_tx_mem_addr(0);
9223
      // prepare BDs
9224
      if (num_of_bd == 0)
9225
      begin
9226
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9227
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9228
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9229
        set_tx_bd(3, 3, (16'hFFFF - 5), 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9230
        set_tx_bd(4, 4, (16'hFFFF - 4), 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9231
        set_tx_bd_wrap(4);
9232
        set_tx_bd_ready(0, 0);
9233
      end
9234
      else if (num_of_bd == 1)
9235
        set_tx_bd_ready(1, 1);
9236
      else if (num_of_bd == 2)
9237
        set_tx_bd_ready(2, 2);
9238
      else if (num_of_bd == 3)
9239
        set_tx_bd_ready(3, 3);
9240
      else if (num_of_bd == 4)
9241
        set_tx_bd_ready(4, 4);
9242
      // CHECK END OF TRANSMITION
9243
      check_tx_bd(num_of_bd, data);
9244
        wait (MTxEn === 1'b1); // start transmit
9245
      check_tx_bd(num_of_bd, data);
9246
        if (data[15] !== 1)
9247
        begin
9248
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
9249
          fail = fail + 1;
9250
        end
9251
        wait (MTxEn === 1'b0); // end transmit
9252
        while (data[15] === 1)
9253
        begin
9254
      check_tx_bd(num_of_bd, data);
9255
          @(posedge wb_clk);
9256
        end
9257
        repeat (1) @(posedge wb_clk);
9258
      // check length of a PACKET
9259
      $display("   eth_phy length = %0d", eth_phy.tx_len);
9260
      tmp_len = eth_phy.tx_len;
9261
      #1;
9262
      if (tmp_len != (i_length + 4))
9263
      begin
9264
        test_fail("Wrong length of the packet out from MAC");
9265
        fail = fail + 1;
9266
      end
9267
      // checking packet
9268
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
9269
      if (tmp > 0)
9270
      begin
9271
        test_fail("Wrong data of the transmitted packet");
9272
        fail = fail + 1;
9273
      end
9274
      // check transmited TX packet CRC
9275
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
9276
      if (tmp > 0)
9277
      begin
9278
        test_fail("Wrong CRC of the transmitted packet");
9279
        fail = fail + 1;
9280
      end
9281
      // check WB INT signal
9282
      if (wb_int !== 1'b1)
9283
      begin
9284
        `TIME; $display("*E WB INT signal should be set");
9285
        test_fail("WB INT signal should be set");
9286
        fail = fail + 1;
9287
      end
9288
      // check TX buffer descriptor of a packet
9289
      check_tx_bd(num_of_bd, data);
9290
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 4)) || // wrap bit
9291
           ((data[15:0] !== 16'h5800) && (num_of_bd < 4)) )   // without wrap bit
9292
      begin
9293
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
9294
        test_fail("TX buffer descriptor status is not correct");
9295
        fail = fail + 1;
9296
      end
9297
      // check interrupts
9298
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9299
      if ((data & `ETH_INT_TXB) !== 1'b1)
9300
      begin
9301
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
9302
        test_fail("Interrupt Transmit Buffer was not set");
9303
        fail = fail + 1;
9304
      end
9305
      if ((data & (~`ETH_INT_TXB)) !== 0)
9306
      begin
9307
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
9308
        test_fail("Other interrupts (except Transmit Buffer) were set");
9309
        fail = fail + 1;
9310
      end
9311
      // clear interrupts
9312
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9313
      // check WB INT signal
9314
      if (wb_int !== 1'b0)
9315
      begin
9316
        test_fail("WB INT signal should not be set");
9317
        fail = fail + 1;
9318
      end
9319
      // INTERMEDIATE DISPLAYS
9320
      $display("    ->packet with length %0d sent", (i_length + 4));
9321
      // set length (loop variable)
9322
      if ((num_of_bd < 2) || (num_of_bd >= 3))
9323
        i_length = i_length + 1;
9324
      else if (num_of_bd == 2)
9325
        i_length = (16'hFFFF - 5);
9326
      // the number of frame transmitted
9327
      num_of_frames = num_of_frames + 1;
9328
      num_of_bd = num_of_bd + 1;
9329
      @(posedge wb_clk);
9330
    end
9331
    // disable TX
9332
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
9333
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9334
    @(posedge wb_clk);
9335
    if(fail == 0)
9336
      test_ok;
9337
    else
9338
      fail = 0;
9339
  end
9340
 
9341
 
9342
  ////////////////////////////////////////////////////////////////////
9343
  ////                                                            ////
9344
  ////  Test IPG during Back-to-Back transmit at                  ////
9345
  ////  88 TX buffer decriptors ( 10Mbps ).                       ////
9346
  ////                                                            ////
9347
  ////////////////////////////////////////////////////////////////////
9348
  if (test_num == 18) // without and with padding
9349
  begin
9350
    // TEST 18: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 10Mbps )
9351
    test_name = "TEST 18: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 10Mbps )";
9352
    `TIME; $display("  TEST 18: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 10Mbps )");
9353
 
9354
    // reset MAC registers
9355
    hard_reset;
9356
    // reset MAC and MII LOGIC with soft reset
9357
    reset_mac;
9358
    reset_mii;
9359
    // set wb slave response
9360
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
9361
 
9362
    max_tmp = 0;
9363
    min_tmp = 0;
9364
    num_of_frames = 0;
9365
    num_of_bd = 0;
9366
    tmp_ipgt = 0;
9367
    // set 88 TX buffer descriptors - must be set before TX enable
9368
    wbm_write(`ETH_TX_BD_NUM, 32'h58, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9369
    // enable TX, set full-duplex mode, NO padding and CRC appending
9370
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9371
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9372
    // prepare two packets of MAXFL length
9373
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9374
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
9375
    min_tmp = tmp[31:16];
9376
    st_data = 8'h29;
9377
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
9378
    // check WB INT signal
9379
    if (wb_int !== 1'b0)
9380
    begin
9381
      test_fail("WB INT signal should not be set");
9382
      fail = fail + 1;
9383
    end
9384
 
9385
    // write to phy's control register for 10Mbps
9386
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
9387
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
9388
    speed = 10;
9389
 
9390
    i_length = (min_tmp - 4);
9391
    while (i_length < (max_tmp - 4))
9392
    begin
9393
      // disable TX, set full-duplex mode, NO padding and CRC appending
9394
      wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9395
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9396
      // set IPGT register
9397
      wbm_write(`ETH_IPGT, tmp_ipgt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9398
      // enable TX, set full-duplex mode, NO padding and CRC appending
9399
      wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9400
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9401
      // Reset_tx_bd enable interrupt generation
9402
      // unmask interrupts
9403
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
9404
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9405
      // not detect carrier sense in FD and no collision
9406
      eth_phy.carrier_sense_tx_fd_detect(0);
9407
      eth_phy.collision(0);
9408
      // first destination address on ethernet PHY
9409
      eth_phy.set_tx_mem_addr(0);
9410
      // prepare BDs
9411
      if (num_of_bd == 0)
9412
      begin
9413
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9414
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9415
        set_tx_bd_wrap(1);
9416
        set_tx_bd_ready(0, 0);
9417
        set_tx_bd_ready(1, 1);
9418
      end
9419
      // CHECK END OF TWO TRANSMITIONs
9420
      // wait for first transmit to end
9421
      check_tx_bd(num_of_bd, data);
9422
      wait (MTxEn === 1'b1); // start transmit
9423
      if (data[15] !== 1)
9424
      begin
9425
        test_fail("Wrong buffer descriptor's ready bit read out from MAC");
9426
        fail = fail + 1;
9427
      end
9428
      wait (MTxEn === 1'b0); // end transmit
9429
      num_of_frames = num_of_frames + 1;
9430
      num_of_bd = num_of_bd + 1;
9431
      #Tp;
9432
      // destination address on ethernet PHY
9433
      eth_phy.set_tx_mem_addr(0);
9434
      i1 = 0;
9435
      i2 = 0;
9436
      // count IPG clock periods
9437
      fork
9438
        begin
9439
          wait (MTxEn === 1'b1); // start second transmit
9440
          #Tp;
9441
          disable count_rising;
9442
          disable count_falling;
9443
        end
9444
        begin: count_rising
9445
          forever
9446
          begin
9447
            @(posedge mtx_clk);
9448
            i1 = i1 + 1;
9449
            #Tp;
9450
          end
9451
        end
9452
        begin: count_falling
9453
          forever
9454
          begin
9455
            @(negedge mtx_clk);
9456
            i2 = i2 + 1;
9457
            #Tp;
9458
          end
9459
        end
9460
      join
9461
      // check IPG length - INTERMEDIATE DISPLAYS
9462
      if((i1 == i2) && (i1 >= (tmp_ipgt + 3)))
9463
      begin
9464
        $display("    ->IPG with %0d mtx_clk periods (min %0d) between packets with lengths %0d and %0d checked",
9465
                  i1, (tmp_ipgt + 3), (i_length + 4), (i_length + 4 + 1));
9466
      end
9467
      else
9468
      begin
9469
        `TIME; $display("*E IPG is not correct: (%0d + %0d) / 2, requested: %d", i1, i2, (tmp_ipgt + 3));
9470
        fail = fail + 1;
9471
        test_fail("IPG is not correct");
9472
      end
9473
      // wait for second transmit to end
9474
      wait (MTxEn === 1'b0); // end second transmit
9475
      while (data[15] === 1)
9476
      begin
9477
        check_tx_bd(num_of_bd, data);
9478
        @(posedge wb_clk);
9479
      end
9480
      repeat (1) @(posedge wb_clk);
9481
      // check length of a second PACKET
9482
      tmp_len = eth_phy.tx_len;
9483
      #1;
9484
      if (tmp_len != (i_length + 4 + 1))
9485
      begin
9486
        test_fail("Wrong length of second packet out from MAC");
9487
        fail = fail + 1;
9488
      end
9489
      // checking second packet
9490
      check_tx_packet(`MEMORY_BASE, 0, (i_length + 1), tmp);
9491
      if (tmp > 0)
9492
      begin
9493
        test_fail("Wrong data of second transmitted packet");
9494
        fail = fail + 1;
9495
      end
9496
      // check second transmited TX packet CRC
9497
      check_tx_crc(0, (i_length + 1), 1'b0, tmp); // length without CRC
9498
      if (tmp > 0)
9499
      begin
9500
        test_fail("Wrong CRC of second transmitted packet");
9501
        fail = fail + 1;
9502
      end
9503
      // check WB INT signal
9504
      if (wb_int !== 1'b1)
9505
      begin
9506
        `TIME; $display("*E WB INT signal should be set");
9507
        test_fail("WB INT signal should be set");
9508
        fail = fail + 1;
9509
      end
9510
      // check TX buffer descriptor of a packet
9511
      check_tx_bd(num_of_bd, data);
9512
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
9513
           ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
9514
      begin
9515
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
9516
        test_fail("TX buffer descriptor status is not correct");
9517
        fail = fail + 1;
9518
      end
9519
      // check interrupts
9520
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9521
      if ((data & `ETH_INT_TXB) !== 1'b1)
9522
      begin
9523
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
9524
        test_fail("Interrupt Transmit Buffer was not set");
9525
        fail = fail + 1;
9526
      end
9527
      if ((data & (~`ETH_INT_TXB)) !== 0)
9528
      begin
9529
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
9530
        test_fail("Other interrupts (except Transmit Buffer) were set");
9531
        fail = fail + 1;
9532
      end
9533
      // clear interrupts
9534
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9535
      // check WB INT signal
9536
      if (wb_int !== 1'b0)
9537
      begin
9538
        test_fail("WB INT signal should not be set");
9539
        fail = fail + 1;
9540
      end
9541
      // set length (LOOP variable)
9542
      if ((tmp_ipgt + 3) < 130) // tmp_ipgt < 124
9543
        i_length = i_length + 2;
9544
      else
9545
        i_length = (max_tmp - 4);
9546
      // set IPGT
9547
      if ((tmp_ipgt + 3) < 10)
9548
        tmp_ipgt = tmp_ipgt + 1;
9549
      else if ((tmp_ipgt + 3) < 24)
9550
        tmp_ipgt = tmp_ipgt + 7;
9551
      else if ((tmp_ipgt + 3) == 24)
9552
        tmp_ipgt = 38 - 3;
9553
      else if ((tmp_ipgt + 3) == 38)
9554
        tmp_ipgt = 72 - 3;
9555
      else if ((tmp_ipgt + 3) == 72)
9556
        tmp_ipgt = 130 - 3; // 124 - 3
9557
      // the number of frame transmitted
9558
      num_of_frames = num_of_frames + 1;
9559
      num_of_bd = 0;
9560
      @(posedge wb_clk);
9561
    end
9562
    // disable TX
9563
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
9564
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9565
    @(posedge wb_clk);
9566
    if(fail == 0)
9567
      test_ok;
9568
    else
9569
      fail = 0;
9570
  end
9571
 
9572
 
9573
  ////////////////////////////////////////////////////////////////////
9574
  ////                                                            ////
9575
  ////  Test IPG during Back-to-Back transmit at                  ////
9576
  ////  88 TX buffer decriptors ( 100Mbps ).                      ////
9577
  ////                                                            ////
9578
  ////////////////////////////////////////////////////////////////////
9579
  if (test_num == 19) // without and with padding
9580
  begin
9581
    // TEST 19: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 100Mbps )
9582
    test_name = "TEST 19: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 100Mbps )";
9583
    `TIME; $display("  TEST 19: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 100Mbps )");
9584
 
9585
    // reset MAC registers
9586
    hard_reset;
9587
    // reset MAC and MII LOGIC with soft reset
9588
    reset_mac;
9589
    reset_mii;
9590
    // set wb slave response
9591
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
9592
 
9593
    max_tmp = 0;
9594
    min_tmp = 0;
9595
    num_of_frames = 0;
9596
    num_of_bd = 0;
9597
    tmp_ipgt = 0;
9598
    // set 88 TX buffer descriptors - must be set before TX enable
9599
    wbm_write(`ETH_TX_BD_NUM, 32'h58, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9600
    // enable TX, set full-duplex mode, NO padding and CRC appending
9601
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9602
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9603
    // prepare two packets of MAXFL length
9604
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9605
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
9606
    min_tmp = tmp[31:16];
9607
    st_data = 8'h29;
9608
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
9609
    // check WB INT signal
9610
    if (wb_int !== 1'b0)
9611
    begin
9612
      test_fail("WB INT signal should not be set");
9613
      fail = fail + 1;
9614
    end
9615
 
9616
    // write to phy's control register for 100Mbps
9617
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
9618
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
9619
    speed = 100;
9620
 
9621
    i_length = (min_tmp - 4);
9622
    while (i_length < (max_tmp - 4))
9623
    begin
9624
      // disable TX, set full-duplex mode, NO padding and CRC appending
9625
      wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9626
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9627
      // set IPGT register
9628
      wbm_write(`ETH_IPGT, tmp_ipgt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9629
      // enable TX, set full-duplex mode, NO padding and CRC appending
9630
      wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9631
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9632
      // Reset_tx_bd enable interrupt generation
9633
      // unmask interrupts
9634
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
9635
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9636
      // not detect carrier sense in FD and no collision
9637
      eth_phy.carrier_sense_tx_fd_detect(0);
9638
      eth_phy.collision(0);
9639
      // first destination address on ethernet PHY
9640
      eth_phy.set_tx_mem_addr(0);
9641
      // prepare BDs
9642
      if (num_of_bd == 0)
9643
      begin
9644
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9645
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9646
        set_tx_bd_wrap(1);
9647
        set_tx_bd_ready(0, 0);
9648
        set_tx_bd_ready(1, 1);
9649
      end
9650
      // CHECK END OF TWO TRANSMITIONs
9651
      // wait for first transmit to end
9652
      check_tx_bd(num_of_bd, data);
9653
      wait (MTxEn === 1'b1); // start transmit
9654
      if (data[15] !== 1)
9655
      begin
9656
        test_fail("Wrong buffer descriptor's ready bit read out from MAC");
9657
        fail = fail + 1;
9658
      end
9659
      wait (MTxEn === 1'b0); // end transmit
9660
      num_of_frames = num_of_frames + 1;
9661
      num_of_bd = num_of_bd + 1;
9662
      #Tp;
9663
      // destination address on ethernet PHY
9664
      eth_phy.set_tx_mem_addr(0);
9665
      i1 = 0;
9666
      i2 = 0;
9667
      // count IPG clock periods
9668
      fork
9669
        begin
9670
          wait (MTxEn === 1'b1); // start second transmit
9671
          #Tp;
9672
          disable count_rising1;
9673
          disable count_falling1;
9674
        end
9675
        begin: count_rising1
9676
          forever
9677
          begin
9678
            @(posedge mtx_clk);
9679
            i1 = i1 + 1;
9680
            #Tp;
9681
          end
9682
        end
9683
        begin: count_falling1
9684
          forever
9685
          begin
9686
            @(negedge mtx_clk);
9687
            i2 = i2 + 1;
9688
            #Tp;
9689
          end
9690
        end
9691
      join
9692
      // check IPG length - INTERMEDIATE DISPLAYS
9693
      if((i1 == i2) && (i1 >= (tmp_ipgt + 3)))
9694
      begin
9695
        $display("    ->IPG with %0d mtx_clk periods (min %0d) between packets with lengths %0d and %0d checked",
9696
                  i1, (tmp_ipgt + 3), (i_length + 4), (i_length + 4 + 1));
9697
      end
9698
      else
9699
      begin
9700
        `TIME; $display("*E IPG is not correct: (%0d + %0d) / 2, requested: %d", i1, i2, (tmp_ipgt + 3));
9701
        fail = fail + 1;
9702
        test_fail("IPG is not correct");
9703
      end
9704
      // wait for second transmit to end
9705
      wait (MTxEn === 1'b0); // end second transmit
9706
      while (data[15] === 1)
9707
      begin
9708
        check_tx_bd(num_of_bd, data);
9709
        @(posedge wb_clk);
9710
      end
9711
      repeat (1) @(posedge wb_clk);
9712
      // check length of a second PACKET
9713
      tmp_len = eth_phy.tx_len;
9714
      #1;
9715
      if (tmp_len != (i_length + 4 + 1))
9716
      begin
9717
        test_fail("Wrong length of second packet out from MAC");
9718
        fail = fail + 1;
9719
      end
9720
      // checking second packet
9721
      check_tx_packet(`MEMORY_BASE, 0, (i_length + 1), tmp);
9722
      if (tmp > 0)
9723
      begin
9724
        test_fail("Wrong data of second transmitted packet");
9725
        fail = fail + 1;
9726
      end
9727
      // check second transmited TX packet CRC
9728
      check_tx_crc(0, (i_length + 1), 1'b0, tmp); // length without CRC
9729
      if (tmp > 0)
9730
      begin
9731
        test_fail("Wrong CRC of second transmitted packet");
9732
        fail = fail + 1;
9733
      end
9734
      // check WB INT signal
9735
      if (wb_int !== 1'b1)
9736
      begin
9737
        `TIME; $display("*E WB INT signal should be set");
9738
        test_fail("WB INT signal should be set");
9739
        fail = fail + 1;
9740
      end
9741
      // check TX buffer descriptor of a packet
9742
      check_tx_bd(num_of_bd, data);
9743
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
9744
           ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
9745
      begin
9746
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
9747
        test_fail("TX buffer descriptor status is not correct");
9748
        fail = fail + 1;
9749
      end
9750
      // check interrupts
9751
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9752
      if ((data & `ETH_INT_TXB) !== 1'b1)
9753
      begin
9754
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
9755
        test_fail("Interrupt Transmit Buffer was not set");
9756
        fail = fail + 1;
9757
      end
9758
      if ((data & (~`ETH_INT_TXB)) !== 0)
9759
      begin
9760
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
9761
        test_fail("Other interrupts (except Transmit Buffer) were set");
9762
        fail = fail + 1;
9763
      end
9764
      // clear interrupts
9765
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9766
      // check WB INT signal
9767
      if (wb_int !== 1'b0)
9768
      begin
9769
        test_fail("WB INT signal should not be set");
9770
        fail = fail + 1;
9771
      end
9772
      // set length (LOOP variable)
9773
      if ((tmp_ipgt + 3) < 130) // tmp_ipgt < 124
9774
        i_length = i_length + 2;
9775
      else
9776
        i_length = (max_tmp - 4);
9777
      // set IPGT
9778
      if ((tmp_ipgt + 3) < 10)
9779
        tmp_ipgt = tmp_ipgt + 1;
9780
      else if ((tmp_ipgt + 3) < 24)
9781
        tmp_ipgt = tmp_ipgt + 7;
9782
      else if ((tmp_ipgt + 3) == 24)
9783
        tmp_ipgt = 38 - 3;
9784
      else if ((tmp_ipgt + 3) == 38)
9785
        tmp_ipgt = 72 - 3;
9786
      else if ((tmp_ipgt + 3) == 72)
9787
        tmp_ipgt = 130 - 3; // 124 - 3
9788
      // the number of frame transmitted
9789
      num_of_frames = num_of_frames + 1;
9790
      num_of_bd = 0;
9791
      @(posedge wb_clk);
9792
    end
9793
    // disable TX
9794
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
9795
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9796
    @(posedge wb_clk);
9797
    if(fail == 0)
9798
      test_ok;
9799
    else
9800
      fail = 0;
9801
  end
9802
 
9803
 
9804
  ////////////////////////////////////////////////////////////////////
9805
  ////                                                            ////
9806
  ////  Test transmit packets after TX under-run on each packet's ////
9807
  ////  byte at 2 TX buffer decriptors ( 10Mbps ).                ////
9808
  ////                                                            ////
9809
  ////////////////////////////////////////////////////////////////////
9810
  if (test_num == 20) // without padding
9811
  begin
9812
    // TEST 20: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 10Mbps )
9813
    test_name = "TEST 20: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 10Mbps )";
9814
    `TIME;
9815
    $display("  TEST 20: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 10Mbps )");
9816
 
9817
    // reset MAC registers
9818
    hard_reset;
9819
    // reset MAC and MII LOGIC with soft reset
9820
    reset_mac;
9821
    reset_mii;
9822
    // set wb slave response
9823
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
9824
 
9825
    max_tmp = 0;
9826
    min_tmp = 0;
9827
    // set 2 TX buffer descriptors - must be set before TX enable
9828
    wbm_write(`ETH_TX_BD_NUM, 32'h2, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9829
    // enable TX, set full-duplex mode, NO padding and CRC appending
9830
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN |/* `ETH_MODER_PAD |*/ `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9831
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9832
    // prepare a packet of MAXFL length
9833
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9834
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
9835
    min_tmp = tmp[31:16];
9836
    st_data = 8'h99;
9837
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
9838
    // read IPG value
9839
    wbm_read(`ETH_IPGT, tmp_ipgt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9840
    // check WB INT signal
9841
    if (wb_int !== 1'b0)
9842
    begin
9843
      test_fail("WB INT signal should not be set");
9844
      fail = fail + 1;
9845
    end
9846
 
9847
    // write to phy's control register for 10Mbps
9848
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
9849
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
9850
    speed = 10;
9851
 
9852 223 tadejm
    num_of_frames = 40; // (0..3) => start under-run on first word
9853 209 tadejm
    num_of_bd = 0;
9854
    i_data = 3; // (3) => one BYTE read in first word - FIRST byte
9855
    i_length = (min_tmp + 4);
9856
    while (i_length < (max_tmp - 4))
9857
    begin
9858
      // Reset_tx_bd enable interrupt generation
9859
      // unmask interrupts
9860
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
9861
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9862
      // not detect carrier sense in FD and no collision
9863
      eth_phy.carrier_sense_tx_fd_detect(0);
9864
      eth_phy.collision(0);
9865
      // first destination address on ethernet PHY
9866
      eth_phy.set_tx_mem_addr(0);
9867
      // prepare BDs
9868
      if (num_of_bd == 0)
9869
      begin
9870
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_data[1:0]));
9871
        set_tx_bd(1, 1, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9872
        set_tx_bd_wrap(1);
9873
        // set wb slave response: ACK (response), wbs_waits[2:0] (waits before response), 
9874
        //                       wbs_retries[7:0] (RTYs before ACK if RTY response selected)
9875 223 tadejm
        #1 wb_slave.cycle_response(`ACK_RESPONSE, 3'h0, 8'h0);
9876 209 tadejm
        set_tx_bd_ready(1, 1);
9877
        set_tx_bd_ready(0, 0);
9878
      end
9879
      // frame under-run checking
9880
      frame_started = 0;
9881
      frame_ended = 0;
9882
      wait_for_frame = 0;
9883
      fork
9884
        begin
9885
          // for every 4 frames bytes 1, 2, 3 and 4 respectively are read in first word => 1 ACK
9886
          // in other words 4 bytes are read, since length is MINFL => num_of_frames[31:2] ACKs
9887
          repeat ((num_of_frames[31:2] + 1'b1)) @(posedge eth_ma_wb_ack_i);
9888
          @(negedge eth_ma_wb_ack_i); // wait for last ACK to finish
9889
          // set wb slave response: ACK (response), wbs_waits[2:0] (waits before response), 
9890
          //                       wbs_retries[7:0] (RTYs before ACK if RTY response selected)
9891 223 tadejm
          #1 wb_slave.cycle_response(`NO_RESPONSE, 3'h0, 8'hFF);
9892 209 tadejm
          // wait for synchronization and some additional clocks
9893
          wait_for_frame = 1;
9894
          // wait for frame
9895
          wait ((wait_for_frame == 0) || (frame_started == 1))
9896
          if ((wait_for_frame == 0) && (frame_started == 0)) // frame didn't start
9897
          begin
9898
            disable check_fr;
9899
          end
9900
          else if ((wait_for_frame == 1) && (frame_started == 1)) // frame started
9901
          begin
9902
            disable wait_fr;
9903
            wait (frame_ended == 1);
9904
          end
9905
          repeat (2) @(posedge wb_clk);
9906
          // set wb slave response: ACK (response), wbs_waits[2:0] (waits before response), 
9907
          //                       wbs_retries[7:0] (RTYs before ACK if RTY response selected)
9908 223 tadejm
          wb_slave.cycle_response(`ACK_RESPONSE, 3'h0, 8'h0);
9909 209 tadejm
        end
9910
        begin: wait_fr
9911
          wait (wait_for_frame == 1)
9912
          begin
9913
            // wait for synchronization and some additional clocks
9914
            repeat (3) @(posedge wb_clk);
9915
            repeat (2 * tmp_ipgt) @(posedge mtx_clk);
9916
            repeat (2) @(posedge wb_clk);
9917
            repeat (2) @(posedge mtx_clk);
9918
            wait_for_frame = 0;
9919
          end
9920
        end
9921
        begin: check_fr
9922
          // wait for frame to start
9923
          @(posedge MTxEn);
9924
          frame_started = 1;
9925 223 tadejm
`TIME; $display("  Under-run (on %0d. byte) frame started", (num_of_frames + 1));
9926 209 tadejm
          // wait for frame to end due to under-run
9927
          @(negedge MTxEn);
9928
          frame_ended = 1;
9929 223 tadejm
`TIME; $display("  Under-run frame ended");
9930 209 tadejm
        end
9931
      join
9932
      // wait for first transmit to end, if under-run didn't happen
9933
      if (frame_ended == 0)
9934
      begin
9935
        // WAIT FOR FIRST TRANSMIT
9936
        check_tx_bd(num_of_bd, data);
9937
        wait (MTxEn === 1'b1); // start first transmit
9938
        if (data[15] !== 1)
9939
        begin
9940
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
9941
          fail = fail + 1;
9942
        end
9943
        wait (MTxEn === 1'b0); // end first transmit
9944
        while (data[15] === 1)
9945
        begin
9946
          check_tx_bd(num_of_bd, data);
9947
          @(posedge wb_clk);
9948
        end
9949
        repeat (1) @(posedge wb_clk);
9950
        // CHECK FIRST FRAME
9951
        // check length of a first PACKET
9952
        tmp_len = eth_phy.tx_len;
9953
        #1;
9954
        if (tmp_len != (i_length + 4))
9955
        begin
9956 223 tadejm
          `TIME; $display("*E Wrong length of first packet out from MAC");
9957
          test_fail("Wrong length of first packet out from MAC");
9958 209 tadejm
          fail = fail + 1;
9959
        end
9960
        // checking first packet
9961
        check_tx_packet((`MEMORY_BASE + i_data[1:0]), 0, (i_length), tmp);
9962
        if (tmp > 0)
9963
        begin
9964 223 tadejm
          `TIME; $display("*E Wrong data of first transmitted packet");
9965
          test_fail("Wrong data of first transmitted packet");
9966 209 tadejm
          fail = fail + 1;
9967
        end
9968
        // check first transmited TX packet CRC
9969
        check_tx_crc(0, (i_length), 1'b0, tmp); // length without CRC
9970
        if (tmp > 0)
9971
        begin
9972 223 tadejm
          `TIME; $display("*E Wrong CRC of first transmitted packet");
9973
          test_fail("Wrong CRC of first transmitted packet");
9974 209 tadejm
          fail = fail + 1;
9975
        end
9976
        // check WB INT signal
9977
        if (wb_int !== 1'b1)
9978
        begin
9979
          `TIME; $display("*E WB INT signal should be set");
9980
          test_fail("WB INT signal should be set");
9981
          fail = fail + 1;
9982
        end
9983
        // check TX buffer descriptor of a packet
9984
        check_tx_bd(num_of_bd, data);
9985
        if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
9986
             ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
9987
        begin
9988
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
9989
          test_fail("TX buffer descriptor status is not correct");
9990
          fail = fail + 1;
9991
        end
9992
        // check interrupts
9993
        wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9994
        if ((data & `ETH_INT_TXB) !== 1'b1)
9995
        begin
9996
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
9997
          test_fail("Interrupt Transmit Buffer was not set");
9998
          fail = fail + 1;
9999
        end
10000
        if ((data & (~`ETH_INT_TXB)) !== 0)
10001
        begin
10002
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
10003
          test_fail("Other interrupts (except Transmit Buffer) were set");
10004
          fail = fail + 1;
10005
        end
10006
        // clear interrupts
10007
        wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10008
        // check WB INT signal
10009
        if (wb_int !== 1'b0)
10010
        begin
10011
          test_fail("WB INT signal should not be set");
10012
          fail = fail + 1;
10013
        end
10014
      end
10015 223 tadejm
      else
10016
      begin
10017
        // CHECK FIRST FRAME
10018
        // check length of a first PACKET
10019
        tmp_len = eth_phy.tx_len_err;
10020
        #1;
10021
        if (tmp_len != (num_of_frames + (4 - i_data)))
10022
        begin
10023
          `TIME; $display("*E Wrong length of first packet out from MAC");
10024
          test_fail("Wrong length of first packet out from MAC");
10025
          fail = fail + 1;
10026
        end
10027
        // checking first packet
10028
        check_tx_packet((`MEMORY_BASE + i_data[1:0]), 0, (num_of_frames), tmp);
10029
        if (tmp > 0)
10030
        begin
10031
          `TIME; $display("*E Wrong data of first transmitted packet");
10032
          test_fail("Wrong data of first transmitted packet");
10033
          fail = fail + 1;
10034
        end
10035
        // check WB INT signal
10036
        if (wb_int !== 1'b1)
10037
        begin
10038
          `TIME; $display("*E WB INT signal should be set");
10039
          test_fail("WB INT signal should be set");
10040
          fail = fail + 1;
10041
        end
10042
        // check TX buffer descriptor of a packet
10043
        check_tx_bd(num_of_bd, data);
10044
        if ( ((data[15:0] !== 16'h7900) && (num_of_bd == 1)) || // under-run, wrap bit
10045
             ((data[15:0] !== 16'h5900) && (num_of_bd < 1)) )   // under-run, without wrap bit
10046
        begin
10047
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
10048
          test_fail("TX buffer descriptor status is not correct");
10049
          fail = fail + 1;
10050
        end
10051
        // check interrupts
10052
        wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10053
        if ((data & `ETH_INT_TXE) !== 2'b10)
10054
        begin
10055
          `TIME; $display("*E Interrupt Transmit Error was not set, interrupt reg: %0h", data);
10056
          test_fail("Interrupt Transmit Buffer was not set");
10057
          fail = fail + 1;
10058
        end
10059
        if ((data & (~`ETH_INT_TXE)) !== 0)
10060
        begin
10061
          `TIME; $display("*E Other interrupts (except Transmit Error) were set, interrupt reg: %0h", data);
10062
          test_fail("Other interrupts (except Transmit Buffer) were set");
10063
          fail = fail + 1;
10064
        end
10065
        // clear interrupts
10066
        wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10067
        // check WB INT signal
10068
        if (wb_int !== 1'b0)
10069
        begin
10070
          test_fail("WB INT signal should not be set");
10071
          fail = fail + 1;
10072
        end
10073
      end
10074 209 tadejm
      num_of_bd = num_of_bd + 1;
10075
      // destination address on ethernet PHY
10076
      eth_phy.set_tx_mem_addr(0);
10077 223 tadejm
      // WAIT FOR SECOND TRANSMIT
10078 209 tadejm
      check_tx_bd(num_of_bd, data);
10079
      wait (MTxEn === 1'b1); // start first transmit
10080
      if (data[15] !== 1)
10081
      begin
10082
        test_fail("Wrong buffer descriptor's ready bit read out from MAC");
10083
        fail = fail + 1;
10084
      end
10085
      wait (MTxEn === 1'b0); // end first transmit
10086
      while (data[15] === 1)
10087
      begin
10088
        check_tx_bd(num_of_bd, data);
10089
        @(posedge wb_clk);
10090
      end
10091
      repeat (1) @(posedge wb_clk);
10092
      // CHECK SECOND FRAME
10093
      // check length of a second PACKET
10094 223 tadejm
if (frame_ended == 1'b1)
10095
begin
10096
`TIME; $display("  Second frame after under-run ended");
10097
end
10098 209 tadejm
      tmp_len = eth_phy.tx_len;
10099
      #1;
10100
      if (tmp_len != (i_length + 4))
10101
      begin
10102 223 tadejm
        `TIME; $display("*E Wrong length of second packet out from MAC");
10103 209 tadejm
        test_fail("Wrong length of second packet out from MAC");
10104
        fail = fail + 1;
10105
      end
10106
      // checking second packet
10107
      check_tx_packet(`MEMORY_BASE, 0, (i_length), tmp);
10108
      if (tmp > 0)
10109
      begin
10110 223 tadejm
        `TIME; $display("*E Wrong data of second transmitted packet");
10111 209 tadejm
        test_fail("Wrong data of second transmitted packet");
10112
        fail = fail + 1;
10113
      end
10114
      // check second transmited TX packet CRC
10115
      check_tx_crc(0, (i_length), 1'b0, tmp); // length without CRC
10116
      if (tmp > 0)
10117
      begin
10118 223 tadejm
        `TIME; $display("*E Wrong CRC of second transmitted packet");
10119 209 tadejm
        test_fail("Wrong CRC of second transmitted packet");
10120
        fail = fail + 1;
10121
      end
10122
      // check WB INT signal
10123
      if (wb_int !== 1'b1)
10124
      begin
10125
        `TIME; $display("*E WB INT signal should be set");
10126
        test_fail("WB INT signal should be set");
10127
        fail = fail + 1;
10128
      end
10129
      // check TX buffer descriptor of a packet
10130
      check_tx_bd(num_of_bd, data);
10131
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
10132
           ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
10133
      begin
10134
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
10135
        test_fail("TX buffer descriptor status is not correct");
10136
        fail = fail + 1;
10137
      end
10138
      // check interrupts
10139
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10140
      if ((data & `ETH_INT_TXB) !== 1'b1)
10141
      begin
10142
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
10143
        test_fail("Interrupt Transmit Buffer was not set");
10144
        fail = fail + 1;
10145
      end
10146
      if ((data & (~`ETH_INT_TXB)) !== 0)
10147
      begin
10148
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
10149
        test_fail("Other interrupts (except Transmit Buffer) were set");
10150
        fail = fail + 1;
10151
      end
10152
      // clear interrupts
10153
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10154
      // check WB INT signal
10155
      if (wb_int !== 1'b0)
10156
      begin
10157
        test_fail("WB INT signal should not be set");
10158
        fail = fail + 1;
10159
      end
10160
      // set initial value
10161
      i_data = i_data - 1;
10162
      // the number of frame transmitted
10163
      num_of_frames = num_of_frames + 1;
10164
      num_of_bd = 0;
10165
      // set length (LOOP variable)
10166 223 tadejm
      if (num_of_frames == i_length + 4) // 64 => this was last Byte (1st .. 64th) when i_length = min_tmp - 4
10167 209 tadejm
        i_length = (max_tmp - 4);
10168
      @(posedge wb_clk);
10169
    end
10170
    // disable TX
10171
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
10172
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10173
    @(posedge wb_clk);
10174
    if(fail == 0)
10175
      test_ok;
10176
    else
10177
      fail = 0;
10178
  end
10179
 
10180
 
10181
  ////////////////////////////////////////////////////////////////////
10182
  ////                                                            ////
10183
  ////  Test transmit packets after TX under-run on each packet's ////
10184
  ////  byte at 2 TX buffer decriptors ( 100Mbps ).               ////
10185
  ////                                                            ////
10186
  ////////////////////////////////////////////////////////////////////
10187
  if (test_num == 21) // without padding
10188
  begin
10189
    // TEST 21: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 100Mbps )
10190
    test_name = "TEST 21: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 100Mbps )";
10191
    `TIME;
10192
    $display("  TEST 21: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 100Mbps )");
10193
 
10194
    // reset MAC registers
10195
    hard_reset;
10196
    // reset MAC and MII LOGIC with soft reset
10197
    reset_mac;
10198
    reset_mii;
10199
    // set wb slave response
10200
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
10201
 
10202
    max_tmp = 0;
10203
    min_tmp = 0;
10204
    // set 2 TX buffer descriptors - must be set before TX enable
10205
    wbm_write(`ETH_TX_BD_NUM, 32'h2, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10206
    // enable TX, set full-duplex mode, NO padding and CRC appending
10207
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN |/* `ETH_MODER_PAD |*/ `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
10208
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10209
    // prepare a packet of MAXFL length
10210
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10211
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
10212
    min_tmp = tmp[31:16];
10213
    st_data = 8'h99;
10214
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
10215
    // read IPG value
10216
    wbm_read(`ETH_IPGT, tmp_ipgt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10217
    // check WB INT signal
10218
    if (wb_int !== 1'b0)
10219
    begin
10220
      test_fail("WB INT signal should not be set");
10221
      fail = fail + 1;
10222
    end
10223
 
10224
    // write to phy's control register for 100Mbps
10225
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
10226
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
10227
    speed = 100;
10228
 
10229
    num_of_frames = 0; // (0..3) => start under-run on first word
10230
    num_of_bd = 0;
10231
    i_data = 3; // (3) => one BYTE read in first word - FIRST byte
10232
    i_length = (min_tmp + 4);
10233
    while (i_length < (max_tmp - 4))
10234
    begin
10235
      // Reset_tx_bd enable interrupt generation
10236
      // unmask interrupts
10237
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
10238
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10239
      // not detect carrier sense in FD and no collision
10240
      eth_phy.carrier_sense_tx_fd_detect(0);
10241
      eth_phy.collision(0);
10242
      // first destination address on ethernet PHY
10243
      eth_phy.set_tx_mem_addr(0);
10244
      // prepare BDs
10245
      if (num_of_bd == 0)
10246
      begin
10247
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_data[1:0]));
10248
        set_tx_bd(1, 1, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
10249
        set_tx_bd_wrap(1);
10250
        // set wb slave response: ACK (response), wbs_waits[2:0] (waits before response), 
10251
        //                       wbs_retries[7:0] (RTYs before ACK if RTY response selected)
10252
        #1 wb_slave.cycle_response(`ACK_RESPONSE, 3'h2, 8'h0);
10253
        set_tx_bd_ready(1, 1);
10254
        set_tx_bd_ready(0, 0);
10255
      end
10256
      // frame under-run checking
10257
      frame_started = 0;
10258
      frame_ended = 0;
10259
      wait_for_frame = 0;
10260
      fork
10261
        begin
10262
          // for every 4 frames bytes 1, 2, 3 and 4 respectively are read in first word => 1 ACK
10263
          // in other words 4 bytes are read, since length is MINFL => num_of_frames[31:2] ACKs
10264
          repeat ((num_of_frames[31:2] + 1'b1)) @(posedge eth_ma_wb_ack_i);
10265
          @(negedge eth_ma_wb_ack_i); // wait for last ACK to finish
10266
          // set wb slave response: ACK (response), wbs_waits[2:0] (waits before response), 
10267
          //                       wbs_retries[7:0] (RTYs before ACK if RTY response selected)
10268
          #1 wb_slave.cycle_response(`NO_RESPONSE, 3'h7, 8'hFF);
10269
          // wait for synchronization and some additional clocks
10270
          wait_for_frame = 1;
10271
          // wait for frame
10272
          wait ((wait_for_frame == 0) || (frame_started == 1))
10273
          if ((wait_for_frame == 0) && (frame_started == 0)) // frame didn't start
10274
          begin
10275
            disable check_fr1;
10276
          end
10277
          else if ((wait_for_frame == 1) && (frame_started == 1)) // frame started
10278
          begin
10279
            disable wait_fr1;
10280
            wait (frame_ended == 1);
10281
          end
10282
          repeat (2) @(posedge wb_clk);
10283
          // set wb slave response: ACK (response), wbs_waits[2:0] (waits before response), 
10284
          //                       wbs_retries[7:0] (RTYs before ACK if RTY response selected)
10285
          wb_slave.cycle_response(`ACK_RESPONSE, 3'h2, 8'h0);
10286
        end
10287
        begin: wait_fr1
10288
          wait (wait_for_frame == 1)
10289
          begin
10290
            // wait for synchronization and some additional clocks
10291
            repeat (3) @(posedge wb_clk);
10292
            repeat (2 * tmp_ipgt) @(posedge mtx_clk);
10293
            repeat (2) @(posedge wb_clk);
10294
            repeat (2) @(posedge mtx_clk);
10295
            wait_for_frame = 0;
10296
          end
10297
        end
10298
        begin: check_fr1
10299
          // wait for frame to start
10300
          @(posedge MTxEn);
10301
          frame_started = 1;
10302
$display("  Under-run (on %0d. byte) frame started", (num_of_frames + 1));
10303
          // wait for frame to end due to under-run
10304
          @(negedge MTxEn);
10305
          frame_ended = 1;
10306
$display("  Under-run frame ended");
10307
        end
10308
      join
10309
      // wait for first transmit to end, if under-run didn't happen
10310
      if (frame_ended == 0)
10311
      begin
10312
        // WAIT FOR FIRST TRANSMIT
10313
        check_tx_bd(num_of_bd, data);
10314
        wait (MTxEn === 1'b1); // start first transmit
10315
        if (data[15] !== 1)
10316
        begin
10317
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
10318
          fail = fail + 1;
10319
        end
10320
        wait (MTxEn === 1'b0); // end first transmit
10321
        while (data[15] === 1)
10322
        begin
10323
          check_tx_bd(num_of_bd, data);
10324
          @(posedge wb_clk);
10325
        end
10326
        repeat (1) @(posedge wb_clk);
10327
        // CHECK FIRST FRAME
10328
        // check length of a first PACKET
10329
        tmp_len = eth_phy.tx_len;
10330
        #1;
10331
        if (tmp_len != (i_length + 4))
10332
        begin
10333
          test_fail("Wrong length of second packet out from MAC");
10334
          fail = fail + 1;
10335
        end
10336
        // checking first packet
10337
        check_tx_packet((`MEMORY_BASE + i_data[1:0]), 0, (i_length), tmp);
10338
        if (tmp > 0)
10339
        begin
10340
          test_fail("Wrong data of second transmitted packet");
10341
          fail = fail + 1;
10342
        end
10343
        // check first transmited TX packet CRC
10344
        check_tx_crc(0, (i_length), 1'b0, tmp); // length without CRC
10345
        if (tmp > 0)
10346
        begin
10347
          test_fail("Wrong CRC of second transmitted packet");
10348
          fail = fail + 1;
10349
        end
10350
        // check WB INT signal
10351
        if (wb_int !== 1'b1)
10352
        begin
10353
          `TIME; $display("*E WB INT signal should be set");
10354
          test_fail("WB INT signal should be set");
10355
          fail = fail + 1;
10356
        end
10357
        // check TX buffer descriptor of a packet
10358
        check_tx_bd(num_of_bd, data);
10359
        if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
10360
             ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
10361
        begin
10362
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
10363
          test_fail("TX buffer descriptor status is not correct");
10364
          fail = fail + 1;
10365
        end
10366
        // check interrupts
10367
        wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10368
        if ((data & `ETH_INT_TXB) !== 1'b1)
10369
        begin
10370
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
10371
          test_fail("Interrupt Transmit Buffer was not set");
10372
          fail = fail + 1;
10373
        end
10374
        if ((data & (~`ETH_INT_TXB)) !== 0)
10375
        begin
10376
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
10377
          test_fail("Other interrupts (except Transmit Buffer) were set");
10378
          fail = fail + 1;
10379
        end
10380
        // clear interrupts
10381
        wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10382
        // check WB INT signal
10383
        if (wb_int !== 1'b0)
10384
        begin
10385
          test_fail("WB INT signal should not be set");
10386
          fail = fail + 1;
10387
        end
10388
      end
10389
      num_of_bd = num_of_bd + 1;
10390
      // destination address on ethernet PHY
10391
      eth_phy.set_tx_mem_addr(0);
10392
      // WAIT FOR FIRST TRANSMIT
10393
      check_tx_bd(num_of_bd, data);
10394
      wait (MTxEn === 1'b1); // start first transmit
10395
      if (data[15] !== 1)
10396
      begin
10397
        test_fail("Wrong buffer descriptor's ready bit read out from MAC");
10398
        fail = fail + 1;
10399
      end
10400
      wait (MTxEn === 1'b0); // end first transmit
10401
      while (data[15] === 1)
10402
      begin
10403
        check_tx_bd(num_of_bd, data);
10404
        @(posedge wb_clk);
10405
      end
10406
      repeat (1) @(posedge wb_clk);
10407
      // CHECK SECOND FRAME
10408
      // check length of a second PACKET
10409
      tmp_len = eth_phy.tx_len;
10410
      #1;
10411
      if (tmp_len != (i_length + 4))
10412
      begin
10413
        test_fail("Wrong length of second packet out from MAC");
10414
        fail = fail + 1;
10415
      end
10416
      // checking second packet
10417
      check_tx_packet(`MEMORY_BASE, 0, (i_length), tmp);
10418
      if (tmp > 0)
10419
      begin
10420
        test_fail("Wrong data of second transmitted packet");
10421
        fail = fail + 1;
10422
      end
10423
      // check second transmited TX packet CRC
10424
      check_tx_crc(0, (i_length), 1'b0, tmp); // length without CRC
10425
      if (tmp > 0)
10426
      begin
10427
        test_fail("Wrong CRC of second transmitted packet");
10428
        fail = fail + 1;
10429
      end
10430
      // check WB INT signal
10431
      if (wb_int !== 1'b1)
10432
      begin
10433
        `TIME; $display("*E WB INT signal should be set");
10434
        test_fail("WB INT signal should be set");
10435
        fail = fail + 1;
10436
      end
10437
      // check TX buffer descriptor of a packet
10438
      check_tx_bd(num_of_bd, data);
10439
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
10440
           ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
10441
      begin
10442
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
10443
        test_fail("TX buffer descriptor status is not correct");
10444
        fail = fail + 1;
10445
      end
10446
      // check interrupts
10447
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10448
      if ((data & `ETH_INT_TXB) !== 1'b1)
10449
      begin
10450
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
10451
        test_fail("Interrupt Transmit Buffer was not set");
10452
        fail = fail + 1;
10453
      end
10454
      if ((data & (~`ETH_INT_TXB)) !== 0)
10455
      begin
10456
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
10457
        test_fail("Other interrupts (except Transmit Buffer) were set");
10458
        fail = fail + 1;
10459
      end
10460
      // clear interrupts
10461
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10462
      // check WB INT signal
10463
      if (wb_int !== 1'b0)
10464
      begin
10465
        test_fail("WB INT signal should not be set");
10466
        fail = fail + 1;
10467
      end
10468
      // set initial value
10469
      i_data = i_data - 1;
10470
      // the number of frame transmitted
10471
      num_of_frames = num_of_frames + 1;
10472
      num_of_bd = 0;
10473
      // set length (LOOP variable)
10474
      if (num_of_frames == i_length + 4) // 64 => this vas last Byte (1st .. 64th) when i_length = min_tmp - 4
10475
        i_length = (max_tmp - 4);
10476
      @(posedge wb_clk);
10477
    end
10478
    // disable TX
10479
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
10480
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10481
    @(posedge wb_clk);
10482
    if(fail == 0)
10483
      test_ok;
10484
    else
10485
      fail = 0;
10486
  end
10487
 
10488 194 tadej
end   //  for (test_num=start_task; test_num <= end_task; test_num=test_num+1)
10489 169 mohor
 
10490
end
10491
endtask // test_mac_full_duplex_transmit
10492
 
10493
 
10494 209 tadejm
task test_mac_full_duplex_receive;
10495
  input  [31:0]  start_task;
10496
  input  [31:0]  end_task;
10497
  integer        bit_start_1;
10498
  integer        bit_end_1;
10499
  integer        bit_start_2;
10500
  integer        bit_end_2;
10501
  integer        num_of_reg;
10502
  integer        num_of_frames;
10503
  integer        num_of_bd;
10504
  integer        i_addr;
10505
  integer        i_data;
10506
  integer        i_length;
10507
  integer        tmp_len;
10508
  integer        tmp_bd;
10509
  integer        tmp_bd_num;
10510
  integer        tmp_data;
10511
  integer        tmp_ipgt;
10512
  integer        test_num;
10513
  reg    [31:0]  tx_bd_num;
10514
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_data;
10515
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_tmp_data;
10516
  integer        i;
10517
  integer        i1;
10518
  integer        i2;
10519
  integer        i3;
10520
  integer        fail;
10521
  integer        speed;
10522
  reg            frame_started;
10523
  reg            frame_ended;
10524
  reg            wait_for_frame;
10525 243 tadejm
  reg            check_frame;
10526
  reg            stop_checking_frame;
10527
  reg            first_fr_received;
10528 209 tadejm
  reg    [31:0]  addr;
10529
  reg    [31:0]  data;
10530
  reg    [31:0]  tmp;
10531
  reg    [ 7:0]  st_data;
10532
  reg    [15:0]  max_tmp;
10533
  reg    [15:0]  min_tmp;
10534
begin
10535
// MAC FULL DUPLEX RECEIVE TEST
10536
test_heading("MAC FULL DUPLEX RECEIVE TEST");
10537
$display(" ");
10538
$display("MAC FULL DUPLEX RECEIVE TEST");
10539
fail = 0;
10540
 
10541
// reset MAC registers
10542
hard_reset;
10543
// reset MAC and MII LOGIC with soft reset
10544
reset_mac;
10545
reset_mii;
10546
// set wb slave response
10547
wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
10548
 
10549
  /*
10550
  TASKS for set and control TX buffer descriptors (also send packet - set_tx_bd_ready):
10551
  -------------------------------------------------------------------------------------
10552
  set_tx_bd
10553
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0], len[15:0], irq, pad, crc, txpnt[31:0]);
10554
  set_tx_bd_wrap
10555
    (tx_bd_num_end[6:0]);
10556
  set_tx_bd_ready
10557
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
10558
  check_tx_bd
10559
    (tx_bd_num_start[6:0], tx_bd_status[31:0]);
10560
  clear_tx_bd
10561
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
10562
 
10563
  TASKS for set and control RX buffer descriptors:
10564
  ------------------------------------------------
10565
  set_rx_bd
10566
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0], irq, rxpnt[31:0]);
10567
  set_rx_bd_wrap
10568
    (rx_bd_num_end[6:0]);
10569
  set_rx_bd_empty
10570
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
10571
  check_rx_bd
10572
    (rx_bd_num_end[6:0], rx_bd_status);
10573
  clear_rx_bd
10574
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
10575
 
10576
  TASKS for set and check TX packets:
10577
  -----------------------------------
10578
  set_tx_packet
10579
    (txpnt[31:0], len[15:0], eth_start_data[7:0]);
10580
  check_tx_packet
10581
    (txpnt_wb[31:0], txpnt_phy[31:0], len[15:0], failure[31:0]);
10582
 
10583
  TASKS for set and check RX packets:
10584
  -----------------------------------
10585
  set_rx_packet
10586
    (rxpnt[31:0], len[15:0], plus_nibble, d_addr[47:0], s_addr[47:0], type_len[15:0], start_data[7:0]);
10587
  check_rx_packet
10588
    (rxpnt_phy[31:0], rxpnt_wb[31:0], len[15:0], plus_nibble, successful_nibble, failure[31:0]);
10589
 
10590
  TASKS for append and check CRC to/of TX packet:
10591
  -----------------------------------------------
10592
  append_tx_crc
10593
    (txpnt_wb[31:0], len[15:0], negated_crc);
10594
  check_tx_crc
10595
    (txpnt_phy[31:0], len[15:0], negated_crc, failure[31:0]);
10596
 
10597
  TASK for append CRC to RX packet (CRC is checked together with check_rx_packet):
10598
  --------------------------------------------------------------------------------
10599
  append_rx_crc
10600
    (rxpnt_phy[31:0], len[15:0], plus_nibble, negated_crc);
10601
  */
10602
 
10603
//////////////////////////////////////////////////////////////////////
10604
////                                                              ////
10605
////  test_mac_full_duplex_receive:                               ////
10606
////                                                              ////
10607
////  0: Test no receive when all buffers are TX ( 10Mbps ).      ////
10608
////  1: Test no receive when all buffers are TX ( 100Mbps ).     ////
10609 243 tadejm
////  2: Test receive packet synchronization with receive         ////
10610
////     disable/enable ( 10Mbps ).                               ////
10611
////  3: Test receive packet synchronization with receive         ////
10612
////     disable/enable ( 100Mbps ).                              ////
10613
////  4: Test receive packets form MINFL to MAXFL sizes at        ////
10614 209 tadejm
////     one RX buffer decriptor ( 10Mbps ).                      ////
10615 243 tadejm
////  5: Test receive packets form MINFL to MAXFL sizes at        ////
10616 209 tadejm
////     one RX buffer decriptor ( 100Mbps ).                     ////
10617
////                                                              ////
10618
//////////////////////////////////////////////////////////////////////
10619
for (test_num = start_task; test_num <= end_task; test_num = test_num + 1)
10620
begin
10621
 
10622
  ////////////////////////////////////////////////////////////////////
10623
  ////                                                            ////
10624
  ////  Test no receive when all buffers are TX ( 10Mbps ).       ////
10625
  ////                                                            ////
10626
  ////////////////////////////////////////////////////////////////////
10627
  if (test_num == 0) // Test no receive when all buffers are TX ( 10Mbps ).
10628
  begin
10629
    // TEST 0: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 10Mbps )
10630
    test_name   = "TEST 0: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 10Mbps )";
10631
    `TIME; $display("  TEST 0: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 10Mbps )");
10632
 
10633
    // unmask interrupts
10634
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
10635
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10636
    // set all buffer descriptors to TX - must be set before RX enable
10637
    wbm_write(`ETH_TX_BD_NUM, 32'h80, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10638 223 tadejm
    // enable RX, set full-duplex mode, receive small, NO correct IFG
10639 209 tadejm
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
10640
              `ETH_MODER_PRO | `ETH_MODER_BRO,
10641
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10642
 
10643
    // write to phy's control register for 10Mbps
10644
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
10645
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
10646
    speed = 10;
10647
 
10648
    i = 0;
10649
    while (i < 128)
10650
    begin
10651
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
10652
      begin
10653
        set_rx_packet((i1 * 50), 10, 1'b0, 48'h1234_5678_8765, 48'h0011_2233_4455, 16'h0101, 8'h0);
10654
        append_rx_crc((i1 * 50), 10, 1'b0, 1'b0);
10655
        set_rx_bd(i1, i1, 1'b1, (`MEMORY_BASE + (i1 * 50)));
10656
      end
10657
      set_rx_bd_wrap(i);
10658
      set_rx_bd_empty(0, i);
10659
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
10660
      begin
10661
        #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, (i1 * 50), 14, 1'b0);
10662
        repeat(10) @(posedge mrx_clk);
10663
      end
10664
      @(posedge mrx_clk);
10665
      for (i2 = 0; i2 < 20; i2 = i2 + 1)
10666
      begin
10667
        check_rx_bd(0, tmp);
10668
        #1;
10669
        if (tmp[15] === 1'b0)
10670
        begin
10671
          test_fail("Receive should not start at all");
10672
          fail = fail + 1;
10673
          `TIME; $display("*E Receive of %d packets should not start at all - empty is 0", i);
10674
        end
10675
        if (tmp[7:0] !== 0)
10676
        begin
10677
          test_fail("Receive should not be finished since it should not start at all");
10678
          fail = fail + 1;
10679
          `TIME; $display("*E Receive of should not be finished since it should not start at all");
10680
        end
10681
        @(posedge wb_clk);
10682
      end
10683
      wbm_read(`ETH_INT, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10684
      if (tmp[6:0] !== 0)
10685
      begin
10686
        test_fail("Receive should not get INT since it should not start at all");
10687
        fail = fail + 1;
10688
        `TIME; $display("*E Receive of should not get INT since it should not start at all");
10689
      end
10690
      clear_rx_bd(0, i);
10691
      if ((i < 5) || (i > 124))
10692
        i = i + 1;
10693
      else
10694
        i = i + 120;
10695
    end
10696
    // disable RX
10697
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
10698
              `ETH_MODER_PRO | `ETH_MODER_BRO,
10699
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10700
    if(fail == 0)
10701
      test_ok;
10702
    else
10703
      fail = 0;
10704
  end
10705
 
10706
 
10707
  ////////////////////////////////////////////////////////////////////
10708
  ////                                                            ////
10709
  ////  Test no receive when all buffers are TX ( 100Mbps ).      ////
10710
  ////                                                            ////
10711
  ////////////////////////////////////////////////////////////////////
10712
  if (test_num == 1) // Test no receive when all buffers are TX ( 100Mbps ).
10713
  begin
10714
    // TEST 1: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 100Mbps )
10715
    test_name   = "TEST 1: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 100Mbps )";
10716
    `TIME; $display("  TEST 1: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 100Mbps )");
10717
 
10718
    // unmask interrupts
10719
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
10720
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10721
    // set all buffer descriptors to TX - must be set before RX enable
10722
    wbm_write(`ETH_TX_BD_NUM, 32'h80, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10723 223 tadejm
    // enable RX, set full-duplex mode, receive small, NO correct IFG
10724 209 tadejm
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
10725
              `ETH_MODER_PRO | `ETH_MODER_BRO,
10726
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10727
 
10728
    // write to phy's control register for 100Mbps
10729
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
10730
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
10731
    speed = 100;
10732
 
10733
    i = 0;
10734
    while (i < 128)
10735
    begin
10736
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
10737
      begin
10738
        set_rx_packet((i1 * 50), 10, 1'b0, 48'h1234_5678_8765, 48'h0011_2233_4455, 16'h0101, 8'h0);
10739
        append_rx_crc((i1 * 50), 10, 1'b0, 1'b0);
10740
        set_rx_bd(i1, i1, 1'b1, (`MEMORY_BASE + (i1 * 50)));
10741
      end
10742
      set_rx_bd_wrap(i);
10743
      set_rx_bd_empty(0, i);
10744
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
10745
      begin
10746
        #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, (i1 * 50), 14, 1'b0);
10747
        repeat(10) @(posedge mrx_clk);
10748
      end
10749
      @(posedge mrx_clk);
10750
      for (i2 = 0; i2 < 20; i2 = i2 + 1)
10751
      begin
10752
        check_rx_bd(0, tmp);
10753
        #1;
10754
        if (tmp[15] === 1'b0)
10755
        begin
10756
          test_fail("Receive should not start at all");
10757
          fail = fail + 1;
10758
          `TIME; $display("*E Receive of %d packets should not start at all - empty is 0", i);
10759
        end
10760
        if (tmp[7:0] !== 0)
10761
        begin
10762
          test_fail("Receive should not be finished since it should not start at all");
10763
          fail = fail + 1;
10764
          `TIME; $display("*E Receive of should not be finished since it should not start at all");
10765
        end
10766
        @(posedge wb_clk);
10767
      end
10768
      wbm_read(`ETH_INT, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10769
      if (tmp[6:0] !== 0)
10770
      begin
10771
        test_fail("Receive should not get INT since it should not start at all");
10772
        fail = fail + 1;
10773
        `TIME; $display("*E Receive of should not get INT since it should not start at all");
10774
      end
10775
      clear_rx_bd(0, i);
10776
      if ((i < 5) || (i > 124))
10777
        i = i + 1;
10778
      else
10779
        i = i + 120;
10780
    end
10781
    // disable RX
10782
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
10783
              `ETH_MODER_PRO | `ETH_MODER_BRO,
10784
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10785
    if(fail == 0)
10786
      test_ok;
10787
    else
10788
      fail = 0;
10789
  end
10790
 
10791
 
10792
  ////////////////////////////////////////////////////////////////////
10793
  ////                                                            ////
10794 243 tadejm
  ////  Test receive packet synchronization with receive          ////
10795
  ////  disable/enable ( 10Mbps ).                                ////
10796
  ////                                                            ////
10797
  ////////////////////////////////////////////////////////////////////
10798
  if (test_num == 2) // Test no receive when all buffers are TX ( 10Mbps ).
10799
  begin
10800
    // TEST 2: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )
10801
    test_name   = "TEST 2: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )";
10802
    `TIME; $display("  TEST 2: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )");
10803
 
10804
    // unmask interrupts
10805
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
10806
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10807
    // set 10 RX buffer descriptor (8'h80 - 8'hA) - must be set before RX enable
10808
    wbm_write(`ETH_TX_BD_NUM, 32'h76, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10809
    // disable RX, set full-duplex mode, NO receive small, NO correct IFG
10810
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_IFG |
10811
              `ETH_MODER_PRO | `ETH_MODER_BRO,
10812
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10813
    // prepare two packets of MAXFL length
10814
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10815
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
10816
    min_tmp = tmp[31:16];
10817
    st_data = 8'h0F;
10818
    set_rx_packet(0, (min_tmp + 1), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
10819
    append_rx_crc(0, (min_tmp + 1), 1'b0, 1'b0);
10820
    st_data = 8'h1A;
10821
    set_rx_packet(max_tmp, (min_tmp + 1), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
10822
    append_rx_crc(max_tmp, (min_tmp + 1), 1'b0, 1'b0);
10823
    // check WB INT signal
10824
    if (wb_int !== 1'b0)
10825
    begin
10826
      test_fail("WB INT signal should not be set");
10827
      fail = fail + 1;
10828
    end
10829
 
10830
    // write to phy's control register for 10Mbps
10831
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
10832
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
10833
    speed = 10;
10834
 
10835
    frame_started = 0;
10836
    frame_ended = 0;
10837
    wait_for_frame = 0;
10838
    check_frame = 0;
10839
    stop_checking_frame = 0;
10840
    first_fr_received = 1; // at the beginning the first frame of each two will be received!
10841
 
10842
    num_of_frames = 0; // 
10843
    num_of_bd = 0;
10844
    i_length = (min_tmp + 1); // 5 bytes longer than MINFL
10845
    while (i_length < (max_tmp - 4))
10846
    begin
10847
      // choose generating carrier sense and collision 
10848
      case (num_of_frames[1:0])
10849
      2'h0: // Interrupt is generated
10850
      begin
10851
        // enable interrupt generation
10852
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
10853
        // not detect carrier sense in FD and no collision
10854
        eth_phy.no_carrier_sense_rx_fd_detect(0);
10855
        eth_phy.collision(0);
10856
      end
10857
      2'h1: // Interrupt is generated
10858
      begin
10859
        // enable interrupt generation
10860
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
10861
        // detect carrier sense in FD and no collision
10862
        eth_phy.no_carrier_sense_rx_fd_detect(1);
10863
        eth_phy.collision(0);
10864
      end
10865
      2'h2: // Interrupt is generated
10866
      begin
10867
        // disable interrupt generation
10868
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
10869
        // not detect carrier sense in FD and set collision
10870
        eth_phy.no_carrier_sense_rx_fd_detect(0);
10871
        eth_phy.collision(1);
10872
      end
10873
      default: // 2'h3: // Interrupt is generated
10874
      begin
10875
        // disable interrupt generation
10876
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
10877
        // detect carrier sense in FD and set collision
10878
        eth_phy.no_carrier_sense_rx_fd_detect(1);
10879
        eth_phy.collision(1);
10880
      end
10881
      endcase
10882
      // set wrap bit
10883
      set_rx_bd_wrap(118);
10884
      set_rx_bd_empty(118, 118);
10885
      check_frame = 0;
10886
      stop_checking_frame = 0;
10887
      tmp_data = 0;
10888
      fork
10889
        begin // enable RX MAC on first of each two packets - every second should be recived normaly
10890
          if (num_of_frames[0] == 1'b0)
10891
          begin
10892
            repeat(1) @(posedge wb_clk);
10893
            if (num_of_frames[1] == 1'b0)
10894
            begin
10895
              repeat (num_of_frames[31:2]) @(posedge mrx_clk); // for every (second) frame enable receiver one clock later
10896
            end
10897
            else
10898
            begin
10899
              @(posedge mrx_clk);
10900
              repeat (num_of_frames[31:2]) @(negedge mrx_clk); // for every (second) frame enable receiver one clock later
10901
            end
10902
            // enable RX, set full-duplex mode, NO receive small, NO correct IFG
10903
            wbm_init_waits = 4'h0;
10904
            wbm_subseq_waits = 4'h0;
10905
            #1 wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
10906
                      `ETH_MODER_PRO | `ETH_MODER_BRO,
10907
                      4'hF, 1, wbm_init_waits, wbm_subseq_waits); // write ASAP
10908
          end
10909
        end
10910
        begin // send a packet from PHY RX
10911
          repeat(1) @(posedge wb_clk); // wait for WB write when it is without delays
10912
          if (num_of_frames[1] == 1'b0)
10913
          begin
10914
            set_rx_addr_type(0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E);
10915
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
10916
          end
10917
          else
10918
          begin
10919
            set_rx_addr_type((max_tmp), 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E);
10920
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
10921
          end
10922
        end
10923
        begin: send_packet0
10924
          wait (MRxDV === 1'b1); // start transmit
10925
          wait (MRxDV === 1'b0); // end transmit
10926
          check_frame = 1;
10927
          repeat(10) @(posedge mrx_clk);
10928
          repeat(15) @(posedge wb_clk);
10929
          stop_checking_frame = 1;
10930
        end
10931
        begin // count WB clocks between ACK (negedge) and RX_DV (posedge) or vice-versa
10932
          @(posedge eth_sl_wb_ack_o or posedge MRxDV);
10933
          if ((eth_sl_wb_ack_o === 1'b1) && (MRxDV === 1'b1))
10934
          begin
10935
            tmp_data = 32'h8000_0001; // bit[31]==1 => 'posedge MRxDV' was before 'negedge eth_sl_wb_ack_o'
10936
          end
10937
          else if (MRxDV === 1'b1)
10938
          begin
10939
            while (eth_sl_wb_ack_o === 1'b0)
10940
            begin
10941
              @(posedge wb_clk);
10942
              tmp_data = tmp_data + 1;
10943
            end
10944
            tmp_data = tmp_data | 32'h8000_0000; // bit[31]==1 => 'posedge MRxDV' was before 'negedge eth_sl_wb_ack_o'
10945
          end
10946
          else if (eth_sl_wb_ack_o === 1'b1)
10947
          begin
10948
            @(posedge wb_clk); // wait for one clock => tmp_data 'becomes' 0
10949
            while (MRxDV === 1'b0)
10950
            begin
10951
              @(posedge wb_clk);
10952
              tmp_data = tmp_data + 1; // bit[31]==0 => 'negedge eth_sl_wb_ack_o' was equal or before 'posedge MRxDV'
10953
            end
10954
          end
10955
        end
10956
        begin // check packet
10957
          wait (check_frame == 1);
10958
          check_rx_bd(118, tmp_bd);
10959
          while ((tmp_bd[15] === 1) && (stop_checking_frame == 0))
10960
          begin
10961
            #1 check_rx_bd(118, tmp_bd);
10962
            @(posedge wb_clk);
10963
          end
10964
          if (num_of_frames[0] == 1'b0)
10965
          begin
10966
            if (tmp_bd[15] === 1)
10967
            begin
10968
              if (first_fr_received == 1)
10969
              begin
10970
                first_fr_received = 0;
10971
                $display("    %0d packets (without this one) are checked - packets are received by two in a set",
10972
                         num_of_frames); // +1 due to start with 0 AND -1 because this packet is excluded
10973
                $display("    From this moment:");
10974
                $display("    first one of two packets (including this one) is not accepted due to late RX enable");
10975
                if (tmp_data[31])
10976
                  $display("    ->RX enable set %0d WB clks after RX_DV", tmp_data[30:0]);
10977
                else
10978
                  $display("    ->RX enable set %0d WB clks before RX_DV", tmp_data[30:0]);
10979
              end
10980
            end
10981
          end
10982
          if (stop_checking_frame == 0)
10983
            disable send_packet0;
10984
        end
10985
      join
10986
      // ONLY IF packet was received!
10987
      if (tmp_bd[15] === 0)
10988
      begin
10989
        // check length of a PACKET
10990
        if (tmp_bd[31:16] != (i_length + 4))
10991
        begin
10992
          `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
10993
                          tmp_bd[31:16], (i_length + 4));
10994
          test_fail("Wrong length of the packet out from PHY");
10995
          fail = fail + 1;
10996
        end
10997
        // check received RX packet data and CRC
10998
        if (first_fr_received == 0) // if PREVIOUS RX buffer descriptor was not ready, pointer address is -1
10999
        begin
11000
          if (num_of_frames[1] == 1'b0)
11001
          begin
11002
            check_rx_packet(0, (`MEMORY_BASE + num_of_frames[1:0] - 1'b1), (i_length + 4), 1'b0, 1'b0, tmp);
11003
          end
11004
          else
11005
          begin
11006
            check_rx_packet(max_tmp, (`MEMORY_BASE + num_of_frames[1:0] - 1'b1), (i_length + 4), 1'b0, 1'b0, tmp);
11007
          end
11008
          if (tmp > 0)
11009
          begin
11010
            `TIME; $display("*E Wrong data of the received packet");
11011
            test_fail("Wrong data of the received packet");
11012
            fail = fail + 1;
11013
          end
11014
        end
11015
        else // if PREVIOUS RX buffer descriptor was ready
11016
        begin
11017
          if (num_of_frames[1] == 1'b0)
11018
          begin
11019
            check_rx_packet(0, (`MEMORY_BASE + num_of_frames[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
11020
          end
11021
          else
11022
          begin
11023
            check_rx_packet(max_tmp, (`MEMORY_BASE + num_of_frames[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
11024
          end
11025
          if (tmp > 0)
11026
          begin
11027
            `TIME; $display("*E Wrong data of the received packet");
11028
            test_fail("Wrong data of the received packet");
11029
            fail = fail + 1;
11030
          end
11031
        end
11032
      end
11033
      // check WB INT signal
11034
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11035
      begin
11036
        if (wb_int !== 1'b0)
11037
        begin
11038
          `TIME; $display("*E WB INT signal should not be set");
11039
          test_fail("WB INT signal should not be set");
11040
          fail = fail + 1;
11041
        end
11042
      end
11043
      else
11044
      begin
11045
        if (wb_int !== 1'b1)
11046
        begin
11047
          `TIME; $display("*E WB INT signal should be set");
11048
          test_fail("WB INT signal should be set");
11049
          fail = fail + 1;
11050
        end
11051
      end
11052
      // check RX buffer descriptor of a packet - only 15 LSBits
11053
      check_rx_bd(118, data);
11054
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11055
      begin
11056
        if (data[15:0] !== 16'hE000)
11057
        begin
11058
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11059
          test_fail("RX buffer descriptor status is not correct");
11060
          fail = fail + 1;
11061
        end
11062
      end
11063
      else // interrupt enabled
11064
      begin
11065
        if (data[15:0] !== 16'h6000)
11066
        begin
11067
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11068
          test_fail("RX buffer descriptor status is not correct");
11069
          fail = fail + 1;
11070
        end
11071
      end
11072
      // check interrupts
11073
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11074
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11075
      begin
11076
        if (data !== 0)
11077
        begin
11078
          `TIME; $display("*E Any of interrupts was set, interrupt reg: %0h, len: %0h", data, num_of_frames[1:0]);
11079
          test_fail("Any of interrupts was set");
11080
          fail = fail + 1;
11081
        end
11082
      end
11083
      else
11084
      begin
11085
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
11086
        begin
11087
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
11088
          test_fail("Interrupt Receive Buffer was not set");
11089
          fail = fail + 1;
11090
        end
11091
        if ((data & (~`ETH_INT_RXB)) !== 0)
11092
        begin
11093
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
11094
          test_fail("Other interrupts (except Receive Buffer) were set");
11095
          fail = fail + 1;
11096
        end
11097
      end
11098
      // clear interrupts
11099
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11100
      // check WB INT signal
11101
      if (wb_int !== 1'b0)
11102
      begin
11103
        test_fail("WB INT signal should not be set");
11104
        fail = fail + 1;
11105
      end
11106
      // disable RX after two packets
11107
      if (num_of_frames[0] == 1'b1)
11108
      begin
11109
        // disable RX, set full-duplex mode, NO receive small, NO correct IFG
11110
        wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_IFG |
11111
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
11112
                  4'hF, 1, 4'h0, 4'h0); // write ASAP
11113
      end
11114
      // the number of frame transmitted
11115
      num_of_frames = num_of_frames + 1;
11116
      num_of_bd = 0;
11117
      // set length (LOOP variable)
11118
      if (num_of_frames[31:2] == (i_length * 2 + 16)) // 64 => this vas last Byte (1st .. 64th) when i_length = min_tmp - 4
11119
        i_length = (max_tmp - 4);
11120
      @(posedge wb_clk);
11121
    end
11122
    // disable RX
11123
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
11124
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11125
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11126
    if(fail == 0)
11127
      test_ok;
11128
    else
11129
      fail = 0;
11130
  end
11131
 
11132
 
11133
  ////////////////////////////////////////////////////////////////////
11134
  ////                                                            ////
11135
  ////  Test receive packet synchronization with receive          ////
11136
  ////  disable/enable ( 100Mbps ).                               ////
11137
  ////                                                            ////
11138
  ////////////////////////////////////////////////////////////////////
11139
  if (test_num == 3) // Test no receive when all buffers are TX ( 100Mbps ).
11140
  begin
11141
    // TEST 3: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 100Mbps )
11142
    test_name   = "TEST 3: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 100Mbps )";
11143
    `TIME; $display("  TEST 3: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 100Mbps )");
11144
 
11145
    // unmask interrupts
11146
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11147
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11148
    // set 10 RX buffer descriptor (8'h80 - 8'hA) - must be set before RX enable
11149
    wbm_write(`ETH_TX_BD_NUM, 32'h76, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11150
    // disable RX, set full-duplex mode, NO receive small, NO correct IFG
11151
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_IFG |
11152
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11153
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11154
    // prepare two packets of MAXFL length
11155
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11156
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
11157
    min_tmp = tmp[31:16];
11158
    st_data = 8'h0F;
11159
    set_rx_packet(0, (min_tmp + 1), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
11160
    append_rx_crc(0, (min_tmp + 1), 1'b0, 1'b0);
11161
    st_data = 8'h1A;
11162
    set_rx_packet(max_tmp, (min_tmp + 1), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
11163
    append_rx_crc(max_tmp, (min_tmp + 1), 1'b0, 1'b0);
11164
    // check WB INT signal
11165
    if (wb_int !== 1'b0)
11166
    begin
11167
      test_fail("WB INT signal should not be set");
11168
      fail = fail + 1;
11169
    end
11170
 
11171
    // write to phy's control register for 100Mbps
11172
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
11173
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
11174
    speed = 100;
11175
 
11176
    frame_started = 0;
11177
    frame_ended = 0;
11178
    wait_for_frame = 0;
11179
    check_frame = 0;
11180
    stop_checking_frame = 0;
11181
    first_fr_received = 1; // at the beginning the first frame of each two will be received!
11182
 
11183
    num_of_frames = 0; // 
11184
    num_of_bd = 0;
11185
    i_length = (min_tmp + 1); // 5 bytes longer than MINFL
11186
    while (i_length < (max_tmp - 4))
11187
    begin
11188
      // choose generating carrier sense and collision 
11189
      case (num_of_frames[1:0])
11190
      2'h0: // Interrupt is generated
11191
      begin
11192
        // enable interrupt generation
11193
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
11194
        // not detect carrier sense in FD and no collision
11195
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11196
        eth_phy.collision(0);
11197
      end
11198
      2'h1: // Interrupt is generated
11199
      begin
11200
        // enable interrupt generation
11201
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
11202
        // detect carrier sense in FD and no collision
11203
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11204
        eth_phy.collision(0);
11205
      end
11206
      2'h2: // Interrupt is generated
11207
      begin
11208
        // disable interrupt generation
11209
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
11210
        // not detect carrier sense in FD and set collision
11211
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11212
        eth_phy.collision(1);
11213
      end
11214
      default: // 2'h3: // Interrupt is generated
11215
      begin
11216
        // disable interrupt generation
11217
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
11218
        // detect carrier sense in FD and set collision
11219
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11220
        eth_phy.collision(1);
11221
      end
11222
      endcase
11223
//if (first_fr_received == 0)
11224
//begin
11225
//  check_rx_bd(118, data);
11226
//  wbm_read((`TX_BD_BASE + (118 * 8) + 4), tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11227
//  $display("RX BD set : %h, %h", data, tmp);
11228
//end
11229
      // set wrap bit
11230
      set_rx_bd_wrap(118);
11231
      set_rx_bd_empty(118, 118);
11232
      check_frame = 0;
11233
      stop_checking_frame = 0;
11234
      tmp_data = 0;
11235
$display("mama 1");
11236
      fork
11237
        begin // enable RX MAC on first of each two packets - every second should be recived normaly
11238
          if (num_of_frames[0] == 1'b0)
11239
          begin
11240
            repeat(1) @(posedge wb_clk);
11241
            if (num_of_frames[1] == 1'b0)
11242
            begin
11243
              repeat (num_of_frames[31:2]) @(posedge mrx_clk); // for every (second) frame enable receiver one clock later
11244
            end
11245
            else
11246
            begin
11247
              @(posedge mrx_clk);
11248
              repeat (num_of_frames[31:2]) @(negedge mrx_clk); // for every (second) frame enable receiver one clock later
11249
            end
11250
            // enable RX, set full-duplex mode, NO receive small, NO correct IFG
11251
            wbm_init_waits = 4'h0;
11252
            wbm_subseq_waits = 4'h0;
11253
            #1 wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
11254
                      `ETH_MODER_PRO | `ETH_MODER_BRO,
11255
                      4'hF, 1, wbm_init_waits, wbm_subseq_waits); // write ASAP
11256
$display("mama 2, num_of_frames=%0h", num_of_frames);
11257
          end
11258
        end
11259
        begin // send a packet from PHY RX
11260
          repeat(1) @(posedge wb_clk); // wait for WB write when it is without delays
11261
          if (num_of_frames[1] == 1'b0)
11262
          begin
11263
            set_rx_addr_type(0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E);
11264
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
11265
          end
11266
          else
11267
          begin
11268
            set_rx_addr_type((max_tmp), 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E);
11269
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
11270
          end
11271
        end
11272
        begin: send_packet1
11273
          wait (MRxDV === 1'b1); // start transmit
11274
          wait (MRxDV === 1'b0); // end transmit
11275
          check_frame = 1;
11276
$display("mama 3");
11277
          repeat(10) @(posedge mrx_clk);
11278
          repeat(15) @(posedge wb_clk);
11279
          stop_checking_frame = 1;
11280
        end
11281
        begin // count WB clocks between ACK (negedge) and RX_DV (posedge) or vice-versa
11282
          @(posedge eth_sl_wb_ack_o or posedge MRxDV);
11283
$display("mama 4");
11284
          if ((eth_sl_wb_ack_o === 1'b1) && (MRxDV === 1'b1))
11285
          begin
11286
            tmp_data = 32'h8000_0001; // bit[31]==1 => 'posedge MRxDV' was before 'negedge eth_sl_wb_ack_o'
11287
$display("mama 4_1");
11288
          end
11289
          else if (MRxDV === 1'b1)
11290
          begin
11291
            while (eth_sl_wb_ack_o === 1'b0)
11292
            begin
11293
              @(posedge wb_clk);
11294
              tmp_data = tmp_data + 1;
11295
            end
11296
            tmp_data = tmp_data | 32'h8000_0000; // bit[31]==1 => 'posedge MRxDV' was before 'negedge eth_sl_wb_ack_o'
11297
$display("mama 4_2");
11298
          end
11299
          else if (eth_sl_wb_ack_o === 1'b1)
11300
          begin
11301
            @(posedge wb_clk); // wait for one clock => tmp_data 'becomes' 0
11302
            while (MRxDV === 1'b0)
11303
            begin
11304
              @(posedge wb_clk);
11305
              tmp_data = tmp_data + 1; // bit[31]==0 => 'negedge eth_sl_wb_ack_o' was equal or before 'posedge MRxDV'
11306
            end
11307
$display("mama 4_3");
11308
          end
11309
        end
11310
        begin // check packet
11311
          wait (check_frame == 1);
11312
          check_rx_bd(118, tmp_bd);
11313
          while ((tmp_bd[15] === 1) && (stop_checking_frame == 0))
11314
          begin
11315
            #1 check_rx_bd(118, tmp_bd);
11316
            @(posedge wb_clk);
11317
          end
11318
$display("mama 5, tmp_bd=%0h", tmp_bd);
11319
          if (num_of_frames[0] == 1'b0)
11320
          begin
11321
            if (tmp_bd[15] === 1)
11322
            begin
11323
              if (first_fr_received == 1)
11324
              begin
11325
                first_fr_received = 0;
11326
                $display("    %0d packets (without this one) are checked - packets are received by two in a set",
11327
                         num_of_frames); // +1 due to start with 0 AND -1 because this packet is excluded
11328
                $display("    From this moment:");
11329
                $display("    first one of two packets (including this one) is not accepted due to late RX enable");
11330
                if (tmp_data[31])
11331
                  $display("    ->RX enable set %0d WB clks after RX_DV", tmp_data[30:0]);
11332
                else
11333
                  $display("    ->RX enable set %0d WB clks before RX_DV", tmp_data[30:0]);
11334
              end
11335
              // check FB, etc.
11336
 
11337
            end
11338
            else // (tmp_bd[15] === 0)
11339
            begin // check FB, packet, etc.
11340
 
11341
            end
11342
$display("mama 5_1");
11343
          end
11344
          else // (num_of_frames[0] == 1'b1)
11345
          begin
11346
            if (tmp_bd[15] === 1) // ERROR, because second packet of each two frames should be received
11347
            begin // check NOTHING
11348
 
11349
            end
11350
            else // (tmp_bd[15] === 0)
11351
            begin // check FB, packet, etc.
11352
 
11353
            end
11354
$display("mama 5_2");
11355
          end
11356
          if (stop_checking_frame == 0)
11357
            disable send_packet1;
11358
        end
11359
      join
11360
      // ONLY IF packet was received!
11361
$display("mama 6");
11362
      if (tmp_bd[15] === 0)
11363
      begin
11364
        // check length of a PACKET
11365
        if (tmp_bd[31:16] != (i_length + 4))
11366
        begin
11367
          `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
11368
                          tmp_bd[31:16], (i_length + 4));
11369
          test_fail("Wrong length of the packet out from PHY");
11370
          fail = fail + 1;
11371
        end
11372
        // check received RX packet data and CRC
11373
        if (first_fr_received == 0) // if PREVIOUS RX buffer descriptor was not ready, pointer address is -1
11374
        begin
11375
          if (num_of_frames[1] == 1'b0)
11376
          begin
11377
            check_rx_packet(0, (`MEMORY_BASE + num_of_frames[1:0] - 1'b1), (i_length + 4), 1'b0, 1'b0, tmp);
11378
          end
11379
          else
11380
          begin
11381
            check_rx_packet(max_tmp, (`MEMORY_BASE + num_of_frames[1:0] - 1'b1), (i_length + 4), 1'b0, 1'b0, tmp);
11382
          end
11383
          if (tmp > 0)
11384
          begin
11385
            `TIME; $display("*E Wrong data of the received packet");
11386
            test_fail("Wrong data of the received packet");
11387
            fail = fail + 1;
11388
          end
11389
        end
11390
        else // if PREVIOUS RX buffer descriptor was ready
11391
        begin
11392
          if (num_of_frames[1] == 1'b0)
11393
          begin
11394
            check_rx_packet(0, (`MEMORY_BASE + num_of_frames[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
11395
          end
11396
          else
11397
          begin
11398
            check_rx_packet(max_tmp, (`MEMORY_BASE + num_of_frames[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
11399
          end
11400
          if (tmp > 0)
11401
          begin
11402
            `TIME; $display("*E Wrong data of the received packet");
11403
            test_fail("Wrong data of the received packet");
11404
            fail = fail + 1;
11405
          end
11406
        end
11407
      end
11408
      // check WB INT signal
11409
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11410
      begin
11411
        if (wb_int !== 1'b0)
11412
        begin
11413
          `TIME; $display("*E WB INT signal should not be set");
11414
          test_fail("WB INT signal should not be set");
11415
          fail = fail + 1;
11416
        end
11417
      end
11418
      else
11419
      begin
11420
        if (wb_int !== 1'b1)
11421
        begin
11422
          `TIME; $display("*E WB INT signal should be set");
11423
          test_fail("WB INT signal should be set");
11424
          fail = fail + 1;
11425
        end
11426
      end
11427
      // check RX buffer descriptor of a packet - only 15 LSBits
11428
      check_rx_bd(118, data);
11429
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11430
      begin
11431
        if (data[15:0] !== 16'hE000)
11432
        begin
11433
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11434
          test_fail("RX buffer descriptor status is not correct");
11435
          fail = fail + 1;
11436
        end
11437
      end
11438
      else // interrupt enabled
11439
      begin
11440
        if (data[15:0] !== 16'h6000)
11441
        begin
11442
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11443
          test_fail("RX buffer descriptor status is not correct");
11444
          fail = fail + 1;
11445
        end
11446
      end
11447
      // check interrupts
11448
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11449
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11450
      begin
11451
        if (data !== 0)
11452
        begin
11453
          `TIME; $display("*E Any of interrupts was set, interrupt reg: %0h, len: %0h", data, num_of_frames[1:0]);
11454
          test_fail("Any of interrupts was set");
11455
          fail = fail + 1;
11456
        end
11457
      end
11458
      else
11459
      begin
11460
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
11461
        begin
11462
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
11463
          test_fail("Interrupt Receive Buffer was not set");
11464
          fail = fail + 1;
11465
        end
11466
        if ((data & (~`ETH_INT_RXB)) !== 0)
11467
        begin
11468
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
11469
          test_fail("Other interrupts (except Receive Buffer) were set");
11470
          fail = fail + 1;
11471
        end
11472
      end
11473
      // clear interrupts
11474
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11475
      // check WB INT signal
11476
      if (wb_int !== 1'b0)
11477
      begin
11478
        test_fail("WB INT signal should not be set");
11479
        fail = fail + 1;
11480
      end
11481
      // disable RX after two packets
11482
      if (num_of_frames[0] == 1'b1)
11483
      begin
11484
        // disable RX, set full-duplex mode, NO receive small, NO correct IFG
11485
        wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_IFG |
11486
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
11487
                  4'hF, 1, 4'h0, 4'h0); // write ASAP
11488
      end
11489
      // the number of frame transmitted
11490
      num_of_frames = num_of_frames + 1;
11491
      num_of_bd = 0;
11492
      // set length (LOOP variable)
11493
      if (num_of_frames[31:2] == (i_length * 2 + 16)) // 64 => this vas last Byte (1st .. 64th) when i_length = min_tmp - 4
11494
        i_length = (max_tmp - 4);
11495
      @(posedge wb_clk);
11496
    end
11497
    // disable RX
11498
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
11499
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11500
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11501
    if(fail == 0)
11502
      test_ok;
11503
    else
11504
      fail = 0;
11505
  end
11506
 
11507
 
11508
  ////////////////////////////////////////////////////////////////////
11509
  ////                                                            ////
11510 209 tadejm
  ////  Test receive packets form MINFL to MAXFL sizes at         ////
11511
  ////  one RX buffer decriptor ( 10Mbps ).                       ////
11512
  ////                                                            ////
11513
  ////////////////////////////////////////////////////////////////////
11514 243 tadejm
  if (test_num == 4) // 
11515 209 tadejm
  begin
11516 243 tadejm
    // TEST 4: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 10Mbps )
11517
    test_name   = "TEST 4: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 10Mbps )";
11518
    `TIME; $display("  TEST 4: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 10Mbps )");
11519 209 tadejm
 
11520
    // unmask interrupts
11521
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11522
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11523
    // set 1 RX buffer descriptor (8'h80 - 1) - must be set before RX enable
11524
    wbm_write(`ETH_TX_BD_NUM, 32'h7F, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11525
    // enable RX, set full-duplex mode, NO receive small, NO correct IFG
11526
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
11527
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11528
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11529
    // prepare two packets of MAXFL length
11530
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11531
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
11532
    min_tmp = tmp[31:16];
11533
    st_data = 8'h0F;
11534
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
11535
    st_data = 8'h1A;
11536
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
11537
    // check WB INT signal
11538
    if (wb_int !== 1'b0)
11539
    begin
11540
      test_fail("WB INT signal should not be set");
11541
      fail = fail + 1;
11542
    end
11543
 
11544
    // write to phy's control register for 10Mbps
11545
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
11546
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
11547
    speed = 10;
11548
 
11549
    i_length = (min_tmp - 4);
11550
    while (i_length <= (max_tmp - 4))
11551
    begin
11552
      // choose generating carrier sense and collision for first and last 64 lengths of frames
11553
      case (i_length[1:0])
11554
      2'h0: // Interrupt is generated
11555
      begin
11556
        // enable interrupt generation
11557
        set_rx_bd(127, 127, 1'b1, (`MEMORY_BASE + i_length[1:0]));
11558
        // unmask interrupts
11559
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11560
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11561
        // not detect carrier sense in FD and no collision
11562
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11563
        eth_phy.collision(0);
11564
      end
11565
      2'h1: // Interrupt is not generated
11566
      begin
11567
        // enable interrupt generation
11568
        set_rx_bd(127, 127, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
11569
        // mask interrupts
11570
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11571
        // detect carrier sense in FD and no collision
11572
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11573
        eth_phy.collision(0);
11574
      end
11575
      2'h2: // Interrupt is not generated
11576
      begin
11577
        // disable interrupt generation
11578
        set_rx_bd(127, 127, 1'b0, (`MEMORY_BASE + i_length[1:0]));
11579
        // unmask interrupts
11580
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11581
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11582
        // not detect carrier sense in FD and set collision
11583
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11584
        eth_phy.collision(1);
11585
      end
11586
      default: // 2'h3: // Interrupt is not generated
11587
      begin
11588
        // disable interrupt generation
11589
        set_rx_bd(127, 127, 1'b0, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
11590
        // mask interrupts
11591
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11592
        // detect carrier sense in FD and set collision
11593
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11594
        eth_phy.collision(1);
11595
      end
11596
      endcase
11597
      if (i_length[0] == 1'b0)
11598
        append_rx_crc (0, i_length, 1'b0, 1'b0);
11599
      else
11600
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
11601
      // set wrap bit
11602
      set_rx_bd_wrap(127);
11603
      set_rx_bd_empty(127, 127);
11604
      fork
11605
        begin
11606
          if (i_length[0] == 1'b0)
11607
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
11608
          else
11609
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
11610
          repeat(10) @(posedge mrx_clk);
11611
        end
11612
        begin
11613
          #1 check_rx_bd(127, data);
11614
          if (i_length < min_tmp) // just first four
11615
          begin
11616
            while (data[15] === 1)
11617
            begin
11618
              #1 check_rx_bd(127, data);
11619
              @(posedge wb_clk);
11620
            end
11621
            repeat (1) @(posedge wb_clk);
11622
          end
11623
          else
11624
          begin
11625
            wait (MRxDV === 1'b1); // start transmit
11626
            #1 check_rx_bd(127, data);
11627
            if (data[15] !== 1)
11628
            begin
11629
              test_fail("Wrong buffer descriptor's ready bit read out from MAC");
11630
              fail = fail + 1;
11631
            end
11632
            wait (MRxDV === 1'b0); // end transmit
11633
            while (data[15] === 1)
11634
            begin
11635
              #1 check_rx_bd(127, data);
11636
              @(posedge wb_clk);
11637
            end
11638
            repeat (1) @(posedge wb_clk);
11639
          end
11640
        end
11641
      join
11642
      // check length of a PACKET
11643
      if (data[31:16] != (i_length + 4))
11644
      begin
11645
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
11646
                        data[31:16], (i_length + 4));
11647
        test_fail("Wrong length of the packet out from PHY");
11648
        fail = fail + 1;
11649
      end
11650
      // checking in the following if statement is performed only for first and last 64 lengths
11651
      // check received RX packet data and CRC
11652
      if (i_length[0] == 1'b0)
11653
      begin
11654
        check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
11655
      end
11656
      else
11657
      begin
11658
        check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
11659
      end
11660
      if (tmp > 0)
11661
      begin
11662
        `TIME; $display("*E Wrong data of the received packet");
11663
        test_fail("Wrong data of the received packet");
11664
        fail = fail + 1;
11665
      end
11666
      // check WB INT signal
11667
      if (i_length[1:0] == 2'h0)
11668
      begin
11669
        if (wb_int !== 1'b1)
11670
        begin
11671
          `TIME; $display("*E WB INT signal should be set");
11672
          test_fail("WB INT signal should be set");
11673
          fail = fail + 1;
11674
        end
11675
      end
11676
      else
11677
      begin
11678
        if (wb_int !== 1'b0)
11679
        begin
11680
          `TIME; $display("*E WB INT signal should not be set");
11681
          test_fail("WB INT signal should not be set");
11682
          fail = fail + 1;
11683
        end
11684
      end
11685
      // check RX buffer descriptor of a packet
11686
      check_rx_bd(127, data);
11687
      if (i_length[1] == 1'b0) // interrupt enabled no_carrier_sense_rx_fd_detect
11688
      begin
11689
        if ( ((data[15:0] !== 16'h6000) && (i_length[0] == 1'b0)) ||
11690
             ((data[15:0] !== 16'h6000) && (i_length[0] == 1'b1)) )
11691
        begin
11692
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11693
          test_fail("RX buffer descriptor status is not correct");
11694
          fail = fail + 1;
11695
        end
11696
      end
11697
      else // interrupt not enabled
11698
      begin
11699
        if ( ((data[15:0] !== 16'h2000) && (i_length[0] == 1'b0)) ||
11700
             ((data[15:0] !== 16'h2000) && (i_length[0] == 1'b1)) )
11701
        begin
11702
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11703
          test_fail("RX buffer descriptor status is not correct");
11704
          fail = fail + 1;
11705
        end
11706
      end
11707
      // clear RX buffer descriptor for first 4 frames
11708
      if (i_length < min_tmp)
11709
        clear_rx_bd(127, 127);
11710
      // check interrupts
11711
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11712
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
11713
      begin
11714
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
11715
        begin
11716
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
11717
          test_fail("Interrupt Receive Buffer was not set");
11718
          fail = fail + 1;
11719
        end
11720
        if ((data & (~`ETH_INT_RXB)) !== 0)
11721
        begin
11722
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
11723
          test_fail("Other interrupts (except Receive Buffer) were set");
11724
          fail = fail + 1;
11725
        end
11726
      end
11727
      else
11728
      begin
11729
        if (data !== 0)
11730
        begin
11731
          `TIME; $display("*E Any of interrupts (except Receive Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
11732
          test_fail("Any of interrupts (except Receive Buffer) was set");
11733
          fail = fail + 1;
11734
        end
11735
      end
11736
      // clear interrupts
11737
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11738
      // check WB INT signal
11739
      if (wb_int !== 1'b0)
11740
      begin
11741
        test_fail("WB INT signal should not be set");
11742
        fail = fail + 1;
11743
      end
11744
      // INTERMEDIATE DISPLAYS
11745
      if ((i_length + 4) == (min_tmp + 64))
11746
      begin
11747
        // starting length is min_tmp, ending length is (min_tmp + 64)
11748
        $display("    receive small packets is NOT selected");
11749
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
11750
                 min_tmp, (min_tmp + 64));
11751
        // set receive small, remain the rest
11752
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
11753
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
11754
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11755
      end
11756
      else if ((i_length + 4) == (max_tmp - 16))
11757
      begin
11758
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
11759
        $display("    receive small packets is selected");
11760
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
11761
                 (min_tmp + 64 + 128), tmp_data);
11762
        // reset receive small, remain the rest
11763
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
11764
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
11765
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11766
      end
11767
      else if ((i_length + 4) == max_tmp)
11768
      begin
11769
        $display("    receive small packets is NOT selected");
11770
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
11771
                 (max_tmp - (4 + 16)), max_tmp);
11772
      end
11773
      // set length (loop variable)
11774
      if ((i_length + 4) < (min_tmp + 64))
11775
        i_length = i_length + 1;
11776
      else if ( ((i_length + 4) >= (min_tmp + 64)) && ((i_length + 4) <= (max_tmp - 256)) )
11777
      begin
11778
        i_length = i_length + 128;
11779
        tmp_data = i_length + 4; // last tmp_data is ending length
11780
      end
11781
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
11782
        i_length = max_tmp - (4 + 16);
11783
      else if ((i_length + 4) >= (max_tmp - 16))
11784
        i_length = i_length + 1;
11785
      else
11786
      begin
11787
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
11788
        #10 $stop;
11789
      end
11790
    end
11791
    // disable RX
11792
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
11793
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11794
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11795
    if(fail == 0)
11796
      test_ok;
11797
    else
11798
      fail = 0;
11799
  end
11800
 
11801
 
11802
  ////////////////////////////////////////////////////////////////////
11803
  ////                                                            ////
11804
  ////  Test receive packets form MINFL to MAXFL sizes at         ////
11805
  ////  one RX buffer decriptor ( 100Mbps ).                      ////
11806
  ////                                                            ////
11807
  ////////////////////////////////////////////////////////////////////
11808 243 tadejm
  if (test_num == 5) // Test no receive when all buffers are TX ( 100Mbps ).
11809 209 tadejm
  begin
11810 243 tadejm
    // TEST 5: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 100Mbps )
11811
    test_name   = "TEST 5: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 100Mbps )";
11812
    `TIME; $display("  TEST 5: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 100Mbps )");
11813 209 tadejm
 
11814
    // unmask interrupts
11815
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11816
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11817
    // set 1 RX buffer descriptor (8'h80 - 1) - must be set before RX enable
11818
    wbm_write(`ETH_TX_BD_NUM, 32'h7F, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11819
    // enable RX, set full-duplex mode, NO receive small, NO correct IFG
11820
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
11821
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11822
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11823
    // prepare two packets of MAXFL length
11824
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11825
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
11826
    min_tmp = tmp[31:16];
11827
    st_data = 8'h0F;
11828
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
11829
    st_data = 8'h1A;
11830
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
11831
    // check WB INT signal
11832
    if (wb_int !== 1'b0)
11833
    begin
11834
      test_fail("WB INT signal should not be set");
11835
      fail = fail + 1;
11836
    end
11837
 
11838
    // write to phy's control register for 100Mbps
11839
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
11840
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
11841
    speed = 100;
11842
 
11843
    i_length = (min_tmp - 4);
11844
    while (i_length <= (max_tmp - 4))
11845
    begin
11846
      // choose generating carrier sense and collision for first and last 64 lengths of frames
11847
      case (i_length[1:0])
11848
      2'h0: // Interrupt is generated
11849
      begin
11850
        // enable interrupt generation
11851
        set_rx_bd(127, 127, 1'b1, (`MEMORY_BASE + i_length[1:0]));
11852
        // unmask interrupts
11853
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11854
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11855
        // not detect carrier sense in FD and no collision
11856
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11857
        eth_phy.collision(0);
11858
      end
11859
      2'h1: // Interrupt is not generated
11860
      begin
11861
        // enable interrupt generation
11862
        set_rx_bd(127, 127, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
11863
        // mask interrupts
11864
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11865
        // detect carrier sense in FD and no collision
11866
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11867
        eth_phy.collision(0);
11868
      end
11869
      2'h2: // Interrupt is not generated
11870
      begin
11871
        // disable interrupt generation
11872
        set_rx_bd(127, 127, 1'b0, (`MEMORY_BASE + i_length[1:0]));
11873
        // unmask interrupts
11874
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11875
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11876
        // not detect carrier sense in FD and set collision
11877
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11878
        eth_phy.collision(1);
11879
      end
11880
      default: // 2'h3: // Interrupt is not generated
11881
      begin
11882
        // disable interrupt generation
11883
        set_rx_bd(127, 127, 1'b0, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
11884
        // mask interrupts
11885
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11886
        // detect carrier sense in FD and set collision
11887
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11888
        eth_phy.collision(1);
11889
      end
11890
      endcase
11891
      if (i_length[0] == 1'b0)
11892
        append_rx_crc (0, i_length, 1'b0, 1'b0);
11893
      else
11894
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
11895
      // set wrap bit
11896
      set_rx_bd_wrap(127);
11897
      set_rx_bd_empty(127, 127);
11898
      fork
11899
        begin
11900
          if (i_length[0] == 1'b0)
11901
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
11902
          else
11903
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
11904
          repeat(10) @(posedge mrx_clk);
11905
        end
11906
        begin
11907
          #1 check_rx_bd(127, data);
11908
          if (i_length < min_tmp) // just first four
11909
          begin
11910
            while (data[15] === 1)
11911
            begin
11912
              #1 check_rx_bd(127, data);
11913
              @(posedge wb_clk);
11914
            end
11915
            repeat (1) @(posedge wb_clk);
11916
          end
11917
          else
11918
          begin
11919
            wait (MRxDV === 1'b1); // start transmit
11920
            #1 check_rx_bd(127, data);
11921
            if (data[15] !== 1)
11922
            begin
11923
              test_fail("Wrong buffer descriptor's ready bit read out from MAC");
11924
              fail = fail + 1;
11925
            end
11926
            wait (MRxDV === 1'b0); // end transmit
11927
            while (data[15] === 1)
11928
            begin
11929
              #1 check_rx_bd(127, data);
11930
              @(posedge wb_clk);
11931
            end
11932
            repeat (1) @(posedge wb_clk);
11933
          end
11934
        end
11935
      join
11936
      // check length of a PACKET
11937
      if (data[31:16] != (i_length + 4))
11938
      begin
11939
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
11940
                        data[31:16], (i_length + 4));
11941
        test_fail("Wrong length of the packet out from PHY");
11942
        fail = fail + 1;
11943
      end
11944
      // checking in the following if statement is performed only for first and last 64 lengths
11945
      // check received RX packet data and CRC
11946
      if (i_length[0] == 1'b0)
11947
      begin
11948
        check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
11949
      end
11950
      else
11951
      begin
11952
        check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
11953
      end
11954
      if (tmp > 0)
11955
      begin
11956
        `TIME; $display("*E Wrong data of the received packet");
11957
        test_fail("Wrong data of the received packet");
11958
        fail = fail + 1;
11959
      end
11960
      // check WB INT signal
11961
      if (i_length[1:0] == 2'h0)
11962
      begin
11963
        if (wb_int !== 1'b1)
11964
        begin
11965
          `TIME; $display("*E WB INT signal should be set");
11966
          test_fail("WB INT signal should be set");
11967
          fail = fail + 1;
11968
        end
11969
      end
11970
      else
11971
      begin
11972
        if (wb_int !== 1'b0)
11973
        begin
11974
          `TIME; $display("*E WB INT signal should not be set");
11975
          test_fail("WB INT signal should not be set");
11976
          fail = fail + 1;
11977
        end
11978
      end
11979
      // check RX buffer descriptor of a packet
11980
      check_rx_bd(127, data);
11981
      if (i_length[1] == 1'b0) // interrupt enabled 
11982
      begin
11983
        if ( ((data[15:0] !== 16'h6000) && (i_length[0] == 1'b0)) ||
11984
             ((data[15:0] !== 16'h6000) && (i_length[0] == 1'b1)) )
11985
        begin
11986
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11987
          test_fail("RX buffer descriptor status is not correct");
11988
          fail = fail + 1;
11989
        end
11990
      end
11991
      else // interrupt not enabled
11992
      begin
11993
        if ( ((data[15:0] !== 16'h2000) && (i_length[0] == 1'b0)) ||
11994
             ((data[15:0] !== 16'h2000) && (i_length[0] == 1'b1)) )
11995
        begin
11996
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11997
          test_fail("RX buffer descriptor status is not correct");
11998
          fail = fail + 1;
11999
        end
12000
      end
12001
      // clear RX buffer descriptor for first 4 frames
12002
      if (i_length < min_tmp)
12003
        clear_rx_bd(127, 127);
12004
      // check interrupts
12005
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12006
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
12007
      begin
12008
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
12009
        begin
12010
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
12011
          test_fail("Interrupt Receive Buffer was not set");
12012
          fail = fail + 1;
12013
        end
12014
        if ((data & (~`ETH_INT_RXB)) !== 0)
12015
        begin
12016
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
12017
          test_fail("Other interrupts (except Receive Buffer) were set");
12018
          fail = fail + 1;
12019
        end
12020
      end
12021
      else
12022
      begin
12023
        if (data !== 0)
12024
        begin
12025
          `TIME; $display("*E Any of interrupts (except Receive Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
12026
          test_fail("Any of interrupts (except Receive Buffer) was set");
12027
          fail = fail + 1;
12028
        end
12029
      end
12030
      // clear interrupts
12031
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12032
      // check WB INT signal
12033
      if (wb_int !== 1'b0)
12034
      begin
12035
        test_fail("WB INT signal should not be set");
12036
        fail = fail + 1;
12037
      end
12038
      // INTERMEDIATE DISPLAYS
12039
      if ((i_length + 4) == (min_tmp + 64))
12040
      begin
12041
        // starting length is min_tmp, ending length is (min_tmp + 64)
12042
        $display("    receive small packets is NOT selected");
12043
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
12044
                 min_tmp, (min_tmp + 64));
12045
        // set receive small, remain the rest
12046
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12047
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12048
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12049
      end
12050
      else if ((i_length + 4) == (max_tmp - 16))
12051
      begin
12052
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
12053
        $display("    receive small packets is selected");
12054
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
12055
                 (min_tmp + 64 + 128), tmp_data);
12056
        // reset receive small, remain the rest
12057
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12058
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12059
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12060
      end
12061
      else if ((i_length + 4) == max_tmp)
12062
      begin
12063
        $display("    receive small packets is NOT selected");
12064
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
12065
                 (max_tmp - (4 + 16)), max_tmp);
12066
      end
12067
      // set length (loop variable)
12068
      if ((i_length + 4) < (min_tmp + 64))
12069
        i_length = i_length + 1;
12070
      else if ( ((i_length + 4) >= (min_tmp + 64)) && ((i_length + 4) <= (max_tmp - 256)) )
12071
      begin
12072
        i_length = i_length + 128;
12073
        tmp_data = i_length + 4; // last tmp_data is ending length
12074
      end
12075
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
12076
        i_length = max_tmp - (4 + 16);
12077
      else if ((i_length + 4) >= (max_tmp - 16))
12078
        i_length = i_length + 1;
12079
      else
12080
      begin
12081
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
12082
        #10 $stop;
12083
      end
12084
    end
12085
    // disable RX
12086
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12087
              `ETH_MODER_PRO | `ETH_MODER_BRO,
12088
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12089
    if(fail == 0)
12090
      test_ok;
12091
    else
12092
      fail = 0;
12093
  end
12094
 
12095
 
12096
  ////////////////////////////////////////////////////////////////////
12097
  ////                                                            ////
12098
  ////  Test receive packets form MINFL to MAXFL sizes at         ////
12099
  ////  maximum RX buffer decriptors ( 10Mbps ).                  ////
12100
  ////                                                            ////
12101
  ////////////////////////////////////////////////////////////////////
12102 243 tadejm
  if (test_num == 6) // 
12103 209 tadejm
  begin
12104 243 tadejm
    // TEST 6: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 10Mbps )
12105
    test_name = "TEST 6: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 10Mbps )";
12106
    `TIME; $display("  TEST 6: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 10Mbps )");
12107 209 tadejm
 
12108
    // reset MAC registers
12109
    hard_reset;
12110
    // reset MAC and MII LOGIC with soft reset
12111
    reset_mac;
12112
    reset_mii;
12113
    // set wb slave response
12114
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
12115
 
12116
    max_tmp = 0;
12117
    min_tmp = 0;
12118
    num_of_frames = 0;
12119
    num_of_bd = 0;
12120
    // set maximum RX buffer descriptors (128) - must be set before RX enable
12121
    wbm_write(`ETH_TX_BD_NUM, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12122
    // enable RX, set full-duplex mode, NO receive small, NO correct IFG
12123
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12124
              `ETH_MODER_PRO | `ETH_MODER_BRO,
12125
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12126
    // prepare two packets of MAXFL length
12127
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12128
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
12129
    min_tmp = tmp[31:16];
12130
    st_data = 8'hAC;
12131
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
12132
    st_data = 8'h35;
12133
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
12134
    // check WB INT signal
12135
    if (wb_int !== 1'b0)
12136
    begin
12137
      test_fail("WB INT signal should not be set");
12138
      fail = fail + 1;
12139
    end
12140
 
12141
    // write to phy's control register for 10Mbps
12142
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
12143
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
12144
    speed = 10;
12145
 
12146
    i_length = (min_tmp - 4);
12147
    while (i_length <= (max_tmp - 4))
12148
    begin
12149
      // append CRC to packet
12150
      if (i_length[0] == 1'b0)
12151
        append_rx_crc (0, i_length, 1'b0, 1'b0);
12152
      else
12153
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
12154
      // choose generating carrier sense and collision
12155
      case (i_length[1:0])
12156
      2'h0: // Interrupt is generated
12157
      begin
12158
        // Reset_tx_bd nable interrupt generation
12159
        // unmask interrupts
12160
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
12161
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12162
        // not detect carrier sense in FD and no collision
12163
        eth_phy.no_carrier_sense_rx_fd_detect(0);
12164
        eth_phy.collision(0);
12165
      end
12166
      2'h1: // Interrupt is not generated
12167
      begin
12168
        // set_tx_bd enable interrupt generation
12169
        // mask interrupts
12170
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12171
        // detect carrier sense in FD and no collision
12172
        eth_phy.no_carrier_sense_rx_fd_detect(1);
12173
        eth_phy.collision(0);
12174
      end
12175
      2'h2: // Interrupt is not generated
12176
      begin
12177
        // set_tx_bd disable the interrupt generation
12178
        // unmask interrupts
12179
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
12180
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12181
        // not detect carrier sense in FD and set collision
12182
        eth_phy.no_carrier_sense_rx_fd_detect(0);
12183
        eth_phy.collision(1);
12184
      end
12185
      default: // 2'h3: // Interrupt is not generated
12186
      begin
12187
        // set_tx_bd disable the interrupt generation
12188
        // mask interrupts
12189
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12190
        // detect carrier sense in FD and set collision
12191
        eth_phy.no_carrier_sense_rx_fd_detect(1);
12192
        eth_phy.collision(1);
12193
      end
12194
      endcase
12195
      // first 8 frames are received with RX BD 0 (wrap bit on RX BD 0)
12196
      // number of all frames is 154 (146 without first 8)
12197
      if (num_of_frames < 8)
12198
      begin
12199
        case (i_length[1:0])
12200
        2'h0: // Interrupt is generated
12201
        begin
12202
          // enable interrupt generation
12203
          set_rx_bd(0, 0, 1'b1, (`MEMORY_BASE + i_length[1:0]));
12204
          // interrupts are unmasked
12205
        end
12206
        2'h1: // Interrupt is not generated
12207
        begin
12208
          // enable interrupt generation
12209
          set_rx_bd(0, 0, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
12210
          // interrupts are masked
12211
        end
12212
        2'h2: // Interrupt is not generated
12213
        begin
12214
          // disable interrupt generation
12215
          set_rx_bd(0, 0, 1'b0, (`MEMORY_BASE + i_length[1:0]));
12216
          // interrupts are unmasked
12217
        end
12218
        default: // 2'h3: // Interrupt is not generated
12219
        begin
12220
          // disable interrupt generation
12221
          set_rx_bd(0, 0, 1'b0, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
12222
          // interrupts are masked
12223
        end
12224
        endcase
12225
        // set wrap bit
12226
        set_rx_bd_wrap(0);
12227
      end
12228
      // after first 8 number of frames, 128 frames form RX BD 0 to 127 will be received
12229
      else if ((num_of_frames - 8) == 0)
12230
      begin
12231
        tmp_len = i_length; // length of frame
12232
        tmp_bd_num = 0; // RX BD number
12233
        while (tmp_bd_num < 128) // (tmp_len <= (max_tmp - 4)) - this is the last frame
12234
        begin
12235
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
12236
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
12237
          if (tmp_len[0] == 0)
12238
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], (`MEMORY_BASE + tmp_len[1:0]));
12239
          else
12240
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
12241
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
12242
          if ((tmp_len + 4) < (min_tmp + 128))
12243
            tmp_len = tmp_len + 1;
12244
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12245
            tmp_len = 256;
12246
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12247
            tmp_len = tmp_len + 128;
12248
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
12249
            tmp_len = max_tmp - (4 + 16);
12250
          else if ((tmp_len + 4) >= (max_tmp - 16))
12251
            tmp_len = tmp_len + 1;
12252
          // set RX BD number
12253
          tmp_bd_num = tmp_bd_num + 1;
12254
        end
12255
        // set wrap bit
12256
        set_rx_bd_wrap(127);
12257
      end
12258
      // after 128 + first 8 number of frames, 19 frames form RX BD 0 to 18 will be received
12259
      else if ((num_of_frames - 8) == 20) // 128
12260
      begin
12261
        tmp_len = tmp_len; // length of frame remaines from previous settings
12262
        tmp_bd_num = 0; // TX BD number
12263
        while (tmp_bd_num < 19) // (tmp_len <= (max_tmp - 4)) - this is the last frame
12264
        begin
12265
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
12266
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
12267
          if (tmp_len[0] == 0)
12268
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], (`MEMORY_BASE + tmp_len[1:0]));
12269
          else
12270
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
12271
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
12272
          if ((tmp_len + 4) < (min_tmp + 128))
12273
            tmp_len = tmp_len + 1;
12274
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12275
            tmp_len = 256;
12276
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12277
            tmp_len = tmp_len + 128;
12278
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
12279
            tmp_len = max_tmp - (4 + 16);
12280
          else if ((tmp_len + 4) >= (max_tmp - 16))
12281
            tmp_len = tmp_len + 1;
12282
          // set TX BD number
12283
          tmp_bd_num = tmp_bd_num + 1;
12284
        end
12285
      end
12286
      // set empty bit
12287
      if (num_of_frames < 8)
12288
        set_rx_bd_empty(0, 0);
12289
      else if ((num_of_frames - 8) < 128)
12290
        set_rx_bd_empty((num_of_frames - 8), (num_of_frames - 8));
12291
      else if ((num_of_frames - 136) < 19)
12292
        set_rx_bd_empty((num_of_frames - 136), (num_of_frames - 136));
12293
      // CHECK END OF RECEIVE
12294
      fork
12295
        begin
12296
          if (i_length[0] == 1'b0)
12297
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
12298
          else
12299
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
12300
          repeat(10) @(posedge mrx_clk);
12301
        end
12302
        begin
12303
          #1 check_rx_bd(num_of_bd, data);
12304
          if (i_length < min_tmp) // just first four
12305
          begin
12306
            while (data[15] === 1)
12307
            begin
12308
              #1 check_rx_bd(num_of_bd, data);
12309
              @(posedge wb_clk);
12310
            end
12311
            repeat (1) @(posedge wb_clk);
12312
          end
12313
          else
12314
          begin
12315
            wait (MRxDV === 1'b1); // start transmit
12316
            #1 check_rx_bd(num_of_bd, data);
12317
            if (data[15] !== 1)
12318
            begin
12319
              test_fail("Wrong buffer descriptor's ready bit read out from MAC");
12320
              fail = fail + 1;
12321
            end
12322
            wait (MRxDV === 1'b0); // end transmit
12323
            while (data[15] === 1)
12324
            begin
12325
              #1 check_rx_bd(num_of_bd, data);
12326
              @(posedge wb_clk);
12327
            end
12328
            repeat (1) @(posedge wb_clk);
12329
          end
12330
        end
12331
      join
12332
      // check length of a PACKET
12333
      if (data[31:16] != (i_length + 4))
12334
      begin
12335
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
12336
                        data[31:16], (i_length + 4));
12337
        test_fail("Wrong length of the packet out from PHY");
12338
        fail = fail + 1;
12339
      end
12340
      // checking in the following if statement is performed only for first and last 64 lengths
12341
      // check received RX packet data and CRC
12342
      if (i_length[0] == 1'b0)
12343
      begin
12344
        check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
12345
      end
12346
      else
12347
      begin
12348
        check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
12349
      end
12350
      if (tmp > 0)
12351
      begin
12352
        `TIME; $display("*E Wrong data of the received packet");
12353
        test_fail("Wrong data of the received packet");
12354
        fail = fail + 1;
12355
      end
12356
      // check WB INT signal
12357
      if (i_length[1:0] == 2'h0)
12358
      begin
12359
        if (wb_int !== 1'b1)
12360
        begin
12361
          `TIME; $display("*E WB INT signal should be set");
12362
          test_fail("WB INT signal should be set");
12363
          fail = fail + 1;
12364
        end
12365
      end
12366
      else
12367
      begin
12368
        if (wb_int !== 1'b0)
12369
        begin
12370
          `TIME; $display("*E WB INT signal should not be set");
12371
          test_fail("WB INT signal should not be set");
12372
          fail = fail + 1;
12373
        end
12374
      end
12375
      // check RX buffer descriptor of a packet
12376
      check_rx_bd(num_of_bd, data);
12377
      if (i_length[1] == 1'b0) // interrupt enabled
12378
      begin
12379
        if ( ((data[15:0] !== 16'h6000) && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
12380
             ((data[15:0] !== 16'h4000) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
12381
        begin
12382
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
12383
          test_fail("RX buffer descriptor status is not correct");
12384
          fail = fail + 1;
12385
        end
12386
      end
12387
      else // interrupt not enabled
12388
      begin
12389
        if ( ((data[15:0] !== 16'h2000)  && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
12390
             ((data[15:0] !== 16'h0000) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
12391
        begin
12392
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
12393
          test_fail("RX buffer descriptor status is not correct");
12394
          fail = fail + 1;
12395
        end
12396
      end
12397
      // clear first half of 8 frames from RX buffer descriptor 0
12398
      if (num_of_frames < 4)
12399
        clear_rx_bd(num_of_bd, num_of_bd);
12400
      // clear BD with wrap bit
12401
      if (num_of_frames == 140)
12402
        clear_rx_bd(127, 127);
12403
      // check interrupts
12404
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12405
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
12406
      begin
12407
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
12408
        begin
12409
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
12410
          test_fail("Interrupt Receive Buffer was not set");
12411
          fail = fail + 1;
12412
        end
12413
        if ((data & (~`ETH_INT_RXB)) !== 0)
12414
        begin
12415
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
12416
          test_fail("Other interrupts (except Receive Buffer) were set");
12417
          fail = fail + 1;
12418
        end
12419
      end
12420
      else
12421
      begin
12422
        if (data !== 0)
12423
        begin
12424
          `TIME; $display("*E Any of interrupts (except Receive Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
12425
          test_fail("Any of interrupts (except Receive Buffer) was set");
12426
          fail = fail + 1;
12427
        end
12428
      end
12429
      // clear interrupts
12430
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12431
      // check WB INT signal
12432
      if (wb_int !== 1'b0)
12433
      begin
12434
        test_fail("WB INT signal should not be set");
12435
        fail = fail + 1;
12436
      end
12437
      // INTERMEDIATE DISPLAYS
12438
      if ((i_length + 4) == (min_tmp + 7))
12439
      begin
12440
        // starting length is min_tmp, ending length is (min_tmp + 128)
12441
        $display("    receive small packets is NOT selected");
12442
        $display("    using only RX BD 0 out of 128 BDs assigned to RX (wrap at first BD - RX BD 0)");
12443
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
12444
                 min_tmp, (min_tmp + 7));
12445
        $display("    ->all packets were received on RX BD 0");
12446
        // reset receive small, remain the rest
12447
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12448
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12449
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12450
      end
12451
      else if ((i_length + 4) == (min_tmp + 128))
12452
      begin
12453
        // starting length is min_tmp, ending length is (min_tmp + 128)
12454
        $display("    receive small packets is NOT selected");
12455
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12456
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
12457
                 (min_tmp + 8), (min_tmp + 128));
12458
        $display("    ->packets were received on RX BD %0d to RX BD %0d respectively",
12459
                 1'b0, num_of_bd);
12460
        tmp_bd = num_of_bd + 1;
12461
        // set receive small, remain the rest
12462
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12463
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12464
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12465
      end
12466
      else if ((i_length + 4) == (max_tmp - 16))
12467
      begin
12468
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
12469
        $display("    receive small packets is selected");
12470
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12471
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
12472
                 (min_tmp + 64 + 128), tmp_data);
12473
        if (tmp_bd > num_of_bd)
12474
          $display("    ->packets were received from RX BD %0d to RX BD 127 and from RX BD 0 to RX BD %0d respectively",
12475
                   tmp_bd, num_of_bd);
12476
        else
12477
          $display("    ->packets were received from RX BD %0d to RX BD %0d respectively",
12478
                   tmp_bd, num_of_bd);
12479
        tmp_bd = num_of_bd + 1;
12480
        // reset receive small, remain the rest
12481
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12482
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12483
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12484
      end
12485
      else if ((i_length + 4) == max_tmp)
12486
      begin
12487
        $display("    receive small packets is NOT selected");
12488
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12489
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
12490
                 (max_tmp - (4 + 16)), max_tmp);
12491
        if (tmp_bd > num_of_bd)
12492
          $display("    ->packets were received from RX BD %0d to RX BD 127 and from RX BD 0 to RX BD %0d respectively",
12493
                   tmp_bd, num_of_bd);
12494
        else
12495
          $display("    ->packets were received from RX BD %0d to RX BD %0d respectively",
12496
                   tmp_bd, num_of_bd);
12497
      end
12498
      // set length (loop variable)
12499
      if ((i_length + 4) < (min_tmp + 128))
12500
        i_length = i_length + 1;
12501
      else if ( ((i_length + 4) == (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
12502
        i_length = 256;
12503
      else if ( ((i_length + 4) > (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
12504
      begin
12505
        i_length = i_length + 128;
12506
        tmp_data = i_length + 4; // last tmp_data is ending length
12507
      end
12508
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
12509
        i_length = max_tmp - (4 + 16);
12510
      else if ((i_length + 4) >= (max_tmp - 16))
12511
        i_length = i_length + 1;
12512
      else
12513
      begin
12514
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
12515
        #10 $stop;
12516
      end
12517
      // the number of frame transmitted
12518
      num_of_frames = num_of_frames + 1;
12519
      if ((num_of_frames <= 8) || ((num_of_frames - 8) == 128))
12520
        num_of_bd = 0;
12521
      else
12522
        num_of_bd = num_of_bd + 1;
12523
    end
12524
    // disable RX
12525
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12526
              `ETH_MODER_PRO | `ETH_MODER_BRO,
12527
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12528
    @(posedge wb_clk);
12529
    if(fail == 0)
12530
      test_ok;
12531
    else
12532
      fail = 0;
12533
  end
12534
 
12535
 
12536
  ////////////////////////////////////////////////////////////////////
12537
  ////                                                            ////
12538
  ////  Test receive packets form MINFL to MAXFL sizes at         ////
12539
  ////  maximum RX buffer decriptors ( 100Mbps ).                 ////
12540
  ////                                                            ////
12541
  ////////////////////////////////////////////////////////////////////
12542 243 tadejm
  if (test_num == 7) // 
12543 209 tadejm
  begin
12544 243 tadejm
    // TEST 7: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 100Mbps )
12545
    test_name = "TEST 7: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 100Mbps )";
12546
    `TIME; $display("  TEST 7: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 100Mbps )");
12547 209 tadejm
 
12548
    // reset MAC registers
12549
    hard_reset;
12550
    // reset MAC and MII LOGIC with soft reset
12551
    reset_mac;
12552
    reset_mii;
12553
    // set wb slave response
12554
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
12555
 
12556
    max_tmp = 0;
12557
    min_tmp = 0;
12558
    num_of_frames = 0;
12559
    num_of_bd = 0;
12560
    // set maximum RX buffer descriptors (128) - must be set before RX enable
12561
    wbm_write(`ETH_TX_BD_NUM, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12562
    // enable RX, set full-duplex mode, NO receive small, NO correct IFG
12563
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12564
              `ETH_MODER_PRO | `ETH_MODER_BRO,
12565
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12566
    // prepare two packets of MAXFL length
12567
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12568
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
12569
    min_tmp = tmp[31:16];
12570
    st_data = 8'hAC;
12571
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
12572
    st_data = 8'h35;
12573
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
12574
    // check WB INT signal
12575
    if (wb_int !== 1'b0)
12576
    begin
12577
      test_fail("WB INT signal should not be set");
12578
      fail = fail + 1;
12579
    end
12580
 
12581
    // write to phy's control register for 100Mbps
12582
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
12583
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
12584
    speed = 100;
12585
 
12586
    i_length = (min_tmp - 4);
12587
    while (i_length <= (max_tmp - 4))
12588
    begin
12589
      // append CRC to packet
12590
      if (i_length[0] == 1'b0)
12591
        append_rx_crc (0, i_length, 1'b0, 1'b0);
12592
      else
12593
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
12594
      // choose generating carrier sense and collision
12595
      case (i_length[1:0])
12596
      2'h0: // Interrupt is generated
12597
      begin
12598
        // Reset_tx_bd nable interrupt generation
12599
        // unmask interrupts
12600
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
12601
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12602
        // not detect carrier sense in FD and no collision
12603
        eth_phy.no_carrier_sense_rx_fd_detect(0);
12604
        eth_phy.collision(0);
12605
      end
12606
      2'h1: // Interrupt is not generated
12607
      begin
12608
        // set_tx_bd enable interrupt generation
12609
        // mask interrupts
12610
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12611
        // detect carrier sense in FD and no collision
12612
        eth_phy.no_carrier_sense_rx_fd_detect(1);
12613
        eth_phy.collision(0);
12614
      end
12615
      2'h2: // Interrupt is not generated
12616
      begin
12617
        // set_tx_bd disable the interrupt generation
12618
        // unmask interrupts
12619
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
12620
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12621
        // not detect carrier sense in FD and set collision
12622
        eth_phy.no_carrier_sense_rx_fd_detect(0);
12623
        eth_phy.collision(1);
12624
      end
12625
      default: // 2'h3: // Interrupt is not generated
12626
      begin
12627
        // set_tx_bd disable the interrupt generation
12628
        // mask interrupts
12629
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12630
        // detect carrier sense in FD and set collision
12631
        eth_phy.no_carrier_sense_rx_fd_detect(1);
12632
        eth_phy.collision(1);
12633
      end
12634
      endcase
12635
      // first 8 frames are received with RX BD 0 (wrap bit on RX BD 0)
12636
      // number of all frames is 154 (146 without first 8)
12637
      if (num_of_frames < 8)
12638
      begin
12639
        case (i_length[1:0])
12640
        2'h0: // Interrupt is generated
12641
        begin
12642
          // enable interrupt generation
12643
          set_rx_bd(0, 0, 1'b1, (`MEMORY_BASE + i_length[1:0]));
12644
          // interrupts are unmasked
12645
        end
12646
        2'h1: // Interrupt is not generated
12647
        begin
12648
          // enable interrupt generation
12649
          set_rx_bd(0, 0, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
12650
          // interrupts are masked
12651
        end
12652
        2'h2: // Interrupt is not generated
12653
        begin
12654
          // disable interrupt generation
12655
          set_rx_bd(0, 0, 1'b0, (`MEMORY_BASE + i_length[1:0]));
12656
          // interrupts are unmasked
12657
        end
12658
        default: // 2'h3: // Interrupt is not generated
12659
        begin
12660
          // disable interrupt generation
12661
          set_rx_bd(0, 0, 1'b0, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
12662
          // interrupts are masked
12663
        end
12664
        endcase
12665
        // set wrap bit
12666
        set_rx_bd_wrap(0);
12667
      end
12668
      // after first 8 number of frames, 128 frames form RX BD 0 to 127 will be received
12669
      else if ((num_of_frames - 8) == 0)
12670
      begin
12671
        tmp_len = i_length; // length of frame
12672
        tmp_bd_num = 0; // RX BD number
12673
        while (tmp_bd_num < 128) // (tmp_len <= (max_tmp - 4)) - this is the last frame
12674
        begin
12675
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
12676
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
12677
          if (tmp_len[0] == 0)
12678
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], (`MEMORY_BASE + tmp_len[1:0]));
12679
          else
12680
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
12681
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
12682
          if ((tmp_len + 4) < (min_tmp + 128))
12683
            tmp_len = tmp_len + 1;
12684
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12685
            tmp_len = 256;
12686
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12687
            tmp_len = tmp_len + 128;
12688
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
12689
            tmp_len = max_tmp - (4 + 16);
12690
          else if ((tmp_len + 4) >= (max_tmp - 16))
12691
            tmp_len = tmp_len + 1;
12692
          // set RX BD number
12693
          tmp_bd_num = tmp_bd_num + 1;
12694
        end
12695
        // set wrap bit
12696
        set_rx_bd_wrap(127);
12697
      end
12698
      // after 128 + first 8 number of frames, 19 frames form RX BD 0 to 18 will be received
12699
      else if ((num_of_frames - 8) == 20) // 128
12700
      begin
12701
        tmp_len = tmp_len; // length of frame remaines from previous settings
12702
        tmp_bd_num = 0; // TX BD number
12703
        while (tmp_bd_num < 19) // (tmp_len <= (max_tmp - 4)) - this is the last frame
12704
        begin
12705
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
12706
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
12707
          if (tmp_len[0] == 0)
12708
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], (`MEMORY_BASE + tmp_len[1:0]));
12709
          else
12710
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
12711
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
12712
          if ((tmp_len + 4) < (min_tmp + 128))
12713
            tmp_len = tmp_len + 1;
12714
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12715
            tmp_len = 256;
12716
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12717
            tmp_len = tmp_len + 128;
12718
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
12719
            tmp_len = max_tmp - (4 + 16);
12720
          else if ((tmp_len + 4) >= (max_tmp - 16))
12721
            tmp_len = tmp_len + 1;
12722
          // set TX BD number
12723
          tmp_bd_num = tmp_bd_num + 1;
12724
        end
12725
      end
12726
      // set empty bit
12727
      if (num_of_frames < 8)
12728
        set_rx_bd_empty(0, 0);
12729
      else if ((num_of_frames - 8) < 128)
12730
        set_rx_bd_empty((num_of_frames - 8), (num_of_frames - 8));
12731
      else if ((num_of_frames - 136) < 19)
12732
        set_rx_bd_empty((num_of_frames - 136), (num_of_frames - 136));
12733
      // CHECK END OF RECEIVE
12734
      fork
12735
        begin
12736
          if (i_length[0] == 1'b0)
12737
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
12738
          else
12739
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
12740
          repeat(10) @(posedge mrx_clk);
12741
        end
12742
        begin
12743
          #1 check_rx_bd(num_of_bd, data);
12744
          if (i_length < min_tmp) // just first four
12745
          begin
12746
            while (data[15] === 1)
12747
            begin
12748
              #1 check_rx_bd(num_of_bd, data);
12749
              @(posedge wb_clk);
12750
            end
12751
            repeat (1) @(posedge wb_clk);
12752
          end
12753
          else
12754
          begin
12755
            wait (MRxDV === 1'b1); // start transmit
12756
            #1 check_rx_bd(num_of_bd, data);
12757
            if (data[15] !== 1)
12758
            begin
12759
              test_fail("Wrong buffer descriptor's ready bit read out from MAC");
12760
              fail = fail + 1;
12761
            end
12762
            wait (MRxDV === 1'b0); // end transmit
12763
            while (data[15] === 1)
12764
            begin
12765
              #1 check_rx_bd(num_of_bd, data);
12766
              @(posedge wb_clk);
12767
            end
12768
            repeat (1) @(posedge wb_clk);
12769
          end
12770
        end
12771
      join
12772
      // check length of a PACKET
12773
      if (data[31:16] != (i_length + 4))
12774
      begin
12775
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
12776
                        data[31:16], (i_length + 4));
12777
        test_fail("Wrong length of the packet out from PHY");
12778
        fail = fail + 1;
12779
      end
12780
      // check received RX packet data and CRC
12781
      if (i_length[0] == 1'b0)
12782
      begin
12783
        check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
12784
      end
12785
      else
12786
      begin
12787
        check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
12788
      end
12789
      if (tmp > 0)
12790
      begin
12791
        `TIME; $display("*E Wrong data of the received packet");
12792
        test_fail("Wrong data of the received packet");
12793
        fail = fail + 1;
12794
      end
12795
      // check WB INT signal
12796
      if (i_length[1:0] == 2'h0)
12797
      begin
12798
        if (wb_int !== 1'b1)
12799
        begin
12800
          `TIME; $display("*E WB INT signal should be set");
12801
          test_fail("WB INT signal should be set");
12802
          fail = fail + 1;
12803
        end
12804
      end
12805
      else
12806
      begin
12807
        if (wb_int !== 1'b0)
12808
        begin
12809
          `TIME; $display("*E WB INT signal should not be set");
12810
          test_fail("WB INT signal should not be set");
12811
          fail = fail + 1;
12812
        end
12813
      end
12814
      // check RX buffer descriptor of a packet
12815
      check_rx_bd(num_of_bd, data);
12816
      if (i_length[1] == 1'b0) // interrupt enabled
12817
      begin
12818
        if ( ((data[15:0] !== 16'h6000) && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
12819
             ((data[15:0] !== 16'h4000) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
12820
        begin
12821
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
12822
          test_fail("RX buffer descriptor status is not correct");
12823
          fail = fail + 1;
12824
        end
12825
      end
12826
      else // interrupt not enabled
12827
      begin
12828
        if ( ((data[15:0] !== 16'h2000)  && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
12829
             ((data[15:0] !== 16'h0000) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
12830
        begin
12831
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
12832
          test_fail("RX buffer descriptor status is not correct");
12833
          fail = fail + 1;
12834
        end
12835
      end
12836
      // clear first half of 8 frames from RX buffer descriptor 0
12837
      if (num_of_frames < 4)
12838
        clear_rx_bd(num_of_bd, num_of_bd);
12839
      // clear BD with wrap bit
12840
      if (num_of_frames == 140)
12841
        clear_rx_bd(127, 127);
12842
      // check interrupts
12843
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12844
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
12845
      begin
12846
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
12847
        begin
12848
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
12849
          test_fail("Interrupt Receive Buffer was not set");
12850
          fail = fail + 1;
12851
        end
12852
        if ((data & (~`ETH_INT_RXB)) !== 0)
12853
        begin
12854
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
12855
          test_fail("Other interrupts (except Receive Buffer) were set");
12856
          fail = fail + 1;
12857
        end
12858
      end
12859
      else
12860
      begin
12861
        if (data !== 0)
12862
        begin
12863
          `TIME; $display("*E Any of interrupts (except Receive Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
12864
          test_fail("Any of interrupts (except Receive Buffer) was set");
12865
          fail = fail + 1;
12866
        end
12867
      end
12868
      // clear interrupts
12869
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12870
      // check WB INT signal
12871
      if (wb_int !== 1'b0)
12872
      begin
12873
        test_fail("WB INT signal should not be set");
12874
        fail = fail + 1;
12875
      end
12876
      // INTERMEDIATE DISPLAYS
12877
      if ((i_length + 4) == (min_tmp + 7))
12878
      begin
12879
        // starting length is min_tmp, ending length is (min_tmp + 128)
12880
        $display("    receive small packets is NOT selected");
12881
        $display("    using only RX BD 0 out of 128 BDs assigned to RX (wrap at first BD - RX BD 0)");
12882
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
12883
                 min_tmp, (min_tmp + 7));
12884
        $display("    ->all packets were received on RX BD 0");
12885
        // reset receive small, remain the rest
12886
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12887
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12888
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12889
      end
12890
      else if ((i_length + 4) == (min_tmp + 128))
12891
      begin
12892
        // starting length is min_tmp, ending length is (min_tmp + 128)
12893
        $display("    receive small packets is NOT selected");
12894
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12895
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
12896
                 (min_tmp + 8), (min_tmp + 128));
12897
        $display("    ->packets were received on RX BD %0d to RX BD %0d respectively",
12898
                 1'b0, num_of_bd);
12899
        tmp_bd = num_of_bd + 1;
12900
        // set receive small, remain the rest
12901
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12902
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12903
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12904
      end
12905
      else if ((i_length + 4) == (max_tmp - 16))
12906
      begin
12907
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
12908
        $display("    receive small packets is selected");
12909
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12910
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
12911
                 (min_tmp + 64 + 128), tmp_data);
12912
        if (tmp_bd > num_of_bd)
12913
          $display("    ->packets were received from RX BD %0d to RX BD 127 and from RX BD 0 to RX BD %0d respectively",
12914
                   tmp_bd, num_of_bd);
12915
        else
12916
          $display("    ->packets were received from RX BD %0d to RX BD %0d respectively",
12917
                   tmp_bd, num_of_bd);
12918
        tmp_bd = num_of_bd + 1;
12919
        // reset receive small, remain the rest
12920
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12921
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12922
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12923
      end
12924
      else if ((i_length + 4) == max_tmp)
12925
      begin
12926
        $display("    receive small packets is NOT selected");
12927
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12928
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
12929
                 (max_tmp - (4 + 16)), max_tmp);
12930
        if (tmp_bd > num_of_bd)
12931
          $display("    ->packets were received from RX BD %0d to RX BD 127 and from RX BD 0 to RX BD %0d respectively",
12932
                   tmp_bd, num_of_bd);
12933
        else
12934
          $display("    ->packets were received from RX BD %0d to RX BD %0d respectively",
12935
                   tmp_bd, num_of_bd);
12936
      end
12937
      // set length (loop variable)
12938
      if ((i_length + 4) < (min_tmp + 128))
12939
        i_length = i_length + 1;
12940
      else if ( ((i_length + 4) == (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
12941
        i_length = 256;
12942
      else if ( ((i_length + 4) > (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
12943
      begin
12944
        i_length = i_length + 128;
12945
        tmp_data = i_length + 4; // last tmp_data is ending length
12946
      end
12947
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
12948
        i_length = max_tmp - (4 + 16);
12949
      else if ((i_length + 4) >= (max_tmp - 16))
12950
        i_length = i_length + 1;
12951
      else
12952
      begin
12953
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
12954
        #10 $stop;
12955
      end
12956
      // the number of frame transmitted
12957
      num_of_frames = num_of_frames + 1;
12958
      if ((num_of_frames <= 8) || ((num_of_frames - 8) == 128))
12959
        num_of_bd = 0;
12960
      else
12961
        num_of_bd = num_of_bd + 1;
12962
    end
12963
    // disable RX
12964
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12965
              `ETH_MODER_PRO | `ETH_MODER_BRO,
12966
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12967
    @(posedge wb_clk);
12968
    if(fail == 0)
12969
      test_ok;
12970
    else
12971
      fail = 0;
12972
  end
12973
 
12974
 
12975
  ////////////////////////////////////////////////////////////////////
12976
  ////                                                            ////
12977 243 tadejm
  ////  Test receive packets form 0 to (MINFL + 12) sizes at       ////
12978 209 tadejm
  ////  8 RX buffer decriptors ( 10Mbps ).                        ////
12979
  ////                                                            ////
12980
  ////////////////////////////////////////////////////////////////////
12981 243 tadejm
  if (test_num == 8) // 
12982 209 tadejm
  begin
12983 243 tadejm
    // TEST 8: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 10Mbps )
12984
    test_name = "TEST 8: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 10Mbps )";
12985
    `TIME; $display("  TEST 8: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 10Mbps )");
12986 209 tadejm
 
12987
    // reset MAC registers
12988
    hard_reset;
12989
    // reset MAC and MII LOGIC with soft reset
12990
    reset_mac;
12991
    reset_mii;
12992
    // set wb slave response
12993
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
12994
 
12995
    max_tmp = 0;
12996
    min_tmp = 0;
12997
    // set 8 RX buffer descriptors (120 - 127) - must be set before RX enable
12998
    wbm_write(`ETH_TX_BD_NUM, 32'h78, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12999
    // enable RX, set full-duplex mode, receive small, NO correct IFG
13000
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
13001
              `ETH_MODER_PRO | `ETH_MODER_BRO,
13002
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13003
    // prepare two packets of MAXFL length
13004
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13005
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
13006
    min_tmp = tmp[31:16];
13007
    st_data = 8'hAC;
13008
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
13009
    st_data = 8'h35;
13010
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
13011
    // check WB INT signal
13012
    if (wb_int !== 1'b0)
13013
    begin
13014
      test_fail("WB INT signal should not be set");
13015
      fail = fail + 1;
13016
    end
13017 243 tadejm
    // unmask interrupts
13018
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
13019
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13020 209 tadejm
 
13021
    // write to phy's control register for 10Mbps
13022
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
13023
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
13024
    speed = 10;
13025
 
13026 243 tadejm
    frame_ended = 0;
13027
    num_of_frames = 0;// 0; // 10;
13028
    num_of_bd = 120;
13029
    i_length = 0 - 4;// (0 - 4); // 6; // 4 less due to CRC
13030
    while ((i_length + 4) < 78) // (min_tmp - 4))
13031 209 tadejm
    begin
13032 243 tadejm
      // append CRC to packet
13033
      if ((i_length[0] == 1'b0) && (num_of_frames > 4))
13034
        append_rx_crc (0, i_length, 1'b0, 1'b0);
13035
      else if (num_of_frames > 4)
13036
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
13037 209 tadejm
      // choose generating carrier sense and collision
13038
      case (i_length[1:0])
13039 243 tadejm
      2'h0:
13040 209 tadejm
      begin
13041
        // not detect carrier sense in FD and no collision
13042 243 tadejm
        eth_phy.no_carrier_sense_rx_fd_detect(0);
13043 209 tadejm
        eth_phy.collision(0);
13044
      end
13045 243 tadejm
      2'h1:
13046 209 tadejm
      begin
13047
        // detect carrier sense in FD and no collision
13048 243 tadejm
        eth_phy.no_carrier_sense_rx_fd_detect(1);
13049 209 tadejm
        eth_phy.collision(0);
13050
      end
13051 243 tadejm
      2'h2:
13052 209 tadejm
      begin
13053
        // not detect carrier sense in FD and set collision
13054 243 tadejm
        eth_phy.no_carrier_sense_rx_fd_detect(0);
13055 209 tadejm
        eth_phy.collision(1);
13056
      end
13057 243 tadejm
      default: // 2'h3:
13058 209 tadejm
      begin
13059
        // detect carrier sense in FD and set collision
13060 243 tadejm
        eth_phy.no_carrier_sense_rx_fd_detect(1);
13061 209 tadejm
        eth_phy.collision(1);
13062
      end
13063
      endcase
13064
      #1;
13065 243 tadejm
      // first 10 frames are received with RX BD 120 (wrap bit on RX BD 120)
13066 209 tadejm
      if (num_of_frames <= 9)
13067
      begin
13068 243 tadejm
        case (i_length[1:0])
13069
        2'h0: // Interrupt is generated
13070
        begin
13071
          // enable interrupt generation
13072
          set_rx_bd(120, 120, 1'b1, (`MEMORY_BASE + i_length[1:0]));
13073
        end
13074
        2'h1: // Interrupt is generated
13075
        begin
13076
          // enable interrupt generation
13077
          set_rx_bd(120, 120, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
13078
        end
13079
        2'h2: // Interrupt is generated
13080
        begin
13081
          // enable interrupt generation
13082
          set_rx_bd(120, 120, 1'b1, (`MEMORY_BASE + i_length[1:0]));
13083
        end
13084
        default: // 2'h3: // Interrupt is generated
13085
        begin
13086
          // enable interrupt generation
13087
          set_rx_bd(120, 120, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
13088
        end
13089
        endcase
13090 209 tadejm
        // set wrap bit
13091 243 tadejm
        set_rx_bd_wrap(120);
13092 209 tadejm
      end
13093 243 tadejm
      // 10 <= num_of_frames < 18 => wrap set to TX BD 123
13094 209 tadejm
      else if ((num_of_frames == 10) || (num_of_frames == 14))
13095
      begin
13096
        tmp_len = i_length; // length of frame
13097 243 tadejm
        tmp_bd_num = 120; // RX BD number
13098
        while (tmp_bd_num < 124) // 
13099 209 tadejm
        begin
13100
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13101
          if (tmp_len[0] == 0)
13102 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13103 209 tadejm
          else
13104 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13105
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13106 209 tadejm
          tmp_len = tmp_len + 1;
13107 243 tadejm
          // set RX BD number
13108 209 tadejm
          tmp_bd_num = tmp_bd_num + 1;
13109
        end
13110
        // set wrap bit
13111 243 tadejm
        set_rx_bd_wrap(123);
13112 209 tadejm
      end
13113 243 tadejm
      // 18 <= num_of_frames < 28 => wrap set to RX BD 124
13114 209 tadejm
      else if ((num_of_frames == 18) || (num_of_frames == 23))
13115
      begin
13116
        tmp_len = i_length; // length of frame
13117 243 tadejm
        tmp_bd_num = 120; // RX BD number
13118
        while (tmp_bd_num < 125) // 
13119 209 tadejm
        begin
13120
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13121
          if (tmp_len[0] == 0)
13122 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13123 209 tadejm
          else
13124 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13125
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13126 209 tadejm
          tmp_len = tmp_len + 1;
13127 243 tadejm
          // set RX BD number
13128 209 tadejm
          tmp_bd_num = tmp_bd_num + 1;
13129
        end
13130
        // set wrap bit
13131 243 tadejm
        set_rx_bd_wrap(124);
13132 209 tadejm
      end
13133 243 tadejm
      // 28 <= num_of_frames < 40 => wrap set to RX BD 125
13134 209 tadejm
      else if ((num_of_frames == 28) || (num_of_frames == 34))
13135
      begin
13136
        tmp_len = i_length; // length of frame
13137 243 tadejm
        tmp_bd_num = 120; // RX BD number
13138
        while (tmp_bd_num < 126) // 
13139 209 tadejm
        begin
13140
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13141
          if (tmp_len[0] == 0)
13142 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13143 209 tadejm
          else
13144 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13145
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13146 209 tadejm
          tmp_len = tmp_len + 1;
13147 243 tadejm
          // set RX BD number
13148 209 tadejm
          tmp_bd_num = tmp_bd_num + 1;
13149
        end
13150
        // set wrap bit
13151 243 tadejm
        set_rx_bd_wrap(125);
13152 209 tadejm
      end
13153 243 tadejm
      // 40 <= num_of_frames < 54 => wrap set to RX BD 126
13154 209 tadejm
      else if ((num_of_frames == 40) || (num_of_frames == 47))
13155
      begin
13156
        tmp_len = i_length; // length of frame
13157 243 tadejm
        tmp_bd_num = 120; // RX BD number
13158
        while (tmp_bd_num < 127) // 
13159 209 tadejm
        begin
13160
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13161
          if (tmp_len[0] == 0)
13162 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13163 209 tadejm
          else
13164 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13165
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13166 209 tadejm
          tmp_len = tmp_len + 1;
13167 243 tadejm
          // set RX BD number
13168 209 tadejm
          tmp_bd_num = tmp_bd_num + 1;
13169
        end
13170
        // set wrap bit
13171 243 tadejm
        set_rx_bd_wrap(126);
13172 209 tadejm
      end
13173 243 tadejm
      // 54 <= num_of_frames < 70 => wrap set to RX BD 127
13174 209 tadejm
      else if ((num_of_frames == 54) || (num_of_frames == 62))
13175
      begin
13176
        tmp_len = i_length; // length of frame
13177 243 tadejm
        tmp_bd_num = 120; // RX BD number
13178
        while (tmp_bd_num < 128) // 
13179 209 tadejm
        begin
13180
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13181
          if (tmp_len[0] == 0)
13182 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13183 209 tadejm
          else
13184 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13185
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13186 209 tadejm
          tmp_len = tmp_len + 1;
13187 243 tadejm
          // set RX BD number
13188 209 tadejm
          tmp_bd_num = tmp_bd_num + 1;
13189
        end
13190
        // set wrap bit
13191 243 tadejm
        set_rx_bd_wrap(127);
13192 209 tadejm
      end
13193 243 tadejm
      // 70 <= num_of_frames < 78 => wrap set to RX BD 127
13194
      else if (num_of_frames == 70)
13195
      begin
13196
        tmp_len = i_length; // length of frame
13197
        tmp_bd_num = 120; // RX BD number
13198
        while (tmp_bd_num < 128) // 
13199
        begin
13200
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13201
          if (tmp_len[0] == 0)
13202
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13203
          else
13204
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13205
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13206
          tmp_len = tmp_len + 1;
13207
          // set RX BD number
13208
          tmp_bd_num = tmp_bd_num + 1;
13209
        end
13210
        // set wrap bit
13211
        set_rx_bd_wrap(127);
13212
      end
13213 209 tadejm
      #1;
13214 243 tadejm
      // SET empty bit
13215 209 tadejm
      if (num_of_frames < 10)
13216 243 tadejm
        set_rx_bd_empty(120, 120);
13217 209 tadejm
      else if (num_of_frames < 14)
13218 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 10), (120 + num_of_frames - 10));
13219 209 tadejm
      else if (num_of_frames < 18)
13220 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 14), (120 + num_of_frames - 14));
13221 209 tadejm
      else if (num_of_frames < 23)
13222 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 18), (120 + num_of_frames - 18));
13223 209 tadejm
      else if (num_of_frames < 28)
13224 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 23), (120 + num_of_frames - 23));
13225 209 tadejm
      else if (num_of_frames < 34)
13226 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 28), (120 + num_of_frames - 28));
13227 209 tadejm
      else if (num_of_frames < 40)
13228 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 34), (120 + num_of_frames - 34));
13229 209 tadejm
      else if (num_of_frames < 47)
13230 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 40), (120 + num_of_frames - 40));
13231 209 tadejm
      else if (num_of_frames < 54)
13232 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 47), (120 + num_of_frames - 47));
13233 209 tadejm
      else if (num_of_frames < 62)
13234 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 54), (120 + num_of_frames - 54));
13235 209 tadejm
      else if (num_of_frames < 70)
13236 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 62), (120 + num_of_frames - 62));
13237
      else if (num_of_frames < 78)
13238
        set_rx_bd_empty((120 + num_of_frames - 70), (120 + num_of_frames - 70));
13239
      // CHECK END OF RECEIVE
13240 252 tadejm
      // receive just preamble between some packets
13241
      if ((num_of_frames == 0) || (num_of_frames == 4) || (num_of_frames == 9))
13242
      begin
13243
        #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h6, 8'h55, 0, 0, 1'b0);
13244
        @(posedge mrx_clk);
13245
        wait (MRxDV === 1'b0); // end receive
13246
        repeat(10) @(posedge mrx_clk);
13247
        repeat(15) @(posedge wb_clk);
13248
      end
13249
      // receiving frames and checking end of them
13250 243 tadejm
      frame_ended = 0;
13251
      check_frame = 0;
13252 209 tadejm
      fork
13253
        begin
13254 243 tadejm
          if (i_length[0] == 1'b0)
13255
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
13256
          else
13257
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
13258
          repeat(10) @(posedge mrx_clk);
13259
        end
13260
        begin: fr_end1
13261
          wait (MRxDV === 1'b1); // start receive
13262
          #1 check_rx_bd(num_of_bd, data);
13263
          if (data[15] !== 1)
13264 209 tadejm
          begin
13265 243 tadejm
            test_fail("Wrong buffer descriptor's empty bit read out from MAC");
13266
            fail = fail + 1;
13267 209 tadejm
          end
13268 243 tadejm
          wait (MRxDV === 1'b0); // end receive
13269
          while ((data[15] === 1) && (check_frame == 0))
13270
          begin
13271
            #1 check_rx_bd(num_of_bd, data);
13272
            @(posedge wb_clk);
13273
          end
13274
          if (data[15] === 0)
13275
            frame_ended = 1;
13276
          repeat (1) @(posedge wb_clk);
13277 209 tadejm
        end
13278 243 tadejm
        begin
13279
          wait (MRxDV === 1'b1); // start receive
13280
          wait (MRxDV === 1'b0); // end receive
13281
          repeat(10) @(posedge mrx_clk);
13282
          repeat(15) @(posedge wb_clk);
13283
          check_frame = 1;
13284
        end
13285
      join
13286
      // check length of a PACKET
13287
      if ( ((data[31:16] != (i_length + 4)) && (num_of_frames >= 3)) ||
13288
           ((data[31:16] != 0) && (num_of_frames < 3)) )
13289
      begin
13290
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
13291
                        data[31:16], (i_length + 4));
13292
        test_fail("Wrong length of the packet out from PHY");
13293
        fail = fail + 1;
13294
      end
13295
      // check received RX packet data and CRC
13296
      if ((frame_ended == 1) && (num_of_frames >= 5)) // 5 bytes is minimum size without CRC error, since
13297
      begin                                           // CRC has 4 bytes for itself
13298
        if (i_length[0] == 1'b0)
13299
        begin
13300
          check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
13301
        end
13302 209 tadejm
        else
13303
        begin
13304 243 tadejm
          check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
13305 209 tadejm
        end
13306 243 tadejm
        if (tmp > 0)
13307
        begin
13308
          `TIME; $display("*E Wrong data of the received packet");
13309
          test_fail("Wrong data of the received packet");
13310
          fail = fail + 1;
13311
        end
13312 209 tadejm
      end
13313 243 tadejm
      // check WB INT signal
13314
      if (num_of_frames >= 3) // Frames smaller than 3 are not received.
13315
      begin                   // Frames greater then 5 always cause an interrupt (Frame received)
13316
        if (wb_int !== 1'b1)  // Frames with length 3 or 4 always cause an interrupt (CRC error)
13317
        begin
13318
          `TIME; $display("*E WB INT signal should be set");
13319
          test_fail("WB INT signal should be set");
13320
          fail = fail + 1;
13321
        end
13322
      end
13323
      else
13324
      begin
13325
        if (wb_int !== 1'b0)
13326
        begin
13327
          `TIME; $display("*E WB INT signal should not be set");
13328
          test_fail("WB INT signal should not be set");
13329
          fail = fail + 1;
13330
        end
13331
      end
13332
      // check RX buffer descriptor of a packet
13333 254 mohor
      // check RX buffer descriptor of a packet
13334 243 tadejm
      if (num_of_frames >= min_tmp)
13335
      begin
13336 254 mohor
        if ( (data[15:0] !== 16'h6080) && // wrap bit
13337
             (data[15:0] !== 16'h4080) ) // without wrap bit
13338 243 tadejm
        begin
13339
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13340
          test_fail("RX buffer descriptor status is not correct");
13341
          fail = fail + 1;
13342
        end
13343
      end
13344 254 mohor
      else if (num_of_frames > 6)
13345 243 tadejm
      begin
13346 254 mohor
        if ( (data[15:0] !== 16'h6084) && // wrap bit
13347
             (data[15:0] !== 16'h4084) ) // without wrap bit
13348
        begin
13349
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13350
          test_fail("RX buffer descriptor status is not correct");
13351
          fail = fail + 1;
13352
        end
13353
      end
13354
      else if (num_of_frames > 4) // MAC does not recognize Dest. ADDR. for lengths 5, 6 => no MISS
13355
      begin
13356 243 tadejm
        if ( (data[15:0] !== 16'h6004) && // wrap bit
13357
             (data[15:0] !== 16'h4004) ) // without wrap bit
13358
        begin
13359
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13360
          test_fail("RX buffer descriptor status is not correct");
13361
          fail = fail + 1;
13362
        end
13363
      end
13364 254 mohor
      else if (num_of_frames > 2) // MAC does not recognize Dest. ADDR. for length 3, 4 => no MISS, CRC ERROR
13365 243 tadejm
      begin
13366
        if ( (data[15:0] !== 16'h6006) && // wrap bit
13367
             (data[15:0] !== 16'h4006) ) // without wrap bit
13368
        begin
13369
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13370
          test_fail("RX buffer descriptor status is not correct");
13371
          fail = fail + 1;
13372
        end
13373
      end
13374
      else
13375
      begin
13376
        if (data[15] !== 1'b1)
13377
        begin
13378
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13379
          test_fail("RX buffer descriptor status is not correct");
13380
          fail = fail + 1;
13381
        end
13382
      end
13383
      // check interrupts
13384
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13385 209 tadejm
      if (num_of_frames >= 5)
13386
      begin
13387 243 tadejm
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
13388 209 tadejm
        begin
13389 243 tadejm
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
13390
          test_fail("Interrupt Receive Buffer was not set");
13391
          fail = fail + 1;
13392 209 tadejm
        end
13393 243 tadejm
        if ((data & (~`ETH_INT_RXB)) !== 0)
13394 209 tadejm
        begin
13395 243 tadejm
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
13396
          test_fail("Other interrupts (except Receive Buffer) were set");
13397
          fail = fail + 1;
13398 209 tadejm
        end
13399 243 tadejm
      end
13400
      else if ((num_of_frames < 3)) // Frames smaller than 3 are not received.
13401
      begin
13402
        if (data) // Checking if any interrupt is pending)
13403 209 tadejm
        begin
13404 243 tadejm
          `TIME; $display("*E Interrupt(s) is(are) pending although frame was ignored, interrupt reg: %0h", data);
13405
          test_fail("Interrupts were set");
13406
          fail = fail + 1;
13407
        end
13408
      end
13409
      else
13410
      begin
13411
        if ((data & `ETH_INT_RXE) !== `ETH_INT_RXE)
13412
        begin
13413
          `TIME; $display("*E Interrupt Receive Buffer Error was not set, interrupt reg: %0h", data);
13414
          test_fail("Interrupt Receive Buffer Error was not set");
13415
          fail = fail + 1;
13416
        end
13417
        if ((data & (~`ETH_INT_RXE)) !== 0)
13418
        begin
13419
          `TIME; $display("*E Other interrupts (except Receive Buffer Error) were set, interrupt reg: %0h", data);
13420
          test_fail("Other interrupts (except Receive Buffer Error) were set");
13421
          fail = fail + 1;
13422
        end
13423
      end
13424
      // clear interrupts
13425
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13426
      // check WB INT signal
13427
      if (wb_int !== 1'b0)
13428
      begin
13429
        test_fail("WB INT signal should not be set");
13430
        fail = fail + 1;
13431
      end
13432
      // INTERMEDIATE DISPLAYS
13433
      if (num_of_frames == 3)
13434
      begin
13435
        $display("    using 1 BD out of 8 BDs (120..127) assigned to RX (wrap at 1st BD - RX BD 120)");
13436
        $display("    ->packets with lengths from %0d to %0d are not received (length increasing by 1 byte)",
13437
                 0, 3);
13438
      end
13439
      else if (num_of_frames == 9)
13440
      begin
13441
        $display("    using 1 BD out of 8 BDs (120..127) assigned to RX (wrap at 1st BD - RX BD 120)");
13442
        $display("    ->packet with length 4 is not received (length increasing by 1 byte)");
13443
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13444
                 5, 9);
13445
      end
13446
      else if (num_of_frames == 17)
13447
      begin
13448
        $display("    using 4 BDs out of 8 BDs (120..127) assigned to RX (wrap at 4th BD - RX BD 123)");
13449
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13450
                 10, 17);
13451
      end
13452
      else if (num_of_frames == 27)
13453
      begin
13454
        $display("    using 5 BDs out of 8 BDs (120..127) assigned to RX (wrap at 5th BD - RX BD 124)");
13455
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13456
                 18, 27);
13457
      end
13458
      else if (num_of_frames == 40)
13459
      begin
13460
        $display("    using 6 BDs out of 8 BDs (120..127) assigned to RX (wrap at 6th BD - RX BD 125)");
13461
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13462
                 28, 40);
13463
      end
13464
      else if (num_of_frames == 54)
13465
      begin
13466
        $display("    using 7 BDs out of 8 BDs (120..127) assigned to RX (wrap at 7th BD - RX BD 126)");
13467
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13468
                 41, 54);
13469
      end
13470
      else if (num_of_frames == 69)
13471
      begin
13472
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
13473
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13474
                 55, 69);
13475
      end
13476
      else if (num_of_frames == 69)
13477
      begin
13478
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
13479
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13480
                 55, 69);
13481
      end
13482
      else if (num_of_frames == 77)
13483
      begin
13484
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
13485
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13486
                 70, 77);
13487
      end
13488
      // set length (loop variable)
13489
      i_length = i_length + 1;
13490
      // the number of frame transmitted
13491
      num_of_frames = num_of_frames + 1;
13492
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
13493
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
13494
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
13495
          (num_of_frames == 54) || (num_of_frames == 62) || (num_of_frames == 70))
13496
        num_of_bd = 120;
13497
      else
13498
        num_of_bd = num_of_bd + 1;
13499
    end
13500
    // disable RX
13501
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
13502
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13503
    @(posedge wb_clk);
13504
    if(fail == 0)
13505
      test_ok;
13506
    else
13507
      fail = 0;
13508
  end
13509
 
13510
 
13511
  ////////////////////////////////////////////////////////////////////
13512
  ////                                                            ////
13513
  ////  Test receive packets form 0 to (MINFL + 12) sizes at      ////
13514
  ////  8 RX buffer decriptors ( 100Mbps ).                       ////
13515
  ////                                                            ////
13516
  ////////////////////////////////////////////////////////////////////
13517
  if (test_num == 9) // 
13518
  begin
13519
    // TEST 9: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 100Mbps )
13520
    test_name = "TEST 9: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 100Mbps )";
13521
    `TIME; $display("  TEST 9: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 100Mbps )");
13522
 
13523
    // reset MAC registers
13524
    hard_reset;
13525
    // reset MAC and MII LOGIC with soft reset
13526
    reset_mac;
13527
    reset_mii;
13528
    // set wb slave response
13529
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
13530
 
13531
    max_tmp = 0;
13532
    min_tmp = 0;
13533
    // set 8 RX buffer descriptors (120 - 127) - must be set before RX enable
13534
    wbm_write(`ETH_TX_BD_NUM, 32'h78, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13535
    // enable RX, set full-duplex mode, receive small, NO correct IFG
13536
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
13537
              `ETH_MODER_PRO | `ETH_MODER_BRO,
13538
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13539
    // prepare two packets of MAXFL length
13540
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13541
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
13542
    min_tmp = tmp[31:16];
13543
    st_data = 8'hAC;
13544
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
13545
    st_data = 8'h35;
13546
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
13547
    // check WB INT signal
13548
    if (wb_int !== 1'b0)
13549
    begin
13550
      test_fail("WB INT signal should not be set");
13551
      fail = fail + 1;
13552
    end
13553
    // unmask interrupts
13554
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
13555
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13556
 
13557
    // write to phy's control register for 100Mbps
13558
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
13559
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
13560
    speed = 100;
13561
 
13562
    frame_ended = 0;
13563 252 tadejm
    num_of_frames = 0;// 0; // 10;
13564 243 tadejm
    num_of_bd = 120;
13565
    i_length = 0 - 4;// (0 - 4); // 6; // 4 less due to CRC
13566
    while ((i_length + 4) < 78) // (min_tmp - 4))
13567
    begin
13568
      // append CRC to packet
13569 252 tadejm
      if ((i_length[0] == 1'b0) && (num_of_frames > 4))
13570 243 tadejm
        append_rx_crc (0, i_length, 1'b0, 1'b0);
13571 252 tadejm
      else if (num_of_frames > 4)
13572 243 tadejm
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
13573
      // choose generating carrier sense and collision
13574
      case (i_length[1:0])
13575
      2'h0:
13576
      begin
13577
        // not detect carrier sense in FD and no collision
13578
        eth_phy.no_carrier_sense_rx_fd_detect(0);
13579
        eth_phy.collision(0);
13580
      end
13581
      2'h1:
13582
      begin
13583
        // detect carrier sense in FD and no collision
13584
        eth_phy.no_carrier_sense_rx_fd_detect(1);
13585
        eth_phy.collision(0);
13586
      end
13587
      2'h2:
13588
      begin
13589
        // not detect carrier sense in FD and set collision
13590
        eth_phy.no_carrier_sense_rx_fd_detect(0);
13591
        eth_phy.collision(1);
13592
      end
13593
      default: // 2'h3:
13594
      begin
13595
        // detect carrier sense in FD and set collision
13596
        eth_phy.no_carrier_sense_rx_fd_detect(1);
13597
        eth_phy.collision(1);
13598
      end
13599
      endcase
13600
      #1;
13601
      // first 10 frames are received with RX BD 120 (wrap bit on RX BD 120)
13602
      if (num_of_frames <= 9)
13603
      begin
13604
        case (i_length[1:0])
13605
        2'h0: // Interrupt is generated
13606
        begin
13607
          // enable interrupt generation
13608
          set_rx_bd(120, 120, 1'b1, (`MEMORY_BASE + i_length[1:0]));
13609
        end
13610
        2'h1: // Interrupt is generated
13611
        begin
13612
          // enable interrupt generation
13613
          set_rx_bd(120, 120, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
13614
        end
13615
        2'h2: // Interrupt is generated
13616
        begin
13617
          // enable interrupt generation
13618
          set_rx_bd(120, 120, 1'b1, (`MEMORY_BASE + i_length[1:0]));
13619
        end
13620
        default: // 2'h3: // Interrupt is generated
13621
        begin
13622
          // enable interrupt generation
13623
          set_rx_bd(120, 120, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
13624
        end
13625
        endcase
13626
        // set wrap bit
13627
        set_rx_bd_wrap(120);
13628
      end
13629
      // 10 <= num_of_frames < 18 => wrap set to TX BD 123
13630
      else if ((num_of_frames == 10) || (num_of_frames == 14))
13631
      begin
13632
        tmp_len = i_length; // length of frame
13633
        tmp_bd_num = 120; // RX BD number
13634
        while (tmp_bd_num < 124) // 
13635
        begin
13636
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13637
          if (tmp_len[0] == 0)
13638
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13639
          else
13640
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13641
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13642
          tmp_len = tmp_len + 1;
13643
          // set RX BD number
13644
          tmp_bd_num = tmp_bd_num + 1;
13645
        end
13646
        // set wrap bit
13647
        set_rx_bd_wrap(123);
13648
      end
13649
      // 18 <= num_of_frames < 28 => wrap set to RX BD 124
13650
      else if ((num_of_frames == 18) || (num_of_frames == 23))
13651
      begin
13652
        tmp_len = i_length; // length of frame
13653
        tmp_bd_num = 120; // RX BD number
13654
        while (tmp_bd_num < 125) // 
13655
        begin
13656
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13657
          if (tmp_len[0] == 0)
13658
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13659
          else
13660
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13661
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13662
          tmp_len = tmp_len + 1;
13663
          // set RX BD number
13664
          tmp_bd_num = tmp_bd_num + 1;
13665
        end
13666
        // set wrap bit
13667
        set_rx_bd_wrap(124);
13668
      end
13669
      // 28 <= num_of_frames < 40 => wrap set to RX BD 125
13670
      else if ((num_of_frames == 28) || (num_of_frames == 34))
13671
      begin
13672
        tmp_len = i_length; // length of frame
13673
        tmp_bd_num = 120; // RX BD number
13674
        while (tmp_bd_num < 126) // 
13675
        begin
13676
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13677
          if (tmp_len[0] == 0)
13678
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13679
          else
13680
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13681
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13682
          tmp_len = tmp_len + 1;
13683
          // set RX BD number
13684
          tmp_bd_num = tmp_bd_num + 1;
13685
        end
13686
        // set wrap bit
13687
        set_rx_bd_wrap(125);
13688
      end
13689
      // 40 <= num_of_frames < 54 => wrap set to RX BD 126
13690
      else if ((num_of_frames == 40) || (num_of_frames == 47))
13691
      begin
13692
        tmp_len = i_length; // length of frame
13693
        tmp_bd_num = 120; // RX BD number
13694
        while (tmp_bd_num < 127) // 
13695
        begin
13696
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13697
          if (tmp_len[0] == 0)
13698
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13699
          else
13700
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13701
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13702
          tmp_len = tmp_len + 1;
13703
          // set RX BD number
13704
          tmp_bd_num = tmp_bd_num + 1;
13705
        end
13706
        // set wrap bit
13707
        set_rx_bd_wrap(126);
13708
      end
13709
      // 54 <= num_of_frames < 70 => wrap set to RX BD 127
13710
      else if ((num_of_frames == 54) || (num_of_frames == 62))
13711
      begin
13712
        tmp_len = i_length; // length of frame
13713
        tmp_bd_num = 120; // RX BD number
13714
        while (tmp_bd_num < 128) // 
13715
        begin
13716
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13717
          if (tmp_len[0] == 0)
13718
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13719
          else
13720
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13721
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13722
          tmp_len = tmp_len + 1;
13723
          // set RX BD number
13724
          tmp_bd_num = tmp_bd_num + 1;
13725
        end
13726
        // set wrap bit
13727
        set_rx_bd_wrap(127);
13728
      end
13729
      // 70 <= num_of_frames < 78 => wrap set to RX BD 127
13730
      else if (num_of_frames == 70)
13731
      begin
13732
        tmp_len = i_length; // length of frame
13733
        tmp_bd_num = 120; // RX BD number
13734
        while (tmp_bd_num < 128) // 
13735
        begin
13736
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13737
          if (tmp_len[0] == 0)
13738
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13739
          else
13740
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13741
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13742
          tmp_len = tmp_len + 1;
13743
          // set RX BD number
13744
          tmp_bd_num = tmp_bd_num + 1;
13745
        end
13746
        // set wrap bit
13747
        set_rx_bd_wrap(127);
13748
      end
13749
      #1;
13750
      // SET empty bit
13751
      if (num_of_frames < 10)
13752
        set_rx_bd_empty(120, 120);
13753
      else if (num_of_frames < 14)
13754
        set_rx_bd_empty((120 + num_of_frames - 10), (120 + num_of_frames - 10));
13755
      else if (num_of_frames < 18)
13756
        set_rx_bd_empty((120 + num_of_frames - 14), (120 + num_of_frames - 14));
13757
      else if (num_of_frames < 23)
13758
        set_rx_bd_empty((120 + num_of_frames - 18), (120 + num_of_frames - 18));
13759
      else if (num_of_frames < 28)
13760
        set_rx_bd_empty((120 + num_of_frames - 23), (120 + num_of_frames - 23));
13761
      else if (num_of_frames < 34)
13762
        set_rx_bd_empty((120 + num_of_frames - 28), (120 + num_of_frames - 28));
13763
      else if (num_of_frames < 40)
13764
        set_rx_bd_empty((120 + num_of_frames - 34), (120 + num_of_frames - 34));
13765
      else if (num_of_frames < 47)
13766
        set_rx_bd_empty((120 + num_of_frames - 40), (120 + num_of_frames - 40));
13767
      else if (num_of_frames < 54)
13768
        set_rx_bd_empty((120 + num_of_frames - 47), (120 + num_of_frames - 47));
13769
      else if (num_of_frames < 62)
13770
        set_rx_bd_empty((120 + num_of_frames - 54), (120 + num_of_frames - 54));
13771
      else if (num_of_frames < 70)
13772
        set_rx_bd_empty((120 + num_of_frames - 62), (120 + num_of_frames - 62));
13773
      else if (num_of_frames < 78)
13774
        set_rx_bd_empty((120 + num_of_frames - 70), (120 + num_of_frames - 70));
13775
      // CHECK END OF RECEIVE
13776 252 tadejm
      // receive just preamble between some packets
13777
      if ((num_of_frames == 0) || (num_of_frames == 4) || (num_of_frames == 9))
13778
      begin
13779
        #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h6, 8'h55, 0, 0, 1'b0);
13780
        @(posedge mrx_clk);
13781
        wait (MRxDV === 1'b0); // end receive
13782
        repeat(10) @(posedge mrx_clk);
13783
        repeat(15) @(posedge wb_clk);
13784
      end
13785
      // receiving frames and checking end of them
13786 243 tadejm
      frame_ended = 0;
13787
      check_frame = 0;
13788
      fork
13789
        begin
13790
          if (i_length[0] == 1'b0)
13791
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
13792
          else
13793
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
13794
          repeat(10) @(posedge mrx_clk);
13795
        end
13796 254 mohor
        begin: fr_end2
13797 243 tadejm
          wait (MRxDV === 1'b1); // start receive
13798
          #1 check_rx_bd(num_of_bd, data);
13799
          if (data[15] !== 1)
13800 209 tadejm
          begin
13801 243 tadejm
            test_fail("Wrong buffer descriptor's empty bit read out from MAC");
13802 209 tadejm
            fail = fail + 1;
13803
          end
13804 243 tadejm
          wait (MRxDV === 1'b0); // end receive
13805
          while ((data[15] === 1) && (check_frame == 0))
13806
          begin
13807
            #1 check_rx_bd(num_of_bd, data);
13808
            @(posedge wb_clk);
13809
          end
13810
          if (data[15] === 0)
13811
            frame_ended = 1;
13812
          repeat (1) @(posedge wb_clk);
13813 209 tadejm
        end
13814
        begin
13815 243 tadejm
          wait (MRxDV === 1'b1); // start receive
13816
          wait (MRxDV === 1'b0); // end receive
13817
          repeat(10) @(posedge mrx_clk);
13818
          repeat(15) @(posedge wb_clk);
13819
          check_frame = 1;
13820 209 tadejm
        end
13821 243 tadejm
      join
13822
      // check length of a PACKET
13823 252 tadejm
      if ( ((data[31:16] != (i_length + 4)) && (num_of_frames >= 3)) ||
13824
           ((data[31:16] != 0) && (num_of_frames < 3)) )
13825 243 tadejm
      begin
13826
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
13827
                        data[31:16], (i_length + 4));
13828
        test_fail("Wrong length of the packet out from PHY");
13829
        fail = fail + 1;
13830
      end
13831
      // check received RX packet data and CRC
13832 252 tadejm
      if ((frame_ended == 1) && (num_of_frames >= 5)) // 5 bytes is minimum size without CRC error, since
13833
      begin                                           // CRC has 4 bytes for itself
13834 243 tadejm
        if (i_length[0] == 1'b0)
13835 209 tadejm
        begin
13836 243 tadejm
          check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
13837 209 tadejm
        end
13838 243 tadejm
        else
13839 209 tadejm
        begin
13840 243 tadejm
          check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
13841 209 tadejm
        end
13842
        if (tmp > 0)
13843
        begin
13844 243 tadejm
          `TIME; $display("*E Wrong data of the received packet");
13845
          test_fail("Wrong data of the received packet");
13846 209 tadejm
          fail = fail + 1;
13847
        end
13848
      end
13849
      // check WB INT signal
13850 252 tadejm
      if (num_of_frames >= 3) // Frames smaller than 3 are not received.
13851
      begin                   // Frames greater then 5 always cause an interrupt (Frame received)
13852
        if (wb_int !== 1'b1)  // Frames with length 3 or 4 always cause an interrupt (CRC error)
13853 209 tadejm
        begin
13854
          `TIME; $display("*E WB INT signal should be set");
13855
          test_fail("WB INT signal should be set");
13856
          fail = fail + 1;
13857
        end
13858
      end
13859 252 tadejm
      else
13860 209 tadejm
      begin
13861
        if (wb_int !== 1'b0)
13862
        begin
13863
          `TIME; $display("*E WB INT signal should not be set");
13864
          test_fail("WB INT signal should not be set");
13865
          fail = fail + 1;
13866
        end
13867
      end
13868 243 tadejm
      // check RX buffer descriptor of a packet
13869
      if (num_of_frames >= min_tmp)
13870
      begin
13871 254 mohor
        if ( (data[15:0] !== 16'h6080) && // wrap bit
13872
             (data[15:0] !== 16'h4080) ) // without wrap bit
13873 209 tadejm
        begin
13874 243 tadejm
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13875
          test_fail("RX buffer descriptor status is not correct");
13876
          fail = fail + 1;
13877 209 tadejm
        end
13878 243 tadejm
      end
13879 254 mohor
      else if (num_of_frames > 6)
13880 243 tadejm
      begin
13881 254 mohor
        if ( (data[15:0] !== 16'h6084) && // wrap bit
13882
             (data[15:0] !== 16'h4084) ) // without wrap bit
13883
        begin
13884
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13885
          test_fail("RX buffer descriptor status is not correct");
13886
          fail = fail + 1;
13887
        end
13888
      end
13889
      else if (num_of_frames > 4) // MAC does not recognize Dest. ADDR. for lengths 5, 6 => no MISS
13890
      begin
13891 243 tadejm
        if ( (data[15:0] !== 16'h6004) && // wrap bit
13892
             (data[15:0] !== 16'h4004) ) // without wrap bit
13893 209 tadejm
        begin
13894 243 tadejm
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13895
          test_fail("RX buffer descriptor status is not correct");
13896
          fail = fail + 1;
13897 209 tadejm
        end
13898
      end
13899 254 mohor
      else if (num_of_frames > 2) // MAC does not recognize Dest. ADDR. for length 3, 4 => no MISS, CRC ERROR
13900 243 tadejm
      begin
13901
        if ( (data[15:0] !== 16'h6006) && // wrap bit
13902
             (data[15:0] !== 16'h4006) ) // without wrap bit
13903
        begin
13904
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13905
          test_fail("RX buffer descriptor status is not correct");
13906
          fail = fail + 1;
13907
        end
13908
      end
13909 209 tadejm
      else
13910
      begin
13911
        if (data[15] !== 1'b1)
13912
        begin
13913 243 tadejm
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13914
          test_fail("RX buffer descriptor status is not correct");
13915 209 tadejm
          fail = fail + 1;
13916
        end
13917
      end
13918
      // check interrupts
13919
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13920 252 tadejm
      if (num_of_frames >= 5)
13921 209 tadejm
      begin
13922 252 tadejm
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
13923 209 tadejm
        begin
13924 243 tadejm
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
13925
          test_fail("Interrupt Receive Buffer was not set");
13926 209 tadejm
          fail = fail + 1;
13927
        end
13928 243 tadejm
        if ((data & (~`ETH_INT_RXB)) !== 0)
13929 209 tadejm
        begin
13930 243 tadejm
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
13931
          test_fail("Other interrupts (except Receive Buffer) were set");
13932 209 tadejm
          fail = fail + 1;
13933
        end
13934
      end
13935 252 tadejm
      else if ((num_of_frames < 3)) // Frames smaller than 3 are not received.
13936
      begin
13937
        if (data) // Checking if any interrupt is pending)
13938
        begin
13939
          `TIME; $display("*E Interrupt(s) is(are) pending although frame was ignored, interrupt reg: %0h", data);
13940
          test_fail("Interrupts were set");
13941
          fail = fail + 1;
13942
        end
13943
      end
13944 209 tadejm
      else
13945
      begin
13946 252 tadejm
        if ((data & `ETH_INT_RXE) !== `ETH_INT_RXE)
13947 209 tadejm
        begin
13948 243 tadejm
          `TIME; $display("*E Interrupt Receive Buffer Error was not set, interrupt reg: %0h", data);
13949
          test_fail("Interrupt Receive Buffer Error was not set");
13950 209 tadejm
          fail = fail + 1;
13951
        end
13952 243 tadejm
        if ((data & (~`ETH_INT_RXE)) !== 0)
13953
        begin
13954
          `TIME; $display("*E Other interrupts (except Receive Buffer Error) were set, interrupt reg: %0h", data);
13955
          test_fail("Other interrupts (except Receive Buffer Error) were set");
13956
          fail = fail + 1;
13957
        end
13958 209 tadejm
      end
13959
      // clear interrupts
13960
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13961
      // check WB INT signal
13962
      if (wb_int !== 1'b0)
13963
      begin
13964
        test_fail("WB INT signal should not be set");
13965
        fail = fail + 1;
13966
      end
13967
      // INTERMEDIATE DISPLAYS
13968 243 tadejm
      if (num_of_frames == 3)
13969 209 tadejm
      begin
13970 243 tadejm
        $display("    using 1 BD out of 8 BDs (120..127) assigned to RX (wrap at 1st BD - RX BD 120)");
13971
        $display("    ->packets with lengths from %0d to %0d are not received (length increasing by 1 byte)",
13972 209 tadejm
                 0, 3);
13973
      end
13974 243 tadejm
      else if (num_of_frames == 9)
13975 209 tadejm
      begin
13976 243 tadejm
        $display("    using 1 BD out of 8 BDs (120..127) assigned to RX (wrap at 1st BD - RX BD 120)");
13977
        $display("    ->packet with length 4 is not received (length increasing by 1 byte)");
13978 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13979
                 5, 9);
13980
      end
13981 243 tadejm
      else if (num_of_frames == 17)
13982 209 tadejm
      begin
13983 243 tadejm
        $display("    using 4 BDs out of 8 BDs (120..127) assigned to RX (wrap at 4th BD - RX BD 123)");
13984 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13985
                 10, 17);
13986
      end
13987 243 tadejm
      else if (num_of_frames == 27)
13988 209 tadejm
      begin
13989 243 tadejm
        $display("    using 5 BDs out of 8 BDs (120..127) assigned to RX (wrap at 5th BD - RX BD 124)");
13990 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13991
                 18, 27);
13992
      end
13993 243 tadejm
      else if (num_of_frames == 40)
13994 209 tadejm
      begin
13995 243 tadejm
        $display("    using 6 BDs out of 8 BDs (120..127) assigned to RX (wrap at 6th BD - RX BD 125)");
13996 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13997
                 28, 40);
13998
      end
13999 243 tadejm
      else if (num_of_frames == 54)
14000 209 tadejm
      begin
14001 243 tadejm
        $display("    using 7 BDs out of 8 BDs (120..127) assigned to RX (wrap at 7th BD - RX BD 126)");
14002 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
14003
                 41, 54);
14004
      end
14005 243 tadejm
      else if (num_of_frames == 69)
14006 209 tadejm
      begin
14007 243 tadejm
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
14008 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
14009
                 55, 69);
14010
      end
14011 243 tadejm
      else if (num_of_frames == 69)
14012
      begin
14013
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
14014
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
14015
                 55, 69);
14016
      end
14017
      else if (num_of_frames == 77)
14018
      begin
14019
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
14020
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
14021
                 70, 77);
14022
      end
14023 209 tadejm
      // set length (loop variable)
14024
      i_length = i_length + 1;
14025
      // the number of frame transmitted
14026
      num_of_frames = num_of_frames + 1;
14027
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
14028
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
14029
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
14030 243 tadejm
          (num_of_frames == 54) || (num_of_frames == 62) || (num_of_frames == 70))
14031
        num_of_bd = 120;
14032 209 tadejm
      else
14033
        num_of_bd = num_of_bd + 1;
14034
    end
14035 243 tadejm
    // disable RX
14036 209 tadejm
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
14037
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14038
    @(posedge wb_clk);
14039
    if(fail == 0)
14040
      test_ok;
14041
    else
14042
      fail = 0;
14043
  end
14044
 
14045
 
14046 243 tadejm
  ////////////////////////////////////////////////////////////////////
14047
  ////                                                            ////
14048
  ////  Test receive packet synchronization with receive          ////
14049
  ////  disable/enable ( 10Mbps ).                                ////
14050
  ////                                                            ////
14051
  ////////////////////////////////////////////////////////////////////
14052
  if (test_num == 10) // Test no receive when all buffers are TX ( 10Mbps ).
14053
  begin
14054
    // TEST 10: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )
14055
    test_name   = "TEST 10: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )";
14056
    `TIME; $display("  TEST 10: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )");
14057 209 tadejm
 
14058 243 tadejm
 
14059
 
14060
 
14061
 
14062
 
14063
  end
14064
 
14065
 
14066
  ////////////////////////////////////////////////////////////////////
14067
  ////                                                            ////
14068
  ////  Test receive packet synchronization with receive          ////
14069
  ////  disable/enable ( 10Mbps ).                                ////
14070
  ////                                                            ////
14071
  ////////////////////////////////////////////////////////////////////
14072
  if (test_num == 12) // Test no receive when all buffers are TX ( 10Mbps ).
14073
  begin
14074
    // TEST 12: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )
14075
    test_name   = "TEST 12: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )";
14076
    `TIME; $display("  TEST 12: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )");
14077
 
14078
 
14079
 
14080
 
14081
 
14082
 
14083
  end
14084
 
14085
 
14086
  ////////////////////////////////////////////////////////////////////
14087
  ////                                                            ////
14088
  ////  Test receive packet synchronization with receive          ////
14089
  ////  disable/enable ( 10Mbps ).                                ////
14090
  ////                                                            ////
14091
  ////////////////////////////////////////////////////////////////////
14092
  if (test_num == 14) // Test no receive when all buffers are TX ( 10Mbps ).
14093
  begin
14094
    // TEST 14: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )
14095
    test_name   = "TEST 14: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )";
14096
    `TIME; $display("  TEST 14: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )");
14097
 
14098
 
14099
 
14100
 
14101
 
14102
 
14103
  end
14104
 
14105
 
14106
  ////////////////////////////////////////////////////////////////////
14107
  ////                                                            ////
14108
  ////  Test receive packet synchronization with receive          ////
14109
  ////  disable/enable ( 10Mbps ).                                ////
14110
  ////                                                            ////
14111
  ////////////////////////////////////////////////////////////////////
14112
  if (test_num == 16) // Test no receive when all buffers are TX ( 10Mbps ).
14113
  begin
14114
    // TEST 16: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )
14115
    test_name   = "TEST 16: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )";
14116
    `TIME; $display("  TEST 16: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )");
14117
 
14118
 
14119
 
14120
 
14121
 
14122
 
14123
  end
14124
 
14125
 
14126
 
14127 209 tadejm
end   //  for (test_num=start_task; test_num <= end_task; test_num=test_num+1)
14128
 
14129
end
14130
endtask // test_mac_full_duplex_receive
14131
 
14132
 
14133 263 mohor
task test_mac_full_duplex_flow_control;
14134 209 tadejm
  input  [31:0]  start_task;
14135
  input  [31:0]  end_task;
14136
  integer        bit_start_1;
14137
  integer        bit_end_1;
14138
  integer        bit_start_2;
14139
  integer        bit_end_2;
14140
  integer        num_of_reg;
14141
  integer        num_of_frames;
14142
  integer        num_of_bd;
14143
  integer        i_addr;
14144
  integer        i_data;
14145
  integer        i_length;
14146
  integer        tmp_len;
14147
  integer        tmp_bd;
14148
  integer        tmp_bd_num;
14149
  integer        tmp_data;
14150
  integer        tmp_ipgt;
14151
  integer        test_num;
14152
  reg    [31:0]  tx_bd_num;
14153
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_data;
14154
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_tmp_data;
14155
  integer        i;
14156
  integer        i1;
14157
  integer        i2;
14158
  integer        i3;
14159
  integer        fail;
14160
  integer        speed;
14161 260 mohor
  integer        mac_hi_addr;
14162
  integer        mac_lo_addr;
14163 209 tadejm
  reg            frame_started;
14164
  reg            frame_ended;
14165
  reg            wait_for_frame;
14166
  reg    [31:0]  addr;
14167
  reg    [31:0]  data;
14168
  reg    [31:0]  tmp;
14169
  reg    [ 7:0]  st_data;
14170
  reg    [15:0]  max_tmp;
14171
  reg    [15:0]  min_tmp;
14172 263 mohor
  reg            PassAll;
14173
  reg            RxFlow;
14174 266 mohor
  reg            enable_irq_in_rxbd;
14175 267 mohor
  reg    [15:0]  pause_value;
14176 209 tadejm
begin
14177 263 mohor
// MAC FULL DUPLEX FLOW CONTROL TEST
14178
test_heading("MAC FULL DUPLEX FLOW CONTROL TEST");
14179 209 tadejm
$display(" ");
14180 263 mohor
$display("MAC FULL DUPLEX FLOW CONTROL TEST");
14181 209 tadejm
fail = 0;
14182
 
14183
// reset MAC registers
14184
hard_reset;
14185
// reset MAC and MII LOGIC with soft reset
14186
reset_mac;
14187
reset_mii;
14188
// set wb slave response
14189
wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
14190
 
14191
  /*
14192
  TASKS for set and control TX buffer descriptors (also send packet - set_tx_bd_ready):
14193
  -------------------------------------------------------------------------------------
14194
  set_tx_bd
14195
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0], len[15:0], irq, pad, crc, txpnt[31:0]);
14196
  set_tx_bd_wrap
14197
    (tx_bd_num_end[6:0]);
14198
  set_tx_bd_ready
14199
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
14200
  check_tx_bd
14201
    (tx_bd_num_start[6:0], tx_bd_status[31:0]);
14202
  clear_tx_bd
14203
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
14204
 
14205
  TASKS for set and control RX buffer descriptors:
14206
  ------------------------------------------------
14207
  set_rx_bd
14208
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0], irq, rxpnt[31:0]);
14209
  set_rx_bd_wrap
14210
    (rx_bd_num_end[6:0]);
14211
  set_rx_bd_empty
14212
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
14213
  check_rx_bd
14214
    (rx_bd_num_end[6:0], rx_bd_status);
14215
  clear_rx_bd
14216
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
14217
 
14218
  TASKS for set and check TX packets:
14219
  -----------------------------------
14220
  set_tx_packet
14221
    (txpnt[31:0], len[15:0], eth_start_data[7:0]);
14222
  check_tx_packet
14223
    (txpnt_wb[31:0], txpnt_phy[31:0], len[15:0], failure[31:0]);
14224
 
14225
  TASKS for set and check RX packets:
14226
  -----------------------------------
14227
  set_rx_packet
14228
    (rxpnt[31:0], len[15:0], plus_nibble, d_addr[47:0], s_addr[47:0], type_len[15:0], start_data[7:0]);
14229
  check_rx_packet
14230
    (rxpnt_phy[31:0], rxpnt_wb[31:0], len[15:0], plus_nibble, successful_nibble, failure[31:0]);
14231
 
14232
  TASKS for append and check CRC to/of TX packet:
14233
  -----------------------------------------------
14234
  append_tx_crc
14235
    (txpnt_wb[31:0], len[15:0], negated_crc);
14236
  check_tx_crc
14237
    (txpnt_phy[31:0], len[15:0], negated_crc, failure[31:0]);
14238
 
14239
  TASK for append CRC to RX packet (CRC is checked together with check_rx_packet):
14240
  --------------------------------------------------------------------------------
14241
  append_rx_crc
14242
    (rxpnt_phy[31:0], len[15:0], plus_nibble, negated_crc);
14243
  */
14244
 
14245
//////////////////////////////////////////////////////////////////////
14246
////                                                              ////
14247 263 mohor
////  test_mac_full_duplex_flow_control:                          ////
14248 209 tadejm
////                                                              ////
14249
////  0: Test                                                     ////
14250
////                                                              ////
14251
//////////////////////////////////////////////////////////////////////
14252 260 mohor
 
14253 209 tadejm
for (test_num = start_task; test_num <= end_task; test_num = test_num + 1)
14254
begin
14255
 
14256
  ////////////////////////////////////////////////////////////////////
14257
  ////                                                            ////
14258 254 mohor
  ////  Test inserts control frames while transmitting normal     ////
14259
  ////  frames. Using 4 TX buffer decriptors ( 10Mbps ).          ////
14260 209 tadejm
  ////                                                            ////
14261
  ////////////////////////////////////////////////////////////////////
14262 254 mohor
  if (test_num == 0) // 
14263 209 tadejm
  begin
14264 254 mohor
    // TEST 0: INSERT CONTROL FRM. WHILE TRANSMITTING NORMAL FRM. AT 4 TX BD ( 10Mbps )
14265
    test_name = "TEST 0: INSERT CONTROL FRM. WHILE TRANSMITTING NORMAL FRM. AT 4 TX BD ( 10Mbps )";
14266
    `TIME; $display("  TEST 0: INSERT CONTROL FRM. WHILE TRANSMITTING NORMAL FRM. AT 4 TX BD ( 10Mbps )");
14267 260 mohor
 
14268 254 mohor
    // reset MAC completely
14269
    hard_reset;
14270
    // set wb slave response
14271
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
14272 209 tadejm
 
14273 254 mohor
    max_tmp = 0;
14274
    min_tmp = 0;
14275
    // set 4 TX buffer descriptors - must be set before TX enable
14276
    wbm_write(`ETH_TX_BD_NUM, 32'h4, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14277
    // enable TX, set full-duplex mode, padding and CRC appending
14278
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
14279
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14280
    // enable TX flow control
14281
    wbm_write(`ETH_CTRLMODER, `ETH_CTRLMODER_TXFLOW, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14282 260 mohor
 
14283
    // Set MAC address
14284
    mac_hi_addr = 32'h00000001;
14285
    mac_lo_addr = 32'h02030405;
14286
    wbm_write(`ETH_MAC_ADDR1, mac_hi_addr, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14287
    wbm_write(`ETH_MAC_ADDR0, mac_lo_addr, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14288
 
14289 254 mohor
    // prepare two packets of MAXFL length
14290
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14291
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
14292
    min_tmp = tmp[31:16];
14293 260 mohor
    st_data = 8'h34;
14294 254 mohor
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC. Writing data to the memory
14295 260 mohor
    st_data = 8'h56;
14296 254 mohor
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp - 4), st_data); // length without CRC. Writing data to the memory
14297
    // check WB INT signal
14298
    if (wb_int !== 1'b0)
14299
    begin
14300
      test_fail("WB INT signal should not be set");
14301
      fail = fail + 1;
14302
    end
14303
 
14304
    // write to phy's control register for 10Mbps
14305
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
14306
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
14307
    speed = 10;
14308
 
14309
    frame_started = 0;
14310
    num_of_frames = 0;
14311
    num_of_bd = 0;
14312
    i_length = 0; // 0;
14313 209 tadejm
 
14314 254 mohor
 
14315 260 mohor
    // Initialize one part of memory with data of control packet
14316
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h0), 32'h0180c200, 4'hF);
14317
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h4), {16'h0001, mac_hi_addr[15:0]}, 4'hF);
14318
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h8), mac_lo_addr, 4'hF);
14319
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'hc), 32'h88080001, 4'hF);
14320
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h10), 32'h11110000, 4'hF);
14321
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h14), 32'h00000000, 4'hF);
14322
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h18), 32'h00000000, 4'hF);
14323
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h1c), 32'h00000000, 4'hF);
14324
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h20), 32'h00000000, 4'hF);
14325
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h24), 32'h00000000, 4'hF);
14326
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h28), 32'h00000000, 4'hF);
14327
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h2c), 32'h00000000, 4'hF);
14328
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h30), 32'h00000000, 4'hF);
14329
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h34), 32'h00000000, 4'hF);
14330
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h38), 32'h00000000, 4'hF);
14331
//    append_tx_crc(`MEMORY_BASE + 2 * max_tmp, 60, 0);       // CRC is appended after the data
14332
 
14333
 
14334 254 mohor
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14335
    // In the following section, control frame will be sent while no other transmission is in progress.//
14336
    // TXC interrupt won't be unmasked.                                                                //
14337
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14338
    wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14339
    if(data)
14340
      begin
14341
        test_fail("IRQ already pending!");
14342
        fail = fail + 1;
14343
        `TIME; $display("*E IRQ already pending!");
14344
      end
14345
 
14346
    if (wb_int)
14347
    begin
14348
      test_fail("WB INT signal should not be set!");
14349
      fail = fail + 1;
14350
      `TIME; $display("*E WB INT signal should not be set!");
14351
    end
14352
 
14353 260 mohor
    // first destination address on ethernet PHY
14354
    eth_phy.set_tx_mem_addr(0);
14355
    // Request sending the control frame with pause value = 0x1111
14356
    wbm_write(`ETH_TX_CTRL, `ETH_TX_CTRL_TXPAUSERQ | 32'h1111, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14357 254 mohor
 
14358
    wait (MTxEn === 1'b1); // start transmit
14359
    wait (MTxEn === 1'b0); // end transmit
14360
    repeat(10) @ (posedge wb_clk);  // wait some time
14361
    repeat(10) @ (posedge mtx_clk); // wait some time
14362
 
14363
    wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14364
    if(data !== `ETH_INT_TXC)
14365
      begin
14366
        test_fail("TXC IRQ should be set!");
14367
        fail = fail + 1;
14368
        `TIME; $display("*E TXC IRQ should be set!");
14369
      end
14370
 
14371
    if (wb_int)
14372
    begin
14373
      test_fail("WB INT signal should not be set because TXC irq is masked!");
14374
      fail = fail + 1;
14375
      `TIME; $display("*E WB INT signal should not be set because TXC irq is masked!");
14376
    end
14377
 
14378
    // Clear TXC interrupt
14379
    wbm_write(`ETH_INT, `ETH_INT_TXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14380
 
14381
    if (wb_int)
14382
    begin
14383
      test_fail("WB INT signal should not be set!");
14384
      fail = fail + 1;
14385
      `TIME; $display("*E WB INT signal should not be set!");
14386
    end
14387
 
14388 260 mohor
    check_tx_packet((`MEMORY_BASE + 2 * max_tmp), 0, 60, tmp);
14389
    if (tmp > 0)
14390
    begin
14391
      $display("Wrong data of the transmitted packet");
14392
      test_fail("Wrong data of the transmitted packet");
14393
      fail = fail + 1;
14394
    end
14395
    // check transmited TX packet CRC
14396
    #1 check_tx_crc(0, 60, 1'b0, tmp); // length without CRC
14397
    if (tmp > 0)
14398
    begin
14399
      $display("Wrong CRC of the transmitted packet");
14400
      test_fail("Wrong CRC of the transmitted packet");
14401
      fail = fail + 1;
14402
    end
14403 254 mohor
 
14404
 
14405
 
14406
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14407
    // In the following section, control frame will be sent while no other transmission is in progress.//
14408
    // TXC interrupt is unmasked.                                                                      //
14409
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14410
 
14411
    // unmask all interrupts
14412
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
14413
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14414
 
14415
    wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14416
    if(data)
14417
      begin
14418
        test_fail("IRQ already pending!");
14419
        fail = fail + 1;
14420
        `TIME; $display("*E IRQ already pending!");
14421
      end
14422
 
14423
    if (wb_int)
14424
    begin
14425
      test_fail("WB INT signal should not be set!");
14426
      fail = fail + 1;
14427
      `TIME; $display("*E WB INT signal should not be set!");
14428
    end
14429
 
14430
    // unmask only TXC interrupts
14431
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14432
 
14433 260 mohor
    // first destination address on ethernet PHY
14434
    eth_phy.set_tx_mem_addr(0);
14435
    // Request sending the control frame with pause value = 0x2222
14436
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h10), 32'h22220000, 4'hF);  // Just for data test
14437
    wbm_write(`ETH_TX_CTRL, `ETH_TX_CTRL_TXPAUSERQ | 32'h2222, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14438 254 mohor
 
14439
    wait (MTxEn === 1'b1); // start transmit
14440
    wait (MTxEn === 1'b0); // end transmit
14441
    repeat(10) @ (posedge wb_clk);  // wait some time
14442
    repeat(10) @ (posedge mtx_clk); // wait some time
14443
 
14444
    wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14445
    if(data !== `ETH_INT_TXC)
14446
      begin
14447
        test_fail("TXC IRQ should be set!");
14448
        fail = fail + 1;
14449
        `TIME; $display("*E TXC IRQ should be set!");
14450
      end
14451
 
14452
    if (!wb_int)
14453
    begin
14454
      test_fail("WB INT signal should be set!");
14455
      fail = fail + 1;
14456
      `TIME; $display("*E WB INT signal should be set!");
14457
    end
14458
 
14459
    // Clear TXC interrupt
14460
    wbm_write(`ETH_INT, `ETH_INT_TXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14461
 
14462
    if (wb_int)
14463
    begin
14464
      test_fail("WB INT signal should not be set!");
14465
      fail = fail + 1;
14466
      `TIME; $display("*E WB INT signal should not be set!");
14467
    end
14468
 
14469
 
14470 260 mohor
    check_tx_packet((`MEMORY_BASE + 2 * max_tmp), 0, 60, tmp);
14471
    if (tmp > 0)
14472
    begin
14473
      $display("Wrong data of the transmitted packet");
14474
      test_fail("Wrong data of the transmitted packet");
14475
      fail = fail + 1;
14476
    end
14477
    // check transmited TX packet CRC
14478
    #1 check_tx_crc(0, 60, 1'b0, tmp); // length without CRC
14479
    if (tmp > 0)
14480
    begin
14481
      $display("Wrong CRC of the transmitted packet");
14482
      test_fail("Wrong CRC of the transmitted packet");
14483
      fail = fail + 1;
14484
    end
14485 254 mohor
 
14486 260 mohor
 
14487 254 mohor
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14488 260 mohor
    // In the following section, control frame sending is requested while no other transmission        //
14489
    // is in progress. TXC interrupt is unmasked.                                                      //
14490 254 mohor
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14491
 
14492
    // unmask all interrupts
14493
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
14494
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14495
 
14496
    wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14497
    if(data)
14498
      begin
14499
        test_fail("IRQ already pending!");
14500
        fail = fail + 1;
14501
        `TIME; $display("*E IRQ already pending!");
14502
      end
14503
 
14504
    if (wb_int)
14505
    begin
14506
      test_fail("WB INT signal should not be set!");
14507
      fail = fail + 1;
14508
      `TIME; $display("*E WB INT signal should not be set!");
14509
    end
14510
 
14511
 
14512 260 mohor
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14513
    // In the following section, control frame request and data send request are both set. At the      //
14514
    // beginning control frame request will be faster than data send request, later the opposite.      //
14515
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14516
    for (i=0; i<32; i=i+1)
14517
    begin
14518
      // Request sending the control frame with pause value = 0x5678
14519
      set_tx_bd(0, 0, 16'h100, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE)); // irq, pad, crc
14520
      set_tx_bd_wrap(0);
14521
      // first destination address on ethernet PHY
14522
      eth_phy.set_tx_mem_addr(0);
14523
      set_tx_bd_ready(0, 0);
14524
 
14525
      wait (MTxEn === 1'b1); // start transmit
14526
 
14527
      repeat(i) @ (posedge mtx_clk);  // We need to wait some time until TX module starts using the data (preamble stage is over)
14528
 
14529
      // Send control frame request
14530
      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
14531
      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);
14532
 
14533
      wait (MTxEn === 1'b0); // Wait until data frame transmission is over
14534
      repeat(10) @ (posedge mtx_clk); // wait some time so status is written
14535
      tmp_len = eth_phy.tx_len; // the length of a packet which was sent out first!!!
14536
      repeat(10) @ (posedge wb_clk);  // wait some time so status is written
14537
 
14538
      // first destination address on ethernet PHY
14539
      eth_phy.set_tx_mem_addr(0);
14540
 
14541
      if(tmp_len == 64)  // Control frame
14542 254 mohor
      begin
14543 260 mohor
        wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14544
        if(data !== `ETH_INT_TXC)
14545
        begin
14546
          test_fail("TXC IRQ should be set!");
14547
          fail = fail + 1;
14548
          `TIME; $display("*E TXC IRQ should be set!");
14549
          `TIME; $display("ETH_INT = 0x%0x", data);
14550
        end
14551
      end
14552
      else
14553
      begin
14554
        wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14555
        if(data !== `ETH_INT_TXB)
14556
        begin
14557
          test_fail("TXB IRQ should be set!");
14558
          fail = fail + 1;
14559
          `TIME; $display("*E TXB IRQ should be set!");
14560
          `TIME; $display("ETH_INT = 0x%0x", data);
14561
        end
14562
      end
14563
 
14564
      if(tmp_len == 64)  // Control frame
14565
        check_tx_packet((`MEMORY_BASE + 2 * max_tmp), 0, 60, tmp);
14566
      else
14567
        check_tx_packet((`MEMORY_BASE), 0, 32'h100, tmp);
14568
 
14569
      if (tmp > 0)
14570
      begin
14571
        $display("Wrong data of the transmitted packet");
14572
        test_fail("Wrong data of the transmitted packet");
14573 254 mohor
        fail = fail + 1;
14574
      end
14575 260 mohor
 
14576
      // check transmited TX packet CRC
14577
      if(tmp_len == 64)  // Control frame
14578
        #1 check_tx_crc(0, 60, 1'b0, tmp); // length without CRC
14579
      else
14580
        #1 check_tx_crc(0, 32'h100, 1'b0, tmp); // length without CRC
14581
 
14582
      if (tmp > 0)
14583 254 mohor
      begin
14584 260 mohor
        $display("Wrong CRC of the transmitted packet");
14585
        test_fail("Wrong CRC of the transmitted packet");
14586
        fail = fail + 1;
14587
      end
14588
 
14589
 
14590
      wait (MTxEn === 1'b1); // start transmit of the control frame
14591
      wait (MTxEn === 1'b0); // end transmit of the control frame
14592
      repeat(10) @ (posedge wb_clk);  // wait some time
14593
      repeat(10) @ (posedge mtx_clk); // wait some time so status is written
14594
 
14595
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14596
      if(data !== (`ETH_INT_TXC | `ETH_INT_TXB))
14597
      begin
14598 254 mohor
        test_fail("TXC and TXB IRQ should be set!");
14599
        fail = fail + 1;
14600 260 mohor
        `TIME; $display("*E TXC and TXB IRQ should be set! (ETH_INT=0x%0x)", data);
14601 254 mohor
      end
14602 260 mohor
 
14603
      if (!wb_int)
14604
      begin
14605
        test_fail("WB INT signal should be set!");
14606
        fail = fail + 1;
14607
        `TIME; $display("*E WB INT signal should be set!");
14608
      end
14609
 
14610
      // Clear TXC and TXB interrupt
14611
      wbm_write(`ETH_INT, `ETH_INT_TXC | `ETH_INT_TXB, 4'hF, 1, 4'h0, 4'h0);
14612
 
14613
      if (wb_int)
14614
      begin
14615
        test_fail("WB INT signal should not be set!");
14616
        fail = fail + 1;
14617
        `TIME; $display("*E WB INT signal should not be set!");
14618
      end
14619
 
14620
      if(tmp_len == 64)  // Control frame
14621
        check_tx_packet((`MEMORY_BASE), 0, 32'h100, tmp);
14622
      else
14623
        check_tx_packet((`MEMORY_BASE + 2 * max_tmp), 0, 60, tmp);
14624
 
14625
      if (tmp > 0)
14626
      begin
14627
        $display("Wrong data of the transmitted packet");
14628
        test_fail("Wrong data of the transmitted packet");
14629
        fail = fail + 1;
14630
      end
14631
 
14632
      // check transmited TX packet CRC
14633
      if(tmp_len == 64)  // Control frame
14634
        #1 check_tx_crc(0, 32'h100, 1'b0, tmp); // length without CRC
14635
      else
14636
        #1 check_tx_crc(0, 60, 1'b0, tmp); // length without CRC
14637
 
14638
      if (tmp > 0)
14639
      begin
14640
        $display("Wrong CRC of the transmitted packet");
14641
        test_fail("Wrong CRC of the transmitted packet");
14642
        fail = fail + 1;
14643
      end
14644
    end // for loop
14645 254 mohor
 
14646 260 mohor
    if(fail)
14647 254 mohor
    begin
14648 260 mohor
      test_name = "TEST 0: FINISHED WITH ERRORS";
14649
      `TIME; $display("  TEST 0: FINISHED WITH ERRORS");
14650 254 mohor
    end
14651 260 mohor
    else
14652 254 mohor
    begin
14653 260 mohor
      test_name = "TEST 0: SUCCESSFULLY FINISHED";
14654
      `TIME; $display("  TEST 0: SUCCESSFULLY FINISHED");
14655 254 mohor
    end
14656
 
14657 260 mohor
    if(fail == 0)
14658
      test_ok;
14659
    else
14660
      fail = 0;
14661 254 mohor
 
14662 260 mohor
  end
14663 254 mohor
 
14664
 
14665 263 mohor
  if (test_num == 1) // 
14666
  begin
14667
    #1;
14668
    // TEST 1: INSERT CONTROL FRM. WHILE TRANSMITTING NORMAL FRM. AT 4 TX BD ( 100Mbps )
14669
    test_name = "TEST 1: INSERT CONTROL FRM. WHILE TRANSMITTING NORMAL FRM. AT 4 TX BD ( 100Mbps )";
14670
    `TIME; $display("  TEST 1: INSERT CONTROL FRM. WHILE TRANSMITTING NORMAL FRM. AT 4 TX BD ( 100Mbps )");
14671
  end
14672 254 mohor
 
14673 260 mohor
  ////////////////////////////////////////////////////////////////////
14674
  ////                                                            ////
14675 267 mohor
  ////  Receive control frames with PASSALL option turned on and  ////
14676
  ////  off. Using only one RX buffer decriptor ( 10Mbps ).       ////
14677 260 mohor
  ////                                                            ////
14678
  ////////////////////////////////////////////////////////////////////
14679 263 mohor
  if (test_num == 2) // 
14680 260 mohor
  begin
14681 267 mohor
    // TEST 2: RECEIVE CONTROL FRAMES WITH PASSALL OPTION TURNED ON AND OFF AT ONE RX BD ( 10Mbps )
14682
    test_name   = "TEST 2: RECEIVE CONTROL FRAMES WITH PASSALL OPTION TURNED ON AND OFF AT ONE RX BD ( 10Mbps )";
14683
    `TIME; $display("  TEST 2: RECEIVE CONTROL FRAMES WITH PASSALL OPTION TURNED ON AND OFF AT ONE RX BD ( 10Mbps )");
14684 254 mohor
 
14685 260 mohor
    // unmask interrupts
14686
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
14687
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14688
    // set 1 RX buffer descriptor (8'h80 - 1) - must be set before RX enable
14689
    wbm_write(`ETH_TX_BD_NUM, 32'h7F, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14690
    // enable RX, set full-duplex mode, NO receive small, NO correct IFG
14691 267 mohor
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
14692 260 mohor
              `ETH_MODER_PRO | `ETH_MODER_BRO,
14693
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14694 263 mohor
    // enable RX_FLOW control
14695
    wbm_write(`ETH_CTRLMODER, `ETH_CTRLMODER_RXFLOW, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14696 260 mohor
    // prepare one packet of 100 bytes long
14697 267 mohor
//    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
//    append_rx_crc (64, 100, 1'b0, 1'b0); // CRC for data packet
14700
    st_data = 8'h01;
14701
    set_tx_packet(`MEMORY_BASE + 64, 100, 8'h01); // length without CRC
14702
    set_tx_bd(0, 0, 100, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + 64));
14703
    set_tx_bd_wrap(0);
14704
 
14705 260 mohor
    // check WB INT signal
14706
    if (wb_int !== 1'b0)
14707
    begin
14708
      test_fail("WB INT signal should not be set");
14709
      fail = fail + 1;
14710
    end
14711
 
14712
    // write to phy's control register for 10Mbps
14713
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
14714
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
14715
    speed = 10;
14716 254 mohor
 
14717 266 mohor
    // RXB and RXC interrupts masked
14718
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXE | `ETH_INT_BUSY |
14719
                             `ETH_INT_TXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14720
 
14721
    // Test irq logic while RXB and RXC interrupts are masked. IRQ in RxBD is cleared
14722
    for (i=0; i<3; i=i+1)
14723
    begin
14724 267 mohor
      pause_value = i+1;
14725
      set_rx_control_packet(0, pause_value);  // CRC already appended
14726 266 mohor
      // choose generating carrier sense and collision for first and last 64 lengths of frames
14727
      case (i)
14728
      0: // PASSALL = 0, RXFLOW = 1, IRQ in RxBD = 1
14729
      begin
14730
        PassAll=0; RxFlow=1; enable_irq_in_rxbd=1;
14731
        // enable interrupt generation
14732
        set_rx_bd(127, 127, 1'b1, `MEMORY_BASE);
14733
        // Set PASSALL = 0 and RXFLOW = 0
14734
        wbm_write(`ETH_CTRLMODER, `ETH_CTRLMODER_RXFLOW, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14735
      end
14736
      1: // PASSALL = 1, RXFLOW = 0, IRQ in RxBD = 1
14737
      begin
14738
        PassAll=1; RxFlow=0; enable_irq_in_rxbd=1;
14739
        // enable interrupt generation
14740
        set_rx_bd(127, 127, 1'b1, `MEMORY_BASE);
14741
        // Set PASSALL = 0 and RXFLOW = 0
14742
        wbm_write(`ETH_CTRLMODER, `ETH_CTRLMODER_PASSALL, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14743
      end
14744
      2: // PASSALL = 1, RXFLOW = 0, IRQ in RxBD = 0
14745
      begin
14746
        PassAll=1; RxFlow=0; enable_irq_in_rxbd=0;
14747
        // enable interrupt generation
14748
        set_rx_bd(127, 127, 1'b0, `MEMORY_BASE);
14749
        // Set PASSALL = 0 and RXFLOW = 0
14750
        wbm_write(`ETH_CTRLMODER, `ETH_CTRLMODER_PASSALL, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14751
      end
14752
      default:
14753
      begin
14754
        $display("*E We should never get here !!!");
14755
        test_fail("We should never get here !!!");
14756
        fail = fail + 1;
14757
      end
14758
      endcase
14759
 
14760
      // not detect carrier sense in FD and no collision
14761
      eth_phy.no_carrier_sense_rx_fd_detect(0);
14762
      eth_phy.collision(0);
14763
 
14764
      // set wrap bit and empty bit
14765
      set_rx_bd_wrap(127);
14766
      set_rx_bd_empty(127, 127);
14767
 
14768
      fork
14769
        begin
14770
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, 64, 1'b0);
14771
          repeat(10) @(posedge mrx_clk);
14772
        end
14773
        begin
14774
          wait (MRxDV === 1'b1); // start transmit
14775
          #1 check_rx_bd(127, data);
14776
          if (data[15] !== 1)
14777
          begin
14778
            $display("*E Wrong buffer descriptor's ready bit read out from MAC");
14779
            test_fail("Wrong buffer descriptor's ready bit read out from MAC");
14780
            fail = fail + 1;
14781
          end
14782 267 mohor
 
14783
          wait (MRxDV === 1'b0); // received pause frame
14784
          repeat(5) @(posedge mrx_clk);  // Wait some time so pause is activated.
14785
          repeat(5) @(posedge mtx_clk);  // Wait some time so pause is activated.
14786
 
14787
          set_tx_bd_ready(0, 0); // Request sending the data. Data should not be sent when pause frame was received
14788
                                 // and RxFlow enabled.
14789
 
14790
 
14791
          // When we exit the while loop, status frame is received
14792
          repeat(`ETH_TX_FIFO_DEPTH) @(eth_ma_wb_ack_i);  // Waiting until TX fifo is filled.
14793
          repeat(10) @(posedge mtx_clk);  // Wait some time for tx start.
14794 266 mohor
        end
14795 267 mohor
 
14796 266 mohor
      join
14797
 
14798
      #1 check_rx_bd(127, data);
14799
      // Checking buffer descriptor
14800
      if(PassAll)
14801
      begin
14802
        if(enable_irq_in_rxbd)
14803
        begin
14804
          if(data !== 32'h406100)    // Rx BD must not be marked as EMPTY (control frame is received)
14805
          begin
14806
            $display("*E Rx BD is not OK. Control frame should be received because PASSALL bit is 1");
14807
            $display("RxBD = 0x%0x", data);
14808
            test_fail("Rx BD is not OK. Control frame should be received because PASSALL bit is 1");
14809
            fail = fail + 1;
14810
          end
14811
        end
14812
        else
14813
        begin
14814
          if(data !== 32'h402100)    // Rx BD must not be marked as EMPTY (control frame is received)
14815
          begin
14816
            $display("*E Rx BD is not OK. Control frame should be received because PASSALL bit is 1");
14817
            $display("RxBD = 0x%0x", data);
14818
            test_fail("Rx BD is not OK. Control frame should be received because PASSALL bit is 1");
14819
            fail = fail + 1;
14820
          end
14821
        end
14822
      end
14823
      else
14824
      begin
14825
        if(data !== 32'he000)    // Rx BD must be marked as EMPTY (no packet received)
14826
        begin
14827
          $display("*E Rx BD should be marked as EMPTY because a control frame was received while PASSALL bit is 0");
14828
          $display("RxBD = 0x%0x", data);
14829
          test_fail("Rx BD should be marked as EMPTY because a control frame was received while PASSALL bit is 0");
14830
          fail = fail + 1;
14831
        end
14832
      end
14833
 
14834
      // Checking if interrupt was generated
14835
      if (wb_int)
14836
      begin
14837
        `TIME; $display("*E WB INT signal should not be set because both RXB and RXC interrupts are masked");
14838
        test_fail("WB INT signal should not be set because both RXB and RXC interrupts are masked");
14839
        fail = fail + 1;
14840
      end
14841
 
14842
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14843
      if(RxFlow)
14844
      begin
14845
        if(data !== (`ETH_INT_RXC))
14846
        begin
14847
          test_fail("RXC is not set or multiple IRQs active!");
14848
          fail = fail + 1;
14849
          `TIME; $display("*E RXC is not set or multiple IRQs active! (ETH_INT=0x%0x)", data);
14850
        end
14851
        // Clear RXC interrupt
14852
        wbm_write(`ETH_INT, `ETH_INT_RXC, 4'hF, 1, 4'h0, 4'h0);
14853
      end
14854
      else if(enable_irq_in_rxbd)
14855
      begin
14856
        if(data !== (`ETH_INT_RXB))
14857
        begin
14858
          test_fail("RXB is not set or multiple IRQs active!");
14859
          fail = fail + 1;
14860
          `TIME; $display("*E RXB is not set or multiple IRQs active! (ETH_INT=0x%0x)", data);
14861
        end
14862
        // Clear RXC interrupt
14863
        wbm_write(`ETH_INT, `ETH_INT_RXB, 4'hF, 1, 4'h0, 4'h0);
14864
      end
14865
      else
14866
      begin
14867
        if(data !== 0)
14868
        begin
14869
          test_fail("Some IRQs is active!");
14870
          fail = fail + 1;
14871
          `TIME; $display("*E Some IRQs is active! (ETH_INT=0x%0x)", data);
14872
        end
14873
      end
14874 267 mohor
 
14875
 
14876
      if(RxFlow)
14877
        begin
14878
          if(MTxEn)   // If pause frame was received OK, transmission of the data packet should not start
14879
            begin
14880
              `TIME; $display("*E Transmission should not be started because pause frame was received.");
14881
              test_fail("Transmission should not be started because pause frame was received.");
14882
              fail = fail + 1;
14883
            end
14884
          while(pause_value)
14885
            begin
14886
              pause_value=pause_value-1;
14887
              repeat(2*64) @(posedge mtx_clk);  // Wait for the time needed for the pause (1 slot).
14888
              if((!pause_value) && (!MTxEn))        // Transmission should be enabled now.
14889
                begin
14890
                  `TIME; $display("*E Transmission should be started because pause passed.");
14891
                  test_fail("Transmission should be started because pause passed.");
14892
                  fail = fail + 1;
14893
                end
14894
              else if((pause_value) && (MTxEn))     // Transmission should not be enabled now.
14895
                begin
14896
                  `TIME; $display("*E Transmission should still be paused.");
14897
                  test_fail("Transmission should still be paused.");
14898
                  fail = fail + 1;
14899
                end
14900
            end
14901
        end
14902
      else
14903
        begin
14904
          if(!MTxEn)   // Pause frame was not received because RxFlow is turned off.
14905
            begin
14906
              `TIME; $display("*E Transmission should be started because pause frame was not received (RxFlow=0).");
14907
              test_fail("Transmission should be started because pause frame was not received (RxFlow=0).");
14908
              fail = fail + 1;
14909
            end
14910
        end
14911
 
14912
 
14913
      wait(wb_int);   // Wait antil frame transmission is over and irq generated
14914
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14915
      if(data !== (`ETH_INT_TXB))
14916
        begin
14917
          test_fail("TXB is not set or multiple IRQs active!");
14918
          fail = fail + 1;
14919
          `TIME; $display("*E TXB is not set or multiple IRQs active! (ETH_INT=0x%0x)", data);
14920
        end
14921
        // Clear TXB interrupt
14922
        wbm_write(`ETH_INT, `ETH_INT_TXB, 4'hF, 1, 4'h0, 4'h0);
14923 266 mohor
    end
14924
    // End: Test is irq is set while RXB and RXC interrupts are masked.
14925
 
14926
 
14927
    // Now all interrupts are unmasked. Performing tests again.
14928 263 mohor
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
14929
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14930
 
14931 260 mohor
    for (i=0; i<4; i=i+1)
14932 254 mohor
    begin
14933 267 mohor
      pause_value = i+1;
14934
      set_rx_control_packet(0, pause_value);  // CRC already appended
14935 260 mohor
      // choose generating carrier sense and collision for first and last 64 lengths of frames
14936
      case (i)
14937 263 mohor
      0: // PASSALL = 0, RXFLOW = 0
14938 254 mohor
      begin
14939 263 mohor
        PassAll=0; RxFlow=0;
14940 260 mohor
        // enable interrupt generation
14941 263 mohor
        set_rx_bd(127, 127, 1'b1, `MEMORY_BASE);
14942
        // Set PASSALL = 0 and RXFLOW = 0
14943
        wbm_write(`ETH_CTRLMODER, 0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14944 254 mohor
      end
14945 263 mohor
      1: // PASSALL = 0, RXFLOW = 1
14946 254 mohor
      begin
14947 263 mohor
        PassAll=0; RxFlow=1;
14948 260 mohor
        // enable interrupt generation
14949 263 mohor
        set_rx_bd(127, 127, 1'b1, `MEMORY_BASE);
14950
        // Set PASSALL = 0 and RXFLOW = 0
14951
        wbm_write(`ETH_CTRLMODER, `ETH_CTRLMODER_RXFLOW, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14952 254 mohor
      end
14953 263 mohor
      2: // PASSALL = 1, RXFLOW = 0
14954 254 mohor
      begin
14955 263 mohor
        PassAll=1; RxFlow=0;
14956
        // enable interrupt generation
14957
        set_rx_bd(127, 127, 1'b1, `MEMORY_BASE);
14958
        // Set PASSALL = 0 and RXFLOW = 0
14959
        wbm_write(`ETH_CTRLMODER, `ETH_CTRLMODER_PASSALL, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14960 254 mohor
      end
14961 263 mohor
      default: // 3: PASSALL = 1, RXFLOW = 1
14962 254 mohor
      begin
14963 263 mohor
        PassAll=1; RxFlow=1;
14964
        // enable interrupt generation
14965
        set_rx_bd(127, 127, 1'b1, `MEMORY_BASE);
14966
        // Set PASSALL = 1 and RXFLOW = 1
14967
        wbm_write(`ETH_CTRLMODER, `ETH_CTRLMODER_PASSALL | `ETH_CTRLMODER_RXFLOW, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14968 254 mohor
      end
14969
      endcase
14970 260 mohor
 
14971 263 mohor
      // not detect carrier sense in FD and no collision
14972
      eth_phy.no_carrier_sense_rx_fd_detect(0);
14973
      eth_phy.collision(0);
14974
 
14975
      // set wrap bit and empty bit
14976 260 mohor
      set_rx_bd_wrap(127);
14977
      set_rx_bd_empty(127, 127);
14978 263 mohor
 
14979 260 mohor
      fork
14980 254 mohor
        begin
14981 260 mohor
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, 64, 1'b0);
14982
          repeat(10) @(posedge mrx_clk);
14983 254 mohor
        end
14984
        begin
14985 260 mohor
          #1 check_rx_bd(127, data);
14986
          wait (MRxDV === 1'b1); // start transmit
14987 263 mohor
          #1 check_rx_bd(127, data);
14988 260 mohor
          if (data[15] !== 1)
14989
          begin
14990 263 mohor
            $display("*E Wrong buffer descriptor's ready bit read out from MAC");
14991 260 mohor
            test_fail("Wrong buffer descriptor's ready bit read out from MAC");
14992
            fail = fail + 1;
14993
          end
14994 263 mohor
 
14995 260 mohor
          wait (MRxDV === 1'b0); // end transmit
14996 263 mohor
          repeat(50) @(posedge mrx_clk);  // Wait some time so frame is received and
14997
          repeat (100) @(posedge wb_clk); // status/irq is written.
14998 267 mohor
 
14999
          if(RxFlow)    // Waiting x slot times before continuing so pause is deactivated.
15000
            repeat(64 * 2 * pause_value) @(posedge mrx_clk);
15001 254 mohor
        end
15002 260 mohor
      join
15003 263 mohor
 
15004
      #1 check_rx_bd(127, data);
15005
 
15006
      // Checking buffer descriptor
15007
      if(PassAll)
15008 254 mohor
      begin
15009 263 mohor
        if(data !== 32'h406100)    // Rx BD must not be marked as EMPTY (control frame is received)
15010
        begin
15011
          $display("*E Rx BD is not OK. Control frame should be received because PASSALL bit is 1");
15012
          $display("RxBD = 0x%0x", data);
15013
          test_fail("Rx BD is not OK. Control frame should be received because PASSALL bit is 1");
15014
          fail = fail + 1;
15015
        end
15016 254 mohor
      end
15017 260 mohor
      else
15018 254 mohor
      begin
15019 263 mohor
        if(data !== 32'he000)    // Rx BD must be marked as EMPTY (no packet received)
15020
        begin
15021
          $display("*E Rx BD should be marked as EMPTY because a control frame was received while PASSALL bit is 0");
15022
          $display("RxBD = 0x%0x", data);
15023
          test_fail("Rx BD should be marked as EMPTY because a control frame was received while PASSALL bit is 0");
15024
          fail = fail + 1;
15025
        end
15026 254 mohor
      end
15027 263 mohor
 
15028
      // Checking if interrupt was generated
15029
      if(RxFlow || PassAll)
15030 254 mohor
      begin
15031 263 mohor
        if (!wb_int)
15032 254 mohor
        begin
15033
          `TIME; $display("*E WB INT signal should be set");
15034
          test_fail("WB INT signal should be set");
15035
          fail = fail + 1;
15036
        end
15037
      end
15038
      else
15039
      begin
15040 263 mohor
        if (wb_int)
15041 254 mohor
        begin
15042
          `TIME; $display("*E WB INT signal should not be set");
15043
          test_fail("WB INT signal should not be set");
15044
          fail = fail + 1;
15045
        end
15046
      end
15047 263 mohor
 
15048
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15049
      if(RxFlow)
15050 254 mohor
      begin
15051 263 mohor
        if(data !== (`ETH_INT_RXC))
15052 254 mohor
        begin
15053 263 mohor
          test_fail("RXC is not set or multiple IRQs active!");
15054 260 mohor
          fail = fail + 1;
15055 263 mohor
          `TIME; $display("*E RXC is not set or multiple IRQs active! (ETH_INT=0x%0x)", data);
15056 254 mohor
        end
15057 263 mohor
        // Clear RXC interrupt
15058
        wbm_write(`ETH_INT, `ETH_INT_RXC, 4'hF, 1, 4'h0, 4'h0);
15059 254 mohor
      end
15060 263 mohor
      else if(PassAll)
15061 254 mohor
      begin
15062 263 mohor
        if(data !== (`ETH_INT_RXB))
15063 254 mohor
        begin
15064 263 mohor
          test_fail("RXB is not set or multiple IRQs active!");
15065 254 mohor
          fail = fail + 1;
15066 263 mohor
          `TIME; $display("*E RXB is not set or multiple IRQs active! (ETH_INT=0x%0x)", data);
15067 254 mohor
        end
15068 263 mohor
        // Clear RXB interrupt
15069
        wbm_write(`ETH_INT, `ETH_INT_RXB, 4'hF, 1, 4'h0, 4'h0);
15070 254 mohor
      end
15071
      else
15072
      begin
15073 263 mohor
        if(data !== 0)
15074 254 mohor
        begin
15075 263 mohor
          test_fail("No interrupt should be set!");
15076 254 mohor
          fail = fail + 1;
15077 263 mohor
          `TIME; $display("*E No interrupt should be set! (ETH_INT=0x%0x)", data);
15078 254 mohor
        end
15079
      end
15080
    end
15081 266 mohor
 
15082 263 mohor
 
15083 260 mohor
    // disable RX
15084
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
15085
              `ETH_MODER_PRO | `ETH_MODER_BRO,
15086 254 mohor
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15087
    if(fail == 0)
15088
      test_ok;
15089
    else
15090
      fail = 0;
15091 209 tadejm
  end
15092
 
15093
 
15094
end   //  for (test_num=start_task; test_num <= end_task; test_num=test_num+1)
15095
 
15096 260 mohor
 
15097 209 tadejm
end
15098 263 mohor
endtask // test_mac_full_duplex_flow_control
15099 209 tadejm
 
15100
 
15101 169 mohor
//////////////////////////////////////////////////////////////
15102
// WB Behavioral Models Basic tasks
15103
//////////////////////////////////////////////////////////////
15104
 
15105
task wbm_write;
15106
  input  [31:0] address_i;
15107
  input  [((`MAX_BLK_SIZE * 32) - 1):0] data_i;
15108
  input  [3:0]  sel_i;
15109
  input  [31:0] size_i;
15110
  input  [3:0]  init_waits_i;
15111
  input  [3:0]  subseq_waits_i;
15112
 
15113
  reg `WRITE_STIM_TYPE write_data;
15114
  reg `WB_TRANSFER_FLAGS flags;
15115
  reg `WRITE_RETURN_TYPE write_status;
15116
  integer i;
15117
begin
15118
  write_status = 0;
15119
 
15120
  flags                    = 0;
15121
  flags`WB_TRANSFER_SIZE   = size_i;
15122
  flags`INIT_WAITS         = init_waits_i;
15123
  flags`SUBSEQ_WAITS       = subseq_waits_i;
15124
 
15125
  write_data               = 0;
15126
  write_data`WRITE_DATA    = data_i[31:0];
15127
  write_data`WRITE_ADDRESS = address_i;
15128
  write_data`WRITE_SEL     = sel_i;
15129
 
15130
  for (i = 0; i < size_i; i = i + 1)
15131
  begin
15132
    wb_master.blk_write_data[i] = write_data;
15133
    data_i                      = data_i >> 32;
15134
    write_data`WRITE_DATA       = data_i[31:0];
15135
    write_data`WRITE_ADDRESS    = write_data`WRITE_ADDRESS + 4;
15136 116 mohor
  end
15137
 
15138 169 mohor
  wb_master.wb_block_write(flags, write_status);
15139 116 mohor
 
15140 169 mohor
  if (write_status`CYC_ACTUAL_TRANSFER !== size_i)
15141
  begin
15142
    `TIME;
15143
    $display("*E WISHBONE Master was unable to complete the requested write operation to MAC!");
15144
  end
15145
end
15146
endtask // wbm_write
15147 116 mohor
 
15148 169 mohor
task wbm_read;
15149
  input  [31:0] address_i;
15150
  output [((`MAX_BLK_SIZE * 32) - 1):0] data_o;
15151
  input  [3:0]  sel_i;
15152
  input  [31:0] size_i;
15153
  input  [3:0]  init_waits_i;
15154
  input  [3:0]  subseq_waits_i;
15155
 
15156
  reg `READ_RETURN_TYPE read_data;
15157
  reg `WB_TRANSFER_FLAGS flags;
15158
  reg `READ_RETURN_TYPE read_status;
15159
  integer i;
15160
begin
15161
  read_status = 0;
15162
  data_o      = 0;
15163
 
15164
  flags                  = 0;
15165
  flags`WB_TRANSFER_SIZE = size_i;
15166
  flags`INIT_WAITS       = init_waits_i;
15167
  flags`SUBSEQ_WAITS     = subseq_waits_i;
15168
 
15169
  read_data              = 0;
15170
  read_data`READ_ADDRESS = address_i;
15171
  read_data`READ_SEL     = sel_i;
15172
 
15173
  for (i = 0; i < size_i; i = i + 1)
15174 116 mohor
  begin
15175 169 mohor
    wb_master.blk_read_data_in[i] = read_data;
15176
    read_data`READ_ADDRESS        = read_data`READ_ADDRESS + 4;
15177
  end
15178
 
15179
  wb_master.wb_block_read(flags, read_status);
15180
 
15181
  if (read_status`CYC_ACTUAL_TRANSFER !== size_i)
15182
  begin
15183
    `TIME;
15184
    $display("*E WISHBONE Master was unable to complete the requested read operation from MAC!");
15185
  end
15186
 
15187
  for (i = 0; i < size_i; i = i + 1)
15188
  begin
15189
    data_o       = data_o << 32;
15190
    read_data    = wb_master.blk_read_data_out[(size_i - 1) - i]; // [31 - i];
15191
    data_o[31:0] = read_data`READ_DATA;
15192
  end
15193
end
15194
endtask // wbm_read
15195
 
15196
 
15197
//////////////////////////////////////////////////////////////
15198
// Ethernet Basic tasks
15199
//////////////////////////////////////////////////////////////
15200
 
15201
task hard_reset; //  MAC registers
15202
begin
15203
  // reset MAC registers
15204
  @(posedge wb_clk);
15205
  #2 wb_rst = 1'b1;
15206
  repeat(2) @(posedge wb_clk);
15207
  #2 wb_rst = 1'b0;
15208
end
15209
endtask // hard_reset
15210
 
15211
task reset_mac; //  MAC module
15212
  reg [31:0] tmp;
15213
  reg [31:0] tmp_no_rst;
15214
begin
15215
  // read MODER register first
15216
  wbm_read(`ETH_MODER, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15217
  // set reset bit - write back to MODER register with RESET bit
15218
  wbm_write(`ETH_MODER, (`ETH_MODER_RST | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15219
  // clear reset bit - write back to MODER register without RESET bit
15220
  tmp_no_rst = `ETH_MODER_RST;
15221
  tmp_no_rst = ~tmp_no_rst;
15222
  wbm_write(`ETH_MODER, (tmp_no_rst & tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15223
end
15224
endtask // reset_mac
15225
 
15226
task set_tx_bd;
15227
  input  [6:0]  tx_bd_num_start;
15228
  input  [6:0]  tx_bd_num_end;
15229
  input  [15:0] len;
15230
  input         irq;
15231
  input         pad;
15232
  input         crc;
15233
  input  [31:0] txpnt;
15234
 
15235
  integer       i;
15236
  integer       bd_status_addr, bd_ptr_addr;
15237
//  integer       buf_addr;
15238
begin
15239
  for(i = tx_bd_num_start; i <= tx_bd_num_end; i = i + 1)
15240
  begin
15241
//    buf_addr = `TX_BUF_BASE + i * 32'h600;
15242
    bd_status_addr = `TX_BD_BASE + i * 8;
15243
    bd_ptr_addr = bd_status_addr + 4;
15244
    // initialize BD - status
15245
//    wbm_write(bd_status_addr, 32'h00005800, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // IRQ + PAD + CRC
15246
    wbm_write(bd_status_addr, {len, 1'b0, irq, 1'b0, pad, crc, 11'h0},
15247
              4'hF, 1, wbm_init_waits, wbm_subseq_waits); // IRQ + PAD + CRC
15248
    // initialize BD - pointer
15249
//    wbm_write(bd_ptr_addr, buf_addr, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // Initializing BD-pointer
15250
    wbm_write(bd_ptr_addr, txpnt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // Initializing BD-pointer
15251
  end
15252
end
15253
endtask // set_tx_bd
15254
 
15255
task set_tx_bd_wrap;
15256
  input  [6:0]  tx_bd_num_end;
15257
  integer       bd_status_addr, tmp;
15258
begin
15259
  bd_status_addr = `TX_BD_BASE + tx_bd_num_end * 8;
15260
  wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15261
  // set wrap bit to this BD - this BD should be last-one
15262
  wbm_write(bd_status_addr, (`ETH_TX_BD_WRAP | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15263
end
15264
endtask // set_tx_bd_wrap
15265
 
15266
task set_tx_bd_ready;
15267
  input  [6:0]  tx_nd_num_strat;
15268
  input  [6:0]  tx_bd_num_end;
15269
  integer       i;
15270
  integer       bd_status_addr, tmp;
15271
begin
15272
  for(i = tx_nd_num_strat; i <= tx_bd_num_end; i = i + 1)
15273
  begin
15274
    bd_status_addr = `TX_BD_BASE + i * 8;
15275
    wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15276
    // set empty bit to this BD - this BD should be ready
15277
    wbm_write(bd_status_addr, (`ETH_TX_BD_READY | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15278
  end
15279
end
15280
endtask // set_tx_bd_ready
15281
 
15282
task check_tx_bd;
15283
  input  [6:0]  tx_bd_num_end;
15284
  output [31:0] tx_bd_status;
15285
  integer       bd_status_addr, tmp;
15286
begin
15287
  bd_status_addr = `TX_BD_BASE + tx_bd_num_end * 8;
15288
  wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15289
  #1 tx_bd_status = tmp;
15290
  #1;
15291
end
15292
endtask // check_tx_bd
15293
 
15294
task clear_tx_bd;
15295
  input  [6:0]  tx_nd_num_strat;
15296
  input  [6:0]  tx_bd_num_end;
15297
  integer       i;
15298
  integer       bd_status_addr, bd_ptr_addr;
15299
begin
15300
  for(i = tx_nd_num_strat; i <= tx_bd_num_end; i = i + 1)
15301
  begin
15302
    bd_status_addr = `TX_BD_BASE + i * 8;
15303
    bd_ptr_addr = bd_status_addr + 4;
15304
    // clear BD - status
15305
    wbm_write(bd_status_addr, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15306
    // clear BD - pointer
15307
    wbm_write(bd_ptr_addr, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15308
  end
15309
end
15310
endtask // clear_tx_bd
15311
 
15312
task set_rx_bd;
15313
  input  [6:0]  rx_bd_num_strat;
15314
  input  [6:0]  rx_bd_num_end;
15315
  input         irq;
15316
  input  [31:0] rxpnt;
15317
//  input  [6:0]  rxbd_num;
15318
  integer       i;
15319
  integer       bd_status_addr, bd_ptr_addr;
15320
//  integer       buf_addr;
15321
begin
15322
  for(i = rx_bd_num_strat; i <= rx_bd_num_end; i = i + 1)
15323
  begin
15324
//    buf_addr = `RX_BUF_BASE + i * 32'h600;
15325 209 tadejm
//    bd_status_addr = `RX_BD_BASE + i * 8;
15326
//    bd_ptr_addr = bd_status_addr + 4; 
15327
    bd_status_addr = `TX_BD_BASE + i * 8;
15328
    bd_ptr_addr = bd_status_addr + 4;
15329 116 mohor
 
15330 169 mohor
    // initialize BD - status
15331
//    wbm_write(bd_status_addr, 32'h0000c000, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // IRQ + PAD + CRC
15332
    wbm_write(bd_status_addr, {17'h0, irq, 14'h0},
15333
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15334
    // initialize BD - pointer
15335
//    wbm_write(bd_ptr_addr, buf_addr, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // Initializing BD-pointer
15336
    wbm_write(bd_ptr_addr, rxpnt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // Initializing BD-pointer
15337
  end
15338
end
15339
endtask // set_rx_bd
15340 116 mohor
 
15341 169 mohor
task set_rx_bd_wrap;
15342
  input  [6:0]  rx_bd_num_end;
15343
  integer       bd_status_addr, tmp;
15344
begin
15345 209 tadejm
//  bd_status_addr = `RX_BD_BASE + rx_bd_num_end * 8;
15346
  bd_status_addr = `TX_BD_BASE + rx_bd_num_end * 8;
15347 169 mohor
  wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15348
  // set wrap bit to this BD - this BD should be last-one
15349
  wbm_write(bd_status_addr, (`ETH_RX_BD_WRAP | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15350
end
15351
endtask // set_rx_bd_wrap
15352 116 mohor
 
15353 169 mohor
task set_rx_bd_empty;
15354
  input  [6:0]  rx_bd_num_strat;
15355
  input  [6:0]  rx_bd_num_end;
15356
  integer       i;
15357
  integer       bd_status_addr, tmp;
15358
begin
15359
  for(i = rx_bd_num_strat; i <= rx_bd_num_end; i = i + 1)
15360
  begin
15361 209 tadejm
//    bd_status_addr = `RX_BD_BASE + i * 8;
15362
    bd_status_addr = `TX_BD_BASE + i * 8;
15363 169 mohor
    wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15364
    // set empty bit to this BD - this BD should be ready
15365
    wbm_write(bd_status_addr, (`ETH_RX_BD_EMPTY | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15366 116 mohor
  end
15367 169 mohor
end
15368
endtask // set_rx_bd_empty
15369 116 mohor
 
15370 169 mohor
task check_rx_bd;
15371
  input  [6:0]  rx_bd_num_end;
15372
  output [31:0] rx_bd_status;
15373
  integer       bd_status_addr, tmp;
15374
begin
15375 209 tadejm
//  bd_status_addr = `RX_BD_BASE + rx_bd_num_end * 8;
15376
  bd_status_addr = `TX_BD_BASE + rx_bd_num_end * 8;
15377 169 mohor
  wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15378
  #1 rx_bd_status = tmp;
15379
  #1;
15380
end
15381
endtask // check_rx_bd
15382 116 mohor
 
15383 169 mohor
task clear_rx_bd;
15384
  input  [6:0]  rx_bd_num_strat;
15385
  input  [6:0]  rx_bd_num_end;
15386
  integer       i;
15387
  integer       bd_status_addr, bd_ptr_addr;
15388
begin
15389
  for(i = rx_bd_num_strat; i <= rx_bd_num_end; i = i + 1)
15390
  begin
15391 209 tadejm
//    bd_status_addr = `RX_BD_BASE + i * 8;
15392
    bd_status_addr = `TX_BD_BASE + i * 8;
15393 169 mohor
    bd_ptr_addr = bd_status_addr + 4;
15394
    // clear BD - status
15395
    wbm_write(bd_status_addr, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15396
    // clear BD - pointer
15397
    wbm_write(bd_ptr_addr, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15398
  end
15399
end
15400
endtask // clear_rx_bd
15401 116 mohor
 
15402 169 mohor
task set_tx_packet;
15403
  input  [31:0] txpnt;
15404
  input  [15:0] len;
15405
  input  [7:0]  eth_start_data;
15406
  integer       i, sd;
15407
  integer       buffer;
15408
  reg           delta_t;
15409
begin
15410
  buffer = txpnt;
15411
  sd = eth_start_data;
15412
  delta_t = 0;
15413 116 mohor
 
15414 169 mohor
  // First write might not be word allign.
15415
  if(buffer[1:0] == 1)
15416
  begin
15417
    wb_slave.wr_mem(buffer - 1, {8'h0, sd[7:0], sd[7:0] + 3'h1, sd[7:0] + 3'h2}, 4'h7);
15418
    sd = sd + 3;
15419
    i = 3;
15420
  end
15421
  else if(buffer[1:0] == 2)
15422
  begin
15423
    wb_slave.wr_mem(buffer - 2, {16'h0, sd[7:0], sd[7:0] + 3'h1}, 4'h3);
15424
    sd = sd + 2;
15425
    i = 2;
15426
  end
15427
  else if(buffer[1:0] == 3)
15428
  begin
15429
    wb_slave.wr_mem(buffer - 3, {24'h0, sd[7:0]}, 4'h1);
15430
    sd = sd + 1;
15431
    i = 1;
15432
  end
15433
  else
15434
    i = 0;
15435
  delta_t = !delta_t;
15436 116 mohor
 
15437 169 mohor
  for(i = i; i < (len - 4); i = i + 4) // Last 0-3 bytes are not written
15438
  begin
15439
    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);
15440
    sd = sd + 4;
15441
  end
15442
  delta_t = !delta_t;
15443
 
15444
  // Last word
15445
  if((len - i) == 3)
15446 116 mohor
  begin
15447 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);
15448
  end
15449
  else if((len - i) == 2)
15450
  begin
15451
    wb_slave.wr_mem(buffer + i, {sd[7:0], sd[7:0] + 3'h1, 16'h0}, 4'hC);
15452
  end
15453
  else if((len - i) == 1)
15454
  begin
15455
    wb_slave.wr_mem(buffer + i, {sd[7:0], 24'h0}, 4'h8);
15456
  end
15457
  else if((len - i) == 4)
15458
  begin
15459
    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);
15460
  end
15461
  else
15462
    $display("(%0t)(%m) ERROR", $time);
15463
  delta_t = !delta_t;
15464
end
15465
endtask // set_tx_packet
15466
 
15467
task check_tx_packet;
15468
  input  [31:0] txpnt_wb;  // source
15469
  input  [31:0] txpnt_phy; // destination
15470
  input  [15:0] len;
15471
  output [31:0] failure;
15472
  integer       i, data_wb, data_phy;
15473
  reg    [31:0] addr_wb, addr_phy;
15474
  reg    [31:0] failure;
15475
  reg           delta_t;
15476
begin
15477
  addr_wb = txpnt_wb;
15478
  addr_phy = txpnt_phy;
15479
  delta_t = 0;
15480
  failure = 0;
15481 209 tadejm
  #1;
15482 169 mohor
  // First write might not be word allign.
15483
  if(addr_wb[1:0] == 1)
15484
  begin
15485
    wb_slave.rd_mem(addr_wb - 1, data_wb, 4'h7);
15486
    data_phy[31:24] = 0;
15487
    data_phy[23:16] = eth_phy.tx_mem[addr_phy[21:0]];
15488
    data_phy[15: 8] = eth_phy.tx_mem[addr_phy[21:0] + 1];
15489
    data_phy[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0] + 2];
15490
    i = 3;
15491
    if (data_phy[23:0] !== data_wb[23:0])
15492
    begin
15493
      `TIME;
15494 209 tadejm
      $display("*E Wrong 1. word (3 bytes) of TX packet! phy: %0h, wb: %0h", data_phy[23:0], data_wb[23:0]);
15495
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15496 169 mohor
      failure = 1;
15497
    end
15498
  end
15499
  else if (addr_wb[1:0] == 2)
15500
  begin
15501
    wb_slave.rd_mem(addr_wb - 2, data_wb, 4'h3);
15502
    data_phy[31:16] = 0;
15503
    data_phy[15: 8] = eth_phy.tx_mem[addr_phy[21:0]];
15504
    data_phy[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0] + 1];
15505
    i = 2;
15506
    if (data_phy[15:0] !== data_wb[15:0])
15507
    begin
15508
      `TIME;
15509 209 tadejm
      $display("*E Wrong 1. word (2 bytes) of TX packet! phy: %0h, wb: %0h", data_phy[15:0], data_wb[15:0]);
15510
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15511 169 mohor
      failure = 1;
15512
    end
15513
  end
15514
  else if (addr_wb[1:0] == 3)
15515
  begin
15516
    wb_slave.rd_mem(addr_wb - 3, data_wb, 4'h1);
15517
    data_phy[31: 8] = 0;
15518
    data_phy[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0]];
15519
    i = 1;
15520
    if (data_phy[7:0] !== data_wb[7:0])
15521
    begin
15522
      `TIME;
15523 209 tadejm
      $display("*E Wrong 1. word (1 byte) of TX packet! phy: %0h, wb: %0h", data_phy[7:0], data_wb[7:0]);
15524
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15525 169 mohor
      failure = 1;
15526
    end
15527
  end
15528
  else
15529
    i = 0;
15530
  delta_t = !delta_t;
15531 209 tadejm
  #1;
15532 169 mohor
  for(i = i; i < (len - 4); i = i + 4) // Last 0-3 bytes are not checked
15533
  begin
15534
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hF);
15535
    data_phy[31:24] = eth_phy.tx_mem[addr_phy[21:0] + i];
15536
    data_phy[23:16] = eth_phy.tx_mem[addr_phy[21:0] + i + 1];
15537
    data_phy[15: 8] = eth_phy.tx_mem[addr_phy[21:0] + i + 2];
15538
    data_phy[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0] + i + 3];
15539
    if (data_phy[31:0] !== data_wb[31:0])
15540
    begin
15541
      `TIME;
15542 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]);
15543
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15544 169 mohor
      failure = failure + 1;
15545
    end
15546
  end
15547
  delta_t = !delta_t;
15548 209 tadejm
  #1;
15549 169 mohor
  // Last word
15550
  if((len - i) == 3)
15551
  begin
15552
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hE);
15553
    data_phy[31:24] = eth_phy.tx_mem[addr_phy[21:0] + i];
15554
    data_phy[23:16] = eth_phy.tx_mem[addr_phy[21:0] + i + 1];
15555
    data_phy[15: 8] = eth_phy.tx_mem[addr_phy[21:0] + i + 2];
15556
    data_phy[ 7: 0] = 0;
15557
    if (data_phy[31:8] !== data_wb[31:8])
15558
    begin
15559
      `TIME;
15560 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]);
15561
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15562 169 mohor
      failure = failure + 1;
15563
    end
15564
  end
15565
  else if((len - i) == 2)
15566
  begin
15567
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hC);
15568
    data_phy[31:24] = eth_phy.tx_mem[addr_phy[21:0] + i];
15569
    data_phy[23:16] = eth_phy.tx_mem[addr_phy[21:0] + i + 1];
15570
    data_phy[15: 8] = 0;
15571
    data_phy[ 7: 0] = 0;
15572
    if (data_phy[31:16] !== data_wb[31:16])
15573
    begin
15574
      `TIME;
15575 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]);
15576
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15577 169 mohor
      failure = failure + 1;
15578
    end
15579
  end
15580
  else if((len - i) == 1)
15581
  begin
15582
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'h8);
15583
    data_phy[31:24] = eth_phy.tx_mem[addr_phy[21:0] + i];
15584
    data_phy[23:16] = 0;
15585
    data_phy[15: 8] = 0;
15586
    data_phy[ 7: 0] = 0;
15587
    if (data_phy[31:24] !== data_wb[31:24])
15588
    begin
15589
      `TIME;
15590 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]);
15591
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15592 169 mohor
      failure = failure + 1;
15593
    end
15594
  end
15595
  else if((len - i) == 4)
15596
  begin
15597
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hF);
15598
    data_phy[31:24] = eth_phy.tx_mem[addr_phy[21:0] + i];
15599
    data_phy[23:16] = eth_phy.tx_mem[addr_phy[21:0] + i + 1];
15600
    data_phy[15: 8] = eth_phy.tx_mem[addr_phy[21:0] + i + 2];
15601
    data_phy[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0] + i + 3];
15602
    if (data_phy[31:0] !== data_wb[31:0])
15603
    begin
15604
      `TIME;
15605 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]);
15606
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15607 169 mohor
      failure = failure + 1;
15608
    end
15609
  end
15610
  else
15611
    $display("(%0t)(%m) ERROR", $time);
15612
  delta_t = !delta_t;
15613
end
15614
endtask // check_tx_packet
15615
 
15616
task set_rx_packet;
15617
  input  [31:0] rxpnt;
15618
  input  [15:0] len;
15619
  input         plus_dribble_nibble; // if length is longer for one nibble
15620
  input  [47:0] eth_dest_addr;
15621
  input  [47:0] eth_source_addr;
15622
  input  [15:0] eth_type_len;
15623
  input  [7:0]  eth_start_data;
15624
  integer       i, sd;
15625
  reg    [47:0] dest_addr;
15626
  reg    [47:0] source_addr;
15627
  reg    [15:0] type_len;
15628
  reg    [21:0] buffer;
15629
  reg           delta_t;
15630
begin
15631
  buffer = rxpnt[21:0];
15632
  dest_addr = eth_dest_addr;
15633
  source_addr = eth_source_addr;
15634
  type_len = eth_type_len;
15635
  sd = eth_start_data;
15636
  delta_t = 0;
15637
  for(i = 0; i < len; i = i + 1)
15638
  begin
15639
    if (i < 6)
15640
    begin
15641
      eth_phy.rx_mem[buffer] = dest_addr[47:40];
15642
      dest_addr = dest_addr << 8;
15643
    end
15644
    else if (i < 12)
15645
    begin
15646
      eth_phy.rx_mem[buffer] = source_addr[47:40];
15647
      source_addr = source_addr << 8;
15648
    end
15649
    else if (i < 14)
15650
    begin
15651
      eth_phy.rx_mem[buffer] = type_len[15:8];
15652
      type_len = type_len << 8;
15653
    end
15654
    else
15655
    begin
15656
      eth_phy.rx_mem[buffer] = sd[7:0];
15657
      sd = sd + 1;
15658
    end
15659
    buffer = buffer + 1;
15660
  end
15661
  delta_t = !delta_t;
15662
  if (plus_dribble_nibble)
15663
    eth_phy.rx_mem[buffer] = {4'h0, 4'hD /*sd[3:0]*/};
15664
  delta_t = !delta_t;
15665
end
15666
endtask // set_rx_packet
15667
 
15668 267 mohor
task set_rx_control_packet;
15669
  input  [31:0] rxpnt;
15670
  input  [15:0] PauseTV;
15671
  integer       i;
15672
  reg    [47:0] dest_addr;
15673
  reg    [47:0] source_addr;
15674
  reg    [15:0] type_len;
15675
  reg    [21:0] buffer;
15676
  reg           delta_t;
15677
  reg    [15:0] PTV;
15678
  reg    [15:0] opcode;
15679
begin
15680
  buffer = rxpnt[21:0];
15681
  dest_addr = 48'h0180_c200_0001;
15682
  source_addr = 48'h0708_090A_0B0C;
15683
  type_len = 16'h8808;
15684
  opcode = 16'h0001;
15685
  PTV = PauseTV;
15686
  delta_t = 0;
15687
  for(i = 0; i < 60; i = i + 1)
15688
  begin
15689
    if (i < 6)
15690
    begin
15691
      eth_phy.rx_mem[buffer] = dest_addr[47:40];
15692
      dest_addr = dest_addr << 8;
15693
    end
15694
    else if (i < 12)
15695
    begin
15696
      eth_phy.rx_mem[buffer] = source_addr[47:40];
15697
      source_addr = source_addr << 8;
15698
    end
15699
    else if (i < 14)
15700
    begin
15701
      eth_phy.rx_mem[buffer] = type_len[15:8];
15702
      type_len = type_len << 8;
15703
    end
15704
    else if (i < 16)
15705
    begin
15706
      eth_phy.rx_mem[buffer] = opcode[15:8];
15707
      opcode = opcode << 8;
15708
    end
15709
    else if (i < 18)
15710
    begin
15711
      eth_phy.rx_mem[buffer] = PTV[15:8];
15712
      PTV = PTV << 8;
15713
    end
15714
    else
15715
    begin
15716
      eth_phy.rx_mem[buffer] = 0;
15717
    end
15718
    buffer = buffer + 1;
15719
  end
15720
  delta_t = !delta_t;
15721
  append_rx_crc (rxpnt, 60, 1'b0, 1'b0); // CRC for control packet
15722
end
15723
endtask // set_rx_control_packet
15724
 
15725 243 tadejm
task set_rx_addr_type;
15726
  input  [31:0] rxpnt;
15727
  input  [47:0] eth_dest_addr;
15728
  input  [47:0] eth_source_addr;
15729
  input  [15:0] eth_type_len;
15730
  integer       i;
15731
  reg    [47:0] dest_addr;
15732
  reg    [47:0] source_addr;
15733
  reg    [15:0] type_len;
15734
  reg    [21:0] buffer;
15735
  reg           delta_t;
15736
begin
15737
  buffer = rxpnt[21:0];
15738
  dest_addr = eth_dest_addr;
15739
  source_addr = eth_source_addr;
15740
  type_len = eth_type_len;
15741
  delta_t = 0;
15742
  for(i = 0; i < 14; i = i + 1)
15743
  begin
15744
    if (i < 6)
15745
    begin
15746
      eth_phy.rx_mem[buffer] = dest_addr[47:40];
15747
      dest_addr = dest_addr << 8;
15748
    end
15749
    else if (i < 12)
15750
    begin
15751
      eth_phy.rx_mem[buffer] = source_addr[47:40];
15752
      source_addr = source_addr << 8;
15753
    end
15754
    else // if (i < 14)
15755
    begin
15756
      eth_phy.rx_mem[buffer] = type_len[15:8];
15757
      type_len = type_len << 8;
15758
    end
15759
    buffer = buffer + 1;
15760
  end
15761
  delta_t = !delta_t;
15762
end
15763
endtask // set_rx_addr_type
15764
 
15765 169 mohor
task check_rx_packet;
15766
  input  [31:0] rxpnt_phy; // source
15767
  input  [31:0] rxpnt_wb;  // destination
15768
  input  [15:0] len;
15769
  input         plus_dribble_nibble; // if length is longer for one nibble
15770
  input         successful_dribble_nibble; // if additional nibble is stored into memory
15771
  output [31:0] failure;
15772
  integer       i, data_wb, data_phy;
15773
  reg    [31:0] addr_wb, addr_phy;
15774
  reg    [31:0] failure;
15775
  reg    [21:0] buffer;
15776
  reg           delta_t;
15777
begin
15778
  addr_phy = rxpnt_phy;
15779
  addr_wb = rxpnt_wb;
15780
  delta_t = 0;
15781
  failure = 0;
15782
 
15783
  // First write might not be word allign.
15784
  if(addr_wb[1:0] == 1)
15785
  begin
15786
    wb_slave.rd_mem(addr_wb - 1, data_wb, 4'h7);
15787
    data_phy[31:24] = 0;
15788
    data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0]];
15789
    data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0] + 1];
15790
    data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0] + 2];
15791
    i = 3;
15792
    if (data_phy[23:0] !== data_wb[23:0])
15793
    begin
15794
      `TIME;
15795 243 tadejm
      $display("   addr_phy = %h, addr_wb = %h", rxpnt_phy, rxpnt_wb);
15796
      $display("*E Wrong 1. word (3 bytes) of RX packet! phy = %h, wb = %h", data_phy[23:0], data_wb[23:0]);
15797 169 mohor
      failure = 1;
15798
    end
15799
  end
15800
  else if (addr_wb[1:0] == 2)
15801
  begin
15802
    wb_slave.rd_mem(addr_wb - 2, data_wb, 4'h3);
15803
    data_phy[31:16] = 0;
15804
    data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0]];
15805
    data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0] + 1];
15806
    i = 2;
15807
    if (data_phy[15:0] !== data_wb[15:0])
15808
    begin
15809
      `TIME;
15810 243 tadejm
      $display("   addr_phy = %h, addr_wb = %h", rxpnt_phy, rxpnt_wb);
15811
      $display("*E Wrong 1. word (2 bytes) of RX packet! phy = %h, wb = %h", data_phy[15:0], data_wb[15:0]);
15812 169 mohor
      failure = 1;
15813
    end
15814
  end
15815
  else if (addr_wb[1:0] == 3)
15816
  begin
15817
    wb_slave.rd_mem(addr_wb - 3, data_wb, 4'h1);
15818
    data_phy[31: 8] = 0;
15819
    data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0]];
15820
    i = 1;
15821
    if (data_phy[7:0] !== data_wb[7:0])
15822
    begin
15823
      `TIME;
15824 243 tadejm
      $display("   addr_phy = %h, addr_wb = %h", rxpnt_phy, rxpnt_wb);
15825
      $display("*E Wrong 1. word (1 byte) of RX packet! phy = %h, wb = %h", data_phy[7:0], data_wb[7:0]);
15826 169 mohor
      failure = 1;
15827
    end
15828
  end
15829
  else
15830
    i = 0;
15831
  delta_t = !delta_t;
15832
 
15833
  for(i = i; i < (len - 4); i = i + 4) // Last 0-3 bytes are not checked
15834
  begin
15835
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hF);
15836
    data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i];
15837
    data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0] + i + 1];
15838
    data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0] + i + 2];
15839
    data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0] + i + 3];
15840
    if (data_phy[31:0] !== data_wb[31:0])
15841
    begin
15842
      `TIME;
15843 243 tadejm
      if (i == 0)
15844
        $display("   addr_phy = %h, addr_wb = %h", rxpnt_phy, rxpnt_wb);
15845
      $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]);
15846 169 mohor
      failure = failure + 1;
15847
    end
15848
  end
15849
  delta_t = !delta_t;
15850
 
15851
  // Last word
15852
  if((len - i) == 3)
15853
  begin
15854
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hF);
15855
    data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i];
15856
    data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0] + i + 1];
15857
    data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0] + i + 2];
15858
    if (plus_dribble_nibble)
15859
      data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0] + i + 3];
15860
    else
15861
      data_phy[ 7: 0] = 0;
15862
    if (data_phy[31:8] !== data_wb[31:8])
15863
    begin
15864
      `TIME;
15865 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]);
15866 169 mohor
      failure = failure + 1;
15867
    end
15868
    if (plus_dribble_nibble && successful_dribble_nibble)
15869
    begin
15870
      if (data_phy[3:0] !== data_wb[3:0])
15871 116 mohor
      begin
15872 169 mohor
        `TIME;
15873 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (3 bytes) of RX packet!", ((i/4)+1));
15874 169 mohor
        failure = failure + 1;
15875 116 mohor
      end
15876 169 mohor
    end
15877
    else if (plus_dribble_nibble && !successful_dribble_nibble)
15878
    begin
15879
      if (data_phy[3:0] === data_wb[3:0])
15880 116 mohor
      begin
15881 169 mohor
        `TIME;
15882 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (3 bytes) of RX packet!", ((i/4)+1));
15883 169 mohor
        failure = failure + 1;
15884 116 mohor
      end
15885 169 mohor
    end
15886
  end
15887
  else if((len - i) == 2)
15888
  begin
15889
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hE);
15890
    data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i];
15891
    data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0] + i + 1];
15892
    if (plus_dribble_nibble)
15893
      data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0] + i + 2];
15894
    else
15895
      data_phy[15: 8] = 0;
15896
    data_phy[ 7: 0] = 0;
15897
    if (data_phy[31:16] !== data_wb[31:16])
15898
    begin
15899
      `TIME;
15900 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]);
15901 169 mohor
      failure = failure + 1;
15902
    end
15903
    if (plus_dribble_nibble && successful_dribble_nibble)
15904
    begin
15905
      if (data_phy[11:8] !== data_wb[11:8])
15906
      begin
15907
        `TIME;
15908 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (2 bytes) of RX packet!", ((i/4)+1));
15909 169 mohor
        failure = failure + 1;
15910
      end
15911
    end
15912
    else if (plus_dribble_nibble && !successful_dribble_nibble)
15913
    begin
15914
      if (data_phy[11:8] === data_wb[11:8])
15915
      begin
15916
        `TIME;
15917 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (2 bytes) of RX packet!", ((i/4)+1));
15918 169 mohor
        failure = failure + 1;
15919
      end
15920
    end
15921
  end
15922
  else if((len - i) == 1)
15923
  begin
15924
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hC);
15925
    data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i];
15926
    if (plus_dribble_nibble)
15927
      data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0] + i + 1];
15928
    else
15929
      data_phy[23:16] = 0;
15930
    data_phy[15: 8] = 0;
15931
    data_phy[ 7: 0] = 0;
15932
    if (data_phy[31:24] !== data_wb[31:24])
15933
    begin
15934
      `TIME;
15935 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]);
15936 169 mohor
      failure = failure + 1;
15937
    end
15938
    if (plus_dribble_nibble && successful_dribble_nibble)
15939
    begin
15940
      if (data_phy[19:16] !== data_wb[19:16])
15941
      begin
15942
        `TIME;
15943 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (1 byte) of RX packet!", ((i/4)+1));
15944 169 mohor
        failure = failure + 1;
15945
      end
15946
    end
15947
    else if (plus_dribble_nibble && !successful_dribble_nibble)
15948
    begin
15949
      if (data_phy[19:16] === data_wb[19:16])
15950
      begin
15951
        `TIME;
15952 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (1 byte) of RX packet!", ((i/4)+1));
15953 169 mohor
        failure = failure + 1;
15954
      end
15955
    end
15956
  end
15957
  else if((len - i) == 4)
15958
  begin
15959
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hF);
15960
    data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i];
15961
    data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0] + i + 1];
15962
    data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0] + i + 2];
15963
    data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0] + i + 3];
15964
    if (data_phy[31:0] !== data_wb[31:0])
15965
    begin
15966
      `TIME;
15967 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]);
15968 169 mohor
      failure = failure + 1;
15969
    end
15970
    if (plus_dribble_nibble)
15971
    begin
15972
      wb_slave.rd_mem(addr_wb + i + 4, data_wb, 4'h8);
15973
      data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i + 4];
15974
      if (successful_dribble_nibble)
15975
      begin
15976
        if (data_phy[27:24] !== data_wb[27:24])
15977
        begin
15978
          `TIME;
15979 243 tadejm
          $display("*E Wrong dribble nibble in %0d. word (0 bytes) of RX packet!", ((i/4)+2));
15980 169 mohor
          failure = failure + 1;
15981
        end
15982
      end
15983
      else
15984
      begin
15985
        if (data_phy[27:24] === data_wb[27:24])
15986
        begin
15987
          `TIME;
15988 243 tadejm
          $display("*E Wrong dribble nibble in %0d. word (0 bytes) of RX packet!", ((i/4)+2));
15989 169 mohor
          failure = failure + 1;
15990
        end
15991
      end
15992
    end
15993
  end
15994
  else
15995
    $display("(%0t)(%m) ERROR", $time);
15996
  delta_t = !delta_t;
15997
end
15998
endtask // check_rx_packet
15999 116 mohor
 
16000 169 mohor
//////////////////////////////////////////////////////////////
16001
// Ethernet CRC Basic tasks
16002
//////////////////////////////////////////////////////////////
16003
 
16004
task append_tx_crc;
16005
  input  [31:0] txpnt_wb;  // source
16006
  input  [15:0] len; // length in bytes without CRC
16007
  input         negated_crc; // if appended CRC is correct or not
16008
  reg    [31:0] crc;
16009
  reg    [31:0] addr_wb;
16010
  reg           delta_t;
16011
begin
16012 243 tadejm
  addr_wb = txpnt_wb + {16'h0, len};
16013 169 mohor
  delta_t = 0;
16014
  // calculate CRC from prepared packet
16015
  paralel_crc_mac(txpnt_wb, {16'h0, len}, 1'b0, crc);
16016
  if (negated_crc)
16017
    crc = ~crc;
16018
  delta_t = !delta_t;
16019
 
16020
  // Write might not be word allign.
16021
  if (addr_wb[1:0] == 1)
16022
  begin
16023
    wb_slave.wr_mem(addr_wb - 1, {8'h0, crc[7:0], crc[15:8], crc[23:16]}, 4'h7);
16024
    wb_slave.wr_mem(addr_wb + 3, {crc[31:24], 24'h0}, 4'h8);
16025 116 mohor
  end
16026 169 mohor
  else if (addr_wb[1:0] == 2)
16027
  begin
16028
    wb_slave.wr_mem(addr_wb - 2, {16'h0, crc[7:0], crc[15:8]}, 4'h3);
16029
    wb_slave.wr_mem(addr_wb + 2, {crc[23:16], crc[31:24], 16'h0}, 4'hC);
16030
  end
16031
  else if (addr_wb[1:0] == 3)
16032
  begin
16033
    wb_slave.wr_mem(addr_wb - 3, {24'h0, crc[7:0]}, 4'h1);
16034
    wb_slave.wr_mem(addr_wb + 1, {crc[15:8], crc[23:16], crc[31:24], 8'h0}, 4'hE);
16035
  end
16036
  else
16037
  begin
16038
    wb_slave.wr_mem(addr_wb, {crc[7:0], crc[15:8], crc[23:16], crc[31:24]}, 4'hF);
16039
  end
16040
  delta_t = !delta_t;
16041
end
16042
endtask // append_tx_crc
16043 116 mohor
 
16044 169 mohor
task check_tx_crc; // used to check crc added to TX packets by MAC
16045
  input  [31:0] txpnt_phy; // destination
16046
  input  [15:0] len; // length in bytes without CRC
16047
  input         negated_crc; // if appended CRC is correct or not
16048
  output [31:0] failure;
16049
  reg    [31:0] failure;
16050
  reg    [31:0] crc_calc;
16051
  reg    [31:0] crc;
16052
  reg    [31:0] addr_phy;
16053
  reg           delta_t;
16054
begin
16055
  addr_phy = txpnt_phy;
16056
  failure = 0;
16057
  // calculate CRC from sent packet
16058
//  serial_crc_phy_tx(addr_phy, {16'h0, len}, 1'b0, crc_calc);
16059
//#10;
16060
  paralel_crc_phy_tx(addr_phy, {16'h0, len}, 1'b0, crc_calc);
16061 209 tadejm
  #1;
16062 169 mohor
  addr_phy = addr_phy + len;
16063
  // Read CRC - BIG endian
16064
  crc[31:24] = eth_phy.tx_mem[addr_phy[21:0]];
16065
  crc[23:16] = eth_phy.tx_mem[addr_phy[21:0] + 1];
16066
  crc[15: 8] = eth_phy.tx_mem[addr_phy[21:0] + 2];
16067
  crc[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0] + 3];
16068
 
16069
  delta_t = !delta_t;
16070
  if (negated_crc)
16071
  begin
16072
    if ((~crc_calc) !== crc)
16073
    begin
16074
      `TIME;
16075
      $display("*E Negated CRC was not successfuly transmitted!");
16076
      failure = failure + 1;
16077
    end
16078
  end
16079
  else
16080
  begin
16081
    if (crc_calc !== crc)
16082
    begin
16083
      `TIME;
16084 209 tadejm
      $display("*E Transmitted CRC was not correct; crc_calc: %0h, crc_mem: %0h", crc_calc, crc);
16085 169 mohor
      failure = failure + 1;
16086
    end
16087
  end
16088
  delta_t = !delta_t;
16089
end
16090
endtask // check_tx_crc
16091
 
16092
task append_rx_crc;
16093
  input  [31:0] rxpnt_phy; // source
16094
  input  [15:0] len; // length in bytes without CRC
16095
  input         plus_dribble_nibble; // if length is longer for one nibble
16096
  input         negated_crc; // if appended CRC is correct or not
16097
  reg    [31:0] crc;
16098
  reg    [7:0]  tmp;
16099
  reg    [31:0] addr_phy;
16100
  reg           delta_t;
16101
begin
16102
  addr_phy = rxpnt_phy + len;
16103
  delta_t = 0;
16104
  // calculate CRC from prepared packet
16105
  paralel_crc_phy_rx(rxpnt_phy, {16'h0, len}, plus_dribble_nibble, crc);
16106
  if (negated_crc)
16107
    crc = ~crc;
16108
  delta_t = !delta_t;
16109
 
16110
  if (plus_dribble_nibble)
16111
  begin
16112
    tmp = eth_phy.rx_mem[addr_phy];
16113 209 tadejm
    eth_phy.rx_mem[addr_phy]     = {crc[27:24], tmp[3:0]};
16114
    eth_phy.rx_mem[addr_phy + 1] = {crc[19:16], crc[31:28]};
16115
    eth_phy.rx_mem[addr_phy + 2] = {crc[11:8], crc[23:20]};
16116
    eth_phy.rx_mem[addr_phy + 3] = {crc[3:0], crc[15:12]};
16117
    eth_phy.rx_mem[addr_phy + 4] = {4'h0, crc[7:4]};
16118 169 mohor
  end
16119
  else
16120
  begin
16121 209 tadejm
    eth_phy.rx_mem[addr_phy]     = crc[31:24];
16122
    eth_phy.rx_mem[addr_phy + 1] = crc[23:16];
16123
    eth_phy.rx_mem[addr_phy + 2] = crc[15:8];
16124
    eth_phy.rx_mem[addr_phy + 3] = crc[7:0];
16125 169 mohor
  end
16126
end
16127
endtask // append_rx_crc
16128
 
16129
// paralel CRC checking for PHY TX
16130
task paralel_crc_phy_tx;
16131
  input  [31:0] start_addr; // start address
16132
  input  [31:0] len; // length of frame in Bytes without CRC length
16133
  input         plus_dribble_nibble; // if length is longer for one nibble
16134
  output [31:0] crc_out;
16135
  reg    [21:0] addr_cnt; // only 22 address lines
16136
  integer       word_cnt;
16137
  integer       nibble_cnt;
16138
  reg    [31:0] load_reg;
16139
  reg           delta_t;
16140
  reg    [31:0] crc_next;
16141
  reg    [31:0] crc;
16142
  reg           crc_error;
16143
  reg     [3:0] data_in;
16144
  integer       i;
16145
begin
16146
  #1 addr_cnt = start_addr[21:0];
16147
  word_cnt = 24; // 27; // start of the frame - nibble granularity (MSbit first)
16148
  crc = 32'hFFFF_FFFF; // INITIAL value
16149
  delta_t = 0;
16150
  // length must include 4 bytes of ZEROs, to generate CRC
16151
  // get number of nibbles from Byte length (2^1 = 2)
16152
  if (plus_dribble_nibble)
16153
    nibble_cnt = ((len + 4) << 1) + 1'b1; // one nibble longer
16154
  else
16155
    nibble_cnt = ((len + 4) << 1);
16156
  // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16157
  load_reg[31:24] = eth_phy.tx_mem[addr_cnt];
16158
  addr_cnt = addr_cnt + 1;
16159
  load_reg[23:16] = eth_phy.tx_mem[addr_cnt];
16160
  addr_cnt = addr_cnt + 1;
16161
  load_reg[15: 8] = eth_phy.tx_mem[addr_cnt];
16162
  addr_cnt = addr_cnt + 1;
16163
  load_reg[ 7: 0] = eth_phy.tx_mem[addr_cnt];
16164
  addr_cnt = addr_cnt + 1;
16165
  while (nibble_cnt > 0)
16166
  begin
16167
    // wait for delta time
16168
    delta_t = !delta_t;
16169
    // shift data in
16170
 
16171
    if(nibble_cnt <= 8) // for additional 8 nibbles shift ZEROs in!
16172
      data_in[3:0] = 4'h0;
16173
    else
16174
 
16175
      data_in[3:0] = {load_reg[word_cnt], load_reg[word_cnt+1], load_reg[word_cnt+2], load_reg[word_cnt+3]};
16176
    crc_next[0]  = (data_in[0] ^ crc[28]);
16177
    crc_next[1]  = (data_in[1] ^ data_in[0] ^ crc[28]    ^ crc[29]);
16178
    crc_next[2]  = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]);
16179
    crc_next[3]  = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]);
16180
    crc_next[4]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[0];
16181
    crc_next[5]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[1];
16182
    crc_next[6]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[ 2];
16183
    crc_next[7]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[3];
16184
    crc_next[8]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[4];
16185
    crc_next[9]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[5];
16186
    crc_next[10] = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[6];
16187
    crc_next[11] = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[7];
16188
    crc_next[12] = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]) ^ crc[8];
16189
    crc_next[13] = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]) ^ crc[9];
16190
    crc_next[14] = (data_in[3] ^ data_in[2] ^ crc[30]    ^ crc[31]) ^ crc[10];
16191
    crc_next[15] = (data_in[3] ^ crc[31])   ^ crc[11];
16192
    crc_next[16] = (data_in[0] ^ crc[28])   ^ crc[12];
16193
    crc_next[17] = (data_in[1] ^ crc[29])   ^ crc[13];
16194
    crc_next[18] = (data_in[2] ^ crc[30])   ^ crc[14];
16195
    crc_next[19] = (data_in[3] ^ crc[31])   ^ crc[15];
16196
    crc_next[20] =  crc[16];
16197
    crc_next[21] =  crc[17];
16198
    crc_next[22] = (data_in[0] ^ crc[28])   ^ crc[18];
16199
    crc_next[23] = (data_in[1] ^ data_in[0] ^ crc[29]    ^ crc[28]) ^ crc[19];
16200
    crc_next[24] = (data_in[2] ^ data_in[1] ^ crc[30]    ^ crc[29]) ^ crc[20];
16201
    crc_next[25] = (data_in[3] ^ data_in[2] ^ crc[31]    ^ crc[30]) ^ crc[21];
16202
    crc_next[26] = (data_in[3] ^ data_in[0] ^ crc[31]    ^ crc[28]) ^ crc[22];
16203
    crc_next[27] = (data_in[1] ^ crc[29])   ^ crc[23];
16204
    crc_next[28] = (data_in[2] ^ crc[30])   ^ crc[24];
16205
    crc_next[29] = (data_in[3] ^ crc[31])   ^ crc[25];
16206
    crc_next[30] =  crc[26];
16207
    crc_next[31] =  crc[27];
16208
 
16209
    crc = crc_next;
16210
    crc_error = crc[31:0] != 32'hc704dd7b;  // CRC not equal to magic number
16211
    case (nibble_cnt)
16212
    9: crc_out = {!crc[24], !crc[25], !crc[26], !crc[27], !crc[28], !crc[29], !crc[30], !crc[31],
16213
                  !crc[16], !crc[17], !crc[18], !crc[19], !crc[20], !crc[21], !crc[22], !crc[23],
16214
                  !crc[ 8], !crc[ 9], !crc[10], !crc[11], !crc[12], !crc[13], !crc[14], !crc[15],
16215
                  !crc[ 0], !crc[ 1], !crc[ 2], !crc[ 3], !crc[ 4], !crc[ 5], !crc[ 6], !crc[ 7]};
16216
    default: crc_out = crc_out;
16217
    endcase
16218
    // wait for delta time
16219
    delta_t = !delta_t;
16220
    // increment address and load new data
16221
    if ((word_cnt+3) == 7)//4)
16222
    begin
16223
      // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16224
      load_reg[31:24] = eth_phy.tx_mem[addr_cnt];
16225
      addr_cnt = addr_cnt + 1;
16226
      load_reg[23:16] = eth_phy.tx_mem[addr_cnt];
16227
      addr_cnt = addr_cnt + 1;
16228
      load_reg[15: 8] = eth_phy.tx_mem[addr_cnt];
16229
      addr_cnt = addr_cnt + 1;
16230
      load_reg[ 7: 0] = eth_phy.tx_mem[addr_cnt];
16231
      addr_cnt = addr_cnt + 1;
16232
    end
16233
    // set new load bit position
16234
    if((word_cnt+3) == 31)
16235
      word_cnt = 16;
16236
    else if ((word_cnt+3) == 23)
16237
      word_cnt = 8;
16238
    else if ((word_cnt+3) == 15)
16239
      word_cnt = 0;
16240
    else if ((word_cnt+3) == 7)
16241
      word_cnt = 24;
16242
    else
16243
      word_cnt = word_cnt + 4;// - 4;
16244
    // decrement nibble counter
16245
    nibble_cnt = nibble_cnt - 1;
16246
    // wait for delta time
16247
    delta_t = !delta_t;
16248
  end // while
16249
  #1;
16250
end
16251
endtask // paralel_crc_phy_tx
16252
 
16253
// paralel CRC calculating for PHY RX
16254
task paralel_crc_phy_rx;
16255
  input  [31:0] start_addr; // start address
16256
  input  [31:0] len; // length of frame in Bytes without CRC length
16257
  input         plus_dribble_nibble; // if length is longer for one nibble
16258 209 tadejm
  output [31:0] crc_out;
16259 169 mohor
  reg    [21:0] addr_cnt; // only 22 address lines
16260
  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[21:0];
16271 209 tadejm
  word_cnt = 24; // 27; // start of the frame - nibble granularity (MSbit first)
16272
  crc = 32'hFFFF_FFFF; // INITIAL value
16273 169 mohor
  delta_t = 0;
16274
  // length must include 4 bytes of ZEROs, to generate CRC
16275 209 tadejm
  // get number of nibbles from Byte length (2^1 = 2)
16276 169 mohor
  if (plus_dribble_nibble)
16277 209 tadejm
    nibble_cnt = ((len + 4) << 1) + 1'b1; // one nibble longer
16278 169 mohor
  else
16279 209 tadejm
    nibble_cnt = ((len + 4) << 1);
16280
  // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16281 169 mohor
  load_reg[31:24] = eth_phy.rx_mem[addr_cnt];
16282
  addr_cnt = addr_cnt + 1;
16283
  load_reg[23:16] = eth_phy.rx_mem[addr_cnt];
16284
  addr_cnt = addr_cnt + 1;
16285 209 tadejm
  load_reg[15: 8] = eth_phy.rx_mem[addr_cnt];
16286 169 mohor
  addr_cnt = addr_cnt + 1;
16287 209 tadejm
  load_reg[ 7: 0] = eth_phy.rx_mem[addr_cnt];
16288
  addr_cnt = addr_cnt + 1;
16289
  while (nibble_cnt > 0)
16290 169 mohor
  begin
16291
    // wait for delta time
16292
    delta_t = !delta_t;
16293
    // shift data in
16294 209 tadejm
 
16295
    if(nibble_cnt <= 8) // for additional 8 nibbles shift ZEROs in!
16296
      data_in[3:0] = 4'h0;
16297 169 mohor
    else
16298 209 tadejm
 
16299
      data_in[3:0] = {load_reg[word_cnt], load_reg[word_cnt+1], load_reg[word_cnt+2], load_reg[word_cnt+3]};
16300
    crc_next[0]  = (data_in[0] ^ crc[28]);
16301
    crc_next[1]  = (data_in[1] ^ data_in[0] ^ crc[28]    ^ crc[29]);
16302
    crc_next[2]  = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]);
16303
    crc_next[3]  = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]);
16304
    crc_next[4]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[0];
16305
    crc_next[5]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[1];
16306
    crc_next[6]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[ 2];
16307
    crc_next[7]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[3];
16308
    crc_next[8]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[4];
16309
    crc_next[9]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[5];
16310
    crc_next[10] = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[6];
16311
    crc_next[11] = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[7];
16312
    crc_next[12] = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]) ^ crc[8];
16313
    crc_next[13] = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]) ^ crc[9];
16314
    crc_next[14] = (data_in[3] ^ data_in[2] ^ crc[30]    ^ crc[31]) ^ crc[10];
16315
    crc_next[15] = (data_in[3] ^ crc[31])   ^ crc[11];
16316
    crc_next[16] = (data_in[0] ^ crc[28])   ^ crc[12];
16317
    crc_next[17] = (data_in[1] ^ crc[29])   ^ crc[13];
16318
    crc_next[18] = (data_in[2] ^ crc[30])   ^ crc[14];
16319
    crc_next[19] = (data_in[3] ^ crc[31])   ^ crc[15];
16320
    crc_next[20] =  crc[16];
16321
    crc_next[21] =  crc[17];
16322
    crc_next[22] = (data_in[0] ^ crc[28])   ^ crc[18];
16323
    crc_next[23] = (data_in[1] ^ data_in[0] ^ crc[29]    ^ crc[28]) ^ crc[19];
16324
    crc_next[24] = (data_in[2] ^ data_in[1] ^ crc[30]    ^ crc[29]) ^ crc[20];
16325
    crc_next[25] = (data_in[3] ^ data_in[2] ^ crc[31]    ^ crc[30]) ^ crc[21];
16326
    crc_next[26] = (data_in[3] ^ data_in[0] ^ crc[31]    ^ crc[28]) ^ crc[22];
16327
    crc_next[27] = (data_in[1] ^ crc[29])   ^ crc[23];
16328
    crc_next[28] = (data_in[2] ^ crc[30])   ^ crc[24];
16329
    crc_next[29] = (data_in[3] ^ crc[31])   ^ crc[25];
16330
    crc_next[30] =  crc[26];
16331
    crc_next[31] =  crc[27];
16332
 
16333
    crc = crc_next;
16334
    crc_error = crc[31:0] != 32'hc704dd7b;  // CRC not equal to magic number
16335
    case (nibble_cnt)
16336
    9: crc_out = {!crc[24], !crc[25], !crc[26], !crc[27], !crc[28], !crc[29], !crc[30], !crc[31],
16337
                  !crc[16], !crc[17], !crc[18], !crc[19], !crc[20], !crc[21], !crc[22], !crc[23],
16338
                  !crc[ 8], !crc[ 9], !crc[10], !crc[11], !crc[12], !crc[13], !crc[14], !crc[15],
16339
                  !crc[ 0], !crc[ 1], !crc[ 2], !crc[ 3], !crc[ 4], !crc[ 5], !crc[ 6], !crc[ 7]};
16340
    default: crc_out = crc_out;
16341
    endcase
16342 169 mohor
    // wait for delta time
16343
    delta_t = !delta_t;
16344
    // increment address and load new data
16345 209 tadejm
    if ((word_cnt+3) == 7)//4)
16346 169 mohor
    begin
16347 209 tadejm
      // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16348 169 mohor
      load_reg[31:24] = eth_phy.rx_mem[addr_cnt];
16349
      addr_cnt = addr_cnt + 1;
16350
      load_reg[23:16] = eth_phy.rx_mem[addr_cnt];
16351
      addr_cnt = addr_cnt + 1;
16352 209 tadejm
      load_reg[15: 8] = eth_phy.rx_mem[addr_cnt];
16353 169 mohor
      addr_cnt = addr_cnt + 1;
16354 209 tadejm
      load_reg[ 7: 0] = eth_phy.rx_mem[addr_cnt];
16355
      addr_cnt = addr_cnt + 1;
16356 169 mohor
    end
16357
    // set new load bit position
16358 209 tadejm
    if((word_cnt+3) == 31)
16359 169 mohor
      word_cnt = 16;
16360 209 tadejm
    else if ((word_cnt+3) == 23)
16361 169 mohor
      word_cnt = 8;
16362 209 tadejm
    else if ((word_cnt+3) == 15)
16363 169 mohor
      word_cnt = 0;
16364 209 tadejm
    else if ((word_cnt+3) == 7)
16365 169 mohor
      word_cnt = 24;
16366
    else
16367 209 tadejm
      word_cnt = word_cnt + 4;// - 4;
16368
    // decrement nibble counter
16369
    nibble_cnt = nibble_cnt - 1;
16370 169 mohor
    // wait for delta time
16371
    delta_t = !delta_t;
16372
  end // while
16373
  #1;
16374
end
16375
endtask // paralel_crc_phy_rx
16376
 
16377
// paralel CRC checking for MAC
16378
task paralel_crc_mac;
16379
  input  [31:0] start_addr; // start address
16380
  input  [31:0] len; // length of frame in Bytes without CRC length
16381
  input         plus_dribble_nibble; // if length is longer for one nibble
16382 209 tadejm
  output [31:0] crc_out;
16383
 
16384
  reg    [21:0] addr_cnt; // only 22 address lines
16385 169 mohor
  integer       word_cnt;
16386 209 tadejm
  integer       nibble_cnt;
16387 169 mohor
  reg    [31:0] load_reg;
16388
  reg           delta_t;
16389 209 tadejm
  reg    [31:0] crc_next;
16390
  reg    [31:0] crc;
16391
  reg           crc_error;
16392
  reg     [3:0] data_in;
16393
  integer       i;
16394 169 mohor
begin
16395
  #1 addr_cnt = start_addr[19:0];
16396
  // set starting point depending with which byte frame starts (e.g. if addr_cnt[1:0] == 0, then
16397
  //   MSB of the packet must be written to the LSB of Big ENDIAN Word [31:24])
16398
  if (addr_cnt[1:0] == 2'h1)
16399
    word_cnt = 16; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16400
  else if (addr_cnt[1:0] == 2'h2)
16401
    word_cnt = 8; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16402
  else if (addr_cnt[1:0] == 2'h3)
16403
    word_cnt = 0; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16404
  else
16405
    word_cnt = 24; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16406 209 tadejm
  crc = 32'hFFFF_FFFF; // INITIAL value
16407 169 mohor
  delta_t = 0;
16408
  // length must include 4 bytes of ZEROs, to generate CRC
16409 209 tadejm
  // get number of nibbles from Byte length (2^1 = 2)
16410 169 mohor
  if (plus_dribble_nibble)
16411 209 tadejm
    nibble_cnt = ((len + 4) << 1) + 1'b1; // one nibble longer
16412 169 mohor
  else
16413 209 tadejm
    nibble_cnt = ((len + 4) << 1);
16414 169 mohor
  load_reg = wb_slave.wb_memory[{12'h0, addr_cnt}];
16415 209 tadejm
  addr_cnt = addr_cnt + 4;
16416
  while (nibble_cnt > 0)
16417 169 mohor
  begin
16418
    // wait for delta time
16419
    delta_t = !delta_t;
16420
    // shift data in
16421 209 tadejm
 
16422
    if(nibble_cnt <= 8) // for additional 8 nibbles shift ZEROs in!
16423
      data_in[3:0] = 4'h0;
16424 169 mohor
    else
16425 209 tadejm
 
16426
      data_in[3:0] = {load_reg[word_cnt], load_reg[word_cnt+1], load_reg[word_cnt+2], load_reg[word_cnt+3]};
16427
    crc_next[0]  = (data_in[0] ^ crc[28]);
16428
    crc_next[1]  = (data_in[1] ^ data_in[0] ^ crc[28]    ^ crc[29]);
16429
    crc_next[2]  = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]);
16430
    crc_next[3]  = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]);
16431
    crc_next[4]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[0];
16432
    crc_next[5]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[1];
16433
    crc_next[6]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[ 2];
16434
    crc_next[7]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[3];
16435
    crc_next[8]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[4];
16436
    crc_next[9]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[5];
16437
    crc_next[10] = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[6];
16438
    crc_next[11] = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[7];
16439
    crc_next[12] = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]) ^ crc[8];
16440
    crc_next[13] = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]) ^ crc[9];
16441
    crc_next[14] = (data_in[3] ^ data_in[2] ^ crc[30]    ^ crc[31]) ^ crc[10];
16442
    crc_next[15] = (data_in[3] ^ crc[31])   ^ crc[11];
16443
    crc_next[16] = (data_in[0] ^ crc[28])   ^ crc[12];
16444
    crc_next[17] = (data_in[1] ^ crc[29])   ^ crc[13];
16445
    crc_next[18] = (data_in[2] ^ crc[30])   ^ crc[14];
16446
    crc_next[19] = (data_in[3] ^ crc[31])   ^ crc[15];
16447
    crc_next[20] =  crc[16];
16448
    crc_next[21] =  crc[17];
16449
    crc_next[22] = (data_in[0] ^ crc[28])   ^ crc[18];
16450
    crc_next[23] = (data_in[1] ^ data_in[0] ^ crc[29]    ^ crc[28]) ^ crc[19];
16451
    crc_next[24] = (data_in[2] ^ data_in[1] ^ crc[30]    ^ crc[29]) ^ crc[20];
16452
    crc_next[25] = (data_in[3] ^ data_in[2] ^ crc[31]    ^ crc[30]) ^ crc[21];
16453
    crc_next[26] = (data_in[3] ^ data_in[0] ^ crc[31]    ^ crc[28]) ^ crc[22];
16454
    crc_next[27] = (data_in[1] ^ crc[29])   ^ crc[23];
16455
    crc_next[28] = (data_in[2] ^ crc[30])   ^ crc[24];
16456
    crc_next[29] = (data_in[3] ^ crc[31])   ^ crc[25];
16457
    crc_next[30] =  crc[26];
16458
    crc_next[31] =  crc[27];
16459
 
16460
    crc = crc_next;
16461
    crc_error = crc[31:0] != 32'hc704dd7b;  // CRC not equal to magic number
16462
    case (nibble_cnt)
16463
    9: crc_out = {!crc[24], !crc[25], !crc[26], !crc[27], !crc[28], !crc[29], !crc[30], !crc[31],
16464
                  !crc[16], !crc[17], !crc[18], !crc[19], !crc[20], !crc[21], !crc[22], !crc[23],
16465
                  !crc[ 8], !crc[ 9], !crc[10], !crc[11], !crc[12], !crc[13], !crc[14], !crc[15],
16466
                  !crc[ 0], !crc[ 1], !crc[ 2], !crc[ 3], !crc[ 4], !crc[ 5], !crc[ 6], !crc[ 7]};
16467
    default: crc_out = crc_out;
16468
    endcase
16469 169 mohor
    // wait for delta time
16470
    delta_t = !delta_t;
16471 209 tadejm
    // increment address and load new data
16472
    if ((word_cnt+3) == 7)//4)
16473 169 mohor
    begin
16474 209 tadejm
      // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16475
      load_reg = wb_slave.wb_memory[{12'h0, addr_cnt}];
16476 169 mohor
      addr_cnt = addr_cnt + 4;
16477
    end
16478 209 tadejm
    // set new load bit position
16479
    if((word_cnt+3) == 31)
16480 169 mohor
      word_cnt = 16;
16481 209 tadejm
    else if ((word_cnt+3) == 23)
16482 169 mohor
      word_cnt = 8;
16483 209 tadejm
    else if ((word_cnt+3) == 15)
16484 169 mohor
      word_cnt = 0;
16485 209 tadejm
    else if ((word_cnt+3) == 7)
16486 169 mohor
      word_cnt = 24;
16487
    else
16488 209 tadejm
      word_cnt = word_cnt + 4;// - 4;
16489
    // decrement nibble counter
16490
    nibble_cnt = nibble_cnt - 1;
16491 169 mohor
    // wait for delta time
16492
    delta_t = !delta_t;
16493
  end // while
16494
  #1;
16495
end
16496
endtask // paralel_crc_mac
16497
 
16498
// serial CRC checking for PHY TX
16499
task serial_crc_phy_tx;
16500
  input  [31:0] start_addr; // start address
16501
  input  [31:0] len; // length of frame in Bytes without CRC length
16502
  input         plus_dribble_nibble; // if length is longer for one nibble
16503
  output [31:0] crc;
16504
  reg    [21:0] addr_cnt; // only 22 address lines
16505
  integer       word_cnt;
16506
  integer       bit_cnt;
16507
  reg    [31:0] load_reg;
16508
  reg    [31:0] crc_shift_reg;
16509
  reg    [31:0] crc_store_reg;
16510
  reg           delta_t;
16511
begin
16512
  #1 addr_cnt = start_addr[21:0];
16513
  word_cnt = 24; // 27; // start of the frame - nibble granularity (MSbit first)
16514
  crc_store_reg = 32'hFFFF_FFFF; // INITIAL value
16515
  delta_t = 0;
16516
  // length must include 4 bytes of ZEROs, to generate CRC
16517
  // get number of bits from Byte length (2^3 = 8)
16518
  if (plus_dribble_nibble)
16519
    bit_cnt = ((len + 4) << 3) + 3'h4; // one nibble longer
16520
  else
16521
    bit_cnt = ((len + 4) << 3);
16522
  // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16523
  load_reg[31:24] = eth_phy.tx_mem[addr_cnt];
16524
  addr_cnt = addr_cnt + 1;
16525
  load_reg[23:16] = eth_phy.tx_mem[addr_cnt];
16526
  addr_cnt = addr_cnt + 1;
16527
  load_reg[15: 8] = eth_phy.tx_mem[addr_cnt];
16528
  addr_cnt = addr_cnt + 1;
16529
  load_reg[ 7: 0] = eth_phy.tx_mem[addr_cnt];
16530
  addr_cnt = addr_cnt + 1;
16531
#1;
16532
  while (bit_cnt > 0)
16533
  begin
16534
    // wait for delta time
16535
    delta_t = !delta_t;
16536
#1;
16537
    // shift data in
16538
 
16539
    if(bit_cnt <= 32) // for additional 32 bits shift ZEROs in!
16540
     crc_shift_reg[0] = 1'b0               ^ crc_store_reg[31];
16541
    else
16542
 
16543
     crc_shift_reg[0] = load_reg[word_cnt] ^ crc_store_reg[31];
16544
    crc_shift_reg[1]  = crc_store_reg[0]   ^ crc_store_reg[31];
16545
    crc_shift_reg[2]  = crc_store_reg[1]   ^ crc_store_reg[31];
16546
    crc_shift_reg[3]  = crc_store_reg[2];
16547
    crc_shift_reg[4]  = crc_store_reg[3]   ^ crc_store_reg[31];
16548
    crc_shift_reg[5]  = crc_store_reg[4]   ^ crc_store_reg[31];
16549
    crc_shift_reg[6]  = crc_store_reg[5];
16550
    crc_shift_reg[7]  = crc_store_reg[6]   ^ crc_store_reg[31];
16551
    crc_shift_reg[8]  = crc_store_reg[7]   ^ crc_store_reg[31];
16552
    crc_shift_reg[9]  = crc_store_reg[8];
16553
    crc_shift_reg[10] = crc_store_reg[9]   ^ crc_store_reg[31];
16554
    crc_shift_reg[11] = crc_store_reg[10]  ^ crc_store_reg[31];
16555
    crc_shift_reg[12] = crc_store_reg[11]  ^ crc_store_reg[31];
16556
    crc_shift_reg[13] = crc_store_reg[12];
16557
    crc_shift_reg[14] = crc_store_reg[13];
16558
    crc_shift_reg[15] = crc_store_reg[14];
16559
    crc_shift_reg[16] = crc_store_reg[15]  ^ crc_store_reg[31];
16560
    crc_shift_reg[17] = crc_store_reg[16];
16561
    crc_shift_reg[18] = crc_store_reg[17];
16562
    crc_shift_reg[19] = crc_store_reg[18];
16563
    crc_shift_reg[20] = crc_store_reg[19];
16564
    crc_shift_reg[21] = crc_store_reg[20];
16565
    crc_shift_reg[22] = crc_store_reg[21]  ^ crc_store_reg[31];
16566
    crc_shift_reg[23] = crc_store_reg[22]  ^ crc_store_reg[31];
16567
    crc_shift_reg[24] = crc_store_reg[23];
16568
    crc_shift_reg[25] = crc_store_reg[24];
16569
    crc_shift_reg[26] = crc_store_reg[25]  ^ crc_store_reg[31];
16570
    crc_shift_reg[27] = crc_store_reg[26];
16571
    crc_shift_reg[28] = crc_store_reg[27];
16572
    crc_shift_reg[29] = crc_store_reg[28];
16573
    crc_shift_reg[30] = crc_store_reg[29];
16574
    crc_shift_reg[31] = crc_store_reg[30];
16575
    // wait for delta time
16576
    delta_t = !delta_t;
16577
 
16578
    // store previous data
16579
    crc_store_reg = crc_shift_reg;
16580
 
16581
    // put CRC out
16582
    case (bit_cnt)
16583
    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:
16584
    begin
16585
      crc = crc_store_reg;
16586
      crc = {!crc[24], !crc[25], !crc[26], !crc[27], !crc[28], !crc[29], !crc[30], !crc[31],
16587
             !crc[16], !crc[17], !crc[18], !crc[19], !crc[20], !crc[21], !crc[22], !crc[23],
16588
             !crc[ 8], !crc[ 9], !crc[10], !crc[11], !crc[12], !crc[13], !crc[14], !crc[15],
16589
             !crc[ 0], !crc[ 1], !crc[ 2], !crc[ 3], !crc[ 4], !crc[ 5], !crc[ 6], !crc[ 7]};
16590
    end
16591
    default: crc = crc;
16592
    endcase
16593
 
16594
    // increment address and load new data
16595
#1;
16596
    if (word_cnt == 7)//4)
16597
    begin
16598
      // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16599
      load_reg[31:24] = eth_phy.tx_mem[addr_cnt];
16600
//      load_reg[31:24] = {load_reg[28], load_reg[29], load_reg[30], load_reg[31], 
16601
//                         load_reg[24], load_reg[25], load_reg[26], load_reg[27]};
16602
      addr_cnt = addr_cnt + 1;
16603
      load_reg[23:16] = eth_phy.tx_mem[addr_cnt];
16604
//      load_reg[23:16] = {load_reg[20], load_reg[21], load_reg[22], load_reg[23], 
16605
//                         load_reg[16], load_reg[17], load_reg[18], load_reg[19]};
16606
      addr_cnt = addr_cnt + 1;
16607
      load_reg[15: 8] = eth_phy.tx_mem[addr_cnt];
16608
//      load_reg[15: 8] = {load_reg[12], load_reg[13], load_reg[14], load_reg[15], 
16609
//                         load_reg[ 8], load_reg[ 9], load_reg[10], load_reg[11]};
16610
      addr_cnt = addr_cnt + 1;
16611
      load_reg[ 7: 0] = eth_phy.tx_mem[addr_cnt];
16612
//      load_reg[ 7: 0] = {load_reg[ 4], load_reg[ 5], load_reg[ 6], load_reg[ 7], 
16613
//                         load_reg[ 0], load_reg[ 1], load_reg[ 2], load_reg[ 3]};
16614
      addr_cnt = addr_cnt + 1;
16615
    end
16616
#1;
16617
    // set new load bit position
16618
    if(word_cnt == 31)
16619
      word_cnt = 16;
16620
    else if (word_cnt == 23)
16621
      word_cnt = 8;
16622
    else if (word_cnt == 15)
16623
      word_cnt = 0;
16624
    else if (word_cnt == 7)
16625
      word_cnt = 24;
16626
 
16627
//   if(word_cnt == 24)
16628
//     word_cnt = 31;
16629
//   else if (word_cnt == 28)
16630
//     word_cnt = 19;
16631
//   else if (word_cnt == 16)
16632
//     word_cnt = 23;
16633
//   else if (word_cnt == 20)
16634
//     word_cnt = 11;
16635
//   else if(word_cnt == 8)
16636
//     word_cnt = 15;
16637
//   else if (word_cnt == 12)
16638
//     word_cnt = 3;
16639
//   else if (word_cnt == 0)
16640
//     word_cnt = 7;
16641
//   else if (word_cnt == 4)
16642
//     word_cnt = 27;
16643
    else
16644
      word_cnt = word_cnt + 1;// - 1;
16645
#1;
16646
    // decrement bit counter
16647
    bit_cnt = bit_cnt - 1;
16648
#1;
16649
    // wait for delta time
16650
    delta_t = !delta_t;
16651
  end // while
16652
 
16653
  #1;
16654
end
16655
endtask // serial_crc_phy_tx
16656
 
16657
// serial CRC calculating for PHY RX
16658
task serial_crc_phy_rx;
16659
  input  [31:0] start_addr; // start address
16660
  input  [31:0] len; // length of frame in Bytes without CRC length
16661
  input         plus_dribble_nibble; // if length is longer for one nibble
16662
  output [31:0] crc;
16663
  reg    [21:0] addr_cnt; // only 22 address lines
16664
  integer       word_cnt;
16665
  integer       bit_cnt;
16666
  reg    [31:0] load_reg;
16667
  reg    [31:0] crc_shift_reg;
16668
  reg    [31:0] crc_store_reg;
16669
  reg           delta_t;
16670
begin
16671
  #1 addr_cnt = start_addr[21:0];
16672
  word_cnt = 24; // start of the frame
16673
  crc_shift_reg = 0;
16674
  delta_t = 0;
16675
  // length must include 4 bytes of ZEROs, to generate CRC
16676
  // get number of bits from Byte length (2^3 = 8)
16677
  if (plus_dribble_nibble)
16678
    bit_cnt = ((len + 4) << 3) + 3'h4; // one nibble longer
16679
  else
16680
    bit_cnt = ((len + 4) << 3);
16681
  load_reg[31:24] = eth_phy.rx_mem[addr_cnt];
16682
  addr_cnt = addr_cnt + 1;
16683
  load_reg[23:16] = eth_phy.rx_mem[addr_cnt];
16684
  addr_cnt = addr_cnt + 1;
16685
  load_reg[15:8]  = eth_phy.rx_mem[addr_cnt];
16686
  addr_cnt = addr_cnt + 1;
16687
  load_reg[7:0]   = eth_phy.rx_mem[addr_cnt];
16688
 
16689
  while (bit_cnt > 0)
16690
  begin
16691
    // wait for delta time
16692
    delta_t = !delta_t;
16693
    // store previous data
16694
    crc_store_reg = crc_shift_reg;
16695
    // shift data in
16696
    if(bit_cnt <= 32) // for additional 32 bits shift ZEROs in!
16697
     crc_shift_reg[0] = 1'b0               ^ crc_store_reg[31];
16698
    else
16699
     crc_shift_reg[0] = load_reg[word_cnt] ^ crc_store_reg[31];
16700
    crc_shift_reg[1]  = crc_store_reg[0]   ^ crc_store_reg[31];
16701
    crc_shift_reg[2]  = crc_store_reg[1]   ^ crc_store_reg[31];
16702
    crc_shift_reg[3]  = crc_store_reg[2];
16703
    crc_shift_reg[4]  = crc_store_reg[3]   ^ crc_store_reg[31];
16704
    crc_shift_reg[5]  = crc_store_reg[4]   ^ crc_store_reg[31];
16705
    crc_shift_reg[6]  = crc_store_reg[5];
16706
    crc_shift_reg[7]  = crc_store_reg[6]   ^ crc_store_reg[31];
16707
    crc_shift_reg[8]  = crc_store_reg[7]   ^ crc_store_reg[31];
16708
    crc_shift_reg[9]  = crc_store_reg[8];
16709
    crc_shift_reg[10] = crc_store_reg[9]   ^ crc_store_reg[31];
16710
    crc_shift_reg[11] = crc_store_reg[10]  ^ crc_store_reg[31];
16711
    crc_shift_reg[12] = crc_store_reg[11]  ^ crc_store_reg[31];
16712
    crc_shift_reg[13] = crc_store_reg[12];
16713
    crc_shift_reg[14] = crc_store_reg[13];
16714
    crc_shift_reg[15] = crc_store_reg[14];
16715
    crc_shift_reg[16] = crc_store_reg[15]  ^ crc_store_reg[31];
16716
    crc_shift_reg[17] = crc_store_reg[16];
16717
    crc_shift_reg[18] = crc_store_reg[17];
16718
    crc_shift_reg[19] = crc_store_reg[18];
16719
    crc_shift_reg[20] = crc_store_reg[19];
16720
    crc_shift_reg[21] = crc_store_reg[20];
16721
    crc_shift_reg[22] = crc_store_reg[21]  ^ crc_store_reg[31];
16722
    crc_shift_reg[23] = crc_store_reg[22]  ^ crc_store_reg[31];
16723
    crc_shift_reg[24] = crc_store_reg[23];
16724
    crc_shift_reg[25] = crc_store_reg[24];
16725
    crc_shift_reg[26] = crc_store_reg[25]  ^ crc_store_reg[31];
16726
    crc_shift_reg[27] = crc_store_reg[26];
16727
    crc_shift_reg[28] = crc_store_reg[27];
16728
    crc_shift_reg[29] = crc_store_reg[28];
16729
    crc_shift_reg[30] = crc_store_reg[29];
16730
    crc_shift_reg[31] = crc_store_reg[30];
16731
    // wait for delta time
16732
    delta_t = !delta_t;
16733
    // increment address and load new data
16734
    if (word_cnt == 7)
16735
    begin
16736
      addr_cnt = addr_cnt + 1;
16737
      load_reg[31:24] = eth_phy.rx_mem[addr_cnt];
16738
      addr_cnt = addr_cnt + 1;
16739
      load_reg[23:16] = eth_phy.rx_mem[addr_cnt];
16740
      addr_cnt = addr_cnt + 1;
16741
      load_reg[15:8]  = eth_phy.rx_mem[addr_cnt];
16742
      addr_cnt = addr_cnt + 1;
16743
      load_reg[7:0]   = eth_phy.rx_mem[addr_cnt];
16744
    end
16745
    // set new load bit position
16746
    if(word_cnt == 31)
16747
      word_cnt = 16;
16748
    else if (word_cnt == 23)
16749
      word_cnt = 8;
16750
    else if (word_cnt == 15)
16751
      word_cnt = 0;
16752
    else if (word_cnt == 7)
16753
      word_cnt = 24;
16754
    else
16755
      word_cnt = word_cnt + 1;
16756
    // decrement bit counter
16757
    bit_cnt = bit_cnt - 1;
16758
    // wait for delta time
16759
    delta_t = !delta_t;
16760
  end // while
16761
 
16762
  // put CRC out
16763
  crc = crc_shift_reg;
16764
  #1;
16765
end
16766
endtask // serial_crc_phy_rx
16767
 
16768
// serial CRC checking for MAC
16769
task serial_crc_mac;
16770
  input  [31:0] start_addr; // start address
16771
  input  [31:0] len; // length of frame in Bytes without CRC length
16772
  input         plus_dribble_nibble; // if length is longer for one nibble
16773
  output [31:0] crc;
16774
  reg    [19:0] addr_cnt; // only 20 address lines
16775
  integer       word_cnt;
16776
  integer       bit_cnt;
16777
  reg    [31:0] load_reg;
16778
  reg    [31:0] crc_shift_reg;
16779
  reg    [31:0] crc_store_reg;
16780
  reg           delta_t;
16781
begin
16782
  #1 addr_cnt = start_addr[19:0];
16783
  // set starting point depending with which byte frame starts (e.g. if addr_cnt[1:0] == 0, then
16784
  //   MSB of the packet must be written to the LSB of Big ENDIAN Word [31:24])
16785
  if (addr_cnt[1:0] == 2'h1)
16786
    word_cnt = 16; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16787
  else if (addr_cnt[1:0] == 2'h2)
16788
    word_cnt = 8; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16789
  else if (addr_cnt[1:0] == 2'h3)
16790
    word_cnt = 0; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16791
  else
16792
    word_cnt = 24; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16793
 
16794
  crc_shift_reg = 0;
16795
  delta_t = 0;
16796
  // length must include 4 bytes of ZEROs, to generate CRC
16797
  // get number of bits from Byte length (2^3 = 8)
16798
  if (plus_dribble_nibble)
16799
    bit_cnt = ((len + 4) << 3) + 3'h4; // one nibble longer
16800
  else
16801
    bit_cnt = ((len + 4) << 3);
16802
  load_reg = wb_slave.wb_memory[{12'h0, addr_cnt}];
16803
 
16804
  while (bit_cnt > 0)
16805
  begin
16806
    // wait for delta time
16807
    delta_t = !delta_t;
16808
    // store previous data
16809
    crc_store_reg = crc_shift_reg;
16810
    // shift data in
16811
    if(bit_cnt <= 32) // for additional 32 bits shift ZEROs in!
16812
     crc_shift_reg[0] = 1'b0               ^ crc_store_reg[31];
16813
    else
16814
     crc_shift_reg[0] = load_reg[word_cnt] ^ crc_store_reg[31];
16815
    crc_shift_reg[1]  = crc_store_reg[0]   ^ crc_store_reg[31];
16816
    crc_shift_reg[2]  = crc_store_reg[1]   ^ crc_store_reg[31];
16817
    crc_shift_reg[3]  = crc_store_reg[2];
16818
    crc_shift_reg[4]  = crc_store_reg[3]   ^ crc_store_reg[31];
16819
    crc_shift_reg[5]  = crc_store_reg[4]   ^ crc_store_reg[31];
16820
    crc_shift_reg[6]  = crc_store_reg[5];
16821
    crc_shift_reg[7]  = crc_store_reg[6]   ^ crc_store_reg[31];
16822
    crc_shift_reg[8]  = crc_store_reg[7]   ^ crc_store_reg[31];
16823
    crc_shift_reg[9]  = crc_store_reg[8];
16824
    crc_shift_reg[10] = crc_store_reg[9]   ^ crc_store_reg[31];
16825
    crc_shift_reg[11] = crc_store_reg[10]  ^ crc_store_reg[31];
16826
    crc_shift_reg[12] = crc_store_reg[11]  ^ crc_store_reg[31];
16827
    crc_shift_reg[13] = crc_store_reg[12];
16828
    crc_shift_reg[14] = crc_store_reg[13];
16829
    crc_shift_reg[15] = crc_store_reg[14];
16830
    crc_shift_reg[16] = crc_store_reg[15]  ^ crc_store_reg[31];
16831
    crc_shift_reg[17] = crc_store_reg[16];
16832
    crc_shift_reg[18] = crc_store_reg[17];
16833
    crc_shift_reg[19] = crc_store_reg[18];
16834
    crc_shift_reg[20] = crc_store_reg[19];
16835
    crc_shift_reg[21] = crc_store_reg[20];
16836
    crc_shift_reg[22] = crc_store_reg[21]  ^ crc_store_reg[31];
16837
    crc_shift_reg[23] = crc_store_reg[22]  ^ crc_store_reg[31];
16838
    crc_shift_reg[24] = crc_store_reg[23];
16839
    crc_shift_reg[25] = crc_store_reg[24];
16840
    crc_shift_reg[26] = crc_store_reg[25]  ^ crc_store_reg[31];
16841
    crc_shift_reg[27] = crc_store_reg[26];
16842
    crc_shift_reg[28] = crc_store_reg[27];
16843
    crc_shift_reg[29] = crc_store_reg[28];
16844
    crc_shift_reg[30] = crc_store_reg[29];
16845
    crc_shift_reg[31] = crc_store_reg[30];
16846
    // wait for delta time
16847
    delta_t = !delta_t;
16848
    // increment address and load new data for Big ENDIAN Bytes (Litle ENDIAN bits)
16849
    if (word_cnt == 7)
16850
    begin
16851
      addr_cnt = addr_cnt + 4;
16852
      load_reg = wb_slave.wb_memory[{12'h0, addr_cnt}];
16853
    end
16854
    // set new load bit position for Big ENDIAN Bytes (Litle ENDIAN bits)
16855
    if(word_cnt == 31)
16856
      word_cnt = 16;
16857
    else if (word_cnt == 23)
16858
      word_cnt = 8;
16859
    else if (word_cnt == 15)
16860
      word_cnt = 0;
16861
    else if (word_cnt == 7)
16862
      word_cnt = 24;
16863
    else
16864
      word_cnt = word_cnt + 1;
16865
    // decrement bit counter
16866
    bit_cnt = bit_cnt - 1;
16867
    // wait for delta time
16868
    delta_t = !delta_t;
16869
  end // while
16870
 
16871
  // put CRC out
16872
  crc = crc_shift_reg;
16873
  #1;
16874
end
16875
endtask // serial_crc_mac
16876
 
16877
//////////////////////////////////////////////////////////////
16878
// MIIM Basic tasks
16879
//////////////////////////////////////////////////////////////
16880
 
16881
task reset_mii; //  MII module
16882
  reg [31:0] tmp;
16883
  reg [31:0] tmp_no_rst;
16884
begin
16885
  // read MII mode register first
16886
  wbm_read(`ETH_MIIMODER, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16887
  // set reset bit - write back to MII mode register with RESET bit
16888
  wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_RST | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16889
  // clear reset bit - write back to MII mode register without RESET bit
16890
  tmp_no_rst = `ETH_MIIMODER_RST;
16891
  tmp_no_rst = ~tmp_no_rst;
16892
  wbm_write(`ETH_MIIMODER, (tmp_no_rst & tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16893
end
16894
endtask // reset_mii
16895
 
16896
task mii_set_clk_div; // set clock divider for MII clock
16897
  input [7:0]  clk_div;
16898
begin
16899
  // MII mode register
16900
  wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_CLKDIV & clk_div), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16901
end
16902
endtask // mii_set_clk_div
16903
 
16904
 
16905
task check_mii_busy; // MII - check if BUSY
16906
  reg [31:0] tmp;
16907
begin
16908
  @(posedge wb_clk);
16909
  // MII read status register
16910
  wbm_read(`ETH_MIISTATUS, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16911
  while(tmp[`ETH_MIISTATUS_BUSY] !== 1'b0) //`ETH_MIISTATUS_BUSY
16912
  begin
16913
    @(posedge wb_clk);
16914
    wbm_read(`ETH_MIISTATUS, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16915
  end
16916
end
16917
endtask // check_mii_busy
16918
 
16919
 
16920
task check_mii_scan_valid; // MII - check if SCAN data are valid
16921
  reg [31:0] tmp;
16922
begin
16923
  @(posedge wb_clk);
16924
  // MII read status register
16925
  wbm_read(`ETH_MIISTATUS, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16926
  while(tmp[`ETH_MIISTATUS_NVALID] !== 1'b0) //`ETH_MIISTATUS_NVALID
16927
  begin
16928
    @(posedge wb_clk);
16929
    wbm_read(`ETH_MIISTATUS, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16930
  end
16931
end
16932
endtask // check_mii_scan_valid
16933
 
16934
 
16935
task mii_write_req; // requests write to MII
16936
  input [4:0]  phy_addr;
16937
  input [4:0]  reg_addr;
16938
  input [15:0] data_in;
16939
begin
16940
  // MII address, PHY address = 1, command register address = 0
16941
  wbm_write(`ETH_MIIADDRESS, (`ETH_MIIADDRESS_FIAD & phy_addr) | (`ETH_MIIADDRESS_RGAD & (reg_addr << 8)),
16942
            4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16943
  // MII TX data
16944
  wbm_write(`ETH_MIITX_DATA, {16'h0000, data_in}, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16945
  // MII command
16946
  wbm_write(`ETH_MIICOMMAND, `ETH_MIICOMMAND_WCTRLDATA, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16947
  @(posedge wb_clk);
16948
end
16949
endtask // mii_write_req
16950
 
16951
 
16952
task mii_read_req; // requests read from MII
16953
  input [4:0]  phy_addr;
16954
  input [4:0]  reg_addr;
16955
begin
16956
  // MII address, PHY address = 1, command register address = 0
16957
  wbm_write(`ETH_MIIADDRESS, (`ETH_MIIADDRESS_FIAD & phy_addr) | (`ETH_MIIADDRESS_RGAD & (reg_addr << 8)),
16958
            4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16959
  // MII command
16960
  wbm_write(`ETH_MIICOMMAND, `ETH_MIICOMMAND_RSTAT, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16961
  @(posedge wb_clk);
16962
end
16963
endtask // mii_read_req
16964
 
16965
 
16966
task mii_scan_req; // requests scan from MII
16967
  input [4:0]  phy_addr;
16968
  input [4:0]  reg_addr;
16969
begin
16970
  // MII address, PHY address = 1, command register address = 0
16971
  wbm_write(`ETH_MIIADDRESS, (`ETH_MIIADDRESS_FIAD & phy_addr) | (`ETH_MIIADDRESS_RGAD & (reg_addr << 8)),
16972
            4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16973
  // MII command
16974
  wbm_write(`ETH_MIICOMMAND, `ETH_MIICOMMAND_SCANSTAT, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16975
  @(posedge wb_clk);
16976
end
16977
endtask // mii_scan_req
16978
 
16979
 
16980
task mii_scan_finish; // finish scan from MII
16981
begin
16982
  // MII command
16983
  wbm_write(`ETH_MIICOMMAND, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16984
  @(posedge wb_clk);
16985
end
16986
endtask // mii_scan_finish
16987
 
16988
//////////////////////////////////////////////////////////////
16989
// Log files and memory tasks
16990
//////////////////////////////////////////////////////////////
16991
 
16992
task clear_memories;
16993
  reg    [22:0]  adr_i;
16994
  reg            delta_t;
16995
begin
16996
  delta_t = 0;
16997
  for (adr_i = 0; adr_i < 4194304; adr_i = adr_i + 1)
16998
  begin
16999
    eth_phy.rx_mem[adr_i[21:0]] = 0;
17000
    eth_phy.tx_mem[adr_i[21:0]] = 0;
17001
    wb_slave.wb_memory[adr_i[21:2]] = 0;
17002
    delta_t = !delta_t;
17003
  end
17004
end
17005
endtask // clear_memories
17006
 
17007 243 tadejm
task clear_buffer_descriptors;
17008
  reg    [8:0]  adr_i;
17009
  reg            delta_t;
17010
begin
17011
  delta_t = 0;
17012
  for (adr_i = 0; adr_i < 256; adr_i = adr_i + 1)
17013
  begin
17014
    wbm_write((`TX_BD_BASE + {adr_i[7:0], 2'b0}), 32'h0, 4'hF, 1, 4'h1, 4'h1);
17015
    delta_t = !delta_t;
17016
  end
17017
end
17018
endtask // clear_buffer_descriptors
17019
 
17020 169 mohor
task test_note;
17021
  input [799:0] test_note ;
17022
  reg   [799:0] display_note ;
17023
begin
17024
  display_note = test_note;
17025
  while ( display_note[799:792] == 0 )
17026
    display_note = display_note << 8 ;
17027
  $fdisplay( tb_log_file, " " ) ;
17028
  $fdisplay( tb_log_file, "NOTE: %s", display_note ) ;
17029
  $fdisplay( tb_log_file, " " ) ;
17030
end
17031
endtask // test_note
17032
 
17033
task test_heading;
17034
  input [799:0] test_heading ;
17035
  reg   [799:0] display_test ;
17036
begin
17037
  display_test = test_heading;
17038
  while ( display_test[799:792] == 0 )
17039
    display_test = display_test << 8 ;
17040
  $fdisplay( tb_log_file, "  ***************************************************************************************" ) ;
17041
  $fdisplay( tb_log_file, "  ***************************************************************************************" ) ;
17042
  $fdisplay( tb_log_file, "  Heading: %s", display_test ) ;
17043
  $fdisplay( tb_log_file, "  ***************************************************************************************" ) ;
17044
  $fdisplay( tb_log_file, "  ***************************************************************************************" ) ;
17045
  $fdisplay( tb_log_file, " " ) ;
17046
end
17047
endtask // test_heading
17048
 
17049
 
17050
task test_fail ;
17051
  input [7999:0] failure_reason ;
17052
//  reg   [8007:0] display_failure ;
17053
  reg   [7999:0] display_failure ;
17054
  reg   [799:0] display_test ;
17055
begin
17056
  tests_failed = tests_failed + 1 ;
17057
 
17058
  display_failure = failure_reason; // {failure_reason, "!"} ;
17059
  while ( display_failure[7999:7992] == 0 )
17060
    display_failure = display_failure << 8 ;
17061
 
17062
  display_test = test_name ;
17063
  while ( display_test[799:792] == 0 )
17064
    display_test = display_test << 8 ;
17065
 
17066
  $fdisplay( tb_log_file, "    *************************************************************************************" ) ;
17067
  $fdisplay( tb_log_file, "    At time: %t ", $time ) ;
17068
  $fdisplay( tb_log_file, "    Test: %s", display_test ) ;
17069
  $fdisplay( tb_log_file, "    *FAILED* because") ;
17070
  $fdisplay( tb_log_file, "    %s", display_failure ) ;
17071
  $fdisplay( tb_log_file, "    *************************************************************************************" ) ;
17072
  $fdisplay( tb_log_file, " " ) ;
17073
 
17074
 `ifdef STOP_ON_FAILURE
17075
    #20 $stop ;
17076
 `endif
17077
end
17078
endtask // test_fail
17079
 
17080
 
17081
task test_ok ;
17082
  reg [799:0] display_test ;
17083
begin
17084
  tests_successfull = tests_successfull + 1 ;
17085
 
17086
  display_test = test_name ;
17087
  while ( display_test[799:792] == 0 )
17088
    display_test = display_test << 8 ;
17089
 
17090
  $fdisplay( tb_log_file, "    *************************************************************************************" ) ;
17091
  $fdisplay( tb_log_file, "    At time: %t ", $time ) ;
17092
  $fdisplay( tb_log_file, "    Test: %s", display_test ) ;
17093
  $fdisplay( tb_log_file, "    reported *SUCCESSFULL*! ") ;
17094
  $fdisplay( tb_log_file, "    *************************************************************************************" ) ;
17095
  $fdisplay( tb_log_file, " " ) ;
17096
end
17097
endtask // test_ok
17098
 
17099
 
17100
task test_summary;
17101
begin
17102
  $fdisplay(tb_log_file, "**************************** Ethernet MAC test summary **********************************") ;
17103
  $fdisplay(tb_log_file, "Tests performed:   %d", tests_successfull + tests_failed) ;
17104
  $fdisplay(tb_log_file, "Failed tests   :   %d", tests_failed) ;
17105
  $fdisplay(tb_log_file, "Successfull tests: %d", tests_successfull) ;
17106
  $fdisplay(tb_log_file, "**************************** Ethernet MAC test summary **********************************") ;
17107
  $fclose(tb_log_file) ;
17108
end
17109
endtask // test_summary
17110
 
17111
 
17112 116 mohor
endmodule

powered by: WebSVN 2.1.0

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