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

Subversion Repositories ethmac

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

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

Line No. Rev Author Line
1 116 mohor
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  tb_ethernet.v                                               ////
4
////                                                              ////
5
////  This file is part of the Ethernet IP core project           ////
6
////  http://www.opencores.org/projects/ethmac/                   ////
7
////                                                              ////
8
////  Author(s):                                                  ////
9 169 mohor
////      - Tadej Markovic, tadej@opencores.org                   ////
10 243 tadejm
////      - Igor Mohor,     igorM@opencores.org                  ////
11 116 mohor
////                                                              ////
12 169 mohor
////  All additional information is available in the Readme.txt   ////
13 116 mohor
////  file.                                                       ////
14
////                                                              ////
15
//////////////////////////////////////////////////////////////////////
16
////                                                              ////
17
//// Copyright (C) 2001, 2002 Authors                             ////
18
////                                                              ////
19
//// This source file may be used and distributed without         ////
20
//// restriction provided that this copyright statement is not    ////
21
//// removed from the file and that any derivative work contains  ////
22
//// the original copyright notice and the associated disclaimer. ////
23
////                                                              ////
24
//// This source file is free software; you can redistribute it   ////
25
//// and/or modify it under the terms of the GNU Lesser General   ////
26
//// Public License as published by the Free Software Foundation; ////
27
//// either version 2.1 of the License, or (at your option) any   ////
28
//// later version.                                               ////
29
////                                                              ////
30
//// This source is distributed in the hope that it will be       ////
31
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
32
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
33
//// PURPOSE.  See the GNU Lesser General Public License for more ////
34
//// details.                                                     ////
35
////                                                              ////
36
//// You should have received a copy of the GNU Lesser General    ////
37
//// Public License along with this source; if not, download it   ////
38
//// from http://www.opencores.org/lgpl.shtml                     ////
39
////                                                              ////
40
//////////////////////////////////////////////////////////////////////
41
//
42
// CVS Revision History
43
//
44
// $Log: not supported by cvs2svn $
45 254 mohor
// Revision 1.20  2002/11/19 17:41:19  tadejm
46
// Just some updates.
47
//
48 252 tadejm
// Revision 1.19  2002/11/14 13:12:47  tadejm
49
// Late collision is not reported any more.
50
//
51 243 tadejm
// Revision 1.18  2002/10/18 17:03:34  tadejm
52
// Changed BIST scan signals.
53
//
54 227 tadejm
// Revision 1.17  2002/10/18 13:58:22  tadejm
55
// Some code changed due to bug fixes.
56
//
57 223 tadejm
// Revision 1.16  2002/10/09 13:16:51  tadejm
58
// Just back-up; not completed testbench and some testcases are not
59
// wotking properly yet.
60
//
61 209 tadejm
// Revision 1.15  2002/09/20 14:29:12  tadej
62
// Full duplex tests modified and testbench bug repaired.
63
//
64 194 tadej
// Revision 1.14  2002/09/18 17:56:38  tadej
65
// Some additional reports added
66
//
67 192 tadej
// Revision 1.13  2002/09/16 17:53:49  tadej
68
// Full duplex test improved.
69
//
70 182 tadej
// Revision 1.12  2002/09/16 15:10:42  mohor
71
// MIIM test look better.
72
//
73 181 mohor
// Revision 1.11  2002/09/13 19:18:04  mohor
74
// Bench outputs data to display every 128 bytes.
75
//
76 180 mohor
// Revision 1.10  2002/09/13 18:44:29  mohor
77
// Beautiful tests merget together
78
//
79 179 mohor
// Revision 1.9  2002/09/13 18:41:45  mohor
80
// Rearanged testcases
81
//
82 178 mohor
// Revision 1.8  2002/09/13 14:50:15  mohor
83
// Bug in MIIM fixed.
84
//
85 177 mohor
// Revision 1.7  2002/09/13 12:29:14  mohor
86
// Headers changed.
87
//
88 170 mohor
// Revision 1.6  2002/09/13 11:57:20  mohor
89
// New testbench. Thanks to Tadej M - "The Spammer".
90
//
91 121 mohor
// Revision 1.2  2002/07/19 14:02:47  mohor
92
// Clock mrx_clk set to 2.5 MHz.
93
//
94 117 mohor
// Revision 1.1  2002/07/19 13:57:53  mohor
95
// Testing environment also includes traffic cop, memory interface and host
96
// interface.
97 116 mohor
//
98
//
99
//
100
//
101 117 mohor
//
102 116 mohor
 
103
 
104 169 mohor
`include "eth_phy_defines.v"
105
`include "wb_model_defines.v"
106 116 mohor
`include "tb_eth_defines.v"
107
`include "eth_defines.v"
108
`include "timescale.v"
109
 
110
module tb_ethernet();
111
 
112
 
113 169 mohor
reg           wb_clk;
114
reg           wb_rst;
115
wire          wb_int;
116 116 mohor
 
117 169 mohor
wire          mtx_clk;  // This goes to PHY
118
wire          mrx_clk;  // This goes to PHY
119 116 mohor
 
120
wire   [3:0]  MTxD;
121
wire          MTxEn;
122
wire          MTxErr;
123
 
124 169 mohor
wire   [3:0]  MRxD;     // This goes to PHY
125
wire          MRxDV;    // This goes to PHY
126
wire          MRxErr;   // This goes to PHY
127
wire          MColl;    // This goes to PHY
128
wire          MCrs;     // This goes to PHY
129 116 mohor
 
130
wire          Mdi_I;
131
wire          Mdo_O;
132
wire          Mdo_OE;
133 169 mohor
tri           Mdio_IO;
134 116 mohor
wire          Mdc_O;
135
 
136
 
137 169 mohor
parameter Tp = 1;
138 116 mohor
 
139 121 mohor
 
140 116 mohor
// Ethernet Slave Interface signals
141 169 mohor
wire [31:0] eth_sl_wb_adr;
142 116 mohor
wire [31:0] eth_sl_wb_adr_i, eth_sl_wb_dat_o, eth_sl_wb_dat_i;
143
wire  [3:0] eth_sl_wb_sel_i;
144
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;
145
 
146
// Ethernet Master Interface signals
147
wire [31:0] eth_ma_wb_adr_o, eth_ma_wb_dat_i, eth_ma_wb_dat_o;
148
wire  [3:0] eth_ma_wb_sel_o;
149
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;
150
 
151
 
152
 
153
 
154
// Connecting Ethernet top module
155 169 mohor
eth_top eth_top
156 116 mohor
(
157
  // WISHBONE common
158 169 mohor
  .wb_clk_i(wb_clk),              .wb_rst_i(wb_rst),
159 116 mohor
 
160
  // WISHBONE slave
161 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),
162
  .wb_cyc_i(eth_sl_wb_cyc_i),       .wb_stb_i(eth_sl_wb_stb_i),   .wb_ack_o(eth_sl_wb_ack_o),
163
  .wb_err_o(eth_sl_wb_err_o),       .wb_dat_i(eth_sl_wb_dat_i),   .wb_dat_o(eth_sl_wb_dat_o),
164 116 mohor
 
165
  // WISHBONE master
166
  .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),
167
  .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),
168
  .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),
169
 
170
  //TX
171
  .mtx_clk_pad_i(mtx_clk), .mtxd_pad_o(MTxD), .mtxen_pad_o(MTxEn), .mtxerr_pad_o(MTxErr),
172
 
173
  //RX
174
  .mrx_clk_pad_i(mrx_clk), .mrxd_pad_i(MRxD), .mrxdv_pad_i(MRxDV), .mrxerr_pad_i(MRxErr),
175
  .mcoll_pad_i(MColl),    .mcrs_pad_i(MCrs),
176
 
177
  // MIIM
178
  .mdc_pad_o(Mdc_O), .md_pad_i(Mdi_I), .md_pad_o(Mdo_O), .md_padoe_o(Mdo_OE),
179
 
180 169 mohor
  .int_o(wb_int)
181 227 tadejm
 
182
  // Bist
183
`ifdef ETH_BIST
184
  ,
185
  .scanb_rst      (1'b0),
186
  .scanb_clk      (1'b0),
187
  .scanb_si       (1'b0),
188
  .scanb_so       (),
189
  .scanb_en       (1'b0)
190
`endif
191 116 mohor
);
192
 
193
 
194
 
195 169 mohor
// Connecting Ethernet PHY Module
196
assign Mdio_IO = Mdo_OE ? Mdo_O : 1'bz ;
197
assign Mdi_I   = Mdio_IO;
198
integer phy_log_file_desc;
199
 
200
eth_phy eth_phy
201 116 mohor
(
202 169 mohor
  // WISHBONE reset
203
  .m_rst_n_i(!wb_rst),
204 116 mohor
 
205 169 mohor
  // MAC TX
206
  .mtx_clk_o(mtx_clk),    .mtxd_i(MTxD),    .mtxen_i(MTxEn),    .mtxerr_i(MTxErr),
207
 
208
  // MAC RX
209
  .mrx_clk_o(mrx_clk),    .mrxd_o(MRxD),    .mrxdv_o(MRxDV),    .mrxerr_o(MRxErr),
210
  .mcoll_o(MColl),        .mcrs_o(MCrs),
211
 
212
  // MIIM
213
  .mdc_i(Mdc_O),          .md_io(Mdio_IO),
214
 
215
  // SYSTEM
216
  .phy_log(phy_log_file_desc)
217 116 mohor
);
218
 
219
 
220 169 mohor
 
221
// Connecting WB Master as Host Interface
222
integer host_log_file_desc;
223
 
224
WB_MASTER_BEHAVIORAL wb_master
225 116 mohor
(
226 169 mohor
    .CLK_I(wb_clk),
227
    .RST_I(wb_rst),
228
    .TAG_I({`WB_TAG_WIDTH{1'b0}}),
229
    .TAG_O(),
230
    .ACK_I(eth_sl_wb_ack_o),
231
    .ADR_O(eth_sl_wb_adr), // only eth_sl_wb_adr_i[11:2] used
232
    .CYC_O(eth_sl_wb_cyc_i),
233
    .DAT_I(eth_sl_wb_dat_o),
234
    .DAT_O(eth_sl_wb_dat_i),
235
    .ERR_I(eth_sl_wb_err_o),
236
    .RTY_I(1'b0),  // inactive (1'b0)
237
    .SEL_O(eth_sl_wb_sel_i),
238
    .STB_O(eth_sl_wb_stb_i),
239
    .WE_O (eth_sl_wb_we_i),
240
    .CAB_O()       // NOT USED for now!
241
);
242
 
243
assign eth_sl_wb_adr_i = {20'h0, eth_sl_wb_adr[11:2], 2'h0};
244
 
245
 
246
 
247
// Connecting WB Slave as Memory Interface Module
248
integer memory_log_file_desc;
249
 
250
WB_SLAVE_BEHAVIORAL wb_slave
251
(
252
    .CLK_I(wb_clk),
253
    .RST_I(wb_rst),
254
    .ACK_O(eth_ma_wb_ack_i),
255
    .ADR_I(eth_ma_wb_adr_o),
256
    .CYC_I(eth_ma_wb_cyc_o),
257
    .DAT_O(eth_ma_wb_dat_i),
258
    .DAT_I(eth_ma_wb_dat_o),
259
    .ERR_O(eth_ma_wb_err_i),
260
    .RTY_O(),      // NOT USED for now!
261
    .SEL_I(eth_ma_wb_sel_o),
262
    .STB_I(eth_ma_wb_stb_o),
263
    .WE_I (eth_ma_wb_we_o),
264
    .CAB_I(1'b0)   // inactive (1'b0)
265
);
266
 
267
 
268
 
269
// Connecting WISHBONE Bus Monitors to ethernet master and slave interfaces
270
integer wb_s_mon_log_file_desc ;
271
integer wb_m_mon_log_file_desc ;
272
 
273
WB_BUS_MON wb_eth_slave_bus_mon
274
(
275 116 mohor
  // WISHBONE common
276 169 mohor
  .CLK_I(wb_clk),
277
  .RST_I(wb_rst),
278 116 mohor
 
279 169 mohor
  // WISHBONE slave
280
  .ACK_I(eth_sl_wb_ack_o),
281
  .ADDR_O({20'h0, eth_sl_wb_adr_i[11:2], 2'b0}),
282
  .CYC_O(eth_sl_wb_cyc_i),
283
  .DAT_I(eth_sl_wb_dat_o),
284
  .DAT_O(eth_sl_wb_dat_i),
285
  .ERR_I(eth_sl_wb_err_o),
286
  .RTY_I(1'b0),
287
  .SEL_O(eth_sl_wb_sel_i),
288
  .STB_O(eth_sl_wb_stb_i),
289
  .WE_O (eth_sl_wb_we_i),
290
  .TAG_I({`WB_TAG_WIDTH{1'b0}}),
291
  .TAG_O(),
292
  .CAB_O(1'b0),
293
  .log_file_desc (wb_s_mon_log_file_desc)
294
);
295
 
296
WB_BUS_MON wb_eth_master_bus_mon
297
(
298
  // WISHBONE common
299
  .CLK_I(wb_clk),
300
  .RST_I(wb_rst),
301
 
302 116 mohor
  // WISHBONE master
303 169 mohor
  .ACK_I(eth_ma_wb_ack_i),
304
  .ADDR_O(eth_ma_wb_adr_o),
305
  .CYC_O(eth_ma_wb_cyc_o),
306
  .DAT_I(eth_ma_wb_dat_i),
307
  .DAT_O(eth_ma_wb_dat_o),
308
  .ERR_I(eth_ma_wb_err_i),
309
  .RTY_I(1'b0),
310
  .SEL_O(eth_ma_wb_sel_o),
311
  .STB_O(eth_ma_wb_stb_o),
312
  .WE_O (eth_ma_wb_we_o),
313
  .TAG_I({`WB_TAG_WIDTH{1'b0}}),
314
  .TAG_O(),
315
  .CAB_O(1'b0),
316
  .log_file_desc(wb_m_mon_log_file_desc)
317 116 mohor
);
318
 
319
 
320
 
321 169 mohor
reg         StartTB;
322
integer     tb_log_file;
323 116 mohor
 
324 169 mohor
initial
325
begin
326
  tb_log_file = $fopen("../log/eth_tb.log");
327
  if (tb_log_file < 2)
328
  begin
329
    $display("*E Could not open/create testbench log file in ../log/ directory!");
330
    $finish;
331
  end
332
  $fdisplay(tb_log_file, "========================== ETHERNET IP Core Testbench results ===========================");
333
  $fdisplay(tb_log_file, " ");
334 116 mohor
 
335 169 mohor
  phy_log_file_desc = $fopen("../log/eth_tb_phy.log");
336
  if (phy_log_file_desc < 2)
337
  begin
338
    $fdisplay(tb_log_file, "*E Could not open/create eth_tb_phy.log file in ../log/ directory!");
339
    $finish;
340
  end
341
  $fdisplay(phy_log_file_desc, "================ PHY Module  Testbench access log ================");
342
  $fdisplay(phy_log_file_desc, " ");
343
 
344
  memory_log_file_desc = $fopen("../log/eth_tb_memory.log");
345
  if (memory_log_file_desc < 2)
346
  begin
347
    $fdisplay(tb_log_file, "*E Could not open/create eth_tb_memory.log file in ../log/ directory!");
348
    $finish;
349
  end
350
  $fdisplay(memory_log_file_desc, "=============== MEMORY Module Testbench access log ===============");
351
  $fdisplay(memory_log_file_desc, " ");
352
 
353
  host_log_file_desc = $fopen("../log/eth_tb_host.log");
354
  if (host_log_file_desc < 2)
355
  begin
356
    $fdisplay(tb_log_file, "*E Could not open/create eth_tb_host.log file in ../log/ directory!");
357
    $finish;
358
  end
359
  $fdisplay(host_log_file_desc, "================ HOST Module Testbench access log ================");
360
  $fdisplay(host_log_file_desc, " ");
361
 
362
  wb_s_mon_log_file_desc = $fopen("../log/eth_tb_wb_s_mon.log");
363
  if (wb_s_mon_log_file_desc < 2)
364
  begin
365
    $fdisplay(tb_log_file, "*E Could not open/create eth_tb_wb_s_mon.log file in ../log/ directory!");
366
    $finish;
367
  end
368
  $fdisplay(wb_s_mon_log_file_desc, "============== WISHBONE Slave Bus Monitor error log ==============");
369
  $fdisplay(wb_s_mon_log_file_desc, " ");
370
  $fdisplay(wb_s_mon_log_file_desc, "   Only ERRONEOUS conditions are logged !");
371
  $fdisplay(wb_s_mon_log_file_desc, " ");
372
 
373
  wb_m_mon_log_file_desc = $fopen("../log/eth_tb_wb_m_mon.log");
374
  if (wb_m_mon_log_file_desc < 2)
375
  begin
376
    $fdisplay(tb_log_file, "*E Could not open/create eth_tb_wb_m_mon.log file in ../log/ directory!");
377
    $finish;
378
  end
379
  $fdisplay(wb_m_mon_log_file_desc, "============= WISHBONE Master Bus Monitor  error log =============");
380
  $fdisplay(wb_m_mon_log_file_desc, " ");
381
  $fdisplay(wb_m_mon_log_file_desc, "   Only ERRONEOUS conditions are logged !");
382
  $fdisplay(wb_m_mon_log_file_desc, " ");
383
 
384 243 tadejm
  // Reset pulse
385
  wb_rst =  1'b1;
386
  #423 wb_rst =  1'b0;
387
 
388 169 mohor
  // Clear memories
389
  clear_memories;
390 243 tadejm
  clear_buffer_descriptors;
391 169 mohor
 
392
  #423 StartTB  =  1'b1;
393
end
394
 
395
 
396
 
397
// Generating wb_clk clock
398 116 mohor
initial
399
begin
400 169 mohor
  wb_clk=0;
401
//  forever #2.5 wb_clk = ~wb_clk;  // 2*2.5 ns -> 200.0 MHz    
402 209 tadejm
  forever #5 wb_clk = ~wb_clk;  // 2*5 ns -> 100.0 MHz    
403 169 mohor
//  forever #10 wb_clk = ~wb_clk;  // 2*10 ns -> 50.0 MHz    
404
//  forever #12.5 wb_clk = ~wb_clk;  // 2*12.5 ns -> 40 MHz    
405
//  forever #15 wb_clk = ~wb_clk;  // 2*10 ns -> 33.3 MHz    
406 209 tadejm
//  forever #20 wb_clk = ~wb_clk;  // 2*20 ns -> 25 MHz    
407 169 mohor
//  forever #25 wb_clk = ~wb_clk;  // 2*25 ns -> 20.0 MHz
408
//  forever #31.25 wb_clk = ~wb_clk;  // 2*31.25 ns -> 16.0 MHz    
409
//  forever #50 wb_clk = ~wb_clk;  // 2*50 ns -> 10.0 MHz
410
//  forever #55 wb_clk = ~wb_clk;  // 2*55 ns ->  9.1 MHz    
411 116 mohor
end
412
 
413
 
414
 
415 169 mohor
integer      tests_successfull;
416
integer      tests_failed;
417
reg [799:0]  test_name; // used for tb_log_file
418 121 mohor
 
419 169 mohor
reg   [3:0]  wbm_init_waits; // initial wait cycles between CYC_O and STB_O of WB Master
420
reg   [3:0]  wbm_subseq_waits; // subsequent wait cycles between STB_Os of WB Master
421
reg   [2:0]  wbs_waits; // wait cycles befor WB Slave responds
422
reg   [7:0]  wbs_retries; // if RTY response, then this is the number of retries before ACK
423
 
424 116 mohor
initial
425
begin
426 169 mohor
  wait(StartTB);  // Start of testbench
427
 
428
  // Initial global values
429
  tests_successfull = 0;
430
  tests_failed = 0;
431
 
432
  wbm_init_waits = 4'h1;
433
  wbm_subseq_waits = 4'h3;
434
  wbs_waits = 4'h1;
435
  wbs_retries = 8'h2;
436
  wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
437
 
438
 
439
  //  Call tests
440
  //  ----------
441 194 tadej
//    test_access_to_mac_reg(0, 3);           // 0 - 3
442
//    test_mii(0, 17);                        // 0 - 17
443 169 mohor
  test_note("PHY generates ideal Carrier sense and Collision signals for following tests");
444
  eth_phy.carrier_sense_real_delay(0);
445 243 tadejm
//    test_mac_full_duplex_transmit(8, 9);    // 0 - (21)
446 254 mohor
//    test_mac_full_duplex_receive(8, 9);
447
    test_mac_full_duplex_flow(0, 0);
448 169 mohor
 
449 243 tadejm
  test_note("PHY generates 'real delayed' Carrier sense and Collision signals for following tests");
450 169 mohor
  eth_phy.carrier_sense_real_delay(1);
451
 
452
 
453
  // Finish test's logs
454
  test_summary;
455
  $display("\n\n END of SIMULATION");
456
  $fclose(tb_log_file | phy_log_file_desc | memory_log_file_desc | host_log_file_desc);
457
  $fclose(wb_s_mon_log_file_desc | wb_m_mon_log_file_desc);
458
 
459
  $stop;
460 116 mohor
end
461 169 mohor
 
462 116 mohor
 
463 169 mohor
 
464
//////////////////////////////////////////////////////////////
465
// Test tasks
466
//////////////////////////////////////////////////////////////
467
 
468
task test_access_to_mac_reg;
469
  input  [31:0]  start_task;
470
  input  [31:0]  end_task;
471
  integer        bit_start_1;
472
  integer        bit_end_1;
473
  integer        bit_start_2;
474
  integer        bit_end_2;
475
  integer        num_of_reg;
476
  integer        i_addr;
477
  integer        i_data;
478
  integer        i_length;
479
  integer        tmp_data;
480
  reg    [31:0]  tx_bd_num;
481
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_data;
482
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_tmp_data;
483
  integer        i;
484
  integer        i1;
485
  integer        i2;
486
  integer        i3;
487
  integer        fail;
488 178 mohor
  integer        test_num;
489 169 mohor
  reg    [31:0]  addr;
490
  reg    [31:0]  data;
491
  reg    [31:0]  data_max;
492 116 mohor
begin
493 169 mohor
// ACCESS TO MAC REGISTERS TEST
494
test_heading("ACCESS TO MAC REGISTERS TEST");
495
$display(" ");
496
$display("ACCESS TO MAC REGISTERS TEST");
497
fail = 0;
498
 
499 192 tadej
// reset MAC registers
500
hard_reset;
501
// reset MAC and MII LOGIC with soft reset
502
reset_mac;
503
reset_mii;
504 169 mohor
 
505 192 tadej
 
506 178 mohor
//////////////////////////////////////////////////////////////////////
507
////                                                              ////
508
////  test_access_to_mac_reg:                                     ////
509
////                                                              ////
510
////  0: Walking 1 with single cycles across MAC regs.            ////
511
////  1: Walking 1 with single cycles across MAC buffer descript. ////
512
////  2: Test max reg. values and reg. values after writing       ////
513
////     inverse reset values and hard reset of the MAC           ////
514
////  3: Test buffer desc. RAM preserving values after hard reset ////
515
////     of the MAC and resetting the logic                       ////
516
////                                                              ////
517
//////////////////////////////////////////////////////////////////////
518 194 tadej
for (test_num = start_task; test_num <= end_task; test_num = test_num + 1)
519 169 mohor
begin
520
 
521 178 mohor
  ////////////////////////////////////////////////////////////////////
522
  ////                                                            ////
523
  ////  Walking 1 with single cycles across MAC regs.             ////
524
  ////                                                            ////
525
  ////////////////////////////////////////////////////////////////////
526
  if (test_num == 0) // Walking 1 with single cycles across MAC regs.
527 194 tadej
  begin
528
    // TEST 0: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )
529
    test_name   = "TEST 0: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )";
530
    `TIME; $display("  TEST 0: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )");
531 178 mohor
 
532 194 tadej
    data = 0;
533
    for (i = 0; i <= 4; i = i + 1) // for initial wait cycles on WB bus
534
      begin
535
        wbm_init_waits = i;
536
        wbm_subseq_waits = {$random} % 5; // it is not important for single accesses
537
        for (i_addr = 0; i_addr <= 32'h4C; i_addr = i_addr + 4) // register address
538
          begin
539
            addr = `ETH_BASE + i_addr;
540
            // set ranges of R/W bits
541
            case (addr)
542
              `ETH_MODER:
543
                begin
544
                  bit_start_1 = 0;
545
                  bit_end_1   = 16;
546
                  bit_start_2 = 32; // not used
547
                  bit_end_2   = 32; // not used
548
                end
549
              `ETH_INT: // READONLY - tested within INT test
550
                begin
551
                  bit_start_1 = 32; // not used
552
                  bit_end_1   = 32; // not used
553
                  bit_start_2 = 32; // not used
554
                  bit_end_2   = 32; // not used
555
                end
556
              `ETH_INT_MASK:
557
                begin
558
                  bit_start_1 = 0;
559
                  bit_end_1   = 6;
560
                  bit_start_2 = 32; // not used
561
                  bit_end_2   = 32; // not used
562
                end
563
              `ETH_IPGT:
564
                begin
565
                  bit_start_1 = 0;
566
                  bit_end_1   = 6;
567
                  bit_start_2 = 32; // not used
568
                  bit_end_2   = 32; // not used
569
                end
570
              `ETH_IPGR1:
571
                begin
572
                  bit_start_1 = 0;
573
                  bit_end_1   = 6;
574
                  bit_start_2 = 32; // not used
575
                  bit_end_2   = 32; // not used
576
                end
577
              `ETH_IPGR2:
578
                begin
579
                  bit_start_1 = 0;
580
                  bit_end_1   = 6;
581
                  bit_start_2 = 32; // not used
582
                  bit_end_2   = 32; // not used
583
                end
584
              `ETH_PACKETLEN:
585
                begin
586
                  bit_start_1 = 0;
587
                  bit_end_1   = 31;
588
                  bit_start_2 = 32; // not used
589
                  bit_end_2   = 32; // not used
590
                end
591
              `ETH_COLLCONF:
592
                begin
593
                  bit_start_1 = 0;
594
                  bit_end_1   = 5;
595
                  bit_start_2 = 16;
596
                  bit_end_2   = 19;
597
                end
598
              `ETH_TX_BD_NUM:
599
                begin
600
                  bit_start_1 = 0;
601
                  bit_end_1   = 7;
602
                  bit_start_2 = 32; // not used
603
                  bit_end_2   = 32; // not used
604
                end
605
              `ETH_CTRLMODER:
606
                begin
607
                  bit_start_1 = 0;
608
                  bit_end_1   = 2;
609
                  bit_start_2 = 32; // not used
610
                  bit_end_2   = 32; // not used
611
                end
612
              `ETH_MIIMODER:
613
                begin
614
                  bit_start_1 = 0;
615
                  bit_end_1   = 9;
616
                  bit_start_2 = 32; // not used
617
                  bit_end_2   = 32; // not used
618
                end
619
              `ETH_MIICOMMAND: // "WRITEONLY" - tested within MIIM test - 3 LSBits are not written here!!!
620
                begin
621
                  bit_start_1 = 32; // not used
622
                  bit_end_1   = 32; // not used
623
                  bit_start_2 = 32; // not used
624
                  bit_end_2   = 32; // not used
625
                end
626
              `ETH_MIIADDRESS:
627
                begin
628
                  bit_start_1 = 0;
629
                  bit_end_1   = 4;
630
                  bit_start_2 = 8;
631
                  bit_end_2   = 12;
632
                end
633
              `ETH_MIITX_DATA:
634
                begin
635
                  bit_start_1 = 0;
636
                  bit_end_1   = 15;
637
                  bit_start_2 = 32; // not used
638
                  bit_end_2   = 32; // not used
639
                end
640
              `ETH_MIIRX_DATA: // READONLY - tested within MIIM test
641
                begin
642
                  bit_start_1 = 32; // not used
643
                  bit_end_1   = 32; // not used
644
                  bit_start_2 = 32; // not used
645
                  bit_end_2   = 32; // not used
646
                end
647
              `ETH_MIISTATUS: // READONLY - tested within MIIM test
648
                begin
649
                  bit_start_1 = 32; // not used
650
                  bit_end_1   = 32; // not used
651
                  bit_start_2 = 32; // not used
652
                  bit_end_2   = 32; // not used
653
                end
654
              `ETH_MAC_ADDR0:
655
                begin
656
                  bit_start_1 = 0;
657
                  bit_end_1   = 31;
658
                  bit_start_2 = 32; // not used
659
                  bit_end_2   = 32; // not used
660 178 mohor
                  end
661 194 tadej
              `ETH_MAC_ADDR1:
662
                begin
663
                  bit_start_1 = 0;
664
                  bit_end_1   = 15;
665
                  bit_start_2 = 32; // not used
666
                  bit_end_2   = 32; // not used
667
                end
668
              `ETH_HASH_ADDR0:
669
                begin
670
                  bit_start_1 = 0;
671
                  bit_end_1   = 31;
672
                  bit_start_2 = 32; // not used
673
                  bit_end_2   = 32; // not used
674
                end
675
              default: // `ETH_HASH_ADDR1:
676
                begin
677
                  bit_start_1 = 0;
678
                  bit_end_1   = 31;
679
                  bit_start_2 = 32; // not used
680
                  bit_end_2   = 32; // not used
681
                end
682
            endcase
683
 
684
            for (i_data = 0; i_data <= 31; i_data = i_data + 1) // the position of walking one
685
              begin
686
                data = 1'b1 << i_data;
687
                if ( (addr == `ETH_MIICOMMAND) && (i_data <= 2) ) // DO NOT WRITE to 3 LSBits of MIICOMMAND !!!
688
                  ;
689
                else
690 178 mohor
                  begin
691 194 tadej
                    wbm_write(addr, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
692
                    wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
693
                    if ( ((i_data >= bit_start_1) && (i_data <= bit_end_1)) ||
694
                         ((i_data >= bit_start_2) && (i_data <= bit_end_2)) ) // data should be equal to tmp_data
695
                      begin
696
                        if (tmp_data !== data)
697 178 mohor
                        begin
698 194 tadej
                          fail = fail + 1;
699
                          test_fail("RW bit of the MAC register was not written or not read");
700
                          `TIME;
701
                          $display("wbm_init_waits %d, addr %h, data %h, tmp_data %h",
702
                                    wbm_init_waits, addr, data, tmp_data);
703
                        end
704
                      end
705
                    else // data should not be equal to tmp_data
706
                      begin
707
                        if (tmp_data === data)
708 178 mohor
                          begin
709
                            fail = fail + 1;
710 194 tadej
                            test_fail("NON RW bit of the MAC register was written, but it shouldn't be");
711 178 mohor
                            `TIME;
712 194 tadej
                            $display("wbm_init_waits %d, addr %h, data %h, tmp_data %h",
713 178 mohor
                                      wbm_init_waits, addr, data, tmp_data);
714
                          end
715 194 tadej
                      end
716
                  end
717
              end
718
          end
719
      end
720 209 tadejm
    // INTERMEDIATE DISPLAYS (The only one)
721
    $display("    ->buffer descriptors tested with 0, 1, 2, 3 and 4 bus delay cycles");
722 194 tadej
    if(fail == 0)
723
      test_ok;
724
    else
725
      fail = 0;    // Errors were reported previously
726
  end
727 178 mohor
 
728
 
729
  ////////////////////////////////////////////////////////////////////
730
  ////                                                            ////
731
  ////  Walking 1 with single cycles across MAC buffer descript.  ////
732
  ////                                                            ////
733
  ////////////////////////////////////////////////////////////////////
734
  if (test_num == 1) // Start Walking 1 with single cycles across MAC buffer descript.
735 169 mohor
  begin
736 194 tadej
    // TEST 1: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC BUFFER DESC. ( VARIOUS BUS DELAYS )
737
    test_name   = "TEST 1: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC BUFFER DESC. ( VARIOUS BUS DELAYS )";
738
    `TIME; $display("  TEST 1: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC BUFFER DESC. ( VARIOUS BUS DELAYS )");
739 178 mohor
 
740
    data = 0;
741
    // set TX and RX buffer descriptors
742
    tx_bd_num = 32'h40;
743
    wbm_write(`ETH_TX_BD_NUM, tx_bd_num, 4'hF, 1, 0, 0);
744
    for (i = 0; i <= 4; i = i + 1) // for initial wait cycles on WB bus
745
    begin
746 169 mohor
      wbm_init_waits = i;
747
      wbm_subseq_waits = {$random} % 5; // it is not important for single accesses
748 178 mohor
      for (i_addr = 32'h400; i_addr <= 32'h7FC; i_addr = i_addr + 4) // buffer descriptor address
749 169 mohor
      begin
750
        addr = `ETH_BASE + i_addr;
751 178 mohor
        if (i_addr < (32'h400 + (tx_bd_num << 3))) // TX buffer descriptors
752
        begin
753
          // set ranges of R/W bits
754
          case (addr[3])
755
            1'b0: // buffer control bits
756
            begin
757
              bit_start_1 = 0;
758
              bit_end_1   = 31; // 8;
759
              bit_start_2 = 11;
760
              bit_end_2   = 31;
761
            end
762
            default: // 1'b1: // buffer pointer
763
            begin
764
              bit_start_1 = 0;
765
              bit_end_1   = 31;
766
              bit_start_2 = 32; // not used
767
              bit_end_2   = 32; // not used
768
            end
769
          endcase
770
        end
771
        else // RX buffer descriptors
772
        begin
773
          // set ranges of R/W bits
774
          case (addr[3])
775
            1'b0: // buffer control bits
776
            begin
777
              bit_start_1 = 0;
778
              bit_end_1   = 31; // 7;
779
              bit_start_2 = 13;
780
              bit_end_2   = 31;
781
            end
782
            default: // 1'b1: // buffer pointer
783
            begin
784
              bit_start_1 = 0;
785
              bit_end_1   = 31;
786
              bit_start_2 = 32; // not used
787
              bit_end_2   = 32; // not used
788
            end
789
          endcase
790
        end
791
 
792 169 mohor
        for (i_data = 0; i_data <= 31; i_data = i_data + 1) // the position of walking one
793
        begin
794
          data = 1'b1 << i_data;
795 178 mohor
          if ( (addr[3] == 0) && (i_data == 15) ) // DO NOT WRITE to this bit !!!
796
            ;
797 169 mohor
          else
798
          begin
799
            wbm_write(addr, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
800
            wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
801
            if ( ((i_data >= bit_start_1) && (i_data <= bit_end_1)) ||
802
                 ((i_data >= bit_start_2) && (i_data <= bit_end_2)) ) // data should be equal to tmp_data
803
            begin
804
              if (tmp_data !== data)
805
              begin
806
                fail = fail + 1;
807 178 mohor
                test_fail("RW bit of the MAC buffer descriptors was not written or not read");
808 169 mohor
                `TIME;
809
                $display("wbm_init_waits %d, addr %h, data %h, tmp_data %h",
810
                          wbm_init_waits, addr, data, tmp_data);
811
              end
812
            end
813
            else // data should not be equal to tmp_data
814
            begin
815
              if (tmp_data === data)
816
              begin
817
                fail = fail + 1;
818 178 mohor
                test_fail("NON RW bit of the MAC buffer descriptors was written, but it shouldn't be");
819 169 mohor
                `TIME;
820
                $display("wbm_init_waits %d, addr %h, data %h, tmp_data %h",
821
                          wbm_init_waits, addr, data, tmp_data);
822
              end
823
            end
824
          end
825
        end
826
      end
827 178 mohor
      // INTERMEDIATE DISPLAYS
828
      case (i)
829 209 tadejm
        0:       $display("    ->buffer descriptors tested with 0 bus delay");
830
        1:       $display("    ->buffer descriptors tested with 1 bus delay cycle");
831
        2:       $display("    ->buffer descriptors tested with 2 bus delay cycles");
832
        3:       $display("    ->buffer descriptors tested with 3 bus delay cycles");
833
        default: $display("    ->buffer descriptors tested with 4 bus delay cycles");
834 178 mohor
      endcase
835
    end
836
    if(fail == 0)
837
      test_ok;
838
    else
839
      fail = 0;
840 169 mohor
  end
841 178 mohor
 
842
 
843
  ////////////////////////////////////////////////////////////////////
844
  ////                                                            ////
845
  ////  Test max reg. values and reg. values after writing        ////
846
  ////  inverse reset values and hard reset of the MAC            ////
847
  ////                                                            ////
848
  ////////////////////////////////////////////////////////////////////
849
  if (test_num == 2) // Start this task
850 169 mohor
  begin
851 194 tadej
    // TEST 2: MAX REG. VALUES AND REG. VALUES AFTER WRITING INVERSE RESET VALUES AND HARD RESET OF THE MAC
852 178 mohor
    test_name   =
853 194 tadej
      "TEST 2: MAX REG. VALUES AND REG. VALUES AFTER WRITING INVERSE RESET VALUES AND HARD RESET OF THE MAC";
854 178 mohor
    `TIME; $display(
855 194 tadej
      "  TEST 2: MAX REG. VALUES AND REG. VALUES AFTER WRITING INVERSE RESET VALUES AND HARD RESET OF THE MAC");
856 178 mohor
 
857
    // reset MAC registers
858
    hard_reset;
859
    for (i = 0; i <= 4; i = i + 1) // 0, 2 - WRITE; 1, 3, 4 - READ
860 169 mohor
    begin
861 178 mohor
      for (i_addr = 0; i_addr <= 32'h4C; i_addr = i_addr + 4) // register address
862 169 mohor
      begin
863 178 mohor
        addr = `ETH_BASE + i_addr;
864
        // set ranges of R/W bits
865
        case (addr)
866
          `ETH_MODER:
867 169 mohor
          begin
868 178 mohor
            data = 32'h0000_A800;
869
            data_max = 32'h0001_FFFF;
870 169 mohor
          end
871 178 mohor
          `ETH_INT: // READONLY - tested within INT test
872 169 mohor
          begin
873 178 mohor
            data = 32'h0000_0000;
874
            data_max = 32'h0000_0000;
875 169 mohor
          end
876
          `ETH_INT_MASK:
877 178 mohor
          begin
878
            data = 32'h0000_0000;
879
            data_max = 32'h0000_007F;
880
          end
881 169 mohor
          `ETH_IPGT:
882 178 mohor
          begin
883
            data = 32'h0000_0012;
884
            data_max = 32'h0000_007F;
885
          end
886 169 mohor
          `ETH_IPGR1:
887 178 mohor
          begin
888
            data = 32'h0000_000C;
889
            data_max = 32'h0000_007F;
890
          end
891 169 mohor
          `ETH_IPGR2:
892 178 mohor
          begin
893
            data = 32'h0000_0012;
894
            data_max = 32'h0000_007F;
895
          end
896 169 mohor
          `ETH_PACKETLEN:
897 178 mohor
          begin
898
            data = 32'h0040_0600;
899
            data_max = 32'hFFFF_FFFF;
900
          end
901 169 mohor
          `ETH_COLLCONF:
902 178 mohor
          begin
903
            data = 32'h000F_003F;
904
            data_max = 32'h000F_003F;
905
          end
906 169 mohor
          `ETH_TX_BD_NUM:
907 178 mohor
          begin
908
            data = 32'h0000_0040;
909
            data_max = 32'h0000_0080;
910
          end
911 169 mohor
          `ETH_CTRLMODER:
912 178 mohor
          begin
913
            data = 32'h0000_0000;
914
            data_max = 32'h0000_0007;
915
          end
916 169 mohor
          `ETH_MIIMODER:
917 178 mohor
          begin
918
            data = 32'h0000_0064;
919
            data_max = 32'h0000_03FF;
920
          end
921 169 mohor
          `ETH_MIICOMMAND: // "WRITEONLY" - tested within MIIM test - 3 LSBits are not written here!!!
922 178 mohor
          begin
923
            data = 32'h0000_0000;
924
            data_max = 32'h0000_0007;
925
          end
926 169 mohor
          `ETH_MIIADDRESS:
927 178 mohor
          begin
928
            data = 32'h0000_0000;
929
            data_max = 32'h0000_1F1F;
930
          end
931 169 mohor
          `ETH_MIITX_DATA:
932 178 mohor
          begin
933
            data = 32'h0000_0000;
934
            data_max = 32'h0000_FFFF;
935
          end
936 169 mohor
          `ETH_MIIRX_DATA: // READONLY - tested within MIIM test
937
          begin
938 178 mohor
            data = 32'h0000_0000;
939
            data_max = 32'h0000_0000;
940 169 mohor
          end
941 178 mohor
          `ETH_MIISTATUS: // READONLY - tested within MIIM test
942 169 mohor
          begin
943 178 mohor
            data = 32'h0000_0000;
944
            data_max = 32'h0000_0000;
945 169 mohor
          end
946 178 mohor
          `ETH_MAC_ADDR0:
947 169 mohor
          begin
948 178 mohor
            data = 32'h0000_0000;
949
            data_max = 32'hFFFF_FFFF;
950 169 mohor
          end
951 178 mohor
          `ETH_MAC_ADDR1:
952 169 mohor
          begin
953 178 mohor
            data = 32'h0000_0000;
954
            data_max = 32'h0000_FFFF;
955 169 mohor
          end
956 178 mohor
          `ETH_HASH_ADDR0:
957 169 mohor
          begin
958 178 mohor
            data = 32'h0000_0000;
959
            data_max = 32'hFFFF_FFFF;
960 169 mohor
          end
961 178 mohor
          default: // `ETH_HASH_ADDR1:
962 169 mohor
          begin
963 178 mohor
            data = 32'h0000_0000;
964
            data_max = 32'hFFFF_FFFF;
965 169 mohor
          end
966
        endcase
967 178 mohor
 
968
        wbm_init_waits = {$random} % 3;
969
        wbm_subseq_waits = {$random} % 5; // it is not important for single accesses
970
        if (i == 0)
971
          wbm_write(addr, ~data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
972
        else if (i == 2)
973
          wbm_write(addr, 32'hFFFFFFFF, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
974
        else if ((i == 1) || (i == 4))
975 169 mohor
        begin
976 178 mohor
          wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
977
          if (tmp_data !== data)
978
          begin
979
            fail = fail + 1;
980
            test_fail("RESET value of the MAC register is not correct");
981
            `TIME;
982
            $display("  addr %h, data %h, tmp_data %h", addr, data, tmp_data);
983
          end
984 169 mohor
        end
985 178 mohor
        else // check maximum values
986 169 mohor
        begin
987
          wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
988 178 mohor
          if (addr == `ETH_TX_BD_NUM) // previous data should remain in this register
989 169 mohor
          begin
990
            if (tmp_data !== data)
991
            begin
992
              fail = fail + 1;
993 178 mohor
              test_fail("Previous value of the TX_BD_NUM register did not remain");
994 169 mohor
              `TIME;
995 178 mohor
              $display("  addr %h, data_max %h, tmp_data %h", addr, data_max, tmp_data);
996 169 mohor
            end
997 178 mohor
            // try maximum (80)
998
            wbm_write(addr, data_max, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
999
            wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1000
            if (tmp_data !== data_max)
1001
            begin
1002
              fail = fail + 1;
1003
              test_fail("MAX value of the TX_BD_NUM register is not correct");
1004
              `TIME;
1005
              $display("  addr %h, data_max %h, tmp_data %h", addr, data_max, tmp_data);
1006
            end
1007
            // try one less than maximum (80)
1008
            wbm_write(addr, (data_max - 1), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1009
            wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1010
            if (tmp_data !== (data_max - 1))
1011
            begin
1012
              fail = fail + 1;
1013
              test_fail("ONE less than MAX value of the TX_BD_NUM register is not correct");
1014
              `TIME;
1015
              $display("  addr %h, data_max %h, tmp_data %h", addr, data_max, tmp_data);
1016
            end
1017
            // try one more than maximum (80)
1018
            wbm_write(addr, (data_max + 1), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1019
            wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1020
            if (tmp_data !== (data_max - 1)) // previous data should remain in this register
1021
            begin
1022
              fail = fail + 1;
1023
              test_fail("Previous value of the TX_BD_NUM register did not remain");
1024
              `TIME;
1025
              $display("  addr %h, data_max %h, tmp_data %h", addr, data_max, tmp_data);
1026
            end
1027 169 mohor
          end
1028 178 mohor
          else
1029 169 mohor
          begin
1030 178 mohor
            if (tmp_data !== data_max)
1031 169 mohor
            begin
1032
              fail = fail + 1;
1033 178 mohor
              test_fail("MAX value of the MAC register is not correct");
1034 169 mohor
              `TIME;
1035 178 mohor
              $display("  addr %h, data_max %h, tmp_data %h", addr, data_max, tmp_data);
1036 169 mohor
            end
1037
          end
1038
        end
1039
      end
1040 178 mohor
      // reset MAC registers
1041
      if ((i == 0) || (i == 3))
1042
        hard_reset;
1043 169 mohor
    end
1044 178 mohor
    if(fail == 0)
1045
      test_ok;
1046
    else
1047
      fail = 0;
1048 169 mohor
  end
1049 116 mohor
 
1050 156 mohor
 
1051 181 mohor
  ////////////////////////////////////////////////////////////////////
1052
  ////                                                            ////
1053
  ////  Test buffer desc. ram preserving values after hard reset  ////
1054
  ////  of the mac and reseting the logic                         ////
1055
  ////                                                            ////
1056
  ////////////////////////////////////////////////////////////////////
1057 178 mohor
  if (test_num == 3) // Start this task
1058 169 mohor
  begin
1059 194 tadej
    // TEST 3: BUFFER DESC. RAM PRESERVING VALUES AFTER HARD RESET OF THE MAC AND RESETING THE LOGIC
1060
    test_name   = "TEST 3: BUFFER DESC. RAM PRESERVING VALUES AFTER HARD RESET OF THE MAC AND RESETING THE LOGIC";
1061 178 mohor
    `TIME;
1062 194 tadej
    $display("  TEST 3: BUFFER DESC. RAM PRESERVING VALUES AFTER HARD RESET OF THE MAC AND RESETING THE LOGIC");
1063 178 mohor
 
1064
    // reset MAC registers
1065
    hard_reset;
1066
    // reset LOGIC with soft reset
1067
    reset_mac;
1068
    reset_mii;
1069
    for (i = 0; i <= 3; i = i + 1) // 0, 2 - WRITE; 1, 3 - READ
1070 169 mohor
    begin
1071 178 mohor
      for (i_addr = 32'h400; i_addr <= 32'h7FC; i_addr = i_addr + 4) // buffer descriptor address
1072 169 mohor
      begin
1073 178 mohor
        addr = `ETH_BASE + i_addr;
1074
 
1075
        wbm_init_waits = {$random} % 3;
1076
        wbm_subseq_waits = {$random} % 5; // it is not important for single accesses
1077
        if (i == 0)
1078 169 mohor
        begin
1079 178 mohor
          data = 32'hFFFFFFFF;
1080
          wbm_write(addr, 32'hFFFFFFFF, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1081 169 mohor
        end
1082 178 mohor
        else if (i == 2)
1083 169 mohor
        begin
1084 178 mohor
          data = 32'h00000000;
1085
          wbm_write(addr, 32'h00000000, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1086 169 mohor
        end
1087
        else
1088
        begin
1089 178 mohor
          wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1090
          if (tmp_data !== data)
1091 169 mohor
          begin
1092
            fail = fail + 1;
1093 178 mohor
            test_fail("PRESERVED value of the MAC buffer descriptors is not correct");
1094 169 mohor
            `TIME;
1095 178 mohor
            $display("  addr %h, data %h, tmp_data %h", addr, data, tmp_data);
1096 169 mohor
          end
1097
        end
1098
      end
1099 178 mohor
      if ((i == 0) || (i == 2))
1100
      begin
1101
        // reset MAC registers
1102
        hard_reset;
1103
        // reset LOGIC with soft reset
1104
        reset_mac;
1105
        reset_mii;
1106
      end
1107 169 mohor
    end
1108 178 mohor
    if(fail == 0)
1109
      test_ok;
1110
    else
1111
    fail = 0;
1112 169 mohor
  end
1113 116 mohor
 
1114
 
1115 178 mohor
  if (test_num == 4) // Start this task
1116 169 mohor
  begin
1117 194 tadej
        /*  // TEST 4: 'WALKING ONE' WITH BURST CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )
1118
          test_name   = "TEST 4: 'WALKING ONE' WITH BURST CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )";
1119
          `TIME; $display("  TEST 4: 'WALKING ONE' WITH BURST CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )");
1120 178 mohor
 
1121
          data = 0;
1122
          burst_data = 0;
1123
          burst_tmp_data = 0;
1124
          i_length = 10; // two bursts for length 20
1125
          for (i = 0; i <= 4; i = i + 1) // for initial wait cycles on WB bus
1126
          begin
1127
            for (i1 = 0; i1 <= 4; i1 = i1 + 1) // for initial wait cycles on WB bus
1128
            begin
1129
              wbm_init_waits = i;
1130
              wbm_subseq_waits = i1;
1131
              #1;
1132
              for (i_data = 0; i_data <= 31; i_data = i_data + 1) // the position of walking one
1133
              begin
1134
                data = 1'b1 << i_data;
1135
                #1;
1136
                for (i2 = 32'h4C; i2 >= 0; i2 = i2 - 4)
1137
                begin
1138
                  burst_data = burst_data << 32;
1139
                  // DO NOT WRITE to 3 LSBits of MIICOMMAND !!!
1140
                  if ( ((`ETH_BASE + i2) == `ETH_MIICOMMAND) && (i_data <= 2) )
1141
                  begin
1142
                    #1 burst_data[31:0] = 0;
1143
                  end
1144
                  else
1145
                  begin
1146
                    #1 burst_data[31:0] = data;
1147
                  end
1148
                end
1149
                #1;
1150
                // 2 burst writes
1151
                addr = `ETH_BASE; // address of a first burst
1152
                wbm_write(addr, burst_data[(32 * 10 - 1):0], 4'hF, i_length, wbm_init_waits, wbm_subseq_waits);
1153
                burst_tmp_data = burst_data >> (32 * i_length);
1154
                addr = addr + 32'h28; // address of a second burst
1155
                wbm_write(addr, burst_tmp_data[(32 * 10 - 1):0], 4'hF, i_length, wbm_init_waits, wbm_subseq_waits);
1156
                #1;
1157
                // 2 burst reads
1158
                addr = `ETH_BASE; // address of a first burst
1159
                wbm_read(addr, burst_tmp_data[(32 * 10 - 1):0], 4'hF, i_length,
1160
                         wbm_init_waits, wbm_subseq_waits); // first burst
1161
                burst_tmp_data = burst_tmp_data << (32 * i_length);
1162
                addr = addr + 32'h28; // address of a second burst
1163
                wbm_read(addr, burst_tmp_data[(32 * 10 - 1):0], 4'hF, i_length,
1164
                         wbm_init_waits, wbm_subseq_waits); // second burst
1165
                #1;
1166
                for (i2 = 0; i2 <= 32'h4C; i2 = i2 + 4)
1167
                begin
1168
                  // set ranges of R/W bits
1169
                  case (`ETH_BASE + i2)
1170
                  `ETH_MODER:
1171
                    begin
1172
                      bit_start_1 = 0;
1173
                      bit_end_1   = 16;
1174
                      bit_start_2 = 32; // not used
1175
                      bit_end_2   = 32; // not used
1176
                    end
1177
                  `ETH_INT: // READONLY - tested within INT test
1178
                    begin
1179
                      bit_start_1 = 32; // not used
1180
                      bit_end_1   = 32; // not used
1181
                      bit_start_2 = 32; // not used
1182
                      bit_end_2   = 32; // not used
1183
                    end
1184
                  `ETH_INT_MASK:
1185
                    begin
1186
                      bit_start_1 = 0;
1187
                      bit_end_1   = 6;
1188
                      bit_start_2 = 32; // not used
1189
                      bit_end_2   = 32; // not used
1190
                    end
1191
                  `ETH_IPGT:
1192
                    begin
1193
                      bit_start_1 = 0;
1194
                      bit_end_1   = 6;
1195
                      bit_start_2 = 32; // not used
1196
                      bit_end_2   = 32; // not used
1197
                    end
1198
                  `ETH_IPGR1:
1199
                    begin
1200
                      bit_start_1 = 0;
1201
                      bit_end_1   = 6;
1202
                      bit_start_2 = 32; // not used
1203
                      bit_end_2   = 32; // not used
1204
                    end
1205
                  `ETH_IPGR2:
1206
                    begin
1207
                      bit_start_1 = 0;
1208
                      bit_end_1   = 6;
1209
                      bit_start_2 = 32; // not used
1210
                      bit_end_2   = 32; // not used
1211
                    end
1212
                  `ETH_PACKETLEN:
1213
                    begin
1214
                      bit_start_1 = 0;
1215
                      bit_end_1   = 31;
1216
                      bit_start_2 = 32; // not used
1217
                      bit_end_2   = 32; // not used
1218
                    end
1219
                  `ETH_COLLCONF:
1220
                    begin
1221
                      bit_start_1 = 0;
1222
                      bit_end_1   = 5;
1223
                      bit_start_2 = 16;
1224
                      bit_end_2   = 19;
1225
                    end
1226
                  `ETH_TX_BD_NUM:
1227
                    begin
1228
                      bit_start_1 = 0;
1229
                      bit_end_1   = 7;
1230
                      bit_start_2 = 32; // not used
1231
                      bit_end_2   = 32; // not used
1232
                    end
1233
                  `ETH_CTRLMODER:
1234
                    begin
1235
                      bit_start_1 = 0;
1236
                      bit_end_1   = 2;
1237
                      bit_start_2 = 32; // not used
1238
                      bit_end_2   = 32; // not used
1239
                    end
1240
                  `ETH_MIIMODER:
1241
                    begin
1242
                      bit_start_1 = 0;
1243
                      bit_end_1   = 9;
1244
                      bit_start_2 = 32; // not used
1245
                      bit_end_2   = 32; // not used
1246
                    end
1247
                  `ETH_MIICOMMAND: // "WRITEONLY" - tested within MIIM test - 3 LSBits are not written here!!!
1248
                    begin
1249
                      bit_start_1 = 32; // not used
1250
                      bit_end_1   = 32; // not used
1251
                      bit_start_2 = 32; // not used
1252
                      bit_end_2   = 32; // not used
1253
                    end
1254
                  `ETH_MIIADDRESS:
1255
                    begin
1256
                      bit_start_1 = 0;
1257
                      bit_end_1   = 4;
1258
                      bit_start_2 = 8;
1259
                      bit_end_2   = 12;
1260
                    end
1261
                  `ETH_MIITX_DATA:
1262
                    begin
1263
                      bit_start_1 = 0;
1264
                      bit_end_1   = 15;
1265
                      bit_start_2 = 32; // not used
1266
                      bit_end_2   = 32; // not used
1267
                    end
1268
                  `ETH_MIIRX_DATA: // READONLY - tested within MIIM test
1269
                    begin
1270
                      bit_start_1 = 32; // not used
1271
                      bit_end_1   = 32; // not used
1272
                      bit_start_2 = 32; // not used
1273
                      bit_end_2   = 32; // not used
1274
                    end
1275
                  `ETH_MIISTATUS: // READONLY - tested within MIIM test
1276
                    begin
1277
                      bit_start_1 = 32; // not used
1278
                      bit_end_1   = 32; // not used
1279
                      bit_start_2 = 32; // not used
1280
                      bit_end_2   = 32; // not used
1281
                    end
1282
                  `ETH_MAC_ADDR0:
1283
                    begin
1284
                      bit_start_1 = 0;
1285
                      bit_end_1   = 31;
1286
                      bit_start_2 = 32; // not used
1287
                      bit_end_2   = 32; // not used
1288
                    end
1289
                  `ETH_MAC_ADDR1:
1290
                    begin
1291
                      bit_start_1 = 0;
1292
                      bit_end_1   = 15;
1293
                      bit_start_2 = 32; // not used
1294
                      bit_end_2   = 32; // not used
1295
                    end
1296
                  `ETH_HASH_ADDR0:
1297
                    begin
1298
                      bit_start_1 = 0;
1299
                      bit_end_1   = 31;
1300
                      bit_start_2 = 32; // not used
1301
                      bit_end_2   = 32; // not used
1302
                    end
1303
                  default: // `ETH_HASH_ADDR1:
1304
                    begin
1305
                      bit_start_1 = 0;
1306
                      bit_end_1   = 31;
1307
                      bit_start_2 = 32; // not used
1308
                      bit_end_2   = 32; // not used
1309
                    end
1310
                  endcase
1311
                  #1;
1312
                  // 3 LSBits of MIICOMMAND are NOT written !!!
1313
                  if ( ((`ETH_BASE + i2) == `ETH_MIICOMMAND) && (i_data <= 2) )
1314
                  begin
1315
                    if (burst_tmp_data[31:0] !== burst_data[31:0])
1316
                    begin
1317
                      fail = fail + 1;
1318
                      test_fail("NON WR bit of the MAC MIICOMMAND register was wrong written or read");
1319
                      `TIME;
1320
                      $display("wbm_init_waits %d, wbm_subseq_waits %d, addr %h, data %h, tmp_data %h",
1321
                                wbm_init_waits, wbm_subseq_waits, i2, burst_data[31:0], burst_tmp_data[31:0]);
1322
                    end
1323
                  end
1324
                  else
1325
                  begin
1326
                    if ( ((i_data >= bit_start_1) && (i_data <= bit_end_1)) ||
1327
                         ((i_data >= bit_start_2) && (i_data <= bit_end_2)) ) // data should be equal to tmp_data
1328
                    begin
1329
                      if (burst_tmp_data[31:0] !== burst_data[31:0])
1330
                      begin
1331
                        fail = fail + 1;
1332
                        test_fail("RW bit of the MAC register was not written or not read");
1333
                        `TIME;
1334
                        $display("wbm_init_waits %d, wbm_subseq_waits %d, addr %h, data %h, tmp_data %h",
1335
                                  wbm_init_waits, wbm_subseq_waits, i2, burst_data[31:0], burst_tmp_data[31:0]);
1336
                      end
1337
                    end
1338
                    else // data should not be equal to tmp_data
1339
                    begin
1340
                      if (burst_tmp_data[31:0] === burst_data[31:0])
1341
                      begin
1342
                        fail = fail + 1;
1343
                        test_fail("NON RW bit of the MAC register was written, but it shouldn't be");
1344
                        `TIME;
1345
                        $display("wbm_init_waits %d, wbm_subseq_waits %d, addr %h, data %h, tmp_data %h",
1346
                                  wbm_init_waits, wbm_subseq_waits, i2, burst_data[31:0], burst_tmp_data[31:0]);
1347
                      end
1348
                    end
1349
                  end
1350
                  burst_tmp_data = burst_tmp_data >> 32;
1351
                  burst_data = burst_data >> 32;
1352
                end
1353
              end
1354
            end
1355
          end
1356
          if(fail == 0)
1357
            test_ok;
1358
          else
1359
            fail = 0;*/
1360
  end
1361 116 mohor
 
1362 169 mohor
end
1363 156 mohor
 
1364 169 mohor
end
1365
endtask // test_access_to_mac_reg
1366 156 mohor
 
1367
 
1368 169 mohor
task test_mii;
1369
  input  [31:0]  start_task;
1370
  input  [31:0]  end_task;
1371
  integer        i;
1372
  integer        i1;
1373
  integer        i2;
1374
  integer        i3;
1375
  integer        cnt;
1376
  integer        fail;
1377 181 mohor
  integer        test_num;
1378 169 mohor
  reg     [8:0]  clk_div; // only 8 bits are valid!
1379
  reg     [4:0]  phy_addr;
1380
  reg     [4:0]  reg_addr;
1381
  reg     [15:0] phy_data;
1382
  reg     [15:0] tmp_data;
1383
begin
1384
// MIIM MODULE TEST
1385
test_heading("MIIM MODULE TEST");
1386
$display(" ");
1387
$display("MIIM MODULE TEST");
1388
fail = 0;
1389 156 mohor
 
1390 192 tadej
// reset MAC registers
1391
hard_reset;
1392
// reset MAC and MII LOGIC with soft reset
1393
reset_mac;
1394 169 mohor
reset_mii;
1395 116 mohor
 
1396 194 tadej
 
1397 181 mohor
//////////////////////////////////////////////////////////////////////
1398
////                                                              ////
1399
////  test_mii:                                                   ////
1400
////                                                              ////
1401
////  0:  Test clock divider of mii management module with all    ////
1402
////      possible frequences.                                    ////
1403
////  1:  Test various readings from 'real' phy registers.        ////
1404
////  2:  Test various writings to 'real' phy registers (control  ////
1405
////      and non writable registers)                             ////
1406
////  3:  Test reset phy through mii management module            ////
1407
////  4:  Test 'walking one' across phy address (with and without ////
1408
////      preamble)                                               ////
1409
////  5:  Test 'walking one' across phy's register address (with  ////
1410
////      and without preamble)                                   ////
1411
////  6:  Test 'walking one' across phy's data (with and without  ////
1412
////      preamble)                                               ////
1413
////  7:  Test reading from phy with wrong phy address (host      ////
1414
////      reading high 'z' data)                                  ////
1415
////  8:  Test writing to phy with wrong phy address and reading  ////
1416
////      from correct one                                        ////
1417
////  9:  Test sliding stop scan command immediately after read   ////
1418
////      request (with and without preamble)                     ////
1419
//// 10:  Test sliding stop scan command immediately after write  ////
1420
////      request (with and without preamble)                     ////
1421
//// 11:  Test busy and nvalid status durations during write      ////
1422
////      (with and without preamble)                             ////
1423
//// 12:  Test busy and nvalid status durations during write      ////
1424
////      (with and without preamble)                             ////
1425
//// 13:  Test busy and nvalid status durations during scan (with ////
1426
////      and without preamble)                                   ////
1427
//// 14:  Test scan status from phy with detecting link-fail bit  ////
1428
////      (with and without preamble)                             ////
1429
//// 15:  Test scan status from phy with sliding link-fail bit    ////
1430
////      (with and without preamble)                             ////
1431
//// 16:  Test sliding stop scan command immediately after scan   ////
1432
////      request (with and without preamble)                     ////
1433
//// 17:  Test sliding stop scan command after 2. scan (with and  ////
1434
////      without preamble)                                       ////
1435
////                                                              ////
1436
//////////////////////////////////////////////////////////////////////
1437 194 tadej
for (test_num = start_task; test_num <= end_task; test_num = test_num + 1)
1438 169 mohor
begin
1439 194 tadej
 
1440 181 mohor
  ////////////////////////////////////////////////////////////////////
1441
  ////                                                            ////
1442
  ////  Test clock divider of mii management module with all      ////
1443
  ////  possible frequences.                                      ////
1444
  ////                                                            ////
1445
  ////////////////////////////////////////////////////////////////////
1446
  if (test_num == 0) // Test clock divider of mii management module with all possible frequences.
1447 169 mohor
  begin
1448 194 tadej
    // TEST 0: CLOCK DIVIDER OF MII MANAGEMENT MODULE WITH ALL POSSIBLE FREQUENCES
1449
    test_name   = "TEST 0: CLOCK DIVIDER OF MII MANAGEMENT MODULE WITH ALL POSSIBLE FREQUENCES";
1450
    `TIME; $display("  TEST 0: CLOCK DIVIDER OF MII MANAGEMENT MODULE WITH ALL POSSIBLE FREQUENCES");
1451 181 mohor
 
1452
    wait(Mdc_O); // wait for MII clock to be 1
1453
    for(clk_div = 0; clk_div <= 255; clk_div = clk_div + 1)
1454
    begin
1455
      i1 = 0;
1456
      i2 = 0;
1457
      #Tp mii_set_clk_div(clk_div[7:0]);
1458
      @(posedge Mdc_O);
1459
      #Tp;
1460
      fork
1461 169 mohor
        begin
1462 181 mohor
          @(posedge Mdc_O);
1463 169 mohor
          #Tp;
1464 181 mohor
          disable count_i1;
1465
          disable count_i2;
1466 169 mohor
        end
1467 181 mohor
        begin: count_i1
1468
          forever
1469
          begin
1470
            @(posedge wb_clk);
1471
            i1 = i1 + 1;
1472
            #Tp;
1473
          end
1474
        end
1475
        begin: count_i2
1476
          forever
1477
          begin
1478
            @(negedge wb_clk);
1479
            i2 = i2 + 1;
1480
            #Tp;
1481
          end
1482
        end
1483
      join
1484
      if((clk_div[7:0] == 0) || (clk_div[7:0] == 1) || (clk_div[7:0] == 2) || (clk_div[7:0] == 3))
1485
      begin
1486
        if((i1 == i2) && (i1 == 2))
1487 169 mohor
        begin
1488
        end
1489 181 mohor
        else
1490
        begin
1491
          fail = fail + 1;
1492 209 tadejm
          test_fail("Clock divider of MII module did'nt divide frequency corectly (it should divide by 2)");
1493 181 mohor
        end
1494 169 mohor
      end
1495
      else
1496
      begin
1497 181 mohor
        if((i1 == i2) && (i1 == {clk_div[7:1], 1'b0}))
1498
        begin
1499
        end
1500
        else
1501
        begin
1502
          fail = fail + 1;
1503
          test_fail("Clock divider of MII module did'nt divide frequency corectly");
1504
        end
1505 169 mohor
      end
1506
    end
1507 181 mohor
    if(fail == 0)
1508
      test_ok;
1509 169 mohor
    else
1510 181 mohor
      fail = 0;
1511
  end
1512
 
1513
 
1514
  ////////////////////////////////////////////////////////////////////
1515
  ////                                                            ////
1516
  ////  Test various readings from 'real' phy registers.          ////
1517
  ////                                                            ////
1518
  ////////////////////////////////////////////////////////////////////
1519
  if (test_num == 1) // Test various readings from 'real' phy registers.
1520
  begin
1521 194 tadej
    // TEST 1: VARIOUS READINGS FROM 'REAL' PHY REGISTERS
1522
    test_name   = "TEST 1: VARIOUS READINGS FROM 'REAL' PHY REGISTERS";
1523
    `TIME; $display("  TEST 1: VARIOUS READINGS FROM 'REAL' PHY REGISTERS");
1524 181 mohor
 
1525
    // set the fastest possible MII
1526
    clk_div = 0;
1527
    mii_set_clk_div(clk_div[7:0]);
1528
    // set address
1529
    reg_addr = 5'h1F;
1530
    phy_addr = 5'h1;
1531
    while(reg_addr >= 5'h4)
1532 169 mohor
    begin
1533 181 mohor
      // read request
1534
      #Tp mii_read_req(phy_addr, reg_addr);
1535
      check_mii_busy; // wait for read to finish
1536
      // read data
1537
      #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1538
      if (phy_data !== 16'hDEAD)
1539 169 mohor
      begin
1540 181 mohor
        test_fail("Wrong data was read from PHY from 'not used' address space");
1541
        fail = fail + 1;
1542 169 mohor
      end
1543 181 mohor
      if (reg_addr == 5'h4) // go out of for loop
1544
        reg_addr = 5'h3;
1545 169 mohor
      else
1546 181 mohor
        reg_addr = reg_addr - 5'h9;
1547 169 mohor
    end
1548 181 mohor
 
1549
    // set address
1550
    reg_addr = 5'h3;
1551
    // read request
1552
    #Tp mii_read_req(phy_addr, reg_addr);
1553
    check_mii_busy; // wait for read to finish
1554
    // read data
1555
    #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1556
    if (phy_data !== {`PHY_ID2, `MAN_MODEL_NUM, `MAN_REVISION_NUM})
1557
    begin
1558
      test_fail("Wrong data was read from PHY from ID register 2");
1559
      fail = fail + 1;
1560
    end
1561
    if(fail == 0)
1562
      test_ok;
1563
    else
1564
      fail = 0;
1565 169 mohor
  end
1566 116 mohor
 
1567
 
1568 181 mohor
  ////////////////////////////////////////////////////////////////////
1569
  ////                                                            ////
1570
  ////  Test various writings to 'real' phy registers (control    ////
1571
  ////  and non writable registers)                               ////
1572
  ////                                                            ////
1573
  ////////////////////////////////////////////////////////////////////
1574
  if (test_num == 2) // 
1575 169 mohor
  begin
1576 194 tadej
    // TEST 2: VARIOUS WRITINGS TO 'REAL' PHY REGISTERS ( CONTROL AND NON WRITABLE REGISTERS )
1577
    test_name   = "TEST 2: VARIOUS WRITINGS TO 'REAL' PHY REGISTERS ( CONTROL AND NON WRITABLE REGISTERS )";
1578
    `TIME; $display("  TEST 2: VARIOUS WRITINGS TO 'REAL' PHY REGISTERS ( CONTROL AND NON WRITABLE REGISTERS )");
1579 181 mohor
 
1580
    // negate data and try to write into unwritable register
1581
    tmp_data = ~phy_data;
1582
    // write request
1583
    #Tp mii_write_req(phy_addr, reg_addr, tmp_data);
1584
    check_mii_busy; // wait for write to finish
1585 169 mohor
    // read request
1586
    #Tp mii_read_req(phy_addr, reg_addr);
1587
    check_mii_busy; // wait for read to finish
1588
    // read data
1589 181 mohor
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1590
    if (tmp_data !== phy_data)
1591
    begin
1592
      test_fail("Data was written into unwritable PHY register - ID register 2");
1593
      fail = fail + 1;
1594
    end
1595
 
1596
    // set address
1597
    reg_addr = 5'h0; // control register
1598
    // read request
1599
    #Tp mii_read_req(phy_addr, reg_addr);
1600
    check_mii_busy; // wait for read to finish
1601
    // read data
1602
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1603
    // write request
1604
    phy_data = 16'h7DFF; // bit 15 (RESET bit) and bit 9 are self clearing bits
1605
    #Tp mii_write_req(phy_addr, reg_addr, phy_data);
1606
    check_mii_busy; // wait for write to finish
1607
    // read request
1608
    #Tp mii_read_req(phy_addr, reg_addr);
1609
    check_mii_busy; // wait for read to finish
1610
    // read data
1611 169 mohor
    #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1612 181 mohor
    if (phy_data !== 16'h7DFF)
1613 169 mohor
    begin
1614 181 mohor
      test_fail("Data was not correctly written into OR read from writable PHY register - control register");
1615 169 mohor
      fail = fail + 1;
1616
    end
1617 181 mohor
    // write request
1618
    #Tp mii_write_req(phy_addr, reg_addr, tmp_data);
1619
    check_mii_busy; // wait for write to finish
1620
    // read request
1621
    #Tp mii_read_req(phy_addr, reg_addr);
1622
    check_mii_busy; // wait for read to finish
1623
    // read data
1624
    #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1625
    if (phy_data !== tmp_data)
1626
    begin
1627
      test_fail("Data was not correctly written into OR read from writable PHY register - control register");
1628
      fail = fail + 1;
1629
    end
1630
    if(fail == 0)
1631
      test_ok;
1632 116 mohor
    else
1633 181 mohor
      fail = 0;
1634 169 mohor
  end
1635 116 mohor
 
1636
 
1637 181 mohor
  ////////////////////////////////////////////////////////////////////
1638
  ////                                                            ////
1639
  ////  Test reset phy through mii management module              ////
1640
  ////                                                            ////
1641
  ////////////////////////////////////////////////////////////////////
1642
  if (test_num == 3) // 
1643 169 mohor
  begin
1644 194 tadej
    // TEST 3: RESET PHY THROUGH MII MANAGEMENT MODULE
1645
    test_name   = "TEST 3: RESET PHY THROUGH MII MANAGEMENT MODULE";
1646
    `TIME; $display("  TEST 3: RESET PHY THROUGH MII MANAGEMENT MODULE");
1647 181 mohor
 
1648
    // set address
1649
    reg_addr = 5'h0; // control register
1650
    // write request
1651
    phy_data = 16'h7DFF; // bit 15 (RESET bit) and bit 9 are self clearing bits
1652
    #Tp mii_write_req(phy_addr, reg_addr, phy_data);
1653
    check_mii_busy; // wait for write to finish
1654
    // read request
1655
    #Tp mii_read_req(phy_addr, reg_addr);
1656
    check_mii_busy; // wait for read to finish
1657
    // read data
1658
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1659
    if (phy_data !== tmp_data)
1660
    begin
1661
      test_fail("Data was not correctly written into OR read from writable PHY register - control register");
1662
      fail = fail + 1;
1663
    end
1664
    // set reset bit - selfclearing bit in PHY
1665
    phy_data = phy_data | 16'h8000;
1666
    // write request
1667
    #Tp mii_write_req(phy_addr, reg_addr, phy_data);
1668
    check_mii_busy; // wait for write to finish
1669
    // read request
1670
    #Tp mii_read_req(phy_addr, reg_addr);
1671
    check_mii_busy; // wait for read to finish
1672
    // read data
1673
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1674
    // check self clearing of reset bit
1675
    if (tmp_data[15] !== 1'b0)
1676
    begin
1677
      test_fail("Reset bit should be self cleared - control register");
1678
      fail = fail + 1;
1679
    end
1680
    // check reset value of control register
1681
    if (tmp_data !== {2'h0, (`LED_CFG1 || `LED_CFG2), `LED_CFG1, 3'h0, `LED_CFG3, 8'h0})
1682
    begin
1683
      test_fail("PHY was not reset correctly AND/OR reset bit not self cleared");
1684
      fail = fail + 1;
1685
    end
1686
    if(fail == 0)
1687
      test_ok;
1688
    else
1689
      fail = 0;
1690 169 mohor
  end
1691
 
1692
 
1693 181 mohor
  ////////////////////////////////////////////////////////////////////
1694
  ////                                                            ////
1695
  ////  Test 'walking one' across phy address (with and without   ////
1696
  ////  preamble)                                                 ////
1697
  ////                                                            ////
1698
  ////////////////////////////////////////////////////////////////////
1699
  if (test_num == 4) // 
1700 169 mohor
  begin
1701 194 tadej
    // TEST 4: 'WALKING ONE' ACROSS PHY ADDRESS ( WITH AND WITHOUT PREAMBLE )
1702
    test_name   = "TEST 4: 'WALKING ONE' ACROSS PHY ADDRESS ( WITH AND WITHOUT PREAMBLE )";
1703
    `TIME; $display("  TEST 4: 'WALKING ONE' ACROSS PHY ADDRESS ( WITH AND WITHOUT PREAMBLE )");
1704 181 mohor
 
1705
    // set PHY to test mode
1706
    #Tp eth_phy.test_regs(1); // set test registers (wholy writable registers) and respond to all PHY addresses
1707
    for (i = 0; i <= 1; i = i + 1)
1708 169 mohor
    begin
1709 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
1710
      #Tp eth_phy.clear_test_regs;
1711
      // MII mode register
1712
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}), 4'hF, 1, wbm_init_waits,
1713
                wbm_subseq_waits);
1714
      // walk one across phy address
1715
      for (phy_addr = 5'h1; phy_addr > 5'h0; phy_addr = phy_addr << 1)
1716 169 mohor
      begin
1717 181 mohor
        reg_addr = $random;
1718
        tmp_data = $random;
1719
        // write request
1720
        #Tp mii_write_req(phy_addr, reg_addr, tmp_data);
1721
        check_mii_busy; // wait for write to finish
1722
        // read request
1723
        #Tp mii_read_req(phy_addr, reg_addr);
1724
        check_mii_busy; // wait for read to finish
1725
        // read data
1726
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1727
        #Tp;
1728
        if (phy_data !== tmp_data)
1729
        begin
1730
          if (i)
1731
            test_fail("Data was not correctly written into OR read from test registers (without preamble)");
1732
          else
1733
            test_fail("Data was not correctly written into OR read from test registers (with preamble)");
1734
          fail = fail + 1;
1735
        end
1736
        @(posedge wb_clk);
1737
        #Tp;
1738 169 mohor
      end
1739
    end
1740 181 mohor
    // set PHY to normal mode
1741
    #Tp eth_phy.test_regs(0);
1742
    #Tp eth_phy.preamble_suppresed(0);
1743
    // MII mode register
1744
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1745
    if(fail == 0)
1746
      test_ok;
1747
    else
1748
      fail = 0;
1749 169 mohor
  end
1750
 
1751
 
1752 181 mohor
  ////////////////////////////////////////////////////////////////////
1753
  ////                                                            ////
1754
  ////  Test 'walking one' across phy's register address (with    ////
1755
  ////  and without preamble)                                     ////
1756
  ////                                                            ////
1757
  ////////////////////////////////////////////////////////////////////
1758
  if (test_num == 5) // 
1759 169 mohor
  begin
1760 194 tadej
    // TEST 5: 'WALKING ONE' ACROSS PHY'S REGISTER ADDRESS ( WITH AND WITHOUT PREAMBLE )
1761
    test_name   = "TEST 5: 'WALKING ONE' ACROSS PHY'S REGISTER ADDRESS ( WITH AND WITHOUT PREAMBLE )";
1762
    `TIME; $display("  TEST 5: 'WALKING ONE' ACROSS PHY'S REGISTER ADDRESS ( WITH AND WITHOUT PREAMBLE )");
1763 181 mohor
 
1764
    // set PHY to test mode
1765
    #Tp eth_phy.test_regs(1); // set test registers (wholy writable registers) and respond to all PHY addresses
1766
    for (i = 0; i <= 1; i = i + 1)
1767 169 mohor
    begin
1768 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
1769
      #Tp eth_phy.clear_test_regs;
1770
      // MII mode register
1771
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}), 4'hF, 1, wbm_init_waits,
1772
                wbm_subseq_waits);
1773
      // walk one across reg address
1774
      for (reg_addr = 5'h1; reg_addr > 5'h0; reg_addr = reg_addr << 1)
1775 169 mohor
      begin
1776 181 mohor
        phy_addr = $random;
1777
        tmp_data = $random;
1778
        // write request
1779
        #Tp mii_write_req(phy_addr, reg_addr, tmp_data);
1780
        check_mii_busy; // wait for write to finish
1781
        // read request
1782
        #Tp mii_read_req(phy_addr, reg_addr);
1783
        check_mii_busy; // wait for read to finish
1784
        // read data
1785
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1786
        #Tp;
1787
        if (phy_data !== tmp_data)
1788
        begin
1789
          if (i)
1790
            test_fail("Data was not correctly written into OR read from test registers (without preamble)");
1791
          else
1792
            test_fail("Data was not correctly written into OR read from test registers (with preamble)");
1793
          fail = fail + 1;
1794
        end
1795
        @(posedge wb_clk);
1796
        #Tp;
1797 169 mohor
      end
1798
    end
1799 181 mohor
    // set PHY to normal mode
1800
    #Tp eth_phy.test_regs(0);
1801
    #Tp eth_phy.preamble_suppresed(0);
1802
    // MII mode register
1803
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1804
    if(fail == 0)
1805
      test_ok;
1806
    else
1807
      fail = 0;
1808 169 mohor
  end
1809
 
1810
 
1811 181 mohor
  ////////////////////////////////////////////////////////////////////
1812
  ////                                                            ////
1813
  ////  Test 'walking one' across phy's data (with and without    ////
1814
  ////  preamble)                                                 ////
1815
  ////                                                            ////
1816
  ////////////////////////////////////////////////////////////////////
1817
  if (test_num == 6) // 
1818 169 mohor
  begin
1819 194 tadej
    // TEST 6: 'WALKING ONE' ACROSS PHY'S DATA ( WITH AND WITHOUT PREAMBLE )
1820
    test_name   = "TEST 6: 'WALKING ONE' ACROSS PHY'S DATA ( WITH AND WITHOUT PREAMBLE )";
1821
    `TIME; $display("  TEST 6: 'WALKING ONE' ACROSS PHY'S DATA ( WITH AND WITHOUT PREAMBLE )");
1822 181 mohor
 
1823
    // set PHY to test mode
1824
    #Tp eth_phy.test_regs(1); // set test registers (wholy writable registers) and respond to all PHY addresses
1825
    for (i = 0; i <= 1; i = i + 1)
1826 169 mohor
    begin
1827 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
1828
      #Tp eth_phy.clear_test_regs;
1829
      // MII mode register
1830
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}), 4'hF, 1, wbm_init_waits,
1831
                wbm_subseq_waits);
1832
      // walk one across data
1833
      for (tmp_data = 16'h1; tmp_data > 16'h0; tmp_data = tmp_data << 1)
1834 169 mohor
      begin
1835 181 mohor
        phy_addr = $random;
1836
        reg_addr = $random;
1837
        // write request
1838
        #Tp mii_write_req(phy_addr, reg_addr, tmp_data);
1839
        check_mii_busy; // wait for write to finish
1840
        // read request
1841
        #Tp mii_read_req(phy_addr, reg_addr);
1842
        check_mii_busy; // wait for read to finish
1843
        // read data
1844
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1845
        #Tp;
1846
        if (phy_data !== tmp_data)
1847
        begin
1848
          if (i)
1849
            test_fail("Data was not correctly written into OR read from test registers (without preamble)");
1850
          else
1851
            test_fail("Data was not correctly written into OR read from test registers (with preamble)");
1852
          fail = fail + 1;
1853
        end
1854
        @(posedge wb_clk);
1855
        #Tp;
1856 169 mohor
      end
1857
    end
1858 181 mohor
    // set PHY to normal mode
1859
    #Tp eth_phy.test_regs(0);
1860
    #Tp eth_phy.preamble_suppresed(0);
1861
    // MII mode register
1862
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1863
    if(fail == 0)
1864
      test_ok;
1865
    else
1866
      fail = 0;
1867 169 mohor
  end
1868
 
1869
 
1870 181 mohor
  ////////////////////////////////////////////////////////////////////
1871
  ////                                                            ////
1872
  ////  Test reading from phy with wrong phy address (host        ////
1873
  ////  reading high 'z' data)                                    ////
1874
  ////                                                            ////
1875
  ////////////////////////////////////////////////////////////////////
1876
  if (test_num == 7) // 
1877 169 mohor
  begin
1878 194 tadej
    // TEST 7: READING FROM PHY WITH WRONG PHY ADDRESS ( HOST READING HIGH 'Z' DATA )
1879
    test_name   = "TEST 7: READING FROM PHY WITH WRONG PHY ADDRESS ( HOST READING HIGH 'Z' DATA )";
1880
    `TIME; $display("  TEST 7: READING FROM PHY WITH WRONG PHY ADDRESS ( HOST READING HIGH 'Z' DATA )");
1881 181 mohor
 
1882
    phy_addr = 5'h2; // wrong PHY address
1883
    // read request
1884
    #Tp mii_read_req(phy_addr, reg_addr);
1885
    check_mii_busy; // wait for read to finish
1886
    // read data
1887
    $display("  => Two errors will be displayed from WB Bus Monitor, because correct HIGH Z data was read");
1888
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1889
    if (tmp_data !== 16'hzzzz)
1890
    begin
1891
      test_fail("Data was read from PHY register with wrong PHY address - control register");
1892
      fail = fail + 1;
1893
    end
1894
    if(fail == 0)
1895
      test_ok;
1896
    else
1897
      fail = 0;
1898 169 mohor
  end
1899
 
1900
 
1901 181 mohor
  ////////////////////////////////////////////////////////////////////
1902
  ////                                                            ////
1903
  ////  Test writing to phy with wrong phy address and reading    ////
1904
  ////  from correct one                                          ////
1905
  ////                                                            ////
1906
  ////////////////////////////////////////////////////////////////////
1907
  if (test_num == 8) // 
1908 169 mohor
  begin
1909 194 tadej
    // TEST 8: WRITING TO PHY WITH WRONG PHY ADDRESS AND READING FROM CORRECT ONE
1910
    test_name   = "TEST 8: WRITING TO PHY WITH WRONG PHY ADDRESS AND READING FROM CORRECT ONE";
1911
    `TIME; $display("  TEST 8: WRITING TO PHY WITH WRONG PHY ADDRESS AND READING FROM CORRECT ONE");
1912 181 mohor
 
1913
    // set address
1914
    reg_addr = 5'h0; // control register
1915
    phy_addr = 5'h2; // wrong PHY address
1916
    // write request
1917
    phy_data = 16'h7DFF; // bit 15 (RESET bit) and bit 9 are self clearing bits
1918
    #Tp mii_write_req(phy_addr, reg_addr, phy_data);
1919
    check_mii_busy; // wait for write to finish
1920
 
1921
    phy_addr = 5'h1; // correct PHY address
1922
    // read request
1923
    #Tp mii_read_req(phy_addr, reg_addr);
1924
    check_mii_busy; // wait for read to finish
1925
    // read data
1926
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1927
    if (phy_data === tmp_data)
1928
    begin
1929
      test_fail("Data was written into PHY register with wrong PHY address - control register");
1930
      fail = fail + 1;
1931
    end
1932
    if(fail == 0)
1933
      test_ok;
1934
    else
1935
      fail = 0;
1936 169 mohor
  end
1937
 
1938
 
1939 181 mohor
  ////////////////////////////////////////////////////////////////////
1940
  ////                                                            ////
1941
  ////  Test sliding stop scan command immediately after read     ////
1942
  ////  request (with and without preamble)                       ////
1943
  ////                                                            ////
1944
  ////////////////////////////////////////////////////////////////////
1945
  if (test_num == 9) // 
1946 169 mohor
  begin
1947 194 tadej
    // TEST 9: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER READ REQUEST ( WITH AND WITHOUT PREAMBLE )
1948
    test_name = "TEST 9: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER READ REQUEST ( WITH AND WITHOUT PREAMBLE )";
1949 181 mohor
    `TIME;
1950 194 tadej
    $display("  TEST 9: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER READ REQUEST ( WITH AND WITHOUT PREAMBLE )");
1951 181 mohor
 
1952
    for (i2 = 0; i2 <= 1; i2 = i2 + 1) // choose preamble or not
1953 169 mohor
    begin
1954 181 mohor
      #Tp eth_phy.preamble_suppresed(i2);
1955
      // MII mode register
1956
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i2, 8'h0}), 4'hF, 1, wbm_init_waits,
1957
               wbm_subseq_waits);
1958
      i = 0;
1959
      cnt = 0;
1960
      while (i < 80) // delay for sliding of writing a STOP SCAN command
1961 169 mohor
      begin
1962 181 mohor
        for (i3 = 0; i3 <= 1; i3 = i3 + 1) // choose read or write after read will be finished
1963
        begin
1964
          // set address
1965
          reg_addr = 5'h0; // control register
1966
          phy_addr = 5'h1; // correct PHY address
1967
          cnt = 0;
1968
          // read request
1969
          #Tp mii_read_req(phy_addr, reg_addr);
1970
          fork
1971
            begin
1972
              repeat(i) @(posedge Mdc_O);
1973
              // write command 0x0 into MII command register
1974
              // MII command written while read in progress
1975
              wbm_write(`ETH_MIICOMMAND, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1976
              @(posedge wb_clk);
1977
              #Tp check_mii_busy; // wait for read to finish
1978
            end
1979
            begin
1980
              // wait for serial bus to become active
1981
              wait(Mdio_IO !== 1'bz);
1982
              // count transfer length
1983
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
1984
              begin
1985
                @(posedge Mdc_O);
1986
                #Tp cnt = cnt + 1;
1987
              end
1988
            end
1989
          join
1990
          // check transfer length
1991
          if (i2) // without preamble
1992 169 mohor
          begin
1993 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
1994
            begin
1995
              test_fail("Read request did not proceed correctly, while SCAN STOP command was written");
1996
              fail = fail + 1;
1997
            end
1998 169 mohor
          end
1999 181 mohor
          else // with preamble
2000 169 mohor
          begin
2001 181 mohor
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
2002
            begin
2003
              test_fail("Read request did not proceed correctly, while SCAN STOP command was written");
2004
              fail = fail + 1;
2005
            end
2006
          end
2007
          // check the BUSY signal to see if the bus is still IDLE
2008
          for (i1 = 0; i1 < 8; i1 = i1 + 1)
2009
            check_mii_busy; // wait for bus to become idle
2010
 
2011
          // try normal write or read after read was finished
2012
          #Tp phy_data = {8'h7D, (i[7:0] + 1)};
2013
          #Tp cnt = 0;
2014
          if (i3 == 0) // write after read
2015
          begin
2016
            // write request
2017
            #Tp mii_write_req(phy_addr, reg_addr, phy_data);
2018 169 mohor
            // wait for serial bus to become active
2019
            wait(Mdio_IO !== 1'bz);
2020
            // count transfer length
2021 181 mohor
            while(Mdio_IO !== 1'bz)
2022 169 mohor
            begin
2023
              @(posedge Mdc_O);
2024
              #Tp cnt = cnt + 1;
2025
            end
2026 181 mohor
            @(posedge Mdc_O);
2027
            // read request
2028
            #Tp mii_read_req(phy_addr, reg_addr);
2029
            check_mii_busy; // wait for read to finish
2030
            // read and check data
2031
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2032
            if (phy_data !== tmp_data)
2033
            begin
2034
              test_fail("Data was not correctly written into OR read from PHY register - control register");
2035
              fail = fail + 1;
2036
            end
2037 169 mohor
          end
2038 181 mohor
          else // read after read
2039 169 mohor
          begin
2040 181 mohor
            // read request
2041
            #Tp mii_read_req(phy_addr, reg_addr);
2042
            // wait for serial bus to become active
2043
            wait(Mdio_IO !== 1'bz);
2044
            // count transfer length
2045
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
2046
            begin
2047
              @(posedge Mdc_O);
2048
              #Tp cnt = cnt + 1;
2049
            end
2050 169 mohor
            @(posedge Mdc_O);
2051 181 mohor
            check_mii_busy; // wait for read to finish
2052
            // read and check data
2053
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2054
            if (phy_data !== tmp_data)
2055
            begin
2056
              test_fail("Data was not correctly written into OR read from PHY register - control register");
2057
              fail = fail + 1;
2058
            end
2059 169 mohor
          end
2060 181 mohor
          // check if transfer was a proper length
2061
          if (i2) // without preamble
2062 169 mohor
          begin
2063 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
2064
            begin
2065
              test_fail("New request did not proceed correctly, after read request");
2066
              fail = fail + 1;
2067
            end
2068 169 mohor
          end
2069 181 mohor
          else // with preamble
2070 169 mohor
          begin
2071 181 mohor
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
2072
            begin
2073
              test_fail("New request did not proceed correctly, after read request");
2074
              fail = fail + 1;
2075
            end
2076 169 mohor
          end
2077
        end
2078 181 mohor
        #Tp;
2079
        // set delay of writing the command
2080 169 mohor
        if (i2) // without preamble
2081
        begin
2082 181 mohor
          case(i)
2083
            0, 1:               i = i + 1;
2084
            18, 19, 20, 21, 22,
2085
            23, 24, 25, 26, 27,
2086
            28, 29, 30, 31, 32,
2087
            33, 34, 35:         i = i + 1;
2088
            36:                 i = 80;
2089
            default:            i = 18;
2090
          endcase
2091 169 mohor
        end
2092
        else // with preamble
2093
        begin
2094 181 mohor
          case(i)
2095
            0, 1:               i = i + 1;
2096
            50, 51, 52, 53, 54,
2097
            55, 56, 57, 58, 59,
2098
            60, 61, 62, 63, 64,
2099
            65, 66, 67:         i = i + 1;
2100
            68:                 i = 80;
2101
            default:            i = 50;
2102
          endcase
2103 169 mohor
        end
2104 181 mohor
        @(posedge wb_clk);
2105 169 mohor
      end
2106
    end
2107 181 mohor
    // set PHY to normal mode
2108
    #Tp eth_phy.preamble_suppresed(0);
2109
    // MII mode register
2110
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2111
    if(fail == 0)
2112
      test_ok;
2113
    else
2114
      fail = 0;
2115 169 mohor
  end
2116
 
2117
 
2118 181 mohor
  ////////////////////////////////////////////////////////////////////
2119
  ////                                                            ////
2120
  ////  Test sliding stop scan command immediately after write    ////
2121
  ////  request (with and without preamble)                       ////
2122
  ////                                                            ////
2123
  ////////////////////////////////////////////////////////////////////
2124
  if (test_num == 10) // 
2125 169 mohor
  begin
2126 194 tadej
    // TEST 10: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER WRITE REQUEST ( WITH AND WITHOUT PREAMBLE )
2127
    test_name = "TEST 10: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER WRITE REQUEST ( WITH AND WITHOUT PREAMBLE )";
2128 181 mohor
    `TIME;
2129 194 tadej
    $display("  TEST 10: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER WRITE REQUEST ( WITH AND WITHOUT PREAMBLE )");
2130 181 mohor
 
2131
    for (i2 = 0; i2 <= 1; i2 = i2 + 1) // choose preamble or not
2132 169 mohor
    begin
2133 181 mohor
      #Tp eth_phy.preamble_suppresed(i2);
2134
      // MII mode register
2135
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i2, 8'h0}), 4'hF, 1, wbm_init_waits,
2136
                wbm_subseq_waits);
2137
      i = 0;
2138
      cnt = 0;
2139
      while (i < 80) // delay for sliding of writing a STOP SCAN command
2140 169 mohor
      begin
2141 181 mohor
        for (i3 = 0; i3 <= 1; i3 = i3 + 1) // choose read or write after write will be finished
2142
        begin
2143
          // set address
2144
          reg_addr = 5'h0; // control register
2145
          phy_addr = 5'h1; // correct PHY address
2146
          cnt = 0;
2147
          // write request
2148
          phy_data = {8'h75, (i[7:0] + 1)};
2149
          #Tp mii_write_req(phy_addr, reg_addr, phy_data);
2150
          fork
2151
            begin
2152
              repeat(i) @(posedge Mdc_O);
2153
              // write command 0x0 into MII command register
2154
              // MII command written while read in progress
2155
              wbm_write(`ETH_MIICOMMAND, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2156
              @(posedge wb_clk);
2157
              #Tp check_mii_busy; // wait for write to finish
2158
            end
2159
            begin
2160
              // wait for serial bus to become active
2161
              wait(Mdio_IO !== 1'bz);
2162
              // count transfer length
2163
              while(Mdio_IO !== 1'bz)
2164
              begin
2165
                @(posedge Mdc_O);
2166
                #Tp cnt = cnt + 1;
2167
              end
2168
            end
2169
          join
2170
          // check transfer length
2171
          if (i2) // without preamble
2172 169 mohor
          begin
2173 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
2174
            begin
2175
              test_fail("Write request did not proceed correctly, while SCAN STOP command was written");
2176
              fail = fail + 1;
2177
            end
2178 169 mohor
          end
2179 181 mohor
          else // with preamble
2180 169 mohor
          begin
2181 181 mohor
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
2182
            begin
2183
              test_fail("Write request did not proceed correctly, while SCAN STOP command was written");
2184
              fail = fail + 1;
2185
            end
2186
          end
2187
          // check the BUSY signal to see if the bus is still IDLE
2188
          for (i1 = 0; i1 < 8; i1 = i1 + 1)
2189
            check_mii_busy; // wait for bus to become idle
2190
 
2191
          // try normal write or read after write was finished
2192
          #Tp cnt = 0;
2193
          if (i3 == 0) // write after write
2194
          begin
2195
            phy_data = {8'h7A, (i[7:0] + 1)};
2196
            // write request
2197
            #Tp mii_write_req(phy_addr, reg_addr, phy_data);
2198 169 mohor
            // wait for serial bus to become active
2199
            wait(Mdio_IO !== 1'bz);
2200
            // count transfer length
2201
            while(Mdio_IO !== 1'bz)
2202
            begin
2203
              @(posedge Mdc_O);
2204
              #Tp cnt = cnt + 1;
2205
            end
2206 181 mohor
            @(posedge Mdc_O);
2207
            // read request
2208
            #Tp mii_read_req(phy_addr, reg_addr);
2209
            check_mii_busy; // wait for read to finish
2210
            // read and check data
2211
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data , 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2212
            if (phy_data !== tmp_data)
2213
            begin
2214
              test_fail("Data was not correctly written into OR read from PHY register - control register");
2215
              fail = fail + 1;
2216
            end
2217 169 mohor
          end
2218 181 mohor
          else // read after write
2219 169 mohor
          begin
2220 181 mohor
            // read request
2221
            #Tp mii_read_req(phy_addr, reg_addr);
2222
            // wait for serial bus to become active
2223
            wait(Mdio_IO !== 1'bz);
2224
            // count transfer length
2225
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
2226
            begin
2227
              @(posedge Mdc_O);
2228
              #Tp cnt = cnt + 1;
2229
            end
2230 169 mohor
            @(posedge Mdc_O);
2231 181 mohor
            check_mii_busy; // wait for read to finish
2232
            // read and check data
2233
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data , 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2234
            if (phy_data !== tmp_data)
2235
            begin
2236
              test_fail("Data was not correctly written into OR read from PHY register - control register");
2237
              fail = fail + 1;
2238
            end
2239 169 mohor
          end
2240 181 mohor
          // check if transfer was a proper length
2241
          if (i2) // without preamble
2242 169 mohor
          begin
2243 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
2244
            begin
2245
              test_fail("New request did not proceed correctly, after write request");
2246
              fail = fail + 1;
2247
            end
2248 169 mohor
          end
2249 181 mohor
          else // with preamble
2250 169 mohor
          begin
2251 181 mohor
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
2252
            begin
2253
              test_fail("New request did not proceed correctly, after write request");
2254
              fail = fail + 1;
2255
            end
2256 169 mohor
          end
2257
        end
2258 181 mohor
        #Tp;
2259
        // set delay of writing the command
2260 169 mohor
        if (i2) // without preamble
2261
        begin
2262 181 mohor
          case(i)
2263
            0, 1:               i = i + 1;
2264
            18, 19, 20, 21, 22,
2265
            23, 24, 25, 26, 27,
2266
            28, 29, 30, 31, 32,
2267
            33, 34, 35:         i = i + 1;
2268
            36:                 i = 80;
2269
            default:            i = 18;
2270
          endcase
2271 169 mohor
        end
2272
        else // with preamble
2273
        begin
2274 181 mohor
          case(i)
2275
            0, 1:               i = i + 1;
2276
            50, 51, 52, 53, 54,
2277
            55, 56, 57, 58, 59,
2278
            60, 61, 62, 63, 64,
2279
            65, 66, 67:         i = i + 1;
2280
            68:                 i = 80;
2281
            default:            i = 50;
2282
          endcase
2283 169 mohor
        end
2284 181 mohor
        @(posedge wb_clk);
2285 169 mohor
      end
2286
    end
2287 181 mohor
    // set PHY to normal mode
2288
    #Tp eth_phy.preamble_suppresed(0);
2289
    // MII mode register
2290
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2291
    if(fail == 0)
2292
      test_ok;
2293
    else
2294
      fail = 0;
2295 169 mohor
  end
2296
 
2297
 
2298 181 mohor
  ////////////////////////////////////////////////////////////////////
2299
  ////                                                            ////
2300
  ////  Test busy and nvalid status durations during write (with  ////
2301
  ////  and without preamble)                                     ////
2302
  ////                                                            ////
2303
  ////////////////////////////////////////////////////////////////////
2304
  if (test_num == 11) // 
2305 169 mohor
  begin
2306 194 tadej
    // TEST 11: BUSY AND NVALID STATUS DURATIONS DURING WRITE ( WITH AND WITHOUT PREAMBLE )
2307
    test_name   = "TEST 11: BUSY AND NVALID STATUS DURATIONS DURING WRITE ( WITH AND WITHOUT PREAMBLE )";
2308
    `TIME; $display("  TEST 11: BUSY AND NVALID STATUS DURATIONS DURING WRITE ( WITH AND WITHOUT PREAMBLE )");
2309 181 mohor
 
2310
    reset_mii; // reset MII
2311
    // set link up, if it wasn't due to previous tests, since there weren't PHY registers
2312
    #Tp eth_phy.link_up_down(1);
2313
    // set the MII
2314
    clk_div = 64;
2315
    mii_set_clk_div(clk_div[7:0]);
2316
    // set address
2317
    reg_addr = 5'h1; // status register
2318
    phy_addr = 5'h1; // correct PHY address
2319
 
2320
    for (i = 0; i <= 1; i = i + 1)
2321 169 mohor
    begin
2322 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
2323
      // MII mode register
2324
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}) | (`ETH_MIIMODER_CLKDIV & clk_div),
2325
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2326
      @(posedge Mdc_O);
2327
      // write request
2328
      #Tp mii_write_req(phy_addr, reg_addr, 16'h5A5A);
2329
      // read data from MII status register - Busy and Nvalid bits
2330
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2331
 
2332
      // check MII IO signal and Busy and Nvalid bits
2333
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2334 169 mohor
      begin
2335 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is not HIGH Z - set higher clock divider");
2336
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2337
        begin
2338
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z anymore - 1. read");
2339
          fail = fail + 1;
2340
        end
2341
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2342
        begin
2343
          test_fail("Nvalid signal was set during write");
2344
          fail = fail + 1;
2345
        end
2346 169 mohor
      end
2347 181 mohor
      else // Busy bit should already be set to '1', due to reads from MII status register
2348 169 mohor
      begin
2349 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2350
        begin
2351
          test_fail("Busy signal should be set after write, due to reads from MII status register");
2352
          fail = fail + 1;
2353
        end
2354
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2355
        begin
2356
          test_fail("Nvalid signal was set during write");
2357
          fail = fail + 1;
2358
        end
2359 169 mohor
      end
2360 181 mohor
 
2361
      // wait for serial bus to become active
2362
      wait(Mdio_IO !== 1'bz);
2363
      // count transfer bits
2364
      if (i)
2365 169 mohor
      begin
2366 181 mohor
        repeat(32) @(posedge Mdc_O);
2367 169 mohor
      end
2368 181 mohor
      else
2369 169 mohor
      begin
2370 181 mohor
        repeat(64) @(posedge Mdc_O);
2371 169 mohor
      end
2372 181 mohor
      // read data from MII status register - Busy and Nvalid bits
2373
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2374
 
2375
      // check MII IO signal and Busy and Nvalid bits
2376
      if (Mdio_IO === 1'bz) // Mdio_IO should not be HIGH Z here - testbench selfcheck
2377 169 mohor
      begin
2378 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is HIGH Z - set higher clock divider");
2379
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2380
        begin
2381
          test_fail("Busy signal should be set while MII IO signal is not active anymore");
2382
          fail = fail + 1;
2383
        end
2384
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2385
        begin
2386
          test_fail("Nvalid signal was set during write");
2387
          fail = fail + 1;
2388
        end
2389 169 mohor
      end
2390 181 mohor
      else // Busy bit should still be set to '1'
2391 169 mohor
      begin
2392 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2393
        begin
2394
          test_fail("Busy signal should be set while MII IO signal not HIGH Z");
2395
          fail = fail + 1;
2396
        end
2397
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2398
        begin
2399
          test_fail("Nvalid signal was set during write");
2400
          fail = fail + 1;
2401
        end
2402 169 mohor
      end
2403 181 mohor
 
2404
      // wait for next negative clock edge
2405
      @(negedge Mdc_O);
2406 169 mohor
      // read data from MII status register - Busy and Nvalid bits
2407
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2408 181 mohor
 
2409 169 mohor
      // check MII IO signal and Busy and Nvalid bits
2410
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2411
      begin
2412
        test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2413
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2414
        begin
2415
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2416
          fail = fail + 1;
2417
        end
2418
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2419
        begin
2420
          test_fail("Nvalid signal was set during write");
2421
          fail = fail + 1;
2422
        end
2423
      end
2424 181 mohor
      else // Busy bit should still be set to '1'
2425 169 mohor
      begin
2426
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2427
        begin
2428 181 mohor
          test_fail("Busy signal should be set after MII IO signal become HIGH Z");
2429
          fail = fail + 1;
2430 169 mohor
        end
2431 181 mohor
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2432 169 mohor
        begin
2433 181 mohor
          test_fail("Nvalid signal was set during write");
2434
          fail = fail + 1;
2435
        end
2436
      end
2437
 
2438
      // wait for Busy to become inactive
2439
      i1 = 0;
2440
      while (i1 <= 2)
2441
      begin
2442
        // wait for next positive clock edge
2443
        @(posedge Mdc_O);
2444
        // read data from MII status register - Busy and Nvalid bits
2445
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2446
 
2447
        // check MII IO signal and Busy and Nvalid bits
2448
        if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2449
        begin
2450
          test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2451
          if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2452 169 mohor
          begin
2453 181 mohor
            test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2454 169 mohor
            fail = fail + 1;
2455
          end
2456 181 mohor
          if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2457
          begin
2458
            test_fail("Nvalid signal was set during write");
2459
            fail = fail + 1;
2460
          end
2461 169 mohor
        end
2462 181 mohor
        else // wait for Busy bit to be set to '0'
2463 169 mohor
        begin
2464 181 mohor
          if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2465
          begin
2466
            i1 = 3; // end of Busy checking
2467
          end
2468
          else
2469
          begin
2470
            if (i1 == 2)
2471
            begin
2472
              test_fail("Busy signal should be cleared after 2 periods after MII IO signal become HIGH Z");
2473
              fail = fail + 1;
2474
            end
2475
            #Tp i1 = i1 + 1;
2476
          end
2477
          if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2478
          begin
2479
            test_fail("Nvalid signal was set after write");
2480
            fail = fail + 1;
2481
          end
2482 169 mohor
        end
2483
      end
2484
    end
2485 181 mohor
    // set PHY to normal mode
2486
    #Tp eth_phy.preamble_suppresed(0);
2487
    // MII mode register
2488
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2489
    if(fail == 0)
2490
      test_ok;
2491
    else
2492
      fail = 0;
2493 116 mohor
  end
2494
 
2495
 
2496 181 mohor
  ////////////////////////////////////////////////////////////////////
2497
  ////                                                            ////
2498
  ////  Test busy and nvalid status durations during write (with  ////
2499
  ////  and without preamble)                                     ////
2500
  ////                                                            ////
2501
  ////////////////////////////////////////////////////////////////////
2502
  if (test_num == 12) // 
2503 169 mohor
  begin
2504 194 tadej
    // TEST 12: BUSY AND NVALID STATUS DURATIONS DURING READ ( WITH AND WITHOUT PREAMBLE )
2505
    test_name   = "TEST 12: BUSY AND NVALID STATUS DURATIONS DURING READ ( WITH AND WITHOUT PREAMBLE )";
2506
    `TIME; $display("  TEST 12: BUSY AND NVALID STATUS DURATIONS DURING READ ( WITH AND WITHOUT PREAMBLE )");
2507 181 mohor
 
2508
    reset_mii; // reset MII
2509
    // set link up, if it wasn't due to previous tests, since there weren't PHY registers
2510
    #Tp eth_phy.link_up_down(1);
2511
    // set the MII
2512
    clk_div = 64;
2513
    mii_set_clk_div(clk_div[7:0]);
2514
    // set address
2515
    reg_addr = 5'h1; // status register
2516
    phy_addr = 5'h1; // correct PHY address
2517
 
2518
    for (i = 0; i <= 1; i = i + 1)
2519 169 mohor
    begin
2520 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
2521
      // MII mode register
2522
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}) | (`ETH_MIIMODER_CLKDIV & clk_div),
2523
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2524 169 mohor
      @(posedge Mdc_O);
2525 181 mohor
      // read request
2526
      #Tp mii_read_req(phy_addr, reg_addr);
2527 169 mohor
      // read data from MII status register - Busy and Nvalid bits
2528
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2529 181 mohor
 
2530 169 mohor
      // check MII IO signal and Busy and Nvalid bits
2531
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2532
      begin
2533 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is not HIGH Z - set higher clock divider");
2534 169 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2535
        begin
2536 181 mohor
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z anymore - 1. read");
2537 169 mohor
          fail = fail + 1;
2538
        end
2539
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2540
        begin
2541
          test_fail("Nvalid signal was set during read");
2542
          fail = fail + 1;
2543
        end
2544
      end
2545 181 mohor
      else // Busy bit should already be set to '1', due to reads from MII status register
2546 169 mohor
      begin
2547
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2548
        begin
2549 181 mohor
          test_fail("Busy signal should be set after read, due to reads from MII status register");
2550
          fail = fail + 1;
2551 169 mohor
        end
2552
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2553
        begin
2554 181 mohor
          test_fail("Nvalid signal was set during read");
2555 169 mohor
          fail = fail + 1;
2556
        end
2557
      end
2558 181 mohor
 
2559
      // wait for serial bus to become active
2560
      wait(Mdio_IO !== 1'bz);
2561
      // count transfer bits
2562
      if (i)
2563 169 mohor
      begin
2564 181 mohor
        repeat(31) @(posedge Mdc_O);
2565 169 mohor
      end
2566 181 mohor
      else
2567 169 mohor
      begin
2568 181 mohor
        repeat(63) @(posedge Mdc_O);
2569 169 mohor
      end
2570 181 mohor
      // wait for next negative clock edge
2571
      @(negedge Mdc_O);
2572
      // read data from MII status register - Busy and Nvalid bits
2573
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2574
 
2575
      // check MII IO signal and Busy and Nvalid bits
2576
      if (Mdio_IO === 1'bz) // Mdio_IO should not be HIGH Z here - testbench selfcheck
2577 169 mohor
      begin
2578 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is HIGH Z - set higher clock divider");
2579
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2580
        begin
2581
          test_fail("Busy signal should be set while MII IO signal is not active anymore");
2582
          fail = fail + 1;
2583
        end
2584
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2585
        begin
2586
          test_fail("Nvalid signal was set during read");
2587
          fail = fail + 1;
2588
        end
2589 169 mohor
      end
2590 181 mohor
      else // Busy bit should still be set to '1'
2591 169 mohor
      begin
2592 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2593
        begin
2594
          test_fail("Busy signal should be set while MII IO signal not HIGH Z");
2595
          fail = fail + 1;
2596
        end
2597
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2598
        begin
2599
          test_fail("Nvalid signal was set during read");
2600
          fail = fail + 1;
2601
        end
2602 169 mohor
      end
2603 181 mohor
 
2604 169 mohor
      // wait for next positive clock edge
2605
      @(posedge Mdc_O);
2606
      // read data from MII status register - Busy and Nvalid bits
2607
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2608 181 mohor
 
2609 169 mohor
      // check MII IO signal and Busy and Nvalid bits
2610
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2611
      begin
2612
        test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2613 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2614 169 mohor
        begin
2615 181 mohor
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2616
          fail = fail + 1;
2617
        end
2618
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2619
        begin
2620
          test_fail("Nvalid signal was set during read");
2621
          fail = fail + 1;
2622
        end
2623
      end
2624
      else // Busy bit should still be set to '1'
2625
      begin
2626
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2627
        begin
2628
          test_fail("Busy signal should be set after MII IO signal become HIGH Z");
2629
          fail = fail + 1;
2630
        end
2631
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2632
        begin
2633
          test_fail("Nvalid signal was set during read");
2634
          fail = fail + 1;
2635
        end
2636
      end
2637
 
2638
      // wait for Busy to become inactive
2639
      i1 = 0;
2640
      while (i1 <= 2)
2641
      begin
2642
        // wait for next positive clock edge
2643
        @(posedge Mdc_O);
2644
        // read data from MII status register - Busy and Nvalid bits
2645
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2646
 
2647
        // check MII IO signal and Busy and Nvalid bits
2648
        if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2649
        begin
2650
          test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2651 169 mohor
          if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2652
          begin
2653
            test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2654
            fail = fail + 1;
2655
          end
2656 181 mohor
          if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2657 169 mohor
          begin
2658 181 mohor
            test_fail("Nvalid signal was set during read");
2659 169 mohor
            fail = fail + 1;
2660
          end
2661
        end
2662 181 mohor
        else // wait for Busy bit to be set to '0'
2663 169 mohor
        begin
2664
          if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2665
          begin
2666
            i1 = 3; // end of Busy checking
2667
          end
2668
          else
2669
          begin
2670
            if (i1 == 2)
2671
            begin
2672
              test_fail("Busy signal should be cleared after 2 periods after MII IO signal become HIGH Z");
2673
              fail = fail + 1;
2674
            end
2675
            #Tp i1 = i1 + 1;
2676
          end
2677 181 mohor
          if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2678 169 mohor
          begin
2679 181 mohor
            test_fail("Nvalid signal was set after read");
2680 169 mohor
            fail = fail + 1;
2681
          end
2682
        end
2683
      end
2684
    end
2685 181 mohor
    // set PHY to normal mode
2686
    #Tp eth_phy.preamble_suppresed(0);
2687
    // MII mode register
2688
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2689
    if(fail == 0)
2690
      test_ok;
2691
    else
2692
      fail = 0;
2693 169 mohor
  end
2694
 
2695
 
2696 181 mohor
  ////////////////////////////////////////////////////////////////////
2697
  ////                                                            ////
2698
  ////  Test busy and nvalid status durations during scan (with   ////
2699
  ////  and without preamble)                                     ////
2700
  ////                                                            ////
2701
  ////////////////////////////////////////////////////////////////////
2702
  if (test_num == 13) // 
2703 169 mohor
  begin
2704 194 tadej
    // TEST 13: BUSY AND NVALID STATUS DURATIONS DURING SCAN ( WITH AND WITHOUT PREAMBLE )
2705
    test_name   = "TEST 13: BUSY AND NVALID STATUS DURATIONS DURING SCAN ( WITH AND WITHOUT PREAMBLE )";
2706
    `TIME; $display("  TEST 13: BUSY AND NVALID STATUS DURATIONS DURING SCAN ( WITH AND WITHOUT PREAMBLE )");
2707 181 mohor
 
2708
    reset_mii; // reset MII
2709
    // set link up, if it wasn't due to previous tests, since there weren't PHY registers
2710
    #Tp eth_phy.link_up_down(1);
2711
    // set the MII
2712
    clk_div = 64;
2713
    mii_set_clk_div(clk_div[7:0]);
2714
    // set address
2715
    reg_addr = 5'h1; // status register
2716
    phy_addr = 5'h1; // correct PHY address
2717
 
2718
    for (i = 0; i <= 1; i = i + 1)
2719 169 mohor
    begin
2720 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
2721
      // MII mode register
2722
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}) | (`ETH_MIIMODER_CLKDIV & clk_div),
2723
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2724
      @(posedge Mdc_O);
2725
      // scan request
2726
      #Tp mii_scan_req(phy_addr, reg_addr);
2727
      // read data from MII status register - Busy and Nvalid bits
2728
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2729
 
2730
      // check MII IO signal and Busy and Nvalid bits
2731
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2732 169 mohor
      begin
2733 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is not HIGH Z - set higher clock divider");
2734
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2735
        begin
2736
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z anymore - 1. read");
2737
          fail = fail + 1;
2738
        end
2739
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2740
        begin
2741
          test_fail("Nvalid signal was not set while MII IO signal is not HIGH Z anymore - 1. read");
2742
          fail = fail + 1;
2743
        end
2744 169 mohor
      end
2745 181 mohor
      else // Busy bit should already be set to '1', due to reads from MII status register
2746 169 mohor
      begin
2747 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2748
        begin
2749
          test_fail("Busy signal should be set after scan, due to reads from MII status register");
2750
          fail = fail + 1;
2751
        end
2752
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2753
        begin
2754
          test_fail("Nvalid signal should be set after scan, due to reads from MII status register");
2755
          fail = fail + 1;
2756
        end
2757 169 mohor
      end
2758 181 mohor
 
2759
      // wait for serial bus to become active
2760 169 mohor
      wait(Mdio_IO !== 1'bz);
2761 181 mohor
      // count transfer bits
2762
      if (i)
2763 169 mohor
      begin
2764 181 mohor
        repeat(21) @(posedge Mdc_O);
2765 169 mohor
      end
2766 181 mohor
      else
2767 169 mohor
      begin
2768 181 mohor
        repeat(53) @(posedge Mdc_O);
2769 169 mohor
      end
2770 181 mohor
      // stop scan
2771
      #Tp mii_scan_finish; // finish scan operation
2772
 
2773
      // wait for next positive clock edge
2774
      repeat(10) @(posedge Mdc_O);
2775
      // read data from MII status register - Busy and Nvalid bits
2776
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2777
 
2778
      // check MII IO signal and Busy and Nvalid bits
2779
      if (Mdio_IO === 1'bz) // Mdio_IO should not be HIGH Z here - testbench selfcheck
2780 169 mohor
      begin
2781 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is HIGH Z - set higher clock divider");
2782
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2783 169 mohor
        begin
2784 181 mohor
          test_fail("Busy signal should be set while MII IO signal is not active anymore");
2785 169 mohor
          fail = fail + 1;
2786
        end
2787 181 mohor
        // Nvalid signal can be cleared here - it is still Testbench error
2788 169 mohor
      end
2789 181 mohor
      else // Busy bit should still be set to '1', Nvalid bit should still be set to '1'
2790 169 mohor
      begin
2791 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2792 169 mohor
        begin
2793 181 mohor
          test_fail("Busy signal should be set while MII IO signal not HIGH Z");
2794 169 mohor
          fail = fail + 1;
2795
        end
2796 181 mohor
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2797 169 mohor
        begin
2798 181 mohor
          test_fail("Nvalid signal should be set while MII IO signal not HIGH Z");
2799 169 mohor
          fail = fail + 1;
2800
        end
2801 181 mohor
      end
2802
 
2803
      // wait for next negative clock edge
2804
      @(negedge Mdc_O);
2805
      // read data from MII status register - Busy and Nvalid bits
2806
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2807
 
2808
      // check MII IO signal and Busy and Nvalid bits
2809
      if (Mdio_IO === 1'bz) // Mdio_IO should not be HIGH Z here - testbench selfcheck
2810
      begin
2811
        test_fail("Testbench error - read was to late, Mdio_IO is HIGH Z - set higher clock divider");
2812
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2813 169 mohor
        begin
2814 181 mohor
          test_fail("Busy signal should be set while MII IO signal is not active anymore");
2815
          fail = fail + 1;
2816 169 mohor
        end
2817 181 mohor
        // Nvalid signal can be cleared here - it is still Testbench error
2818 169 mohor
      end
2819 181 mohor
      else // Busy bit should still be set to '1', Nvalid bit should still be set to '1'
2820 169 mohor
      begin
2821 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2822 169 mohor
        begin
2823 181 mohor
          test_fail("Busy signal should be set while MII IO signal not HIGH Z");
2824
          fail = fail + 1;
2825 169 mohor
        end
2826 181 mohor
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2827 169 mohor
        begin
2828 181 mohor
          test_fail("Nvalid signal should be set while MII IO signal not HIGH Z");
2829
          fail = fail + 1;
2830 169 mohor
        end
2831
      end
2832 181 mohor
 
2833
      // wait for next negative clock edge
2834
      @(posedge Mdc_O);
2835
      // read data from MII status register - Busy and Nvalid bits
2836 169 mohor
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2837 181 mohor
 
2838
      // check MII IO signal and Busy and Nvalid bits
2839
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2840 169 mohor
      begin
2841 181 mohor
        test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2842
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2843 169 mohor
        begin
2844 181 mohor
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2845 169 mohor
          fail = fail + 1;
2846
        end
2847 181 mohor
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2848 169 mohor
        begin
2849 181 mohor
          test_fail("Nvalid signal was not set while MII IO signal is not HIGH Z");
2850 169 mohor
          fail = fail + 1;
2851
        end
2852
      end
2853 181 mohor
      else // Busy bit should still be set to '1', Nvalid bit can be set to '0'
2854 169 mohor
      begin
2855 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2856 169 mohor
        begin
2857 181 mohor
          test_fail("Busy signal should be set after MII IO signal become HIGH Z");
2858
          fail = fail + 1;
2859 169 mohor
        end
2860 181 mohor
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2861 169 mohor
        begin
2862 181 mohor
          i2 = 1; // check finished
2863 169 mohor
        end
2864 181 mohor
        else
2865 169 mohor
        begin
2866 181 mohor
          i2 = 0; // check must continue
2867 169 mohor
        end
2868
      end
2869 181 mohor
 
2870
      // wait for Busy to become inactive
2871
      i1 = 0;
2872
      while ((i1 <= 2) || (i2 == 0))
2873
      begin
2874
        // wait for next positive clock edge
2875
        @(posedge Mdc_O);
2876
        // read data from MII status register - Busy and Nvalid bits
2877
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2878
 
2879
        // check MII IO signal and Busy and Nvalid bits
2880
        if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2881 169 mohor
        begin
2882 181 mohor
          test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2883
          if (i1 <= 2)
2884 169 mohor
          begin
2885 181 mohor
            if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2886 169 mohor
            begin
2887 181 mohor
              test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2888 169 mohor
              fail = fail + 1;
2889
            end
2890
          end
2891 181 mohor
          if (i2 == 0)
2892 169 mohor
          begin
2893 181 mohor
            if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2894 169 mohor
            begin
2895 181 mohor
              test_fail("Nvalid signal was not set while MII IO signal is not HIGH Z");
2896 169 mohor
              fail = fail + 1;
2897
            end
2898
          end
2899
        end
2900 181 mohor
        else // wait for Busy bit to be set to '0'
2901 169 mohor
        begin
2902 181 mohor
          if (i1 <= 2)
2903 169 mohor
          begin
2904 181 mohor
            if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2905 169 mohor
            begin
2906 181 mohor
              i1 = 3; // end of Busy checking
2907 169 mohor
            end
2908 181 mohor
            else
2909 169 mohor
            begin
2910 181 mohor
              if (i1 == 2)
2911 169 mohor
              begin
2912 181 mohor
                test_fail("Busy signal should be cleared after 2 periods after MII IO signal become HIGH Z");
2913 169 mohor
                fail = fail + 1;
2914
              end
2915 181 mohor
              #Tp i1 = i1 + 1;
2916 169 mohor
            end
2917 181 mohor
          end
2918
          if (i2 == 0)
2919
          begin
2920
            if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2921 169 mohor
            begin
2922 181 mohor
              i2 = 1;
2923 169 mohor
            end
2924 181 mohor
            else
2925
            begin
2926
              test_fail("Nvalid signal should be cleared after MII IO signal become HIGH Z");
2927
              fail = fail + 1;
2928
            end
2929 169 mohor
          end
2930
        end
2931 181 mohor
      end
2932
    end
2933
    // set PHY to normal mode
2934
    #Tp eth_phy.preamble_suppresed(0);
2935
    // MII mode register
2936
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2937
    if(fail == 0)
2938
      test_ok;
2939
    else
2940
      fail = 0;
2941
  end
2942
 
2943
 
2944
  ////////////////////////////////////////////////////////////////////
2945
  ////                                                            ////
2946
  ////  Test scan status from phy with detecting link-fail bit    ////
2947
  ////  (with and without preamble)                               ////
2948
  ////                                                            ////
2949
  ////////////////////////////////////////////////////////////////////
2950
  if (test_num == 14) // 
2951
  begin
2952 194 tadej
    // TEST 14: SCAN STATUS FROM PHY WITH DETECTING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )
2953
    test_name   = "TEST 14: SCAN STATUS FROM PHY WITH DETECTING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )";
2954
    `TIME; $display("  TEST 14: SCAN STATUS FROM PHY WITH DETECTING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )");
2955 181 mohor
 
2956
    reset_mii; // reset MII
2957
    // set link up, if it wasn't due to previous tests, since there weren't PHY registers
2958
    #Tp eth_phy.link_up_down(1);
2959
    // set MII speed
2960
    clk_div = 6;
2961
    mii_set_clk_div(clk_div[7:0]);
2962
    // set address
2963
    reg_addr = 5'h1; // status register
2964
    phy_addr = 5'h1; // correct PHY address
2965
 
2966
    // read request
2967
    #Tp mii_read_req(phy_addr, reg_addr);
2968
    check_mii_busy; // wait for read to finish
2969
    // read data from PHY status register - remember LINK-UP status
2970
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2971
 
2972
    for (i = 0; i <= 1; i = i + 1)
2973
    begin
2974
      #Tp eth_phy.preamble_suppresed(i);
2975
      // MII mode register
2976
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}) | (`ETH_MIIMODER_CLKDIV & clk_div),
2977
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2978
      if (i)
2979
      begin
2980
        // change saved data when preamble is suppressed
2981
        #Tp tmp_data = tmp_data | 16'h0040; // put bit 6 to ONE
2982
      end
2983
 
2984
      // scan request
2985
      #Tp mii_scan_req(phy_addr, reg_addr);
2986
      check_mii_scan_valid; // wait for scan to make first data valid
2987
 
2988 169 mohor
      fork
2989 181 mohor
      begin
2990 169 mohor
        repeat(2) @(posedge Mdc_O);
2991
        // read data from PHY status register
2992
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2993 181 mohor
        if (phy_data !== tmp_data)
2994 169 mohor
        begin
2995 181 mohor
          test_fail("Data was not correctly scaned from status register");
2996 169 mohor
          fail = fail + 1;
2997
        end
2998
        // read data from MII status register
2999
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3000 181 mohor
        if (phy_data[0] !== 1'b0)
3001 169 mohor
        begin
3002 181 mohor
          test_fail("Link FAIL bit was set in the MII status register");
3003 169 mohor
          fail = fail + 1;
3004
        end
3005
      end
3006
      begin
3007 181 mohor
      // Completely check second scan
3008 169 mohor
        #Tp cnt = 0;
3009
        // wait for serial bus to become active - second scan
3010
        wait(Mdio_IO !== 1'bz);
3011
        // count transfer length
3012 181 mohor
        while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i == 0)) || ((cnt == 15) && (i == 1)) )
3013 169 mohor
        begin
3014
          @(posedge Mdc_O);
3015
          #Tp cnt = cnt + 1;
3016
        end
3017
        // check transfer length
3018 181 mohor
        if (i) // without preamble
3019 169 mohor
        begin
3020
          if (cnt != 33) // at this value Mdio_IO is HIGH Z
3021
          begin
3022 181 mohor
            test_fail("Second scan request did not proceed correctly");
3023 169 mohor
            fail = fail + 1;
3024
          end
3025
        end
3026
        else // with preamble
3027
        begin
3028
          if (cnt != 65) // at this value Mdio_IO is HIGH Z
3029
          begin
3030 181 mohor
            test_fail("Second scan request did not proceed correctly");
3031 169 mohor
            fail = fail + 1;
3032
          end
3033
        end
3034
      end
3035
      join
3036 181 mohor
      // check third to fifth scans
3037
      for (i3 = 0; i3 <= 2; i3 = i3 + 1)
3038
      begin
3039
        fork
3040 169 mohor
        begin
3041
          repeat(2) @(posedge Mdc_O);
3042
          // read data from PHY status register
3043
          #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3044 181 mohor
          if (phy_data !== tmp_data)
3045 169 mohor
          begin
3046 181 mohor
            test_fail("Data was not correctly scaned from status register");
3047
            fail = fail + 1;
3048 169 mohor
          end
3049
          // read data from MII status register
3050
          #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3051 181 mohor
          if (phy_data[0] !== 1'b0)
3052 169 mohor
          begin
3053 181 mohor
            test_fail("Link FAIL bit was set in the MII status register");
3054
            fail = fail + 1;
3055 169 mohor
          end
3056 181 mohor
          if (i3 == 2) // after fourth scan read
3057 169 mohor
          begin
3058 181 mohor
            @(posedge Mdc_O);
3059
            // change saved data
3060
            #Tp tmp_data = tmp_data & 16'hFFFB; // put bit 3 to ZERO
3061
            // set link down
3062
            #Tp eth_phy.link_up_down(0);
3063 169 mohor
          end
3064
        end
3065
        begin
3066 181 mohor
        // Completely check scans
3067
          #Tp cnt = 0;
3068
          // wait for serial bus to become active - second scan
3069
          wait(Mdio_IO !== 1'bz);
3070
          // count transfer length
3071
          while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i == 0)) || ((cnt == 15) && (i == 1)) )
3072 169 mohor
          begin
3073 181 mohor
            @(posedge Mdc_O);
3074
            #Tp cnt = cnt + 1;
3075
          end
3076
          // check transfer length
3077
          if (i) // without preamble
3078
          begin
3079
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
3080 169 mohor
            begin
3081 181 mohor
              test_fail("Fifth scan request did not proceed correctly");
3082
              fail = fail + 1;
3083 169 mohor
            end
3084 181 mohor
          end
3085
          else // with preamble
3086
          begin
3087
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
3088 169 mohor
            begin
3089 181 mohor
              test_fail("Fifth scan request did not proceed correctly");
3090
              fail = fail + 1;
3091 169 mohor
            end
3092
          end
3093
        end
3094 181 mohor
        join
3095
      end
3096
 
3097
      fork
3098
      begin
3099
        repeat(2) @(posedge Mdc_O);
3100
        // read data from PHY status register
3101
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3102
        if (phy_data !== tmp_data)
3103
        begin
3104
          test_fail("Data was not correctly scaned from status register");
3105
          fail = fail + 1;
3106
        end
3107
        // read data from MII status register
3108
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3109
        if (phy_data[0] === 1'b0)
3110
        begin
3111
          test_fail("Link FAIL bit was not set in the MII status register");
3112
          fail = fail + 1;
3113
        end
3114
        // wait to see if data stayed latched
3115
        repeat(4) @(posedge Mdc_O);
3116
        // read data from PHY status register
3117
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3118
        if (phy_data !== tmp_data)
3119
        begin
3120
          test_fail("Data was not latched correctly in status register");
3121
          fail = fail + 1;
3122
        end
3123
        // read data from MII status register
3124
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3125
        if (phy_data[0] === 1'b0)
3126
        begin
3127
          test_fail("Link FAIL bit was not set in the MII status register");
3128
          fail = fail + 1;
3129
        end
3130
        // change saved data
3131
        #Tp tmp_data = tmp_data | 16'h0004; // put bit 2 to ONE
3132
        // set link up
3133
        #Tp eth_phy.link_up_down(1);
3134
      end
3135
      begin
3136
      // Wait for sixth scan
3137
        // wait for serial bus to become active - sixth scan
3138
        wait(Mdio_IO !== 1'bz);
3139
        // wait for serial bus to become inactive - turn-around cycle in sixth scan
3140
        wait(Mdio_IO === 1'bz);
3141
        // wait for serial bus to become active - end of turn-around cycle in sixth scan
3142
        wait(Mdio_IO !== 1'bz);
3143
        // wait for serial bus to become inactive - end of sixth scan
3144
        wait(Mdio_IO === 1'bz);
3145
      end
3146 169 mohor
      join
3147 181 mohor
 
3148
      @(posedge Mdc_O);
3149 169 mohor
      // read data from PHY status register
3150
      #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3151
      if (phy_data !== tmp_data)
3152
      begin
3153 181 mohor
        test_fail("Data was not correctly scaned from status register");
3154 169 mohor
        fail = fail + 1;
3155
      end
3156
      // read data from MII status register
3157
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3158
      if (phy_data[0] !== 1'b0)
3159
      begin
3160
        test_fail("Link FAIL bit was set in the MII status register");
3161
        fail = fail + 1;
3162
      end
3163 181 mohor
      // wait to see if data stayed latched
3164
      repeat(4) @(posedge Mdc_O);
3165
      // read data from PHY status register
3166
      #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3167
      if (phy_data !== tmp_data)
3168 169 mohor
      begin
3169 181 mohor
        test_fail("Data was not correctly scaned from status register");
3170
        fail = fail + 1;
3171 169 mohor
      end
3172 181 mohor
      // read data from MII status register
3173
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3174
      if (phy_data[0] !== 1'b0)
3175 169 mohor
      begin
3176 181 mohor
        test_fail("Link FAIL bit was set in the MII status register");
3177
        fail = fail + 1;
3178 169 mohor
      end
3179 181 mohor
 
3180
      // STOP SCAN
3181
      #Tp mii_scan_finish; // finish scan operation
3182
      #Tp check_mii_busy; // wait for scan to finish
3183 169 mohor
    end
3184 181 mohor
    // set PHY to normal mode
3185
    #Tp eth_phy.preamble_suppresed(0);
3186
    // MII mode register
3187
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3188
    if(fail == 0)
3189
      test_ok;
3190
    else
3191
      fail = 0;
3192 169 mohor
  end
3193
 
3194
 
3195 181 mohor
  ////////////////////////////////////////////////////////////////////
3196
  ////                                                            ////
3197
  ////  Test scan status from phy with sliding link-fail bit      ////
3198
  ////  (with and without preamble)                               ////
3199
  ////                                                            ////
3200
  ////////////////////////////////////////////////////////////////////
3201
  if (test_num == 15) // 
3202 169 mohor
  begin
3203 194 tadej
    // TEST 15: SCAN STATUS FROM PHY WITH SLIDING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )
3204
    test_name   = "TEST 15: SCAN STATUS FROM PHY WITH SLIDING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )";
3205
    `TIME; $display("  TEST 15: SCAN STATUS FROM PHY WITH SLIDING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )");
3206 181 mohor
 
3207
    // set address
3208
    reg_addr = 5'h1; // status register
3209
    phy_addr = 5'h1; // correct PHY address
3210
 
3211
    // read request
3212
    #Tp mii_read_req(phy_addr, reg_addr);
3213
    check_mii_busy; // wait for read to finish
3214
    // read data from PHY status register - remember LINK-UP status
3215
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3216
 
3217
    for (i2 = 0; i2 <= 1; i2 = i2 + 1) // choose preamble or not
3218 169 mohor
    begin
3219 181 mohor
      #Tp eth_phy.preamble_suppresed(i2);
3220
      // MII mode register
3221
      #Tp wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i2, 8'h0}), 4'hF, 1, wbm_init_waits,
3222
                    wbm_subseq_waits);
3223
      if (i2)
3224 169 mohor
      begin
3225 181 mohor
        // change saved data when preamble is suppressed
3226
        #Tp tmp_data = tmp_data | 16'h0040; // put bit 6 to ONE
3227
      end
3228
 
3229
      i = 0;
3230
      while (i < 80) // delay for sliding of LinkFail bit
3231
      begin
3232
        // first there are two scans
3233
        #Tp cnt = 0;
3234 169 mohor
        // scan request
3235
        #Tp mii_scan_req(phy_addr, reg_addr);
3236 181 mohor
        #Tp check_mii_scan_valid; // wait for scan to make first data valid
3237
 
3238
        // check second scan
3239 169 mohor
        fork
3240 181 mohor
        begin
3241
          repeat(4) @(posedge Mdc_O);
3242
          // read data from PHY status register
3243
          #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3244
          if (phy_data !== tmp_data)
3245 169 mohor
          begin
3246 181 mohor
            test_fail("Second data was not correctly scaned from status register");
3247
            fail = fail + 1;
3248 169 mohor
          end
3249 181 mohor
          // read data from MII status register
3250
          #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3251
          if (phy_data[0] !== 1'b0)
3252
          begin
3253
            test_fail("Link FAIL bit was set in the MII status register");
3254
            fail = fail + 1;
3255
          end
3256
        end
3257
        begin
3258
        // Completely check scan
3259
          #Tp cnt = 0;
3260
          // wait for serial bus to become active - second scan
3261
          wait(Mdio_IO !== 1'bz);
3262
          // count transfer length
3263
          while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3264
          begin
3265
            @(posedge Mdc_O);
3266
            #Tp cnt = cnt + 1;
3267
          end
3268
          // check transfer length
3269
          if (i2) // without preamble
3270
          begin
3271
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
3272 169 mohor
            begin
3273 181 mohor
              test_fail("Second scan request did not proceed correctly");
3274
              fail = fail + 1;
3275 169 mohor
            end
3276 181 mohor
          end
3277
          else // with preamble
3278
          begin
3279
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
3280
            begin
3281
              test_fail("Second scan request did not proceed correctly");
3282
              fail = fail + 1;
3283
            end
3284
          end
3285
        end
3286
        join
3287
        // reset counter 
3288
        #Tp cnt = 0;
3289
        // SLIDING LINK DOWN and CHECK
3290
        fork
3291
          begin
3292
          // set link down
3293
            repeat(i) @(posedge Mdc_O);
3294
            // set link down
3295
            #Tp eth_phy.link_up_down(0);
3296
          end
3297
          begin
3298
          // check data in MII registers after each scan in this fork statement
3299 169 mohor
            if (i2) // without preamble
3300 181 mohor
              wait (cnt == 32);
3301
            else // with preamble
3302
              wait (cnt == 64);
3303
            repeat(3) @(posedge Mdc_O);
3304
            // read data from PHY status register
3305
            #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3306
            if ( ((i < 49) && !i2) || ((i < 17) && i2) )
3307 169 mohor
            begin
3308 181 mohor
              if (phy_data !== (tmp_data & 16'hFFFB)) // bit 3 is ZERO
3309 169 mohor
              begin
3310 181 mohor
                test_fail("Third data was not correctly scaned from status register");
3311 169 mohor
                fail = fail + 1;
3312
              end
3313
            end
3314 181 mohor
            else
3315 169 mohor
            begin
3316 181 mohor
              if (phy_data !== tmp_data)
3317 169 mohor
              begin
3318 181 mohor
                test_fail("Third data was not correctly scaned from status register");
3319 169 mohor
                fail = fail + 1;
3320
              end
3321
            end
3322 181 mohor
            // read data from MII status register
3323
            #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3324
            if ( ((i < 49) && !i2) || ((i < 17) && i2) )
3325 169 mohor
            begin
3326 181 mohor
              if (phy_data[0] === 1'b0)
3327
              begin
3328
                test_fail("Link FAIL bit was not set in the MII status register");
3329
                fail = fail + 1;
3330
              end
3331 169 mohor
            end
3332 181 mohor
            else
3333 169 mohor
            begin
3334 181 mohor
              if (phy_data[0] !== 1'b0)
3335 169 mohor
              begin
3336 181 mohor
                test_fail("Link FAIL bit was set in the MII status register");
3337 169 mohor
                fail = fail + 1;
3338
              end
3339
            end
3340 181 mohor
          end
3341
          begin
3342
          // check length
3343
            for (i3 = 0; i3 <= 1; i3 = i3 + 1) // two scans
3344 169 mohor
            begin
3345 181 mohor
              #Tp cnt = 0;
3346
              // wait for serial bus to become active if there is more than one scan
3347
              wait(Mdio_IO !== 1'bz);
3348
              // count transfer length
3349
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3350 169 mohor
              begin
3351 181 mohor
                @(posedge Mdc_O);
3352
                #Tp cnt = cnt + 1;
3353 169 mohor
              end
3354 181 mohor
              // check transfer length
3355
              if (i2) // without preamble
3356
              begin
3357
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3358
                begin
3359
                  test_fail("3. or 4. scan request did not proceed correctly, while SCAN STOP was written");
3360
                  fail = fail + 1;
3361
                end
3362
              end
3363
              else // with preamble
3364
              begin
3365
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3366
                begin
3367
                  test_fail("3. or 4. scan request did not proceed correctly, while SCAN STOP was written");
3368
                  fail = fail + 1;
3369
                end
3370
              end
3371 169 mohor
            end
3372
          end
3373
        join
3374 181 mohor
        // reset counter
3375
        #Tp cnt = 0;
3376
        // check fifth scan and data from fourth scan
3377
        fork
3378 169 mohor
        begin
3379 181 mohor
          repeat(2) @(posedge Mdc_O);
3380
          // read data from PHY status register
3381
          #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3382
          if (phy_data !== (tmp_data & 16'hFFFB)) // bit 3 is ZERO
3383 169 mohor
          begin
3384 181 mohor
            test_fail("4. data was not correctly scaned from status register");
3385
            fail = fail + 1;
3386 169 mohor
          end
3387 181 mohor
          // read data from MII status register
3388
          #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3389
          if (phy_data[0] === 1'b0)
3390 169 mohor
          begin
3391 181 mohor
            test_fail("Link FAIL bit was not set in the MII status register");
3392 169 mohor
            fail = fail + 1;
3393
          end
3394
        end
3395
        begin
3396 181 mohor
        // Completely check intermediate scan
3397
          #Tp cnt = 0;
3398
          // wait for serial bus to become active - second scan
3399 169 mohor
          wait(Mdio_IO !== 1'bz);
3400
          // count transfer length
3401
          while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3402
          begin
3403
            @(posedge Mdc_O);
3404
            #Tp cnt = cnt + 1;
3405
          end
3406 181 mohor
          // check transfer length
3407
          if (i2) // without preamble
3408 169 mohor
          begin
3409 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
3410
            begin
3411
              test_fail("Fifth scan request did not proceed correctly");
3412
              fail = fail + 1;
3413
            end
3414 169 mohor
          end
3415 181 mohor
          else // with preamble
3416
          begin
3417
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
3418
            begin
3419
              test_fail("Fifth scan request did not proceed correctly");
3420
              fail = fail + 1;
3421
            end
3422
          end
3423 169 mohor
        end
3424 181 mohor
        join
3425
        // reset counter 
3426
        #Tp cnt = 0;
3427
        // SLIDING LINK UP and CHECK
3428
        fork
3429 169 mohor
          begin
3430 181 mohor
          // set link up
3431
            repeat(i) @(posedge Mdc_O);
3432
            // set link up
3433
            #Tp eth_phy.link_up_down(1);
3434 169 mohor
          end
3435 181 mohor
          begin
3436
          // check data in MII registers after each scan in this fork statement
3437
            repeat(2) @(posedge Mdc_O);
3438
            if (i2) // without preamble
3439
              wait (cnt == 32);
3440
            else // with preamble
3441
              wait (cnt == 64);
3442
            repeat(3) @(posedge Mdc_O);
3443
            // read data from PHY status register
3444
            #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3445
            if ( ((i < 49) && !i2) || ((i < 17) && i2) )
3446
            begin
3447
              if (phy_data !== tmp_data)
3448
              begin
3449
                test_fail("6. data was not correctly scaned from status register");
3450
                fail = fail + 1;
3451
              end
3452
            end
3453
            else
3454
            begin
3455
              if (phy_data !== (tmp_data & 16'hFFFB)) // bit 3 is ZERO
3456
              begin
3457
                test_fail("6. data was not correctly scaned from status register");
3458
                fail = fail + 1;
3459
              end
3460
            end
3461
            // read data from MII status register
3462
            #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3463
            if ( ((i < 49) && !i2) || ((i < 17) && i2) )
3464
            begin
3465
              if (phy_data[0] !== 1'b0)
3466
              begin
3467
                test_fail("Link FAIL bit was set in the MII status register");
3468
                fail = fail + 1;
3469
              end
3470
            end
3471
            else
3472
            begin
3473
              if (phy_data[0] === 1'b0)
3474
              begin
3475
                test_fail("Link FAIL bit was not set in the MII status register");
3476
                fail = fail + 1;
3477
              end
3478
            end
3479
          end
3480
          begin
3481
          // check length
3482
            for (i3 = 0; i3 <= 1; i3 = i3 + 1) // two scans
3483
            begin
3484
              #Tp cnt = 0;
3485
              // wait for serial bus to become active if there is more than one scan
3486
              wait(Mdio_IO !== 1'bz);
3487
              // count transfer length
3488
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3489
              begin
3490
                @(posedge Mdc_O);
3491
                #Tp cnt = cnt + 1;
3492
              end
3493
              // check transfer length
3494
              if (i2) // without preamble
3495
              begin
3496
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3497
                begin
3498
                  test_fail("Scan request did not proceed correctly, while SCAN STOP was written");
3499
                  fail = fail + 1;
3500
                end
3501
              end
3502
              else // with preamble
3503
              begin
3504
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3505
                begin
3506
                  test_fail("Scan request did not proceed correctly, while SCAN STOP was written");
3507
                  fail = fail + 1;
3508
                end
3509
              end
3510
            end
3511
          end
3512
        join
3513
        // check last scan 
3514
        repeat(4) @(posedge Mdc_O);
3515
        // read data from PHY status register
3516
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3517
        if (phy_data !== tmp_data)
3518
        begin
3519
          test_fail("7. data was not correctly scaned from status register");
3520
          fail = fail + 1;
3521 169 mohor
        end
3522 181 mohor
        // read data from MII status register
3523
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3524
        if (phy_data[0] !== 1'b0)
3525
        begin
3526
          test_fail("Link FAIL bit was set in the MII status register");
3527
          fail = fail + 1;
3528
        end
3529
 
3530
        #Tp mii_scan_finish; // finish scan operation
3531
        #Tp check_mii_busy; // wait for scan to finish
3532
        #Tp;
3533
        // set delay of writing the command
3534
        if (i2) // without preamble
3535
        begin
3536
          case(i)
3537
            0,  1,  2,  3,  4:  i = i + 1;
3538
            13, 14, 15, 16, 17,
3539
            18, 19, 20, 21, 22,
3540
            23, 24, 25, 26, 27,
3541
            28, 29, 30, 31, 32,
3542
            33, 34, 35:         i = i + 1;
3543
            36:                 i = 80;
3544
            default:            i = 13;
3545
          endcase
3546
        end
3547 169 mohor
        else // with preamble
3548
        begin
3549 181 mohor
          case(i)
3550
            0,  1,  2,  3,  4:  i = i + 1;
3551
            45, 46, 47, 48, 49,
3552
            50, 51, 52, 53, 54,
3553
            55, 56, 57, 58, 59,
3554
            60, 61, 62, 63, 64,
3555
            65, 66, 67:         i = i + 1;
3556
            68:                 i = 80;
3557
            default:            i = 45;
3558
          endcase
3559 169 mohor
        end
3560 181 mohor
        @(posedge wb_clk);
3561
        #Tp;
3562 169 mohor
      end
3563
    end
3564 181 mohor
    // set PHY to normal mode
3565
    #Tp eth_phy.preamble_suppresed(0);
3566
    // MII mode register
3567
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3568
    if(fail == 0)
3569
      test_ok;
3570
    else
3571
      fail = 0;
3572 169 mohor
  end
3573
 
3574
 
3575 181 mohor
  ////////////////////////////////////////////////////////////////////
3576
  ////                                                            ////
3577
  ////  Test sliding stop scan command immediately after scan     ////
3578
  ////  request (with and without preamble)                       ////
3579
  ////                                                            ////
3580
  ////////////////////////////////////////////////////////////////////
3581
  if (test_num == 16) // 
3582 116 mohor
  begin
3583 194 tadej
    // TEST 16: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER SCAN REQUEST ( WITH AND WITHOUT PREAMBLE )
3584
    test_name = "TEST 16: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER SCAN REQUEST ( WITH AND WITHOUT PREAMBLE )";
3585 181 mohor
    `TIME;
3586 194 tadej
    $display("  TEST 16: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER SCAN REQUEST ( WITH AND WITHOUT PREAMBLE )");
3587 181 mohor
 
3588
    for (i2 = 0; i2 <= 1; i2 = i2 + 1) // choose preamble or not
3589 169 mohor
    begin
3590 181 mohor
      #Tp eth_phy.preamble_suppresed(i2);
3591
      // MII mode register
3592
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i2, 8'h0}), 4'hF, 1, wbm_init_waits,
3593
                wbm_subseq_waits);
3594
      i = 0;
3595
      cnt = 0;
3596
      while (i < 80) // delay for sliding of writing a STOP SCAN command
3597 169 mohor
      begin
3598 181 mohor
        for (i3 = 0; i3 <= 1; i3 = i3 + 1) // choose read or write after scan will be finished
3599 169 mohor
        begin
3600 181 mohor
          // set address
3601
          reg_addr = 5'h0; // control register
3602
          phy_addr = 5'h1; // correct PHY address
3603
          cnt = 0;
3604
          // scan request
3605
          #Tp mii_scan_req(phy_addr, reg_addr);
3606
          fork
3607
            begin
3608
              repeat(i) @(posedge Mdc_O);
3609
              // write command 0x0 into MII command register
3610
              // MII command written while scan in progress
3611
              wbm_write(`ETH_MIICOMMAND, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3612
              @(posedge wb_clk);
3613
              #Tp check_mii_busy; // wait for scan to finish
3614
              @(posedge wb_clk);
3615
              disable check;
3616
            end
3617
            begin: check
3618
              // wait for serial bus to become active
3619
              wait(Mdio_IO !== 1'bz);
3620
              // count transfer length
3621
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3622
              begin
3623
                @(posedge Mdc_O);
3624
                #Tp cnt = cnt + 1;
3625
              end
3626
              // check transfer length
3627
              if (i2) // without preamble
3628
              begin
3629
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3630
                begin
3631
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3632
                  fail = fail + 1;
3633
                end
3634
              end
3635
              else // with preamble
3636
              begin
3637
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3638
                begin
3639
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3640
                  fail = fail + 1;
3641
                end
3642
              end
3643
              cnt = 0;
3644
              // wait for serial bus to become active if there is more than one scan
3645
              wait(Mdio_IO !== 1'bz);
3646
              // count transfer length
3647
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3648
              begin
3649
                @(posedge Mdc_O);
3650
                #Tp cnt = cnt + 1;
3651
              end
3652
              // check transfer length
3653
              if (i2) // without preamble
3654
              begin
3655
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3656
                begin
3657
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3658
                  fail = fail + 1;
3659
                end
3660
              end
3661
              else // with preamble
3662
              begin
3663
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3664
                begin
3665
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3666
                  fail = fail + 1;
3667
                end
3668
              end
3669
            end
3670
          join
3671
          // check the BUSY signal to see if the bus is still IDLE
3672
          for (i1 = 0; i1 < 8; i1 = i1 + 1)
3673
            check_mii_busy; // wait for bus to become idle
3674
 
3675
          // try normal write or read after scan was finished
3676
          phy_data = {8'h7D, (i[7:0] + 1)};
3677
          cnt = 0;
3678
          if (i3 == 0) // write after scan
3679 169 mohor
          begin
3680 181 mohor
            // write request
3681
            #Tp mii_write_req(phy_addr, reg_addr, phy_data);
3682
            // wait for serial bus to become active
3683
            wait(Mdio_IO !== 1'bz);
3684
            // count transfer length
3685
            while(Mdio_IO !== 1'bz)
3686
            begin
3687
              @(posedge Mdc_O);
3688
              #Tp cnt = cnt + 1;
3689
            end
3690 169 mohor
            @(posedge Mdc_O);
3691 181 mohor
            // read request
3692
            #Tp mii_read_req(phy_addr, reg_addr);
3693
            check_mii_busy; // wait for read to finish
3694
            // read and check data
3695
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3696
            if (phy_data !== tmp_data)
3697 169 mohor
            begin
3698 181 mohor
              test_fail("Data was not correctly written into OR read from PHY register - control register");
3699 169 mohor
              fail = fail + 1;
3700
            end
3701
          end
3702 181 mohor
          else // read after scan
3703 169 mohor
          begin
3704 181 mohor
            // read request
3705
            #Tp mii_read_req(phy_addr, reg_addr);
3706
            // wait for serial bus to become active
3707
            wait(Mdio_IO !== 1'bz);
3708
            // count transfer length
3709
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3710 169 mohor
            begin
3711 181 mohor
              @(posedge Mdc_O);
3712
              #Tp cnt = cnt + 1;
3713
            end
3714
            @(posedge Mdc_O);
3715
            check_mii_busy; // wait for read to finish
3716
            // read and check data
3717
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3718
            if (phy_data !== tmp_data)
3719
            begin
3720
              test_fail("Data was not correctly written into OR read from PHY register - control register");
3721 169 mohor
              fail = fail + 1;
3722
            end
3723
          end
3724 181 mohor
          // check if transfer was a proper length
3725 169 mohor
          if (i2) // without preamble
3726
          begin
3727
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
3728
            begin
3729 181 mohor
              test_fail("New request did not proceed correctly, after scan request");
3730 169 mohor
              fail = fail + 1;
3731
            end
3732
          end
3733
          else // with preamble
3734
          begin
3735
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
3736
            begin
3737 181 mohor
              test_fail("New request did not proceed correctly, after scan request");
3738 169 mohor
              fail = fail + 1;
3739
            end
3740
          end
3741
        end
3742 181 mohor
        #Tp;
3743
        // set delay of writing the command
3744
        if (i2) // without preamble
3745
        begin
3746
          case(i)
3747
            0, 1:               i = i + 1;
3748
            18, 19, 20, 21, 22,
3749
            23, 24, 25, 26, 27,
3750
            28, 29, 30, 31, 32,
3751
            33, 34, 35:         i = i + 1;
3752
            36:                 i = 80;
3753
            default:            i = 18;
3754
          endcase
3755
        end
3756
        else // with preamble
3757
        begin
3758
          case(i)
3759
            0, 1:               i = i + 1;
3760
            50, 51, 52, 53, 54,
3761
            55, 56, 57, 58, 59,
3762
            60, 61, 62, 63, 64,
3763
            65, 66, 67:         i = i + 1;
3764
            68:                 i = 80;
3765
            default:            i = 50;
3766
          endcase
3767
        end
3768
        @(posedge wb_clk);
3769
      end
3770
    end
3771
    // set PHY to normal mode
3772
    #Tp eth_phy.preamble_suppresed(0);
3773
    // MII mode register
3774
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3775
    if(fail == 0)
3776
      test_ok;
3777
    else
3778
      fail = 0;
3779
  end
3780 169 mohor
 
3781 181 mohor
 
3782
  ////////////////////////////////////////////////////////////////////
3783
  ////                                                            ////
3784
  ////  Test sliding stop scan command after 2. scan (with and    ////
3785
  ////  without preamble)                                         ////
3786
  ////                                                            ////
3787
  ////////////////////////////////////////////////////////////////////
3788
  if (test_num == 17) // 
3789
  begin
3790 194 tadej
    // TEST 17: SLIDING STOP SCAN COMMAND AFTER 2. SCAN ( WITH AND WITHOUT PREAMBLE )
3791
    test_name = "TEST 17: SLIDING STOP SCAN COMMAND AFTER 2. SCAN ( WITH AND WITHOUT PREAMBLE )";
3792
    `TIME; $display("  TEST 17: SLIDING STOP SCAN COMMAND AFTER 2. SCAN ( WITH AND WITHOUT PREAMBLE )");
3793 181 mohor
 
3794
    for (i2 = 0; i2 <= 1; i2 = i2 + 1) // choose preamble or not
3795
    begin
3796
      #Tp eth_phy.preamble_suppresed(i2);
3797
      // MII mode register
3798
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i2, 8'h0}), 4'hF, 1, wbm_init_waits,
3799
                wbm_subseq_waits);
3800
 
3801
      i = 0;
3802
      cnt = 0;
3803
      while (i < 80) // delay for sliding of writing a STOP SCAN command
3804
      begin
3805
        for (i3 = 0; i3 <= 1; i3 = i3 + 1) // choose read or write after scan will be finished
3806
        begin
3807
          // first there are two scans
3808
          // set address
3809
          reg_addr = 5'h0; // control register
3810
          phy_addr = 5'h1; // correct PHY address
3811
          cnt = 0;
3812
          // scan request
3813
          #Tp mii_scan_req(phy_addr, reg_addr);
3814
          // wait and check first 2 scans
3815 169 mohor
          begin
3816
            // wait for serial bus to become active
3817
            wait(Mdio_IO !== 1'bz);
3818
            // count transfer length
3819
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3820
            begin
3821
              @(posedge Mdc_O);
3822
              #Tp cnt = cnt + 1;
3823
            end
3824
            // check transfer length
3825
            if (i2) // without preamble
3826
            begin
3827
              if (cnt != 33) // at this value Mdio_IO is HIGH Z
3828
              begin
3829
                test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3830
                fail = fail + 1;
3831
              end
3832
            end
3833
            else // with preamble
3834
            begin
3835
              if (cnt != 65) // at this value Mdio_IO is HIGH Z
3836
              begin
3837
                test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3838
                fail = fail + 1;
3839
              end
3840
            end
3841
            cnt = 0;
3842
            // wait for serial bus to become active if there is more than one scan
3843
            wait(Mdio_IO !== 1'bz);
3844
            // count transfer length
3845
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3846
            begin
3847
              @(posedge Mdc_O);
3848
              #Tp cnt = cnt + 1;
3849
            end
3850
            // check transfer length
3851
            if (i2) // without preamble
3852
            begin
3853
              if (cnt != 33) // at this value Mdio_IO is HIGH Z
3854
              begin
3855
                test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3856
                fail = fail + 1;
3857
              end
3858
            end
3859
            else // with preamble
3860
            begin
3861
              if (cnt != 65) // at this value Mdio_IO is HIGH Z
3862
              begin
3863
                test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3864
                fail = fail + 1;
3865
              end
3866
            end
3867
          end
3868
 
3869 181 mohor
          // reset counter 
3870
          cnt = 0;
3871
          fork
3872
            begin
3873
              repeat(i) @(posedge Mdc_O);
3874
              // write command 0x0 into MII command register
3875
              // MII command written while scan in progress
3876
              wbm_write(`ETH_MIICOMMAND, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3877
              @(posedge wb_clk);
3878
              #Tp check_mii_busy; // wait for scan to finish
3879
              @(posedge wb_clk);
3880
              disable check_3;
3881
            end
3882
            begin: check_3
3883
              // wait for serial bus to become active
3884
              wait(Mdio_IO !== 1'bz);
3885
              // count transfer length
3886
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3887
              begin
3888
                @(posedge Mdc_O);
3889
                #Tp cnt = cnt + 1;
3890
              end
3891
              // check transfer length
3892
              if (i2) // without preamble
3893
              begin
3894
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3895
                begin
3896
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3897
                  fail = fail + 1;
3898
                end
3899
              end
3900
              else // with preamble
3901
              begin
3902
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3903
                begin
3904
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3905
                  fail = fail + 1;
3906
                end
3907
              end
3908
              cnt = 0;
3909
              // wait for serial bus to become active if there is more than one scan
3910
              wait(Mdio_IO !== 1'bz);
3911
              // count transfer length
3912
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3913
              begin
3914
                @(posedge Mdc_O);
3915
                #Tp cnt = cnt + 1;
3916
              end
3917
              // check transfer length
3918
              if (i2) // without preamble
3919
              begin
3920
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3921
                begin
3922
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3923
                  fail = fail + 1;
3924
                end
3925
              end
3926
              else // with preamble
3927
              begin
3928
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3929
                begin
3930
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3931
                  fail = fail + 1;
3932
                end
3933
              end
3934
            end
3935
          join
3936
          // check the BUSY signal to see if the bus is still IDLE
3937
          for (i1 = 0; i1 < 8; i1 = i1 + 1)
3938
            check_mii_busy; // wait for bus to become idle
3939
 
3940
          // try normal write or read after scan was finished
3941
          phy_data = {8'h7D, (i[7:0] + 1)};
3942
          cnt = 0;
3943
          if (i3 == 0) // write after scan
3944 169 mohor
          begin
3945 181 mohor
            // write request
3946
            #Tp mii_write_req(phy_addr, reg_addr, phy_data);
3947
            // wait for serial bus to become active
3948
            wait(Mdio_IO !== 1'bz);
3949
            // count transfer length
3950
            while(Mdio_IO !== 1'bz)
3951
            begin
3952
              @(posedge Mdc_O);
3953
              #Tp cnt = cnt + 1;
3954
            end
3955 169 mohor
            @(posedge Mdc_O);
3956 181 mohor
            // read request
3957
            #Tp mii_read_req(phy_addr, reg_addr);
3958
            check_mii_busy; // wait for read to finish
3959
            // read and check data
3960
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3961
            if (phy_data !== tmp_data)
3962
            begin
3963
              test_fail("Data was not correctly written into OR read from PHY register - control register");
3964
              fail = fail + 1;
3965
            end
3966 169 mohor
          end
3967 181 mohor
          else // read after scan
3968 169 mohor
          begin
3969 181 mohor
            // read request
3970
            #Tp mii_read_req(phy_addr, reg_addr);
3971
            // wait for serial bus to become active
3972
            wait(Mdio_IO !== 1'bz);
3973
            // count transfer length
3974
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3975
            begin
3976
              @(posedge Mdc_O);
3977
              #Tp cnt = cnt + 1;
3978
            end
3979
            @(posedge Mdc_O);
3980
            check_mii_busy; // wait for read to finish
3981
            // read and check data
3982
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3983
            if (phy_data !== tmp_data)
3984
            begin
3985
              test_fail("Data was not correctly written into OR read from PHY register - control register");
3986
              fail = fail + 1;
3987
            end
3988 169 mohor
          end
3989 181 mohor
          // check if transfer was a proper length
3990
          if (i2) // without preamble
3991 169 mohor
          begin
3992 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
3993
            begin
3994
              test_fail("New request did not proceed correctly, after scan request");
3995
              fail = fail + 1;
3996
            end
3997 169 mohor
          end
3998 181 mohor
          else // with preamble
3999 169 mohor
          begin
4000 181 mohor
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
4001
            begin
4002
              test_fail("New request did not proceed correctly, after scan request");
4003
              fail = fail + 1;
4004
            end
4005 169 mohor
          end
4006
        end
4007 181 mohor
        #Tp;
4008
        // set delay of writing the command
4009 169 mohor
        if (i2) // without preamble
4010
        begin
4011 181 mohor
          case(i)
4012
            0, 1:               i = i + 1;
4013
            18, 19, 20, 21, 22,
4014
            23, 24, 25, 26, 27,
4015
            28, 29, 30, 31, 32,
4016
            33, 34, 35:         i = i + 1;
4017
            36:                 i = 80;
4018
            default:            i = 18;
4019
          endcase
4020 169 mohor
        end
4021
        else // with preamble
4022
        begin
4023 181 mohor
          case(i)
4024
            0, 1:               i = i + 1;
4025
            50, 51, 52, 53, 54,
4026
            55, 56, 57, 58, 59,
4027
            60, 61, 62, 63, 64,
4028
            65, 66, 67:         i = i + 1;
4029
            68:                 i = 80;
4030
            default:            i = 50;
4031
          endcase
4032 169 mohor
        end
4033 181 mohor
        @(posedge wb_clk);
4034 116 mohor
      end
4035
    end
4036 181 mohor
    // set PHY to normal mode
4037
    #Tp eth_phy.preamble_suppresed(0);
4038
    // MII mode register
4039
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4040
    if(fail == 0)
4041
      test_ok;
4042
    else
4043
      fail = 0;
4044 169 mohor
  end
4045 116 mohor
 
4046 181 mohor
end   //  for (test_num=start_task; test_num <= end_task; test_num=test_num+1)
4047
 
4048 169 mohor
end
4049
endtask // test_mii
4050
 
4051
 
4052
task test_mac_full_duplex_transmit;
4053
  input  [31:0]  start_task;
4054
  input  [31:0]  end_task;
4055
  integer        bit_start_1;
4056
  integer        bit_end_1;
4057
  integer        bit_start_2;
4058
  integer        bit_end_2;
4059
  integer        num_of_reg;
4060 209 tadejm
  integer        num_of_frames;
4061
  integer        num_of_bd;
4062 169 mohor
  integer        i_addr;
4063
  integer        i_data;
4064
  integer        i_length;
4065 209 tadejm
  integer        tmp_len;
4066
  integer        tmp_bd;
4067
  integer        tmp_bd_num;
4068 169 mohor
  integer        tmp_data;
4069 209 tadejm
  integer        tmp_ipgt;
4070 194 tadej
  integer        test_num;
4071 169 mohor
  reg    [31:0]  tx_bd_num;
4072
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_data;
4073
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_tmp_data;
4074
  integer        i;
4075
  integer        i1;
4076
  integer        i2;
4077
  integer        i3;
4078
  integer        fail;
4079
  integer        speed;
4080 209 tadejm
  reg            frame_started;
4081
  reg            frame_ended;
4082
  reg            wait_for_frame;
4083 169 mohor
  reg    [31:0]  addr;
4084
  reg    [31:0]  data;
4085
  reg    [31:0]  tmp;
4086
  reg    [ 7:0]  st_data;
4087
  reg    [15:0]  max_tmp;
4088
  reg    [15:0]  min_tmp;
4089
begin
4090
// MAC FULL DUPLEX TRANSMIT TEST
4091
test_heading("MAC FULL DUPLEX TRANSMIT TEST");
4092
$display(" ");
4093
$display("MAC FULL DUPLEX TRANSMIT TEST");
4094
fail = 0;
4095
 
4096
// reset MAC registers
4097
hard_reset;
4098
// reset MAC and MII LOGIC with soft reset
4099
reset_mac;
4100
reset_mii;
4101
// set wb slave response
4102
wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
4103
 
4104
  /*
4105
  TASKS for set and control TX buffer descriptors (also send packet - set_tx_bd_ready):
4106
  -------------------------------------------------------------------------------------
4107
  set_tx_bd
4108
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0], len[15:0], irq, pad, crc, txpnt[31:0]);
4109
  set_tx_bd_wrap
4110
    (tx_bd_num_end[6:0]);
4111
  set_tx_bd_ready
4112
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
4113
  check_tx_bd
4114
    (tx_bd_num_start[6:0], tx_bd_status[31:0]);
4115
  clear_tx_bd
4116
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
4117
 
4118
  TASKS for set and control RX buffer descriptors:
4119
  ------------------------------------------------
4120
  set_rx_bd
4121
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0], irq, rxpnt[31:0]);
4122
  set_rx_bd_wrap
4123
    (rx_bd_num_end[6:0]);
4124
  set_rx_bd_empty
4125
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
4126
  check_rx_bd
4127
    (rx_bd_num_end[6:0], rx_bd_status);
4128
  clear_rx_bd
4129
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
4130
 
4131
  TASKS for set and check TX packets:
4132
  -----------------------------------
4133
  set_tx_packet
4134
    (txpnt[31:0], len[15:0], eth_start_data[7:0]);
4135
  check_tx_packet
4136
    (txpnt_wb[31:0], txpnt_phy[31:0], len[15:0], failure[31:0]);
4137
 
4138
  TASKS for set and check RX packets:
4139
  -----------------------------------
4140
  set_rx_packet
4141
    (rxpnt[31:0], len[15:0], plus_nibble, d_addr[47:0], s_addr[47:0], type_len[15:0], start_data[7:0]);
4142
  check_rx_packet
4143
    (rxpnt_phy[31:0], rxpnt_wb[31:0], len[15:0], plus_nibble, successful_nibble, failure[31:0]);
4144
 
4145
  TASKS for append and check CRC to/of TX packet:
4146
  -----------------------------------------------
4147
  append_tx_crc
4148
    (txpnt_wb[31:0], len[15:0], negated_crc);
4149
  check_tx_crc
4150
    (txpnt_phy[31:0], len[15:0], negated_crc, failure[31:0]);
4151
 
4152
  TASK for append CRC to RX packet (CRC is checked together with check_rx_packet):
4153
  --------------------------------------------------------------------------------
4154
  append_rx_crc
4155
    (rxpnt_phy[31:0], len[15:0], plus_nibble, negated_crc);
4156
  */
4157
 
4158 194 tadej
//////////////////////////////////////////////////////////////////////
4159
////                                                              ////
4160
////  test_mac_full_duplex_transmit:                              ////
4161
////                                                              ////
4162
////  0: Test no transmit when all buffers are RX ( 10Mbps ).     ////
4163
////  1: Test no transmit when all buffers are RX ( 100Mbps ).    ////
4164
////  2: Test transmit packets form MINFL to MAXFL sizes at       ////
4165
////     one TX buffer decriptor ( 10Mbps ).                      ////
4166
////  3: Test transmit packets form MINFL to MAXFL sizes at       ////
4167
////     one TX buffer decriptor ( 100Mbps ).                     ////
4168
////                                                              ////
4169
//////////////////////////////////////////////////////////////////////
4170
for (test_num = start_task; test_num <= end_task; test_num = test_num + 1)
4171 169 mohor
begin
4172
 
4173 194 tadej
  ////////////////////////////////////////////////////////////////////
4174
  ////                                                            ////
4175
  ////  Test no transmit when all buffers are RX ( 10Mbps ).      ////
4176
  ////                                                            ////
4177
  ////////////////////////////////////////////////////////////////////
4178
  if (test_num == 0) // Test no transmit when all buffers are RX ( 10Mbps ).
4179 169 mohor
  begin
4180 194 tadej
    // TEST 0: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 10Mbps )
4181
    test_name   = "TEST 0: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 10Mbps )";
4182
    `TIME; $display("  TEST 0: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 10Mbps )");
4183
 
4184
    // unmask interrupts
4185 209 tadejm
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4186 194 tadej
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4187
    // set all buffer descriptors to RX - must be set before TX enable
4188
    wbm_write(`ETH_TX_BD_NUM, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4189
    // enable TX, set full-duplex mode, padding and CRC appending
4190
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4191
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4192
 
4193
    // write to phy's control register for 10Mbps
4194
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
4195
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
4196
    speed = 10;
4197
 
4198
    i = 0;
4199
    while (i < 128)
4200 169 mohor
    begin
4201 194 tadej
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
4202 169 mohor
      begin
4203 194 tadej
        set_tx_packet((`MEMORY_BASE + (i1 * 200)), 100, 0);
4204
        set_tx_bd(i1, i1, 100, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + (i1 * 200)));
4205 169 mohor
      end
4206 194 tadej
      set_tx_bd_wrap(i);
4207
      fork
4208
        begin
4209
          set_tx_bd_ready(0, i);
4210
          repeat(20) @(negedge mtx_clk);
4211
          #1 disable check_tx_en10;
4212
        end
4213
        begin: check_tx_en10
4214
          wait (MTxEn === 1'b1);
4215
          test_fail("Tramsmit should not start at all");
4216
          fail = fail + 1;
4217
          `TIME; $display("*E Transmit of %d packets should not start at all - active MTxEn", i);
4218
        end
4219
      join
4220
      for (i2 = 0; i2 < 20; i2 = i2 + 1)
4221 169 mohor
      begin
4222 194 tadej
        check_tx_bd(0, tmp);
4223
        #1;
4224
        if (tmp[15] === 1'b0)
4225
        begin
4226
          test_fail("Tramsmit should not start at all");
4227
          fail = fail + 1;
4228
          `TIME; $display("*E Transmit of %d packets should not start at all - ready is 0", i);
4229
        end
4230
        if (tmp[8:0] !== 0)
4231
        begin
4232
          test_fail("Tramsmit should not be finished since it should not start at all");
4233
          fail = fail + 1;
4234
          `TIME; $display("*E Transmit of should not be finished since it should not start at all");
4235
        end
4236
        @(posedge wb_clk);
4237 169 mohor
      end
4238 194 tadej
      wbm_read(`ETH_INT, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4239
      if (tmp[6:0] !== 0)
4240 169 mohor
      begin
4241 194 tadej
        test_fail("Tramsmit should not get INT since it should not start at all");
4242 169 mohor
        fail = fail + 1;
4243 194 tadej
        `TIME; $display("*E Transmit of should not get INT since it should not start at all");
4244 169 mohor
      end
4245 194 tadej
      clear_tx_bd(0, i);
4246
      if ((i < 5) || (i > 124))
4247
        i = i + 1;
4248
      else
4249
        i = i + 120;
4250 116 mohor
    end
4251 194 tadej
    // disable TX
4252
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4253
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4254
    if(fail == 0)
4255
      test_ok;
4256 116 mohor
    else
4257 194 tadej
      fail = 0;
4258 169 mohor
  end
4259 116 mohor
 
4260
 
4261 194 tadej
  ////////////////////////////////////////////////////////////////////
4262
  ////                                                            ////
4263
  ////  Test no transmit when all buffers are RX ( 100Mbps ).     ////
4264
  ////                                                            ////
4265
  ////////////////////////////////////////////////////////////////////
4266
  if (test_num == 1) // Test no transmit when all buffers are RX ( 100Mbps ).
4267 169 mohor
  begin
4268 194 tadej
    // TEST 1: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 100Mbps )
4269
    test_name   = "TEST 1: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 100Mbps )";
4270
    `TIME; $display("  TEST 1: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 100Mbps )");
4271
 
4272
    // unmask interrupts
4273 209 tadejm
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4274 194 tadej
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4275
    // set all buffer descriptors to RX - must be set before TX enable
4276
    wbm_write(`ETH_TX_BD_NUM, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4277
    // enable TX, set full-duplex mode, padding and CRC appending
4278
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4279
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4280
 
4281
    // write to phy's control register for 100Mbps
4282
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
4283
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
4284
    speed = 100;
4285
 
4286
    i = 0;
4287
    while (i < 128)
4288 169 mohor
    begin
4289 194 tadej
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
4290 169 mohor
      begin
4291 194 tadej
        set_tx_packet((`MEMORY_BASE + (i1 * 200)), 100, 0);
4292
        set_tx_bd(i1, i1, 100, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + (i1 * 200)));
4293 169 mohor
      end
4294 194 tadej
      set_tx_bd_wrap(i);
4295
      fork
4296
        begin
4297
          set_tx_bd_ready(0, i);
4298
          repeat(20) @(negedge mtx_clk);
4299
          #1 disable check_tx_en100;
4300
        end
4301
        begin: check_tx_en100
4302
          wait (MTxEn === 1'b1);
4303
          test_fail("Tramsmit should not start at all");
4304
          fail = fail + 1;
4305
          `TIME; $display("*E Transmit of %d packets should not start at all - active MTxEn", i);
4306
        end
4307
      join
4308
      for (i2 = 0; i2 < 20; i2 = i2 + 1)
4309 169 mohor
      begin
4310 194 tadej
        check_tx_bd(0, tmp);
4311
        #1;
4312
        if (tmp[15] === 1'b0)
4313
        begin
4314
          test_fail("Tramsmit should not start at all");
4315
          fail = fail + 1;
4316
          `TIME; $display("*E Transmit of %d packets should not start at all - ready is 0", i);
4317
        end
4318
        if (tmp[8:0] !== 0)
4319
        begin
4320
          test_fail("Tramsmit should not be finished since it should not start at all");
4321
          fail = fail + 1;
4322
          `TIME; $display("*E Transmit of should not be finished since it should not start at all");
4323
        end
4324
        @(posedge wb_clk);
4325 169 mohor
      end
4326 194 tadej
      wbm_read(`ETH_INT, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4327
      if (tmp[6:0] !== 0)
4328 169 mohor
      begin
4329 194 tadej
        test_fail("Tramsmit should not get INT since it should not start at all");
4330 169 mohor
        fail = fail + 1;
4331 194 tadej
        `TIME; $display("*E Transmit of should not get INT since it should not start at all");
4332 169 mohor
      end
4333 194 tadej
      clear_tx_bd(0, i);
4334
      if ((i < 5) || (i > 124))
4335
        i = i + 1;
4336
      else
4337
        i = i + 120;
4338 169 mohor
    end
4339 194 tadej
    // disable TX
4340
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4341
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4342
    if(fail == 0)
4343
      test_ok;
4344 169 mohor
    else
4345 194 tadej
      fail = 0;
4346 169 mohor
  end
4347
 
4348
 
4349 194 tadej
  ////////////////////////////////////////////////////////////////////
4350
  ////                                                            ////
4351
  ////  Test transmit packets form MINFL to MAXFL sizes at        ////
4352
  ////  one TX buffer decriptor ( 10Mbps ).                       ////
4353
  ////                                                            ////
4354
  ////////////////////////////////////////////////////////////////////
4355 209 tadejm
  if (test_num == 2) // without and with padding
4356 169 mohor
  begin
4357 194 tadej
    // TEST 2: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 10Mbps )
4358
    test_name = "TEST 2: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 10Mbps )";
4359
    `TIME; $display("  TEST 2: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 10Mbps )");
4360
 
4361
    max_tmp = 0;
4362
    min_tmp = 0;
4363
    // set one TX buffer descriptor - must be set before TX enable
4364
    wbm_write(`ETH_TX_BD_NUM, 32'h1, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4365 209 tadejm
    // enable TX, set full-duplex mode, NO padding and CRC appending
4366
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
4367 194 tadej
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4368
    // prepare two packets of MAXFL length
4369
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4370
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
4371
    min_tmp = tmp[31:16];
4372 209 tadejm
    st_data = 8'h01;
4373
    set_tx_packet(`MEMORY_BASE, (max_tmp), st_data); // length without CRC
4374 194 tadej
    st_data = 8'h10;
4375 209 tadejm
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp), st_data); // length without CRC
4376 194 tadej
    // check WB INT signal
4377
    if (wb_int !== 1'b0)
4378 169 mohor
    begin
4379 194 tadej
      test_fail("WB INT signal should not be set");
4380
      fail = fail + 1;
4381 169 mohor
    end
4382 194 tadej
 
4383
    // write to phy's control register for 10Mbps
4384
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
4385
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
4386
    speed = 10;
4387
 
4388
    i_length = (min_tmp - 4);
4389
    while (i_length <= (max_tmp - 4))
4390 169 mohor
    begin
4391 194 tadej
      // choose generating carrier sense and collision for first and last 64 lengths of frames
4392
      case (i_length[1:0])
4393
      2'h0: // Interrupt is generated
4394 169 mohor
      begin
4395 194 tadej
        // enable interrupt generation
4396 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
4397 194 tadej
        // unmask interrupts
4398 209 tadejm
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4399 194 tadej
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4400
        // not detect carrier sense in FD and no collision
4401
        eth_phy.carrier_sense_tx_fd_detect(0);
4402
        eth_phy.collision(0);
4403 169 mohor
      end
4404 194 tadej
      2'h1: // Interrupt is not generated
4405 169 mohor
      begin
4406 194 tadej
        // enable interrupt generation
4407 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
4408 194 tadej
        // mask interrupts
4409
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4410
        // detect carrier sense in FD and no collision
4411
        eth_phy.carrier_sense_tx_fd_detect(1);
4412
        eth_phy.collision(0);
4413 169 mohor
      end
4414 194 tadej
      2'h2: // Interrupt is not generated
4415
      begin
4416
        // disable interrupt generation
4417 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
4418 194 tadej
        // unmask interrupts
4419 209 tadejm
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4420 194 tadej
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4421
        // not detect carrier sense in FD and set collision
4422
        eth_phy.carrier_sense_tx_fd_detect(0);
4423
        eth_phy.collision(1);
4424
      end
4425
      default: // 2'h3: // Interrupt is not generated
4426
      begin
4427
        // disable interrupt generation
4428 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
4429 194 tadej
        // mask interrupts
4430
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4431
        // detect carrier sense in FD and set collision
4432
        eth_phy.carrier_sense_tx_fd_detect(1);
4433
        eth_phy.collision(1);
4434
      end
4435
      endcase
4436
      eth_phy.set_tx_mem_addr(max_tmp);
4437
      // set wrap bit
4438
      set_tx_bd_wrap(0);
4439
      set_tx_bd_ready(0, 0);
4440 169 mohor
      #1 check_tx_bd(0, data);
4441 194 tadej
      if (i_length < min_tmp) // just first four
4442 169 mohor
      begin
4443 194 tadej
        while (data[15] === 1)
4444
        begin
4445
          #1 check_tx_bd(0, data);
4446
          @(posedge wb_clk);
4447
        end
4448 209 tadejm
        repeat (1) @(posedge wb_clk);
4449 169 mohor
      end
4450 194 tadej
      else if (i_length > (max_tmp - 8)) // just last four
4451 192 tadej
      begin
4452 194 tadej
        tmp = 0;
4453
        wait (MTxEn === 1'b1); // start transmit
4454
        while (tmp < (i_length - 20))
4455
        begin
4456
          #1 tmp = tmp + 1;
4457
          @(posedge wb_clk);
4458
        end
4459
        #1 check_tx_bd(0, data);
4460
        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 192 tadej
      end
4467
      else
4468
      begin
4469 194 tadej
        wait (MTxEn === 1'b1); // start transmit
4470
        #1 check_tx_bd(0, data);
4471
        if (data[15] !== 1)
4472
        begin
4473
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
4474
          fail = fail + 1;
4475
        end
4476
        wait (MTxEn === 1'b0); // end transmit
4477
        while (data[15] === 1)
4478
        begin
4479
          #1 check_tx_bd(0, data);
4480
          @(posedge wb_clk);
4481
        end
4482
        repeat (1) @(posedge wb_clk);
4483 192 tadej
      end
4484 194 tadej
      // check length of a PACKET
4485
      if (eth_phy.tx_len != (i_length + 4))
4486 192 tadej
      begin
4487 194 tadej
        test_fail("Wrong length of the packet out from MAC");
4488 192 tadej
        fail = fail + 1;
4489
      end
4490 194 tadej
      // checking in the following if statement is performed only for first and last 64 lengths
4491
      if ( ((i_length + 4) <= (min_tmp + 64)) || ((i_length + 4) > (max_tmp - 64)) )
4492 192 tadej
      begin
4493 194 tadej
        // check transmitted TX packet data
4494
        if (i_length[0] == 0)
4495
        begin
4496 209 tadejm
          check_tx_packet((`MEMORY_BASE + i_length[1:0]), max_tmp, i_length, tmp);
4497 194 tadej
        end
4498
        else
4499
        begin
4500 209 tadejm
          check_tx_packet(((`MEMORY_BASE + i_length[1:0]) + max_tmp), max_tmp, i_length, tmp);
4501 194 tadej
        end
4502
        if (tmp > 0)
4503
        begin
4504
          test_fail("Wrong data of the transmitted packet");
4505
          fail = fail + 1;
4506
        end
4507
        // check transmited TX packet CRC
4508
        check_tx_crc(max_tmp, i_length, 1'b0, tmp); // length without CRC
4509
        if (tmp > 0)
4510
        begin
4511
          test_fail("Wrong CRC of the transmitted packet");
4512
          fail = fail + 1;
4513
        end
4514 192 tadej
      end
4515 194 tadej
      // check WB INT signal
4516
      if (i_length[1:0] == 2'h0)
4517 192 tadej
      begin
4518 194 tadej
        if (wb_int !== 1'b1)
4519
        begin
4520
          `TIME; $display("*E WB INT signal should be set");
4521
          test_fail("WB INT signal should be set");
4522
          fail = fail + 1;
4523
        end
4524 192 tadej
      end
4525 194 tadej
      else
4526 192 tadej
      begin
4527 194 tadej
        if (wb_int !== 1'b0)
4528
        begin
4529
          `TIME; $display("*E WB INT signal should not be set");
4530
          test_fail("WB INT signal should not be set");
4531
          fail = fail + 1;
4532
        end
4533 192 tadej
      end
4534 194 tadej
      // check TX buffer descriptor of a packet
4535
      check_tx_bd(0, data);
4536
      if (i_length[1] == 1'b0) // interrupt enabled
4537 192 tadej
      begin
4538 194 tadej
        if (data[15:0] !== 16'h7800)
4539
        begin
4540
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
4541
          test_fail("TX buffer descriptor status is not correct");
4542
          fail = fail + 1;
4543
        end
4544 192 tadej
      end
4545 194 tadej
      else // interrupt not enabled
4546 192 tadej
      begin
4547 194 tadej
        if (data[15:0] !== 16'h3800)
4548
        begin
4549
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
4550
          test_fail("TX buffer descriptor status is not correct");
4551
          fail = fail + 1;
4552
        end
4553 192 tadej
      end
4554 194 tadej
      // clear TX buffer descriptor
4555
      clear_tx_bd(0, 0);
4556
      // check interrupts
4557
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4558
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
4559 192 tadej
      begin
4560 194 tadej
        if ((data & `ETH_INT_TXB) !== 1'b1)
4561
        begin
4562
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
4563
          test_fail("Interrupt Transmit Buffer was not set");
4564
          fail = fail + 1;
4565
        end
4566
        if ((data & (~`ETH_INT_TXB)) !== 0)
4567
        begin
4568
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
4569
          test_fail("Other interrupts (except Transmit Buffer) were set");
4570
          fail = fail + 1;
4571
        end
4572 192 tadej
      end
4573 194 tadej
      else
4574 192 tadej
      begin
4575 194 tadej
        if (data !== 0)
4576
        begin
4577
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
4578
          test_fail("Any of interrupts (except Transmit Buffer) was set");
4579
          fail = fail + 1;
4580
        end
4581 192 tadej
      end
4582 194 tadej
      // clear interrupts
4583
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4584
      // check WB INT signal
4585
      if (wb_int !== 1'b0)
4586 192 tadej
      begin
4587 194 tadej
        test_fail("WB INT signal should not be set");
4588 192 tadej
        fail = fail + 1;
4589
      end
4590 194 tadej
      // INTERMEDIATE DISPLAYS
4591
      if ((i_length + 4) == (min_tmp + 64))
4592 209 tadejm
      begin
4593 194 tadej
        // starting length is min_tmp, ending length is (min_tmp + 64)
4594 209 tadejm
        $display("    pads appending to packets is NOT selected");
4595
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
4596 194 tadej
                 min_tmp, (min_tmp + 64));
4597 209 tadejm
        // set padding, remain the rest
4598
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4599
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4600
      end
4601 194 tadej
      else if ((i_length + 4) == (max_tmp - 16))
4602 209 tadejm
      begin
4603 194 tadej
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
4604 209 tadejm
        $display("    pads appending to packets is selected");
4605
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
4606 194 tadej
                 (min_tmp + 64 + 128), tmp_data);
4607 209 tadejm
        // reset padding, remain the rest
4608
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
4609
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4610
      end
4611 194 tadej
      else if ((i_length + 4) == max_tmp)
4612 209 tadejm
      begin
4613
        $display("    pads appending to packets is NOT selected");
4614
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
4615 194 tadej
                 (max_tmp - (4 + 16)), max_tmp);
4616 209 tadejm
      end
4617 194 tadej
      // set length (loop variable)
4618
      if ((i_length + 4) < (min_tmp + 64))
4619
        i_length = i_length + 1;
4620
      else if ( ((i_length + 4) >= (min_tmp + 64)) && ((i_length + 4) <= (max_tmp - 256)) )
4621
      begin
4622
        i_length = i_length + 128;
4623
        tmp_data = i_length + 4; // last tmp_data is ending length
4624
      end
4625
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
4626
        i_length = max_tmp - (4 + 16);
4627
      else if ((i_length + 4) >= (max_tmp - 16))
4628
        i_length = i_length + 1;
4629
      else
4630
      begin
4631
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
4632
        #10 $stop;
4633
      end
4634 192 tadej
    end
4635 194 tadej
    // disable TX
4636
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4637
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4638
    if(fail == 0)
4639
      test_ok;
4640
    else
4641
      fail = 0;
4642
  end
4643
 
4644
 
4645
  ////////////////////////////////////////////////////////////////////
4646
  ////                                                            ////
4647
  ////  Test transmit packets form MINFL to MAXFL sizes at        ////
4648
  ////  one TX buffer decriptor ( 100Mbps ).                      ////
4649
  ////                                                            ////
4650
  ////////////////////////////////////////////////////////////////////
4651 209 tadejm
  if (test_num == 3) // with and without padding
4652 194 tadej
  begin
4653
    // TEST 3: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 100Mbps )
4654
    test_name = "TEST 3: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 100Mbps )";
4655
    `TIME; $display("  TEST 3: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 100Mbps )");
4656
 
4657
    max_tmp = 0;
4658
    min_tmp = 0;
4659
    // set one TX buffer descriptor - must be set before TX enable
4660
    wbm_write(`ETH_TX_BD_NUM, 32'h1, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4661 209 tadejm
    // enable TX, set full-duplex mode, NO padding and CRC appending
4662
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
4663 194 tadej
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4664
    // prepare two packets of MAXFL length
4665
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4666
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
4667
    min_tmp = tmp[31:16];
4668
    st_data = 8'h5A;
4669 209 tadejm
    set_tx_packet(`MEMORY_BASE, (max_tmp), st_data); // length without CRC
4670 194 tadej
    st_data = 8'h10;
4671 209 tadejm
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp), st_data); // length without CRC
4672 169 mohor
    // check WB INT signal
4673 192 tadej
    if (wb_int !== 1'b0)
4674 169 mohor
    begin
4675
      test_fail("WB INT signal should not be set");
4676
      fail = fail + 1;
4677
    end
4678 194 tadej
 
4679
    // write to phy's control register for 100Mbps
4680
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
4681
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
4682
    speed = 100;
4683 192 tadej
 
4684 194 tadej
    i_length = (min_tmp - 4);
4685
    while (i_length <= (max_tmp - 4))
4686 192 tadej
    begin
4687 194 tadej
      // choose generating carrier sense and collision
4688
      case (i_length[1:0])
4689
      2'h0: // Interrupt is generated
4690 192 tadej
      begin
4691 194 tadej
        // enable interrupt generation
4692 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
4693 194 tadej
        // unmask interrupts
4694 209 tadejm
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4695 194 tadej
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4696
        // not detect carrier sense in FD and no collision
4697
        eth_phy.carrier_sense_tx_fd_detect(0);
4698
        eth_phy.collision(0);
4699 192 tadej
      end
4700 194 tadej
      2'h1: // Interrupt is not generated
4701 192 tadej
      begin
4702 194 tadej
        // enable interrupt generation
4703 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
4704 194 tadej
        // mask interrupts
4705
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4706
        // detect carrier sense in FD and no collision
4707
        eth_phy.carrier_sense_tx_fd_detect(1);
4708
        eth_phy.collision(0);
4709 192 tadej
      end
4710 194 tadej
      2'h2: // Interrupt is not generated
4711
      begin
4712
        // disable interrupt generation
4713 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
4714 194 tadej
        // unmask interrupts
4715 209 tadejm
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4716 194 tadej
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4717
        // not detect carrier sense in FD and set collision
4718
        eth_phy.carrier_sense_tx_fd_detect(0);
4719
        eth_phy.collision(1);
4720
      end
4721
      default: // 2'h3: // Interrupt is not generated
4722
      begin
4723
        // disable interrupt generation
4724 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
4725 194 tadej
        // mask interrupts
4726
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4727
        // detect carrier sense in FD and set collision
4728
        eth_phy.carrier_sense_tx_fd_detect(1);
4729
        eth_phy.collision(1);
4730
      end
4731
      endcase
4732
      eth_phy.set_tx_mem_addr(max_tmp);
4733
      // set wrap bit
4734
      set_tx_bd_wrap(0);
4735
      set_tx_bd_ready(0, 0);
4736 192 tadej
      #1 check_tx_bd(0, data);
4737 194 tadej
      if (i_length < min_tmp) // just first four
4738 192 tadej
      begin
4739 194 tadej
        while (data[15] === 1)
4740
        begin
4741
          #1 check_tx_bd(0, data);
4742
          @(posedge wb_clk);
4743
        end
4744 209 tadejm
        repeat (1) @(posedge wb_clk);
4745 194 tadej
      end
4746
      else if (i_length > (max_tmp - 8)) // just last four
4747
      begin
4748
        tmp = 0;
4749
        wait (MTxEn === 1'b1); // start transmit
4750
        while (tmp < (i_length - 20))
4751
        begin
4752
          #1 tmp = tmp + 1;
4753
          @(posedge wb_clk);
4754
        end
4755 192 tadej
        #1 check_tx_bd(0, data);
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 192 tadej
      end
4763 194 tadej
      else
4764
      begin
4765
        wait (MTxEn === 1'b1); // start transmit
4766
        #1 check_tx_bd(0, data);
4767
        if (data[15] !== 1)
4768
        begin
4769
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
4770
          fail = fail + 1;
4771
        end
4772
        wait (MTxEn === 1'b0); // end transmit
4773
        while (data[15] === 1)
4774
        begin
4775
          #1 check_tx_bd(0, data);
4776
          @(posedge wb_clk);
4777
        end
4778
        repeat (1) @(posedge wb_clk);
4779
      end
4780
      // check length of a PACKET
4781
      if (eth_phy.tx_len != (i_length + 4))
4782
      begin
4783
        test_fail("Wrong length of the packet out from MAC");
4784
        fail = fail + 1;
4785
      end
4786 192 tadej
      // check transmitted TX packet data
4787
      if (i_length[0] == 0)
4788
      begin
4789 209 tadejm
        check_tx_packet((`MEMORY_BASE + i_length[1:0]), max_tmp, i_length, tmp);
4790 192 tadej
      end
4791
      else
4792
      begin
4793 209 tadejm
        check_tx_packet(((`MEMORY_BASE + i_length[1:0]) + max_tmp), max_tmp, i_length, tmp);
4794 192 tadej
      end
4795
      if (tmp > 0)
4796
      begin
4797
        test_fail("Wrong data of the transmitted packet");
4798
        fail = fail + 1;
4799
      end
4800
      // check transmited TX packet CRC
4801
      check_tx_crc(max_tmp, i_length, 1'b0, tmp); // length without CRC
4802
      if (tmp > 0)
4803
      begin
4804
        test_fail("Wrong CRC of the transmitted packet");
4805
        fail = fail + 1;
4806
      end
4807 194 tadej
      // check WB INT signal
4808
      if (i_length[1:0] == 2'h0)
4809 192 tadej
      begin
4810 194 tadej
        if (wb_int !== 1'b1)
4811
        begin
4812
          `TIME; $display("*E WB INT signal should be set");
4813
          test_fail("WB INT signal should be set");
4814
          fail = fail + 1;
4815
        end
4816 192 tadej
      end
4817 194 tadej
      else
4818 192 tadej
      begin
4819 194 tadej
        if (wb_int !== 1'b0)
4820
        begin
4821
          `TIME; $display("*E WB INT signal should not be set");
4822
          test_fail("WB INT signal should not be set");
4823
          fail = fail + 1;
4824
        end
4825 192 tadej
      end
4826 194 tadej
      // check TX buffer descriptor of a packet
4827
      check_tx_bd(0, data);
4828
      if (i_length[1] == 1'b0) // interrupt enabled
4829 192 tadej
      begin
4830 194 tadej
        if (data[15:0] !== 16'h7800)
4831
        begin
4832
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
4833
          test_fail("TX buffer descriptor status is not correct");
4834
          fail = fail + 1;
4835
        end
4836 192 tadej
      end
4837 194 tadej
      else // interrupt not enabled
4838 192 tadej
      begin
4839 194 tadej
        if (data[15:0] !== 16'h3800)
4840
        begin
4841
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
4842
          test_fail("TX buffer descriptor status is not correct");
4843
          fail = fail + 1;
4844
        end
4845 192 tadej
      end
4846 194 tadej
      // clear TX buffer descriptor
4847
      clear_tx_bd(0, 0);
4848
      // check interrupts
4849
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4850
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
4851 192 tadej
      begin
4852 194 tadej
        if ((data & `ETH_INT_TXB) !== 1'b1)
4853
        begin
4854
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
4855
          test_fail("Interrupt Transmit Buffer was not set");
4856
          fail = fail + 1;
4857
        end
4858
        if ((data & (~`ETH_INT_TXB)) !== 0)
4859
        begin
4860
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
4861
          test_fail("Other interrupts (except Transmit Buffer) were set");
4862
          fail = fail + 1;
4863
        end
4864 192 tadej
      end
4865 194 tadej
      else
4866 192 tadej
      begin
4867 194 tadej
        if (data !== 0)
4868
        begin
4869
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h", data);
4870
          test_fail("Any of interrupts (except Transmit Buffer) was set");
4871
          fail = fail + 1;
4872
        end
4873 192 tadej
      end
4874 194 tadej
      // clear interrupts
4875
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4876
      // check WB INT signal
4877
      if (wb_int !== 1'b0)
4878 192 tadej
      begin
4879 194 tadej
        test_fail("WB INT signal should not be set");
4880 192 tadej
        fail = fail + 1;
4881
      end
4882 194 tadej
      // INTERMEDIATE DISPLAYS
4883
      if ((i_length + 4) == (min_tmp + 64))
4884 209 tadejm
      begin
4885 194 tadej
        // starting length is min_tmp, ending length is (min_tmp + 64)
4886 209 tadejm
        $display("    pads appending to packets is NOT selected");
4887
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
4888 194 tadej
                 min_tmp, (min_tmp + 64));
4889 209 tadejm
        // set padding, remain the rest
4890
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4891
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4892
      end
4893 194 tadej
      else if ((i_length + 4) == (max_tmp - 16))
4894 209 tadejm
      begin
4895 194 tadej
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
4896 209 tadejm
        $display("    pads appending to packets is selected");
4897
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
4898 194 tadej
                 (min_tmp + 64 + 128), tmp_data);
4899 209 tadejm
        // reset padding, remain the rest
4900
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
4901
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4902
      end
4903 194 tadej
      else if ((i_length + 4) == max_tmp)
4904 209 tadejm
      begin
4905
        $display("    pads appending to packets is NOT selected");
4906
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
4907 194 tadej
                 (max_tmp - (4 + 16)), max_tmp);
4908 209 tadejm
      end
4909 194 tadej
      // set length (loop variable)
4910
      if ((i_length + 4) < (min_tmp + 64))
4911
        i_length = i_length + 1;
4912
      else if ( ((i_length + 4) >= (min_tmp + 64)) && ((i_length + 4) <= (max_tmp - 256)) )
4913
      begin
4914
        i_length = i_length + 128;
4915
        tmp_data = i_length + 4; // last tmp_data is ending length
4916
      end
4917
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
4918
        i_length = max_tmp - (4 + 16);
4919
      else if ((i_length + 4) >= (max_tmp - 16))
4920
        i_length = i_length + 1;
4921 192 tadej
      else
4922 194 tadej
      begin
4923
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
4924
        #10 $stop;
4925
      end
4926 179 mohor
    end
4927 194 tadej
    // disable TX
4928
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4929
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4930
    if(fail == 0)
4931
      test_ok;
4932
    else
4933
      fail = 0;
4934 169 mohor
  end
4935
 
4936
 
4937 209 tadejm
  ////////////////////////////////////////////////////////////////////
4938
  ////                                                            ////
4939
  ////  Test transmit packets form MINFL to MAXFL sizes at        ////
4940
  ////  maximum TX buffer decriptors ( 10Mbps ).                  ////
4941
  ////                                                            ////
4942
  ////////////////////////////////////////////////////////////////////
4943
  if (test_num == 4) // without and with padding
4944
  begin
4945
    // TEST 4: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 10Mbps )
4946
    test_name = "TEST 4: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 10Mbps )";
4947
    `TIME; $display("  TEST 4: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 10Mbps )");
4948
 
4949
    // reset MAC registers
4950
    hard_reset;
4951
    // reset MAC and MII LOGIC with soft reset
4952
    reset_mac;
4953
    reset_mii;
4954
    // set wb slave response
4955
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
4956 192 tadej
 
4957 209 tadejm
    max_tmp = 0;
4958
    min_tmp = 0;
4959
    num_of_frames = 0;
4960
    num_of_bd = 0;
4961
    // set maximum TX buffer descriptors (128) - must be set before TX enable
4962
    wbm_write(`ETH_TX_BD_NUM, 32'h80, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4963
    // enable TX, set full-duplex mode, NO padding and CRC appending
4964
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
4965
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4966
    // prepare two packets of MAXFL length
4967
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4968
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
4969
    min_tmp = tmp[31:16];
4970
    st_data = 8'hA3;
4971
    set_tx_packet(`MEMORY_BASE, (max_tmp), st_data); // length without CRC
4972
    st_data = 8'h81;
4973
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp), st_data); // length without CRC
4974
    // check WB INT signal
4975
    if (wb_int !== 1'b0)
4976
    begin
4977
      test_fail("WB INT signal should not be set");
4978
      fail = fail + 1;
4979
    end
4980
 
4981
    // write to phy's control register for 10Mbps
4982
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
4983
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
4984
    speed = 10;
4985
 
4986
    i_length = (min_tmp - 4);
4987
    while (i_length <= (max_tmp - 4))
4988
    begin
4989
      // choose generating carrier sense and collision
4990
      case (i_length[1:0])
4991
      2'h0: // Interrupt is generated
4992
      begin
4993
        // Reset_tx_bd nable interrupt generation
4994
        // unmask interrupts
4995
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4996
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4997
        // not detect carrier sense in FD and no collision
4998
        eth_phy.carrier_sense_tx_fd_detect(0);
4999
        eth_phy.collision(0);
5000
      end
5001
      2'h1: // Interrupt is not generated
5002
      begin
5003
        // set_tx_bd enable interrupt generation
5004
        // mask interrupts
5005
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5006
        // detect carrier sense in FD and no collision
5007
        eth_phy.carrier_sense_tx_fd_detect(1);
5008
        eth_phy.collision(0);
5009
      end
5010
      2'h2: // Interrupt is not generated
5011
      begin
5012
        // set_tx_bd disable the interrupt generation
5013
        // unmask interrupts
5014
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
5015
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5016
        // not detect carrier sense in FD and set collision
5017
        eth_phy.carrier_sense_tx_fd_detect(0);
5018
        eth_phy.collision(1);
5019
      end
5020
      default: // 2'h3: // Interrupt is not generated
5021
      begin
5022
        // set_tx_bd disable the interrupt generation
5023
        // mask interrupts
5024
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5025
        // detect carrier sense in FD and set collision
5026
        eth_phy.carrier_sense_tx_fd_detect(1);
5027
        eth_phy.collision(1);
5028
      end
5029
      endcase
5030
      // first destination address on ethernet PHY
5031
      if (i_length[0] == 0)
5032
        eth_phy.set_tx_mem_addr(0);
5033
      else
5034
        eth_phy.set_tx_mem_addr(max_tmp);
5035
      // first 8 frames are transmitted with TX BD 0 (wrap bit on TX BD 0)
5036
      // number of all frames is 154 (146 without first 8)
5037
      if (num_of_frames < 8)
5038
      begin
5039
        case (i_length[1:0])
5040
        2'h0: // Interrupt is generated
5041
        begin
5042
          // enable interrupt generation
5043
          set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
5044
          // interrupts are unmasked
5045
        end
5046
        2'h1: // Interrupt is not generated
5047
        begin
5048
          // enable interrupt generation
5049
          set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
5050
          // interrupts are masked
5051
        end
5052
        2'h2: // Interrupt is not generated
5053
        begin
5054
          // disable interrupt generation
5055
          set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
5056
          // interrupts are unmasked
5057
        end
5058
        default: // 2'h3: // Interrupt is not generated
5059
        begin
5060
          // disable interrupt generation
5061
          set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
5062
          // interrupts are masked
5063
        end
5064
        endcase
5065
        // set wrap bit
5066
        set_tx_bd_wrap(0);
5067
      end
5068
      // after first 8 number of frames, 128 frames form TX BD 0 to 127 will be transmitted
5069
      else if ((num_of_frames - 8) == 0)
5070
      begin
5071
        tmp_len = i_length; // length of frame
5072
        tmp_bd_num = 0; // TX BD number
5073
        while (tmp_bd_num < 128) // (tmp_len <= (max_tmp - 4)) - this is the last frame
5074
        begin
5075
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5076
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5077
          if (tmp_len[0] == 0)
5078
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
5079
          else
5080
            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));
5081
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
5082
          if ((tmp_len + 4) < (min_tmp + 128))
5083
            tmp_len = tmp_len + 1;
5084
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5085
            tmp_len = 256;
5086
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5087
            tmp_len = tmp_len + 128;
5088
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
5089
            tmp_len = max_tmp - (4 + 16);
5090
          else if ((tmp_len + 4) >= (max_tmp - 16))
5091
            tmp_len = tmp_len + 1;
5092
          // set TX BD number
5093
          tmp_bd_num = tmp_bd_num + 1;
5094
        end
5095
        // set wrap bit
5096
        set_tx_bd_wrap(127);
5097
      end
5098
      // after 128 + first 8 number of frames, 19 frames form TX BD 0 to 18 will be transmitted
5099
      else if ((num_of_frames - 8) == 20) // 128
5100
      begin
5101
        tmp_len = tmp_len; // length of frame remaines from previous settings
5102
        tmp_bd_num = 0; // TX BD number
5103
        while (tmp_bd_num < 19) // (tmp_len <= (max_tmp - 4)) - this is the last frame
5104
        begin
5105
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5106
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5107
          if (tmp_len[0] == 0)
5108
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
5109
          else
5110
            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));
5111
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
5112
          if ((tmp_len + 4) < (min_tmp + 128))
5113
            tmp_len = tmp_len + 1;
5114
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5115
            tmp_len = 256;
5116
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5117
            tmp_len = tmp_len + 128;
5118
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
5119
            tmp_len = max_tmp - (4 + 16);
5120
          else if ((tmp_len + 4) >= (max_tmp - 16))
5121
            tmp_len = tmp_len + 1;
5122
          // set TX BD number
5123
          tmp_bd_num = tmp_bd_num + 1;
5124
        end
5125
      end
5126
      // set ready bit
5127
      if (num_of_frames < 8)
5128
        set_tx_bd_ready(0, 0);
5129
      else if ((num_of_frames - 8) < 128)
5130
        set_tx_bd_ready((num_of_frames - 8), (num_of_frames - 8));
5131
      else if ((num_of_frames - 136) < 19)
5132
        set_tx_bd_ready((num_of_frames - 136), (num_of_frames - 136));
5133
      // CHECK END OF TRANSMITION
5134
      #1 check_tx_bd(num_of_bd, data);
5135
      if (i_length < min_tmp) // just first four
5136
      begin
5137
        while (data[15] === 1)
5138
        begin
5139
          #1 check_tx_bd(num_of_bd, data);
5140
          @(posedge wb_clk);
5141
        end
5142
        repeat (1) @(posedge wb_clk);
5143
      end
5144
      else if (i_length > (max_tmp - 8)) // just last four
5145
      begin
5146
        tmp = 0;
5147
        wait (MTxEn === 1'b1); // start transmit
5148
        while (tmp < (i_length - 20))
5149
        begin
5150
          #1 tmp = tmp + 1;
5151
          @(posedge wb_clk);
5152
        end
5153
        #1 check_tx_bd(num_of_bd, data);
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
5162
      begin
5163
        wait (MTxEn === 1'b1); // start transmit
5164
        #1 check_tx_bd(num_of_bd, data);
5165
        if (data[15] !== 1)
5166
        begin
5167
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
5168
          fail = fail + 1;
5169
        end
5170
        wait (MTxEn === 1'b0); // end transmit
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
      // check length of a PACKET
5179
      if (eth_phy.tx_len != (i_length + 4))
5180
      begin
5181
        test_fail("Wrong length of the packet out from MAC");
5182
        fail = fail + 1;
5183
      end
5184
        // check transmitted TX packet data
5185
        if (i_length[0] == 0)
5186
        begin
5187
          check_tx_packet((`MEMORY_BASE + i_length[1:0]), 0, i_length, tmp);
5188
        end
5189
        else
5190
        begin
5191
          check_tx_packet(((`MEMORY_BASE + i_length[1:0]) + max_tmp), max_tmp, i_length, tmp);
5192
        end
5193
        if (tmp > 0)
5194
        begin
5195
          test_fail("Wrong data of the transmitted packet");
5196
          fail = fail + 1;
5197
        end
5198
        // check transmited TX packet CRC
5199
        if (i_length[0] == 0)
5200
          check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
5201
        else
5202
          check_tx_crc(max_tmp, i_length, 1'b0, tmp); // length without CRC
5203
        if (tmp > 0)
5204
        begin
5205
          test_fail("Wrong CRC of the transmitted packet");
5206
          fail = fail + 1;
5207
        end
5208
      // check WB INT signal
5209
      if (i_length[1:0] == 2'h0)
5210
      begin
5211
        if (wb_int !== 1'b1)
5212
        begin
5213
          `TIME; $display("*E WB INT signal should be set");
5214
          test_fail("WB INT signal should be set");
5215
          fail = fail + 1;
5216
        end
5217
      end
5218
      else
5219
      begin
5220
        if (wb_int !== 1'b0)
5221
        begin
5222
          `TIME; $display("*E WB INT signal should not be set");
5223
          test_fail("WB INT signal should not be set");
5224
          fail = fail + 1;
5225
        end
5226
      end
5227
      // check TX buffer descriptor of a packet
5228
      check_tx_bd(num_of_bd, data);
5229
      if (i_length[1] == 1'b0) // interrupt enabled
5230
      begin
5231
        if ( ((data[15:0] !== 16'h7800) && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
5232
             ((data[15:0] !== 16'h5800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
5233
        begin
5234
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
5235
          test_fail("TX buffer descriptor status is not correct");
5236
          fail = fail + 1;
5237
        end
5238
      end
5239
      else // interrupt not enabled
5240
      begin
5241
        if ( ((data[15:0] !== 16'h3800)  && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
5242
             ((data[15:0] !== 16'h1800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
5243
        begin
5244
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
5245
          test_fail("TX buffer descriptor status is not correct");
5246
          fail = fail + 1;
5247
        end
5248
      end
5249
      // clear first half of 8 frames from TX buffer descriptor 0
5250
      if (num_of_frames < 4)
5251
        clear_tx_bd(num_of_bd, num_of_bd);
5252
      // clear BD with wrap bit
5253
      if (num_of_frames == 140)
5254
        clear_tx_bd(127, 127);
5255
      // check interrupts
5256
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5257
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
5258
      begin
5259
        if ((data & `ETH_INT_TXB) !== 1'b1)
5260
        begin
5261
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
5262
          test_fail("Interrupt Transmit Buffer was not set");
5263
          fail = fail + 1;
5264
        end
5265
        if ((data & (~`ETH_INT_TXB)) !== 0)
5266
        begin
5267
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
5268
          test_fail("Other interrupts (except Transmit Buffer) were set");
5269
          fail = fail + 1;
5270
        end
5271
      end
5272
      else
5273
      begin
5274
        if (data !== 0)
5275
        begin
5276
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
5277
          test_fail("Any of interrupts (except Transmit Buffer) was set");
5278
          fail = fail + 1;
5279
        end
5280
      end
5281
      // clear interrupts
5282
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5283
      // check WB INT signal
5284
      if (wb_int !== 1'b0)
5285
      begin
5286
        test_fail("WB INT signal should not be set");
5287
        fail = fail + 1;
5288
      end
5289
      // INTERMEDIATE DISPLAYS
5290
      if ((i_length + 4) == (min_tmp + 7))
5291
      begin
5292
        // starting length is min_tmp, ending length is (min_tmp + 128)
5293
        $display("    pads appending to packets is NOT selected");
5294
        $display("    using only TX BD 0 out of 128 BDs assigned to TX (wrap at first BD - TX BD 0)");
5295
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
5296
                 min_tmp, (min_tmp + 7));
5297
        $display("    ->all packets were send from TX BD 0");
5298
        // set padding, remain the rest
5299
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5300
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5301
      end
5302
      else if ((i_length + 4) == (min_tmp + 128))
5303
      begin
5304
        // starting length is min_tmp, ending length is (min_tmp + 128)
5305
        $display("    pads appending to packets is NOT selected");
5306
        $display("    using all 128 BDs assigned to TX (wrap at 128th BD - TX BD 127)");
5307
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
5308
                 (min_tmp + 8), (min_tmp + 128));
5309
        $display("    ->packets were send from TX BD %0d to TX BD %0d respectively",
5310
                 1'b0, num_of_bd);
5311
        tmp_bd = num_of_bd + 1;
5312
        // set padding, remain the rest
5313
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5314
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5315
      end
5316
      else if ((i_length + 4) == (max_tmp - 16))
5317
      begin
5318
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
5319
        $display("    pads appending to packets is selected");
5320
        $display("    using all 128 BDs assigned to TX (wrap at 128th BD - TX BD 127)");
5321
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
5322
                 (min_tmp + 64 + 128), tmp_data);
5323
        if (tmp_bd > num_of_bd)
5324
          $display("    ->packets were send from TX BD %0d to TX BD 127 and from TX BD 0 to TX BD %0d respectively",
5325
                   tmp_bd, num_of_bd);
5326
        else
5327
          $display("    ->packets were send from TX BD %0d to TX BD %0d respectively",
5328
                   tmp_bd, num_of_bd);
5329
        tmp_bd = num_of_bd + 1;
5330
        // reset padding, remain the rest
5331
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
5332
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5333
      end
5334
      else if ((i_length + 4) == max_tmp)
5335
      begin
5336
        $display("    pads appending to packets is NOT 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 (MAXFL) are checked (length increasing by 1 byte)",
5339
                 (max_tmp - (4 + 16)), max_tmp);
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
      end
5347
      // set length (loop variable)
5348
      if ((i_length + 4) < (min_tmp + 128))
5349
        i_length = i_length + 1;
5350
      else if ( ((i_length + 4) == (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
5351
        i_length = 256;
5352
      else if ( ((i_length + 4) > (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
5353
      begin
5354
        i_length = i_length + 128;
5355
        tmp_data = i_length + 4; // last tmp_data is ending length
5356
      end
5357
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
5358
        i_length = max_tmp - (4 + 16);
5359
      else if ((i_length + 4) >= (max_tmp - 16))
5360
        i_length = i_length + 1;
5361
      else
5362
      begin
5363
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
5364
        #10 $stop;
5365
      end
5366
      // the number of frame transmitted
5367
      num_of_frames = num_of_frames + 1;
5368
      if ((num_of_frames <= 8) || ((num_of_frames - 8) == 128))
5369
        num_of_bd = 0;
5370
      else
5371
        num_of_bd = num_of_bd + 1;
5372
    end
5373
    // disable TX
5374
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5375
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5376
    @(posedge wb_clk);
5377
    if(fail == 0)
5378
      test_ok;
5379
    else
5380
      fail = 0;
5381
  end
5382
 
5383
 
5384
  ////////////////////////////////////////////////////////////////////
5385
  ////                                                            ////
5386
  ////  Test transmit packets form MINFL to MAXFL sizes at        ////
5387
  ////  maximum TX buffer decriptors ( 100Mbps ).                 ////
5388
  ////                                                            ////
5389
  ////////////////////////////////////////////////////////////////////
5390
  if (test_num == 5) // with and without padding
5391
  begin
5392
    // TEST 5: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 100Mbps )
5393
    test_name = "TEST 5: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 100Mbps )";
5394
    `TIME; $display("  TEST 5: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 100Mbps )");
5395
 
5396
    // reset MAC registers
5397
    hard_reset;
5398
    // reset MAC and MII LOGIC with soft reset
5399
    reset_mac;
5400
    reset_mii;
5401
    // set wb slave response
5402
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
5403
 
5404
    max_tmp = 0;
5405
    min_tmp = 0;
5406
    num_of_frames = 0;
5407
    num_of_bd = 0;
5408
    // set maximum TX buffer descriptors (128) - must be set before TX enable
5409
    wbm_write(`ETH_TX_BD_NUM, 32'h80, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5410
    // enable TX, set full-duplex mode, NO padding and CRC appending
5411
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
5412
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5413
    // prepare two packets of MAXFL length
5414
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5415
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
5416
    min_tmp = tmp[31:16];
5417
    st_data = 8'hA5;
5418
    set_tx_packet(`MEMORY_BASE, (max_tmp), st_data); // length without CRC
5419
    st_data = 8'h71;
5420
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp), st_data); // length without CRC
5421
    // check WB INT signal
5422
    if (wb_int !== 1'b0)
5423
    begin
5424
      test_fail("WB INT signal should not be set");
5425
      fail = fail + 1;
5426
    end
5427
 
5428
    // write to phy's control register for 100Mbps
5429
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
5430
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
5431
    speed = 100;
5432
 
5433
    i_length = (min_tmp - 4);
5434
    while (i_length <= (max_tmp - 4))
5435
    begin
5436
      // choose generating carrier sense and collision
5437
      case (i_length[1:0])
5438
      2'h0: // Interrupt is generated
5439
      begin
5440
        // Reset_tx_bd nable interrupt generation
5441
        // unmask interrupts
5442
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
5443
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5444
        // not detect carrier sense in FD and no collision
5445
        eth_phy.carrier_sense_tx_fd_detect(0);
5446
        eth_phy.collision(0);
5447
      end
5448
      2'h1: // Interrupt is not generated
5449
      begin
5450
        // set_tx_bd enable interrupt generation
5451
        // mask interrupts
5452
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5453
        // detect carrier sense in FD and no collision
5454
        eth_phy.carrier_sense_tx_fd_detect(1);
5455
        eth_phy.collision(0);
5456
      end
5457
      2'h2: // Interrupt is not generated
5458
      begin
5459
        // set_tx_bd disable the interrupt generation
5460
        // unmask interrupts
5461
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
5462
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5463
        // not detect carrier sense in FD and set collision
5464
        eth_phy.carrier_sense_tx_fd_detect(0);
5465
        eth_phy.collision(1);
5466
      end
5467
      default: // 2'h3: // Interrupt is not generated
5468
      begin
5469
        // set_tx_bd disable the interrupt generation
5470
        // mask interrupts
5471
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5472
        // detect carrier sense in FD and set collision
5473
        eth_phy.carrier_sense_tx_fd_detect(1);
5474
        eth_phy.collision(1);
5475
      end
5476
      endcase
5477
      // first destination address on ethernet PHY
5478
      if (i_length[0] == 0)
5479
        eth_phy.set_tx_mem_addr(0);
5480
      else
5481
        eth_phy.set_tx_mem_addr(max_tmp);
5482
      // first 8 frames are transmitted with TX BD 0 (wrap bit on TX BD 0)
5483
      // number of all frames is 154 (146 without first 8)
5484
      if (num_of_frames < 8)
5485
      begin
5486
        case (i_length[1:0])
5487
        2'h0: // Interrupt is generated
5488
        begin
5489
          // enable interrupt generation
5490
          set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
5491
          // interrupts are unmasked
5492
        end
5493
        2'h1: // Interrupt is not generated
5494
        begin
5495
          // enable interrupt generation
5496
          set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
5497
          // interrupts are masked
5498
        end
5499
        2'h2: // Interrupt is not generated
5500
        begin
5501
          // disable interrupt generation
5502
          set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
5503
          // interrupts are unmasked
5504
        end
5505
        default: // 2'h3: // Interrupt is not generated
5506
        begin
5507
          // disable interrupt generation
5508
          set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
5509
          // interrupts are masked
5510
        end
5511
        endcase
5512
        // set wrap bit
5513
        set_tx_bd_wrap(0);
5514
      end
5515
      // after first 8 number of frames, 128 frames form TX BD 0 to 127 will be transmitted
5516
      else if ((num_of_frames - 8) == 0)
5517
      begin
5518
        tmp_len = i_length; // length of frame
5519
        tmp_bd_num = 0; // TX BD number
5520
        while (tmp_bd_num < 128) // (tmp_len <= (max_tmp - 4)) - this is the last frame
5521
        begin
5522
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5523
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5524
          if (tmp_len[0] == 0)
5525
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
5526
          else
5527
            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));
5528
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
5529
          if ((tmp_len + 4) < (min_tmp + 128))
5530
            tmp_len = tmp_len + 1;
5531
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5532
            tmp_len = 256;
5533
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5534
            tmp_len = tmp_len + 128;
5535
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
5536
            tmp_len = max_tmp - (4 + 16);
5537
          else if ((tmp_len + 4) >= (max_tmp - 16))
5538
            tmp_len = tmp_len + 1;
5539
          // set TX BD number
5540
          tmp_bd_num = tmp_bd_num + 1;
5541
        end
5542
        // set wrap bit
5543
        set_tx_bd_wrap(127);
5544
      end
5545
      // after 128 + first 8 number of frames, 19 frames form TX BD 0 to 18 will be transmitted
5546
      else if ((num_of_frames - 8) == 20) // 128
5547
      begin
5548
        tmp_len = tmp_len; // length of frame remaines from previous settings
5549
        tmp_bd_num = 0; // TX BD number
5550
        while (tmp_bd_num < 19) // (tmp_len <= (max_tmp - 4)) - this is the last frame
5551
        begin
5552
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5553
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5554
          if (tmp_len[0] == 0)
5555
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
5556
          else
5557
            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));
5558
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
5559
          if ((tmp_len + 4) < (min_tmp + 128))
5560
            tmp_len = tmp_len + 1;
5561
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5562
            tmp_len = 256;
5563
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5564
            tmp_len = tmp_len + 128;
5565
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
5566
            tmp_len = max_tmp - (4 + 16);
5567
          else if ((tmp_len + 4) >= (max_tmp - 16))
5568
            tmp_len = tmp_len + 1;
5569
          // set TX BD number
5570
          tmp_bd_num = tmp_bd_num + 1;
5571
        end
5572
      end
5573
      // set ready bit
5574
      if (num_of_frames < 8)
5575
        set_tx_bd_ready(0, 0);
5576
      else if ((num_of_frames - 8) < 128)
5577
        set_tx_bd_ready((num_of_frames - 8), (num_of_frames - 8));
5578
      else if ((num_of_frames - 136) < 19)
5579
        set_tx_bd_ready((num_of_frames - 136), (num_of_frames - 136));
5580
      // CHECK END OF TRANSMITION
5581
      #1 check_tx_bd(num_of_bd, data);
5582
      if (i_length < min_tmp) // just first four
5583
      begin
5584
        while (data[15] === 1)
5585
        begin
5586
          #1 check_tx_bd(num_of_bd, data);
5587
          @(posedge wb_clk);
5588
        end
5589
        repeat (1) @(posedge wb_clk);
5590
      end
5591
      else if (i_length > (max_tmp - 8)) // just last four
5592
      begin
5593
        tmp = 0;
5594
        wait (MTxEn === 1'b1); // start transmit
5595
        while (tmp < (i_length - 20))
5596
        begin
5597
          #1 tmp = tmp + 1;
5598
          @(posedge wb_clk);
5599
        end
5600
        #1 check_tx_bd(num_of_bd, data);
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
5609
      begin
5610
        wait (MTxEn === 1'b1); // start transmit
5611
        #1 check_tx_bd(num_of_bd, data);
5612
        if (data[15] !== 1)
5613
        begin
5614
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
5615
          fail = fail + 1;
5616
        end
5617
        wait (MTxEn === 1'b0); // end transmit
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
      // check length of a PACKET
5626
      if (eth_phy.tx_len != (i_length + 4))
5627
      begin
5628
        test_fail("Wrong length of the packet out from MAC");
5629
        fail = fail + 1;
5630
      end
5631
      // checking in the following if statement is performed only for first and last 64 lengths
5632
        // check transmitted TX packet data
5633
        if (i_length[0] == 0)
5634
        begin
5635
          check_tx_packet((`MEMORY_BASE + i_length[1:0]), 0, i_length, tmp);
5636
        end
5637
        else
5638
        begin
5639
          check_tx_packet(((`MEMORY_BASE + i_length[1:0]) + max_tmp), max_tmp, i_length, tmp);
5640
        end
5641
        if (tmp > 0)
5642
        begin
5643
          test_fail("Wrong data of the transmitted packet");
5644
          fail = fail + 1;
5645
        end
5646
        // check transmited TX packet CRC
5647
        if (i_length[0] == 0)
5648
          check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
5649
        else
5650
          check_tx_crc(max_tmp, i_length, 1'b0, tmp); // length without CRC
5651
        if (tmp > 0)
5652
        begin
5653
          test_fail("Wrong CRC of the transmitted packet");
5654
          fail = fail + 1;
5655
        end
5656
      // check WB INT signal
5657
      if (i_length[1:0] == 2'h0)
5658
      begin
5659
        if (wb_int !== 1'b1)
5660
        begin
5661
          `TIME; $display("*E WB INT signal should be set");
5662
          test_fail("WB INT signal should be set");
5663
          fail = fail + 1;
5664
        end
5665
      end
5666
      else
5667
      begin
5668
        if (wb_int !== 1'b0)
5669
        begin
5670
          `TIME; $display("*E WB INT signal should not be set");
5671
          test_fail("WB INT signal should not be set");
5672
          fail = fail + 1;
5673
        end
5674
      end
5675
      // check TX buffer descriptor of a packet
5676
      check_tx_bd(num_of_bd, data);
5677
      if (i_length[1] == 1'b0) // interrupt enabled
5678
      begin
5679
        if ( ((data[15:0] !== 16'h7800) && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
5680
             ((data[15:0] !== 16'h5800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
5681
        begin
5682
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
5683
          test_fail("TX buffer descriptor status is not correct");
5684
          fail = fail + 1;
5685
        end
5686
      end
5687
      else // interrupt not enabled
5688
      begin
5689
        if ( ((data[15:0] !== 16'h3800)  && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
5690
             ((data[15:0] !== 16'h1800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
5691
        begin
5692
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
5693
          test_fail("TX buffer descriptor status is not correct");
5694
          fail = fail + 1;
5695
        end
5696
      end
5697
      // clear first half of 8 frames from TX buffer descriptor 0
5698
      if (num_of_frames < 4)
5699
        clear_tx_bd(num_of_bd, num_of_bd);
5700
      // clear BD with wrap bit
5701
      if (num_of_frames == 140)
5702
        clear_tx_bd(127, 127);
5703
      // check interrupts
5704
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5705
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
5706
      begin
5707
        if ((data & `ETH_INT_TXB) !== 1'b1)
5708
        begin
5709
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
5710
          test_fail("Interrupt Transmit Buffer was not set");
5711
          fail = fail + 1;
5712
        end
5713
        if ((data & (~`ETH_INT_TXB)) !== 0)
5714
        begin
5715
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
5716
          test_fail("Other interrupts (except Transmit Buffer) were set");
5717
          fail = fail + 1;
5718
        end
5719
      end
5720
      else
5721
      begin
5722
        if (data !== 0)
5723
        begin
5724
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
5725
          test_fail("Any of interrupts (except Transmit Buffer) was set");
5726
          fail = fail + 1;
5727
        end
5728
      end
5729
      // clear interrupts
5730
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5731
      // check WB INT signal
5732
      if (wb_int !== 1'b0)
5733
      begin
5734
        test_fail("WB INT signal should not be set");
5735
        fail = fail + 1;
5736
      end
5737
      // INTERMEDIATE DISPLAYS
5738
      if ((i_length + 4) == (min_tmp + 7))
5739
      begin
5740
        // starting length is min_tmp, ending length is (min_tmp + 128)
5741
        $display("    pads appending to packets is NOT selected");
5742
        $display("    using only TX BD 0 out of 128 BDs assigned to TX (wrap at first BD - TX BD 0)");
5743
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
5744
                 min_tmp, (min_tmp + 7));
5745
        $display("    ->all packets were send from TX BD 0");
5746
        // set padding, remain the rest
5747
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5748
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5749
      end
5750
      else if ((i_length + 4) == (min_tmp + 128))
5751
      begin
5752
        // starting length is min_tmp, ending length is (min_tmp + 128)
5753
        $display("    pads appending to packets is NOT selected");
5754
        $display("    using all 128 BDs assigned to TX (wrap at 128th BD - TX BD 127)");
5755
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
5756
                 (min_tmp + 8), (min_tmp + 128));
5757
        $display("    ->packets were send from TX BD %0d to TX BD %0d respectively",
5758
                 1'b0, num_of_bd);
5759
        tmp_bd = num_of_bd + 1;
5760
        // set padding, remain the rest
5761
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5762
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5763
      end
5764
      else if ((i_length + 4) == (max_tmp - 16))
5765
      begin
5766
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
5767
        $display("    pads appending to packets is selected");
5768
        $display("    using all 128 BDs assigned to TX (wrap at 128th BD - TX BD 127)");
5769
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
5770
                 (min_tmp + 64 + 128), tmp_data);
5771
        if (tmp_bd > num_of_bd)
5772
          $display("    ->packets were send from TX BD %0d to TX BD 127 and from TX BD 0 to TX BD %0d respectively",
5773
                   tmp_bd, num_of_bd);
5774
        else
5775
          $display("    ->packets were send from TX BD %0d to TX BD %0d respectively",
5776
                   tmp_bd, num_of_bd);
5777
        tmp_bd = num_of_bd + 1;
5778
        // reset padding, remain the rest
5779
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
5780
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5781
      end
5782
      else if ((i_length + 4) == max_tmp)
5783
      begin
5784
        $display("    pads appending to packets is NOT 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 (MAXFL) are checked (length increasing by 1 byte)",
5787
                 (max_tmp - (4 + 16)), max_tmp);
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
      end
5795
      // set length (loop variable)
5796
      if ((i_length + 4) < (min_tmp + 128))
5797
        i_length = i_length + 1;
5798
      else if ( ((i_length + 4) == (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
5799
        i_length = 256;
5800
      else if ( ((i_length + 4) > (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
5801
      begin
5802
        i_length = i_length + 128;
5803
        tmp_data = i_length + 4; // last tmp_data is ending length
5804
      end
5805
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
5806
        i_length = max_tmp - (4 + 16);
5807
      else if ((i_length + 4) >= (max_tmp - 16))
5808
        i_length = i_length + 1;
5809
      else
5810
      begin
5811
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
5812
        #10 $stop;
5813
      end
5814
      // the number of frame transmitted
5815
      num_of_frames = num_of_frames + 1;
5816
      if ((num_of_frames <= 8) || ((num_of_frames - 8) == 128))
5817
        num_of_bd = 0;
5818
      else
5819
        num_of_bd = num_of_bd + 1;
5820
    end
5821
    // disable TX
5822
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5823
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5824
    @(posedge wb_clk);
5825
    if(fail == 0)
5826
      test_ok;
5827
    else
5828
      fail = 0;
5829
  end
5830
 
5831
 
5832
  ////////////////////////////////////////////////////////////////////
5833
  ////                                                            ////
5834
  ////  Test transmit packets form 0 to (MINFL - 1) sizes at      ////
5835
  ////  8 TX buffer decriptors ( 10Mbps ).                        ////
5836
  ////                                                            ////
5837
  ////////////////////////////////////////////////////////////////////
5838
  if (test_num == 6) // 
5839
  begin
5840
    // TEST 6: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )
5841
    test_name = "TEST 6: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )";
5842
    `TIME; $display("  TEST 6: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )");
5843
 
5844
    // reset MAC registers
5845
    hard_reset;
5846
    // reset MAC and MII LOGIC with soft reset
5847
    reset_mac;
5848
    reset_mii;
5849
    // set wb slave response
5850
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
5851
 
5852
    max_tmp = 0;
5853
    min_tmp = 0;
5854
    // set 8 TX buffer descriptors - must be set before TX enable
5855
    wbm_write(`ETH_TX_BD_NUM, 32'h8, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5856
    // enable TX, set full-duplex mode, padding and CRC appending
5857
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
5858
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5859
    // prepare two packets of MAXFL length
5860
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5861
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
5862
    min_tmp = tmp[31:16];
5863
    st_data = 8'h12;
5864
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
5865
    st_data = 8'h34;
5866
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp - 4), st_data); // length without CRC
5867
    // check WB INT signal
5868
    if (wb_int !== 1'b0)
5869
    begin
5870
      test_fail("WB INT signal should not be set");
5871
      fail = fail + 1;
5872
    end
5873
 
5874
    // write to phy's control register for 10Mbps
5875
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
5876
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
5877
    speed = 10;
5878
 
5879
    frame_started = 0;
5880
    num_of_frames = 0;
5881
    num_of_bd = 0;
5882
    i_length = 0; // 0;
5883
    while (i_length < 70) // (min_tmp - 4))
5884
    begin
5885
      #1;
5886
      // choose generating carrier sense and collision
5887
      case (i_length[1:0])
5888
      2'h0: // Interrupt is generated
5889
      begin
5890
        // Reset_tx_bd nable interrupt generation
5891
        // unmask interrupts
5892
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
5893
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5894
        // not detect carrier sense in FD and no collision
5895
        eth_phy.carrier_sense_tx_fd_detect(0);
5896
        eth_phy.collision(0);
5897
      end
5898
      2'h1: // Interrupt is not generated
5899
      begin
5900
        // set_tx_bd enable interrupt generation
5901
        // mask interrupts
5902
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5903
        // detect carrier sense in FD and no collision
5904
        eth_phy.carrier_sense_tx_fd_detect(1);
5905
        eth_phy.collision(0);
5906
      end
5907
      2'h2: // Interrupt is not generated
5908
      begin
5909
        // set_tx_bd disable the interrupt generation
5910
        // unmask interrupts
5911
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
5912
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5913
        // not detect carrier sense in FD and set collision
5914
        eth_phy.carrier_sense_tx_fd_detect(0);
5915
        eth_phy.collision(1);
5916
      end
5917
      default: // 2'h3: // Interrupt is not generated
5918
      begin
5919
        // set_tx_bd disable the interrupt generation
5920
        // mask interrupts
5921
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5922
        // detect carrier sense in FD and set collision
5923
        eth_phy.carrier_sense_tx_fd_detect(1);
5924
        eth_phy.collision(1);
5925
      end
5926
      endcase
5927
      #1;
5928
      // first destination address on ethernet PHY
5929
      eth_phy.set_tx_mem_addr(num_of_frames * 16);
5930
      // SET packets and wrap bit
5931
      // num_of_frames <= 9 => wrap set to TX BD 0
5932
      if (num_of_frames <= 9)
5933
      begin
5934
        tmp_len = i_length; // length of frame
5935
        tmp_bd_num = 0; // TX BD number
5936
        // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5937
        // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5938
        if (tmp_len[0] == 0)
5939
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
5940
        else
5941
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
5942
        // set wrap bit
5943
        set_tx_bd_wrap(0);
5944
      end
5945
      // 10 <= num_of_frames < 18 => wrap set to TX BD 3
5946
      else if ((num_of_frames == 10) || (num_of_frames == 14))
5947
      begin
5948
        tmp_len = i_length; // length of frame
5949
        tmp_bd_num = 0; // TX BD number
5950
        while (tmp_bd_num < 4) //
5951
        begin
5952
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5953
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5954
          if (tmp_len[0] == 0)
5955
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
5956
          else
5957
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
5958
          tmp_len = tmp_len + 1;
5959
          // set TX BD number
5960
          tmp_bd_num = tmp_bd_num + 1;
5961
        end
5962
        // set wrap bit
5963
        set_tx_bd_wrap(3);
5964
      end
5965
      // 18 <= num_of_frames < 28 => wrap set to TX BD 4
5966
      else if ((num_of_frames == 18) || (num_of_frames == 23))
5967
      begin
5968
        tmp_len = i_length; // length of frame
5969
        tmp_bd_num = 0; // TX BD number
5970
        while (tmp_bd_num < 5) //
5971
        begin
5972
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5973
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5974
          if (tmp_len[0] == 0)
5975
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
5976
          else
5977
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
5978
          tmp_len = tmp_len + 1;
5979
          // set TX BD number
5980
          tmp_bd_num = tmp_bd_num + 1;
5981
        end
5982
        // set wrap bit
5983
        set_tx_bd_wrap(4);
5984
      end
5985
      // 28 <= num_of_frames < 40 => wrap set to TX BD 5
5986
      else if ((num_of_frames == 28) || (num_of_frames == 34))
5987
      begin
5988
        tmp_len = i_length; // length of frame
5989
        tmp_bd_num = 0; // TX BD number
5990
        while (tmp_bd_num < 6) //
5991
        begin
5992
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5993
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5994
          if (tmp_len[0] == 0)
5995
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
5996
          else
5997
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
5998
          tmp_len = tmp_len + 1;
5999
          // set TX BD number
6000
          tmp_bd_num = tmp_bd_num + 1;
6001
        end
6002
        // set wrap bit
6003
        set_tx_bd_wrap(5);
6004
      end
6005
      // 40 <= num_of_frames < 54 => wrap set to TX BD 6
6006
      else if ((num_of_frames == 40) || (num_of_frames == 47))
6007
      begin
6008
        tmp_len = i_length; // length of frame
6009
        tmp_bd_num = 0; // TX BD number
6010
        while (tmp_bd_num < 7) //
6011
        begin
6012
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6013
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6014
          if (tmp_len[0] == 0)
6015
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6016
          else
6017
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6018
          tmp_len = tmp_len + 1;
6019
          // set TX BD number
6020
          tmp_bd_num = tmp_bd_num + 1;
6021
        end
6022
        // set wrap bit
6023
        set_tx_bd_wrap(6);
6024
      end
6025
      // 54 <= num_of_frames < 70 => wrap set to TX BD 7
6026
      else if ((num_of_frames == 54) || (num_of_frames == 62))
6027
      begin
6028
        tmp_len = i_length; // length of frame
6029
        tmp_bd_num = 0; // TX BD number
6030
        while (tmp_bd_num < 8) //
6031
        begin
6032
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6033
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6034
          if (tmp_len[0] == 0)
6035
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6036
          else
6037
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6038
          tmp_len = tmp_len + 1;
6039
          // set TX BD number
6040
          tmp_bd_num = tmp_bd_num + 1;
6041
        end
6042
        // set wrap bit
6043
        set_tx_bd_wrap(7);
6044
      end
6045
      #1;
6046
      // SET ready bit
6047
      if (num_of_frames < 10)
6048
        set_tx_bd_ready(0, 0);
6049
      else if (num_of_frames < 14)
6050
        set_tx_bd_ready((num_of_frames - 10), (num_of_frames - 10));
6051
      else if (num_of_frames < 18)
6052
        set_tx_bd_ready((num_of_frames - 14), (num_of_frames - 14));
6053
      else if (num_of_frames < 23)
6054
        set_tx_bd_ready((num_of_frames - 18), (num_of_frames - 18));
6055
      else if (num_of_frames < 28)
6056
        set_tx_bd_ready((num_of_frames - 23), (num_of_frames - 23));
6057
      else if (num_of_frames < 34)
6058
        set_tx_bd_ready((num_of_frames - 28), (num_of_frames - 28));
6059
      else if (num_of_frames < 40)
6060
        set_tx_bd_ready((num_of_frames - 34), (num_of_frames - 34));
6061
      else if (num_of_frames < 47)
6062
        set_tx_bd_ready((num_of_frames - 40), (num_of_frames - 40));
6063
      else if (num_of_frames < 54)
6064
        set_tx_bd_ready((num_of_frames - 47), (num_of_frames - 47));
6065
      else if (num_of_frames < 62)
6066
        set_tx_bd_ready((num_of_frames - 54), (num_of_frames - 54));
6067
      else if (num_of_frames < 70)
6068
        set_tx_bd_ready((num_of_frames - 62), (num_of_frames - 62));
6069
      // CHECK END OF TRANSMITION
6070
      frame_started = 0;
6071
      if (num_of_frames >= 5)
6072
        #1 check_tx_bd(num_of_bd, data);
6073
      fork
6074
      begin: fr_st
6075
        wait (MTxEn === 1'b1); // start transmit
6076
        frame_started = 1;
6077
      end
6078
      begin
6079
        repeat (30) @(posedge mtx_clk);
6080
        if (num_of_frames < 5)
6081
        begin
6082
          if (frame_started == 1)
6083
          begin
6084
            `TIME; $display("*E Frame should NOT start!");
6085
          end
6086
          disable fr_st;
6087
        end
6088
        else
6089
        begin
6090
          if (frame_started == 0)
6091
          begin
6092
            `TIME; $display("*W Frame should start!");
6093
            disable fr_st;
6094
          end
6095
        end
6096
      end
6097
      join
6098
      // check packets larger than 4 bytes
6099
      if (num_of_frames >= 5)
6100
      begin
6101
        wait (MTxEn === 1'b0); // end transmit
6102
        while (data[15] === 1)
6103
        begin
6104
          #1 check_tx_bd(num_of_bd, data);
6105
          @(posedge wb_clk);
6106
        end
6107
        repeat (1) @(posedge wb_clk);
6108
        // check length of a PACKET
6109
        if (i_length <= (min_tmp - 4))
6110
        begin
6111
          if (eth_phy.tx_len != min_tmp)
6112
          begin
6113
            test_fail("Wrong length of the packet out from MAC");
6114
            fail = fail + 1;
6115
          end
6116
        end
6117
        else
6118
        begin
6119
          if (eth_phy.tx_len != (i_length + 4))
6120
          begin
6121
            test_fail("Wrong length of the packet out from MAC");
6122
            fail = fail + 1;
6123
          end
6124
        end
6125
        // check transmitted TX packet data
6126
        if (i_length[0] == 0)
6127
        begin
6128
          #1 check_tx_packet(`MEMORY_BASE, (num_of_frames * 16), i_length, tmp);
6129
        end
6130
        else
6131
        begin
6132
          #1 check_tx_packet((`MEMORY_BASE + max_tmp), (num_of_frames * 16), i_length, tmp);
6133
        end
6134
        if (tmp > 0)
6135
        begin
6136
          test_fail("Wrong data of the transmitted packet");
6137
          fail = fail + 1;
6138
        end
6139
        // check transmited TX packet CRC
6140
        if (num_of_frames < (min_tmp - 4))
6141
          #1 check_tx_crc((num_of_frames * 16), (min_tmp - 4), 1'b0, tmp); // length without CRC
6142
        else
6143
          #1 check_tx_crc((num_of_frames * 16), i_length, 1'b0, tmp); // length without CRC
6144
        if (tmp > 0)
6145
        begin
6146
          test_fail("Wrong CRC of the transmitted packet");
6147
          fail = fail + 1;
6148
        end
6149
      end
6150
      // check WB INT signal
6151
      if ((i_length[1:0] == 2'h0) && (num_of_frames >= 5))
6152
      begin
6153
        if (wb_int !== 1'b1)
6154
        begin
6155
          `TIME; $display("*E WB INT signal should be set");
6156
          test_fail("WB INT signal should be set");
6157
          fail = fail + 1;
6158
        end
6159
      end
6160
      else
6161
      begin
6162
        if (wb_int !== 1'b0)
6163
        begin
6164
          `TIME; $display("*E WB INT signal should not be set");
6165
          test_fail("WB INT signal should not be set");
6166
          fail = fail + 1;
6167
        end
6168
      end
6169
      // check TX buffer descriptor of a packet
6170
      check_tx_bd(num_of_bd, data);
6171
      if (num_of_frames >= 5)
6172
      begin
6173
        if (i_length[1] == 1'b0) // interrupt enabled
6174
        begin
6175
          if ( (data[15:0] !== 16'h7800) && // wrap bit
6176
               (data[15:0] !== 16'h5800) ) // without wrap bit
6177
          begin
6178
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6179
            test_fail("TX buffer descriptor status is not correct");
6180
            fail = fail + 1;
6181
          end
6182
        end
6183
        else // interrupt not enabled
6184
        begin
6185
          if ( (data[15:0] !== 16'h3800) && // wrap bit
6186
               (data[15:0] !== 16'h1800) ) // without wrap bit
6187
          begin
6188
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6189
            test_fail("TX buffer descriptor status is not correct");
6190
            fail = fail + 1;
6191
          end
6192
        end
6193
      end
6194
      else
6195
      begin
6196
        if (data[15] !== 1'b1)
6197
        begin
6198
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6199
          test_fail("TX buffer descriptor status is not correct");
6200
          fail = fail + 1;
6201
        end
6202
      end
6203
      // clear TX BD with wrap bit
6204
      if (num_of_frames == 63)
6205
        clear_tx_bd(16, 16);
6206
      // check interrupts
6207
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6208
      if ( ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1)) && (num_of_frames >= 5) )
6209
      begin
6210
        if ((data & `ETH_INT_TXB) !== 1'b1)
6211
        begin
6212
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
6213
          test_fail("Interrupt Transmit Buffer was not set");
6214
          fail = fail + 1;
6215
        end
6216
        if ((data & (~`ETH_INT_TXB)) !== 0)
6217
        begin
6218
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
6219
          test_fail("Other interrupts (except Transmit Buffer) were set");
6220
          fail = fail + 1;
6221
        end
6222
      end
6223
      else
6224
      begin
6225
        if (data !== 0)
6226
        begin
6227
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
6228
          test_fail("Any of interrupts (except Transmit Buffer) was set");
6229
          fail = fail + 1;
6230
        end
6231
      end
6232
      // clear interrupts
6233
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6234
      // check WB INT signal
6235
      if (wb_int !== 1'b0)
6236
      begin
6237
        test_fail("WB INT signal should not be set");
6238
        fail = fail + 1;
6239
      end
6240
      // INTERMEDIATE DISPLAYS
6241
      if (i_length == 3)
6242
      begin
6243
        $display("    pads appending to packets is selected");
6244
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
6245
        $display("    ->packets with lengths from %0d to %0d are not transmitted (length increasing by 1 byte)",
6246
                 0, 3);
6247
      end
6248
      else if (i_length == 9)
6249
      begin
6250
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
6251
        $display("    ->packet with length 4 is not transmitted (length increasing by 1 byte)");
6252
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6253
                 5, 9);
6254
      end
6255
      else if (i_length == 17)
6256
      begin
6257
        $display("    using 4 BDs out of 8 BDs assigned to TX (wrap at 4th BD - TX BD 3)");
6258
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6259
                 10, 17);
6260
      end
6261
      else if (i_length == 27)
6262
      begin
6263
        $display("    using 5 BDs out of 8 BDs assigned to TX (wrap at 5th BD - TX BD 4)");
6264
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6265
                 18, 27);
6266
      end
6267
      else if (i_length == 40)
6268
      begin
6269
        $display("    using 6 BDs out of 8 BDs assigned to TX (wrap at 6th BD - TX BD 5)");
6270
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6271
                 28, 40);
6272
      end
6273
      else if (i_length == 54)
6274
      begin
6275
        $display("    using 7 BDs out of 8 BDs assigned to TX (wrap at 7th BD - TX BD 6)");
6276
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6277
                 41, 54);
6278
      end
6279
      else if (i_length == 69)
6280
      begin
6281
        $display("    using 8 BDs out of 8 BDs assigned to TX (wrap at 8th BD - TX BD 7)");
6282
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6283
                 55, 69);
6284
      end
6285
      // set length (loop variable)
6286
      i_length = i_length + 1;
6287
      // the number of frame transmitted
6288
      num_of_frames = num_of_frames + 1;
6289
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
6290
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
6291
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
6292
          (num_of_frames == 54) || (num_of_frames == 62))
6293
        num_of_bd = 0;
6294
      else
6295
        num_of_bd = num_of_bd + 1;
6296
    end
6297
    // disable TX
6298
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
6299
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6300
    @(posedge wb_clk);
6301
    if(fail == 0)
6302
      test_ok;
6303
    else
6304
      fail = 0;
6305
  end
6306
 
6307
 
6308
  ////////////////////////////////////////////////////////////////////
6309
  ////                                                            ////
6310
  ////  Test transmit packets form 0 to (MINFL - 1) sizes at      ////
6311
  ////  8 TX buffer decriptors ( 100Mbps ).                       ////
6312
  ////                                                            ////
6313
  ////////////////////////////////////////////////////////////////////
6314
  if (test_num == 7) // 
6315
  begin
6316
    // TEST 7: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )
6317
    test_name = "TEST 7: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )";
6318
    `TIME; $display("  TEST 7: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )");
6319
 
6320
    // reset MAC registers
6321
    hard_reset;
6322
    // reset MAC and MII LOGIC with soft reset
6323
    reset_mac;
6324
    reset_mii;
6325
    // set wb slave response
6326
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
6327
 
6328
    max_tmp = 0;
6329
    min_tmp = 0;
6330
    // set 8 TX buffer descriptors - must be set before TX enable
6331
    wbm_write(`ETH_TX_BD_NUM, 32'h8, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6332
    // enable TX, set full-duplex mode, padding and CRC appending
6333
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
6334
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6335
    // prepare two packets of MAXFL length
6336
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6337
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
6338
    min_tmp = tmp[31:16];
6339
    st_data = 8'h12;
6340
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
6341
    st_data = 8'h34;
6342
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp - 4), st_data); // length without CRC
6343
    // check WB INT signal
6344
    if (wb_int !== 1'b0)
6345
    begin
6346
      test_fail("WB INT signal should not be set");
6347
      fail = fail + 1;
6348
    end
6349
 
6350
    // write to phy's control register for 100Mbps
6351
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
6352
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
6353
    speed = 100;
6354
 
6355
    frame_started = 0;
6356
    num_of_frames = 0;
6357
    num_of_bd = 0;
6358
    i_length = 0; // 0;
6359
    while (i_length < 70) // (min_tmp - 4))
6360
    begin
6361
      #1;
6362
      // choose generating carrier sense and collision
6363
      case (i_length[1:0])
6364
      2'h0: // Interrupt is generated
6365
      begin
6366
        // Reset_tx_bd nable interrupt generation
6367
        // unmask interrupts
6368
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
6369
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6370
        // not detect carrier sense in FD and no collision
6371
        eth_phy.carrier_sense_tx_fd_detect(0);
6372
        eth_phy.collision(0);
6373
      end
6374
      2'h1: // Interrupt is not generated
6375
      begin
6376
        // set_tx_bd enable interrupt generation
6377
        // mask interrupts
6378
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6379
        // detect carrier sense in FD and no collision
6380
        eth_phy.carrier_sense_tx_fd_detect(1);
6381
        eth_phy.collision(0);
6382
      end
6383
      2'h2: // Interrupt is not generated
6384
      begin
6385
        // set_tx_bd disable the interrupt generation
6386
        // unmask interrupts
6387
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
6388
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6389
        // not detect carrier sense in FD and set collision
6390
        eth_phy.carrier_sense_tx_fd_detect(0);
6391
        eth_phy.collision(1);
6392
      end
6393
      default: // 2'h3: // Interrupt is not generated
6394
      begin
6395
        // set_tx_bd disable the interrupt generation
6396
        // mask interrupts
6397
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6398
        // detect carrier sense in FD and set collision
6399
        eth_phy.carrier_sense_tx_fd_detect(1);
6400
        eth_phy.collision(1);
6401
      end
6402
      endcase
6403
      #1;
6404
      // first destination address on ethernet PHY
6405
      eth_phy.set_tx_mem_addr(num_of_frames * 16);
6406
      // SET packets and wrap bit
6407
      // num_of_frames <= 9 => wrap set to TX BD 0
6408
      if (num_of_frames <= 9)
6409
      begin
6410
        tmp_len = i_length; // length of frame
6411
        tmp_bd_num = 0; // TX BD number
6412
        // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6413
        // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6414
        if (tmp_len[0] == 0)
6415
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6416
        else
6417
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6418
        // set wrap bit
6419
        set_tx_bd_wrap(0);
6420
      end
6421
      // 10 <= num_of_frames < 18 => wrap set to TX BD 3
6422
      else if ((num_of_frames == 10) || (num_of_frames == 14))
6423
      begin
6424
        tmp_len = i_length; // length of frame
6425
        tmp_bd_num = 0; // TX BD number
6426
        while (tmp_bd_num < 4) //
6427
        begin
6428
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6429
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6430
          if (tmp_len[0] == 0)
6431
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6432
          else
6433
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6434
          tmp_len = tmp_len + 1;
6435
          // set TX BD number
6436
          tmp_bd_num = tmp_bd_num + 1;
6437
        end
6438
        // set wrap bit
6439
        set_tx_bd_wrap(3);
6440
      end
6441
      // 18 <= num_of_frames < 28 => wrap set to TX BD 4
6442
      else if ((num_of_frames == 18) || (num_of_frames == 23))
6443
      begin
6444
        tmp_len = i_length; // length of frame
6445
        tmp_bd_num = 0; // TX BD number
6446
        while (tmp_bd_num < 5) //
6447
        begin
6448
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6449
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6450
          if (tmp_len[0] == 0)
6451
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6452
          else
6453
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6454
          tmp_len = tmp_len + 1;
6455
          // set TX BD number
6456
          tmp_bd_num = tmp_bd_num + 1;
6457
        end
6458
        // set wrap bit
6459
        set_tx_bd_wrap(4);
6460
      end
6461
      // 28 <= num_of_frames < 40 => wrap set to TX BD 5
6462
      else if ((num_of_frames == 28) || (num_of_frames == 34))
6463
      begin
6464
        tmp_len = i_length; // length of frame
6465
        tmp_bd_num = 0; // TX BD number
6466
        while (tmp_bd_num < 6) //
6467
        begin
6468
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6469
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6470
          if (tmp_len[0] == 0)
6471
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6472
          else
6473
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6474
          tmp_len = tmp_len + 1;
6475
          // set TX BD number
6476
          tmp_bd_num = tmp_bd_num + 1;
6477
        end
6478
        // set wrap bit
6479
        set_tx_bd_wrap(5);
6480
      end
6481
      // 40 <= num_of_frames < 54 => wrap set to TX BD 6
6482
      else if ((num_of_frames == 40) || (num_of_frames == 47))
6483
      begin
6484
        tmp_len = i_length; // length of frame
6485
        tmp_bd_num = 0; // TX BD number
6486
        while (tmp_bd_num < 7) //
6487
        begin
6488
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6489
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6490
          if (tmp_len[0] == 0)
6491
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6492
          else
6493
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6494
          tmp_len = tmp_len + 1;
6495
          // set TX BD number
6496
          tmp_bd_num = tmp_bd_num + 1;
6497
        end
6498
        // set wrap bit
6499
        set_tx_bd_wrap(6);
6500
      end
6501
      // 54 <= num_of_frames < 70 => wrap set to TX BD 7
6502
      else if ((num_of_frames == 54) || (num_of_frames == 62))
6503
      begin
6504
        tmp_len = i_length; // length of frame
6505
        tmp_bd_num = 0; // TX BD number
6506
        while (tmp_bd_num < 8) //
6507
        begin
6508
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6509
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6510
          if (tmp_len[0] == 0)
6511
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6512
          else
6513
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6514
          tmp_len = tmp_len + 1;
6515
          // set TX BD number
6516
          tmp_bd_num = tmp_bd_num + 1;
6517
        end
6518
        // set wrap bit
6519
        set_tx_bd_wrap(7);
6520
      end
6521
      #1;
6522
      // SET ready bit
6523
      if (num_of_frames < 10)
6524
        set_tx_bd_ready(0, 0);
6525
      else if (num_of_frames < 14)
6526
        set_tx_bd_ready((num_of_frames - 10), (num_of_frames - 10));
6527
      else if (num_of_frames < 18)
6528
        set_tx_bd_ready((num_of_frames - 14), (num_of_frames - 14));
6529
      else if (num_of_frames < 23)
6530
        set_tx_bd_ready((num_of_frames - 18), (num_of_frames - 18));
6531
      else if (num_of_frames < 28)
6532
        set_tx_bd_ready((num_of_frames - 23), (num_of_frames - 23));
6533
      else if (num_of_frames < 34)
6534
        set_tx_bd_ready((num_of_frames - 28), (num_of_frames - 28));
6535
      else if (num_of_frames < 40)
6536
        set_tx_bd_ready((num_of_frames - 34), (num_of_frames - 34));
6537
      else if (num_of_frames < 47)
6538
        set_tx_bd_ready((num_of_frames - 40), (num_of_frames - 40));
6539
      else if (num_of_frames < 54)
6540
        set_tx_bd_ready((num_of_frames - 47), (num_of_frames - 47));
6541
      else if (num_of_frames < 62)
6542
        set_tx_bd_ready((num_of_frames - 54), (num_of_frames - 54));
6543
      else if (num_of_frames < 70)
6544
        set_tx_bd_ready((num_of_frames - 62), (num_of_frames - 62));
6545
      // CHECK END OF TRANSMITION
6546
      frame_started = 0;
6547
      if (num_of_frames >= 5)
6548
        #1 check_tx_bd(num_of_bd, data);
6549
      fork
6550
      begin: fr_st1
6551
        wait (MTxEn === 1'b1); // start transmit
6552
        frame_started = 1;
6553
      end
6554
      begin
6555
        repeat (30) @(posedge mtx_clk);
6556
        if (num_of_frames < 5)
6557
        begin
6558
          if (frame_started == 1)
6559
          begin
6560
            `TIME; $display("*E Frame should NOT start!");
6561
          end
6562
          disable fr_st1;
6563
        end
6564
        else
6565
        begin
6566
          if (frame_started == 0)
6567
          begin
6568
            `TIME; $display("*W Frame should start!");
6569
            disable fr_st1;
6570
          end
6571
        end
6572
      end
6573
      join
6574
      // check packets larger than 4 bytes
6575
      if (num_of_frames >= 5)
6576
      begin
6577
        wait (MTxEn === 1'b0); // end transmit
6578
        while (data[15] === 1)
6579
        begin
6580
          #1 check_tx_bd(num_of_bd, data);
6581
          @(posedge wb_clk);
6582
        end
6583
        repeat (1) @(posedge wb_clk);
6584
        // check length of a PACKET
6585
        if (i_length <= (min_tmp - 4))
6586
        begin
6587
          if (eth_phy.tx_len != min_tmp)
6588
          begin
6589
            test_fail("Wrong length of the packet out from MAC");
6590
            fail = fail + 1;
6591
          end
6592
        end
6593
        else
6594
        begin
6595
          if (eth_phy.tx_len != (i_length + 4))
6596
          begin
6597
            test_fail("Wrong length of the packet out from MAC");
6598
            fail = fail + 1;
6599
          end
6600
        end
6601
        // check transmitted TX packet data
6602
        if (i_length[0] == 0)
6603
        begin
6604
          #1 check_tx_packet(`MEMORY_BASE, (num_of_frames * 16), i_length, tmp);
6605
        end
6606
        else
6607
        begin
6608
          #1 check_tx_packet((`MEMORY_BASE + max_tmp), (num_of_frames * 16), i_length, tmp);
6609
        end
6610
        if (tmp > 0)
6611
        begin
6612
          test_fail("Wrong data of the transmitted packet");
6613
          fail = fail + 1;
6614
        end
6615
        // check transmited TX packet CRC
6616
        if (num_of_frames < (min_tmp - 4))
6617
          #1 check_tx_crc((num_of_frames * 16), (min_tmp - 4), 1'b0, tmp); // length without CRC
6618
        else
6619
          #1 check_tx_crc((num_of_frames * 16), i_length, 1'b0, tmp); // length without CRC
6620
        if (tmp > 0)
6621
        begin
6622
          test_fail("Wrong CRC of the transmitted packet");
6623
          fail = fail + 1;
6624
        end
6625
      end
6626
      // check WB INT signal
6627
      if ((i_length[1:0] == 2'h0) && (num_of_frames >= 5))
6628
      begin
6629
        if (wb_int !== 1'b1)
6630
        begin
6631
          `TIME; $display("*E WB INT signal should be set");
6632
          test_fail("WB INT signal should be set");
6633
          fail = fail + 1;
6634
        end
6635
      end
6636
      else
6637
      begin
6638
        if (wb_int !== 1'b0)
6639
        begin
6640
          `TIME; $display("*E WB INT signal should not be set");
6641
          test_fail("WB INT signal should not be set");
6642
          fail = fail + 1;
6643
        end
6644
      end
6645
      // check TX buffer descriptor of a packet
6646
      check_tx_bd(num_of_bd, data);
6647
      if (num_of_frames >= 5)
6648
      begin
6649
        if (i_length[1] == 1'b0) // interrupt enabled
6650
        begin
6651
          if ( (data[15:0] !== 16'h7800) && // wrap bit
6652
               (data[15:0] !== 16'h5800) ) // without wrap bit
6653
          begin
6654
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6655
            test_fail("TX buffer descriptor status is not correct");
6656
            fail = fail + 1;
6657
          end
6658
        end
6659
        else // interrupt not enabled
6660
        begin
6661
          if ( (data[15:0] !== 16'h3800) && // wrap bit
6662
               (data[15:0] !== 16'h1800) ) // without wrap bit
6663
          begin
6664
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6665
            test_fail("TX buffer descriptor status is not correct");
6666
            fail = fail + 1;
6667
          end
6668
        end
6669
      end
6670
      else
6671
      begin
6672
        if (data[15] !== 1'b1)
6673
        begin
6674
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6675
          test_fail("TX buffer descriptor status is not correct");
6676
          fail = fail + 1;
6677
        end
6678
      end
6679
      // clear TX BD with wrap bit
6680
      if (num_of_frames == 63)
6681
        clear_tx_bd(16, 16);
6682
      // check interrupts
6683
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6684
      if ( ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1)) && (num_of_frames >= 5) )
6685
      begin
6686
        if ((data & `ETH_INT_TXB) !== 1'b1)
6687
        begin
6688
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
6689
          test_fail("Interrupt Transmit Buffer was not set");
6690
          fail = fail + 1;
6691
        end
6692
        if ((data & (~`ETH_INT_TXB)) !== 0)
6693
        begin
6694
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
6695
          test_fail("Other interrupts (except Transmit Buffer) were set");
6696
          fail = fail + 1;
6697
        end
6698
      end
6699
      else
6700
      begin
6701
        if (data !== 0)
6702
        begin
6703
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
6704
          test_fail("Any of interrupts (except Transmit Buffer) was set");
6705
          fail = fail + 1;
6706
        end
6707
      end
6708
      // clear interrupts
6709
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6710
      // check WB INT signal
6711
      if (wb_int !== 1'b0)
6712
      begin
6713
        test_fail("WB INT signal should not be set");
6714
        fail = fail + 1;
6715
      end
6716
      // INTERMEDIATE DISPLAYS
6717
      if (i_length == 3)
6718
      begin
6719
        $display("    pads appending to packets is selected");
6720
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
6721
        $display("    ->packets with lengths from %0d to %0d are not transmitted (length increasing by 1 byte)",
6722
                 0, 3);
6723
      end
6724
      else if (i_length == 9)
6725
      begin
6726
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
6727
        $display("    ->packet with length 4 is not transmitted (length increasing by 1 byte)");
6728
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6729
                 5, 9);
6730
      end
6731
      else if (i_length == 17)
6732
      begin
6733
        $display("    using 4 BDs out of 8 BDs assigned to TX (wrap at 4th BD - TX BD 3)");
6734
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6735
                 10, 17);
6736
      end
6737
      else if (i_length == 27)
6738
      begin
6739
        $display("    using 5 BDs out of 8 BDs assigned to TX (wrap at 5th BD - TX BD 4)");
6740
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6741
                 18, 27);
6742
      end
6743
      else if (i_length == 40)
6744
      begin
6745
        $display("    using 6 BDs out of 8 BDs assigned to TX (wrap at 6th BD - TX BD 5)");
6746
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6747
                 28, 40);
6748
      end
6749
      else if (i_length == 54)
6750
      begin
6751
        $display("    using 7 BDs out of 8 BDs assigned to TX (wrap at 7th BD - TX BD 6)");
6752
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6753
                 41, 54);
6754
      end
6755
      else if (i_length == 69)
6756
      begin
6757
        $display("    using 8 BDs out of 8 BDs assigned to TX (wrap at 8th BD - TX BD 7)");
6758
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6759
                 55, 69);
6760
      end
6761
      // set length (loop variable)
6762
      i_length = i_length + 1;
6763
      // the number of frame transmitted
6764
      num_of_frames = num_of_frames + 1;
6765
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
6766
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
6767
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
6768
          (num_of_frames == 54) || (num_of_frames == 62))
6769
        num_of_bd = 0;
6770
      else
6771
        num_of_bd = num_of_bd + 1;
6772
    end
6773
    // disable TX
6774
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
6775
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6776
    @(posedge wb_clk);
6777
    if(fail == 0)
6778
      test_ok;
6779
    else
6780
      fail = 0;
6781
  end
6782
 
6783
 
6784
  ////////////////////////////////////////////////////////////////////
6785
  ////                                                            ////
6786 243 tadejm
  ////  Test transmit packets (no pads) from 0 to (MINFL - 1)     ////
6787 209 tadejm
  ////  sizes at 8 TX buffer decriptors ( 10Mbps ).               ////
6788
  ////                                                            ////
6789
  ////////////////////////////////////////////////////////////////////
6790
  if (test_num == 8) // 
6791
  begin
6792 243 tadejm
    // TEST 8: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )
6793
    test_name = "TEST 8: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )";
6794
    `TIME; $display("  TEST 8: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )");
6795 209 tadejm
 
6796
    // reset MAC registers
6797
    hard_reset;
6798
    // reset MAC and MII LOGIC with soft reset
6799
    reset_mac;
6800
    reset_mii;
6801
    // set wb slave response
6802
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
6803
 
6804
    max_tmp = 0;
6805
    min_tmp = 0;
6806
    // set 8 TX buffer descriptors - must be set before TX enable
6807
    wbm_write(`ETH_TX_BD_NUM, 32'h8, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6808
    // enable TX, set full-duplex mode, padding and CRC appending
6809 243 tadejm
//    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
6810
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD,
6811 209 tadejm
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6812
    // prepare two packets of MAXFL length
6813
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6814
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
6815
    min_tmp = tmp[31:16];
6816
    st_data = 8'h12;
6817
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
6818
    st_data = 8'h34;
6819
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp - 4), st_data); // length without CRC
6820
    // check WB INT signal
6821
    if (wb_int !== 1'b0)
6822
    begin
6823
      test_fail("WB INT signal should not be set");
6824
      fail = fail + 1;
6825
    end
6826
 
6827
    // write to phy's control register for 10Mbps
6828
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
6829
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
6830
    speed = 10;
6831
 
6832
    frame_started = 0;
6833
    num_of_frames = 0;
6834
    num_of_bd = 0;
6835
    i_length = 0; // 0;
6836
    while (i_length < 70) // (min_tmp - 4))
6837
    begin
6838
      #1;
6839
      // choose generating carrier sense and collision
6840
      case (i_length[1:0])
6841
      2'h0: // Interrupt is generated
6842
      begin
6843
        // Reset_tx_bd nable interrupt generation
6844
        // unmask interrupts
6845
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
6846
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6847
        // not detect carrier sense in FD and no collision
6848
        eth_phy.carrier_sense_tx_fd_detect(0);
6849
        eth_phy.collision(0);
6850
      end
6851
      2'h1: // Interrupt is not generated
6852
      begin
6853
        // set_tx_bd enable interrupt generation
6854
        // mask interrupts
6855
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6856
        // detect carrier sense in FD and no collision
6857
        eth_phy.carrier_sense_tx_fd_detect(1);
6858
        eth_phy.collision(0);
6859
      end
6860
      2'h2: // Interrupt is not generated
6861
      begin
6862
        // set_tx_bd disable the interrupt generation
6863
        // unmask interrupts
6864
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
6865
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6866
        // not detect carrier sense in FD and set collision
6867
        eth_phy.carrier_sense_tx_fd_detect(0);
6868
        eth_phy.collision(1);
6869
      end
6870
      default: // 2'h3: // Interrupt is not generated
6871
      begin
6872
        // set_tx_bd disable the interrupt generation
6873
        // mask interrupts
6874
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6875
        // detect carrier sense in FD and set collision
6876
        eth_phy.carrier_sense_tx_fd_detect(1);
6877
        eth_phy.collision(1);
6878
      end
6879
      endcase
6880 243 tadejm
//      // append CRC
6881
//      if ((i_length[0] == 1'b0) && (num_of_frames >= 6))
6882
//      begin
6883
//        append_tx_crc(`MEMORY_BASE, i_length, 1'b0);
6884
//      end
6885 209 tadejm
      #1;
6886
      // first destination address on ethernet PHY
6887
      eth_phy.set_tx_mem_addr(num_of_frames * 16);
6888
      // SET packets and wrap bit
6889
      // num_of_frames <= 9 => wrap set to TX BD 0
6890 243 tadejm
      if (num_of_frames <= 5)
6891
        begin
6892
          tmp_len = i_length; // length of frame
6893
          tmp_bd_num = 0; // TX BD number
6894
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6895
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6896
          if (tmp_len[0] == 0) // CRC appended by 'HARDWARE'
6897
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, `MEMORY_BASE);
6898
          else
6899
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
6900
          // set wrap bit
6901
          set_tx_bd_wrap(0);
6902
        end
6903
        else if (num_of_frames <= 9)
6904
        begin
6905
          tmp_len = i_length; // length of frame
6906
          tmp_bd_num = 0; // TX BD number
6907
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6908
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6909
          if (tmp_len[0] == 0) // CRC appended by 'SOFTWARE'
6910
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
6911
          else
6912
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
6913
          // set wrap bit
6914
          set_tx_bd_wrap(0);
6915 209 tadejm
      end
6916
      // 10 <= num_of_frames < 18 => wrap set to TX BD 3
6917
      else if ((num_of_frames == 10) || (num_of_frames == 14))
6918
      begin
6919
        tmp_len = i_length; // length of frame
6920
        tmp_bd_num = 0; // TX BD number
6921
        while (tmp_bd_num < 4) //
6922
        begin
6923
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6924
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6925
          if (tmp_len[0] == 0)
6926 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
6927 209 tadejm
          else
6928 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
6929 209 tadejm
          tmp_len = tmp_len + 1;
6930
          // set TX BD number
6931
          tmp_bd_num = tmp_bd_num + 1;
6932
        end
6933
        // set wrap bit
6934
        set_tx_bd_wrap(3);
6935
      end
6936
      // 18 <= num_of_frames < 28 => wrap set to TX BD 4
6937
      else if ((num_of_frames == 18) || (num_of_frames == 23))
6938
      begin
6939
        tmp_len = i_length; // length of frame
6940
        tmp_bd_num = 0; // TX BD number
6941
        while (tmp_bd_num < 5) //
6942
        begin
6943
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6944
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6945
          if (tmp_len[0] == 0)
6946 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
6947
          else // when (num_of_frames == 23), (i_length == 23) and therefor i_length[0] == 1 !!!
6948
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1],
6949
                      ((num_of_frames == 23) && (tmp_bd_num == 0)), 1'b1, (`MEMORY_BASE + max_tmp));
6950 209 tadejm
          tmp_len = tmp_len + 1;
6951
          // set TX BD number
6952
          tmp_bd_num = tmp_bd_num + 1;
6953
        end
6954
        // set wrap bit
6955
        set_tx_bd_wrap(4);
6956
      end
6957
      // 28 <= num_of_frames < 40 => wrap set to TX BD 5
6958
      else if ((num_of_frames == 28) || (num_of_frames == 34))
6959
      begin
6960
        tmp_len = i_length; // length of frame
6961
        tmp_bd_num = 0; // TX BD number
6962
        while (tmp_bd_num < 6) //
6963
        begin
6964
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6965
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6966
          if (tmp_len[0] == 0)
6967 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
6968 209 tadejm
          else
6969 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
6970 209 tadejm
          tmp_len = tmp_len + 1;
6971
          // set TX BD number
6972
          tmp_bd_num = tmp_bd_num + 1;
6973
        end
6974
        // set wrap bit
6975
        set_tx_bd_wrap(5);
6976
      end
6977
      // 40 <= num_of_frames < 54 => wrap set to TX BD 6
6978
      else if ((num_of_frames == 40) || (num_of_frames == 47))
6979
      begin
6980
        tmp_len = i_length; // length of frame
6981
        tmp_bd_num = 0; // TX BD number
6982
        while (tmp_bd_num < 7) //
6983
        begin
6984
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6985
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6986
          if (tmp_len[0] == 0)
6987 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
6988 209 tadejm
          else
6989 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
6990 209 tadejm
          tmp_len = tmp_len + 1;
6991
          // set TX BD number
6992
          tmp_bd_num = tmp_bd_num + 1;
6993
        end
6994
        // set wrap bit
6995
        set_tx_bd_wrap(6);
6996
      end
6997
      // 54 <= num_of_frames < 70 => wrap set to TX BD 7
6998
      else if ((num_of_frames == 54) || (num_of_frames == 62))
6999
      begin
7000
        tmp_len = i_length; // length of frame
7001
        tmp_bd_num = 0; // TX BD number
7002
        while (tmp_bd_num < 8) //
7003
        begin
7004
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7005
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7006
          if (tmp_len[0] == 0)
7007 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
7008 209 tadejm
          else
7009 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
7010 209 tadejm
          tmp_len = tmp_len + 1;
7011
          // set TX BD number
7012
          tmp_bd_num = tmp_bd_num + 1;
7013
        end
7014
        // set wrap bit
7015
        set_tx_bd_wrap(7);
7016
      end
7017
      #1;
7018
      // SET ready bit
7019
      if (num_of_frames < 10)
7020
        set_tx_bd_ready(0, 0);
7021
      else if (num_of_frames < 14)
7022
        set_tx_bd_ready((num_of_frames - 10), (num_of_frames - 10));
7023
      else if (num_of_frames < 18)
7024
        set_tx_bd_ready((num_of_frames - 14), (num_of_frames - 14));
7025
      else if (num_of_frames < 23)
7026
        set_tx_bd_ready((num_of_frames - 18), (num_of_frames - 18));
7027
      else if (num_of_frames < 28)
7028
        set_tx_bd_ready((num_of_frames - 23), (num_of_frames - 23));
7029
      else if (num_of_frames < 34)
7030
        set_tx_bd_ready((num_of_frames - 28), (num_of_frames - 28));
7031
      else if (num_of_frames < 40)
7032
        set_tx_bd_ready((num_of_frames - 34), (num_of_frames - 34));
7033
      else if (num_of_frames < 47)
7034
        set_tx_bd_ready((num_of_frames - 40), (num_of_frames - 40));
7035
      else if (num_of_frames < 54)
7036
        set_tx_bd_ready((num_of_frames - 47), (num_of_frames - 47));
7037
      else if (num_of_frames < 62)
7038
        set_tx_bd_ready((num_of_frames - 54), (num_of_frames - 54));
7039
      else if (num_of_frames < 70)
7040
        set_tx_bd_ready((num_of_frames - 62), (num_of_frames - 62));
7041
      // CHECK END OF TRANSMITION
7042
      frame_started = 0;
7043
      if (num_of_frames >= 5)
7044
        #1 check_tx_bd(num_of_bd, data);
7045
      fork
7046
      begin: fr_st2
7047
        wait (MTxEn === 1'b1); // start transmit
7048
        frame_started = 1;
7049
      end
7050
      begin
7051
        repeat (30) @(posedge mtx_clk);
7052
        if (num_of_frames < 5)
7053
        begin
7054
          if (frame_started == 1)
7055
          begin
7056
            `TIME; $display("*E Frame should NOT start!");
7057
          end
7058
          disable fr_st2;
7059
        end
7060
        else
7061
        begin
7062
          if (frame_started == 0)
7063
          begin
7064
            `TIME; $display("*W Frame should start!");
7065
            disable fr_st2;
7066
          end
7067
        end
7068
      end
7069
      join
7070
      // check packets larger than 4 bytes
7071
      if (num_of_frames >= 5)
7072
      begin
7073
        wait (MTxEn === 1'b0); // end transmit
7074
        while (data[15] === 1)
7075
        begin
7076
          #1 check_tx_bd(num_of_bd, data);
7077
          @(posedge wb_clk);
7078
        end
7079
        repeat (1) @(posedge wb_clk);
7080 243 tadejm
        // check length of a PACKET 
7081
        if ((eth_phy.tx_len != i_length) && (i_length[0] == 1'b0) && (num_of_frames >= 6))
7082 209 tadejm
        begin
7083
          `TIME; $display("*E Wrong length of the packet out from MAC");
7084
          test_fail("Wrong length of the packet out from MAC");
7085
          fail = fail + 1;
7086
        end
7087 243 tadejm
        else if ((eth_phy.tx_len != (i_length + 4)) && (num_of_frames != 23))
7088
        begin
7089
          `TIME; $display("*E Wrong length of the packet out from MAC");
7090
          test_fail("Wrong length of the packet out from MAC");
7091
          fail = fail + 1;
7092
        end
7093
        else if ((eth_phy.tx_len != (min_tmp)) && (num_of_frames == 23))
7094
        begin
7095
          `TIME; $display("*E Wrong length of the packet out from MAC");
7096
          test_fail("Wrong length of the packet out from MAC");
7097
          fail = fail + 1;
7098
        end
7099 209 tadejm
        // check transmitted TX packet data
7100
        if (i_length[0] == 0)
7101
        begin
7102
          #1 check_tx_packet(`MEMORY_BASE, (num_of_frames * 16), i_length, tmp);
7103
        end
7104 243 tadejm
        else if (num_of_frames == 23) // i_length[0] == 1 here
7105
        begin
7106
          #1 check_tx_packet((`MEMORY_BASE + max_tmp), (num_of_frames * 16), (min_tmp - 4), tmp);
7107
        end
7108 209 tadejm
        else
7109
        begin
7110
          #1 check_tx_packet((`MEMORY_BASE + max_tmp), (num_of_frames * 16), i_length, tmp);
7111
        end
7112
        if (tmp > 0)
7113
        begin
7114
          test_fail("Wrong data of the transmitted packet");
7115
          fail = fail + 1;
7116
        end
7117
        // check transmited TX packet CRC
7118 243 tadejm
        #1;
7119
        if ((i_length[0] == 1'b0) && (num_of_frames >= 6))
7120
        begin
7121
        end
7122
        else
7123
          check_tx_crc((num_of_frames * 16), (eth_phy.tx_len - 4), 1'b0, tmp); // length without CRC
7124 209 tadejm
        if (tmp > 0)
7125
        begin
7126
          test_fail("Wrong CRC of the transmitted packet");
7127
          fail = fail + 1;
7128
        end
7129
      end
7130
      // check WB INT signal
7131
      if ((i_length[1:0] == 2'h0) && (num_of_frames >= 5))
7132
      begin
7133
        if (wb_int !== 1'b1)
7134
        begin
7135
          `TIME; $display("*E WB INT signal should be set");
7136
          test_fail("WB INT signal should be set");
7137
          fail = fail + 1;
7138
        end
7139
      end
7140
      else
7141
      begin
7142
        if (wb_int !== 1'b0)
7143
        begin
7144
          `TIME; $display("*E WB INT signal should not be set");
7145
          test_fail("WB INT signal should not be set");
7146
          fail = fail + 1;
7147
        end
7148
      end
7149
      // check TX buffer descriptor of a packet
7150
      check_tx_bd(num_of_bd, data);
7151
      if (num_of_frames >= 5)
7152
      begin
7153 243 tadejm
        if ((i_length[1] == 1'b0) && (i_length[0] == 1'b0)) // interrupt enabled
7154 209 tadejm
        begin
7155 243 tadejm
          if ( (data[15:0] !== 16'h6000) &&  // wrap bit
7156
               (data[15:0] !== 16'h4000) )  // without wrap bit
7157 209 tadejm
          begin
7158
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7159
            test_fail("TX buffer descriptor status is not correct");
7160
            fail = fail + 1;
7161
          end
7162
        end
7163 243 tadejm
        else if ((i_length[1] == 1'b1) && (i_length[0] == 1'b0)) // interrupt not enabled
7164 209 tadejm
        begin
7165 243 tadejm
          if ( (data[15:0] !== 16'h2000) && // wrap bit
7166
               (data[15:0] !== 16'h0000) ) // without wrap bit
7167
          begin
7168
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7169
            test_fail("TX buffer descriptor status is not correct");
7170
            fail = fail + 1;
7171
          end
7172
        end
7173
        else if ((i_length[1] == 1'b0) && (i_length[0] == 1'b1)) // interrupt enabled
7174
        begin
7175
          if ( (data[15:0] !== 16'h6800) && // wrap bit
7176
               (data[15:0] !== 16'h4800) ) // without wrap bit
7177
          begin
7178
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7179
            test_fail("TX buffer descriptor status is not correct");
7180
            fail = fail + 1;
7181
          end
7182
        end
7183
        else if (num_of_frames != 23) // ((i_length[1] == 1'b1) && (i_length[0] == 1'b1)) // interrupt not enabled
7184
        begin
7185
          if ( (data[15:0] !== 16'h2800) && // wrap bit
7186
               (data[15:0] !== 16'h0800) ) // without wrap bit
7187
          begin
7188
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7189
            test_fail("TX buffer descriptor status is not correct");
7190
            fail = fail + 1;
7191
          end
7192
        end
7193
        else // ((num_of_frames != 23) && (i_length[1] == 1'b1) && (i_length[0] == 1'b1)) // interrupt not enabled
7194
        begin
7195 209 tadejm
          if ( (data[15:0] !== 16'h3800) && // wrap bit
7196
               (data[15:0] !== 16'h1800) ) // without wrap bit
7197
          begin
7198
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7199
            test_fail("TX buffer descriptor status is not correct");
7200
            fail = fail + 1;
7201
          end
7202
        end
7203
      end
7204
      else
7205
      begin
7206
        if (data[15] !== 1'b1)
7207
        begin
7208
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7209
          test_fail("TX buffer descriptor status is not correct");
7210
          fail = fail + 1;
7211
        end
7212
      end
7213
      // clear TX BD with wrap bit
7214
      if (num_of_frames == 63)
7215
        clear_tx_bd(16, 16);
7216
      // check interrupts
7217
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7218
      if ( ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1)) && (num_of_frames >= 5) )
7219
      begin
7220
        if ((data & `ETH_INT_TXB) !== 1'b1)
7221
        begin
7222
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
7223
          test_fail("Interrupt Transmit Buffer was not set");
7224
          fail = fail + 1;
7225
        end
7226
        if ((data & (~`ETH_INT_TXB)) !== 0)
7227
        begin
7228
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
7229
          test_fail("Other interrupts (except Transmit Buffer) were set");
7230
          fail = fail + 1;
7231
        end
7232
      end
7233
      else
7234
      begin
7235
        if (data !== 0)
7236
        begin
7237
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
7238
          test_fail("Any of interrupts (except Transmit Buffer) was set");
7239
          fail = fail + 1;
7240
        end
7241
      end
7242
      // clear interrupts
7243
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7244
      // check WB INT signal
7245
      if (wb_int !== 1'b0)
7246
      begin
7247
        test_fail("WB INT signal should not be set");
7248
        fail = fail + 1;
7249
      end
7250
      // INTERMEDIATE DISPLAYS
7251
      if (i_length == 3)
7252
      begin
7253
        $display("    pads appending to packets is selected");
7254
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
7255
        $display("    ->packets with lengths from %0d to %0d are not transmitted (length increasing by 1 byte)",
7256
                 0, 3);
7257
      end
7258
      else if (i_length == 9)
7259
      begin
7260
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
7261
        $display("    ->packet with length 4 is not transmitted (length increasing by 1 byte)");
7262
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7263
                 5, 9);
7264
      end
7265
      else if (i_length == 17)
7266
      begin
7267
        $display("    using 4 BDs out of 8 BDs assigned to TX (wrap at 4th BD - TX BD 3)");
7268
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7269
                 10, 17);
7270
      end
7271
      else if (i_length == 27)
7272
      begin
7273
        $display("    using 5 BDs out of 8 BDs assigned to TX (wrap at 5th BD - TX BD 4)");
7274
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7275
                 18, 27);
7276
      end
7277
      else if (i_length == 40)
7278
      begin
7279
        $display("    using 6 BDs out of 8 BDs assigned to TX (wrap at 6th BD - TX BD 5)");
7280
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7281
                 28, 40);
7282
      end
7283
      else if (i_length == 54)
7284
      begin
7285
        $display("    using 7 BDs out of 8 BDs assigned to TX (wrap at 7th BD - TX BD 6)");
7286
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7287
                 41, 54);
7288
      end
7289
      else if (i_length == 69)
7290
      begin
7291
        $display("    using 8 BDs out of 8 BDs assigned to TX (wrap at 8th BD - TX BD 7)");
7292
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7293
                 55, 69);
7294
      end
7295
      // set length (loop variable)
7296
      i_length = i_length + 1;
7297
      // the number of frame transmitted
7298
      num_of_frames = num_of_frames + 1;
7299
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
7300
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
7301
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
7302
          (num_of_frames == 54) || (num_of_frames == 62))
7303
        num_of_bd = 0;
7304
      else
7305
        num_of_bd = num_of_bd + 1;
7306
    end
7307
    // disable TX
7308
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
7309
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7310
    @(posedge wb_clk);
7311
    if(fail == 0)
7312
      test_ok;
7313
    else
7314
      fail = 0;
7315
  end
7316
 
7317
 
7318
  ////////////////////////////////////////////////////////////////////
7319
  ////                                                            ////
7320 243 tadejm
  ////  Test transmit packets (no pads) form 0 to (MINFL - 1)     ////
7321 209 tadejm
  ////  sizes at 8 TX buffer decriptors ( 100Mbps ).              ////
7322
  ////                                                            ////
7323
  ////////////////////////////////////////////////////////////////////
7324
  if (test_num == 9) // 
7325
  begin
7326 243 tadejm
    // TEST 9: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )
7327
    test_name = "TEST 9: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )";
7328
    `TIME; $display("  TEST 9: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )");
7329 209 tadejm
 
7330
    // reset MAC registers
7331
    hard_reset;
7332
    // reset MAC and MII LOGIC with soft reset
7333
    reset_mac;
7334
    reset_mii;
7335
    // set wb slave response
7336
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
7337
 
7338
    max_tmp = 0;
7339
    min_tmp = 0;
7340
    // set 8 TX buffer descriptors - must be set before TX enable
7341
    wbm_write(`ETH_TX_BD_NUM, 32'h8, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7342
    // enable TX, set full-duplex mode, padding and CRC appending
7343
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
7344
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7345
    // prepare two packets of MAXFL length
7346
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7347
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
7348
    min_tmp = tmp[31:16];
7349
    st_data = 8'h12;
7350
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
7351
    st_data = 8'h34;
7352
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp - 4), st_data); // length without CRC
7353
    // check WB INT signal
7354
    if (wb_int !== 1'b0)
7355
    begin
7356
      test_fail("WB INT signal should not be set");
7357
      fail = fail + 1;
7358
    end
7359
 
7360
    // write to phy's control register for 100Mbps
7361
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
7362
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
7363
    speed = 100;
7364
 
7365
    frame_started = 0;
7366
    num_of_frames = 0;
7367
    num_of_bd = 0;
7368
    i_length = 0; // 0;
7369
    while (i_length < 70) // (min_tmp - 4))
7370
    begin
7371
      #1;
7372
      // choose generating carrier sense and collision
7373
      case (i_length[1:0])
7374
      2'h0: // Interrupt is generated
7375
      begin
7376
        // Reset_tx_bd nable interrupt generation
7377
        // unmask interrupts
7378
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
7379
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7380
        // not detect carrier sense in FD and no collision
7381
        eth_phy.carrier_sense_tx_fd_detect(0);
7382
        eth_phy.collision(0);
7383
      end
7384
      2'h1: // Interrupt is not generated
7385
      begin
7386
        // set_tx_bd enable interrupt generation
7387
        // mask interrupts
7388
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7389
        // detect carrier sense in FD and no collision
7390
        eth_phy.carrier_sense_tx_fd_detect(1);
7391
        eth_phy.collision(0);
7392
      end
7393
      2'h2: // Interrupt is not generated
7394
      begin
7395
        // set_tx_bd disable the interrupt generation
7396
        // unmask interrupts
7397
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
7398
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7399
        // not detect carrier sense in FD and set collision
7400
        eth_phy.carrier_sense_tx_fd_detect(0);
7401
        eth_phy.collision(1);
7402
      end
7403
      default: // 2'h3: // Interrupt is not generated
7404
      begin
7405
        // set_tx_bd disable the interrupt generation
7406
        // mask interrupts
7407
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7408
        // detect carrier sense in FD and set collision
7409
        eth_phy.carrier_sense_tx_fd_detect(1);
7410
        eth_phy.collision(1);
7411
      end
7412
      endcase
7413
      #1;
7414
      // first destination address on ethernet PHY
7415
      eth_phy.set_tx_mem_addr(num_of_frames * 16);
7416
      // SET packets and wrap bit
7417
      // num_of_frames <= 9 => wrap set to TX BD 0
7418
      if (num_of_frames <= 9)
7419
      begin
7420
        tmp_len = i_length; // length of frame
7421
        tmp_bd_num = 0; // TX BD number
7422
        // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7423
        // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7424
        if (tmp_len[0] == 0)
7425
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7426
        else
7427
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7428
        // set wrap bit
7429
        set_tx_bd_wrap(0);
7430
      end
7431
      // 10 <= num_of_frames < 18 => wrap set to TX BD 3
7432
      else if ((num_of_frames == 10) || (num_of_frames == 14))
7433
      begin
7434
        tmp_len = i_length; // length of frame
7435
        tmp_bd_num = 0; // TX BD number
7436
        while (tmp_bd_num < 4) //
7437
        begin
7438
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7439
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7440
          if (tmp_len[0] == 0)
7441
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7442
          else
7443
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7444
          tmp_len = tmp_len + 1;
7445
          // set TX BD number
7446
          tmp_bd_num = tmp_bd_num + 1;
7447
        end
7448
        // set wrap bit
7449
        set_tx_bd_wrap(3);
7450
      end
7451
      // 18 <= num_of_frames < 28 => wrap set to TX BD 4
7452
      else if ((num_of_frames == 18) || (num_of_frames == 23))
7453
      begin
7454
        tmp_len = i_length; // length of frame
7455
        tmp_bd_num = 0; // TX BD number
7456
        while (tmp_bd_num < 5) //
7457
        begin
7458
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7459
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7460
          if (tmp_len[0] == 0)
7461
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7462
          else
7463
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7464
          tmp_len = tmp_len + 1;
7465
          // set TX BD number
7466
          tmp_bd_num = tmp_bd_num + 1;
7467
        end
7468
        // set wrap bit
7469
        set_tx_bd_wrap(4);
7470
      end
7471
      // 28 <= num_of_frames < 40 => wrap set to TX BD 5
7472
      else if ((num_of_frames == 28) || (num_of_frames == 34))
7473
      begin
7474
        tmp_len = i_length; // length of frame
7475
        tmp_bd_num = 0; // TX BD number
7476
        while (tmp_bd_num < 6) //
7477
        begin
7478
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7479
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7480
          if (tmp_len[0] == 0)
7481
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7482
          else
7483
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7484
          tmp_len = tmp_len + 1;
7485
          // set TX BD number
7486
          tmp_bd_num = tmp_bd_num + 1;
7487
        end
7488
        // set wrap bit
7489
        set_tx_bd_wrap(5);
7490
      end
7491
      // 40 <= num_of_frames < 54 => wrap set to TX BD 6
7492
      else if ((num_of_frames == 40) || (num_of_frames == 47))
7493
      begin
7494
        tmp_len = i_length; // length of frame
7495
        tmp_bd_num = 0; // TX BD number
7496
        while (tmp_bd_num < 7) //
7497
        begin
7498
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7499
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7500
          if (tmp_len[0] == 0)
7501
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7502
          else
7503
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7504
          tmp_len = tmp_len + 1;
7505
          // set TX BD number
7506
          tmp_bd_num = tmp_bd_num + 1;
7507
        end
7508
        // set wrap bit
7509
        set_tx_bd_wrap(6);
7510
      end
7511
      // 54 <= num_of_frames < 70 => wrap set to TX BD 7
7512
      else if ((num_of_frames == 54) || (num_of_frames == 62))
7513
      begin
7514
        tmp_len = i_length; // length of frame
7515
        tmp_bd_num = 0; // TX BD number
7516
        while (tmp_bd_num < 8) //
7517
        begin
7518
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7519
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7520
          if (tmp_len[0] == 0)
7521
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7522
          else
7523
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7524
          tmp_len = tmp_len + 1;
7525
          // set TX BD number
7526
          tmp_bd_num = tmp_bd_num + 1;
7527
        end
7528
        // set wrap bit
7529
        set_tx_bd_wrap(7);
7530
      end
7531
      #1;
7532
      // SET ready bit
7533
      if (num_of_frames < 10)
7534
        set_tx_bd_ready(0, 0);
7535
      else if (num_of_frames < 14)
7536
        set_tx_bd_ready((num_of_frames - 10), (num_of_frames - 10));
7537
      else if (num_of_frames < 18)
7538
        set_tx_bd_ready((num_of_frames - 14), (num_of_frames - 14));
7539
      else if (num_of_frames < 23)
7540
        set_tx_bd_ready((num_of_frames - 18), (num_of_frames - 18));
7541
      else if (num_of_frames < 28)
7542
        set_tx_bd_ready((num_of_frames - 23), (num_of_frames - 23));
7543
      else if (num_of_frames < 34)
7544
        set_tx_bd_ready((num_of_frames - 28), (num_of_frames - 28));
7545
      else if (num_of_frames < 40)
7546
        set_tx_bd_ready((num_of_frames - 34), (num_of_frames - 34));
7547
      else if (num_of_frames < 47)
7548
        set_tx_bd_ready((num_of_frames - 40), (num_of_frames - 40));
7549
      else if (num_of_frames < 54)
7550
        set_tx_bd_ready((num_of_frames - 47), (num_of_frames - 47));
7551
      else if (num_of_frames < 62)
7552
        set_tx_bd_ready((num_of_frames - 54), (num_of_frames - 54));
7553
      else if (num_of_frames < 70)
7554
        set_tx_bd_ready((num_of_frames - 62), (num_of_frames - 62));
7555
      // CHECK END OF TRANSMITION
7556
      frame_started = 0;
7557
      if (num_of_frames >= 5)
7558
        #1 check_tx_bd(num_of_bd, data);
7559
      fork
7560
      begin: fr_st3
7561
        wait (MTxEn === 1'b1); // start transmit
7562
        frame_started = 1;
7563
      end
7564
      begin
7565
        repeat (30) @(posedge mtx_clk);
7566
        if (num_of_frames < 5)
7567
        begin
7568
          if (frame_started == 1)
7569
          begin
7570
            `TIME; $display("*E Frame should NOT start!");
7571
          end
7572
          disable fr_st3;
7573
        end
7574
        else
7575
        begin
7576
          if (frame_started == 0)
7577
          begin
7578
            `TIME; $display("*W Frame should start!");
7579
            disable fr_st3;
7580
          end
7581
        end
7582
      end
7583
      join
7584
      // check packets larger than 4 bytes
7585
      if (num_of_frames >= 5)
7586
      begin
7587
        wait (MTxEn === 1'b0); // end transmit
7588
        while (data[15] === 1)
7589
        begin
7590
          #1 check_tx_bd(num_of_bd, data);
7591
          @(posedge wb_clk);
7592
        end
7593
        repeat (1) @(posedge wb_clk);
7594
        // check length of a PACKET
7595
        if (eth_phy.tx_len != (i_length + 4))
7596
        begin
7597
          `TIME; $display("*E Wrong length of the packet out from MAC");
7598
          test_fail("Wrong length of the packet out from MAC");
7599
          fail = fail + 1;
7600
        end
7601
        // check transmitted TX packet data
7602
        if (i_length[0] == 0)
7603
        begin
7604
          #1 check_tx_packet(`MEMORY_BASE, (num_of_frames * 16), i_length, tmp);
7605
        end
7606
        else
7607
        begin
7608
          #1 check_tx_packet((`MEMORY_BASE + max_tmp), (num_of_frames * 16), i_length, tmp);
7609
        end
7610
        if (tmp > 0)
7611
        begin
7612
          test_fail("Wrong data of the transmitted packet");
7613
          fail = fail + 1;
7614
        end
7615
        // check transmited TX packet CRC
7616
        #1 check_tx_crc((num_of_frames * 16), (eth_phy.tx_len - 4), 1'b0, tmp); // length without CRC
7617
        if (tmp > 0)
7618
        begin
7619
          test_fail("Wrong CRC of the transmitted packet");
7620
          fail = fail + 1;
7621
        end
7622
      end
7623
      // check WB INT signal
7624
      if ((i_length[1:0] == 2'h0) && (num_of_frames >= 5))
7625
      begin
7626
        if (wb_int !== 1'b1)
7627
        begin
7628
          `TIME; $display("*E WB INT signal should be set");
7629
          test_fail("WB INT signal should be set");
7630
          fail = fail + 1;
7631
        end
7632
      end
7633
      else
7634
      begin
7635
        if (wb_int !== 1'b0)
7636
        begin
7637
          `TIME; $display("*E WB INT signal should not be set");
7638
          test_fail("WB INT signal should not be set");
7639
          fail = fail + 1;
7640
        end
7641
      end
7642
      // check TX buffer descriptor of a packet
7643
      check_tx_bd(num_of_bd, data);
7644
      if (num_of_frames >= 5)
7645
      begin
7646
        if (i_length[1] == 1'b0) // interrupt enabled
7647
        begin
7648
          if ( (data[15:0] !== 16'h7800) && // wrap bit
7649
               (data[15:0] !== 16'h5800) ) // without wrap bit
7650
          begin
7651
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7652
            test_fail("TX buffer descriptor status is not correct");
7653
            fail = fail + 1;
7654
          end
7655
        end
7656
        else // interrupt not enabled
7657
        begin
7658
          if ( (data[15:0] !== 16'h3800) && // wrap bit
7659
               (data[15:0] !== 16'h1800) ) // without wrap bit
7660
          begin
7661
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7662
            test_fail("TX buffer descriptor status is not correct");
7663
            fail = fail + 1;
7664
          end
7665
        end
7666
      end
7667
      else
7668
      begin
7669
        if (data[15] !== 1'b1)
7670
        begin
7671
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7672
          test_fail("TX buffer descriptor status is not correct");
7673
          fail = fail + 1;
7674
        end
7675
      end
7676
      // clear TX BD with wrap bit
7677
      if (num_of_frames == 63)
7678
        clear_tx_bd(16, 16);
7679
      // check interrupts
7680
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7681
      if ( ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1)) && (num_of_frames >= 5) )
7682
      begin
7683
        if ((data & `ETH_INT_TXB) !== 1'b1)
7684
        begin
7685
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
7686
          test_fail("Interrupt Transmit Buffer was not set");
7687
          fail = fail + 1;
7688
        end
7689
        if ((data & (~`ETH_INT_TXB)) !== 0)
7690
        begin
7691
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
7692
          test_fail("Other interrupts (except Transmit Buffer) were set");
7693
          fail = fail + 1;
7694
        end
7695
      end
7696
      else
7697
      begin
7698
        if (data !== 0)
7699
        begin
7700
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
7701
          test_fail("Any of interrupts (except Transmit Buffer) was set");
7702
          fail = fail + 1;
7703
        end
7704
      end
7705
      // clear interrupts
7706
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7707
      // check WB INT signal
7708
      if (wb_int !== 1'b0)
7709
      begin
7710
        test_fail("WB INT signal should not be set");
7711
        fail = fail + 1;
7712
      end
7713
      // INTERMEDIATE DISPLAYS
7714
      if (i_length == 3)
7715
      begin
7716
        $display("    pads appending to packets is selected");
7717
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
7718
        $display("    ->packets with lengths from %0d to %0d are not transmitted (length increasing by 1 byte)",
7719
                 0, 3);
7720
      end
7721
      else if (i_length == 9)
7722
      begin
7723
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
7724
        $display("    ->packet with length 4 is not transmitted (length increasing by 1 byte)");
7725
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7726
                 5, 9);
7727
      end
7728
      else if (i_length == 17)
7729
      begin
7730
        $display("    using 4 BDs out of 8 BDs assigned to TX (wrap at 4th BD - TX BD 3)");
7731
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7732
                 10, 17);
7733
      end
7734
      else if (i_length == 27)
7735
      begin
7736
        $display("    using 5 BDs out of 8 BDs assigned to TX (wrap at 5th BD - TX BD 4)");
7737
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7738
                 18, 27);
7739
      end
7740
      else if (i_length == 40)
7741
      begin
7742
        $display("    using 6 BDs out of 8 BDs assigned to TX (wrap at 6th BD - TX BD 5)");
7743
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7744
                 28, 40);
7745
      end
7746
      else if (i_length == 54)
7747
      begin
7748
        $display("    using 7 BDs out of 8 BDs assigned to TX (wrap at 7th BD - TX BD 6)");
7749
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7750
                 41, 54);
7751
      end
7752
      else if (i_length == 69)
7753
      begin
7754
        $display("    using 8 BDs out of 8 BDs assigned to TX (wrap at 8th BD - TX BD 7)");
7755
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7756
                 55, 69);
7757
      end
7758
      // set length (loop variable)
7759
      i_length = i_length + 1;
7760
      // the number of frame transmitted
7761
      num_of_frames = num_of_frames + 1;
7762
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
7763
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
7764
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
7765
          (num_of_frames == 54) || (num_of_frames == 62))
7766
        num_of_bd = 0;
7767
      else
7768
        num_of_bd = num_of_bd + 1;
7769
    end
7770
    // disable TX
7771
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
7772
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7773
    @(posedge wb_clk);
7774
    if(fail == 0)
7775
      test_ok;
7776
    else
7777
      fail = 0;
7778
  end
7779
 
7780
 
7781
  ////////////////////////////////////////////////////////////////////
7782
  ////                                                            ////
7783
  ////  Test transmit packets across MAXFL value at               ////
7784
  ////  13 TX buffer decriptors ( 10Mbps ).                       ////
7785
  ////                                                            ////
7786
  ////////////////////////////////////////////////////////////////////
7787
  if (test_num == 10) // without and with padding
7788
  begin
7789
    // TEST 10: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 10Mbps )
7790
    test_name = "TEST 10: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 10Mbps )";
7791
    `TIME; $display("  TEST 10: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 10Mbps )");
7792
 
7793
    // reset MAC registers
7794
    hard_reset;
7795
    // reset MAC and MII LOGIC with soft reset
7796
    reset_mac;
7797
    reset_mii;
7798
    // set wb slave response
7799
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
7800
 
7801
    max_tmp = 0;
7802
    min_tmp = 0;
7803
    num_of_frames = 0;
7804
    num_of_bd = 0;
7805
    // set 13 TX buffer descriptors - must be set before TX enable
7806
    wbm_write(`ETH_TX_BD_NUM, 32'hD, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7807
    // enable TX, set full-duplex mode, NO padding and CRC appending
7808
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
7809
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7810
    // prepare a packet of MAXFL + 10 length
7811
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7812
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
7813
    min_tmp = tmp[31:16];
7814
    st_data = 8'hA3;
7815
    set_tx_packet(`MEMORY_BASE, (max_tmp + 10), st_data); // length without CRC
7816
    // check WB INT signal
7817
    if (wb_int !== 1'b0)
7818
    begin
7819
      test_fail("WB INT signal should not be set");
7820
      fail = fail + 1;
7821
    end
7822
 
7823
    // write to phy's control register for 10Mbps
7824
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
7825
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
7826
    speed = 10;
7827
 
7828
    i_length = (max_tmp - 5);
7829
    while (i_length <= (max_tmp - 3)) // (max_tmp - 4) is the limit
7830
    begin
7831
$display("   i_length = %0d", i_length);
7832
      // choose generating carrier sense and collision
7833
//      case (i_length[1:0])
7834
//      2'h0: // Interrupt is generated
7835
//      begin
7836
        // Reset_tx_bd nable interrupt generation
7837
        // unmask interrupts
7838
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
7839
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7840
        // not detect carrier sense in FD and no collision
7841
        eth_phy.carrier_sense_tx_fd_detect(0);
7842
        eth_phy.collision(0);
7843
//      end
7844
//      2'h1: // Interrupt is not generated
7845
//      begin
7846
        // set_tx_bd enable interrupt generation
7847
        // mask interrupts
7848
//        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7849
        // detect carrier sense in FD and no collision
7850
//        eth_phy.carrier_sense_tx_fd_detect(1);
7851
//        eth_phy.collision(0);
7852
//      end
7853
//      2'h2: // Interrupt is not generated
7854
//      begin
7855
        // set_tx_bd disable the interrupt generation
7856
        // unmask interrupts
7857
//        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
7858
//                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7859
        // not detect carrier sense in FD and set collision
7860
//        eth_phy.carrier_sense_tx_fd_detect(0);
7861
//        eth_phy.collision(1);
7862
//      end
7863
//      default: // 2'h3: // Interrupt is not generated
7864
//      begin
7865
        // set_tx_bd disable the interrupt generation
7866
        // mask interrupts
7867
//        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7868
        // detect carrier sense in FD and set collision
7869
//        eth_phy.carrier_sense_tx_fd_detect(1);
7870
//        eth_phy.collision(1);
7871
//      end
7872
//      endcase
7873
      // first destination address on ethernet PHY
7874
      eth_phy.set_tx_mem_addr(0);
7875
      // 
7876
if (num_of_bd == 0)
7877
begin
7878
set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
7879
set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
7880
set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
7881
set_tx_bd_wrap(2);
7882
set_tx_bd_ready(0, 0);
7883
end
7884
else if (num_of_bd == 1)
7885
set_tx_bd_ready(1, 1);
7886
else if (num_of_bd == 2)
7887
set_tx_bd_ready(2, 2);
7888
 
7889
 
7890
//        tmp_len = i_length; // length of frame
7891
//        tmp_bd_num = 0; // TX BD number
7892
//        while (tmp_bd_num < 8) // 
7893
//        begin
7894
//          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7895
//          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7896
//          if (tmp_len[0] == 0)
7897
//            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7898
//          else
7899
//            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + 2*max_tmp));
7900
//          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
7901
//          tmp_len = tmp_len + 1;
7902
//          // set TX BD number
7903
//          tmp_bd_num = tmp_bd_num + 1;
7904
//        end
7905
//        // set wrap bit
7906
//        set_tx_bd_wrap(7);
7907
//      // set ready bit
7908
//      set_tx_bd_ready((i_length - (max_tmp - 8)), (i_length - (max_tmp - 8)));
7909
      // CHECK END OF TRANSMITION
7910
check_tx_bd(num_of_bd, data);
7911
//      #1 check_tx_bd((i_length - (max_tmp - 8)), data);
7912
        wait (MTxEn === 1'b1); // start transmit
7913
check_tx_bd(num_of_bd, data);
7914
//        #1 check_tx_bd((i_length - (max_tmp - 8)), data);
7915
        if (data[15] !== 1)
7916
        begin
7917
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
7918
          fail = fail + 1;
7919
        end
7920
        wait (MTxEn === 1'b0); // end transmit
7921
        while (data[15] === 1)
7922
        begin
7923
check_tx_bd(num_of_bd, data);
7924
//          #1 check_tx_bd((i_length - (max_tmp - 8)), data);
7925
          @(posedge wb_clk);
7926
        end
7927
        repeat (1) @(posedge wb_clk);
7928
      // check length of a PACKET
7929
$display("   eth_phy length = %0d", eth_phy.tx_len);
7930
tmp_len = eth_phy.tx_len;
7931
#1;
7932
if (tmp_len != (i_length + 4))
7933
//      if (eth_phy.tx_len != (i_length + 4))
7934
      begin
7935
        test_fail("Wrong length of the packet out from MAC");
7936
        fail = fail + 1;
7937
      end
7938
      // checking in the following if statement is performed only for first and last 64 lengths
7939
//      if ( ((i_length + 4) <= (min_tmp + 64)) || ((i_length + 4) > (max_tmp - 64)) )
7940
//      begin
7941
        // check transmitted TX packet data
7942
//        if (i_length[0] == 0)
7943
//        begin
7944
          check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
7945
//        end
7946
//        else
7947
//        begin
7948
//          check_tx_packet((`MEMORY_BASE + 2*max_tmp), 0, i_length, tmp);
7949
//        end
7950
        if (tmp > 0)
7951
        begin
7952
          test_fail("Wrong data of the transmitted packet");
7953
          fail = fail + 1;
7954
        end
7955
        // check transmited TX packet CRC
7956
//        if (i_length[0] == 0)
7957
          check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
7958
//        else
7959
//          check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
7960
        if (tmp > 0)
7961
        begin
7962
          test_fail("Wrong CRC of the transmitted packet");
7963
          fail = fail + 1;
7964
        end
7965
//      end
7966
      // check WB INT signal
7967
//      if (i_length[1:0] == 2'h0)
7968
//      begin
7969
        if (wb_int !== 1'b1)
7970
        begin
7971
          `TIME; $display("*E WB INT signal should be set");
7972
          test_fail("WB INT signal should be set");
7973
          fail = fail + 1;
7974
        end
7975
//      end
7976
//      else
7977
//      begin
7978
//        if (wb_int !== 1'b0)
7979
//        begin
7980
//          `TIME; $display("*E WB INT signal should not be set");
7981
//          test_fail("WB INT signal should not be set");
7982
//          fail = fail + 1;
7983
//        end
7984
//      end
7985
//      // check TX buffer descriptor of a packet
7986
//      check_tx_bd((i_length - (max_tmp - 8)), data);
7987
check_tx_bd(num_of_bd, data);
7988
if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
7989
     ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
7990
//      if (i_length[1] == 1'b0) // interrupt enabled
7991
//      begin
7992
//        if ( ((data[15:0] !== 16'h7800) && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
7993
//             ((data[15:0] !== 16'h5800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
7994
        begin
7995
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7996
          test_fail("TX buffer descriptor status is not correct");
7997
          fail = fail + 1;
7998
        end
7999
//      end
8000
//      else // interrupt not enabled
8001
//      begin
8002
//        if ( ((data[15:0] !== 16'h3800)  && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
8003
//             ((data[15:0] !== 16'h1800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
8004
//        begin
8005
//          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8006
//          test_fail("TX buffer descriptor status is not correct");
8007
//          fail = fail + 1;
8008
//        end
8009
//      end
8010
//      // clear first half of 8 frames from TX buffer descriptor 0
8011
//      if (num_of_frames < 4)
8012
//        clear_tx_bd((i_length - (max_tmp - 8)), (i_length - (max_tmp - 8)));
8013
      // check interrupts
8014
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8015
//      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
8016
//      begin
8017
        if ((data & `ETH_INT_TXB) !== 1'b1)
8018
        begin
8019
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8020
          test_fail("Interrupt Transmit Buffer was not set");
8021
          fail = fail + 1;
8022
        end
8023
        if ((data & (~`ETH_INT_TXB)) !== 0)
8024
        begin
8025
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8026
          test_fail("Other interrupts (except Transmit Buffer) were set");
8027
          fail = fail + 1;
8028
        end
8029
//      end
8030
//      else
8031
//      begin
8032
//        if (data !== 0)
8033
//        begin
8034
//          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
8035
//          test_fail("Any of interrupts (except Transmit Buffer) was set");
8036
//          fail = fail + 1;
8037
//        end
8038
//      end
8039
      // clear interrupts
8040
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8041
      // check WB INT signal
8042
      if (wb_int !== 1'b0)
8043
      begin
8044
        test_fail("WB INT signal should not be set");
8045
        fail = fail + 1;
8046
      end
8047
      // INTERMEDIATE DISPLAYS
8048
if (num_of_bd == 0)
8049
  $display("    ->packet with length %0d sent", (i_length + 4));
8050
else if (num_of_bd == 1)
8051
  $display("    ->packet with length %0d sent", (i_length + 4));
8052
else if (num_of_bd == 2)
8053
  $display("    ->packet with length %0d sent", (i_length + 4));
8054
      // set length (loop variable)
8055
      i_length = i_length + 1;
8056
      // the number of frame transmitted
8057
      num_of_frames = num_of_frames + 1;
8058
      num_of_bd = num_of_bd + 1;
8059
      @(posedge wb_clk);
8060
    end
8061
    // disable TX
8062
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8063
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8064
    @(posedge wb_clk);
8065
    if(fail == 0)
8066
      test_ok;
8067
    else
8068
      fail = 0;
8069
  end
8070
 
8071
 
8072
  ////////////////////////////////////////////////////////////////////
8073
  ////                                                            ////
8074
  ////  Test transmit packets across MAXFL value at               ////
8075
  ////  13 TX buffer decriptors ( 100Mbps ).                      ////
8076
  ////                                                            ////
8077
  ////////////////////////////////////////////////////////////////////
8078
  if (test_num == 11) // without and with padding
8079
  begin
8080
    // TEST 11: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 100Mbps )
8081
    test_name = "TEST 11: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 100Mbps )";
8082
    `TIME; $display("  TEST 11: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 100Mbps )");
8083
 
8084
    // reset MAC registers
8085
    hard_reset;
8086
    // reset MAC and MII LOGIC with soft reset
8087
    reset_mac;
8088
    reset_mii;
8089
    // set wb slave response
8090
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
8091
 
8092
    max_tmp = 0;
8093
    min_tmp = 0;
8094
    num_of_frames = 0;
8095
    num_of_bd = 0;
8096
    // set 13 TX buffer descriptors - must be set before TX enable
8097
    wbm_write(`ETH_TX_BD_NUM, 32'hD, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8098
    // enable TX, set full-duplex mode, NO padding and CRC appending
8099
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
8100
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8101
    // prepare a packet of MAXFL + 10 length
8102
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8103
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
8104
    min_tmp = tmp[31:16];
8105
    st_data = 8'hA3;
8106
    set_tx_packet(`MEMORY_BASE, (max_tmp + 10), st_data); // length without CRC
8107
    // check WB INT signal
8108
    if (wb_int !== 1'b0)
8109
    begin
8110
      test_fail("WB INT signal should not be set");
8111
      fail = fail + 1;
8112
    end
8113
 
8114
    // write to phy's control register for 100Mbps
8115
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
8116
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
8117
    speed = 100;
8118
 
8119
    i_length = (max_tmp - 5);
8120
    while (i_length <= (max_tmp - 3)) // (max_tmp - 4) is the limit
8121
    begin
8122
      $display("   i_length = %0d", i_length);
8123
      // Reset_tx_bd nable interrupt generation
8124
      // unmask interrupts
8125
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
8126
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8127
      // not detect carrier sense in FD and no collision
8128
      eth_phy.carrier_sense_tx_fd_detect(0);
8129
      eth_phy.collision(0);
8130
      // first destination address on ethernet PHY
8131
      eth_phy.set_tx_mem_addr(0);
8132
      // prepare BDs
8133
      if (num_of_bd == 0)
8134
      begin
8135
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8136
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8137
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8138
        set_tx_bd_wrap(2);
8139
        set_tx_bd_ready(0, 0);
8140
      end
8141
      else if (num_of_bd == 1)
8142
        set_tx_bd_ready(1, 1);
8143
      else if (num_of_bd == 2)
8144
        set_tx_bd_ready(2, 2);
8145
      // CHECK END OF TRANSMITION
8146
      check_tx_bd(num_of_bd, data);
8147
        wait (MTxEn === 1'b1); // start transmit
8148
      check_tx_bd(num_of_bd, data);
8149
        if (data[15] !== 1)
8150
        begin
8151
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
8152
          fail = fail + 1;
8153
        end
8154
        wait (MTxEn === 1'b0); // end transmit
8155
        while (data[15] === 1)
8156
        begin
8157
      check_tx_bd(num_of_bd, data);
8158
          @(posedge wb_clk);
8159
        end
8160
        repeat (1) @(posedge wb_clk);
8161
      // check length of a PACKET
8162
      $display("   eth_phy length = %0d", eth_phy.tx_len);
8163
      tmp_len = eth_phy.tx_len;
8164
      #1;
8165
      if (tmp_len != (i_length + 4))
8166
      begin
8167
        test_fail("Wrong length of the packet out from MAC");
8168
        fail = fail + 1;
8169
      end
8170
      // checking packet
8171
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
8172
      if (tmp > 0)
8173
      begin
8174
        test_fail("Wrong data of the transmitted packet");
8175
        fail = fail + 1;
8176
      end
8177
      // check transmited TX packet CRC
8178
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
8179
      if (tmp > 0)
8180
      begin
8181
        test_fail("Wrong CRC of the transmitted packet");
8182
        fail = fail + 1;
8183
      end
8184
      // check WB INT signal
8185
      if (wb_int !== 1'b1)
8186
      begin
8187
        `TIME; $display("*E WB INT signal should be set");
8188
        test_fail("WB INT signal should be set");
8189
        fail = fail + 1;
8190
      end
8191
      // check TX buffer descriptor of a packet
8192
      check_tx_bd(num_of_bd, data);
8193
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
8194
           ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
8195
      begin
8196
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8197
        test_fail("TX buffer descriptor status is not correct");
8198
        fail = fail + 1;
8199
      end
8200
      // check interrupts
8201
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8202
      if ((data & `ETH_INT_TXB) !== 1'b1)
8203
      begin
8204
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8205
        test_fail("Interrupt Transmit Buffer was not set");
8206
        fail = fail + 1;
8207
      end
8208
      if ((data & (~`ETH_INT_TXB)) !== 0)
8209
      begin
8210
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8211
        test_fail("Other interrupts (except Transmit Buffer) were set");
8212
        fail = fail + 1;
8213
      end
8214
      // clear interrupts
8215
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8216
      // check WB INT signal
8217
      if (wb_int !== 1'b0)
8218
      begin
8219
        test_fail("WB INT signal should not be set");
8220
        fail = fail + 1;
8221
      end
8222
      // INTERMEDIATE DISPLAYS
8223
      if (num_of_bd == 0)
8224
        $display("    ->packet with length %0d sent", (i_length + 4));
8225
      else if (num_of_bd == 1)
8226
        $display("    ->packet with length %0d sent", (i_length + 4));
8227
      else if (num_of_bd == 2)
8228
        $display("    ->packet with length %0d sent", (i_length + 4));
8229
      // set length (loop variable)
8230
      i_length = i_length + 1;
8231
      // the number of frame transmitted
8232
      num_of_frames = num_of_frames + 1;
8233
      num_of_bd = num_of_bd + 1;
8234
      @(posedge wb_clk);
8235
    end
8236
    // disable TX
8237
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8238
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8239
    @(posedge wb_clk);
8240
    if(fail == 0)
8241
      test_ok;
8242
    else
8243
      fail = 0;
8244
  end
8245
 
8246
 
8247
  ////////////////////////////////////////////////////////////////////
8248
  ////                                                            ////
8249
  ////  Test transmit packets across changed MAXFL value at       ////
8250
  ////  47 TX buffer decriptors ( 10Mbps ).                       ////
8251
  ////                                                            ////
8252
  ////////////////////////////////////////////////////////////////////
8253
  if (test_num == 12) // without and with padding
8254
  begin
8255
    // TEST 12: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 10Mbps )
8256
    test_name = "TEST 12: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 10Mbps )";
8257
    `TIME; $display("  TEST 12: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 10Mbps )");
8258
 
8259
    // reset MAC registers
8260
    hard_reset;
8261
    // reset MAC and MII LOGIC with soft reset
8262
    reset_mac;
8263
    reset_mii;
8264
    // set wb slave response
8265
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
8266
 
8267
    max_tmp = 0;
8268
    min_tmp = 0;
8269
    num_of_frames = 0;
8270
    num_of_bd = 0;
8271
    // set 47 TX buffer descriptors - must be set before TX enable
8272
    wbm_write(`ETH_TX_BD_NUM, 32'h2F, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8273
    // prepare a packet of MAXFL + 10 length
8274
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8275
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
8276
    min_tmp = tmp[31:16];
8277
    // change MAXFL value
8278
    max_tmp = min_tmp + 53;
8279
    wbm_write(`ETH_PACKETLEN, {min_tmp, max_tmp}, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8280
    st_data = 8'h62;
8281
    set_tx_packet(`MEMORY_BASE, max_tmp, st_data); // length with CRC
8282
    append_tx_crc(`MEMORY_BASE, (max_tmp - 5), 1'b0); // for first packet
8283
    // enable TX, set full-duplex mode, NO padding and NO CRC appending
8284
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD,
8285
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8286
    // check WB INT signal
8287
    if (wb_int !== 1'b0)
8288
    begin
8289
      test_fail("WB INT signal should not be set");
8290
      fail = fail + 1;
8291
    end
8292
 
8293
    // write to phy's control register for 10Mbps
8294
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
8295
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
8296
    speed = 10;
8297
 
8298
    i_length = (max_tmp - 5); // (max_tmp - 1); // not (max_tmp - 5) because NO automatic CRC appending
8299
    while (i_length <= (max_tmp - 3)) // (max_tmp + 1)) // (max_tmp) is the limit
8300
    begin
8301
      $display("   i_length = %0d", i_length);
8302
      // prepare packet's CRC
8303
      if (num_of_bd == 1)
8304
        append_tx_crc(`MEMORY_BASE, (max_tmp - 4), 1'b0); // for second and third packets
8305
      // Reset_tx_bd nable interrupt generation
8306
      // unmask interrupts
8307
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
8308
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8309
      // not detect carrier sense in FD and no collision
8310
      eth_phy.carrier_sense_tx_fd_detect(0);
8311
      eth_phy.collision(0);
8312
      // first destination address on ethernet PHY
8313
      eth_phy.set_tx_mem_addr(0);
8314
      // prepare BDs
8315
      if (num_of_bd == 0)
8316
      begin
8317
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8318
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8319
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8320
        set_tx_bd_wrap(2);
8321
        set_tx_bd_ready(0, 0);
8322
      end
8323
      else if (num_of_bd == 1)
8324
        set_tx_bd_ready(1, 1);
8325
      else if (num_of_bd == 2)
8326
        set_tx_bd_ready(2, 2);
8327
      // CHECK END OF TRANSMITION
8328
      check_tx_bd(num_of_bd, data);
8329
        wait (MTxEn === 1'b1); // start transmit
8330
      check_tx_bd(num_of_bd, data);
8331
        if (data[15] !== 1)
8332
        begin
8333
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
8334
          fail = fail + 1;
8335
        end
8336
        wait (MTxEn === 1'b0); // end transmit
8337
        while (data[15] === 1)
8338
        begin
8339
      check_tx_bd(num_of_bd, data);
8340
          @(posedge wb_clk);
8341
        end
8342
        repeat (1) @(posedge wb_clk);
8343
      // check length of a PACKET
8344
      $display("   eth_phy length = %0d", eth_phy.tx_len);
8345
      tmp_len = eth_phy.tx_len;
8346
      #1;
8347
      if (tmp_len != (i_length + 4))
8348
      begin
8349
        test_fail("Wrong length of the packet out from MAC");
8350
        fail = fail + 1;
8351
      end
8352
      // checking packet
8353
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
8354
      if (tmp > 0)
8355
      begin
8356
        test_fail("Wrong data of the transmitted packet");
8357
        fail = fail + 1;
8358
      end
8359
      // check transmited TX packet CRC
8360
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
8361
      if (tmp > 0)
8362
      begin
8363
        test_fail("Wrong CRC of the transmitted packet");
8364
        fail = fail + 1;
8365
      end
8366
      // check WB INT signal
8367
      if (wb_int !== 1'b1)
8368
      begin
8369
        `TIME; $display("*E WB INT signal should be set");
8370
        test_fail("WB INT signal should be set");
8371
        fail = fail + 1;
8372
      end
8373
      // check TX buffer descriptor of a packet
8374
      check_tx_bd(num_of_bd, data);
8375
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
8376
           ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
8377
      begin
8378
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8379
        test_fail("TX buffer descriptor status is not correct");
8380
        fail = fail + 1;
8381
      end
8382
      // check interrupts
8383
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8384
      if ((data & `ETH_INT_TXB) !== 1'b1)
8385
      begin
8386
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8387
        test_fail("Interrupt Transmit Buffer was not set");
8388
        fail = fail + 1;
8389
      end
8390
      if ((data & (~`ETH_INT_TXB)) !== 0)
8391
      begin
8392
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8393
        test_fail("Other interrupts (except Transmit Buffer) were set");
8394
        fail = fail + 1;
8395
      end
8396
      // clear interrupts
8397
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8398
      // check WB INT signal
8399
      if (wb_int !== 1'b0)
8400
      begin
8401
        test_fail("WB INT signal should not be set");
8402
        fail = fail + 1;
8403
      end
8404
      // INTERMEDIATE DISPLAYS
8405
      if (num_of_bd == 0)
8406
        $display("    ->packet with length %0d sent", (i_length + 4));
8407
      else if (num_of_bd == 1)
8408
        $display("    ->packet with length %0d sent", (i_length + 4));
8409
      else if (num_of_bd == 2)
8410
        $display("    ->packet with length %0d sent", (i_length + 4));
8411
      // set length (loop variable)
8412
      i_length = i_length + 1;
8413
      // the number of frame transmitted
8414
      num_of_frames = num_of_frames + 1;
8415
      num_of_bd = num_of_bd + 1;
8416
      @(posedge wb_clk);
8417
    end
8418
    // disable TX
8419
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8420
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8421
    @(posedge wb_clk);
8422
    if(fail == 0)
8423
      test_ok;
8424
    else
8425
      fail = 0;
8426
  end
8427
 
8428
 
8429
  ////////////////////////////////////////////////////////////////////
8430
  ////                                                            ////
8431
  ////  Test transmit packets across changed MAXFL value at       ////
8432
  ////  47 TX buffer decriptors ( 100Mbps ).                      ////
8433
  ////                                                            ////
8434
  ////////////////////////////////////////////////////////////////////
8435
  if (test_num == 13) // without and with padding
8436
  begin
8437
    // TEST 13: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 100Mbps )
8438
    test_name = "TEST 13: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 100Mbps )";
8439
    `TIME; $display("  TEST 13: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 100Mbps )");
8440
 
8441
    // reset MAC registers
8442
    hard_reset;
8443
    // reset MAC and MII LOGIC with soft reset
8444
    reset_mac;
8445
    reset_mii;
8446
    // set wb slave response
8447
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
8448
 
8449
    max_tmp = 0;
8450
    min_tmp = 0;
8451
    num_of_frames = 0;
8452
    num_of_bd = 0;
8453
    // set 47 TX buffer descriptors - must be set before TX enable
8454
    wbm_write(`ETH_TX_BD_NUM, 32'h2F, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8455
    // prepare a packet of MAXFL + 10 length
8456
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8457
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
8458
    min_tmp = tmp[31:16];
8459
    // change MAXFL value
8460
    max_tmp = min_tmp + 53;
8461
    wbm_write(`ETH_PACKETLEN, {min_tmp, max_tmp}, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8462
    st_data = 8'h62;
8463
    set_tx_packet(`MEMORY_BASE, max_tmp, st_data); // length with CRC
8464
    append_tx_crc(`MEMORY_BASE, (max_tmp - 5), 1'b0); // for first packet
8465
    // enable TX, set full-duplex mode, NO padding and NO CRC appending
8466
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD,
8467
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8468
    // check WB INT signal
8469
    if (wb_int !== 1'b0)
8470
    begin
8471
      test_fail("WB INT signal should not be set");
8472
      fail = fail + 1;
8473
    end
8474
 
8475
    // write to phy's control register for 100Mbps
8476
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
8477
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
8478
    speed = 100;
8479
 
8480
    i_length = (max_tmp - 5); // (max_tmp - 1); // not (max_tmp - 5) because NO automatic CRC appending
8481
    while (i_length <= (max_tmp - 3)) // (max_tmp + 1)) // (max_tmp) is the limit
8482
    begin
8483
      $display("   i_length = %0d", i_length);
8484
      // prepare packet's CRC
8485
      if (num_of_bd == 1)
8486
        append_tx_crc(`MEMORY_BASE, (max_tmp - 4), 1'b0); // for second and third packets
8487
      // Reset_tx_bd nable interrupt generation
8488
      // unmask interrupts
8489
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
8490
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8491
      // not detect carrier sense in FD and no collision
8492
      eth_phy.carrier_sense_tx_fd_detect(0);
8493
      eth_phy.collision(0);
8494
      // first destination address on ethernet PHY
8495
      eth_phy.set_tx_mem_addr(0);
8496
      // prepare BDs
8497
      if (num_of_bd == 0)
8498
      begin
8499
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8500
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8501
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8502
        set_tx_bd_wrap(2);
8503
        set_tx_bd_ready(0, 0);
8504
      end
8505
      else if (num_of_bd == 1)
8506
        set_tx_bd_ready(1, 1);
8507
      else if (num_of_bd == 2)
8508
        set_tx_bd_ready(2, 2);
8509
      // CHECK END OF TRANSMITION
8510
      check_tx_bd(num_of_bd, data);
8511
        wait (MTxEn === 1'b1); // start transmit
8512
      check_tx_bd(num_of_bd, data);
8513
        if (data[15] !== 1)
8514
        begin
8515
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
8516
          fail = fail + 1;
8517
        end
8518
        wait (MTxEn === 1'b0); // end transmit
8519
        while (data[15] === 1)
8520
        begin
8521
      check_tx_bd(num_of_bd, data);
8522
          @(posedge wb_clk);
8523
        end
8524
        repeat (1) @(posedge wb_clk);
8525
      // check length of a PACKET
8526
      $display("   eth_phy length = %0d", eth_phy.tx_len);
8527
      tmp_len = eth_phy.tx_len;
8528
      #1;
8529
      if (tmp_len != (i_length + 4))
8530
      begin
8531
        test_fail("Wrong length of the packet out from MAC");
8532
        fail = fail + 1;
8533
      end
8534
      // checking packet
8535
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
8536
      if (tmp > 0)
8537
      begin
8538
        test_fail("Wrong data of the transmitted packet");
8539
        fail = fail + 1;
8540
      end
8541
      // check transmited TX packet CRC
8542
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
8543
      if (tmp > 0)
8544
      begin
8545
        test_fail("Wrong CRC of the transmitted packet");
8546
        fail = fail + 1;
8547
      end
8548
      // check WB INT signal
8549
      if (wb_int !== 1'b1)
8550
      begin
8551
        `TIME; $display("*E WB INT signal should be set");
8552
        test_fail("WB INT signal should be set");
8553
        fail = fail + 1;
8554
      end
8555
      // check TX buffer descriptor of a packet
8556
      check_tx_bd(num_of_bd, data);
8557
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
8558
           ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
8559
      begin
8560
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8561
        test_fail("TX buffer descriptor status is not correct");
8562
        fail = fail + 1;
8563
      end
8564
      // check interrupts
8565
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8566
      if ((data & `ETH_INT_TXB) !== 1'b1)
8567
      begin
8568
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8569
        test_fail("Interrupt Transmit Buffer was not set");
8570
        fail = fail + 1;
8571
      end
8572
      if ((data & (~`ETH_INT_TXB)) !== 0)
8573
      begin
8574
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8575
        test_fail("Other interrupts (except Transmit Buffer) were set");
8576
        fail = fail + 1;
8577
      end
8578
      // clear interrupts
8579
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8580
      // check WB INT signal
8581
      if (wb_int !== 1'b0)
8582
      begin
8583
        test_fail("WB INT signal should not be set");
8584
        fail = fail + 1;
8585
      end
8586
      // INTERMEDIATE DISPLAYS
8587
      if (num_of_bd == 0)
8588
        $display("    ->packet with length %0d sent", (i_length + 4));
8589
      else if (num_of_bd == 1)
8590
        $display("    ->packet with length %0d sent", (i_length + 4));
8591
      else if (num_of_bd == 2)
8592
        $display("    ->packet with length %0d sent", (i_length + 4));
8593
      // set length (loop variable)
8594
      i_length = i_length + 1;
8595
      // the number of frame transmitted
8596
      num_of_frames = num_of_frames + 1;
8597
      num_of_bd = num_of_bd + 1;
8598
      @(posedge wb_clk);
8599
    end
8600
    // disable TX
8601
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8602
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8603
    @(posedge wb_clk);
8604
    if(fail == 0)
8605
      test_ok;
8606
    else
8607
      fail = 0;
8608
  end
8609
 
8610
 
8611
  ////////////////////////////////////////////////////////////////////
8612
  ////                                                            ////
8613
  ////  Test transmit packets across changed MINFL value at       ////
8614
  ////  7 TX buffer decriptors ( 10Mbps ).                        ////
8615
  ////                                                            ////
8616
  ////////////////////////////////////////////////////////////////////
8617
  if (test_num == 14) // without and with padding
8618
  begin
8619
    // TEST 14: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 10Mbps )
8620
    test_name = "TEST 14: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 10Mbps )";
8621
    `TIME; $display("  TEST 14: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 10Mbps )");
8622
 
8623
    // reset MAC registers
8624
    hard_reset;
8625
    // reset MAC and MII LOGIC with soft reset
8626
    reset_mac;
8627
    reset_mii;
8628
    // set wb slave response
8629
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
8630
 
8631
    max_tmp = 0;
8632
    min_tmp = 0;
8633
    num_of_frames = 0;
8634
    num_of_bd = 0;
8635
    // set 7 TX buffer descriptors - must be set before TX enable
8636
    wbm_write(`ETH_TX_BD_NUM, 32'h7, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8637
    // prepare a packet of MAXFL + 10 length
8638
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8639
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
8640
    min_tmp = tmp[31:16];
8641
    // change MINFL value
8642
    min_tmp = max_tmp - 177;
8643
    wbm_write(`ETH_PACKETLEN, {min_tmp, max_tmp}, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8644
    st_data = 8'h62;
8645
    set_tx_packet(`MEMORY_BASE, min_tmp, st_data); // length without CRC
8646
    // enable TX, set full-duplex mode, padding and CRC appending
8647
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
8648
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8649
    // check WB INT signal
8650
    if (wb_int !== 1'b0)
8651
    begin
8652
      test_fail("WB INT signal should not be set");
8653
      fail = fail + 1;
8654
    end
8655
 
8656
    // write to phy's control register for 10Mbps
8657
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
8658
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
8659
    speed = 10;
8660
 
8661
    i_length = (min_tmp - 5);
8662
    while (i_length <= (min_tmp - 3)) // (min_tmp - 4) is the limit
8663
    begin
8664
      $display("   i_length = %0d", i_length);
8665
      // Reset_tx_bd nable interrupt generation
8666
      // unmask interrupts
8667
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
8668
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8669
      // not detect carrier sense in FD and no collision
8670
      eth_phy.carrier_sense_tx_fd_detect(0);
8671
      eth_phy.collision(0);
8672
      // first destination address on ethernet PHY
8673
      eth_phy.set_tx_mem_addr(0);
8674
      // prepare BDs
8675
      if (num_of_bd == 0)
8676
      begin
8677
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8678
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8679
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8680
        set_tx_bd_wrap(2);
8681
        set_tx_bd_ready(0, 0);
8682
      end
8683
      else if (num_of_bd == 1)
8684
        set_tx_bd_ready(1, 1);
8685
      else if (num_of_bd == 2)
8686
        set_tx_bd_ready(2, 2);
8687
      // CHECK END OF TRANSMITION
8688
      check_tx_bd(num_of_bd, data);
8689
        wait (MTxEn === 1'b1); // start transmit
8690
      check_tx_bd(num_of_bd, data);
8691
        if (data[15] !== 1)
8692
        begin
8693
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
8694
          fail = fail + 1;
8695
        end
8696
        wait (MTxEn === 1'b0); // end transmit
8697
        while (data[15] === 1)
8698
        begin
8699
      check_tx_bd(num_of_bd, data);
8700
          @(posedge wb_clk);
8701
        end
8702
        repeat (1) @(posedge wb_clk);
8703
      // check length of a PACKET
8704
      $display("   eth_phy length = %0d", eth_phy.tx_len);
8705
      tmp_len = eth_phy.tx_len;
8706
      #1;
8707
      if (tmp_len != (i_length + 4))
8708
      begin
8709
        test_fail("Wrong length of the packet out from MAC");
8710
        fail = fail + 1;
8711
      end
8712
      // checking packet
8713
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
8714
      if (tmp > 0)
8715
      begin
8716
        test_fail("Wrong data of the transmitted packet");
8717
        fail = fail + 1;
8718
      end
8719
      // check transmited TX packet CRC
8720
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
8721
      if (tmp > 0)
8722
      begin
8723
        test_fail("Wrong CRC of the transmitted packet");
8724
        fail = fail + 1;
8725
      end
8726
      // check WB INT signal
8727
      if (wb_int !== 1'b1)
8728
      begin
8729
        `TIME; $display("*E WB INT signal should be set");
8730
        test_fail("WB INT signal should be set");
8731
        fail = fail + 1;
8732
      end
8733
      // check TX buffer descriptor of a packet
8734
      check_tx_bd(num_of_bd, data);
8735
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
8736
           ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
8737
      begin
8738
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8739
        test_fail("TX buffer descriptor status is not correct");
8740
        fail = fail + 1;
8741
      end
8742
      // check interrupts
8743
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8744
      if ((data & `ETH_INT_TXB) !== 1'b1)
8745
      begin
8746
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8747
        test_fail("Interrupt Transmit Buffer was not set");
8748
        fail = fail + 1;
8749
      end
8750
      if ((data & (~`ETH_INT_TXB)) !== 0)
8751
      begin
8752
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8753
        test_fail("Other interrupts (except Transmit Buffer) were set");
8754
        fail = fail + 1;
8755
      end
8756
      // clear interrupts
8757
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8758
      // check WB INT signal
8759
      if (wb_int !== 1'b0)
8760
      begin
8761
        test_fail("WB INT signal should not be set");
8762
        fail = fail + 1;
8763
      end
8764
      // INTERMEDIATE DISPLAYS
8765
      if (num_of_bd == 0)
8766
        $display("    ->packet with length %0d sent", (i_length + 4));
8767
      else if (num_of_bd == 1)
8768
        $display("    ->packet with length %0d sent", (i_length + 4));
8769
      else if (num_of_bd == 2)
8770
        $display("    ->packet with length %0d sent", (i_length + 4));
8771
      // set length (loop variable)
8772
      i_length = i_length + 1;
8773
      // the number of frame transmitted
8774
      num_of_frames = num_of_frames + 1;
8775
      num_of_bd = num_of_bd + 1;
8776
      @(posedge wb_clk);
8777
    end
8778
    // disable TX
8779
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8780
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8781
    @(posedge wb_clk);
8782
    if(fail == 0)
8783
      test_ok;
8784
    else
8785
      fail = 0;
8786
  end
8787
 
8788
 
8789
  ////////////////////////////////////////////////////////////////////
8790
  ////                                                            ////
8791
  ////  Test transmit packets across changed MINFL value at       ////
8792
  ////  7 TX buffer decriptors ( 100Mbps ).                       ////
8793
  ////                                                            ////
8794
  ////////////////////////////////////////////////////////////////////
8795
  if (test_num == 15) // without and with padding
8796
  begin
8797
    // TEST 15: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 100Mbps )
8798
    test_name = "TEST 15: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 100Mbps )";
8799
    `TIME; $display("  TEST 15: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 100Mbps )");
8800
 
8801
    // reset MAC registers
8802
    hard_reset;
8803
    // reset MAC and MII LOGIC with soft reset
8804
    reset_mac;
8805
    reset_mii;
8806
    // set wb slave response
8807
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
8808
 
8809
    max_tmp = 0;
8810
    min_tmp = 0;
8811
    num_of_frames = 0;
8812
    num_of_bd = 0;
8813
    // set 7 TX buffer descriptors - must be set before TX enable
8814
    wbm_write(`ETH_TX_BD_NUM, 32'h7, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8815
    // prepare a packet of MAXFL + 10 length
8816
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8817
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
8818
    min_tmp = tmp[31:16];
8819
    // change MINFL value
8820
    min_tmp = max_tmp - 177;
8821
    wbm_write(`ETH_PACKETLEN, {min_tmp, max_tmp}, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8822
    st_data = 8'h62;
8823
    set_tx_packet(`MEMORY_BASE, min_tmp, st_data); // length without CRC
8824
    // enable TX, set full-duplex mode, padding and CRC appending
8825
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
8826
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8827
    // check WB INT signal
8828
    if (wb_int !== 1'b0)
8829
    begin
8830
      test_fail("WB INT signal should not be set");
8831
      fail = fail + 1;
8832
    end
8833
 
8834
    // write to phy's control register for 100Mbps
8835
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
8836
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
8837
    speed = 100;
8838
 
8839
    i_length = (min_tmp - 5);
8840
    while (i_length <= (min_tmp - 3)) // (min_tmp - 4) is the limit
8841
    begin
8842
      $display("   i_length = %0d", i_length);
8843
      // Reset_tx_bd nable interrupt generation
8844
      // unmask interrupts
8845
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
8846
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8847
      // not detect carrier sense in FD and no collision
8848
      eth_phy.carrier_sense_tx_fd_detect(0);
8849
      eth_phy.collision(0);
8850
      // first destination address on ethernet PHY
8851
      eth_phy.set_tx_mem_addr(0);
8852
      // prepare BDs
8853
      if (num_of_bd == 0)
8854
      begin
8855
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8856
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8857
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8858
        set_tx_bd_wrap(2);
8859
        set_tx_bd_ready(0, 0);
8860
      end
8861
      else if (num_of_bd == 1)
8862
        set_tx_bd_ready(1, 1);
8863
      else if (num_of_bd == 2)
8864
        set_tx_bd_ready(2, 2);
8865
      // CHECK END OF TRANSMITION
8866
      check_tx_bd(num_of_bd, data);
8867
        wait (MTxEn === 1'b1); // start transmit
8868
      check_tx_bd(num_of_bd, data);
8869
        if (data[15] !== 1)
8870
        begin
8871
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
8872
          fail = fail + 1;
8873
        end
8874
        wait (MTxEn === 1'b0); // end transmit
8875
        while (data[15] === 1)
8876
        begin
8877
      check_tx_bd(num_of_bd, data);
8878
          @(posedge wb_clk);
8879
        end
8880
        repeat (1) @(posedge wb_clk);
8881
      // check length of a PACKET
8882
      $display("   eth_phy length = %0d", eth_phy.tx_len);
8883
      tmp_len = eth_phy.tx_len;
8884
      #1;
8885
      if (tmp_len != (i_length + 4))
8886
      begin
8887
        test_fail("Wrong length of the packet out from MAC");
8888
        fail = fail + 1;
8889
      end
8890
      // checking packet
8891
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
8892
      if (tmp > 0)
8893
      begin
8894
        test_fail("Wrong data of the transmitted packet");
8895
        fail = fail + 1;
8896
      end
8897
      // check transmited TX packet CRC
8898
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
8899
      if (tmp > 0)
8900
      begin
8901
        test_fail("Wrong CRC of the transmitted packet");
8902
        fail = fail + 1;
8903
      end
8904
      // check WB INT signal
8905
      if (wb_int !== 1'b1)
8906
      begin
8907
        `TIME; $display("*E WB INT signal should be set");
8908
        test_fail("WB INT signal should be set");
8909
        fail = fail + 1;
8910
      end
8911
      // check TX buffer descriptor of a packet
8912
      check_tx_bd(num_of_bd, data);
8913
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
8914
           ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
8915
      begin
8916
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8917
        test_fail("TX buffer descriptor status is not correct");
8918
        fail = fail + 1;
8919
      end
8920
      // check interrupts
8921
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8922
      if ((data & `ETH_INT_TXB) !== 1'b1)
8923
      begin
8924
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8925
        test_fail("Interrupt Transmit Buffer was not set");
8926
        fail = fail + 1;
8927
      end
8928
      if ((data & (~`ETH_INT_TXB)) !== 0)
8929
      begin
8930
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8931
        test_fail("Other interrupts (except Transmit Buffer) were set");
8932
        fail = fail + 1;
8933
      end
8934
      // clear interrupts
8935
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8936
      // check WB INT signal
8937
      if (wb_int !== 1'b0)
8938
      begin
8939
        test_fail("WB INT signal should not be set");
8940
        fail = fail + 1;
8941
      end
8942
      // INTERMEDIATE DISPLAYS
8943
      if (num_of_bd == 0)
8944
        $display("    ->packet with length %0d sent", (i_length + 4));
8945
      else if (num_of_bd == 1)
8946
        $display("    ->packet with length %0d sent", (i_length + 4));
8947
      else if (num_of_bd == 2)
8948
        $display("    ->packet with length %0d sent", (i_length + 4));
8949
      // set length (loop variable)
8950
      i_length = i_length + 1;
8951
      // the number of frame transmitted
8952
      num_of_frames = num_of_frames + 1;
8953
      num_of_bd = num_of_bd + 1;
8954
      @(posedge wb_clk);
8955
    end
8956
    // disable TX
8957
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8958
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8959
    @(posedge wb_clk);
8960
    if(fail == 0)
8961
      test_ok;
8962
    else
8963
      fail = 0;
8964
  end
8965
 
8966
 
8967
  ////////////////////////////////////////////////////////////////////
8968
  ////                                                            ////
8969
  ////  Test transmit packets across MAXFL with HUGEN at          ////
8970
  ////  19 TX buffer decriptors ( 10Mbps ).                       ////
8971
  ////                                                            ////
8972
  ////////////////////////////////////////////////////////////////////
8973
  if (test_num == 16) // without and with padding
8974
  begin
8975
    // TEST 16: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 10Mbps )
8976
    test_name = "TEST 16: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 10Mbps )";
8977
    `TIME; $display("  TEST 16: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 10Mbps )");
8978
 
8979
    // reset MAC registers
8980
    hard_reset;
8981
    // reset MAC and MII LOGIC with soft reset
8982
    reset_mac;
8983
    reset_mii;
8984
    // set wb slave response
8985
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
8986
 
8987
    max_tmp = 0;
8988
    min_tmp = 0;
8989
    num_of_frames = 0;
8990
    num_of_bd = 0;
8991
    // set 19 TX buffer descriptors - must be set before TX enable
8992
    wbm_write(`ETH_TX_BD_NUM, 32'h13, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8993
    // prepare a packet of 64k - 1 length (16'hFFFF)
8994
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8995
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
8996
    min_tmp = tmp[31:16];
8997
    st_data = 8'h8D;
8998
    set_tx_packet(`MEMORY_BASE, 16'hFFFF, st_data); // length with CRC
8999
    // enable TX, set full-duplex mode, NO padding, CRC appending and huge enabled
9000
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN | `ETH_MODER_HUGEN,
9001
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9002
    // check WB INT signal
9003
    if (wb_int !== 1'b0)
9004
    begin
9005
      test_fail("WB INT signal should not be set");
9006
      fail = fail + 1;
9007
    end
9008
 
9009
    // write to phy's control register for 10Mbps
9010
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
9011
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
9012
    speed = 10;
9013
 
9014
    i_length = (max_tmp - 5); // (max_tmp - 4) is the MAXFL limit
9015
    while (i_length <= (16'hFFFF - 4)) // (16'hFFFF - 4) is the limit
9016
    begin
9017
      $display("   i_length = %0d", i_length);
9018
      // Reset_tx_bd nable interrupt generation
9019
      // unmask interrupts
9020
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
9021
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9022
      // not detect carrier sense in FD and no collision
9023
      eth_phy.carrier_sense_tx_fd_detect(0);
9024
      eth_phy.collision(0);
9025
      // first destination address on ethernet PHY
9026
      eth_phy.set_tx_mem_addr(0);
9027
      // prepare BDs
9028
      if (num_of_bd == 0)
9029
      begin
9030
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9031
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9032
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9033
        set_tx_bd(3, 3, (16'hFFFF - 5), 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9034
        set_tx_bd(4, 4, (16'hFFFF - 4), 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9035
        set_tx_bd_wrap(4);
9036
        set_tx_bd_ready(0, 0);
9037
      end
9038
      else if (num_of_bd == 1)
9039
        set_tx_bd_ready(1, 1);
9040
      else if (num_of_bd == 2)
9041
        set_tx_bd_ready(2, 2);
9042
      else if (num_of_bd == 3)
9043
        set_tx_bd_ready(3, 3);
9044
      else if (num_of_bd == 4)
9045
        set_tx_bd_ready(4, 4);
9046
      // CHECK END OF TRANSMITION
9047
      check_tx_bd(num_of_bd, data);
9048
        wait (MTxEn === 1'b1); // start transmit
9049
      check_tx_bd(num_of_bd, data);
9050
        if (data[15] !== 1)
9051
        begin
9052
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
9053
          fail = fail + 1;
9054
        end
9055
        wait (MTxEn === 1'b0); // end transmit
9056
        while (data[15] === 1)
9057
        begin
9058
      check_tx_bd(num_of_bd, data);
9059
          @(posedge wb_clk);
9060
        end
9061
        repeat (1) @(posedge wb_clk);
9062
      // check length of a PACKET
9063
      $display("   eth_phy length = %0d", eth_phy.tx_len);
9064
      tmp_len = eth_phy.tx_len;
9065
      #1;
9066
      if (tmp_len != (i_length + 4))
9067
      begin
9068
        test_fail("Wrong length of the packet out from MAC");
9069
        fail = fail + 1;
9070
      end
9071
      // checking packet
9072
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
9073
      if (tmp > 0)
9074
      begin
9075
        test_fail("Wrong data of the transmitted packet");
9076
        fail = fail + 1;
9077
      end
9078
      // check transmited TX packet CRC
9079
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
9080
      if (tmp > 0)
9081
      begin
9082
        test_fail("Wrong CRC of the transmitted packet");
9083
        fail = fail + 1;
9084
      end
9085
      // check WB INT signal
9086
      if (wb_int !== 1'b1)
9087
      begin
9088
        `TIME; $display("*E WB INT signal should be set");
9089
        test_fail("WB INT signal should be set");
9090
        fail = fail + 1;
9091
      end
9092
      // check TX buffer descriptor of a packet
9093
      check_tx_bd(num_of_bd, data);
9094
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 4)) || // wrap bit
9095
           ((data[15:0] !== 16'h5800) && (num_of_bd < 4)) )   // without wrap bit
9096
      begin
9097
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
9098
        test_fail("TX buffer descriptor status is not correct");
9099
        fail = fail + 1;
9100
      end
9101
      // check interrupts
9102
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9103
      if ((data & `ETH_INT_TXB) !== 1'b1)
9104
      begin
9105
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
9106
        test_fail("Interrupt Transmit Buffer was not set");
9107
        fail = fail + 1;
9108
      end
9109
      if ((data & (~`ETH_INT_TXB)) !== 0)
9110
      begin
9111
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
9112
        test_fail("Other interrupts (except Transmit Buffer) were set");
9113
        fail = fail + 1;
9114
      end
9115
      // clear interrupts
9116
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9117
      // check WB INT signal
9118
      if (wb_int !== 1'b0)
9119
      begin
9120
        test_fail("WB INT signal should not be set");
9121
        fail = fail + 1;
9122
      end
9123
      // INTERMEDIATE DISPLAYS
9124
      $display("    ->packet with length %0d sent", (i_length + 4));
9125
      // set length (loop variable)
9126
      if ((num_of_bd < 2) || (num_of_bd >= 3))
9127
        i_length = i_length + 1;
9128
      else if (num_of_bd == 2)
9129
        i_length = (16'hFFFF - 5);
9130
      // the number of frame transmitted
9131
      num_of_frames = num_of_frames + 1;
9132
      num_of_bd = num_of_bd + 1;
9133
      @(posedge wb_clk);
9134
    end
9135
    // disable TX
9136
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
9137
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9138
    @(posedge wb_clk);
9139
    if(fail == 0)
9140
      test_ok;
9141
    else
9142
      fail = 0;
9143
  end
9144
 
9145
 
9146
  ////////////////////////////////////////////////////////////////////
9147
  ////                                                            ////
9148
  ////  Test transmit packets across MAXFL with HUGEN at          ////
9149
  ////  19 TX buffer decriptors ( 100Mbps ).                      ////
9150
  ////                                                            ////
9151
  ////////////////////////////////////////////////////////////////////
9152
  if (test_num == 17) // without and with padding
9153
  begin
9154
    // TEST 17: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 100Mbps )
9155
    test_name = "TEST 17: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 100Mbps )";
9156
    `TIME; $display("  TEST 17: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 100Mbps )");
9157
 
9158
    // reset MAC registers
9159
    hard_reset;
9160
    // reset MAC and MII LOGIC with soft reset
9161
    reset_mac;
9162
    reset_mii;
9163
    // set wb slave response
9164
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
9165
 
9166
    max_tmp = 0;
9167
    min_tmp = 0;
9168
    num_of_frames = 0;
9169
    num_of_bd = 0;
9170
    // set 19 TX buffer descriptors - must be set before TX enable
9171
    wbm_write(`ETH_TX_BD_NUM, 32'h13, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9172
    // prepare a packet of 64k - 1 length (16'hFFFF)
9173
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9174
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
9175
    min_tmp = tmp[31:16];
9176
    st_data = 8'h8D;
9177
    set_tx_packet(`MEMORY_BASE, 16'hFFFF, st_data); // length with CRC
9178
    // enable TX, set full-duplex mode, NO padding, CRC appending and huge enabled
9179
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN | `ETH_MODER_HUGEN,
9180
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9181
    // check WB INT signal
9182
    if (wb_int !== 1'b0)
9183
    begin
9184
      test_fail("WB INT signal should not be set");
9185
      fail = fail + 1;
9186
    end
9187
 
9188
    // write to phy's control register for 100Mbps
9189
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
9190
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
9191
    speed = 100;
9192
 
9193
    i_length = (max_tmp - 5); // (max_tmp - 4) is the MAXFL limit
9194
    while (i_length <= (16'hFFFF - 4)) // (16'hFFFF - 4) is the limit
9195
    begin
9196
      $display("   i_length = %0d", i_length);
9197
      // Reset_tx_bd nable interrupt generation
9198
      // unmask interrupts
9199
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
9200
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9201
      // not detect carrier sense in FD and no collision
9202
      eth_phy.carrier_sense_tx_fd_detect(0);
9203
      eth_phy.collision(0);
9204
      // first destination address on ethernet PHY
9205
      eth_phy.set_tx_mem_addr(0);
9206
      // prepare BDs
9207
      if (num_of_bd == 0)
9208
      begin
9209
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9210
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9211
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9212
        set_tx_bd(3, 3, (16'hFFFF - 5), 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9213
        set_tx_bd(4, 4, (16'hFFFF - 4), 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9214
        set_tx_bd_wrap(4);
9215
        set_tx_bd_ready(0, 0);
9216
      end
9217
      else if (num_of_bd == 1)
9218
        set_tx_bd_ready(1, 1);
9219
      else if (num_of_bd == 2)
9220
        set_tx_bd_ready(2, 2);
9221
      else if (num_of_bd == 3)
9222
        set_tx_bd_ready(3, 3);
9223
      else if (num_of_bd == 4)
9224
        set_tx_bd_ready(4, 4);
9225
      // CHECK END OF TRANSMITION
9226
      check_tx_bd(num_of_bd, data);
9227
        wait (MTxEn === 1'b1); // start transmit
9228
      check_tx_bd(num_of_bd, data);
9229
        if (data[15] !== 1)
9230
        begin
9231
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
9232
          fail = fail + 1;
9233
        end
9234
        wait (MTxEn === 1'b0); // end transmit
9235
        while (data[15] === 1)
9236
        begin
9237
      check_tx_bd(num_of_bd, data);
9238
          @(posedge wb_clk);
9239
        end
9240
        repeat (1) @(posedge wb_clk);
9241
      // check length of a PACKET
9242
      $display("   eth_phy length = %0d", eth_phy.tx_len);
9243
      tmp_len = eth_phy.tx_len;
9244
      #1;
9245
      if (tmp_len != (i_length + 4))
9246
      begin
9247
        test_fail("Wrong length of the packet out from MAC");
9248
        fail = fail + 1;
9249
      end
9250
      // checking packet
9251
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
9252
      if (tmp > 0)
9253
      begin
9254
        test_fail("Wrong data of the transmitted packet");
9255
        fail = fail + 1;
9256
      end
9257
      // check transmited TX packet CRC
9258
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
9259
      if (tmp > 0)
9260
      begin
9261
        test_fail("Wrong CRC of the transmitted packet");
9262
        fail = fail + 1;
9263
      end
9264
      // check WB INT signal
9265
      if (wb_int !== 1'b1)
9266
      begin
9267
        `TIME; $display("*E WB INT signal should be set");
9268
        test_fail("WB INT signal should be set");
9269
        fail = fail + 1;
9270
      end
9271
      // check TX buffer descriptor of a packet
9272
      check_tx_bd(num_of_bd, data);
9273
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 4)) || // wrap bit
9274
           ((data[15:0] !== 16'h5800) && (num_of_bd < 4)) )   // without wrap bit
9275
      begin
9276
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
9277
        test_fail("TX buffer descriptor status is not correct");
9278
        fail = fail + 1;
9279
      end
9280
      // check interrupts
9281
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9282
      if ((data & `ETH_INT_TXB) !== 1'b1)
9283
      begin
9284
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
9285
        test_fail("Interrupt Transmit Buffer was not set");
9286
        fail = fail + 1;
9287
      end
9288
      if ((data & (~`ETH_INT_TXB)) !== 0)
9289
      begin
9290
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
9291
        test_fail("Other interrupts (except Transmit Buffer) were set");
9292
        fail = fail + 1;
9293
      end
9294
      // clear interrupts
9295
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9296
      // check WB INT signal
9297
      if (wb_int !== 1'b0)
9298
      begin
9299
        test_fail("WB INT signal should not be set");
9300
        fail = fail + 1;
9301
      end
9302
      // INTERMEDIATE DISPLAYS
9303
      $display("    ->packet with length %0d sent", (i_length + 4));
9304
      // set length (loop variable)
9305
      if ((num_of_bd < 2) || (num_of_bd >= 3))
9306
        i_length = i_length + 1;
9307
      else if (num_of_bd == 2)
9308
        i_length = (16'hFFFF - 5);
9309
      // the number of frame transmitted
9310
      num_of_frames = num_of_frames + 1;
9311
      num_of_bd = num_of_bd + 1;
9312
      @(posedge wb_clk);
9313
    end
9314
    // disable TX
9315
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
9316
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9317
    @(posedge wb_clk);
9318
    if(fail == 0)
9319
      test_ok;
9320
    else
9321
      fail = 0;
9322
  end
9323
 
9324
 
9325
  ////////////////////////////////////////////////////////////////////
9326
  ////                                                            ////
9327
  ////  Test IPG during Back-to-Back transmit at                  ////
9328
  ////  88 TX buffer decriptors ( 10Mbps ).                       ////
9329
  ////                                                            ////
9330
  ////////////////////////////////////////////////////////////////////
9331
  if (test_num == 18) // without and with padding
9332
  begin
9333
    // TEST 18: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 10Mbps )
9334
    test_name = "TEST 18: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 10Mbps )";
9335
    `TIME; $display("  TEST 18: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 10Mbps )");
9336
 
9337
    // reset MAC registers
9338
    hard_reset;
9339
    // reset MAC and MII LOGIC with soft reset
9340
    reset_mac;
9341
    reset_mii;
9342
    // set wb slave response
9343
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
9344
 
9345
    max_tmp = 0;
9346
    min_tmp = 0;
9347
    num_of_frames = 0;
9348
    num_of_bd = 0;
9349
    tmp_ipgt = 0;
9350
    // set 88 TX buffer descriptors - must be set before TX enable
9351
    wbm_write(`ETH_TX_BD_NUM, 32'h58, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9352
    // enable TX, set full-duplex mode, NO padding and CRC appending
9353
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9354
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9355
    // prepare two packets of MAXFL length
9356
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9357
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
9358
    min_tmp = tmp[31:16];
9359
    st_data = 8'h29;
9360
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
9361
    // check WB INT signal
9362
    if (wb_int !== 1'b0)
9363
    begin
9364
      test_fail("WB INT signal should not be set");
9365
      fail = fail + 1;
9366
    end
9367
 
9368
    // write to phy's control register for 10Mbps
9369
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
9370
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
9371
    speed = 10;
9372
 
9373
    i_length = (min_tmp - 4);
9374
    while (i_length < (max_tmp - 4))
9375
    begin
9376
      // disable TX, set full-duplex mode, NO padding and CRC appending
9377
      wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9378
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9379
      // set IPGT register
9380
      wbm_write(`ETH_IPGT, tmp_ipgt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9381
      // enable TX, set full-duplex mode, NO padding and CRC appending
9382
      wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9383
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9384
      // Reset_tx_bd enable interrupt generation
9385
      // unmask interrupts
9386
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
9387
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9388
      // not detect carrier sense in FD and no collision
9389
      eth_phy.carrier_sense_tx_fd_detect(0);
9390
      eth_phy.collision(0);
9391
      // first destination address on ethernet PHY
9392
      eth_phy.set_tx_mem_addr(0);
9393
      // prepare BDs
9394
      if (num_of_bd == 0)
9395
      begin
9396
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9397
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9398
        set_tx_bd_wrap(1);
9399
        set_tx_bd_ready(0, 0);
9400
        set_tx_bd_ready(1, 1);
9401
      end
9402
      // CHECK END OF TWO TRANSMITIONs
9403
      // wait for first transmit to end
9404
      check_tx_bd(num_of_bd, data);
9405
      wait (MTxEn === 1'b1); // start transmit
9406
      if (data[15] !== 1)
9407
      begin
9408
        test_fail("Wrong buffer descriptor's ready bit read out from MAC");
9409
        fail = fail + 1;
9410
      end
9411
      wait (MTxEn === 1'b0); // end transmit
9412
      num_of_frames = num_of_frames + 1;
9413
      num_of_bd = num_of_bd + 1;
9414
      #Tp;
9415
      // destination address on ethernet PHY
9416
      eth_phy.set_tx_mem_addr(0);
9417
      i1 = 0;
9418
      i2 = 0;
9419
      // count IPG clock periods
9420
      fork
9421
        begin
9422
          wait (MTxEn === 1'b1); // start second transmit
9423
          #Tp;
9424
          disable count_rising;
9425
          disable count_falling;
9426
        end
9427
        begin: count_rising
9428
          forever
9429
          begin
9430
            @(posedge mtx_clk);
9431
            i1 = i1 + 1;
9432
            #Tp;
9433
          end
9434
        end
9435
        begin: count_falling
9436
          forever
9437
          begin
9438
            @(negedge mtx_clk);
9439
            i2 = i2 + 1;
9440
            #Tp;
9441
          end
9442
        end
9443
      join
9444
      // check IPG length - INTERMEDIATE DISPLAYS
9445
      if((i1 == i2) && (i1 >= (tmp_ipgt + 3)))
9446
      begin
9447
        $display("    ->IPG with %0d mtx_clk periods (min %0d) between packets with lengths %0d and %0d checked",
9448
                  i1, (tmp_ipgt + 3), (i_length + 4), (i_length + 4 + 1));
9449
      end
9450
      else
9451
      begin
9452
        `TIME; $display("*E IPG is not correct: (%0d + %0d) / 2, requested: %d", i1, i2, (tmp_ipgt + 3));
9453
        fail = fail + 1;
9454
        test_fail("IPG is not correct");
9455
      end
9456
      // wait for second transmit to end
9457
      wait (MTxEn === 1'b0); // end second transmit
9458
      while (data[15] === 1)
9459
      begin
9460
        check_tx_bd(num_of_bd, data);
9461
        @(posedge wb_clk);
9462
      end
9463
      repeat (1) @(posedge wb_clk);
9464
      // check length of a second PACKET
9465
      tmp_len = eth_phy.tx_len;
9466
      #1;
9467
      if (tmp_len != (i_length + 4 + 1))
9468
      begin
9469
        test_fail("Wrong length of second packet out from MAC");
9470
        fail = fail + 1;
9471
      end
9472
      // checking second packet
9473
      check_tx_packet(`MEMORY_BASE, 0, (i_length + 1), tmp);
9474
      if (tmp > 0)
9475
      begin
9476
        test_fail("Wrong data of second transmitted packet");
9477
        fail = fail + 1;
9478
      end
9479
      // check second transmited TX packet CRC
9480
      check_tx_crc(0, (i_length + 1), 1'b0, tmp); // length without CRC
9481
      if (tmp > 0)
9482
      begin
9483
        test_fail("Wrong CRC of second transmitted packet");
9484
        fail = fail + 1;
9485
      end
9486
      // check WB INT signal
9487
      if (wb_int !== 1'b1)
9488
      begin
9489
        `TIME; $display("*E WB INT signal should be set");
9490
        test_fail("WB INT signal should be set");
9491
        fail = fail + 1;
9492
      end
9493
      // check TX buffer descriptor of a packet
9494
      check_tx_bd(num_of_bd, data);
9495
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
9496
           ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
9497
      begin
9498
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
9499
        test_fail("TX buffer descriptor status is not correct");
9500
        fail = fail + 1;
9501
      end
9502
      // check interrupts
9503
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9504
      if ((data & `ETH_INT_TXB) !== 1'b1)
9505
      begin
9506
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
9507
        test_fail("Interrupt Transmit Buffer was not set");
9508
        fail = fail + 1;
9509
      end
9510
      if ((data & (~`ETH_INT_TXB)) !== 0)
9511
      begin
9512
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
9513
        test_fail("Other interrupts (except Transmit Buffer) were set");
9514
        fail = fail + 1;
9515
      end
9516
      // clear interrupts
9517
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9518
      // check WB INT signal
9519
      if (wb_int !== 1'b0)
9520
      begin
9521
        test_fail("WB INT signal should not be set");
9522
        fail = fail + 1;
9523
      end
9524
      // set length (LOOP variable)
9525
      if ((tmp_ipgt + 3) < 130) // tmp_ipgt < 124
9526
        i_length = i_length + 2;
9527
      else
9528
        i_length = (max_tmp - 4);
9529
      // set IPGT
9530
      if ((tmp_ipgt + 3) < 10)
9531
        tmp_ipgt = tmp_ipgt + 1;
9532
      else if ((tmp_ipgt + 3) < 24)
9533
        tmp_ipgt = tmp_ipgt + 7;
9534
      else if ((tmp_ipgt + 3) == 24)
9535
        tmp_ipgt = 38 - 3;
9536
      else if ((tmp_ipgt + 3) == 38)
9537
        tmp_ipgt = 72 - 3;
9538
      else if ((tmp_ipgt + 3) == 72)
9539
        tmp_ipgt = 130 - 3; // 124 - 3
9540
      // the number of frame transmitted
9541
      num_of_frames = num_of_frames + 1;
9542
      num_of_bd = 0;
9543
      @(posedge wb_clk);
9544
    end
9545
    // disable TX
9546
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
9547
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9548
    @(posedge wb_clk);
9549
    if(fail == 0)
9550
      test_ok;
9551
    else
9552
      fail = 0;
9553
  end
9554
 
9555
 
9556
  ////////////////////////////////////////////////////////////////////
9557
  ////                                                            ////
9558
  ////  Test IPG during Back-to-Back transmit at                  ////
9559
  ////  88 TX buffer decriptors ( 100Mbps ).                      ////
9560
  ////                                                            ////
9561
  ////////////////////////////////////////////////////////////////////
9562
  if (test_num == 19) // without and with padding
9563
  begin
9564
    // TEST 19: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 100Mbps )
9565
    test_name = "TEST 19: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 100Mbps )";
9566
    `TIME; $display("  TEST 19: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 100Mbps )");
9567
 
9568
    // reset MAC registers
9569
    hard_reset;
9570
    // reset MAC and MII LOGIC with soft reset
9571
    reset_mac;
9572
    reset_mii;
9573
    // set wb slave response
9574
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
9575
 
9576
    max_tmp = 0;
9577
    min_tmp = 0;
9578
    num_of_frames = 0;
9579
    num_of_bd = 0;
9580
    tmp_ipgt = 0;
9581
    // set 88 TX buffer descriptors - must be set before TX enable
9582
    wbm_write(`ETH_TX_BD_NUM, 32'h58, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9583
    // enable TX, set full-duplex mode, NO padding and CRC appending
9584
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9585
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9586
    // prepare two packets of MAXFL length
9587
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9588
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
9589
    min_tmp = tmp[31:16];
9590
    st_data = 8'h29;
9591
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
9592
    // check WB INT signal
9593
    if (wb_int !== 1'b0)
9594
    begin
9595
      test_fail("WB INT signal should not be set");
9596
      fail = fail + 1;
9597
    end
9598
 
9599
    // write to phy's control register for 100Mbps
9600
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
9601
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
9602
    speed = 100;
9603
 
9604
    i_length = (min_tmp - 4);
9605
    while (i_length < (max_tmp - 4))
9606
    begin
9607
      // disable TX, set full-duplex mode, NO padding and CRC appending
9608
      wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9609
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9610
      // set IPGT register
9611
      wbm_write(`ETH_IPGT, tmp_ipgt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9612
      // enable TX, set full-duplex mode, NO padding and CRC appending
9613
      wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9614
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9615
      // Reset_tx_bd enable interrupt generation
9616
      // unmask interrupts
9617
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
9618
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9619
      // not detect carrier sense in FD and no collision
9620
      eth_phy.carrier_sense_tx_fd_detect(0);
9621
      eth_phy.collision(0);
9622
      // first destination address on ethernet PHY
9623
      eth_phy.set_tx_mem_addr(0);
9624
      // prepare BDs
9625
      if (num_of_bd == 0)
9626
      begin
9627
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9628
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9629
        set_tx_bd_wrap(1);
9630
        set_tx_bd_ready(0, 0);
9631
        set_tx_bd_ready(1, 1);
9632
      end
9633
      // CHECK END OF TWO TRANSMITIONs
9634
      // wait for first transmit to end
9635
      check_tx_bd(num_of_bd, data);
9636
      wait (MTxEn === 1'b1); // start transmit
9637
      if (data[15] !== 1)
9638
      begin
9639
        test_fail("Wrong buffer descriptor's ready bit read out from MAC");
9640
        fail = fail + 1;
9641
      end
9642
      wait (MTxEn === 1'b0); // end transmit
9643
      num_of_frames = num_of_frames + 1;
9644
      num_of_bd = num_of_bd + 1;
9645
      #Tp;
9646
      // destination address on ethernet PHY
9647
      eth_phy.set_tx_mem_addr(0);
9648
      i1 = 0;
9649
      i2 = 0;
9650
      // count IPG clock periods
9651
      fork
9652
        begin
9653
          wait (MTxEn === 1'b1); // start second transmit
9654
          #Tp;
9655
          disable count_rising1;
9656
          disable count_falling1;
9657
        end
9658
        begin: count_rising1
9659
          forever
9660
          begin
9661
            @(posedge mtx_clk);
9662
            i1 = i1 + 1;
9663
            #Tp;
9664
          end
9665
        end
9666
        begin: count_falling1
9667
          forever
9668
          begin
9669
            @(negedge mtx_clk);
9670
            i2 = i2 + 1;
9671
            #Tp;
9672
          end
9673
        end
9674
      join
9675
      // check IPG length - INTERMEDIATE DISPLAYS
9676
      if((i1 == i2) && (i1 >= (tmp_ipgt + 3)))
9677
      begin
9678
        $display("    ->IPG with %0d mtx_clk periods (min %0d) between packets with lengths %0d and %0d checked",
9679
                  i1, (tmp_ipgt + 3), (i_length + 4), (i_length + 4 + 1));
9680
      end
9681
      else
9682
      begin
9683
        `TIME; $display("*E IPG is not correct: (%0d + %0d) / 2, requested: %d", i1, i2, (tmp_ipgt + 3));
9684
        fail = fail + 1;
9685
        test_fail("IPG is not correct");
9686
      end
9687
      // wait for second transmit to end
9688
      wait (MTxEn === 1'b0); // end second transmit
9689
      while (data[15] === 1)
9690
      begin
9691
        check_tx_bd(num_of_bd, data);
9692
        @(posedge wb_clk);
9693
      end
9694
      repeat (1) @(posedge wb_clk);
9695
      // check length of a second PACKET
9696
      tmp_len = eth_phy.tx_len;
9697
      #1;
9698
      if (tmp_len != (i_length + 4 + 1))
9699
      begin
9700
        test_fail("Wrong length of second packet out from MAC");
9701
        fail = fail + 1;
9702
      end
9703
      // checking second packet
9704
      check_tx_packet(`MEMORY_BASE, 0, (i_length + 1), tmp);
9705
      if (tmp > 0)
9706
      begin
9707
        test_fail("Wrong data of second transmitted packet");
9708
        fail = fail + 1;
9709
      end
9710
      // check second transmited TX packet CRC
9711
      check_tx_crc(0, (i_length + 1), 1'b0, tmp); // length without CRC
9712
      if (tmp > 0)
9713
      begin
9714
        test_fail("Wrong CRC of second transmitted packet");
9715
        fail = fail + 1;
9716
      end
9717
      // check WB INT signal
9718
      if (wb_int !== 1'b1)
9719
      begin
9720
        `TIME; $display("*E WB INT signal should be set");
9721
        test_fail("WB INT signal should be set");
9722
        fail = fail + 1;
9723
      end
9724
      // check TX buffer descriptor of a packet
9725
      check_tx_bd(num_of_bd, data);
9726
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
9727
           ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
9728
      begin
9729
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
9730
        test_fail("TX buffer descriptor status is not correct");
9731
        fail = fail + 1;
9732
      end
9733
      // check interrupts
9734
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9735
      if ((data & `ETH_INT_TXB) !== 1'b1)
9736
      begin
9737
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
9738
        test_fail("Interrupt Transmit Buffer was not set");
9739
        fail = fail + 1;
9740
      end
9741
      if ((data & (~`ETH_INT_TXB)) !== 0)
9742
      begin
9743
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
9744
        test_fail("Other interrupts (except Transmit Buffer) were set");
9745
        fail = fail + 1;
9746
      end
9747
      // clear interrupts
9748
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9749
      // check WB INT signal
9750
      if (wb_int !== 1'b0)
9751
      begin
9752
        test_fail("WB INT signal should not be set");
9753
        fail = fail + 1;
9754
      end
9755
      // set length (LOOP variable)
9756
      if ((tmp_ipgt + 3) < 130) // tmp_ipgt < 124
9757
        i_length = i_length + 2;
9758
      else
9759
        i_length = (max_tmp - 4);
9760
      // set IPGT
9761
      if ((tmp_ipgt + 3) < 10)
9762
        tmp_ipgt = tmp_ipgt + 1;
9763
      else if ((tmp_ipgt + 3) < 24)
9764
        tmp_ipgt = tmp_ipgt + 7;
9765
      else if ((tmp_ipgt + 3) == 24)
9766
        tmp_ipgt = 38 - 3;
9767
      else if ((tmp_ipgt + 3) == 38)
9768
        tmp_ipgt = 72 - 3;
9769
      else if ((tmp_ipgt + 3) == 72)
9770
        tmp_ipgt = 130 - 3; // 124 - 3
9771
      // the number of frame transmitted
9772
      num_of_frames = num_of_frames + 1;
9773
      num_of_bd = 0;
9774
      @(posedge wb_clk);
9775
    end
9776
    // disable TX
9777
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
9778
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9779
    @(posedge wb_clk);
9780
    if(fail == 0)
9781
      test_ok;
9782
    else
9783
      fail = 0;
9784
  end
9785
 
9786
 
9787
  ////////////////////////////////////////////////////////////////////
9788
  ////                                                            ////
9789
  ////  Test transmit packets after TX under-run on each packet's ////
9790
  ////  byte at 2 TX buffer decriptors ( 10Mbps ).                ////
9791
  ////                                                            ////
9792
  ////////////////////////////////////////////////////////////////////
9793
  if (test_num == 20) // without padding
9794
  begin
9795
    // TEST 20: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 10Mbps )
9796
    test_name = "TEST 20: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 10Mbps )";
9797
    `TIME;
9798
    $display("  TEST 20: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 10Mbps )");
9799
 
9800
    // reset MAC registers
9801
    hard_reset;
9802
    // reset MAC and MII LOGIC with soft reset
9803
    reset_mac;
9804
    reset_mii;
9805
    // set wb slave response
9806
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
9807
 
9808
    max_tmp = 0;
9809
    min_tmp = 0;
9810
    // set 2 TX buffer descriptors - must be set before TX enable
9811
    wbm_write(`ETH_TX_BD_NUM, 32'h2, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9812
    // enable TX, set full-duplex mode, NO padding and CRC appending
9813
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN |/* `ETH_MODER_PAD |*/ `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9814
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9815
    // prepare a packet of MAXFL length
9816
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9817
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
9818
    min_tmp = tmp[31:16];
9819
    st_data = 8'h99;
9820
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
9821
    // read IPG value
9822
    wbm_read(`ETH_IPGT, tmp_ipgt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9823
    // check WB INT signal
9824
    if (wb_int !== 1'b0)
9825
    begin
9826
      test_fail("WB INT signal should not be set");
9827
      fail = fail + 1;
9828
    end
9829
 
9830
    // write to phy's control register for 10Mbps
9831
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
9832
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
9833
    speed = 10;
9834
 
9835 223 tadejm
    num_of_frames = 40; // (0..3) => start under-run on first word
9836 209 tadejm
    num_of_bd = 0;
9837
    i_data = 3; // (3) => one BYTE read in first word - FIRST byte
9838
    i_length = (min_tmp + 4);
9839
    while (i_length < (max_tmp - 4))
9840
    begin
9841
      // Reset_tx_bd enable interrupt generation
9842
      // unmask interrupts
9843
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
9844
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9845
      // not detect carrier sense in FD and no collision
9846
      eth_phy.carrier_sense_tx_fd_detect(0);
9847
      eth_phy.collision(0);
9848
      // first destination address on ethernet PHY
9849
      eth_phy.set_tx_mem_addr(0);
9850
      // prepare BDs
9851
      if (num_of_bd == 0)
9852
      begin
9853
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_data[1:0]));
9854
        set_tx_bd(1, 1, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9855
        set_tx_bd_wrap(1);
9856
        // set wb slave response: ACK (response), wbs_waits[2:0] (waits before response), 
9857
        //                       wbs_retries[7:0] (RTYs before ACK if RTY response selected)
9858 223 tadejm
        #1 wb_slave.cycle_response(`ACK_RESPONSE, 3'h0, 8'h0);
9859 209 tadejm
        set_tx_bd_ready(1, 1);
9860
        set_tx_bd_ready(0, 0);
9861
      end
9862
      // frame under-run checking
9863
      frame_started = 0;
9864
      frame_ended = 0;
9865
      wait_for_frame = 0;
9866
      fork
9867
        begin
9868
          // for every 4 frames bytes 1, 2, 3 and 4 respectively are read in first word => 1 ACK
9869
          // in other words 4 bytes are read, since length is MINFL => num_of_frames[31:2] ACKs
9870
          repeat ((num_of_frames[31:2] + 1'b1)) @(posedge eth_ma_wb_ack_i);
9871
          @(negedge eth_ma_wb_ack_i); // wait for last ACK to finish
9872
          // set wb slave response: ACK (response), wbs_waits[2:0] (waits before response), 
9873
          //                       wbs_retries[7:0] (RTYs before ACK if RTY response selected)
9874 223 tadejm
          #1 wb_slave.cycle_response(`NO_RESPONSE, 3'h0, 8'hFF);
9875 209 tadejm
          // wait for synchronization and some additional clocks
9876
          wait_for_frame = 1;
9877
          // wait for frame
9878
          wait ((wait_for_frame == 0) || (frame_started == 1))
9879
          if ((wait_for_frame == 0) && (frame_started == 0)) // frame didn't start
9880
          begin
9881
            disable check_fr;
9882
          end
9883
          else if ((wait_for_frame == 1) && (frame_started == 1)) // frame started
9884
          begin
9885
            disable wait_fr;
9886
            wait (frame_ended == 1);
9887
          end
9888
          repeat (2) @(posedge wb_clk);
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
          wb_slave.cycle_response(`ACK_RESPONSE, 3'h0, 8'h0);
9892 209 tadejm
        end
9893
        begin: wait_fr
9894
          wait (wait_for_frame == 1)
9895
          begin
9896
            // wait for synchronization and some additional clocks
9897
            repeat (3) @(posedge wb_clk);
9898
            repeat (2 * tmp_ipgt) @(posedge mtx_clk);
9899
            repeat (2) @(posedge wb_clk);
9900
            repeat (2) @(posedge mtx_clk);
9901
            wait_for_frame = 0;
9902
          end
9903
        end
9904
        begin: check_fr
9905
          // wait for frame to start
9906
          @(posedge MTxEn);
9907
          frame_started = 1;
9908 223 tadejm
`TIME; $display("  Under-run (on %0d. byte) frame started", (num_of_frames + 1));
9909 209 tadejm
          // wait for frame to end due to under-run
9910
          @(negedge MTxEn);
9911
          frame_ended = 1;
9912 223 tadejm
`TIME; $display("  Under-run frame ended");
9913 209 tadejm
        end
9914
      join
9915
      // wait for first transmit to end, if under-run didn't happen
9916
      if (frame_ended == 0)
9917
      begin
9918
        // WAIT FOR FIRST TRANSMIT
9919
        check_tx_bd(num_of_bd, data);
9920
        wait (MTxEn === 1'b1); // start first transmit
9921
        if (data[15] !== 1)
9922
        begin
9923
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
9924
          fail = fail + 1;
9925
        end
9926
        wait (MTxEn === 1'b0); // end first transmit
9927
        while (data[15] === 1)
9928
        begin
9929
          check_tx_bd(num_of_bd, data);
9930
          @(posedge wb_clk);
9931
        end
9932
        repeat (1) @(posedge wb_clk);
9933
        // CHECK FIRST FRAME
9934
        // check length of a first PACKET
9935
        tmp_len = eth_phy.tx_len;
9936
        #1;
9937
        if (tmp_len != (i_length + 4))
9938
        begin
9939 223 tadejm
          `TIME; $display("*E Wrong length of first packet out from MAC");
9940
          test_fail("Wrong length of first packet out from MAC");
9941 209 tadejm
          fail = fail + 1;
9942
        end
9943
        // checking first packet
9944
        check_tx_packet((`MEMORY_BASE + i_data[1:0]), 0, (i_length), tmp);
9945
        if (tmp > 0)
9946
        begin
9947 223 tadejm
          `TIME; $display("*E Wrong data of first transmitted packet");
9948
          test_fail("Wrong data of first transmitted packet");
9949 209 tadejm
          fail = fail + 1;
9950
        end
9951
        // check first transmited TX packet CRC
9952
        check_tx_crc(0, (i_length), 1'b0, tmp); // length without CRC
9953
        if (tmp > 0)
9954
        begin
9955 223 tadejm
          `TIME; $display("*E Wrong CRC of first transmitted packet");
9956
          test_fail("Wrong CRC of first transmitted packet");
9957 209 tadejm
          fail = fail + 1;
9958
        end
9959
        // check WB INT signal
9960
        if (wb_int !== 1'b1)
9961
        begin
9962
          `TIME; $display("*E WB INT signal should be set");
9963
          test_fail("WB INT signal should be set");
9964
          fail = fail + 1;
9965
        end
9966
        // check TX buffer descriptor of a packet
9967
        check_tx_bd(num_of_bd, data);
9968
        if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
9969
             ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
9970
        begin
9971
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
9972
          test_fail("TX buffer descriptor status is not correct");
9973
          fail = fail + 1;
9974
        end
9975
        // check interrupts
9976
        wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9977
        if ((data & `ETH_INT_TXB) !== 1'b1)
9978
        begin
9979
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
9980
          test_fail("Interrupt Transmit Buffer was not set");
9981
          fail = fail + 1;
9982
        end
9983
        if ((data & (~`ETH_INT_TXB)) !== 0)
9984
        begin
9985
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
9986
          test_fail("Other interrupts (except Transmit Buffer) were set");
9987
          fail = fail + 1;
9988
        end
9989
        // clear interrupts
9990
        wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9991
        // check WB INT signal
9992
        if (wb_int !== 1'b0)
9993
        begin
9994
          test_fail("WB INT signal should not be set");
9995
          fail = fail + 1;
9996
        end
9997
      end
9998 223 tadejm
      else
9999
      begin
10000
        // CHECK FIRST FRAME
10001
        // check length of a first PACKET
10002
        tmp_len = eth_phy.tx_len_err;
10003
        #1;
10004
        if (tmp_len != (num_of_frames + (4 - i_data)))
10005
        begin
10006
          `TIME; $display("*E Wrong length of first packet out from MAC");
10007
          test_fail("Wrong length of first packet out from MAC");
10008
          fail = fail + 1;
10009
        end
10010
        // checking first packet
10011
        check_tx_packet((`MEMORY_BASE + i_data[1:0]), 0, (num_of_frames), tmp);
10012
        if (tmp > 0)
10013
        begin
10014
          `TIME; $display("*E Wrong data of first transmitted packet");
10015
          test_fail("Wrong data of first transmitted packet");
10016
          fail = fail + 1;
10017
        end
10018
        // check WB INT signal
10019
        if (wb_int !== 1'b1)
10020
        begin
10021
          `TIME; $display("*E WB INT signal should be set");
10022
          test_fail("WB INT signal should be set");
10023
          fail = fail + 1;
10024
        end
10025
        // check TX buffer descriptor of a packet
10026
        check_tx_bd(num_of_bd, data);
10027
        if ( ((data[15:0] !== 16'h7900) && (num_of_bd == 1)) || // under-run, wrap bit
10028
             ((data[15:0] !== 16'h5900) && (num_of_bd < 1)) )   // under-run, without wrap bit
10029
        begin
10030
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
10031
          test_fail("TX buffer descriptor status is not correct");
10032
          fail = fail + 1;
10033
        end
10034
        // check interrupts
10035
        wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10036
        if ((data & `ETH_INT_TXE) !== 2'b10)
10037
        begin
10038
          `TIME; $display("*E Interrupt Transmit Error was not set, interrupt reg: %0h", data);
10039
          test_fail("Interrupt Transmit Buffer was not set");
10040
          fail = fail + 1;
10041
        end
10042
        if ((data & (~`ETH_INT_TXE)) !== 0)
10043
        begin
10044
          `TIME; $display("*E Other interrupts (except Transmit Error) were set, interrupt reg: %0h", data);
10045
          test_fail("Other interrupts (except Transmit Buffer) were set");
10046
          fail = fail + 1;
10047
        end
10048
        // clear interrupts
10049
        wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10050
        // check WB INT signal
10051
        if (wb_int !== 1'b0)
10052
        begin
10053
          test_fail("WB INT signal should not be set");
10054
          fail = fail + 1;
10055
        end
10056
      end
10057 209 tadejm
      num_of_bd = num_of_bd + 1;
10058
      // destination address on ethernet PHY
10059
      eth_phy.set_tx_mem_addr(0);
10060 223 tadejm
      // WAIT FOR SECOND TRANSMIT
10061 209 tadejm
      check_tx_bd(num_of_bd, data);
10062
      wait (MTxEn === 1'b1); // start first transmit
10063
      if (data[15] !== 1)
10064
      begin
10065
        test_fail("Wrong buffer descriptor's ready bit read out from MAC");
10066
        fail = fail + 1;
10067
      end
10068
      wait (MTxEn === 1'b0); // end first transmit
10069
      while (data[15] === 1)
10070
      begin
10071
        check_tx_bd(num_of_bd, data);
10072
        @(posedge wb_clk);
10073
      end
10074
      repeat (1) @(posedge wb_clk);
10075
      // CHECK SECOND FRAME
10076
      // check length of a second PACKET
10077 223 tadejm
if (frame_ended == 1'b1)
10078
begin
10079
`TIME; $display("  Second frame after under-run ended");
10080
end
10081 209 tadejm
      tmp_len = eth_phy.tx_len;
10082
      #1;
10083
      if (tmp_len != (i_length + 4))
10084
      begin
10085 223 tadejm
        `TIME; $display("*E Wrong length of second packet out from MAC");
10086 209 tadejm
        test_fail("Wrong length of second packet out from MAC");
10087
        fail = fail + 1;
10088
      end
10089
      // checking second packet
10090
      check_tx_packet(`MEMORY_BASE, 0, (i_length), tmp);
10091
      if (tmp > 0)
10092
      begin
10093 223 tadejm
        `TIME; $display("*E Wrong data of second transmitted packet");
10094 209 tadejm
        test_fail("Wrong data of second transmitted packet");
10095
        fail = fail + 1;
10096
      end
10097
      // check second transmited TX packet CRC
10098
      check_tx_crc(0, (i_length), 1'b0, tmp); // length without CRC
10099
      if (tmp > 0)
10100
      begin
10101 223 tadejm
        `TIME; $display("*E Wrong CRC of second transmitted packet");
10102 209 tadejm
        test_fail("Wrong CRC of second transmitted packet");
10103
        fail = fail + 1;
10104
      end
10105
      // check WB INT signal
10106
      if (wb_int !== 1'b1)
10107
      begin
10108
        `TIME; $display("*E WB INT signal should be set");
10109
        test_fail("WB INT signal should be set");
10110
        fail = fail + 1;
10111
      end
10112
      // check TX buffer descriptor of a packet
10113
      check_tx_bd(num_of_bd, data);
10114
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
10115
           ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
10116
      begin
10117
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
10118
        test_fail("TX buffer descriptor status is not correct");
10119
        fail = fail + 1;
10120
      end
10121
      // check interrupts
10122
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10123
      if ((data & `ETH_INT_TXB) !== 1'b1)
10124
      begin
10125
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
10126
        test_fail("Interrupt Transmit Buffer was not set");
10127
        fail = fail + 1;
10128
      end
10129
      if ((data & (~`ETH_INT_TXB)) !== 0)
10130
      begin
10131
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
10132
        test_fail("Other interrupts (except Transmit Buffer) were set");
10133
        fail = fail + 1;
10134
      end
10135
      // clear interrupts
10136
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10137
      // check WB INT signal
10138
      if (wb_int !== 1'b0)
10139
      begin
10140
        test_fail("WB INT signal should not be set");
10141
        fail = fail + 1;
10142
      end
10143
      // set initial value
10144
      i_data = i_data - 1;
10145
      // the number of frame transmitted
10146
      num_of_frames = num_of_frames + 1;
10147
      num_of_bd = 0;
10148
      // set length (LOOP variable)
10149 223 tadejm
      if (num_of_frames == i_length + 4) // 64 => this was last Byte (1st .. 64th) when i_length = min_tmp - 4
10150 209 tadejm
        i_length = (max_tmp - 4);
10151
      @(posedge wb_clk);
10152
    end
10153
    // disable TX
10154
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
10155
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10156
    @(posedge wb_clk);
10157
    if(fail == 0)
10158
      test_ok;
10159
    else
10160
      fail = 0;
10161
  end
10162
 
10163
 
10164
  ////////////////////////////////////////////////////////////////////
10165
  ////                                                            ////
10166
  ////  Test transmit packets after TX under-run on each packet's ////
10167
  ////  byte at 2 TX buffer decriptors ( 100Mbps ).               ////
10168
  ////                                                            ////
10169
  ////////////////////////////////////////////////////////////////////
10170
  if (test_num == 21) // without padding
10171
  begin
10172
    // TEST 21: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 100Mbps )
10173
    test_name = "TEST 21: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 100Mbps )";
10174
    `TIME;
10175
    $display("  TEST 21: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 100Mbps )");
10176
 
10177
    // reset MAC registers
10178
    hard_reset;
10179
    // reset MAC and MII LOGIC with soft reset
10180
    reset_mac;
10181
    reset_mii;
10182
    // set wb slave response
10183
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
10184
 
10185
    max_tmp = 0;
10186
    min_tmp = 0;
10187
    // set 2 TX buffer descriptors - must be set before TX enable
10188
    wbm_write(`ETH_TX_BD_NUM, 32'h2, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10189
    // enable TX, set full-duplex mode, NO padding and CRC appending
10190
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN |/* `ETH_MODER_PAD |*/ `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
10191
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10192
    // prepare a packet of MAXFL length
10193
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10194
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
10195
    min_tmp = tmp[31:16];
10196
    st_data = 8'h99;
10197
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
10198
    // read IPG value
10199
    wbm_read(`ETH_IPGT, tmp_ipgt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10200
    // check WB INT signal
10201
    if (wb_int !== 1'b0)
10202
    begin
10203
      test_fail("WB INT signal should not be set");
10204
      fail = fail + 1;
10205
    end
10206
 
10207
    // write to phy's control register for 100Mbps
10208
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
10209
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
10210
    speed = 100;
10211
 
10212
    num_of_frames = 0; // (0..3) => start under-run on first word
10213
    num_of_bd = 0;
10214
    i_data = 3; // (3) => one BYTE read in first word - FIRST byte
10215
    i_length = (min_tmp + 4);
10216
    while (i_length < (max_tmp - 4))
10217
    begin
10218
      // Reset_tx_bd enable interrupt generation
10219
      // unmask interrupts
10220
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
10221
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10222
      // not detect carrier sense in FD and no collision
10223
      eth_phy.carrier_sense_tx_fd_detect(0);
10224
      eth_phy.collision(0);
10225
      // first destination address on ethernet PHY
10226
      eth_phy.set_tx_mem_addr(0);
10227
      // prepare BDs
10228
      if (num_of_bd == 0)
10229
      begin
10230
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_data[1:0]));
10231
        set_tx_bd(1, 1, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
10232
        set_tx_bd_wrap(1);
10233
        // set wb slave response: ACK (response), wbs_waits[2:0] (waits before response), 
10234
        //                       wbs_retries[7:0] (RTYs before ACK if RTY response selected)
10235
        #1 wb_slave.cycle_response(`ACK_RESPONSE, 3'h2, 8'h0);
10236
        set_tx_bd_ready(1, 1);
10237
        set_tx_bd_ready(0, 0);
10238
      end
10239
      // frame under-run checking
10240
      frame_started = 0;
10241
      frame_ended = 0;
10242
      wait_for_frame = 0;
10243
      fork
10244
        begin
10245
          // for every 4 frames bytes 1, 2, 3 and 4 respectively are read in first word => 1 ACK
10246
          // in other words 4 bytes are read, since length is MINFL => num_of_frames[31:2] ACKs
10247
          repeat ((num_of_frames[31:2] + 1'b1)) @(posedge eth_ma_wb_ack_i);
10248
          @(negedge eth_ma_wb_ack_i); // wait for last ACK to finish
10249
          // set wb slave response: ACK (response), wbs_waits[2:0] (waits before response), 
10250
          //                       wbs_retries[7:0] (RTYs before ACK if RTY response selected)
10251
          #1 wb_slave.cycle_response(`NO_RESPONSE, 3'h7, 8'hFF);
10252
          // wait for synchronization and some additional clocks
10253
          wait_for_frame = 1;
10254
          // wait for frame
10255
          wait ((wait_for_frame == 0) || (frame_started == 1))
10256
          if ((wait_for_frame == 0) && (frame_started == 0)) // frame didn't start
10257
          begin
10258
            disable check_fr1;
10259
          end
10260
          else if ((wait_for_frame == 1) && (frame_started == 1)) // frame started
10261
          begin
10262
            disable wait_fr1;
10263
            wait (frame_ended == 1);
10264
          end
10265
          repeat (2) @(posedge wb_clk);
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
          wb_slave.cycle_response(`ACK_RESPONSE, 3'h2, 8'h0);
10269
        end
10270
        begin: wait_fr1
10271
          wait (wait_for_frame == 1)
10272
          begin
10273
            // wait for synchronization and some additional clocks
10274
            repeat (3) @(posedge wb_clk);
10275
            repeat (2 * tmp_ipgt) @(posedge mtx_clk);
10276
            repeat (2) @(posedge wb_clk);
10277
            repeat (2) @(posedge mtx_clk);
10278
            wait_for_frame = 0;
10279
          end
10280
        end
10281
        begin: check_fr1
10282
          // wait for frame to start
10283
          @(posedge MTxEn);
10284
          frame_started = 1;
10285
$display("  Under-run (on %0d. byte) frame started", (num_of_frames + 1));
10286
          // wait for frame to end due to under-run
10287
          @(negedge MTxEn);
10288
          frame_ended = 1;
10289
$display("  Under-run frame ended");
10290
        end
10291
      join
10292
      // wait for first transmit to end, if under-run didn't happen
10293
      if (frame_ended == 0)
10294
      begin
10295
        // WAIT FOR FIRST TRANSMIT
10296
        check_tx_bd(num_of_bd, data);
10297
        wait (MTxEn === 1'b1); // start first transmit
10298
        if (data[15] !== 1)
10299
        begin
10300
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
10301
          fail = fail + 1;
10302
        end
10303
        wait (MTxEn === 1'b0); // end first transmit
10304
        while (data[15] === 1)
10305
        begin
10306
          check_tx_bd(num_of_bd, data);
10307
          @(posedge wb_clk);
10308
        end
10309
        repeat (1) @(posedge wb_clk);
10310
        // CHECK FIRST FRAME
10311
        // check length of a first PACKET
10312
        tmp_len = eth_phy.tx_len;
10313
        #1;
10314
        if (tmp_len != (i_length + 4))
10315
        begin
10316
          test_fail("Wrong length of second packet out from MAC");
10317
          fail = fail + 1;
10318
        end
10319
        // checking first packet
10320
        check_tx_packet((`MEMORY_BASE + i_data[1:0]), 0, (i_length), tmp);
10321
        if (tmp > 0)
10322
        begin
10323
          test_fail("Wrong data of second transmitted packet");
10324
          fail = fail + 1;
10325
        end
10326
        // check first transmited TX packet CRC
10327
        check_tx_crc(0, (i_length), 1'b0, tmp); // length without CRC
10328
        if (tmp > 0)
10329
        begin
10330
          test_fail("Wrong CRC of second transmitted packet");
10331
          fail = fail + 1;
10332
        end
10333
        // check WB INT signal
10334
        if (wb_int !== 1'b1)
10335
        begin
10336
          `TIME; $display("*E WB INT signal should be set");
10337
          test_fail("WB INT signal should be set");
10338
          fail = fail + 1;
10339
        end
10340
        // check TX buffer descriptor of a packet
10341
        check_tx_bd(num_of_bd, data);
10342
        if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
10343
             ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
10344
        begin
10345
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
10346
          test_fail("TX buffer descriptor status is not correct");
10347
          fail = fail + 1;
10348
        end
10349
        // check interrupts
10350
        wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10351
        if ((data & `ETH_INT_TXB) !== 1'b1)
10352
        begin
10353
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
10354
          test_fail("Interrupt Transmit Buffer was not set");
10355
          fail = fail + 1;
10356
        end
10357
        if ((data & (~`ETH_INT_TXB)) !== 0)
10358
        begin
10359
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
10360
          test_fail("Other interrupts (except Transmit Buffer) were set");
10361
          fail = fail + 1;
10362
        end
10363
        // clear interrupts
10364
        wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10365
        // check WB INT signal
10366
        if (wb_int !== 1'b0)
10367
        begin
10368
          test_fail("WB INT signal should not be set");
10369
          fail = fail + 1;
10370
        end
10371
      end
10372
      num_of_bd = num_of_bd + 1;
10373
      // destination address on ethernet PHY
10374
      eth_phy.set_tx_mem_addr(0);
10375
      // WAIT FOR FIRST TRANSMIT
10376
      check_tx_bd(num_of_bd, data);
10377
      wait (MTxEn === 1'b1); // start first transmit
10378
      if (data[15] !== 1)
10379
      begin
10380
        test_fail("Wrong buffer descriptor's ready bit read out from MAC");
10381
        fail = fail + 1;
10382
      end
10383
      wait (MTxEn === 1'b0); // end first transmit
10384
      while (data[15] === 1)
10385
      begin
10386
        check_tx_bd(num_of_bd, data);
10387
        @(posedge wb_clk);
10388
      end
10389
      repeat (1) @(posedge wb_clk);
10390
      // CHECK SECOND FRAME
10391
      // check length of a second PACKET
10392
      tmp_len = eth_phy.tx_len;
10393
      #1;
10394
      if (tmp_len != (i_length + 4))
10395
      begin
10396
        test_fail("Wrong length of second packet out from MAC");
10397
        fail = fail + 1;
10398
      end
10399
      // checking second packet
10400
      check_tx_packet(`MEMORY_BASE, 0, (i_length), tmp);
10401
      if (tmp > 0)
10402
      begin
10403
        test_fail("Wrong data of second transmitted packet");
10404
        fail = fail + 1;
10405
      end
10406
      // check second transmited TX packet CRC
10407
      check_tx_crc(0, (i_length), 1'b0, tmp); // length without CRC
10408
      if (tmp > 0)
10409
      begin
10410
        test_fail("Wrong CRC of second transmitted packet");
10411
        fail = fail + 1;
10412
      end
10413
      // check WB INT signal
10414
      if (wb_int !== 1'b1)
10415
      begin
10416
        `TIME; $display("*E WB INT signal should be set");
10417
        test_fail("WB INT signal should be set");
10418
        fail = fail + 1;
10419
      end
10420
      // check TX buffer descriptor of a packet
10421
      check_tx_bd(num_of_bd, data);
10422
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
10423
           ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
10424
      begin
10425
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
10426
        test_fail("TX buffer descriptor status is not correct");
10427
        fail = fail + 1;
10428
      end
10429
      // check interrupts
10430
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10431
      if ((data & `ETH_INT_TXB) !== 1'b1)
10432
      begin
10433
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
10434
        test_fail("Interrupt Transmit Buffer was not set");
10435
        fail = fail + 1;
10436
      end
10437
      if ((data & (~`ETH_INT_TXB)) !== 0)
10438
      begin
10439
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
10440
        test_fail("Other interrupts (except Transmit Buffer) were set");
10441
        fail = fail + 1;
10442
      end
10443
      // clear interrupts
10444
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10445
      // check WB INT signal
10446
      if (wb_int !== 1'b0)
10447
      begin
10448
        test_fail("WB INT signal should not be set");
10449
        fail = fail + 1;
10450
      end
10451
      // set initial value
10452
      i_data = i_data - 1;
10453
      // the number of frame transmitted
10454
      num_of_frames = num_of_frames + 1;
10455
      num_of_bd = 0;
10456
      // set length (LOOP variable)
10457
      if (num_of_frames == i_length + 4) // 64 => this vas last Byte (1st .. 64th) when i_length = min_tmp - 4
10458
        i_length = (max_tmp - 4);
10459
      @(posedge wb_clk);
10460
    end
10461
    // disable TX
10462
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
10463
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10464
    @(posedge wb_clk);
10465
    if(fail == 0)
10466
      test_ok;
10467
    else
10468
      fail = 0;
10469
  end
10470
 
10471 194 tadej
end   //  for (test_num=start_task; test_num <= end_task; test_num=test_num+1)
10472 169 mohor
 
10473
end
10474
endtask // test_mac_full_duplex_transmit
10475
 
10476
 
10477 209 tadejm
task test_mac_full_duplex_receive;
10478
  input  [31:0]  start_task;
10479
  input  [31:0]  end_task;
10480
  integer        bit_start_1;
10481
  integer        bit_end_1;
10482
  integer        bit_start_2;
10483
  integer        bit_end_2;
10484
  integer        num_of_reg;
10485
  integer        num_of_frames;
10486
  integer        num_of_bd;
10487
  integer        i_addr;
10488
  integer        i_data;
10489
  integer        i_length;
10490
  integer        tmp_len;
10491
  integer        tmp_bd;
10492
  integer        tmp_bd_num;
10493
  integer        tmp_data;
10494
  integer        tmp_ipgt;
10495
  integer        test_num;
10496
  reg    [31:0]  tx_bd_num;
10497
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_data;
10498
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_tmp_data;
10499
  integer        i;
10500
  integer        i1;
10501
  integer        i2;
10502
  integer        i3;
10503
  integer        fail;
10504
  integer        speed;
10505
  reg            frame_started;
10506
  reg            frame_ended;
10507
  reg            wait_for_frame;
10508 243 tadejm
  reg            check_frame;
10509
  reg            stop_checking_frame;
10510
  reg            first_fr_received;
10511 209 tadejm
  reg    [31:0]  addr;
10512
  reg    [31:0]  data;
10513
  reg    [31:0]  tmp;
10514
  reg    [ 7:0]  st_data;
10515
  reg    [15:0]  max_tmp;
10516
  reg    [15:0]  min_tmp;
10517
begin
10518
// MAC FULL DUPLEX RECEIVE TEST
10519
test_heading("MAC FULL DUPLEX RECEIVE TEST");
10520
$display(" ");
10521
$display("MAC FULL DUPLEX RECEIVE TEST");
10522
fail = 0;
10523
 
10524
// reset MAC registers
10525
hard_reset;
10526
// reset MAC and MII LOGIC with soft reset
10527
reset_mac;
10528
reset_mii;
10529
// set wb slave response
10530
wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
10531
 
10532
  /*
10533
  TASKS for set and control TX buffer descriptors (also send packet - set_tx_bd_ready):
10534
  -------------------------------------------------------------------------------------
10535
  set_tx_bd
10536
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0], len[15:0], irq, pad, crc, txpnt[31:0]);
10537
  set_tx_bd_wrap
10538
    (tx_bd_num_end[6:0]);
10539
  set_tx_bd_ready
10540
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
10541
  check_tx_bd
10542
    (tx_bd_num_start[6:0], tx_bd_status[31:0]);
10543
  clear_tx_bd
10544
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
10545
 
10546
  TASKS for set and control RX buffer descriptors:
10547
  ------------------------------------------------
10548
  set_rx_bd
10549
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0], irq, rxpnt[31:0]);
10550
  set_rx_bd_wrap
10551
    (rx_bd_num_end[6:0]);
10552
  set_rx_bd_empty
10553
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
10554
  check_rx_bd
10555
    (rx_bd_num_end[6:0], rx_bd_status);
10556
  clear_rx_bd
10557
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
10558
 
10559
  TASKS for set and check TX packets:
10560
  -----------------------------------
10561
  set_tx_packet
10562
    (txpnt[31:0], len[15:0], eth_start_data[7:0]);
10563
  check_tx_packet
10564
    (txpnt_wb[31:0], txpnt_phy[31:0], len[15:0], failure[31:0]);
10565
 
10566
  TASKS for set and check RX packets:
10567
  -----------------------------------
10568
  set_rx_packet
10569
    (rxpnt[31:0], len[15:0], plus_nibble, d_addr[47:0], s_addr[47:0], type_len[15:0], start_data[7:0]);
10570
  check_rx_packet
10571
    (rxpnt_phy[31:0], rxpnt_wb[31:0], len[15:0], plus_nibble, successful_nibble, failure[31:0]);
10572
 
10573
  TASKS for append and check CRC to/of TX packet:
10574
  -----------------------------------------------
10575
  append_tx_crc
10576
    (txpnt_wb[31:0], len[15:0], negated_crc);
10577
  check_tx_crc
10578
    (txpnt_phy[31:0], len[15:0], negated_crc, failure[31:0]);
10579
 
10580
  TASK for append CRC to RX packet (CRC is checked together with check_rx_packet):
10581
  --------------------------------------------------------------------------------
10582
  append_rx_crc
10583
    (rxpnt_phy[31:0], len[15:0], plus_nibble, negated_crc);
10584
  */
10585
 
10586
//////////////////////////////////////////////////////////////////////
10587
////                                                              ////
10588
////  test_mac_full_duplex_receive:                               ////
10589
////                                                              ////
10590
////  0: Test no receive when all buffers are TX ( 10Mbps ).      ////
10591
////  1: Test no receive when all buffers are TX ( 100Mbps ).     ////
10592 243 tadejm
////  2: Test receive packet synchronization with receive         ////
10593
////     disable/enable ( 10Mbps ).                               ////
10594
////  3: Test receive packet synchronization with receive         ////
10595
////     disable/enable ( 100Mbps ).                              ////
10596
////  4: Test receive packets form MINFL to MAXFL sizes at        ////
10597 209 tadejm
////     one RX buffer decriptor ( 10Mbps ).                      ////
10598 243 tadejm
////  5: Test receive packets form MINFL to MAXFL sizes at        ////
10599 209 tadejm
////     one RX buffer decriptor ( 100Mbps ).                     ////
10600
////                                                              ////
10601
//////////////////////////////////////////////////////////////////////
10602
for (test_num = start_task; test_num <= end_task; test_num = test_num + 1)
10603
begin
10604
 
10605
  ////////////////////////////////////////////////////////////////////
10606
  ////                                                            ////
10607
  ////  Test no receive when all buffers are TX ( 10Mbps ).       ////
10608
  ////                                                            ////
10609
  ////////////////////////////////////////////////////////////////////
10610
  if (test_num == 0) // Test no receive when all buffers are TX ( 10Mbps ).
10611
  begin
10612
    // TEST 0: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 10Mbps )
10613
    test_name   = "TEST 0: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 10Mbps )";
10614
    `TIME; $display("  TEST 0: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 10Mbps )");
10615
 
10616
    // unmask interrupts
10617
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
10618
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10619
    // set all buffer descriptors to TX - must be set before RX enable
10620
    wbm_write(`ETH_TX_BD_NUM, 32'h80, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10621 223 tadejm
    // enable RX, set full-duplex mode, receive small, NO correct IFG
10622 209 tadejm
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
10623
              `ETH_MODER_PRO | `ETH_MODER_BRO,
10624
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10625
 
10626
    // write to phy's control register for 10Mbps
10627
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
10628
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
10629
    speed = 10;
10630
 
10631
    i = 0;
10632
    while (i < 128)
10633
    begin
10634
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
10635
      begin
10636
        set_rx_packet((i1 * 50), 10, 1'b0, 48'h1234_5678_8765, 48'h0011_2233_4455, 16'h0101, 8'h0);
10637
        append_rx_crc((i1 * 50), 10, 1'b0, 1'b0);
10638
        set_rx_bd(i1, i1, 1'b1, (`MEMORY_BASE + (i1 * 50)));
10639
      end
10640
      set_rx_bd_wrap(i);
10641
      set_rx_bd_empty(0, i);
10642
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
10643
      begin
10644
        #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, (i1 * 50), 14, 1'b0);
10645
        repeat(10) @(posedge mrx_clk);
10646
      end
10647
      @(posedge mrx_clk);
10648
      for (i2 = 0; i2 < 20; i2 = i2 + 1)
10649
      begin
10650
        check_rx_bd(0, tmp);
10651
        #1;
10652
        if (tmp[15] === 1'b0)
10653
        begin
10654
          test_fail("Receive should not start at all");
10655
          fail = fail + 1;
10656
          `TIME; $display("*E Receive of %d packets should not start at all - empty is 0", i);
10657
        end
10658
        if (tmp[7:0] !== 0)
10659
        begin
10660
          test_fail("Receive should not be finished since it should not start at all");
10661
          fail = fail + 1;
10662
          `TIME; $display("*E Receive of should not be finished since it should not start at all");
10663
        end
10664
        @(posedge wb_clk);
10665
      end
10666
      wbm_read(`ETH_INT, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10667
      if (tmp[6:0] !== 0)
10668
      begin
10669
        test_fail("Receive should not get INT since it should not start at all");
10670
        fail = fail + 1;
10671
        `TIME; $display("*E Receive of should not get INT since it should not start at all");
10672
      end
10673
      clear_rx_bd(0, i);
10674
      if ((i < 5) || (i > 124))
10675
        i = i + 1;
10676
      else
10677
        i = i + 120;
10678
    end
10679
    // disable RX
10680
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
10681
              `ETH_MODER_PRO | `ETH_MODER_BRO,
10682
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10683
    if(fail == 0)
10684
      test_ok;
10685
    else
10686
      fail = 0;
10687
  end
10688
 
10689
 
10690
  ////////////////////////////////////////////////////////////////////
10691
  ////                                                            ////
10692
  ////  Test no receive when all buffers are TX ( 100Mbps ).      ////
10693
  ////                                                            ////
10694
  ////////////////////////////////////////////////////////////////////
10695
  if (test_num == 1) // Test no receive when all buffers are TX ( 100Mbps ).
10696
  begin
10697
    // TEST 1: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 100Mbps )
10698
    test_name   = "TEST 1: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 100Mbps )";
10699
    `TIME; $display("  TEST 1: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 100Mbps )");
10700
 
10701
    // unmask interrupts
10702
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
10703
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10704
    // set all buffer descriptors to TX - must be set before RX enable
10705
    wbm_write(`ETH_TX_BD_NUM, 32'h80, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10706 223 tadejm
    // enable RX, set full-duplex mode, receive small, NO correct IFG
10707 209 tadejm
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
10708
              `ETH_MODER_PRO | `ETH_MODER_BRO,
10709
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10710
 
10711
    // write to phy's control register for 100Mbps
10712
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
10713
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
10714
    speed = 100;
10715
 
10716
    i = 0;
10717
    while (i < 128)
10718
    begin
10719
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
10720
      begin
10721
        set_rx_packet((i1 * 50), 10, 1'b0, 48'h1234_5678_8765, 48'h0011_2233_4455, 16'h0101, 8'h0);
10722
        append_rx_crc((i1 * 50), 10, 1'b0, 1'b0);
10723
        set_rx_bd(i1, i1, 1'b1, (`MEMORY_BASE + (i1 * 50)));
10724
      end
10725
      set_rx_bd_wrap(i);
10726
      set_rx_bd_empty(0, i);
10727
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
10728
      begin
10729
        #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, (i1 * 50), 14, 1'b0);
10730
        repeat(10) @(posedge mrx_clk);
10731
      end
10732
      @(posedge mrx_clk);
10733
      for (i2 = 0; i2 < 20; i2 = i2 + 1)
10734
      begin
10735
        check_rx_bd(0, tmp);
10736
        #1;
10737
        if (tmp[15] === 1'b0)
10738
        begin
10739
          test_fail("Receive should not start at all");
10740
          fail = fail + 1;
10741
          `TIME; $display("*E Receive of %d packets should not start at all - empty is 0", i);
10742
        end
10743
        if (tmp[7:0] !== 0)
10744
        begin
10745
          test_fail("Receive should not be finished since it should not start at all");
10746
          fail = fail + 1;
10747
          `TIME; $display("*E Receive of should not be finished since it should not start at all");
10748
        end
10749
        @(posedge wb_clk);
10750
      end
10751
      wbm_read(`ETH_INT, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10752
      if (tmp[6:0] !== 0)
10753
      begin
10754
        test_fail("Receive should not get INT since it should not start at all");
10755
        fail = fail + 1;
10756
        `TIME; $display("*E Receive of should not get INT since it should not start at all");
10757
      end
10758
      clear_rx_bd(0, i);
10759
      if ((i < 5) || (i > 124))
10760
        i = i + 1;
10761
      else
10762
        i = i + 120;
10763
    end
10764
    // disable RX
10765
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
10766
              `ETH_MODER_PRO | `ETH_MODER_BRO,
10767
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10768
    if(fail == 0)
10769
      test_ok;
10770
    else
10771
      fail = 0;
10772
  end
10773
 
10774
 
10775
  ////////////////////////////////////////////////////////////////////
10776
  ////                                                            ////
10777 243 tadejm
  ////  Test receive packet synchronization with receive          ////
10778
  ////  disable/enable ( 10Mbps ).                                ////
10779
  ////                                                            ////
10780
  ////////////////////////////////////////////////////////////////////
10781
  if (test_num == 2) // Test no receive when all buffers are TX ( 10Mbps ).
10782
  begin
10783
    // TEST 2: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )
10784
    test_name   = "TEST 2: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )";
10785
    `TIME; $display("  TEST 2: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )");
10786
 
10787
    // unmask interrupts
10788
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
10789
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10790
    // set 10 RX buffer descriptor (8'h80 - 8'hA) - must be set before RX enable
10791
    wbm_write(`ETH_TX_BD_NUM, 32'h76, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10792
    // disable RX, set full-duplex mode, NO receive small, NO correct IFG
10793
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_IFG |
10794
              `ETH_MODER_PRO | `ETH_MODER_BRO,
10795
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10796
    // prepare two packets of MAXFL length
10797
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10798
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
10799
    min_tmp = tmp[31:16];
10800
    st_data = 8'h0F;
10801
    set_rx_packet(0, (min_tmp + 1), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
10802
    append_rx_crc(0, (min_tmp + 1), 1'b0, 1'b0);
10803
    st_data = 8'h1A;
10804
    set_rx_packet(max_tmp, (min_tmp + 1), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
10805
    append_rx_crc(max_tmp, (min_tmp + 1), 1'b0, 1'b0);
10806
    // check WB INT signal
10807
    if (wb_int !== 1'b0)
10808
    begin
10809
      test_fail("WB INT signal should not be set");
10810
      fail = fail + 1;
10811
    end
10812
 
10813
    // write to phy's control register for 10Mbps
10814
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
10815
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
10816
    speed = 10;
10817
 
10818
    frame_started = 0;
10819
    frame_ended = 0;
10820
    wait_for_frame = 0;
10821
    check_frame = 0;
10822
    stop_checking_frame = 0;
10823
    first_fr_received = 1; // at the beginning the first frame of each two will be received!
10824
 
10825
    num_of_frames = 0; // 
10826
    num_of_bd = 0;
10827
    i_length = (min_tmp + 1); // 5 bytes longer than MINFL
10828
    while (i_length < (max_tmp - 4))
10829
    begin
10830
      // choose generating carrier sense and collision 
10831
      case (num_of_frames[1:0])
10832
      2'h0: // Interrupt is generated
10833
      begin
10834
        // enable interrupt generation
10835
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
10836
        // not detect carrier sense in FD and no collision
10837
        eth_phy.no_carrier_sense_rx_fd_detect(0);
10838
        eth_phy.collision(0);
10839
      end
10840
      2'h1: // Interrupt is generated
10841
      begin
10842
        // enable interrupt generation
10843
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
10844
        // detect carrier sense in FD and no collision
10845
        eth_phy.no_carrier_sense_rx_fd_detect(1);
10846
        eth_phy.collision(0);
10847
      end
10848
      2'h2: // Interrupt is generated
10849
      begin
10850
        // disable interrupt generation
10851
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
10852
        // not detect carrier sense in FD and set collision
10853
        eth_phy.no_carrier_sense_rx_fd_detect(0);
10854
        eth_phy.collision(1);
10855
      end
10856
      default: // 2'h3: // Interrupt is generated
10857
      begin
10858
        // disable interrupt generation
10859
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
10860
        // detect carrier sense in FD and set collision
10861
        eth_phy.no_carrier_sense_rx_fd_detect(1);
10862
        eth_phy.collision(1);
10863
      end
10864
      endcase
10865
      // set wrap bit
10866
      set_rx_bd_wrap(118);
10867
      set_rx_bd_empty(118, 118);
10868
      check_frame = 0;
10869
      stop_checking_frame = 0;
10870
      tmp_data = 0;
10871
      fork
10872
        begin // enable RX MAC on first of each two packets - every second should be recived normaly
10873
          if (num_of_frames[0] == 1'b0)
10874
          begin
10875
            repeat(1) @(posedge wb_clk);
10876
            if (num_of_frames[1] == 1'b0)
10877
            begin
10878
              repeat (num_of_frames[31:2]) @(posedge mrx_clk); // for every (second) frame enable receiver one clock later
10879
            end
10880
            else
10881
            begin
10882
              @(posedge mrx_clk);
10883
              repeat (num_of_frames[31:2]) @(negedge mrx_clk); // for every (second) frame enable receiver one clock later
10884
            end
10885
            // enable RX, set full-duplex mode, NO receive small, NO correct IFG
10886
            wbm_init_waits = 4'h0;
10887
            wbm_subseq_waits = 4'h0;
10888
            #1 wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
10889
                      `ETH_MODER_PRO | `ETH_MODER_BRO,
10890
                      4'hF, 1, wbm_init_waits, wbm_subseq_waits); // write ASAP
10891
          end
10892
        end
10893
        begin // send a packet from PHY RX
10894
          repeat(1) @(posedge wb_clk); // wait for WB write when it is without delays
10895
          if (num_of_frames[1] == 1'b0)
10896
          begin
10897
            set_rx_addr_type(0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E);
10898
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
10899
          end
10900
          else
10901
          begin
10902
            set_rx_addr_type((max_tmp), 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E);
10903
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
10904
          end
10905
        end
10906
        begin: send_packet0
10907
          wait (MRxDV === 1'b1); // start transmit
10908
          wait (MRxDV === 1'b0); // end transmit
10909
          check_frame = 1;
10910
          repeat(10) @(posedge mrx_clk);
10911
          repeat(15) @(posedge wb_clk);
10912
          stop_checking_frame = 1;
10913
        end
10914
        begin // count WB clocks between ACK (negedge) and RX_DV (posedge) or vice-versa
10915
          @(posedge eth_sl_wb_ack_o or posedge MRxDV);
10916
          if ((eth_sl_wb_ack_o === 1'b1) && (MRxDV === 1'b1))
10917
          begin
10918
            tmp_data = 32'h8000_0001; // bit[31]==1 => 'posedge MRxDV' was before 'negedge eth_sl_wb_ack_o'
10919
          end
10920
          else if (MRxDV === 1'b1)
10921
          begin
10922
            while (eth_sl_wb_ack_o === 1'b0)
10923
            begin
10924
              @(posedge wb_clk);
10925
              tmp_data = tmp_data + 1;
10926
            end
10927
            tmp_data = tmp_data | 32'h8000_0000; // bit[31]==1 => 'posedge MRxDV' was before 'negedge eth_sl_wb_ack_o'
10928
          end
10929
          else if (eth_sl_wb_ack_o === 1'b1)
10930
          begin
10931
            @(posedge wb_clk); // wait for one clock => tmp_data 'becomes' 0
10932
            while (MRxDV === 1'b0)
10933
            begin
10934
              @(posedge wb_clk);
10935
              tmp_data = tmp_data + 1; // bit[31]==0 => 'negedge eth_sl_wb_ack_o' was equal or before 'posedge MRxDV'
10936
            end
10937
          end
10938
        end
10939
        begin // check packet
10940
          wait (check_frame == 1);
10941
          check_rx_bd(118, tmp_bd);
10942
          while ((tmp_bd[15] === 1) && (stop_checking_frame == 0))
10943
          begin
10944
            #1 check_rx_bd(118, tmp_bd);
10945
            @(posedge wb_clk);
10946
          end
10947
          if (num_of_frames[0] == 1'b0)
10948
          begin
10949
            if (tmp_bd[15] === 1)
10950
            begin
10951
              if (first_fr_received == 1)
10952
              begin
10953
                first_fr_received = 0;
10954
                $display("    %0d packets (without this one) are checked - packets are received by two in a set",
10955
                         num_of_frames); // +1 due to start with 0 AND -1 because this packet is excluded
10956
                $display("    From this moment:");
10957
                $display("    first one of two packets (including this one) is not accepted due to late RX enable");
10958
                if (tmp_data[31])
10959
                  $display("    ->RX enable set %0d WB clks after RX_DV", tmp_data[30:0]);
10960
                else
10961
                  $display("    ->RX enable set %0d WB clks before RX_DV", tmp_data[30:0]);
10962
              end
10963
            end
10964
          end
10965
          if (stop_checking_frame == 0)
10966
            disable send_packet0;
10967
        end
10968
      join
10969
      // ONLY IF packet was received!
10970
      if (tmp_bd[15] === 0)
10971
      begin
10972
        // check length of a PACKET
10973
        if (tmp_bd[31:16] != (i_length + 4))
10974
        begin
10975
          `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
10976
                          tmp_bd[31:16], (i_length + 4));
10977
          test_fail("Wrong length of the packet out from PHY");
10978
          fail = fail + 1;
10979
        end
10980
        // check received RX packet data and CRC
10981
        if (first_fr_received == 0) // if PREVIOUS RX buffer descriptor was not ready, pointer address is -1
10982
        begin
10983
          if (num_of_frames[1] == 1'b0)
10984
          begin
10985
            check_rx_packet(0, (`MEMORY_BASE + num_of_frames[1:0] - 1'b1), (i_length + 4), 1'b0, 1'b0, tmp);
10986
          end
10987
          else
10988
          begin
10989
            check_rx_packet(max_tmp, (`MEMORY_BASE + num_of_frames[1:0] - 1'b1), (i_length + 4), 1'b0, 1'b0, tmp);
10990
          end
10991
          if (tmp > 0)
10992
          begin
10993
            `TIME; $display("*E Wrong data of the received packet");
10994
            test_fail("Wrong data of the received packet");
10995
            fail = fail + 1;
10996
          end
10997
        end
10998
        else // if PREVIOUS RX buffer descriptor was ready
10999
        begin
11000
          if (num_of_frames[1] == 1'b0)
11001
          begin
11002
            check_rx_packet(0, (`MEMORY_BASE + num_of_frames[1:0]), (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]), (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
      end
11016
      // check WB INT signal
11017
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11018
      begin
11019
        if (wb_int !== 1'b0)
11020
        begin
11021
          `TIME; $display("*E WB INT signal should not be set");
11022
          test_fail("WB INT signal should not be set");
11023
          fail = fail + 1;
11024
        end
11025
      end
11026
      else
11027
      begin
11028
        if (wb_int !== 1'b1)
11029
        begin
11030
          `TIME; $display("*E WB INT signal should be set");
11031
          test_fail("WB INT signal should be set");
11032
          fail = fail + 1;
11033
        end
11034
      end
11035
      // check RX buffer descriptor of a packet - only 15 LSBits
11036
      check_rx_bd(118, data);
11037
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11038
      begin
11039
        if (data[15:0] !== 16'hE000)
11040
        begin
11041
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11042
          test_fail("RX buffer descriptor status is not correct");
11043
          fail = fail + 1;
11044
        end
11045
      end
11046
      else // interrupt enabled
11047
      begin
11048
        if (data[15:0] !== 16'h6000)
11049
        begin
11050
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11051
          test_fail("RX buffer descriptor status is not correct");
11052
          fail = fail + 1;
11053
        end
11054
      end
11055
      // check interrupts
11056
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11057
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11058
      begin
11059
        if (data !== 0)
11060
        begin
11061
          `TIME; $display("*E Any of interrupts was set, interrupt reg: %0h, len: %0h", data, num_of_frames[1:0]);
11062
          test_fail("Any of interrupts was set");
11063
          fail = fail + 1;
11064
        end
11065
      end
11066
      else
11067
      begin
11068
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
11069
        begin
11070
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
11071
          test_fail("Interrupt Receive Buffer was not set");
11072
          fail = fail + 1;
11073
        end
11074
        if ((data & (~`ETH_INT_RXB)) !== 0)
11075
        begin
11076
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
11077
          test_fail("Other interrupts (except Receive Buffer) were set");
11078
          fail = fail + 1;
11079
        end
11080
      end
11081
      // clear interrupts
11082
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11083
      // check WB INT signal
11084
      if (wb_int !== 1'b0)
11085
      begin
11086
        test_fail("WB INT signal should not be set");
11087
        fail = fail + 1;
11088
      end
11089
      // disable RX after two packets
11090
      if (num_of_frames[0] == 1'b1)
11091
      begin
11092
        // disable RX, set full-duplex mode, NO receive small, NO correct IFG
11093
        wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_IFG |
11094
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
11095
                  4'hF, 1, 4'h0, 4'h0); // write ASAP
11096
      end
11097
      // the number of frame transmitted
11098
      num_of_frames = num_of_frames + 1;
11099
      num_of_bd = 0;
11100
      // set length (LOOP variable)
11101
      if (num_of_frames[31:2] == (i_length * 2 + 16)) // 64 => this vas last Byte (1st .. 64th) when i_length = min_tmp - 4
11102
        i_length = (max_tmp - 4);
11103
      @(posedge wb_clk);
11104
    end
11105
    // disable RX
11106
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
11107
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11108
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11109
    if(fail == 0)
11110
      test_ok;
11111
    else
11112
      fail = 0;
11113
  end
11114
 
11115
 
11116
  ////////////////////////////////////////////////////////////////////
11117
  ////                                                            ////
11118
  ////  Test receive packet synchronization with receive          ////
11119
  ////  disable/enable ( 100Mbps ).                               ////
11120
  ////                                                            ////
11121
  ////////////////////////////////////////////////////////////////////
11122
  if (test_num == 3) // Test no receive when all buffers are TX ( 100Mbps ).
11123
  begin
11124
    // TEST 3: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 100Mbps )
11125
    test_name   = "TEST 3: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 100Mbps )";
11126
    `TIME; $display("  TEST 3: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 100Mbps )");
11127
 
11128
    // unmask interrupts
11129
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11130
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11131
    // set 10 RX buffer descriptor (8'h80 - 8'hA) - must be set before RX enable
11132
    wbm_write(`ETH_TX_BD_NUM, 32'h76, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11133
    // disable RX, set full-duplex mode, NO receive small, NO correct IFG
11134
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_IFG |
11135
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11136
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11137
    // prepare two packets of MAXFL length
11138
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11139
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
11140
    min_tmp = tmp[31:16];
11141
    st_data = 8'h0F;
11142
    set_rx_packet(0, (min_tmp + 1), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
11143
    append_rx_crc(0, (min_tmp + 1), 1'b0, 1'b0);
11144
    st_data = 8'h1A;
11145
    set_rx_packet(max_tmp, (min_tmp + 1), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
11146
    append_rx_crc(max_tmp, (min_tmp + 1), 1'b0, 1'b0);
11147
    // check WB INT signal
11148
    if (wb_int !== 1'b0)
11149
    begin
11150
      test_fail("WB INT signal should not be set");
11151
      fail = fail + 1;
11152
    end
11153
 
11154
    // write to phy's control register for 100Mbps
11155
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
11156
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
11157
    speed = 100;
11158
 
11159
    frame_started = 0;
11160
    frame_ended = 0;
11161
    wait_for_frame = 0;
11162
    check_frame = 0;
11163
    stop_checking_frame = 0;
11164
    first_fr_received = 1; // at the beginning the first frame of each two will be received!
11165
 
11166
    num_of_frames = 0; // 
11167
    num_of_bd = 0;
11168
    i_length = (min_tmp + 1); // 5 bytes longer than MINFL
11169
    while (i_length < (max_tmp - 4))
11170
    begin
11171
      // choose generating carrier sense and collision 
11172
      case (num_of_frames[1:0])
11173
      2'h0: // Interrupt is generated
11174
      begin
11175
        // enable interrupt generation
11176
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
11177
        // not detect carrier sense in FD and no collision
11178
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11179
        eth_phy.collision(0);
11180
      end
11181
      2'h1: // Interrupt is generated
11182
      begin
11183
        // enable interrupt generation
11184
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
11185
        // detect carrier sense in FD and no collision
11186
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11187
        eth_phy.collision(0);
11188
      end
11189
      2'h2: // Interrupt is generated
11190
      begin
11191
        // disable interrupt generation
11192
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
11193
        // not detect carrier sense in FD and set collision
11194
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11195
        eth_phy.collision(1);
11196
      end
11197
      default: // 2'h3: // Interrupt is generated
11198
      begin
11199
        // disable interrupt generation
11200
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
11201
        // detect carrier sense in FD and set collision
11202
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11203
        eth_phy.collision(1);
11204
      end
11205
      endcase
11206
//if (first_fr_received == 0)
11207
//begin
11208
//  check_rx_bd(118, data);
11209
//  wbm_read((`TX_BD_BASE + (118 * 8) + 4), tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11210
//  $display("RX BD set : %h, %h", data, tmp);
11211
//end
11212
      // set wrap bit
11213
      set_rx_bd_wrap(118);
11214
      set_rx_bd_empty(118, 118);
11215
      check_frame = 0;
11216
      stop_checking_frame = 0;
11217
      tmp_data = 0;
11218
$display("mama 1");
11219
      fork
11220
        begin // enable RX MAC on first of each two packets - every second should be recived normaly
11221
          if (num_of_frames[0] == 1'b0)
11222
          begin
11223
            repeat(1) @(posedge wb_clk);
11224
            if (num_of_frames[1] == 1'b0)
11225
            begin
11226
              repeat (num_of_frames[31:2]) @(posedge mrx_clk); // for every (second) frame enable receiver one clock later
11227
            end
11228
            else
11229
            begin
11230
              @(posedge mrx_clk);
11231
              repeat (num_of_frames[31:2]) @(negedge mrx_clk); // for every (second) frame enable receiver one clock later
11232
            end
11233
            // enable RX, set full-duplex mode, NO receive small, NO correct IFG
11234
            wbm_init_waits = 4'h0;
11235
            wbm_subseq_waits = 4'h0;
11236
            #1 wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
11237
                      `ETH_MODER_PRO | `ETH_MODER_BRO,
11238
                      4'hF, 1, wbm_init_waits, wbm_subseq_waits); // write ASAP
11239
$display("mama 2, num_of_frames=%0h", num_of_frames);
11240
          end
11241
        end
11242
        begin // send a packet from PHY RX
11243
          repeat(1) @(posedge wb_clk); // wait for WB write when it is without delays
11244
          if (num_of_frames[1] == 1'b0)
11245
          begin
11246
            set_rx_addr_type(0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E);
11247
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
11248
          end
11249
          else
11250
          begin
11251
            set_rx_addr_type((max_tmp), 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E);
11252
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
11253
          end
11254
        end
11255
        begin: send_packet1
11256
          wait (MRxDV === 1'b1); // start transmit
11257
          wait (MRxDV === 1'b0); // end transmit
11258
          check_frame = 1;
11259
$display("mama 3");
11260
          repeat(10) @(posedge mrx_clk);
11261
          repeat(15) @(posedge wb_clk);
11262
          stop_checking_frame = 1;
11263
        end
11264
        begin // count WB clocks between ACK (negedge) and RX_DV (posedge) or vice-versa
11265
          @(posedge eth_sl_wb_ack_o or posedge MRxDV);
11266
$display("mama 4");
11267
          if ((eth_sl_wb_ack_o === 1'b1) && (MRxDV === 1'b1))
11268
          begin
11269
            tmp_data = 32'h8000_0001; // bit[31]==1 => 'posedge MRxDV' was before 'negedge eth_sl_wb_ack_o'
11270
$display("mama 4_1");
11271
          end
11272
          else if (MRxDV === 1'b1)
11273
          begin
11274
            while (eth_sl_wb_ack_o === 1'b0)
11275
            begin
11276
              @(posedge wb_clk);
11277
              tmp_data = tmp_data + 1;
11278
            end
11279
            tmp_data = tmp_data | 32'h8000_0000; // bit[31]==1 => 'posedge MRxDV' was before 'negedge eth_sl_wb_ack_o'
11280
$display("mama 4_2");
11281
          end
11282
          else if (eth_sl_wb_ack_o === 1'b1)
11283
          begin
11284
            @(posedge wb_clk); // wait for one clock => tmp_data 'becomes' 0
11285
            while (MRxDV === 1'b0)
11286
            begin
11287
              @(posedge wb_clk);
11288
              tmp_data = tmp_data + 1; // bit[31]==0 => 'negedge eth_sl_wb_ack_o' was equal or before 'posedge MRxDV'
11289
            end
11290
$display("mama 4_3");
11291
          end
11292
        end
11293
        begin // check packet
11294
          wait (check_frame == 1);
11295
          check_rx_bd(118, tmp_bd);
11296
          while ((tmp_bd[15] === 1) && (stop_checking_frame == 0))
11297
          begin
11298
            #1 check_rx_bd(118, tmp_bd);
11299
            @(posedge wb_clk);
11300
          end
11301
$display("mama 5, tmp_bd=%0h", tmp_bd);
11302
          if (num_of_frames[0] == 1'b0)
11303
          begin
11304
            if (tmp_bd[15] === 1)
11305
            begin
11306
              if (first_fr_received == 1)
11307
              begin
11308
                first_fr_received = 0;
11309
                $display("    %0d packets (without this one) are checked - packets are received by two in a set",
11310
                         num_of_frames); // +1 due to start with 0 AND -1 because this packet is excluded
11311
                $display("    From this moment:");
11312
                $display("    first one of two packets (including this one) is not accepted due to late RX enable");
11313
                if (tmp_data[31])
11314
                  $display("    ->RX enable set %0d WB clks after RX_DV", tmp_data[30:0]);
11315
                else
11316
                  $display("    ->RX enable set %0d WB clks before RX_DV", tmp_data[30:0]);
11317
              end
11318
              // check FB, etc.
11319
 
11320
            end
11321
            else // (tmp_bd[15] === 0)
11322
            begin // check FB, packet, etc.
11323
 
11324
            end
11325
$display("mama 5_1");
11326
          end
11327
          else // (num_of_frames[0] == 1'b1)
11328
          begin
11329
            if (tmp_bd[15] === 1) // ERROR, because second packet of each two frames should be received
11330
            begin // check NOTHING
11331
 
11332
            end
11333
            else // (tmp_bd[15] === 0)
11334
            begin // check FB, packet, etc.
11335
 
11336
            end
11337
$display("mama 5_2");
11338
          end
11339
          if (stop_checking_frame == 0)
11340
            disable send_packet1;
11341
        end
11342
      join
11343
      // ONLY IF packet was received!
11344
$display("mama 6");
11345
      if (tmp_bd[15] === 0)
11346
      begin
11347
        // check length of a PACKET
11348
        if (tmp_bd[31:16] != (i_length + 4))
11349
        begin
11350
          `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
11351
                          tmp_bd[31:16], (i_length + 4));
11352
          test_fail("Wrong length of the packet out from PHY");
11353
          fail = fail + 1;
11354
        end
11355
        // check received RX packet data and CRC
11356
        if (first_fr_received == 0) // if PREVIOUS RX buffer descriptor was not ready, pointer address is -1
11357
        begin
11358
          if (num_of_frames[1] == 1'b0)
11359
          begin
11360
            check_rx_packet(0, (`MEMORY_BASE + num_of_frames[1:0] - 1'b1), (i_length + 4), 1'b0, 1'b0, tmp);
11361
          end
11362
          else
11363
          begin
11364
            check_rx_packet(max_tmp, (`MEMORY_BASE + num_of_frames[1:0] - 1'b1), (i_length + 4), 1'b0, 1'b0, tmp);
11365
          end
11366
          if (tmp > 0)
11367
          begin
11368
            `TIME; $display("*E Wrong data of the received packet");
11369
            test_fail("Wrong data of the received packet");
11370
            fail = fail + 1;
11371
          end
11372
        end
11373
        else // if PREVIOUS RX buffer descriptor was ready
11374
        begin
11375
          if (num_of_frames[1] == 1'b0)
11376
          begin
11377
            check_rx_packet(0, (`MEMORY_BASE + num_of_frames[1:0]), (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]), (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
      end
11391
      // check WB INT signal
11392
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11393
      begin
11394
        if (wb_int !== 1'b0)
11395
        begin
11396
          `TIME; $display("*E WB INT signal should not be set");
11397
          test_fail("WB INT signal should not be set");
11398
          fail = fail + 1;
11399
        end
11400
      end
11401
      else
11402
      begin
11403
        if (wb_int !== 1'b1)
11404
        begin
11405
          `TIME; $display("*E WB INT signal should be set");
11406
          test_fail("WB INT signal should be set");
11407
          fail = fail + 1;
11408
        end
11409
      end
11410
      // check RX buffer descriptor of a packet - only 15 LSBits
11411
      check_rx_bd(118, data);
11412
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11413
      begin
11414
        if (data[15:0] !== 16'hE000)
11415
        begin
11416
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11417
          test_fail("RX buffer descriptor status is not correct");
11418
          fail = fail + 1;
11419
        end
11420
      end
11421
      else // interrupt enabled
11422
      begin
11423
        if (data[15:0] !== 16'h6000)
11424
        begin
11425
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11426
          test_fail("RX buffer descriptor status is not correct");
11427
          fail = fail + 1;
11428
        end
11429
      end
11430
      // check interrupts
11431
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11432
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11433
      begin
11434
        if (data !== 0)
11435
        begin
11436
          `TIME; $display("*E Any of interrupts was set, interrupt reg: %0h, len: %0h", data, num_of_frames[1:0]);
11437
          test_fail("Any of interrupts was set");
11438
          fail = fail + 1;
11439
        end
11440
      end
11441
      else
11442
      begin
11443
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
11444
        begin
11445
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
11446
          test_fail("Interrupt Receive Buffer was not set");
11447
          fail = fail + 1;
11448
        end
11449
        if ((data & (~`ETH_INT_RXB)) !== 0)
11450
        begin
11451
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
11452
          test_fail("Other interrupts (except Receive Buffer) were set");
11453
          fail = fail + 1;
11454
        end
11455
      end
11456
      // clear interrupts
11457
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11458
      // check WB INT signal
11459
      if (wb_int !== 1'b0)
11460
      begin
11461
        test_fail("WB INT signal should not be set");
11462
        fail = fail + 1;
11463
      end
11464
      // disable RX after two packets
11465
      if (num_of_frames[0] == 1'b1)
11466
      begin
11467
        // disable RX, set full-duplex mode, NO receive small, NO correct IFG
11468
        wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_IFG |
11469
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
11470
                  4'hF, 1, 4'h0, 4'h0); // write ASAP
11471
      end
11472
      // the number of frame transmitted
11473
      num_of_frames = num_of_frames + 1;
11474
      num_of_bd = 0;
11475
      // set length (LOOP variable)
11476
      if (num_of_frames[31:2] == (i_length * 2 + 16)) // 64 => this vas last Byte (1st .. 64th) when i_length = min_tmp - 4
11477
        i_length = (max_tmp - 4);
11478
      @(posedge wb_clk);
11479
    end
11480
    // disable RX
11481
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
11482
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11483
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11484
    if(fail == 0)
11485
      test_ok;
11486
    else
11487
      fail = 0;
11488
  end
11489
 
11490
 
11491
  ////////////////////////////////////////////////////////////////////
11492
  ////                                                            ////
11493 209 tadejm
  ////  Test receive packets form MINFL to MAXFL sizes at         ////
11494
  ////  one RX buffer decriptor ( 10Mbps ).                       ////
11495
  ////                                                            ////
11496
  ////////////////////////////////////////////////////////////////////
11497 243 tadejm
  if (test_num == 4) // 
11498 209 tadejm
  begin
11499 243 tadejm
    // TEST 4: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 10Mbps )
11500
    test_name   = "TEST 4: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 10Mbps )";
11501
    `TIME; $display("  TEST 4: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 10Mbps )");
11502 209 tadejm
 
11503
    // unmask interrupts
11504
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11505
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11506
    // set 1 RX buffer descriptor (8'h80 - 1) - must be set before RX enable
11507
    wbm_write(`ETH_TX_BD_NUM, 32'h7F, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11508
    // enable RX, set full-duplex mode, NO receive small, NO correct IFG
11509
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
11510
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11511
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11512
    // prepare two packets of MAXFL length
11513
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11514
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
11515
    min_tmp = tmp[31:16];
11516
    st_data = 8'h0F;
11517
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
11518
    st_data = 8'h1A;
11519
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
11520
    // check WB INT signal
11521
    if (wb_int !== 1'b0)
11522
    begin
11523
      test_fail("WB INT signal should not be set");
11524
      fail = fail + 1;
11525
    end
11526
 
11527
    // write to phy's control register for 10Mbps
11528
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
11529
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
11530
    speed = 10;
11531
 
11532
    i_length = (min_tmp - 4);
11533
    while (i_length <= (max_tmp - 4))
11534
    begin
11535
      // choose generating carrier sense and collision for first and last 64 lengths of frames
11536
      case (i_length[1:0])
11537
      2'h0: // Interrupt is generated
11538
      begin
11539
        // enable interrupt generation
11540
        set_rx_bd(127, 127, 1'b1, (`MEMORY_BASE + i_length[1:0]));
11541
        // unmask interrupts
11542
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11543
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11544
        // not detect carrier sense in FD and no collision
11545
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11546
        eth_phy.collision(0);
11547
      end
11548
      2'h1: // Interrupt is not generated
11549
      begin
11550
        // enable interrupt generation
11551
        set_rx_bd(127, 127, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
11552
        // mask interrupts
11553
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11554
        // detect carrier sense in FD and no collision
11555
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11556
        eth_phy.collision(0);
11557
      end
11558
      2'h2: // Interrupt is not generated
11559
      begin
11560
        // disable interrupt generation
11561
        set_rx_bd(127, 127, 1'b0, (`MEMORY_BASE + i_length[1:0]));
11562
        // unmask interrupts
11563
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11564
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11565
        // not detect carrier sense in FD and set collision
11566
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11567
        eth_phy.collision(1);
11568
      end
11569
      default: // 2'h3: // Interrupt is not generated
11570
      begin
11571
        // disable interrupt generation
11572
        set_rx_bd(127, 127, 1'b0, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
11573
        // mask interrupts
11574
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11575
        // detect carrier sense in FD and set collision
11576
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11577
        eth_phy.collision(1);
11578
      end
11579
      endcase
11580
      if (i_length[0] == 1'b0)
11581
        append_rx_crc (0, i_length, 1'b0, 1'b0);
11582
      else
11583
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
11584
      // set wrap bit
11585
      set_rx_bd_wrap(127);
11586
      set_rx_bd_empty(127, 127);
11587
      fork
11588
        begin
11589
          if (i_length[0] == 1'b0)
11590
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
11591
          else
11592
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
11593
          repeat(10) @(posedge mrx_clk);
11594
        end
11595
        begin
11596
          #1 check_rx_bd(127, data);
11597
          if (i_length < min_tmp) // just first four
11598
          begin
11599
            while (data[15] === 1)
11600
            begin
11601
              #1 check_rx_bd(127, data);
11602
              @(posedge wb_clk);
11603
            end
11604
            repeat (1) @(posedge wb_clk);
11605
          end
11606
          else
11607
          begin
11608
            wait (MRxDV === 1'b1); // start transmit
11609
            #1 check_rx_bd(127, data);
11610
            if (data[15] !== 1)
11611
            begin
11612
              test_fail("Wrong buffer descriptor's ready bit read out from MAC");
11613
              fail = fail + 1;
11614
            end
11615
            wait (MRxDV === 1'b0); // end transmit
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
        end
11624
      join
11625
      // check length of a PACKET
11626
      if (data[31:16] != (i_length + 4))
11627
      begin
11628
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
11629
                        data[31:16], (i_length + 4));
11630
        test_fail("Wrong length of the packet out from PHY");
11631
        fail = fail + 1;
11632
      end
11633
      // checking in the following if statement is performed only for first and last 64 lengths
11634
      // check received RX packet data and CRC
11635
      if (i_length[0] == 1'b0)
11636
      begin
11637
        check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
11638
      end
11639
      else
11640
      begin
11641
        check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
11642
      end
11643
      if (tmp > 0)
11644
      begin
11645
        `TIME; $display("*E Wrong data of the received packet");
11646
        test_fail("Wrong data of the received packet");
11647
        fail = fail + 1;
11648
      end
11649
      // check WB INT signal
11650
      if (i_length[1:0] == 2'h0)
11651
      begin
11652
        if (wb_int !== 1'b1)
11653
        begin
11654
          `TIME; $display("*E WB INT signal should be set");
11655
          test_fail("WB INT signal should be set");
11656
          fail = fail + 1;
11657
        end
11658
      end
11659
      else
11660
      begin
11661
        if (wb_int !== 1'b0)
11662
        begin
11663
          `TIME; $display("*E WB INT signal should not be set");
11664
          test_fail("WB INT signal should not be set");
11665
          fail = fail + 1;
11666
        end
11667
      end
11668
      // check RX buffer descriptor of a packet
11669
      check_rx_bd(127, data);
11670
      if (i_length[1] == 1'b0) // interrupt enabled no_carrier_sense_rx_fd_detect
11671
      begin
11672
        if ( ((data[15:0] !== 16'h6000) && (i_length[0] == 1'b0)) ||
11673
             ((data[15:0] !== 16'h6000) && (i_length[0] == 1'b1)) )
11674
        begin
11675
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11676
          test_fail("RX buffer descriptor status is not correct");
11677
          fail = fail + 1;
11678
        end
11679
      end
11680
      else // interrupt not enabled
11681
      begin
11682
        if ( ((data[15:0] !== 16'h2000) && (i_length[0] == 1'b0)) ||
11683
             ((data[15:0] !== 16'h2000) && (i_length[0] == 1'b1)) )
11684
        begin
11685
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11686
          test_fail("RX buffer descriptor status is not correct");
11687
          fail = fail + 1;
11688
        end
11689
      end
11690
      // clear RX buffer descriptor for first 4 frames
11691
      if (i_length < min_tmp)
11692
        clear_rx_bd(127, 127);
11693
      // check interrupts
11694
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11695
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
11696
      begin
11697
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
11698
        begin
11699
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
11700
          test_fail("Interrupt Receive Buffer was not set");
11701
          fail = fail + 1;
11702
        end
11703
        if ((data & (~`ETH_INT_RXB)) !== 0)
11704
        begin
11705
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
11706
          test_fail("Other interrupts (except Receive Buffer) were set");
11707
          fail = fail + 1;
11708
        end
11709
      end
11710
      else
11711
      begin
11712
        if (data !== 0)
11713
        begin
11714
          `TIME; $display("*E Any of interrupts (except Receive Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
11715
          test_fail("Any of interrupts (except Receive Buffer) was set");
11716
          fail = fail + 1;
11717
        end
11718
      end
11719
      // clear interrupts
11720
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11721
      // check WB INT signal
11722
      if (wb_int !== 1'b0)
11723
      begin
11724
        test_fail("WB INT signal should not be set");
11725
        fail = fail + 1;
11726
      end
11727
      // INTERMEDIATE DISPLAYS
11728
      if ((i_length + 4) == (min_tmp + 64))
11729
      begin
11730
        // starting length is min_tmp, ending length is (min_tmp + 64)
11731
        $display("    receive small packets is NOT selected");
11732
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
11733
                 min_tmp, (min_tmp + 64));
11734
        // set receive small, remain the rest
11735
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
11736
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
11737
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11738
      end
11739
      else if ((i_length + 4) == (max_tmp - 16))
11740
      begin
11741
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
11742
        $display("    receive small packets is selected");
11743
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
11744
                 (min_tmp + 64 + 128), tmp_data);
11745
        // reset receive small, remain the rest
11746
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
11747
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
11748
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11749
      end
11750
      else if ((i_length + 4) == max_tmp)
11751
      begin
11752
        $display("    receive small packets is NOT selected");
11753
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
11754
                 (max_tmp - (4 + 16)), max_tmp);
11755
      end
11756
      // set length (loop variable)
11757
      if ((i_length + 4) < (min_tmp + 64))
11758
        i_length = i_length + 1;
11759
      else if ( ((i_length + 4) >= (min_tmp + 64)) && ((i_length + 4) <= (max_tmp - 256)) )
11760
      begin
11761
        i_length = i_length + 128;
11762
        tmp_data = i_length + 4; // last tmp_data is ending length
11763
      end
11764
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
11765
        i_length = max_tmp - (4 + 16);
11766
      else if ((i_length + 4) >= (max_tmp - 16))
11767
        i_length = i_length + 1;
11768
      else
11769
      begin
11770
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
11771
        #10 $stop;
11772
      end
11773
    end
11774
    // disable RX
11775
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
11776
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11777
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11778
    if(fail == 0)
11779
      test_ok;
11780
    else
11781
      fail = 0;
11782
  end
11783
 
11784
 
11785
  ////////////////////////////////////////////////////////////////////
11786
  ////                                                            ////
11787
  ////  Test receive packets form MINFL to MAXFL sizes at         ////
11788
  ////  one RX buffer decriptor ( 100Mbps ).                      ////
11789
  ////                                                            ////
11790
  ////////////////////////////////////////////////////////////////////
11791 243 tadejm
  if (test_num == 5) // Test no receive when all buffers are TX ( 100Mbps ).
11792 209 tadejm
  begin
11793 243 tadejm
    // TEST 5: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 100Mbps )
11794
    test_name   = "TEST 5: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 100Mbps )";
11795
    `TIME; $display("  TEST 5: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 100Mbps )");
11796 209 tadejm
 
11797
    // unmask interrupts
11798
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11799
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11800
    // set 1 RX buffer descriptor (8'h80 - 1) - must be set before RX enable
11801
    wbm_write(`ETH_TX_BD_NUM, 32'h7F, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11802
    // enable RX, set full-duplex mode, NO receive small, NO correct IFG
11803
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
11804
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11805
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11806
    // prepare two packets of MAXFL length
11807
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11808
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
11809
    min_tmp = tmp[31:16];
11810
    st_data = 8'h0F;
11811
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
11812
    st_data = 8'h1A;
11813
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
11814
    // check WB INT signal
11815
    if (wb_int !== 1'b0)
11816
    begin
11817
      test_fail("WB INT signal should not be set");
11818
      fail = fail + 1;
11819
    end
11820
 
11821
    // write to phy's control register for 100Mbps
11822
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
11823
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
11824
    speed = 100;
11825
 
11826
    i_length = (min_tmp - 4);
11827
    while (i_length <= (max_tmp - 4))
11828
    begin
11829
      // choose generating carrier sense and collision for first and last 64 lengths of frames
11830
      case (i_length[1:0])
11831
      2'h0: // Interrupt is generated
11832
      begin
11833
        // enable interrupt generation
11834
        set_rx_bd(127, 127, 1'b1, (`MEMORY_BASE + i_length[1:0]));
11835
        // unmask interrupts
11836
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11837
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11838
        // not detect carrier sense in FD and no collision
11839
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11840
        eth_phy.collision(0);
11841
      end
11842
      2'h1: // Interrupt is not generated
11843
      begin
11844
        // enable interrupt generation
11845
        set_rx_bd(127, 127, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
11846
        // mask interrupts
11847
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11848
        // detect carrier sense in FD and no collision
11849
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11850
        eth_phy.collision(0);
11851
      end
11852
      2'h2: // Interrupt is not generated
11853
      begin
11854
        // disable interrupt generation
11855
        set_rx_bd(127, 127, 1'b0, (`MEMORY_BASE + i_length[1:0]));
11856
        // unmask interrupts
11857
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11858
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11859
        // not detect carrier sense in FD and set collision
11860
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11861
        eth_phy.collision(1);
11862
      end
11863
      default: // 2'h3: // Interrupt is not generated
11864
      begin
11865
        // disable interrupt generation
11866
        set_rx_bd(127, 127, 1'b0, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
11867
        // mask interrupts
11868
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11869
        // detect carrier sense in FD and set collision
11870
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11871
        eth_phy.collision(1);
11872
      end
11873
      endcase
11874
      if (i_length[0] == 1'b0)
11875
        append_rx_crc (0, i_length, 1'b0, 1'b0);
11876
      else
11877
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
11878
      // set wrap bit
11879
      set_rx_bd_wrap(127);
11880
      set_rx_bd_empty(127, 127);
11881
      fork
11882
        begin
11883
          if (i_length[0] == 1'b0)
11884
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
11885
          else
11886
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
11887
          repeat(10) @(posedge mrx_clk);
11888
        end
11889
        begin
11890
          #1 check_rx_bd(127, data);
11891
          if (i_length < min_tmp) // just first four
11892
          begin
11893
            while (data[15] === 1)
11894
            begin
11895
              #1 check_rx_bd(127, data);
11896
              @(posedge wb_clk);
11897
            end
11898
            repeat (1) @(posedge wb_clk);
11899
          end
11900
          else
11901
          begin
11902
            wait (MRxDV === 1'b1); // start transmit
11903
            #1 check_rx_bd(127, data);
11904
            if (data[15] !== 1)
11905
            begin
11906
              test_fail("Wrong buffer descriptor's ready bit read out from MAC");
11907
              fail = fail + 1;
11908
            end
11909
            wait (MRxDV === 1'b0); // end transmit
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
        end
11918
      join
11919
      // check length of a PACKET
11920
      if (data[31:16] != (i_length + 4))
11921
      begin
11922
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
11923
                        data[31:16], (i_length + 4));
11924
        test_fail("Wrong length of the packet out from PHY");
11925
        fail = fail + 1;
11926
      end
11927
      // checking in the following if statement is performed only for first and last 64 lengths
11928
      // check received RX packet data and CRC
11929
      if (i_length[0] == 1'b0)
11930
      begin
11931
        check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
11932
      end
11933
      else
11934
      begin
11935
        check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
11936
      end
11937
      if (tmp > 0)
11938
      begin
11939
        `TIME; $display("*E Wrong data of the received packet");
11940
        test_fail("Wrong data of the received packet");
11941
        fail = fail + 1;
11942
      end
11943
      // check WB INT signal
11944
      if (i_length[1:0] == 2'h0)
11945
      begin
11946
        if (wb_int !== 1'b1)
11947
        begin
11948
          `TIME; $display("*E WB INT signal should be set");
11949
          test_fail("WB INT signal should be set");
11950
          fail = fail + 1;
11951
        end
11952
      end
11953
      else
11954
      begin
11955
        if (wb_int !== 1'b0)
11956
        begin
11957
          `TIME; $display("*E WB INT signal should not be set");
11958
          test_fail("WB INT signal should not be set");
11959
          fail = fail + 1;
11960
        end
11961
      end
11962
      // check RX buffer descriptor of a packet
11963
      check_rx_bd(127, data);
11964
      if (i_length[1] == 1'b0) // interrupt enabled 
11965
      begin
11966
        if ( ((data[15:0] !== 16'h6000) && (i_length[0] == 1'b0)) ||
11967
             ((data[15:0] !== 16'h6000) && (i_length[0] == 1'b1)) )
11968
        begin
11969
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11970
          test_fail("RX buffer descriptor status is not correct");
11971
          fail = fail + 1;
11972
        end
11973
      end
11974
      else // interrupt not enabled
11975
      begin
11976
        if ( ((data[15:0] !== 16'h2000) && (i_length[0] == 1'b0)) ||
11977
             ((data[15:0] !== 16'h2000) && (i_length[0] == 1'b1)) )
11978
        begin
11979
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11980
          test_fail("RX buffer descriptor status is not correct");
11981
          fail = fail + 1;
11982
        end
11983
      end
11984
      // clear RX buffer descriptor for first 4 frames
11985
      if (i_length < min_tmp)
11986
        clear_rx_bd(127, 127);
11987
      // check interrupts
11988
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11989
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
11990
      begin
11991
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
11992
        begin
11993
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
11994
          test_fail("Interrupt Receive Buffer was not set");
11995
          fail = fail + 1;
11996
        end
11997
        if ((data & (~`ETH_INT_RXB)) !== 0)
11998
        begin
11999
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
12000
          test_fail("Other interrupts (except Receive Buffer) were set");
12001
          fail = fail + 1;
12002
        end
12003
      end
12004
      else
12005
      begin
12006
        if (data !== 0)
12007
        begin
12008
          `TIME; $display("*E Any of interrupts (except Receive Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
12009
          test_fail("Any of interrupts (except Receive Buffer) was set");
12010
          fail = fail + 1;
12011
        end
12012
      end
12013
      // clear interrupts
12014
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12015
      // check WB INT signal
12016
      if (wb_int !== 1'b0)
12017
      begin
12018
        test_fail("WB INT signal should not be set");
12019
        fail = fail + 1;
12020
      end
12021
      // INTERMEDIATE DISPLAYS
12022
      if ((i_length + 4) == (min_tmp + 64))
12023
      begin
12024
        // starting length is min_tmp, ending length is (min_tmp + 64)
12025
        $display("    receive small packets is NOT selected");
12026
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
12027
                 min_tmp, (min_tmp + 64));
12028
        // set receive small, remain the rest
12029
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12030
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12031
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12032
      end
12033
      else if ((i_length + 4) == (max_tmp - 16))
12034
      begin
12035
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
12036
        $display("    receive small packets is selected");
12037
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
12038
                 (min_tmp + 64 + 128), tmp_data);
12039
        // reset receive small, remain the rest
12040
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12041
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12042
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12043
      end
12044
      else if ((i_length + 4) == max_tmp)
12045
      begin
12046
        $display("    receive small packets is NOT selected");
12047
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
12048
                 (max_tmp - (4 + 16)), max_tmp);
12049
      end
12050
      // set length (loop variable)
12051
      if ((i_length + 4) < (min_tmp + 64))
12052
        i_length = i_length + 1;
12053
      else if ( ((i_length + 4) >= (min_tmp + 64)) && ((i_length + 4) <= (max_tmp - 256)) )
12054
      begin
12055
        i_length = i_length + 128;
12056
        tmp_data = i_length + 4; // last tmp_data is ending length
12057
      end
12058
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
12059
        i_length = max_tmp - (4 + 16);
12060
      else if ((i_length + 4) >= (max_tmp - 16))
12061
        i_length = i_length + 1;
12062
      else
12063
      begin
12064
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
12065
        #10 $stop;
12066
      end
12067
    end
12068
    // disable RX
12069
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12070
              `ETH_MODER_PRO | `ETH_MODER_BRO,
12071
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12072
    if(fail == 0)
12073
      test_ok;
12074
    else
12075
      fail = 0;
12076
  end
12077
 
12078
 
12079
  ////////////////////////////////////////////////////////////////////
12080
  ////                                                            ////
12081
  ////  Test receive packets form MINFL to MAXFL sizes at         ////
12082
  ////  maximum RX buffer decriptors ( 10Mbps ).                  ////
12083
  ////                                                            ////
12084
  ////////////////////////////////////////////////////////////////////
12085 243 tadejm
  if (test_num == 6) // 
12086 209 tadejm
  begin
12087 243 tadejm
    // TEST 6: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 10Mbps )
12088
    test_name = "TEST 6: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 10Mbps )";
12089
    `TIME; $display("  TEST 6: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 10Mbps )");
12090 209 tadejm
 
12091
    // reset MAC registers
12092
    hard_reset;
12093
    // reset MAC and MII LOGIC with soft reset
12094
    reset_mac;
12095
    reset_mii;
12096
    // set wb slave response
12097
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
12098
 
12099
    max_tmp = 0;
12100
    min_tmp = 0;
12101
    num_of_frames = 0;
12102
    num_of_bd = 0;
12103
    // set maximum RX buffer descriptors (128) - must be set before RX enable
12104
    wbm_write(`ETH_TX_BD_NUM, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12105
    // enable RX, set full-duplex mode, NO receive small, NO correct IFG
12106
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12107
              `ETH_MODER_PRO | `ETH_MODER_BRO,
12108
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12109
    // prepare two packets of MAXFL length
12110
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12111
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
12112
    min_tmp = tmp[31:16];
12113
    st_data = 8'hAC;
12114
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
12115
    st_data = 8'h35;
12116
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
12117
    // check WB INT signal
12118
    if (wb_int !== 1'b0)
12119
    begin
12120
      test_fail("WB INT signal should not be set");
12121
      fail = fail + 1;
12122
    end
12123
 
12124
    // write to phy's control register for 10Mbps
12125
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
12126
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
12127
    speed = 10;
12128
 
12129
    i_length = (min_tmp - 4);
12130
    while (i_length <= (max_tmp - 4))
12131
    begin
12132
      // append CRC to packet
12133
      if (i_length[0] == 1'b0)
12134
        append_rx_crc (0, i_length, 1'b0, 1'b0);
12135
      else
12136
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
12137
      // choose generating carrier sense and collision
12138
      case (i_length[1:0])
12139
      2'h0: // Interrupt is generated
12140
      begin
12141
        // Reset_tx_bd nable interrupt generation
12142
        // unmask interrupts
12143
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
12144
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12145
        // not detect carrier sense in FD and no collision
12146
        eth_phy.no_carrier_sense_rx_fd_detect(0);
12147
        eth_phy.collision(0);
12148
      end
12149
      2'h1: // Interrupt is not generated
12150
      begin
12151
        // set_tx_bd enable interrupt generation
12152
        // mask interrupts
12153
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12154
        // detect carrier sense in FD and no collision
12155
        eth_phy.no_carrier_sense_rx_fd_detect(1);
12156
        eth_phy.collision(0);
12157
      end
12158
      2'h2: // Interrupt is not generated
12159
      begin
12160
        // set_tx_bd disable the interrupt generation
12161
        // unmask interrupts
12162
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
12163
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12164
        // not detect carrier sense in FD and set collision
12165
        eth_phy.no_carrier_sense_rx_fd_detect(0);
12166
        eth_phy.collision(1);
12167
      end
12168
      default: // 2'h3: // Interrupt is not generated
12169
      begin
12170
        // set_tx_bd disable the interrupt generation
12171
        // mask interrupts
12172
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12173
        // detect carrier sense in FD and set collision
12174
        eth_phy.no_carrier_sense_rx_fd_detect(1);
12175
        eth_phy.collision(1);
12176
      end
12177
      endcase
12178
      // first 8 frames are received with RX BD 0 (wrap bit on RX BD 0)
12179
      // number of all frames is 154 (146 without first 8)
12180
      if (num_of_frames < 8)
12181
      begin
12182
        case (i_length[1:0])
12183
        2'h0: // Interrupt is generated
12184
        begin
12185
          // enable interrupt generation
12186
          set_rx_bd(0, 0, 1'b1, (`MEMORY_BASE + i_length[1:0]));
12187
          // interrupts are unmasked
12188
        end
12189
        2'h1: // Interrupt is not generated
12190
        begin
12191
          // enable interrupt generation
12192
          set_rx_bd(0, 0, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
12193
          // interrupts are masked
12194
        end
12195
        2'h2: // Interrupt is not generated
12196
        begin
12197
          // disable interrupt generation
12198
          set_rx_bd(0, 0, 1'b0, (`MEMORY_BASE + i_length[1:0]));
12199
          // interrupts are unmasked
12200
        end
12201
        default: // 2'h3: // Interrupt is not generated
12202
        begin
12203
          // disable interrupt generation
12204
          set_rx_bd(0, 0, 1'b0, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
12205
          // interrupts are masked
12206
        end
12207
        endcase
12208
        // set wrap bit
12209
        set_rx_bd_wrap(0);
12210
      end
12211
      // after first 8 number of frames, 128 frames form RX BD 0 to 127 will be received
12212
      else if ((num_of_frames - 8) == 0)
12213
      begin
12214
        tmp_len = i_length; // length of frame
12215
        tmp_bd_num = 0; // RX BD number
12216
        while (tmp_bd_num < 128) // (tmp_len <= (max_tmp - 4)) - this is the last frame
12217
        begin
12218
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
12219
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
12220
          if (tmp_len[0] == 0)
12221
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], (`MEMORY_BASE + tmp_len[1:0]));
12222
          else
12223
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
12224
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
12225
          if ((tmp_len + 4) < (min_tmp + 128))
12226
            tmp_len = tmp_len + 1;
12227
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12228
            tmp_len = 256;
12229
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12230
            tmp_len = tmp_len + 128;
12231
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
12232
            tmp_len = max_tmp - (4 + 16);
12233
          else if ((tmp_len + 4) >= (max_tmp - 16))
12234
            tmp_len = tmp_len + 1;
12235
          // set RX BD number
12236
          tmp_bd_num = tmp_bd_num + 1;
12237
        end
12238
        // set wrap bit
12239
        set_rx_bd_wrap(127);
12240
      end
12241
      // after 128 + first 8 number of frames, 19 frames form RX BD 0 to 18 will be received
12242
      else if ((num_of_frames - 8) == 20) // 128
12243
      begin
12244
        tmp_len = tmp_len; // length of frame remaines from previous settings
12245
        tmp_bd_num = 0; // TX BD number
12246
        while (tmp_bd_num < 19) // (tmp_len <= (max_tmp - 4)) - this is the last frame
12247
        begin
12248
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
12249
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
12250
          if (tmp_len[0] == 0)
12251
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], (`MEMORY_BASE + tmp_len[1:0]));
12252
          else
12253
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
12254
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
12255
          if ((tmp_len + 4) < (min_tmp + 128))
12256
            tmp_len = tmp_len + 1;
12257
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12258
            tmp_len = 256;
12259
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12260
            tmp_len = tmp_len + 128;
12261
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
12262
            tmp_len = max_tmp - (4 + 16);
12263
          else if ((tmp_len + 4) >= (max_tmp - 16))
12264
            tmp_len = tmp_len + 1;
12265
          // set TX BD number
12266
          tmp_bd_num = tmp_bd_num + 1;
12267
        end
12268
      end
12269
      // set empty bit
12270
      if (num_of_frames < 8)
12271
        set_rx_bd_empty(0, 0);
12272
      else if ((num_of_frames - 8) < 128)
12273
        set_rx_bd_empty((num_of_frames - 8), (num_of_frames - 8));
12274
      else if ((num_of_frames - 136) < 19)
12275
        set_rx_bd_empty((num_of_frames - 136), (num_of_frames - 136));
12276
      // CHECK END OF RECEIVE
12277
      fork
12278
        begin
12279
          if (i_length[0] == 1'b0)
12280
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
12281
          else
12282
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
12283
          repeat(10) @(posedge mrx_clk);
12284
        end
12285
        begin
12286
          #1 check_rx_bd(num_of_bd, data);
12287
          if (i_length < min_tmp) // just first four
12288
          begin
12289
            while (data[15] === 1)
12290
            begin
12291
              #1 check_rx_bd(num_of_bd, data);
12292
              @(posedge wb_clk);
12293
            end
12294
            repeat (1) @(posedge wb_clk);
12295
          end
12296
          else
12297
          begin
12298
            wait (MRxDV === 1'b1); // start transmit
12299
            #1 check_rx_bd(num_of_bd, data);
12300
            if (data[15] !== 1)
12301
            begin
12302
              test_fail("Wrong buffer descriptor's ready bit read out from MAC");
12303
              fail = fail + 1;
12304
            end
12305
            wait (MRxDV === 1'b0); // end transmit
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
        end
12314
      join
12315
      // check length of a PACKET
12316
      if (data[31:16] != (i_length + 4))
12317
      begin
12318
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
12319
                        data[31:16], (i_length + 4));
12320
        test_fail("Wrong length of the packet out from PHY");
12321
        fail = fail + 1;
12322
      end
12323
      // checking in the following if statement is performed only for first and last 64 lengths
12324
      // check received RX packet data and CRC
12325
      if (i_length[0] == 1'b0)
12326
      begin
12327
        check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
12328
      end
12329
      else
12330
      begin
12331
        check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
12332
      end
12333
      if (tmp > 0)
12334
      begin
12335
        `TIME; $display("*E Wrong data of the received packet");
12336
        test_fail("Wrong data of the received packet");
12337
        fail = fail + 1;
12338
      end
12339
      // check WB INT signal
12340
      if (i_length[1:0] == 2'h0)
12341
      begin
12342
        if (wb_int !== 1'b1)
12343
        begin
12344
          `TIME; $display("*E WB INT signal should be set");
12345
          test_fail("WB INT signal should be set");
12346
          fail = fail + 1;
12347
        end
12348
      end
12349
      else
12350
      begin
12351
        if (wb_int !== 1'b0)
12352
        begin
12353
          `TIME; $display("*E WB INT signal should not be set");
12354
          test_fail("WB INT signal should not be set");
12355
          fail = fail + 1;
12356
        end
12357
      end
12358
      // check RX buffer descriptor of a packet
12359
      check_rx_bd(num_of_bd, data);
12360
      if (i_length[1] == 1'b0) // interrupt enabled
12361
      begin
12362
        if ( ((data[15:0] !== 16'h6000) && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
12363
             ((data[15:0] !== 16'h4000) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
12364
        begin
12365
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
12366
          test_fail("RX buffer descriptor status is not correct");
12367
          fail = fail + 1;
12368
        end
12369
      end
12370
      else // interrupt not enabled
12371
      begin
12372
        if ( ((data[15:0] !== 16'h2000)  && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
12373
             ((data[15:0] !== 16'h0000) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
12374
        begin
12375
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
12376
          test_fail("RX buffer descriptor status is not correct");
12377
          fail = fail + 1;
12378
        end
12379
      end
12380
      // clear first half of 8 frames from RX buffer descriptor 0
12381
      if (num_of_frames < 4)
12382
        clear_rx_bd(num_of_bd, num_of_bd);
12383
      // clear BD with wrap bit
12384
      if (num_of_frames == 140)
12385
        clear_rx_bd(127, 127);
12386
      // check interrupts
12387
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12388
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
12389
      begin
12390
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
12391
        begin
12392
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
12393
          test_fail("Interrupt Receive Buffer was not set");
12394
          fail = fail + 1;
12395
        end
12396
        if ((data & (~`ETH_INT_RXB)) !== 0)
12397
        begin
12398
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
12399
          test_fail("Other interrupts (except Receive Buffer) were set");
12400
          fail = fail + 1;
12401
        end
12402
      end
12403
      else
12404
      begin
12405
        if (data !== 0)
12406
        begin
12407
          `TIME; $display("*E Any of interrupts (except Receive Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
12408
          test_fail("Any of interrupts (except Receive Buffer) was set");
12409
          fail = fail + 1;
12410
        end
12411
      end
12412
      // clear interrupts
12413
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12414
      // check WB INT signal
12415
      if (wb_int !== 1'b0)
12416
      begin
12417
        test_fail("WB INT signal should not be set");
12418
        fail = fail + 1;
12419
      end
12420
      // INTERMEDIATE DISPLAYS
12421
      if ((i_length + 4) == (min_tmp + 7))
12422
      begin
12423
        // starting length is min_tmp, ending length is (min_tmp + 128)
12424
        $display("    receive small packets is NOT selected");
12425
        $display("    using only RX BD 0 out of 128 BDs assigned to RX (wrap at first BD - RX BD 0)");
12426
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
12427
                 min_tmp, (min_tmp + 7));
12428
        $display("    ->all packets were received on RX BD 0");
12429
        // reset receive small, remain the rest
12430
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12431
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12432
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12433
      end
12434
      else if ((i_length + 4) == (min_tmp + 128))
12435
      begin
12436
        // starting length is min_tmp, ending length is (min_tmp + 128)
12437
        $display("    receive small packets is NOT selected");
12438
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12439
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
12440
                 (min_tmp + 8), (min_tmp + 128));
12441
        $display("    ->packets were received on RX BD %0d to RX BD %0d respectively",
12442
                 1'b0, num_of_bd);
12443
        tmp_bd = num_of_bd + 1;
12444
        // set receive small, remain the rest
12445
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12446
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12447
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12448
      end
12449
      else if ((i_length + 4) == (max_tmp - 16))
12450
      begin
12451
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
12452
        $display("    receive small packets is selected");
12453
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12454
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
12455
                 (min_tmp + 64 + 128), tmp_data);
12456
        if (tmp_bd > num_of_bd)
12457
          $display("    ->packets were received from RX BD %0d to RX BD 127 and from RX BD 0 to RX BD %0d respectively",
12458
                   tmp_bd, num_of_bd);
12459
        else
12460
          $display("    ->packets were received from RX BD %0d to RX BD %0d respectively",
12461
                   tmp_bd, num_of_bd);
12462
        tmp_bd = num_of_bd + 1;
12463
        // reset receive small, remain the rest
12464
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12465
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12466
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12467
      end
12468
      else if ((i_length + 4) == max_tmp)
12469
      begin
12470
        $display("    receive small packets is NOT selected");
12471
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12472
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
12473
                 (max_tmp - (4 + 16)), max_tmp);
12474
        if (tmp_bd > num_of_bd)
12475
          $display("    ->packets were received from RX BD %0d to RX BD 127 and from RX BD 0 to RX BD %0d respectively",
12476
                   tmp_bd, num_of_bd);
12477
        else
12478
          $display("    ->packets were received from RX BD %0d to RX BD %0d respectively",
12479
                   tmp_bd, num_of_bd);
12480
      end
12481
      // set length (loop variable)
12482
      if ((i_length + 4) < (min_tmp + 128))
12483
        i_length = i_length + 1;
12484
      else if ( ((i_length + 4) == (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
12485
        i_length = 256;
12486
      else if ( ((i_length + 4) > (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
12487
      begin
12488
        i_length = i_length + 128;
12489
        tmp_data = i_length + 4; // last tmp_data is ending length
12490
      end
12491
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
12492
        i_length = max_tmp - (4 + 16);
12493
      else if ((i_length + 4) >= (max_tmp - 16))
12494
        i_length = i_length + 1;
12495
      else
12496
      begin
12497
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
12498
        #10 $stop;
12499
      end
12500
      // the number of frame transmitted
12501
      num_of_frames = num_of_frames + 1;
12502
      if ((num_of_frames <= 8) || ((num_of_frames - 8) == 128))
12503
        num_of_bd = 0;
12504
      else
12505
        num_of_bd = num_of_bd + 1;
12506
    end
12507
    // disable RX
12508
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12509
              `ETH_MODER_PRO | `ETH_MODER_BRO,
12510
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12511
    @(posedge wb_clk);
12512
    if(fail == 0)
12513
      test_ok;
12514
    else
12515
      fail = 0;
12516
  end
12517
 
12518
 
12519
  ////////////////////////////////////////////////////////////////////
12520
  ////                                                            ////
12521
  ////  Test receive packets form MINFL to MAXFL sizes at         ////
12522
  ////  maximum RX buffer decriptors ( 100Mbps ).                 ////
12523
  ////                                                            ////
12524
  ////////////////////////////////////////////////////////////////////
12525 243 tadejm
  if (test_num == 7) // 
12526 209 tadejm
  begin
12527 243 tadejm
    // TEST 7: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 100Mbps )
12528
    test_name = "TEST 7: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 100Mbps )";
12529
    `TIME; $display("  TEST 7: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 100Mbps )");
12530 209 tadejm
 
12531
    // reset MAC registers
12532
    hard_reset;
12533
    // reset MAC and MII LOGIC with soft reset
12534
    reset_mac;
12535
    reset_mii;
12536
    // set wb slave response
12537
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
12538
 
12539
    max_tmp = 0;
12540
    min_tmp = 0;
12541
    num_of_frames = 0;
12542
    num_of_bd = 0;
12543
    // set maximum RX buffer descriptors (128) - must be set before RX enable
12544
    wbm_write(`ETH_TX_BD_NUM, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12545
    // enable RX, set full-duplex mode, NO receive small, NO correct IFG
12546
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12547
              `ETH_MODER_PRO | `ETH_MODER_BRO,
12548
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12549
    // prepare two packets of MAXFL length
12550
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12551
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
12552
    min_tmp = tmp[31:16];
12553
    st_data = 8'hAC;
12554
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
12555
    st_data = 8'h35;
12556
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
12557
    // check WB INT signal
12558
    if (wb_int !== 1'b0)
12559
    begin
12560
      test_fail("WB INT signal should not be set");
12561
      fail = fail + 1;
12562
    end
12563
 
12564
    // write to phy's control register for 100Mbps
12565
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
12566
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
12567
    speed = 100;
12568
 
12569
    i_length = (min_tmp - 4);
12570
    while (i_length <= (max_tmp - 4))
12571
    begin
12572
      // append CRC to packet
12573
      if (i_length[0] == 1'b0)
12574
        append_rx_crc (0, i_length, 1'b0, 1'b0);
12575
      else
12576
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
12577
      // choose generating carrier sense and collision
12578
      case (i_length[1:0])
12579
      2'h0: // Interrupt is generated
12580
      begin
12581
        // Reset_tx_bd nable interrupt generation
12582
        // unmask interrupts
12583
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
12584
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12585
        // not detect carrier sense in FD and no collision
12586
        eth_phy.no_carrier_sense_rx_fd_detect(0);
12587
        eth_phy.collision(0);
12588
      end
12589
      2'h1: // Interrupt is not generated
12590
      begin
12591
        // set_tx_bd enable interrupt generation
12592
        // mask interrupts
12593
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12594
        // detect carrier sense in FD and no collision
12595
        eth_phy.no_carrier_sense_rx_fd_detect(1);
12596
        eth_phy.collision(0);
12597
      end
12598
      2'h2: // Interrupt is not generated
12599
      begin
12600
        // set_tx_bd disable the interrupt generation
12601
        // unmask interrupts
12602
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
12603
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12604
        // not detect carrier sense in FD and set collision
12605
        eth_phy.no_carrier_sense_rx_fd_detect(0);
12606
        eth_phy.collision(1);
12607
      end
12608
      default: // 2'h3: // Interrupt is not generated
12609
      begin
12610
        // set_tx_bd disable the interrupt generation
12611
        // mask interrupts
12612
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12613
        // detect carrier sense in FD and set collision
12614
        eth_phy.no_carrier_sense_rx_fd_detect(1);
12615
        eth_phy.collision(1);
12616
      end
12617
      endcase
12618
      // first 8 frames are received with RX BD 0 (wrap bit on RX BD 0)
12619
      // number of all frames is 154 (146 without first 8)
12620
      if (num_of_frames < 8)
12621
      begin
12622
        case (i_length[1:0])
12623
        2'h0: // Interrupt is generated
12624
        begin
12625
          // enable interrupt generation
12626
          set_rx_bd(0, 0, 1'b1, (`MEMORY_BASE + i_length[1:0]));
12627
          // interrupts are unmasked
12628
        end
12629
        2'h1: // Interrupt is not generated
12630
        begin
12631
          // enable interrupt generation
12632
          set_rx_bd(0, 0, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
12633
          // interrupts are masked
12634
        end
12635
        2'h2: // Interrupt is not generated
12636
        begin
12637
          // disable interrupt generation
12638
          set_rx_bd(0, 0, 1'b0, (`MEMORY_BASE + i_length[1:0]));
12639
          // interrupts are unmasked
12640
        end
12641
        default: // 2'h3: // Interrupt is not generated
12642
        begin
12643
          // disable interrupt generation
12644
          set_rx_bd(0, 0, 1'b0, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
12645
          // interrupts are masked
12646
        end
12647
        endcase
12648
        // set wrap bit
12649
        set_rx_bd_wrap(0);
12650
      end
12651
      // after first 8 number of frames, 128 frames form RX BD 0 to 127 will be received
12652
      else if ((num_of_frames - 8) == 0)
12653
      begin
12654
        tmp_len = i_length; // length of frame
12655
        tmp_bd_num = 0; // RX BD number
12656
        while (tmp_bd_num < 128) // (tmp_len <= (max_tmp - 4)) - this is the last frame
12657
        begin
12658
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
12659
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
12660
          if (tmp_len[0] == 0)
12661
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], (`MEMORY_BASE + tmp_len[1:0]));
12662
          else
12663
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
12664
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
12665
          if ((tmp_len + 4) < (min_tmp + 128))
12666
            tmp_len = tmp_len + 1;
12667
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12668
            tmp_len = 256;
12669
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12670
            tmp_len = tmp_len + 128;
12671
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
12672
            tmp_len = max_tmp - (4 + 16);
12673
          else if ((tmp_len + 4) >= (max_tmp - 16))
12674
            tmp_len = tmp_len + 1;
12675
          // set RX BD number
12676
          tmp_bd_num = tmp_bd_num + 1;
12677
        end
12678
        // set wrap bit
12679
        set_rx_bd_wrap(127);
12680
      end
12681
      // after 128 + first 8 number of frames, 19 frames form RX BD 0 to 18 will be received
12682
      else if ((num_of_frames - 8) == 20) // 128
12683
      begin
12684
        tmp_len = tmp_len; // length of frame remaines from previous settings
12685
        tmp_bd_num = 0; // TX BD number
12686
        while (tmp_bd_num < 19) // (tmp_len <= (max_tmp - 4)) - this is the last frame
12687
        begin
12688
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
12689
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
12690
          if (tmp_len[0] == 0)
12691
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], (`MEMORY_BASE + tmp_len[1:0]));
12692
          else
12693
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
12694
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
12695
          if ((tmp_len + 4) < (min_tmp + 128))
12696
            tmp_len = tmp_len + 1;
12697
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12698
            tmp_len = 256;
12699
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12700
            tmp_len = tmp_len + 128;
12701
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
12702
            tmp_len = max_tmp - (4 + 16);
12703
          else if ((tmp_len + 4) >= (max_tmp - 16))
12704
            tmp_len = tmp_len + 1;
12705
          // set TX BD number
12706
          tmp_bd_num = tmp_bd_num + 1;
12707
        end
12708
      end
12709
      // set empty bit
12710
      if (num_of_frames < 8)
12711
        set_rx_bd_empty(0, 0);
12712
      else if ((num_of_frames - 8) < 128)
12713
        set_rx_bd_empty((num_of_frames - 8), (num_of_frames - 8));
12714
      else if ((num_of_frames - 136) < 19)
12715
        set_rx_bd_empty((num_of_frames - 136), (num_of_frames - 136));
12716
      // CHECK END OF RECEIVE
12717
      fork
12718
        begin
12719
          if (i_length[0] == 1'b0)
12720
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
12721
          else
12722
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
12723
          repeat(10) @(posedge mrx_clk);
12724
        end
12725
        begin
12726
          #1 check_rx_bd(num_of_bd, data);
12727
          if (i_length < min_tmp) // just first four
12728
          begin
12729
            while (data[15] === 1)
12730
            begin
12731
              #1 check_rx_bd(num_of_bd, data);
12732
              @(posedge wb_clk);
12733
            end
12734
            repeat (1) @(posedge wb_clk);
12735
          end
12736
          else
12737
          begin
12738
            wait (MRxDV === 1'b1); // start transmit
12739
            #1 check_rx_bd(num_of_bd, data);
12740
            if (data[15] !== 1)
12741
            begin
12742
              test_fail("Wrong buffer descriptor's ready bit read out from MAC");
12743
              fail = fail + 1;
12744
            end
12745
            wait (MRxDV === 1'b0); // end transmit
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
        end
12754
      join
12755
      // check length of a PACKET
12756
      if (data[31:16] != (i_length + 4))
12757
      begin
12758
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
12759
                        data[31:16], (i_length + 4));
12760
        test_fail("Wrong length of the packet out from PHY");
12761
        fail = fail + 1;
12762
      end
12763
      // check received RX packet data and CRC
12764
      if (i_length[0] == 1'b0)
12765
      begin
12766
        check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
12767
      end
12768
      else
12769
      begin
12770
        check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
12771
      end
12772
      if (tmp > 0)
12773
      begin
12774
        `TIME; $display("*E Wrong data of the received packet");
12775
        test_fail("Wrong data of the received packet");
12776
        fail = fail + 1;
12777
      end
12778
      // check WB INT signal
12779
      if (i_length[1:0] == 2'h0)
12780
      begin
12781
        if (wb_int !== 1'b1)
12782
        begin
12783
          `TIME; $display("*E WB INT signal should be set");
12784
          test_fail("WB INT signal should be set");
12785
          fail = fail + 1;
12786
        end
12787
      end
12788
      else
12789
      begin
12790
        if (wb_int !== 1'b0)
12791
        begin
12792
          `TIME; $display("*E WB INT signal should not be set");
12793
          test_fail("WB INT signal should not be set");
12794
          fail = fail + 1;
12795
        end
12796
      end
12797
      // check RX buffer descriptor of a packet
12798
      check_rx_bd(num_of_bd, data);
12799
      if (i_length[1] == 1'b0) // interrupt enabled
12800
      begin
12801
        if ( ((data[15:0] !== 16'h6000) && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
12802
             ((data[15:0] !== 16'h4000) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
12803
        begin
12804
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
12805
          test_fail("RX buffer descriptor status is not correct");
12806
          fail = fail + 1;
12807
        end
12808
      end
12809
      else // interrupt not enabled
12810
      begin
12811
        if ( ((data[15:0] !== 16'h2000)  && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
12812
             ((data[15:0] !== 16'h0000) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
12813
        begin
12814
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
12815
          test_fail("RX buffer descriptor status is not correct");
12816
          fail = fail + 1;
12817
        end
12818
      end
12819
      // clear first half of 8 frames from RX buffer descriptor 0
12820
      if (num_of_frames < 4)
12821
        clear_rx_bd(num_of_bd, num_of_bd);
12822
      // clear BD with wrap bit
12823
      if (num_of_frames == 140)
12824
        clear_rx_bd(127, 127);
12825
      // check interrupts
12826
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12827
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
12828
      begin
12829
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
12830
        begin
12831
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
12832
          test_fail("Interrupt Receive Buffer was not set");
12833
          fail = fail + 1;
12834
        end
12835
        if ((data & (~`ETH_INT_RXB)) !== 0)
12836
        begin
12837
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
12838
          test_fail("Other interrupts (except Receive Buffer) were set");
12839
          fail = fail + 1;
12840
        end
12841
      end
12842
      else
12843
      begin
12844
        if (data !== 0)
12845
        begin
12846
          `TIME; $display("*E Any of interrupts (except Receive Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
12847
          test_fail("Any of interrupts (except Receive Buffer) was set");
12848
          fail = fail + 1;
12849
        end
12850
      end
12851
      // clear interrupts
12852
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12853
      // check WB INT signal
12854
      if (wb_int !== 1'b0)
12855
      begin
12856
        test_fail("WB INT signal should not be set");
12857
        fail = fail + 1;
12858
      end
12859
      // INTERMEDIATE DISPLAYS
12860
      if ((i_length + 4) == (min_tmp + 7))
12861
      begin
12862
        // starting length is min_tmp, ending length is (min_tmp + 128)
12863
        $display("    receive small packets is NOT selected");
12864
        $display("    using only RX BD 0 out of 128 BDs assigned to RX (wrap at first BD - RX BD 0)");
12865
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
12866
                 min_tmp, (min_tmp + 7));
12867
        $display("    ->all packets were received on RX BD 0");
12868
        // reset receive small, remain the rest
12869
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12870
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12871
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12872
      end
12873
      else if ((i_length + 4) == (min_tmp + 128))
12874
      begin
12875
        // starting length is min_tmp, ending length is (min_tmp + 128)
12876
        $display("    receive small packets is NOT selected");
12877
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12878
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
12879
                 (min_tmp + 8), (min_tmp + 128));
12880
        $display("    ->packets were received on RX BD %0d to RX BD %0d respectively",
12881
                 1'b0, num_of_bd);
12882
        tmp_bd = num_of_bd + 1;
12883
        // set receive small, remain the rest
12884
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12885
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12886
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12887
      end
12888
      else if ((i_length + 4) == (max_tmp - 16))
12889
      begin
12890
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
12891
        $display("    receive small packets is selected");
12892
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12893
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
12894
                 (min_tmp + 64 + 128), tmp_data);
12895
        if (tmp_bd > num_of_bd)
12896
          $display("    ->packets were received from RX BD %0d to RX BD 127 and from RX BD 0 to RX BD %0d respectively",
12897
                   tmp_bd, num_of_bd);
12898
        else
12899
          $display("    ->packets were received from RX BD %0d to RX BD %0d respectively",
12900
                   tmp_bd, num_of_bd);
12901
        tmp_bd = num_of_bd + 1;
12902
        // reset receive small, remain the rest
12903
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12904
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12905
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12906
      end
12907
      else if ((i_length + 4) == max_tmp)
12908
      begin
12909
        $display("    receive small packets is NOT selected");
12910
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12911
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
12912
                 (max_tmp - (4 + 16)), max_tmp);
12913
        if (tmp_bd > num_of_bd)
12914
          $display("    ->packets were received from RX BD %0d to RX BD 127 and from RX BD 0 to RX BD %0d respectively",
12915
                   tmp_bd, num_of_bd);
12916
        else
12917
          $display("    ->packets were received from RX BD %0d to RX BD %0d respectively",
12918
                   tmp_bd, num_of_bd);
12919
      end
12920
      // set length (loop variable)
12921
      if ((i_length + 4) < (min_tmp + 128))
12922
        i_length = i_length + 1;
12923
      else if ( ((i_length + 4) == (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
12924
        i_length = 256;
12925
      else if ( ((i_length + 4) > (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
12926
      begin
12927
        i_length = i_length + 128;
12928
        tmp_data = i_length + 4; // last tmp_data is ending length
12929
      end
12930
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
12931
        i_length = max_tmp - (4 + 16);
12932
      else if ((i_length + 4) >= (max_tmp - 16))
12933
        i_length = i_length + 1;
12934
      else
12935
      begin
12936
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
12937
        #10 $stop;
12938
      end
12939
      // the number of frame transmitted
12940
      num_of_frames = num_of_frames + 1;
12941
      if ((num_of_frames <= 8) || ((num_of_frames - 8) == 128))
12942
        num_of_bd = 0;
12943
      else
12944
        num_of_bd = num_of_bd + 1;
12945
    end
12946
    // disable RX
12947
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12948
              `ETH_MODER_PRO | `ETH_MODER_BRO,
12949
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12950
    @(posedge wb_clk);
12951
    if(fail == 0)
12952
      test_ok;
12953
    else
12954
      fail = 0;
12955
  end
12956
 
12957
 
12958
  ////////////////////////////////////////////////////////////////////
12959
  ////                                                            ////
12960 243 tadejm
  ////  Test receive packets form 0 to (MINFL + 12) sizes at       ////
12961 209 tadejm
  ////  8 RX buffer decriptors ( 10Mbps ).                        ////
12962
  ////                                                            ////
12963
  ////////////////////////////////////////////////////////////////////
12964 243 tadejm
  if (test_num == 8) // 
12965 209 tadejm
  begin
12966 243 tadejm
    // TEST 8: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 10Mbps )
12967
    test_name = "TEST 8: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 10Mbps )";
12968
    `TIME; $display("  TEST 8: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 10Mbps )");
12969 209 tadejm
 
12970
    // reset MAC registers
12971
    hard_reset;
12972
    // reset MAC and MII LOGIC with soft reset
12973
    reset_mac;
12974
    reset_mii;
12975
    // set wb slave response
12976
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
12977
 
12978
    max_tmp = 0;
12979
    min_tmp = 0;
12980
    // set 8 RX buffer descriptors (120 - 127) - must be set before RX enable
12981
    wbm_write(`ETH_TX_BD_NUM, 32'h78, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12982
    // enable RX, set full-duplex mode, receive small, NO correct IFG
12983
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12984
              `ETH_MODER_PRO | `ETH_MODER_BRO,
12985
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12986
    // prepare two packets of MAXFL length
12987
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12988
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
12989
    min_tmp = tmp[31:16];
12990
    st_data = 8'hAC;
12991
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
12992
    st_data = 8'h35;
12993
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
12994
    // check WB INT signal
12995
    if (wb_int !== 1'b0)
12996
    begin
12997
      test_fail("WB INT signal should not be set");
12998
      fail = fail + 1;
12999
    end
13000 243 tadejm
    // unmask interrupts
13001
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
13002
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13003 209 tadejm
 
13004
    // write to phy's control register for 10Mbps
13005
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
13006
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
13007
    speed = 10;
13008
 
13009 243 tadejm
    frame_ended = 0;
13010
    num_of_frames = 0;// 0; // 10;
13011
    num_of_bd = 120;
13012
    i_length = 0 - 4;// (0 - 4); // 6; // 4 less due to CRC
13013
    while ((i_length + 4) < 78) // (min_tmp - 4))
13014 209 tadejm
    begin
13015 243 tadejm
      // append CRC to packet
13016
      if ((i_length[0] == 1'b0) && (num_of_frames > 4))
13017
        append_rx_crc (0, i_length, 1'b0, 1'b0);
13018
      else if (num_of_frames > 4)
13019
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
13020 209 tadejm
      // choose generating carrier sense and collision
13021
      case (i_length[1:0])
13022 243 tadejm
      2'h0:
13023 209 tadejm
      begin
13024
        // not detect carrier sense in FD and no collision
13025 243 tadejm
        eth_phy.no_carrier_sense_rx_fd_detect(0);
13026 209 tadejm
        eth_phy.collision(0);
13027
      end
13028 243 tadejm
      2'h1:
13029 209 tadejm
      begin
13030
        // detect carrier sense in FD and no collision
13031 243 tadejm
        eth_phy.no_carrier_sense_rx_fd_detect(1);
13032 209 tadejm
        eth_phy.collision(0);
13033
      end
13034 243 tadejm
      2'h2:
13035 209 tadejm
      begin
13036
        // not detect carrier sense in FD and set collision
13037 243 tadejm
        eth_phy.no_carrier_sense_rx_fd_detect(0);
13038 209 tadejm
        eth_phy.collision(1);
13039
      end
13040 243 tadejm
      default: // 2'h3:
13041 209 tadejm
      begin
13042
        // detect carrier sense in FD and set collision
13043 243 tadejm
        eth_phy.no_carrier_sense_rx_fd_detect(1);
13044 209 tadejm
        eth_phy.collision(1);
13045
      end
13046
      endcase
13047
      #1;
13048 243 tadejm
      // first 10 frames are received with RX BD 120 (wrap bit on RX BD 120)
13049 209 tadejm
      if (num_of_frames <= 9)
13050
      begin
13051 243 tadejm
        case (i_length[1:0])
13052
        2'h0: // Interrupt is generated
13053
        begin
13054
          // enable interrupt generation
13055
          set_rx_bd(120, 120, 1'b1, (`MEMORY_BASE + i_length[1:0]));
13056
        end
13057
        2'h1: // Interrupt is generated
13058
        begin
13059
          // enable interrupt generation
13060
          set_rx_bd(120, 120, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
13061
        end
13062
        2'h2: // Interrupt is generated
13063
        begin
13064
          // enable interrupt generation
13065
          set_rx_bd(120, 120, 1'b1, (`MEMORY_BASE + i_length[1:0]));
13066
        end
13067
        default: // 2'h3: // Interrupt is generated
13068
        begin
13069
          // enable interrupt generation
13070
          set_rx_bd(120, 120, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
13071
        end
13072
        endcase
13073 209 tadejm
        // set wrap bit
13074 243 tadejm
        set_rx_bd_wrap(120);
13075 209 tadejm
      end
13076 243 tadejm
      // 10 <= num_of_frames < 18 => wrap set to TX BD 123
13077 209 tadejm
      else if ((num_of_frames == 10) || (num_of_frames == 14))
13078
      begin
13079
        tmp_len = i_length; // length of frame
13080 243 tadejm
        tmp_bd_num = 120; // RX BD number
13081
        while (tmp_bd_num < 124) // 
13082 209 tadejm
        begin
13083
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13084
          if (tmp_len[0] == 0)
13085 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13086 209 tadejm
          else
13087 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13088
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13089 209 tadejm
          tmp_len = tmp_len + 1;
13090 243 tadejm
          // set RX BD number
13091 209 tadejm
          tmp_bd_num = tmp_bd_num + 1;
13092
        end
13093
        // set wrap bit
13094 243 tadejm
        set_rx_bd_wrap(123);
13095 209 tadejm
      end
13096 243 tadejm
      // 18 <= num_of_frames < 28 => wrap set to RX BD 124
13097 209 tadejm
      else if ((num_of_frames == 18) || (num_of_frames == 23))
13098
      begin
13099
        tmp_len = i_length; // length of frame
13100 243 tadejm
        tmp_bd_num = 120; // RX BD number
13101
        while (tmp_bd_num < 125) // 
13102 209 tadejm
        begin
13103
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13104
          if (tmp_len[0] == 0)
13105 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13106 209 tadejm
          else
13107 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13108
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13109 209 tadejm
          tmp_len = tmp_len + 1;
13110 243 tadejm
          // set RX BD number
13111 209 tadejm
          tmp_bd_num = tmp_bd_num + 1;
13112
        end
13113
        // set wrap bit
13114 243 tadejm
        set_rx_bd_wrap(124);
13115 209 tadejm
      end
13116 243 tadejm
      // 28 <= num_of_frames < 40 => wrap set to RX BD 125
13117 209 tadejm
      else if ((num_of_frames == 28) || (num_of_frames == 34))
13118
      begin
13119
        tmp_len = i_length; // length of frame
13120 243 tadejm
        tmp_bd_num = 120; // RX BD number
13121
        while (tmp_bd_num < 126) // 
13122 209 tadejm
        begin
13123
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13124
          if (tmp_len[0] == 0)
13125 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13126 209 tadejm
          else
13127 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13128
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13129 209 tadejm
          tmp_len = tmp_len + 1;
13130 243 tadejm
          // set RX BD number
13131 209 tadejm
          tmp_bd_num = tmp_bd_num + 1;
13132
        end
13133
        // set wrap bit
13134 243 tadejm
        set_rx_bd_wrap(125);
13135 209 tadejm
      end
13136 243 tadejm
      // 40 <= num_of_frames < 54 => wrap set to RX BD 126
13137 209 tadejm
      else if ((num_of_frames == 40) || (num_of_frames == 47))
13138
      begin
13139
        tmp_len = i_length; // length of frame
13140 243 tadejm
        tmp_bd_num = 120; // RX BD number
13141
        while (tmp_bd_num < 127) // 
13142 209 tadejm
        begin
13143
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13144
          if (tmp_len[0] == 0)
13145 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13146 209 tadejm
          else
13147 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13148
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13149 209 tadejm
          tmp_len = tmp_len + 1;
13150 243 tadejm
          // set RX BD number
13151 209 tadejm
          tmp_bd_num = tmp_bd_num + 1;
13152
        end
13153
        // set wrap bit
13154 243 tadejm
        set_rx_bd_wrap(126);
13155 209 tadejm
      end
13156 243 tadejm
      // 54 <= num_of_frames < 70 => wrap set to RX BD 127
13157 209 tadejm
      else if ((num_of_frames == 54) || (num_of_frames == 62))
13158
      begin
13159
        tmp_len = i_length; // length of frame
13160 243 tadejm
        tmp_bd_num = 120; // RX BD number
13161
        while (tmp_bd_num < 128) // 
13162 209 tadejm
        begin
13163
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13164
          if (tmp_len[0] == 0)
13165 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13166 209 tadejm
          else
13167 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13168
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13169 209 tadejm
          tmp_len = tmp_len + 1;
13170 243 tadejm
          // set RX BD number
13171 209 tadejm
          tmp_bd_num = tmp_bd_num + 1;
13172
        end
13173
        // set wrap bit
13174 243 tadejm
        set_rx_bd_wrap(127);
13175 209 tadejm
      end
13176 243 tadejm
      // 70 <= num_of_frames < 78 => wrap set to RX BD 127
13177
      else if (num_of_frames == 70)
13178
      begin
13179
        tmp_len = i_length; // length of frame
13180
        tmp_bd_num = 120; // RX BD number
13181
        while (tmp_bd_num < 128) // 
13182
        begin
13183
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13184
          if (tmp_len[0] == 0)
13185
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13186
          else
13187
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13188
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13189
          tmp_len = tmp_len + 1;
13190
          // set RX BD number
13191
          tmp_bd_num = tmp_bd_num + 1;
13192
        end
13193
        // set wrap bit
13194
        set_rx_bd_wrap(127);
13195
      end
13196 209 tadejm
      #1;
13197 243 tadejm
      // SET empty bit
13198 209 tadejm
      if (num_of_frames < 10)
13199 243 tadejm
        set_rx_bd_empty(120, 120);
13200 209 tadejm
      else if (num_of_frames < 14)
13201 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 10), (120 + num_of_frames - 10));
13202 209 tadejm
      else if (num_of_frames < 18)
13203 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 14), (120 + num_of_frames - 14));
13204 209 tadejm
      else if (num_of_frames < 23)
13205 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 18), (120 + num_of_frames - 18));
13206 209 tadejm
      else if (num_of_frames < 28)
13207 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 23), (120 + num_of_frames - 23));
13208 209 tadejm
      else if (num_of_frames < 34)
13209 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 28), (120 + num_of_frames - 28));
13210 209 tadejm
      else if (num_of_frames < 40)
13211 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 34), (120 + num_of_frames - 34));
13212 209 tadejm
      else if (num_of_frames < 47)
13213 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 40), (120 + num_of_frames - 40));
13214 209 tadejm
      else if (num_of_frames < 54)
13215 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 47), (120 + num_of_frames - 47));
13216 209 tadejm
      else if (num_of_frames < 62)
13217 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 54), (120 + num_of_frames - 54));
13218 209 tadejm
      else if (num_of_frames < 70)
13219 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 62), (120 + num_of_frames - 62));
13220
      else if (num_of_frames < 78)
13221
        set_rx_bd_empty((120 + num_of_frames - 70), (120 + num_of_frames - 70));
13222
      // CHECK END OF RECEIVE
13223 252 tadejm
      // receive just preamble between some packets
13224
      if ((num_of_frames == 0) || (num_of_frames == 4) || (num_of_frames == 9))
13225
      begin
13226
        #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h6, 8'h55, 0, 0, 1'b0);
13227
        @(posedge mrx_clk);
13228
        wait (MRxDV === 1'b0); // end receive
13229
        repeat(10) @(posedge mrx_clk);
13230
        repeat(15) @(posedge wb_clk);
13231
      end
13232
      // receiving frames and checking end of them
13233 243 tadejm
      frame_ended = 0;
13234
      check_frame = 0;
13235 209 tadejm
      fork
13236
        begin
13237 243 tadejm
          if (i_length[0] == 1'b0)
13238
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
13239
          else
13240
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
13241
          repeat(10) @(posedge mrx_clk);
13242
        end
13243
        begin: fr_end1
13244
          wait (MRxDV === 1'b1); // start receive
13245
          #1 check_rx_bd(num_of_bd, data);
13246
          if (data[15] !== 1)
13247 209 tadejm
          begin
13248 243 tadejm
            test_fail("Wrong buffer descriptor's empty bit read out from MAC");
13249
            fail = fail + 1;
13250 209 tadejm
          end
13251 243 tadejm
          wait (MRxDV === 1'b0); // end receive
13252
          while ((data[15] === 1) && (check_frame == 0))
13253
          begin
13254
            #1 check_rx_bd(num_of_bd, data);
13255
            @(posedge wb_clk);
13256
          end
13257
          if (data[15] === 0)
13258
            frame_ended = 1;
13259
          repeat (1) @(posedge wb_clk);
13260 209 tadejm
        end
13261 243 tadejm
        begin
13262
          wait (MRxDV === 1'b1); // start receive
13263
          wait (MRxDV === 1'b0); // end receive
13264
          repeat(10) @(posedge mrx_clk);
13265
          repeat(15) @(posedge wb_clk);
13266
          check_frame = 1;
13267
        end
13268
      join
13269
      // check length of a PACKET
13270
      if ( ((data[31:16] != (i_length + 4)) && (num_of_frames >= 3)) ||
13271
           ((data[31:16] != 0) && (num_of_frames < 3)) )
13272
      begin
13273
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
13274
                        data[31:16], (i_length + 4));
13275
        test_fail("Wrong length of the packet out from PHY");
13276
        fail = fail + 1;
13277
      end
13278
      // check received RX packet data and CRC
13279
      if ((frame_ended == 1) && (num_of_frames >= 5)) // 5 bytes is minimum size without CRC error, since
13280
      begin                                           // CRC has 4 bytes for itself
13281
        if (i_length[0] == 1'b0)
13282
        begin
13283
          check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
13284
        end
13285 209 tadejm
        else
13286
        begin
13287 243 tadejm
          check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
13288 209 tadejm
        end
13289 243 tadejm
        if (tmp > 0)
13290
        begin
13291
          `TIME; $display("*E Wrong data of the received packet");
13292
          test_fail("Wrong data of the received packet");
13293
          fail = fail + 1;
13294
        end
13295 209 tadejm
      end
13296 243 tadejm
      // check WB INT signal
13297
      if (num_of_frames >= 3) // Frames smaller than 3 are not received.
13298
      begin                   // Frames greater then 5 always cause an interrupt (Frame received)
13299
        if (wb_int !== 1'b1)  // Frames with length 3 or 4 always cause an interrupt (CRC error)
13300
        begin
13301
          `TIME; $display("*E WB INT signal should be set");
13302
          test_fail("WB INT signal should be set");
13303
          fail = fail + 1;
13304
        end
13305
      end
13306
      else
13307
      begin
13308
        if (wb_int !== 1'b0)
13309
        begin
13310
          `TIME; $display("*E WB INT signal should not be set");
13311
          test_fail("WB INT signal should not be set");
13312
          fail = fail + 1;
13313
        end
13314
      end
13315
      // check RX buffer descriptor of a packet
13316 254 mohor
      // check RX buffer descriptor of a packet
13317 243 tadejm
      if (num_of_frames >= min_tmp)
13318
      begin
13319 254 mohor
        if ( (data[15:0] !== 16'h6080) && // wrap bit
13320
             (data[15:0] !== 16'h4080) ) // without wrap bit
13321 243 tadejm
        begin
13322
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13323
          test_fail("RX buffer descriptor status is not correct");
13324
          fail = fail + 1;
13325
        end
13326
      end
13327 254 mohor
      else if (num_of_frames > 6)
13328 243 tadejm
      begin
13329 254 mohor
        if ( (data[15:0] !== 16'h6084) && // wrap bit
13330
             (data[15:0] !== 16'h4084) ) // without wrap bit
13331
        begin
13332
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13333
          test_fail("RX buffer descriptor status is not correct");
13334
          fail = fail + 1;
13335
        end
13336
      end
13337
      else if (num_of_frames > 4) // MAC does not recognize Dest. ADDR. for lengths 5, 6 => no MISS
13338
      begin
13339 243 tadejm
        if ( (data[15:0] !== 16'h6004) && // wrap bit
13340
             (data[15:0] !== 16'h4004) ) // without wrap bit
13341
        begin
13342
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13343
          test_fail("RX buffer descriptor status is not correct");
13344
          fail = fail + 1;
13345
        end
13346
      end
13347 254 mohor
      else if (num_of_frames > 2) // MAC does not recognize Dest. ADDR. for length 3, 4 => no MISS, CRC ERROR
13348 243 tadejm
      begin
13349
        if ( (data[15:0] !== 16'h6006) && // wrap bit
13350
             (data[15:0] !== 16'h4006) ) // without wrap bit
13351
        begin
13352
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13353
          test_fail("RX buffer descriptor status is not correct");
13354
          fail = fail + 1;
13355
        end
13356
      end
13357
      else
13358
      begin
13359
        if (data[15] !== 1'b1)
13360
        begin
13361
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13362
          test_fail("RX buffer descriptor status is not correct");
13363
          fail = fail + 1;
13364
        end
13365
      end
13366
      // check interrupts
13367
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13368 209 tadejm
      if (num_of_frames >= 5)
13369
      begin
13370 243 tadejm
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
13371 209 tadejm
        begin
13372 243 tadejm
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
13373
          test_fail("Interrupt Receive Buffer was not set");
13374
          fail = fail + 1;
13375 209 tadejm
        end
13376 243 tadejm
        if ((data & (~`ETH_INT_RXB)) !== 0)
13377 209 tadejm
        begin
13378 243 tadejm
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
13379
          test_fail("Other interrupts (except Receive Buffer) were set");
13380
          fail = fail + 1;
13381 209 tadejm
        end
13382 243 tadejm
      end
13383
      else if ((num_of_frames < 3)) // Frames smaller than 3 are not received.
13384
      begin
13385
        if (data) // Checking if any interrupt is pending)
13386 209 tadejm
        begin
13387 243 tadejm
          `TIME; $display("*E Interrupt(s) is(are) pending although frame was ignored, interrupt reg: %0h", data);
13388
          test_fail("Interrupts were set");
13389
          fail = fail + 1;
13390
        end
13391
      end
13392
      else
13393
      begin
13394
        if ((data & `ETH_INT_RXE) !== `ETH_INT_RXE)
13395
        begin
13396
          `TIME; $display("*E Interrupt Receive Buffer Error was not set, interrupt reg: %0h", data);
13397
          test_fail("Interrupt Receive Buffer Error was not set");
13398
          fail = fail + 1;
13399
        end
13400
        if ((data & (~`ETH_INT_RXE)) !== 0)
13401
        begin
13402
          `TIME; $display("*E Other interrupts (except Receive Buffer Error) were set, interrupt reg: %0h", data);
13403
          test_fail("Other interrupts (except Receive Buffer Error) were set");
13404
          fail = fail + 1;
13405
        end
13406
      end
13407
      // clear interrupts
13408
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13409
      // check WB INT signal
13410
      if (wb_int !== 1'b0)
13411
      begin
13412
        test_fail("WB INT signal should not be set");
13413
        fail = fail + 1;
13414
      end
13415
      // INTERMEDIATE DISPLAYS
13416
      if (num_of_frames == 3)
13417
      begin
13418
        $display("    using 1 BD out of 8 BDs (120..127) assigned to RX (wrap at 1st BD - RX BD 120)");
13419
        $display("    ->packets with lengths from %0d to %0d are not received (length increasing by 1 byte)",
13420
                 0, 3);
13421
      end
13422
      else if (num_of_frames == 9)
13423
      begin
13424
        $display("    using 1 BD out of 8 BDs (120..127) assigned to RX (wrap at 1st BD - RX BD 120)");
13425
        $display("    ->packet with length 4 is not received (length increasing by 1 byte)");
13426
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13427
                 5, 9);
13428
      end
13429
      else if (num_of_frames == 17)
13430
      begin
13431
        $display("    using 4 BDs out of 8 BDs (120..127) assigned to RX (wrap at 4th BD - RX BD 123)");
13432
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13433
                 10, 17);
13434
      end
13435
      else if (num_of_frames == 27)
13436
      begin
13437
        $display("    using 5 BDs out of 8 BDs (120..127) assigned to RX (wrap at 5th BD - RX BD 124)");
13438
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13439
                 18, 27);
13440
      end
13441
      else if (num_of_frames == 40)
13442
      begin
13443
        $display("    using 6 BDs out of 8 BDs (120..127) assigned to RX (wrap at 6th BD - RX BD 125)");
13444
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13445
                 28, 40);
13446
      end
13447
      else if (num_of_frames == 54)
13448
      begin
13449
        $display("    using 7 BDs out of 8 BDs (120..127) assigned to RX (wrap at 7th BD - RX BD 126)");
13450
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13451
                 41, 54);
13452
      end
13453
      else if (num_of_frames == 69)
13454
      begin
13455
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
13456
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13457
                 55, 69);
13458
      end
13459
      else if (num_of_frames == 69)
13460
      begin
13461
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
13462
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13463
                 55, 69);
13464
      end
13465
      else if (num_of_frames == 77)
13466
      begin
13467
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
13468
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13469
                 70, 77);
13470
      end
13471
      // set length (loop variable)
13472
      i_length = i_length + 1;
13473
      // the number of frame transmitted
13474
      num_of_frames = num_of_frames + 1;
13475
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
13476
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
13477
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
13478
          (num_of_frames == 54) || (num_of_frames == 62) || (num_of_frames == 70))
13479
        num_of_bd = 120;
13480
      else
13481
        num_of_bd = num_of_bd + 1;
13482
    end
13483
    // disable RX
13484
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
13485
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13486
    @(posedge wb_clk);
13487
    if(fail == 0)
13488
      test_ok;
13489
    else
13490
      fail = 0;
13491
  end
13492
 
13493
 
13494
  ////////////////////////////////////////////////////////////////////
13495
  ////                                                            ////
13496
  ////  Test receive packets form 0 to (MINFL + 12) sizes at      ////
13497
  ////  8 RX buffer decriptors ( 100Mbps ).                       ////
13498
  ////                                                            ////
13499
  ////////////////////////////////////////////////////////////////////
13500
  if (test_num == 9) // 
13501
  begin
13502
    // TEST 9: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 100Mbps )
13503
    test_name = "TEST 9: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 100Mbps )";
13504
    `TIME; $display("  TEST 9: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 100Mbps )");
13505
 
13506
    // reset MAC registers
13507
    hard_reset;
13508
    // reset MAC and MII LOGIC with soft reset
13509
    reset_mac;
13510
    reset_mii;
13511
    // set wb slave response
13512
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
13513
 
13514
    max_tmp = 0;
13515
    min_tmp = 0;
13516
    // set 8 RX buffer descriptors (120 - 127) - must be set before RX enable
13517
    wbm_write(`ETH_TX_BD_NUM, 32'h78, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13518
    // enable RX, set full-duplex mode, receive small, NO correct IFG
13519
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
13520
              `ETH_MODER_PRO | `ETH_MODER_BRO,
13521
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13522
    // prepare two packets of MAXFL length
13523
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13524
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
13525
    min_tmp = tmp[31:16];
13526
    st_data = 8'hAC;
13527
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
13528
    st_data = 8'h35;
13529
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
13530
    // check WB INT signal
13531
    if (wb_int !== 1'b0)
13532
    begin
13533
      test_fail("WB INT signal should not be set");
13534
      fail = fail + 1;
13535
    end
13536
    // unmask interrupts
13537
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
13538
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13539
 
13540
    // write to phy's control register for 100Mbps
13541
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
13542
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
13543
    speed = 100;
13544
 
13545
    frame_ended = 0;
13546 252 tadejm
    num_of_frames = 0;// 0; // 10;
13547 243 tadejm
    num_of_bd = 120;
13548
    i_length = 0 - 4;// (0 - 4); // 6; // 4 less due to CRC
13549
    while ((i_length + 4) < 78) // (min_tmp - 4))
13550
    begin
13551
      // append CRC to packet
13552 252 tadejm
      if ((i_length[0] == 1'b0) && (num_of_frames > 4))
13553 243 tadejm
        append_rx_crc (0, i_length, 1'b0, 1'b0);
13554 252 tadejm
      else if (num_of_frames > 4)
13555 243 tadejm
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
13556
      // choose generating carrier sense and collision
13557
      case (i_length[1:0])
13558
      2'h0:
13559
      begin
13560
        // not detect carrier sense in FD and no collision
13561
        eth_phy.no_carrier_sense_rx_fd_detect(0);
13562
        eth_phy.collision(0);
13563
      end
13564
      2'h1:
13565
      begin
13566
        // detect carrier sense in FD and no collision
13567
        eth_phy.no_carrier_sense_rx_fd_detect(1);
13568
        eth_phy.collision(0);
13569
      end
13570
      2'h2:
13571
      begin
13572
        // not detect carrier sense in FD and set collision
13573
        eth_phy.no_carrier_sense_rx_fd_detect(0);
13574
        eth_phy.collision(1);
13575
      end
13576
      default: // 2'h3:
13577
      begin
13578
        // detect carrier sense in FD and set collision
13579
        eth_phy.no_carrier_sense_rx_fd_detect(1);
13580
        eth_phy.collision(1);
13581
      end
13582
      endcase
13583
      #1;
13584
      // first 10 frames are received with RX BD 120 (wrap bit on RX BD 120)
13585
      if (num_of_frames <= 9)
13586
      begin
13587
        case (i_length[1:0])
13588
        2'h0: // Interrupt is generated
13589
        begin
13590
          // enable interrupt generation
13591
          set_rx_bd(120, 120, 1'b1, (`MEMORY_BASE + i_length[1:0]));
13592
        end
13593
        2'h1: // Interrupt is generated
13594
        begin
13595
          // enable interrupt generation
13596
          set_rx_bd(120, 120, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
13597
        end
13598
        2'h2: // Interrupt is generated
13599
        begin
13600
          // enable interrupt generation
13601
          set_rx_bd(120, 120, 1'b1, (`MEMORY_BASE + i_length[1:0]));
13602
        end
13603
        default: // 2'h3: // Interrupt is generated
13604
        begin
13605
          // enable interrupt generation
13606
          set_rx_bd(120, 120, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
13607
        end
13608
        endcase
13609
        // set wrap bit
13610
        set_rx_bd_wrap(120);
13611
      end
13612
      // 10 <= num_of_frames < 18 => wrap set to TX BD 123
13613
      else if ((num_of_frames == 10) || (num_of_frames == 14))
13614
      begin
13615
        tmp_len = i_length; // length of frame
13616
        tmp_bd_num = 120; // RX BD number
13617
        while (tmp_bd_num < 124) // 
13618
        begin
13619
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13620
          if (tmp_len[0] == 0)
13621
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13622
          else
13623
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13624
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13625
          tmp_len = tmp_len + 1;
13626
          // set RX BD number
13627
          tmp_bd_num = tmp_bd_num + 1;
13628
        end
13629
        // set wrap bit
13630
        set_rx_bd_wrap(123);
13631
      end
13632
      // 18 <= num_of_frames < 28 => wrap set to RX BD 124
13633
      else if ((num_of_frames == 18) || (num_of_frames == 23))
13634
      begin
13635
        tmp_len = i_length; // length of frame
13636
        tmp_bd_num = 120; // RX BD number
13637
        while (tmp_bd_num < 125) // 
13638
        begin
13639
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13640
          if (tmp_len[0] == 0)
13641
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13642
          else
13643
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13644
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13645
          tmp_len = tmp_len + 1;
13646
          // set RX BD number
13647
          tmp_bd_num = tmp_bd_num + 1;
13648
        end
13649
        // set wrap bit
13650
        set_rx_bd_wrap(124);
13651
      end
13652
      // 28 <= num_of_frames < 40 => wrap set to RX BD 125
13653
      else if ((num_of_frames == 28) || (num_of_frames == 34))
13654
      begin
13655
        tmp_len = i_length; // length of frame
13656
        tmp_bd_num = 120; // RX BD number
13657
        while (tmp_bd_num < 126) // 
13658
        begin
13659
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13660
          if (tmp_len[0] == 0)
13661
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13662
          else
13663
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13664
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13665
          tmp_len = tmp_len + 1;
13666
          // set RX BD number
13667
          tmp_bd_num = tmp_bd_num + 1;
13668
        end
13669
        // set wrap bit
13670
        set_rx_bd_wrap(125);
13671
      end
13672
      // 40 <= num_of_frames < 54 => wrap set to RX BD 126
13673
      else if ((num_of_frames == 40) || (num_of_frames == 47))
13674
      begin
13675
        tmp_len = i_length; // length of frame
13676
        tmp_bd_num = 120; // RX BD number
13677
        while (tmp_bd_num < 127) // 
13678
        begin
13679
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13680
          if (tmp_len[0] == 0)
13681
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13682
          else
13683
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13684
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13685
          tmp_len = tmp_len + 1;
13686
          // set RX BD number
13687
          tmp_bd_num = tmp_bd_num + 1;
13688
        end
13689
        // set wrap bit
13690
        set_rx_bd_wrap(126);
13691
      end
13692
      // 54 <= num_of_frames < 70 => wrap set to RX BD 127
13693
      else if ((num_of_frames == 54) || (num_of_frames == 62))
13694
      begin
13695
        tmp_len = i_length; // length of frame
13696
        tmp_bd_num = 120; // RX BD number
13697
        while (tmp_bd_num < 128) // 
13698
        begin
13699
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13700
          if (tmp_len[0] == 0)
13701
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13702
          else
13703
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13704
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13705
          tmp_len = tmp_len + 1;
13706
          // set RX BD number
13707
          tmp_bd_num = tmp_bd_num + 1;
13708
        end
13709
        // set wrap bit
13710
        set_rx_bd_wrap(127);
13711
      end
13712
      // 70 <= num_of_frames < 78 => wrap set to RX BD 127
13713
      else if (num_of_frames == 70)
13714
      begin
13715
        tmp_len = i_length; // length of frame
13716
        tmp_bd_num = 120; // RX BD number
13717
        while (tmp_bd_num < 128) // 
13718
        begin
13719
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13720
          if (tmp_len[0] == 0)
13721
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13722
          else
13723
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13724
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13725
          tmp_len = tmp_len + 1;
13726
          // set RX BD number
13727
          tmp_bd_num = tmp_bd_num + 1;
13728
        end
13729
        // set wrap bit
13730
        set_rx_bd_wrap(127);
13731
      end
13732
      #1;
13733
      // SET empty bit
13734
      if (num_of_frames < 10)
13735
        set_rx_bd_empty(120, 120);
13736
      else if (num_of_frames < 14)
13737
        set_rx_bd_empty((120 + num_of_frames - 10), (120 + num_of_frames - 10));
13738
      else if (num_of_frames < 18)
13739
        set_rx_bd_empty((120 + num_of_frames - 14), (120 + num_of_frames - 14));
13740
      else if (num_of_frames < 23)
13741
        set_rx_bd_empty((120 + num_of_frames - 18), (120 + num_of_frames - 18));
13742
      else if (num_of_frames < 28)
13743
        set_rx_bd_empty((120 + num_of_frames - 23), (120 + num_of_frames - 23));
13744
      else if (num_of_frames < 34)
13745
        set_rx_bd_empty((120 + num_of_frames - 28), (120 + num_of_frames - 28));
13746
      else if (num_of_frames < 40)
13747
        set_rx_bd_empty((120 + num_of_frames - 34), (120 + num_of_frames - 34));
13748
      else if (num_of_frames < 47)
13749
        set_rx_bd_empty((120 + num_of_frames - 40), (120 + num_of_frames - 40));
13750
      else if (num_of_frames < 54)
13751
        set_rx_bd_empty((120 + num_of_frames - 47), (120 + num_of_frames - 47));
13752
      else if (num_of_frames < 62)
13753
        set_rx_bd_empty((120 + num_of_frames - 54), (120 + num_of_frames - 54));
13754
      else if (num_of_frames < 70)
13755
        set_rx_bd_empty((120 + num_of_frames - 62), (120 + num_of_frames - 62));
13756
      else if (num_of_frames < 78)
13757
        set_rx_bd_empty((120 + num_of_frames - 70), (120 + num_of_frames - 70));
13758
      // CHECK END OF RECEIVE
13759 252 tadejm
      // receive just preamble between some packets
13760
      if ((num_of_frames == 0) || (num_of_frames == 4) || (num_of_frames == 9))
13761
      begin
13762
        #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h6, 8'h55, 0, 0, 1'b0);
13763
        @(posedge mrx_clk);
13764
        wait (MRxDV === 1'b0); // end receive
13765
        repeat(10) @(posedge mrx_clk);
13766
        repeat(15) @(posedge wb_clk);
13767
      end
13768
      // receiving frames and checking end of them
13769 243 tadejm
      frame_ended = 0;
13770
      check_frame = 0;
13771
      fork
13772
        begin
13773
          if (i_length[0] == 1'b0)
13774
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
13775
          else
13776
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
13777
          repeat(10) @(posedge mrx_clk);
13778
        end
13779 254 mohor
        begin: fr_end2
13780 243 tadejm
          wait (MRxDV === 1'b1); // start receive
13781
          #1 check_rx_bd(num_of_bd, data);
13782
          if (data[15] !== 1)
13783 209 tadejm
          begin
13784 243 tadejm
            test_fail("Wrong buffer descriptor's empty bit read out from MAC");
13785 209 tadejm
            fail = fail + 1;
13786
          end
13787 243 tadejm
          wait (MRxDV === 1'b0); // end receive
13788
          while ((data[15] === 1) && (check_frame == 0))
13789
          begin
13790
            #1 check_rx_bd(num_of_bd, data);
13791
            @(posedge wb_clk);
13792
          end
13793
          if (data[15] === 0)
13794
            frame_ended = 1;
13795
          repeat (1) @(posedge wb_clk);
13796 209 tadejm
        end
13797
        begin
13798 243 tadejm
          wait (MRxDV === 1'b1); // start receive
13799
          wait (MRxDV === 1'b0); // end receive
13800
          repeat(10) @(posedge mrx_clk);
13801
          repeat(15) @(posedge wb_clk);
13802
          check_frame = 1;
13803 209 tadejm
        end
13804 243 tadejm
      join
13805
      // check length of a PACKET
13806 252 tadejm
      if ( ((data[31:16] != (i_length + 4)) && (num_of_frames >= 3)) ||
13807
           ((data[31:16] != 0) && (num_of_frames < 3)) )
13808 243 tadejm
      begin
13809
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
13810
                        data[31:16], (i_length + 4));
13811
        test_fail("Wrong length of the packet out from PHY");
13812
        fail = fail + 1;
13813
      end
13814
      // check received RX packet data and CRC
13815 252 tadejm
      if ((frame_ended == 1) && (num_of_frames >= 5)) // 5 bytes is minimum size without CRC error, since
13816
      begin                                           // CRC has 4 bytes for itself
13817 243 tadejm
        if (i_length[0] == 1'b0)
13818 209 tadejm
        begin
13819 243 tadejm
          check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
13820 209 tadejm
        end
13821 243 tadejm
        else
13822 209 tadejm
        begin
13823 243 tadejm
          check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
13824 209 tadejm
        end
13825
        if (tmp > 0)
13826
        begin
13827 243 tadejm
          `TIME; $display("*E Wrong data of the received packet");
13828
          test_fail("Wrong data of the received packet");
13829 209 tadejm
          fail = fail + 1;
13830
        end
13831
      end
13832
      // check WB INT signal
13833 252 tadejm
      if (num_of_frames >= 3) // Frames smaller than 3 are not received.
13834
      begin                   // Frames greater then 5 always cause an interrupt (Frame received)
13835
        if (wb_int !== 1'b1)  // Frames with length 3 or 4 always cause an interrupt (CRC error)
13836 209 tadejm
        begin
13837
          `TIME; $display("*E WB INT signal should be set");
13838
          test_fail("WB INT signal should be set");
13839
          fail = fail + 1;
13840
        end
13841
      end
13842 252 tadejm
      else
13843 209 tadejm
      begin
13844
        if (wb_int !== 1'b0)
13845
        begin
13846
          `TIME; $display("*E WB INT signal should not be set");
13847
          test_fail("WB INT signal should not be set");
13848
          fail = fail + 1;
13849
        end
13850
      end
13851 243 tadejm
      // check RX buffer descriptor of a packet
13852
      if (num_of_frames >= min_tmp)
13853
      begin
13854 254 mohor
        if ( (data[15:0] !== 16'h6080) && // wrap bit
13855
             (data[15:0] !== 16'h4080) ) // without wrap bit
13856 209 tadejm
        begin
13857 243 tadejm
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13858
          test_fail("RX buffer descriptor status is not correct");
13859
          fail = fail + 1;
13860 209 tadejm
        end
13861 243 tadejm
      end
13862 254 mohor
      else if (num_of_frames > 6)
13863 243 tadejm
      begin
13864 254 mohor
        if ( (data[15:0] !== 16'h6084) && // wrap bit
13865
             (data[15:0] !== 16'h4084) ) // without wrap bit
13866
        begin
13867
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13868
          test_fail("RX buffer descriptor status is not correct");
13869
          fail = fail + 1;
13870
        end
13871
      end
13872
      else if (num_of_frames > 4) // MAC does not recognize Dest. ADDR. for lengths 5, 6 => no MISS
13873
      begin
13874 243 tadejm
        if ( (data[15:0] !== 16'h6004) && // wrap bit
13875
             (data[15:0] !== 16'h4004) ) // without wrap bit
13876 209 tadejm
        begin
13877 243 tadejm
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13878
          test_fail("RX buffer descriptor status is not correct");
13879
          fail = fail + 1;
13880 209 tadejm
        end
13881
      end
13882 254 mohor
      else if (num_of_frames > 2) // MAC does not recognize Dest. ADDR. for length 3, 4 => no MISS, CRC ERROR
13883 243 tadejm
      begin
13884
        if ( (data[15:0] !== 16'h6006) && // wrap bit
13885
             (data[15:0] !== 16'h4006) ) // without wrap bit
13886
        begin
13887
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13888
          test_fail("RX buffer descriptor status is not correct");
13889
          fail = fail + 1;
13890
        end
13891
      end
13892 209 tadejm
      else
13893
      begin
13894
        if (data[15] !== 1'b1)
13895
        begin
13896 243 tadejm
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13897
          test_fail("RX buffer descriptor status is not correct");
13898 209 tadejm
          fail = fail + 1;
13899
        end
13900
      end
13901
      // check interrupts
13902
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13903 252 tadejm
      if (num_of_frames >= 5)
13904 209 tadejm
      begin
13905 252 tadejm
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
13906 209 tadejm
        begin
13907 243 tadejm
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
13908
          test_fail("Interrupt Receive Buffer was not set");
13909 209 tadejm
          fail = fail + 1;
13910
        end
13911 243 tadejm
        if ((data & (~`ETH_INT_RXB)) !== 0)
13912 209 tadejm
        begin
13913 243 tadejm
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
13914
          test_fail("Other interrupts (except Receive Buffer) were set");
13915 209 tadejm
          fail = fail + 1;
13916
        end
13917
      end
13918 252 tadejm
      else if ((num_of_frames < 3)) // Frames smaller than 3 are not received.
13919
      begin
13920
        if (data) // Checking if any interrupt is pending)
13921
        begin
13922
          `TIME; $display("*E Interrupt(s) is(are) pending although frame was ignored, interrupt reg: %0h", data);
13923
          test_fail("Interrupts were set");
13924
          fail = fail + 1;
13925
        end
13926
      end
13927 209 tadejm
      else
13928
      begin
13929 252 tadejm
        if ((data & `ETH_INT_RXE) !== `ETH_INT_RXE)
13930 209 tadejm
        begin
13931 243 tadejm
          `TIME; $display("*E Interrupt Receive Buffer Error was not set, interrupt reg: %0h", data);
13932
          test_fail("Interrupt Receive Buffer Error was not set");
13933 209 tadejm
          fail = fail + 1;
13934
        end
13935 243 tadejm
        if ((data & (~`ETH_INT_RXE)) !== 0)
13936
        begin
13937
          `TIME; $display("*E Other interrupts (except Receive Buffer Error) were set, interrupt reg: %0h", data);
13938
          test_fail("Other interrupts (except Receive Buffer Error) were set");
13939
          fail = fail + 1;
13940
        end
13941 209 tadejm
      end
13942
      // clear interrupts
13943
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13944
      // check WB INT signal
13945
      if (wb_int !== 1'b0)
13946
      begin
13947
        test_fail("WB INT signal should not be set");
13948
        fail = fail + 1;
13949
      end
13950
      // INTERMEDIATE DISPLAYS
13951 243 tadejm
      if (num_of_frames == 3)
13952 209 tadejm
      begin
13953 243 tadejm
        $display("    using 1 BD out of 8 BDs (120..127) assigned to RX (wrap at 1st BD - RX BD 120)");
13954
        $display("    ->packets with lengths from %0d to %0d are not received (length increasing by 1 byte)",
13955 209 tadejm
                 0, 3);
13956
      end
13957 243 tadejm
      else if (num_of_frames == 9)
13958 209 tadejm
      begin
13959 243 tadejm
        $display("    using 1 BD out of 8 BDs (120..127) assigned to RX (wrap at 1st BD - RX BD 120)");
13960
        $display("    ->packet with length 4 is not received (length increasing by 1 byte)");
13961 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13962
                 5, 9);
13963
      end
13964 243 tadejm
      else if (num_of_frames == 17)
13965 209 tadejm
      begin
13966 243 tadejm
        $display("    using 4 BDs out of 8 BDs (120..127) assigned to RX (wrap at 4th BD - RX BD 123)");
13967 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13968
                 10, 17);
13969
      end
13970 243 tadejm
      else if (num_of_frames == 27)
13971 209 tadejm
      begin
13972 243 tadejm
        $display("    using 5 BDs out of 8 BDs (120..127) assigned to RX (wrap at 5th BD - RX BD 124)");
13973 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13974
                 18, 27);
13975
      end
13976 243 tadejm
      else if (num_of_frames == 40)
13977 209 tadejm
      begin
13978 243 tadejm
        $display("    using 6 BDs out of 8 BDs (120..127) assigned to RX (wrap at 6th BD - RX BD 125)");
13979 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13980
                 28, 40);
13981
      end
13982 243 tadejm
      else if (num_of_frames == 54)
13983 209 tadejm
      begin
13984 243 tadejm
        $display("    using 7 BDs out of 8 BDs (120..127) assigned to RX (wrap at 7th BD - RX BD 126)");
13985 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13986
                 41, 54);
13987
      end
13988 243 tadejm
      else if (num_of_frames == 69)
13989 209 tadejm
      begin
13990 243 tadejm
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
13991 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13992
                 55, 69);
13993
      end
13994 243 tadejm
      else if (num_of_frames == 69)
13995
      begin
13996
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
13997
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13998
                 55, 69);
13999
      end
14000
      else if (num_of_frames == 77)
14001
      begin
14002
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
14003
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
14004
                 70, 77);
14005
      end
14006 209 tadejm
      // set length (loop variable)
14007
      i_length = i_length + 1;
14008
      // the number of frame transmitted
14009
      num_of_frames = num_of_frames + 1;
14010
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
14011
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
14012
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
14013 243 tadejm
          (num_of_frames == 54) || (num_of_frames == 62) || (num_of_frames == 70))
14014
        num_of_bd = 120;
14015 209 tadejm
      else
14016
        num_of_bd = num_of_bd + 1;
14017
    end
14018 243 tadejm
    // disable RX
14019 209 tadejm
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
14020
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14021
    @(posedge wb_clk);
14022
    if(fail == 0)
14023
      test_ok;
14024
    else
14025
      fail = 0;
14026
  end
14027
 
14028
 
14029 243 tadejm
  ////////////////////////////////////////////////////////////////////
14030
  ////                                                            ////
14031
  ////  Test receive packet synchronization with receive          ////
14032
  ////  disable/enable ( 10Mbps ).                                ////
14033
  ////                                                            ////
14034
  ////////////////////////////////////////////////////////////////////
14035
  if (test_num == 10) // Test no receive when all buffers are TX ( 10Mbps ).
14036
  begin
14037
    // TEST 10: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )
14038
    test_name   = "TEST 10: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )";
14039
    `TIME; $display("  TEST 10: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )");
14040 209 tadejm
 
14041 243 tadejm
 
14042
 
14043
 
14044
 
14045
 
14046
  end
14047
 
14048
 
14049
  ////////////////////////////////////////////////////////////////////
14050
  ////                                                            ////
14051
  ////  Test receive packet synchronization with receive          ////
14052
  ////  disable/enable ( 10Mbps ).                                ////
14053
  ////                                                            ////
14054
  ////////////////////////////////////////////////////////////////////
14055
  if (test_num == 12) // Test no receive when all buffers are TX ( 10Mbps ).
14056
  begin
14057
    // TEST 12: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )
14058
    test_name   = "TEST 12: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )";
14059
    `TIME; $display("  TEST 12: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )");
14060
 
14061
 
14062
 
14063
 
14064
 
14065
 
14066
  end
14067
 
14068
 
14069
  ////////////////////////////////////////////////////////////////////
14070
  ////                                                            ////
14071
  ////  Test receive packet synchronization with receive          ////
14072
  ////  disable/enable ( 10Mbps ).                                ////
14073
  ////                                                            ////
14074
  ////////////////////////////////////////////////////////////////////
14075
  if (test_num == 14) // Test no receive when all buffers are TX ( 10Mbps ).
14076
  begin
14077
    // TEST 14: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )
14078
    test_name   = "TEST 14: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )";
14079
    `TIME; $display("  TEST 14: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )");
14080
 
14081
 
14082
 
14083
 
14084
 
14085
 
14086
  end
14087
 
14088
 
14089
  ////////////////////////////////////////////////////////////////////
14090
  ////                                                            ////
14091
  ////  Test receive packet synchronization with receive          ////
14092
  ////  disable/enable ( 10Mbps ).                                ////
14093
  ////                                                            ////
14094
  ////////////////////////////////////////////////////////////////////
14095
  if (test_num == 16) // Test no receive when all buffers are TX ( 10Mbps ).
14096
  begin
14097
    // TEST 16: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )
14098
    test_name   = "TEST 16: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )";
14099
    `TIME; $display("  TEST 16: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )");
14100
 
14101
 
14102
 
14103
 
14104
 
14105
 
14106
  end
14107
 
14108
 
14109
 
14110 209 tadejm
end   //  for (test_num=start_task; test_num <= end_task; test_num=test_num+1)
14111
 
14112
end
14113
endtask // test_mac_full_duplex_receive
14114
 
14115
 
14116
task test_mac_full_duplex_flow;
14117
  input  [31:0]  start_task;
14118
  input  [31:0]  end_task;
14119
  integer        bit_start_1;
14120
  integer        bit_end_1;
14121
  integer        bit_start_2;
14122
  integer        bit_end_2;
14123
  integer        num_of_reg;
14124
  integer        num_of_frames;
14125
  integer        num_of_bd;
14126
  integer        i_addr;
14127
  integer        i_data;
14128
  integer        i_length;
14129
  integer        tmp_len;
14130
  integer        tmp_bd;
14131
  integer        tmp_bd_num;
14132
  integer        tmp_data;
14133
  integer        tmp_ipgt;
14134
  integer        test_num;
14135
  reg    [31:0]  tx_bd_num;
14136
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_data;
14137
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_tmp_data;
14138
  integer        i;
14139
  integer        i1;
14140
  integer        i2;
14141
  integer        i3;
14142
  integer        fail;
14143
  integer        speed;
14144
  reg            frame_started;
14145
  reg            frame_ended;
14146
  reg            wait_for_frame;
14147
  reg    [31:0]  addr;
14148
  reg    [31:0]  data;
14149
  reg    [31:0]  tmp;
14150
  reg    [ 7:0]  st_data;
14151
  reg    [15:0]  max_tmp;
14152
  reg    [15:0]  min_tmp;
14153
begin
14154
// MAC FULL DUPLEX FLOW TEST
14155
test_heading("MAC FULL DUPLEX FLOW TEST");
14156
$display(" ");
14157
$display("MAC FULL DUPLEX FLOW TEST");
14158
fail = 0;
14159
 
14160
// reset MAC registers
14161
hard_reset;
14162
// reset MAC and MII LOGIC with soft reset
14163
reset_mac;
14164
reset_mii;
14165
// set wb slave response
14166
wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
14167
 
14168
  /*
14169
  TASKS for set and control TX buffer descriptors (also send packet - set_tx_bd_ready):
14170
  -------------------------------------------------------------------------------------
14171
  set_tx_bd
14172
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0], len[15:0], irq, pad, crc, txpnt[31:0]);
14173
  set_tx_bd_wrap
14174
    (tx_bd_num_end[6:0]);
14175
  set_tx_bd_ready
14176
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
14177
  check_tx_bd
14178
    (tx_bd_num_start[6:0], tx_bd_status[31:0]);
14179
  clear_tx_bd
14180
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
14181
 
14182
  TASKS for set and control RX buffer descriptors:
14183
  ------------------------------------------------
14184
  set_rx_bd
14185
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0], irq, rxpnt[31:0]);
14186
  set_rx_bd_wrap
14187
    (rx_bd_num_end[6:0]);
14188
  set_rx_bd_empty
14189
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
14190
  check_rx_bd
14191
    (rx_bd_num_end[6:0], rx_bd_status);
14192
  clear_rx_bd
14193
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
14194
 
14195
  TASKS for set and check TX packets:
14196
  -----------------------------------
14197
  set_tx_packet
14198
    (txpnt[31:0], len[15:0], eth_start_data[7:0]);
14199
  check_tx_packet
14200
    (txpnt_wb[31:0], txpnt_phy[31:0], len[15:0], failure[31:0]);
14201
 
14202
  TASKS for set and check RX packets:
14203
  -----------------------------------
14204
  set_rx_packet
14205
    (rxpnt[31:0], len[15:0], plus_nibble, d_addr[47:0], s_addr[47:0], type_len[15:0], start_data[7:0]);
14206
  check_rx_packet
14207
    (rxpnt_phy[31:0], rxpnt_wb[31:0], len[15:0], plus_nibble, successful_nibble, failure[31:0]);
14208
 
14209
  TASKS for append and check CRC to/of TX packet:
14210
  -----------------------------------------------
14211
  append_tx_crc
14212
    (txpnt_wb[31:0], len[15:0], negated_crc);
14213
  check_tx_crc
14214
    (txpnt_phy[31:0], len[15:0], negated_crc, failure[31:0]);
14215
 
14216
  TASK for append CRC to RX packet (CRC is checked together with check_rx_packet):
14217
  --------------------------------------------------------------------------------
14218
  append_rx_crc
14219
    (rxpnt_phy[31:0], len[15:0], plus_nibble, negated_crc);
14220
  */
14221
 
14222
//////////////////////////////////////////////////////////////////////
14223
////                                                              ////
14224
////  test_mac_full_duplex_flow:                                  ////
14225
////                                                              ////
14226
////  0: Test                                                     ////
14227
////                                                              ////
14228
//////////////////////////////////////////////////////////////////////
14229
for (test_num = start_task; test_num <= end_task; test_num = test_num + 1)
14230
begin
14231
 
14232
  ////////////////////////////////////////////////////////////////////
14233
  ////                                                            ////
14234 254 mohor
  ////  Test inserts control frames while transmitting normal     ////
14235
  ////  frames. Using 4 TX buffer decriptors ( 10Mbps ).          ////
14236 209 tadejm
  ////                                                            ////
14237
  ////////////////////////////////////////////////////////////////////
14238 254 mohor
  if (test_num == 0) // 
14239 209 tadejm
  begin
14240 254 mohor
    // TEST 0: INSERT CONTROL FRM. WHILE TRANSMITTING NORMAL FRM. AT 4 TX BD ( 10Mbps )
14241
    test_name = "TEST 0: INSERT CONTROL FRM. WHILE TRANSMITTING NORMAL FRM. AT 4 TX BD ( 10Mbps )";
14242
    `TIME; $display("  TEST 0: INSERT CONTROL FRM. WHILE TRANSMITTING NORMAL FRM. AT 4 TX BD ( 10Mbps )");
14243
 
14244
    // reset MAC completely
14245
    hard_reset;
14246
    // set wb slave response
14247
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
14248 209 tadejm
 
14249 254 mohor
    max_tmp = 0;
14250
    min_tmp = 0;
14251
    // set 4 TX buffer descriptors - must be set before TX enable
14252
    wbm_write(`ETH_TX_BD_NUM, 32'h4, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14253
    // enable TX, set full-duplex mode, padding and CRC appending
14254
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
14255
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14256
    // enable TX flow control
14257
    wbm_write(`ETH_CTRLMODER, `ETH_CTRLMODER_TXFLOW, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14258
    // prepare two packets of MAXFL length
14259
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14260
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
14261
    min_tmp = tmp[31:16];
14262
    st_data = 8'h12;
14263
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC. Writing data to the memory
14264
    st_data = 8'h34;
14265
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp - 4), st_data); // length without CRC. Writing data to the memory
14266
    // check WB INT signal
14267
    if (wb_int !== 1'b0)
14268
    begin
14269
      test_fail("WB INT signal should not be set");
14270
      fail = fail + 1;
14271
    end
14272
 
14273
    // write to phy's control register for 10Mbps
14274
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
14275
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
14276
    speed = 10;
14277
 
14278
    frame_started = 0;
14279
    num_of_frames = 0;
14280
    num_of_bd = 0;
14281
    i_length = 0; // 0;
14282 209 tadejm
 
14283 254 mohor
 
14284
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14285
    // In the following section, control frame will be sent while no other transmission is in progress.//
14286
    // TXC interrupt won't be unmasked.                                                                //
14287
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14288
    wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14289
    if(data)
14290
      begin
14291
        test_fail("IRQ already pending!");
14292
        fail = fail + 1;
14293
        `TIME; $display("*E IRQ already pending!");
14294
      end
14295
 
14296
    if (wb_int)
14297
    begin
14298
      test_fail("WB INT signal should not be set!");
14299
      fail = fail + 1;
14300
      `TIME; $display("*E WB INT signal should not be set!");
14301
    end
14302
 
14303
    // Request sending the control frame with pause value = 0x1234
14304
    wbm_write(`ETH_TX_CTRL, `ETH_TX_CTRL_TXPAUSERQ | 32'h1234, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14305
 
14306
    wait (MTxEn === 1'b1); // start transmit
14307
    wait (MTxEn === 1'b0); // end transmit
14308
    repeat(10) @ (posedge wb_clk);  // wait some time
14309
    repeat(10) @ (posedge mtx_clk); // wait some time
14310
 
14311
    wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14312
    if(data !== `ETH_INT_TXC)
14313
      begin
14314
        test_fail("TXC IRQ should be set!");
14315
        fail = fail + 1;
14316
        `TIME; $display("*E TXC IRQ should be set!");
14317
      end
14318
 
14319
    if (wb_int)
14320
    begin
14321
      test_fail("WB INT signal should not be set because TXC irq is masked!");
14322
      fail = fail + 1;
14323
      `TIME; $display("*E WB INT signal should not be set because TXC irq is masked!");
14324
    end
14325
 
14326
    // Clear TXC interrupt
14327
    wbm_write(`ETH_INT, `ETH_INT_TXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14328
 
14329
    if (wb_int)
14330
    begin
14331
      test_fail("WB INT signal should not be set!");
14332
      fail = fail + 1;
14333
      `TIME; $display("*E WB INT signal should not be set!");
14334
    end
14335
 
14336
 
14337
 
14338
 
14339
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14340
    // In the following section, control frame will be sent while no other transmission is in progress.//
14341
    // TXC interrupt is unmasked.                                                                      //
14342
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14343
 
14344
    // unmask all interrupts
14345
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
14346
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14347
 
14348
    wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14349
    if(data)
14350
      begin
14351
        test_fail("IRQ already pending!");
14352
        fail = fail + 1;
14353
        `TIME; $display("*E IRQ already pending!");
14354
      end
14355
 
14356
    if (wb_int)
14357
    begin
14358
      test_fail("WB INT signal should not be set!");
14359
      fail = fail + 1;
14360
      `TIME; $display("*E WB INT signal should not be set!");
14361
    end
14362
 
14363
    // unmask only TXC interrupts
14364
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14365
 
14366
    // Request sending the control frame with pause value = 0x1234
14367
    wbm_write(`ETH_TX_CTRL, `ETH_TX_CTRL_TXPAUSERQ | 32'h1234, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14368
 
14369
    wait (MTxEn === 1'b1); // start transmit
14370
    wait (MTxEn === 1'b0); // end transmit
14371
    repeat(10) @ (posedge wb_clk);  // wait some time
14372
    repeat(10) @ (posedge mtx_clk); // wait some time
14373
 
14374
    wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14375
    if(data !== `ETH_INT_TXC)
14376
      begin
14377
        test_fail("TXC IRQ should be set!");
14378
        fail = fail + 1;
14379
        `TIME; $display("*E TXC IRQ should be set!");
14380
      end
14381
 
14382
    if (!wb_int)
14383
    begin
14384
      test_fail("WB INT signal should be set!");
14385
      fail = fail + 1;
14386
      `TIME; $display("*E WB INT signal should be set!");
14387
    end
14388
 
14389
    // Clear TXC interrupt
14390
    wbm_write(`ETH_INT, `ETH_INT_TXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14391
 
14392
    if (wb_int)
14393
    begin
14394
      test_fail("WB INT signal should not be set!");
14395
      fail = fail + 1;
14396
      `TIME; $display("*E WB INT signal should not be set!");
14397
    end
14398
 
14399
 
14400
 
14401
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14402
    // In the following section, control frame sending is requested while will be sent while no other transmission is in progress.//
14403
    // TXC interrupt is unmasked.                                                                      //
14404
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14405
 
14406
    // unmask all interrupts
14407
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
14408
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14409
 
14410
    wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14411
    if(data)
14412
      begin
14413
        test_fail("IRQ already pending!");
14414
        fail = fail + 1;
14415
        `TIME; $display("*E IRQ already pending!");
14416
      end
14417
 
14418
    if (wb_int)
14419
    begin
14420
      test_fail("WB INT signal should not be set!");
14421
      fail = fail + 1;
14422
      `TIME; $display("*E WB INT signal should not be set!");
14423
    end
14424
`TIME; $display("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
14425
 
14426
    // Request sending the control frame with pause value = 0x5678
14427
//    set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
14428
    set_tx_bd(0, 0, 16'h100, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE)); // irq, pad, crc
14429
    set_tx_bd_wrap(0);
14430
    set_tx_bd_ready(0, 0);
14431
`TIME; $display("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
14432
 
14433
    wait (MTxEn === 1'b1); // start transmit
14434
`TIME; $display("CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC");
14435
    // Send control frame request
14436
    wbm_write(`ETH_TX_CTRL, `ETH_TX_CTRL_TXPAUSERQ | 32'h5678, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14437
    wait (MTxEn === 1'b0); // end transmit
14438
`TIME; $display("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");
14439
    repeat(10) @ (posedge wb_clk);  // wait some time
14440
    repeat(10) @ (posedge mtx_clk); // wait some time
14441
 
14442
    wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14443
    if(data !== `ETH_INT_TXB)
14444
      begin
14445
        test_fail("TXB IRQ should be set!");
14446
        fail = fail + 1;
14447
        `TIME; $display("*E TXB IRQ should be set!");
14448
      end
14449
 
14450
    wait (MTxEn === 1'b0); // end transmit of control frame
14451
    repeat(10) @ (posedge wb_clk);  // wait some time
14452
    repeat(10) @ (posedge mtx_clk); // wait some time
14453
 
14454
    wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14455
    if(data !== (`ETH_INT_TXC | `ETH_INT_TXB))
14456
      begin
14457
        test_fail("TXC and TXB IRQ should be set!");
14458
        fail = fail + 1;
14459
        `TIME; $display("*E TXC and TXB IRQ should be set!");
14460
      end
14461
 
14462
    if (!wb_int)
14463
    begin
14464
      test_fail("WB INT signal should be set!");
14465
      fail = fail + 1;
14466
      `TIME; $display("*E WB INT signal should be set!");
14467
    end
14468
 
14469
    // Clear TXC and TXB interrupt
14470
    wbm_write(`ETH_INT, `ETH_INT_TXC | `ETH_INT_TXB, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14471
 
14472
    if (wb_int)
14473
    begin
14474
      test_fail("WB INT signal should not be set!");
14475
      fail = fail + 1;
14476
      `TIME; $display("*E WB INT signal should not be set!");
14477
    end
14478
 
14479
 
14480
 
14481
 
14482
$display("\n\n\n\n MAMA: Konec !!!");
14483
 
14484
 
14485
 
14486
/*
14487
    while (i_length < 70) // (min_tmp - 4))
14488
    begin
14489
      #1;
14490
      // choose generating carrier sense and collision
14491
      case (i_length[1:0])
14492
      2'h0: // Interrupt is generated
14493
      begin
14494
        // Reset_tx_bd nable interrupt generation
14495
        // unmask interrupts
14496
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
14497
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14498
        // not detect carrier sense in FD and no collision
14499
        eth_phy.carrier_sense_tx_fd_detect(0);
14500
        eth_phy.collision(0);
14501
      end
14502
      2'h1: // Interrupt is not generated
14503
      begin
14504
        // set_tx_bd enable interrupt generation
14505
        // mask interrupts
14506
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14507
        // detect carrier sense in FD and no collision
14508
        eth_phy.carrier_sense_tx_fd_detect(1);
14509
        eth_phy.collision(0);
14510
      end
14511
      2'h2: // Interrupt is not generated
14512
      begin
14513
        // set_tx_bd disable the interrupt generation
14514
        // unmask interrupts
14515
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
14516
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14517
        // not detect carrier sense in FD and set collision
14518
        eth_phy.carrier_sense_tx_fd_detect(0);
14519
        eth_phy.collision(1);
14520
      end
14521
      default: // 2'h3: // Interrupt is not generated
14522
      begin
14523
        // set_tx_bd disable the interrupt generation
14524
        // mask interrupts
14525
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14526
        // detect carrier sense in FD and set collision
14527
        eth_phy.carrier_sense_tx_fd_detect(1);
14528
        eth_phy.collision(1);
14529
      end
14530
      endcase
14531
      #1;
14532
      // first destination address on ethernet PHY
14533
      eth_phy.set_tx_mem_addr(num_of_frames * 16);
14534
      // SET packets and wrap bit
14535
      // num_of_frames <= 9 => wrap set to TX BD 0
14536
      if (num_of_frames <= 9)
14537
      begin
14538
        tmp_len = i_length; // length of frame
14539
        tmp_bd_num = 0; // TX BD number
14540
        // if i_length[1] == 0 then enable interrupt generation otherwise disable it
14541
        // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
14542
        if (tmp_len[0] == 0)
14543
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
14544
        else
14545
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
14546
        // set wrap bit
14547
        set_tx_bd_wrap(0);
14548
      end
14549
      // 10 <= num_of_frames < 18 => wrap set to TX BD 3
14550
      else if ((num_of_frames == 10) || (num_of_frames == 14))
14551
      begin
14552
        tmp_len = i_length; // length of frame
14553
        tmp_bd_num = 0; // TX BD number
14554
        while (tmp_bd_num < 4) //
14555
        begin
14556
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
14557
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
14558
          if (tmp_len[0] == 0)
14559
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
14560
          else
14561
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
14562
          tmp_len = tmp_len + 1;
14563
          // set TX BD number
14564
          tmp_bd_num = tmp_bd_num + 1;
14565
        end
14566
        // set wrap bit
14567
        set_tx_bd_wrap(3);
14568
      end
14569
      // 18 <= num_of_frames < 28 => wrap set to TX BD 4
14570
      else if ((num_of_frames == 18) || (num_of_frames == 23))
14571
      begin
14572
        tmp_len = i_length; // length of frame
14573
        tmp_bd_num = 0; // TX BD number
14574
        while (tmp_bd_num < 5) //
14575
        begin
14576
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
14577
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
14578
          if (tmp_len[0] == 0)
14579
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
14580
          else
14581
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
14582
          tmp_len = tmp_len + 1;
14583
          // set TX BD number
14584
          tmp_bd_num = tmp_bd_num + 1;
14585
        end
14586
        // set wrap bit
14587
        set_tx_bd_wrap(4);
14588
      end
14589
      // 28 <= num_of_frames < 40 => wrap set to TX BD 5
14590
      else if ((num_of_frames == 28) || (num_of_frames == 34))
14591
      begin
14592
        tmp_len = i_length; // length of frame
14593
        tmp_bd_num = 0; // TX BD number
14594
        while (tmp_bd_num < 6) //
14595
        begin
14596
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
14597
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
14598
          if (tmp_len[0] == 0)
14599
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
14600
          else
14601
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
14602
          tmp_len = tmp_len + 1;
14603
          // set TX BD number
14604
          tmp_bd_num = tmp_bd_num + 1;
14605
        end
14606
        // set wrap bit
14607
        set_tx_bd_wrap(5);
14608
      end
14609
      // 40 <= num_of_frames < 54 => wrap set to TX BD 6
14610
      else if ((num_of_frames == 40) || (num_of_frames == 47))
14611
      begin
14612
        tmp_len = i_length; // length of frame
14613
        tmp_bd_num = 0; // TX BD number
14614
        while (tmp_bd_num < 7) //
14615
        begin
14616
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
14617
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
14618
          if (tmp_len[0] == 0)
14619
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
14620
          else
14621
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
14622
          tmp_len = tmp_len + 1;
14623
          // set TX BD number
14624
          tmp_bd_num = tmp_bd_num + 1;
14625
        end
14626
        // set wrap bit
14627
        set_tx_bd_wrap(6);
14628
      end
14629
      // 54 <= num_of_frames < 70 => wrap set to TX BD 7
14630
      else if ((num_of_frames == 54) || (num_of_frames == 62))
14631
      begin
14632
        tmp_len = i_length; // length of frame
14633
        tmp_bd_num = 0; // TX BD number
14634
        while (tmp_bd_num < 8) //
14635
        begin
14636
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
14637
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
14638
          if (tmp_len[0] == 0)
14639
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
14640
          else
14641
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
14642
          tmp_len = tmp_len + 1;
14643
          // set TX BD number
14644
          tmp_bd_num = tmp_bd_num + 1;
14645
        end
14646
        // set wrap bit
14647
        set_tx_bd_wrap(7);
14648
      end
14649
      #1;
14650
      // SET ready bit
14651
      if (num_of_frames < 10)
14652
        set_tx_bd_ready(0, 0);
14653
      else if (num_of_frames < 14)
14654
        set_tx_bd_ready((num_of_frames - 10), (num_of_frames - 10));
14655
      else if (num_of_frames < 18)
14656
        set_tx_bd_ready((num_of_frames - 14), (num_of_frames - 14));
14657
      else if (num_of_frames < 23)
14658
        set_tx_bd_ready((num_of_frames - 18), (num_of_frames - 18));
14659
      else if (num_of_frames < 28)
14660
        set_tx_bd_ready((num_of_frames - 23), (num_of_frames - 23));
14661
      else if (num_of_frames < 34)
14662
        set_tx_bd_ready((num_of_frames - 28), (num_of_frames - 28));
14663
      else if (num_of_frames < 40)
14664
        set_tx_bd_ready((num_of_frames - 34), (num_of_frames - 34));
14665
      else if (num_of_frames < 47)
14666
        set_tx_bd_ready((num_of_frames - 40), (num_of_frames - 40));
14667
      else if (num_of_frames < 54)
14668
        set_tx_bd_ready((num_of_frames - 47), (num_of_frames - 47));
14669
      else if (num_of_frames < 62)
14670
        set_tx_bd_ready((num_of_frames - 54), (num_of_frames - 54));
14671
      else if (num_of_frames < 70)
14672
        set_tx_bd_ready((num_of_frames - 62), (num_of_frames - 62));
14673
      // CHECK END OF TRANSMITION
14674
      frame_started = 0;
14675
      if (num_of_frames >= 5)
14676
        #1 check_tx_bd(num_of_bd, data);
14677
      fork
14678
      begin: fr_st
14679
        wait (MTxEn === 1'b1); // start transmit
14680
        frame_started = 1;
14681
      end
14682
      begin
14683
        repeat (30) @(posedge mtx_clk);
14684
        if (num_of_frames < 5)
14685
        begin
14686
          if (frame_started == 1)
14687
          begin
14688
            `TIME; $display("*E Frame should NOT start!");
14689
          end
14690
          disable fr_st;
14691
        end
14692
        else
14693
        begin
14694
          if (frame_started == 0)
14695
          begin
14696
            `TIME; $display("*W Frame should start!");
14697
            disable fr_st;
14698
          end
14699
        end
14700
      end
14701
      join
14702
      // check packets larger than 4 bytes
14703
      if (num_of_frames >= 5)
14704
      begin
14705
        wait (MTxEn === 1'b0); // end transmit
14706
        while (data[15] === 1)
14707
        begin
14708
          #1 check_tx_bd(num_of_bd, data);
14709
          @(posedge wb_clk);
14710
        end
14711
        repeat (1) @(posedge wb_clk);
14712
        // check length of a PACKET
14713
        if (i_length <= (min_tmp - 4))
14714
        begin
14715
          if (eth_phy.tx_len != min_tmp)
14716
          begin
14717
            test_fail("Wrong length of the packet out from MAC");
14718
            fail = fail + 1;
14719
          end
14720
        end
14721
        else
14722
        begin
14723
          if (eth_phy.tx_len != (i_length + 4))
14724
          begin
14725
            test_fail("Wrong length of the packet out from MAC");
14726
            fail = fail + 1;
14727
          end
14728
        end
14729
        // check transmitted TX packet data
14730
        if (i_length[0] == 0)
14731
        begin
14732
          #1 check_tx_packet(`MEMORY_BASE, (num_of_frames * 16), i_length, tmp);
14733
        end
14734
        else
14735
        begin
14736
          #1 check_tx_packet((`MEMORY_BASE + max_tmp), (num_of_frames * 16), i_length, tmp);
14737
        end
14738
        if (tmp > 0)
14739
        begin
14740
          test_fail("Wrong data of the transmitted packet");
14741
          fail = fail + 1;
14742
        end
14743
        // check transmited TX packet CRC
14744
        if (num_of_frames < (min_tmp - 4))
14745
          #1 check_tx_crc((num_of_frames * 16), (min_tmp - 4), 1'b0, tmp); // length without CRC
14746
        else
14747
          #1 check_tx_crc((num_of_frames * 16), i_length, 1'b0, tmp); // length without CRC
14748
        if (tmp > 0)
14749
        begin
14750
          test_fail("Wrong CRC of the transmitted packet");
14751
          fail = fail + 1;
14752
        end
14753
      end
14754
      // check WB INT signal
14755
      if ((i_length[1:0] == 2'h0) && (num_of_frames >= 5))
14756
      begin
14757
        if (wb_int !== 1'b1)
14758
        begin
14759
          `TIME; $display("*E WB INT signal should be set");
14760
          test_fail("WB INT signal should be set");
14761
          fail = fail + 1;
14762
        end
14763
      end
14764
      else
14765
      begin
14766
        if (wb_int !== 1'b0)
14767
        begin
14768
          `TIME; $display("*E WB INT signal should not be set");
14769
          test_fail("WB INT signal should not be set");
14770
          fail = fail + 1;
14771
        end
14772
      end
14773
      // check TX buffer descriptor of a packet
14774
      check_tx_bd(num_of_bd, data);
14775
      if (num_of_frames >= 5)
14776
      begin
14777
        if (i_length[1] == 1'b0) // interrupt enabled
14778
        begin
14779
          if ( (data[15:0] !== 16'h7800) && // wrap bit
14780
               (data[15:0] !== 16'h5800) ) // without wrap bit
14781
          begin
14782
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
14783
            test_fail("TX buffer descriptor status is not correct");
14784
            fail = fail + 1;
14785
          end
14786
        end
14787
        else // interrupt not enabled
14788
        begin
14789
          if ( (data[15:0] !== 16'h3800) && // wrap bit
14790
               (data[15:0] !== 16'h1800) ) // without wrap bit
14791
          begin
14792
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
14793
            test_fail("TX buffer descriptor status is not correct");
14794
            fail = fail + 1;
14795
          end
14796
        end
14797
      end
14798
      else
14799
      begin
14800
        if (data[15] !== 1'b1)
14801
        begin
14802
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
14803
          test_fail("TX buffer descriptor status is not correct");
14804
          fail = fail + 1;
14805
        end
14806
      end
14807
      // clear TX BD with wrap bit
14808
      if (num_of_frames == 63)
14809
        clear_tx_bd(16, 16);
14810
      // check interrupts
14811
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14812
      if ( ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1)) && (num_of_frames >= 5) )
14813
      begin
14814
        if ((data & `ETH_INT_TXB) !== 1'b1)
14815
        begin
14816
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
14817
          test_fail("Interrupt Transmit Buffer was not set");
14818
          fail = fail + 1;
14819
        end
14820
        if ((data & (~`ETH_INT_TXB)) !== 0)
14821
        begin
14822
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
14823
          test_fail("Other interrupts (except Transmit Buffer) were set");
14824
          fail = fail + 1;
14825
        end
14826
      end
14827
      else
14828
      begin
14829
        if (data !== 0)
14830
        begin
14831
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
14832
          test_fail("Any of interrupts (except Transmit Buffer) was set");
14833
          fail = fail + 1;
14834
        end
14835
      end
14836
      // clear interrupts
14837
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14838
      // check WB INT signal
14839
      if (wb_int !== 1'b0)
14840
      begin
14841
        test_fail("WB INT signal should not be set");
14842
        fail = fail + 1;
14843
      end
14844
      // INTERMEDIATE DISPLAYS
14845
      if (i_length == 3)
14846
      begin
14847
        $display("    pads appending to packets is selected");
14848
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
14849
        $display("    ->packets with lengths from %0d to %0d are not transmitted (length increasing by 1 byte)",
14850
                 0, 3);
14851
      end
14852
      else if (i_length == 9)
14853
      begin
14854
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
14855
        $display("    ->packet with length 4 is not transmitted (length increasing by 1 byte)");
14856
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
14857
                 5, 9);
14858
      end
14859
      else if (i_length == 17)
14860
      begin
14861
        $display("    using 4 BDs out of 8 BDs assigned to TX (wrap at 4th BD - TX BD 3)");
14862
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
14863
                 10, 17);
14864
      end
14865
      else if (i_length == 27)
14866
      begin
14867
        $display("    using 5 BDs out of 8 BDs assigned to TX (wrap at 5th BD - TX BD 4)");
14868
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
14869
                 18, 27);
14870
      end
14871
      else if (i_length == 40)
14872
      begin
14873
        $display("    using 6 BDs out of 8 BDs assigned to TX (wrap at 6th BD - TX BD 5)");
14874
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
14875
                 28, 40);
14876
      end
14877
      else if (i_length == 54)
14878
      begin
14879
        $display("    using 7 BDs out of 8 BDs assigned to TX (wrap at 7th BD - TX BD 6)");
14880
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
14881
                 41, 54);
14882
      end
14883
      else if (i_length == 69)
14884
      begin
14885
        $display("    using 8 BDs out of 8 BDs assigned to TX (wrap at 8th BD - TX BD 7)");
14886
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
14887
                 55, 69);
14888
      end
14889
      // set length (loop variable)
14890
      i_length = i_length + 1;
14891
      // the number of frame transmitted
14892
      num_of_frames = num_of_frames + 1;
14893
//      if (//(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||// (num_of_frames <= 10) ||
14894
      if ((num_of_frames <= 10) ||
14895
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
14896
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
14897
          (num_of_frames == 54) || (num_of_frames == 62))
14898
        num_of_bd = 0;
14899
      else
14900
        num_of_bd = num_of_bd + 1;
14901
    end
14902
    // disable TX
14903
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
14904
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14905
    @(posedge wb_clk);
14906
    if(fail == 0)
14907
      test_ok;
14908
    else
14909
      fail = 0;
14910
*/
14911
 
14912 209 tadejm
  end
14913
 
14914
 
14915
end   //  for (test_num=start_task; test_num <= end_task; test_num=test_num+1)
14916
 
14917
end
14918
endtask // test_mac_full_duplex_flow
14919
 
14920
 
14921 169 mohor
//////////////////////////////////////////////////////////////
14922
// WB Behavioral Models Basic tasks
14923
//////////////////////////////////////////////////////////////
14924
 
14925
task wbm_write;
14926
  input  [31:0] address_i;
14927
  input  [((`MAX_BLK_SIZE * 32) - 1):0] data_i;
14928
  input  [3:0]  sel_i;
14929
  input  [31:0] size_i;
14930
  input  [3:0]  init_waits_i;
14931
  input  [3:0]  subseq_waits_i;
14932
 
14933
  reg `WRITE_STIM_TYPE write_data;
14934
  reg `WB_TRANSFER_FLAGS flags;
14935
  reg `WRITE_RETURN_TYPE write_status;
14936
  integer i;
14937
begin
14938
  write_status = 0;
14939
 
14940
  flags                    = 0;
14941
  flags`WB_TRANSFER_SIZE   = size_i;
14942
  flags`INIT_WAITS         = init_waits_i;
14943
  flags`SUBSEQ_WAITS       = subseq_waits_i;
14944
 
14945
  write_data               = 0;
14946
  write_data`WRITE_DATA    = data_i[31:0];
14947
  write_data`WRITE_ADDRESS = address_i;
14948
  write_data`WRITE_SEL     = sel_i;
14949
 
14950
  for (i = 0; i < size_i; i = i + 1)
14951
  begin
14952
    wb_master.blk_write_data[i] = write_data;
14953
    data_i                      = data_i >> 32;
14954
    write_data`WRITE_DATA       = data_i[31:0];
14955
    write_data`WRITE_ADDRESS    = write_data`WRITE_ADDRESS + 4;
14956 116 mohor
  end
14957
 
14958 169 mohor
  wb_master.wb_block_write(flags, write_status);
14959 116 mohor
 
14960 169 mohor
  if (write_status`CYC_ACTUAL_TRANSFER !== size_i)
14961
  begin
14962
    `TIME;
14963
    $display("*E WISHBONE Master was unable to complete the requested write operation to MAC!");
14964
  end
14965
end
14966
endtask // wbm_write
14967 116 mohor
 
14968 169 mohor
task wbm_read;
14969
  input  [31:0] address_i;
14970
  output [((`MAX_BLK_SIZE * 32) - 1):0] data_o;
14971
  input  [3:0]  sel_i;
14972
  input  [31:0] size_i;
14973
  input  [3:0]  init_waits_i;
14974
  input  [3:0]  subseq_waits_i;
14975
 
14976
  reg `READ_RETURN_TYPE read_data;
14977
  reg `WB_TRANSFER_FLAGS flags;
14978
  reg `READ_RETURN_TYPE read_status;
14979
  integer i;
14980
begin
14981
  read_status = 0;
14982
  data_o      = 0;
14983
 
14984
  flags                  = 0;
14985
  flags`WB_TRANSFER_SIZE = size_i;
14986
  flags`INIT_WAITS       = init_waits_i;
14987
  flags`SUBSEQ_WAITS     = subseq_waits_i;
14988
 
14989
  read_data              = 0;
14990
  read_data`READ_ADDRESS = address_i;
14991
  read_data`READ_SEL     = sel_i;
14992
 
14993
  for (i = 0; i < size_i; i = i + 1)
14994 116 mohor
  begin
14995 169 mohor
    wb_master.blk_read_data_in[i] = read_data;
14996
    read_data`READ_ADDRESS        = read_data`READ_ADDRESS + 4;
14997
  end
14998
 
14999
  wb_master.wb_block_read(flags, read_status);
15000
 
15001
  if (read_status`CYC_ACTUAL_TRANSFER !== size_i)
15002
  begin
15003
    `TIME;
15004
    $display("*E WISHBONE Master was unable to complete the requested read operation from MAC!");
15005
  end
15006
 
15007
  for (i = 0; i < size_i; i = i + 1)
15008
  begin
15009
    data_o       = data_o << 32;
15010
    read_data    = wb_master.blk_read_data_out[(size_i - 1) - i]; // [31 - i];
15011
    data_o[31:0] = read_data`READ_DATA;
15012
  end
15013
end
15014
endtask // wbm_read
15015
 
15016
 
15017
//////////////////////////////////////////////////////////////
15018
// Ethernet Basic tasks
15019
//////////////////////////////////////////////////////////////
15020
 
15021
task hard_reset; //  MAC registers
15022
begin
15023
  // reset MAC registers
15024
  @(posedge wb_clk);
15025
  #2 wb_rst = 1'b1;
15026
  repeat(2) @(posedge wb_clk);
15027
  #2 wb_rst = 1'b0;
15028
end
15029
endtask // hard_reset
15030
 
15031
task reset_mac; //  MAC module
15032
  reg [31:0] tmp;
15033
  reg [31:0] tmp_no_rst;
15034
begin
15035
  // read MODER register first
15036
  wbm_read(`ETH_MODER, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15037
  // set reset bit - write back to MODER register with RESET bit
15038
  wbm_write(`ETH_MODER, (`ETH_MODER_RST | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15039
  // clear reset bit - write back to MODER register without RESET bit
15040
  tmp_no_rst = `ETH_MODER_RST;
15041
  tmp_no_rst = ~tmp_no_rst;
15042
  wbm_write(`ETH_MODER, (tmp_no_rst & tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15043
end
15044
endtask // reset_mac
15045
 
15046
task set_tx_bd;
15047
  input  [6:0]  tx_bd_num_start;
15048
  input  [6:0]  tx_bd_num_end;
15049
  input  [15:0] len;
15050
  input         irq;
15051
  input         pad;
15052
  input         crc;
15053
  input  [31:0] txpnt;
15054
 
15055
  integer       i;
15056
  integer       bd_status_addr, bd_ptr_addr;
15057
//  integer       buf_addr;
15058
begin
15059
  for(i = tx_bd_num_start; i <= tx_bd_num_end; i = i + 1)
15060
  begin
15061
//    buf_addr = `TX_BUF_BASE + i * 32'h600;
15062
    bd_status_addr = `TX_BD_BASE + i * 8;
15063
    bd_ptr_addr = bd_status_addr + 4;
15064
    // initialize BD - status
15065
//    wbm_write(bd_status_addr, 32'h00005800, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // IRQ + PAD + CRC
15066
    wbm_write(bd_status_addr, {len, 1'b0, irq, 1'b0, pad, crc, 11'h0},
15067
              4'hF, 1, wbm_init_waits, wbm_subseq_waits); // IRQ + PAD + CRC
15068
    // initialize BD - pointer
15069
//    wbm_write(bd_ptr_addr, buf_addr, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // Initializing BD-pointer
15070
    wbm_write(bd_ptr_addr, txpnt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // Initializing BD-pointer
15071
  end
15072
end
15073
endtask // set_tx_bd
15074
 
15075
task set_tx_bd_wrap;
15076
  input  [6:0]  tx_bd_num_end;
15077
  integer       bd_status_addr, tmp;
15078
begin
15079
  bd_status_addr = `TX_BD_BASE + tx_bd_num_end * 8;
15080
  wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15081
  // set wrap bit to this BD - this BD should be last-one
15082
  wbm_write(bd_status_addr, (`ETH_TX_BD_WRAP | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15083
end
15084
endtask // set_tx_bd_wrap
15085
 
15086
task set_tx_bd_ready;
15087
  input  [6:0]  tx_nd_num_strat;
15088
  input  [6:0]  tx_bd_num_end;
15089
  integer       i;
15090
  integer       bd_status_addr, tmp;
15091
begin
15092
  for(i = tx_nd_num_strat; i <= tx_bd_num_end; i = i + 1)
15093
  begin
15094
    bd_status_addr = `TX_BD_BASE + i * 8;
15095
    wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15096
    // set empty bit to this BD - this BD should be ready
15097
    wbm_write(bd_status_addr, (`ETH_TX_BD_READY | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15098
  end
15099
end
15100
endtask // set_tx_bd_ready
15101
 
15102
task check_tx_bd;
15103
  input  [6:0]  tx_bd_num_end;
15104
  output [31:0] tx_bd_status;
15105
  integer       bd_status_addr, tmp;
15106
begin
15107
  bd_status_addr = `TX_BD_BASE + tx_bd_num_end * 8;
15108
  wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15109
  #1 tx_bd_status = tmp;
15110
  #1;
15111
end
15112
endtask // check_tx_bd
15113
 
15114
task clear_tx_bd;
15115
  input  [6:0]  tx_nd_num_strat;
15116
  input  [6:0]  tx_bd_num_end;
15117
  integer       i;
15118
  integer       bd_status_addr, bd_ptr_addr;
15119
begin
15120
  for(i = tx_nd_num_strat; i <= tx_bd_num_end; i = i + 1)
15121
  begin
15122
    bd_status_addr = `TX_BD_BASE + i * 8;
15123
    bd_ptr_addr = bd_status_addr + 4;
15124
    // clear BD - status
15125
    wbm_write(bd_status_addr, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15126
    // clear BD - pointer
15127
    wbm_write(bd_ptr_addr, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15128
  end
15129
end
15130
endtask // clear_tx_bd
15131
 
15132
task set_rx_bd;
15133
  input  [6:0]  rx_bd_num_strat;
15134
  input  [6:0]  rx_bd_num_end;
15135
  input         irq;
15136
  input  [31:0] rxpnt;
15137
//  input  [6:0]  rxbd_num;
15138
  integer       i;
15139
  integer       bd_status_addr, bd_ptr_addr;
15140
//  integer       buf_addr;
15141
begin
15142
  for(i = rx_bd_num_strat; i <= rx_bd_num_end; i = i + 1)
15143
  begin
15144
//    buf_addr = `RX_BUF_BASE + i * 32'h600;
15145 209 tadejm
//    bd_status_addr = `RX_BD_BASE + i * 8;
15146
//    bd_ptr_addr = bd_status_addr + 4; 
15147
    bd_status_addr = `TX_BD_BASE + i * 8;
15148
    bd_ptr_addr = bd_status_addr + 4;
15149 116 mohor
 
15150 169 mohor
    // initialize BD - status
15151
//    wbm_write(bd_status_addr, 32'h0000c000, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // IRQ + PAD + CRC
15152
    wbm_write(bd_status_addr, {17'h0, irq, 14'h0},
15153
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15154
    // initialize BD - pointer
15155
//    wbm_write(bd_ptr_addr, buf_addr, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // Initializing BD-pointer
15156
    wbm_write(bd_ptr_addr, rxpnt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // Initializing BD-pointer
15157
  end
15158
end
15159
endtask // set_rx_bd
15160 116 mohor
 
15161 169 mohor
task set_rx_bd_wrap;
15162
  input  [6:0]  rx_bd_num_end;
15163
  integer       bd_status_addr, tmp;
15164
begin
15165 209 tadejm
//  bd_status_addr = `RX_BD_BASE + rx_bd_num_end * 8;
15166
  bd_status_addr = `TX_BD_BASE + rx_bd_num_end * 8;
15167 169 mohor
  wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15168
  // set wrap bit to this BD - this BD should be last-one
15169
  wbm_write(bd_status_addr, (`ETH_RX_BD_WRAP | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15170
end
15171
endtask // set_rx_bd_wrap
15172 116 mohor
 
15173 169 mohor
task set_rx_bd_empty;
15174
  input  [6:0]  rx_bd_num_strat;
15175
  input  [6:0]  rx_bd_num_end;
15176
  integer       i;
15177
  integer       bd_status_addr, tmp;
15178
begin
15179
  for(i = rx_bd_num_strat; i <= rx_bd_num_end; i = i + 1)
15180
  begin
15181 209 tadejm
//    bd_status_addr = `RX_BD_BASE + i * 8;
15182
    bd_status_addr = `TX_BD_BASE + i * 8;
15183 169 mohor
    wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15184
    // set empty bit to this BD - this BD should be ready
15185
    wbm_write(bd_status_addr, (`ETH_RX_BD_EMPTY | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15186 116 mohor
  end
15187 169 mohor
end
15188
endtask // set_rx_bd_empty
15189 116 mohor
 
15190 169 mohor
task check_rx_bd;
15191
  input  [6:0]  rx_bd_num_end;
15192
  output [31:0] rx_bd_status;
15193
  integer       bd_status_addr, tmp;
15194
begin
15195 209 tadejm
//  bd_status_addr = `RX_BD_BASE + rx_bd_num_end * 8;
15196
  bd_status_addr = `TX_BD_BASE + rx_bd_num_end * 8;
15197 169 mohor
  wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15198
  #1 rx_bd_status = tmp;
15199
  #1;
15200
end
15201
endtask // check_rx_bd
15202 116 mohor
 
15203 169 mohor
task clear_rx_bd;
15204
  input  [6:0]  rx_bd_num_strat;
15205
  input  [6:0]  rx_bd_num_end;
15206
  integer       i;
15207
  integer       bd_status_addr, bd_ptr_addr;
15208
begin
15209
  for(i = rx_bd_num_strat; i <= rx_bd_num_end; i = i + 1)
15210
  begin
15211 209 tadejm
//    bd_status_addr = `RX_BD_BASE + i * 8;
15212
    bd_status_addr = `TX_BD_BASE + i * 8;
15213 169 mohor
    bd_ptr_addr = bd_status_addr + 4;
15214
    // clear BD - status
15215
    wbm_write(bd_status_addr, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15216
    // clear BD - pointer
15217
    wbm_write(bd_ptr_addr, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15218
  end
15219
end
15220
endtask // clear_rx_bd
15221 116 mohor
 
15222 169 mohor
task set_tx_packet;
15223
  input  [31:0] txpnt;
15224
  input  [15:0] len;
15225
  input  [7:0]  eth_start_data;
15226
  integer       i, sd;
15227
  integer       buffer;
15228
  reg           delta_t;
15229
begin
15230
  buffer = txpnt;
15231
  sd = eth_start_data;
15232
  delta_t = 0;
15233 116 mohor
 
15234 169 mohor
  // First write might not be word allign.
15235
  if(buffer[1:0] == 1)
15236
  begin
15237
    wb_slave.wr_mem(buffer - 1, {8'h0, sd[7:0], sd[7:0] + 3'h1, sd[7:0] + 3'h2}, 4'h7);
15238
    sd = sd + 3;
15239
    i = 3;
15240
  end
15241
  else if(buffer[1:0] == 2)
15242
  begin
15243
    wb_slave.wr_mem(buffer - 2, {16'h0, sd[7:0], sd[7:0] + 3'h1}, 4'h3);
15244
    sd = sd + 2;
15245
    i = 2;
15246
  end
15247
  else if(buffer[1:0] == 3)
15248
  begin
15249
    wb_slave.wr_mem(buffer - 3, {24'h0, sd[7:0]}, 4'h1);
15250
    sd = sd + 1;
15251
    i = 1;
15252
  end
15253
  else
15254
    i = 0;
15255
  delta_t = !delta_t;
15256 116 mohor
 
15257 169 mohor
  for(i = i; i < (len - 4); i = i + 4) // Last 0-3 bytes are not written
15258
  begin
15259
    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);
15260
    sd = sd + 4;
15261
  end
15262
  delta_t = !delta_t;
15263
 
15264
  // Last word
15265
  if((len - i) == 3)
15266 116 mohor
  begin
15267 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);
15268
  end
15269
  else if((len - i) == 2)
15270
  begin
15271
    wb_slave.wr_mem(buffer + i, {sd[7:0], sd[7:0] + 3'h1, 16'h0}, 4'hC);
15272
  end
15273
  else if((len - i) == 1)
15274
  begin
15275
    wb_slave.wr_mem(buffer + i, {sd[7:0], 24'h0}, 4'h8);
15276
  end
15277
  else if((len - i) == 4)
15278
  begin
15279
    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);
15280
  end
15281
  else
15282
    $display("(%0t)(%m) ERROR", $time);
15283
  delta_t = !delta_t;
15284
end
15285
endtask // set_tx_packet
15286
 
15287
task check_tx_packet;
15288
  input  [31:0] txpnt_wb;  // source
15289
  input  [31:0] txpnt_phy; // destination
15290
  input  [15:0] len;
15291
  output [31:0] failure;
15292
  integer       i, data_wb, data_phy;
15293
  reg    [31:0] addr_wb, addr_phy;
15294
  reg    [31:0] failure;
15295
  reg           delta_t;
15296
begin
15297
  addr_wb = txpnt_wb;
15298
  addr_phy = txpnt_phy;
15299
  delta_t = 0;
15300
  failure = 0;
15301 209 tadejm
  #1;
15302 169 mohor
  // First write might not be word allign.
15303
  if(addr_wb[1:0] == 1)
15304
  begin
15305
    wb_slave.rd_mem(addr_wb - 1, data_wb, 4'h7);
15306
    data_phy[31:24] = 0;
15307
    data_phy[23:16] = eth_phy.tx_mem[addr_phy[21:0]];
15308
    data_phy[15: 8] = eth_phy.tx_mem[addr_phy[21:0] + 1];
15309
    data_phy[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0] + 2];
15310
    i = 3;
15311
    if (data_phy[23:0] !== data_wb[23:0])
15312
    begin
15313
      `TIME;
15314 209 tadejm
      $display("*E Wrong 1. word (3 bytes) of TX packet! phy: %0h, wb: %0h", data_phy[23:0], data_wb[23:0]);
15315
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15316 169 mohor
      failure = 1;
15317
    end
15318
  end
15319
  else if (addr_wb[1:0] == 2)
15320
  begin
15321
    wb_slave.rd_mem(addr_wb - 2, data_wb, 4'h3);
15322
    data_phy[31:16] = 0;
15323
    data_phy[15: 8] = eth_phy.tx_mem[addr_phy[21:0]];
15324
    data_phy[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0] + 1];
15325
    i = 2;
15326
    if (data_phy[15:0] !== data_wb[15:0])
15327
    begin
15328
      `TIME;
15329 209 tadejm
      $display("*E Wrong 1. word (2 bytes) of TX packet! phy: %0h, wb: %0h", data_phy[15:0], data_wb[15:0]);
15330
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15331 169 mohor
      failure = 1;
15332
    end
15333
  end
15334
  else if (addr_wb[1:0] == 3)
15335
  begin
15336
    wb_slave.rd_mem(addr_wb - 3, data_wb, 4'h1);
15337
    data_phy[31: 8] = 0;
15338
    data_phy[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0]];
15339
    i = 1;
15340
    if (data_phy[7:0] !== data_wb[7:0])
15341
    begin
15342
      `TIME;
15343 209 tadejm
      $display("*E Wrong 1. word (1 byte) of TX packet! phy: %0h, wb: %0h", data_phy[7:0], data_wb[7:0]);
15344
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15345 169 mohor
      failure = 1;
15346
    end
15347
  end
15348
  else
15349
    i = 0;
15350
  delta_t = !delta_t;
15351 209 tadejm
  #1;
15352 169 mohor
  for(i = i; i < (len - 4); i = i + 4) // Last 0-3 bytes are not checked
15353
  begin
15354
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hF);
15355
    data_phy[31:24] = eth_phy.tx_mem[addr_phy[21:0] + i];
15356
    data_phy[23:16] = eth_phy.tx_mem[addr_phy[21:0] + i + 1];
15357
    data_phy[15: 8] = eth_phy.tx_mem[addr_phy[21:0] + i + 2];
15358
    data_phy[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0] + i + 3];
15359
    if (data_phy[31:0] !== data_wb[31:0])
15360
    begin
15361
      `TIME;
15362 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]);
15363
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15364 169 mohor
      failure = failure + 1;
15365
    end
15366
  end
15367
  delta_t = !delta_t;
15368 209 tadejm
  #1;
15369 169 mohor
  // Last word
15370
  if((len - i) == 3)
15371
  begin
15372
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hE);
15373
    data_phy[31:24] = eth_phy.tx_mem[addr_phy[21:0] + i];
15374
    data_phy[23:16] = eth_phy.tx_mem[addr_phy[21:0] + i + 1];
15375
    data_phy[15: 8] = eth_phy.tx_mem[addr_phy[21:0] + i + 2];
15376
    data_phy[ 7: 0] = 0;
15377
    if (data_phy[31:8] !== data_wb[31:8])
15378
    begin
15379
      `TIME;
15380 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]);
15381
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15382 169 mohor
      failure = failure + 1;
15383
    end
15384
  end
15385
  else if((len - i) == 2)
15386
  begin
15387
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hC);
15388
    data_phy[31:24] = eth_phy.tx_mem[addr_phy[21:0] + i];
15389
    data_phy[23:16] = eth_phy.tx_mem[addr_phy[21:0] + i + 1];
15390
    data_phy[15: 8] = 0;
15391
    data_phy[ 7: 0] = 0;
15392
    if (data_phy[31:16] !== data_wb[31:16])
15393
    begin
15394
      `TIME;
15395 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]);
15396
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15397 169 mohor
      failure = failure + 1;
15398
    end
15399
  end
15400
  else if((len - i) == 1)
15401
  begin
15402
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'h8);
15403
    data_phy[31:24] = eth_phy.tx_mem[addr_phy[21:0] + i];
15404
    data_phy[23:16] = 0;
15405
    data_phy[15: 8] = 0;
15406
    data_phy[ 7: 0] = 0;
15407
    if (data_phy[31:24] !== data_wb[31:24])
15408
    begin
15409
      `TIME;
15410 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]);
15411
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15412 169 mohor
      failure = failure + 1;
15413
    end
15414
  end
15415
  else if((len - i) == 4)
15416
  begin
15417
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hF);
15418
    data_phy[31:24] = eth_phy.tx_mem[addr_phy[21:0] + i];
15419
    data_phy[23:16] = eth_phy.tx_mem[addr_phy[21:0] + i + 1];
15420
    data_phy[15: 8] = eth_phy.tx_mem[addr_phy[21:0] + i + 2];
15421
    data_phy[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0] + i + 3];
15422
    if (data_phy[31:0] !== data_wb[31:0])
15423
    begin
15424
      `TIME;
15425 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]);
15426
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15427 169 mohor
      failure = failure + 1;
15428
    end
15429
  end
15430
  else
15431
    $display("(%0t)(%m) ERROR", $time);
15432
  delta_t = !delta_t;
15433
end
15434
endtask // check_tx_packet
15435
 
15436
task set_rx_packet;
15437
  input  [31:0] rxpnt;
15438
  input  [15:0] len;
15439
  input         plus_dribble_nibble; // if length is longer for one nibble
15440
  input  [47:0] eth_dest_addr;
15441
  input  [47:0] eth_source_addr;
15442
  input  [15:0] eth_type_len;
15443
  input  [7:0]  eth_start_data;
15444
  integer       i, sd;
15445
  reg    [47:0] dest_addr;
15446
  reg    [47:0] source_addr;
15447
  reg    [15:0] type_len;
15448
  reg    [21:0] buffer;
15449
  reg           delta_t;
15450
begin
15451
  buffer = rxpnt[21:0];
15452
  dest_addr = eth_dest_addr;
15453
  source_addr = eth_source_addr;
15454
  type_len = eth_type_len;
15455
  sd = eth_start_data;
15456
  delta_t = 0;
15457
  for(i = 0; i < len; i = i + 1)
15458
  begin
15459
    if (i < 6)
15460
    begin
15461
      eth_phy.rx_mem[buffer] = dest_addr[47:40];
15462
      dest_addr = dest_addr << 8;
15463
    end
15464
    else if (i < 12)
15465
    begin
15466
      eth_phy.rx_mem[buffer] = source_addr[47:40];
15467
      source_addr = source_addr << 8;
15468
    end
15469
    else if (i < 14)
15470
    begin
15471
      eth_phy.rx_mem[buffer] = type_len[15:8];
15472
      type_len = type_len << 8;
15473
    end
15474
    else
15475
    begin
15476
      eth_phy.rx_mem[buffer] = sd[7:0];
15477
      sd = sd + 1;
15478
    end
15479
    buffer = buffer + 1;
15480
  end
15481
  delta_t = !delta_t;
15482
  if (plus_dribble_nibble)
15483
    eth_phy.rx_mem[buffer] = {4'h0, 4'hD /*sd[3:0]*/};
15484
  delta_t = !delta_t;
15485
end
15486
endtask // set_rx_packet
15487
 
15488 243 tadejm
task set_rx_addr_type;
15489
  input  [31:0] rxpnt;
15490
  input  [47:0] eth_dest_addr;
15491
  input  [47:0] eth_source_addr;
15492
  input  [15:0] eth_type_len;
15493
  integer       i;
15494
  reg    [47:0] dest_addr;
15495
  reg    [47:0] source_addr;
15496
  reg    [15:0] type_len;
15497
  reg    [21:0] buffer;
15498
  reg           delta_t;
15499
begin
15500
  buffer = rxpnt[21:0];
15501
  dest_addr = eth_dest_addr;
15502
  source_addr = eth_source_addr;
15503
  type_len = eth_type_len;
15504
  delta_t = 0;
15505
  for(i = 0; i < 14; i = i + 1)
15506
  begin
15507
    if (i < 6)
15508
    begin
15509
      eth_phy.rx_mem[buffer] = dest_addr[47:40];
15510
      dest_addr = dest_addr << 8;
15511
    end
15512
    else if (i < 12)
15513
    begin
15514
      eth_phy.rx_mem[buffer] = source_addr[47:40];
15515
      source_addr = source_addr << 8;
15516
    end
15517
    else // if (i < 14)
15518
    begin
15519
      eth_phy.rx_mem[buffer] = type_len[15:8];
15520
      type_len = type_len << 8;
15521
    end
15522
    buffer = buffer + 1;
15523
  end
15524
  delta_t = !delta_t;
15525
end
15526
endtask // set_rx_addr_type
15527
 
15528 169 mohor
task check_rx_packet;
15529
  input  [31:0] rxpnt_phy; // source
15530
  input  [31:0] rxpnt_wb;  // destination
15531
  input  [15:0] len;
15532
  input         plus_dribble_nibble; // if length is longer for one nibble
15533
  input         successful_dribble_nibble; // if additional nibble is stored into memory
15534
  output [31:0] failure;
15535
  integer       i, data_wb, data_phy;
15536
  reg    [31:0] addr_wb, addr_phy;
15537
  reg    [31:0] failure;
15538
  reg    [21:0] buffer;
15539
  reg           delta_t;
15540
begin
15541
  addr_phy = rxpnt_phy;
15542
  addr_wb = rxpnt_wb;
15543
  delta_t = 0;
15544
  failure = 0;
15545
 
15546
  // First write might not be word allign.
15547
  if(addr_wb[1:0] == 1)
15548
  begin
15549
    wb_slave.rd_mem(addr_wb - 1, data_wb, 4'h7);
15550
    data_phy[31:24] = 0;
15551
    data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0]];
15552
    data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0] + 1];
15553
    data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0] + 2];
15554
    i = 3;
15555
    if (data_phy[23:0] !== data_wb[23:0])
15556
    begin
15557
      `TIME;
15558 243 tadejm
      $display("   addr_phy = %h, addr_wb = %h", rxpnt_phy, rxpnt_wb);
15559
      $display("*E Wrong 1. word (3 bytes) of RX packet! phy = %h, wb = %h", data_phy[23:0], data_wb[23:0]);
15560 169 mohor
      failure = 1;
15561
    end
15562
  end
15563
  else if (addr_wb[1:0] == 2)
15564
  begin
15565
    wb_slave.rd_mem(addr_wb - 2, data_wb, 4'h3);
15566
    data_phy[31:16] = 0;
15567
    data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0]];
15568
    data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0] + 1];
15569
    i = 2;
15570
    if (data_phy[15:0] !== data_wb[15:0])
15571
    begin
15572
      `TIME;
15573 243 tadejm
      $display("   addr_phy = %h, addr_wb = %h", rxpnt_phy, rxpnt_wb);
15574
      $display("*E Wrong 1. word (2 bytes) of RX packet! phy = %h, wb = %h", data_phy[15:0], data_wb[15:0]);
15575 169 mohor
      failure = 1;
15576
    end
15577
  end
15578
  else if (addr_wb[1:0] == 3)
15579
  begin
15580
    wb_slave.rd_mem(addr_wb - 3, data_wb, 4'h1);
15581
    data_phy[31: 8] = 0;
15582
    data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0]];
15583
    i = 1;
15584
    if (data_phy[7:0] !== data_wb[7:0])
15585
    begin
15586
      `TIME;
15587 243 tadejm
      $display("   addr_phy = %h, addr_wb = %h", rxpnt_phy, rxpnt_wb);
15588
      $display("*E Wrong 1. word (1 byte) of RX packet! phy = %h, wb = %h", data_phy[7:0], data_wb[7:0]);
15589 169 mohor
      failure = 1;
15590
    end
15591
  end
15592
  else
15593
    i = 0;
15594
  delta_t = !delta_t;
15595
 
15596
  for(i = i; i < (len - 4); i = i + 4) // Last 0-3 bytes are not checked
15597
  begin
15598
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hF);
15599
    data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i];
15600
    data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0] + i + 1];
15601
    data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0] + i + 2];
15602
    data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0] + i + 3];
15603
    if (data_phy[31:0] !== data_wb[31:0])
15604
    begin
15605
      `TIME;
15606 243 tadejm
      if (i == 0)
15607
        $display("   addr_phy = %h, addr_wb = %h", rxpnt_phy, rxpnt_wb);
15608
      $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]);
15609 169 mohor
      failure = failure + 1;
15610
    end
15611
  end
15612
  delta_t = !delta_t;
15613
 
15614
  // Last word
15615
  if((len - i) == 3)
15616
  begin
15617
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hF);
15618
    data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i];
15619
    data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0] + i + 1];
15620
    data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0] + i + 2];
15621
    if (plus_dribble_nibble)
15622
      data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0] + i + 3];
15623
    else
15624
      data_phy[ 7: 0] = 0;
15625
    if (data_phy[31:8] !== data_wb[31:8])
15626
    begin
15627
      `TIME;
15628 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]);
15629 169 mohor
      failure = failure + 1;
15630
    end
15631
    if (plus_dribble_nibble && successful_dribble_nibble)
15632
    begin
15633
      if (data_phy[3:0] !== data_wb[3:0])
15634 116 mohor
      begin
15635 169 mohor
        `TIME;
15636 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (3 bytes) of RX packet!", ((i/4)+1));
15637 169 mohor
        failure = failure + 1;
15638 116 mohor
      end
15639 169 mohor
    end
15640
    else if (plus_dribble_nibble && !successful_dribble_nibble)
15641
    begin
15642
      if (data_phy[3:0] === data_wb[3:0])
15643 116 mohor
      begin
15644 169 mohor
        `TIME;
15645 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (3 bytes) of RX packet!", ((i/4)+1));
15646 169 mohor
        failure = failure + 1;
15647 116 mohor
      end
15648 169 mohor
    end
15649
  end
15650
  else if((len - i) == 2)
15651
  begin
15652
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hE);
15653
    data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i];
15654
    data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0] + i + 1];
15655
    if (plus_dribble_nibble)
15656
      data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0] + i + 2];
15657
    else
15658
      data_phy[15: 8] = 0;
15659
    data_phy[ 7: 0] = 0;
15660
    if (data_phy[31:16] !== data_wb[31:16])
15661
    begin
15662
      `TIME;
15663 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]);
15664 169 mohor
      failure = failure + 1;
15665
    end
15666
    if (plus_dribble_nibble && successful_dribble_nibble)
15667
    begin
15668
      if (data_phy[11:8] !== data_wb[11:8])
15669
      begin
15670
        `TIME;
15671 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (2 bytes) of RX packet!", ((i/4)+1));
15672 169 mohor
        failure = failure + 1;
15673
      end
15674
    end
15675
    else if (plus_dribble_nibble && !successful_dribble_nibble)
15676
    begin
15677
      if (data_phy[11:8] === data_wb[11:8])
15678
      begin
15679
        `TIME;
15680 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (2 bytes) of RX packet!", ((i/4)+1));
15681 169 mohor
        failure = failure + 1;
15682
      end
15683
    end
15684
  end
15685
  else if((len - i) == 1)
15686
  begin
15687
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hC);
15688
    data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i];
15689
    if (plus_dribble_nibble)
15690
      data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0] + i + 1];
15691
    else
15692
      data_phy[23:16] = 0;
15693
    data_phy[15: 8] = 0;
15694
    data_phy[ 7: 0] = 0;
15695
    if (data_phy[31:24] !== data_wb[31:24])
15696
    begin
15697
      `TIME;
15698 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]);
15699 169 mohor
      failure = failure + 1;
15700
    end
15701
    if (plus_dribble_nibble && successful_dribble_nibble)
15702
    begin
15703
      if (data_phy[19:16] !== data_wb[19:16])
15704
      begin
15705
        `TIME;
15706 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (1 byte) of RX packet!", ((i/4)+1));
15707 169 mohor
        failure = failure + 1;
15708
      end
15709
    end
15710
    else if (plus_dribble_nibble && !successful_dribble_nibble)
15711
    begin
15712
      if (data_phy[19:16] === data_wb[19:16])
15713
      begin
15714
        `TIME;
15715 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (1 byte) of RX packet!", ((i/4)+1));
15716 169 mohor
        failure = failure + 1;
15717
      end
15718
    end
15719
  end
15720
  else if((len - i) == 4)
15721
  begin
15722
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hF);
15723
    data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i];
15724
    data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0] + i + 1];
15725
    data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0] + i + 2];
15726
    data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0] + i + 3];
15727
    if (data_phy[31:0] !== data_wb[31:0])
15728
    begin
15729
      `TIME;
15730 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]);
15731 169 mohor
      failure = failure + 1;
15732
    end
15733
    if (plus_dribble_nibble)
15734
    begin
15735
      wb_slave.rd_mem(addr_wb + i + 4, data_wb, 4'h8);
15736
      data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i + 4];
15737
      if (successful_dribble_nibble)
15738
      begin
15739
        if (data_phy[27:24] !== data_wb[27:24])
15740
        begin
15741
          `TIME;
15742 243 tadejm
          $display("*E Wrong dribble nibble in %0d. word (0 bytes) of RX packet!", ((i/4)+2));
15743 169 mohor
          failure = failure + 1;
15744
        end
15745
      end
15746
      else
15747
      begin
15748
        if (data_phy[27:24] === data_wb[27:24])
15749
        begin
15750
          `TIME;
15751 243 tadejm
          $display("*E Wrong dribble nibble in %0d. word (0 bytes) of RX packet!", ((i/4)+2));
15752 169 mohor
          failure = failure + 1;
15753
        end
15754
      end
15755
    end
15756
  end
15757
  else
15758
    $display("(%0t)(%m) ERROR", $time);
15759
  delta_t = !delta_t;
15760
end
15761
endtask // check_rx_packet
15762 116 mohor
 
15763 169 mohor
//////////////////////////////////////////////////////////////
15764
// Ethernet CRC Basic tasks
15765
//////////////////////////////////////////////////////////////
15766
 
15767
task append_tx_crc;
15768
  input  [31:0] txpnt_wb;  // source
15769
  input  [15:0] len; // length in bytes without CRC
15770
  input         negated_crc; // if appended CRC is correct or not
15771
  reg    [31:0] crc;
15772
  reg    [31:0] addr_wb;
15773
  reg           delta_t;
15774
begin
15775 243 tadejm
  addr_wb = txpnt_wb + {16'h0, len};
15776 169 mohor
  delta_t = 0;
15777
  // calculate CRC from prepared packet
15778
  paralel_crc_mac(txpnt_wb, {16'h0, len}, 1'b0, crc);
15779
  if (negated_crc)
15780
    crc = ~crc;
15781
  delta_t = !delta_t;
15782
 
15783
  // Write might not be word allign.
15784
  if (addr_wb[1:0] == 1)
15785
  begin
15786
    wb_slave.wr_mem(addr_wb - 1, {8'h0, crc[7:0], crc[15:8], crc[23:16]}, 4'h7);
15787
    wb_slave.wr_mem(addr_wb + 3, {crc[31:24], 24'h0}, 4'h8);
15788 116 mohor
  end
15789 169 mohor
  else if (addr_wb[1:0] == 2)
15790
  begin
15791
    wb_slave.wr_mem(addr_wb - 2, {16'h0, crc[7:0], crc[15:8]}, 4'h3);
15792
    wb_slave.wr_mem(addr_wb + 2, {crc[23:16], crc[31:24], 16'h0}, 4'hC);
15793
  end
15794
  else if (addr_wb[1:0] == 3)
15795
  begin
15796
    wb_slave.wr_mem(addr_wb - 3, {24'h0, crc[7:0]}, 4'h1);
15797
    wb_slave.wr_mem(addr_wb + 1, {crc[15:8], crc[23:16], crc[31:24], 8'h0}, 4'hE);
15798
  end
15799
  else
15800
  begin
15801
    wb_slave.wr_mem(addr_wb, {crc[7:0], crc[15:8], crc[23:16], crc[31:24]}, 4'hF);
15802
  end
15803
  delta_t = !delta_t;
15804
end
15805
endtask // append_tx_crc
15806 116 mohor
 
15807 169 mohor
task check_tx_crc; // used to check crc added to TX packets by MAC
15808
  input  [31:0] txpnt_phy; // destination
15809
  input  [15:0] len; // length in bytes without CRC
15810
  input         negated_crc; // if appended CRC is correct or not
15811
  output [31:0] failure;
15812
  reg    [31:0] failure;
15813
  reg    [31:0] crc_calc;
15814
  reg    [31:0] crc;
15815
  reg    [31:0] addr_phy;
15816
  reg           delta_t;
15817
begin
15818
  addr_phy = txpnt_phy;
15819
  failure = 0;
15820
  // calculate CRC from sent packet
15821
//  serial_crc_phy_tx(addr_phy, {16'h0, len}, 1'b0, crc_calc);
15822
//#10;
15823
  paralel_crc_phy_tx(addr_phy, {16'h0, len}, 1'b0, crc_calc);
15824 209 tadejm
  #1;
15825 169 mohor
  addr_phy = addr_phy + len;
15826
  // Read CRC - BIG endian
15827
  crc[31:24] = eth_phy.tx_mem[addr_phy[21:0]];
15828
  crc[23:16] = eth_phy.tx_mem[addr_phy[21:0] + 1];
15829
  crc[15: 8] = eth_phy.tx_mem[addr_phy[21:0] + 2];
15830
  crc[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0] + 3];
15831
 
15832
  delta_t = !delta_t;
15833
  if (negated_crc)
15834
  begin
15835
    if ((~crc_calc) !== crc)
15836
    begin
15837
      `TIME;
15838
      $display("*E Negated CRC was not successfuly transmitted!");
15839
      failure = failure + 1;
15840
    end
15841
  end
15842
  else
15843
  begin
15844
    if (crc_calc !== crc)
15845
    begin
15846
      `TIME;
15847 209 tadejm
      $display("*E Transmitted CRC was not correct; crc_calc: %0h, crc_mem: %0h", crc_calc, crc);
15848 169 mohor
      failure = failure + 1;
15849
    end
15850
  end
15851
  delta_t = !delta_t;
15852
end
15853
endtask // check_tx_crc
15854
 
15855
task append_rx_crc;
15856
  input  [31:0] rxpnt_phy; // source
15857
  input  [15:0] len; // length in bytes without CRC
15858
  input         plus_dribble_nibble; // if length is longer for one nibble
15859
  input         negated_crc; // if appended CRC is correct or not
15860
  reg    [31:0] crc;
15861
  reg    [7:0]  tmp;
15862
  reg    [31:0] addr_phy;
15863
  reg           delta_t;
15864
begin
15865
  addr_phy = rxpnt_phy + len;
15866
  delta_t = 0;
15867
  // calculate CRC from prepared packet
15868
  paralel_crc_phy_rx(rxpnt_phy, {16'h0, len}, plus_dribble_nibble, crc);
15869
  if (negated_crc)
15870
    crc = ~crc;
15871
  delta_t = !delta_t;
15872
 
15873
  if (plus_dribble_nibble)
15874
  begin
15875
    tmp = eth_phy.rx_mem[addr_phy];
15876 209 tadejm
    eth_phy.rx_mem[addr_phy]     = {crc[27:24], tmp[3:0]};
15877
    eth_phy.rx_mem[addr_phy + 1] = {crc[19:16], crc[31:28]};
15878
    eth_phy.rx_mem[addr_phy + 2] = {crc[11:8], crc[23:20]};
15879
    eth_phy.rx_mem[addr_phy + 3] = {crc[3:0], crc[15:12]};
15880
    eth_phy.rx_mem[addr_phy + 4] = {4'h0, crc[7:4]};
15881 169 mohor
  end
15882
  else
15883
  begin
15884 209 tadejm
    eth_phy.rx_mem[addr_phy]     = crc[31:24];
15885
    eth_phy.rx_mem[addr_phy + 1] = crc[23:16];
15886
    eth_phy.rx_mem[addr_phy + 2] = crc[15:8];
15887
    eth_phy.rx_mem[addr_phy + 3] = crc[7:0];
15888 169 mohor
  end
15889
end
15890
endtask // append_rx_crc
15891
 
15892
// paralel CRC checking for PHY TX
15893
task paralel_crc_phy_tx;
15894
  input  [31:0] start_addr; // start address
15895
  input  [31:0] len; // length of frame in Bytes without CRC length
15896
  input         plus_dribble_nibble; // if length is longer for one nibble
15897
  output [31:0] crc_out;
15898
  reg    [21:0] addr_cnt; // only 22 address lines
15899
  integer       word_cnt;
15900
  integer       nibble_cnt;
15901
  reg    [31:0] load_reg;
15902
  reg           delta_t;
15903
  reg    [31:0] crc_next;
15904
  reg    [31:0] crc;
15905
  reg           crc_error;
15906
  reg     [3:0] data_in;
15907
  integer       i;
15908
begin
15909
  #1 addr_cnt = start_addr[21:0];
15910
  word_cnt = 24; // 27; // start of the frame - nibble granularity (MSbit first)
15911
  crc = 32'hFFFF_FFFF; // INITIAL value
15912
  delta_t = 0;
15913
  // length must include 4 bytes of ZEROs, to generate CRC
15914
  // get number of nibbles from Byte length (2^1 = 2)
15915
  if (plus_dribble_nibble)
15916
    nibble_cnt = ((len + 4) << 1) + 1'b1; // one nibble longer
15917
  else
15918
    nibble_cnt = ((len + 4) << 1);
15919
  // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
15920
  load_reg[31:24] = eth_phy.tx_mem[addr_cnt];
15921
  addr_cnt = addr_cnt + 1;
15922
  load_reg[23:16] = eth_phy.tx_mem[addr_cnt];
15923
  addr_cnt = addr_cnt + 1;
15924
  load_reg[15: 8] = eth_phy.tx_mem[addr_cnt];
15925
  addr_cnt = addr_cnt + 1;
15926
  load_reg[ 7: 0] = eth_phy.tx_mem[addr_cnt];
15927
  addr_cnt = addr_cnt + 1;
15928
  while (nibble_cnt > 0)
15929
  begin
15930
    // wait for delta time
15931
    delta_t = !delta_t;
15932
    // shift data in
15933
 
15934
    if(nibble_cnt <= 8) // for additional 8 nibbles shift ZEROs in!
15935
      data_in[3:0] = 4'h0;
15936
    else
15937
 
15938
      data_in[3:0] = {load_reg[word_cnt], load_reg[word_cnt+1], load_reg[word_cnt+2], load_reg[word_cnt+3]};
15939
    crc_next[0]  = (data_in[0] ^ crc[28]);
15940
    crc_next[1]  = (data_in[1] ^ data_in[0] ^ crc[28]    ^ crc[29]);
15941
    crc_next[2]  = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]);
15942
    crc_next[3]  = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]);
15943
    crc_next[4]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[0];
15944
    crc_next[5]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[1];
15945
    crc_next[6]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[ 2];
15946
    crc_next[7]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[3];
15947
    crc_next[8]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[4];
15948
    crc_next[9]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[5];
15949
    crc_next[10] = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[6];
15950
    crc_next[11] = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[7];
15951
    crc_next[12] = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]) ^ crc[8];
15952
    crc_next[13] = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]) ^ crc[9];
15953
    crc_next[14] = (data_in[3] ^ data_in[2] ^ crc[30]    ^ crc[31]) ^ crc[10];
15954
    crc_next[15] = (data_in[3] ^ crc[31])   ^ crc[11];
15955
    crc_next[16] = (data_in[0] ^ crc[28])   ^ crc[12];
15956
    crc_next[17] = (data_in[1] ^ crc[29])   ^ crc[13];
15957
    crc_next[18] = (data_in[2] ^ crc[30])   ^ crc[14];
15958
    crc_next[19] = (data_in[3] ^ crc[31])   ^ crc[15];
15959
    crc_next[20] =  crc[16];
15960
    crc_next[21] =  crc[17];
15961
    crc_next[22] = (data_in[0] ^ crc[28])   ^ crc[18];
15962
    crc_next[23] = (data_in[1] ^ data_in[0] ^ crc[29]    ^ crc[28]) ^ crc[19];
15963
    crc_next[24] = (data_in[2] ^ data_in[1] ^ crc[30]    ^ crc[29]) ^ crc[20];
15964
    crc_next[25] = (data_in[3] ^ data_in[2] ^ crc[31]    ^ crc[30]) ^ crc[21];
15965
    crc_next[26] = (data_in[3] ^ data_in[0] ^ crc[31]    ^ crc[28]) ^ crc[22];
15966
    crc_next[27] = (data_in[1] ^ crc[29])   ^ crc[23];
15967
    crc_next[28] = (data_in[2] ^ crc[30])   ^ crc[24];
15968
    crc_next[29] = (data_in[3] ^ crc[31])   ^ crc[25];
15969
    crc_next[30] =  crc[26];
15970
    crc_next[31] =  crc[27];
15971
 
15972
    crc = crc_next;
15973
    crc_error = crc[31:0] != 32'hc704dd7b;  // CRC not equal to magic number
15974
    case (nibble_cnt)
15975
    9: crc_out = {!crc[24], !crc[25], !crc[26], !crc[27], !crc[28], !crc[29], !crc[30], !crc[31],
15976
                  !crc[16], !crc[17], !crc[18], !crc[19], !crc[20], !crc[21], !crc[22], !crc[23],
15977
                  !crc[ 8], !crc[ 9], !crc[10], !crc[11], !crc[12], !crc[13], !crc[14], !crc[15],
15978
                  !crc[ 0], !crc[ 1], !crc[ 2], !crc[ 3], !crc[ 4], !crc[ 5], !crc[ 6], !crc[ 7]};
15979
    default: crc_out = crc_out;
15980
    endcase
15981
    // wait for delta time
15982
    delta_t = !delta_t;
15983
    // increment address and load new data
15984
    if ((word_cnt+3) == 7)//4)
15985
    begin
15986
      // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
15987
      load_reg[31:24] = eth_phy.tx_mem[addr_cnt];
15988
      addr_cnt = addr_cnt + 1;
15989
      load_reg[23:16] = eth_phy.tx_mem[addr_cnt];
15990
      addr_cnt = addr_cnt + 1;
15991
      load_reg[15: 8] = eth_phy.tx_mem[addr_cnt];
15992
      addr_cnt = addr_cnt + 1;
15993
      load_reg[ 7: 0] = eth_phy.tx_mem[addr_cnt];
15994
      addr_cnt = addr_cnt + 1;
15995
    end
15996
    // set new load bit position
15997
    if((word_cnt+3) == 31)
15998
      word_cnt = 16;
15999
    else if ((word_cnt+3) == 23)
16000
      word_cnt = 8;
16001
    else if ((word_cnt+3) == 15)
16002
      word_cnt = 0;
16003
    else if ((word_cnt+3) == 7)
16004
      word_cnt = 24;
16005
    else
16006
      word_cnt = word_cnt + 4;// - 4;
16007
    // decrement nibble counter
16008
    nibble_cnt = nibble_cnt - 1;
16009
    // wait for delta time
16010
    delta_t = !delta_t;
16011
  end // while
16012
  #1;
16013
end
16014
endtask // paralel_crc_phy_tx
16015
 
16016
// paralel CRC calculating for PHY RX
16017
task paralel_crc_phy_rx;
16018
  input  [31:0] start_addr; // start address
16019
  input  [31:0] len; // length of frame in Bytes without CRC length
16020
  input         plus_dribble_nibble; // if length is longer for one nibble
16021 209 tadejm
  output [31:0] crc_out;
16022 169 mohor
  reg    [21:0] addr_cnt; // only 22 address lines
16023
  integer       word_cnt;
16024 209 tadejm
  integer       nibble_cnt;
16025 169 mohor
  reg    [31:0] load_reg;
16026
  reg           delta_t;
16027 209 tadejm
  reg    [31:0] crc_next;
16028
  reg    [31:0] crc;
16029
  reg           crc_error;
16030
  reg     [3:0] data_in;
16031
  integer       i;
16032 169 mohor
begin
16033
  #1 addr_cnt = start_addr[21:0];
16034 209 tadejm
  word_cnt = 24; // 27; // start of the frame - nibble granularity (MSbit first)
16035
  crc = 32'hFFFF_FFFF; // INITIAL value
16036 169 mohor
  delta_t = 0;
16037
  // length must include 4 bytes of ZEROs, to generate CRC
16038 209 tadejm
  // get number of nibbles from Byte length (2^1 = 2)
16039 169 mohor
  if (plus_dribble_nibble)
16040 209 tadejm
    nibble_cnt = ((len + 4) << 1) + 1'b1; // one nibble longer
16041 169 mohor
  else
16042 209 tadejm
    nibble_cnt = ((len + 4) << 1);
16043
  // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16044 169 mohor
  load_reg[31:24] = eth_phy.rx_mem[addr_cnt];
16045
  addr_cnt = addr_cnt + 1;
16046
  load_reg[23:16] = eth_phy.rx_mem[addr_cnt];
16047
  addr_cnt = addr_cnt + 1;
16048 209 tadejm
  load_reg[15: 8] = eth_phy.rx_mem[addr_cnt];
16049 169 mohor
  addr_cnt = addr_cnt + 1;
16050 209 tadejm
  load_reg[ 7: 0] = eth_phy.rx_mem[addr_cnt];
16051
  addr_cnt = addr_cnt + 1;
16052
  while (nibble_cnt > 0)
16053 169 mohor
  begin
16054
    // wait for delta time
16055
    delta_t = !delta_t;
16056
    // shift data in
16057 209 tadejm
 
16058
    if(nibble_cnt <= 8) // for additional 8 nibbles shift ZEROs in!
16059
      data_in[3:0] = 4'h0;
16060 169 mohor
    else
16061 209 tadejm
 
16062
      data_in[3:0] = {load_reg[word_cnt], load_reg[word_cnt+1], load_reg[word_cnt+2], load_reg[word_cnt+3]};
16063
    crc_next[0]  = (data_in[0] ^ crc[28]);
16064
    crc_next[1]  = (data_in[1] ^ data_in[0] ^ crc[28]    ^ crc[29]);
16065
    crc_next[2]  = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]);
16066
    crc_next[3]  = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]);
16067
    crc_next[4]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[0];
16068
    crc_next[5]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[1];
16069
    crc_next[6]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[ 2];
16070
    crc_next[7]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[3];
16071
    crc_next[8]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[4];
16072
    crc_next[9]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[5];
16073
    crc_next[10] = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[6];
16074
    crc_next[11] = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[7];
16075
    crc_next[12] = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]) ^ crc[8];
16076
    crc_next[13] = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]) ^ crc[9];
16077
    crc_next[14] = (data_in[3] ^ data_in[2] ^ crc[30]    ^ crc[31]) ^ crc[10];
16078
    crc_next[15] = (data_in[3] ^ crc[31])   ^ crc[11];
16079
    crc_next[16] = (data_in[0] ^ crc[28])   ^ crc[12];
16080
    crc_next[17] = (data_in[1] ^ crc[29])   ^ crc[13];
16081
    crc_next[18] = (data_in[2] ^ crc[30])   ^ crc[14];
16082
    crc_next[19] = (data_in[3] ^ crc[31])   ^ crc[15];
16083
    crc_next[20] =  crc[16];
16084
    crc_next[21] =  crc[17];
16085
    crc_next[22] = (data_in[0] ^ crc[28])   ^ crc[18];
16086
    crc_next[23] = (data_in[1] ^ data_in[0] ^ crc[29]    ^ crc[28]) ^ crc[19];
16087
    crc_next[24] = (data_in[2] ^ data_in[1] ^ crc[30]    ^ crc[29]) ^ crc[20];
16088
    crc_next[25] = (data_in[3] ^ data_in[2] ^ crc[31]    ^ crc[30]) ^ crc[21];
16089
    crc_next[26] = (data_in[3] ^ data_in[0] ^ crc[31]    ^ crc[28]) ^ crc[22];
16090
    crc_next[27] = (data_in[1] ^ crc[29])   ^ crc[23];
16091
    crc_next[28] = (data_in[2] ^ crc[30])   ^ crc[24];
16092
    crc_next[29] = (data_in[3] ^ crc[31])   ^ crc[25];
16093
    crc_next[30] =  crc[26];
16094
    crc_next[31] =  crc[27];
16095
 
16096
    crc = crc_next;
16097
    crc_error = crc[31:0] != 32'hc704dd7b;  // CRC not equal to magic number
16098
    case (nibble_cnt)
16099
    9: crc_out = {!crc[24], !crc[25], !crc[26], !crc[27], !crc[28], !crc[29], !crc[30], !crc[31],
16100
                  !crc[16], !crc[17], !crc[18], !crc[19], !crc[20], !crc[21], !crc[22], !crc[23],
16101
                  !crc[ 8], !crc[ 9], !crc[10], !crc[11], !crc[12], !crc[13], !crc[14], !crc[15],
16102
                  !crc[ 0], !crc[ 1], !crc[ 2], !crc[ 3], !crc[ 4], !crc[ 5], !crc[ 6], !crc[ 7]};
16103
    default: crc_out = crc_out;
16104
    endcase
16105 169 mohor
    // wait for delta time
16106
    delta_t = !delta_t;
16107
    // increment address and load new data
16108 209 tadejm
    if ((word_cnt+3) == 7)//4)
16109 169 mohor
    begin
16110 209 tadejm
      // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16111 169 mohor
      load_reg[31:24] = eth_phy.rx_mem[addr_cnt];
16112
      addr_cnt = addr_cnt + 1;
16113
      load_reg[23:16] = eth_phy.rx_mem[addr_cnt];
16114
      addr_cnt = addr_cnt + 1;
16115 209 tadejm
      load_reg[15: 8] = eth_phy.rx_mem[addr_cnt];
16116 169 mohor
      addr_cnt = addr_cnt + 1;
16117 209 tadejm
      load_reg[ 7: 0] = eth_phy.rx_mem[addr_cnt];
16118
      addr_cnt = addr_cnt + 1;
16119 169 mohor
    end
16120
    // set new load bit position
16121 209 tadejm
    if((word_cnt+3) == 31)
16122 169 mohor
      word_cnt = 16;
16123 209 tadejm
    else if ((word_cnt+3) == 23)
16124 169 mohor
      word_cnt = 8;
16125 209 tadejm
    else if ((word_cnt+3) == 15)
16126 169 mohor
      word_cnt = 0;
16127 209 tadejm
    else if ((word_cnt+3) == 7)
16128 169 mohor
      word_cnt = 24;
16129
    else
16130 209 tadejm
      word_cnt = word_cnt + 4;// - 4;
16131
    // decrement nibble counter
16132
    nibble_cnt = nibble_cnt - 1;
16133 169 mohor
    // wait for delta time
16134
    delta_t = !delta_t;
16135
  end // while
16136
  #1;
16137
end
16138
endtask // paralel_crc_phy_rx
16139
 
16140
// paralel CRC checking for MAC
16141
task paralel_crc_mac;
16142
  input  [31:0] start_addr; // start address
16143
  input  [31:0] len; // length of frame in Bytes without CRC length
16144
  input         plus_dribble_nibble; // if length is longer for one nibble
16145 209 tadejm
  output [31:0] crc_out;
16146
 
16147
  reg    [21:0] addr_cnt; // only 22 address lines
16148 169 mohor
  integer       word_cnt;
16149 209 tadejm
  integer       nibble_cnt;
16150 169 mohor
  reg    [31:0] load_reg;
16151
  reg           delta_t;
16152 209 tadejm
  reg    [31:0] crc_next;
16153
  reg    [31:0] crc;
16154
  reg           crc_error;
16155
  reg     [3:0] data_in;
16156
  integer       i;
16157 169 mohor
begin
16158
  #1 addr_cnt = start_addr[19:0];
16159
  // set starting point depending with which byte frame starts (e.g. if addr_cnt[1:0] == 0, then
16160
  //   MSB of the packet must be written to the LSB of Big ENDIAN Word [31:24])
16161
  if (addr_cnt[1:0] == 2'h1)
16162
    word_cnt = 16; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16163
  else if (addr_cnt[1:0] == 2'h2)
16164
    word_cnt = 8; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16165
  else if (addr_cnt[1:0] == 2'h3)
16166
    word_cnt = 0; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16167
  else
16168
    word_cnt = 24; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16169 209 tadejm
  crc = 32'hFFFF_FFFF; // INITIAL value
16170 169 mohor
  delta_t = 0;
16171
  // length must include 4 bytes of ZEROs, to generate CRC
16172 209 tadejm
  // get number of nibbles from Byte length (2^1 = 2)
16173 169 mohor
  if (plus_dribble_nibble)
16174 209 tadejm
    nibble_cnt = ((len + 4) << 1) + 1'b1; // one nibble longer
16175 169 mohor
  else
16176 209 tadejm
    nibble_cnt = ((len + 4) << 1);
16177 169 mohor
  load_reg = wb_slave.wb_memory[{12'h0, addr_cnt}];
16178 209 tadejm
  addr_cnt = addr_cnt + 4;
16179
  while (nibble_cnt > 0)
16180 169 mohor
  begin
16181
    // wait for delta time
16182
    delta_t = !delta_t;
16183
    // shift data in
16184 209 tadejm
 
16185
    if(nibble_cnt <= 8) // for additional 8 nibbles shift ZEROs in!
16186
      data_in[3:0] = 4'h0;
16187 169 mohor
    else
16188 209 tadejm
 
16189
      data_in[3:0] = {load_reg[word_cnt], load_reg[word_cnt+1], load_reg[word_cnt+2], load_reg[word_cnt+3]};
16190
    crc_next[0]  = (data_in[0] ^ crc[28]);
16191
    crc_next[1]  = (data_in[1] ^ data_in[0] ^ crc[28]    ^ crc[29]);
16192
    crc_next[2]  = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]);
16193
    crc_next[3]  = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]);
16194
    crc_next[4]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[0];
16195
    crc_next[5]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[1];
16196
    crc_next[6]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[ 2];
16197
    crc_next[7]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[3];
16198
    crc_next[8]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[4];
16199
    crc_next[9]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[5];
16200
    crc_next[10] = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[6];
16201
    crc_next[11] = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[7];
16202
    crc_next[12] = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]) ^ crc[8];
16203
    crc_next[13] = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]) ^ crc[9];
16204
    crc_next[14] = (data_in[3] ^ data_in[2] ^ crc[30]    ^ crc[31]) ^ crc[10];
16205
    crc_next[15] = (data_in[3] ^ crc[31])   ^ crc[11];
16206
    crc_next[16] = (data_in[0] ^ crc[28])   ^ crc[12];
16207
    crc_next[17] = (data_in[1] ^ crc[29])   ^ crc[13];
16208
    crc_next[18] = (data_in[2] ^ crc[30])   ^ crc[14];
16209
    crc_next[19] = (data_in[3] ^ crc[31])   ^ crc[15];
16210
    crc_next[20] =  crc[16];
16211
    crc_next[21] =  crc[17];
16212
    crc_next[22] = (data_in[0] ^ crc[28])   ^ crc[18];
16213
    crc_next[23] = (data_in[1] ^ data_in[0] ^ crc[29]    ^ crc[28]) ^ crc[19];
16214
    crc_next[24] = (data_in[2] ^ data_in[1] ^ crc[30]    ^ crc[29]) ^ crc[20];
16215
    crc_next[25] = (data_in[3] ^ data_in[2] ^ crc[31]    ^ crc[30]) ^ crc[21];
16216
    crc_next[26] = (data_in[3] ^ data_in[0] ^ crc[31]    ^ crc[28]) ^ crc[22];
16217
    crc_next[27] = (data_in[1] ^ crc[29])   ^ crc[23];
16218
    crc_next[28] = (data_in[2] ^ crc[30])   ^ crc[24];
16219
    crc_next[29] = (data_in[3] ^ crc[31])   ^ crc[25];
16220
    crc_next[30] =  crc[26];
16221
    crc_next[31] =  crc[27];
16222
 
16223
    crc = crc_next;
16224
    crc_error = crc[31:0] != 32'hc704dd7b;  // CRC not equal to magic number
16225
    case (nibble_cnt)
16226
    9: crc_out = {!crc[24], !crc[25], !crc[26], !crc[27], !crc[28], !crc[29], !crc[30], !crc[31],
16227
                  !crc[16], !crc[17], !crc[18], !crc[19], !crc[20], !crc[21], !crc[22], !crc[23],
16228
                  !crc[ 8], !crc[ 9], !crc[10], !crc[11], !crc[12], !crc[13], !crc[14], !crc[15],
16229
                  !crc[ 0], !crc[ 1], !crc[ 2], !crc[ 3], !crc[ 4], !crc[ 5], !crc[ 6], !crc[ 7]};
16230
    default: crc_out = crc_out;
16231
    endcase
16232 169 mohor
    // wait for delta time
16233
    delta_t = !delta_t;
16234 209 tadejm
    // increment address and load new data
16235
    if ((word_cnt+3) == 7)//4)
16236 169 mohor
    begin
16237 209 tadejm
      // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16238
      load_reg = wb_slave.wb_memory[{12'h0, addr_cnt}];
16239 169 mohor
      addr_cnt = addr_cnt + 4;
16240
    end
16241 209 tadejm
    // set new load bit position
16242
    if((word_cnt+3) == 31)
16243 169 mohor
      word_cnt = 16;
16244 209 tadejm
    else if ((word_cnt+3) == 23)
16245 169 mohor
      word_cnt = 8;
16246 209 tadejm
    else if ((word_cnt+3) == 15)
16247 169 mohor
      word_cnt = 0;
16248 209 tadejm
    else if ((word_cnt+3) == 7)
16249 169 mohor
      word_cnt = 24;
16250
    else
16251 209 tadejm
      word_cnt = word_cnt + 4;// - 4;
16252
    // decrement nibble counter
16253
    nibble_cnt = nibble_cnt - 1;
16254 169 mohor
    // wait for delta time
16255
    delta_t = !delta_t;
16256
  end // while
16257
  #1;
16258
end
16259
endtask // paralel_crc_mac
16260
 
16261
// serial CRC checking for PHY TX
16262
task serial_crc_phy_tx;
16263
  input  [31:0] start_addr; // start address
16264
  input  [31:0] len; // length of frame in Bytes without CRC length
16265
  input         plus_dribble_nibble; // if length is longer for one nibble
16266
  output [31:0] crc;
16267
  reg    [21:0] addr_cnt; // only 22 address lines
16268
  integer       word_cnt;
16269
  integer       bit_cnt;
16270
  reg    [31:0] load_reg;
16271
  reg    [31:0] crc_shift_reg;
16272
  reg    [31:0] crc_store_reg;
16273
  reg           delta_t;
16274
begin
16275
  #1 addr_cnt = start_addr[21:0];
16276
  word_cnt = 24; // 27; // start of the frame - nibble granularity (MSbit first)
16277
  crc_store_reg = 32'hFFFF_FFFF; // INITIAL value
16278
  delta_t = 0;
16279
  // length must include 4 bytes of ZEROs, to generate CRC
16280
  // get number of bits from Byte length (2^3 = 8)
16281
  if (plus_dribble_nibble)
16282
    bit_cnt = ((len + 4) << 3) + 3'h4; // one nibble longer
16283
  else
16284
    bit_cnt = ((len + 4) << 3);
16285
  // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16286
  load_reg[31:24] = eth_phy.tx_mem[addr_cnt];
16287
  addr_cnt = addr_cnt + 1;
16288
  load_reg[23:16] = eth_phy.tx_mem[addr_cnt];
16289
  addr_cnt = addr_cnt + 1;
16290
  load_reg[15: 8] = eth_phy.tx_mem[addr_cnt];
16291
  addr_cnt = addr_cnt + 1;
16292
  load_reg[ 7: 0] = eth_phy.tx_mem[addr_cnt];
16293
  addr_cnt = addr_cnt + 1;
16294
#1;
16295
  while (bit_cnt > 0)
16296
  begin
16297
    // wait for delta time
16298
    delta_t = !delta_t;
16299
#1;
16300
    // shift data in
16301
 
16302
    if(bit_cnt <= 32) // for additional 32 bits shift ZEROs in!
16303
     crc_shift_reg[0] = 1'b0               ^ crc_store_reg[31];
16304
    else
16305
 
16306
     crc_shift_reg[0] = load_reg[word_cnt] ^ crc_store_reg[31];
16307
    crc_shift_reg[1]  = crc_store_reg[0]   ^ crc_store_reg[31];
16308
    crc_shift_reg[2]  = crc_store_reg[1]   ^ crc_store_reg[31];
16309
    crc_shift_reg[3]  = crc_store_reg[2];
16310
    crc_shift_reg[4]  = crc_store_reg[3]   ^ crc_store_reg[31];
16311
    crc_shift_reg[5]  = crc_store_reg[4]   ^ crc_store_reg[31];
16312
    crc_shift_reg[6]  = crc_store_reg[5];
16313
    crc_shift_reg[7]  = crc_store_reg[6]   ^ crc_store_reg[31];
16314
    crc_shift_reg[8]  = crc_store_reg[7]   ^ crc_store_reg[31];
16315
    crc_shift_reg[9]  = crc_store_reg[8];
16316
    crc_shift_reg[10] = crc_store_reg[9]   ^ crc_store_reg[31];
16317
    crc_shift_reg[11] = crc_store_reg[10]  ^ crc_store_reg[31];
16318
    crc_shift_reg[12] = crc_store_reg[11]  ^ crc_store_reg[31];
16319
    crc_shift_reg[13] = crc_store_reg[12];
16320
    crc_shift_reg[14] = crc_store_reg[13];
16321
    crc_shift_reg[15] = crc_store_reg[14];
16322
    crc_shift_reg[16] = crc_store_reg[15]  ^ crc_store_reg[31];
16323
    crc_shift_reg[17] = crc_store_reg[16];
16324
    crc_shift_reg[18] = crc_store_reg[17];
16325
    crc_shift_reg[19] = crc_store_reg[18];
16326
    crc_shift_reg[20] = crc_store_reg[19];
16327
    crc_shift_reg[21] = crc_store_reg[20];
16328
    crc_shift_reg[22] = crc_store_reg[21]  ^ crc_store_reg[31];
16329
    crc_shift_reg[23] = crc_store_reg[22]  ^ crc_store_reg[31];
16330
    crc_shift_reg[24] = crc_store_reg[23];
16331
    crc_shift_reg[25] = crc_store_reg[24];
16332
    crc_shift_reg[26] = crc_store_reg[25]  ^ crc_store_reg[31];
16333
    crc_shift_reg[27] = crc_store_reg[26];
16334
    crc_shift_reg[28] = crc_store_reg[27];
16335
    crc_shift_reg[29] = crc_store_reg[28];
16336
    crc_shift_reg[30] = crc_store_reg[29];
16337
    crc_shift_reg[31] = crc_store_reg[30];
16338
    // wait for delta time
16339
    delta_t = !delta_t;
16340
 
16341
    // store previous data
16342
    crc_store_reg = crc_shift_reg;
16343
 
16344
    // put CRC out
16345
    case (bit_cnt)
16346
    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:
16347
    begin
16348
      crc = crc_store_reg;
16349
      crc = {!crc[24], !crc[25], !crc[26], !crc[27], !crc[28], !crc[29], !crc[30], !crc[31],
16350
             !crc[16], !crc[17], !crc[18], !crc[19], !crc[20], !crc[21], !crc[22], !crc[23],
16351
             !crc[ 8], !crc[ 9], !crc[10], !crc[11], !crc[12], !crc[13], !crc[14], !crc[15],
16352
             !crc[ 0], !crc[ 1], !crc[ 2], !crc[ 3], !crc[ 4], !crc[ 5], !crc[ 6], !crc[ 7]};
16353
    end
16354
    default: crc = crc;
16355
    endcase
16356
 
16357
    // increment address and load new data
16358
#1;
16359
    if (word_cnt == 7)//4)
16360
    begin
16361
      // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16362
      load_reg[31:24] = eth_phy.tx_mem[addr_cnt];
16363
//      load_reg[31:24] = {load_reg[28], load_reg[29], load_reg[30], load_reg[31], 
16364
//                         load_reg[24], load_reg[25], load_reg[26], load_reg[27]};
16365
      addr_cnt = addr_cnt + 1;
16366
      load_reg[23:16] = eth_phy.tx_mem[addr_cnt];
16367
//      load_reg[23:16] = {load_reg[20], load_reg[21], load_reg[22], load_reg[23], 
16368
//                         load_reg[16], load_reg[17], load_reg[18], load_reg[19]};
16369
      addr_cnt = addr_cnt + 1;
16370
      load_reg[15: 8] = eth_phy.tx_mem[addr_cnt];
16371
//      load_reg[15: 8] = {load_reg[12], load_reg[13], load_reg[14], load_reg[15], 
16372
//                         load_reg[ 8], load_reg[ 9], load_reg[10], load_reg[11]};
16373
      addr_cnt = addr_cnt + 1;
16374
      load_reg[ 7: 0] = eth_phy.tx_mem[addr_cnt];
16375
//      load_reg[ 7: 0] = {load_reg[ 4], load_reg[ 5], load_reg[ 6], load_reg[ 7], 
16376
//                         load_reg[ 0], load_reg[ 1], load_reg[ 2], load_reg[ 3]};
16377
      addr_cnt = addr_cnt + 1;
16378
    end
16379
#1;
16380
    // set new load bit position
16381
    if(word_cnt == 31)
16382
      word_cnt = 16;
16383
    else if (word_cnt == 23)
16384
      word_cnt = 8;
16385
    else if (word_cnt == 15)
16386
      word_cnt = 0;
16387
    else if (word_cnt == 7)
16388
      word_cnt = 24;
16389
 
16390
//   if(word_cnt == 24)
16391
//     word_cnt = 31;
16392
//   else if (word_cnt == 28)
16393
//     word_cnt = 19;
16394
//   else if (word_cnt == 16)
16395
//     word_cnt = 23;
16396
//   else if (word_cnt == 20)
16397
//     word_cnt = 11;
16398
//   else if(word_cnt == 8)
16399
//     word_cnt = 15;
16400
//   else if (word_cnt == 12)
16401
//     word_cnt = 3;
16402
//   else if (word_cnt == 0)
16403
//     word_cnt = 7;
16404
//   else if (word_cnt == 4)
16405
//     word_cnt = 27;
16406
    else
16407
      word_cnt = word_cnt + 1;// - 1;
16408
#1;
16409
    // decrement bit counter
16410
    bit_cnt = bit_cnt - 1;
16411
#1;
16412
    // wait for delta time
16413
    delta_t = !delta_t;
16414
  end // while
16415
 
16416
  #1;
16417
end
16418
endtask // serial_crc_phy_tx
16419
 
16420
// serial CRC calculating for PHY RX
16421
task serial_crc_phy_rx;
16422
  input  [31:0] start_addr; // start address
16423
  input  [31:0] len; // length of frame in Bytes without CRC length
16424
  input         plus_dribble_nibble; // if length is longer for one nibble
16425
  output [31:0] crc;
16426
  reg    [21:0] addr_cnt; // only 22 address lines
16427
  integer       word_cnt;
16428
  integer       bit_cnt;
16429
  reg    [31:0] load_reg;
16430
  reg    [31:0] crc_shift_reg;
16431
  reg    [31:0] crc_store_reg;
16432
  reg           delta_t;
16433
begin
16434
  #1 addr_cnt = start_addr[21:0];
16435
  word_cnt = 24; // start of the frame
16436
  crc_shift_reg = 0;
16437
  delta_t = 0;
16438
  // length must include 4 bytes of ZEROs, to generate CRC
16439
  // get number of bits from Byte length (2^3 = 8)
16440
  if (plus_dribble_nibble)
16441
    bit_cnt = ((len + 4) << 3) + 3'h4; // one nibble longer
16442
  else
16443
    bit_cnt = ((len + 4) << 3);
16444
  load_reg[31:24] = eth_phy.rx_mem[addr_cnt];
16445
  addr_cnt = addr_cnt + 1;
16446
  load_reg[23:16] = eth_phy.rx_mem[addr_cnt];
16447
  addr_cnt = addr_cnt + 1;
16448
  load_reg[15:8]  = eth_phy.rx_mem[addr_cnt];
16449
  addr_cnt = addr_cnt + 1;
16450
  load_reg[7:0]   = eth_phy.rx_mem[addr_cnt];
16451
 
16452
  while (bit_cnt > 0)
16453
  begin
16454
    // wait for delta time
16455
    delta_t = !delta_t;
16456
    // store previous data
16457
    crc_store_reg = crc_shift_reg;
16458
    // shift data in
16459
    if(bit_cnt <= 32) // for additional 32 bits shift ZEROs in!
16460
     crc_shift_reg[0] = 1'b0               ^ crc_store_reg[31];
16461
    else
16462
     crc_shift_reg[0] = load_reg[word_cnt] ^ crc_store_reg[31];
16463
    crc_shift_reg[1]  = crc_store_reg[0]   ^ crc_store_reg[31];
16464
    crc_shift_reg[2]  = crc_store_reg[1]   ^ crc_store_reg[31];
16465
    crc_shift_reg[3]  = crc_store_reg[2];
16466
    crc_shift_reg[4]  = crc_store_reg[3]   ^ crc_store_reg[31];
16467
    crc_shift_reg[5]  = crc_store_reg[4]   ^ crc_store_reg[31];
16468
    crc_shift_reg[6]  = crc_store_reg[5];
16469
    crc_shift_reg[7]  = crc_store_reg[6]   ^ crc_store_reg[31];
16470
    crc_shift_reg[8]  = crc_store_reg[7]   ^ crc_store_reg[31];
16471
    crc_shift_reg[9]  = crc_store_reg[8];
16472
    crc_shift_reg[10] = crc_store_reg[9]   ^ crc_store_reg[31];
16473
    crc_shift_reg[11] = crc_store_reg[10]  ^ crc_store_reg[31];
16474
    crc_shift_reg[12] = crc_store_reg[11]  ^ crc_store_reg[31];
16475
    crc_shift_reg[13] = crc_store_reg[12];
16476
    crc_shift_reg[14] = crc_store_reg[13];
16477
    crc_shift_reg[15] = crc_store_reg[14];
16478
    crc_shift_reg[16] = crc_store_reg[15]  ^ crc_store_reg[31];
16479
    crc_shift_reg[17] = crc_store_reg[16];
16480
    crc_shift_reg[18] = crc_store_reg[17];
16481
    crc_shift_reg[19] = crc_store_reg[18];
16482
    crc_shift_reg[20] = crc_store_reg[19];
16483
    crc_shift_reg[21] = crc_store_reg[20];
16484
    crc_shift_reg[22] = crc_store_reg[21]  ^ crc_store_reg[31];
16485
    crc_shift_reg[23] = crc_store_reg[22]  ^ crc_store_reg[31];
16486
    crc_shift_reg[24] = crc_store_reg[23];
16487
    crc_shift_reg[25] = crc_store_reg[24];
16488
    crc_shift_reg[26] = crc_store_reg[25]  ^ crc_store_reg[31];
16489
    crc_shift_reg[27] = crc_store_reg[26];
16490
    crc_shift_reg[28] = crc_store_reg[27];
16491
    crc_shift_reg[29] = crc_store_reg[28];
16492
    crc_shift_reg[30] = crc_store_reg[29];
16493
    crc_shift_reg[31] = crc_store_reg[30];
16494
    // wait for delta time
16495
    delta_t = !delta_t;
16496
    // increment address and load new data
16497
    if (word_cnt == 7)
16498
    begin
16499
      addr_cnt = addr_cnt + 1;
16500
      load_reg[31:24] = eth_phy.rx_mem[addr_cnt];
16501
      addr_cnt = addr_cnt + 1;
16502
      load_reg[23:16] = eth_phy.rx_mem[addr_cnt];
16503
      addr_cnt = addr_cnt + 1;
16504
      load_reg[15:8]  = eth_phy.rx_mem[addr_cnt];
16505
      addr_cnt = addr_cnt + 1;
16506
      load_reg[7:0]   = eth_phy.rx_mem[addr_cnt];
16507
    end
16508
    // set new load bit position
16509
    if(word_cnt == 31)
16510
      word_cnt = 16;
16511
    else if (word_cnt == 23)
16512
      word_cnt = 8;
16513
    else if (word_cnt == 15)
16514
      word_cnt = 0;
16515
    else if (word_cnt == 7)
16516
      word_cnt = 24;
16517
    else
16518
      word_cnt = word_cnt + 1;
16519
    // decrement bit counter
16520
    bit_cnt = bit_cnt - 1;
16521
    // wait for delta time
16522
    delta_t = !delta_t;
16523
  end // while
16524
 
16525
  // put CRC out
16526
  crc = crc_shift_reg;
16527
  #1;
16528
end
16529
endtask // serial_crc_phy_rx
16530
 
16531
// serial CRC checking for MAC
16532
task serial_crc_mac;
16533
  input  [31:0] start_addr; // start address
16534
  input  [31:0] len; // length of frame in Bytes without CRC length
16535
  input         plus_dribble_nibble; // if length is longer for one nibble
16536
  output [31:0] crc;
16537
  reg    [19:0] addr_cnt; // only 20 address lines
16538
  integer       word_cnt;
16539
  integer       bit_cnt;
16540
  reg    [31:0] load_reg;
16541
  reg    [31:0] crc_shift_reg;
16542
  reg    [31:0] crc_store_reg;
16543
  reg           delta_t;
16544
begin
16545
  #1 addr_cnt = start_addr[19:0];
16546
  // set starting point depending with which byte frame starts (e.g. if addr_cnt[1:0] == 0, then
16547
  //   MSB of the packet must be written to the LSB of Big ENDIAN Word [31:24])
16548
  if (addr_cnt[1:0] == 2'h1)
16549
    word_cnt = 16; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16550
  else if (addr_cnt[1:0] == 2'h2)
16551
    word_cnt = 8; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16552
  else if (addr_cnt[1:0] == 2'h3)
16553
    word_cnt = 0; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16554
  else
16555
    word_cnt = 24; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16556
 
16557
  crc_shift_reg = 0;
16558
  delta_t = 0;
16559
  // length must include 4 bytes of ZEROs, to generate CRC
16560
  // get number of bits from Byte length (2^3 = 8)
16561
  if (plus_dribble_nibble)
16562
    bit_cnt = ((len + 4) << 3) + 3'h4; // one nibble longer
16563
  else
16564
    bit_cnt = ((len + 4) << 3);
16565
  load_reg = wb_slave.wb_memory[{12'h0, addr_cnt}];
16566
 
16567
  while (bit_cnt > 0)
16568
  begin
16569
    // wait for delta time
16570
    delta_t = !delta_t;
16571
    // store previous data
16572
    crc_store_reg = crc_shift_reg;
16573
    // shift data in
16574
    if(bit_cnt <= 32) // for additional 32 bits shift ZEROs in!
16575
     crc_shift_reg[0] = 1'b0               ^ crc_store_reg[31];
16576
    else
16577
     crc_shift_reg[0] = load_reg[word_cnt] ^ crc_store_reg[31];
16578
    crc_shift_reg[1]  = crc_store_reg[0]   ^ crc_store_reg[31];
16579
    crc_shift_reg[2]  = crc_store_reg[1]   ^ crc_store_reg[31];
16580
    crc_shift_reg[3]  = crc_store_reg[2];
16581
    crc_shift_reg[4]  = crc_store_reg[3]   ^ crc_store_reg[31];
16582
    crc_shift_reg[5]  = crc_store_reg[4]   ^ crc_store_reg[31];
16583
    crc_shift_reg[6]  = crc_store_reg[5];
16584
    crc_shift_reg[7]  = crc_store_reg[6]   ^ crc_store_reg[31];
16585
    crc_shift_reg[8]  = crc_store_reg[7]   ^ crc_store_reg[31];
16586
    crc_shift_reg[9]  = crc_store_reg[8];
16587
    crc_shift_reg[10] = crc_store_reg[9]   ^ crc_store_reg[31];
16588
    crc_shift_reg[11] = crc_store_reg[10]  ^ crc_store_reg[31];
16589
    crc_shift_reg[12] = crc_store_reg[11]  ^ crc_store_reg[31];
16590
    crc_shift_reg[13] = crc_store_reg[12];
16591
    crc_shift_reg[14] = crc_store_reg[13];
16592
    crc_shift_reg[15] = crc_store_reg[14];
16593
    crc_shift_reg[16] = crc_store_reg[15]  ^ crc_store_reg[31];
16594
    crc_shift_reg[17] = crc_store_reg[16];
16595
    crc_shift_reg[18] = crc_store_reg[17];
16596
    crc_shift_reg[19] = crc_store_reg[18];
16597
    crc_shift_reg[20] = crc_store_reg[19];
16598
    crc_shift_reg[21] = crc_store_reg[20];
16599
    crc_shift_reg[22] = crc_store_reg[21]  ^ crc_store_reg[31];
16600
    crc_shift_reg[23] = crc_store_reg[22]  ^ crc_store_reg[31];
16601
    crc_shift_reg[24] = crc_store_reg[23];
16602
    crc_shift_reg[25] = crc_store_reg[24];
16603
    crc_shift_reg[26] = crc_store_reg[25]  ^ crc_store_reg[31];
16604
    crc_shift_reg[27] = crc_store_reg[26];
16605
    crc_shift_reg[28] = crc_store_reg[27];
16606
    crc_shift_reg[29] = crc_store_reg[28];
16607
    crc_shift_reg[30] = crc_store_reg[29];
16608
    crc_shift_reg[31] = crc_store_reg[30];
16609
    // wait for delta time
16610
    delta_t = !delta_t;
16611
    // increment address and load new data for Big ENDIAN Bytes (Litle ENDIAN bits)
16612
    if (word_cnt == 7)
16613
    begin
16614
      addr_cnt = addr_cnt + 4;
16615
      load_reg = wb_slave.wb_memory[{12'h0, addr_cnt}];
16616
    end
16617
    // set new load bit position for Big ENDIAN Bytes (Litle ENDIAN bits)
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
    else
16627
      word_cnt = word_cnt + 1;
16628
    // decrement bit counter
16629
    bit_cnt = bit_cnt - 1;
16630
    // wait for delta time
16631
    delta_t = !delta_t;
16632
  end // while
16633
 
16634
  // put CRC out
16635
  crc = crc_shift_reg;
16636
  #1;
16637
end
16638
endtask // serial_crc_mac
16639
 
16640
//////////////////////////////////////////////////////////////
16641
// MIIM Basic tasks
16642
//////////////////////////////////////////////////////////////
16643
 
16644
task reset_mii; //  MII module
16645
  reg [31:0] tmp;
16646
  reg [31:0] tmp_no_rst;
16647
begin
16648
  // read MII mode register first
16649
  wbm_read(`ETH_MIIMODER, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16650
  // set reset bit - write back to MII mode register with RESET bit
16651
  wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_RST | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16652
  // clear reset bit - write back to MII mode register without RESET bit
16653
  tmp_no_rst = `ETH_MIIMODER_RST;
16654
  tmp_no_rst = ~tmp_no_rst;
16655
  wbm_write(`ETH_MIIMODER, (tmp_no_rst & tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16656
end
16657
endtask // reset_mii
16658
 
16659
task mii_set_clk_div; // set clock divider for MII clock
16660
  input [7:0]  clk_div;
16661
begin
16662
  // MII mode register
16663
  wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_CLKDIV & clk_div), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16664
end
16665
endtask // mii_set_clk_div
16666
 
16667
 
16668
task check_mii_busy; // MII - check if BUSY
16669
  reg [31:0] tmp;
16670
begin
16671
  @(posedge wb_clk);
16672
  // MII read status register
16673
  wbm_read(`ETH_MIISTATUS, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16674
  while(tmp[`ETH_MIISTATUS_BUSY] !== 1'b0) //`ETH_MIISTATUS_BUSY
16675
  begin
16676
    @(posedge wb_clk);
16677
    wbm_read(`ETH_MIISTATUS, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16678
  end
16679
end
16680
endtask // check_mii_busy
16681
 
16682
 
16683
task check_mii_scan_valid; // MII - check if SCAN data are valid
16684
  reg [31:0] tmp;
16685
begin
16686
  @(posedge wb_clk);
16687
  // MII read status register
16688
  wbm_read(`ETH_MIISTATUS, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16689
  while(tmp[`ETH_MIISTATUS_NVALID] !== 1'b0) //`ETH_MIISTATUS_NVALID
16690
  begin
16691
    @(posedge wb_clk);
16692
    wbm_read(`ETH_MIISTATUS, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16693
  end
16694
end
16695
endtask // check_mii_scan_valid
16696
 
16697
 
16698
task mii_write_req; // requests write to MII
16699
  input [4:0]  phy_addr;
16700
  input [4:0]  reg_addr;
16701
  input [15:0] data_in;
16702
begin
16703
  // MII address, PHY address = 1, command register address = 0
16704
  wbm_write(`ETH_MIIADDRESS, (`ETH_MIIADDRESS_FIAD & phy_addr) | (`ETH_MIIADDRESS_RGAD & (reg_addr << 8)),
16705
            4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16706
  // MII TX data
16707
  wbm_write(`ETH_MIITX_DATA, {16'h0000, data_in}, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16708
  // MII command
16709
  wbm_write(`ETH_MIICOMMAND, `ETH_MIICOMMAND_WCTRLDATA, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16710
  @(posedge wb_clk);
16711
end
16712
endtask // mii_write_req
16713
 
16714
 
16715
task mii_read_req; // requests read from MII
16716
  input [4:0]  phy_addr;
16717
  input [4:0]  reg_addr;
16718
begin
16719
  // MII address, PHY address = 1, command register address = 0
16720
  wbm_write(`ETH_MIIADDRESS, (`ETH_MIIADDRESS_FIAD & phy_addr) | (`ETH_MIIADDRESS_RGAD & (reg_addr << 8)),
16721
            4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16722
  // MII command
16723
  wbm_write(`ETH_MIICOMMAND, `ETH_MIICOMMAND_RSTAT, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16724
  @(posedge wb_clk);
16725
end
16726
endtask // mii_read_req
16727
 
16728
 
16729
task mii_scan_req; // requests scan from MII
16730
  input [4:0]  phy_addr;
16731
  input [4:0]  reg_addr;
16732
begin
16733
  // MII address, PHY address = 1, command register address = 0
16734
  wbm_write(`ETH_MIIADDRESS, (`ETH_MIIADDRESS_FIAD & phy_addr) | (`ETH_MIIADDRESS_RGAD & (reg_addr << 8)),
16735
            4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16736
  // MII command
16737
  wbm_write(`ETH_MIICOMMAND, `ETH_MIICOMMAND_SCANSTAT, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16738
  @(posedge wb_clk);
16739
end
16740
endtask // mii_scan_req
16741
 
16742
 
16743
task mii_scan_finish; // finish scan from MII
16744
begin
16745
  // MII command
16746
  wbm_write(`ETH_MIICOMMAND, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16747
  @(posedge wb_clk);
16748
end
16749
endtask // mii_scan_finish
16750
 
16751
//////////////////////////////////////////////////////////////
16752
// Log files and memory tasks
16753
//////////////////////////////////////////////////////////////
16754
 
16755
task clear_memories;
16756
  reg    [22:0]  adr_i;
16757
  reg            delta_t;
16758
begin
16759
  delta_t = 0;
16760
  for (adr_i = 0; adr_i < 4194304; adr_i = adr_i + 1)
16761
  begin
16762
    eth_phy.rx_mem[adr_i[21:0]] = 0;
16763
    eth_phy.tx_mem[adr_i[21:0]] = 0;
16764
    wb_slave.wb_memory[adr_i[21:2]] = 0;
16765
    delta_t = !delta_t;
16766
  end
16767
end
16768
endtask // clear_memories
16769
 
16770 243 tadejm
task clear_buffer_descriptors;
16771
  reg    [8:0]  adr_i;
16772
  reg            delta_t;
16773
begin
16774
  delta_t = 0;
16775
  for (adr_i = 0; adr_i < 256; adr_i = adr_i + 1)
16776
  begin
16777
    wbm_write((`TX_BD_BASE + {adr_i[7:0], 2'b0}), 32'h0, 4'hF, 1, 4'h1, 4'h1);
16778
    delta_t = !delta_t;
16779
  end
16780
end
16781
endtask // clear_buffer_descriptors
16782
 
16783 169 mohor
task test_note;
16784
  input [799:0] test_note ;
16785
  reg   [799:0] display_note ;
16786
begin
16787
  display_note = test_note;
16788
  while ( display_note[799:792] == 0 )
16789
    display_note = display_note << 8 ;
16790
  $fdisplay( tb_log_file, " " ) ;
16791
  $fdisplay( tb_log_file, "NOTE: %s", display_note ) ;
16792
  $fdisplay( tb_log_file, " " ) ;
16793
end
16794
endtask // test_note
16795
 
16796
task test_heading;
16797
  input [799:0] test_heading ;
16798
  reg   [799:0] display_test ;
16799
begin
16800
  display_test = test_heading;
16801
  while ( display_test[799:792] == 0 )
16802
    display_test = display_test << 8 ;
16803
  $fdisplay( tb_log_file, "  ***************************************************************************************" ) ;
16804
  $fdisplay( tb_log_file, "  ***************************************************************************************" ) ;
16805
  $fdisplay( tb_log_file, "  Heading: %s", display_test ) ;
16806
  $fdisplay( tb_log_file, "  ***************************************************************************************" ) ;
16807
  $fdisplay( tb_log_file, "  ***************************************************************************************" ) ;
16808
  $fdisplay( tb_log_file, " " ) ;
16809
end
16810
endtask // test_heading
16811
 
16812
 
16813
task test_fail ;
16814
  input [7999:0] failure_reason ;
16815
//  reg   [8007:0] display_failure ;
16816
  reg   [7999:0] display_failure ;
16817
  reg   [799:0] display_test ;
16818
begin
16819
  tests_failed = tests_failed + 1 ;
16820
 
16821
  display_failure = failure_reason; // {failure_reason, "!"} ;
16822
  while ( display_failure[7999:7992] == 0 )
16823
    display_failure = display_failure << 8 ;
16824
 
16825
  display_test = test_name ;
16826
  while ( display_test[799:792] == 0 )
16827
    display_test = display_test << 8 ;
16828
 
16829
  $fdisplay( tb_log_file, "    *************************************************************************************" ) ;
16830
  $fdisplay( tb_log_file, "    At time: %t ", $time ) ;
16831
  $fdisplay( tb_log_file, "    Test: %s", display_test ) ;
16832
  $fdisplay( tb_log_file, "    *FAILED* because") ;
16833
  $fdisplay( tb_log_file, "    %s", display_failure ) ;
16834
  $fdisplay( tb_log_file, "    *************************************************************************************" ) ;
16835
  $fdisplay( tb_log_file, " " ) ;
16836
 
16837
 `ifdef STOP_ON_FAILURE
16838
    #20 $stop ;
16839
 `endif
16840
end
16841
endtask // test_fail
16842
 
16843
 
16844
task test_ok ;
16845
  reg [799:0] display_test ;
16846
begin
16847
  tests_successfull = tests_successfull + 1 ;
16848
 
16849
  display_test = test_name ;
16850
  while ( display_test[799:792] == 0 )
16851
    display_test = display_test << 8 ;
16852
 
16853
  $fdisplay( tb_log_file, "    *************************************************************************************" ) ;
16854
  $fdisplay( tb_log_file, "    At time: %t ", $time ) ;
16855
  $fdisplay( tb_log_file, "    Test: %s", display_test ) ;
16856
  $fdisplay( tb_log_file, "    reported *SUCCESSFULL*! ") ;
16857
  $fdisplay( tb_log_file, "    *************************************************************************************" ) ;
16858
  $fdisplay( tb_log_file, " " ) ;
16859
end
16860
endtask // test_ok
16861
 
16862
 
16863
task test_summary;
16864
begin
16865
  $fdisplay(tb_log_file, "**************************** Ethernet MAC test summary **********************************") ;
16866
  $fdisplay(tb_log_file, "Tests performed:   %d", tests_successfull + tests_failed) ;
16867
  $fdisplay(tb_log_file, "Failed tests   :   %d", tests_failed) ;
16868
  $fdisplay(tb_log_file, "Successfull tests: %d", tests_successfull) ;
16869
  $fdisplay(tb_log_file, "**************************** Ethernet MAC test summary **********************************") ;
16870
  $fclose(tb_log_file) ;
16871
end
16872
endtask // test_summary
16873
 
16874
 
16875 116 mohor
endmodule

powered by: WebSVN 2.1.0

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