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

Subversion Repositories ethmac

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

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 260 mohor
// Revision 1.21  2002/11/19 20:27:45  mohor
46
// Temp version.
47
//
48 254 mohor
// Revision 1.20  2002/11/19 17:41:19  tadejm
49
// Just some updates.
50
//
51 252 tadejm
// Revision 1.19  2002/11/14 13:12:47  tadejm
52
// Late collision is not reported any more.
53
//
54 243 tadejm
// Revision 1.18  2002/10/18 17:03:34  tadejm
55
// Changed BIST scan signals.
56
//
57 227 tadejm
// Revision 1.17  2002/10/18 13:58:22  tadejm
58
// Some code changed due to bug fixes.
59
//
60 223 tadejm
// Revision 1.16  2002/10/09 13:16:51  tadejm
61
// Just back-up; not completed testbench and some testcases are not
62
// wotking properly yet.
63
//
64 209 tadejm
// Revision 1.15  2002/09/20 14:29:12  tadej
65
// Full duplex tests modified and testbench bug repaired.
66
//
67 194 tadej
// Revision 1.14  2002/09/18 17:56:38  tadej
68
// Some additional reports added
69
//
70 192 tadej
// Revision 1.13  2002/09/16 17:53:49  tadej
71
// Full duplex test improved.
72
//
73 182 tadej
// Revision 1.12  2002/09/16 15:10:42  mohor
74
// MIIM test look better.
75
//
76 181 mohor
// Revision 1.11  2002/09/13 19:18:04  mohor
77
// Bench outputs data to display every 128 bytes.
78
//
79 180 mohor
// Revision 1.10  2002/09/13 18:44:29  mohor
80
// Beautiful tests merget together
81
//
82 179 mohor
// Revision 1.9  2002/09/13 18:41:45  mohor
83
// Rearanged testcases
84
//
85 178 mohor
// Revision 1.8  2002/09/13 14:50:15  mohor
86
// Bug in MIIM fixed.
87
//
88 177 mohor
// Revision 1.7  2002/09/13 12:29:14  mohor
89
// Headers changed.
90
//
91 170 mohor
// Revision 1.6  2002/09/13 11:57:20  mohor
92
// New testbench. Thanks to Tadej M - "The Spammer".
93
//
94 121 mohor
// Revision 1.2  2002/07/19 14:02:47  mohor
95
// Clock mrx_clk set to 2.5 MHz.
96
//
97 117 mohor
// Revision 1.1  2002/07/19 13:57:53  mohor
98
// Testing environment also includes traffic cop, memory interface and host
99
// interface.
100 116 mohor
//
101
//
102
//
103
//
104 117 mohor
//
105 116 mohor
 
106
 
107 169 mohor
`include "eth_phy_defines.v"
108
`include "wb_model_defines.v"
109 116 mohor
`include "tb_eth_defines.v"
110
`include "eth_defines.v"
111
`include "timescale.v"
112
 
113
module tb_ethernet();
114
 
115
 
116 169 mohor
reg           wb_clk;
117
reg           wb_rst;
118
wire          wb_int;
119 116 mohor
 
120 169 mohor
wire          mtx_clk;  // This goes to PHY
121
wire          mrx_clk;  // This goes to PHY
122 116 mohor
 
123
wire   [3:0]  MTxD;
124
wire          MTxEn;
125
wire          MTxErr;
126
 
127 169 mohor
wire   [3:0]  MRxD;     // This goes to PHY
128
wire          MRxDV;    // This goes to PHY
129
wire          MRxErr;   // This goes to PHY
130
wire          MColl;    // This goes to PHY
131
wire          MCrs;     // This goes to PHY
132 116 mohor
 
133
wire          Mdi_I;
134
wire          Mdo_O;
135
wire          Mdo_OE;
136 169 mohor
tri           Mdio_IO;
137 116 mohor
wire          Mdc_O;
138
 
139
 
140 169 mohor
parameter Tp = 1;
141 116 mohor
 
142 121 mohor
 
143 116 mohor
// Ethernet Slave Interface signals
144 169 mohor
wire [31:0] eth_sl_wb_adr;
145 116 mohor
wire [31:0] eth_sl_wb_adr_i, eth_sl_wb_dat_o, eth_sl_wb_dat_i;
146
wire  [3:0] eth_sl_wb_sel_i;
147
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;
148
 
149
// Ethernet Master Interface signals
150
wire [31:0] eth_ma_wb_adr_o, eth_ma_wb_dat_i, eth_ma_wb_dat_o;
151
wire  [3:0] eth_ma_wb_sel_o;
152
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;
153
 
154
 
155
 
156
 
157
// Connecting Ethernet top module
158 169 mohor
eth_top eth_top
159 116 mohor
(
160
  // WISHBONE common
161 169 mohor
  .wb_clk_i(wb_clk),              .wb_rst_i(wb_rst),
162 116 mohor
 
163
  // WISHBONE slave
164 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),
165
  .wb_cyc_i(eth_sl_wb_cyc_i),       .wb_stb_i(eth_sl_wb_stb_i),   .wb_ack_o(eth_sl_wb_ack_o),
166
  .wb_err_o(eth_sl_wb_err_o),       .wb_dat_i(eth_sl_wb_dat_i),   .wb_dat_o(eth_sl_wb_dat_o),
167 116 mohor
 
168
  // WISHBONE master
169
  .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),
170
  .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),
171
  .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),
172
 
173
  //TX
174
  .mtx_clk_pad_i(mtx_clk), .mtxd_pad_o(MTxD), .mtxen_pad_o(MTxEn), .mtxerr_pad_o(MTxErr),
175
 
176
  //RX
177
  .mrx_clk_pad_i(mrx_clk), .mrxd_pad_i(MRxD), .mrxdv_pad_i(MRxDV), .mrxerr_pad_i(MRxErr),
178
  .mcoll_pad_i(MColl),    .mcrs_pad_i(MCrs),
179
 
180
  // MIIM
181
  .mdc_pad_o(Mdc_O), .md_pad_i(Mdi_I), .md_pad_o(Mdo_O), .md_padoe_o(Mdo_OE),
182
 
183 169 mohor
  .int_o(wb_int)
184 227 tadejm
 
185
  // Bist
186
`ifdef ETH_BIST
187
  ,
188
  .scanb_rst      (1'b0),
189
  .scanb_clk      (1'b0),
190
  .scanb_si       (1'b0),
191
  .scanb_so       (),
192
  .scanb_en       (1'b0)
193
`endif
194 116 mohor
);
195
 
196
 
197
 
198 169 mohor
// Connecting Ethernet PHY Module
199
assign Mdio_IO = Mdo_OE ? Mdo_O : 1'bz ;
200
assign Mdi_I   = Mdio_IO;
201
integer phy_log_file_desc;
202
 
203
eth_phy eth_phy
204 116 mohor
(
205 169 mohor
  // WISHBONE reset
206
  .m_rst_n_i(!wb_rst),
207 116 mohor
 
208 169 mohor
  // MAC TX
209
  .mtx_clk_o(mtx_clk),    .mtxd_i(MTxD),    .mtxen_i(MTxEn),    .mtxerr_i(MTxErr),
210
 
211
  // MAC RX
212
  .mrx_clk_o(mrx_clk),    .mrxd_o(MRxD),    .mrxdv_o(MRxDV),    .mrxerr_o(MRxErr),
213
  .mcoll_o(MColl),        .mcrs_o(MCrs),
214
 
215
  // MIIM
216
  .mdc_i(Mdc_O),          .md_io(Mdio_IO),
217
 
218
  // SYSTEM
219
  .phy_log(phy_log_file_desc)
220 116 mohor
);
221
 
222
 
223 169 mohor
 
224
// Connecting WB Master as Host Interface
225
integer host_log_file_desc;
226
 
227
WB_MASTER_BEHAVIORAL wb_master
228 116 mohor
(
229 169 mohor
    .CLK_I(wb_clk),
230
    .RST_I(wb_rst),
231
    .TAG_I({`WB_TAG_WIDTH{1'b0}}),
232
    .TAG_O(),
233
    .ACK_I(eth_sl_wb_ack_o),
234
    .ADR_O(eth_sl_wb_adr), // only eth_sl_wb_adr_i[11:2] used
235
    .CYC_O(eth_sl_wb_cyc_i),
236
    .DAT_I(eth_sl_wb_dat_o),
237
    .DAT_O(eth_sl_wb_dat_i),
238
    .ERR_I(eth_sl_wb_err_o),
239
    .RTY_I(1'b0),  // inactive (1'b0)
240
    .SEL_O(eth_sl_wb_sel_i),
241
    .STB_O(eth_sl_wb_stb_i),
242
    .WE_O (eth_sl_wb_we_i),
243
    .CAB_O()       // NOT USED for now!
244
);
245
 
246
assign eth_sl_wb_adr_i = {20'h0, eth_sl_wb_adr[11:2], 2'h0};
247
 
248
 
249
 
250
// Connecting WB Slave as Memory Interface Module
251
integer memory_log_file_desc;
252
 
253
WB_SLAVE_BEHAVIORAL wb_slave
254
(
255
    .CLK_I(wb_clk),
256
    .RST_I(wb_rst),
257
    .ACK_O(eth_ma_wb_ack_i),
258
    .ADR_I(eth_ma_wb_adr_o),
259
    .CYC_I(eth_ma_wb_cyc_o),
260
    .DAT_O(eth_ma_wb_dat_i),
261
    .DAT_I(eth_ma_wb_dat_o),
262
    .ERR_O(eth_ma_wb_err_i),
263
    .RTY_O(),      // NOT USED for now!
264
    .SEL_I(eth_ma_wb_sel_o),
265
    .STB_I(eth_ma_wb_stb_o),
266
    .WE_I (eth_ma_wb_we_o),
267
    .CAB_I(1'b0)   // inactive (1'b0)
268
);
269
 
270
 
271
 
272
// Connecting WISHBONE Bus Monitors to ethernet master and slave interfaces
273
integer wb_s_mon_log_file_desc ;
274
integer wb_m_mon_log_file_desc ;
275
 
276
WB_BUS_MON wb_eth_slave_bus_mon
277
(
278 116 mohor
  // WISHBONE common
279 169 mohor
  .CLK_I(wb_clk),
280
  .RST_I(wb_rst),
281 116 mohor
 
282 169 mohor
  // WISHBONE slave
283
  .ACK_I(eth_sl_wb_ack_o),
284
  .ADDR_O({20'h0, eth_sl_wb_adr_i[11:2], 2'b0}),
285
  .CYC_O(eth_sl_wb_cyc_i),
286
  .DAT_I(eth_sl_wb_dat_o),
287
  .DAT_O(eth_sl_wb_dat_i),
288
  .ERR_I(eth_sl_wb_err_o),
289
  .RTY_I(1'b0),
290
  .SEL_O(eth_sl_wb_sel_i),
291
  .STB_O(eth_sl_wb_stb_i),
292
  .WE_O (eth_sl_wb_we_i),
293
  .TAG_I({`WB_TAG_WIDTH{1'b0}}),
294
  .TAG_O(),
295
  .CAB_O(1'b0),
296
  .log_file_desc (wb_s_mon_log_file_desc)
297
);
298
 
299
WB_BUS_MON wb_eth_master_bus_mon
300
(
301
  // WISHBONE common
302
  .CLK_I(wb_clk),
303
  .RST_I(wb_rst),
304
 
305 116 mohor
  // WISHBONE master
306 169 mohor
  .ACK_I(eth_ma_wb_ack_i),
307
  .ADDR_O(eth_ma_wb_adr_o),
308
  .CYC_O(eth_ma_wb_cyc_o),
309
  .DAT_I(eth_ma_wb_dat_i),
310
  .DAT_O(eth_ma_wb_dat_o),
311
  .ERR_I(eth_ma_wb_err_i),
312
  .RTY_I(1'b0),
313
  .SEL_O(eth_ma_wb_sel_o),
314
  .STB_O(eth_ma_wb_stb_o),
315
  .WE_O (eth_ma_wb_we_o),
316
  .TAG_I({`WB_TAG_WIDTH{1'b0}}),
317
  .TAG_O(),
318
  .CAB_O(1'b0),
319
  .log_file_desc(wb_m_mon_log_file_desc)
320 116 mohor
);
321
 
322
 
323
 
324 169 mohor
reg         StartTB;
325
integer     tb_log_file;
326 116 mohor
 
327 169 mohor
initial
328
begin
329
  tb_log_file = $fopen("../log/eth_tb.log");
330
  if (tb_log_file < 2)
331
  begin
332
    $display("*E Could not open/create testbench log file in ../log/ directory!");
333
    $finish;
334
  end
335
  $fdisplay(tb_log_file, "========================== ETHERNET IP Core Testbench results ===========================");
336
  $fdisplay(tb_log_file, " ");
337 116 mohor
 
338 169 mohor
  phy_log_file_desc = $fopen("../log/eth_tb_phy.log");
339
  if (phy_log_file_desc < 2)
340
  begin
341
    $fdisplay(tb_log_file, "*E Could not open/create eth_tb_phy.log file in ../log/ directory!");
342
    $finish;
343
  end
344
  $fdisplay(phy_log_file_desc, "================ PHY Module  Testbench access log ================");
345
  $fdisplay(phy_log_file_desc, " ");
346
 
347
  memory_log_file_desc = $fopen("../log/eth_tb_memory.log");
348
  if (memory_log_file_desc < 2)
349
  begin
350
    $fdisplay(tb_log_file, "*E Could not open/create eth_tb_memory.log file in ../log/ directory!");
351
    $finish;
352
  end
353
  $fdisplay(memory_log_file_desc, "=============== MEMORY Module Testbench access log ===============");
354
  $fdisplay(memory_log_file_desc, " ");
355
 
356
  host_log_file_desc = $fopen("../log/eth_tb_host.log");
357
  if (host_log_file_desc < 2)
358
  begin
359
    $fdisplay(tb_log_file, "*E Could not open/create eth_tb_host.log file in ../log/ directory!");
360
    $finish;
361
  end
362
  $fdisplay(host_log_file_desc, "================ HOST Module Testbench access log ================");
363
  $fdisplay(host_log_file_desc, " ");
364
 
365
  wb_s_mon_log_file_desc = $fopen("../log/eth_tb_wb_s_mon.log");
366
  if (wb_s_mon_log_file_desc < 2)
367
  begin
368
    $fdisplay(tb_log_file, "*E Could not open/create eth_tb_wb_s_mon.log file in ../log/ directory!");
369
    $finish;
370
  end
371
  $fdisplay(wb_s_mon_log_file_desc, "============== WISHBONE Slave Bus Monitor error log ==============");
372
  $fdisplay(wb_s_mon_log_file_desc, " ");
373
  $fdisplay(wb_s_mon_log_file_desc, "   Only ERRONEOUS conditions are logged !");
374
  $fdisplay(wb_s_mon_log_file_desc, " ");
375
 
376
  wb_m_mon_log_file_desc = $fopen("../log/eth_tb_wb_m_mon.log");
377
  if (wb_m_mon_log_file_desc < 2)
378
  begin
379
    $fdisplay(tb_log_file, "*E Could not open/create eth_tb_wb_m_mon.log file in ../log/ directory!");
380
    $finish;
381
  end
382
  $fdisplay(wb_m_mon_log_file_desc, "============= WISHBONE Master Bus Monitor  error log =============");
383
  $fdisplay(wb_m_mon_log_file_desc, " ");
384
  $fdisplay(wb_m_mon_log_file_desc, "   Only ERRONEOUS conditions are logged !");
385
  $fdisplay(wb_m_mon_log_file_desc, " ");
386
 
387 243 tadejm
  // Reset pulse
388
  wb_rst =  1'b1;
389
  #423 wb_rst =  1'b0;
390
 
391 169 mohor
  // Clear memories
392
  clear_memories;
393 243 tadejm
  clear_buffer_descriptors;
394 169 mohor
 
395
  #423 StartTB  =  1'b1;
396
end
397
 
398
 
399
 
400
// Generating wb_clk clock
401 116 mohor
initial
402
begin
403 169 mohor
  wb_clk=0;
404
//  forever #2.5 wb_clk = ~wb_clk;  // 2*2.5 ns -> 200.0 MHz    
405 209 tadejm
  forever #5 wb_clk = ~wb_clk;  // 2*5 ns -> 100.0 MHz    
406 169 mohor
//  forever #10 wb_clk = ~wb_clk;  // 2*10 ns -> 50.0 MHz    
407
//  forever #12.5 wb_clk = ~wb_clk;  // 2*12.5 ns -> 40 MHz    
408
//  forever #15 wb_clk = ~wb_clk;  // 2*10 ns -> 33.3 MHz    
409 209 tadejm
//  forever #20 wb_clk = ~wb_clk;  // 2*20 ns -> 25 MHz    
410 169 mohor
//  forever #25 wb_clk = ~wb_clk;  // 2*25 ns -> 20.0 MHz
411
//  forever #31.25 wb_clk = ~wb_clk;  // 2*31.25 ns -> 16.0 MHz    
412
//  forever #50 wb_clk = ~wb_clk;  // 2*50 ns -> 10.0 MHz
413
//  forever #55 wb_clk = ~wb_clk;  // 2*55 ns ->  9.1 MHz    
414 116 mohor
end
415
 
416
 
417
 
418 169 mohor
integer      tests_successfull;
419
integer      tests_failed;
420
reg [799:0]  test_name; // used for tb_log_file
421 121 mohor
 
422 169 mohor
reg   [3:0]  wbm_init_waits; // initial wait cycles between CYC_O and STB_O of WB Master
423
reg   [3:0]  wbm_subseq_waits; // subsequent wait cycles between STB_Os of WB Master
424
reg   [2:0]  wbs_waits; // wait cycles befor WB Slave responds
425
reg   [7:0]  wbs_retries; // if RTY response, then this is the number of retries before ACK
426
 
427 116 mohor
initial
428
begin
429 169 mohor
  wait(StartTB);  // Start of testbench
430
 
431
  // Initial global values
432
  tests_successfull = 0;
433
  tests_failed = 0;
434
 
435
  wbm_init_waits = 4'h1;
436
  wbm_subseq_waits = 4'h3;
437
  wbs_waits = 4'h1;
438
  wbs_retries = 8'h2;
439
  wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
440
 
441
 
442
  //  Call tests
443
  //  ----------
444 194 tadej
//    test_access_to_mac_reg(0, 3);           // 0 - 3
445
//    test_mii(0, 17);                        // 0 - 17
446 169 mohor
  test_note("PHY generates ideal Carrier sense and Collision signals for following tests");
447
  eth_phy.carrier_sense_real_delay(0);
448 243 tadejm
//    test_mac_full_duplex_transmit(8, 9);    // 0 - (21)
449 254 mohor
//    test_mac_full_duplex_receive(8, 9);
450
    test_mac_full_duplex_flow(0, 0);
451 169 mohor
 
452 243 tadejm
  test_note("PHY generates 'real delayed' Carrier sense and Collision signals for following tests");
453 169 mohor
  eth_phy.carrier_sense_real_delay(1);
454
 
455
 
456
  // Finish test's logs
457
  test_summary;
458
  $display("\n\n END of SIMULATION");
459
  $fclose(tb_log_file | phy_log_file_desc | memory_log_file_desc | host_log_file_desc);
460
  $fclose(wb_s_mon_log_file_desc | wb_m_mon_log_file_desc);
461
 
462
  $stop;
463 116 mohor
end
464 169 mohor
 
465 116 mohor
 
466 169 mohor
 
467
//////////////////////////////////////////////////////////////
468
// Test tasks
469
//////////////////////////////////////////////////////////////
470
 
471
task test_access_to_mac_reg;
472
  input  [31:0]  start_task;
473
  input  [31:0]  end_task;
474
  integer        bit_start_1;
475
  integer        bit_end_1;
476
  integer        bit_start_2;
477
  integer        bit_end_2;
478
  integer        num_of_reg;
479
  integer        i_addr;
480
  integer        i_data;
481
  integer        i_length;
482
  integer        tmp_data;
483
  reg    [31:0]  tx_bd_num;
484
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_data;
485
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_tmp_data;
486
  integer        i;
487
  integer        i1;
488
  integer        i2;
489
  integer        i3;
490
  integer        fail;
491 178 mohor
  integer        test_num;
492 169 mohor
  reg    [31:0]  addr;
493
  reg    [31:0]  data;
494
  reg    [31:0]  data_max;
495 116 mohor
begin
496 169 mohor
// ACCESS TO MAC REGISTERS TEST
497
test_heading("ACCESS TO MAC REGISTERS TEST");
498
$display(" ");
499
$display("ACCESS TO MAC REGISTERS TEST");
500
fail = 0;
501
 
502 192 tadej
// reset MAC registers
503
hard_reset;
504
// reset MAC and MII LOGIC with soft reset
505
reset_mac;
506
reset_mii;
507 169 mohor
 
508 192 tadej
 
509 178 mohor
//////////////////////////////////////////////////////////////////////
510
////                                                              ////
511
////  test_access_to_mac_reg:                                     ////
512
////                                                              ////
513
////  0: Walking 1 with single cycles across MAC regs.            ////
514
////  1: Walking 1 with single cycles across MAC buffer descript. ////
515
////  2: Test max reg. values and reg. values after writing       ////
516
////     inverse reset values and hard reset of the MAC           ////
517
////  3: Test buffer desc. RAM preserving values after hard reset ////
518
////     of the MAC and resetting the logic                       ////
519
////                                                              ////
520
//////////////////////////////////////////////////////////////////////
521 194 tadej
for (test_num = start_task; test_num <= end_task; test_num = test_num + 1)
522 169 mohor
begin
523
 
524 178 mohor
  ////////////////////////////////////////////////////////////////////
525
  ////                                                            ////
526
  ////  Walking 1 with single cycles across MAC regs.             ////
527
  ////                                                            ////
528
  ////////////////////////////////////////////////////////////////////
529
  if (test_num == 0) // Walking 1 with single cycles across MAC regs.
530 194 tadej
  begin
531
    // TEST 0: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )
532
    test_name   = "TEST 0: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )";
533
    `TIME; $display("  TEST 0: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )");
534 178 mohor
 
535 194 tadej
    data = 0;
536
    for (i = 0; i <= 4; i = i + 1) // for initial wait cycles on WB bus
537
      begin
538
        wbm_init_waits = i;
539
        wbm_subseq_waits = {$random} % 5; // it is not important for single accesses
540
        for (i_addr = 0; i_addr <= 32'h4C; i_addr = i_addr + 4) // register address
541
          begin
542
            addr = `ETH_BASE + i_addr;
543
            // set ranges of R/W bits
544
            case (addr)
545
              `ETH_MODER:
546
                begin
547
                  bit_start_1 = 0;
548
                  bit_end_1   = 16;
549
                  bit_start_2 = 32; // not used
550
                  bit_end_2   = 32; // not used
551
                end
552
              `ETH_INT: // READONLY - tested within INT test
553
                begin
554
                  bit_start_1 = 32; // not used
555
                  bit_end_1   = 32; // not used
556
                  bit_start_2 = 32; // not used
557
                  bit_end_2   = 32; // not used
558
                end
559
              `ETH_INT_MASK:
560
                begin
561
                  bit_start_1 = 0;
562
                  bit_end_1   = 6;
563
                  bit_start_2 = 32; // not used
564
                  bit_end_2   = 32; // not used
565
                end
566
              `ETH_IPGT:
567
                begin
568
                  bit_start_1 = 0;
569
                  bit_end_1   = 6;
570
                  bit_start_2 = 32; // not used
571
                  bit_end_2   = 32; // not used
572
                end
573
              `ETH_IPGR1:
574
                begin
575
                  bit_start_1 = 0;
576
                  bit_end_1   = 6;
577
                  bit_start_2 = 32; // not used
578
                  bit_end_2   = 32; // not used
579
                end
580
              `ETH_IPGR2:
581
                begin
582
                  bit_start_1 = 0;
583
                  bit_end_1   = 6;
584
                  bit_start_2 = 32; // not used
585
                  bit_end_2   = 32; // not used
586
                end
587
              `ETH_PACKETLEN:
588
                begin
589
                  bit_start_1 = 0;
590
                  bit_end_1   = 31;
591
                  bit_start_2 = 32; // not used
592
                  bit_end_2   = 32; // not used
593
                end
594
              `ETH_COLLCONF:
595
                begin
596
                  bit_start_1 = 0;
597
                  bit_end_1   = 5;
598
                  bit_start_2 = 16;
599
                  bit_end_2   = 19;
600
                end
601
              `ETH_TX_BD_NUM:
602
                begin
603
                  bit_start_1 = 0;
604
                  bit_end_1   = 7;
605
                  bit_start_2 = 32; // not used
606
                  bit_end_2   = 32; // not used
607
                end
608
              `ETH_CTRLMODER:
609
                begin
610
                  bit_start_1 = 0;
611
                  bit_end_1   = 2;
612
                  bit_start_2 = 32; // not used
613
                  bit_end_2   = 32; // not used
614
                end
615
              `ETH_MIIMODER:
616
                begin
617
                  bit_start_1 = 0;
618
                  bit_end_1   = 9;
619
                  bit_start_2 = 32; // not used
620
                  bit_end_2   = 32; // not used
621
                end
622
              `ETH_MIICOMMAND: // "WRITEONLY" - tested within MIIM test - 3 LSBits are not written here!!!
623
                begin
624
                  bit_start_1 = 32; // not used
625
                  bit_end_1   = 32; // not used
626
                  bit_start_2 = 32; // not used
627
                  bit_end_2   = 32; // not used
628
                end
629
              `ETH_MIIADDRESS:
630
                begin
631
                  bit_start_1 = 0;
632
                  bit_end_1   = 4;
633
                  bit_start_2 = 8;
634
                  bit_end_2   = 12;
635
                end
636
              `ETH_MIITX_DATA:
637
                begin
638
                  bit_start_1 = 0;
639
                  bit_end_1   = 15;
640
                  bit_start_2 = 32; // not used
641
                  bit_end_2   = 32; // not used
642
                end
643
              `ETH_MIIRX_DATA: // READONLY - tested within MIIM test
644
                begin
645
                  bit_start_1 = 32; // not used
646
                  bit_end_1   = 32; // not used
647
                  bit_start_2 = 32; // not used
648
                  bit_end_2   = 32; // not used
649
                end
650
              `ETH_MIISTATUS: // READONLY - tested within MIIM test
651
                begin
652
                  bit_start_1 = 32; // not used
653
                  bit_end_1   = 32; // not used
654
                  bit_start_2 = 32; // not used
655
                  bit_end_2   = 32; // not used
656
                end
657
              `ETH_MAC_ADDR0:
658
                begin
659
                  bit_start_1 = 0;
660
                  bit_end_1   = 31;
661
                  bit_start_2 = 32; // not used
662
                  bit_end_2   = 32; // not used
663 178 mohor
                  end
664 194 tadej
              `ETH_MAC_ADDR1:
665
                begin
666
                  bit_start_1 = 0;
667
                  bit_end_1   = 15;
668
                  bit_start_2 = 32; // not used
669
                  bit_end_2   = 32; // not used
670
                end
671
              `ETH_HASH_ADDR0:
672
                begin
673
                  bit_start_1 = 0;
674
                  bit_end_1   = 31;
675
                  bit_start_2 = 32; // not used
676
                  bit_end_2   = 32; // not used
677
                end
678
              default: // `ETH_HASH_ADDR1:
679
                begin
680
                  bit_start_1 = 0;
681
                  bit_end_1   = 31;
682
                  bit_start_2 = 32; // not used
683
                  bit_end_2   = 32; // not used
684
                end
685
            endcase
686
 
687
            for (i_data = 0; i_data <= 31; i_data = i_data + 1) // the position of walking one
688
              begin
689
                data = 1'b1 << i_data;
690
                if ( (addr == `ETH_MIICOMMAND) && (i_data <= 2) ) // DO NOT WRITE to 3 LSBits of MIICOMMAND !!!
691
                  ;
692
                else
693 178 mohor
                  begin
694 194 tadej
                    wbm_write(addr, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
695
                    wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
696
                    if ( ((i_data >= bit_start_1) && (i_data <= bit_end_1)) ||
697
                         ((i_data >= bit_start_2) && (i_data <= bit_end_2)) ) // data should be equal to tmp_data
698
                      begin
699
                        if (tmp_data !== data)
700 178 mohor
                        begin
701 194 tadej
                          fail = fail + 1;
702
                          test_fail("RW bit of the MAC register was not written or not read");
703
                          `TIME;
704
                          $display("wbm_init_waits %d, addr %h, data %h, tmp_data %h",
705
                                    wbm_init_waits, addr, data, tmp_data);
706
                        end
707
                      end
708
                    else // data should not be equal to tmp_data
709
                      begin
710
                        if (tmp_data === data)
711 178 mohor
                          begin
712
                            fail = fail + 1;
713 194 tadej
                            test_fail("NON RW bit of the MAC register was written, but it shouldn't be");
714 178 mohor
                            `TIME;
715 194 tadej
                            $display("wbm_init_waits %d, addr %h, data %h, tmp_data %h",
716 178 mohor
                                      wbm_init_waits, addr, data, tmp_data);
717
                          end
718 194 tadej
                      end
719
                  end
720
              end
721
          end
722
      end
723 209 tadejm
    // INTERMEDIATE DISPLAYS (The only one)
724
    $display("    ->buffer descriptors tested with 0, 1, 2, 3 and 4 bus delay cycles");
725 194 tadej
    if(fail == 0)
726
      test_ok;
727
    else
728
      fail = 0;    // Errors were reported previously
729
  end
730 178 mohor
 
731
 
732
  ////////////////////////////////////////////////////////////////////
733
  ////                                                            ////
734
  ////  Walking 1 with single cycles across MAC buffer descript.  ////
735
  ////                                                            ////
736
  ////////////////////////////////////////////////////////////////////
737
  if (test_num == 1) // Start Walking 1 with single cycles across MAC buffer descript.
738 169 mohor
  begin
739 194 tadej
    // TEST 1: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC BUFFER DESC. ( VARIOUS BUS DELAYS )
740
    test_name   = "TEST 1: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC BUFFER DESC. ( VARIOUS BUS DELAYS )";
741
    `TIME; $display("  TEST 1: 'WALKING ONE' WITH SINGLE CYCLES ACROSS MAC BUFFER DESC. ( VARIOUS BUS DELAYS )");
742 178 mohor
 
743
    data = 0;
744
    // set TX and RX buffer descriptors
745
    tx_bd_num = 32'h40;
746
    wbm_write(`ETH_TX_BD_NUM, tx_bd_num, 4'hF, 1, 0, 0);
747
    for (i = 0; i <= 4; i = i + 1) // for initial wait cycles on WB bus
748
    begin
749 169 mohor
      wbm_init_waits = i;
750
      wbm_subseq_waits = {$random} % 5; // it is not important for single accesses
751 178 mohor
      for (i_addr = 32'h400; i_addr <= 32'h7FC; i_addr = i_addr + 4) // buffer descriptor address
752 169 mohor
      begin
753
        addr = `ETH_BASE + i_addr;
754 178 mohor
        if (i_addr < (32'h400 + (tx_bd_num << 3))) // TX buffer descriptors
755
        begin
756
          // set ranges of R/W bits
757
          case (addr[3])
758
            1'b0: // buffer control bits
759
            begin
760
              bit_start_1 = 0;
761
              bit_end_1   = 31; // 8;
762
              bit_start_2 = 11;
763
              bit_end_2   = 31;
764
            end
765
            default: // 1'b1: // buffer pointer
766
            begin
767
              bit_start_1 = 0;
768
              bit_end_1   = 31;
769
              bit_start_2 = 32; // not used
770
              bit_end_2   = 32; // not used
771
            end
772
          endcase
773
        end
774
        else // RX buffer descriptors
775
        begin
776
          // set ranges of R/W bits
777
          case (addr[3])
778
            1'b0: // buffer control bits
779
            begin
780
              bit_start_1 = 0;
781
              bit_end_1   = 31; // 7;
782
              bit_start_2 = 13;
783
              bit_end_2   = 31;
784
            end
785
            default: // 1'b1: // buffer pointer
786
            begin
787
              bit_start_1 = 0;
788
              bit_end_1   = 31;
789
              bit_start_2 = 32; // not used
790
              bit_end_2   = 32; // not used
791
            end
792
          endcase
793
        end
794
 
795 169 mohor
        for (i_data = 0; i_data <= 31; i_data = i_data + 1) // the position of walking one
796
        begin
797
          data = 1'b1 << i_data;
798 178 mohor
          if ( (addr[3] == 0) && (i_data == 15) ) // DO NOT WRITE to this bit !!!
799
            ;
800 169 mohor
          else
801
          begin
802
            wbm_write(addr, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
803
            wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
804
            if ( ((i_data >= bit_start_1) && (i_data <= bit_end_1)) ||
805
                 ((i_data >= bit_start_2) && (i_data <= bit_end_2)) ) // data should be equal to tmp_data
806
            begin
807
              if (tmp_data !== data)
808
              begin
809
                fail = fail + 1;
810 178 mohor
                test_fail("RW bit of the MAC buffer descriptors was not written or not read");
811 169 mohor
                `TIME;
812
                $display("wbm_init_waits %d, addr %h, data %h, tmp_data %h",
813
                          wbm_init_waits, addr, data, tmp_data);
814
              end
815
            end
816
            else // data should not be equal to tmp_data
817
            begin
818
              if (tmp_data === data)
819
              begin
820
                fail = fail + 1;
821 178 mohor
                test_fail("NON RW bit of the MAC buffer descriptors was written, but it shouldn't be");
822 169 mohor
                `TIME;
823
                $display("wbm_init_waits %d, addr %h, data %h, tmp_data %h",
824
                          wbm_init_waits, addr, data, tmp_data);
825
              end
826
            end
827
          end
828
        end
829
      end
830 178 mohor
      // INTERMEDIATE DISPLAYS
831
      case (i)
832 209 tadejm
        0:       $display("    ->buffer descriptors tested with 0 bus delay");
833
        1:       $display("    ->buffer descriptors tested with 1 bus delay cycle");
834
        2:       $display("    ->buffer descriptors tested with 2 bus delay cycles");
835
        3:       $display("    ->buffer descriptors tested with 3 bus delay cycles");
836
        default: $display("    ->buffer descriptors tested with 4 bus delay cycles");
837 178 mohor
      endcase
838
    end
839
    if(fail == 0)
840
      test_ok;
841
    else
842
      fail = 0;
843 169 mohor
  end
844 178 mohor
 
845
 
846
  ////////////////////////////////////////////////////////////////////
847
  ////                                                            ////
848
  ////  Test max reg. values and reg. values after writing        ////
849
  ////  inverse reset values and hard reset of the MAC            ////
850
  ////                                                            ////
851
  ////////////////////////////////////////////////////////////////////
852
  if (test_num == 2) // Start this task
853 169 mohor
  begin
854 194 tadej
    // TEST 2: MAX REG. VALUES AND REG. VALUES AFTER WRITING INVERSE RESET VALUES AND HARD RESET OF THE MAC
855 178 mohor
    test_name   =
856 194 tadej
      "TEST 2: MAX REG. VALUES AND REG. VALUES AFTER WRITING INVERSE RESET VALUES AND HARD RESET OF THE MAC";
857 178 mohor
    `TIME; $display(
858 194 tadej
      "  TEST 2: MAX REG. VALUES AND REG. VALUES AFTER WRITING INVERSE RESET VALUES AND HARD RESET OF THE MAC");
859 178 mohor
 
860
    // reset MAC registers
861
    hard_reset;
862
    for (i = 0; i <= 4; i = i + 1) // 0, 2 - WRITE; 1, 3, 4 - READ
863 169 mohor
    begin
864 178 mohor
      for (i_addr = 0; i_addr <= 32'h4C; i_addr = i_addr + 4) // register address
865 169 mohor
      begin
866 178 mohor
        addr = `ETH_BASE + i_addr;
867
        // set ranges of R/W bits
868
        case (addr)
869
          `ETH_MODER:
870 169 mohor
          begin
871 178 mohor
            data = 32'h0000_A800;
872
            data_max = 32'h0001_FFFF;
873 169 mohor
          end
874 178 mohor
          `ETH_INT: // READONLY - tested within INT test
875 169 mohor
          begin
876 178 mohor
            data = 32'h0000_0000;
877
            data_max = 32'h0000_0000;
878 169 mohor
          end
879
          `ETH_INT_MASK:
880 178 mohor
          begin
881
            data = 32'h0000_0000;
882
            data_max = 32'h0000_007F;
883
          end
884 169 mohor
          `ETH_IPGT:
885 178 mohor
          begin
886
            data = 32'h0000_0012;
887
            data_max = 32'h0000_007F;
888
          end
889 169 mohor
          `ETH_IPGR1:
890 178 mohor
          begin
891
            data = 32'h0000_000C;
892
            data_max = 32'h0000_007F;
893
          end
894 169 mohor
          `ETH_IPGR2:
895 178 mohor
          begin
896
            data = 32'h0000_0012;
897
            data_max = 32'h0000_007F;
898
          end
899 169 mohor
          `ETH_PACKETLEN:
900 178 mohor
          begin
901
            data = 32'h0040_0600;
902
            data_max = 32'hFFFF_FFFF;
903
          end
904 169 mohor
          `ETH_COLLCONF:
905 178 mohor
          begin
906
            data = 32'h000F_003F;
907
            data_max = 32'h000F_003F;
908
          end
909 169 mohor
          `ETH_TX_BD_NUM:
910 178 mohor
          begin
911
            data = 32'h0000_0040;
912
            data_max = 32'h0000_0080;
913
          end
914 169 mohor
          `ETH_CTRLMODER:
915 178 mohor
          begin
916
            data = 32'h0000_0000;
917
            data_max = 32'h0000_0007;
918
          end
919 169 mohor
          `ETH_MIIMODER:
920 178 mohor
          begin
921
            data = 32'h0000_0064;
922
            data_max = 32'h0000_03FF;
923
          end
924 169 mohor
          `ETH_MIICOMMAND: // "WRITEONLY" - tested within MIIM test - 3 LSBits are not written here!!!
925 178 mohor
          begin
926
            data = 32'h0000_0000;
927
            data_max = 32'h0000_0007;
928
          end
929 169 mohor
          `ETH_MIIADDRESS:
930 178 mohor
          begin
931
            data = 32'h0000_0000;
932
            data_max = 32'h0000_1F1F;
933
          end
934 169 mohor
          `ETH_MIITX_DATA:
935 178 mohor
          begin
936
            data = 32'h0000_0000;
937
            data_max = 32'h0000_FFFF;
938
          end
939 169 mohor
          `ETH_MIIRX_DATA: // READONLY - tested within MIIM test
940
          begin
941 178 mohor
            data = 32'h0000_0000;
942
            data_max = 32'h0000_0000;
943 169 mohor
          end
944 178 mohor
          `ETH_MIISTATUS: // READONLY - tested within MIIM test
945 169 mohor
          begin
946 178 mohor
            data = 32'h0000_0000;
947
            data_max = 32'h0000_0000;
948 169 mohor
          end
949 178 mohor
          `ETH_MAC_ADDR0:
950 169 mohor
          begin
951 178 mohor
            data = 32'h0000_0000;
952
            data_max = 32'hFFFF_FFFF;
953 169 mohor
          end
954 178 mohor
          `ETH_MAC_ADDR1:
955 169 mohor
          begin
956 178 mohor
            data = 32'h0000_0000;
957
            data_max = 32'h0000_FFFF;
958 169 mohor
          end
959 178 mohor
          `ETH_HASH_ADDR0:
960 169 mohor
          begin
961 178 mohor
            data = 32'h0000_0000;
962
            data_max = 32'hFFFF_FFFF;
963 169 mohor
          end
964 178 mohor
          default: // `ETH_HASH_ADDR1:
965 169 mohor
          begin
966 178 mohor
            data = 32'h0000_0000;
967
            data_max = 32'hFFFF_FFFF;
968 169 mohor
          end
969
        endcase
970 178 mohor
 
971
        wbm_init_waits = {$random} % 3;
972
        wbm_subseq_waits = {$random} % 5; // it is not important for single accesses
973
        if (i == 0)
974
          wbm_write(addr, ~data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
975
        else if (i == 2)
976
          wbm_write(addr, 32'hFFFFFFFF, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
977
        else if ((i == 1) || (i == 4))
978 169 mohor
        begin
979 178 mohor
          wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
980
          if (tmp_data !== data)
981
          begin
982
            fail = fail + 1;
983
            test_fail("RESET value of the MAC register is not correct");
984
            `TIME;
985
            $display("  addr %h, data %h, tmp_data %h", addr, data, tmp_data);
986
          end
987 169 mohor
        end
988 178 mohor
        else // check maximum values
989 169 mohor
        begin
990
          wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
991 178 mohor
          if (addr == `ETH_TX_BD_NUM) // previous data should remain in this register
992 169 mohor
          begin
993
            if (tmp_data !== data)
994
            begin
995
              fail = fail + 1;
996 178 mohor
              test_fail("Previous value of the TX_BD_NUM register did not remain");
997 169 mohor
              `TIME;
998 178 mohor
              $display("  addr %h, data_max %h, tmp_data %h", addr, data_max, tmp_data);
999 169 mohor
            end
1000 178 mohor
            // try maximum (80)
1001
            wbm_write(addr, data_max, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1002
            wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1003
            if (tmp_data !== data_max)
1004
            begin
1005
              fail = fail + 1;
1006
              test_fail("MAX value of the TX_BD_NUM register is not correct");
1007
              `TIME;
1008
              $display("  addr %h, data_max %h, tmp_data %h", addr, data_max, tmp_data);
1009
            end
1010
            // try one less than maximum (80)
1011
            wbm_write(addr, (data_max - 1), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1012
            wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1013
            if (tmp_data !== (data_max - 1))
1014
            begin
1015
              fail = fail + 1;
1016
              test_fail("ONE less than MAX value of the TX_BD_NUM register is not correct");
1017
              `TIME;
1018
              $display("  addr %h, data_max %h, tmp_data %h", addr, data_max, tmp_data);
1019
            end
1020
            // try one more than maximum (80)
1021
            wbm_write(addr, (data_max + 1), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1022
            wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1023
            if (tmp_data !== (data_max - 1)) // previous data should remain in this register
1024
            begin
1025
              fail = fail + 1;
1026
              test_fail("Previous value of the TX_BD_NUM register did not remain");
1027
              `TIME;
1028
              $display("  addr %h, data_max %h, tmp_data %h", addr, data_max, tmp_data);
1029
            end
1030 169 mohor
          end
1031 178 mohor
          else
1032 169 mohor
          begin
1033 178 mohor
            if (tmp_data !== data_max)
1034 169 mohor
            begin
1035
              fail = fail + 1;
1036 178 mohor
              test_fail("MAX value of the MAC register is not correct");
1037 169 mohor
              `TIME;
1038 178 mohor
              $display("  addr %h, data_max %h, tmp_data %h", addr, data_max, tmp_data);
1039 169 mohor
            end
1040
          end
1041
        end
1042
      end
1043 178 mohor
      // reset MAC registers
1044
      if ((i == 0) || (i == 3))
1045
        hard_reset;
1046 169 mohor
    end
1047 178 mohor
    if(fail == 0)
1048
      test_ok;
1049
    else
1050
      fail = 0;
1051 169 mohor
  end
1052 116 mohor
 
1053 156 mohor
 
1054 181 mohor
  ////////////////////////////////////////////////////////////////////
1055
  ////                                                            ////
1056
  ////  Test buffer desc. ram preserving values after hard reset  ////
1057
  ////  of the mac and reseting the logic                         ////
1058
  ////                                                            ////
1059
  ////////////////////////////////////////////////////////////////////
1060 178 mohor
  if (test_num == 3) // Start this task
1061 169 mohor
  begin
1062 194 tadej
    // TEST 3: BUFFER DESC. RAM PRESERVING VALUES AFTER HARD RESET OF THE MAC AND RESETING THE LOGIC
1063
    test_name   = "TEST 3: BUFFER DESC. RAM PRESERVING VALUES AFTER HARD RESET OF THE MAC AND RESETING THE LOGIC";
1064 178 mohor
    `TIME;
1065 194 tadej
    $display("  TEST 3: BUFFER DESC. RAM PRESERVING VALUES AFTER HARD RESET OF THE MAC AND RESETING THE LOGIC");
1066 178 mohor
 
1067
    // reset MAC registers
1068
    hard_reset;
1069
    // reset LOGIC with soft reset
1070
    reset_mac;
1071
    reset_mii;
1072
    for (i = 0; i <= 3; i = i + 1) // 0, 2 - WRITE; 1, 3 - READ
1073 169 mohor
    begin
1074 178 mohor
      for (i_addr = 32'h400; i_addr <= 32'h7FC; i_addr = i_addr + 4) // buffer descriptor address
1075 169 mohor
      begin
1076 178 mohor
        addr = `ETH_BASE + i_addr;
1077
 
1078
        wbm_init_waits = {$random} % 3;
1079
        wbm_subseq_waits = {$random} % 5; // it is not important for single accesses
1080
        if (i == 0)
1081 169 mohor
        begin
1082 178 mohor
          data = 32'hFFFFFFFF;
1083
          wbm_write(addr, 32'hFFFFFFFF, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1084 169 mohor
        end
1085 178 mohor
        else if (i == 2)
1086 169 mohor
        begin
1087 178 mohor
          data = 32'h00000000;
1088
          wbm_write(addr, 32'h00000000, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1089 169 mohor
        end
1090
        else
1091
        begin
1092 178 mohor
          wbm_read(addr, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1093
          if (tmp_data !== data)
1094 169 mohor
          begin
1095
            fail = fail + 1;
1096 178 mohor
            test_fail("PRESERVED value of the MAC buffer descriptors is not correct");
1097 169 mohor
            `TIME;
1098 178 mohor
            $display("  addr %h, data %h, tmp_data %h", addr, data, tmp_data);
1099 169 mohor
          end
1100
        end
1101
      end
1102 178 mohor
      if ((i == 0) || (i == 2))
1103
      begin
1104
        // reset MAC registers
1105
        hard_reset;
1106
        // reset LOGIC with soft reset
1107
        reset_mac;
1108
        reset_mii;
1109
      end
1110 169 mohor
    end
1111 178 mohor
    if(fail == 0)
1112
      test_ok;
1113
    else
1114
    fail = 0;
1115 169 mohor
  end
1116 116 mohor
 
1117
 
1118 178 mohor
  if (test_num == 4) // Start this task
1119 169 mohor
  begin
1120 194 tadej
        /*  // TEST 4: 'WALKING ONE' WITH BURST CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )
1121
          test_name   = "TEST 4: 'WALKING ONE' WITH BURST CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )";
1122
          `TIME; $display("  TEST 4: 'WALKING ONE' WITH BURST CYCLES ACROSS MAC REGISTERS ( VARIOUS BUS DELAYS )");
1123 178 mohor
 
1124
          data = 0;
1125
          burst_data = 0;
1126
          burst_tmp_data = 0;
1127
          i_length = 10; // two bursts for length 20
1128
          for (i = 0; i <= 4; i = i + 1) // for initial wait cycles on WB bus
1129
          begin
1130
            for (i1 = 0; i1 <= 4; i1 = i1 + 1) // for initial wait cycles on WB bus
1131
            begin
1132
              wbm_init_waits = i;
1133
              wbm_subseq_waits = i1;
1134
              #1;
1135
              for (i_data = 0; i_data <= 31; i_data = i_data + 1) // the position of walking one
1136
              begin
1137
                data = 1'b1 << i_data;
1138
                #1;
1139
                for (i2 = 32'h4C; i2 >= 0; i2 = i2 - 4)
1140
                begin
1141
                  burst_data = burst_data << 32;
1142
                  // DO NOT WRITE to 3 LSBits of MIICOMMAND !!!
1143
                  if ( ((`ETH_BASE + i2) == `ETH_MIICOMMAND) && (i_data <= 2) )
1144
                  begin
1145
                    #1 burst_data[31:0] = 0;
1146
                  end
1147
                  else
1148
                  begin
1149
                    #1 burst_data[31:0] = data;
1150
                  end
1151
                end
1152
                #1;
1153
                // 2 burst writes
1154
                addr = `ETH_BASE; // address of a first burst
1155
                wbm_write(addr, burst_data[(32 * 10 - 1):0], 4'hF, i_length, wbm_init_waits, wbm_subseq_waits);
1156
                burst_tmp_data = burst_data >> (32 * i_length);
1157
                addr = addr + 32'h28; // address of a second burst
1158
                wbm_write(addr, burst_tmp_data[(32 * 10 - 1):0], 4'hF, i_length, wbm_init_waits, wbm_subseq_waits);
1159
                #1;
1160
                // 2 burst reads
1161
                addr = `ETH_BASE; // address of a first burst
1162
                wbm_read(addr, burst_tmp_data[(32 * 10 - 1):0], 4'hF, i_length,
1163
                         wbm_init_waits, wbm_subseq_waits); // first burst
1164
                burst_tmp_data = burst_tmp_data << (32 * i_length);
1165
                addr = addr + 32'h28; // address of a second burst
1166
                wbm_read(addr, burst_tmp_data[(32 * 10 - 1):0], 4'hF, i_length,
1167
                         wbm_init_waits, wbm_subseq_waits); // second burst
1168
                #1;
1169
                for (i2 = 0; i2 <= 32'h4C; i2 = i2 + 4)
1170
                begin
1171
                  // set ranges of R/W bits
1172
                  case (`ETH_BASE + i2)
1173
                  `ETH_MODER:
1174
                    begin
1175
                      bit_start_1 = 0;
1176
                      bit_end_1   = 16;
1177
                      bit_start_2 = 32; // not used
1178
                      bit_end_2   = 32; // not used
1179
                    end
1180
                  `ETH_INT: // READONLY - tested within INT test
1181
                    begin
1182
                      bit_start_1 = 32; // not used
1183
                      bit_end_1   = 32; // not used
1184
                      bit_start_2 = 32; // not used
1185
                      bit_end_2   = 32; // not used
1186
                    end
1187
                  `ETH_INT_MASK:
1188
                    begin
1189
                      bit_start_1 = 0;
1190
                      bit_end_1   = 6;
1191
                      bit_start_2 = 32; // not used
1192
                      bit_end_2   = 32; // not used
1193
                    end
1194
                  `ETH_IPGT:
1195
                    begin
1196
                      bit_start_1 = 0;
1197
                      bit_end_1   = 6;
1198
                      bit_start_2 = 32; // not used
1199
                      bit_end_2   = 32; // not used
1200
                    end
1201
                  `ETH_IPGR1:
1202
                    begin
1203
                      bit_start_1 = 0;
1204
                      bit_end_1   = 6;
1205
                      bit_start_2 = 32; // not used
1206
                      bit_end_2   = 32; // not used
1207
                    end
1208
                  `ETH_IPGR2:
1209
                    begin
1210
                      bit_start_1 = 0;
1211
                      bit_end_1   = 6;
1212
                      bit_start_2 = 32; // not used
1213
                      bit_end_2   = 32; // not used
1214
                    end
1215
                  `ETH_PACKETLEN:
1216
                    begin
1217
                      bit_start_1 = 0;
1218
                      bit_end_1   = 31;
1219
                      bit_start_2 = 32; // not used
1220
                      bit_end_2   = 32; // not used
1221
                    end
1222
                  `ETH_COLLCONF:
1223
                    begin
1224
                      bit_start_1 = 0;
1225
                      bit_end_1   = 5;
1226
                      bit_start_2 = 16;
1227
                      bit_end_2   = 19;
1228
                    end
1229
                  `ETH_TX_BD_NUM:
1230
                    begin
1231
                      bit_start_1 = 0;
1232
                      bit_end_1   = 7;
1233
                      bit_start_2 = 32; // not used
1234
                      bit_end_2   = 32; // not used
1235
                    end
1236
                  `ETH_CTRLMODER:
1237
                    begin
1238
                      bit_start_1 = 0;
1239
                      bit_end_1   = 2;
1240
                      bit_start_2 = 32; // not used
1241
                      bit_end_2   = 32; // not used
1242
                    end
1243
                  `ETH_MIIMODER:
1244
                    begin
1245
                      bit_start_1 = 0;
1246
                      bit_end_1   = 9;
1247
                      bit_start_2 = 32; // not used
1248
                      bit_end_2   = 32; // not used
1249
                    end
1250
                  `ETH_MIICOMMAND: // "WRITEONLY" - tested within MIIM test - 3 LSBits are not written here!!!
1251
                    begin
1252
                      bit_start_1 = 32; // not used
1253
                      bit_end_1   = 32; // not used
1254
                      bit_start_2 = 32; // not used
1255
                      bit_end_2   = 32; // not used
1256
                    end
1257
                  `ETH_MIIADDRESS:
1258
                    begin
1259
                      bit_start_1 = 0;
1260
                      bit_end_1   = 4;
1261
                      bit_start_2 = 8;
1262
                      bit_end_2   = 12;
1263
                    end
1264
                  `ETH_MIITX_DATA:
1265
                    begin
1266
                      bit_start_1 = 0;
1267
                      bit_end_1   = 15;
1268
                      bit_start_2 = 32; // not used
1269
                      bit_end_2   = 32; // not used
1270
                    end
1271
                  `ETH_MIIRX_DATA: // READONLY - tested within MIIM test
1272
                    begin
1273
                      bit_start_1 = 32; // not used
1274
                      bit_end_1   = 32; // not used
1275
                      bit_start_2 = 32; // not used
1276
                      bit_end_2   = 32; // not used
1277
                    end
1278
                  `ETH_MIISTATUS: // READONLY - tested within MIIM test
1279
                    begin
1280
                      bit_start_1 = 32; // not used
1281
                      bit_end_1   = 32; // not used
1282
                      bit_start_2 = 32; // not used
1283
                      bit_end_2   = 32; // not used
1284
                    end
1285
                  `ETH_MAC_ADDR0:
1286
                    begin
1287
                      bit_start_1 = 0;
1288
                      bit_end_1   = 31;
1289
                      bit_start_2 = 32; // not used
1290
                      bit_end_2   = 32; // not used
1291
                    end
1292
                  `ETH_MAC_ADDR1:
1293
                    begin
1294
                      bit_start_1 = 0;
1295
                      bit_end_1   = 15;
1296
                      bit_start_2 = 32; // not used
1297
                      bit_end_2   = 32; // not used
1298
                    end
1299
                  `ETH_HASH_ADDR0:
1300
                    begin
1301
                      bit_start_1 = 0;
1302
                      bit_end_1   = 31;
1303
                      bit_start_2 = 32; // not used
1304
                      bit_end_2   = 32; // not used
1305
                    end
1306
                  default: // `ETH_HASH_ADDR1:
1307
                    begin
1308
                      bit_start_1 = 0;
1309
                      bit_end_1   = 31;
1310
                      bit_start_2 = 32; // not used
1311
                      bit_end_2   = 32; // not used
1312
                    end
1313
                  endcase
1314
                  #1;
1315
                  // 3 LSBits of MIICOMMAND are NOT written !!!
1316
                  if ( ((`ETH_BASE + i2) == `ETH_MIICOMMAND) && (i_data <= 2) )
1317
                  begin
1318
                    if (burst_tmp_data[31:0] !== burst_data[31:0])
1319
                    begin
1320
                      fail = fail + 1;
1321
                      test_fail("NON WR bit of the MAC MIICOMMAND register was wrong written or read");
1322
                      `TIME;
1323
                      $display("wbm_init_waits %d, wbm_subseq_waits %d, addr %h, data %h, tmp_data %h",
1324
                                wbm_init_waits, wbm_subseq_waits, i2, burst_data[31:0], burst_tmp_data[31:0]);
1325
                    end
1326
                  end
1327
                  else
1328
                  begin
1329
                    if ( ((i_data >= bit_start_1) && (i_data <= bit_end_1)) ||
1330
                         ((i_data >= bit_start_2) && (i_data <= bit_end_2)) ) // data should be equal to tmp_data
1331
                    begin
1332
                      if (burst_tmp_data[31:0] !== burst_data[31:0])
1333
                      begin
1334
                        fail = fail + 1;
1335
                        test_fail("RW bit of the MAC register was not written or not read");
1336
                        `TIME;
1337
                        $display("wbm_init_waits %d, wbm_subseq_waits %d, addr %h, data %h, tmp_data %h",
1338
                                  wbm_init_waits, wbm_subseq_waits, i2, burst_data[31:0], burst_tmp_data[31:0]);
1339
                      end
1340
                    end
1341
                    else // data should not be equal to tmp_data
1342
                    begin
1343
                      if (burst_tmp_data[31:0] === burst_data[31:0])
1344
                      begin
1345
                        fail = fail + 1;
1346
                        test_fail("NON RW bit of the MAC register was written, but it shouldn't be");
1347
                        `TIME;
1348
                        $display("wbm_init_waits %d, wbm_subseq_waits %d, addr %h, data %h, tmp_data %h",
1349
                                  wbm_init_waits, wbm_subseq_waits, i2, burst_data[31:0], burst_tmp_data[31:0]);
1350
                      end
1351
                    end
1352
                  end
1353
                  burst_tmp_data = burst_tmp_data >> 32;
1354
                  burst_data = burst_data >> 32;
1355
                end
1356
              end
1357
            end
1358
          end
1359
          if(fail == 0)
1360
            test_ok;
1361
          else
1362
            fail = 0;*/
1363
  end
1364 116 mohor
 
1365 169 mohor
end
1366 156 mohor
 
1367 169 mohor
end
1368
endtask // test_access_to_mac_reg
1369 156 mohor
 
1370
 
1371 169 mohor
task test_mii;
1372
  input  [31:0]  start_task;
1373
  input  [31:0]  end_task;
1374
  integer        i;
1375
  integer        i1;
1376
  integer        i2;
1377
  integer        i3;
1378
  integer        cnt;
1379
  integer        fail;
1380 181 mohor
  integer        test_num;
1381 169 mohor
  reg     [8:0]  clk_div; // only 8 bits are valid!
1382
  reg     [4:0]  phy_addr;
1383
  reg     [4:0]  reg_addr;
1384
  reg     [15:0] phy_data;
1385
  reg     [15:0] tmp_data;
1386
begin
1387
// MIIM MODULE TEST
1388
test_heading("MIIM MODULE TEST");
1389
$display(" ");
1390
$display("MIIM MODULE TEST");
1391
fail = 0;
1392 156 mohor
 
1393 192 tadej
// reset MAC registers
1394
hard_reset;
1395
// reset MAC and MII LOGIC with soft reset
1396
reset_mac;
1397 169 mohor
reset_mii;
1398 116 mohor
 
1399 194 tadej
 
1400 181 mohor
//////////////////////////////////////////////////////////////////////
1401
////                                                              ////
1402
////  test_mii:                                                   ////
1403
////                                                              ////
1404
////  0:  Test clock divider of mii management module with all    ////
1405
////      possible frequences.                                    ////
1406
////  1:  Test various readings from 'real' phy registers.        ////
1407
////  2:  Test various writings to 'real' phy registers (control  ////
1408
////      and non writable registers)                             ////
1409
////  3:  Test reset phy through mii management module            ////
1410
////  4:  Test 'walking one' across phy address (with and without ////
1411
////      preamble)                                               ////
1412
////  5:  Test 'walking one' across phy's register address (with  ////
1413
////      and without preamble)                                   ////
1414
////  6:  Test 'walking one' across phy's data (with and without  ////
1415
////      preamble)                                               ////
1416
////  7:  Test reading from phy with wrong phy address (host      ////
1417
////      reading high 'z' data)                                  ////
1418
////  8:  Test writing to phy with wrong phy address and reading  ////
1419
////      from correct one                                        ////
1420
////  9:  Test sliding stop scan command immediately after read   ////
1421
////      request (with and without preamble)                     ////
1422
//// 10:  Test sliding stop scan command immediately after write  ////
1423
////      request (with and without preamble)                     ////
1424
//// 11:  Test busy and nvalid status durations during write      ////
1425
////      (with and without preamble)                             ////
1426
//// 12:  Test busy and nvalid status durations during write      ////
1427
////      (with and without preamble)                             ////
1428
//// 13:  Test busy and nvalid status durations during scan (with ////
1429
////      and without preamble)                                   ////
1430
//// 14:  Test scan status from phy with detecting link-fail bit  ////
1431
////      (with and without preamble)                             ////
1432
//// 15:  Test scan status from phy with sliding link-fail bit    ////
1433
////      (with and without preamble)                             ////
1434
//// 16:  Test sliding stop scan command immediately after scan   ////
1435
////      request (with and without preamble)                     ////
1436
//// 17:  Test sliding stop scan command after 2. scan (with and  ////
1437
////      without preamble)                                       ////
1438
////                                                              ////
1439
//////////////////////////////////////////////////////////////////////
1440 194 tadej
for (test_num = start_task; test_num <= end_task; test_num = test_num + 1)
1441 169 mohor
begin
1442 194 tadej
 
1443 181 mohor
  ////////////////////////////////////////////////////////////////////
1444
  ////                                                            ////
1445
  ////  Test clock divider of mii management module with all      ////
1446
  ////  possible frequences.                                      ////
1447
  ////                                                            ////
1448
  ////////////////////////////////////////////////////////////////////
1449
  if (test_num == 0) // Test clock divider of mii management module with all possible frequences.
1450 169 mohor
  begin
1451 194 tadej
    // TEST 0: CLOCK DIVIDER OF MII MANAGEMENT MODULE WITH ALL POSSIBLE FREQUENCES
1452
    test_name   = "TEST 0: CLOCK DIVIDER OF MII MANAGEMENT MODULE WITH ALL POSSIBLE FREQUENCES";
1453
    `TIME; $display("  TEST 0: CLOCK DIVIDER OF MII MANAGEMENT MODULE WITH ALL POSSIBLE FREQUENCES");
1454 181 mohor
 
1455
    wait(Mdc_O); // wait for MII clock to be 1
1456
    for(clk_div = 0; clk_div <= 255; clk_div = clk_div + 1)
1457
    begin
1458
      i1 = 0;
1459
      i2 = 0;
1460
      #Tp mii_set_clk_div(clk_div[7:0]);
1461
      @(posedge Mdc_O);
1462
      #Tp;
1463
      fork
1464 169 mohor
        begin
1465 181 mohor
          @(posedge Mdc_O);
1466 169 mohor
          #Tp;
1467 181 mohor
          disable count_i1;
1468
          disable count_i2;
1469 169 mohor
        end
1470 181 mohor
        begin: count_i1
1471
          forever
1472
          begin
1473
            @(posedge wb_clk);
1474
            i1 = i1 + 1;
1475
            #Tp;
1476
          end
1477
        end
1478
        begin: count_i2
1479
          forever
1480
          begin
1481
            @(negedge wb_clk);
1482
            i2 = i2 + 1;
1483
            #Tp;
1484
          end
1485
        end
1486
      join
1487
      if((clk_div[7:0] == 0) || (clk_div[7:0] == 1) || (clk_div[7:0] == 2) || (clk_div[7:0] == 3))
1488
      begin
1489
        if((i1 == i2) && (i1 == 2))
1490 169 mohor
        begin
1491
        end
1492 181 mohor
        else
1493
        begin
1494
          fail = fail + 1;
1495 209 tadejm
          test_fail("Clock divider of MII module did'nt divide frequency corectly (it should divide by 2)");
1496 181 mohor
        end
1497 169 mohor
      end
1498
      else
1499
      begin
1500 181 mohor
        if((i1 == i2) && (i1 == {clk_div[7:1], 1'b0}))
1501
        begin
1502
        end
1503
        else
1504
        begin
1505
          fail = fail + 1;
1506
          test_fail("Clock divider of MII module did'nt divide frequency corectly");
1507
        end
1508 169 mohor
      end
1509
    end
1510 181 mohor
    if(fail == 0)
1511
      test_ok;
1512 169 mohor
    else
1513 181 mohor
      fail = 0;
1514
  end
1515
 
1516
 
1517
  ////////////////////////////////////////////////////////////////////
1518
  ////                                                            ////
1519
  ////  Test various readings from 'real' phy registers.          ////
1520
  ////                                                            ////
1521
  ////////////////////////////////////////////////////////////////////
1522
  if (test_num == 1) // Test various readings from 'real' phy registers.
1523
  begin
1524 194 tadej
    // TEST 1: VARIOUS READINGS FROM 'REAL' PHY REGISTERS
1525
    test_name   = "TEST 1: VARIOUS READINGS FROM 'REAL' PHY REGISTERS";
1526
    `TIME; $display("  TEST 1: VARIOUS READINGS FROM 'REAL' PHY REGISTERS");
1527 181 mohor
 
1528
    // set the fastest possible MII
1529
    clk_div = 0;
1530
    mii_set_clk_div(clk_div[7:0]);
1531
    // set address
1532
    reg_addr = 5'h1F;
1533
    phy_addr = 5'h1;
1534
    while(reg_addr >= 5'h4)
1535 169 mohor
    begin
1536 181 mohor
      // read request
1537
      #Tp mii_read_req(phy_addr, reg_addr);
1538
      check_mii_busy; // wait for read to finish
1539
      // read data
1540
      #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1541
      if (phy_data !== 16'hDEAD)
1542 169 mohor
      begin
1543 181 mohor
        test_fail("Wrong data was read from PHY from 'not used' address space");
1544
        fail = fail + 1;
1545 169 mohor
      end
1546 181 mohor
      if (reg_addr == 5'h4) // go out of for loop
1547
        reg_addr = 5'h3;
1548 169 mohor
      else
1549 181 mohor
        reg_addr = reg_addr - 5'h9;
1550 169 mohor
    end
1551 181 mohor
 
1552
    // set address
1553
    reg_addr = 5'h3;
1554
    // read request
1555
    #Tp mii_read_req(phy_addr, reg_addr);
1556
    check_mii_busy; // wait for read to finish
1557
    // read data
1558
    #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1559
    if (phy_data !== {`PHY_ID2, `MAN_MODEL_NUM, `MAN_REVISION_NUM})
1560
    begin
1561
      test_fail("Wrong data was read from PHY from ID register 2");
1562
      fail = fail + 1;
1563
    end
1564
    if(fail == 0)
1565
      test_ok;
1566
    else
1567
      fail = 0;
1568 169 mohor
  end
1569 116 mohor
 
1570
 
1571 181 mohor
  ////////////////////////////////////////////////////////////////////
1572
  ////                                                            ////
1573
  ////  Test various writings to 'real' phy registers (control    ////
1574
  ////  and non writable registers)                               ////
1575
  ////                                                            ////
1576
  ////////////////////////////////////////////////////////////////////
1577
  if (test_num == 2) // 
1578 169 mohor
  begin
1579 194 tadej
    // TEST 2: VARIOUS WRITINGS TO 'REAL' PHY REGISTERS ( CONTROL AND NON WRITABLE REGISTERS )
1580
    test_name   = "TEST 2: VARIOUS WRITINGS TO 'REAL' PHY REGISTERS ( CONTROL AND NON WRITABLE REGISTERS )";
1581
    `TIME; $display("  TEST 2: VARIOUS WRITINGS TO 'REAL' PHY REGISTERS ( CONTROL AND NON WRITABLE REGISTERS )");
1582 181 mohor
 
1583
    // negate data and try to write into unwritable register
1584
    tmp_data = ~phy_data;
1585
    // write request
1586
    #Tp mii_write_req(phy_addr, reg_addr, tmp_data);
1587
    check_mii_busy; // wait for write to finish
1588 169 mohor
    // read request
1589
    #Tp mii_read_req(phy_addr, reg_addr);
1590
    check_mii_busy; // wait for read to finish
1591
    // read data
1592 181 mohor
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1593
    if (tmp_data !== phy_data)
1594
    begin
1595
      test_fail("Data was written into unwritable PHY register - ID register 2");
1596
      fail = fail + 1;
1597
    end
1598
 
1599
    // set address
1600
    reg_addr = 5'h0; // control register
1601
    // read request
1602
    #Tp mii_read_req(phy_addr, reg_addr);
1603
    check_mii_busy; // wait for read to finish
1604
    // read data
1605
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1606
    // write request
1607
    phy_data = 16'h7DFF; // bit 15 (RESET bit) and bit 9 are self clearing bits
1608
    #Tp mii_write_req(phy_addr, reg_addr, phy_data);
1609
    check_mii_busy; // wait for write to finish
1610
    // read request
1611
    #Tp mii_read_req(phy_addr, reg_addr);
1612
    check_mii_busy; // wait for read to finish
1613
    // read data
1614 169 mohor
    #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1615 181 mohor
    if (phy_data !== 16'h7DFF)
1616 169 mohor
    begin
1617 181 mohor
      test_fail("Data was not correctly written into OR read from writable PHY register - control register");
1618 169 mohor
      fail = fail + 1;
1619
    end
1620 181 mohor
    // write request
1621
    #Tp mii_write_req(phy_addr, reg_addr, tmp_data);
1622
    check_mii_busy; // wait for write to finish
1623
    // read request
1624
    #Tp mii_read_req(phy_addr, reg_addr);
1625
    check_mii_busy; // wait for read to finish
1626
    // read data
1627
    #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1628
    if (phy_data !== tmp_data)
1629
    begin
1630
      test_fail("Data was not correctly written into OR read from writable PHY register - control register");
1631
      fail = fail + 1;
1632
    end
1633
    if(fail == 0)
1634
      test_ok;
1635 116 mohor
    else
1636 181 mohor
      fail = 0;
1637 169 mohor
  end
1638 116 mohor
 
1639
 
1640 181 mohor
  ////////////////////////////////////////////////////////////////////
1641
  ////                                                            ////
1642
  ////  Test reset phy through mii management module              ////
1643
  ////                                                            ////
1644
  ////////////////////////////////////////////////////////////////////
1645
  if (test_num == 3) // 
1646 169 mohor
  begin
1647 194 tadej
    // TEST 3: RESET PHY THROUGH MII MANAGEMENT MODULE
1648
    test_name   = "TEST 3: RESET PHY THROUGH MII MANAGEMENT MODULE";
1649
    `TIME; $display("  TEST 3: RESET PHY THROUGH MII MANAGEMENT MODULE");
1650 181 mohor
 
1651
    // set address
1652
    reg_addr = 5'h0; // control register
1653
    // write request
1654
    phy_data = 16'h7DFF; // bit 15 (RESET bit) and bit 9 are self clearing bits
1655
    #Tp mii_write_req(phy_addr, reg_addr, phy_data);
1656
    check_mii_busy; // wait for write to finish
1657
    // read request
1658
    #Tp mii_read_req(phy_addr, reg_addr);
1659
    check_mii_busy; // wait for read to finish
1660
    // read data
1661
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1662
    if (phy_data !== tmp_data)
1663
    begin
1664
      test_fail("Data was not correctly written into OR read from writable PHY register - control register");
1665
      fail = fail + 1;
1666
    end
1667
    // set reset bit - selfclearing bit in PHY
1668
    phy_data = phy_data | 16'h8000;
1669
    // write request
1670
    #Tp mii_write_req(phy_addr, reg_addr, phy_data);
1671
    check_mii_busy; // wait for write to finish
1672
    // read request
1673
    #Tp mii_read_req(phy_addr, reg_addr);
1674
    check_mii_busy; // wait for read to finish
1675
    // read data
1676
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1677
    // check self clearing of reset bit
1678
    if (tmp_data[15] !== 1'b0)
1679
    begin
1680
      test_fail("Reset bit should be self cleared - control register");
1681
      fail = fail + 1;
1682
    end
1683
    // check reset value of control register
1684
    if (tmp_data !== {2'h0, (`LED_CFG1 || `LED_CFG2), `LED_CFG1, 3'h0, `LED_CFG3, 8'h0})
1685
    begin
1686
      test_fail("PHY was not reset correctly AND/OR reset bit not self cleared");
1687
      fail = fail + 1;
1688
    end
1689
    if(fail == 0)
1690
      test_ok;
1691
    else
1692
      fail = 0;
1693 169 mohor
  end
1694
 
1695
 
1696 181 mohor
  ////////////////////////////////////////////////////////////////////
1697
  ////                                                            ////
1698
  ////  Test 'walking one' across phy address (with and without   ////
1699
  ////  preamble)                                                 ////
1700
  ////                                                            ////
1701
  ////////////////////////////////////////////////////////////////////
1702
  if (test_num == 4) // 
1703 169 mohor
  begin
1704 194 tadej
    // TEST 4: 'WALKING ONE' ACROSS PHY ADDRESS ( WITH AND WITHOUT PREAMBLE )
1705
    test_name   = "TEST 4: 'WALKING ONE' ACROSS PHY ADDRESS ( WITH AND WITHOUT PREAMBLE )";
1706
    `TIME; $display("  TEST 4: 'WALKING ONE' ACROSS PHY ADDRESS ( WITH AND WITHOUT PREAMBLE )");
1707 181 mohor
 
1708
    // set PHY to test mode
1709
    #Tp eth_phy.test_regs(1); // set test registers (wholy writable registers) and respond to all PHY addresses
1710
    for (i = 0; i <= 1; i = i + 1)
1711 169 mohor
    begin
1712 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
1713
      #Tp eth_phy.clear_test_regs;
1714
      // MII mode register
1715
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}), 4'hF, 1, wbm_init_waits,
1716
                wbm_subseq_waits);
1717
      // walk one across phy address
1718
      for (phy_addr = 5'h1; phy_addr > 5'h0; phy_addr = phy_addr << 1)
1719 169 mohor
      begin
1720 181 mohor
        reg_addr = $random;
1721
        tmp_data = $random;
1722
        // write request
1723
        #Tp mii_write_req(phy_addr, reg_addr, tmp_data);
1724
        check_mii_busy; // wait for write to finish
1725
        // read request
1726
        #Tp mii_read_req(phy_addr, reg_addr);
1727
        check_mii_busy; // wait for read to finish
1728
        // read data
1729
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1730
        #Tp;
1731
        if (phy_data !== tmp_data)
1732
        begin
1733
          if (i)
1734
            test_fail("Data was not correctly written into OR read from test registers (without preamble)");
1735
          else
1736
            test_fail("Data was not correctly written into OR read from test registers (with preamble)");
1737
          fail = fail + 1;
1738
        end
1739
        @(posedge wb_clk);
1740
        #Tp;
1741 169 mohor
      end
1742
    end
1743 181 mohor
    // set PHY to normal mode
1744
    #Tp eth_phy.test_regs(0);
1745
    #Tp eth_phy.preamble_suppresed(0);
1746
    // MII mode register
1747
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1748
    if(fail == 0)
1749
      test_ok;
1750
    else
1751
      fail = 0;
1752 169 mohor
  end
1753
 
1754
 
1755 181 mohor
  ////////////////////////////////////////////////////////////////////
1756
  ////                                                            ////
1757
  ////  Test 'walking one' across phy's register address (with    ////
1758
  ////  and without preamble)                                     ////
1759
  ////                                                            ////
1760
  ////////////////////////////////////////////////////////////////////
1761
  if (test_num == 5) // 
1762 169 mohor
  begin
1763 194 tadej
    // TEST 5: 'WALKING ONE' ACROSS PHY'S REGISTER ADDRESS ( WITH AND WITHOUT PREAMBLE )
1764
    test_name   = "TEST 5: 'WALKING ONE' ACROSS PHY'S REGISTER ADDRESS ( WITH AND WITHOUT PREAMBLE )";
1765
    `TIME; $display("  TEST 5: 'WALKING ONE' ACROSS PHY'S REGISTER ADDRESS ( WITH AND WITHOUT PREAMBLE )");
1766 181 mohor
 
1767
    // set PHY to test mode
1768
    #Tp eth_phy.test_regs(1); // set test registers (wholy writable registers) and respond to all PHY addresses
1769
    for (i = 0; i <= 1; i = i + 1)
1770 169 mohor
    begin
1771 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
1772
      #Tp eth_phy.clear_test_regs;
1773
      // MII mode register
1774
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}), 4'hF, 1, wbm_init_waits,
1775
                wbm_subseq_waits);
1776
      // walk one across reg address
1777
      for (reg_addr = 5'h1; reg_addr > 5'h0; reg_addr = reg_addr << 1)
1778 169 mohor
      begin
1779 181 mohor
        phy_addr = $random;
1780
        tmp_data = $random;
1781
        // write request
1782
        #Tp mii_write_req(phy_addr, reg_addr, tmp_data);
1783
        check_mii_busy; // wait for write to finish
1784
        // read request
1785
        #Tp mii_read_req(phy_addr, reg_addr);
1786
        check_mii_busy; // wait for read to finish
1787
        // read data
1788
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1789
        #Tp;
1790
        if (phy_data !== tmp_data)
1791
        begin
1792
          if (i)
1793
            test_fail("Data was not correctly written into OR read from test registers (without preamble)");
1794
          else
1795
            test_fail("Data was not correctly written into OR read from test registers (with preamble)");
1796
          fail = fail + 1;
1797
        end
1798
        @(posedge wb_clk);
1799
        #Tp;
1800 169 mohor
      end
1801
    end
1802 181 mohor
    // set PHY to normal mode
1803
    #Tp eth_phy.test_regs(0);
1804
    #Tp eth_phy.preamble_suppresed(0);
1805
    // MII mode register
1806
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1807
    if(fail == 0)
1808
      test_ok;
1809
    else
1810
      fail = 0;
1811 169 mohor
  end
1812
 
1813
 
1814 181 mohor
  ////////////////////////////////////////////////////////////////////
1815
  ////                                                            ////
1816
  ////  Test 'walking one' across phy's data (with and without    ////
1817
  ////  preamble)                                                 ////
1818
  ////                                                            ////
1819
  ////////////////////////////////////////////////////////////////////
1820
  if (test_num == 6) // 
1821 169 mohor
  begin
1822 194 tadej
    // TEST 6: 'WALKING ONE' ACROSS PHY'S DATA ( WITH AND WITHOUT PREAMBLE )
1823
    test_name   = "TEST 6: 'WALKING ONE' ACROSS PHY'S DATA ( WITH AND WITHOUT PREAMBLE )";
1824
    `TIME; $display("  TEST 6: 'WALKING ONE' ACROSS PHY'S DATA ( WITH AND WITHOUT PREAMBLE )");
1825 181 mohor
 
1826
    // set PHY to test mode
1827
    #Tp eth_phy.test_regs(1); // set test registers (wholy writable registers) and respond to all PHY addresses
1828
    for (i = 0; i <= 1; i = i + 1)
1829 169 mohor
    begin
1830 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
1831
      #Tp eth_phy.clear_test_regs;
1832
      // MII mode register
1833
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}), 4'hF, 1, wbm_init_waits,
1834
                wbm_subseq_waits);
1835
      // walk one across data
1836
      for (tmp_data = 16'h1; tmp_data > 16'h0; tmp_data = tmp_data << 1)
1837 169 mohor
      begin
1838 181 mohor
        phy_addr = $random;
1839
        reg_addr = $random;
1840
        // write request
1841
        #Tp mii_write_req(phy_addr, reg_addr, tmp_data);
1842
        check_mii_busy; // wait for write to finish
1843
        // read request
1844
        #Tp mii_read_req(phy_addr, reg_addr);
1845
        check_mii_busy; // wait for read to finish
1846
        // read data
1847
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1848
        #Tp;
1849
        if (phy_data !== tmp_data)
1850
        begin
1851
          if (i)
1852
            test_fail("Data was not correctly written into OR read from test registers (without preamble)");
1853
          else
1854
            test_fail("Data was not correctly written into OR read from test registers (with preamble)");
1855
          fail = fail + 1;
1856
        end
1857
        @(posedge wb_clk);
1858
        #Tp;
1859 169 mohor
      end
1860
    end
1861 181 mohor
    // set PHY to normal mode
1862
    #Tp eth_phy.test_regs(0);
1863
    #Tp eth_phy.preamble_suppresed(0);
1864
    // MII mode register
1865
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1866
    if(fail == 0)
1867
      test_ok;
1868
    else
1869
      fail = 0;
1870 169 mohor
  end
1871
 
1872
 
1873 181 mohor
  ////////////////////////////////////////////////////////////////////
1874
  ////                                                            ////
1875
  ////  Test reading from phy with wrong phy address (host        ////
1876
  ////  reading high 'z' data)                                    ////
1877
  ////                                                            ////
1878
  ////////////////////////////////////////////////////////////////////
1879
  if (test_num == 7) // 
1880 169 mohor
  begin
1881 194 tadej
    // TEST 7: READING FROM PHY WITH WRONG PHY ADDRESS ( HOST READING HIGH 'Z' DATA )
1882
    test_name   = "TEST 7: READING FROM PHY WITH WRONG PHY ADDRESS ( HOST READING HIGH 'Z' DATA )";
1883
    `TIME; $display("  TEST 7: READING FROM PHY WITH WRONG PHY ADDRESS ( HOST READING HIGH 'Z' DATA )");
1884 181 mohor
 
1885
    phy_addr = 5'h2; // wrong PHY address
1886
    // read request
1887
    #Tp mii_read_req(phy_addr, reg_addr);
1888
    check_mii_busy; // wait for read to finish
1889
    // read data
1890
    $display("  => Two errors will be displayed from WB Bus Monitor, because correct HIGH Z data was read");
1891
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1892
    if (tmp_data !== 16'hzzzz)
1893
    begin
1894
      test_fail("Data was read from PHY register with wrong PHY address - control register");
1895
      fail = fail + 1;
1896
    end
1897
    if(fail == 0)
1898
      test_ok;
1899
    else
1900
      fail = 0;
1901 169 mohor
  end
1902
 
1903
 
1904 181 mohor
  ////////////////////////////////////////////////////////////////////
1905
  ////                                                            ////
1906
  ////  Test writing to phy with wrong phy address and reading    ////
1907
  ////  from correct one                                          ////
1908
  ////                                                            ////
1909
  ////////////////////////////////////////////////////////////////////
1910
  if (test_num == 8) // 
1911 169 mohor
  begin
1912 194 tadej
    // TEST 8: WRITING TO PHY WITH WRONG PHY ADDRESS AND READING FROM CORRECT ONE
1913
    test_name   = "TEST 8: WRITING TO PHY WITH WRONG PHY ADDRESS AND READING FROM CORRECT ONE";
1914
    `TIME; $display("  TEST 8: WRITING TO PHY WITH WRONG PHY ADDRESS AND READING FROM CORRECT ONE");
1915 181 mohor
 
1916
    // set address
1917
    reg_addr = 5'h0; // control register
1918
    phy_addr = 5'h2; // wrong PHY address
1919
    // write request
1920
    phy_data = 16'h7DFF; // bit 15 (RESET bit) and bit 9 are self clearing bits
1921
    #Tp mii_write_req(phy_addr, reg_addr, phy_data);
1922
    check_mii_busy; // wait for write to finish
1923
 
1924
    phy_addr = 5'h1; // correct PHY address
1925
    // read request
1926
    #Tp mii_read_req(phy_addr, reg_addr);
1927
    check_mii_busy; // wait for read to finish
1928
    // read data
1929
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1930
    if (phy_data === tmp_data)
1931
    begin
1932
      test_fail("Data was written into PHY register with wrong PHY address - control register");
1933
      fail = fail + 1;
1934
    end
1935
    if(fail == 0)
1936
      test_ok;
1937
    else
1938
      fail = 0;
1939 169 mohor
  end
1940
 
1941
 
1942 181 mohor
  ////////////////////////////////////////////////////////////////////
1943
  ////                                                            ////
1944
  ////  Test sliding stop scan command immediately after read     ////
1945
  ////  request (with and without preamble)                       ////
1946
  ////                                                            ////
1947
  ////////////////////////////////////////////////////////////////////
1948
  if (test_num == 9) // 
1949 169 mohor
  begin
1950 194 tadej
    // TEST 9: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER READ REQUEST ( WITH AND WITHOUT PREAMBLE )
1951
    test_name = "TEST 9: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER READ REQUEST ( WITH AND WITHOUT PREAMBLE )";
1952 181 mohor
    `TIME;
1953 194 tadej
    $display("  TEST 9: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER READ REQUEST ( WITH AND WITHOUT PREAMBLE )");
1954 181 mohor
 
1955
    for (i2 = 0; i2 <= 1; i2 = i2 + 1) // choose preamble or not
1956 169 mohor
    begin
1957 181 mohor
      #Tp eth_phy.preamble_suppresed(i2);
1958
      // MII mode register
1959
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i2, 8'h0}), 4'hF, 1, wbm_init_waits,
1960
               wbm_subseq_waits);
1961
      i = 0;
1962
      cnt = 0;
1963
      while (i < 80) // delay for sliding of writing a STOP SCAN command
1964 169 mohor
      begin
1965 181 mohor
        for (i3 = 0; i3 <= 1; i3 = i3 + 1) // choose read or write after read will be finished
1966
        begin
1967
          // set address
1968
          reg_addr = 5'h0; // control register
1969
          phy_addr = 5'h1; // correct PHY address
1970
          cnt = 0;
1971
          // read request
1972
          #Tp mii_read_req(phy_addr, reg_addr);
1973
          fork
1974
            begin
1975
              repeat(i) @(posedge Mdc_O);
1976
              // write command 0x0 into MII command register
1977
              // MII command written while read in progress
1978
              wbm_write(`ETH_MIICOMMAND, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
1979
              @(posedge wb_clk);
1980
              #Tp check_mii_busy; // wait for read to finish
1981
            end
1982
            begin
1983
              // wait for serial bus to become active
1984
              wait(Mdio_IO !== 1'bz);
1985
              // count transfer length
1986
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
1987
              begin
1988
                @(posedge Mdc_O);
1989
                #Tp cnt = cnt + 1;
1990
              end
1991
            end
1992
          join
1993
          // check transfer length
1994
          if (i2) // without preamble
1995 169 mohor
          begin
1996 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
1997
            begin
1998
              test_fail("Read request did not proceed correctly, while SCAN STOP command was written");
1999
              fail = fail + 1;
2000
            end
2001 169 mohor
          end
2002 181 mohor
          else // with preamble
2003 169 mohor
          begin
2004 181 mohor
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
2005
            begin
2006
              test_fail("Read request did not proceed correctly, while SCAN STOP command was written");
2007
              fail = fail + 1;
2008
            end
2009
          end
2010
          // check the BUSY signal to see if the bus is still IDLE
2011
          for (i1 = 0; i1 < 8; i1 = i1 + 1)
2012
            check_mii_busy; // wait for bus to become idle
2013
 
2014
          // try normal write or read after read was finished
2015
          #Tp phy_data = {8'h7D, (i[7:0] + 1)};
2016
          #Tp cnt = 0;
2017
          if (i3 == 0) // write after read
2018
          begin
2019
            // write request
2020
            #Tp mii_write_req(phy_addr, reg_addr, phy_data);
2021 169 mohor
            // wait for serial bus to become active
2022
            wait(Mdio_IO !== 1'bz);
2023
            // count transfer length
2024 181 mohor
            while(Mdio_IO !== 1'bz)
2025 169 mohor
            begin
2026
              @(posedge Mdc_O);
2027
              #Tp cnt = cnt + 1;
2028
            end
2029 181 mohor
            @(posedge Mdc_O);
2030
            // read request
2031
            #Tp mii_read_req(phy_addr, reg_addr);
2032
            check_mii_busy; // wait for read to finish
2033
            // read and check data
2034
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2035
            if (phy_data !== tmp_data)
2036
            begin
2037
              test_fail("Data was not correctly written into OR read from PHY register - control register");
2038
              fail = fail + 1;
2039
            end
2040 169 mohor
          end
2041 181 mohor
          else // read after read
2042 169 mohor
          begin
2043 181 mohor
            // read request
2044
            #Tp mii_read_req(phy_addr, reg_addr);
2045
            // wait for serial bus to become active
2046
            wait(Mdio_IO !== 1'bz);
2047
            // count transfer length
2048
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
2049
            begin
2050
              @(posedge Mdc_O);
2051
              #Tp cnt = cnt + 1;
2052
            end
2053 169 mohor
            @(posedge Mdc_O);
2054 181 mohor
            check_mii_busy; // wait for read to finish
2055
            // read and check data
2056
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2057
            if (phy_data !== tmp_data)
2058
            begin
2059
              test_fail("Data was not correctly written into OR read from PHY register - control register");
2060
              fail = fail + 1;
2061
            end
2062 169 mohor
          end
2063 181 mohor
          // check if transfer was a proper length
2064
          if (i2) // without preamble
2065 169 mohor
          begin
2066 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
2067
            begin
2068
              test_fail("New request did not proceed correctly, after read request");
2069
              fail = fail + 1;
2070
            end
2071 169 mohor
          end
2072 181 mohor
          else // with preamble
2073 169 mohor
          begin
2074 181 mohor
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
2075
            begin
2076
              test_fail("New request did not proceed correctly, after read request");
2077
              fail = fail + 1;
2078
            end
2079 169 mohor
          end
2080
        end
2081 181 mohor
        #Tp;
2082
        // set delay of writing the command
2083 169 mohor
        if (i2) // without preamble
2084
        begin
2085 181 mohor
          case(i)
2086
            0, 1:               i = i + 1;
2087
            18, 19, 20, 21, 22,
2088
            23, 24, 25, 26, 27,
2089
            28, 29, 30, 31, 32,
2090
            33, 34, 35:         i = i + 1;
2091
            36:                 i = 80;
2092
            default:            i = 18;
2093
          endcase
2094 169 mohor
        end
2095
        else // with preamble
2096
        begin
2097 181 mohor
          case(i)
2098
            0, 1:               i = i + 1;
2099
            50, 51, 52, 53, 54,
2100
            55, 56, 57, 58, 59,
2101
            60, 61, 62, 63, 64,
2102
            65, 66, 67:         i = i + 1;
2103
            68:                 i = 80;
2104
            default:            i = 50;
2105
          endcase
2106 169 mohor
        end
2107 181 mohor
        @(posedge wb_clk);
2108 169 mohor
      end
2109
    end
2110 181 mohor
    // set PHY to normal mode
2111
    #Tp eth_phy.preamble_suppresed(0);
2112
    // MII mode register
2113
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2114
    if(fail == 0)
2115
      test_ok;
2116
    else
2117
      fail = 0;
2118 169 mohor
  end
2119
 
2120
 
2121 181 mohor
  ////////////////////////////////////////////////////////////////////
2122
  ////                                                            ////
2123
  ////  Test sliding stop scan command immediately after write    ////
2124
  ////  request (with and without preamble)                       ////
2125
  ////                                                            ////
2126
  ////////////////////////////////////////////////////////////////////
2127
  if (test_num == 10) // 
2128 169 mohor
  begin
2129 194 tadej
    // TEST 10: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER WRITE REQUEST ( WITH AND WITHOUT PREAMBLE )
2130
    test_name = "TEST 10: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER WRITE REQUEST ( WITH AND WITHOUT PREAMBLE )";
2131 181 mohor
    `TIME;
2132 194 tadej
    $display("  TEST 10: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER WRITE REQUEST ( WITH AND WITHOUT PREAMBLE )");
2133 181 mohor
 
2134
    for (i2 = 0; i2 <= 1; i2 = i2 + 1) // choose preamble or not
2135 169 mohor
    begin
2136 181 mohor
      #Tp eth_phy.preamble_suppresed(i2);
2137
      // MII mode register
2138
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i2, 8'h0}), 4'hF, 1, wbm_init_waits,
2139
                wbm_subseq_waits);
2140
      i = 0;
2141
      cnt = 0;
2142
      while (i < 80) // delay for sliding of writing a STOP SCAN command
2143 169 mohor
      begin
2144 181 mohor
        for (i3 = 0; i3 <= 1; i3 = i3 + 1) // choose read or write after write will be finished
2145
        begin
2146
          // set address
2147
          reg_addr = 5'h0; // control register
2148
          phy_addr = 5'h1; // correct PHY address
2149
          cnt = 0;
2150
          // write request
2151
          phy_data = {8'h75, (i[7:0] + 1)};
2152
          #Tp mii_write_req(phy_addr, reg_addr, phy_data);
2153
          fork
2154
            begin
2155
              repeat(i) @(posedge Mdc_O);
2156
              // write command 0x0 into MII command register
2157
              // MII command written while read in progress
2158
              wbm_write(`ETH_MIICOMMAND, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2159
              @(posedge wb_clk);
2160
              #Tp check_mii_busy; // wait for write to finish
2161
            end
2162
            begin
2163
              // wait for serial bus to become active
2164
              wait(Mdio_IO !== 1'bz);
2165
              // count transfer length
2166
              while(Mdio_IO !== 1'bz)
2167
              begin
2168
                @(posedge Mdc_O);
2169
                #Tp cnt = cnt + 1;
2170
              end
2171
            end
2172
          join
2173
          // check transfer length
2174
          if (i2) // without preamble
2175 169 mohor
          begin
2176 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
2177
            begin
2178
              test_fail("Write request did not proceed correctly, while SCAN STOP command was written");
2179
              fail = fail + 1;
2180
            end
2181 169 mohor
          end
2182 181 mohor
          else // with preamble
2183 169 mohor
          begin
2184 181 mohor
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
2185
            begin
2186
              test_fail("Write request did not proceed correctly, while SCAN STOP command was written");
2187
              fail = fail + 1;
2188
            end
2189
          end
2190
          // check the BUSY signal to see if the bus is still IDLE
2191
          for (i1 = 0; i1 < 8; i1 = i1 + 1)
2192
            check_mii_busy; // wait for bus to become idle
2193
 
2194
          // try normal write or read after write was finished
2195
          #Tp cnt = 0;
2196
          if (i3 == 0) // write after write
2197
          begin
2198
            phy_data = {8'h7A, (i[7:0] + 1)};
2199
            // write request
2200
            #Tp mii_write_req(phy_addr, reg_addr, phy_data);
2201 169 mohor
            // wait for serial bus to become active
2202
            wait(Mdio_IO !== 1'bz);
2203
            // count transfer length
2204
            while(Mdio_IO !== 1'bz)
2205
            begin
2206
              @(posedge Mdc_O);
2207
              #Tp cnt = cnt + 1;
2208
            end
2209 181 mohor
            @(posedge Mdc_O);
2210
            // read request
2211
            #Tp mii_read_req(phy_addr, reg_addr);
2212
            check_mii_busy; // wait for read to finish
2213
            // read and check data
2214
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data , 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2215
            if (phy_data !== tmp_data)
2216
            begin
2217
              test_fail("Data was not correctly written into OR read from PHY register - control register");
2218
              fail = fail + 1;
2219
            end
2220 169 mohor
          end
2221 181 mohor
          else // read after write
2222 169 mohor
          begin
2223 181 mohor
            // read request
2224
            #Tp mii_read_req(phy_addr, reg_addr);
2225
            // wait for serial bus to become active
2226
            wait(Mdio_IO !== 1'bz);
2227
            // count transfer length
2228
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
2229
            begin
2230
              @(posedge Mdc_O);
2231
              #Tp cnt = cnt + 1;
2232
            end
2233 169 mohor
            @(posedge Mdc_O);
2234 181 mohor
            check_mii_busy; // wait for read to finish
2235
            // read and check data
2236
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data , 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2237
            if (phy_data !== tmp_data)
2238
            begin
2239
              test_fail("Data was not correctly written into OR read from PHY register - control register");
2240
              fail = fail + 1;
2241
            end
2242 169 mohor
          end
2243 181 mohor
          // check if transfer was a proper length
2244
          if (i2) // without preamble
2245 169 mohor
          begin
2246 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
2247
            begin
2248
              test_fail("New request did not proceed correctly, after write request");
2249
              fail = fail + 1;
2250
            end
2251 169 mohor
          end
2252 181 mohor
          else // with preamble
2253 169 mohor
          begin
2254 181 mohor
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
2255
            begin
2256
              test_fail("New request did not proceed correctly, after write request");
2257
              fail = fail + 1;
2258
            end
2259 169 mohor
          end
2260
        end
2261 181 mohor
        #Tp;
2262
        // set delay of writing the command
2263 169 mohor
        if (i2) // without preamble
2264
        begin
2265 181 mohor
          case(i)
2266
            0, 1:               i = i + 1;
2267
            18, 19, 20, 21, 22,
2268
            23, 24, 25, 26, 27,
2269
            28, 29, 30, 31, 32,
2270
            33, 34, 35:         i = i + 1;
2271
            36:                 i = 80;
2272
            default:            i = 18;
2273
          endcase
2274 169 mohor
        end
2275
        else // with preamble
2276
        begin
2277 181 mohor
          case(i)
2278
            0, 1:               i = i + 1;
2279
            50, 51, 52, 53, 54,
2280
            55, 56, 57, 58, 59,
2281
            60, 61, 62, 63, 64,
2282
            65, 66, 67:         i = i + 1;
2283
            68:                 i = 80;
2284
            default:            i = 50;
2285
          endcase
2286 169 mohor
        end
2287 181 mohor
        @(posedge wb_clk);
2288 169 mohor
      end
2289
    end
2290 181 mohor
    // set PHY to normal mode
2291
    #Tp eth_phy.preamble_suppresed(0);
2292
    // MII mode register
2293
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2294
    if(fail == 0)
2295
      test_ok;
2296
    else
2297
      fail = 0;
2298 169 mohor
  end
2299
 
2300
 
2301 181 mohor
  ////////////////////////////////////////////////////////////////////
2302
  ////                                                            ////
2303
  ////  Test busy and nvalid status durations during write (with  ////
2304
  ////  and without preamble)                                     ////
2305
  ////                                                            ////
2306
  ////////////////////////////////////////////////////////////////////
2307
  if (test_num == 11) // 
2308 169 mohor
  begin
2309 194 tadej
    // TEST 11: BUSY AND NVALID STATUS DURATIONS DURING WRITE ( WITH AND WITHOUT PREAMBLE )
2310
    test_name   = "TEST 11: BUSY AND NVALID STATUS DURATIONS DURING WRITE ( WITH AND WITHOUT PREAMBLE )";
2311
    `TIME; $display("  TEST 11: BUSY AND NVALID STATUS DURATIONS DURING WRITE ( WITH AND WITHOUT PREAMBLE )");
2312 181 mohor
 
2313
    reset_mii; // reset MII
2314
    // set link up, if it wasn't due to previous tests, since there weren't PHY registers
2315
    #Tp eth_phy.link_up_down(1);
2316
    // set the MII
2317
    clk_div = 64;
2318
    mii_set_clk_div(clk_div[7:0]);
2319
    // set address
2320
    reg_addr = 5'h1; // status register
2321
    phy_addr = 5'h1; // correct PHY address
2322
 
2323
    for (i = 0; i <= 1; i = i + 1)
2324 169 mohor
    begin
2325 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
2326
      // MII mode register
2327
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}) | (`ETH_MIIMODER_CLKDIV & clk_div),
2328
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2329
      @(posedge Mdc_O);
2330
      // write request
2331
      #Tp mii_write_req(phy_addr, reg_addr, 16'h5A5A);
2332
      // read data from MII status register - Busy and Nvalid bits
2333
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2334
 
2335
      // check MII IO signal and Busy and Nvalid bits
2336
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2337 169 mohor
      begin
2338 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is not HIGH Z - set higher clock divider");
2339
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2340
        begin
2341
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z anymore - 1. read");
2342
          fail = fail + 1;
2343
        end
2344
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2345
        begin
2346
          test_fail("Nvalid signal was set during write");
2347
          fail = fail + 1;
2348
        end
2349 169 mohor
      end
2350 181 mohor
      else // Busy bit should already be set to '1', due to reads from MII status register
2351 169 mohor
      begin
2352 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2353
        begin
2354
          test_fail("Busy signal should be set after write, due to reads from MII status register");
2355
          fail = fail + 1;
2356
        end
2357
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2358
        begin
2359
          test_fail("Nvalid signal was set during write");
2360
          fail = fail + 1;
2361
        end
2362 169 mohor
      end
2363 181 mohor
 
2364
      // wait for serial bus to become active
2365
      wait(Mdio_IO !== 1'bz);
2366
      // count transfer bits
2367
      if (i)
2368 169 mohor
      begin
2369 181 mohor
        repeat(32) @(posedge Mdc_O);
2370 169 mohor
      end
2371 181 mohor
      else
2372 169 mohor
      begin
2373 181 mohor
        repeat(64) @(posedge Mdc_O);
2374 169 mohor
      end
2375 181 mohor
      // read data from MII status register - Busy and Nvalid bits
2376
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2377
 
2378
      // check MII IO signal and Busy and Nvalid bits
2379
      if (Mdio_IO === 1'bz) // Mdio_IO should not be HIGH Z here - testbench selfcheck
2380 169 mohor
      begin
2381 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is HIGH Z - set higher clock divider");
2382
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2383
        begin
2384
          test_fail("Busy signal should be set while MII IO signal is not active anymore");
2385
          fail = fail + 1;
2386
        end
2387
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2388
        begin
2389
          test_fail("Nvalid signal was set during write");
2390
          fail = fail + 1;
2391
        end
2392 169 mohor
      end
2393 181 mohor
      else // Busy bit should still be set to '1'
2394 169 mohor
      begin
2395 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2396
        begin
2397
          test_fail("Busy signal should be set while MII IO signal not HIGH Z");
2398
          fail = fail + 1;
2399
        end
2400
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2401
        begin
2402
          test_fail("Nvalid signal was set during write");
2403
          fail = fail + 1;
2404
        end
2405 169 mohor
      end
2406 181 mohor
 
2407
      // wait for next negative clock edge
2408
      @(negedge Mdc_O);
2409 169 mohor
      // read data from MII status register - Busy and Nvalid bits
2410
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2411 181 mohor
 
2412 169 mohor
      // check MII IO signal and Busy and Nvalid bits
2413
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2414
      begin
2415
        test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2416
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2417
        begin
2418
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2419
          fail = fail + 1;
2420
        end
2421
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2422
        begin
2423
          test_fail("Nvalid signal was set during write");
2424
          fail = fail + 1;
2425
        end
2426
      end
2427 181 mohor
      else // Busy bit should still be set to '1'
2428 169 mohor
      begin
2429
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2430
        begin
2431 181 mohor
          test_fail("Busy signal should be set after MII IO signal become HIGH Z");
2432
          fail = fail + 1;
2433 169 mohor
        end
2434 181 mohor
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2435 169 mohor
        begin
2436 181 mohor
          test_fail("Nvalid signal was set during write");
2437
          fail = fail + 1;
2438
        end
2439
      end
2440
 
2441
      // wait for Busy to become inactive
2442
      i1 = 0;
2443
      while (i1 <= 2)
2444
      begin
2445
        // wait for next positive clock edge
2446
        @(posedge Mdc_O);
2447
        // read data from MII status register - Busy and Nvalid bits
2448
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2449
 
2450
        // check MII IO signal and Busy and Nvalid bits
2451
        if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2452
        begin
2453
          test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2454
          if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2455 169 mohor
          begin
2456 181 mohor
            test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2457 169 mohor
            fail = fail + 1;
2458
          end
2459 181 mohor
          if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2460
          begin
2461
            test_fail("Nvalid signal was set during write");
2462
            fail = fail + 1;
2463
          end
2464 169 mohor
        end
2465 181 mohor
        else // wait for Busy bit to be set to '0'
2466 169 mohor
        begin
2467 181 mohor
          if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2468
          begin
2469
            i1 = 3; // end of Busy checking
2470
          end
2471
          else
2472
          begin
2473
            if (i1 == 2)
2474
            begin
2475
              test_fail("Busy signal should be cleared after 2 periods after MII IO signal become HIGH Z");
2476
              fail = fail + 1;
2477
            end
2478
            #Tp i1 = i1 + 1;
2479
          end
2480
          if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2481
          begin
2482
            test_fail("Nvalid signal was set after write");
2483
            fail = fail + 1;
2484
          end
2485 169 mohor
        end
2486
      end
2487
    end
2488 181 mohor
    // set PHY to normal mode
2489
    #Tp eth_phy.preamble_suppresed(0);
2490
    // MII mode register
2491
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2492
    if(fail == 0)
2493
      test_ok;
2494
    else
2495
      fail = 0;
2496 116 mohor
  end
2497
 
2498
 
2499 181 mohor
  ////////////////////////////////////////////////////////////////////
2500
  ////                                                            ////
2501
  ////  Test busy and nvalid status durations during write (with  ////
2502
  ////  and without preamble)                                     ////
2503
  ////                                                            ////
2504
  ////////////////////////////////////////////////////////////////////
2505
  if (test_num == 12) // 
2506 169 mohor
  begin
2507 194 tadej
    // TEST 12: BUSY AND NVALID STATUS DURATIONS DURING READ ( WITH AND WITHOUT PREAMBLE )
2508
    test_name   = "TEST 12: BUSY AND NVALID STATUS DURATIONS DURING READ ( WITH AND WITHOUT PREAMBLE )";
2509
    `TIME; $display("  TEST 12: BUSY AND NVALID STATUS DURATIONS DURING READ ( WITH AND WITHOUT PREAMBLE )");
2510 181 mohor
 
2511
    reset_mii; // reset MII
2512
    // set link up, if it wasn't due to previous tests, since there weren't PHY registers
2513
    #Tp eth_phy.link_up_down(1);
2514
    // set the MII
2515
    clk_div = 64;
2516
    mii_set_clk_div(clk_div[7:0]);
2517
    // set address
2518
    reg_addr = 5'h1; // status register
2519
    phy_addr = 5'h1; // correct PHY address
2520
 
2521
    for (i = 0; i <= 1; i = i + 1)
2522 169 mohor
    begin
2523 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
2524
      // MII mode register
2525
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}) | (`ETH_MIIMODER_CLKDIV & clk_div),
2526
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2527 169 mohor
      @(posedge Mdc_O);
2528 181 mohor
      // read request
2529
      #Tp mii_read_req(phy_addr, reg_addr);
2530 169 mohor
      // read data from MII status register - Busy and Nvalid bits
2531
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2532 181 mohor
 
2533 169 mohor
      // check MII IO signal and Busy and Nvalid bits
2534
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2535
      begin
2536 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is not HIGH Z - set higher clock divider");
2537 169 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2538
        begin
2539 181 mohor
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z anymore - 1. read");
2540 169 mohor
          fail = fail + 1;
2541
        end
2542
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2543
        begin
2544
          test_fail("Nvalid signal was set during read");
2545
          fail = fail + 1;
2546
        end
2547
      end
2548 181 mohor
      else // Busy bit should already be set to '1', due to reads from MII status register
2549 169 mohor
      begin
2550
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2551
        begin
2552 181 mohor
          test_fail("Busy signal should be set after read, due to reads from MII status register");
2553
          fail = fail + 1;
2554 169 mohor
        end
2555
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2556
        begin
2557 181 mohor
          test_fail("Nvalid signal was set during read");
2558 169 mohor
          fail = fail + 1;
2559
        end
2560
      end
2561 181 mohor
 
2562
      // wait for serial bus to become active
2563
      wait(Mdio_IO !== 1'bz);
2564
      // count transfer bits
2565
      if (i)
2566 169 mohor
      begin
2567 181 mohor
        repeat(31) @(posedge Mdc_O);
2568 169 mohor
      end
2569 181 mohor
      else
2570 169 mohor
      begin
2571 181 mohor
        repeat(63) @(posedge Mdc_O);
2572 169 mohor
      end
2573 181 mohor
      // wait for next negative clock edge
2574
      @(negedge Mdc_O);
2575
      // read data from MII status register - Busy and Nvalid bits
2576
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2577
 
2578
      // check MII IO signal and Busy and Nvalid bits
2579
      if (Mdio_IO === 1'bz) // Mdio_IO should not be HIGH Z here - testbench selfcheck
2580 169 mohor
      begin
2581 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is HIGH Z - set higher clock divider");
2582
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2583
        begin
2584
          test_fail("Busy signal should be set while MII IO signal is not active anymore");
2585
          fail = fail + 1;
2586
        end
2587
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2588
        begin
2589
          test_fail("Nvalid signal was set during read");
2590
          fail = fail + 1;
2591
        end
2592 169 mohor
      end
2593 181 mohor
      else // Busy bit should still be set to '1'
2594 169 mohor
      begin
2595 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2596
        begin
2597
          test_fail("Busy signal should be set while MII IO signal not HIGH Z");
2598
          fail = fail + 1;
2599
        end
2600
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2601
        begin
2602
          test_fail("Nvalid signal was set during read");
2603
          fail = fail + 1;
2604
        end
2605 169 mohor
      end
2606 181 mohor
 
2607 169 mohor
      // wait for next positive clock edge
2608
      @(posedge Mdc_O);
2609
      // read data from MII status register - Busy and Nvalid bits
2610
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2611 181 mohor
 
2612 169 mohor
      // check MII IO signal and Busy and Nvalid bits
2613
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2614
      begin
2615
        test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2616 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2617 169 mohor
        begin
2618 181 mohor
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2619
          fail = fail + 1;
2620
        end
2621
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2622
        begin
2623
          test_fail("Nvalid signal was set during read");
2624
          fail = fail + 1;
2625
        end
2626
      end
2627
      else // Busy bit should still be set to '1'
2628
      begin
2629
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2630
        begin
2631
          test_fail("Busy signal should be set after MII IO signal become HIGH Z");
2632
          fail = fail + 1;
2633
        end
2634
        if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2635
        begin
2636
          test_fail("Nvalid signal was set during read");
2637
          fail = fail + 1;
2638
        end
2639
      end
2640
 
2641
      // wait for Busy to become inactive
2642
      i1 = 0;
2643
      while (i1 <= 2)
2644
      begin
2645
        // wait for next positive clock edge
2646
        @(posedge Mdc_O);
2647
        // read data from MII status register - Busy and Nvalid bits
2648
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2649
 
2650
        // check MII IO signal and Busy and Nvalid bits
2651
        if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2652
        begin
2653
          test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2654 169 mohor
          if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2655
          begin
2656
            test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2657
            fail = fail + 1;
2658
          end
2659 181 mohor
          if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2660 169 mohor
          begin
2661 181 mohor
            test_fail("Nvalid signal was set during read");
2662 169 mohor
            fail = fail + 1;
2663
          end
2664
        end
2665 181 mohor
        else // wait for Busy bit to be set to '0'
2666 169 mohor
        begin
2667
          if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2668
          begin
2669
            i1 = 3; // end of Busy checking
2670
          end
2671
          else
2672
          begin
2673
            if (i1 == 2)
2674
            begin
2675
              test_fail("Busy signal should be cleared after 2 periods after MII IO signal become HIGH Z");
2676
              fail = fail + 1;
2677
            end
2678
            #Tp i1 = i1 + 1;
2679
          end
2680 181 mohor
          if (phy_data[`ETH_MIISTATUS_NVALID] !== 1'b0)
2681 169 mohor
          begin
2682 181 mohor
            test_fail("Nvalid signal was set after read");
2683 169 mohor
            fail = fail + 1;
2684
          end
2685
        end
2686
      end
2687
    end
2688 181 mohor
    // set PHY to normal mode
2689
    #Tp eth_phy.preamble_suppresed(0);
2690
    // MII mode register
2691
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2692
    if(fail == 0)
2693
      test_ok;
2694
    else
2695
      fail = 0;
2696 169 mohor
  end
2697
 
2698
 
2699 181 mohor
  ////////////////////////////////////////////////////////////////////
2700
  ////                                                            ////
2701
  ////  Test busy and nvalid status durations during scan (with   ////
2702
  ////  and without preamble)                                     ////
2703
  ////                                                            ////
2704
  ////////////////////////////////////////////////////////////////////
2705
  if (test_num == 13) // 
2706 169 mohor
  begin
2707 194 tadej
    // TEST 13: BUSY AND NVALID STATUS DURATIONS DURING SCAN ( WITH AND WITHOUT PREAMBLE )
2708
    test_name   = "TEST 13: BUSY AND NVALID STATUS DURATIONS DURING SCAN ( WITH AND WITHOUT PREAMBLE )";
2709
    `TIME; $display("  TEST 13: BUSY AND NVALID STATUS DURATIONS DURING SCAN ( WITH AND WITHOUT PREAMBLE )");
2710 181 mohor
 
2711
    reset_mii; // reset MII
2712
    // set link up, if it wasn't due to previous tests, since there weren't PHY registers
2713
    #Tp eth_phy.link_up_down(1);
2714
    // set the MII
2715
    clk_div = 64;
2716
    mii_set_clk_div(clk_div[7:0]);
2717
    // set address
2718
    reg_addr = 5'h1; // status register
2719
    phy_addr = 5'h1; // correct PHY address
2720
 
2721
    for (i = 0; i <= 1; i = i + 1)
2722 169 mohor
    begin
2723 181 mohor
      #Tp eth_phy.preamble_suppresed(i);
2724
      // MII mode register
2725
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}) | (`ETH_MIIMODER_CLKDIV & clk_div),
2726
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2727
      @(posedge Mdc_O);
2728
      // scan request
2729
      #Tp mii_scan_req(phy_addr, reg_addr);
2730
      // read data from MII status register - Busy and Nvalid bits
2731
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2732
 
2733
      // check MII IO signal and Busy and Nvalid bits
2734
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2735 169 mohor
      begin
2736 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is not HIGH Z - set higher clock divider");
2737
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2738
        begin
2739
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z anymore - 1. read");
2740
          fail = fail + 1;
2741
        end
2742
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2743
        begin
2744
          test_fail("Nvalid signal was not set while MII IO signal is not HIGH Z anymore - 1. read");
2745
          fail = fail + 1;
2746
        end
2747 169 mohor
      end
2748 181 mohor
      else // Busy bit should already be set to '1', due to reads from MII status register
2749 169 mohor
      begin
2750 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2751
        begin
2752
          test_fail("Busy signal should be set after scan, due to reads from MII status register");
2753
          fail = fail + 1;
2754
        end
2755
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2756
        begin
2757
          test_fail("Nvalid signal should be set after scan, due to reads from MII status register");
2758
          fail = fail + 1;
2759
        end
2760 169 mohor
      end
2761 181 mohor
 
2762
      // wait for serial bus to become active
2763 169 mohor
      wait(Mdio_IO !== 1'bz);
2764 181 mohor
      // count transfer bits
2765
      if (i)
2766 169 mohor
      begin
2767 181 mohor
        repeat(21) @(posedge Mdc_O);
2768 169 mohor
      end
2769 181 mohor
      else
2770 169 mohor
      begin
2771 181 mohor
        repeat(53) @(posedge Mdc_O);
2772 169 mohor
      end
2773 181 mohor
      // stop scan
2774
      #Tp mii_scan_finish; // finish scan operation
2775
 
2776
      // wait for next positive clock edge
2777
      repeat(10) @(posedge Mdc_O);
2778
      // read data from MII status register - Busy and Nvalid bits
2779
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2780
 
2781
      // check MII IO signal and Busy and Nvalid bits
2782
      if (Mdio_IO === 1'bz) // Mdio_IO should not be HIGH Z here - testbench selfcheck
2783 169 mohor
      begin
2784 181 mohor
        test_fail("Testbench error - read was to late, Mdio_IO is HIGH Z - set higher clock divider");
2785
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2786 169 mohor
        begin
2787 181 mohor
          test_fail("Busy signal should be set while MII IO signal is not active anymore");
2788 169 mohor
          fail = fail + 1;
2789
        end
2790 181 mohor
        // Nvalid signal can be cleared here - it is still Testbench error
2791 169 mohor
      end
2792 181 mohor
      else // Busy bit should still be set to '1', Nvalid bit should still be set to '1'
2793 169 mohor
      begin
2794 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2795 169 mohor
        begin
2796 181 mohor
          test_fail("Busy signal should be set while MII IO signal not HIGH Z");
2797 169 mohor
          fail = fail + 1;
2798
        end
2799 181 mohor
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2800 169 mohor
        begin
2801 181 mohor
          test_fail("Nvalid signal should be set while MII IO signal not HIGH Z");
2802 169 mohor
          fail = fail + 1;
2803
        end
2804 181 mohor
      end
2805
 
2806
      // wait for next negative clock edge
2807
      @(negedge Mdc_O);
2808
      // read data from MII status register - Busy and Nvalid bits
2809
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2810
 
2811
      // check MII IO signal and Busy and Nvalid bits
2812
      if (Mdio_IO === 1'bz) // Mdio_IO should not be HIGH Z here - testbench selfcheck
2813
      begin
2814
        test_fail("Testbench error - read was to late, Mdio_IO is HIGH Z - set higher clock divider");
2815
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2816 169 mohor
        begin
2817 181 mohor
          test_fail("Busy signal should be set while MII IO signal is not active anymore");
2818
          fail = fail + 1;
2819 169 mohor
        end
2820 181 mohor
        // Nvalid signal can be cleared here - it is still Testbench error
2821 169 mohor
      end
2822 181 mohor
      else // Busy bit should still be set to '1', Nvalid bit should still be set to '1'
2823 169 mohor
      begin
2824 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2825 169 mohor
        begin
2826 181 mohor
          test_fail("Busy signal should be set while MII IO signal not HIGH Z");
2827
          fail = fail + 1;
2828 169 mohor
        end
2829 181 mohor
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2830 169 mohor
        begin
2831 181 mohor
          test_fail("Nvalid signal should be set while MII IO signal not HIGH Z");
2832
          fail = fail + 1;
2833 169 mohor
        end
2834
      end
2835 181 mohor
 
2836
      // wait for next negative clock edge
2837
      @(posedge Mdc_O);
2838
      // read data from MII status register - Busy and Nvalid bits
2839 169 mohor
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2840 181 mohor
 
2841
      // check MII IO signal and Busy and Nvalid bits
2842
      if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2843 169 mohor
      begin
2844 181 mohor
        test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2845
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2846 169 mohor
        begin
2847 181 mohor
          test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2848 169 mohor
          fail = fail + 1;
2849
        end
2850 181 mohor
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2851 169 mohor
        begin
2852 181 mohor
          test_fail("Nvalid signal was not set while MII IO signal is not HIGH Z");
2853 169 mohor
          fail = fail + 1;
2854
        end
2855
      end
2856 181 mohor
      else // Busy bit should still be set to '1', Nvalid bit can be set to '0'
2857 169 mohor
      begin
2858 181 mohor
        if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2859 169 mohor
        begin
2860 181 mohor
          test_fail("Busy signal should be set after MII IO signal become HIGH Z");
2861
          fail = fail + 1;
2862 169 mohor
        end
2863 181 mohor
        if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2864 169 mohor
        begin
2865 181 mohor
          i2 = 1; // check finished
2866 169 mohor
        end
2867 181 mohor
        else
2868 169 mohor
        begin
2869 181 mohor
          i2 = 0; // check must continue
2870 169 mohor
        end
2871
      end
2872 181 mohor
 
2873
      // wait for Busy to become inactive
2874
      i1 = 0;
2875
      while ((i1 <= 2) || (i2 == 0))
2876
      begin
2877
        // wait for next positive clock edge
2878
        @(posedge Mdc_O);
2879
        // read data from MII status register - Busy and Nvalid bits
2880
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2881
 
2882
        // check MII IO signal and Busy and Nvalid bits
2883
        if (Mdio_IO !== 1'bz) // Mdio_IO should be HIGH Z here - testbench selfcheck
2884 169 mohor
        begin
2885 181 mohor
          test_fail("Testbench error - read was to early, Mdio_IO is not HIGH Z - set higher clock divider");
2886
          if (i1 <= 2)
2887 169 mohor
          begin
2888 181 mohor
            if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2889 169 mohor
            begin
2890 181 mohor
              test_fail("Busy signal was not set while MII IO signal is not HIGH Z");
2891 169 mohor
              fail = fail + 1;
2892
            end
2893
          end
2894 181 mohor
          if (i2 == 0)
2895 169 mohor
          begin
2896 181 mohor
            if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2897 169 mohor
            begin
2898 181 mohor
              test_fail("Nvalid signal was not set while MII IO signal is not HIGH Z");
2899 169 mohor
              fail = fail + 1;
2900
            end
2901
          end
2902
        end
2903 181 mohor
        else // wait for Busy bit to be set to '0'
2904 169 mohor
        begin
2905 181 mohor
          if (i1 <= 2)
2906 169 mohor
          begin
2907 181 mohor
            if (phy_data[`ETH_MIISTATUS_BUSY] === 1'b0)
2908 169 mohor
            begin
2909 181 mohor
              i1 = 3; // end of Busy checking
2910 169 mohor
            end
2911 181 mohor
            else
2912 169 mohor
            begin
2913 181 mohor
              if (i1 == 2)
2914 169 mohor
              begin
2915 181 mohor
                test_fail("Busy signal should be cleared after 2 periods after MII IO signal become HIGH Z");
2916 169 mohor
                fail = fail + 1;
2917
              end
2918 181 mohor
              #Tp i1 = i1 + 1;
2919 169 mohor
            end
2920 181 mohor
          end
2921
          if (i2 == 0)
2922
          begin
2923
            if (phy_data[`ETH_MIISTATUS_NVALID] === 1'b0)
2924 169 mohor
            begin
2925 181 mohor
              i2 = 1;
2926 169 mohor
            end
2927 181 mohor
            else
2928
            begin
2929
              test_fail("Nvalid signal should be cleared after MII IO signal become HIGH Z");
2930
              fail = fail + 1;
2931
            end
2932 169 mohor
          end
2933
        end
2934 181 mohor
      end
2935
    end
2936
    // set PHY to normal mode
2937
    #Tp eth_phy.preamble_suppresed(0);
2938
    // MII mode register
2939
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2940
    if(fail == 0)
2941
      test_ok;
2942
    else
2943
      fail = 0;
2944
  end
2945
 
2946
 
2947
  ////////////////////////////////////////////////////////////////////
2948
  ////                                                            ////
2949
  ////  Test scan status from phy with detecting link-fail bit    ////
2950
  ////  (with and without preamble)                               ////
2951
  ////                                                            ////
2952
  ////////////////////////////////////////////////////////////////////
2953
  if (test_num == 14) // 
2954
  begin
2955 194 tadej
    // TEST 14: SCAN STATUS FROM PHY WITH DETECTING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )
2956
    test_name   = "TEST 14: SCAN STATUS FROM PHY WITH DETECTING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )";
2957
    `TIME; $display("  TEST 14: SCAN STATUS FROM PHY WITH DETECTING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )");
2958 181 mohor
 
2959
    reset_mii; // reset MII
2960
    // set link up, if it wasn't due to previous tests, since there weren't PHY registers
2961
    #Tp eth_phy.link_up_down(1);
2962
    // set MII speed
2963
    clk_div = 6;
2964
    mii_set_clk_div(clk_div[7:0]);
2965
    // set address
2966
    reg_addr = 5'h1; // status register
2967
    phy_addr = 5'h1; // correct PHY address
2968
 
2969
    // read request
2970
    #Tp mii_read_req(phy_addr, reg_addr);
2971
    check_mii_busy; // wait for read to finish
2972
    // read data from PHY status register - remember LINK-UP status
2973
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2974
 
2975
    for (i = 0; i <= 1; i = i + 1)
2976
    begin
2977
      #Tp eth_phy.preamble_suppresed(i);
2978
      // MII mode register
2979
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i, 8'h0}) | (`ETH_MIIMODER_CLKDIV & clk_div),
2980
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2981
      if (i)
2982
      begin
2983
        // change saved data when preamble is suppressed
2984
        #Tp tmp_data = tmp_data | 16'h0040; // put bit 6 to ONE
2985
      end
2986
 
2987
      // scan request
2988
      #Tp mii_scan_req(phy_addr, reg_addr);
2989
      check_mii_scan_valid; // wait for scan to make first data valid
2990
 
2991 169 mohor
      fork
2992 181 mohor
      begin
2993 169 mohor
        repeat(2) @(posedge Mdc_O);
2994
        // read data from PHY status register
2995
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
2996 181 mohor
        if (phy_data !== tmp_data)
2997 169 mohor
        begin
2998 181 mohor
          test_fail("Data was not correctly scaned from status register");
2999 169 mohor
          fail = fail + 1;
3000
        end
3001
        // read data from MII status register
3002
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3003 181 mohor
        if (phy_data[0] !== 1'b0)
3004 169 mohor
        begin
3005 181 mohor
          test_fail("Link FAIL bit was set in the MII status register");
3006 169 mohor
          fail = fail + 1;
3007
        end
3008
      end
3009
      begin
3010 181 mohor
      // Completely check second scan
3011 169 mohor
        #Tp cnt = 0;
3012
        // wait for serial bus to become active - second scan
3013
        wait(Mdio_IO !== 1'bz);
3014
        // count transfer length
3015 181 mohor
        while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i == 0)) || ((cnt == 15) && (i == 1)) )
3016 169 mohor
        begin
3017
          @(posedge Mdc_O);
3018
          #Tp cnt = cnt + 1;
3019
        end
3020
        // check transfer length
3021 181 mohor
        if (i) // without preamble
3022 169 mohor
        begin
3023
          if (cnt != 33) // at this value Mdio_IO is HIGH Z
3024
          begin
3025 181 mohor
            test_fail("Second scan request did not proceed correctly");
3026 169 mohor
            fail = fail + 1;
3027
          end
3028
        end
3029
        else // with preamble
3030
        begin
3031
          if (cnt != 65) // at this value Mdio_IO is HIGH Z
3032
          begin
3033 181 mohor
            test_fail("Second scan request did not proceed correctly");
3034 169 mohor
            fail = fail + 1;
3035
          end
3036
        end
3037
      end
3038
      join
3039 181 mohor
      // check third to fifth scans
3040
      for (i3 = 0; i3 <= 2; i3 = i3 + 1)
3041
      begin
3042
        fork
3043 169 mohor
        begin
3044
          repeat(2) @(posedge Mdc_O);
3045
          // read data from PHY status register
3046
          #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3047 181 mohor
          if (phy_data !== tmp_data)
3048 169 mohor
          begin
3049 181 mohor
            test_fail("Data was not correctly scaned from status register");
3050
            fail = fail + 1;
3051 169 mohor
          end
3052
          // read data from MII status register
3053
          #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3054 181 mohor
          if (phy_data[0] !== 1'b0)
3055 169 mohor
          begin
3056 181 mohor
            test_fail("Link FAIL bit was set in the MII status register");
3057
            fail = fail + 1;
3058 169 mohor
          end
3059 181 mohor
          if (i3 == 2) // after fourth scan read
3060 169 mohor
          begin
3061 181 mohor
            @(posedge Mdc_O);
3062
            // change saved data
3063
            #Tp tmp_data = tmp_data & 16'hFFFB; // put bit 3 to ZERO
3064
            // set link down
3065
            #Tp eth_phy.link_up_down(0);
3066 169 mohor
          end
3067
        end
3068
        begin
3069 181 mohor
        // Completely check scans
3070
          #Tp cnt = 0;
3071
          // wait for serial bus to become active - second scan
3072
          wait(Mdio_IO !== 1'bz);
3073
          // count transfer length
3074
          while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i == 0)) || ((cnt == 15) && (i == 1)) )
3075 169 mohor
          begin
3076 181 mohor
            @(posedge Mdc_O);
3077
            #Tp cnt = cnt + 1;
3078
          end
3079
          // check transfer length
3080
          if (i) // without preamble
3081
          begin
3082
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
3083 169 mohor
            begin
3084 181 mohor
              test_fail("Fifth scan request did not proceed correctly");
3085
              fail = fail + 1;
3086 169 mohor
            end
3087 181 mohor
          end
3088
          else // with preamble
3089
          begin
3090
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
3091 169 mohor
            begin
3092 181 mohor
              test_fail("Fifth scan request did not proceed correctly");
3093
              fail = fail + 1;
3094 169 mohor
            end
3095
          end
3096
        end
3097 181 mohor
        join
3098
      end
3099
 
3100
      fork
3101
      begin
3102
        repeat(2) @(posedge Mdc_O);
3103
        // read data from PHY status register
3104
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3105
        if (phy_data !== tmp_data)
3106
        begin
3107
          test_fail("Data was not correctly scaned from status register");
3108
          fail = fail + 1;
3109
        end
3110
        // read data from MII status register
3111
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3112
        if (phy_data[0] === 1'b0)
3113
        begin
3114
          test_fail("Link FAIL bit was not set in the MII status register");
3115
          fail = fail + 1;
3116
        end
3117
        // wait to see if data stayed latched
3118
        repeat(4) @(posedge Mdc_O);
3119
        // read data from PHY status register
3120
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3121
        if (phy_data !== tmp_data)
3122
        begin
3123
          test_fail("Data was not latched correctly in status register");
3124
          fail = fail + 1;
3125
        end
3126
        // read data from MII status register
3127
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3128
        if (phy_data[0] === 1'b0)
3129
        begin
3130
          test_fail("Link FAIL bit was not set in the MII status register");
3131
          fail = fail + 1;
3132
        end
3133
        // change saved data
3134
        #Tp tmp_data = tmp_data | 16'h0004; // put bit 2 to ONE
3135
        // set link up
3136
        #Tp eth_phy.link_up_down(1);
3137
      end
3138
      begin
3139
      // Wait for sixth scan
3140
        // wait for serial bus to become active - sixth scan
3141
        wait(Mdio_IO !== 1'bz);
3142
        // wait for serial bus to become inactive - turn-around cycle in sixth scan
3143
        wait(Mdio_IO === 1'bz);
3144
        // wait for serial bus to become active - end of turn-around cycle in sixth scan
3145
        wait(Mdio_IO !== 1'bz);
3146
        // wait for serial bus to become inactive - end of sixth scan
3147
        wait(Mdio_IO === 1'bz);
3148
      end
3149 169 mohor
      join
3150 181 mohor
 
3151
      @(posedge Mdc_O);
3152 169 mohor
      // read data from PHY status register
3153
      #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3154
      if (phy_data !== tmp_data)
3155
      begin
3156 181 mohor
        test_fail("Data was not correctly scaned from status register");
3157 169 mohor
        fail = fail + 1;
3158
      end
3159
      // read data from MII status register
3160
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3161
      if (phy_data[0] !== 1'b0)
3162
      begin
3163
        test_fail("Link FAIL bit was set in the MII status register");
3164
        fail = fail + 1;
3165
      end
3166 181 mohor
      // wait to see if data stayed latched
3167
      repeat(4) @(posedge Mdc_O);
3168
      // read data from PHY status register
3169
      #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3170
      if (phy_data !== tmp_data)
3171 169 mohor
      begin
3172 181 mohor
        test_fail("Data was not correctly scaned from status register");
3173
        fail = fail + 1;
3174 169 mohor
      end
3175 181 mohor
      // read data from MII status register
3176
      #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3177
      if (phy_data[0] !== 1'b0)
3178 169 mohor
      begin
3179 181 mohor
        test_fail("Link FAIL bit was set in the MII status register");
3180
        fail = fail + 1;
3181 169 mohor
      end
3182 181 mohor
 
3183
      // STOP SCAN
3184
      #Tp mii_scan_finish; // finish scan operation
3185
      #Tp check_mii_busy; // wait for scan to finish
3186 169 mohor
    end
3187 181 mohor
    // set PHY to normal mode
3188
    #Tp eth_phy.preamble_suppresed(0);
3189
    // MII mode register
3190
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3191
    if(fail == 0)
3192
      test_ok;
3193
    else
3194
      fail = 0;
3195 169 mohor
  end
3196
 
3197
 
3198 181 mohor
  ////////////////////////////////////////////////////////////////////
3199
  ////                                                            ////
3200
  ////  Test scan status from phy with sliding link-fail bit      ////
3201
  ////  (with and without preamble)                               ////
3202
  ////                                                            ////
3203
  ////////////////////////////////////////////////////////////////////
3204
  if (test_num == 15) // 
3205 169 mohor
  begin
3206 194 tadej
    // TEST 15: SCAN STATUS FROM PHY WITH SLIDING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )
3207
    test_name   = "TEST 15: SCAN STATUS FROM PHY WITH SLIDING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )";
3208
    `TIME; $display("  TEST 15: SCAN STATUS FROM PHY WITH SLIDING LINK-FAIL BIT ( WITH AND WITHOUT PREAMBLE )");
3209 181 mohor
 
3210
    // set address
3211
    reg_addr = 5'h1; // status register
3212
    phy_addr = 5'h1; // correct PHY address
3213
 
3214
    // read request
3215
    #Tp mii_read_req(phy_addr, reg_addr);
3216
    check_mii_busy; // wait for read to finish
3217
    // read data from PHY status register - remember LINK-UP status
3218
    #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3219
 
3220
    for (i2 = 0; i2 <= 1; i2 = i2 + 1) // choose preamble or not
3221 169 mohor
    begin
3222 181 mohor
      #Tp eth_phy.preamble_suppresed(i2);
3223
      // MII mode register
3224
      #Tp wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i2, 8'h0}), 4'hF, 1, wbm_init_waits,
3225
                    wbm_subseq_waits);
3226
      if (i2)
3227 169 mohor
      begin
3228 181 mohor
        // change saved data when preamble is suppressed
3229
        #Tp tmp_data = tmp_data | 16'h0040; // put bit 6 to ONE
3230
      end
3231
 
3232
      i = 0;
3233
      while (i < 80) // delay for sliding of LinkFail bit
3234
      begin
3235
        // first there are two scans
3236
        #Tp cnt = 0;
3237 169 mohor
        // scan request
3238
        #Tp mii_scan_req(phy_addr, reg_addr);
3239 181 mohor
        #Tp check_mii_scan_valid; // wait for scan to make first data valid
3240
 
3241
        // check second scan
3242 169 mohor
        fork
3243 181 mohor
        begin
3244
          repeat(4) @(posedge Mdc_O);
3245
          // read data from PHY status register
3246
          #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3247
          if (phy_data !== tmp_data)
3248 169 mohor
          begin
3249 181 mohor
            test_fail("Second data was not correctly scaned from status register");
3250
            fail = fail + 1;
3251 169 mohor
          end
3252 181 mohor
          // read data from MII status register
3253
          #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3254
          if (phy_data[0] !== 1'b0)
3255
          begin
3256
            test_fail("Link FAIL bit was set in the MII status register");
3257
            fail = fail + 1;
3258
          end
3259
        end
3260
        begin
3261
        // Completely check scan
3262
          #Tp cnt = 0;
3263
          // wait for serial bus to become active - second scan
3264
          wait(Mdio_IO !== 1'bz);
3265
          // count transfer length
3266
          while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3267
          begin
3268
            @(posedge Mdc_O);
3269
            #Tp cnt = cnt + 1;
3270
          end
3271
          // check transfer length
3272
          if (i2) // without preamble
3273
          begin
3274
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
3275 169 mohor
            begin
3276 181 mohor
              test_fail("Second scan request did not proceed correctly");
3277
              fail = fail + 1;
3278 169 mohor
            end
3279 181 mohor
          end
3280
          else // with preamble
3281
          begin
3282
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
3283
            begin
3284
              test_fail("Second scan request did not proceed correctly");
3285
              fail = fail + 1;
3286
            end
3287
          end
3288
        end
3289
        join
3290
        // reset counter 
3291
        #Tp cnt = 0;
3292
        // SLIDING LINK DOWN and CHECK
3293
        fork
3294
          begin
3295
          // set link down
3296
            repeat(i) @(posedge Mdc_O);
3297
            // set link down
3298
            #Tp eth_phy.link_up_down(0);
3299
          end
3300
          begin
3301
          // check data in MII registers after each scan in this fork statement
3302 169 mohor
            if (i2) // without preamble
3303 181 mohor
              wait (cnt == 32);
3304
            else // with preamble
3305
              wait (cnt == 64);
3306
            repeat(3) @(posedge Mdc_O);
3307
            // read data from PHY status register
3308
            #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3309
            if ( ((i < 49) && !i2) || ((i < 17) && i2) )
3310 169 mohor
            begin
3311 181 mohor
              if (phy_data !== (tmp_data & 16'hFFFB)) // bit 3 is ZERO
3312 169 mohor
              begin
3313 181 mohor
                test_fail("Third data was not correctly scaned from status register");
3314 169 mohor
                fail = fail + 1;
3315
              end
3316
            end
3317 181 mohor
            else
3318 169 mohor
            begin
3319 181 mohor
              if (phy_data !== tmp_data)
3320 169 mohor
              begin
3321 181 mohor
                test_fail("Third data was not correctly scaned from status register");
3322 169 mohor
                fail = fail + 1;
3323
              end
3324
            end
3325 181 mohor
            // read data from MII status register
3326
            #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3327
            if ( ((i < 49) && !i2) || ((i < 17) && i2) )
3328 169 mohor
            begin
3329 181 mohor
              if (phy_data[0] === 1'b0)
3330
              begin
3331
                test_fail("Link FAIL bit was not set in the MII status register");
3332
                fail = fail + 1;
3333
              end
3334 169 mohor
            end
3335 181 mohor
            else
3336 169 mohor
            begin
3337 181 mohor
              if (phy_data[0] !== 1'b0)
3338 169 mohor
              begin
3339 181 mohor
                test_fail("Link FAIL bit was set in the MII status register");
3340 169 mohor
                fail = fail + 1;
3341
              end
3342
            end
3343 181 mohor
          end
3344
          begin
3345
          // check length
3346
            for (i3 = 0; i3 <= 1; i3 = i3 + 1) // two scans
3347 169 mohor
            begin
3348 181 mohor
              #Tp cnt = 0;
3349
              // wait for serial bus to become active if there is more than one scan
3350
              wait(Mdio_IO !== 1'bz);
3351
              // count transfer length
3352
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3353 169 mohor
              begin
3354 181 mohor
                @(posedge Mdc_O);
3355
                #Tp cnt = cnt + 1;
3356 169 mohor
              end
3357 181 mohor
              // check transfer length
3358
              if (i2) // without preamble
3359
              begin
3360
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3361
                begin
3362
                  test_fail("3. or 4. scan request did not proceed correctly, while SCAN STOP was written");
3363
                  fail = fail + 1;
3364
                end
3365
              end
3366
              else // with preamble
3367
              begin
3368
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3369
                begin
3370
                  test_fail("3. or 4. scan request did not proceed correctly, while SCAN STOP was written");
3371
                  fail = fail + 1;
3372
                end
3373
              end
3374 169 mohor
            end
3375
          end
3376
        join
3377 181 mohor
        // reset counter
3378
        #Tp cnt = 0;
3379
        // check fifth scan and data from fourth scan
3380
        fork
3381 169 mohor
        begin
3382 181 mohor
          repeat(2) @(posedge Mdc_O);
3383
          // read data from PHY status register
3384
          #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3385
          if (phy_data !== (tmp_data & 16'hFFFB)) // bit 3 is ZERO
3386 169 mohor
          begin
3387 181 mohor
            test_fail("4. data was not correctly scaned from status register");
3388
            fail = fail + 1;
3389 169 mohor
          end
3390 181 mohor
          // read data from MII status register
3391
          #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3392
          if (phy_data[0] === 1'b0)
3393 169 mohor
          begin
3394 181 mohor
            test_fail("Link FAIL bit was not set in the MII status register");
3395 169 mohor
            fail = fail + 1;
3396
          end
3397
        end
3398
        begin
3399 181 mohor
        // Completely check intermediate scan
3400
          #Tp cnt = 0;
3401
          // wait for serial bus to become active - second scan
3402 169 mohor
          wait(Mdio_IO !== 1'bz);
3403
          // count transfer length
3404
          while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3405
          begin
3406
            @(posedge Mdc_O);
3407
            #Tp cnt = cnt + 1;
3408
          end
3409 181 mohor
          // check transfer length
3410
          if (i2) // without preamble
3411 169 mohor
          begin
3412 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
3413
            begin
3414
              test_fail("Fifth scan request did not proceed correctly");
3415
              fail = fail + 1;
3416
            end
3417 169 mohor
          end
3418 181 mohor
          else // with preamble
3419
          begin
3420
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
3421
            begin
3422
              test_fail("Fifth scan request did not proceed correctly");
3423
              fail = fail + 1;
3424
            end
3425
          end
3426 169 mohor
        end
3427 181 mohor
        join
3428
        // reset counter 
3429
        #Tp cnt = 0;
3430
        // SLIDING LINK UP and CHECK
3431
        fork
3432 169 mohor
          begin
3433 181 mohor
          // set link up
3434
            repeat(i) @(posedge Mdc_O);
3435
            // set link up
3436
            #Tp eth_phy.link_up_down(1);
3437 169 mohor
          end
3438 181 mohor
          begin
3439
          // check data in MII registers after each scan in this fork statement
3440
            repeat(2) @(posedge Mdc_O);
3441
            if (i2) // without preamble
3442
              wait (cnt == 32);
3443
            else // with preamble
3444
              wait (cnt == 64);
3445
            repeat(3) @(posedge Mdc_O);
3446
            // read data from PHY status register
3447
            #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3448
            if ( ((i < 49) && !i2) || ((i < 17) && i2) )
3449
            begin
3450
              if (phy_data !== tmp_data)
3451
              begin
3452
                test_fail("6. data was not correctly scaned from status register");
3453
                fail = fail + 1;
3454
              end
3455
            end
3456
            else
3457
            begin
3458
              if (phy_data !== (tmp_data & 16'hFFFB)) // bit 3 is ZERO
3459
              begin
3460
                test_fail("6. data was not correctly scaned from status register");
3461
                fail = fail + 1;
3462
              end
3463
            end
3464
            // read data from MII status register
3465
            #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3466
            if ( ((i < 49) && !i2) || ((i < 17) && i2) )
3467
            begin
3468
              if (phy_data[0] !== 1'b0)
3469
              begin
3470
                test_fail("Link FAIL bit was set in the MII status register");
3471
                fail = fail + 1;
3472
              end
3473
            end
3474
            else
3475
            begin
3476
              if (phy_data[0] === 1'b0)
3477
              begin
3478
                test_fail("Link FAIL bit was not set in the MII status register");
3479
                fail = fail + 1;
3480
              end
3481
            end
3482
          end
3483
          begin
3484
          // check length
3485
            for (i3 = 0; i3 <= 1; i3 = i3 + 1) // two scans
3486
            begin
3487
              #Tp cnt = 0;
3488
              // wait for serial bus to become active if there is more than one scan
3489
              wait(Mdio_IO !== 1'bz);
3490
              // count transfer length
3491
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3492
              begin
3493
                @(posedge Mdc_O);
3494
                #Tp cnt = cnt + 1;
3495
              end
3496
              // check transfer length
3497
              if (i2) // without preamble
3498
              begin
3499
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3500
                begin
3501
                  test_fail("Scan request did not proceed correctly, while SCAN STOP was written");
3502
                  fail = fail + 1;
3503
                end
3504
              end
3505
              else // with preamble
3506
              begin
3507
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3508
                begin
3509
                  test_fail("Scan request did not proceed correctly, while SCAN STOP was written");
3510
                  fail = fail + 1;
3511
                end
3512
              end
3513
            end
3514
          end
3515
        join
3516
        // check last scan 
3517
        repeat(4) @(posedge Mdc_O);
3518
        // read data from PHY status register
3519
        #Tp wbm_read(`ETH_MIIRX_DATA, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3520
        if (phy_data !== tmp_data)
3521
        begin
3522
          test_fail("7. data was not correctly scaned from status register");
3523
          fail = fail + 1;
3524 169 mohor
        end
3525 181 mohor
        // read data from MII status register
3526
        #Tp wbm_read(`ETH_MIISTATUS, phy_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3527
        if (phy_data[0] !== 1'b0)
3528
        begin
3529
          test_fail("Link FAIL bit was set in the MII status register");
3530
          fail = fail + 1;
3531
        end
3532
 
3533
        #Tp mii_scan_finish; // finish scan operation
3534
        #Tp check_mii_busy; // wait for scan to finish
3535
        #Tp;
3536
        // set delay of writing the command
3537
        if (i2) // without preamble
3538
        begin
3539
          case(i)
3540
            0,  1,  2,  3,  4:  i = i + 1;
3541
            13, 14, 15, 16, 17,
3542
            18, 19, 20, 21, 22,
3543
            23, 24, 25, 26, 27,
3544
            28, 29, 30, 31, 32,
3545
            33, 34, 35:         i = i + 1;
3546
            36:                 i = 80;
3547
            default:            i = 13;
3548
          endcase
3549
        end
3550 169 mohor
        else // with preamble
3551
        begin
3552 181 mohor
          case(i)
3553
            0,  1,  2,  3,  4:  i = i + 1;
3554
            45, 46, 47, 48, 49,
3555
            50, 51, 52, 53, 54,
3556
            55, 56, 57, 58, 59,
3557
            60, 61, 62, 63, 64,
3558
            65, 66, 67:         i = i + 1;
3559
            68:                 i = 80;
3560
            default:            i = 45;
3561
          endcase
3562 169 mohor
        end
3563 181 mohor
        @(posedge wb_clk);
3564
        #Tp;
3565 169 mohor
      end
3566
    end
3567 181 mohor
    // set PHY to normal mode
3568
    #Tp eth_phy.preamble_suppresed(0);
3569
    // MII mode register
3570
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3571
    if(fail == 0)
3572
      test_ok;
3573
    else
3574
      fail = 0;
3575 169 mohor
  end
3576
 
3577
 
3578 181 mohor
  ////////////////////////////////////////////////////////////////////
3579
  ////                                                            ////
3580
  ////  Test sliding stop scan command immediately after scan     ////
3581
  ////  request (with and without preamble)                       ////
3582
  ////                                                            ////
3583
  ////////////////////////////////////////////////////////////////////
3584
  if (test_num == 16) // 
3585 116 mohor
  begin
3586 194 tadej
    // TEST 16: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER SCAN REQUEST ( WITH AND WITHOUT PREAMBLE )
3587
    test_name = "TEST 16: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER SCAN REQUEST ( WITH AND WITHOUT PREAMBLE )";
3588 181 mohor
    `TIME;
3589 194 tadej
    $display("  TEST 16: SLIDING STOP SCAN COMMAND IMMEDIATELY AFTER SCAN REQUEST ( WITH AND WITHOUT PREAMBLE )");
3590 181 mohor
 
3591
    for (i2 = 0; i2 <= 1; i2 = i2 + 1) // choose preamble or not
3592 169 mohor
    begin
3593 181 mohor
      #Tp eth_phy.preamble_suppresed(i2);
3594
      // MII mode register
3595
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i2, 8'h0}), 4'hF, 1, wbm_init_waits,
3596
                wbm_subseq_waits);
3597
      i = 0;
3598
      cnt = 0;
3599
      while (i < 80) // delay for sliding of writing a STOP SCAN command
3600 169 mohor
      begin
3601 181 mohor
        for (i3 = 0; i3 <= 1; i3 = i3 + 1) // choose read or write after scan will be finished
3602 169 mohor
        begin
3603 181 mohor
          // set address
3604
          reg_addr = 5'h0; // control register
3605
          phy_addr = 5'h1; // correct PHY address
3606
          cnt = 0;
3607
          // scan request
3608
          #Tp mii_scan_req(phy_addr, reg_addr);
3609
          fork
3610
            begin
3611
              repeat(i) @(posedge Mdc_O);
3612
              // write command 0x0 into MII command register
3613
              // MII command written while scan in progress
3614
              wbm_write(`ETH_MIICOMMAND, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3615
              @(posedge wb_clk);
3616
              #Tp check_mii_busy; // wait for scan to finish
3617
              @(posedge wb_clk);
3618
              disable check;
3619
            end
3620
            begin: check
3621
              // wait for serial bus to become active
3622
              wait(Mdio_IO !== 1'bz);
3623
              // count transfer length
3624
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3625
              begin
3626
                @(posedge Mdc_O);
3627
                #Tp cnt = cnt + 1;
3628
              end
3629
              // check transfer length
3630
              if (i2) // without preamble
3631
              begin
3632
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3633
                begin
3634
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3635
                  fail = fail + 1;
3636
                end
3637
              end
3638
              else // with preamble
3639
              begin
3640
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3641
                begin
3642
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3643
                  fail = fail + 1;
3644
                end
3645
              end
3646
              cnt = 0;
3647
              // wait for serial bus to become active if there is more than one scan
3648
              wait(Mdio_IO !== 1'bz);
3649
              // count transfer length
3650
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3651
              begin
3652
                @(posedge Mdc_O);
3653
                #Tp cnt = cnt + 1;
3654
              end
3655
              // check transfer length
3656
              if (i2) // without preamble
3657
              begin
3658
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3659
                begin
3660
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3661
                  fail = fail + 1;
3662
                end
3663
              end
3664
              else // with preamble
3665
              begin
3666
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3667
                begin
3668
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3669
                  fail = fail + 1;
3670
                end
3671
              end
3672
            end
3673
          join
3674
          // check the BUSY signal to see if the bus is still IDLE
3675
          for (i1 = 0; i1 < 8; i1 = i1 + 1)
3676
            check_mii_busy; // wait for bus to become idle
3677
 
3678
          // try normal write or read after scan was finished
3679
          phy_data = {8'h7D, (i[7:0] + 1)};
3680
          cnt = 0;
3681
          if (i3 == 0) // write after scan
3682 169 mohor
          begin
3683 181 mohor
            // write request
3684
            #Tp mii_write_req(phy_addr, reg_addr, phy_data);
3685
            // wait for serial bus to become active
3686
            wait(Mdio_IO !== 1'bz);
3687
            // count transfer length
3688
            while(Mdio_IO !== 1'bz)
3689
            begin
3690
              @(posedge Mdc_O);
3691
              #Tp cnt = cnt + 1;
3692
            end
3693 169 mohor
            @(posedge Mdc_O);
3694 181 mohor
            // read request
3695
            #Tp mii_read_req(phy_addr, reg_addr);
3696
            check_mii_busy; // wait for read to finish
3697
            // read and check data
3698
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3699
            if (phy_data !== tmp_data)
3700 169 mohor
            begin
3701 181 mohor
              test_fail("Data was not correctly written into OR read from PHY register - control register");
3702 169 mohor
              fail = fail + 1;
3703
            end
3704
          end
3705 181 mohor
          else // read after scan
3706 169 mohor
          begin
3707 181 mohor
            // read request
3708
            #Tp mii_read_req(phy_addr, reg_addr);
3709
            // wait for serial bus to become active
3710
            wait(Mdio_IO !== 1'bz);
3711
            // count transfer length
3712
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3713 169 mohor
            begin
3714 181 mohor
              @(posedge Mdc_O);
3715
              #Tp cnt = cnt + 1;
3716
            end
3717
            @(posedge Mdc_O);
3718
            check_mii_busy; // wait for read to finish
3719
            // read and check data
3720
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3721
            if (phy_data !== tmp_data)
3722
            begin
3723
              test_fail("Data was not correctly written into OR read from PHY register - control register");
3724 169 mohor
              fail = fail + 1;
3725
            end
3726
          end
3727 181 mohor
          // check if transfer was a proper length
3728 169 mohor
          if (i2) // without preamble
3729
          begin
3730
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
3731
            begin
3732 181 mohor
              test_fail("New request did not proceed correctly, after scan request");
3733 169 mohor
              fail = fail + 1;
3734
            end
3735
          end
3736
          else // with preamble
3737
          begin
3738
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
3739
            begin
3740 181 mohor
              test_fail("New request did not proceed correctly, after scan request");
3741 169 mohor
              fail = fail + 1;
3742
            end
3743
          end
3744
        end
3745 181 mohor
        #Tp;
3746
        // set delay of writing the command
3747
        if (i2) // without preamble
3748
        begin
3749
          case(i)
3750
            0, 1:               i = i + 1;
3751
            18, 19, 20, 21, 22,
3752
            23, 24, 25, 26, 27,
3753
            28, 29, 30, 31, 32,
3754
            33, 34, 35:         i = i + 1;
3755
            36:                 i = 80;
3756
            default:            i = 18;
3757
          endcase
3758
        end
3759
        else // with preamble
3760
        begin
3761
          case(i)
3762
            0, 1:               i = i + 1;
3763
            50, 51, 52, 53, 54,
3764
            55, 56, 57, 58, 59,
3765
            60, 61, 62, 63, 64,
3766
            65, 66, 67:         i = i + 1;
3767
            68:                 i = 80;
3768
            default:            i = 50;
3769
          endcase
3770
        end
3771
        @(posedge wb_clk);
3772
      end
3773
    end
3774
    // set PHY to normal mode
3775
    #Tp eth_phy.preamble_suppresed(0);
3776
    // MII mode register
3777
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3778
    if(fail == 0)
3779
      test_ok;
3780
    else
3781
      fail = 0;
3782
  end
3783 169 mohor
 
3784 181 mohor
 
3785
  ////////////////////////////////////////////////////////////////////
3786
  ////                                                            ////
3787
  ////  Test sliding stop scan command after 2. scan (with and    ////
3788
  ////  without preamble)                                         ////
3789
  ////                                                            ////
3790
  ////////////////////////////////////////////////////////////////////
3791
  if (test_num == 17) // 
3792
  begin
3793 194 tadej
    // TEST 17: SLIDING STOP SCAN COMMAND AFTER 2. SCAN ( WITH AND WITHOUT PREAMBLE )
3794
    test_name = "TEST 17: SLIDING STOP SCAN COMMAND AFTER 2. SCAN ( WITH AND WITHOUT PREAMBLE )";
3795
    `TIME; $display("  TEST 17: SLIDING STOP SCAN COMMAND AFTER 2. SCAN ( WITH AND WITHOUT PREAMBLE )");
3796 181 mohor
 
3797
    for (i2 = 0; i2 <= 1; i2 = i2 + 1) // choose preamble or not
3798
    begin
3799
      #Tp eth_phy.preamble_suppresed(i2);
3800
      // MII mode register
3801
      wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_NOPRE & {23'h0, i2, 8'h0}), 4'hF, 1, wbm_init_waits,
3802
                wbm_subseq_waits);
3803
 
3804
      i = 0;
3805
      cnt = 0;
3806
      while (i < 80) // delay for sliding of writing a STOP SCAN command
3807
      begin
3808
        for (i3 = 0; i3 <= 1; i3 = i3 + 1) // choose read or write after scan will be finished
3809
        begin
3810
          // first there are two scans
3811
          // set address
3812
          reg_addr = 5'h0; // control register
3813
          phy_addr = 5'h1; // correct PHY address
3814
          cnt = 0;
3815
          // scan request
3816
          #Tp mii_scan_req(phy_addr, reg_addr);
3817
          // wait and check first 2 scans
3818 169 mohor
          begin
3819
            // wait for serial bus to become active
3820
            wait(Mdio_IO !== 1'bz);
3821
            // count transfer length
3822
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3823
            begin
3824
              @(posedge Mdc_O);
3825
              #Tp cnt = cnt + 1;
3826
            end
3827
            // check transfer length
3828
            if (i2) // without preamble
3829
            begin
3830
              if (cnt != 33) // at this value Mdio_IO is HIGH Z
3831
              begin
3832
                test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3833
                fail = fail + 1;
3834
              end
3835
            end
3836
            else // with preamble
3837
            begin
3838
              if (cnt != 65) // at this value Mdio_IO is HIGH Z
3839
              begin
3840
                test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3841
                fail = fail + 1;
3842
              end
3843
            end
3844
            cnt = 0;
3845
            // wait for serial bus to become active if there is more than one scan
3846
            wait(Mdio_IO !== 1'bz);
3847
            // count transfer length
3848
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3849
            begin
3850
              @(posedge Mdc_O);
3851
              #Tp cnt = cnt + 1;
3852
            end
3853
            // check transfer length
3854
            if (i2) // without preamble
3855
            begin
3856
              if (cnt != 33) // at this value Mdio_IO is HIGH Z
3857
              begin
3858
                test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3859
                fail = fail + 1;
3860
              end
3861
            end
3862
            else // with preamble
3863
            begin
3864
              if (cnt != 65) // at this value Mdio_IO is HIGH Z
3865
              begin
3866
                test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3867
                fail = fail + 1;
3868
              end
3869
            end
3870
          end
3871
 
3872 181 mohor
          // reset counter 
3873
          cnt = 0;
3874
          fork
3875
            begin
3876
              repeat(i) @(posedge Mdc_O);
3877
              // write command 0x0 into MII command register
3878
              // MII command written while scan in progress
3879
              wbm_write(`ETH_MIICOMMAND, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3880
              @(posedge wb_clk);
3881
              #Tp check_mii_busy; // wait for scan to finish
3882
              @(posedge wb_clk);
3883
              disable check_3;
3884
            end
3885
            begin: check_3
3886
              // wait for serial bus to become active
3887
              wait(Mdio_IO !== 1'bz);
3888
              // count transfer length
3889
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3890
              begin
3891
                @(posedge Mdc_O);
3892
                #Tp cnt = cnt + 1;
3893
              end
3894
              // check transfer length
3895
              if (i2) // without preamble
3896
              begin
3897
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3898
                begin
3899
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3900
                  fail = fail + 1;
3901
                end
3902
              end
3903
              else // with preamble
3904
              begin
3905
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3906
                begin
3907
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3908
                  fail = fail + 1;
3909
                end
3910
              end
3911
              cnt = 0;
3912
              // wait for serial bus to become active if there is more than one scan
3913
              wait(Mdio_IO !== 1'bz);
3914
              // count transfer length
3915
              while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3916
              begin
3917
                @(posedge Mdc_O);
3918
                #Tp cnt = cnt + 1;
3919
              end
3920
              // check transfer length
3921
              if (i2) // without preamble
3922
              begin
3923
                if (cnt != 33) // at this value Mdio_IO is HIGH Z
3924
                begin
3925
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3926
                  fail = fail + 1;
3927
                end
3928
              end
3929
              else // with preamble
3930
              begin
3931
                if (cnt != 65) // at this value Mdio_IO is HIGH Z
3932
                begin
3933
                  test_fail("First scan request did not proceed correctly, while SCAN STOP was written");
3934
                  fail = fail + 1;
3935
                end
3936
              end
3937
            end
3938
          join
3939
          // check the BUSY signal to see if the bus is still IDLE
3940
          for (i1 = 0; i1 < 8; i1 = i1 + 1)
3941
            check_mii_busy; // wait for bus to become idle
3942
 
3943
          // try normal write or read after scan was finished
3944
          phy_data = {8'h7D, (i[7:0] + 1)};
3945
          cnt = 0;
3946
          if (i3 == 0) // write after scan
3947 169 mohor
          begin
3948 181 mohor
            // write request
3949
            #Tp mii_write_req(phy_addr, reg_addr, phy_data);
3950
            // wait for serial bus to become active
3951
            wait(Mdio_IO !== 1'bz);
3952
            // count transfer length
3953
            while(Mdio_IO !== 1'bz)
3954
            begin
3955
              @(posedge Mdc_O);
3956
              #Tp cnt = cnt + 1;
3957
            end
3958 169 mohor
            @(posedge Mdc_O);
3959 181 mohor
            // read request
3960
            #Tp mii_read_req(phy_addr, reg_addr);
3961
            check_mii_busy; // wait for read to finish
3962
            // read and check data
3963
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3964
            if (phy_data !== tmp_data)
3965
            begin
3966
              test_fail("Data was not correctly written into OR read from PHY register - control register");
3967
              fail = fail + 1;
3968
            end
3969 169 mohor
          end
3970 181 mohor
          else // read after scan
3971 169 mohor
          begin
3972 181 mohor
            // read request
3973
            #Tp mii_read_req(phy_addr, reg_addr);
3974
            // wait for serial bus to become active
3975
            wait(Mdio_IO !== 1'bz);
3976
            // count transfer length
3977
            while( (Mdio_IO !== 1'bz) || ((cnt == 47) && (i2 == 0)) || ((cnt == 15) && (i2 == 1)) )
3978
            begin
3979
              @(posedge Mdc_O);
3980
              #Tp cnt = cnt + 1;
3981
            end
3982
            @(posedge Mdc_O);
3983
            check_mii_busy; // wait for read to finish
3984
            // read and check data
3985
            #Tp wbm_read(`ETH_MIIRX_DATA, tmp_data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
3986
            if (phy_data !== tmp_data)
3987
            begin
3988
              test_fail("Data was not correctly written into OR read from PHY register - control register");
3989
              fail = fail + 1;
3990
            end
3991 169 mohor
          end
3992 181 mohor
          // check if transfer was a proper length
3993
          if (i2) // without preamble
3994 169 mohor
          begin
3995 181 mohor
            if (cnt != 33) // at this value Mdio_IO is HIGH Z
3996
            begin
3997
              test_fail("New request did not proceed correctly, after scan request");
3998
              fail = fail + 1;
3999
            end
4000 169 mohor
          end
4001 181 mohor
          else // with preamble
4002 169 mohor
          begin
4003 181 mohor
            if (cnt != 65) // at this value Mdio_IO is HIGH Z
4004
            begin
4005
              test_fail("New request did not proceed correctly, after scan request");
4006
              fail = fail + 1;
4007
            end
4008 169 mohor
          end
4009
        end
4010 181 mohor
        #Tp;
4011
        // set delay of writing the command
4012 169 mohor
        if (i2) // without preamble
4013
        begin
4014 181 mohor
          case(i)
4015
            0, 1:               i = i + 1;
4016
            18, 19, 20, 21, 22,
4017
            23, 24, 25, 26, 27,
4018
            28, 29, 30, 31, 32,
4019
            33, 34, 35:         i = i + 1;
4020
            36:                 i = 80;
4021
            default:            i = 18;
4022
          endcase
4023 169 mohor
        end
4024
        else // with preamble
4025
        begin
4026 181 mohor
          case(i)
4027
            0, 1:               i = i + 1;
4028
            50, 51, 52, 53, 54,
4029
            55, 56, 57, 58, 59,
4030
            60, 61, 62, 63, 64,
4031
            65, 66, 67:         i = i + 1;
4032
            68:                 i = 80;
4033
            default:            i = 50;
4034
          endcase
4035 169 mohor
        end
4036 181 mohor
        @(posedge wb_clk);
4037 116 mohor
      end
4038
    end
4039 181 mohor
    // set PHY to normal mode
4040
    #Tp eth_phy.preamble_suppresed(0);
4041
    // MII mode register
4042
    wbm_write(`ETH_MIIMODER, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4043
    if(fail == 0)
4044
      test_ok;
4045
    else
4046
      fail = 0;
4047 169 mohor
  end
4048 116 mohor
 
4049 181 mohor
end   //  for (test_num=start_task; test_num <= end_task; test_num=test_num+1)
4050
 
4051 169 mohor
end
4052
endtask // test_mii
4053
 
4054
 
4055
task test_mac_full_duplex_transmit;
4056
  input  [31:0]  start_task;
4057
  input  [31:0]  end_task;
4058
  integer        bit_start_1;
4059
  integer        bit_end_1;
4060
  integer        bit_start_2;
4061
  integer        bit_end_2;
4062
  integer        num_of_reg;
4063 209 tadejm
  integer        num_of_frames;
4064
  integer        num_of_bd;
4065 169 mohor
  integer        i_addr;
4066
  integer        i_data;
4067
  integer        i_length;
4068 209 tadejm
  integer        tmp_len;
4069
  integer        tmp_bd;
4070
  integer        tmp_bd_num;
4071 169 mohor
  integer        tmp_data;
4072 209 tadejm
  integer        tmp_ipgt;
4073 194 tadej
  integer        test_num;
4074 169 mohor
  reg    [31:0]  tx_bd_num;
4075
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_data;
4076
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_tmp_data;
4077
  integer        i;
4078
  integer        i1;
4079
  integer        i2;
4080
  integer        i3;
4081
  integer        fail;
4082
  integer        speed;
4083 209 tadejm
  reg            frame_started;
4084
  reg            frame_ended;
4085
  reg            wait_for_frame;
4086 169 mohor
  reg    [31:0]  addr;
4087
  reg    [31:0]  data;
4088
  reg    [31:0]  tmp;
4089
  reg    [ 7:0]  st_data;
4090
  reg    [15:0]  max_tmp;
4091
  reg    [15:0]  min_tmp;
4092
begin
4093
// MAC FULL DUPLEX TRANSMIT TEST
4094
test_heading("MAC FULL DUPLEX TRANSMIT TEST");
4095
$display(" ");
4096
$display("MAC FULL DUPLEX TRANSMIT TEST");
4097
fail = 0;
4098
 
4099
// reset MAC registers
4100
hard_reset;
4101
// reset MAC and MII LOGIC with soft reset
4102
reset_mac;
4103
reset_mii;
4104
// set wb slave response
4105
wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
4106
 
4107
  /*
4108
  TASKS for set and control TX buffer descriptors (also send packet - set_tx_bd_ready):
4109
  -------------------------------------------------------------------------------------
4110
  set_tx_bd
4111
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0], len[15:0], irq, pad, crc, txpnt[31:0]);
4112
  set_tx_bd_wrap
4113
    (tx_bd_num_end[6:0]);
4114
  set_tx_bd_ready
4115
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
4116
  check_tx_bd
4117
    (tx_bd_num_start[6:0], tx_bd_status[31:0]);
4118
  clear_tx_bd
4119
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
4120
 
4121
  TASKS for set and control RX buffer descriptors:
4122
  ------------------------------------------------
4123
  set_rx_bd
4124
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0], irq, rxpnt[31:0]);
4125
  set_rx_bd_wrap
4126
    (rx_bd_num_end[6:0]);
4127
  set_rx_bd_empty
4128
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
4129
  check_rx_bd
4130
    (rx_bd_num_end[6:0], rx_bd_status);
4131
  clear_rx_bd
4132
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
4133
 
4134
  TASKS for set and check TX packets:
4135
  -----------------------------------
4136
  set_tx_packet
4137
    (txpnt[31:0], len[15:0], eth_start_data[7:0]);
4138
  check_tx_packet
4139
    (txpnt_wb[31:0], txpnt_phy[31:0], len[15:0], failure[31:0]);
4140
 
4141
  TASKS for set and check RX packets:
4142
  -----------------------------------
4143
  set_rx_packet
4144
    (rxpnt[31:0], len[15:0], plus_nibble, d_addr[47:0], s_addr[47:0], type_len[15:0], start_data[7:0]);
4145
  check_rx_packet
4146
    (rxpnt_phy[31:0], rxpnt_wb[31:0], len[15:0], plus_nibble, successful_nibble, failure[31:0]);
4147
 
4148
  TASKS for append and check CRC to/of TX packet:
4149
  -----------------------------------------------
4150
  append_tx_crc
4151
    (txpnt_wb[31:0], len[15:0], negated_crc);
4152
  check_tx_crc
4153
    (txpnt_phy[31:0], len[15:0], negated_crc, failure[31:0]);
4154
 
4155
  TASK for append CRC to RX packet (CRC is checked together with check_rx_packet):
4156
  --------------------------------------------------------------------------------
4157
  append_rx_crc
4158
    (rxpnt_phy[31:0], len[15:0], plus_nibble, negated_crc);
4159
  */
4160
 
4161 194 tadej
//////////////////////////////////////////////////////////////////////
4162
////                                                              ////
4163
////  test_mac_full_duplex_transmit:                              ////
4164
////                                                              ////
4165
////  0: Test no transmit when all buffers are RX ( 10Mbps ).     ////
4166
////  1: Test no transmit when all buffers are RX ( 100Mbps ).    ////
4167
////  2: Test transmit packets form MINFL to MAXFL sizes at       ////
4168
////     one TX buffer decriptor ( 10Mbps ).                      ////
4169
////  3: Test transmit packets form MINFL to MAXFL sizes at       ////
4170
////     one TX buffer decriptor ( 100Mbps ).                     ////
4171
////                                                              ////
4172
//////////////////////////////////////////////////////////////////////
4173
for (test_num = start_task; test_num <= end_task; test_num = test_num + 1)
4174 169 mohor
begin
4175
 
4176 194 tadej
  ////////////////////////////////////////////////////////////////////
4177
  ////                                                            ////
4178
  ////  Test no transmit when all buffers are RX ( 10Mbps ).      ////
4179
  ////                                                            ////
4180
  ////////////////////////////////////////////////////////////////////
4181
  if (test_num == 0) // Test no transmit when all buffers are RX ( 10Mbps ).
4182 169 mohor
  begin
4183 194 tadej
    // TEST 0: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 10Mbps )
4184
    test_name   = "TEST 0: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 10Mbps )";
4185
    `TIME; $display("  TEST 0: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 10Mbps )");
4186
 
4187
    // unmask interrupts
4188 209 tadejm
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4189 194 tadej
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4190
    // set all buffer descriptors to RX - must be set before TX enable
4191
    wbm_write(`ETH_TX_BD_NUM, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4192
    // enable TX, set full-duplex mode, padding and CRC appending
4193
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4194
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4195
 
4196
    // write to phy's control register for 10Mbps
4197
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
4198
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
4199
    speed = 10;
4200
 
4201
    i = 0;
4202
    while (i < 128)
4203 169 mohor
    begin
4204 194 tadej
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
4205 169 mohor
      begin
4206 194 tadej
        set_tx_packet((`MEMORY_BASE + (i1 * 200)), 100, 0);
4207
        set_tx_bd(i1, i1, 100, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + (i1 * 200)));
4208 169 mohor
      end
4209 194 tadej
      set_tx_bd_wrap(i);
4210
      fork
4211
        begin
4212
          set_tx_bd_ready(0, i);
4213
          repeat(20) @(negedge mtx_clk);
4214
          #1 disable check_tx_en10;
4215
        end
4216
        begin: check_tx_en10
4217
          wait (MTxEn === 1'b1);
4218
          test_fail("Tramsmit should not start at all");
4219
          fail = fail + 1;
4220
          `TIME; $display("*E Transmit of %d packets should not start at all - active MTxEn", i);
4221
        end
4222
      join
4223
      for (i2 = 0; i2 < 20; i2 = i2 + 1)
4224 169 mohor
      begin
4225 194 tadej
        check_tx_bd(0, tmp);
4226
        #1;
4227
        if (tmp[15] === 1'b0)
4228
        begin
4229
          test_fail("Tramsmit should not start at all");
4230
          fail = fail + 1;
4231
          `TIME; $display("*E Transmit of %d packets should not start at all - ready is 0", i);
4232
        end
4233
        if (tmp[8:0] !== 0)
4234
        begin
4235
          test_fail("Tramsmit should not be finished since it should not start at all");
4236
          fail = fail + 1;
4237
          `TIME; $display("*E Transmit of should not be finished since it should not start at all");
4238
        end
4239
        @(posedge wb_clk);
4240 169 mohor
      end
4241 194 tadej
      wbm_read(`ETH_INT, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4242
      if (tmp[6:0] !== 0)
4243 169 mohor
      begin
4244 194 tadej
        test_fail("Tramsmit should not get INT since it should not start at all");
4245 169 mohor
        fail = fail + 1;
4246 194 tadej
        `TIME; $display("*E Transmit of should not get INT since it should not start at all");
4247 169 mohor
      end
4248 194 tadej
      clear_tx_bd(0, i);
4249
      if ((i < 5) || (i > 124))
4250
        i = i + 1;
4251
      else
4252
        i = i + 120;
4253 116 mohor
    end
4254 194 tadej
    // disable TX
4255
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4256
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4257
    if(fail == 0)
4258
      test_ok;
4259 116 mohor
    else
4260 194 tadej
      fail = 0;
4261 169 mohor
  end
4262 116 mohor
 
4263
 
4264 194 tadej
  ////////////////////////////////////////////////////////////////////
4265
  ////                                                            ////
4266
  ////  Test no transmit when all buffers are RX ( 100Mbps ).     ////
4267
  ////                                                            ////
4268
  ////////////////////////////////////////////////////////////////////
4269
  if (test_num == 1) // Test no transmit when all buffers are RX ( 100Mbps ).
4270 169 mohor
  begin
4271 194 tadej
    // TEST 1: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 100Mbps )
4272
    test_name   = "TEST 1: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 100Mbps )";
4273
    `TIME; $display("  TEST 1: NO TRANSMIT WHEN ALL BUFFERS ARE RX ( 100Mbps )");
4274
 
4275
    // unmask interrupts
4276 209 tadejm
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4277 194 tadej
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4278
    // set all buffer descriptors to RX - must be set before TX enable
4279
    wbm_write(`ETH_TX_BD_NUM, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4280
    // enable TX, set full-duplex mode, padding and CRC appending
4281
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4282
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4283
 
4284
    // write to phy's control register for 100Mbps
4285
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
4286
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
4287
    speed = 100;
4288
 
4289
    i = 0;
4290
    while (i < 128)
4291 169 mohor
    begin
4292 194 tadej
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
4293 169 mohor
      begin
4294 194 tadej
        set_tx_packet((`MEMORY_BASE + (i1 * 200)), 100, 0);
4295
        set_tx_bd(i1, i1, 100, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + (i1 * 200)));
4296 169 mohor
      end
4297 194 tadej
      set_tx_bd_wrap(i);
4298
      fork
4299
        begin
4300
          set_tx_bd_ready(0, i);
4301
          repeat(20) @(negedge mtx_clk);
4302
          #1 disable check_tx_en100;
4303
        end
4304
        begin: check_tx_en100
4305
          wait (MTxEn === 1'b1);
4306
          test_fail("Tramsmit should not start at all");
4307
          fail = fail + 1;
4308
          `TIME; $display("*E Transmit of %d packets should not start at all - active MTxEn", i);
4309
        end
4310
      join
4311
      for (i2 = 0; i2 < 20; i2 = i2 + 1)
4312 169 mohor
      begin
4313 194 tadej
        check_tx_bd(0, tmp);
4314
        #1;
4315
        if (tmp[15] === 1'b0)
4316
        begin
4317
          test_fail("Tramsmit should not start at all");
4318
          fail = fail + 1;
4319
          `TIME; $display("*E Transmit of %d packets should not start at all - ready is 0", i);
4320
        end
4321
        if (tmp[8:0] !== 0)
4322
        begin
4323
          test_fail("Tramsmit should not be finished since it should not start at all");
4324
          fail = fail + 1;
4325
          `TIME; $display("*E Transmit of should not be finished since it should not start at all");
4326
        end
4327
        @(posedge wb_clk);
4328 169 mohor
      end
4329 194 tadej
      wbm_read(`ETH_INT, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4330
      if (tmp[6:0] !== 0)
4331 169 mohor
      begin
4332 194 tadej
        test_fail("Tramsmit should not get INT since it should not start at all");
4333 169 mohor
        fail = fail + 1;
4334 194 tadej
        `TIME; $display("*E Transmit of should not get INT since it should not start at all");
4335 169 mohor
      end
4336 194 tadej
      clear_tx_bd(0, i);
4337
      if ((i < 5) || (i > 124))
4338
        i = i + 1;
4339
      else
4340
        i = i + 120;
4341 169 mohor
    end
4342 194 tadej
    // disable TX
4343
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4344
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4345
    if(fail == 0)
4346
      test_ok;
4347 169 mohor
    else
4348 194 tadej
      fail = 0;
4349 169 mohor
  end
4350
 
4351
 
4352 194 tadej
  ////////////////////////////////////////////////////////////////////
4353
  ////                                                            ////
4354
  ////  Test transmit packets form MINFL to MAXFL sizes at        ////
4355
  ////  one TX buffer decriptor ( 10Mbps ).                       ////
4356
  ////                                                            ////
4357
  ////////////////////////////////////////////////////////////////////
4358 209 tadejm
  if (test_num == 2) // without and with padding
4359 169 mohor
  begin
4360 194 tadej
    // TEST 2: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 10Mbps )
4361
    test_name = "TEST 2: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 10Mbps )";
4362
    `TIME; $display("  TEST 2: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 10Mbps )");
4363
 
4364
    max_tmp = 0;
4365
    min_tmp = 0;
4366
    // set one TX buffer descriptor - must be set before TX enable
4367
    wbm_write(`ETH_TX_BD_NUM, 32'h1, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4368 209 tadejm
    // enable TX, set full-duplex mode, NO padding and CRC appending
4369
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
4370 194 tadej
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4371
    // prepare two packets of MAXFL length
4372
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4373
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
4374
    min_tmp = tmp[31:16];
4375 209 tadejm
    st_data = 8'h01;
4376
    set_tx_packet(`MEMORY_BASE, (max_tmp), st_data); // length without CRC
4377 194 tadej
    st_data = 8'h10;
4378 209 tadejm
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp), st_data); // length without CRC
4379 194 tadej
    // check WB INT signal
4380
    if (wb_int !== 1'b0)
4381 169 mohor
    begin
4382 194 tadej
      test_fail("WB INT signal should not be set");
4383
      fail = fail + 1;
4384 169 mohor
    end
4385 194 tadej
 
4386
    // write to phy's control register for 10Mbps
4387
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
4388
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
4389
    speed = 10;
4390
 
4391
    i_length = (min_tmp - 4);
4392
    while (i_length <= (max_tmp - 4))
4393 169 mohor
    begin
4394 194 tadej
      // choose generating carrier sense and collision for first and last 64 lengths of frames
4395
      case (i_length[1:0])
4396
      2'h0: // Interrupt is generated
4397 169 mohor
      begin
4398 194 tadej
        // enable interrupt generation
4399 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
4400 194 tadej
        // unmask interrupts
4401 209 tadejm
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4402 194 tadej
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4403
        // not detect carrier sense in FD and no collision
4404
        eth_phy.carrier_sense_tx_fd_detect(0);
4405
        eth_phy.collision(0);
4406 169 mohor
      end
4407 194 tadej
      2'h1: // Interrupt is not generated
4408 169 mohor
      begin
4409 194 tadej
        // enable interrupt generation
4410 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
4411 194 tadej
        // mask interrupts
4412
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4413
        // detect carrier sense in FD and no collision
4414
        eth_phy.carrier_sense_tx_fd_detect(1);
4415
        eth_phy.collision(0);
4416 169 mohor
      end
4417 194 tadej
      2'h2: // Interrupt is not generated
4418
      begin
4419
        // disable interrupt generation
4420 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
4421 194 tadej
        // unmask interrupts
4422 209 tadejm
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4423 194 tadej
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4424
        // not detect carrier sense in FD and set collision
4425
        eth_phy.carrier_sense_tx_fd_detect(0);
4426
        eth_phy.collision(1);
4427
      end
4428
      default: // 2'h3: // Interrupt is not generated
4429
      begin
4430
        // disable interrupt generation
4431 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
4432 194 tadej
        // mask interrupts
4433
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4434
        // detect carrier sense in FD and set collision
4435
        eth_phy.carrier_sense_tx_fd_detect(1);
4436
        eth_phy.collision(1);
4437
      end
4438
      endcase
4439
      eth_phy.set_tx_mem_addr(max_tmp);
4440
      // set wrap bit
4441
      set_tx_bd_wrap(0);
4442
      set_tx_bd_ready(0, 0);
4443 169 mohor
      #1 check_tx_bd(0, data);
4444 194 tadej
      if (i_length < min_tmp) // just first four
4445 169 mohor
      begin
4446 194 tadej
        while (data[15] === 1)
4447
        begin
4448
          #1 check_tx_bd(0, data);
4449
          @(posedge wb_clk);
4450
        end
4451 209 tadejm
        repeat (1) @(posedge wb_clk);
4452 169 mohor
      end
4453 194 tadej
      else if (i_length > (max_tmp - 8)) // just last four
4454 192 tadej
      begin
4455 194 tadej
        tmp = 0;
4456
        wait (MTxEn === 1'b1); // start transmit
4457
        while (tmp < (i_length - 20))
4458
        begin
4459
          #1 tmp = tmp + 1;
4460
          @(posedge wb_clk);
4461
        end
4462
        #1 check_tx_bd(0, data);
4463
        while (data[15] === 1)
4464
        begin
4465
          #1 check_tx_bd(0, data);
4466
          @(posedge wb_clk);
4467
        end
4468 209 tadejm
        repeat (1) @(posedge wb_clk);
4469 192 tadej
      end
4470
      else
4471
      begin
4472 194 tadej
        wait (MTxEn === 1'b1); // start transmit
4473
        #1 check_tx_bd(0, data);
4474
        if (data[15] !== 1)
4475
        begin
4476
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
4477
          fail = fail + 1;
4478
        end
4479
        wait (MTxEn === 1'b0); // end transmit
4480
        while (data[15] === 1)
4481
        begin
4482
          #1 check_tx_bd(0, data);
4483
          @(posedge wb_clk);
4484
        end
4485
        repeat (1) @(posedge wb_clk);
4486 192 tadej
      end
4487 194 tadej
      // check length of a PACKET
4488
      if (eth_phy.tx_len != (i_length + 4))
4489 192 tadej
      begin
4490 194 tadej
        test_fail("Wrong length of the packet out from MAC");
4491 192 tadej
        fail = fail + 1;
4492
      end
4493 194 tadej
      // checking in the following if statement is performed only for first and last 64 lengths
4494
      if ( ((i_length + 4) <= (min_tmp + 64)) || ((i_length + 4) > (max_tmp - 64)) )
4495 192 tadej
      begin
4496 194 tadej
        // check transmitted TX packet data
4497
        if (i_length[0] == 0)
4498
        begin
4499 209 tadejm
          check_tx_packet((`MEMORY_BASE + i_length[1:0]), max_tmp, i_length, tmp);
4500 194 tadej
        end
4501
        else
4502
        begin
4503 209 tadejm
          check_tx_packet(((`MEMORY_BASE + i_length[1:0]) + max_tmp), max_tmp, i_length, tmp);
4504 194 tadej
        end
4505
        if (tmp > 0)
4506
        begin
4507
          test_fail("Wrong data of the transmitted packet");
4508
          fail = fail + 1;
4509
        end
4510
        // check transmited TX packet CRC
4511
        check_tx_crc(max_tmp, i_length, 1'b0, tmp); // length without CRC
4512
        if (tmp > 0)
4513
        begin
4514
          test_fail("Wrong CRC of the transmitted packet");
4515
          fail = fail + 1;
4516
        end
4517 192 tadej
      end
4518 194 tadej
      // check WB INT signal
4519
      if (i_length[1:0] == 2'h0)
4520 192 tadej
      begin
4521 194 tadej
        if (wb_int !== 1'b1)
4522
        begin
4523
          `TIME; $display("*E WB INT signal should be set");
4524
          test_fail("WB INT signal should be set");
4525
          fail = fail + 1;
4526
        end
4527 192 tadej
      end
4528 194 tadej
      else
4529 192 tadej
      begin
4530 194 tadej
        if (wb_int !== 1'b0)
4531
        begin
4532
          `TIME; $display("*E WB INT signal should not be set");
4533
          test_fail("WB INT signal should not be set");
4534
          fail = fail + 1;
4535
        end
4536 192 tadej
      end
4537 194 tadej
      // check TX buffer descriptor of a packet
4538
      check_tx_bd(0, data);
4539
      if (i_length[1] == 1'b0) // interrupt enabled
4540 192 tadej
      begin
4541 194 tadej
        if (data[15:0] !== 16'h7800)
4542
        begin
4543
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
4544
          test_fail("TX buffer descriptor status is not correct");
4545
          fail = fail + 1;
4546
        end
4547 192 tadej
      end
4548 194 tadej
      else // interrupt not enabled
4549 192 tadej
      begin
4550 194 tadej
        if (data[15:0] !== 16'h3800)
4551
        begin
4552
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
4553
          test_fail("TX buffer descriptor status is not correct");
4554
          fail = fail + 1;
4555
        end
4556 192 tadej
      end
4557 194 tadej
      // clear TX buffer descriptor
4558
      clear_tx_bd(0, 0);
4559
      // check interrupts
4560
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4561
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
4562 192 tadej
      begin
4563 194 tadej
        if ((data & `ETH_INT_TXB) !== 1'b1)
4564
        begin
4565
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
4566
          test_fail("Interrupt Transmit Buffer was not set");
4567
          fail = fail + 1;
4568
        end
4569
        if ((data & (~`ETH_INT_TXB)) !== 0)
4570
        begin
4571
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
4572
          test_fail("Other interrupts (except Transmit Buffer) were set");
4573
          fail = fail + 1;
4574
        end
4575 192 tadej
      end
4576 194 tadej
      else
4577 192 tadej
      begin
4578 194 tadej
        if (data !== 0)
4579
        begin
4580
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
4581
          test_fail("Any of interrupts (except Transmit Buffer) was set");
4582
          fail = fail + 1;
4583
        end
4584 192 tadej
      end
4585 194 tadej
      // clear interrupts
4586
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4587
      // check WB INT signal
4588
      if (wb_int !== 1'b0)
4589 192 tadej
      begin
4590 194 tadej
        test_fail("WB INT signal should not be set");
4591 192 tadej
        fail = fail + 1;
4592
      end
4593 194 tadej
      // INTERMEDIATE DISPLAYS
4594
      if ((i_length + 4) == (min_tmp + 64))
4595 209 tadejm
      begin
4596 194 tadej
        // starting length is min_tmp, ending length is (min_tmp + 64)
4597 209 tadejm
        $display("    pads appending to packets is NOT selected");
4598
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
4599 194 tadej
                 min_tmp, (min_tmp + 64));
4600 209 tadejm
        // set padding, remain the rest
4601
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4602
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4603
      end
4604 194 tadej
      else if ((i_length + 4) == (max_tmp - 16))
4605 209 tadejm
      begin
4606 194 tadej
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
4607 209 tadejm
        $display("    pads appending to packets is selected");
4608
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
4609 194 tadej
                 (min_tmp + 64 + 128), tmp_data);
4610 209 tadejm
        // reset padding, remain the rest
4611
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
4612
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4613
      end
4614 194 tadej
      else if ((i_length + 4) == max_tmp)
4615 209 tadejm
      begin
4616
        $display("    pads appending to packets is NOT selected");
4617
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
4618 194 tadej
                 (max_tmp - (4 + 16)), max_tmp);
4619 209 tadejm
      end
4620 194 tadej
      // set length (loop variable)
4621
      if ((i_length + 4) < (min_tmp + 64))
4622
        i_length = i_length + 1;
4623
      else if ( ((i_length + 4) >= (min_tmp + 64)) && ((i_length + 4) <= (max_tmp - 256)) )
4624
      begin
4625
        i_length = i_length + 128;
4626
        tmp_data = i_length + 4; // last tmp_data is ending length
4627
      end
4628
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
4629
        i_length = max_tmp - (4 + 16);
4630
      else if ((i_length + 4) >= (max_tmp - 16))
4631
        i_length = i_length + 1;
4632
      else
4633
      begin
4634
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
4635
        #10 $stop;
4636
      end
4637 192 tadej
    end
4638 194 tadej
    // disable TX
4639
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4640
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4641
    if(fail == 0)
4642
      test_ok;
4643
    else
4644
      fail = 0;
4645
  end
4646
 
4647
 
4648
  ////////////////////////////////////////////////////////////////////
4649
  ////                                                            ////
4650
  ////  Test transmit packets form MINFL to MAXFL sizes at        ////
4651
  ////  one TX buffer decriptor ( 100Mbps ).                      ////
4652
  ////                                                            ////
4653
  ////////////////////////////////////////////////////////////////////
4654 209 tadejm
  if (test_num == 3) // with and without padding
4655 194 tadej
  begin
4656
    // TEST 3: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 100Mbps )
4657
    test_name = "TEST 3: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 100Mbps )";
4658
    `TIME; $display("  TEST 3: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT ONE TX BD ( 100Mbps )");
4659
 
4660
    max_tmp = 0;
4661
    min_tmp = 0;
4662
    // set one TX buffer descriptor - must be set before TX enable
4663
    wbm_write(`ETH_TX_BD_NUM, 32'h1, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4664 209 tadejm
    // enable TX, set full-duplex mode, NO padding and CRC appending
4665
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
4666 194 tadej
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4667
    // prepare two packets of MAXFL length
4668
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4669
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
4670
    min_tmp = tmp[31:16];
4671
    st_data = 8'h5A;
4672 209 tadejm
    set_tx_packet(`MEMORY_BASE, (max_tmp), st_data); // length without CRC
4673 194 tadej
    st_data = 8'h10;
4674 209 tadejm
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp), st_data); // length without CRC
4675 169 mohor
    // check WB INT signal
4676 192 tadej
    if (wb_int !== 1'b0)
4677 169 mohor
    begin
4678
      test_fail("WB INT signal should not be set");
4679
      fail = fail + 1;
4680
    end
4681 194 tadej
 
4682
    // write to phy's control register for 100Mbps
4683
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
4684
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
4685
    speed = 100;
4686 192 tadej
 
4687 194 tadej
    i_length = (min_tmp - 4);
4688
    while (i_length <= (max_tmp - 4))
4689 192 tadej
    begin
4690 194 tadej
      // choose generating carrier sense and collision
4691
      case (i_length[1:0])
4692
      2'h0: // Interrupt is generated
4693 192 tadej
      begin
4694 194 tadej
        // enable interrupt generation
4695 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
4696 194 tadej
        // unmask interrupts
4697 209 tadejm
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4698 194 tadej
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4699
        // not detect carrier sense in FD and no collision
4700
        eth_phy.carrier_sense_tx_fd_detect(0);
4701
        eth_phy.collision(0);
4702 192 tadej
      end
4703 194 tadej
      2'h1: // Interrupt is not generated
4704 192 tadej
      begin
4705 194 tadej
        // enable interrupt generation
4706 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
4707 194 tadej
        // mask interrupts
4708
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4709
        // detect carrier sense in FD and no collision
4710
        eth_phy.carrier_sense_tx_fd_detect(1);
4711
        eth_phy.collision(0);
4712 192 tadej
      end
4713 194 tadej
      2'h2: // Interrupt is not generated
4714
      begin
4715
        // disable interrupt generation
4716 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
4717 194 tadej
        // unmask interrupts
4718 209 tadejm
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4719 194 tadej
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4720
        // not detect carrier sense in FD and set collision
4721
        eth_phy.carrier_sense_tx_fd_detect(0);
4722
        eth_phy.collision(1);
4723
      end
4724
      default: // 2'h3: // Interrupt is not generated
4725
      begin
4726
        // disable interrupt generation
4727 209 tadejm
        set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
4728 194 tadej
        // mask interrupts
4729
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4730
        // detect carrier sense in FD and set collision
4731
        eth_phy.carrier_sense_tx_fd_detect(1);
4732
        eth_phy.collision(1);
4733
      end
4734
      endcase
4735
      eth_phy.set_tx_mem_addr(max_tmp);
4736
      // set wrap bit
4737
      set_tx_bd_wrap(0);
4738
      set_tx_bd_ready(0, 0);
4739 192 tadej
      #1 check_tx_bd(0, data);
4740 194 tadej
      if (i_length < min_tmp) // just first four
4741 192 tadej
      begin
4742 194 tadej
        while (data[15] === 1)
4743
        begin
4744
          #1 check_tx_bd(0, data);
4745
          @(posedge wb_clk);
4746
        end
4747 209 tadejm
        repeat (1) @(posedge wb_clk);
4748 194 tadej
      end
4749
      else if (i_length > (max_tmp - 8)) // just last four
4750
      begin
4751
        tmp = 0;
4752
        wait (MTxEn === 1'b1); // start transmit
4753
        while (tmp < (i_length - 20))
4754
        begin
4755
          #1 tmp = tmp + 1;
4756
          @(posedge wb_clk);
4757
        end
4758 192 tadej
        #1 check_tx_bd(0, data);
4759 194 tadej
        while (data[15] === 1)
4760
        begin
4761
          #1 check_tx_bd(0, data);
4762
          @(posedge wb_clk);
4763
        end
4764 209 tadejm
        repeat (1) @(posedge wb_clk);
4765 192 tadej
      end
4766 194 tadej
      else
4767
      begin
4768
        wait (MTxEn === 1'b1); // start transmit
4769
        #1 check_tx_bd(0, data);
4770
        if (data[15] !== 1)
4771
        begin
4772
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
4773
          fail = fail + 1;
4774
        end
4775
        wait (MTxEn === 1'b0); // end transmit
4776
        while (data[15] === 1)
4777
        begin
4778
          #1 check_tx_bd(0, data);
4779
          @(posedge wb_clk);
4780
        end
4781
        repeat (1) @(posedge wb_clk);
4782
      end
4783
      // check length of a PACKET
4784
      if (eth_phy.tx_len != (i_length + 4))
4785
      begin
4786
        test_fail("Wrong length of the packet out from MAC");
4787
        fail = fail + 1;
4788
      end
4789 192 tadej
      // check transmitted TX packet data
4790
      if (i_length[0] == 0)
4791
      begin
4792 209 tadejm
        check_tx_packet((`MEMORY_BASE + i_length[1:0]), max_tmp, i_length, tmp);
4793 192 tadej
      end
4794
      else
4795
      begin
4796 209 tadejm
        check_tx_packet(((`MEMORY_BASE + i_length[1:0]) + max_tmp), max_tmp, i_length, tmp);
4797 192 tadej
      end
4798
      if (tmp > 0)
4799
      begin
4800
        test_fail("Wrong data of the transmitted packet");
4801
        fail = fail + 1;
4802
      end
4803
      // check transmited TX packet CRC
4804
      check_tx_crc(max_tmp, i_length, 1'b0, tmp); // length without CRC
4805
      if (tmp > 0)
4806
      begin
4807
        test_fail("Wrong CRC of the transmitted packet");
4808
        fail = fail + 1;
4809
      end
4810 194 tadej
      // check WB INT signal
4811
      if (i_length[1:0] == 2'h0)
4812 192 tadej
      begin
4813 194 tadej
        if (wb_int !== 1'b1)
4814
        begin
4815
          `TIME; $display("*E WB INT signal should be set");
4816
          test_fail("WB INT signal should be set");
4817
          fail = fail + 1;
4818
        end
4819 192 tadej
      end
4820 194 tadej
      else
4821 192 tadej
      begin
4822 194 tadej
        if (wb_int !== 1'b0)
4823
        begin
4824
          `TIME; $display("*E WB INT signal should not be set");
4825
          test_fail("WB INT signal should not be set");
4826
          fail = fail + 1;
4827
        end
4828 192 tadej
      end
4829 194 tadej
      // check TX buffer descriptor of a packet
4830
      check_tx_bd(0, data);
4831
      if (i_length[1] == 1'b0) // interrupt enabled
4832 192 tadej
      begin
4833 194 tadej
        if (data[15:0] !== 16'h7800)
4834
        begin
4835
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
4836
          test_fail("TX buffer descriptor status is not correct");
4837
          fail = fail + 1;
4838
        end
4839 192 tadej
      end
4840 194 tadej
      else // interrupt not enabled
4841 192 tadej
      begin
4842 194 tadej
        if (data[15:0] !== 16'h3800)
4843
        begin
4844
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
4845
          test_fail("TX buffer descriptor status is not correct");
4846
          fail = fail + 1;
4847
        end
4848 192 tadej
      end
4849 194 tadej
      // clear TX buffer descriptor
4850
      clear_tx_bd(0, 0);
4851
      // check interrupts
4852
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4853
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
4854 192 tadej
      begin
4855 194 tadej
        if ((data & `ETH_INT_TXB) !== 1'b1)
4856
        begin
4857
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
4858
          test_fail("Interrupt Transmit Buffer was not set");
4859
          fail = fail + 1;
4860
        end
4861
        if ((data & (~`ETH_INT_TXB)) !== 0)
4862
        begin
4863
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
4864
          test_fail("Other interrupts (except Transmit Buffer) were set");
4865
          fail = fail + 1;
4866
        end
4867 192 tadej
      end
4868 194 tadej
      else
4869 192 tadej
      begin
4870 194 tadej
        if (data !== 0)
4871
        begin
4872
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h", data);
4873
          test_fail("Any of interrupts (except Transmit Buffer) was set");
4874
          fail = fail + 1;
4875
        end
4876 192 tadej
      end
4877 194 tadej
      // clear interrupts
4878
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4879
      // check WB INT signal
4880
      if (wb_int !== 1'b0)
4881 192 tadej
      begin
4882 194 tadej
        test_fail("WB INT signal should not be set");
4883 192 tadej
        fail = fail + 1;
4884
      end
4885 194 tadej
      // INTERMEDIATE DISPLAYS
4886
      if ((i_length + 4) == (min_tmp + 64))
4887 209 tadejm
      begin
4888 194 tadej
        // starting length is min_tmp, ending length is (min_tmp + 64)
4889 209 tadejm
        $display("    pads appending to packets is NOT selected");
4890
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
4891 194 tadej
                 min_tmp, (min_tmp + 64));
4892 209 tadejm
        // set padding, remain the rest
4893
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4894
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4895
      end
4896 194 tadej
      else if ((i_length + 4) == (max_tmp - 16))
4897 209 tadejm
      begin
4898 194 tadej
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
4899 209 tadejm
        $display("    pads appending to packets is selected");
4900
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
4901 194 tadej
                 (min_tmp + 64 + 128), tmp_data);
4902 209 tadejm
        // reset padding, remain the rest
4903
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
4904
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4905
      end
4906 194 tadej
      else if ((i_length + 4) == max_tmp)
4907 209 tadejm
      begin
4908
        $display("    pads appending to packets is NOT selected");
4909
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
4910 194 tadej
                 (max_tmp - (4 + 16)), max_tmp);
4911 209 tadejm
      end
4912 194 tadej
      // set length (loop variable)
4913
      if ((i_length + 4) < (min_tmp + 64))
4914
        i_length = i_length + 1;
4915
      else if ( ((i_length + 4) >= (min_tmp + 64)) && ((i_length + 4) <= (max_tmp - 256)) )
4916
      begin
4917
        i_length = i_length + 128;
4918
        tmp_data = i_length + 4; // last tmp_data is ending length
4919
      end
4920
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
4921
        i_length = max_tmp - (4 + 16);
4922
      else if ((i_length + 4) >= (max_tmp - 16))
4923
        i_length = i_length + 1;
4924 192 tadej
      else
4925 194 tadej
      begin
4926
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
4927
        #10 $stop;
4928
      end
4929 179 mohor
    end
4930 194 tadej
    // disable TX
4931
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
4932
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4933
    if(fail == 0)
4934
      test_ok;
4935
    else
4936
      fail = 0;
4937 169 mohor
  end
4938
 
4939
 
4940 209 tadejm
  ////////////////////////////////////////////////////////////////////
4941
  ////                                                            ////
4942
  ////  Test transmit packets form MINFL to MAXFL sizes at        ////
4943
  ////  maximum TX buffer decriptors ( 10Mbps ).                  ////
4944
  ////                                                            ////
4945
  ////////////////////////////////////////////////////////////////////
4946
  if (test_num == 4) // without and with padding
4947
  begin
4948
    // TEST 4: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 10Mbps )
4949
    test_name = "TEST 4: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 10Mbps )";
4950
    `TIME; $display("  TEST 4: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 10Mbps )");
4951
 
4952
    // reset MAC registers
4953
    hard_reset;
4954
    // reset MAC and MII LOGIC with soft reset
4955
    reset_mac;
4956
    reset_mii;
4957
    // set wb slave response
4958
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
4959 192 tadej
 
4960 209 tadejm
    max_tmp = 0;
4961
    min_tmp = 0;
4962
    num_of_frames = 0;
4963
    num_of_bd = 0;
4964
    // set maximum TX buffer descriptors (128) - must be set before TX enable
4965
    wbm_write(`ETH_TX_BD_NUM, 32'h80, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4966
    // enable TX, set full-duplex mode, NO padding and CRC appending
4967
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
4968
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4969
    // prepare two packets of MAXFL length
4970
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
4971
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
4972
    min_tmp = tmp[31:16];
4973
    st_data = 8'hA3;
4974
    set_tx_packet(`MEMORY_BASE, (max_tmp), st_data); // length without CRC
4975
    st_data = 8'h81;
4976
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp), st_data); // length without CRC
4977
    // check WB INT signal
4978
    if (wb_int !== 1'b0)
4979
    begin
4980
      test_fail("WB INT signal should not be set");
4981
      fail = fail + 1;
4982
    end
4983
 
4984
    // write to phy's control register for 10Mbps
4985
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
4986
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
4987
    speed = 10;
4988
 
4989
    i_length = (min_tmp - 4);
4990
    while (i_length <= (max_tmp - 4))
4991
    begin
4992
      // choose generating carrier sense and collision
4993
      case (i_length[1:0])
4994
      2'h0: // Interrupt is generated
4995
      begin
4996
        // Reset_tx_bd nable interrupt generation
4997
        // unmask interrupts
4998
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
4999
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5000
        // not detect carrier sense in FD and no collision
5001
        eth_phy.carrier_sense_tx_fd_detect(0);
5002
        eth_phy.collision(0);
5003
      end
5004
      2'h1: // Interrupt is not generated
5005
      begin
5006
        // set_tx_bd enable interrupt generation
5007
        // mask interrupts
5008
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5009
        // detect carrier sense in FD and no collision
5010
        eth_phy.carrier_sense_tx_fd_detect(1);
5011
        eth_phy.collision(0);
5012
      end
5013
      2'h2: // Interrupt is not generated
5014
      begin
5015
        // set_tx_bd disable the interrupt generation
5016
        // unmask interrupts
5017
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
5018
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5019
        // not detect carrier sense in FD and set collision
5020
        eth_phy.carrier_sense_tx_fd_detect(0);
5021
        eth_phy.collision(1);
5022
      end
5023
      default: // 2'h3: // Interrupt is not generated
5024
      begin
5025
        // set_tx_bd disable the interrupt generation
5026
        // mask interrupts
5027
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5028
        // detect carrier sense in FD and set collision
5029
        eth_phy.carrier_sense_tx_fd_detect(1);
5030
        eth_phy.collision(1);
5031
      end
5032
      endcase
5033
      // first destination address on ethernet PHY
5034
      if (i_length[0] == 0)
5035
        eth_phy.set_tx_mem_addr(0);
5036
      else
5037
        eth_phy.set_tx_mem_addr(max_tmp);
5038
      // first 8 frames are transmitted with TX BD 0 (wrap bit on TX BD 0)
5039
      // number of all frames is 154 (146 without first 8)
5040
      if (num_of_frames < 8)
5041
      begin
5042
        case (i_length[1:0])
5043
        2'h0: // Interrupt is generated
5044
        begin
5045
          // enable interrupt generation
5046
          set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
5047
          // interrupts are unmasked
5048
        end
5049
        2'h1: // Interrupt is not generated
5050
        begin
5051
          // enable interrupt generation
5052
          set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
5053
          // interrupts are masked
5054
        end
5055
        2'h2: // Interrupt is not generated
5056
        begin
5057
          // disable interrupt generation
5058
          set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
5059
          // interrupts are unmasked
5060
        end
5061
        default: // 2'h3: // Interrupt is not generated
5062
        begin
5063
          // disable interrupt generation
5064
          set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
5065
          // interrupts are masked
5066
        end
5067
        endcase
5068
        // set wrap bit
5069
        set_tx_bd_wrap(0);
5070
      end
5071
      // after first 8 number of frames, 128 frames form TX BD 0 to 127 will be transmitted
5072
      else if ((num_of_frames - 8) == 0)
5073
      begin
5074
        tmp_len = i_length; // length of frame
5075
        tmp_bd_num = 0; // TX BD number
5076
        while (tmp_bd_num < 128) // (tmp_len <= (max_tmp - 4)) - this is the last frame
5077
        begin
5078
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5079
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5080
          if (tmp_len[0] == 0)
5081
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
5082
          else
5083
            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));
5084
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
5085
          if ((tmp_len + 4) < (min_tmp + 128))
5086
            tmp_len = tmp_len + 1;
5087
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5088
            tmp_len = 256;
5089
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5090
            tmp_len = tmp_len + 128;
5091
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
5092
            tmp_len = max_tmp - (4 + 16);
5093
          else if ((tmp_len + 4) >= (max_tmp - 16))
5094
            tmp_len = tmp_len + 1;
5095
          // set TX BD number
5096
          tmp_bd_num = tmp_bd_num + 1;
5097
        end
5098
        // set wrap bit
5099
        set_tx_bd_wrap(127);
5100
      end
5101
      // after 128 + first 8 number of frames, 19 frames form TX BD 0 to 18 will be transmitted
5102
      else if ((num_of_frames - 8) == 20) // 128
5103
      begin
5104
        tmp_len = tmp_len; // length of frame remaines from previous settings
5105
        tmp_bd_num = 0; // TX BD number
5106
        while (tmp_bd_num < 19) // (tmp_len <= (max_tmp - 4)) - this is the last frame
5107
        begin
5108
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5109
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5110
          if (tmp_len[0] == 0)
5111
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
5112
          else
5113
            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));
5114
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
5115
          if ((tmp_len + 4) < (min_tmp + 128))
5116
            tmp_len = tmp_len + 1;
5117
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5118
            tmp_len = 256;
5119
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5120
            tmp_len = tmp_len + 128;
5121
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
5122
            tmp_len = max_tmp - (4 + 16);
5123
          else if ((tmp_len + 4) >= (max_tmp - 16))
5124
            tmp_len = tmp_len + 1;
5125
          // set TX BD number
5126
          tmp_bd_num = tmp_bd_num + 1;
5127
        end
5128
      end
5129
      // set ready bit
5130
      if (num_of_frames < 8)
5131
        set_tx_bd_ready(0, 0);
5132
      else if ((num_of_frames - 8) < 128)
5133
        set_tx_bd_ready((num_of_frames - 8), (num_of_frames - 8));
5134
      else if ((num_of_frames - 136) < 19)
5135
        set_tx_bd_ready((num_of_frames - 136), (num_of_frames - 136));
5136
      // CHECK END OF TRANSMITION
5137
      #1 check_tx_bd(num_of_bd, data);
5138
      if (i_length < min_tmp) // just first four
5139
      begin
5140
        while (data[15] === 1)
5141
        begin
5142
          #1 check_tx_bd(num_of_bd, data);
5143
          @(posedge wb_clk);
5144
        end
5145
        repeat (1) @(posedge wb_clk);
5146
      end
5147
      else if (i_length > (max_tmp - 8)) // just last four
5148
      begin
5149
        tmp = 0;
5150
        wait (MTxEn === 1'b1); // start transmit
5151
        while (tmp < (i_length - 20))
5152
        begin
5153
          #1 tmp = tmp + 1;
5154
          @(posedge wb_clk);
5155
        end
5156
        #1 check_tx_bd(num_of_bd, data);
5157
        while (data[15] === 1)
5158
        begin
5159
          #1 check_tx_bd(num_of_bd, data);
5160
          @(posedge wb_clk);
5161
        end
5162
        repeat (1) @(posedge wb_clk);
5163
      end
5164
      else
5165
      begin
5166
        wait (MTxEn === 1'b1); // start transmit
5167
        #1 check_tx_bd(num_of_bd, data);
5168
        if (data[15] !== 1)
5169
        begin
5170
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
5171
          fail = fail + 1;
5172
        end
5173
        wait (MTxEn === 1'b0); // end transmit
5174
        while (data[15] === 1)
5175
        begin
5176
          #1 check_tx_bd(num_of_bd, data);
5177
          @(posedge wb_clk);
5178
        end
5179
        repeat (1) @(posedge wb_clk);
5180
      end
5181
      // check length of a PACKET
5182
      if (eth_phy.tx_len != (i_length + 4))
5183
      begin
5184
        test_fail("Wrong length of the packet out from MAC");
5185
        fail = fail + 1;
5186
      end
5187
        // check transmitted TX packet data
5188
        if (i_length[0] == 0)
5189
        begin
5190
          check_tx_packet((`MEMORY_BASE + i_length[1:0]), 0, i_length, tmp);
5191
        end
5192
        else
5193
        begin
5194
          check_tx_packet(((`MEMORY_BASE + i_length[1:0]) + max_tmp), max_tmp, i_length, tmp);
5195
        end
5196
        if (tmp > 0)
5197
        begin
5198
          test_fail("Wrong data of the transmitted packet");
5199
          fail = fail + 1;
5200
        end
5201
        // check transmited TX packet CRC
5202
        if (i_length[0] == 0)
5203
          check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
5204
        else
5205
          check_tx_crc(max_tmp, i_length, 1'b0, tmp); // length without CRC
5206
        if (tmp > 0)
5207
        begin
5208
          test_fail("Wrong CRC of the transmitted packet");
5209
          fail = fail + 1;
5210
        end
5211
      // check WB INT signal
5212
      if (i_length[1:0] == 2'h0)
5213
      begin
5214
        if (wb_int !== 1'b1)
5215
        begin
5216
          `TIME; $display("*E WB INT signal should be set");
5217
          test_fail("WB INT signal should be set");
5218
          fail = fail + 1;
5219
        end
5220
      end
5221
      else
5222
      begin
5223
        if (wb_int !== 1'b0)
5224
        begin
5225
          `TIME; $display("*E WB INT signal should not be set");
5226
          test_fail("WB INT signal should not be set");
5227
          fail = fail + 1;
5228
        end
5229
      end
5230
      // check TX buffer descriptor of a packet
5231
      check_tx_bd(num_of_bd, data);
5232
      if (i_length[1] == 1'b0) // interrupt enabled
5233
      begin
5234
        if ( ((data[15:0] !== 16'h7800) && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
5235
             ((data[15:0] !== 16'h5800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
5236
        begin
5237
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
5238
          test_fail("TX buffer descriptor status is not correct");
5239
          fail = fail + 1;
5240
        end
5241
      end
5242
      else // interrupt not enabled
5243
      begin
5244
        if ( ((data[15:0] !== 16'h3800)  && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
5245
             ((data[15:0] !== 16'h1800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
5246
        begin
5247
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
5248
          test_fail("TX buffer descriptor status is not correct");
5249
          fail = fail + 1;
5250
        end
5251
      end
5252
      // clear first half of 8 frames from TX buffer descriptor 0
5253
      if (num_of_frames < 4)
5254
        clear_tx_bd(num_of_bd, num_of_bd);
5255
      // clear BD with wrap bit
5256
      if (num_of_frames == 140)
5257
        clear_tx_bd(127, 127);
5258
      // check interrupts
5259
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5260
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
5261
      begin
5262
        if ((data & `ETH_INT_TXB) !== 1'b1)
5263
        begin
5264
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
5265
          test_fail("Interrupt Transmit Buffer was not set");
5266
          fail = fail + 1;
5267
        end
5268
        if ((data & (~`ETH_INT_TXB)) !== 0)
5269
        begin
5270
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
5271
          test_fail("Other interrupts (except Transmit Buffer) were set");
5272
          fail = fail + 1;
5273
        end
5274
      end
5275
      else
5276
      begin
5277
        if (data !== 0)
5278
        begin
5279
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
5280
          test_fail("Any of interrupts (except Transmit Buffer) was set");
5281
          fail = fail + 1;
5282
        end
5283
      end
5284
      // clear interrupts
5285
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5286
      // check WB INT signal
5287
      if (wb_int !== 1'b0)
5288
      begin
5289
        test_fail("WB INT signal should not be set");
5290
        fail = fail + 1;
5291
      end
5292
      // INTERMEDIATE DISPLAYS
5293
      if ((i_length + 4) == (min_tmp + 7))
5294
      begin
5295
        // starting length is min_tmp, ending length is (min_tmp + 128)
5296
        $display("    pads appending to packets is NOT selected");
5297
        $display("    using only TX BD 0 out of 128 BDs assigned to TX (wrap at first BD - TX BD 0)");
5298
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
5299
                 min_tmp, (min_tmp + 7));
5300
        $display("    ->all packets were send from TX BD 0");
5301
        // set padding, remain the rest
5302
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5303
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5304
      end
5305
      else if ((i_length + 4) == (min_tmp + 128))
5306
      begin
5307
        // starting length is min_tmp, ending length is (min_tmp + 128)
5308
        $display("    pads appending to packets is NOT selected");
5309
        $display("    using all 128 BDs assigned to TX (wrap at 128th BD - TX BD 127)");
5310
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
5311
                 (min_tmp + 8), (min_tmp + 128));
5312
        $display("    ->packets were send from TX BD %0d to TX BD %0d respectively",
5313
                 1'b0, num_of_bd);
5314
        tmp_bd = num_of_bd + 1;
5315
        // set padding, remain the rest
5316
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5317
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5318
      end
5319
      else if ((i_length + 4) == (max_tmp - 16))
5320
      begin
5321
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
5322
        $display("    pads appending to packets is selected");
5323
        $display("    using all 128 BDs assigned to TX (wrap at 128th BD - TX BD 127)");
5324
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
5325
                 (min_tmp + 64 + 128), tmp_data);
5326
        if (tmp_bd > num_of_bd)
5327
          $display("    ->packets were send from TX BD %0d to TX BD 127 and from TX BD 0 to TX BD %0d respectively",
5328
                   tmp_bd, num_of_bd);
5329
        else
5330
          $display("    ->packets were send from TX BD %0d to TX BD %0d respectively",
5331
                   tmp_bd, num_of_bd);
5332
        tmp_bd = num_of_bd + 1;
5333
        // reset padding, remain the rest
5334
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
5335
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5336
      end
5337
      else if ((i_length + 4) == max_tmp)
5338
      begin
5339
        $display("    pads appending to packets is NOT selected");
5340
        $display("    using all 128 BDs assigned to TX (wrap at 128th BD - TX BD 127)");
5341
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
5342
                 (max_tmp - (4 + 16)), max_tmp);
5343
        if (tmp_bd > num_of_bd)
5344
          $display("    ->packets were send from TX BD %0d to TX BD 127 and from TX BD 0 to TX BD %0d respectively",
5345
                   tmp_bd, num_of_bd);
5346
        else
5347
          $display("    ->packets were send from TX BD %0d to TX BD %0d respectively",
5348
                   tmp_bd, num_of_bd);
5349
      end
5350
      // set length (loop variable)
5351
      if ((i_length + 4) < (min_tmp + 128))
5352
        i_length = i_length + 1;
5353
      else if ( ((i_length + 4) == (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
5354
        i_length = 256;
5355
      else if ( ((i_length + 4) > (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
5356
      begin
5357
        i_length = i_length + 128;
5358
        tmp_data = i_length + 4; // last tmp_data is ending length
5359
      end
5360
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
5361
        i_length = max_tmp - (4 + 16);
5362
      else if ((i_length + 4) >= (max_tmp - 16))
5363
        i_length = i_length + 1;
5364
      else
5365
      begin
5366
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
5367
        #10 $stop;
5368
      end
5369
      // the number of frame transmitted
5370
      num_of_frames = num_of_frames + 1;
5371
      if ((num_of_frames <= 8) || ((num_of_frames - 8) == 128))
5372
        num_of_bd = 0;
5373
      else
5374
        num_of_bd = num_of_bd + 1;
5375
    end
5376
    // disable TX
5377
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5378
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5379
    @(posedge wb_clk);
5380
    if(fail == 0)
5381
      test_ok;
5382
    else
5383
      fail = 0;
5384
  end
5385
 
5386
 
5387
  ////////////////////////////////////////////////////////////////////
5388
  ////                                                            ////
5389
  ////  Test transmit packets form MINFL to MAXFL sizes at        ////
5390
  ////  maximum TX buffer decriptors ( 100Mbps ).                 ////
5391
  ////                                                            ////
5392
  ////////////////////////////////////////////////////////////////////
5393
  if (test_num == 5) // with and without padding
5394
  begin
5395
    // TEST 5: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 100Mbps )
5396
    test_name = "TEST 5: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 100Mbps )";
5397
    `TIME; $display("  TEST 5: TRANSMIT PACKETS FROM MINFL TO MAXFL SIZES AT MAX TX BDs ( 100Mbps )");
5398
 
5399
    // reset MAC registers
5400
    hard_reset;
5401
    // reset MAC and MII LOGIC with soft reset
5402
    reset_mac;
5403
    reset_mii;
5404
    // set wb slave response
5405
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
5406
 
5407
    max_tmp = 0;
5408
    min_tmp = 0;
5409
    num_of_frames = 0;
5410
    num_of_bd = 0;
5411
    // set maximum TX buffer descriptors (128) - must be set before TX enable
5412
    wbm_write(`ETH_TX_BD_NUM, 32'h80, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5413
    // enable TX, set full-duplex mode, NO padding and CRC appending
5414
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
5415
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5416
    // prepare two packets of MAXFL length
5417
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5418
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
5419
    min_tmp = tmp[31:16];
5420
    st_data = 8'hA5;
5421
    set_tx_packet(`MEMORY_BASE, (max_tmp), st_data); // length without CRC
5422
    st_data = 8'h71;
5423
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp), st_data); // length without CRC
5424
    // check WB INT signal
5425
    if (wb_int !== 1'b0)
5426
    begin
5427
      test_fail("WB INT signal should not be set");
5428
      fail = fail + 1;
5429
    end
5430
 
5431
    // write to phy's control register for 100Mbps
5432
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
5433
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
5434
    speed = 100;
5435
 
5436
    i_length = (min_tmp - 4);
5437
    while (i_length <= (max_tmp - 4))
5438
    begin
5439
      // choose generating carrier sense and collision
5440
      case (i_length[1:0])
5441
      2'h0: // Interrupt is generated
5442
      begin
5443
        // Reset_tx_bd nable interrupt generation
5444
        // unmask interrupts
5445
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
5446
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5447
        // not detect carrier sense in FD and no collision
5448
        eth_phy.carrier_sense_tx_fd_detect(0);
5449
        eth_phy.collision(0);
5450
      end
5451
      2'h1: // Interrupt is not generated
5452
      begin
5453
        // set_tx_bd enable interrupt generation
5454
        // mask interrupts
5455
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5456
        // detect carrier sense in FD and no collision
5457
        eth_phy.carrier_sense_tx_fd_detect(1);
5458
        eth_phy.collision(0);
5459
      end
5460
      2'h2: // Interrupt is not generated
5461
      begin
5462
        // set_tx_bd disable the interrupt generation
5463
        // unmask interrupts
5464
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
5465
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5466
        // not detect carrier sense in FD and set collision
5467
        eth_phy.carrier_sense_tx_fd_detect(0);
5468
        eth_phy.collision(1);
5469
      end
5470
      default: // 2'h3: // Interrupt is not generated
5471
      begin
5472
        // set_tx_bd disable the interrupt generation
5473
        // mask interrupts
5474
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5475
        // detect carrier sense in FD and set collision
5476
        eth_phy.carrier_sense_tx_fd_detect(1);
5477
        eth_phy.collision(1);
5478
      end
5479
      endcase
5480
      // first destination address on ethernet PHY
5481
      if (i_length[0] == 0)
5482
        eth_phy.set_tx_mem_addr(0);
5483
      else
5484
        eth_phy.set_tx_mem_addr(max_tmp);
5485
      // first 8 frames are transmitted with TX BD 0 (wrap bit on TX BD 0)
5486
      // number of all frames is 154 (146 without first 8)
5487
      if (num_of_frames < 8)
5488
      begin
5489
        case (i_length[1:0])
5490
        2'h0: // Interrupt is generated
5491
        begin
5492
          // enable interrupt generation
5493
          set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
5494
          // interrupts are unmasked
5495
        end
5496
        2'h1: // Interrupt is not generated
5497
        begin
5498
          // enable interrupt generation
5499
          set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
5500
          // interrupts are masked
5501
        end
5502
        2'h2: // Interrupt is not generated
5503
        begin
5504
          // disable interrupt generation
5505
          set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, (`MEMORY_BASE + i_length[1:0]));
5506
          // interrupts are unmasked
5507
        end
5508
        default: // 2'h3: // Interrupt is not generated
5509
        begin
5510
          // disable interrupt generation
5511
          set_tx_bd(0, 0, i_length, 1'b0, 1'b1, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
5512
          // interrupts are masked
5513
        end
5514
        endcase
5515
        // set wrap bit
5516
        set_tx_bd_wrap(0);
5517
      end
5518
      // after first 8 number of frames, 128 frames form TX BD 0 to 127 will be transmitted
5519
      else if ((num_of_frames - 8) == 0)
5520
      begin
5521
        tmp_len = i_length; // length of frame
5522
        tmp_bd_num = 0; // TX BD number
5523
        while (tmp_bd_num < 128) // (tmp_len <= (max_tmp - 4)) - this is the last frame
5524
        begin
5525
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5526
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5527
          if (tmp_len[0] == 0)
5528
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
5529
          else
5530
            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));
5531
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
5532
          if ((tmp_len + 4) < (min_tmp + 128))
5533
            tmp_len = tmp_len + 1;
5534
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5535
            tmp_len = 256;
5536
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5537
            tmp_len = tmp_len + 128;
5538
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
5539
            tmp_len = max_tmp - (4 + 16);
5540
          else if ((tmp_len + 4) >= (max_tmp - 16))
5541
            tmp_len = tmp_len + 1;
5542
          // set TX BD number
5543
          tmp_bd_num = tmp_bd_num + 1;
5544
        end
5545
        // set wrap bit
5546
        set_tx_bd_wrap(127);
5547
      end
5548
      // after 128 + first 8 number of frames, 19 frames form TX BD 0 to 18 will be transmitted
5549
      else if ((num_of_frames - 8) == 20) // 128
5550
      begin
5551
        tmp_len = tmp_len; // length of frame remaines from previous settings
5552
        tmp_bd_num = 0; // TX BD number
5553
        while (tmp_bd_num < 19) // (tmp_len <= (max_tmp - 4)) - this is the last frame
5554
        begin
5555
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5556
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5557
          if (tmp_len[0] == 0)
5558
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
5559
          else
5560
            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));
5561
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
5562
          if ((tmp_len + 4) < (min_tmp + 128))
5563
            tmp_len = tmp_len + 1;
5564
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5565
            tmp_len = 256;
5566
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
5567
            tmp_len = tmp_len + 128;
5568
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
5569
            tmp_len = max_tmp - (4 + 16);
5570
          else if ((tmp_len + 4) >= (max_tmp - 16))
5571
            tmp_len = tmp_len + 1;
5572
          // set TX BD number
5573
          tmp_bd_num = tmp_bd_num + 1;
5574
        end
5575
      end
5576
      // set ready bit
5577
      if (num_of_frames < 8)
5578
        set_tx_bd_ready(0, 0);
5579
      else if ((num_of_frames - 8) < 128)
5580
        set_tx_bd_ready((num_of_frames - 8), (num_of_frames - 8));
5581
      else if ((num_of_frames - 136) < 19)
5582
        set_tx_bd_ready((num_of_frames - 136), (num_of_frames - 136));
5583
      // CHECK END OF TRANSMITION
5584
      #1 check_tx_bd(num_of_bd, data);
5585
      if (i_length < min_tmp) // just first four
5586
      begin
5587
        while (data[15] === 1)
5588
        begin
5589
          #1 check_tx_bd(num_of_bd, data);
5590
          @(posedge wb_clk);
5591
        end
5592
        repeat (1) @(posedge wb_clk);
5593
      end
5594
      else if (i_length > (max_tmp - 8)) // just last four
5595
      begin
5596
        tmp = 0;
5597
        wait (MTxEn === 1'b1); // start transmit
5598
        while (tmp < (i_length - 20))
5599
        begin
5600
          #1 tmp = tmp + 1;
5601
          @(posedge wb_clk);
5602
        end
5603
        #1 check_tx_bd(num_of_bd, data);
5604
        while (data[15] === 1)
5605
        begin
5606
          #1 check_tx_bd(num_of_bd, data);
5607
          @(posedge wb_clk);
5608
        end
5609
        repeat (1) @(posedge wb_clk);
5610
      end
5611
      else
5612
      begin
5613
        wait (MTxEn === 1'b1); // start transmit
5614
        #1 check_tx_bd(num_of_bd, data);
5615
        if (data[15] !== 1)
5616
        begin
5617
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
5618
          fail = fail + 1;
5619
        end
5620
        wait (MTxEn === 1'b0); // end transmit
5621
        while (data[15] === 1)
5622
        begin
5623
          #1 check_tx_bd(num_of_bd, data);
5624
          @(posedge wb_clk);
5625
        end
5626
        repeat (1) @(posedge wb_clk);
5627
      end
5628
      // check length of a PACKET
5629
      if (eth_phy.tx_len != (i_length + 4))
5630
      begin
5631
        test_fail("Wrong length of the packet out from MAC");
5632
        fail = fail + 1;
5633
      end
5634
      // checking in the following if statement is performed only for first and last 64 lengths
5635
        // check transmitted TX packet data
5636
        if (i_length[0] == 0)
5637
        begin
5638
          check_tx_packet((`MEMORY_BASE + i_length[1:0]), 0, i_length, tmp);
5639
        end
5640
        else
5641
        begin
5642
          check_tx_packet(((`MEMORY_BASE + i_length[1:0]) + max_tmp), max_tmp, i_length, tmp);
5643
        end
5644
        if (tmp > 0)
5645
        begin
5646
          test_fail("Wrong data of the transmitted packet");
5647
          fail = fail + 1;
5648
        end
5649
        // check transmited TX packet CRC
5650
        if (i_length[0] == 0)
5651
          check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
5652
        else
5653
          check_tx_crc(max_tmp, i_length, 1'b0, tmp); // length without CRC
5654
        if (tmp > 0)
5655
        begin
5656
          test_fail("Wrong CRC of the transmitted packet");
5657
          fail = fail + 1;
5658
        end
5659
      // check WB INT signal
5660
      if (i_length[1:0] == 2'h0)
5661
      begin
5662
        if (wb_int !== 1'b1)
5663
        begin
5664
          `TIME; $display("*E WB INT signal should be set");
5665
          test_fail("WB INT signal should be set");
5666
          fail = fail + 1;
5667
        end
5668
      end
5669
      else
5670
      begin
5671
        if (wb_int !== 1'b0)
5672
        begin
5673
          `TIME; $display("*E WB INT signal should not be set");
5674
          test_fail("WB INT signal should not be set");
5675
          fail = fail + 1;
5676
        end
5677
      end
5678
      // check TX buffer descriptor of a packet
5679
      check_tx_bd(num_of_bd, data);
5680
      if (i_length[1] == 1'b0) // interrupt enabled
5681
      begin
5682
        if ( ((data[15:0] !== 16'h7800) && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
5683
             ((data[15:0] !== 16'h5800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
5684
        begin
5685
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
5686
          test_fail("TX buffer descriptor status is not correct");
5687
          fail = fail + 1;
5688
        end
5689
      end
5690
      else // interrupt not enabled
5691
      begin
5692
        if ( ((data[15:0] !== 16'h3800)  && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
5693
             ((data[15:0] !== 16'h1800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
5694
        begin
5695
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
5696
          test_fail("TX buffer descriptor status is not correct");
5697
          fail = fail + 1;
5698
        end
5699
      end
5700
      // clear first half of 8 frames from TX buffer descriptor 0
5701
      if (num_of_frames < 4)
5702
        clear_tx_bd(num_of_bd, num_of_bd);
5703
      // clear BD with wrap bit
5704
      if (num_of_frames == 140)
5705
        clear_tx_bd(127, 127);
5706
      // check interrupts
5707
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5708
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
5709
      begin
5710
        if ((data & `ETH_INT_TXB) !== 1'b1)
5711
        begin
5712
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
5713
          test_fail("Interrupt Transmit Buffer was not set");
5714
          fail = fail + 1;
5715
        end
5716
        if ((data & (~`ETH_INT_TXB)) !== 0)
5717
        begin
5718
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
5719
          test_fail("Other interrupts (except Transmit Buffer) were set");
5720
          fail = fail + 1;
5721
        end
5722
      end
5723
      else
5724
      begin
5725
        if (data !== 0)
5726
        begin
5727
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
5728
          test_fail("Any of interrupts (except Transmit Buffer) was set");
5729
          fail = fail + 1;
5730
        end
5731
      end
5732
      // clear interrupts
5733
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5734
      // check WB INT signal
5735
      if (wb_int !== 1'b0)
5736
      begin
5737
        test_fail("WB INT signal should not be set");
5738
        fail = fail + 1;
5739
      end
5740
      // INTERMEDIATE DISPLAYS
5741
      if ((i_length + 4) == (min_tmp + 7))
5742
      begin
5743
        // starting length is min_tmp, ending length is (min_tmp + 128)
5744
        $display("    pads appending to packets is NOT selected");
5745
        $display("    using only TX BD 0 out of 128 BDs assigned to TX (wrap at first BD - TX BD 0)");
5746
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
5747
                 min_tmp, (min_tmp + 7));
5748
        $display("    ->all packets were send from TX BD 0");
5749
        // set padding, remain the rest
5750
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5751
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5752
      end
5753
      else if ((i_length + 4) == (min_tmp + 128))
5754
      begin
5755
        // starting length is min_tmp, ending length is (min_tmp + 128)
5756
        $display("    pads appending to packets is NOT selected");
5757
        $display("    using all 128 BDs assigned to TX (wrap at 128th BD - TX BD 127)");
5758
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
5759
                 (min_tmp + 8), (min_tmp + 128));
5760
        $display("    ->packets were send from TX BD %0d to TX BD %0d respectively",
5761
                 1'b0, num_of_bd);
5762
        tmp_bd = num_of_bd + 1;
5763
        // set padding, remain the rest
5764
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5765
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5766
      end
5767
      else if ((i_length + 4) == (max_tmp - 16))
5768
      begin
5769
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
5770
        $display("    pads appending to packets is selected");
5771
        $display("    using all 128 BDs assigned to TX (wrap at 128th BD - TX BD 127)");
5772
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
5773
                 (min_tmp + 64 + 128), tmp_data);
5774
        if (tmp_bd > num_of_bd)
5775
          $display("    ->packets were send from TX BD %0d to TX BD 127 and from TX BD 0 to TX BD %0d respectively",
5776
                   tmp_bd, num_of_bd);
5777
        else
5778
          $display("    ->packets were send from TX BD %0d to TX BD %0d respectively",
5779
                   tmp_bd, num_of_bd);
5780
        tmp_bd = num_of_bd + 1;
5781
        // reset padding, remain the rest
5782
        wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
5783
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5784
      end
5785
      else if ((i_length + 4) == max_tmp)
5786
      begin
5787
        $display("    pads appending to packets is NOT selected");
5788
        $display("    using all 128 BDs assigned to TX (wrap at 128th BD - TX BD 127)");
5789
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
5790
                 (max_tmp - (4 + 16)), max_tmp);
5791
        if (tmp_bd > num_of_bd)
5792
          $display("    ->packets were send from TX BD %0d to TX BD 127 and from TX BD 0 to TX BD %0d respectively",
5793
                   tmp_bd, num_of_bd);
5794
        else
5795
          $display("    ->packets were send from TX BD %0d to TX BD %0d respectively",
5796
                   tmp_bd, num_of_bd);
5797
      end
5798
      // set length (loop variable)
5799
      if ((i_length + 4) < (min_tmp + 128))
5800
        i_length = i_length + 1;
5801
      else if ( ((i_length + 4) == (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
5802
        i_length = 256;
5803
      else if ( ((i_length + 4) > (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
5804
      begin
5805
        i_length = i_length + 128;
5806
        tmp_data = i_length + 4; // last tmp_data is ending length
5807
      end
5808
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
5809
        i_length = max_tmp - (4 + 16);
5810
      else if ((i_length + 4) >= (max_tmp - 16))
5811
        i_length = i_length + 1;
5812
      else
5813
      begin
5814
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
5815
        #10 $stop;
5816
      end
5817
      // the number of frame transmitted
5818
      num_of_frames = num_of_frames + 1;
5819
      if ((num_of_frames <= 8) || ((num_of_frames - 8) == 128))
5820
        num_of_bd = 0;
5821
      else
5822
        num_of_bd = num_of_bd + 1;
5823
    end
5824
    // disable TX
5825
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
5826
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5827
    @(posedge wb_clk);
5828
    if(fail == 0)
5829
      test_ok;
5830
    else
5831
      fail = 0;
5832
  end
5833
 
5834
 
5835
  ////////////////////////////////////////////////////////////////////
5836
  ////                                                            ////
5837
  ////  Test transmit packets form 0 to (MINFL - 1) sizes at      ////
5838
  ////  8 TX buffer decriptors ( 10Mbps ).                        ////
5839
  ////                                                            ////
5840
  ////////////////////////////////////////////////////////////////////
5841
  if (test_num == 6) // 
5842
  begin
5843
    // TEST 6: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )
5844
    test_name = "TEST 6: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )";
5845
    `TIME; $display("  TEST 6: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )");
5846
 
5847
    // reset MAC registers
5848
    hard_reset;
5849
    // reset MAC and MII LOGIC with soft reset
5850
    reset_mac;
5851
    reset_mii;
5852
    // set wb slave response
5853
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
5854
 
5855
    max_tmp = 0;
5856
    min_tmp = 0;
5857
    // set 8 TX buffer descriptors - must be set before TX enable
5858
    wbm_write(`ETH_TX_BD_NUM, 32'h8, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5859
    // enable TX, set full-duplex mode, padding and CRC appending
5860
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
5861
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5862
    // prepare two packets of MAXFL length
5863
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5864
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
5865
    min_tmp = tmp[31:16];
5866
    st_data = 8'h12;
5867
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
5868
    st_data = 8'h34;
5869
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp - 4), st_data); // length without CRC
5870
    // check WB INT signal
5871
    if (wb_int !== 1'b0)
5872
    begin
5873
      test_fail("WB INT signal should not be set");
5874
      fail = fail + 1;
5875
    end
5876
 
5877
    // write to phy's control register for 10Mbps
5878
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
5879
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
5880
    speed = 10;
5881
 
5882
    frame_started = 0;
5883
    num_of_frames = 0;
5884
    num_of_bd = 0;
5885
    i_length = 0; // 0;
5886
    while (i_length < 70) // (min_tmp - 4))
5887
    begin
5888
      #1;
5889
      // choose generating carrier sense and collision
5890
      case (i_length[1:0])
5891
      2'h0: // Interrupt is generated
5892
      begin
5893
        // Reset_tx_bd nable interrupt generation
5894
        // unmask interrupts
5895
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
5896
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5897
        // not detect carrier sense in FD and no collision
5898
        eth_phy.carrier_sense_tx_fd_detect(0);
5899
        eth_phy.collision(0);
5900
      end
5901
      2'h1: // Interrupt is not generated
5902
      begin
5903
        // set_tx_bd enable interrupt generation
5904
        // mask interrupts
5905
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5906
        // detect carrier sense in FD and no collision
5907
        eth_phy.carrier_sense_tx_fd_detect(1);
5908
        eth_phy.collision(0);
5909
      end
5910
      2'h2: // Interrupt is not generated
5911
      begin
5912
        // set_tx_bd disable the interrupt generation
5913
        // unmask interrupts
5914
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
5915
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5916
        // not detect carrier sense in FD and set collision
5917
        eth_phy.carrier_sense_tx_fd_detect(0);
5918
        eth_phy.collision(1);
5919
      end
5920
      default: // 2'h3: // Interrupt is not generated
5921
      begin
5922
        // set_tx_bd disable the interrupt generation
5923
        // mask interrupts
5924
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
5925
        // detect carrier sense in FD and set collision
5926
        eth_phy.carrier_sense_tx_fd_detect(1);
5927
        eth_phy.collision(1);
5928
      end
5929
      endcase
5930
      #1;
5931
      // first destination address on ethernet PHY
5932
      eth_phy.set_tx_mem_addr(num_of_frames * 16);
5933
      // SET packets and wrap bit
5934
      // num_of_frames <= 9 => wrap set to TX BD 0
5935
      if (num_of_frames <= 9)
5936
      begin
5937
        tmp_len = i_length; // length of frame
5938
        tmp_bd_num = 0; // TX BD number
5939
        // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5940
        // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5941
        if (tmp_len[0] == 0)
5942
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
5943
        else
5944
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
5945
        // set wrap bit
5946
        set_tx_bd_wrap(0);
5947
      end
5948
      // 10 <= num_of_frames < 18 => wrap set to TX BD 3
5949
      else if ((num_of_frames == 10) || (num_of_frames == 14))
5950
      begin
5951
        tmp_len = i_length; // length of frame
5952
        tmp_bd_num = 0; // TX BD number
5953
        while (tmp_bd_num < 4) //
5954
        begin
5955
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5956
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5957
          if (tmp_len[0] == 0)
5958
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
5959
          else
5960
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
5961
          tmp_len = tmp_len + 1;
5962
          // set TX BD number
5963
          tmp_bd_num = tmp_bd_num + 1;
5964
        end
5965
        // set wrap bit
5966
        set_tx_bd_wrap(3);
5967
      end
5968
      // 18 <= num_of_frames < 28 => wrap set to TX BD 4
5969
      else if ((num_of_frames == 18) || (num_of_frames == 23))
5970
      begin
5971
        tmp_len = i_length; // length of frame
5972
        tmp_bd_num = 0; // TX BD number
5973
        while (tmp_bd_num < 5) //
5974
        begin
5975
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5976
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5977
          if (tmp_len[0] == 0)
5978
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
5979
          else
5980
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
5981
          tmp_len = tmp_len + 1;
5982
          // set TX BD number
5983
          tmp_bd_num = tmp_bd_num + 1;
5984
        end
5985
        // set wrap bit
5986
        set_tx_bd_wrap(4);
5987
      end
5988
      // 28 <= num_of_frames < 40 => wrap set to TX BD 5
5989
      else if ((num_of_frames == 28) || (num_of_frames == 34))
5990
      begin
5991
        tmp_len = i_length; // length of frame
5992
        tmp_bd_num = 0; // TX BD number
5993
        while (tmp_bd_num < 6) //
5994
        begin
5995
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
5996
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
5997
          if (tmp_len[0] == 0)
5998
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
5999
          else
6000
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6001
          tmp_len = tmp_len + 1;
6002
          // set TX BD number
6003
          tmp_bd_num = tmp_bd_num + 1;
6004
        end
6005
        // set wrap bit
6006
        set_tx_bd_wrap(5);
6007
      end
6008
      // 40 <= num_of_frames < 54 => wrap set to TX BD 6
6009
      else if ((num_of_frames == 40) || (num_of_frames == 47))
6010
      begin
6011
        tmp_len = i_length; // length of frame
6012
        tmp_bd_num = 0; // TX BD number
6013
        while (tmp_bd_num < 7) //
6014
        begin
6015
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6016
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6017
          if (tmp_len[0] == 0)
6018
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6019
          else
6020
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6021
          tmp_len = tmp_len + 1;
6022
          // set TX BD number
6023
          tmp_bd_num = tmp_bd_num + 1;
6024
        end
6025
        // set wrap bit
6026
        set_tx_bd_wrap(6);
6027
      end
6028
      // 54 <= num_of_frames < 70 => wrap set to TX BD 7
6029
      else if ((num_of_frames == 54) || (num_of_frames == 62))
6030
      begin
6031
        tmp_len = i_length; // length of frame
6032
        tmp_bd_num = 0; // TX BD number
6033
        while (tmp_bd_num < 8) //
6034
        begin
6035
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6036
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6037
          if (tmp_len[0] == 0)
6038
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6039
          else
6040
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6041
          tmp_len = tmp_len + 1;
6042
          // set TX BD number
6043
          tmp_bd_num = tmp_bd_num + 1;
6044
        end
6045
        // set wrap bit
6046
        set_tx_bd_wrap(7);
6047
      end
6048
      #1;
6049
      // SET ready bit
6050
      if (num_of_frames < 10)
6051
        set_tx_bd_ready(0, 0);
6052
      else if (num_of_frames < 14)
6053
        set_tx_bd_ready((num_of_frames - 10), (num_of_frames - 10));
6054
      else if (num_of_frames < 18)
6055
        set_tx_bd_ready((num_of_frames - 14), (num_of_frames - 14));
6056
      else if (num_of_frames < 23)
6057
        set_tx_bd_ready((num_of_frames - 18), (num_of_frames - 18));
6058
      else if (num_of_frames < 28)
6059
        set_tx_bd_ready((num_of_frames - 23), (num_of_frames - 23));
6060
      else if (num_of_frames < 34)
6061
        set_tx_bd_ready((num_of_frames - 28), (num_of_frames - 28));
6062
      else if (num_of_frames < 40)
6063
        set_tx_bd_ready((num_of_frames - 34), (num_of_frames - 34));
6064
      else if (num_of_frames < 47)
6065
        set_tx_bd_ready((num_of_frames - 40), (num_of_frames - 40));
6066
      else if (num_of_frames < 54)
6067
        set_tx_bd_ready((num_of_frames - 47), (num_of_frames - 47));
6068
      else if (num_of_frames < 62)
6069
        set_tx_bd_ready((num_of_frames - 54), (num_of_frames - 54));
6070
      else if (num_of_frames < 70)
6071
        set_tx_bd_ready((num_of_frames - 62), (num_of_frames - 62));
6072
      // CHECK END OF TRANSMITION
6073
      frame_started = 0;
6074
      if (num_of_frames >= 5)
6075
        #1 check_tx_bd(num_of_bd, data);
6076
      fork
6077
      begin: fr_st
6078
        wait (MTxEn === 1'b1); // start transmit
6079
        frame_started = 1;
6080
      end
6081
      begin
6082
        repeat (30) @(posedge mtx_clk);
6083
        if (num_of_frames < 5)
6084
        begin
6085
          if (frame_started == 1)
6086
          begin
6087
            `TIME; $display("*E Frame should NOT start!");
6088
          end
6089
          disable fr_st;
6090
        end
6091
        else
6092
        begin
6093
          if (frame_started == 0)
6094
          begin
6095
            `TIME; $display("*W Frame should start!");
6096
            disable fr_st;
6097
          end
6098
        end
6099
      end
6100
      join
6101
      // check packets larger than 4 bytes
6102
      if (num_of_frames >= 5)
6103
      begin
6104
        wait (MTxEn === 1'b0); // end transmit
6105
        while (data[15] === 1)
6106
        begin
6107
          #1 check_tx_bd(num_of_bd, data);
6108
          @(posedge wb_clk);
6109
        end
6110
        repeat (1) @(posedge wb_clk);
6111
        // check length of a PACKET
6112
        if (i_length <= (min_tmp - 4))
6113
        begin
6114
          if (eth_phy.tx_len != min_tmp)
6115
          begin
6116
            test_fail("Wrong length of the packet out from MAC");
6117
            fail = fail + 1;
6118
          end
6119
        end
6120
        else
6121
        begin
6122
          if (eth_phy.tx_len != (i_length + 4))
6123
          begin
6124
            test_fail("Wrong length of the packet out from MAC");
6125
            fail = fail + 1;
6126
          end
6127
        end
6128
        // check transmitted TX packet data
6129
        if (i_length[0] == 0)
6130
        begin
6131
          #1 check_tx_packet(`MEMORY_BASE, (num_of_frames * 16), i_length, tmp);
6132
        end
6133
        else
6134
        begin
6135
          #1 check_tx_packet((`MEMORY_BASE + max_tmp), (num_of_frames * 16), i_length, tmp);
6136
        end
6137
        if (tmp > 0)
6138
        begin
6139
          test_fail("Wrong data of the transmitted packet");
6140
          fail = fail + 1;
6141
        end
6142
        // check transmited TX packet CRC
6143
        if (num_of_frames < (min_tmp - 4))
6144
          #1 check_tx_crc((num_of_frames * 16), (min_tmp - 4), 1'b0, tmp); // length without CRC
6145
        else
6146
          #1 check_tx_crc((num_of_frames * 16), i_length, 1'b0, tmp); // length without CRC
6147
        if (tmp > 0)
6148
        begin
6149
          test_fail("Wrong CRC of the transmitted packet");
6150
          fail = fail + 1;
6151
        end
6152
      end
6153
      // check WB INT signal
6154
      if ((i_length[1:0] == 2'h0) && (num_of_frames >= 5))
6155
      begin
6156
        if (wb_int !== 1'b1)
6157
        begin
6158
          `TIME; $display("*E WB INT signal should be set");
6159
          test_fail("WB INT signal should be set");
6160
          fail = fail + 1;
6161
        end
6162
      end
6163
      else
6164
      begin
6165
        if (wb_int !== 1'b0)
6166
        begin
6167
          `TIME; $display("*E WB INT signal should not be set");
6168
          test_fail("WB INT signal should not be set");
6169
          fail = fail + 1;
6170
        end
6171
      end
6172
      // check TX buffer descriptor of a packet
6173
      check_tx_bd(num_of_bd, data);
6174
      if (num_of_frames >= 5)
6175
      begin
6176
        if (i_length[1] == 1'b0) // interrupt enabled
6177
        begin
6178
          if ( (data[15:0] !== 16'h7800) && // wrap bit
6179
               (data[15:0] !== 16'h5800) ) // without wrap bit
6180
          begin
6181
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6182
            test_fail("TX buffer descriptor status is not correct");
6183
            fail = fail + 1;
6184
          end
6185
        end
6186
        else // interrupt not enabled
6187
        begin
6188
          if ( (data[15:0] !== 16'h3800) && // wrap bit
6189
               (data[15:0] !== 16'h1800) ) // without wrap bit
6190
          begin
6191
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6192
            test_fail("TX buffer descriptor status is not correct");
6193
            fail = fail + 1;
6194
          end
6195
        end
6196
      end
6197
      else
6198
      begin
6199
        if (data[15] !== 1'b1)
6200
        begin
6201
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6202
          test_fail("TX buffer descriptor status is not correct");
6203
          fail = fail + 1;
6204
        end
6205
      end
6206
      // clear TX BD with wrap bit
6207
      if (num_of_frames == 63)
6208
        clear_tx_bd(16, 16);
6209
      // check interrupts
6210
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6211
      if ( ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1)) && (num_of_frames >= 5) )
6212
      begin
6213
        if ((data & `ETH_INT_TXB) !== 1'b1)
6214
        begin
6215
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
6216
          test_fail("Interrupt Transmit Buffer was not set");
6217
          fail = fail + 1;
6218
        end
6219
        if ((data & (~`ETH_INT_TXB)) !== 0)
6220
        begin
6221
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
6222
          test_fail("Other interrupts (except Transmit Buffer) were set");
6223
          fail = fail + 1;
6224
        end
6225
      end
6226
      else
6227
      begin
6228
        if (data !== 0)
6229
        begin
6230
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
6231
          test_fail("Any of interrupts (except Transmit Buffer) was set");
6232
          fail = fail + 1;
6233
        end
6234
      end
6235
      // clear interrupts
6236
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6237
      // check WB INT signal
6238
      if (wb_int !== 1'b0)
6239
      begin
6240
        test_fail("WB INT signal should not be set");
6241
        fail = fail + 1;
6242
      end
6243
      // INTERMEDIATE DISPLAYS
6244
      if (i_length == 3)
6245
      begin
6246
        $display("    pads appending to packets is selected");
6247
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
6248
        $display("    ->packets with lengths from %0d to %0d are not transmitted (length increasing by 1 byte)",
6249
                 0, 3);
6250
      end
6251
      else if (i_length == 9)
6252
      begin
6253
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
6254
        $display("    ->packet with length 4 is not transmitted (length increasing by 1 byte)");
6255
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6256
                 5, 9);
6257
      end
6258
      else if (i_length == 17)
6259
      begin
6260
        $display("    using 4 BDs out of 8 BDs assigned to TX (wrap at 4th BD - TX BD 3)");
6261
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6262
                 10, 17);
6263
      end
6264
      else if (i_length == 27)
6265
      begin
6266
        $display("    using 5 BDs out of 8 BDs assigned to TX (wrap at 5th BD - TX BD 4)");
6267
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6268
                 18, 27);
6269
      end
6270
      else if (i_length == 40)
6271
      begin
6272
        $display("    using 6 BDs out of 8 BDs assigned to TX (wrap at 6th BD - TX BD 5)");
6273
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6274
                 28, 40);
6275
      end
6276
      else if (i_length == 54)
6277
      begin
6278
        $display("    using 7 BDs out of 8 BDs assigned to TX (wrap at 7th BD - TX BD 6)");
6279
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6280
                 41, 54);
6281
      end
6282
      else if (i_length == 69)
6283
      begin
6284
        $display("    using 8 BDs out of 8 BDs assigned to TX (wrap at 8th BD - TX BD 7)");
6285
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6286
                 55, 69);
6287
      end
6288
      // set length (loop variable)
6289
      i_length = i_length + 1;
6290
      // the number of frame transmitted
6291
      num_of_frames = num_of_frames + 1;
6292
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
6293
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
6294
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
6295
          (num_of_frames == 54) || (num_of_frames == 62))
6296
        num_of_bd = 0;
6297
      else
6298
        num_of_bd = num_of_bd + 1;
6299
    end
6300
    // disable TX
6301
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
6302
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6303
    @(posedge wb_clk);
6304
    if(fail == 0)
6305
      test_ok;
6306
    else
6307
      fail = 0;
6308
  end
6309
 
6310
 
6311
  ////////////////////////////////////////////////////////////////////
6312
  ////                                                            ////
6313
  ////  Test transmit packets form 0 to (MINFL - 1) sizes at      ////
6314
  ////  8 TX buffer decriptors ( 100Mbps ).                       ////
6315
  ////                                                            ////
6316
  ////////////////////////////////////////////////////////////////////
6317
  if (test_num == 7) // 
6318
  begin
6319
    // TEST 7: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )
6320
    test_name = "TEST 7: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )";
6321
    `TIME; $display("  TEST 7: TRANSMIT PACKETS FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )");
6322
 
6323
    // reset MAC registers
6324
    hard_reset;
6325
    // reset MAC and MII LOGIC with soft reset
6326
    reset_mac;
6327
    reset_mii;
6328
    // set wb slave response
6329
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
6330
 
6331
    max_tmp = 0;
6332
    min_tmp = 0;
6333
    // set 8 TX buffer descriptors - must be set before TX enable
6334
    wbm_write(`ETH_TX_BD_NUM, 32'h8, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6335
    // enable TX, set full-duplex mode, padding and CRC appending
6336
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
6337
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6338
    // prepare two packets of MAXFL length
6339
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6340
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
6341
    min_tmp = tmp[31:16];
6342
    st_data = 8'h12;
6343
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
6344
    st_data = 8'h34;
6345
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp - 4), st_data); // length without CRC
6346
    // check WB INT signal
6347
    if (wb_int !== 1'b0)
6348
    begin
6349
      test_fail("WB INT signal should not be set");
6350
      fail = fail + 1;
6351
    end
6352
 
6353
    // write to phy's control register for 100Mbps
6354
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
6355
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
6356
    speed = 100;
6357
 
6358
    frame_started = 0;
6359
    num_of_frames = 0;
6360
    num_of_bd = 0;
6361
    i_length = 0; // 0;
6362
    while (i_length < 70) // (min_tmp - 4))
6363
    begin
6364
      #1;
6365
      // choose generating carrier sense and collision
6366
      case (i_length[1:0])
6367
      2'h0: // Interrupt is generated
6368
      begin
6369
        // Reset_tx_bd nable interrupt generation
6370
        // unmask interrupts
6371
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
6372
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6373
        // not detect carrier sense in FD and no collision
6374
        eth_phy.carrier_sense_tx_fd_detect(0);
6375
        eth_phy.collision(0);
6376
      end
6377
      2'h1: // Interrupt is not generated
6378
      begin
6379
        // set_tx_bd enable interrupt generation
6380
        // mask interrupts
6381
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6382
        // detect carrier sense in FD and no collision
6383
        eth_phy.carrier_sense_tx_fd_detect(1);
6384
        eth_phy.collision(0);
6385
      end
6386
      2'h2: // Interrupt is not generated
6387
      begin
6388
        // set_tx_bd disable the interrupt generation
6389
        // unmask interrupts
6390
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
6391
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6392
        // not detect carrier sense in FD and set collision
6393
        eth_phy.carrier_sense_tx_fd_detect(0);
6394
        eth_phy.collision(1);
6395
      end
6396
      default: // 2'h3: // Interrupt is not generated
6397
      begin
6398
        // set_tx_bd disable the interrupt generation
6399
        // mask interrupts
6400
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6401
        // detect carrier sense in FD and set collision
6402
        eth_phy.carrier_sense_tx_fd_detect(1);
6403
        eth_phy.collision(1);
6404
      end
6405
      endcase
6406
      #1;
6407
      // first destination address on ethernet PHY
6408
      eth_phy.set_tx_mem_addr(num_of_frames * 16);
6409
      // SET packets and wrap bit
6410
      // num_of_frames <= 9 => wrap set to TX BD 0
6411
      if (num_of_frames <= 9)
6412
      begin
6413
        tmp_len = i_length; // length of frame
6414
        tmp_bd_num = 0; // TX BD number
6415
        // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6416
        // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6417
        if (tmp_len[0] == 0)
6418
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6419
        else
6420
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6421
        // set wrap bit
6422
        set_tx_bd_wrap(0);
6423
      end
6424
      // 10 <= num_of_frames < 18 => wrap set to TX BD 3
6425
      else if ((num_of_frames == 10) || (num_of_frames == 14))
6426
      begin
6427
        tmp_len = i_length; // length of frame
6428
        tmp_bd_num = 0; // TX BD number
6429
        while (tmp_bd_num < 4) //
6430
        begin
6431
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6432
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6433
          if (tmp_len[0] == 0)
6434
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6435
          else
6436
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6437
          tmp_len = tmp_len + 1;
6438
          // set TX BD number
6439
          tmp_bd_num = tmp_bd_num + 1;
6440
        end
6441
        // set wrap bit
6442
        set_tx_bd_wrap(3);
6443
      end
6444
      // 18 <= num_of_frames < 28 => wrap set to TX BD 4
6445
      else if ((num_of_frames == 18) || (num_of_frames == 23))
6446
      begin
6447
        tmp_len = i_length; // length of frame
6448
        tmp_bd_num = 0; // TX BD number
6449
        while (tmp_bd_num < 5) //
6450
        begin
6451
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6452
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6453
          if (tmp_len[0] == 0)
6454
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6455
          else
6456
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6457
          tmp_len = tmp_len + 1;
6458
          // set TX BD number
6459
          tmp_bd_num = tmp_bd_num + 1;
6460
        end
6461
        // set wrap bit
6462
        set_tx_bd_wrap(4);
6463
      end
6464
      // 28 <= num_of_frames < 40 => wrap set to TX BD 5
6465
      else if ((num_of_frames == 28) || (num_of_frames == 34))
6466
      begin
6467
        tmp_len = i_length; // length of frame
6468
        tmp_bd_num = 0; // TX BD number
6469
        while (tmp_bd_num < 6) //
6470
        begin
6471
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6472
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6473
          if (tmp_len[0] == 0)
6474
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6475
          else
6476
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6477
          tmp_len = tmp_len + 1;
6478
          // set TX BD number
6479
          tmp_bd_num = tmp_bd_num + 1;
6480
        end
6481
        // set wrap bit
6482
        set_tx_bd_wrap(5);
6483
      end
6484
      // 40 <= num_of_frames < 54 => wrap set to TX BD 6
6485
      else if ((num_of_frames == 40) || (num_of_frames == 47))
6486
      begin
6487
        tmp_len = i_length; // length of frame
6488
        tmp_bd_num = 0; // TX BD number
6489
        while (tmp_bd_num < 7) //
6490
        begin
6491
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6492
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6493
          if (tmp_len[0] == 0)
6494
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6495
          else
6496
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6497
          tmp_len = tmp_len + 1;
6498
          // set TX BD number
6499
          tmp_bd_num = tmp_bd_num + 1;
6500
        end
6501
        // set wrap bit
6502
        set_tx_bd_wrap(6);
6503
      end
6504
      // 54 <= num_of_frames < 70 => wrap set to TX BD 7
6505
      else if ((num_of_frames == 54) || (num_of_frames == 62))
6506
      begin
6507
        tmp_len = i_length; // length of frame
6508
        tmp_bd_num = 0; // TX BD number
6509
        while (tmp_bd_num < 8) //
6510
        begin
6511
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6512
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6513
          if (tmp_len[0] == 0)
6514
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
6515
          else
6516
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
6517
          tmp_len = tmp_len + 1;
6518
          // set TX BD number
6519
          tmp_bd_num = tmp_bd_num + 1;
6520
        end
6521
        // set wrap bit
6522
        set_tx_bd_wrap(7);
6523
      end
6524
      #1;
6525
      // SET ready bit
6526
      if (num_of_frames < 10)
6527
        set_tx_bd_ready(0, 0);
6528
      else if (num_of_frames < 14)
6529
        set_tx_bd_ready((num_of_frames - 10), (num_of_frames - 10));
6530
      else if (num_of_frames < 18)
6531
        set_tx_bd_ready((num_of_frames - 14), (num_of_frames - 14));
6532
      else if (num_of_frames < 23)
6533
        set_tx_bd_ready((num_of_frames - 18), (num_of_frames - 18));
6534
      else if (num_of_frames < 28)
6535
        set_tx_bd_ready((num_of_frames - 23), (num_of_frames - 23));
6536
      else if (num_of_frames < 34)
6537
        set_tx_bd_ready((num_of_frames - 28), (num_of_frames - 28));
6538
      else if (num_of_frames < 40)
6539
        set_tx_bd_ready((num_of_frames - 34), (num_of_frames - 34));
6540
      else if (num_of_frames < 47)
6541
        set_tx_bd_ready((num_of_frames - 40), (num_of_frames - 40));
6542
      else if (num_of_frames < 54)
6543
        set_tx_bd_ready((num_of_frames - 47), (num_of_frames - 47));
6544
      else if (num_of_frames < 62)
6545
        set_tx_bd_ready((num_of_frames - 54), (num_of_frames - 54));
6546
      else if (num_of_frames < 70)
6547
        set_tx_bd_ready((num_of_frames - 62), (num_of_frames - 62));
6548
      // CHECK END OF TRANSMITION
6549
      frame_started = 0;
6550
      if (num_of_frames >= 5)
6551
        #1 check_tx_bd(num_of_bd, data);
6552
      fork
6553
      begin: fr_st1
6554
        wait (MTxEn === 1'b1); // start transmit
6555
        frame_started = 1;
6556
      end
6557
      begin
6558
        repeat (30) @(posedge mtx_clk);
6559
        if (num_of_frames < 5)
6560
        begin
6561
          if (frame_started == 1)
6562
          begin
6563
            `TIME; $display("*E Frame should NOT start!");
6564
          end
6565
          disable fr_st1;
6566
        end
6567
        else
6568
        begin
6569
          if (frame_started == 0)
6570
          begin
6571
            `TIME; $display("*W Frame should start!");
6572
            disable fr_st1;
6573
          end
6574
        end
6575
      end
6576
      join
6577
      // check packets larger than 4 bytes
6578
      if (num_of_frames >= 5)
6579
      begin
6580
        wait (MTxEn === 1'b0); // end transmit
6581
        while (data[15] === 1)
6582
        begin
6583
          #1 check_tx_bd(num_of_bd, data);
6584
          @(posedge wb_clk);
6585
        end
6586
        repeat (1) @(posedge wb_clk);
6587
        // check length of a PACKET
6588
        if (i_length <= (min_tmp - 4))
6589
        begin
6590
          if (eth_phy.tx_len != min_tmp)
6591
          begin
6592
            test_fail("Wrong length of the packet out from MAC");
6593
            fail = fail + 1;
6594
          end
6595
        end
6596
        else
6597
        begin
6598
          if (eth_phy.tx_len != (i_length + 4))
6599
          begin
6600
            test_fail("Wrong length of the packet out from MAC");
6601
            fail = fail + 1;
6602
          end
6603
        end
6604
        // check transmitted TX packet data
6605
        if (i_length[0] == 0)
6606
        begin
6607
          #1 check_tx_packet(`MEMORY_BASE, (num_of_frames * 16), i_length, tmp);
6608
        end
6609
        else
6610
        begin
6611
          #1 check_tx_packet((`MEMORY_BASE + max_tmp), (num_of_frames * 16), i_length, tmp);
6612
        end
6613
        if (tmp > 0)
6614
        begin
6615
          test_fail("Wrong data of the transmitted packet");
6616
          fail = fail + 1;
6617
        end
6618
        // check transmited TX packet CRC
6619
        if (num_of_frames < (min_tmp - 4))
6620
          #1 check_tx_crc((num_of_frames * 16), (min_tmp - 4), 1'b0, tmp); // length without CRC
6621
        else
6622
          #1 check_tx_crc((num_of_frames * 16), i_length, 1'b0, tmp); // length without CRC
6623
        if (tmp > 0)
6624
        begin
6625
          test_fail("Wrong CRC of the transmitted packet");
6626
          fail = fail + 1;
6627
        end
6628
      end
6629
      // check WB INT signal
6630
      if ((i_length[1:0] == 2'h0) && (num_of_frames >= 5))
6631
      begin
6632
        if (wb_int !== 1'b1)
6633
        begin
6634
          `TIME; $display("*E WB INT signal should be set");
6635
          test_fail("WB INT signal should be set");
6636
          fail = fail + 1;
6637
        end
6638
      end
6639
      else
6640
      begin
6641
        if (wb_int !== 1'b0)
6642
        begin
6643
          `TIME; $display("*E WB INT signal should not be set");
6644
          test_fail("WB INT signal should not be set");
6645
          fail = fail + 1;
6646
        end
6647
      end
6648
      // check TX buffer descriptor of a packet
6649
      check_tx_bd(num_of_bd, data);
6650
      if (num_of_frames >= 5)
6651
      begin
6652
        if (i_length[1] == 1'b0) // interrupt enabled
6653
        begin
6654
          if ( (data[15:0] !== 16'h7800) && // wrap bit
6655
               (data[15:0] !== 16'h5800) ) // without wrap bit
6656
          begin
6657
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6658
            test_fail("TX buffer descriptor status is not correct");
6659
            fail = fail + 1;
6660
          end
6661
        end
6662
        else // interrupt not enabled
6663
        begin
6664
          if ( (data[15:0] !== 16'h3800) && // wrap bit
6665
               (data[15:0] !== 16'h1800) ) // without wrap bit
6666
          begin
6667
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6668
            test_fail("TX buffer descriptor status is not correct");
6669
            fail = fail + 1;
6670
          end
6671
        end
6672
      end
6673
      else
6674
      begin
6675
        if (data[15] !== 1'b1)
6676
        begin
6677
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
6678
          test_fail("TX buffer descriptor status is not correct");
6679
          fail = fail + 1;
6680
        end
6681
      end
6682
      // clear TX BD with wrap bit
6683
      if (num_of_frames == 63)
6684
        clear_tx_bd(16, 16);
6685
      // check interrupts
6686
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6687
      if ( ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1)) && (num_of_frames >= 5) )
6688
      begin
6689
        if ((data & `ETH_INT_TXB) !== 1'b1)
6690
        begin
6691
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
6692
          test_fail("Interrupt Transmit Buffer was not set");
6693
          fail = fail + 1;
6694
        end
6695
        if ((data & (~`ETH_INT_TXB)) !== 0)
6696
        begin
6697
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
6698
          test_fail("Other interrupts (except Transmit Buffer) were set");
6699
          fail = fail + 1;
6700
        end
6701
      end
6702
      else
6703
      begin
6704
        if (data !== 0)
6705
        begin
6706
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
6707
          test_fail("Any of interrupts (except Transmit Buffer) was set");
6708
          fail = fail + 1;
6709
        end
6710
      end
6711
      // clear interrupts
6712
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6713
      // check WB INT signal
6714
      if (wb_int !== 1'b0)
6715
      begin
6716
        test_fail("WB INT signal should not be set");
6717
        fail = fail + 1;
6718
      end
6719
      // INTERMEDIATE DISPLAYS
6720
      if (i_length == 3)
6721
      begin
6722
        $display("    pads appending to packets is selected");
6723
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
6724
        $display("    ->packets with lengths from %0d to %0d are not transmitted (length increasing by 1 byte)",
6725
                 0, 3);
6726
      end
6727
      else if (i_length == 9)
6728
      begin
6729
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
6730
        $display("    ->packet with length 4 is not transmitted (length increasing by 1 byte)");
6731
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6732
                 5, 9);
6733
      end
6734
      else if (i_length == 17)
6735
      begin
6736
        $display("    using 4 BDs out of 8 BDs assigned to TX (wrap at 4th BD - TX BD 3)");
6737
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6738
                 10, 17);
6739
      end
6740
      else if (i_length == 27)
6741
      begin
6742
        $display("    using 5 BDs out of 8 BDs assigned to TX (wrap at 5th BD - TX BD 4)");
6743
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6744
                 18, 27);
6745
      end
6746
      else if (i_length == 40)
6747
      begin
6748
        $display("    using 6 BDs out of 8 BDs assigned to TX (wrap at 6th BD - TX BD 5)");
6749
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6750
                 28, 40);
6751
      end
6752
      else if (i_length == 54)
6753
      begin
6754
        $display("    using 7 BDs out of 8 BDs assigned to TX (wrap at 7th BD - TX BD 6)");
6755
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6756
                 41, 54);
6757
      end
6758
      else if (i_length == 69)
6759
      begin
6760
        $display("    using 8 BDs out of 8 BDs assigned to TX (wrap at 8th BD - TX BD 7)");
6761
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
6762
                 55, 69);
6763
      end
6764
      // set length (loop variable)
6765
      i_length = i_length + 1;
6766
      // the number of frame transmitted
6767
      num_of_frames = num_of_frames + 1;
6768
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
6769
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
6770
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
6771
          (num_of_frames == 54) || (num_of_frames == 62))
6772
        num_of_bd = 0;
6773
      else
6774
        num_of_bd = num_of_bd + 1;
6775
    end
6776
    // disable TX
6777
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
6778
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6779
    @(posedge wb_clk);
6780
    if(fail == 0)
6781
      test_ok;
6782
    else
6783
      fail = 0;
6784
  end
6785
 
6786
 
6787
  ////////////////////////////////////////////////////////////////////
6788
  ////                                                            ////
6789 243 tadejm
  ////  Test transmit packets (no pads) from 0 to (MINFL - 1)     ////
6790 209 tadejm
  ////  sizes at 8 TX buffer decriptors ( 10Mbps ).               ////
6791
  ////                                                            ////
6792
  ////////////////////////////////////////////////////////////////////
6793
  if (test_num == 8) // 
6794
  begin
6795 243 tadejm
    // TEST 8: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )
6796
    test_name = "TEST 8: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )";
6797
    `TIME; $display("  TEST 8: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 10Mbps )");
6798 209 tadejm
 
6799
    // reset MAC registers
6800
    hard_reset;
6801
    // reset MAC and MII LOGIC with soft reset
6802
    reset_mac;
6803
    reset_mii;
6804
    // set wb slave response
6805
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
6806
 
6807
    max_tmp = 0;
6808
    min_tmp = 0;
6809
    // set 8 TX buffer descriptors - must be set before TX enable
6810
    wbm_write(`ETH_TX_BD_NUM, 32'h8, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6811
    // enable TX, set full-duplex mode, padding and CRC appending
6812 243 tadejm
//    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
6813
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD,
6814 209 tadejm
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6815
    // prepare two packets of MAXFL length
6816
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6817
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
6818
    min_tmp = tmp[31:16];
6819
    st_data = 8'h12;
6820
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
6821
    st_data = 8'h34;
6822
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp - 4), st_data); // length without CRC
6823
    // check WB INT signal
6824
    if (wb_int !== 1'b0)
6825
    begin
6826
      test_fail("WB INT signal should not be set");
6827
      fail = fail + 1;
6828
    end
6829
 
6830
    // write to phy's control register for 10Mbps
6831
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
6832
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
6833
    speed = 10;
6834
 
6835
    frame_started = 0;
6836
    num_of_frames = 0;
6837
    num_of_bd = 0;
6838
    i_length = 0; // 0;
6839
    while (i_length < 70) // (min_tmp - 4))
6840
    begin
6841
      #1;
6842
      // choose generating carrier sense and collision
6843
      case (i_length[1:0])
6844
      2'h0: // Interrupt is generated
6845
      begin
6846
        // Reset_tx_bd nable interrupt generation
6847
        // unmask interrupts
6848
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
6849
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6850
        // not detect carrier sense in FD and no collision
6851
        eth_phy.carrier_sense_tx_fd_detect(0);
6852
        eth_phy.collision(0);
6853
      end
6854
      2'h1: // Interrupt is not generated
6855
      begin
6856
        // set_tx_bd enable interrupt generation
6857
        // mask interrupts
6858
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6859
        // detect carrier sense in FD and no collision
6860
        eth_phy.carrier_sense_tx_fd_detect(1);
6861
        eth_phy.collision(0);
6862
      end
6863
      2'h2: // Interrupt is not generated
6864
      begin
6865
        // set_tx_bd disable the interrupt generation
6866
        // unmask interrupts
6867
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
6868
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6869
        // not detect carrier sense in FD and set collision
6870
        eth_phy.carrier_sense_tx_fd_detect(0);
6871
        eth_phy.collision(1);
6872
      end
6873
      default: // 2'h3: // Interrupt is not generated
6874
      begin
6875
        // set_tx_bd disable the interrupt generation
6876
        // mask interrupts
6877
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
6878
        // detect carrier sense in FD and set collision
6879
        eth_phy.carrier_sense_tx_fd_detect(1);
6880
        eth_phy.collision(1);
6881
      end
6882
      endcase
6883 243 tadejm
//      // append CRC
6884
//      if ((i_length[0] == 1'b0) && (num_of_frames >= 6))
6885
//      begin
6886
//        append_tx_crc(`MEMORY_BASE, i_length, 1'b0);
6887
//      end
6888 209 tadejm
      #1;
6889
      // first destination address on ethernet PHY
6890
      eth_phy.set_tx_mem_addr(num_of_frames * 16);
6891
      // SET packets and wrap bit
6892
      // num_of_frames <= 9 => wrap set to TX BD 0
6893 243 tadejm
      if (num_of_frames <= 5)
6894
        begin
6895
          tmp_len = i_length; // length of frame
6896
          tmp_bd_num = 0; // TX BD number
6897
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6898
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6899
          if (tmp_len[0] == 0) // CRC appended by 'HARDWARE'
6900
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, `MEMORY_BASE);
6901
          else
6902
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
6903
          // set wrap bit
6904
          set_tx_bd_wrap(0);
6905
        end
6906
        else if (num_of_frames <= 9)
6907
        begin
6908
          tmp_len = i_length; // length of frame
6909
          tmp_bd_num = 0; // TX BD number
6910
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6911
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6912
          if (tmp_len[0] == 0) // CRC appended by 'SOFTWARE'
6913
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
6914
          else
6915
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
6916
          // set wrap bit
6917
          set_tx_bd_wrap(0);
6918 209 tadejm
      end
6919
      // 10 <= num_of_frames < 18 => wrap set to TX BD 3
6920
      else if ((num_of_frames == 10) || (num_of_frames == 14))
6921
      begin
6922
        tmp_len = i_length; // length of frame
6923
        tmp_bd_num = 0; // TX BD number
6924
        while (tmp_bd_num < 4) //
6925
        begin
6926
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6927
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6928
          if (tmp_len[0] == 0)
6929 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
6930 209 tadejm
          else
6931 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
6932 209 tadejm
          tmp_len = tmp_len + 1;
6933
          // set TX BD number
6934
          tmp_bd_num = tmp_bd_num + 1;
6935
        end
6936
        // set wrap bit
6937
        set_tx_bd_wrap(3);
6938
      end
6939
      // 18 <= num_of_frames < 28 => wrap set to TX BD 4
6940
      else if ((num_of_frames == 18) || (num_of_frames == 23))
6941
      begin
6942
        tmp_len = i_length; // length of frame
6943
        tmp_bd_num = 0; // TX BD number
6944
        while (tmp_bd_num < 5) //
6945
        begin
6946
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6947
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6948
          if (tmp_len[0] == 0)
6949 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
6950
          else // when (num_of_frames == 23), (i_length == 23) and therefor i_length[0] == 1 !!!
6951
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1],
6952
                      ((num_of_frames == 23) && (tmp_bd_num == 0)), 1'b1, (`MEMORY_BASE + max_tmp));
6953 209 tadejm
          tmp_len = tmp_len + 1;
6954
          // set TX BD number
6955
          tmp_bd_num = tmp_bd_num + 1;
6956
        end
6957
        // set wrap bit
6958
        set_tx_bd_wrap(4);
6959
      end
6960
      // 28 <= num_of_frames < 40 => wrap set to TX BD 5
6961
      else if ((num_of_frames == 28) || (num_of_frames == 34))
6962
      begin
6963
        tmp_len = i_length; // length of frame
6964
        tmp_bd_num = 0; // TX BD number
6965
        while (tmp_bd_num < 6) //
6966
        begin
6967
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6968
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6969
          if (tmp_len[0] == 0)
6970 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
6971 209 tadejm
          else
6972 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
6973 209 tadejm
          tmp_len = tmp_len + 1;
6974
          // set TX BD number
6975
          tmp_bd_num = tmp_bd_num + 1;
6976
        end
6977
        // set wrap bit
6978
        set_tx_bd_wrap(5);
6979
      end
6980
      // 40 <= num_of_frames < 54 => wrap set to TX BD 6
6981
      else if ((num_of_frames == 40) || (num_of_frames == 47))
6982
      begin
6983
        tmp_len = i_length; // length of frame
6984
        tmp_bd_num = 0; // TX BD number
6985
        while (tmp_bd_num < 7) //
6986
        begin
6987
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
6988
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
6989
          if (tmp_len[0] == 0)
6990 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
6991 209 tadejm
          else
6992 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
6993 209 tadejm
          tmp_len = tmp_len + 1;
6994
          // set TX BD number
6995
          tmp_bd_num = tmp_bd_num + 1;
6996
        end
6997
        // set wrap bit
6998
        set_tx_bd_wrap(6);
6999
      end
7000
      // 54 <= num_of_frames < 70 => wrap set to TX BD 7
7001
      else if ((num_of_frames == 54) || (num_of_frames == 62))
7002
      begin
7003
        tmp_len = i_length; // length of frame
7004
        tmp_bd_num = 0; // TX BD number
7005
        while (tmp_bd_num < 8) //
7006
        begin
7007
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7008
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7009
          if (tmp_len[0] == 0)
7010 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, (tmp_len + 0), !tmp_len[1], 1'b0, 1'b0, `MEMORY_BASE);
7011 209 tadejm
          else
7012 243 tadejm
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b0, 1'b1, (`MEMORY_BASE + max_tmp));
7013 209 tadejm
          tmp_len = tmp_len + 1;
7014
          // set TX BD number
7015
          tmp_bd_num = tmp_bd_num + 1;
7016
        end
7017
        // set wrap bit
7018
        set_tx_bd_wrap(7);
7019
      end
7020
      #1;
7021
      // SET ready bit
7022
      if (num_of_frames < 10)
7023
        set_tx_bd_ready(0, 0);
7024
      else if (num_of_frames < 14)
7025
        set_tx_bd_ready((num_of_frames - 10), (num_of_frames - 10));
7026
      else if (num_of_frames < 18)
7027
        set_tx_bd_ready((num_of_frames - 14), (num_of_frames - 14));
7028
      else if (num_of_frames < 23)
7029
        set_tx_bd_ready((num_of_frames - 18), (num_of_frames - 18));
7030
      else if (num_of_frames < 28)
7031
        set_tx_bd_ready((num_of_frames - 23), (num_of_frames - 23));
7032
      else if (num_of_frames < 34)
7033
        set_tx_bd_ready((num_of_frames - 28), (num_of_frames - 28));
7034
      else if (num_of_frames < 40)
7035
        set_tx_bd_ready((num_of_frames - 34), (num_of_frames - 34));
7036
      else if (num_of_frames < 47)
7037
        set_tx_bd_ready((num_of_frames - 40), (num_of_frames - 40));
7038
      else if (num_of_frames < 54)
7039
        set_tx_bd_ready((num_of_frames - 47), (num_of_frames - 47));
7040
      else if (num_of_frames < 62)
7041
        set_tx_bd_ready((num_of_frames - 54), (num_of_frames - 54));
7042
      else if (num_of_frames < 70)
7043
        set_tx_bd_ready((num_of_frames - 62), (num_of_frames - 62));
7044
      // CHECK END OF TRANSMITION
7045
      frame_started = 0;
7046
      if (num_of_frames >= 5)
7047
        #1 check_tx_bd(num_of_bd, data);
7048
      fork
7049
      begin: fr_st2
7050
        wait (MTxEn === 1'b1); // start transmit
7051
        frame_started = 1;
7052
      end
7053
      begin
7054
        repeat (30) @(posedge mtx_clk);
7055
        if (num_of_frames < 5)
7056
        begin
7057
          if (frame_started == 1)
7058
          begin
7059
            `TIME; $display("*E Frame should NOT start!");
7060
          end
7061
          disable fr_st2;
7062
        end
7063
        else
7064
        begin
7065
          if (frame_started == 0)
7066
          begin
7067
            `TIME; $display("*W Frame should start!");
7068
            disable fr_st2;
7069
          end
7070
        end
7071
      end
7072
      join
7073
      // check packets larger than 4 bytes
7074
      if (num_of_frames >= 5)
7075
      begin
7076
        wait (MTxEn === 1'b0); // end transmit
7077
        while (data[15] === 1)
7078
        begin
7079
          #1 check_tx_bd(num_of_bd, data);
7080
          @(posedge wb_clk);
7081
        end
7082
        repeat (1) @(posedge wb_clk);
7083 243 tadejm
        // check length of a PACKET 
7084
        if ((eth_phy.tx_len != i_length) && (i_length[0] == 1'b0) && (num_of_frames >= 6))
7085 209 tadejm
        begin
7086
          `TIME; $display("*E Wrong length of the packet out from MAC");
7087
          test_fail("Wrong length of the packet out from MAC");
7088
          fail = fail + 1;
7089
        end
7090 243 tadejm
        else if ((eth_phy.tx_len != (i_length + 4)) && (num_of_frames != 23))
7091
        begin
7092
          `TIME; $display("*E Wrong length of the packet out from MAC");
7093
          test_fail("Wrong length of the packet out from MAC");
7094
          fail = fail + 1;
7095
        end
7096
        else if ((eth_phy.tx_len != (min_tmp)) && (num_of_frames == 23))
7097
        begin
7098
          `TIME; $display("*E Wrong length of the packet out from MAC");
7099
          test_fail("Wrong length of the packet out from MAC");
7100
          fail = fail + 1;
7101
        end
7102 209 tadejm
        // check transmitted TX packet data
7103
        if (i_length[0] == 0)
7104
        begin
7105
          #1 check_tx_packet(`MEMORY_BASE, (num_of_frames * 16), i_length, tmp);
7106
        end
7107 243 tadejm
        else if (num_of_frames == 23) // i_length[0] == 1 here
7108
        begin
7109
          #1 check_tx_packet((`MEMORY_BASE + max_tmp), (num_of_frames * 16), (min_tmp - 4), tmp);
7110
        end
7111 209 tadejm
        else
7112
        begin
7113
          #1 check_tx_packet((`MEMORY_BASE + max_tmp), (num_of_frames * 16), i_length, tmp);
7114
        end
7115
        if (tmp > 0)
7116
        begin
7117
          test_fail("Wrong data of the transmitted packet");
7118
          fail = fail + 1;
7119
        end
7120
        // check transmited TX packet CRC
7121 243 tadejm
        #1;
7122
        if ((i_length[0] == 1'b0) && (num_of_frames >= 6))
7123
        begin
7124
        end
7125
        else
7126
          check_tx_crc((num_of_frames * 16), (eth_phy.tx_len - 4), 1'b0, tmp); // length without CRC
7127 209 tadejm
        if (tmp > 0)
7128
        begin
7129
          test_fail("Wrong CRC of the transmitted packet");
7130
          fail = fail + 1;
7131
        end
7132
      end
7133
      // check WB INT signal
7134
      if ((i_length[1:0] == 2'h0) && (num_of_frames >= 5))
7135
      begin
7136
        if (wb_int !== 1'b1)
7137
        begin
7138
          `TIME; $display("*E WB INT signal should be set");
7139
          test_fail("WB INT signal should be set");
7140
          fail = fail + 1;
7141
        end
7142
      end
7143
      else
7144
      begin
7145
        if (wb_int !== 1'b0)
7146
        begin
7147
          `TIME; $display("*E WB INT signal should not be set");
7148
          test_fail("WB INT signal should not be set");
7149
          fail = fail + 1;
7150
        end
7151
      end
7152
      // check TX buffer descriptor of a packet
7153
      check_tx_bd(num_of_bd, data);
7154
      if (num_of_frames >= 5)
7155
      begin
7156 243 tadejm
        if ((i_length[1] == 1'b0) && (i_length[0] == 1'b0)) // interrupt enabled
7157 209 tadejm
        begin
7158 243 tadejm
          if ( (data[15:0] !== 16'h6000) &&  // wrap bit
7159
               (data[15:0] !== 16'h4000) )  // without wrap bit
7160 209 tadejm
          begin
7161
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7162
            test_fail("TX buffer descriptor status is not correct");
7163
            fail = fail + 1;
7164
          end
7165
        end
7166 243 tadejm
        else if ((i_length[1] == 1'b1) && (i_length[0] == 1'b0)) // interrupt not enabled
7167 209 tadejm
        begin
7168 243 tadejm
          if ( (data[15:0] !== 16'h2000) && // wrap bit
7169
               (data[15:0] !== 16'h0000) ) // without wrap bit
7170
          begin
7171
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7172
            test_fail("TX buffer descriptor status is not correct");
7173
            fail = fail + 1;
7174
          end
7175
        end
7176
        else if ((i_length[1] == 1'b0) && (i_length[0] == 1'b1)) // interrupt enabled
7177
        begin
7178
          if ( (data[15:0] !== 16'h6800) && // wrap bit
7179
               (data[15:0] !== 16'h4800) ) // without wrap bit
7180
          begin
7181
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7182
            test_fail("TX buffer descriptor status is not correct");
7183
            fail = fail + 1;
7184
          end
7185
        end
7186
        else if (num_of_frames != 23) // ((i_length[1] == 1'b1) && (i_length[0] == 1'b1)) // interrupt not enabled
7187
        begin
7188
          if ( (data[15:0] !== 16'h2800) && // wrap bit
7189
               (data[15:0] !== 16'h0800) ) // without wrap bit
7190
          begin
7191
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7192
            test_fail("TX buffer descriptor status is not correct");
7193
            fail = fail + 1;
7194
          end
7195
        end
7196
        else // ((num_of_frames != 23) && (i_length[1] == 1'b1) && (i_length[0] == 1'b1)) // interrupt not enabled
7197
        begin
7198 209 tadejm
          if ( (data[15:0] !== 16'h3800) && // wrap bit
7199
               (data[15:0] !== 16'h1800) ) // without wrap bit
7200
          begin
7201
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7202
            test_fail("TX buffer descriptor status is not correct");
7203
            fail = fail + 1;
7204
          end
7205
        end
7206
      end
7207
      else
7208
      begin
7209
        if (data[15] !== 1'b1)
7210
        begin
7211
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7212
          test_fail("TX buffer descriptor status is not correct");
7213
          fail = fail + 1;
7214
        end
7215
      end
7216
      // clear TX BD with wrap bit
7217
      if (num_of_frames == 63)
7218
        clear_tx_bd(16, 16);
7219
      // check interrupts
7220
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7221
      if ( ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1)) && (num_of_frames >= 5) )
7222
      begin
7223
        if ((data & `ETH_INT_TXB) !== 1'b1)
7224
        begin
7225
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
7226
          test_fail("Interrupt Transmit Buffer was not set");
7227
          fail = fail + 1;
7228
        end
7229
        if ((data & (~`ETH_INT_TXB)) !== 0)
7230
        begin
7231
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
7232
          test_fail("Other interrupts (except Transmit Buffer) were set");
7233
          fail = fail + 1;
7234
        end
7235
      end
7236
      else
7237
      begin
7238
        if (data !== 0)
7239
        begin
7240
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
7241
          test_fail("Any of interrupts (except Transmit Buffer) was set");
7242
          fail = fail + 1;
7243
        end
7244
      end
7245
      // clear interrupts
7246
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7247
      // check WB INT signal
7248
      if (wb_int !== 1'b0)
7249
      begin
7250
        test_fail("WB INT signal should not be set");
7251
        fail = fail + 1;
7252
      end
7253
      // INTERMEDIATE DISPLAYS
7254
      if (i_length == 3)
7255
      begin
7256
        $display("    pads appending to packets is selected");
7257
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
7258
        $display("    ->packets with lengths from %0d to %0d are not transmitted (length increasing by 1 byte)",
7259
                 0, 3);
7260
      end
7261
      else if (i_length == 9)
7262
      begin
7263
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
7264
        $display("    ->packet with length 4 is not transmitted (length increasing by 1 byte)");
7265
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7266
                 5, 9);
7267
      end
7268
      else if (i_length == 17)
7269
      begin
7270
        $display("    using 4 BDs out of 8 BDs assigned to TX (wrap at 4th BD - TX BD 3)");
7271
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7272
                 10, 17);
7273
      end
7274
      else if (i_length == 27)
7275
      begin
7276
        $display("    using 5 BDs out of 8 BDs assigned to TX (wrap at 5th BD - TX BD 4)");
7277
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7278
                 18, 27);
7279
      end
7280
      else if (i_length == 40)
7281
      begin
7282
        $display("    using 6 BDs out of 8 BDs assigned to TX (wrap at 6th BD - TX BD 5)");
7283
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7284
                 28, 40);
7285
      end
7286
      else if (i_length == 54)
7287
      begin
7288
        $display("    using 7 BDs out of 8 BDs assigned to TX (wrap at 7th BD - TX BD 6)");
7289
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7290
                 41, 54);
7291
      end
7292
      else if (i_length == 69)
7293
      begin
7294
        $display("    using 8 BDs out of 8 BDs assigned to TX (wrap at 8th BD - TX BD 7)");
7295
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7296
                 55, 69);
7297
      end
7298
      // set length (loop variable)
7299
      i_length = i_length + 1;
7300
      // the number of frame transmitted
7301
      num_of_frames = num_of_frames + 1;
7302
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
7303
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
7304
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
7305
          (num_of_frames == 54) || (num_of_frames == 62))
7306
        num_of_bd = 0;
7307
      else
7308
        num_of_bd = num_of_bd + 1;
7309
    end
7310
    // disable TX
7311
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
7312
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7313
    @(posedge wb_clk);
7314
    if(fail == 0)
7315
      test_ok;
7316
    else
7317
      fail = 0;
7318
  end
7319
 
7320
 
7321
  ////////////////////////////////////////////////////////////////////
7322
  ////                                                            ////
7323 243 tadejm
  ////  Test transmit packets (no pads) form 0 to (MINFL - 1)     ////
7324 209 tadejm
  ////  sizes at 8 TX buffer decriptors ( 100Mbps ).              ////
7325
  ////                                                            ////
7326
  ////////////////////////////////////////////////////////////////////
7327
  if (test_num == 9) // 
7328
  begin
7329 243 tadejm
    // TEST 9: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )
7330
    test_name = "TEST 9: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )";
7331
    `TIME; $display("  TEST 9: TRANSMIT PACKETS (NO PADs) FROM 0 TO (MINFL - 1) SIZES AT 8 TX BD ( 100Mbps )");
7332 209 tadejm
 
7333
    // reset MAC registers
7334
    hard_reset;
7335
    // reset MAC and MII LOGIC with soft reset
7336
    reset_mac;
7337
    reset_mii;
7338
    // set wb slave response
7339
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
7340
 
7341
    max_tmp = 0;
7342
    min_tmp = 0;
7343
    // set 8 TX buffer descriptors - must be set before TX enable
7344
    wbm_write(`ETH_TX_BD_NUM, 32'h8, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7345
    // enable TX, set full-duplex mode, padding and CRC appending
7346
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
7347
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7348
    // prepare two packets of MAXFL length
7349
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7350
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
7351
    min_tmp = tmp[31:16];
7352
    st_data = 8'h12;
7353
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
7354
    st_data = 8'h34;
7355
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp - 4), st_data); // length without CRC
7356
    // check WB INT signal
7357
    if (wb_int !== 1'b0)
7358
    begin
7359
      test_fail("WB INT signal should not be set");
7360
      fail = fail + 1;
7361
    end
7362
 
7363
    // write to phy's control register for 100Mbps
7364
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
7365
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
7366
    speed = 100;
7367
 
7368
    frame_started = 0;
7369
    num_of_frames = 0;
7370
    num_of_bd = 0;
7371
    i_length = 0; // 0;
7372
    while (i_length < 70) // (min_tmp - 4))
7373
    begin
7374
      #1;
7375
      // choose generating carrier sense and collision
7376
      case (i_length[1:0])
7377
      2'h0: // Interrupt is generated
7378
      begin
7379
        // Reset_tx_bd nable interrupt generation
7380
        // unmask interrupts
7381
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
7382
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7383
        // not detect carrier sense in FD and no collision
7384
        eth_phy.carrier_sense_tx_fd_detect(0);
7385
        eth_phy.collision(0);
7386
      end
7387
      2'h1: // Interrupt is not generated
7388
      begin
7389
        // set_tx_bd enable interrupt generation
7390
        // mask interrupts
7391
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7392
        // detect carrier sense in FD and no collision
7393
        eth_phy.carrier_sense_tx_fd_detect(1);
7394
        eth_phy.collision(0);
7395
      end
7396
      2'h2: // Interrupt is not generated
7397
      begin
7398
        // set_tx_bd disable the interrupt generation
7399
        // unmask interrupts
7400
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
7401
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7402
        // not detect carrier sense in FD and set collision
7403
        eth_phy.carrier_sense_tx_fd_detect(0);
7404
        eth_phy.collision(1);
7405
      end
7406
      default: // 2'h3: // Interrupt is not generated
7407
      begin
7408
        // set_tx_bd disable the interrupt generation
7409
        // mask interrupts
7410
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7411
        // detect carrier sense in FD and set collision
7412
        eth_phy.carrier_sense_tx_fd_detect(1);
7413
        eth_phy.collision(1);
7414
      end
7415
      endcase
7416
      #1;
7417
      // first destination address on ethernet PHY
7418
      eth_phy.set_tx_mem_addr(num_of_frames * 16);
7419
      // SET packets and wrap bit
7420
      // num_of_frames <= 9 => wrap set to TX BD 0
7421
      if (num_of_frames <= 9)
7422
      begin
7423
        tmp_len = i_length; // length of frame
7424
        tmp_bd_num = 0; // TX BD number
7425
        // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7426
        // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7427
        if (tmp_len[0] == 0)
7428
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7429
        else
7430
          set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7431
        // set wrap bit
7432
        set_tx_bd_wrap(0);
7433
      end
7434
      // 10 <= num_of_frames < 18 => wrap set to TX BD 3
7435
      else if ((num_of_frames == 10) || (num_of_frames == 14))
7436
      begin
7437
        tmp_len = i_length; // length of frame
7438
        tmp_bd_num = 0; // TX BD number
7439
        while (tmp_bd_num < 4) //
7440
        begin
7441
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7442
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7443
          if (tmp_len[0] == 0)
7444
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7445
          else
7446
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7447
          tmp_len = tmp_len + 1;
7448
          // set TX BD number
7449
          tmp_bd_num = tmp_bd_num + 1;
7450
        end
7451
        // set wrap bit
7452
        set_tx_bd_wrap(3);
7453
      end
7454
      // 18 <= num_of_frames < 28 => wrap set to TX BD 4
7455
      else if ((num_of_frames == 18) || (num_of_frames == 23))
7456
      begin
7457
        tmp_len = i_length; // length of frame
7458
        tmp_bd_num = 0; // TX BD number
7459
        while (tmp_bd_num < 5) //
7460
        begin
7461
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7462
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7463
          if (tmp_len[0] == 0)
7464
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7465
          else
7466
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7467
          tmp_len = tmp_len + 1;
7468
          // set TX BD number
7469
          tmp_bd_num = tmp_bd_num + 1;
7470
        end
7471
        // set wrap bit
7472
        set_tx_bd_wrap(4);
7473
      end
7474
      // 28 <= num_of_frames < 40 => wrap set to TX BD 5
7475
      else if ((num_of_frames == 28) || (num_of_frames == 34))
7476
      begin
7477
        tmp_len = i_length; // length of frame
7478
        tmp_bd_num = 0; // TX BD number
7479
        while (tmp_bd_num < 6) //
7480
        begin
7481
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7482
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7483
          if (tmp_len[0] == 0)
7484
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7485
          else
7486
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7487
          tmp_len = tmp_len + 1;
7488
          // set TX BD number
7489
          tmp_bd_num = tmp_bd_num + 1;
7490
        end
7491
        // set wrap bit
7492
        set_tx_bd_wrap(5);
7493
      end
7494
      // 40 <= num_of_frames < 54 => wrap set to TX BD 6
7495
      else if ((num_of_frames == 40) || (num_of_frames == 47))
7496
      begin
7497
        tmp_len = i_length; // length of frame
7498
        tmp_bd_num = 0; // TX BD number
7499
        while (tmp_bd_num < 7) //
7500
        begin
7501
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7502
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7503
          if (tmp_len[0] == 0)
7504
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7505
          else
7506
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7507
          tmp_len = tmp_len + 1;
7508
          // set TX BD number
7509
          tmp_bd_num = tmp_bd_num + 1;
7510
        end
7511
        // set wrap bit
7512
        set_tx_bd_wrap(6);
7513
      end
7514
      // 54 <= num_of_frames < 70 => wrap set to TX BD 7
7515
      else if ((num_of_frames == 54) || (num_of_frames == 62))
7516
      begin
7517
        tmp_len = i_length; // length of frame
7518
        tmp_bd_num = 0; // TX BD number
7519
        while (tmp_bd_num < 8) //
7520
        begin
7521
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7522
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7523
          if (tmp_len[0] == 0)
7524
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7525
          else
7526
            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + max_tmp));
7527
          tmp_len = tmp_len + 1;
7528
          // set TX BD number
7529
          tmp_bd_num = tmp_bd_num + 1;
7530
        end
7531
        // set wrap bit
7532
        set_tx_bd_wrap(7);
7533
      end
7534
      #1;
7535
      // SET ready bit
7536
      if (num_of_frames < 10)
7537
        set_tx_bd_ready(0, 0);
7538
      else if (num_of_frames < 14)
7539
        set_tx_bd_ready((num_of_frames - 10), (num_of_frames - 10));
7540
      else if (num_of_frames < 18)
7541
        set_tx_bd_ready((num_of_frames - 14), (num_of_frames - 14));
7542
      else if (num_of_frames < 23)
7543
        set_tx_bd_ready((num_of_frames - 18), (num_of_frames - 18));
7544
      else if (num_of_frames < 28)
7545
        set_tx_bd_ready((num_of_frames - 23), (num_of_frames - 23));
7546
      else if (num_of_frames < 34)
7547
        set_tx_bd_ready((num_of_frames - 28), (num_of_frames - 28));
7548
      else if (num_of_frames < 40)
7549
        set_tx_bd_ready((num_of_frames - 34), (num_of_frames - 34));
7550
      else if (num_of_frames < 47)
7551
        set_tx_bd_ready((num_of_frames - 40), (num_of_frames - 40));
7552
      else if (num_of_frames < 54)
7553
        set_tx_bd_ready((num_of_frames - 47), (num_of_frames - 47));
7554
      else if (num_of_frames < 62)
7555
        set_tx_bd_ready((num_of_frames - 54), (num_of_frames - 54));
7556
      else if (num_of_frames < 70)
7557
        set_tx_bd_ready((num_of_frames - 62), (num_of_frames - 62));
7558
      // CHECK END OF TRANSMITION
7559
      frame_started = 0;
7560
      if (num_of_frames >= 5)
7561
        #1 check_tx_bd(num_of_bd, data);
7562
      fork
7563
      begin: fr_st3
7564
        wait (MTxEn === 1'b1); // start transmit
7565
        frame_started = 1;
7566
      end
7567
      begin
7568
        repeat (30) @(posedge mtx_clk);
7569
        if (num_of_frames < 5)
7570
        begin
7571
          if (frame_started == 1)
7572
          begin
7573
            `TIME; $display("*E Frame should NOT start!");
7574
          end
7575
          disable fr_st3;
7576
        end
7577
        else
7578
        begin
7579
          if (frame_started == 0)
7580
          begin
7581
            `TIME; $display("*W Frame should start!");
7582
            disable fr_st3;
7583
          end
7584
        end
7585
      end
7586
      join
7587
      // check packets larger than 4 bytes
7588
      if (num_of_frames >= 5)
7589
      begin
7590
        wait (MTxEn === 1'b0); // end transmit
7591
        while (data[15] === 1)
7592
        begin
7593
          #1 check_tx_bd(num_of_bd, data);
7594
          @(posedge wb_clk);
7595
        end
7596
        repeat (1) @(posedge wb_clk);
7597
        // check length of a PACKET
7598
        if (eth_phy.tx_len != (i_length + 4))
7599
        begin
7600
          `TIME; $display("*E Wrong length of the packet out from MAC");
7601
          test_fail("Wrong length of the packet out from MAC");
7602
          fail = fail + 1;
7603
        end
7604
        // check transmitted TX packet data
7605
        if (i_length[0] == 0)
7606
        begin
7607
          #1 check_tx_packet(`MEMORY_BASE, (num_of_frames * 16), i_length, tmp);
7608
        end
7609
        else
7610
        begin
7611
          #1 check_tx_packet((`MEMORY_BASE + max_tmp), (num_of_frames * 16), i_length, tmp);
7612
        end
7613
        if (tmp > 0)
7614
        begin
7615
          test_fail("Wrong data of the transmitted packet");
7616
          fail = fail + 1;
7617
        end
7618
        // check transmited TX packet CRC
7619
        #1 check_tx_crc((num_of_frames * 16), (eth_phy.tx_len - 4), 1'b0, tmp); // length without CRC
7620
        if (tmp > 0)
7621
        begin
7622
          test_fail("Wrong CRC of the transmitted packet");
7623
          fail = fail + 1;
7624
        end
7625
      end
7626
      // check WB INT signal
7627
      if ((i_length[1:0] == 2'h0) && (num_of_frames >= 5))
7628
      begin
7629
        if (wb_int !== 1'b1)
7630
        begin
7631
          `TIME; $display("*E WB INT signal should be set");
7632
          test_fail("WB INT signal should be set");
7633
          fail = fail + 1;
7634
        end
7635
      end
7636
      else
7637
      begin
7638
        if (wb_int !== 1'b0)
7639
        begin
7640
          `TIME; $display("*E WB INT signal should not be set");
7641
          test_fail("WB INT signal should not be set");
7642
          fail = fail + 1;
7643
        end
7644
      end
7645
      // check TX buffer descriptor of a packet
7646
      check_tx_bd(num_of_bd, data);
7647
      if (num_of_frames >= 5)
7648
      begin
7649
        if (i_length[1] == 1'b0) // interrupt enabled
7650
        begin
7651
          if ( (data[15:0] !== 16'h7800) && // wrap bit
7652
               (data[15:0] !== 16'h5800) ) // without wrap bit
7653
          begin
7654
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7655
            test_fail("TX buffer descriptor status is not correct");
7656
            fail = fail + 1;
7657
          end
7658
        end
7659
        else // interrupt not enabled
7660
        begin
7661
          if ( (data[15:0] !== 16'h3800) && // wrap bit
7662
               (data[15:0] !== 16'h1800) ) // without wrap bit
7663
          begin
7664
            `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7665
            test_fail("TX buffer descriptor status is not correct");
7666
            fail = fail + 1;
7667
          end
7668
        end
7669
      end
7670
      else
7671
      begin
7672
        if (data[15] !== 1'b1)
7673
        begin
7674
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7675
          test_fail("TX buffer descriptor status is not correct");
7676
          fail = fail + 1;
7677
        end
7678
      end
7679
      // clear TX BD with wrap bit
7680
      if (num_of_frames == 63)
7681
        clear_tx_bd(16, 16);
7682
      // check interrupts
7683
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7684
      if ( ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1)) && (num_of_frames >= 5) )
7685
      begin
7686
        if ((data & `ETH_INT_TXB) !== 1'b1)
7687
        begin
7688
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
7689
          test_fail("Interrupt Transmit Buffer was not set");
7690
          fail = fail + 1;
7691
        end
7692
        if ((data & (~`ETH_INT_TXB)) !== 0)
7693
        begin
7694
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
7695
          test_fail("Other interrupts (except Transmit Buffer) were set");
7696
          fail = fail + 1;
7697
        end
7698
      end
7699
      else
7700
      begin
7701
        if (data !== 0)
7702
        begin
7703
          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
7704
          test_fail("Any of interrupts (except Transmit Buffer) was set");
7705
          fail = fail + 1;
7706
        end
7707
      end
7708
      // clear interrupts
7709
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7710
      // check WB INT signal
7711
      if (wb_int !== 1'b0)
7712
      begin
7713
        test_fail("WB INT signal should not be set");
7714
        fail = fail + 1;
7715
      end
7716
      // INTERMEDIATE DISPLAYS
7717
      if (i_length == 3)
7718
      begin
7719
        $display("    pads appending to packets is selected");
7720
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
7721
        $display("    ->packets with lengths from %0d to %0d are not transmitted (length increasing by 1 byte)",
7722
                 0, 3);
7723
      end
7724
      else if (i_length == 9)
7725
      begin
7726
        $display("    using 1 BD out of 8 BDs assigned to TX (wrap at 1st BD - TX BD 0)");
7727
        $display("    ->packet with length 4 is not transmitted (length increasing by 1 byte)");
7728
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7729
                 5, 9);
7730
      end
7731
      else if (i_length == 17)
7732
      begin
7733
        $display("    using 4 BDs out of 8 BDs assigned to TX (wrap at 4th BD - TX BD 3)");
7734
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7735
                 10, 17);
7736
      end
7737
      else if (i_length == 27)
7738
      begin
7739
        $display("    using 5 BDs out of 8 BDs assigned to TX (wrap at 5th BD - TX BD 4)");
7740
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7741
                 18, 27);
7742
      end
7743
      else if (i_length == 40)
7744
      begin
7745
        $display("    using 6 BDs out of 8 BDs assigned to TX (wrap at 6th BD - TX BD 5)");
7746
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7747
                 28, 40);
7748
      end
7749
      else if (i_length == 54)
7750
      begin
7751
        $display("    using 7 BDs out of 8 BDs assigned to TX (wrap at 7th BD - TX BD 6)");
7752
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7753
                 41, 54);
7754
      end
7755
      else if (i_length == 69)
7756
      begin
7757
        $display("    using 8 BDs out of 8 BDs assigned to TX (wrap at 8th BD - TX BD 7)");
7758
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
7759
                 55, 69);
7760
      end
7761
      // set length (loop variable)
7762
      i_length = i_length + 1;
7763
      // the number of frame transmitted
7764
      num_of_frames = num_of_frames + 1;
7765
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
7766
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
7767
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
7768
          (num_of_frames == 54) || (num_of_frames == 62))
7769
        num_of_bd = 0;
7770
      else
7771
        num_of_bd = num_of_bd + 1;
7772
    end
7773
    // disable TX
7774
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
7775
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7776
    @(posedge wb_clk);
7777
    if(fail == 0)
7778
      test_ok;
7779
    else
7780
      fail = 0;
7781
  end
7782
 
7783
 
7784
  ////////////////////////////////////////////////////////////////////
7785
  ////                                                            ////
7786
  ////  Test transmit packets across MAXFL value at               ////
7787
  ////  13 TX buffer decriptors ( 10Mbps ).                       ////
7788
  ////                                                            ////
7789
  ////////////////////////////////////////////////////////////////////
7790
  if (test_num == 10) // without and with padding
7791
  begin
7792
    // TEST 10: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 10Mbps )
7793
    test_name = "TEST 10: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 10Mbps )";
7794
    `TIME; $display("  TEST 10: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 10Mbps )");
7795
 
7796
    // reset MAC registers
7797
    hard_reset;
7798
    // reset MAC and MII LOGIC with soft reset
7799
    reset_mac;
7800
    reset_mii;
7801
    // set wb slave response
7802
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
7803
 
7804
    max_tmp = 0;
7805
    min_tmp = 0;
7806
    num_of_frames = 0;
7807
    num_of_bd = 0;
7808
    // set 13 TX buffer descriptors - must be set before TX enable
7809
    wbm_write(`ETH_TX_BD_NUM, 32'hD, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7810
    // enable TX, set full-duplex mode, NO padding and CRC appending
7811
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
7812
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7813
    // prepare a packet of MAXFL + 10 length
7814
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7815
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
7816
    min_tmp = tmp[31:16];
7817
    st_data = 8'hA3;
7818
    set_tx_packet(`MEMORY_BASE, (max_tmp + 10), st_data); // length without CRC
7819
    // check WB INT signal
7820
    if (wb_int !== 1'b0)
7821
    begin
7822
      test_fail("WB INT signal should not be set");
7823
      fail = fail + 1;
7824
    end
7825
 
7826
    // write to phy's control register for 10Mbps
7827
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
7828
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
7829
    speed = 10;
7830
 
7831
    i_length = (max_tmp - 5);
7832
    while (i_length <= (max_tmp - 3)) // (max_tmp - 4) is the limit
7833
    begin
7834
$display("   i_length = %0d", i_length);
7835
      // choose generating carrier sense and collision
7836
//      case (i_length[1:0])
7837
//      2'h0: // Interrupt is generated
7838
//      begin
7839
        // Reset_tx_bd nable interrupt generation
7840
        // unmask interrupts
7841
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
7842
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7843
        // not detect carrier sense in FD and no collision
7844
        eth_phy.carrier_sense_tx_fd_detect(0);
7845
        eth_phy.collision(0);
7846
//      end
7847
//      2'h1: // Interrupt is not generated
7848
//      begin
7849
        // set_tx_bd enable interrupt generation
7850
        // mask interrupts
7851
//        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7852
        // detect carrier sense in FD and no collision
7853
//        eth_phy.carrier_sense_tx_fd_detect(1);
7854
//        eth_phy.collision(0);
7855
//      end
7856
//      2'h2: // Interrupt is not generated
7857
//      begin
7858
        // set_tx_bd disable the interrupt generation
7859
        // unmask interrupts
7860
//        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
7861
//                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7862
        // not detect carrier sense in FD and set collision
7863
//        eth_phy.carrier_sense_tx_fd_detect(0);
7864
//        eth_phy.collision(1);
7865
//      end
7866
//      default: // 2'h3: // Interrupt is not generated
7867
//      begin
7868
        // set_tx_bd disable the interrupt generation
7869
        // mask interrupts
7870
//        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
7871
        // detect carrier sense in FD and set collision
7872
//        eth_phy.carrier_sense_tx_fd_detect(1);
7873
//        eth_phy.collision(1);
7874
//      end
7875
//      endcase
7876
      // first destination address on ethernet PHY
7877
      eth_phy.set_tx_mem_addr(0);
7878
      // 
7879
if (num_of_bd == 0)
7880
begin
7881
set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
7882
set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
7883
set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
7884
set_tx_bd_wrap(2);
7885
set_tx_bd_ready(0, 0);
7886
end
7887
else if (num_of_bd == 1)
7888
set_tx_bd_ready(1, 1);
7889
else if (num_of_bd == 2)
7890
set_tx_bd_ready(2, 2);
7891
 
7892
 
7893
//        tmp_len = i_length; // length of frame
7894
//        tmp_bd_num = 0; // TX BD number
7895
//        while (tmp_bd_num < 8) // 
7896
//        begin
7897
//          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
7898
//          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
7899
//          if (tmp_len[0] == 0)
7900
//            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, `MEMORY_BASE);
7901
//          else
7902
//            set_tx_bd(tmp_bd_num, tmp_bd_num, tmp_len, !tmp_len[1], 1'b1, 1'b1, (`MEMORY_BASE + 2*max_tmp));
7903
//          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
7904
//          tmp_len = tmp_len + 1;
7905
//          // set TX BD number
7906
//          tmp_bd_num = tmp_bd_num + 1;
7907
//        end
7908
//        // set wrap bit
7909
//        set_tx_bd_wrap(7);
7910
//      // set ready bit
7911
//      set_tx_bd_ready((i_length - (max_tmp - 8)), (i_length - (max_tmp - 8)));
7912
      // CHECK END OF TRANSMITION
7913
check_tx_bd(num_of_bd, data);
7914
//      #1 check_tx_bd((i_length - (max_tmp - 8)), data);
7915
        wait (MTxEn === 1'b1); // start transmit
7916
check_tx_bd(num_of_bd, data);
7917
//        #1 check_tx_bd((i_length - (max_tmp - 8)), data);
7918
        if (data[15] !== 1)
7919
        begin
7920
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
7921
          fail = fail + 1;
7922
        end
7923
        wait (MTxEn === 1'b0); // end transmit
7924
        while (data[15] === 1)
7925
        begin
7926
check_tx_bd(num_of_bd, data);
7927
//          #1 check_tx_bd((i_length - (max_tmp - 8)), data);
7928
          @(posedge wb_clk);
7929
        end
7930
        repeat (1) @(posedge wb_clk);
7931
      // check length of a PACKET
7932
$display("   eth_phy length = %0d", eth_phy.tx_len);
7933
tmp_len = eth_phy.tx_len;
7934
#1;
7935
if (tmp_len != (i_length + 4))
7936
//      if (eth_phy.tx_len != (i_length + 4))
7937
      begin
7938
        test_fail("Wrong length of the packet out from MAC");
7939
        fail = fail + 1;
7940
      end
7941
      // checking in the following if statement is performed only for first and last 64 lengths
7942
//      if ( ((i_length + 4) <= (min_tmp + 64)) || ((i_length + 4) > (max_tmp - 64)) )
7943
//      begin
7944
        // check transmitted TX packet data
7945
//        if (i_length[0] == 0)
7946
//        begin
7947
          check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
7948
//        end
7949
//        else
7950
//        begin
7951
//          check_tx_packet((`MEMORY_BASE + 2*max_tmp), 0, i_length, tmp);
7952
//        end
7953
        if (tmp > 0)
7954
        begin
7955
          test_fail("Wrong data of the transmitted packet");
7956
          fail = fail + 1;
7957
        end
7958
        // check transmited TX packet CRC
7959
//        if (i_length[0] == 0)
7960
          check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
7961
//        else
7962
//          check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
7963
        if (tmp > 0)
7964
        begin
7965
          test_fail("Wrong CRC of the transmitted packet");
7966
          fail = fail + 1;
7967
        end
7968
//      end
7969
      // check WB INT signal
7970
//      if (i_length[1:0] == 2'h0)
7971
//      begin
7972
        if (wb_int !== 1'b1)
7973
        begin
7974
          `TIME; $display("*E WB INT signal should be set");
7975
          test_fail("WB INT signal should be set");
7976
          fail = fail + 1;
7977
        end
7978
//      end
7979
//      else
7980
//      begin
7981
//        if (wb_int !== 1'b0)
7982
//        begin
7983
//          `TIME; $display("*E WB INT signal should not be set");
7984
//          test_fail("WB INT signal should not be set");
7985
//          fail = fail + 1;
7986
//        end
7987
//      end
7988
//      // check TX buffer descriptor of a packet
7989
//      check_tx_bd((i_length - (max_tmp - 8)), data);
7990
check_tx_bd(num_of_bd, data);
7991
if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
7992
     ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
7993
//      if (i_length[1] == 1'b0) // interrupt enabled
7994
//      begin
7995
//        if ( ((data[15:0] !== 16'h7800) && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
7996
//             ((data[15:0] !== 16'h5800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
7997
        begin
7998
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
7999
          test_fail("TX buffer descriptor status is not correct");
8000
          fail = fail + 1;
8001
        end
8002
//      end
8003
//      else // interrupt not enabled
8004
//      begin
8005
//        if ( ((data[15:0] !== 16'h3800)  && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
8006
//             ((data[15:0] !== 16'h1800) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
8007
//        begin
8008
//          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8009
//          test_fail("TX buffer descriptor status is not correct");
8010
//          fail = fail + 1;
8011
//        end
8012
//      end
8013
//      // clear first half of 8 frames from TX buffer descriptor 0
8014
//      if (num_of_frames < 4)
8015
//        clear_tx_bd((i_length - (max_tmp - 8)), (i_length - (max_tmp - 8)));
8016
      // check interrupts
8017
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8018
//      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
8019
//      begin
8020
        if ((data & `ETH_INT_TXB) !== 1'b1)
8021
        begin
8022
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8023
          test_fail("Interrupt Transmit Buffer was not set");
8024
          fail = fail + 1;
8025
        end
8026
        if ((data & (~`ETH_INT_TXB)) !== 0)
8027
        begin
8028
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8029
          test_fail("Other interrupts (except Transmit Buffer) were set");
8030
          fail = fail + 1;
8031
        end
8032
//      end
8033
//      else
8034
//      begin
8035
//        if (data !== 0)
8036
//        begin
8037
//          `TIME; $display("*E Any of interrupts (except Transmit Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
8038
//          test_fail("Any of interrupts (except Transmit Buffer) was set");
8039
//          fail = fail + 1;
8040
//        end
8041
//      end
8042
      // clear interrupts
8043
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8044
      // check WB INT signal
8045
      if (wb_int !== 1'b0)
8046
      begin
8047
        test_fail("WB INT signal should not be set");
8048
        fail = fail + 1;
8049
      end
8050
      // INTERMEDIATE DISPLAYS
8051
if (num_of_bd == 0)
8052
  $display("    ->packet with length %0d sent", (i_length + 4));
8053
else if (num_of_bd == 1)
8054
  $display("    ->packet with length %0d sent", (i_length + 4));
8055
else if (num_of_bd == 2)
8056
  $display("    ->packet with length %0d sent", (i_length + 4));
8057
      // set length (loop variable)
8058
      i_length = i_length + 1;
8059
      // the number of frame transmitted
8060
      num_of_frames = num_of_frames + 1;
8061
      num_of_bd = num_of_bd + 1;
8062
      @(posedge wb_clk);
8063
    end
8064
    // disable TX
8065
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8066
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8067
    @(posedge wb_clk);
8068
    if(fail == 0)
8069
      test_ok;
8070
    else
8071
      fail = 0;
8072
  end
8073
 
8074
 
8075
  ////////////////////////////////////////////////////////////////////
8076
  ////                                                            ////
8077
  ////  Test transmit packets across MAXFL value at               ////
8078
  ////  13 TX buffer decriptors ( 100Mbps ).                      ////
8079
  ////                                                            ////
8080
  ////////////////////////////////////////////////////////////////////
8081
  if (test_num == 11) // without and with padding
8082
  begin
8083
    // TEST 11: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 100Mbps )
8084
    test_name = "TEST 11: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 100Mbps )";
8085
    `TIME; $display("  TEST 11: TRANSMIT PACKETS ACROSS MAXFL VALUE AT 13 TX BDs ( 100Mbps )");
8086
 
8087
    // reset MAC registers
8088
    hard_reset;
8089
    // reset MAC and MII LOGIC with soft reset
8090
    reset_mac;
8091
    reset_mii;
8092
    // set wb slave response
8093
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
8094
 
8095
    max_tmp = 0;
8096
    min_tmp = 0;
8097
    num_of_frames = 0;
8098
    num_of_bd = 0;
8099
    // set 13 TX buffer descriptors - must be set before TX enable
8100
    wbm_write(`ETH_TX_BD_NUM, 32'hD, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8101
    // enable TX, set full-duplex mode, NO padding and CRC appending
8102
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
8103
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8104
    // prepare a packet of MAXFL + 10 length
8105
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8106
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
8107
    min_tmp = tmp[31:16];
8108
    st_data = 8'hA3;
8109
    set_tx_packet(`MEMORY_BASE, (max_tmp + 10), st_data); // length without CRC
8110
    // check WB INT signal
8111
    if (wb_int !== 1'b0)
8112
    begin
8113
      test_fail("WB INT signal should not be set");
8114
      fail = fail + 1;
8115
    end
8116
 
8117
    // write to phy's control register for 100Mbps
8118
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
8119
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
8120
    speed = 100;
8121
 
8122
    i_length = (max_tmp - 5);
8123
    while (i_length <= (max_tmp - 3)) // (max_tmp - 4) is the limit
8124
    begin
8125
      $display("   i_length = %0d", i_length);
8126
      // Reset_tx_bd nable interrupt generation
8127
      // unmask interrupts
8128
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
8129
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8130
      // not detect carrier sense in FD and no collision
8131
      eth_phy.carrier_sense_tx_fd_detect(0);
8132
      eth_phy.collision(0);
8133
      // first destination address on ethernet PHY
8134
      eth_phy.set_tx_mem_addr(0);
8135
      // prepare BDs
8136
      if (num_of_bd == 0)
8137
      begin
8138
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8139
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8140
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8141
        set_tx_bd_wrap(2);
8142
        set_tx_bd_ready(0, 0);
8143
      end
8144
      else if (num_of_bd == 1)
8145
        set_tx_bd_ready(1, 1);
8146
      else if (num_of_bd == 2)
8147
        set_tx_bd_ready(2, 2);
8148
      // CHECK END OF TRANSMITION
8149
      check_tx_bd(num_of_bd, data);
8150
        wait (MTxEn === 1'b1); // start transmit
8151
      check_tx_bd(num_of_bd, data);
8152
        if (data[15] !== 1)
8153
        begin
8154
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
8155
          fail = fail + 1;
8156
        end
8157
        wait (MTxEn === 1'b0); // end transmit
8158
        while (data[15] === 1)
8159
        begin
8160
      check_tx_bd(num_of_bd, data);
8161
          @(posedge wb_clk);
8162
        end
8163
        repeat (1) @(posedge wb_clk);
8164
      // check length of a PACKET
8165
      $display("   eth_phy length = %0d", eth_phy.tx_len);
8166
      tmp_len = eth_phy.tx_len;
8167
      #1;
8168
      if (tmp_len != (i_length + 4))
8169
      begin
8170
        test_fail("Wrong length of the packet out from MAC");
8171
        fail = fail + 1;
8172
      end
8173
      // checking packet
8174
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
8175
      if (tmp > 0)
8176
      begin
8177
        test_fail("Wrong data of the transmitted packet");
8178
        fail = fail + 1;
8179
      end
8180
      // check transmited TX packet CRC
8181
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
8182
      if (tmp > 0)
8183
      begin
8184
        test_fail("Wrong CRC of the transmitted packet");
8185
        fail = fail + 1;
8186
      end
8187
      // check WB INT signal
8188
      if (wb_int !== 1'b1)
8189
      begin
8190
        `TIME; $display("*E WB INT signal should be set");
8191
        test_fail("WB INT signal should be set");
8192
        fail = fail + 1;
8193
      end
8194
      // check TX buffer descriptor of a packet
8195
      check_tx_bd(num_of_bd, data);
8196
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
8197
           ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
8198
      begin
8199
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8200
        test_fail("TX buffer descriptor status is not correct");
8201
        fail = fail + 1;
8202
      end
8203
      // check interrupts
8204
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8205
      if ((data & `ETH_INT_TXB) !== 1'b1)
8206
      begin
8207
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8208
        test_fail("Interrupt Transmit Buffer was not set");
8209
        fail = fail + 1;
8210
      end
8211
      if ((data & (~`ETH_INT_TXB)) !== 0)
8212
      begin
8213
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8214
        test_fail("Other interrupts (except Transmit Buffer) were set");
8215
        fail = fail + 1;
8216
      end
8217
      // clear interrupts
8218
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8219
      // check WB INT signal
8220
      if (wb_int !== 1'b0)
8221
      begin
8222
        test_fail("WB INT signal should not be set");
8223
        fail = fail + 1;
8224
      end
8225
      // INTERMEDIATE DISPLAYS
8226
      if (num_of_bd == 0)
8227
        $display("    ->packet with length %0d sent", (i_length + 4));
8228
      else if (num_of_bd == 1)
8229
        $display("    ->packet with length %0d sent", (i_length + 4));
8230
      else if (num_of_bd == 2)
8231
        $display("    ->packet with length %0d sent", (i_length + 4));
8232
      // set length (loop variable)
8233
      i_length = i_length + 1;
8234
      // the number of frame transmitted
8235
      num_of_frames = num_of_frames + 1;
8236
      num_of_bd = num_of_bd + 1;
8237
      @(posedge wb_clk);
8238
    end
8239
    // disable TX
8240
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8241
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8242
    @(posedge wb_clk);
8243
    if(fail == 0)
8244
      test_ok;
8245
    else
8246
      fail = 0;
8247
  end
8248
 
8249
 
8250
  ////////////////////////////////////////////////////////////////////
8251
  ////                                                            ////
8252
  ////  Test transmit packets across changed MAXFL value at       ////
8253
  ////  47 TX buffer decriptors ( 10Mbps ).                       ////
8254
  ////                                                            ////
8255
  ////////////////////////////////////////////////////////////////////
8256
  if (test_num == 12) // without and with padding
8257
  begin
8258
    // TEST 12: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 10Mbps )
8259
    test_name = "TEST 12: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 10Mbps )";
8260
    `TIME; $display("  TEST 12: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 10Mbps )");
8261
 
8262
    // reset MAC registers
8263
    hard_reset;
8264
    // reset MAC and MII LOGIC with soft reset
8265
    reset_mac;
8266
    reset_mii;
8267
    // set wb slave response
8268
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
8269
 
8270
    max_tmp = 0;
8271
    min_tmp = 0;
8272
    num_of_frames = 0;
8273
    num_of_bd = 0;
8274
    // set 47 TX buffer descriptors - must be set before TX enable
8275
    wbm_write(`ETH_TX_BD_NUM, 32'h2F, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8276
    // prepare a packet of MAXFL + 10 length
8277
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8278
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
8279
    min_tmp = tmp[31:16];
8280
    // change MAXFL value
8281
    max_tmp = min_tmp + 53;
8282
    wbm_write(`ETH_PACKETLEN, {min_tmp, max_tmp}, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8283
    st_data = 8'h62;
8284
    set_tx_packet(`MEMORY_BASE, max_tmp, st_data); // length with CRC
8285
    append_tx_crc(`MEMORY_BASE, (max_tmp - 5), 1'b0); // for first packet
8286
    // enable TX, set full-duplex mode, NO padding and NO CRC appending
8287
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD,
8288
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8289
    // check WB INT signal
8290
    if (wb_int !== 1'b0)
8291
    begin
8292
      test_fail("WB INT signal should not be set");
8293
      fail = fail + 1;
8294
    end
8295
 
8296
    // write to phy's control register for 10Mbps
8297
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
8298
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
8299
    speed = 10;
8300
 
8301
    i_length = (max_tmp - 5); // (max_tmp - 1); // not (max_tmp - 5) because NO automatic CRC appending
8302
    while (i_length <= (max_tmp - 3)) // (max_tmp + 1)) // (max_tmp) is the limit
8303
    begin
8304
      $display("   i_length = %0d", i_length);
8305
      // prepare packet's CRC
8306
      if (num_of_bd == 1)
8307
        append_tx_crc(`MEMORY_BASE, (max_tmp - 4), 1'b0); // for second and third packets
8308
      // Reset_tx_bd nable interrupt generation
8309
      // unmask interrupts
8310
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
8311
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8312
      // not detect carrier sense in FD and no collision
8313
      eth_phy.carrier_sense_tx_fd_detect(0);
8314
      eth_phy.collision(0);
8315
      // first destination address on ethernet PHY
8316
      eth_phy.set_tx_mem_addr(0);
8317
      // prepare BDs
8318
      if (num_of_bd == 0)
8319
      begin
8320
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8321
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8322
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8323
        set_tx_bd_wrap(2);
8324
        set_tx_bd_ready(0, 0);
8325
      end
8326
      else if (num_of_bd == 1)
8327
        set_tx_bd_ready(1, 1);
8328
      else if (num_of_bd == 2)
8329
        set_tx_bd_ready(2, 2);
8330
      // CHECK END OF TRANSMITION
8331
      check_tx_bd(num_of_bd, data);
8332
        wait (MTxEn === 1'b1); // start transmit
8333
      check_tx_bd(num_of_bd, data);
8334
        if (data[15] !== 1)
8335
        begin
8336
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
8337
          fail = fail + 1;
8338
        end
8339
        wait (MTxEn === 1'b0); // end transmit
8340
        while (data[15] === 1)
8341
        begin
8342
      check_tx_bd(num_of_bd, data);
8343
          @(posedge wb_clk);
8344
        end
8345
        repeat (1) @(posedge wb_clk);
8346
      // check length of a PACKET
8347
      $display("   eth_phy length = %0d", eth_phy.tx_len);
8348
      tmp_len = eth_phy.tx_len;
8349
      #1;
8350
      if (tmp_len != (i_length + 4))
8351
      begin
8352
        test_fail("Wrong length of the packet out from MAC");
8353
        fail = fail + 1;
8354
      end
8355
      // checking packet
8356
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
8357
      if (tmp > 0)
8358
      begin
8359
        test_fail("Wrong data of the transmitted packet");
8360
        fail = fail + 1;
8361
      end
8362
      // check transmited TX packet CRC
8363
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
8364
      if (tmp > 0)
8365
      begin
8366
        test_fail("Wrong CRC of the transmitted packet");
8367
        fail = fail + 1;
8368
      end
8369
      // check WB INT signal
8370
      if (wb_int !== 1'b1)
8371
      begin
8372
        `TIME; $display("*E WB INT signal should be set");
8373
        test_fail("WB INT signal should be set");
8374
        fail = fail + 1;
8375
      end
8376
      // check TX buffer descriptor of a packet
8377
      check_tx_bd(num_of_bd, data);
8378
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
8379
           ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
8380
      begin
8381
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8382
        test_fail("TX buffer descriptor status is not correct");
8383
        fail = fail + 1;
8384
      end
8385
      // check interrupts
8386
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8387
      if ((data & `ETH_INT_TXB) !== 1'b1)
8388
      begin
8389
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8390
        test_fail("Interrupt Transmit Buffer was not set");
8391
        fail = fail + 1;
8392
      end
8393
      if ((data & (~`ETH_INT_TXB)) !== 0)
8394
      begin
8395
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8396
        test_fail("Other interrupts (except Transmit Buffer) were set");
8397
        fail = fail + 1;
8398
      end
8399
      // clear interrupts
8400
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8401
      // check WB INT signal
8402
      if (wb_int !== 1'b0)
8403
      begin
8404
        test_fail("WB INT signal should not be set");
8405
        fail = fail + 1;
8406
      end
8407
      // INTERMEDIATE DISPLAYS
8408
      if (num_of_bd == 0)
8409
        $display("    ->packet with length %0d sent", (i_length + 4));
8410
      else if (num_of_bd == 1)
8411
        $display("    ->packet with length %0d sent", (i_length + 4));
8412
      else if (num_of_bd == 2)
8413
        $display("    ->packet with length %0d sent", (i_length + 4));
8414
      // set length (loop variable)
8415
      i_length = i_length + 1;
8416
      // the number of frame transmitted
8417
      num_of_frames = num_of_frames + 1;
8418
      num_of_bd = num_of_bd + 1;
8419
      @(posedge wb_clk);
8420
    end
8421
    // disable TX
8422
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8423
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8424
    @(posedge wb_clk);
8425
    if(fail == 0)
8426
      test_ok;
8427
    else
8428
      fail = 0;
8429
  end
8430
 
8431
 
8432
  ////////////////////////////////////////////////////////////////////
8433
  ////                                                            ////
8434
  ////  Test transmit packets across changed MAXFL value at       ////
8435
  ////  47 TX buffer decriptors ( 100Mbps ).                      ////
8436
  ////                                                            ////
8437
  ////////////////////////////////////////////////////////////////////
8438
  if (test_num == 13) // without and with padding
8439
  begin
8440
    // TEST 13: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 100Mbps )
8441
    test_name = "TEST 13: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 100Mbps )";
8442
    `TIME; $display("  TEST 13: TRANSMIT PACKETS ACROSS CHANGED MAXFL VALUE AT 13 TX BDs ( 100Mbps )");
8443
 
8444
    // reset MAC registers
8445
    hard_reset;
8446
    // reset MAC and MII LOGIC with soft reset
8447
    reset_mac;
8448
    reset_mii;
8449
    // set wb slave response
8450
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
8451
 
8452
    max_tmp = 0;
8453
    min_tmp = 0;
8454
    num_of_frames = 0;
8455
    num_of_bd = 0;
8456
    // set 47 TX buffer descriptors - must be set before TX enable
8457
    wbm_write(`ETH_TX_BD_NUM, 32'h2F, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8458
    // prepare a packet of MAXFL + 10 length
8459
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8460
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
8461
    min_tmp = tmp[31:16];
8462
    // change MAXFL value
8463
    max_tmp = min_tmp + 53;
8464
    wbm_write(`ETH_PACKETLEN, {min_tmp, max_tmp}, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8465
    st_data = 8'h62;
8466
    set_tx_packet(`MEMORY_BASE, max_tmp, st_data); // length with CRC
8467
    append_tx_crc(`MEMORY_BASE, (max_tmp - 5), 1'b0); // for first packet
8468
    // enable TX, set full-duplex mode, NO padding and NO CRC appending
8469
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD,
8470
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8471
    // check WB INT signal
8472
    if (wb_int !== 1'b0)
8473
    begin
8474
      test_fail("WB INT signal should not be set");
8475
      fail = fail + 1;
8476
    end
8477
 
8478
    // write to phy's control register for 100Mbps
8479
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
8480
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
8481
    speed = 100;
8482
 
8483
    i_length = (max_tmp - 5); // (max_tmp - 1); // not (max_tmp - 5) because NO automatic CRC appending
8484
    while (i_length <= (max_tmp - 3)) // (max_tmp + 1)) // (max_tmp) is the limit
8485
    begin
8486
      $display("   i_length = %0d", i_length);
8487
      // prepare packet's CRC
8488
      if (num_of_bd == 1)
8489
        append_tx_crc(`MEMORY_BASE, (max_tmp - 4), 1'b0); // for second and third packets
8490
      // Reset_tx_bd nable interrupt generation
8491
      // unmask interrupts
8492
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
8493
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8494
      // not detect carrier sense in FD and no collision
8495
      eth_phy.carrier_sense_tx_fd_detect(0);
8496
      eth_phy.collision(0);
8497
      // first destination address on ethernet PHY
8498
      eth_phy.set_tx_mem_addr(0);
8499
      // prepare BDs
8500
      if (num_of_bd == 0)
8501
      begin
8502
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8503
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8504
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8505
        set_tx_bd_wrap(2);
8506
        set_tx_bd_ready(0, 0);
8507
      end
8508
      else if (num_of_bd == 1)
8509
        set_tx_bd_ready(1, 1);
8510
      else if (num_of_bd == 2)
8511
        set_tx_bd_ready(2, 2);
8512
      // CHECK END OF TRANSMITION
8513
      check_tx_bd(num_of_bd, data);
8514
        wait (MTxEn === 1'b1); // start transmit
8515
      check_tx_bd(num_of_bd, data);
8516
        if (data[15] !== 1)
8517
        begin
8518
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
8519
          fail = fail + 1;
8520
        end
8521
        wait (MTxEn === 1'b0); // end transmit
8522
        while (data[15] === 1)
8523
        begin
8524
      check_tx_bd(num_of_bd, data);
8525
          @(posedge wb_clk);
8526
        end
8527
        repeat (1) @(posedge wb_clk);
8528
      // check length of a PACKET
8529
      $display("   eth_phy length = %0d", eth_phy.tx_len);
8530
      tmp_len = eth_phy.tx_len;
8531
      #1;
8532
      if (tmp_len != (i_length + 4))
8533
      begin
8534
        test_fail("Wrong length of the packet out from MAC");
8535
        fail = fail + 1;
8536
      end
8537
      // checking packet
8538
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
8539
      if (tmp > 0)
8540
      begin
8541
        test_fail("Wrong data of the transmitted packet");
8542
        fail = fail + 1;
8543
      end
8544
      // check transmited TX packet CRC
8545
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
8546
      if (tmp > 0)
8547
      begin
8548
        test_fail("Wrong CRC of the transmitted packet");
8549
        fail = fail + 1;
8550
      end
8551
      // check WB INT signal
8552
      if (wb_int !== 1'b1)
8553
      begin
8554
        `TIME; $display("*E WB INT signal should be set");
8555
        test_fail("WB INT signal should be set");
8556
        fail = fail + 1;
8557
      end
8558
      // check TX buffer descriptor of a packet
8559
      check_tx_bd(num_of_bd, data);
8560
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
8561
           ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
8562
      begin
8563
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8564
        test_fail("TX buffer descriptor status is not correct");
8565
        fail = fail + 1;
8566
      end
8567
      // check interrupts
8568
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8569
      if ((data & `ETH_INT_TXB) !== 1'b1)
8570
      begin
8571
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8572
        test_fail("Interrupt Transmit Buffer was not set");
8573
        fail = fail + 1;
8574
      end
8575
      if ((data & (~`ETH_INT_TXB)) !== 0)
8576
      begin
8577
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8578
        test_fail("Other interrupts (except Transmit Buffer) were set");
8579
        fail = fail + 1;
8580
      end
8581
      // clear interrupts
8582
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8583
      // check WB INT signal
8584
      if (wb_int !== 1'b0)
8585
      begin
8586
        test_fail("WB INT signal should not be set");
8587
        fail = fail + 1;
8588
      end
8589
      // INTERMEDIATE DISPLAYS
8590
      if (num_of_bd == 0)
8591
        $display("    ->packet with length %0d sent", (i_length + 4));
8592
      else if (num_of_bd == 1)
8593
        $display("    ->packet with length %0d sent", (i_length + 4));
8594
      else if (num_of_bd == 2)
8595
        $display("    ->packet with length %0d sent", (i_length + 4));
8596
      // set length (loop variable)
8597
      i_length = i_length + 1;
8598
      // the number of frame transmitted
8599
      num_of_frames = num_of_frames + 1;
8600
      num_of_bd = num_of_bd + 1;
8601
      @(posedge wb_clk);
8602
    end
8603
    // disable TX
8604
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8605
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8606
    @(posedge wb_clk);
8607
    if(fail == 0)
8608
      test_ok;
8609
    else
8610
      fail = 0;
8611
  end
8612
 
8613
 
8614
  ////////////////////////////////////////////////////////////////////
8615
  ////                                                            ////
8616
  ////  Test transmit packets across changed MINFL value at       ////
8617
  ////  7 TX buffer decriptors ( 10Mbps ).                        ////
8618
  ////                                                            ////
8619
  ////////////////////////////////////////////////////////////////////
8620
  if (test_num == 14) // without and with padding
8621
  begin
8622
    // TEST 14: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 10Mbps )
8623
    test_name = "TEST 14: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 10Mbps )";
8624
    `TIME; $display("  TEST 14: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 10Mbps )");
8625
 
8626
    // reset MAC registers
8627
    hard_reset;
8628
    // reset MAC and MII LOGIC with soft reset
8629
    reset_mac;
8630
    reset_mii;
8631
    // set wb slave response
8632
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
8633
 
8634
    max_tmp = 0;
8635
    min_tmp = 0;
8636
    num_of_frames = 0;
8637
    num_of_bd = 0;
8638
    // set 7 TX buffer descriptors - must be set before TX enable
8639
    wbm_write(`ETH_TX_BD_NUM, 32'h7, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8640
    // prepare a packet of MAXFL + 10 length
8641
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8642
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
8643
    min_tmp = tmp[31:16];
8644
    // change MINFL value
8645
    min_tmp = max_tmp - 177;
8646
    wbm_write(`ETH_PACKETLEN, {min_tmp, max_tmp}, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8647
    st_data = 8'h62;
8648
    set_tx_packet(`MEMORY_BASE, min_tmp, st_data); // length without CRC
8649
    // enable TX, set full-duplex mode, padding and CRC appending
8650
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
8651
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8652
    // check WB INT signal
8653
    if (wb_int !== 1'b0)
8654
    begin
8655
      test_fail("WB INT signal should not be set");
8656
      fail = fail + 1;
8657
    end
8658
 
8659
    // write to phy's control register for 10Mbps
8660
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
8661
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
8662
    speed = 10;
8663
 
8664
    i_length = (min_tmp - 5);
8665
    while (i_length <= (min_tmp - 3)) // (min_tmp - 4) is the limit
8666
    begin
8667
      $display("   i_length = %0d", i_length);
8668
      // Reset_tx_bd nable interrupt generation
8669
      // unmask interrupts
8670
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
8671
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8672
      // not detect carrier sense in FD and no collision
8673
      eth_phy.carrier_sense_tx_fd_detect(0);
8674
      eth_phy.collision(0);
8675
      // first destination address on ethernet PHY
8676
      eth_phy.set_tx_mem_addr(0);
8677
      // prepare BDs
8678
      if (num_of_bd == 0)
8679
      begin
8680
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8681
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8682
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8683
        set_tx_bd_wrap(2);
8684
        set_tx_bd_ready(0, 0);
8685
      end
8686
      else if (num_of_bd == 1)
8687
        set_tx_bd_ready(1, 1);
8688
      else if (num_of_bd == 2)
8689
        set_tx_bd_ready(2, 2);
8690
      // CHECK END OF TRANSMITION
8691
      check_tx_bd(num_of_bd, data);
8692
        wait (MTxEn === 1'b1); // start transmit
8693
      check_tx_bd(num_of_bd, data);
8694
        if (data[15] !== 1)
8695
        begin
8696
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
8697
          fail = fail + 1;
8698
        end
8699
        wait (MTxEn === 1'b0); // end transmit
8700
        while (data[15] === 1)
8701
        begin
8702
      check_tx_bd(num_of_bd, data);
8703
          @(posedge wb_clk);
8704
        end
8705
        repeat (1) @(posedge wb_clk);
8706
      // check length of a PACKET
8707
      $display("   eth_phy length = %0d", eth_phy.tx_len);
8708
      tmp_len = eth_phy.tx_len;
8709
      #1;
8710
      if (tmp_len != (i_length + 4))
8711
      begin
8712
        test_fail("Wrong length of the packet out from MAC");
8713
        fail = fail + 1;
8714
      end
8715
      // checking packet
8716
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
8717
      if (tmp > 0)
8718
      begin
8719
        test_fail("Wrong data of the transmitted packet");
8720
        fail = fail + 1;
8721
      end
8722
      // check transmited TX packet CRC
8723
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
8724
      if (tmp > 0)
8725
      begin
8726
        test_fail("Wrong CRC of the transmitted packet");
8727
        fail = fail + 1;
8728
      end
8729
      // check WB INT signal
8730
      if (wb_int !== 1'b1)
8731
      begin
8732
        `TIME; $display("*E WB INT signal should be set");
8733
        test_fail("WB INT signal should be set");
8734
        fail = fail + 1;
8735
      end
8736
      // check TX buffer descriptor of a packet
8737
      check_tx_bd(num_of_bd, data);
8738
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
8739
           ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
8740
      begin
8741
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8742
        test_fail("TX buffer descriptor status is not correct");
8743
        fail = fail + 1;
8744
      end
8745
      // check interrupts
8746
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8747
      if ((data & `ETH_INT_TXB) !== 1'b1)
8748
      begin
8749
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8750
        test_fail("Interrupt Transmit Buffer was not set");
8751
        fail = fail + 1;
8752
      end
8753
      if ((data & (~`ETH_INT_TXB)) !== 0)
8754
      begin
8755
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8756
        test_fail("Other interrupts (except Transmit Buffer) were set");
8757
        fail = fail + 1;
8758
      end
8759
      // clear interrupts
8760
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8761
      // check WB INT signal
8762
      if (wb_int !== 1'b0)
8763
      begin
8764
        test_fail("WB INT signal should not be set");
8765
        fail = fail + 1;
8766
      end
8767
      // INTERMEDIATE DISPLAYS
8768
      if (num_of_bd == 0)
8769
        $display("    ->packet with length %0d sent", (i_length + 4));
8770
      else if (num_of_bd == 1)
8771
        $display("    ->packet with length %0d sent", (i_length + 4));
8772
      else if (num_of_bd == 2)
8773
        $display("    ->packet with length %0d sent", (i_length + 4));
8774
      // set length (loop variable)
8775
      i_length = i_length + 1;
8776
      // the number of frame transmitted
8777
      num_of_frames = num_of_frames + 1;
8778
      num_of_bd = num_of_bd + 1;
8779
      @(posedge wb_clk);
8780
    end
8781
    // disable TX
8782
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8783
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8784
    @(posedge wb_clk);
8785
    if(fail == 0)
8786
      test_ok;
8787
    else
8788
      fail = 0;
8789
  end
8790
 
8791
 
8792
  ////////////////////////////////////////////////////////////////////
8793
  ////                                                            ////
8794
  ////  Test transmit packets across changed MINFL value at       ////
8795
  ////  7 TX buffer decriptors ( 100Mbps ).                       ////
8796
  ////                                                            ////
8797
  ////////////////////////////////////////////////////////////////////
8798
  if (test_num == 15) // without and with padding
8799
  begin
8800
    // TEST 15: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 100Mbps )
8801
    test_name = "TEST 15: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 100Mbps )";
8802
    `TIME; $display("  TEST 15: TRANSMIT PACKETS ACROSS CHANGED MINFL VALUE AT 7 TX BDs ( 100Mbps )");
8803
 
8804
    // reset MAC registers
8805
    hard_reset;
8806
    // reset MAC and MII LOGIC with soft reset
8807
    reset_mac;
8808
    reset_mii;
8809
    // set wb slave response
8810
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
8811
 
8812
    max_tmp = 0;
8813
    min_tmp = 0;
8814
    num_of_frames = 0;
8815
    num_of_bd = 0;
8816
    // set 7 TX buffer descriptors - must be set before TX enable
8817
    wbm_write(`ETH_TX_BD_NUM, 32'h7, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8818
    // prepare a packet of MAXFL + 10 length
8819
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8820
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
8821
    min_tmp = tmp[31:16];
8822
    // change MINFL value
8823
    min_tmp = max_tmp - 177;
8824
    wbm_write(`ETH_PACKETLEN, {min_tmp, max_tmp}, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8825
    st_data = 8'h62;
8826
    set_tx_packet(`MEMORY_BASE, min_tmp, st_data); // length without CRC
8827
    // enable TX, set full-duplex mode, padding and CRC appending
8828
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
8829
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8830
    // check WB INT signal
8831
    if (wb_int !== 1'b0)
8832
    begin
8833
      test_fail("WB INT signal should not be set");
8834
      fail = fail + 1;
8835
    end
8836
 
8837
    // write to phy's control register for 100Mbps
8838
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
8839
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
8840
    speed = 100;
8841
 
8842
    i_length = (min_tmp - 5);
8843
    while (i_length <= (min_tmp - 3)) // (min_tmp - 4) is the limit
8844
    begin
8845
      $display("   i_length = %0d", i_length);
8846
      // Reset_tx_bd nable interrupt generation
8847
      // unmask interrupts
8848
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
8849
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8850
      // not detect carrier sense in FD and no collision
8851
      eth_phy.carrier_sense_tx_fd_detect(0);
8852
      eth_phy.collision(0);
8853
      // first destination address on ethernet PHY
8854
      eth_phy.set_tx_mem_addr(0);
8855
      // prepare BDs
8856
      if (num_of_bd == 0)
8857
      begin
8858
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8859
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8860
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
8861
        set_tx_bd_wrap(2);
8862
        set_tx_bd_ready(0, 0);
8863
      end
8864
      else if (num_of_bd == 1)
8865
        set_tx_bd_ready(1, 1);
8866
      else if (num_of_bd == 2)
8867
        set_tx_bd_ready(2, 2);
8868
      // CHECK END OF TRANSMITION
8869
      check_tx_bd(num_of_bd, data);
8870
        wait (MTxEn === 1'b1); // start transmit
8871
      check_tx_bd(num_of_bd, data);
8872
        if (data[15] !== 1)
8873
        begin
8874
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
8875
          fail = fail + 1;
8876
        end
8877
        wait (MTxEn === 1'b0); // end transmit
8878
        while (data[15] === 1)
8879
        begin
8880
      check_tx_bd(num_of_bd, data);
8881
          @(posedge wb_clk);
8882
        end
8883
        repeat (1) @(posedge wb_clk);
8884
      // check length of a PACKET
8885
      $display("   eth_phy length = %0d", eth_phy.tx_len);
8886
      tmp_len = eth_phy.tx_len;
8887
      #1;
8888
      if (tmp_len != (i_length + 4))
8889
      begin
8890
        test_fail("Wrong length of the packet out from MAC");
8891
        fail = fail + 1;
8892
      end
8893
      // checking packet
8894
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
8895
      if (tmp > 0)
8896
      begin
8897
        test_fail("Wrong data of the transmitted packet");
8898
        fail = fail + 1;
8899
      end
8900
      // check transmited TX packet CRC
8901
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
8902
      if (tmp > 0)
8903
      begin
8904
        test_fail("Wrong CRC of the transmitted packet");
8905
        fail = fail + 1;
8906
      end
8907
      // check WB INT signal
8908
      if (wb_int !== 1'b1)
8909
      begin
8910
        `TIME; $display("*E WB INT signal should be set");
8911
        test_fail("WB INT signal should be set");
8912
        fail = fail + 1;
8913
      end
8914
      // check TX buffer descriptor of a packet
8915
      check_tx_bd(num_of_bd, data);
8916
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 2)) || // wrap bit
8917
           ((data[15:0] !== 16'h5800) && (num_of_bd < 2)) )   // without wrap bit
8918
      begin
8919
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
8920
        test_fail("TX buffer descriptor status is not correct");
8921
        fail = fail + 1;
8922
      end
8923
      // check interrupts
8924
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8925
      if ((data & `ETH_INT_TXB) !== 1'b1)
8926
      begin
8927
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
8928
        test_fail("Interrupt Transmit Buffer was not set");
8929
        fail = fail + 1;
8930
      end
8931
      if ((data & (~`ETH_INT_TXB)) !== 0)
8932
      begin
8933
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
8934
        test_fail("Other interrupts (except Transmit Buffer) were set");
8935
        fail = fail + 1;
8936
      end
8937
      // clear interrupts
8938
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8939
      // check WB INT signal
8940
      if (wb_int !== 1'b0)
8941
      begin
8942
        test_fail("WB INT signal should not be set");
8943
        fail = fail + 1;
8944
      end
8945
      // INTERMEDIATE DISPLAYS
8946
      if (num_of_bd == 0)
8947
        $display("    ->packet with length %0d sent", (i_length + 4));
8948
      else if (num_of_bd == 1)
8949
        $display("    ->packet with length %0d sent", (i_length + 4));
8950
      else if (num_of_bd == 2)
8951
        $display("    ->packet with length %0d sent", (i_length + 4));
8952
      // set length (loop variable)
8953
      i_length = i_length + 1;
8954
      // the number of frame transmitted
8955
      num_of_frames = num_of_frames + 1;
8956
      num_of_bd = num_of_bd + 1;
8957
      @(posedge wb_clk);
8958
    end
8959
    // disable TX
8960
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
8961
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8962
    @(posedge wb_clk);
8963
    if(fail == 0)
8964
      test_ok;
8965
    else
8966
      fail = 0;
8967
  end
8968
 
8969
 
8970
  ////////////////////////////////////////////////////////////////////
8971
  ////                                                            ////
8972
  ////  Test transmit packets across MAXFL with HUGEN at          ////
8973
  ////  19 TX buffer decriptors ( 10Mbps ).                       ////
8974
  ////                                                            ////
8975
  ////////////////////////////////////////////////////////////////////
8976
  if (test_num == 16) // without and with padding
8977
  begin
8978
    // TEST 16: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 10Mbps )
8979
    test_name = "TEST 16: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 10Mbps )";
8980
    `TIME; $display("  TEST 16: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 10Mbps )");
8981
 
8982
    // reset MAC registers
8983
    hard_reset;
8984
    // reset MAC and MII LOGIC with soft reset
8985
    reset_mac;
8986
    reset_mii;
8987
    // set wb slave response
8988
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
8989
 
8990
    max_tmp = 0;
8991
    min_tmp = 0;
8992
    num_of_frames = 0;
8993
    num_of_bd = 0;
8994
    // set 19 TX buffer descriptors - must be set before TX enable
8995
    wbm_write(`ETH_TX_BD_NUM, 32'h13, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8996
    // prepare a packet of 64k - 1 length (16'hFFFF)
8997
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
8998
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
8999
    min_tmp = tmp[31:16];
9000
    st_data = 8'h8D;
9001
    set_tx_packet(`MEMORY_BASE, 16'hFFFF, st_data); // length with CRC
9002
    // enable TX, set full-duplex mode, NO padding, CRC appending and huge enabled
9003
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN | `ETH_MODER_HUGEN,
9004
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9005
    // check WB INT signal
9006
    if (wb_int !== 1'b0)
9007
    begin
9008
      test_fail("WB INT signal should not be set");
9009
      fail = fail + 1;
9010
    end
9011
 
9012
    // write to phy's control register for 10Mbps
9013
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
9014
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
9015
    speed = 10;
9016
 
9017
    i_length = (max_tmp - 5); // (max_tmp - 4) is the MAXFL limit
9018
    while (i_length <= (16'hFFFF - 4)) // (16'hFFFF - 4) is the limit
9019
    begin
9020
      $display("   i_length = %0d", i_length);
9021
      // Reset_tx_bd nable interrupt generation
9022
      // unmask interrupts
9023
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
9024
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9025
      // not detect carrier sense in FD and no collision
9026
      eth_phy.carrier_sense_tx_fd_detect(0);
9027
      eth_phy.collision(0);
9028
      // first destination address on ethernet PHY
9029
      eth_phy.set_tx_mem_addr(0);
9030
      // prepare BDs
9031
      if (num_of_bd == 0)
9032
      begin
9033
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9034
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9035
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9036
        set_tx_bd(3, 3, (16'hFFFF - 5), 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9037
        set_tx_bd(4, 4, (16'hFFFF - 4), 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9038
        set_tx_bd_wrap(4);
9039
        set_tx_bd_ready(0, 0);
9040
      end
9041
      else if (num_of_bd == 1)
9042
        set_tx_bd_ready(1, 1);
9043
      else if (num_of_bd == 2)
9044
        set_tx_bd_ready(2, 2);
9045
      else if (num_of_bd == 3)
9046
        set_tx_bd_ready(3, 3);
9047
      else if (num_of_bd == 4)
9048
        set_tx_bd_ready(4, 4);
9049
      // CHECK END OF TRANSMITION
9050
      check_tx_bd(num_of_bd, data);
9051
        wait (MTxEn === 1'b1); // start transmit
9052
      check_tx_bd(num_of_bd, data);
9053
        if (data[15] !== 1)
9054
        begin
9055
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
9056
          fail = fail + 1;
9057
        end
9058
        wait (MTxEn === 1'b0); // end transmit
9059
        while (data[15] === 1)
9060
        begin
9061
      check_tx_bd(num_of_bd, data);
9062
          @(posedge wb_clk);
9063
        end
9064
        repeat (1) @(posedge wb_clk);
9065
      // check length of a PACKET
9066
      $display("   eth_phy length = %0d", eth_phy.tx_len);
9067
      tmp_len = eth_phy.tx_len;
9068
      #1;
9069
      if (tmp_len != (i_length + 4))
9070
      begin
9071
        test_fail("Wrong length of the packet out from MAC");
9072
        fail = fail + 1;
9073
      end
9074
      // checking packet
9075
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
9076
      if (tmp > 0)
9077
      begin
9078
        test_fail("Wrong data of the transmitted packet");
9079
        fail = fail + 1;
9080
      end
9081
      // check transmited TX packet CRC
9082
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
9083
      if (tmp > 0)
9084
      begin
9085
        test_fail("Wrong CRC of the transmitted packet");
9086
        fail = fail + 1;
9087
      end
9088
      // check WB INT signal
9089
      if (wb_int !== 1'b1)
9090
      begin
9091
        `TIME; $display("*E WB INT signal should be set");
9092
        test_fail("WB INT signal should be set");
9093
        fail = fail + 1;
9094
      end
9095
      // check TX buffer descriptor of a packet
9096
      check_tx_bd(num_of_bd, data);
9097
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 4)) || // wrap bit
9098
           ((data[15:0] !== 16'h5800) && (num_of_bd < 4)) )   // without wrap bit
9099
      begin
9100
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
9101
        test_fail("TX buffer descriptor status is not correct");
9102
        fail = fail + 1;
9103
      end
9104
      // check interrupts
9105
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9106
      if ((data & `ETH_INT_TXB) !== 1'b1)
9107
      begin
9108
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
9109
        test_fail("Interrupt Transmit Buffer was not set");
9110
        fail = fail + 1;
9111
      end
9112
      if ((data & (~`ETH_INT_TXB)) !== 0)
9113
      begin
9114
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
9115
        test_fail("Other interrupts (except Transmit Buffer) were set");
9116
        fail = fail + 1;
9117
      end
9118
      // clear interrupts
9119
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9120
      // check WB INT signal
9121
      if (wb_int !== 1'b0)
9122
      begin
9123
        test_fail("WB INT signal should not be set");
9124
        fail = fail + 1;
9125
      end
9126
      // INTERMEDIATE DISPLAYS
9127
      $display("    ->packet with length %0d sent", (i_length + 4));
9128
      // set length (loop variable)
9129
      if ((num_of_bd < 2) || (num_of_bd >= 3))
9130
        i_length = i_length + 1;
9131
      else if (num_of_bd == 2)
9132
        i_length = (16'hFFFF - 5);
9133
      // the number of frame transmitted
9134
      num_of_frames = num_of_frames + 1;
9135
      num_of_bd = num_of_bd + 1;
9136
      @(posedge wb_clk);
9137
    end
9138
    // disable TX
9139
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
9140
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9141
    @(posedge wb_clk);
9142
    if(fail == 0)
9143
      test_ok;
9144
    else
9145
      fail = 0;
9146
  end
9147
 
9148
 
9149
  ////////////////////////////////////////////////////////////////////
9150
  ////                                                            ////
9151
  ////  Test transmit packets across MAXFL with HUGEN at          ////
9152
  ////  19 TX buffer decriptors ( 100Mbps ).                      ////
9153
  ////                                                            ////
9154
  ////////////////////////////////////////////////////////////////////
9155
  if (test_num == 17) // without and with padding
9156
  begin
9157
    // TEST 17: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 100Mbps )
9158
    test_name = "TEST 17: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 100Mbps )";
9159
    `TIME; $display("  TEST 17: TRANSMIT PACKETS ACROSS MAXFL WITH HUGEN AT 19 TX BDs ( 100Mbps )");
9160
 
9161
    // reset MAC registers
9162
    hard_reset;
9163
    // reset MAC and MII LOGIC with soft reset
9164
    reset_mac;
9165
    reset_mii;
9166
    // set wb slave response
9167
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
9168
 
9169
    max_tmp = 0;
9170
    min_tmp = 0;
9171
    num_of_frames = 0;
9172
    num_of_bd = 0;
9173
    // set 19 TX buffer descriptors - must be set before TX enable
9174
    wbm_write(`ETH_TX_BD_NUM, 32'h13, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9175
    // prepare a packet of 64k - 1 length (16'hFFFF)
9176
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9177
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
9178
    min_tmp = tmp[31:16];
9179
    st_data = 8'h8D;
9180
    set_tx_packet(`MEMORY_BASE, 16'hFFFF, st_data); // length with CRC
9181
    // enable TX, set full-duplex mode, NO padding, CRC appending and huge enabled
9182
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN | `ETH_MODER_HUGEN,
9183
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9184
    // check WB INT signal
9185
    if (wb_int !== 1'b0)
9186
    begin
9187
      test_fail("WB INT signal should not be set");
9188
      fail = fail + 1;
9189
    end
9190
 
9191
    // write to phy's control register for 100Mbps
9192
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
9193
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
9194
    speed = 100;
9195
 
9196
    i_length = (max_tmp - 5); // (max_tmp - 4) is the MAXFL limit
9197
    while (i_length <= (16'hFFFF - 4)) // (16'hFFFF - 4) is the limit
9198
    begin
9199
      $display("   i_length = %0d", i_length);
9200
      // Reset_tx_bd nable interrupt generation
9201
      // unmask interrupts
9202
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
9203
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9204
      // not detect carrier sense in FD and no collision
9205
      eth_phy.carrier_sense_tx_fd_detect(0);
9206
      eth_phy.collision(0);
9207
      // first destination address on ethernet PHY
9208
      eth_phy.set_tx_mem_addr(0);
9209
      // prepare BDs
9210
      if (num_of_bd == 0)
9211
      begin
9212
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9213
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9214
        set_tx_bd(2, 2, i_length+2, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9215
        set_tx_bd(3, 3, (16'hFFFF - 5), 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9216
        set_tx_bd(4, 4, (16'hFFFF - 4), 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9217
        set_tx_bd_wrap(4);
9218
        set_tx_bd_ready(0, 0);
9219
      end
9220
      else if (num_of_bd == 1)
9221
        set_tx_bd_ready(1, 1);
9222
      else if (num_of_bd == 2)
9223
        set_tx_bd_ready(2, 2);
9224
      else if (num_of_bd == 3)
9225
        set_tx_bd_ready(3, 3);
9226
      else if (num_of_bd == 4)
9227
        set_tx_bd_ready(4, 4);
9228
      // CHECK END OF TRANSMITION
9229
      check_tx_bd(num_of_bd, data);
9230
        wait (MTxEn === 1'b1); // start transmit
9231
      check_tx_bd(num_of_bd, data);
9232
        if (data[15] !== 1)
9233
        begin
9234
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
9235
          fail = fail + 1;
9236
        end
9237
        wait (MTxEn === 1'b0); // end transmit
9238
        while (data[15] === 1)
9239
        begin
9240
      check_tx_bd(num_of_bd, data);
9241
          @(posedge wb_clk);
9242
        end
9243
        repeat (1) @(posedge wb_clk);
9244
      // check length of a PACKET
9245
      $display("   eth_phy length = %0d", eth_phy.tx_len);
9246
      tmp_len = eth_phy.tx_len;
9247
      #1;
9248
      if (tmp_len != (i_length + 4))
9249
      begin
9250
        test_fail("Wrong length of the packet out from MAC");
9251
        fail = fail + 1;
9252
      end
9253
      // checking packet
9254
      check_tx_packet(`MEMORY_BASE, 0, i_length, tmp);
9255
      if (tmp > 0)
9256
      begin
9257
        test_fail("Wrong data of the transmitted packet");
9258
        fail = fail + 1;
9259
      end
9260
      // check transmited TX packet CRC
9261
      check_tx_crc(0, i_length, 1'b0, tmp); // length without CRC
9262
      if (tmp > 0)
9263
      begin
9264
        test_fail("Wrong CRC of the transmitted packet");
9265
        fail = fail + 1;
9266
      end
9267
      // check WB INT signal
9268
      if (wb_int !== 1'b1)
9269
      begin
9270
        `TIME; $display("*E WB INT signal should be set");
9271
        test_fail("WB INT signal should be set");
9272
        fail = fail + 1;
9273
      end
9274
      // check TX buffer descriptor of a packet
9275
      check_tx_bd(num_of_bd, data);
9276
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 4)) || // wrap bit
9277
           ((data[15:0] !== 16'h5800) && (num_of_bd < 4)) )   // without wrap bit
9278
      begin
9279
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
9280
        test_fail("TX buffer descriptor status is not correct");
9281
        fail = fail + 1;
9282
      end
9283
      // check interrupts
9284
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9285
      if ((data & `ETH_INT_TXB) !== 1'b1)
9286
      begin
9287
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
9288
        test_fail("Interrupt Transmit Buffer was not set");
9289
        fail = fail + 1;
9290
      end
9291
      if ((data & (~`ETH_INT_TXB)) !== 0)
9292
      begin
9293
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
9294
        test_fail("Other interrupts (except Transmit Buffer) were set");
9295
        fail = fail + 1;
9296
      end
9297
      // clear interrupts
9298
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9299
      // check WB INT signal
9300
      if (wb_int !== 1'b0)
9301
      begin
9302
        test_fail("WB INT signal should not be set");
9303
        fail = fail + 1;
9304
      end
9305
      // INTERMEDIATE DISPLAYS
9306
      $display("    ->packet with length %0d sent", (i_length + 4));
9307
      // set length (loop variable)
9308
      if ((num_of_bd < 2) || (num_of_bd >= 3))
9309
        i_length = i_length + 1;
9310
      else if (num_of_bd == 2)
9311
        i_length = (16'hFFFF - 5);
9312
      // the number of frame transmitted
9313
      num_of_frames = num_of_frames + 1;
9314
      num_of_bd = num_of_bd + 1;
9315
      @(posedge wb_clk);
9316
    end
9317
    // disable TX
9318
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
9319
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9320
    @(posedge wb_clk);
9321
    if(fail == 0)
9322
      test_ok;
9323
    else
9324
      fail = 0;
9325
  end
9326
 
9327
 
9328
  ////////////////////////////////////////////////////////////////////
9329
  ////                                                            ////
9330
  ////  Test IPG during Back-to-Back transmit at                  ////
9331
  ////  88 TX buffer decriptors ( 10Mbps ).                       ////
9332
  ////                                                            ////
9333
  ////////////////////////////////////////////////////////////////////
9334
  if (test_num == 18) // without and with padding
9335
  begin
9336
    // TEST 18: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 10Mbps )
9337
    test_name = "TEST 18: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 10Mbps )";
9338
    `TIME; $display("  TEST 18: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 10Mbps )");
9339
 
9340
    // reset MAC registers
9341
    hard_reset;
9342
    // reset MAC and MII LOGIC with soft reset
9343
    reset_mac;
9344
    reset_mii;
9345
    // set wb slave response
9346
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
9347
 
9348
    max_tmp = 0;
9349
    min_tmp = 0;
9350
    num_of_frames = 0;
9351
    num_of_bd = 0;
9352
    tmp_ipgt = 0;
9353
    // set 88 TX buffer descriptors - must be set before TX enable
9354
    wbm_write(`ETH_TX_BD_NUM, 32'h58, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9355
    // enable TX, set full-duplex mode, NO padding and CRC appending
9356
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9357
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9358
    // prepare two packets of MAXFL length
9359
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9360
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
9361
    min_tmp = tmp[31:16];
9362
    st_data = 8'h29;
9363
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
9364
    // check WB INT signal
9365
    if (wb_int !== 1'b0)
9366
    begin
9367
      test_fail("WB INT signal should not be set");
9368
      fail = fail + 1;
9369
    end
9370
 
9371
    // write to phy's control register for 10Mbps
9372
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
9373
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
9374
    speed = 10;
9375
 
9376
    i_length = (min_tmp - 4);
9377
    while (i_length < (max_tmp - 4))
9378
    begin
9379
      // disable TX, set full-duplex mode, NO padding and CRC appending
9380
      wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9381
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9382
      // set IPGT register
9383
      wbm_write(`ETH_IPGT, tmp_ipgt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9384
      // enable TX, set full-duplex mode, NO padding and CRC appending
9385
      wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9386
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9387
      // Reset_tx_bd enable interrupt generation
9388
      // unmask interrupts
9389
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
9390
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9391
      // not detect carrier sense in FD and no collision
9392
      eth_phy.carrier_sense_tx_fd_detect(0);
9393
      eth_phy.collision(0);
9394
      // first destination address on ethernet PHY
9395
      eth_phy.set_tx_mem_addr(0);
9396
      // prepare BDs
9397
      if (num_of_bd == 0)
9398
      begin
9399
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9400
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9401
        set_tx_bd_wrap(1);
9402
        set_tx_bd_ready(0, 0);
9403
        set_tx_bd_ready(1, 1);
9404
      end
9405
      // CHECK END OF TWO TRANSMITIONs
9406
      // wait for first transmit to end
9407
      check_tx_bd(num_of_bd, data);
9408
      wait (MTxEn === 1'b1); // start transmit
9409
      if (data[15] !== 1)
9410
      begin
9411
        test_fail("Wrong buffer descriptor's ready bit read out from MAC");
9412
        fail = fail + 1;
9413
      end
9414
      wait (MTxEn === 1'b0); // end transmit
9415
      num_of_frames = num_of_frames + 1;
9416
      num_of_bd = num_of_bd + 1;
9417
      #Tp;
9418
      // destination address on ethernet PHY
9419
      eth_phy.set_tx_mem_addr(0);
9420
      i1 = 0;
9421
      i2 = 0;
9422
      // count IPG clock periods
9423
      fork
9424
        begin
9425
          wait (MTxEn === 1'b1); // start second transmit
9426
          #Tp;
9427
          disable count_rising;
9428
          disable count_falling;
9429
        end
9430
        begin: count_rising
9431
          forever
9432
          begin
9433
            @(posedge mtx_clk);
9434
            i1 = i1 + 1;
9435
            #Tp;
9436
          end
9437
        end
9438
        begin: count_falling
9439
          forever
9440
          begin
9441
            @(negedge mtx_clk);
9442
            i2 = i2 + 1;
9443
            #Tp;
9444
          end
9445
        end
9446
      join
9447
      // check IPG length - INTERMEDIATE DISPLAYS
9448
      if((i1 == i2) && (i1 >= (tmp_ipgt + 3)))
9449
      begin
9450
        $display("    ->IPG with %0d mtx_clk periods (min %0d) between packets with lengths %0d and %0d checked",
9451
                  i1, (tmp_ipgt + 3), (i_length + 4), (i_length + 4 + 1));
9452
      end
9453
      else
9454
      begin
9455
        `TIME; $display("*E IPG is not correct: (%0d + %0d) / 2, requested: %d", i1, i2, (tmp_ipgt + 3));
9456
        fail = fail + 1;
9457
        test_fail("IPG is not correct");
9458
      end
9459
      // wait for second transmit to end
9460
      wait (MTxEn === 1'b0); // end second transmit
9461
      while (data[15] === 1)
9462
      begin
9463
        check_tx_bd(num_of_bd, data);
9464
        @(posedge wb_clk);
9465
      end
9466
      repeat (1) @(posedge wb_clk);
9467
      // check length of a second PACKET
9468
      tmp_len = eth_phy.tx_len;
9469
      #1;
9470
      if (tmp_len != (i_length + 4 + 1))
9471
      begin
9472
        test_fail("Wrong length of second packet out from MAC");
9473
        fail = fail + 1;
9474
      end
9475
      // checking second packet
9476
      check_tx_packet(`MEMORY_BASE, 0, (i_length + 1), tmp);
9477
      if (tmp > 0)
9478
      begin
9479
        test_fail("Wrong data of second transmitted packet");
9480
        fail = fail + 1;
9481
      end
9482
      // check second transmited TX packet CRC
9483
      check_tx_crc(0, (i_length + 1), 1'b0, tmp); // length without CRC
9484
      if (tmp > 0)
9485
      begin
9486
        test_fail("Wrong CRC of second transmitted packet");
9487
        fail = fail + 1;
9488
      end
9489
      // check WB INT signal
9490
      if (wb_int !== 1'b1)
9491
      begin
9492
        `TIME; $display("*E WB INT signal should be set");
9493
        test_fail("WB INT signal should be set");
9494
        fail = fail + 1;
9495
      end
9496
      // check TX buffer descriptor of a packet
9497
      check_tx_bd(num_of_bd, data);
9498
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
9499
           ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
9500
      begin
9501
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
9502
        test_fail("TX buffer descriptor status is not correct");
9503
        fail = fail + 1;
9504
      end
9505
      // check interrupts
9506
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9507
      if ((data & `ETH_INT_TXB) !== 1'b1)
9508
      begin
9509
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
9510
        test_fail("Interrupt Transmit Buffer was not set");
9511
        fail = fail + 1;
9512
      end
9513
      if ((data & (~`ETH_INT_TXB)) !== 0)
9514
      begin
9515
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
9516
        test_fail("Other interrupts (except Transmit Buffer) were set");
9517
        fail = fail + 1;
9518
      end
9519
      // clear interrupts
9520
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9521
      // check WB INT signal
9522
      if (wb_int !== 1'b0)
9523
      begin
9524
        test_fail("WB INT signal should not be set");
9525
        fail = fail + 1;
9526
      end
9527
      // set length (LOOP variable)
9528
      if ((tmp_ipgt + 3) < 130) // tmp_ipgt < 124
9529
        i_length = i_length + 2;
9530
      else
9531
        i_length = (max_tmp - 4);
9532
      // set IPGT
9533
      if ((tmp_ipgt + 3) < 10)
9534
        tmp_ipgt = tmp_ipgt + 1;
9535
      else if ((tmp_ipgt + 3) < 24)
9536
        tmp_ipgt = tmp_ipgt + 7;
9537
      else if ((tmp_ipgt + 3) == 24)
9538
        tmp_ipgt = 38 - 3;
9539
      else if ((tmp_ipgt + 3) == 38)
9540
        tmp_ipgt = 72 - 3;
9541
      else if ((tmp_ipgt + 3) == 72)
9542
        tmp_ipgt = 130 - 3; // 124 - 3
9543
      // the number of frame transmitted
9544
      num_of_frames = num_of_frames + 1;
9545
      num_of_bd = 0;
9546
      @(posedge wb_clk);
9547
    end
9548
    // disable TX
9549
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
9550
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9551
    @(posedge wb_clk);
9552
    if(fail == 0)
9553
      test_ok;
9554
    else
9555
      fail = 0;
9556
  end
9557
 
9558
 
9559
  ////////////////////////////////////////////////////////////////////
9560
  ////                                                            ////
9561
  ////  Test IPG during Back-to-Back transmit at                  ////
9562
  ////  88 TX buffer decriptors ( 100Mbps ).                      ////
9563
  ////                                                            ////
9564
  ////////////////////////////////////////////////////////////////////
9565
  if (test_num == 19) // without and with padding
9566
  begin
9567
    // TEST 19: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 100Mbps )
9568
    test_name = "TEST 19: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 100Mbps )";
9569
    `TIME; $display("  TEST 19: IPG DURING BACK-TO-BACK TRANSMIT AT 88 TX BDs ( 100Mbps )");
9570
 
9571
    // reset MAC registers
9572
    hard_reset;
9573
    // reset MAC and MII LOGIC with soft reset
9574
    reset_mac;
9575
    reset_mii;
9576
    // set wb slave response
9577
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
9578
 
9579
    max_tmp = 0;
9580
    min_tmp = 0;
9581
    num_of_frames = 0;
9582
    num_of_bd = 0;
9583
    tmp_ipgt = 0;
9584
    // set 88 TX buffer descriptors - must be set before TX enable
9585
    wbm_write(`ETH_TX_BD_NUM, 32'h58, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9586
    // enable TX, set full-duplex mode, NO padding and CRC appending
9587
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9588
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9589
    // prepare two packets of MAXFL length
9590
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9591
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
9592
    min_tmp = tmp[31:16];
9593
    st_data = 8'h29;
9594
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
9595
    // check WB INT signal
9596
    if (wb_int !== 1'b0)
9597
    begin
9598
      test_fail("WB INT signal should not be set");
9599
      fail = fail + 1;
9600
    end
9601
 
9602
    // write to phy's control register for 100Mbps
9603
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
9604
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
9605
    speed = 100;
9606
 
9607
    i_length = (min_tmp - 4);
9608
    while (i_length < (max_tmp - 4))
9609
    begin
9610
      // disable TX, set full-duplex mode, NO padding and CRC appending
9611
      wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9612
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9613
      // set IPGT register
9614
      wbm_write(`ETH_IPGT, tmp_ipgt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9615
      // enable TX, set full-duplex mode, NO padding and CRC appending
9616
      wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9617
                4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9618
      // Reset_tx_bd enable interrupt generation
9619
      // unmask interrupts
9620
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
9621
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9622
      // not detect carrier sense in FD and no collision
9623
      eth_phy.carrier_sense_tx_fd_detect(0);
9624
      eth_phy.collision(0);
9625
      // first destination address on ethernet PHY
9626
      eth_phy.set_tx_mem_addr(0);
9627
      // prepare BDs
9628
      if (num_of_bd == 0)
9629
      begin
9630
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9631
        set_tx_bd(1, 1, i_length+1, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9632
        set_tx_bd_wrap(1);
9633
        set_tx_bd_ready(0, 0);
9634
        set_tx_bd_ready(1, 1);
9635
      end
9636
      // CHECK END OF TWO TRANSMITIONs
9637
      // wait for first transmit to end
9638
      check_tx_bd(num_of_bd, data);
9639
      wait (MTxEn === 1'b1); // start transmit
9640
      if (data[15] !== 1)
9641
      begin
9642
        test_fail("Wrong buffer descriptor's ready bit read out from MAC");
9643
        fail = fail + 1;
9644
      end
9645
      wait (MTxEn === 1'b0); // end transmit
9646
      num_of_frames = num_of_frames + 1;
9647
      num_of_bd = num_of_bd + 1;
9648
      #Tp;
9649
      // destination address on ethernet PHY
9650
      eth_phy.set_tx_mem_addr(0);
9651
      i1 = 0;
9652
      i2 = 0;
9653
      // count IPG clock periods
9654
      fork
9655
        begin
9656
          wait (MTxEn === 1'b1); // start second transmit
9657
          #Tp;
9658
          disable count_rising1;
9659
          disable count_falling1;
9660
        end
9661
        begin: count_rising1
9662
          forever
9663
          begin
9664
            @(posedge mtx_clk);
9665
            i1 = i1 + 1;
9666
            #Tp;
9667
          end
9668
        end
9669
        begin: count_falling1
9670
          forever
9671
          begin
9672
            @(negedge mtx_clk);
9673
            i2 = i2 + 1;
9674
            #Tp;
9675
          end
9676
        end
9677
      join
9678
      // check IPG length - INTERMEDIATE DISPLAYS
9679
      if((i1 == i2) && (i1 >= (tmp_ipgt + 3)))
9680
      begin
9681
        $display("    ->IPG with %0d mtx_clk periods (min %0d) between packets with lengths %0d and %0d checked",
9682
                  i1, (tmp_ipgt + 3), (i_length + 4), (i_length + 4 + 1));
9683
      end
9684
      else
9685
      begin
9686
        `TIME; $display("*E IPG is not correct: (%0d + %0d) / 2, requested: %d", i1, i2, (tmp_ipgt + 3));
9687
        fail = fail + 1;
9688
        test_fail("IPG is not correct");
9689
      end
9690
      // wait for second transmit to end
9691
      wait (MTxEn === 1'b0); // end second transmit
9692
      while (data[15] === 1)
9693
      begin
9694
        check_tx_bd(num_of_bd, data);
9695
        @(posedge wb_clk);
9696
      end
9697
      repeat (1) @(posedge wb_clk);
9698
      // check length of a second PACKET
9699
      tmp_len = eth_phy.tx_len;
9700
      #1;
9701
      if (tmp_len != (i_length + 4 + 1))
9702
      begin
9703
        test_fail("Wrong length of second packet out from MAC");
9704
        fail = fail + 1;
9705
      end
9706
      // checking second packet
9707
      check_tx_packet(`MEMORY_BASE, 0, (i_length + 1), tmp);
9708
      if (tmp > 0)
9709
      begin
9710
        test_fail("Wrong data of second transmitted packet");
9711
        fail = fail + 1;
9712
      end
9713
      // check second transmited TX packet CRC
9714
      check_tx_crc(0, (i_length + 1), 1'b0, tmp); // length without CRC
9715
      if (tmp > 0)
9716
      begin
9717
        test_fail("Wrong CRC of second transmitted packet");
9718
        fail = fail + 1;
9719
      end
9720
      // check WB INT signal
9721
      if (wb_int !== 1'b1)
9722
      begin
9723
        `TIME; $display("*E WB INT signal should be set");
9724
        test_fail("WB INT signal should be set");
9725
        fail = fail + 1;
9726
      end
9727
      // check TX buffer descriptor of a packet
9728
      check_tx_bd(num_of_bd, data);
9729
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
9730
           ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
9731
      begin
9732
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
9733
        test_fail("TX buffer descriptor status is not correct");
9734
        fail = fail + 1;
9735
      end
9736
      // check interrupts
9737
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9738
      if ((data & `ETH_INT_TXB) !== 1'b1)
9739
      begin
9740
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
9741
        test_fail("Interrupt Transmit Buffer was not set");
9742
        fail = fail + 1;
9743
      end
9744
      if ((data & (~`ETH_INT_TXB)) !== 0)
9745
      begin
9746
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
9747
        test_fail("Other interrupts (except Transmit Buffer) were set");
9748
        fail = fail + 1;
9749
      end
9750
      // clear interrupts
9751
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9752
      // check WB INT signal
9753
      if (wb_int !== 1'b0)
9754
      begin
9755
        test_fail("WB INT signal should not be set");
9756
        fail = fail + 1;
9757
      end
9758
      // set length (LOOP variable)
9759
      if ((tmp_ipgt + 3) < 130) // tmp_ipgt < 124
9760
        i_length = i_length + 2;
9761
      else
9762
        i_length = (max_tmp - 4);
9763
      // set IPGT
9764
      if ((tmp_ipgt + 3) < 10)
9765
        tmp_ipgt = tmp_ipgt + 1;
9766
      else if ((tmp_ipgt + 3) < 24)
9767
        tmp_ipgt = tmp_ipgt + 7;
9768
      else if ((tmp_ipgt + 3) == 24)
9769
        tmp_ipgt = 38 - 3;
9770
      else if ((tmp_ipgt + 3) == 38)
9771
        tmp_ipgt = 72 - 3;
9772
      else if ((tmp_ipgt + 3) == 72)
9773
        tmp_ipgt = 130 - 3; // 124 - 3
9774
      // the number of frame transmitted
9775
      num_of_frames = num_of_frames + 1;
9776
      num_of_bd = 0;
9777
      @(posedge wb_clk);
9778
    end
9779
    // disable TX
9780
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
9781
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9782
    @(posedge wb_clk);
9783
    if(fail == 0)
9784
      test_ok;
9785
    else
9786
      fail = 0;
9787
  end
9788
 
9789
 
9790
  ////////////////////////////////////////////////////////////////////
9791
  ////                                                            ////
9792
  ////  Test transmit packets after TX under-run on each packet's ////
9793
  ////  byte at 2 TX buffer decriptors ( 10Mbps ).                ////
9794
  ////                                                            ////
9795
  ////////////////////////////////////////////////////////////////////
9796
  if (test_num == 20) // without padding
9797
  begin
9798
    // TEST 20: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 10Mbps )
9799
    test_name = "TEST 20: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 10Mbps )";
9800
    `TIME;
9801
    $display("  TEST 20: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 10Mbps )");
9802
 
9803
    // reset MAC registers
9804
    hard_reset;
9805
    // reset MAC and MII LOGIC with soft reset
9806
    reset_mac;
9807
    reset_mii;
9808
    // set wb slave response
9809
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
9810
 
9811
    max_tmp = 0;
9812
    min_tmp = 0;
9813
    // set 2 TX buffer descriptors - must be set before TX enable
9814
    wbm_write(`ETH_TX_BD_NUM, 32'h2, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9815
    // enable TX, set full-duplex mode, NO padding and CRC appending
9816
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN |/* `ETH_MODER_PAD |*/ `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
9817
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9818
    // prepare a packet of MAXFL length
9819
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9820
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
9821
    min_tmp = tmp[31:16];
9822
    st_data = 8'h99;
9823
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
9824
    // read IPG value
9825
    wbm_read(`ETH_IPGT, tmp_ipgt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9826
    // check WB INT signal
9827
    if (wb_int !== 1'b0)
9828
    begin
9829
      test_fail("WB INT signal should not be set");
9830
      fail = fail + 1;
9831
    end
9832
 
9833
    // write to phy's control register for 10Mbps
9834
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
9835
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
9836
    speed = 10;
9837
 
9838 223 tadejm
    num_of_frames = 40; // (0..3) => start under-run on first word
9839 209 tadejm
    num_of_bd = 0;
9840
    i_data = 3; // (3) => one BYTE read in first word - FIRST byte
9841
    i_length = (min_tmp + 4);
9842
    while (i_length < (max_tmp - 4))
9843
    begin
9844
      // Reset_tx_bd enable interrupt generation
9845
      // unmask interrupts
9846
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
9847
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9848
      // not detect carrier sense in FD and no collision
9849
      eth_phy.carrier_sense_tx_fd_detect(0);
9850
      eth_phy.collision(0);
9851
      // first destination address on ethernet PHY
9852
      eth_phy.set_tx_mem_addr(0);
9853
      // prepare BDs
9854
      if (num_of_bd == 0)
9855
      begin
9856
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_data[1:0]));
9857
        set_tx_bd(1, 1, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
9858
        set_tx_bd_wrap(1);
9859
        // set wb slave response: ACK (response), wbs_waits[2:0] (waits before response), 
9860
        //                       wbs_retries[7:0] (RTYs before ACK if RTY response selected)
9861 223 tadejm
        #1 wb_slave.cycle_response(`ACK_RESPONSE, 3'h0, 8'h0);
9862 209 tadejm
        set_tx_bd_ready(1, 1);
9863
        set_tx_bd_ready(0, 0);
9864
      end
9865
      // frame under-run checking
9866
      frame_started = 0;
9867
      frame_ended = 0;
9868
      wait_for_frame = 0;
9869
      fork
9870
        begin
9871
          // for every 4 frames bytes 1, 2, 3 and 4 respectively are read in first word => 1 ACK
9872
          // in other words 4 bytes are read, since length is MINFL => num_of_frames[31:2] ACKs
9873
          repeat ((num_of_frames[31:2] + 1'b1)) @(posedge eth_ma_wb_ack_i);
9874
          @(negedge eth_ma_wb_ack_i); // wait for last ACK to finish
9875
          // set wb slave response: ACK (response), wbs_waits[2:0] (waits before response), 
9876
          //                       wbs_retries[7:0] (RTYs before ACK if RTY response selected)
9877 223 tadejm
          #1 wb_slave.cycle_response(`NO_RESPONSE, 3'h0, 8'hFF);
9878 209 tadejm
          // wait for synchronization and some additional clocks
9879
          wait_for_frame = 1;
9880
          // wait for frame
9881
          wait ((wait_for_frame == 0) || (frame_started == 1))
9882
          if ((wait_for_frame == 0) && (frame_started == 0)) // frame didn't start
9883
          begin
9884
            disable check_fr;
9885
          end
9886
          else if ((wait_for_frame == 1) && (frame_started == 1)) // frame started
9887
          begin
9888
            disable wait_fr;
9889
            wait (frame_ended == 1);
9890
          end
9891
          repeat (2) @(posedge wb_clk);
9892
          // set wb slave response: ACK (response), wbs_waits[2:0] (waits before response), 
9893
          //                       wbs_retries[7:0] (RTYs before ACK if RTY response selected)
9894 223 tadejm
          wb_slave.cycle_response(`ACK_RESPONSE, 3'h0, 8'h0);
9895 209 tadejm
        end
9896
        begin: wait_fr
9897
          wait (wait_for_frame == 1)
9898
          begin
9899
            // wait for synchronization and some additional clocks
9900
            repeat (3) @(posedge wb_clk);
9901
            repeat (2 * tmp_ipgt) @(posedge mtx_clk);
9902
            repeat (2) @(posedge wb_clk);
9903
            repeat (2) @(posedge mtx_clk);
9904
            wait_for_frame = 0;
9905
          end
9906
        end
9907
        begin: check_fr
9908
          // wait for frame to start
9909
          @(posedge MTxEn);
9910
          frame_started = 1;
9911 223 tadejm
`TIME; $display("  Under-run (on %0d. byte) frame started", (num_of_frames + 1));
9912 209 tadejm
          // wait for frame to end due to under-run
9913
          @(negedge MTxEn);
9914
          frame_ended = 1;
9915 223 tadejm
`TIME; $display("  Under-run frame ended");
9916 209 tadejm
        end
9917
      join
9918
      // wait for first transmit to end, if under-run didn't happen
9919
      if (frame_ended == 0)
9920
      begin
9921
        // WAIT FOR FIRST TRANSMIT
9922
        check_tx_bd(num_of_bd, data);
9923
        wait (MTxEn === 1'b1); // start first transmit
9924
        if (data[15] !== 1)
9925
        begin
9926
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
9927
          fail = fail + 1;
9928
        end
9929
        wait (MTxEn === 1'b0); // end first transmit
9930
        while (data[15] === 1)
9931
        begin
9932
          check_tx_bd(num_of_bd, data);
9933
          @(posedge wb_clk);
9934
        end
9935
        repeat (1) @(posedge wb_clk);
9936
        // CHECK FIRST FRAME
9937
        // check length of a first PACKET
9938
        tmp_len = eth_phy.tx_len;
9939
        #1;
9940
        if (tmp_len != (i_length + 4))
9941
        begin
9942 223 tadejm
          `TIME; $display("*E Wrong length of first packet out from MAC");
9943
          test_fail("Wrong length of first packet out from MAC");
9944 209 tadejm
          fail = fail + 1;
9945
        end
9946
        // checking first packet
9947
        check_tx_packet((`MEMORY_BASE + i_data[1:0]), 0, (i_length), tmp);
9948
        if (tmp > 0)
9949
        begin
9950 223 tadejm
          `TIME; $display("*E Wrong data of first transmitted packet");
9951
          test_fail("Wrong data of first transmitted packet");
9952 209 tadejm
          fail = fail + 1;
9953
        end
9954
        // check first transmited TX packet CRC
9955
        check_tx_crc(0, (i_length), 1'b0, tmp); // length without CRC
9956
        if (tmp > 0)
9957
        begin
9958 223 tadejm
          `TIME; $display("*E Wrong CRC of first transmitted packet");
9959
          test_fail("Wrong CRC of first transmitted packet");
9960 209 tadejm
          fail = fail + 1;
9961
        end
9962
        // check WB INT signal
9963
        if (wb_int !== 1'b1)
9964
        begin
9965
          `TIME; $display("*E WB INT signal should be set");
9966
          test_fail("WB INT signal should be set");
9967
          fail = fail + 1;
9968
        end
9969
        // check TX buffer descriptor of a packet
9970
        check_tx_bd(num_of_bd, data);
9971
        if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
9972
             ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
9973
        begin
9974
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
9975
          test_fail("TX buffer descriptor status is not correct");
9976
          fail = fail + 1;
9977
        end
9978
        // check interrupts
9979
        wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9980
        if ((data & `ETH_INT_TXB) !== 1'b1)
9981
        begin
9982
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
9983
          test_fail("Interrupt Transmit Buffer was not set");
9984
          fail = fail + 1;
9985
        end
9986
        if ((data & (~`ETH_INT_TXB)) !== 0)
9987
        begin
9988
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
9989
          test_fail("Other interrupts (except Transmit Buffer) were set");
9990
          fail = fail + 1;
9991
        end
9992
        // clear interrupts
9993
        wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
9994
        // check WB INT signal
9995
        if (wb_int !== 1'b0)
9996
        begin
9997
          test_fail("WB INT signal should not be set");
9998
          fail = fail + 1;
9999
        end
10000
      end
10001 223 tadejm
      else
10002
      begin
10003
        // CHECK FIRST FRAME
10004
        // check length of a first PACKET
10005
        tmp_len = eth_phy.tx_len_err;
10006
        #1;
10007
        if (tmp_len != (num_of_frames + (4 - i_data)))
10008
        begin
10009
          `TIME; $display("*E Wrong length of first packet out from MAC");
10010
          test_fail("Wrong length of first packet out from MAC");
10011
          fail = fail + 1;
10012
        end
10013
        // checking first packet
10014
        check_tx_packet((`MEMORY_BASE + i_data[1:0]), 0, (num_of_frames), tmp);
10015
        if (tmp > 0)
10016
        begin
10017
          `TIME; $display("*E Wrong data of first transmitted packet");
10018
          test_fail("Wrong data of first transmitted packet");
10019
          fail = fail + 1;
10020
        end
10021
        // check WB INT signal
10022
        if (wb_int !== 1'b1)
10023
        begin
10024
          `TIME; $display("*E WB INT signal should be set");
10025
          test_fail("WB INT signal should be set");
10026
          fail = fail + 1;
10027
        end
10028
        // check TX buffer descriptor of a packet
10029
        check_tx_bd(num_of_bd, data);
10030
        if ( ((data[15:0] !== 16'h7900) && (num_of_bd == 1)) || // under-run, wrap bit
10031
             ((data[15:0] !== 16'h5900) && (num_of_bd < 1)) )   // under-run, without wrap bit
10032
        begin
10033
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
10034
          test_fail("TX buffer descriptor status is not correct");
10035
          fail = fail + 1;
10036
        end
10037
        // check interrupts
10038
        wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10039
        if ((data & `ETH_INT_TXE) !== 2'b10)
10040
        begin
10041
          `TIME; $display("*E Interrupt Transmit Error was not set, interrupt reg: %0h", data);
10042
          test_fail("Interrupt Transmit Buffer was not set");
10043
          fail = fail + 1;
10044
        end
10045
        if ((data & (~`ETH_INT_TXE)) !== 0)
10046
        begin
10047
          `TIME; $display("*E Other interrupts (except Transmit Error) were set, interrupt reg: %0h", data);
10048
          test_fail("Other interrupts (except Transmit Buffer) were set");
10049
          fail = fail + 1;
10050
        end
10051
        // clear interrupts
10052
        wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10053
        // check WB INT signal
10054
        if (wb_int !== 1'b0)
10055
        begin
10056
          test_fail("WB INT signal should not be set");
10057
          fail = fail + 1;
10058
        end
10059
      end
10060 209 tadejm
      num_of_bd = num_of_bd + 1;
10061
      // destination address on ethernet PHY
10062
      eth_phy.set_tx_mem_addr(0);
10063 223 tadejm
      // WAIT FOR SECOND TRANSMIT
10064 209 tadejm
      check_tx_bd(num_of_bd, data);
10065
      wait (MTxEn === 1'b1); // start first transmit
10066
      if (data[15] !== 1)
10067
      begin
10068
        test_fail("Wrong buffer descriptor's ready bit read out from MAC");
10069
        fail = fail + 1;
10070
      end
10071
      wait (MTxEn === 1'b0); // end first transmit
10072
      while (data[15] === 1)
10073
      begin
10074
        check_tx_bd(num_of_bd, data);
10075
        @(posedge wb_clk);
10076
      end
10077
      repeat (1) @(posedge wb_clk);
10078
      // CHECK SECOND FRAME
10079
      // check length of a second PACKET
10080 223 tadejm
if (frame_ended == 1'b1)
10081
begin
10082
`TIME; $display("  Second frame after under-run ended");
10083
end
10084 209 tadejm
      tmp_len = eth_phy.tx_len;
10085
      #1;
10086
      if (tmp_len != (i_length + 4))
10087
      begin
10088 223 tadejm
        `TIME; $display("*E Wrong length of second packet out from MAC");
10089 209 tadejm
        test_fail("Wrong length of second packet out from MAC");
10090
        fail = fail + 1;
10091
      end
10092
      // checking second packet
10093
      check_tx_packet(`MEMORY_BASE, 0, (i_length), tmp);
10094
      if (tmp > 0)
10095
      begin
10096 223 tadejm
        `TIME; $display("*E Wrong data of second transmitted packet");
10097 209 tadejm
        test_fail("Wrong data of second transmitted packet");
10098
        fail = fail + 1;
10099
      end
10100
      // check second transmited TX packet CRC
10101
      check_tx_crc(0, (i_length), 1'b0, tmp); // length without CRC
10102
      if (tmp > 0)
10103
      begin
10104 223 tadejm
        `TIME; $display("*E Wrong CRC of second transmitted packet");
10105 209 tadejm
        test_fail("Wrong CRC of second transmitted packet");
10106
        fail = fail + 1;
10107
      end
10108
      // check WB INT signal
10109
      if (wb_int !== 1'b1)
10110
      begin
10111
        `TIME; $display("*E WB INT signal should be set");
10112
        test_fail("WB INT signal should be set");
10113
        fail = fail + 1;
10114
      end
10115
      // check TX buffer descriptor of a packet
10116
      check_tx_bd(num_of_bd, data);
10117
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
10118
           ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
10119
      begin
10120
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
10121
        test_fail("TX buffer descriptor status is not correct");
10122
        fail = fail + 1;
10123
      end
10124
      // check interrupts
10125
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10126
      if ((data & `ETH_INT_TXB) !== 1'b1)
10127
      begin
10128
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
10129
        test_fail("Interrupt Transmit Buffer was not set");
10130
        fail = fail + 1;
10131
      end
10132
      if ((data & (~`ETH_INT_TXB)) !== 0)
10133
      begin
10134
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
10135
        test_fail("Other interrupts (except Transmit Buffer) were set");
10136
        fail = fail + 1;
10137
      end
10138
      // clear interrupts
10139
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10140
      // check WB INT signal
10141
      if (wb_int !== 1'b0)
10142
      begin
10143
        test_fail("WB INT signal should not be set");
10144
        fail = fail + 1;
10145
      end
10146
      // set initial value
10147
      i_data = i_data - 1;
10148
      // the number of frame transmitted
10149
      num_of_frames = num_of_frames + 1;
10150
      num_of_bd = 0;
10151
      // set length (LOOP variable)
10152 223 tadejm
      if (num_of_frames == i_length + 4) // 64 => this was last Byte (1st .. 64th) when i_length = min_tmp - 4
10153 209 tadejm
        i_length = (max_tmp - 4);
10154
      @(posedge wb_clk);
10155
    end
10156
    // disable TX
10157
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
10158
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10159
    @(posedge wb_clk);
10160
    if(fail == 0)
10161
      test_ok;
10162
    else
10163
      fail = 0;
10164
  end
10165
 
10166
 
10167
  ////////////////////////////////////////////////////////////////////
10168
  ////                                                            ////
10169
  ////  Test transmit packets after TX under-run on each packet's ////
10170
  ////  byte at 2 TX buffer decriptors ( 100Mbps ).               ////
10171
  ////                                                            ////
10172
  ////////////////////////////////////////////////////////////////////
10173
  if (test_num == 21) // without padding
10174
  begin
10175
    // TEST 21: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 100Mbps )
10176
    test_name = "TEST 21: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 100Mbps )";
10177
    `TIME;
10178
    $display("  TEST 21: TRANSMIT PACKETS AFTER TX UNDER-RUN ON EACH PACKET's BYTE AT 2 TX BDs ( 100Mbps )");
10179
 
10180
    // reset MAC registers
10181
    hard_reset;
10182
    // reset MAC and MII LOGIC with soft reset
10183
    reset_mac;
10184
    reset_mii;
10185
    // set wb slave response
10186
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
10187
 
10188
    max_tmp = 0;
10189
    min_tmp = 0;
10190
    // set 2 TX buffer descriptors - must be set before TX enable
10191
    wbm_write(`ETH_TX_BD_NUM, 32'h2, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10192
    // enable TX, set full-duplex mode, NO padding and CRC appending
10193
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN |/* `ETH_MODER_PAD |*/ `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
10194
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10195
    // prepare a packet of MAXFL length
10196
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10197
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
10198
    min_tmp = tmp[31:16];
10199
    st_data = 8'h99;
10200
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC
10201
    // read IPG value
10202
    wbm_read(`ETH_IPGT, tmp_ipgt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10203
    // check WB INT signal
10204
    if (wb_int !== 1'b0)
10205
    begin
10206
      test_fail("WB INT signal should not be set");
10207
      fail = fail + 1;
10208
    end
10209
 
10210
    // write to phy's control register for 100Mbps
10211
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
10212
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
10213
    speed = 100;
10214
 
10215
    num_of_frames = 0; // (0..3) => start under-run on first word
10216
    num_of_bd = 0;
10217
    i_data = 3; // (3) => one BYTE read in first word - FIRST byte
10218
    i_length = (min_tmp + 4);
10219
    while (i_length < (max_tmp - 4))
10220
    begin
10221
      // Reset_tx_bd enable interrupt generation
10222
      // unmask interrupts
10223
      wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
10224
                               `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10225
      // not detect carrier sense in FD and no collision
10226
      eth_phy.carrier_sense_tx_fd_detect(0);
10227
      eth_phy.collision(0);
10228
      // first destination address on ethernet PHY
10229
      eth_phy.set_tx_mem_addr(0);
10230
      // prepare BDs
10231
      if (num_of_bd == 0)
10232
      begin
10233
        set_tx_bd(0, 0, i_length, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE + i_data[1:0]));
10234
        set_tx_bd(1, 1, i_length, 1'b1, 1'b1, 1'b1, `MEMORY_BASE);
10235
        set_tx_bd_wrap(1);
10236
        // set wb slave response: ACK (response), wbs_waits[2:0] (waits before response), 
10237
        //                       wbs_retries[7:0] (RTYs before ACK if RTY response selected)
10238
        #1 wb_slave.cycle_response(`ACK_RESPONSE, 3'h2, 8'h0);
10239
        set_tx_bd_ready(1, 1);
10240
        set_tx_bd_ready(0, 0);
10241
      end
10242
      // frame under-run checking
10243
      frame_started = 0;
10244
      frame_ended = 0;
10245
      wait_for_frame = 0;
10246
      fork
10247
        begin
10248
          // for every 4 frames bytes 1, 2, 3 and 4 respectively are read in first word => 1 ACK
10249
          // in other words 4 bytes are read, since length is MINFL => num_of_frames[31:2] ACKs
10250
          repeat ((num_of_frames[31:2] + 1'b1)) @(posedge eth_ma_wb_ack_i);
10251
          @(negedge eth_ma_wb_ack_i); // wait for last ACK to finish
10252
          // set wb slave response: ACK (response), wbs_waits[2:0] (waits before response), 
10253
          //                       wbs_retries[7:0] (RTYs before ACK if RTY response selected)
10254
          #1 wb_slave.cycle_response(`NO_RESPONSE, 3'h7, 8'hFF);
10255
          // wait for synchronization and some additional clocks
10256
          wait_for_frame = 1;
10257
          // wait for frame
10258
          wait ((wait_for_frame == 0) || (frame_started == 1))
10259
          if ((wait_for_frame == 0) && (frame_started == 0)) // frame didn't start
10260
          begin
10261
            disable check_fr1;
10262
          end
10263
          else if ((wait_for_frame == 1) && (frame_started == 1)) // frame started
10264
          begin
10265
            disable wait_fr1;
10266
            wait (frame_ended == 1);
10267
          end
10268
          repeat (2) @(posedge wb_clk);
10269
          // set wb slave response: ACK (response), wbs_waits[2:0] (waits before response), 
10270
          //                       wbs_retries[7:0] (RTYs before ACK if RTY response selected)
10271
          wb_slave.cycle_response(`ACK_RESPONSE, 3'h2, 8'h0);
10272
        end
10273
        begin: wait_fr1
10274
          wait (wait_for_frame == 1)
10275
          begin
10276
            // wait for synchronization and some additional clocks
10277
            repeat (3) @(posedge wb_clk);
10278
            repeat (2 * tmp_ipgt) @(posedge mtx_clk);
10279
            repeat (2) @(posedge wb_clk);
10280
            repeat (2) @(posedge mtx_clk);
10281
            wait_for_frame = 0;
10282
          end
10283
        end
10284
        begin: check_fr1
10285
          // wait for frame to start
10286
          @(posedge MTxEn);
10287
          frame_started = 1;
10288
$display("  Under-run (on %0d. byte) frame started", (num_of_frames + 1));
10289
          // wait for frame to end due to under-run
10290
          @(negedge MTxEn);
10291
          frame_ended = 1;
10292
$display("  Under-run frame ended");
10293
        end
10294
      join
10295
      // wait for first transmit to end, if under-run didn't happen
10296
      if (frame_ended == 0)
10297
      begin
10298
        // WAIT FOR FIRST TRANSMIT
10299
        check_tx_bd(num_of_bd, data);
10300
        wait (MTxEn === 1'b1); // start first transmit
10301
        if (data[15] !== 1)
10302
        begin
10303
          test_fail("Wrong buffer descriptor's ready bit read out from MAC");
10304
          fail = fail + 1;
10305
        end
10306
        wait (MTxEn === 1'b0); // end first transmit
10307
        while (data[15] === 1)
10308
        begin
10309
          check_tx_bd(num_of_bd, data);
10310
          @(posedge wb_clk);
10311
        end
10312
        repeat (1) @(posedge wb_clk);
10313
        // CHECK FIRST FRAME
10314
        // check length of a first PACKET
10315
        tmp_len = eth_phy.tx_len;
10316
        #1;
10317
        if (tmp_len != (i_length + 4))
10318
        begin
10319
          test_fail("Wrong length of second packet out from MAC");
10320
          fail = fail + 1;
10321
        end
10322
        // checking first packet
10323
        check_tx_packet((`MEMORY_BASE + i_data[1:0]), 0, (i_length), tmp);
10324
        if (tmp > 0)
10325
        begin
10326
          test_fail("Wrong data of second transmitted packet");
10327
          fail = fail + 1;
10328
        end
10329
        // check first transmited TX packet CRC
10330
        check_tx_crc(0, (i_length), 1'b0, tmp); // length without CRC
10331
        if (tmp > 0)
10332
        begin
10333
          test_fail("Wrong CRC of second transmitted packet");
10334
          fail = fail + 1;
10335
        end
10336
        // check WB INT signal
10337
        if (wb_int !== 1'b1)
10338
        begin
10339
          `TIME; $display("*E WB INT signal should be set");
10340
          test_fail("WB INT signal should be set");
10341
          fail = fail + 1;
10342
        end
10343
        // check TX buffer descriptor of a packet
10344
        check_tx_bd(num_of_bd, data);
10345
        if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
10346
             ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
10347
        begin
10348
          `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
10349
          test_fail("TX buffer descriptor status is not correct");
10350
          fail = fail + 1;
10351
        end
10352
        // check interrupts
10353
        wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10354
        if ((data & `ETH_INT_TXB) !== 1'b1)
10355
        begin
10356
          `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
10357
          test_fail("Interrupt Transmit Buffer was not set");
10358
          fail = fail + 1;
10359
        end
10360
        if ((data & (~`ETH_INT_TXB)) !== 0)
10361
        begin
10362
          `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
10363
          test_fail("Other interrupts (except Transmit Buffer) were set");
10364
          fail = fail + 1;
10365
        end
10366
        // clear interrupts
10367
        wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10368
        // check WB INT signal
10369
        if (wb_int !== 1'b0)
10370
        begin
10371
          test_fail("WB INT signal should not be set");
10372
          fail = fail + 1;
10373
        end
10374
      end
10375
      num_of_bd = num_of_bd + 1;
10376
      // destination address on ethernet PHY
10377
      eth_phy.set_tx_mem_addr(0);
10378
      // WAIT FOR FIRST TRANSMIT
10379
      check_tx_bd(num_of_bd, data);
10380
      wait (MTxEn === 1'b1); // start first transmit
10381
      if (data[15] !== 1)
10382
      begin
10383
        test_fail("Wrong buffer descriptor's ready bit read out from MAC");
10384
        fail = fail + 1;
10385
      end
10386
      wait (MTxEn === 1'b0); // end first transmit
10387
      while (data[15] === 1)
10388
      begin
10389
        check_tx_bd(num_of_bd, data);
10390
        @(posedge wb_clk);
10391
      end
10392
      repeat (1) @(posedge wb_clk);
10393
      // CHECK SECOND FRAME
10394
      // check length of a second PACKET
10395
      tmp_len = eth_phy.tx_len;
10396
      #1;
10397
      if (tmp_len != (i_length + 4))
10398
      begin
10399
        test_fail("Wrong length of second packet out from MAC");
10400
        fail = fail + 1;
10401
      end
10402
      // checking second packet
10403
      check_tx_packet(`MEMORY_BASE, 0, (i_length), tmp);
10404
      if (tmp > 0)
10405
      begin
10406
        test_fail("Wrong data of second transmitted packet");
10407
        fail = fail + 1;
10408
      end
10409
      // check second transmited TX packet CRC
10410
      check_tx_crc(0, (i_length), 1'b0, tmp); // length without CRC
10411
      if (tmp > 0)
10412
      begin
10413
        test_fail("Wrong CRC of second transmitted packet");
10414
        fail = fail + 1;
10415
      end
10416
      // check WB INT signal
10417
      if (wb_int !== 1'b1)
10418
      begin
10419
        `TIME; $display("*E WB INT signal should be set");
10420
        test_fail("WB INT signal should be set");
10421
        fail = fail + 1;
10422
      end
10423
      // check TX buffer descriptor of a packet
10424
      check_tx_bd(num_of_bd, data);
10425
      if ( ((data[15:0] !== 16'h7800) && (num_of_bd == 1)) || // wrap bit
10426
           ((data[15:0] !== 16'h5800) && (num_of_bd < 1)) )   // without wrap bit
10427
      begin
10428
        `TIME; $display("*E TX buffer descriptor status is not correct: %0h", data[15:0]);
10429
        test_fail("TX buffer descriptor status is not correct");
10430
        fail = fail + 1;
10431
      end
10432
      // check interrupts
10433
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10434
      if ((data & `ETH_INT_TXB) !== 1'b1)
10435
      begin
10436
        `TIME; $display("*E Interrupt Transmit Buffer was not set, interrupt reg: %0h", data);
10437
        test_fail("Interrupt Transmit Buffer was not set");
10438
        fail = fail + 1;
10439
      end
10440
      if ((data & (~`ETH_INT_TXB)) !== 0)
10441
      begin
10442
        `TIME; $display("*E Other interrupts (except Transmit Buffer) were set, interrupt reg: %0h", data);
10443
        test_fail("Other interrupts (except Transmit Buffer) were set");
10444
        fail = fail + 1;
10445
      end
10446
      // clear interrupts
10447
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10448
      // check WB INT signal
10449
      if (wb_int !== 1'b0)
10450
      begin
10451
        test_fail("WB INT signal should not be set");
10452
        fail = fail + 1;
10453
      end
10454
      // set initial value
10455
      i_data = i_data - 1;
10456
      // the number of frame transmitted
10457
      num_of_frames = num_of_frames + 1;
10458
      num_of_bd = 0;
10459
      // set length (LOOP variable)
10460
      if (num_of_frames == i_length + 4) // 64 => this vas last Byte (1st .. 64th) when i_length = min_tmp - 4
10461
        i_length = (max_tmp - 4);
10462
      @(posedge wb_clk);
10463
    end
10464
    // disable TX
10465
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
10466
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10467
    @(posedge wb_clk);
10468
    if(fail == 0)
10469
      test_ok;
10470
    else
10471
      fail = 0;
10472
  end
10473
 
10474 194 tadej
end   //  for (test_num=start_task; test_num <= end_task; test_num=test_num+1)
10475 169 mohor
 
10476
end
10477
endtask // test_mac_full_duplex_transmit
10478
 
10479
 
10480 209 tadejm
task test_mac_full_duplex_receive;
10481
  input  [31:0]  start_task;
10482
  input  [31:0]  end_task;
10483
  integer        bit_start_1;
10484
  integer        bit_end_1;
10485
  integer        bit_start_2;
10486
  integer        bit_end_2;
10487
  integer        num_of_reg;
10488
  integer        num_of_frames;
10489
  integer        num_of_bd;
10490
  integer        i_addr;
10491
  integer        i_data;
10492
  integer        i_length;
10493
  integer        tmp_len;
10494
  integer        tmp_bd;
10495
  integer        tmp_bd_num;
10496
  integer        tmp_data;
10497
  integer        tmp_ipgt;
10498
  integer        test_num;
10499
  reg    [31:0]  tx_bd_num;
10500
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_data;
10501
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_tmp_data;
10502
  integer        i;
10503
  integer        i1;
10504
  integer        i2;
10505
  integer        i3;
10506
  integer        fail;
10507
  integer        speed;
10508
  reg            frame_started;
10509
  reg            frame_ended;
10510
  reg            wait_for_frame;
10511 243 tadejm
  reg            check_frame;
10512
  reg            stop_checking_frame;
10513
  reg            first_fr_received;
10514 209 tadejm
  reg    [31:0]  addr;
10515
  reg    [31:0]  data;
10516
  reg    [31:0]  tmp;
10517
  reg    [ 7:0]  st_data;
10518
  reg    [15:0]  max_tmp;
10519
  reg    [15:0]  min_tmp;
10520
begin
10521
// MAC FULL DUPLEX RECEIVE TEST
10522
test_heading("MAC FULL DUPLEX RECEIVE TEST");
10523
$display(" ");
10524
$display("MAC FULL DUPLEX RECEIVE TEST");
10525
fail = 0;
10526
 
10527
// reset MAC registers
10528
hard_reset;
10529
// reset MAC and MII LOGIC with soft reset
10530
reset_mac;
10531
reset_mii;
10532
// set wb slave response
10533
wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
10534
 
10535
  /*
10536
  TASKS for set and control TX buffer descriptors (also send packet - set_tx_bd_ready):
10537
  -------------------------------------------------------------------------------------
10538
  set_tx_bd
10539
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0], len[15:0], irq, pad, crc, txpnt[31:0]);
10540
  set_tx_bd_wrap
10541
    (tx_bd_num_end[6:0]);
10542
  set_tx_bd_ready
10543
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
10544
  check_tx_bd
10545
    (tx_bd_num_start[6:0], tx_bd_status[31:0]);
10546
  clear_tx_bd
10547
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
10548
 
10549
  TASKS for set and control RX buffer descriptors:
10550
  ------------------------------------------------
10551
  set_rx_bd
10552
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0], irq, rxpnt[31:0]);
10553
  set_rx_bd_wrap
10554
    (rx_bd_num_end[6:0]);
10555
  set_rx_bd_empty
10556
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
10557
  check_rx_bd
10558
    (rx_bd_num_end[6:0], rx_bd_status);
10559
  clear_rx_bd
10560
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
10561
 
10562
  TASKS for set and check TX packets:
10563
  -----------------------------------
10564
  set_tx_packet
10565
    (txpnt[31:0], len[15:0], eth_start_data[7:0]);
10566
  check_tx_packet
10567
    (txpnt_wb[31:0], txpnt_phy[31:0], len[15:0], failure[31:0]);
10568
 
10569
  TASKS for set and check RX packets:
10570
  -----------------------------------
10571
  set_rx_packet
10572
    (rxpnt[31:0], len[15:0], plus_nibble, d_addr[47:0], s_addr[47:0], type_len[15:0], start_data[7:0]);
10573
  check_rx_packet
10574
    (rxpnt_phy[31:0], rxpnt_wb[31:0], len[15:0], plus_nibble, successful_nibble, failure[31:0]);
10575
 
10576
  TASKS for append and check CRC to/of TX packet:
10577
  -----------------------------------------------
10578
  append_tx_crc
10579
    (txpnt_wb[31:0], len[15:0], negated_crc);
10580
  check_tx_crc
10581
    (txpnt_phy[31:0], len[15:0], negated_crc, failure[31:0]);
10582
 
10583
  TASK for append CRC to RX packet (CRC is checked together with check_rx_packet):
10584
  --------------------------------------------------------------------------------
10585
  append_rx_crc
10586
    (rxpnt_phy[31:0], len[15:0], plus_nibble, negated_crc);
10587
  */
10588
 
10589
//////////////////////////////////////////////////////////////////////
10590
////                                                              ////
10591
////  test_mac_full_duplex_receive:                               ////
10592
////                                                              ////
10593
////  0: Test no receive when all buffers are TX ( 10Mbps ).      ////
10594
////  1: Test no receive when all buffers are TX ( 100Mbps ).     ////
10595 243 tadejm
////  2: Test receive packet synchronization with receive         ////
10596
////     disable/enable ( 10Mbps ).                               ////
10597
////  3: Test receive packet synchronization with receive         ////
10598
////     disable/enable ( 100Mbps ).                              ////
10599
////  4: Test receive packets form MINFL to MAXFL sizes at        ////
10600 209 tadejm
////     one RX buffer decriptor ( 10Mbps ).                      ////
10601 243 tadejm
////  5: Test receive packets form MINFL to MAXFL sizes at        ////
10602 209 tadejm
////     one RX buffer decriptor ( 100Mbps ).                     ////
10603
////                                                              ////
10604
//////////////////////////////////////////////////////////////////////
10605
for (test_num = start_task; test_num <= end_task; test_num = test_num + 1)
10606
begin
10607
 
10608
  ////////////////////////////////////////////////////////////////////
10609
  ////                                                            ////
10610
  ////  Test no receive when all buffers are TX ( 10Mbps ).       ////
10611
  ////                                                            ////
10612
  ////////////////////////////////////////////////////////////////////
10613
  if (test_num == 0) // Test no receive when all buffers are TX ( 10Mbps ).
10614
  begin
10615
    // TEST 0: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 10Mbps )
10616
    test_name   = "TEST 0: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 10Mbps )";
10617
    `TIME; $display("  TEST 0: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 10Mbps )");
10618
 
10619
    // unmask interrupts
10620
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
10621
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10622
    // set all buffer descriptors to TX - must be set before RX enable
10623
    wbm_write(`ETH_TX_BD_NUM, 32'h80, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10624 223 tadejm
    // enable RX, set full-duplex mode, receive small, NO correct IFG
10625 209 tadejm
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
10626
              `ETH_MODER_PRO | `ETH_MODER_BRO,
10627
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10628
 
10629
    // write to phy's control register for 10Mbps
10630
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
10631
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
10632
    speed = 10;
10633
 
10634
    i = 0;
10635
    while (i < 128)
10636
    begin
10637
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
10638
      begin
10639
        set_rx_packet((i1 * 50), 10, 1'b0, 48'h1234_5678_8765, 48'h0011_2233_4455, 16'h0101, 8'h0);
10640
        append_rx_crc((i1 * 50), 10, 1'b0, 1'b0);
10641
        set_rx_bd(i1, i1, 1'b1, (`MEMORY_BASE + (i1 * 50)));
10642
      end
10643
      set_rx_bd_wrap(i);
10644
      set_rx_bd_empty(0, i);
10645
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
10646
      begin
10647
        #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, (i1 * 50), 14, 1'b0);
10648
        repeat(10) @(posedge mrx_clk);
10649
      end
10650
      @(posedge mrx_clk);
10651
      for (i2 = 0; i2 < 20; i2 = i2 + 1)
10652
      begin
10653
        check_rx_bd(0, tmp);
10654
        #1;
10655
        if (tmp[15] === 1'b0)
10656
        begin
10657
          test_fail("Receive should not start at all");
10658
          fail = fail + 1;
10659
          `TIME; $display("*E Receive of %d packets should not start at all - empty is 0", i);
10660
        end
10661
        if (tmp[7:0] !== 0)
10662
        begin
10663
          test_fail("Receive should not be finished since it should not start at all");
10664
          fail = fail + 1;
10665
          `TIME; $display("*E Receive of should not be finished since it should not start at all");
10666
        end
10667
        @(posedge wb_clk);
10668
      end
10669
      wbm_read(`ETH_INT, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10670
      if (tmp[6:0] !== 0)
10671
      begin
10672
        test_fail("Receive should not get INT since it should not start at all");
10673
        fail = fail + 1;
10674
        `TIME; $display("*E Receive of should not get INT since it should not start at all");
10675
      end
10676
      clear_rx_bd(0, i);
10677
      if ((i < 5) || (i > 124))
10678
        i = i + 1;
10679
      else
10680
        i = i + 120;
10681
    end
10682
    // disable RX
10683
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
10684
              `ETH_MODER_PRO | `ETH_MODER_BRO,
10685
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10686
    if(fail == 0)
10687
      test_ok;
10688
    else
10689
      fail = 0;
10690
  end
10691
 
10692
 
10693
  ////////////////////////////////////////////////////////////////////
10694
  ////                                                            ////
10695
  ////  Test no receive when all buffers are TX ( 100Mbps ).      ////
10696
  ////                                                            ////
10697
  ////////////////////////////////////////////////////////////////////
10698
  if (test_num == 1) // Test no receive when all buffers are TX ( 100Mbps ).
10699
  begin
10700
    // TEST 1: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 100Mbps )
10701
    test_name   = "TEST 1: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 100Mbps )";
10702
    `TIME; $display("  TEST 1: NO RECEIVE WHEN ALL BUFFERS ARE TX ( 100Mbps )");
10703
 
10704
    // unmask interrupts
10705
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
10706
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10707
    // set all buffer descriptors to TX - must be set before RX enable
10708
    wbm_write(`ETH_TX_BD_NUM, 32'h80, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10709 223 tadejm
    // enable RX, set full-duplex mode, receive small, NO correct IFG
10710 209 tadejm
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
10711
              `ETH_MODER_PRO | `ETH_MODER_BRO,
10712
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10713
 
10714
    // write to phy's control register for 100Mbps
10715
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
10716
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
10717
    speed = 100;
10718
 
10719
    i = 0;
10720
    while (i < 128)
10721
    begin
10722
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
10723
      begin
10724
        set_rx_packet((i1 * 50), 10, 1'b0, 48'h1234_5678_8765, 48'h0011_2233_4455, 16'h0101, 8'h0);
10725
        append_rx_crc((i1 * 50), 10, 1'b0, 1'b0);
10726
        set_rx_bd(i1, i1, 1'b1, (`MEMORY_BASE + (i1 * 50)));
10727
      end
10728
      set_rx_bd_wrap(i);
10729
      set_rx_bd_empty(0, i);
10730
      for (i1 = 0; i1 <= i; i1 = i1 + 1)
10731
      begin
10732
        #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, (i1 * 50), 14, 1'b0);
10733
        repeat(10) @(posedge mrx_clk);
10734
      end
10735
      @(posedge mrx_clk);
10736
      for (i2 = 0; i2 < 20; i2 = i2 + 1)
10737
      begin
10738
        check_rx_bd(0, tmp);
10739
        #1;
10740
        if (tmp[15] === 1'b0)
10741
        begin
10742
          test_fail("Receive should not start at all");
10743
          fail = fail + 1;
10744
          `TIME; $display("*E Receive of %d packets should not start at all - empty is 0", i);
10745
        end
10746
        if (tmp[7:0] !== 0)
10747
        begin
10748
          test_fail("Receive should not be finished since it should not start at all");
10749
          fail = fail + 1;
10750
          `TIME; $display("*E Receive of should not be finished since it should not start at all");
10751
        end
10752
        @(posedge wb_clk);
10753
      end
10754
      wbm_read(`ETH_INT, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10755
      if (tmp[6:0] !== 0)
10756
      begin
10757
        test_fail("Receive should not get INT since it should not start at all");
10758
        fail = fail + 1;
10759
        `TIME; $display("*E Receive of should not get INT since it should not start at all");
10760
      end
10761
      clear_rx_bd(0, i);
10762
      if ((i < 5) || (i > 124))
10763
        i = i + 1;
10764
      else
10765
        i = i + 120;
10766
    end
10767
    // disable RX
10768
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
10769
              `ETH_MODER_PRO | `ETH_MODER_BRO,
10770
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10771
    if(fail == 0)
10772
      test_ok;
10773
    else
10774
      fail = 0;
10775
  end
10776
 
10777
 
10778
  ////////////////////////////////////////////////////////////////////
10779
  ////                                                            ////
10780 243 tadejm
  ////  Test receive packet synchronization with receive          ////
10781
  ////  disable/enable ( 10Mbps ).                                ////
10782
  ////                                                            ////
10783
  ////////////////////////////////////////////////////////////////////
10784
  if (test_num == 2) // Test no receive when all buffers are TX ( 10Mbps ).
10785
  begin
10786
    // TEST 2: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )
10787
    test_name   = "TEST 2: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )";
10788
    `TIME; $display("  TEST 2: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )");
10789
 
10790
    // unmask interrupts
10791
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
10792
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10793
    // set 10 RX buffer descriptor (8'h80 - 8'hA) - must be set before RX enable
10794
    wbm_write(`ETH_TX_BD_NUM, 32'h76, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10795
    // disable RX, set full-duplex mode, NO receive small, NO correct IFG
10796
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_IFG |
10797
              `ETH_MODER_PRO | `ETH_MODER_BRO,
10798
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10799
    // prepare two packets of MAXFL length
10800
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
10801
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
10802
    min_tmp = tmp[31:16];
10803
    st_data = 8'h0F;
10804
    set_rx_packet(0, (min_tmp + 1), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
10805
    append_rx_crc(0, (min_tmp + 1), 1'b0, 1'b0);
10806
    st_data = 8'h1A;
10807
    set_rx_packet(max_tmp, (min_tmp + 1), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
10808
    append_rx_crc(max_tmp, (min_tmp + 1), 1'b0, 1'b0);
10809
    // check WB INT signal
10810
    if (wb_int !== 1'b0)
10811
    begin
10812
      test_fail("WB INT signal should not be set");
10813
      fail = fail + 1;
10814
    end
10815
 
10816
    // write to phy's control register for 10Mbps
10817
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
10818
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
10819
    speed = 10;
10820
 
10821
    frame_started = 0;
10822
    frame_ended = 0;
10823
    wait_for_frame = 0;
10824
    check_frame = 0;
10825
    stop_checking_frame = 0;
10826
    first_fr_received = 1; // at the beginning the first frame of each two will be received!
10827
 
10828
    num_of_frames = 0; // 
10829
    num_of_bd = 0;
10830
    i_length = (min_tmp + 1); // 5 bytes longer than MINFL
10831
    while (i_length < (max_tmp - 4))
10832
    begin
10833
      // choose generating carrier sense and collision 
10834
      case (num_of_frames[1:0])
10835
      2'h0: // Interrupt is generated
10836
      begin
10837
        // enable interrupt generation
10838
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
10839
        // not detect carrier sense in FD and no collision
10840
        eth_phy.no_carrier_sense_rx_fd_detect(0);
10841
        eth_phy.collision(0);
10842
      end
10843
      2'h1: // Interrupt is generated
10844
      begin
10845
        // enable interrupt generation
10846
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
10847
        // detect carrier sense in FD and no collision
10848
        eth_phy.no_carrier_sense_rx_fd_detect(1);
10849
        eth_phy.collision(0);
10850
      end
10851
      2'h2: // Interrupt is generated
10852
      begin
10853
        // disable interrupt generation
10854
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
10855
        // not detect carrier sense in FD and set collision
10856
        eth_phy.no_carrier_sense_rx_fd_detect(0);
10857
        eth_phy.collision(1);
10858
      end
10859
      default: // 2'h3: // Interrupt is generated
10860
      begin
10861
        // disable interrupt generation
10862
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
10863
        // detect carrier sense in FD and set collision
10864
        eth_phy.no_carrier_sense_rx_fd_detect(1);
10865
        eth_phy.collision(1);
10866
      end
10867
      endcase
10868
      // set wrap bit
10869
      set_rx_bd_wrap(118);
10870
      set_rx_bd_empty(118, 118);
10871
      check_frame = 0;
10872
      stop_checking_frame = 0;
10873
      tmp_data = 0;
10874
      fork
10875
        begin // enable RX MAC on first of each two packets - every second should be recived normaly
10876
          if (num_of_frames[0] == 1'b0)
10877
          begin
10878
            repeat(1) @(posedge wb_clk);
10879
            if (num_of_frames[1] == 1'b0)
10880
            begin
10881
              repeat (num_of_frames[31:2]) @(posedge mrx_clk); // for every (second) frame enable receiver one clock later
10882
            end
10883
            else
10884
            begin
10885
              @(posedge mrx_clk);
10886
              repeat (num_of_frames[31:2]) @(negedge mrx_clk); // for every (second) frame enable receiver one clock later
10887
            end
10888
            // enable RX, set full-duplex mode, NO receive small, NO correct IFG
10889
            wbm_init_waits = 4'h0;
10890
            wbm_subseq_waits = 4'h0;
10891
            #1 wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
10892
                      `ETH_MODER_PRO | `ETH_MODER_BRO,
10893
                      4'hF, 1, wbm_init_waits, wbm_subseq_waits); // write ASAP
10894
          end
10895
        end
10896
        begin // send a packet from PHY RX
10897
          repeat(1) @(posedge wb_clk); // wait for WB write when it is without delays
10898
          if (num_of_frames[1] == 1'b0)
10899
          begin
10900
            set_rx_addr_type(0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E);
10901
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
10902
          end
10903
          else
10904
          begin
10905
            set_rx_addr_type((max_tmp), 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E);
10906
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
10907
          end
10908
        end
10909
        begin: send_packet0
10910
          wait (MRxDV === 1'b1); // start transmit
10911
          wait (MRxDV === 1'b0); // end transmit
10912
          check_frame = 1;
10913
          repeat(10) @(posedge mrx_clk);
10914
          repeat(15) @(posedge wb_clk);
10915
          stop_checking_frame = 1;
10916
        end
10917
        begin // count WB clocks between ACK (negedge) and RX_DV (posedge) or vice-versa
10918
          @(posedge eth_sl_wb_ack_o or posedge MRxDV);
10919
          if ((eth_sl_wb_ack_o === 1'b1) && (MRxDV === 1'b1))
10920
          begin
10921
            tmp_data = 32'h8000_0001; // bit[31]==1 => 'posedge MRxDV' was before 'negedge eth_sl_wb_ack_o'
10922
          end
10923
          else if (MRxDV === 1'b1)
10924
          begin
10925
            while (eth_sl_wb_ack_o === 1'b0)
10926
            begin
10927
              @(posedge wb_clk);
10928
              tmp_data = tmp_data + 1;
10929
            end
10930
            tmp_data = tmp_data | 32'h8000_0000; // bit[31]==1 => 'posedge MRxDV' was before 'negedge eth_sl_wb_ack_o'
10931
          end
10932
          else if (eth_sl_wb_ack_o === 1'b1)
10933
          begin
10934
            @(posedge wb_clk); // wait for one clock => tmp_data 'becomes' 0
10935
            while (MRxDV === 1'b0)
10936
            begin
10937
              @(posedge wb_clk);
10938
              tmp_data = tmp_data + 1; // bit[31]==0 => 'negedge eth_sl_wb_ack_o' was equal or before 'posedge MRxDV'
10939
            end
10940
          end
10941
        end
10942
        begin // check packet
10943
          wait (check_frame == 1);
10944
          check_rx_bd(118, tmp_bd);
10945
          while ((tmp_bd[15] === 1) && (stop_checking_frame == 0))
10946
          begin
10947
            #1 check_rx_bd(118, tmp_bd);
10948
            @(posedge wb_clk);
10949
          end
10950
          if (num_of_frames[0] == 1'b0)
10951
          begin
10952
            if (tmp_bd[15] === 1)
10953
            begin
10954
              if (first_fr_received == 1)
10955
              begin
10956
                first_fr_received = 0;
10957
                $display("    %0d packets (without this one) are checked - packets are received by two in a set",
10958
                         num_of_frames); // +1 due to start with 0 AND -1 because this packet is excluded
10959
                $display("    From this moment:");
10960
                $display("    first one of two packets (including this one) is not accepted due to late RX enable");
10961
                if (tmp_data[31])
10962
                  $display("    ->RX enable set %0d WB clks after RX_DV", tmp_data[30:0]);
10963
                else
10964
                  $display("    ->RX enable set %0d WB clks before RX_DV", tmp_data[30:0]);
10965
              end
10966
            end
10967
          end
10968
          if (stop_checking_frame == 0)
10969
            disable send_packet0;
10970
        end
10971
      join
10972
      // ONLY IF packet was received!
10973
      if (tmp_bd[15] === 0)
10974
      begin
10975
        // check length of a PACKET
10976
        if (tmp_bd[31:16] != (i_length + 4))
10977
        begin
10978
          `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
10979
                          tmp_bd[31:16], (i_length + 4));
10980
          test_fail("Wrong length of the packet out from PHY");
10981
          fail = fail + 1;
10982
        end
10983
        // check received RX packet data and CRC
10984
        if (first_fr_received == 0) // if PREVIOUS RX buffer descriptor was not ready, pointer address is -1
10985
        begin
10986
          if (num_of_frames[1] == 1'b0)
10987
          begin
10988
            check_rx_packet(0, (`MEMORY_BASE + num_of_frames[1:0] - 1'b1), (i_length + 4), 1'b0, 1'b0, tmp);
10989
          end
10990
          else
10991
          begin
10992
            check_rx_packet(max_tmp, (`MEMORY_BASE + num_of_frames[1:0] - 1'b1), (i_length + 4), 1'b0, 1'b0, tmp);
10993
          end
10994
          if (tmp > 0)
10995
          begin
10996
            `TIME; $display("*E Wrong data of the received packet");
10997
            test_fail("Wrong data of the received packet");
10998
            fail = fail + 1;
10999
          end
11000
        end
11001
        else // if PREVIOUS RX buffer descriptor was ready
11002
        begin
11003
          if (num_of_frames[1] == 1'b0)
11004
          begin
11005
            check_rx_packet(0, (`MEMORY_BASE + num_of_frames[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
11006
          end
11007
          else
11008
          begin
11009
            check_rx_packet(max_tmp, (`MEMORY_BASE + num_of_frames[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
11010
          end
11011
          if (tmp > 0)
11012
          begin
11013
            `TIME; $display("*E Wrong data of the received packet");
11014
            test_fail("Wrong data of the received packet");
11015
            fail = fail + 1;
11016
          end
11017
        end
11018
      end
11019
      // check WB INT signal
11020
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11021
      begin
11022
        if (wb_int !== 1'b0)
11023
        begin
11024
          `TIME; $display("*E WB INT signal should not be set");
11025
          test_fail("WB INT signal should not be set");
11026
          fail = fail + 1;
11027
        end
11028
      end
11029
      else
11030
      begin
11031
        if (wb_int !== 1'b1)
11032
        begin
11033
          `TIME; $display("*E WB INT signal should be set");
11034
          test_fail("WB INT signal should be set");
11035
          fail = fail + 1;
11036
        end
11037
      end
11038
      // check RX buffer descriptor of a packet - only 15 LSBits
11039
      check_rx_bd(118, data);
11040
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11041
      begin
11042
        if (data[15:0] !== 16'hE000)
11043
        begin
11044
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11045
          test_fail("RX buffer descriptor status is not correct");
11046
          fail = fail + 1;
11047
        end
11048
      end
11049
      else // interrupt enabled
11050
      begin
11051
        if (data[15:0] !== 16'h6000)
11052
        begin
11053
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11054
          test_fail("RX buffer descriptor status is not correct");
11055
          fail = fail + 1;
11056
        end
11057
      end
11058
      // check interrupts
11059
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11060
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11061
      begin
11062
        if (data !== 0)
11063
        begin
11064
          `TIME; $display("*E Any of interrupts was set, interrupt reg: %0h, len: %0h", data, num_of_frames[1:0]);
11065
          test_fail("Any of interrupts was set");
11066
          fail = fail + 1;
11067
        end
11068
      end
11069
      else
11070
      begin
11071
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
11072
        begin
11073
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
11074
          test_fail("Interrupt Receive Buffer was not set");
11075
          fail = fail + 1;
11076
        end
11077
        if ((data & (~`ETH_INT_RXB)) !== 0)
11078
        begin
11079
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
11080
          test_fail("Other interrupts (except Receive Buffer) were set");
11081
          fail = fail + 1;
11082
        end
11083
      end
11084
      // clear interrupts
11085
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11086
      // check WB INT signal
11087
      if (wb_int !== 1'b0)
11088
      begin
11089
        test_fail("WB INT signal should not be set");
11090
        fail = fail + 1;
11091
      end
11092
      // disable RX after two packets
11093
      if (num_of_frames[0] == 1'b1)
11094
      begin
11095
        // disable RX, set full-duplex mode, NO receive small, NO correct IFG
11096
        wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_IFG |
11097
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
11098
                  4'hF, 1, 4'h0, 4'h0); // write ASAP
11099
      end
11100
      // the number of frame transmitted
11101
      num_of_frames = num_of_frames + 1;
11102
      num_of_bd = 0;
11103
      // set length (LOOP variable)
11104
      if (num_of_frames[31:2] == (i_length * 2 + 16)) // 64 => this vas last Byte (1st .. 64th) when i_length = min_tmp - 4
11105
        i_length = (max_tmp - 4);
11106
      @(posedge wb_clk);
11107
    end
11108
    // disable RX
11109
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
11110
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11111
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11112
    if(fail == 0)
11113
      test_ok;
11114
    else
11115
      fail = 0;
11116
  end
11117
 
11118
 
11119
  ////////////////////////////////////////////////////////////////////
11120
  ////                                                            ////
11121
  ////  Test receive packet synchronization with receive          ////
11122
  ////  disable/enable ( 100Mbps ).                               ////
11123
  ////                                                            ////
11124
  ////////////////////////////////////////////////////////////////////
11125
  if (test_num == 3) // Test no receive when all buffers are TX ( 100Mbps ).
11126
  begin
11127
    // TEST 3: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 100Mbps )
11128
    test_name   = "TEST 3: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 100Mbps )";
11129
    `TIME; $display("  TEST 3: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 100Mbps )");
11130
 
11131
    // unmask interrupts
11132
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11133
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11134
    // set 10 RX buffer descriptor (8'h80 - 8'hA) - must be set before RX enable
11135
    wbm_write(`ETH_TX_BD_NUM, 32'h76, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11136
    // disable RX, set full-duplex mode, NO receive small, NO correct IFG
11137
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_IFG |
11138
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11139
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11140
    // prepare two packets of MAXFL length
11141
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11142
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
11143
    min_tmp = tmp[31:16];
11144
    st_data = 8'h0F;
11145
    set_rx_packet(0, (min_tmp + 1), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
11146
    append_rx_crc(0, (min_tmp + 1), 1'b0, 1'b0);
11147
    st_data = 8'h1A;
11148
    set_rx_packet(max_tmp, (min_tmp + 1), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
11149
    append_rx_crc(max_tmp, (min_tmp + 1), 1'b0, 1'b0);
11150
    // check WB INT signal
11151
    if (wb_int !== 1'b0)
11152
    begin
11153
      test_fail("WB INT signal should not be set");
11154
      fail = fail + 1;
11155
    end
11156
 
11157
    // write to phy's control register for 100Mbps
11158
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
11159
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
11160
    speed = 100;
11161
 
11162
    frame_started = 0;
11163
    frame_ended = 0;
11164
    wait_for_frame = 0;
11165
    check_frame = 0;
11166
    stop_checking_frame = 0;
11167
    first_fr_received = 1; // at the beginning the first frame of each two will be received!
11168
 
11169
    num_of_frames = 0; // 
11170
    num_of_bd = 0;
11171
    i_length = (min_tmp + 1); // 5 bytes longer than MINFL
11172
    while (i_length < (max_tmp - 4))
11173
    begin
11174
      // choose generating carrier sense and collision 
11175
      case (num_of_frames[1:0])
11176
      2'h0: // Interrupt is generated
11177
      begin
11178
        // enable interrupt generation
11179
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
11180
        // not detect carrier sense in FD and no collision
11181
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11182
        eth_phy.collision(0);
11183
      end
11184
      2'h1: // Interrupt is generated
11185
      begin
11186
        // enable interrupt generation
11187
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
11188
        // detect carrier sense in FD and no collision
11189
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11190
        eth_phy.collision(0);
11191
      end
11192
      2'h2: // Interrupt is generated
11193
      begin
11194
        // disable interrupt generation
11195
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
11196
        // not detect carrier sense in FD and set collision
11197
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11198
        eth_phy.collision(1);
11199
      end
11200
      default: // 2'h3: // Interrupt is generated
11201
      begin
11202
        // disable interrupt generation
11203
        set_rx_bd(118, 118, 1'b1, (`MEMORY_BASE + num_of_frames[1:0]));
11204
        // detect carrier sense in FD and set collision
11205
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11206
        eth_phy.collision(1);
11207
      end
11208
      endcase
11209
//if (first_fr_received == 0)
11210
//begin
11211
//  check_rx_bd(118, data);
11212
//  wbm_read((`TX_BD_BASE + (118 * 8) + 4), tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11213
//  $display("RX BD set : %h, %h", data, tmp);
11214
//end
11215
      // set wrap bit
11216
      set_rx_bd_wrap(118);
11217
      set_rx_bd_empty(118, 118);
11218
      check_frame = 0;
11219
      stop_checking_frame = 0;
11220
      tmp_data = 0;
11221
$display("mama 1");
11222
      fork
11223
        begin // enable RX MAC on first of each two packets - every second should be recived normaly
11224
          if (num_of_frames[0] == 1'b0)
11225
          begin
11226
            repeat(1) @(posedge wb_clk);
11227
            if (num_of_frames[1] == 1'b0)
11228
            begin
11229
              repeat (num_of_frames[31:2]) @(posedge mrx_clk); // for every (second) frame enable receiver one clock later
11230
            end
11231
            else
11232
            begin
11233
              @(posedge mrx_clk);
11234
              repeat (num_of_frames[31:2]) @(negedge mrx_clk); // for every (second) frame enable receiver one clock later
11235
            end
11236
            // enable RX, set full-duplex mode, NO receive small, NO correct IFG
11237
            wbm_init_waits = 4'h0;
11238
            wbm_subseq_waits = 4'h0;
11239
            #1 wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
11240
                      `ETH_MODER_PRO | `ETH_MODER_BRO,
11241
                      4'hF, 1, wbm_init_waits, wbm_subseq_waits); // write ASAP
11242
$display("mama 2, num_of_frames=%0h", num_of_frames);
11243
          end
11244
        end
11245
        begin // send a packet from PHY RX
11246
          repeat(1) @(posedge wb_clk); // wait for WB write when it is without delays
11247
          if (num_of_frames[1] == 1'b0)
11248
          begin
11249
            set_rx_addr_type(0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E);
11250
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
11251
          end
11252
          else
11253
          begin
11254
            set_rx_addr_type((max_tmp), 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E);
11255
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
11256
          end
11257
        end
11258
        begin: send_packet1
11259
          wait (MRxDV === 1'b1); // start transmit
11260
          wait (MRxDV === 1'b0); // end transmit
11261
          check_frame = 1;
11262
$display("mama 3");
11263
          repeat(10) @(posedge mrx_clk);
11264
          repeat(15) @(posedge wb_clk);
11265
          stop_checking_frame = 1;
11266
        end
11267
        begin // count WB clocks between ACK (negedge) and RX_DV (posedge) or vice-versa
11268
          @(posedge eth_sl_wb_ack_o or posedge MRxDV);
11269
$display("mama 4");
11270
          if ((eth_sl_wb_ack_o === 1'b1) && (MRxDV === 1'b1))
11271
          begin
11272
            tmp_data = 32'h8000_0001; // bit[31]==1 => 'posedge MRxDV' was before 'negedge eth_sl_wb_ack_o'
11273
$display("mama 4_1");
11274
          end
11275
          else if (MRxDV === 1'b1)
11276
          begin
11277
            while (eth_sl_wb_ack_o === 1'b0)
11278
            begin
11279
              @(posedge wb_clk);
11280
              tmp_data = tmp_data + 1;
11281
            end
11282
            tmp_data = tmp_data | 32'h8000_0000; // bit[31]==1 => 'posedge MRxDV' was before 'negedge eth_sl_wb_ack_o'
11283
$display("mama 4_2");
11284
          end
11285
          else if (eth_sl_wb_ack_o === 1'b1)
11286
          begin
11287
            @(posedge wb_clk); // wait for one clock => tmp_data 'becomes' 0
11288
            while (MRxDV === 1'b0)
11289
            begin
11290
              @(posedge wb_clk);
11291
              tmp_data = tmp_data + 1; // bit[31]==0 => 'negedge eth_sl_wb_ack_o' was equal or before 'posedge MRxDV'
11292
            end
11293
$display("mama 4_3");
11294
          end
11295
        end
11296
        begin // check packet
11297
          wait (check_frame == 1);
11298
          check_rx_bd(118, tmp_bd);
11299
          while ((tmp_bd[15] === 1) && (stop_checking_frame == 0))
11300
          begin
11301
            #1 check_rx_bd(118, tmp_bd);
11302
            @(posedge wb_clk);
11303
          end
11304
$display("mama 5, tmp_bd=%0h", tmp_bd);
11305
          if (num_of_frames[0] == 1'b0)
11306
          begin
11307
            if (tmp_bd[15] === 1)
11308
            begin
11309
              if (first_fr_received == 1)
11310
              begin
11311
                first_fr_received = 0;
11312
                $display("    %0d packets (without this one) are checked - packets are received by two in a set",
11313
                         num_of_frames); // +1 due to start with 0 AND -1 because this packet is excluded
11314
                $display("    From this moment:");
11315
                $display("    first one of two packets (including this one) is not accepted due to late RX enable");
11316
                if (tmp_data[31])
11317
                  $display("    ->RX enable set %0d WB clks after RX_DV", tmp_data[30:0]);
11318
                else
11319
                  $display("    ->RX enable set %0d WB clks before RX_DV", tmp_data[30:0]);
11320
              end
11321
              // check FB, etc.
11322
 
11323
            end
11324
            else // (tmp_bd[15] === 0)
11325
            begin // check FB, packet, etc.
11326
 
11327
            end
11328
$display("mama 5_1");
11329
          end
11330
          else // (num_of_frames[0] == 1'b1)
11331
          begin
11332
            if (tmp_bd[15] === 1) // ERROR, because second packet of each two frames should be received
11333
            begin // check NOTHING
11334
 
11335
            end
11336
            else // (tmp_bd[15] === 0)
11337
            begin // check FB, packet, etc.
11338
 
11339
            end
11340
$display("mama 5_2");
11341
          end
11342
          if (stop_checking_frame == 0)
11343
            disable send_packet1;
11344
        end
11345
      join
11346
      // ONLY IF packet was received!
11347
$display("mama 6");
11348
      if (tmp_bd[15] === 0)
11349
      begin
11350
        // check length of a PACKET
11351
        if (tmp_bd[31:16] != (i_length + 4))
11352
        begin
11353
          `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
11354
                          tmp_bd[31:16], (i_length + 4));
11355
          test_fail("Wrong length of the packet out from PHY");
11356
          fail = fail + 1;
11357
        end
11358
        // check received RX packet data and CRC
11359
        if (first_fr_received == 0) // if PREVIOUS RX buffer descriptor was not ready, pointer address is -1
11360
        begin
11361
          if (num_of_frames[1] == 1'b0)
11362
          begin
11363
            check_rx_packet(0, (`MEMORY_BASE + num_of_frames[1:0] - 1'b1), (i_length + 4), 1'b0, 1'b0, tmp);
11364
          end
11365
          else
11366
          begin
11367
            check_rx_packet(max_tmp, (`MEMORY_BASE + num_of_frames[1:0] - 1'b1), (i_length + 4), 1'b0, 1'b0, tmp);
11368
          end
11369
          if (tmp > 0)
11370
          begin
11371
            `TIME; $display("*E Wrong data of the received packet");
11372
            test_fail("Wrong data of the received packet");
11373
            fail = fail + 1;
11374
          end
11375
        end
11376
        else // if PREVIOUS RX buffer descriptor was ready
11377
        begin
11378
          if (num_of_frames[1] == 1'b0)
11379
          begin
11380
            check_rx_packet(0, (`MEMORY_BASE + num_of_frames[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
11381
          end
11382
          else
11383
          begin
11384
            check_rx_packet(max_tmp, (`MEMORY_BASE + num_of_frames[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
11385
          end
11386
          if (tmp > 0)
11387
          begin
11388
            `TIME; $display("*E Wrong data of the received packet");
11389
            test_fail("Wrong data of the received packet");
11390
            fail = fail + 1;
11391
          end
11392
        end
11393
      end
11394
      // check WB INT signal
11395
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11396
      begin
11397
        if (wb_int !== 1'b0)
11398
        begin
11399
          `TIME; $display("*E WB INT signal should not be set");
11400
          test_fail("WB INT signal should not be set");
11401
          fail = fail + 1;
11402
        end
11403
      end
11404
      else
11405
      begin
11406
        if (wb_int !== 1'b1)
11407
        begin
11408
          `TIME; $display("*E WB INT signal should be set");
11409
          test_fail("WB INT signal should be set");
11410
          fail = fail + 1;
11411
        end
11412
      end
11413
      // check RX buffer descriptor of a packet - only 15 LSBits
11414
      check_rx_bd(118, data);
11415
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11416
      begin
11417
        if (data[15:0] !== 16'hE000)
11418
        begin
11419
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11420
          test_fail("RX buffer descriptor status is not correct");
11421
          fail = fail + 1;
11422
        end
11423
      end
11424
      else // interrupt enabled
11425
      begin
11426
        if (data[15:0] !== 16'h6000)
11427
        begin
11428
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11429
          test_fail("RX buffer descriptor status is not correct");
11430
          fail = fail + 1;
11431
        end
11432
      end
11433
      // check interrupts
11434
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11435
      if ((num_of_frames[0] == 1'b0) && (first_fr_received == 0)) // interrupt enabled but no receive
11436
      begin
11437
        if (data !== 0)
11438
        begin
11439
          `TIME; $display("*E Any of interrupts was set, interrupt reg: %0h, len: %0h", data, num_of_frames[1:0]);
11440
          test_fail("Any of interrupts was set");
11441
          fail = fail + 1;
11442
        end
11443
      end
11444
      else
11445
      begin
11446
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
11447
        begin
11448
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
11449
          test_fail("Interrupt Receive Buffer was not set");
11450
          fail = fail + 1;
11451
        end
11452
        if ((data & (~`ETH_INT_RXB)) !== 0)
11453
        begin
11454
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
11455
          test_fail("Other interrupts (except Receive Buffer) were set");
11456
          fail = fail + 1;
11457
        end
11458
      end
11459
      // clear interrupts
11460
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11461
      // check WB INT signal
11462
      if (wb_int !== 1'b0)
11463
      begin
11464
        test_fail("WB INT signal should not be set");
11465
        fail = fail + 1;
11466
      end
11467
      // disable RX after two packets
11468
      if (num_of_frames[0] == 1'b1)
11469
      begin
11470
        // disable RX, set full-duplex mode, NO receive small, NO correct IFG
11471
        wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_IFG |
11472
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
11473
                  4'hF, 1, 4'h0, 4'h0); // write ASAP
11474
      end
11475
      // the number of frame transmitted
11476
      num_of_frames = num_of_frames + 1;
11477
      num_of_bd = 0;
11478
      // set length (LOOP variable)
11479
      if (num_of_frames[31:2] == (i_length * 2 + 16)) // 64 => this vas last Byte (1st .. 64th) when i_length = min_tmp - 4
11480
        i_length = (max_tmp - 4);
11481
      @(posedge wb_clk);
11482
    end
11483
    // disable RX
11484
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
11485
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11486
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11487
    if(fail == 0)
11488
      test_ok;
11489
    else
11490
      fail = 0;
11491
  end
11492
 
11493
 
11494
  ////////////////////////////////////////////////////////////////////
11495
  ////                                                            ////
11496 209 tadejm
  ////  Test receive packets form MINFL to MAXFL sizes at         ////
11497
  ////  one RX buffer decriptor ( 10Mbps ).                       ////
11498
  ////                                                            ////
11499
  ////////////////////////////////////////////////////////////////////
11500 243 tadejm
  if (test_num == 4) // 
11501 209 tadejm
  begin
11502 243 tadejm
    // TEST 4: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 10Mbps )
11503
    test_name   = "TEST 4: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 10Mbps )";
11504
    `TIME; $display("  TEST 4: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 10Mbps )");
11505 209 tadejm
 
11506
    // unmask interrupts
11507
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11508
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11509
    // set 1 RX buffer descriptor (8'h80 - 1) - must be set before RX enable
11510
    wbm_write(`ETH_TX_BD_NUM, 32'h7F, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11511
    // enable RX, set full-duplex mode, NO receive small, NO correct IFG
11512
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
11513
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11514
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11515
    // prepare two packets of MAXFL length
11516
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11517
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
11518
    min_tmp = tmp[31:16];
11519
    st_data = 8'h0F;
11520
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
11521
    st_data = 8'h1A;
11522
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
11523
    // check WB INT signal
11524
    if (wb_int !== 1'b0)
11525
    begin
11526
      test_fail("WB INT signal should not be set");
11527
      fail = fail + 1;
11528
    end
11529
 
11530
    // write to phy's control register for 10Mbps
11531
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
11532
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
11533
    speed = 10;
11534
 
11535
    i_length = (min_tmp - 4);
11536
    while (i_length <= (max_tmp - 4))
11537
    begin
11538
      // choose generating carrier sense and collision for first and last 64 lengths of frames
11539
      case (i_length[1:0])
11540
      2'h0: // Interrupt is generated
11541
      begin
11542
        // enable interrupt generation
11543
        set_rx_bd(127, 127, 1'b1, (`MEMORY_BASE + i_length[1:0]));
11544
        // unmask interrupts
11545
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11546
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11547
        // not detect carrier sense in FD and no collision
11548
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11549
        eth_phy.collision(0);
11550
      end
11551
      2'h1: // Interrupt is not generated
11552
      begin
11553
        // enable interrupt generation
11554
        set_rx_bd(127, 127, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
11555
        // mask interrupts
11556
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11557
        // detect carrier sense in FD and no collision
11558
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11559
        eth_phy.collision(0);
11560
      end
11561
      2'h2: // Interrupt is not generated
11562
      begin
11563
        // disable interrupt generation
11564
        set_rx_bd(127, 127, 1'b0, (`MEMORY_BASE + i_length[1:0]));
11565
        // unmask interrupts
11566
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11567
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11568
        // not detect carrier sense in FD and set collision
11569
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11570
        eth_phy.collision(1);
11571
      end
11572
      default: // 2'h3: // Interrupt is not generated
11573
      begin
11574
        // disable interrupt generation
11575
        set_rx_bd(127, 127, 1'b0, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
11576
        // mask interrupts
11577
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11578
        // detect carrier sense in FD and set collision
11579
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11580
        eth_phy.collision(1);
11581
      end
11582
      endcase
11583
      if (i_length[0] == 1'b0)
11584
        append_rx_crc (0, i_length, 1'b0, 1'b0);
11585
      else
11586
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
11587
      // set wrap bit
11588
      set_rx_bd_wrap(127);
11589
      set_rx_bd_empty(127, 127);
11590
      fork
11591
        begin
11592
          if (i_length[0] == 1'b0)
11593
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
11594
          else
11595
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
11596
          repeat(10) @(posedge mrx_clk);
11597
        end
11598
        begin
11599
          #1 check_rx_bd(127, data);
11600
          if (i_length < min_tmp) // just first four
11601
          begin
11602
            while (data[15] === 1)
11603
            begin
11604
              #1 check_rx_bd(127, data);
11605
              @(posedge wb_clk);
11606
            end
11607
            repeat (1) @(posedge wb_clk);
11608
          end
11609
          else
11610
          begin
11611
            wait (MRxDV === 1'b1); // start transmit
11612
            #1 check_rx_bd(127, data);
11613
            if (data[15] !== 1)
11614
            begin
11615
              test_fail("Wrong buffer descriptor's ready bit read out from MAC");
11616
              fail = fail + 1;
11617
            end
11618
            wait (MRxDV === 1'b0); // end transmit
11619
            while (data[15] === 1)
11620
            begin
11621
              #1 check_rx_bd(127, data);
11622
              @(posedge wb_clk);
11623
            end
11624
            repeat (1) @(posedge wb_clk);
11625
          end
11626
        end
11627
      join
11628
      // check length of a PACKET
11629
      if (data[31:16] != (i_length + 4))
11630
      begin
11631
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
11632
                        data[31:16], (i_length + 4));
11633
        test_fail("Wrong length of the packet out from PHY");
11634
        fail = fail + 1;
11635
      end
11636
      // checking in the following if statement is performed only for first and last 64 lengths
11637
      // check received RX packet data and CRC
11638
      if (i_length[0] == 1'b0)
11639
      begin
11640
        check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
11641
      end
11642
      else
11643
      begin
11644
        check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
11645
      end
11646
      if (tmp > 0)
11647
      begin
11648
        `TIME; $display("*E Wrong data of the received packet");
11649
        test_fail("Wrong data of the received packet");
11650
        fail = fail + 1;
11651
      end
11652
      // check WB INT signal
11653
      if (i_length[1:0] == 2'h0)
11654
      begin
11655
        if (wb_int !== 1'b1)
11656
        begin
11657
          `TIME; $display("*E WB INT signal should be set");
11658
          test_fail("WB INT signal should be set");
11659
          fail = fail + 1;
11660
        end
11661
      end
11662
      else
11663
      begin
11664
        if (wb_int !== 1'b0)
11665
        begin
11666
          `TIME; $display("*E WB INT signal should not be set");
11667
          test_fail("WB INT signal should not be set");
11668
          fail = fail + 1;
11669
        end
11670
      end
11671
      // check RX buffer descriptor of a packet
11672
      check_rx_bd(127, data);
11673
      if (i_length[1] == 1'b0) // interrupt enabled no_carrier_sense_rx_fd_detect
11674
      begin
11675
        if ( ((data[15:0] !== 16'h6000) && (i_length[0] == 1'b0)) ||
11676
             ((data[15:0] !== 16'h6000) && (i_length[0] == 1'b1)) )
11677
        begin
11678
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11679
          test_fail("RX buffer descriptor status is not correct");
11680
          fail = fail + 1;
11681
        end
11682
      end
11683
      else // interrupt not enabled
11684
      begin
11685
        if ( ((data[15:0] !== 16'h2000) && (i_length[0] == 1'b0)) ||
11686
             ((data[15:0] !== 16'h2000) && (i_length[0] == 1'b1)) )
11687
        begin
11688
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11689
          test_fail("RX buffer descriptor status is not correct");
11690
          fail = fail + 1;
11691
        end
11692
      end
11693
      // clear RX buffer descriptor for first 4 frames
11694
      if (i_length < min_tmp)
11695
        clear_rx_bd(127, 127);
11696
      // check interrupts
11697
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11698
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
11699
      begin
11700
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
11701
        begin
11702
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
11703
          test_fail("Interrupt Receive Buffer was not set");
11704
          fail = fail + 1;
11705
        end
11706
        if ((data & (~`ETH_INT_RXB)) !== 0)
11707
        begin
11708
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
11709
          test_fail("Other interrupts (except Receive Buffer) were set");
11710
          fail = fail + 1;
11711
        end
11712
      end
11713
      else
11714
      begin
11715
        if (data !== 0)
11716
        begin
11717
          `TIME; $display("*E Any of interrupts (except Receive Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
11718
          test_fail("Any of interrupts (except Receive Buffer) was set");
11719
          fail = fail + 1;
11720
        end
11721
      end
11722
      // clear interrupts
11723
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11724
      // check WB INT signal
11725
      if (wb_int !== 1'b0)
11726
      begin
11727
        test_fail("WB INT signal should not be set");
11728
        fail = fail + 1;
11729
      end
11730
      // INTERMEDIATE DISPLAYS
11731
      if ((i_length + 4) == (min_tmp + 64))
11732
      begin
11733
        // starting length is min_tmp, ending length is (min_tmp + 64)
11734
        $display("    receive small packets is NOT selected");
11735
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
11736
                 min_tmp, (min_tmp + 64));
11737
        // set receive small, remain the rest
11738
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
11739
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
11740
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11741
      end
11742
      else if ((i_length + 4) == (max_tmp - 16))
11743
      begin
11744
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
11745
        $display("    receive small packets is selected");
11746
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
11747
                 (min_tmp + 64 + 128), tmp_data);
11748
        // reset receive small, remain the rest
11749
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
11750
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
11751
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11752
      end
11753
      else if ((i_length + 4) == max_tmp)
11754
      begin
11755
        $display("    receive small packets is NOT selected");
11756
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
11757
                 (max_tmp - (4 + 16)), max_tmp);
11758
      end
11759
      // set length (loop variable)
11760
      if ((i_length + 4) < (min_tmp + 64))
11761
        i_length = i_length + 1;
11762
      else if ( ((i_length + 4) >= (min_tmp + 64)) && ((i_length + 4) <= (max_tmp - 256)) )
11763
      begin
11764
        i_length = i_length + 128;
11765
        tmp_data = i_length + 4; // last tmp_data is ending length
11766
      end
11767
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
11768
        i_length = max_tmp - (4 + 16);
11769
      else if ((i_length + 4) >= (max_tmp - 16))
11770
        i_length = i_length + 1;
11771
      else
11772
      begin
11773
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
11774
        #10 $stop;
11775
      end
11776
    end
11777
    // disable RX
11778
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
11779
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11780
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11781
    if(fail == 0)
11782
      test_ok;
11783
    else
11784
      fail = 0;
11785
  end
11786
 
11787
 
11788
  ////////////////////////////////////////////////////////////////////
11789
  ////                                                            ////
11790
  ////  Test receive packets form MINFL to MAXFL sizes at         ////
11791
  ////  one RX buffer decriptor ( 100Mbps ).                      ////
11792
  ////                                                            ////
11793
  ////////////////////////////////////////////////////////////////////
11794 243 tadejm
  if (test_num == 5) // Test no receive when all buffers are TX ( 100Mbps ).
11795 209 tadejm
  begin
11796 243 tadejm
    // TEST 5: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 100Mbps )
11797
    test_name   = "TEST 5: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 100Mbps )";
11798
    `TIME; $display("  TEST 5: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT ONE RX BD ( 100Mbps )");
11799 209 tadejm
 
11800
    // unmask interrupts
11801
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11802
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11803
    // set 1 RX buffer descriptor (8'h80 - 1) - must be set before RX enable
11804
    wbm_write(`ETH_TX_BD_NUM, 32'h7F, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11805
    // enable RX, set full-duplex mode, NO receive small, NO correct IFG
11806
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
11807
              `ETH_MODER_PRO | `ETH_MODER_BRO,
11808
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11809
    // prepare two packets of MAXFL length
11810
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11811
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
11812
    min_tmp = tmp[31:16];
11813
    st_data = 8'h0F;
11814
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
11815
    st_data = 8'h1A;
11816
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
11817
    // check WB INT signal
11818
    if (wb_int !== 1'b0)
11819
    begin
11820
      test_fail("WB INT signal should not be set");
11821
      fail = fail + 1;
11822
    end
11823
 
11824
    // write to phy's control register for 100Mbps
11825
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
11826
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
11827
    speed = 100;
11828
 
11829
    i_length = (min_tmp - 4);
11830
    while (i_length <= (max_tmp - 4))
11831
    begin
11832
      // choose generating carrier sense and collision for first and last 64 lengths of frames
11833
      case (i_length[1:0])
11834
      2'h0: // Interrupt is generated
11835
      begin
11836
        // enable interrupt generation
11837
        set_rx_bd(127, 127, 1'b1, (`MEMORY_BASE + i_length[1:0]));
11838
        // unmask interrupts
11839
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11840
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11841
        // not detect carrier sense in FD and no collision
11842
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11843
        eth_phy.collision(0);
11844
      end
11845
      2'h1: // Interrupt is not generated
11846
      begin
11847
        // enable interrupt generation
11848
        set_rx_bd(127, 127, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
11849
        // mask interrupts
11850
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11851
        // detect carrier sense in FD and no collision
11852
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11853
        eth_phy.collision(0);
11854
      end
11855
      2'h2: // Interrupt is not generated
11856
      begin
11857
        // disable interrupt generation
11858
        set_rx_bd(127, 127, 1'b0, (`MEMORY_BASE + i_length[1:0]));
11859
        // unmask interrupts
11860
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
11861
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11862
        // not detect carrier sense in FD and set collision
11863
        eth_phy.no_carrier_sense_rx_fd_detect(0);
11864
        eth_phy.collision(1);
11865
      end
11866
      default: // 2'h3: // Interrupt is not generated
11867
      begin
11868
        // disable interrupt generation
11869
        set_rx_bd(127, 127, 1'b0, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
11870
        // mask interrupts
11871
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11872
        // detect carrier sense in FD and set collision
11873
        eth_phy.no_carrier_sense_rx_fd_detect(1);
11874
        eth_phy.collision(1);
11875
      end
11876
      endcase
11877
      if (i_length[0] == 1'b0)
11878
        append_rx_crc (0, i_length, 1'b0, 1'b0);
11879
      else
11880
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
11881
      // set wrap bit
11882
      set_rx_bd_wrap(127);
11883
      set_rx_bd_empty(127, 127);
11884
      fork
11885
        begin
11886
          if (i_length[0] == 1'b0)
11887
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
11888
          else
11889
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
11890
          repeat(10) @(posedge mrx_clk);
11891
        end
11892
        begin
11893
          #1 check_rx_bd(127, data);
11894
          if (i_length < min_tmp) // just first four
11895
          begin
11896
            while (data[15] === 1)
11897
            begin
11898
              #1 check_rx_bd(127, data);
11899
              @(posedge wb_clk);
11900
            end
11901
            repeat (1) @(posedge wb_clk);
11902
          end
11903
          else
11904
          begin
11905
            wait (MRxDV === 1'b1); // start transmit
11906
            #1 check_rx_bd(127, data);
11907
            if (data[15] !== 1)
11908
            begin
11909
              test_fail("Wrong buffer descriptor's ready bit read out from MAC");
11910
              fail = fail + 1;
11911
            end
11912
            wait (MRxDV === 1'b0); // end transmit
11913
            while (data[15] === 1)
11914
            begin
11915
              #1 check_rx_bd(127, data);
11916
              @(posedge wb_clk);
11917
            end
11918
            repeat (1) @(posedge wb_clk);
11919
          end
11920
        end
11921
      join
11922
      // check length of a PACKET
11923
      if (data[31:16] != (i_length + 4))
11924
      begin
11925
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
11926
                        data[31:16], (i_length + 4));
11927
        test_fail("Wrong length of the packet out from PHY");
11928
        fail = fail + 1;
11929
      end
11930
      // checking in the following if statement is performed only for first and last 64 lengths
11931
      // check received RX packet data and CRC
11932
      if (i_length[0] == 1'b0)
11933
      begin
11934
        check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
11935
      end
11936
      else
11937
      begin
11938
        check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
11939
      end
11940
      if (tmp > 0)
11941
      begin
11942
        `TIME; $display("*E Wrong data of the received packet");
11943
        test_fail("Wrong data of the received packet");
11944
        fail = fail + 1;
11945
      end
11946
      // check WB INT signal
11947
      if (i_length[1:0] == 2'h0)
11948
      begin
11949
        if (wb_int !== 1'b1)
11950
        begin
11951
          `TIME; $display("*E WB INT signal should be set");
11952
          test_fail("WB INT signal should be set");
11953
          fail = fail + 1;
11954
        end
11955
      end
11956
      else
11957
      begin
11958
        if (wb_int !== 1'b0)
11959
        begin
11960
          `TIME; $display("*E WB INT signal should not be set");
11961
          test_fail("WB INT signal should not be set");
11962
          fail = fail + 1;
11963
        end
11964
      end
11965
      // check RX buffer descriptor of a packet
11966
      check_rx_bd(127, data);
11967
      if (i_length[1] == 1'b0) // interrupt enabled 
11968
      begin
11969
        if ( ((data[15:0] !== 16'h6000) && (i_length[0] == 1'b0)) ||
11970
             ((data[15:0] !== 16'h6000) && (i_length[0] == 1'b1)) )
11971
        begin
11972
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11973
          test_fail("RX buffer descriptor status is not correct");
11974
          fail = fail + 1;
11975
        end
11976
      end
11977
      else // interrupt not enabled
11978
      begin
11979
        if ( ((data[15:0] !== 16'h2000) && (i_length[0] == 1'b0)) ||
11980
             ((data[15:0] !== 16'h2000) && (i_length[0] == 1'b1)) )
11981
        begin
11982
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
11983
          test_fail("RX buffer descriptor status is not correct");
11984
          fail = fail + 1;
11985
        end
11986
      end
11987
      // clear RX buffer descriptor for first 4 frames
11988
      if (i_length < min_tmp)
11989
        clear_rx_bd(127, 127);
11990
      // check interrupts
11991
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
11992
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
11993
      begin
11994
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
11995
        begin
11996
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
11997
          test_fail("Interrupt Receive Buffer was not set");
11998
          fail = fail + 1;
11999
        end
12000
        if ((data & (~`ETH_INT_RXB)) !== 0)
12001
        begin
12002
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
12003
          test_fail("Other interrupts (except Receive Buffer) were set");
12004
          fail = fail + 1;
12005
        end
12006
      end
12007
      else
12008
      begin
12009
        if (data !== 0)
12010
        begin
12011
          `TIME; $display("*E Any of interrupts (except Receive Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
12012
          test_fail("Any of interrupts (except Receive Buffer) was set");
12013
          fail = fail + 1;
12014
        end
12015
      end
12016
      // clear interrupts
12017
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12018
      // check WB INT signal
12019
      if (wb_int !== 1'b0)
12020
      begin
12021
        test_fail("WB INT signal should not be set");
12022
        fail = fail + 1;
12023
      end
12024
      // INTERMEDIATE DISPLAYS
12025
      if ((i_length + 4) == (min_tmp + 64))
12026
      begin
12027
        // starting length is min_tmp, ending length is (min_tmp + 64)
12028
        $display("    receive small packets is NOT selected");
12029
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
12030
                 min_tmp, (min_tmp + 64));
12031
        // set receive small, remain the rest
12032
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12033
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12034
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12035
      end
12036
      else if ((i_length + 4) == (max_tmp - 16))
12037
      begin
12038
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
12039
        $display("    receive small packets is selected");
12040
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
12041
                 (min_tmp + 64 + 128), tmp_data);
12042
        // reset receive small, remain the rest
12043
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12044
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12045
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12046
      end
12047
      else if ((i_length + 4) == max_tmp)
12048
      begin
12049
        $display("    receive small packets is NOT selected");
12050
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
12051
                 (max_tmp - (4 + 16)), max_tmp);
12052
      end
12053
      // set length (loop variable)
12054
      if ((i_length + 4) < (min_tmp + 64))
12055
        i_length = i_length + 1;
12056
      else if ( ((i_length + 4) >= (min_tmp + 64)) && ((i_length + 4) <= (max_tmp - 256)) )
12057
      begin
12058
        i_length = i_length + 128;
12059
        tmp_data = i_length + 4; // last tmp_data is ending length
12060
      end
12061
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
12062
        i_length = max_tmp - (4 + 16);
12063
      else if ((i_length + 4) >= (max_tmp - 16))
12064
        i_length = i_length + 1;
12065
      else
12066
      begin
12067
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
12068
        #10 $stop;
12069
      end
12070
    end
12071
    // disable RX
12072
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12073
              `ETH_MODER_PRO | `ETH_MODER_BRO,
12074
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12075
    if(fail == 0)
12076
      test_ok;
12077
    else
12078
      fail = 0;
12079
  end
12080
 
12081
 
12082
  ////////////////////////////////////////////////////////////////////
12083
  ////                                                            ////
12084
  ////  Test receive packets form MINFL to MAXFL sizes at         ////
12085
  ////  maximum RX buffer decriptors ( 10Mbps ).                  ////
12086
  ////                                                            ////
12087
  ////////////////////////////////////////////////////////////////////
12088 243 tadejm
  if (test_num == 6) // 
12089 209 tadejm
  begin
12090 243 tadejm
    // TEST 6: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 10Mbps )
12091
    test_name = "TEST 6: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 10Mbps )";
12092
    `TIME; $display("  TEST 6: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 10Mbps )");
12093 209 tadejm
 
12094
    // reset MAC registers
12095
    hard_reset;
12096
    // reset MAC and MII LOGIC with soft reset
12097
    reset_mac;
12098
    reset_mii;
12099
    // set wb slave response
12100
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
12101
 
12102
    max_tmp = 0;
12103
    min_tmp = 0;
12104
    num_of_frames = 0;
12105
    num_of_bd = 0;
12106
    // set maximum RX buffer descriptors (128) - must be set before RX enable
12107
    wbm_write(`ETH_TX_BD_NUM, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12108
    // enable RX, set full-duplex mode, NO receive small, NO correct IFG
12109
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12110
              `ETH_MODER_PRO | `ETH_MODER_BRO,
12111
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12112
    // prepare two packets of MAXFL length
12113
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12114
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
12115
    min_tmp = tmp[31:16];
12116
    st_data = 8'hAC;
12117
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
12118
    st_data = 8'h35;
12119
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
12120
    // check WB INT signal
12121
    if (wb_int !== 1'b0)
12122
    begin
12123
      test_fail("WB INT signal should not be set");
12124
      fail = fail + 1;
12125
    end
12126
 
12127
    // write to phy's control register for 10Mbps
12128
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
12129
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
12130
    speed = 10;
12131
 
12132
    i_length = (min_tmp - 4);
12133
    while (i_length <= (max_tmp - 4))
12134
    begin
12135
      // append CRC to packet
12136
      if (i_length[0] == 1'b0)
12137
        append_rx_crc (0, i_length, 1'b0, 1'b0);
12138
      else
12139
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
12140
      // choose generating carrier sense and collision
12141
      case (i_length[1:0])
12142
      2'h0: // Interrupt is generated
12143
      begin
12144
        // Reset_tx_bd nable interrupt generation
12145
        // unmask interrupts
12146
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
12147
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12148
        // not detect carrier sense in FD and no collision
12149
        eth_phy.no_carrier_sense_rx_fd_detect(0);
12150
        eth_phy.collision(0);
12151
      end
12152
      2'h1: // Interrupt is not generated
12153
      begin
12154
        // set_tx_bd enable interrupt generation
12155
        // mask interrupts
12156
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12157
        // detect carrier sense in FD and no collision
12158
        eth_phy.no_carrier_sense_rx_fd_detect(1);
12159
        eth_phy.collision(0);
12160
      end
12161
      2'h2: // Interrupt is not generated
12162
      begin
12163
        // set_tx_bd disable the interrupt generation
12164
        // unmask interrupts
12165
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
12166
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12167
        // not detect carrier sense in FD and set collision
12168
        eth_phy.no_carrier_sense_rx_fd_detect(0);
12169
        eth_phy.collision(1);
12170
      end
12171
      default: // 2'h3: // Interrupt is not generated
12172
      begin
12173
        // set_tx_bd disable the interrupt generation
12174
        // mask interrupts
12175
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12176
        // detect carrier sense in FD and set collision
12177
        eth_phy.no_carrier_sense_rx_fd_detect(1);
12178
        eth_phy.collision(1);
12179
      end
12180
      endcase
12181
      // first 8 frames are received with RX BD 0 (wrap bit on RX BD 0)
12182
      // number of all frames is 154 (146 without first 8)
12183
      if (num_of_frames < 8)
12184
      begin
12185
        case (i_length[1:0])
12186
        2'h0: // Interrupt is generated
12187
        begin
12188
          // enable interrupt generation
12189
          set_rx_bd(0, 0, 1'b1, (`MEMORY_BASE + i_length[1:0]));
12190
          // interrupts are unmasked
12191
        end
12192
        2'h1: // Interrupt is not generated
12193
        begin
12194
          // enable interrupt generation
12195
          set_rx_bd(0, 0, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
12196
          // interrupts are masked
12197
        end
12198
        2'h2: // Interrupt is not generated
12199
        begin
12200
          // disable interrupt generation
12201
          set_rx_bd(0, 0, 1'b0, (`MEMORY_BASE + i_length[1:0]));
12202
          // interrupts are unmasked
12203
        end
12204
        default: // 2'h3: // Interrupt is not generated
12205
        begin
12206
          // disable interrupt generation
12207
          set_rx_bd(0, 0, 1'b0, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
12208
          // interrupts are masked
12209
        end
12210
        endcase
12211
        // set wrap bit
12212
        set_rx_bd_wrap(0);
12213
      end
12214
      // after first 8 number of frames, 128 frames form RX BD 0 to 127 will be received
12215
      else if ((num_of_frames - 8) == 0)
12216
      begin
12217
        tmp_len = i_length; // length of frame
12218
        tmp_bd_num = 0; // RX BD number
12219
        while (tmp_bd_num < 128) // (tmp_len <= (max_tmp - 4)) - this is the last frame
12220
        begin
12221
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
12222
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
12223
          if (tmp_len[0] == 0)
12224
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], (`MEMORY_BASE + tmp_len[1:0]));
12225
          else
12226
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
12227
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
12228
          if ((tmp_len + 4) < (min_tmp + 128))
12229
            tmp_len = tmp_len + 1;
12230
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12231
            tmp_len = 256;
12232
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12233
            tmp_len = tmp_len + 128;
12234
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
12235
            tmp_len = max_tmp - (4 + 16);
12236
          else if ((tmp_len + 4) >= (max_tmp - 16))
12237
            tmp_len = tmp_len + 1;
12238
          // set RX BD number
12239
          tmp_bd_num = tmp_bd_num + 1;
12240
        end
12241
        // set wrap bit
12242
        set_rx_bd_wrap(127);
12243
      end
12244
      // after 128 + first 8 number of frames, 19 frames form RX BD 0 to 18 will be received
12245
      else if ((num_of_frames - 8) == 20) // 128
12246
      begin
12247
        tmp_len = tmp_len; // length of frame remaines from previous settings
12248
        tmp_bd_num = 0; // TX BD number
12249
        while (tmp_bd_num < 19) // (tmp_len <= (max_tmp - 4)) - this is the last frame
12250
        begin
12251
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
12252
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
12253
          if (tmp_len[0] == 0)
12254
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], (`MEMORY_BASE + tmp_len[1:0]));
12255
          else
12256
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
12257
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
12258
          if ((tmp_len + 4) < (min_tmp + 128))
12259
            tmp_len = tmp_len + 1;
12260
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12261
            tmp_len = 256;
12262
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12263
            tmp_len = tmp_len + 128;
12264
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
12265
            tmp_len = max_tmp - (4 + 16);
12266
          else if ((tmp_len + 4) >= (max_tmp - 16))
12267
            tmp_len = tmp_len + 1;
12268
          // set TX BD number
12269
          tmp_bd_num = tmp_bd_num + 1;
12270
        end
12271
      end
12272
      // set empty bit
12273
      if (num_of_frames < 8)
12274
        set_rx_bd_empty(0, 0);
12275
      else if ((num_of_frames - 8) < 128)
12276
        set_rx_bd_empty((num_of_frames - 8), (num_of_frames - 8));
12277
      else if ((num_of_frames - 136) < 19)
12278
        set_rx_bd_empty((num_of_frames - 136), (num_of_frames - 136));
12279
      // CHECK END OF RECEIVE
12280
      fork
12281
        begin
12282
          if (i_length[0] == 1'b0)
12283
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
12284
          else
12285
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
12286
          repeat(10) @(posedge mrx_clk);
12287
        end
12288
        begin
12289
          #1 check_rx_bd(num_of_bd, data);
12290
          if (i_length < min_tmp) // just first four
12291
          begin
12292
            while (data[15] === 1)
12293
            begin
12294
              #1 check_rx_bd(num_of_bd, data);
12295
              @(posedge wb_clk);
12296
            end
12297
            repeat (1) @(posedge wb_clk);
12298
          end
12299
          else
12300
          begin
12301
            wait (MRxDV === 1'b1); // start transmit
12302
            #1 check_rx_bd(num_of_bd, data);
12303
            if (data[15] !== 1)
12304
            begin
12305
              test_fail("Wrong buffer descriptor's ready bit read out from MAC");
12306
              fail = fail + 1;
12307
            end
12308
            wait (MRxDV === 1'b0); // end transmit
12309
            while (data[15] === 1)
12310
            begin
12311
              #1 check_rx_bd(num_of_bd, data);
12312
              @(posedge wb_clk);
12313
            end
12314
            repeat (1) @(posedge wb_clk);
12315
          end
12316
        end
12317
      join
12318
      // check length of a PACKET
12319
      if (data[31:16] != (i_length + 4))
12320
      begin
12321
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
12322
                        data[31:16], (i_length + 4));
12323
        test_fail("Wrong length of the packet out from PHY");
12324
        fail = fail + 1;
12325
      end
12326
      // checking in the following if statement is performed only for first and last 64 lengths
12327
      // check received RX packet data and CRC
12328
      if (i_length[0] == 1'b0)
12329
      begin
12330
        check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
12331
      end
12332
      else
12333
      begin
12334
        check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
12335
      end
12336
      if (tmp > 0)
12337
      begin
12338
        `TIME; $display("*E Wrong data of the received packet");
12339
        test_fail("Wrong data of the received packet");
12340
        fail = fail + 1;
12341
      end
12342
      // check WB INT signal
12343
      if (i_length[1:0] == 2'h0)
12344
      begin
12345
        if (wb_int !== 1'b1)
12346
        begin
12347
          `TIME; $display("*E WB INT signal should be set");
12348
          test_fail("WB INT signal should be set");
12349
          fail = fail + 1;
12350
        end
12351
      end
12352
      else
12353
      begin
12354
        if (wb_int !== 1'b0)
12355
        begin
12356
          `TIME; $display("*E WB INT signal should not be set");
12357
          test_fail("WB INT signal should not be set");
12358
          fail = fail + 1;
12359
        end
12360
      end
12361
      // check RX buffer descriptor of a packet
12362
      check_rx_bd(num_of_bd, data);
12363
      if (i_length[1] == 1'b0) // interrupt enabled
12364
      begin
12365
        if ( ((data[15:0] !== 16'h6000) && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
12366
             ((data[15:0] !== 16'h4000) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
12367
        begin
12368
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
12369
          test_fail("RX buffer descriptor status is not correct");
12370
          fail = fail + 1;
12371
        end
12372
      end
12373
      else // interrupt not enabled
12374
      begin
12375
        if ( ((data[15:0] !== 16'h2000)  && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
12376
             ((data[15:0] !== 16'h0000) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
12377
        begin
12378
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
12379
          test_fail("RX buffer descriptor status is not correct");
12380
          fail = fail + 1;
12381
        end
12382
      end
12383
      // clear first half of 8 frames from RX buffer descriptor 0
12384
      if (num_of_frames < 4)
12385
        clear_rx_bd(num_of_bd, num_of_bd);
12386
      // clear BD with wrap bit
12387
      if (num_of_frames == 140)
12388
        clear_rx_bd(127, 127);
12389
      // check interrupts
12390
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12391
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
12392
      begin
12393
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
12394
        begin
12395
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
12396
          test_fail("Interrupt Receive Buffer was not set");
12397
          fail = fail + 1;
12398
        end
12399
        if ((data & (~`ETH_INT_RXB)) !== 0)
12400
        begin
12401
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
12402
          test_fail("Other interrupts (except Receive Buffer) were set");
12403
          fail = fail + 1;
12404
        end
12405
      end
12406
      else
12407
      begin
12408
        if (data !== 0)
12409
        begin
12410
          `TIME; $display("*E Any of interrupts (except Receive Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
12411
          test_fail("Any of interrupts (except Receive Buffer) was set");
12412
          fail = fail + 1;
12413
        end
12414
      end
12415
      // clear interrupts
12416
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12417
      // check WB INT signal
12418
      if (wb_int !== 1'b0)
12419
      begin
12420
        test_fail("WB INT signal should not be set");
12421
        fail = fail + 1;
12422
      end
12423
      // INTERMEDIATE DISPLAYS
12424
      if ((i_length + 4) == (min_tmp + 7))
12425
      begin
12426
        // starting length is min_tmp, ending length is (min_tmp + 128)
12427
        $display("    receive small packets is NOT selected");
12428
        $display("    using only RX BD 0 out of 128 BDs assigned to RX (wrap at first BD - RX BD 0)");
12429
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
12430
                 min_tmp, (min_tmp + 7));
12431
        $display("    ->all packets were received on RX BD 0");
12432
        // reset receive small, remain the rest
12433
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12434
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12435
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12436
      end
12437
      else if ((i_length + 4) == (min_tmp + 128))
12438
      begin
12439
        // starting length is min_tmp, ending length is (min_tmp + 128)
12440
        $display("    receive small packets is NOT selected");
12441
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12442
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
12443
                 (min_tmp + 8), (min_tmp + 128));
12444
        $display("    ->packets were received on RX BD %0d to RX BD %0d respectively",
12445
                 1'b0, num_of_bd);
12446
        tmp_bd = num_of_bd + 1;
12447
        // set receive small, remain the rest
12448
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12449
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12450
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12451
      end
12452
      else if ((i_length + 4) == (max_tmp - 16))
12453
      begin
12454
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
12455
        $display("    receive small packets is selected");
12456
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12457
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
12458
                 (min_tmp + 64 + 128), tmp_data);
12459
        if (tmp_bd > num_of_bd)
12460
          $display("    ->packets were received from RX BD %0d to RX BD 127 and from RX BD 0 to RX BD %0d respectively",
12461
                   tmp_bd, num_of_bd);
12462
        else
12463
          $display("    ->packets were received from RX BD %0d to RX BD %0d respectively",
12464
                   tmp_bd, num_of_bd);
12465
        tmp_bd = num_of_bd + 1;
12466
        // reset receive small, remain the rest
12467
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12468
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12469
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12470
      end
12471
      else if ((i_length + 4) == max_tmp)
12472
      begin
12473
        $display("    receive small packets is NOT selected");
12474
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12475
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
12476
                 (max_tmp - (4 + 16)), max_tmp);
12477
        if (tmp_bd > num_of_bd)
12478
          $display("    ->packets were received from RX BD %0d to RX BD 127 and from RX BD 0 to RX BD %0d respectively",
12479
                   tmp_bd, num_of_bd);
12480
        else
12481
          $display("    ->packets were received from RX BD %0d to RX BD %0d respectively",
12482
                   tmp_bd, num_of_bd);
12483
      end
12484
      // set length (loop variable)
12485
      if ((i_length + 4) < (min_tmp + 128))
12486
        i_length = i_length + 1;
12487
      else if ( ((i_length + 4) == (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
12488
        i_length = 256;
12489
      else if ( ((i_length + 4) > (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
12490
      begin
12491
        i_length = i_length + 128;
12492
        tmp_data = i_length + 4; // last tmp_data is ending length
12493
      end
12494
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
12495
        i_length = max_tmp - (4 + 16);
12496
      else if ((i_length + 4) >= (max_tmp - 16))
12497
        i_length = i_length + 1;
12498
      else
12499
      begin
12500
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
12501
        #10 $stop;
12502
      end
12503
      // the number of frame transmitted
12504
      num_of_frames = num_of_frames + 1;
12505
      if ((num_of_frames <= 8) || ((num_of_frames - 8) == 128))
12506
        num_of_bd = 0;
12507
      else
12508
        num_of_bd = num_of_bd + 1;
12509
    end
12510
    // disable RX
12511
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12512
              `ETH_MODER_PRO | `ETH_MODER_BRO,
12513
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12514
    @(posedge wb_clk);
12515
    if(fail == 0)
12516
      test_ok;
12517
    else
12518
      fail = 0;
12519
  end
12520
 
12521
 
12522
  ////////////////////////////////////////////////////////////////////
12523
  ////                                                            ////
12524
  ////  Test receive packets form MINFL to MAXFL sizes at         ////
12525
  ////  maximum RX buffer decriptors ( 100Mbps ).                 ////
12526
  ////                                                            ////
12527
  ////////////////////////////////////////////////////////////////////
12528 243 tadejm
  if (test_num == 7) // 
12529 209 tadejm
  begin
12530 243 tadejm
    // TEST 7: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 100Mbps )
12531
    test_name = "TEST 7: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 100Mbps )";
12532
    `TIME; $display("  TEST 7: RECEIVE PACKETS FROM MINFL TO MAXFL SIZES AT MAX RX BDs ( 100Mbps )");
12533 209 tadejm
 
12534
    // reset MAC registers
12535
    hard_reset;
12536
    // reset MAC and MII LOGIC with soft reset
12537
    reset_mac;
12538
    reset_mii;
12539
    // set wb slave response
12540
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
12541
 
12542
    max_tmp = 0;
12543
    min_tmp = 0;
12544
    num_of_frames = 0;
12545
    num_of_bd = 0;
12546
    // set maximum RX buffer descriptors (128) - must be set before RX enable
12547
    wbm_write(`ETH_TX_BD_NUM, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12548
    // enable RX, set full-duplex mode, NO receive small, NO correct IFG
12549
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12550
              `ETH_MODER_PRO | `ETH_MODER_BRO,
12551
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12552
    // prepare two packets of MAXFL length
12553
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12554
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
12555
    min_tmp = tmp[31:16];
12556
    st_data = 8'hAC;
12557
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
12558
    st_data = 8'h35;
12559
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
12560
    // check WB INT signal
12561
    if (wb_int !== 1'b0)
12562
    begin
12563
      test_fail("WB INT signal should not be set");
12564
      fail = fail + 1;
12565
    end
12566
 
12567
    // write to phy's control register for 100Mbps
12568
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
12569
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
12570
    speed = 100;
12571
 
12572
    i_length = (min_tmp - 4);
12573
    while (i_length <= (max_tmp - 4))
12574
    begin
12575
      // append CRC to packet
12576
      if (i_length[0] == 1'b0)
12577
        append_rx_crc (0, i_length, 1'b0, 1'b0);
12578
      else
12579
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
12580
      // choose generating carrier sense and collision
12581
      case (i_length[1:0])
12582
      2'h0: // Interrupt is generated
12583
      begin
12584
        // Reset_tx_bd nable interrupt generation
12585
        // unmask interrupts
12586
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
12587
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12588
        // not detect carrier sense in FD and no collision
12589
        eth_phy.no_carrier_sense_rx_fd_detect(0);
12590
        eth_phy.collision(0);
12591
      end
12592
      2'h1: // Interrupt is not generated
12593
      begin
12594
        // set_tx_bd enable interrupt generation
12595
        // mask interrupts
12596
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12597
        // detect carrier sense in FD and no collision
12598
        eth_phy.no_carrier_sense_rx_fd_detect(1);
12599
        eth_phy.collision(0);
12600
      end
12601
      2'h2: // Interrupt is not generated
12602
      begin
12603
        // set_tx_bd disable the interrupt generation
12604
        // unmask interrupts
12605
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
12606
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12607
        // not detect carrier sense in FD and set collision
12608
        eth_phy.no_carrier_sense_rx_fd_detect(0);
12609
        eth_phy.collision(1);
12610
      end
12611
      default: // 2'h3: // Interrupt is not generated
12612
      begin
12613
        // set_tx_bd disable the interrupt generation
12614
        // mask interrupts
12615
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12616
        // detect carrier sense in FD and set collision
12617
        eth_phy.no_carrier_sense_rx_fd_detect(1);
12618
        eth_phy.collision(1);
12619
      end
12620
      endcase
12621
      // first 8 frames are received with RX BD 0 (wrap bit on RX BD 0)
12622
      // number of all frames is 154 (146 without first 8)
12623
      if (num_of_frames < 8)
12624
      begin
12625
        case (i_length[1:0])
12626
        2'h0: // Interrupt is generated
12627
        begin
12628
          // enable interrupt generation
12629
          set_rx_bd(0, 0, 1'b1, (`MEMORY_BASE + i_length[1:0]));
12630
          // interrupts are unmasked
12631
        end
12632
        2'h1: // Interrupt is not generated
12633
        begin
12634
          // enable interrupt generation
12635
          set_rx_bd(0, 0, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
12636
          // interrupts are masked
12637
        end
12638
        2'h2: // Interrupt is not generated
12639
        begin
12640
          // disable interrupt generation
12641
          set_rx_bd(0, 0, 1'b0, (`MEMORY_BASE + i_length[1:0]));
12642
          // interrupts are unmasked
12643
        end
12644
        default: // 2'h3: // Interrupt is not generated
12645
        begin
12646
          // disable interrupt generation
12647
          set_rx_bd(0, 0, 1'b0, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
12648
          // interrupts are masked
12649
        end
12650
        endcase
12651
        // set wrap bit
12652
        set_rx_bd_wrap(0);
12653
      end
12654
      // after first 8 number of frames, 128 frames form RX BD 0 to 127 will be received
12655
      else if ((num_of_frames - 8) == 0)
12656
      begin
12657
        tmp_len = i_length; // length of frame
12658
        tmp_bd_num = 0; // RX BD number
12659
        while (tmp_bd_num < 128) // (tmp_len <= (max_tmp - 4)) - this is the last frame
12660
        begin
12661
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
12662
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
12663
          if (tmp_len[0] == 0)
12664
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], (`MEMORY_BASE + tmp_len[1:0]));
12665
          else
12666
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
12667
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
12668
          if ((tmp_len + 4) < (min_tmp + 128))
12669
            tmp_len = tmp_len + 1;
12670
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12671
            tmp_len = 256;
12672
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12673
            tmp_len = tmp_len + 128;
12674
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
12675
            tmp_len = max_tmp - (4 + 16);
12676
          else if ((tmp_len + 4) >= (max_tmp - 16))
12677
            tmp_len = tmp_len + 1;
12678
          // set RX BD number
12679
          tmp_bd_num = tmp_bd_num + 1;
12680
        end
12681
        // set wrap bit
12682
        set_rx_bd_wrap(127);
12683
      end
12684
      // after 128 + first 8 number of frames, 19 frames form RX BD 0 to 18 will be received
12685
      else if ((num_of_frames - 8) == 20) // 128
12686
      begin
12687
        tmp_len = tmp_len; // length of frame remaines from previous settings
12688
        tmp_bd_num = 0; // TX BD number
12689
        while (tmp_bd_num < 19) // (tmp_len <= (max_tmp - 4)) - this is the last frame
12690
        begin
12691
          // if i_length[1] == 0 then enable interrupt generation otherwise disable it
12692
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
12693
          if (tmp_len[0] == 0)
12694
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], (`MEMORY_BASE + tmp_len[1:0]));
12695
          else
12696
            set_rx_bd(tmp_bd_num, tmp_bd_num, !tmp_len[1], ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
12697
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
12698
          if ((tmp_len + 4) < (min_tmp + 128))
12699
            tmp_len = tmp_len + 1;
12700
          else if ( ((tmp_len + 4) == (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12701
            tmp_len = 256;
12702
          else if ( ((tmp_len + 4) > (min_tmp + 128)) && ((tmp_len + 4) <= (max_tmp - 256)) )
12703
            tmp_len = tmp_len + 128;
12704
          else if ( ((tmp_len + 4) > (max_tmp - 256)) && ((tmp_len + 4) < (max_tmp - 16)) )
12705
            tmp_len = max_tmp - (4 + 16);
12706
          else if ((tmp_len + 4) >= (max_tmp - 16))
12707
            tmp_len = tmp_len + 1;
12708
          // set TX BD number
12709
          tmp_bd_num = tmp_bd_num + 1;
12710
        end
12711
      end
12712
      // set empty bit
12713
      if (num_of_frames < 8)
12714
        set_rx_bd_empty(0, 0);
12715
      else if ((num_of_frames - 8) < 128)
12716
        set_rx_bd_empty((num_of_frames - 8), (num_of_frames - 8));
12717
      else if ((num_of_frames - 136) < 19)
12718
        set_rx_bd_empty((num_of_frames - 136), (num_of_frames - 136));
12719
      // CHECK END OF RECEIVE
12720
      fork
12721
        begin
12722
          if (i_length[0] == 1'b0)
12723
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
12724
          else
12725
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
12726
          repeat(10) @(posedge mrx_clk);
12727
        end
12728
        begin
12729
          #1 check_rx_bd(num_of_bd, data);
12730
          if (i_length < min_tmp) // just first four
12731
          begin
12732
            while (data[15] === 1)
12733
            begin
12734
              #1 check_rx_bd(num_of_bd, data);
12735
              @(posedge wb_clk);
12736
            end
12737
            repeat (1) @(posedge wb_clk);
12738
          end
12739
          else
12740
          begin
12741
            wait (MRxDV === 1'b1); // start transmit
12742
            #1 check_rx_bd(num_of_bd, data);
12743
            if (data[15] !== 1)
12744
            begin
12745
              test_fail("Wrong buffer descriptor's ready bit read out from MAC");
12746
              fail = fail + 1;
12747
            end
12748
            wait (MRxDV === 1'b0); // end transmit
12749
            while (data[15] === 1)
12750
            begin
12751
              #1 check_rx_bd(num_of_bd, data);
12752
              @(posedge wb_clk);
12753
            end
12754
            repeat (1) @(posedge wb_clk);
12755
          end
12756
        end
12757
      join
12758
      // check length of a PACKET
12759
      if (data[31:16] != (i_length + 4))
12760
      begin
12761
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
12762
                        data[31:16], (i_length + 4));
12763
        test_fail("Wrong length of the packet out from PHY");
12764
        fail = fail + 1;
12765
      end
12766
      // check received RX packet data and CRC
12767
      if (i_length[0] == 1'b0)
12768
      begin
12769
        check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
12770
      end
12771
      else
12772
      begin
12773
        check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
12774
      end
12775
      if (tmp > 0)
12776
      begin
12777
        `TIME; $display("*E Wrong data of the received packet");
12778
        test_fail("Wrong data of the received packet");
12779
        fail = fail + 1;
12780
      end
12781
      // check WB INT signal
12782
      if (i_length[1:0] == 2'h0)
12783
      begin
12784
        if (wb_int !== 1'b1)
12785
        begin
12786
          `TIME; $display("*E WB INT signal should be set");
12787
          test_fail("WB INT signal should be set");
12788
          fail = fail + 1;
12789
        end
12790
      end
12791
      else
12792
      begin
12793
        if (wb_int !== 1'b0)
12794
        begin
12795
          `TIME; $display("*E WB INT signal should not be set");
12796
          test_fail("WB INT signal should not be set");
12797
          fail = fail + 1;
12798
        end
12799
      end
12800
      // check RX buffer descriptor of a packet
12801
      check_rx_bd(num_of_bd, data);
12802
      if (i_length[1] == 1'b0) // interrupt enabled
12803
      begin
12804
        if ( ((data[15:0] !== 16'h6000) && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
12805
             ((data[15:0] !== 16'h4000) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
12806
        begin
12807
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
12808
          test_fail("RX buffer descriptor status is not correct");
12809
          fail = fail + 1;
12810
        end
12811
      end
12812
      else // interrupt not enabled
12813
      begin
12814
        if ( ((data[15:0] !== 16'h2000)  && ((num_of_frames < 8) || ((num_of_frames - 8) == 127))) || // wrap bit
12815
             ((data[15:0] !== 16'h0000) && (num_of_frames >= 8) && ((num_of_frames - 8) != 127)) ) // without wrap bit
12816
        begin
12817
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
12818
          test_fail("RX buffer descriptor status is not correct");
12819
          fail = fail + 1;
12820
        end
12821
      end
12822
      // clear first half of 8 frames from RX buffer descriptor 0
12823
      if (num_of_frames < 4)
12824
        clear_rx_bd(num_of_bd, num_of_bd);
12825
      // clear BD with wrap bit
12826
      if (num_of_frames == 140)
12827
        clear_rx_bd(127, 127);
12828
      // check interrupts
12829
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12830
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
12831
      begin
12832
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
12833
        begin
12834
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
12835
          test_fail("Interrupt Receive Buffer was not set");
12836
          fail = fail + 1;
12837
        end
12838
        if ((data & (~`ETH_INT_RXB)) !== 0)
12839
        begin
12840
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
12841
          test_fail("Other interrupts (except Receive Buffer) were set");
12842
          fail = fail + 1;
12843
        end
12844
      end
12845
      else
12846
      begin
12847
        if (data !== 0)
12848
        begin
12849
          `TIME; $display("*E Any of interrupts (except Receive Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
12850
          test_fail("Any of interrupts (except Receive Buffer) was set");
12851
          fail = fail + 1;
12852
        end
12853
      end
12854
      // clear interrupts
12855
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12856
      // check WB INT signal
12857
      if (wb_int !== 1'b0)
12858
      begin
12859
        test_fail("WB INT signal should not be set");
12860
        fail = fail + 1;
12861
      end
12862
      // INTERMEDIATE DISPLAYS
12863
      if ((i_length + 4) == (min_tmp + 7))
12864
      begin
12865
        // starting length is min_tmp, ending length is (min_tmp + 128)
12866
        $display("    receive small packets is NOT selected");
12867
        $display("    using only RX BD 0 out of 128 BDs assigned to RX (wrap at first BD - RX BD 0)");
12868
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
12869
                 min_tmp, (min_tmp + 7));
12870
        $display("    ->all packets were received on RX BD 0");
12871
        // reset receive small, remain the rest
12872
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12873
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12874
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12875
      end
12876
      else if ((i_length + 4) == (min_tmp + 128))
12877
      begin
12878
        // starting length is min_tmp, ending length is (min_tmp + 128)
12879
        $display("    receive small packets is NOT selected");
12880
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12881
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
12882
                 (min_tmp + 8), (min_tmp + 128));
12883
        $display("    ->packets were received on RX BD %0d to RX BD %0d respectively",
12884
                 1'b0, num_of_bd);
12885
        tmp_bd = num_of_bd + 1;
12886
        // set receive small, remain the rest
12887
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12888
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12889
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12890
      end
12891
      else if ((i_length + 4) == (max_tmp - 16))
12892
      begin
12893
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
12894
        $display("    receive small packets is selected");
12895
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12896
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
12897
                 (min_tmp + 64 + 128), tmp_data);
12898
        if (tmp_bd > num_of_bd)
12899
          $display("    ->packets were received from RX BD %0d to RX BD 127 and from RX BD 0 to RX BD %0d respectively",
12900
                   tmp_bd, num_of_bd);
12901
        else
12902
          $display("    ->packets were received from RX BD %0d to RX BD %0d respectively",
12903
                   tmp_bd, num_of_bd);
12904
        tmp_bd = num_of_bd + 1;
12905
        // reset receive small, remain the rest
12906
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
12907
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
12908
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12909
      end
12910
      else if ((i_length + 4) == max_tmp)
12911
      begin
12912
        $display("    receive small packets is NOT selected");
12913
        $display("    using all 128 BDs assigned to RX (wrap at 128th BD - RX BD 127)");
12914
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
12915
                 (max_tmp - (4 + 16)), max_tmp);
12916
        if (tmp_bd > num_of_bd)
12917
          $display("    ->packets were received from RX BD %0d to RX BD 127 and from RX BD 0 to RX BD %0d respectively",
12918
                   tmp_bd, num_of_bd);
12919
        else
12920
          $display("    ->packets were received from RX BD %0d to RX BD %0d respectively",
12921
                   tmp_bd, num_of_bd);
12922
      end
12923
      // set length (loop variable)
12924
      if ((i_length + 4) < (min_tmp + 128))
12925
        i_length = i_length + 1;
12926
      else if ( ((i_length + 4) == (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
12927
        i_length = 256;
12928
      else if ( ((i_length + 4) > (min_tmp + 128)) && ((i_length + 4) <= (max_tmp - 256)) )
12929
      begin
12930
        i_length = i_length + 128;
12931
        tmp_data = i_length + 4; // last tmp_data is ending length
12932
      end
12933
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
12934
        i_length = max_tmp - (4 + 16);
12935
      else if ((i_length + 4) >= (max_tmp - 16))
12936
        i_length = i_length + 1;
12937
      else
12938
      begin
12939
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
12940
        #10 $stop;
12941
      end
12942
      // the number of frame transmitted
12943
      num_of_frames = num_of_frames + 1;
12944
      if ((num_of_frames <= 8) || ((num_of_frames - 8) == 128))
12945
        num_of_bd = 0;
12946
      else
12947
        num_of_bd = num_of_bd + 1;
12948
    end
12949
    // disable RX
12950
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12951
              `ETH_MODER_PRO | `ETH_MODER_BRO,
12952
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12953
    @(posedge wb_clk);
12954
    if(fail == 0)
12955
      test_ok;
12956
    else
12957
      fail = 0;
12958
  end
12959
 
12960
 
12961
  ////////////////////////////////////////////////////////////////////
12962
  ////                                                            ////
12963 243 tadejm
  ////  Test receive packets form 0 to (MINFL + 12) sizes at       ////
12964 209 tadejm
  ////  8 RX buffer decriptors ( 10Mbps ).                        ////
12965
  ////                                                            ////
12966
  ////////////////////////////////////////////////////////////////////
12967 243 tadejm
  if (test_num == 8) // 
12968 209 tadejm
  begin
12969 243 tadejm
    // TEST 8: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 10Mbps )
12970
    test_name = "TEST 8: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 10Mbps )";
12971
    `TIME; $display("  TEST 8: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 10Mbps )");
12972 209 tadejm
 
12973
    // reset MAC registers
12974
    hard_reset;
12975
    // reset MAC and MII LOGIC with soft reset
12976
    reset_mac;
12977
    reset_mii;
12978
    // set wb slave response
12979
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
12980
 
12981
    max_tmp = 0;
12982
    min_tmp = 0;
12983
    // set 8 RX buffer descriptors (120 - 127) - must be set before RX enable
12984
    wbm_write(`ETH_TX_BD_NUM, 32'h78, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12985
    // enable RX, set full-duplex mode, receive small, NO correct IFG
12986
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
12987
              `ETH_MODER_PRO | `ETH_MODER_BRO,
12988
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12989
    // prepare two packets of MAXFL length
12990
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
12991
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
12992
    min_tmp = tmp[31:16];
12993
    st_data = 8'hAC;
12994
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
12995
    st_data = 8'h35;
12996
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
12997
    // check WB INT signal
12998
    if (wb_int !== 1'b0)
12999
    begin
13000
      test_fail("WB INT signal should not be set");
13001
      fail = fail + 1;
13002
    end
13003 243 tadejm
    // unmask interrupts
13004
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
13005
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13006 209 tadejm
 
13007
    // write to phy's control register for 10Mbps
13008
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
13009
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
13010
    speed = 10;
13011
 
13012 243 tadejm
    frame_ended = 0;
13013
    num_of_frames = 0;// 0; // 10;
13014
    num_of_bd = 120;
13015
    i_length = 0 - 4;// (0 - 4); // 6; // 4 less due to CRC
13016
    while ((i_length + 4) < 78) // (min_tmp - 4))
13017 209 tadejm
    begin
13018 243 tadejm
      // append CRC to packet
13019
      if ((i_length[0] == 1'b0) && (num_of_frames > 4))
13020
        append_rx_crc (0, i_length, 1'b0, 1'b0);
13021
      else if (num_of_frames > 4)
13022
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
13023 209 tadejm
      // choose generating carrier sense and collision
13024
      case (i_length[1:0])
13025 243 tadejm
      2'h0:
13026 209 tadejm
      begin
13027
        // not detect carrier sense in FD and no collision
13028 243 tadejm
        eth_phy.no_carrier_sense_rx_fd_detect(0);
13029 209 tadejm
        eth_phy.collision(0);
13030
      end
13031 243 tadejm
      2'h1:
13032 209 tadejm
      begin
13033
        // detect carrier sense in FD and no collision
13034 243 tadejm
        eth_phy.no_carrier_sense_rx_fd_detect(1);
13035 209 tadejm
        eth_phy.collision(0);
13036
      end
13037 243 tadejm
      2'h2:
13038 209 tadejm
      begin
13039
        // not detect carrier sense in FD and set collision
13040 243 tadejm
        eth_phy.no_carrier_sense_rx_fd_detect(0);
13041 209 tadejm
        eth_phy.collision(1);
13042
      end
13043 243 tadejm
      default: // 2'h3:
13044 209 tadejm
      begin
13045
        // detect carrier sense in FD and set collision
13046 243 tadejm
        eth_phy.no_carrier_sense_rx_fd_detect(1);
13047 209 tadejm
        eth_phy.collision(1);
13048
      end
13049
      endcase
13050
      #1;
13051 243 tadejm
      // first 10 frames are received with RX BD 120 (wrap bit on RX BD 120)
13052 209 tadejm
      if (num_of_frames <= 9)
13053
      begin
13054 243 tadejm
        case (i_length[1:0])
13055
        2'h0: // Interrupt is generated
13056
        begin
13057
          // enable interrupt generation
13058
          set_rx_bd(120, 120, 1'b1, (`MEMORY_BASE + i_length[1:0]));
13059
        end
13060
        2'h1: // Interrupt is generated
13061
        begin
13062
          // enable interrupt generation
13063
          set_rx_bd(120, 120, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
13064
        end
13065
        2'h2: // Interrupt is generated
13066
        begin
13067
          // enable interrupt generation
13068
          set_rx_bd(120, 120, 1'b1, (`MEMORY_BASE + i_length[1:0]));
13069
        end
13070
        default: // 2'h3: // Interrupt is generated
13071
        begin
13072
          // enable interrupt generation
13073
          set_rx_bd(120, 120, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
13074
        end
13075
        endcase
13076 209 tadejm
        // set wrap bit
13077 243 tadejm
        set_rx_bd_wrap(120);
13078 209 tadejm
      end
13079 243 tadejm
      // 10 <= num_of_frames < 18 => wrap set to TX BD 123
13080 209 tadejm
      else if ((num_of_frames == 10) || (num_of_frames == 14))
13081
      begin
13082
        tmp_len = i_length; // length of frame
13083 243 tadejm
        tmp_bd_num = 120; // RX BD number
13084
        while (tmp_bd_num < 124) // 
13085 209 tadejm
        begin
13086
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13087
          if (tmp_len[0] == 0)
13088 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13089 209 tadejm
          else
13090 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13091
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13092 209 tadejm
          tmp_len = tmp_len + 1;
13093 243 tadejm
          // set RX BD number
13094 209 tadejm
          tmp_bd_num = tmp_bd_num + 1;
13095
        end
13096
        // set wrap bit
13097 243 tadejm
        set_rx_bd_wrap(123);
13098 209 tadejm
      end
13099 243 tadejm
      // 18 <= num_of_frames < 28 => wrap set to RX BD 124
13100 209 tadejm
      else if ((num_of_frames == 18) || (num_of_frames == 23))
13101
      begin
13102
        tmp_len = i_length; // length of frame
13103 243 tadejm
        tmp_bd_num = 120; // RX BD number
13104
        while (tmp_bd_num < 125) // 
13105 209 tadejm
        begin
13106
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13107
          if (tmp_len[0] == 0)
13108 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13109 209 tadejm
          else
13110 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13111
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13112 209 tadejm
          tmp_len = tmp_len + 1;
13113 243 tadejm
          // set RX BD number
13114 209 tadejm
          tmp_bd_num = tmp_bd_num + 1;
13115
        end
13116
        // set wrap bit
13117 243 tadejm
        set_rx_bd_wrap(124);
13118 209 tadejm
      end
13119 243 tadejm
      // 28 <= num_of_frames < 40 => wrap set to RX BD 125
13120 209 tadejm
      else if ((num_of_frames == 28) || (num_of_frames == 34))
13121
      begin
13122
        tmp_len = i_length; // length of frame
13123 243 tadejm
        tmp_bd_num = 120; // RX BD number
13124
        while (tmp_bd_num < 126) // 
13125 209 tadejm
        begin
13126
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13127
          if (tmp_len[0] == 0)
13128 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13129 209 tadejm
          else
13130 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13131
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13132 209 tadejm
          tmp_len = tmp_len + 1;
13133 243 tadejm
          // set RX BD number
13134 209 tadejm
          tmp_bd_num = tmp_bd_num + 1;
13135
        end
13136
        // set wrap bit
13137 243 tadejm
        set_rx_bd_wrap(125);
13138 209 tadejm
      end
13139 243 tadejm
      // 40 <= num_of_frames < 54 => wrap set to RX BD 126
13140 209 tadejm
      else if ((num_of_frames == 40) || (num_of_frames == 47))
13141
      begin
13142
        tmp_len = i_length; // length of frame
13143 243 tadejm
        tmp_bd_num = 120; // RX BD number
13144
        while (tmp_bd_num < 127) // 
13145 209 tadejm
        begin
13146
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13147
          if (tmp_len[0] == 0)
13148 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13149 209 tadejm
          else
13150 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13151
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13152 209 tadejm
          tmp_len = tmp_len + 1;
13153 243 tadejm
          // set RX BD number
13154 209 tadejm
          tmp_bd_num = tmp_bd_num + 1;
13155
        end
13156
        // set wrap bit
13157 243 tadejm
        set_rx_bd_wrap(126);
13158 209 tadejm
      end
13159 243 tadejm
      // 54 <= num_of_frames < 70 => wrap set to RX BD 127
13160 209 tadejm
      else if ((num_of_frames == 54) || (num_of_frames == 62))
13161
      begin
13162
        tmp_len = i_length; // length of frame
13163 243 tadejm
        tmp_bd_num = 120; // RX BD number
13164
        while (tmp_bd_num < 128) // 
13165 209 tadejm
        begin
13166
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13167
          if (tmp_len[0] == 0)
13168 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13169 209 tadejm
          else
13170 243 tadejm
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13171
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13172 209 tadejm
          tmp_len = tmp_len + 1;
13173 243 tadejm
          // set RX BD number
13174 209 tadejm
          tmp_bd_num = tmp_bd_num + 1;
13175
        end
13176
        // set wrap bit
13177 243 tadejm
        set_rx_bd_wrap(127);
13178 209 tadejm
      end
13179 243 tadejm
      // 70 <= num_of_frames < 78 => wrap set to RX BD 127
13180
      else if (num_of_frames == 70)
13181
      begin
13182
        tmp_len = i_length; // length of frame
13183
        tmp_bd_num = 120; // RX BD number
13184
        while (tmp_bd_num < 128) // 
13185
        begin
13186
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13187
          if (tmp_len[0] == 0)
13188
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13189
          else
13190
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13191
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13192
          tmp_len = tmp_len + 1;
13193
          // set RX BD number
13194
          tmp_bd_num = tmp_bd_num + 1;
13195
        end
13196
        // set wrap bit
13197
        set_rx_bd_wrap(127);
13198
      end
13199 209 tadejm
      #1;
13200 243 tadejm
      // SET empty bit
13201 209 tadejm
      if (num_of_frames < 10)
13202 243 tadejm
        set_rx_bd_empty(120, 120);
13203 209 tadejm
      else if (num_of_frames < 14)
13204 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 10), (120 + num_of_frames - 10));
13205 209 tadejm
      else if (num_of_frames < 18)
13206 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 14), (120 + num_of_frames - 14));
13207 209 tadejm
      else if (num_of_frames < 23)
13208 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 18), (120 + num_of_frames - 18));
13209 209 tadejm
      else if (num_of_frames < 28)
13210 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 23), (120 + num_of_frames - 23));
13211 209 tadejm
      else if (num_of_frames < 34)
13212 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 28), (120 + num_of_frames - 28));
13213 209 tadejm
      else if (num_of_frames < 40)
13214 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 34), (120 + num_of_frames - 34));
13215 209 tadejm
      else if (num_of_frames < 47)
13216 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 40), (120 + num_of_frames - 40));
13217 209 tadejm
      else if (num_of_frames < 54)
13218 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 47), (120 + num_of_frames - 47));
13219 209 tadejm
      else if (num_of_frames < 62)
13220 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 54), (120 + num_of_frames - 54));
13221 209 tadejm
      else if (num_of_frames < 70)
13222 243 tadejm
        set_rx_bd_empty((120 + num_of_frames - 62), (120 + num_of_frames - 62));
13223
      else if (num_of_frames < 78)
13224
        set_rx_bd_empty((120 + num_of_frames - 70), (120 + num_of_frames - 70));
13225
      // CHECK END OF RECEIVE
13226 252 tadejm
      // receive just preamble between some packets
13227
      if ((num_of_frames == 0) || (num_of_frames == 4) || (num_of_frames == 9))
13228
      begin
13229
        #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h6, 8'h55, 0, 0, 1'b0);
13230
        @(posedge mrx_clk);
13231
        wait (MRxDV === 1'b0); // end receive
13232
        repeat(10) @(posedge mrx_clk);
13233
        repeat(15) @(posedge wb_clk);
13234
      end
13235
      // receiving frames and checking end of them
13236 243 tadejm
      frame_ended = 0;
13237
      check_frame = 0;
13238 209 tadejm
      fork
13239
        begin
13240 243 tadejm
          if (i_length[0] == 1'b0)
13241
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
13242
          else
13243
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
13244
          repeat(10) @(posedge mrx_clk);
13245
        end
13246
        begin: fr_end1
13247
          wait (MRxDV === 1'b1); // start receive
13248
          #1 check_rx_bd(num_of_bd, data);
13249
          if (data[15] !== 1)
13250 209 tadejm
          begin
13251 243 tadejm
            test_fail("Wrong buffer descriptor's empty bit read out from MAC");
13252
            fail = fail + 1;
13253 209 tadejm
          end
13254 243 tadejm
          wait (MRxDV === 1'b0); // end receive
13255
          while ((data[15] === 1) && (check_frame == 0))
13256
          begin
13257
            #1 check_rx_bd(num_of_bd, data);
13258
            @(posedge wb_clk);
13259
          end
13260
          if (data[15] === 0)
13261
            frame_ended = 1;
13262
          repeat (1) @(posedge wb_clk);
13263 209 tadejm
        end
13264 243 tadejm
        begin
13265
          wait (MRxDV === 1'b1); // start receive
13266
          wait (MRxDV === 1'b0); // end receive
13267
          repeat(10) @(posedge mrx_clk);
13268
          repeat(15) @(posedge wb_clk);
13269
          check_frame = 1;
13270
        end
13271
      join
13272
      // check length of a PACKET
13273
      if ( ((data[31:16] != (i_length + 4)) && (num_of_frames >= 3)) ||
13274
           ((data[31:16] != 0) && (num_of_frames < 3)) )
13275
      begin
13276
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
13277
                        data[31:16], (i_length + 4));
13278
        test_fail("Wrong length of the packet out from PHY");
13279
        fail = fail + 1;
13280
      end
13281
      // check received RX packet data and CRC
13282
      if ((frame_ended == 1) && (num_of_frames >= 5)) // 5 bytes is minimum size without CRC error, since
13283
      begin                                           // CRC has 4 bytes for itself
13284
        if (i_length[0] == 1'b0)
13285
        begin
13286
          check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
13287
        end
13288 209 tadejm
        else
13289
        begin
13290 243 tadejm
          check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
13291 209 tadejm
        end
13292 243 tadejm
        if (tmp > 0)
13293
        begin
13294
          `TIME; $display("*E Wrong data of the received packet");
13295
          test_fail("Wrong data of the received packet");
13296
          fail = fail + 1;
13297
        end
13298 209 tadejm
      end
13299 243 tadejm
      // check WB INT signal
13300
      if (num_of_frames >= 3) // Frames smaller than 3 are not received.
13301
      begin                   // Frames greater then 5 always cause an interrupt (Frame received)
13302
        if (wb_int !== 1'b1)  // Frames with length 3 or 4 always cause an interrupt (CRC error)
13303
        begin
13304
          `TIME; $display("*E WB INT signal should be set");
13305
          test_fail("WB INT signal should be set");
13306
          fail = fail + 1;
13307
        end
13308
      end
13309
      else
13310
      begin
13311
        if (wb_int !== 1'b0)
13312
        begin
13313
          `TIME; $display("*E WB INT signal should not be set");
13314
          test_fail("WB INT signal should not be set");
13315
          fail = fail + 1;
13316
        end
13317
      end
13318
      // check RX buffer descriptor of a packet
13319 254 mohor
      // check RX buffer descriptor of a packet
13320 243 tadejm
      if (num_of_frames >= min_tmp)
13321
      begin
13322 254 mohor
        if ( (data[15:0] !== 16'h6080) && // wrap bit
13323
             (data[15:0] !== 16'h4080) ) // without wrap bit
13324 243 tadejm
        begin
13325
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13326
          test_fail("RX buffer descriptor status is not correct");
13327
          fail = fail + 1;
13328
        end
13329
      end
13330 254 mohor
      else if (num_of_frames > 6)
13331 243 tadejm
      begin
13332 254 mohor
        if ( (data[15:0] !== 16'h6084) && // wrap bit
13333
             (data[15:0] !== 16'h4084) ) // without wrap bit
13334
        begin
13335
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13336
          test_fail("RX buffer descriptor status is not correct");
13337
          fail = fail + 1;
13338
        end
13339
      end
13340
      else if (num_of_frames > 4) // MAC does not recognize Dest. ADDR. for lengths 5, 6 => no MISS
13341
      begin
13342 243 tadejm
        if ( (data[15:0] !== 16'h6004) && // wrap bit
13343
             (data[15:0] !== 16'h4004) ) // without wrap bit
13344
        begin
13345
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13346
          test_fail("RX buffer descriptor status is not correct");
13347
          fail = fail + 1;
13348
        end
13349
      end
13350 254 mohor
      else if (num_of_frames > 2) // MAC does not recognize Dest. ADDR. for length 3, 4 => no MISS, CRC ERROR
13351 243 tadejm
      begin
13352
        if ( (data[15:0] !== 16'h6006) && // wrap bit
13353
             (data[15:0] !== 16'h4006) ) // without wrap bit
13354
        begin
13355
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13356
          test_fail("RX buffer descriptor status is not correct");
13357
          fail = fail + 1;
13358
        end
13359
      end
13360
      else
13361
      begin
13362
        if (data[15] !== 1'b1)
13363
        begin
13364
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13365
          test_fail("RX buffer descriptor status is not correct");
13366
          fail = fail + 1;
13367
        end
13368
      end
13369
      // check interrupts
13370
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13371 209 tadejm
      if (num_of_frames >= 5)
13372
      begin
13373 243 tadejm
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
13374 209 tadejm
        begin
13375 243 tadejm
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
13376
          test_fail("Interrupt Receive Buffer was not set");
13377
          fail = fail + 1;
13378 209 tadejm
        end
13379 243 tadejm
        if ((data & (~`ETH_INT_RXB)) !== 0)
13380 209 tadejm
        begin
13381 243 tadejm
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
13382
          test_fail("Other interrupts (except Receive Buffer) were set");
13383
          fail = fail + 1;
13384 209 tadejm
        end
13385 243 tadejm
      end
13386
      else if ((num_of_frames < 3)) // Frames smaller than 3 are not received.
13387
      begin
13388
        if (data) // Checking if any interrupt is pending)
13389 209 tadejm
        begin
13390 243 tadejm
          `TIME; $display("*E Interrupt(s) is(are) pending although frame was ignored, interrupt reg: %0h", data);
13391
          test_fail("Interrupts were set");
13392
          fail = fail + 1;
13393
        end
13394
      end
13395
      else
13396
      begin
13397
        if ((data & `ETH_INT_RXE) !== `ETH_INT_RXE)
13398
        begin
13399
          `TIME; $display("*E Interrupt Receive Buffer Error was not set, interrupt reg: %0h", data);
13400
          test_fail("Interrupt Receive Buffer Error was not set");
13401
          fail = fail + 1;
13402
        end
13403
        if ((data & (~`ETH_INT_RXE)) !== 0)
13404
        begin
13405
          `TIME; $display("*E Other interrupts (except Receive Buffer Error) were set, interrupt reg: %0h", data);
13406
          test_fail("Other interrupts (except Receive Buffer Error) were set");
13407
          fail = fail + 1;
13408
        end
13409
      end
13410
      // clear interrupts
13411
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13412
      // check WB INT signal
13413
      if (wb_int !== 1'b0)
13414
      begin
13415
        test_fail("WB INT signal should not be set");
13416
        fail = fail + 1;
13417
      end
13418
      // INTERMEDIATE DISPLAYS
13419
      if (num_of_frames == 3)
13420
      begin
13421
        $display("    using 1 BD out of 8 BDs (120..127) assigned to RX (wrap at 1st BD - RX BD 120)");
13422
        $display("    ->packets with lengths from %0d to %0d are not received (length increasing by 1 byte)",
13423
                 0, 3);
13424
      end
13425
      else if (num_of_frames == 9)
13426
      begin
13427
        $display("    using 1 BD out of 8 BDs (120..127) assigned to RX (wrap at 1st BD - RX BD 120)");
13428
        $display("    ->packet with length 4 is not received (length increasing by 1 byte)");
13429
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13430
                 5, 9);
13431
      end
13432
      else if (num_of_frames == 17)
13433
      begin
13434
        $display("    using 4 BDs out of 8 BDs (120..127) assigned to RX (wrap at 4th BD - RX BD 123)");
13435
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13436
                 10, 17);
13437
      end
13438
      else if (num_of_frames == 27)
13439
      begin
13440
        $display("    using 5 BDs out of 8 BDs (120..127) assigned to RX (wrap at 5th BD - RX BD 124)");
13441
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13442
                 18, 27);
13443
      end
13444
      else if (num_of_frames == 40)
13445
      begin
13446
        $display("    using 6 BDs out of 8 BDs (120..127) assigned to RX (wrap at 6th BD - RX BD 125)");
13447
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13448
                 28, 40);
13449
      end
13450
      else if (num_of_frames == 54)
13451
      begin
13452
        $display("    using 7 BDs out of 8 BDs (120..127) assigned to RX (wrap at 7th BD - RX BD 126)");
13453
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13454
                 41, 54);
13455
      end
13456
      else if (num_of_frames == 69)
13457
      begin
13458
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
13459
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13460
                 55, 69);
13461
      end
13462
      else if (num_of_frames == 69)
13463
      begin
13464
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
13465
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13466
                 55, 69);
13467
      end
13468
      else if (num_of_frames == 77)
13469
      begin
13470
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
13471
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13472
                 70, 77);
13473
      end
13474
      // set length (loop variable)
13475
      i_length = i_length + 1;
13476
      // the number of frame transmitted
13477
      num_of_frames = num_of_frames + 1;
13478
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
13479
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
13480
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
13481
          (num_of_frames == 54) || (num_of_frames == 62) || (num_of_frames == 70))
13482
        num_of_bd = 120;
13483
      else
13484
        num_of_bd = num_of_bd + 1;
13485
    end
13486
    // disable RX
13487
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
13488
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13489
    @(posedge wb_clk);
13490
    if(fail == 0)
13491
      test_ok;
13492
    else
13493
      fail = 0;
13494
  end
13495
 
13496
 
13497
  ////////////////////////////////////////////////////////////////////
13498
  ////                                                            ////
13499
  ////  Test receive packets form 0 to (MINFL + 12) sizes at      ////
13500
  ////  8 RX buffer decriptors ( 100Mbps ).                       ////
13501
  ////                                                            ////
13502
  ////////////////////////////////////////////////////////////////////
13503
  if (test_num == 9) // 
13504
  begin
13505
    // TEST 9: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 100Mbps )
13506
    test_name = "TEST 9: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 100Mbps )";
13507
    `TIME; $display("  TEST 9: RECEIVE PACKETS FROM 0 TO (MINFL + 12) SIZES AT 8 TX BD ( 100Mbps )");
13508
 
13509
    // reset MAC registers
13510
    hard_reset;
13511
    // reset MAC and MII LOGIC with soft reset
13512
    reset_mac;
13513
    reset_mii;
13514
    // set wb slave response
13515
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
13516
 
13517
    max_tmp = 0;
13518
    min_tmp = 0;
13519
    // set 8 RX buffer descriptors (120 - 127) - must be set before RX enable
13520
    wbm_write(`ETH_TX_BD_NUM, 32'h78, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13521
    // enable RX, set full-duplex mode, receive small, NO correct IFG
13522
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
13523
              `ETH_MODER_PRO | `ETH_MODER_BRO,
13524
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13525
    // prepare two packets of MAXFL length
13526
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13527
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
13528
    min_tmp = tmp[31:16];
13529
    st_data = 8'hAC;
13530
    set_rx_packet(0, (max_tmp - 4), 1'b0, 48'h0102_0304_0506, 48'h0708_090A_0B0C, 16'h0D0E, st_data); // length without CRC
13531
    st_data = 8'h35;
13532
    set_rx_packet((max_tmp), (max_tmp - 4), 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
13533
    // check WB INT signal
13534
    if (wb_int !== 1'b0)
13535
    begin
13536
      test_fail("WB INT signal should not be set");
13537
      fail = fail + 1;
13538
    end
13539
    // unmask interrupts
13540
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
13541
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13542
 
13543
    // write to phy's control register for 100Mbps
13544
    #Tp eth_phy.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
13545
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset - (10/100), bit 8 set - FD
13546
    speed = 100;
13547
 
13548
    frame_ended = 0;
13549 252 tadejm
    num_of_frames = 0;// 0; // 10;
13550 243 tadejm
    num_of_bd = 120;
13551
    i_length = 0 - 4;// (0 - 4); // 6; // 4 less due to CRC
13552
    while ((i_length + 4) < 78) // (min_tmp - 4))
13553
    begin
13554
      // append CRC to packet
13555 252 tadejm
      if ((i_length[0] == 1'b0) && (num_of_frames > 4))
13556 243 tadejm
        append_rx_crc (0, i_length, 1'b0, 1'b0);
13557 252 tadejm
      else if (num_of_frames > 4)
13558 243 tadejm
        append_rx_crc (max_tmp, i_length, 1'b0, 1'b0);
13559
      // choose generating carrier sense and collision
13560
      case (i_length[1:0])
13561
      2'h0:
13562
      begin
13563
        // not detect carrier sense in FD and no collision
13564
        eth_phy.no_carrier_sense_rx_fd_detect(0);
13565
        eth_phy.collision(0);
13566
      end
13567
      2'h1:
13568
      begin
13569
        // detect carrier sense in FD and no collision
13570
        eth_phy.no_carrier_sense_rx_fd_detect(1);
13571
        eth_phy.collision(0);
13572
      end
13573
      2'h2:
13574
      begin
13575
        // not detect carrier sense in FD and set collision
13576
        eth_phy.no_carrier_sense_rx_fd_detect(0);
13577
        eth_phy.collision(1);
13578
      end
13579
      default: // 2'h3:
13580
      begin
13581
        // detect carrier sense in FD and set collision
13582
        eth_phy.no_carrier_sense_rx_fd_detect(1);
13583
        eth_phy.collision(1);
13584
      end
13585
      endcase
13586
      #1;
13587
      // first 10 frames are received with RX BD 120 (wrap bit on RX BD 120)
13588
      if (num_of_frames <= 9)
13589
      begin
13590
        case (i_length[1:0])
13591
        2'h0: // Interrupt is generated
13592
        begin
13593
          // enable interrupt generation
13594
          set_rx_bd(120, 120, 1'b1, (`MEMORY_BASE + i_length[1:0]));
13595
        end
13596
        2'h1: // Interrupt is generated
13597
        begin
13598
          // enable interrupt generation
13599
          set_rx_bd(120, 120, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
13600
        end
13601
        2'h2: // Interrupt is generated
13602
        begin
13603
          // enable interrupt generation
13604
          set_rx_bd(120, 120, 1'b1, (`MEMORY_BASE + i_length[1:0]));
13605
        end
13606
        default: // 2'h3: // Interrupt is generated
13607
        begin
13608
          // enable interrupt generation
13609
          set_rx_bd(120, 120, 1'b1, ((`MEMORY_BASE + i_length[1:0]) + max_tmp));
13610
        end
13611
        endcase
13612
        // set wrap bit
13613
        set_rx_bd_wrap(120);
13614
      end
13615
      // 10 <= num_of_frames < 18 => wrap set to TX BD 123
13616
      else if ((num_of_frames == 10) || (num_of_frames == 14))
13617
      begin
13618
        tmp_len = i_length; // length of frame
13619
        tmp_bd_num = 120; // RX BD number
13620
        while (tmp_bd_num < 124) // 
13621
        begin
13622
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13623
          if (tmp_len[0] == 0)
13624
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13625
          else
13626
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13627
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13628
          tmp_len = tmp_len + 1;
13629
          // set RX BD number
13630
          tmp_bd_num = tmp_bd_num + 1;
13631
        end
13632
        // set wrap bit
13633
        set_rx_bd_wrap(123);
13634
      end
13635
      // 18 <= num_of_frames < 28 => wrap set to RX BD 124
13636
      else if ((num_of_frames == 18) || (num_of_frames == 23))
13637
      begin
13638
        tmp_len = i_length; // length of frame
13639
        tmp_bd_num = 120; // RX BD number
13640
        while (tmp_bd_num < 125) // 
13641
        begin
13642
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13643
          if (tmp_len[0] == 0)
13644
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13645
          else
13646
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13647
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13648
          tmp_len = tmp_len + 1;
13649
          // set RX BD number
13650
          tmp_bd_num = tmp_bd_num + 1;
13651
        end
13652
        // set wrap bit
13653
        set_rx_bd_wrap(124);
13654
      end
13655
      // 28 <= num_of_frames < 40 => wrap set to RX BD 125
13656
      else if ((num_of_frames == 28) || (num_of_frames == 34))
13657
      begin
13658
        tmp_len = i_length; // length of frame
13659
        tmp_bd_num = 120; // RX BD number
13660
        while (tmp_bd_num < 126) // 
13661
        begin
13662
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13663
          if (tmp_len[0] == 0)
13664
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13665
          else
13666
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13667
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13668
          tmp_len = tmp_len + 1;
13669
          // set RX BD number
13670
          tmp_bd_num = tmp_bd_num + 1;
13671
        end
13672
        // set wrap bit
13673
        set_rx_bd_wrap(125);
13674
      end
13675
      // 40 <= num_of_frames < 54 => wrap set to RX BD 126
13676
      else if ((num_of_frames == 40) || (num_of_frames == 47))
13677
      begin
13678
        tmp_len = i_length; // length of frame
13679
        tmp_bd_num = 120; // RX BD number
13680
        while (tmp_bd_num < 127) // 
13681
        begin
13682
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13683
          if (tmp_len[0] == 0)
13684
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13685
          else
13686
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13687
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13688
          tmp_len = tmp_len + 1;
13689
          // set RX BD number
13690
          tmp_bd_num = tmp_bd_num + 1;
13691
        end
13692
        // set wrap bit
13693
        set_rx_bd_wrap(126);
13694
      end
13695
      // 54 <= num_of_frames < 70 => wrap set to RX BD 127
13696
      else if ((num_of_frames == 54) || (num_of_frames == 62))
13697
      begin
13698
        tmp_len = i_length; // length of frame
13699
        tmp_bd_num = 120; // RX BD number
13700
        while (tmp_bd_num < 128) // 
13701
        begin
13702
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13703
          if (tmp_len[0] == 0)
13704
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13705
          else
13706
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13707
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13708
          tmp_len = tmp_len + 1;
13709
          // set RX BD number
13710
          tmp_bd_num = tmp_bd_num + 1;
13711
        end
13712
        // set wrap bit
13713
        set_rx_bd_wrap(127);
13714
      end
13715
      // 70 <= num_of_frames < 78 => wrap set to RX BD 127
13716
      else if (num_of_frames == 70)
13717
      begin
13718
        tmp_len = i_length; // length of frame
13719
        tmp_bd_num = 120; // RX BD number
13720
        while (tmp_bd_num < 128) // 
13721
        begin
13722
          // if i_length[0] == 0 then base address is `MEMORY_BASE otherwise it is `MEMORY_BASE + max_tmp
13723
          if (tmp_len[0] == 0)
13724
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, (`MEMORY_BASE + tmp_len[1:0]));
13725
          else
13726
            set_rx_bd(tmp_bd_num, tmp_bd_num, 1'b1, ((`MEMORY_BASE + tmp_len[1:0]) + max_tmp));
13727
          // set length (loop variable) - THE SAME AS AT THE END OF THIS TASK !!!
13728
          tmp_len = tmp_len + 1;
13729
          // set RX BD number
13730
          tmp_bd_num = tmp_bd_num + 1;
13731
        end
13732
        // set wrap bit
13733
        set_rx_bd_wrap(127);
13734
      end
13735
      #1;
13736
      // SET empty bit
13737
      if (num_of_frames < 10)
13738
        set_rx_bd_empty(120, 120);
13739
      else if (num_of_frames < 14)
13740
        set_rx_bd_empty((120 + num_of_frames - 10), (120 + num_of_frames - 10));
13741
      else if (num_of_frames < 18)
13742
        set_rx_bd_empty((120 + num_of_frames - 14), (120 + num_of_frames - 14));
13743
      else if (num_of_frames < 23)
13744
        set_rx_bd_empty((120 + num_of_frames - 18), (120 + num_of_frames - 18));
13745
      else if (num_of_frames < 28)
13746
        set_rx_bd_empty((120 + num_of_frames - 23), (120 + num_of_frames - 23));
13747
      else if (num_of_frames < 34)
13748
        set_rx_bd_empty((120 + num_of_frames - 28), (120 + num_of_frames - 28));
13749
      else if (num_of_frames < 40)
13750
        set_rx_bd_empty((120 + num_of_frames - 34), (120 + num_of_frames - 34));
13751
      else if (num_of_frames < 47)
13752
        set_rx_bd_empty((120 + num_of_frames - 40), (120 + num_of_frames - 40));
13753
      else if (num_of_frames < 54)
13754
        set_rx_bd_empty((120 + num_of_frames - 47), (120 + num_of_frames - 47));
13755
      else if (num_of_frames < 62)
13756
        set_rx_bd_empty((120 + num_of_frames - 54), (120 + num_of_frames - 54));
13757
      else if (num_of_frames < 70)
13758
        set_rx_bd_empty((120 + num_of_frames - 62), (120 + num_of_frames - 62));
13759
      else if (num_of_frames < 78)
13760
        set_rx_bd_empty((120 + num_of_frames - 70), (120 + num_of_frames - 70));
13761
      // CHECK END OF RECEIVE
13762 252 tadejm
      // receive just preamble between some packets
13763
      if ((num_of_frames == 0) || (num_of_frames == 4) || (num_of_frames == 9))
13764
      begin
13765
        #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h6, 8'h55, 0, 0, 1'b0);
13766
        @(posedge mrx_clk);
13767
        wait (MRxDV === 1'b0); // end receive
13768
        repeat(10) @(posedge mrx_clk);
13769
        repeat(15) @(posedge wb_clk);
13770
      end
13771
      // receiving frames and checking end of them
13772 243 tadejm
      frame_ended = 0;
13773
      check_frame = 0;
13774
      fork
13775
        begin
13776
          if (i_length[0] == 1'b0)
13777
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, (i_length + 4), 1'b0);
13778
          else
13779
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, max_tmp, (i_length + 4), 1'b0);
13780
          repeat(10) @(posedge mrx_clk);
13781
        end
13782 254 mohor
        begin: fr_end2
13783 243 tadejm
          wait (MRxDV === 1'b1); // start receive
13784
          #1 check_rx_bd(num_of_bd, data);
13785
          if (data[15] !== 1)
13786 209 tadejm
          begin
13787 243 tadejm
            test_fail("Wrong buffer descriptor's empty bit read out from MAC");
13788 209 tadejm
            fail = fail + 1;
13789
          end
13790 243 tadejm
          wait (MRxDV === 1'b0); // end receive
13791
          while ((data[15] === 1) && (check_frame == 0))
13792
          begin
13793
            #1 check_rx_bd(num_of_bd, data);
13794
            @(posedge wb_clk);
13795
          end
13796
          if (data[15] === 0)
13797
            frame_ended = 1;
13798
          repeat (1) @(posedge wb_clk);
13799 209 tadejm
        end
13800
        begin
13801 243 tadejm
          wait (MRxDV === 1'b1); // start receive
13802
          wait (MRxDV === 1'b0); // end receive
13803
          repeat(10) @(posedge mrx_clk);
13804
          repeat(15) @(posedge wb_clk);
13805
          check_frame = 1;
13806 209 tadejm
        end
13807 243 tadejm
      join
13808
      // check length of a PACKET
13809 252 tadejm
      if ( ((data[31:16] != (i_length + 4)) && (num_of_frames >= 3)) ||
13810
           ((data[31:16] != 0) && (num_of_frames < 3)) )
13811 243 tadejm
      begin
13812
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
13813
                        data[31:16], (i_length + 4));
13814
        test_fail("Wrong length of the packet out from PHY");
13815
        fail = fail + 1;
13816
      end
13817
      // check received RX packet data and CRC
13818 252 tadejm
      if ((frame_ended == 1) && (num_of_frames >= 5)) // 5 bytes is minimum size without CRC error, since
13819
      begin                                           // CRC has 4 bytes for itself
13820 243 tadejm
        if (i_length[0] == 1'b0)
13821 209 tadejm
        begin
13822 243 tadejm
          check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
13823 209 tadejm
        end
13824 243 tadejm
        else
13825 209 tadejm
        begin
13826 243 tadejm
          check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
13827 209 tadejm
        end
13828
        if (tmp > 0)
13829
        begin
13830 243 tadejm
          `TIME; $display("*E Wrong data of the received packet");
13831
          test_fail("Wrong data of the received packet");
13832 209 tadejm
          fail = fail + 1;
13833
        end
13834
      end
13835
      // check WB INT signal
13836 252 tadejm
      if (num_of_frames >= 3) // Frames smaller than 3 are not received.
13837
      begin                   // Frames greater then 5 always cause an interrupt (Frame received)
13838
        if (wb_int !== 1'b1)  // Frames with length 3 or 4 always cause an interrupt (CRC error)
13839 209 tadejm
        begin
13840
          `TIME; $display("*E WB INT signal should be set");
13841
          test_fail("WB INT signal should be set");
13842
          fail = fail + 1;
13843
        end
13844
      end
13845 252 tadejm
      else
13846 209 tadejm
      begin
13847
        if (wb_int !== 1'b0)
13848
        begin
13849
          `TIME; $display("*E WB INT signal should not be set");
13850
          test_fail("WB INT signal should not be set");
13851
          fail = fail + 1;
13852
        end
13853
      end
13854 243 tadejm
      // check RX buffer descriptor of a packet
13855
      if (num_of_frames >= min_tmp)
13856
      begin
13857 254 mohor
        if ( (data[15:0] !== 16'h6080) && // wrap bit
13858
             (data[15:0] !== 16'h4080) ) // without wrap bit
13859 209 tadejm
        begin
13860 243 tadejm
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13861
          test_fail("RX buffer descriptor status is not correct");
13862
          fail = fail + 1;
13863 209 tadejm
        end
13864 243 tadejm
      end
13865 254 mohor
      else if (num_of_frames > 6)
13866 243 tadejm
      begin
13867 254 mohor
        if ( (data[15:0] !== 16'h6084) && // wrap bit
13868
             (data[15:0] !== 16'h4084) ) // without wrap bit
13869
        begin
13870
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13871
          test_fail("RX buffer descriptor status is not correct");
13872
          fail = fail + 1;
13873
        end
13874
      end
13875
      else if (num_of_frames > 4) // MAC does not recognize Dest. ADDR. for lengths 5, 6 => no MISS
13876
      begin
13877 243 tadejm
        if ( (data[15:0] !== 16'h6004) && // wrap bit
13878
             (data[15:0] !== 16'h4004) ) // without wrap bit
13879 209 tadejm
        begin
13880 243 tadejm
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13881
          test_fail("RX buffer descriptor status is not correct");
13882
          fail = fail + 1;
13883 209 tadejm
        end
13884
      end
13885 254 mohor
      else if (num_of_frames > 2) // MAC does not recognize Dest. ADDR. for length 3, 4 => no MISS, CRC ERROR
13886 243 tadejm
      begin
13887
        if ( (data[15:0] !== 16'h6006) && // wrap bit
13888
             (data[15:0] !== 16'h4006) ) // without wrap bit
13889
        begin
13890
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13891
          test_fail("RX buffer descriptor status is not correct");
13892
          fail = fail + 1;
13893
        end
13894
      end
13895 209 tadejm
      else
13896
      begin
13897
        if (data[15] !== 1'b1)
13898
        begin
13899 243 tadejm
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h - len: %0d", data[15:0], num_of_frames);
13900
          test_fail("RX buffer descriptor status is not correct");
13901 209 tadejm
          fail = fail + 1;
13902
        end
13903
      end
13904
      // check interrupts
13905
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13906 252 tadejm
      if (num_of_frames >= 5)
13907 209 tadejm
      begin
13908 252 tadejm
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
13909 209 tadejm
        begin
13910 243 tadejm
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
13911
          test_fail("Interrupt Receive Buffer was not set");
13912 209 tadejm
          fail = fail + 1;
13913
        end
13914 243 tadejm
        if ((data & (~`ETH_INT_RXB)) !== 0)
13915 209 tadejm
        begin
13916 243 tadejm
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
13917
          test_fail("Other interrupts (except Receive Buffer) were set");
13918 209 tadejm
          fail = fail + 1;
13919
        end
13920
      end
13921 252 tadejm
      else if ((num_of_frames < 3)) // Frames smaller than 3 are not received.
13922
      begin
13923
        if (data) // Checking if any interrupt is pending)
13924
        begin
13925
          `TIME; $display("*E Interrupt(s) is(are) pending although frame was ignored, interrupt reg: %0h", data);
13926
          test_fail("Interrupts were set");
13927
          fail = fail + 1;
13928
        end
13929
      end
13930 209 tadejm
      else
13931
      begin
13932 252 tadejm
        if ((data & `ETH_INT_RXE) !== `ETH_INT_RXE)
13933 209 tadejm
        begin
13934 243 tadejm
          `TIME; $display("*E Interrupt Receive Buffer Error was not set, interrupt reg: %0h", data);
13935
          test_fail("Interrupt Receive Buffer Error was not set");
13936 209 tadejm
          fail = fail + 1;
13937
        end
13938 243 tadejm
        if ((data & (~`ETH_INT_RXE)) !== 0)
13939
        begin
13940
          `TIME; $display("*E Other interrupts (except Receive Buffer Error) were set, interrupt reg: %0h", data);
13941
          test_fail("Other interrupts (except Receive Buffer Error) were set");
13942
          fail = fail + 1;
13943
        end
13944 209 tadejm
      end
13945
      // clear interrupts
13946
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
13947
      // check WB INT signal
13948
      if (wb_int !== 1'b0)
13949
      begin
13950
        test_fail("WB INT signal should not be set");
13951
        fail = fail + 1;
13952
      end
13953
      // INTERMEDIATE DISPLAYS
13954 243 tadejm
      if (num_of_frames == 3)
13955 209 tadejm
      begin
13956 243 tadejm
        $display("    using 1 BD out of 8 BDs (120..127) assigned to RX (wrap at 1st BD - RX BD 120)");
13957
        $display("    ->packets with lengths from %0d to %0d are not received (length increasing by 1 byte)",
13958 209 tadejm
                 0, 3);
13959
      end
13960 243 tadejm
      else if (num_of_frames == 9)
13961 209 tadejm
      begin
13962 243 tadejm
        $display("    using 1 BD out of 8 BDs (120..127) assigned to RX (wrap at 1st BD - RX BD 120)");
13963
        $display("    ->packet with length 4 is not received (length increasing by 1 byte)");
13964 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13965
                 5, 9);
13966
      end
13967 243 tadejm
      else if (num_of_frames == 17)
13968 209 tadejm
      begin
13969 243 tadejm
        $display("    using 4 BDs out of 8 BDs (120..127) assigned to RX (wrap at 4th BD - RX BD 123)");
13970 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13971
                 10, 17);
13972
      end
13973 243 tadejm
      else if (num_of_frames == 27)
13974 209 tadejm
      begin
13975 243 tadejm
        $display("    using 5 BDs out of 8 BDs (120..127) assigned to RX (wrap at 5th BD - RX BD 124)");
13976 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13977
                 18, 27);
13978
      end
13979 243 tadejm
      else if (num_of_frames == 40)
13980 209 tadejm
      begin
13981 243 tadejm
        $display("    using 6 BDs out of 8 BDs (120..127) assigned to RX (wrap at 6th BD - RX BD 125)");
13982 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13983
                 28, 40);
13984
      end
13985 243 tadejm
      else if (num_of_frames == 54)
13986 209 tadejm
      begin
13987 243 tadejm
        $display("    using 7 BDs out of 8 BDs (120..127) assigned to RX (wrap at 7th BD - RX BD 126)");
13988 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13989
                 41, 54);
13990
      end
13991 243 tadejm
      else if (num_of_frames == 69)
13992 209 tadejm
      begin
13993 243 tadejm
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
13994 209 tadejm
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
13995
                 55, 69);
13996
      end
13997 243 tadejm
      else if (num_of_frames == 69)
13998
      begin
13999
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
14000
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
14001
                 55, 69);
14002
      end
14003
      else if (num_of_frames == 77)
14004
      begin
14005
        $display("    using 8 BDs out of 8 BDs (120..127) assigned to RX (wrap at 8th BD - RX BD 127)");
14006
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 1 byte)",
14007
                 70, 77);
14008
      end
14009 209 tadejm
      // set length (loop variable)
14010
      i_length = i_length + 1;
14011
      // the number of frame transmitted
14012
      num_of_frames = num_of_frames + 1;
14013
      if (/*(num_of_frames == 2) || (num_of_frames == 4) || (num_of_frames == 7) ||*/ (num_of_frames <= 10) ||
14014
          (num_of_frames == 14) || (num_of_frames == 18) || (num_of_frames == 23) || (num_of_frames == 28) ||
14015
          (num_of_frames == 34) || (num_of_frames == 40) || (num_of_frames == 47) ||
14016 243 tadejm
          (num_of_frames == 54) || (num_of_frames == 62) || (num_of_frames == 70))
14017
        num_of_bd = 120;
14018 209 tadejm
      else
14019
        num_of_bd = num_of_bd + 1;
14020
    end
14021 243 tadejm
    // disable RX
14022 209 tadejm
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_PAD | `ETH_MODER_CRCEN,
14023
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14024
    @(posedge wb_clk);
14025
    if(fail == 0)
14026
      test_ok;
14027
    else
14028
      fail = 0;
14029
  end
14030
 
14031
 
14032 243 tadejm
  ////////////////////////////////////////////////////////////////////
14033
  ////                                                            ////
14034
  ////  Test receive packet synchronization with receive          ////
14035
  ////  disable/enable ( 10Mbps ).                                ////
14036
  ////                                                            ////
14037
  ////////////////////////////////////////////////////////////////////
14038
  if (test_num == 10) // Test no receive when all buffers are TX ( 10Mbps ).
14039
  begin
14040
    // TEST 10: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )
14041
    test_name   = "TEST 10: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )";
14042
    `TIME; $display("  TEST 10: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )");
14043 209 tadejm
 
14044 243 tadejm
 
14045
 
14046
 
14047
 
14048
 
14049
  end
14050
 
14051
 
14052
  ////////////////////////////////////////////////////////////////////
14053
  ////                                                            ////
14054
  ////  Test receive packet synchronization with receive          ////
14055
  ////  disable/enable ( 10Mbps ).                                ////
14056
  ////                                                            ////
14057
  ////////////////////////////////////////////////////////////////////
14058
  if (test_num == 12) // Test no receive when all buffers are TX ( 10Mbps ).
14059
  begin
14060
    // TEST 12: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )
14061
    test_name   = "TEST 12: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )";
14062
    `TIME; $display("  TEST 12: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )");
14063
 
14064
 
14065
 
14066
 
14067
 
14068
 
14069
  end
14070
 
14071
 
14072
  ////////////////////////////////////////////////////////////////////
14073
  ////                                                            ////
14074
  ////  Test receive packet synchronization with receive          ////
14075
  ////  disable/enable ( 10Mbps ).                                ////
14076
  ////                                                            ////
14077
  ////////////////////////////////////////////////////////////////////
14078
  if (test_num == 14) // Test no receive when all buffers are TX ( 10Mbps ).
14079
  begin
14080
    // TEST 14: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )
14081
    test_name   = "TEST 14: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )";
14082
    `TIME; $display("  TEST 14: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )");
14083
 
14084
 
14085
 
14086
 
14087
 
14088
 
14089
  end
14090
 
14091
 
14092
  ////////////////////////////////////////////////////////////////////
14093
  ////                                                            ////
14094
  ////  Test receive packet synchronization with receive          ////
14095
  ////  disable/enable ( 10Mbps ).                                ////
14096
  ////                                                            ////
14097
  ////////////////////////////////////////////////////////////////////
14098
  if (test_num == 16) // Test no receive when all buffers are TX ( 10Mbps ).
14099
  begin
14100
    // TEST 16: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )
14101
    test_name   = "TEST 16: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )";
14102
    `TIME; $display("  TEST 16: RECEIVE PACKET SYNCHRONIZATION WITH RECEIVE DISABLE/ENABLE ( 10Mbps )");
14103
 
14104
 
14105
 
14106
 
14107
 
14108
 
14109
  end
14110
 
14111
 
14112
 
14113 209 tadejm
end   //  for (test_num=start_task; test_num <= end_task; test_num=test_num+1)
14114
 
14115
end
14116
endtask // test_mac_full_duplex_receive
14117
 
14118
 
14119
task test_mac_full_duplex_flow;
14120
  input  [31:0]  start_task;
14121
  input  [31:0]  end_task;
14122
  integer        bit_start_1;
14123
  integer        bit_end_1;
14124
  integer        bit_start_2;
14125
  integer        bit_end_2;
14126
  integer        num_of_reg;
14127
  integer        num_of_frames;
14128
  integer        num_of_bd;
14129
  integer        i_addr;
14130
  integer        i_data;
14131
  integer        i_length;
14132
  integer        tmp_len;
14133
  integer        tmp_bd;
14134
  integer        tmp_bd_num;
14135
  integer        tmp_data;
14136
  integer        tmp_ipgt;
14137
  integer        test_num;
14138
  reg    [31:0]  tx_bd_num;
14139
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_data;
14140
  reg    [((`MAX_BLK_SIZE * 32) - 1):0] burst_tmp_data;
14141
  integer        i;
14142
  integer        i1;
14143
  integer        i2;
14144
  integer        i3;
14145
  integer        fail;
14146
  integer        speed;
14147 260 mohor
  integer        mac_hi_addr;
14148
  integer        mac_lo_addr;
14149 209 tadejm
  reg            frame_started;
14150
  reg            frame_ended;
14151
  reg            wait_for_frame;
14152
  reg    [31:0]  addr;
14153
  reg    [31:0]  data;
14154
  reg    [31:0]  tmp;
14155
  reg    [ 7:0]  st_data;
14156
  reg    [15:0]  max_tmp;
14157
  reg    [15:0]  min_tmp;
14158
begin
14159
// MAC FULL DUPLEX FLOW TEST
14160
test_heading("MAC FULL DUPLEX FLOW TEST");
14161
$display(" ");
14162
$display("MAC FULL DUPLEX FLOW TEST");
14163
fail = 0;
14164
 
14165
// reset MAC registers
14166
hard_reset;
14167
// reset MAC and MII LOGIC with soft reset
14168
reset_mac;
14169
reset_mii;
14170
// set wb slave response
14171
wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
14172
 
14173
  /*
14174
  TASKS for set and control TX buffer descriptors (also send packet - set_tx_bd_ready):
14175
  -------------------------------------------------------------------------------------
14176
  set_tx_bd
14177
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0], len[15:0], irq, pad, crc, txpnt[31:0]);
14178
  set_tx_bd_wrap
14179
    (tx_bd_num_end[6:0]);
14180
  set_tx_bd_ready
14181
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
14182
  check_tx_bd
14183
    (tx_bd_num_start[6:0], tx_bd_status[31:0]);
14184
  clear_tx_bd
14185
    (tx_bd_num_start[6:0], tx_bd_num_end[6:0]);
14186
 
14187
  TASKS for set and control RX buffer descriptors:
14188
  ------------------------------------------------
14189
  set_rx_bd
14190
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0], irq, rxpnt[31:0]);
14191
  set_rx_bd_wrap
14192
    (rx_bd_num_end[6:0]);
14193
  set_rx_bd_empty
14194
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
14195
  check_rx_bd
14196
    (rx_bd_num_end[6:0], rx_bd_status);
14197
  clear_rx_bd
14198
    (rx_bd_num_strat[6:0], rx_bd_num_end[6:0]);
14199
 
14200
  TASKS for set and check TX packets:
14201
  -----------------------------------
14202
  set_tx_packet
14203
    (txpnt[31:0], len[15:0], eth_start_data[7:0]);
14204
  check_tx_packet
14205
    (txpnt_wb[31:0], txpnt_phy[31:0], len[15:0], failure[31:0]);
14206
 
14207
  TASKS for set and check RX packets:
14208
  -----------------------------------
14209
  set_rx_packet
14210
    (rxpnt[31:0], len[15:0], plus_nibble, d_addr[47:0], s_addr[47:0], type_len[15:0], start_data[7:0]);
14211
  check_rx_packet
14212
    (rxpnt_phy[31:0], rxpnt_wb[31:0], len[15:0], plus_nibble, successful_nibble, failure[31:0]);
14213
 
14214
  TASKS for append and check CRC to/of TX packet:
14215
  -----------------------------------------------
14216
  append_tx_crc
14217
    (txpnt_wb[31:0], len[15:0], negated_crc);
14218
  check_tx_crc
14219
    (txpnt_phy[31:0], len[15:0], negated_crc, failure[31:0]);
14220
 
14221
  TASK for append CRC to RX packet (CRC is checked together with check_rx_packet):
14222
  --------------------------------------------------------------------------------
14223
  append_rx_crc
14224
    (rxpnt_phy[31:0], len[15:0], plus_nibble, negated_crc);
14225
  */
14226
 
14227
//////////////////////////////////////////////////////////////////////
14228
////                                                              ////
14229
////  test_mac_full_duplex_flow:                                  ////
14230
////                                                              ////
14231
////  0: Test                                                     ////
14232
////                                                              ////
14233
//////////////////////////////////////////////////////////////////////
14234 260 mohor
 
14235 209 tadejm
for (test_num = start_task; test_num <= end_task; test_num = test_num + 1)
14236
begin
14237
 
14238
  ////////////////////////////////////////////////////////////////////
14239
  ////                                                            ////
14240 254 mohor
  ////  Test inserts control frames while transmitting normal     ////
14241
  ////  frames. Using 4 TX buffer decriptors ( 10Mbps ).          ////
14242 209 tadejm
  ////                                                            ////
14243
  ////////////////////////////////////////////////////////////////////
14244 254 mohor
  if (test_num == 0) // 
14245 209 tadejm
  begin
14246 254 mohor
    // TEST 0: INSERT CONTROL FRM. WHILE TRANSMITTING NORMAL FRM. AT 4 TX BD ( 10Mbps )
14247
    test_name = "TEST 0: INSERT CONTROL FRM. WHILE TRANSMITTING NORMAL FRM. AT 4 TX BD ( 10Mbps )";
14248
    `TIME; $display("  TEST 0: INSERT CONTROL FRM. WHILE TRANSMITTING NORMAL FRM. AT 4 TX BD ( 10Mbps )");
14249 260 mohor
 
14250 254 mohor
    // reset MAC completely
14251
    hard_reset;
14252
    // set wb slave response
14253
    wb_slave.cycle_response(`ACK_RESPONSE, wbs_waits, wbs_retries);
14254 209 tadejm
 
14255 254 mohor
    max_tmp = 0;
14256
    min_tmp = 0;
14257
    // set 4 TX buffer descriptors - must be set before TX enable
14258
    wbm_write(`ETH_TX_BD_NUM, 32'h4, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14259
    // enable TX, set full-duplex mode, padding and CRC appending
14260
    wbm_write(`ETH_MODER, `ETH_MODER_TXEN | `ETH_MODER_PAD | `ETH_MODER_FULLD | `ETH_MODER_CRCEN,
14261
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14262
    // enable TX flow control
14263
    wbm_write(`ETH_CTRLMODER, `ETH_CTRLMODER_TXFLOW, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14264 260 mohor
 
14265
    // Set MAC address
14266
    mac_hi_addr = 32'h00000001;
14267
    mac_lo_addr = 32'h02030405;
14268
    wbm_write(`ETH_MAC_ADDR1, mac_hi_addr, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14269
    wbm_write(`ETH_MAC_ADDR0, mac_lo_addr, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14270
 
14271 254 mohor
    // prepare two packets of MAXFL length
14272
    wbm_read(`ETH_PACKETLEN, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14273
    max_tmp = tmp[15:0]; // 18 bytes consists of 6B dest addr, 6B source addr, 2B type/len, 4B CRC
14274
    min_tmp = tmp[31:16];
14275 260 mohor
    st_data = 8'h34;
14276 254 mohor
    set_tx_packet(`MEMORY_BASE, (max_tmp - 4), st_data); // length without CRC. Writing data to the memory
14277 260 mohor
    st_data = 8'h56;
14278 254 mohor
    set_tx_packet((`MEMORY_BASE + max_tmp), (max_tmp - 4), st_data); // length without CRC. Writing data to the memory
14279
    // check WB INT signal
14280
    if (wb_int !== 1'b0)
14281
    begin
14282
      test_fail("WB INT signal should not be set");
14283
      fail = fail + 1;
14284
    end
14285
 
14286
    // write to phy's control register for 10Mbps
14287
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
14288
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
14289
    speed = 10;
14290
 
14291
    frame_started = 0;
14292
    num_of_frames = 0;
14293
    num_of_bd = 0;
14294
    i_length = 0; // 0;
14295 209 tadejm
 
14296 254 mohor
 
14297 260 mohor
    // Initialize one part of memory with data of control packet
14298
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h0), 32'h0180c200, 4'hF);
14299
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h4), {16'h0001, mac_hi_addr[15:0]}, 4'hF);
14300
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h8), mac_lo_addr, 4'hF);
14301
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'hc), 32'h88080001, 4'hF);
14302
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h10), 32'h11110000, 4'hF);
14303
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h14), 32'h00000000, 4'hF);
14304
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h18), 32'h00000000, 4'hF);
14305
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h1c), 32'h00000000, 4'hF);
14306
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h20), 32'h00000000, 4'hF);
14307
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h24), 32'h00000000, 4'hF);
14308
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h28), 32'h00000000, 4'hF);
14309
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h2c), 32'h00000000, 4'hF);
14310
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h30), 32'h00000000, 4'hF);
14311
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h34), 32'h00000000, 4'hF);
14312
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h38), 32'h00000000, 4'hF);
14313
//    append_tx_crc(`MEMORY_BASE + 2 * max_tmp, 60, 0);       // CRC is appended after the data
14314
 
14315
 
14316 254 mohor
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14317
    // In the following section, control frame will be sent while no other transmission is in progress.//
14318
    // TXC interrupt won't be unmasked.                                                                //
14319
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14320
    wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14321
    if(data)
14322
      begin
14323
        test_fail("IRQ already pending!");
14324
        fail = fail + 1;
14325
        `TIME; $display("*E IRQ already pending!");
14326
      end
14327
 
14328
    if (wb_int)
14329
    begin
14330
      test_fail("WB INT signal should not be set!");
14331
      fail = fail + 1;
14332
      `TIME; $display("*E WB INT signal should not be set!");
14333
    end
14334
 
14335 260 mohor
    // first destination address on ethernet PHY
14336
    eth_phy.set_tx_mem_addr(0);
14337
    // Request sending the control frame with pause value = 0x1111
14338
    wbm_write(`ETH_TX_CTRL, `ETH_TX_CTRL_TXPAUSERQ | 32'h1111, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14339 254 mohor
 
14340
    wait (MTxEn === 1'b1); // start transmit
14341
    wait (MTxEn === 1'b0); // end transmit
14342
    repeat(10) @ (posedge wb_clk);  // wait some time
14343
    repeat(10) @ (posedge mtx_clk); // wait some time
14344
 
14345
    wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14346
    if(data !== `ETH_INT_TXC)
14347
      begin
14348
        test_fail("TXC IRQ should be set!");
14349
        fail = fail + 1;
14350
        `TIME; $display("*E TXC IRQ should be set!");
14351
      end
14352
 
14353
    if (wb_int)
14354
    begin
14355
      test_fail("WB INT signal should not be set because TXC irq is masked!");
14356
      fail = fail + 1;
14357
      `TIME; $display("*E WB INT signal should not be set because TXC irq is masked!");
14358
    end
14359
 
14360
    // Clear TXC interrupt
14361
    wbm_write(`ETH_INT, `ETH_INT_TXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14362
 
14363
    if (wb_int)
14364
    begin
14365
      test_fail("WB INT signal should not be set!");
14366
      fail = fail + 1;
14367
      `TIME; $display("*E WB INT signal should not be set!");
14368
    end
14369
 
14370 260 mohor
    check_tx_packet((`MEMORY_BASE + 2 * max_tmp), 0, 60, tmp);
14371
    if (tmp > 0)
14372
    begin
14373
      $display("Wrong data of the transmitted packet");
14374
      test_fail("Wrong data of the transmitted packet");
14375
      fail = fail + 1;
14376
    end
14377
    // check transmited TX packet CRC
14378
    #1 check_tx_crc(0, 60, 1'b0, tmp); // length without CRC
14379
    if (tmp > 0)
14380
    begin
14381
      $display("Wrong CRC of the transmitted packet");
14382
      test_fail("Wrong CRC of the transmitted packet");
14383
      fail = fail + 1;
14384
    end
14385 254 mohor
 
14386
 
14387
 
14388
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14389
    // In the following section, control frame will be sent while no other transmission is in progress.//
14390
    // TXC interrupt is unmasked.                                                                      //
14391
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14392
 
14393
    // unmask all interrupts
14394
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
14395
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14396
 
14397
    wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14398
    if(data)
14399
      begin
14400
        test_fail("IRQ already pending!");
14401
        fail = fail + 1;
14402
        `TIME; $display("*E IRQ already pending!");
14403
      end
14404
 
14405
    if (wb_int)
14406
    begin
14407
      test_fail("WB INT signal should not be set!");
14408
      fail = fail + 1;
14409
      `TIME; $display("*E WB INT signal should not be set!");
14410
    end
14411
 
14412
    // unmask only TXC interrupts
14413
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14414
 
14415 260 mohor
    // first destination address on ethernet PHY
14416
    eth_phy.set_tx_mem_addr(0);
14417
    // Request sending the control frame with pause value = 0x2222
14418
    wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h10), 32'h22220000, 4'hF);  // Just for data test
14419
    wbm_write(`ETH_TX_CTRL, `ETH_TX_CTRL_TXPAUSERQ | 32'h2222, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14420 254 mohor
 
14421
    wait (MTxEn === 1'b1); // start transmit
14422
    wait (MTxEn === 1'b0); // end transmit
14423
    repeat(10) @ (posedge wb_clk);  // wait some time
14424
    repeat(10) @ (posedge mtx_clk); // wait some time
14425
 
14426
    wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14427
    if(data !== `ETH_INT_TXC)
14428
      begin
14429
        test_fail("TXC IRQ should be set!");
14430
        fail = fail + 1;
14431
        `TIME; $display("*E TXC IRQ should be set!");
14432
      end
14433
 
14434
    if (!wb_int)
14435
    begin
14436
      test_fail("WB INT signal should be set!");
14437
      fail = fail + 1;
14438
      `TIME; $display("*E WB INT signal should be set!");
14439
    end
14440
 
14441
    // Clear TXC interrupt
14442
    wbm_write(`ETH_INT, `ETH_INT_TXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14443
 
14444
    if (wb_int)
14445
    begin
14446
      test_fail("WB INT signal should not be set!");
14447
      fail = fail + 1;
14448
      `TIME; $display("*E WB INT signal should not be set!");
14449
    end
14450
 
14451
 
14452 260 mohor
    check_tx_packet((`MEMORY_BASE + 2 * max_tmp), 0, 60, tmp);
14453
    if (tmp > 0)
14454
    begin
14455
      $display("Wrong data of the transmitted packet");
14456
      test_fail("Wrong data of the transmitted packet");
14457
      fail = fail + 1;
14458
    end
14459
    // check transmited TX packet CRC
14460
    #1 check_tx_crc(0, 60, 1'b0, tmp); // length without CRC
14461
    if (tmp > 0)
14462
    begin
14463
      $display("Wrong CRC of the transmitted packet");
14464
      test_fail("Wrong CRC of the transmitted packet");
14465
      fail = fail + 1;
14466
    end
14467 254 mohor
 
14468 260 mohor
 
14469 254 mohor
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14470 260 mohor
    // In the following section, control frame sending is requested while no other transmission        //
14471
    // is in progress. TXC interrupt is unmasked.                                                      //
14472 254 mohor
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14473
 
14474
    // unmask all interrupts
14475
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
14476
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14477
 
14478
    wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14479
    if(data)
14480
      begin
14481
        test_fail("IRQ already pending!");
14482
        fail = fail + 1;
14483
        `TIME; $display("*E IRQ already pending!");
14484
      end
14485
 
14486
    if (wb_int)
14487
    begin
14488
      test_fail("WB INT signal should not be set!");
14489
      fail = fail + 1;
14490
      `TIME; $display("*E WB INT signal should not be set!");
14491
    end
14492
 
14493
 
14494 260 mohor
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14495
    // In the following section, control frame request and data send request are both set. At the      //
14496
    // beginning control frame request will be faster than data send request, later the opposite.      //
14497
    /////////////////////////////////////////////////////////////////////////////////////////////////////
14498
    for (i=0; i<32; i=i+1)
14499
    begin
14500
      // Request sending the control frame with pause value = 0x5678
14501
      set_tx_bd(0, 0, 16'h100, 1'b1, 1'b1, 1'b1, (`MEMORY_BASE)); // irq, pad, crc
14502
      set_tx_bd_wrap(0);
14503
      // first destination address on ethernet PHY
14504
      eth_phy.set_tx_mem_addr(0);
14505
      set_tx_bd_ready(0, 0);
14506
 
14507
      wait (MTxEn === 1'b1); // start transmit
14508
 
14509
      repeat(i) @ (posedge mtx_clk);  // We need to wait some time until TX module starts using the data (preamble stage is over)
14510
 
14511
      // Send control frame request
14512
      wb_slave.wr_mem((`MEMORY_BASE + 2 * max_tmp + 8'h10), {i[3:0], i[3:0], i[3:0], i[3:0], 16'h0}, 4'hF);  // Just for data test
14513
      wbm_write(`ETH_TX_CTRL, `ETH_TX_CTRL_TXPAUSERQ | {16'h0, i[3:0], i[3:0], i[3:0], i[3:0]}, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14514
 
14515
      wait (MTxEn === 1'b0); // Wait until data frame transmission is over
14516
      repeat(10) @ (posedge mtx_clk); // wait some time so status is written
14517
      tmp_len = eth_phy.tx_len; // the length of a packet which was sent out first!!!
14518
      repeat(10) @ (posedge wb_clk);  // wait some time so status is written
14519
 
14520
      // first destination address on ethernet PHY
14521
      eth_phy.set_tx_mem_addr(0);
14522
 
14523
      if(tmp_len == 64)  // Control frame
14524 254 mohor
      begin
14525 260 mohor
        wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14526
        if(data !== `ETH_INT_TXC)
14527
        begin
14528
          test_fail("TXC IRQ should be set!");
14529
          fail = fail + 1;
14530
          `TIME; $display("*E TXC IRQ should be set!");
14531
          `TIME; $display("ETH_INT = 0x%0x", data);
14532
        end
14533
      end
14534
      else
14535
      begin
14536
        wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14537
        if(data !== `ETH_INT_TXB)
14538
        begin
14539
          test_fail("TXB IRQ should be set!");
14540
          fail = fail + 1;
14541
          `TIME; $display("*E TXB IRQ should be set!");
14542
          `TIME; $display("ETH_INT = 0x%0x", data);
14543
        end
14544
      end
14545
 
14546
      if(tmp_len == 64)  // Control frame
14547
        check_tx_packet((`MEMORY_BASE + 2 * max_tmp), 0, 60, tmp);
14548
      else
14549
        check_tx_packet((`MEMORY_BASE), 0, 32'h100, tmp);
14550
 
14551
      if (tmp > 0)
14552
      begin
14553
        $display("Wrong data of the transmitted packet");
14554
        test_fail("Wrong data of the transmitted packet");
14555 254 mohor
        fail = fail + 1;
14556
      end
14557 260 mohor
 
14558
      // check transmited TX packet CRC
14559
      if(tmp_len == 64)  // Control frame
14560
        #1 check_tx_crc(0, 60, 1'b0, tmp); // length without CRC
14561
      else
14562
        #1 check_tx_crc(0, 32'h100, 1'b0, tmp); // length without CRC
14563
 
14564
      if (tmp > 0)
14565 254 mohor
      begin
14566 260 mohor
        $display("Wrong CRC of the transmitted packet");
14567
        test_fail("Wrong CRC of the transmitted packet");
14568
        fail = fail + 1;
14569
      end
14570
 
14571
 
14572
      wait (MTxEn === 1'b1); // start transmit of the control frame
14573
      wait (MTxEn === 1'b0); // end transmit of the control frame
14574
      repeat(10) @ (posedge wb_clk);  // wait some time
14575
      repeat(10) @ (posedge mtx_clk); // wait some time so status is written
14576
 
14577
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14578
      if(data !== (`ETH_INT_TXC | `ETH_INT_TXB))
14579
      begin
14580 254 mohor
        test_fail("TXC and TXB IRQ should be set!");
14581
        fail = fail + 1;
14582 260 mohor
        `TIME; $display("*E TXC and TXB IRQ should be set! (ETH_INT=0x%0x)", data);
14583 254 mohor
      end
14584 260 mohor
 
14585
      if (!wb_int)
14586
      begin
14587
        test_fail("WB INT signal should be set!");
14588
        fail = fail + 1;
14589
        `TIME; $display("*E WB INT signal should be set!");
14590
      end
14591
 
14592
      // Clear TXC and TXB interrupt
14593
      wbm_write(`ETH_INT, `ETH_INT_TXC | `ETH_INT_TXB, 4'hF, 1, 4'h0, 4'h0);
14594
 
14595
      if (wb_int)
14596
      begin
14597
        test_fail("WB INT signal should not be set!");
14598
        fail = fail + 1;
14599
        `TIME; $display("*E WB INT signal should not be set!");
14600
      end
14601
 
14602
      if(tmp_len == 64)  // Control frame
14603
        check_tx_packet((`MEMORY_BASE), 0, 32'h100, tmp);
14604
      else
14605
        check_tx_packet((`MEMORY_BASE + 2 * max_tmp), 0, 60, tmp);
14606
 
14607
      if (tmp > 0)
14608
      begin
14609
        $display("Wrong data of the transmitted packet");
14610
        test_fail("Wrong data of the transmitted packet");
14611
        fail = fail + 1;
14612
      end
14613
 
14614
      // check transmited TX packet CRC
14615
      if(tmp_len == 64)  // Control frame
14616
        #1 check_tx_crc(0, 32'h100, 1'b0, tmp); // length without CRC
14617
      else
14618
        #1 check_tx_crc(0, 60, 1'b0, tmp); // length without CRC
14619
 
14620
      if (tmp > 0)
14621
      begin
14622
        $display("Wrong CRC of the transmitted packet");
14623
        test_fail("Wrong CRC of the transmitted packet");
14624
        fail = fail + 1;
14625
      end
14626
    end // for loop
14627 254 mohor
 
14628 260 mohor
    if(fail)
14629 254 mohor
    begin
14630 260 mohor
      test_name = "TEST 0: FINISHED WITH ERRORS";
14631
      `TIME; $display("  TEST 0: FINISHED WITH ERRORS");
14632 254 mohor
    end
14633 260 mohor
    else
14634 254 mohor
    begin
14635 260 mohor
      test_name = "TEST 0: SUCCESSFULLY FINISHED";
14636
      `TIME; $display("  TEST 0: SUCCESSFULLY FINISHED");
14637 254 mohor
    end
14638
 
14639 260 mohor
    if(fail == 0)
14640
      test_ok;
14641
    else
14642
      fail = 0;
14643 254 mohor
 
14644 260 mohor
  end
14645 254 mohor
 
14646
 
14647
 
14648 260 mohor
  ////////////////////////////////////////////////////////////////////
14649
  ////                                                            ////
14650
  ////  Receive control frames with PASSALL option turned off     ////
14651
  ////  Using only one RX buffer decriptor ( 10Mbps ).            ////
14652
  ////                                                            ////
14653
  ////////////////////////////////////////////////////////////////////
14654
  if (test_num == 1) // 
14655
  begin
14656
    // TEST 1: RECEIVE CONTROL FRAMES WITH PASSALL OPTION TURNED OFF AT ONE RX BD ( 10Mbps )
14657
    test_name   = "TEST 1: RECEIVE CONTROL FRAMES WITH PASSALL OPTION TURNED OFF AT ONE RX BD ( 10Mbps )";
14658
    `TIME; $display("  TEST 1: RECEIVE CONTROL FRAMES WITH PASSALL OPTION TURNED OFF AT ONE RX BD ( 10Mbps )");
14659 254 mohor
 
14660 260 mohor
    // unmask interrupts
14661
    wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
14662
                             `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14663
    // set 1 RX buffer descriptor (8'h80 - 1) - must be set before RX enable
14664
    wbm_write(`ETH_TX_BD_NUM, 32'h7F, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14665
    // enable RX, set full-duplex mode, NO receive small, NO correct IFG
14666
    wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
14667
              `ETH_MODER_PRO | `ETH_MODER_BRO,
14668
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14669
    // prepare one control (PAUSE)packet
14670
    st_data = 8'h00;
14671
    set_rx_packet(0, 60, 1'b0, 48'h0180_c200_0001, 48'h0708_090A_0B0C, 16'h8808, st_data); // length without CRC
14672
    // prepare one packet of 100 bytes long
14673
    st_data = 8'h1A;
14674
    set_rx_packet(64, 100, 1'b0, 48'h1234_5678_8765, 48'hA1B2_C3D4_E5F6, 16'hE77E, st_data);
14675
    // check WB INT signal
14676
    if (wb_int !== 1'b0)
14677
    begin
14678
      test_fail("WB INT signal should not be set");
14679
      fail = fail + 1;
14680
    end
14681
 
14682
    // write to phy's control register for 10Mbps
14683
    #Tp eth_phy.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
14684
    #Tp eth_phy.control_bit8_0   = 9'h1_00;  // bit 6 reset  - (10/100), bit 8 set - FD
14685
    speed = 10;
14686 254 mohor
 
14687 260 mohor
    for (i=0; i<4; i=i+1)
14688 254 mohor
    begin
14689 260 mohor
      // choose generating carrier sense and collision for first and last 64 lengths of frames
14690
      case (i)
14691
      0: // Interrupt is generated
14692 254 mohor
      begin
14693 260 mohor
        // enable interrupt generation
14694
        set_rx_bd(127, 127, 1'b1, (`MEMORY_BASE + i));
14695 254 mohor
        // unmask interrupts
14696
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
14697
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14698
        // not detect carrier sense in FD and no collision
14699 260 mohor
        eth_phy.no_carrier_sense_rx_fd_detect(0);
14700 254 mohor
        eth_phy.collision(0);
14701
      end
14702 260 mohor
      1: // Interrupt is not generated
14703 254 mohor
      begin
14704 260 mohor
        // enable interrupt generation
14705
        set_rx_bd(127, 127, 1'b1, ((`MEMORY_BASE + i) + 64));
14706 254 mohor
        // mask interrupts
14707
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14708
        // detect carrier sense in FD and no collision
14709 260 mohor
        eth_phy.no_carrier_sense_rx_fd_detect(1);
14710 254 mohor
        eth_phy.collision(0);
14711
      end
14712 260 mohor
      2: // Interrupt is not generated
14713 254 mohor
      begin
14714 260 mohor
        // disable interrupt generation
14715
        set_rx_bd(127, 127, 1'b0, (`MEMORY_BASE + i));
14716 254 mohor
        // unmask interrupts
14717
        wbm_write(`ETH_INT_MASK, `ETH_INT_TXB | `ETH_INT_TXE | `ETH_INT_RXB | `ETH_INT_RXE | `ETH_INT_BUSY |
14718
                                 `ETH_INT_TXC | `ETH_INT_RXC, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14719
        // not detect carrier sense in FD and set collision
14720 260 mohor
        eth_phy.no_carrier_sense_rx_fd_detect(0);
14721 254 mohor
        eth_phy.collision(1);
14722
      end
14723 260 mohor
      default: // 3: // Interrupt is not generated
14724 254 mohor
      begin
14725 260 mohor
        // disable interrupt generation
14726
        set_rx_bd(127, 127, 1'b0, ((`MEMORY_BASE + i) + 64));
14727 254 mohor
        // mask interrupts
14728
        wbm_write(`ETH_INT_MASK, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14729
        // detect carrier sense in FD and set collision
14730 260 mohor
        eth_phy.no_carrier_sense_rx_fd_detect(1);
14731 254 mohor
        eth_phy.collision(1);
14732
      end
14733
      endcase
14734 260 mohor
 
14735
      append_rx_crc (64, 100, 1'b0, 1'b0); // To the second (data) packet
14736
      // set wrap bit
14737
      set_rx_bd_wrap(127);
14738
      set_rx_bd_empty(127, 127);
14739
      fork
14740 254 mohor
        begin
14741 260 mohor
          if (i[0] == 1'b0)
14742
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 0, 64, 1'b0);
14743 254 mohor
          else
14744 260 mohor
            #1 eth_phy.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5, 64, 104, 1'b0);
14745
          repeat(10) @(posedge mrx_clk);
14746
$display("1111");
14747 254 mohor
        end
14748
        begin
14749 260 mohor
          #1 check_rx_bd(127, data);
14750
$display("aaaa");
14751
          wait (MRxDV === 1'b1); // start transmit
14752
$display("bbbb");
14753
          #1 check_rx_bd(127, data);
14754
          if (data[15] !== 1)
14755
          begin
14756
            test_fail("Wrong buffer descriptor's ready bit read out from MAC");
14757
            fail = fail + 1;
14758
          end
14759
          wait (MRxDV === 1'b0); // end transmit
14760
$display("cccc");
14761
          while (data[15] === 1)
14762
          begin
14763
            #1 check_rx_bd(127, data);
14764
            @(posedge wb_clk);
14765
          end
14766
          repeat (1) @(posedge wb_clk);
14767
$display("2222");
14768 254 mohor
        end
14769 260 mohor
      join
14770
$display("dddd");
14771
      // check length of a PACKET
14772
      if (data[31:16] != (i_length + 4))
14773 254 mohor
      begin
14774 260 mohor
        `TIME; $display("*E Wrong length of the packet out from PHY (%0d instead of %0d)",
14775
                        data[31:16], (i_length + 4));
14776
        test_fail("Wrong length of the packet out from PHY");
14777
        fail = fail + 1;
14778 254 mohor
      end
14779 260 mohor
      // checking in the following if statement is performed only for first and last 64 lengths
14780
      // check received RX packet data and CRC
14781
      if (i_length[0] == 1'b0)
14782 254 mohor
      begin
14783 260 mohor
        check_rx_packet(0, (`MEMORY_BASE + i_length[1:0]), (i_length + 4), 1'b0, 1'b0, tmp);
14784 254 mohor
      end
14785 260 mohor
      else
14786 254 mohor
      begin
14787 260 mohor
        check_rx_packet(max_tmp, ((`MEMORY_BASE + i_length[1:0]) + max_tmp), (i_length + 4), 1'b0, 1'b0, tmp);
14788 254 mohor
      end
14789 260 mohor
      if (tmp > 0)
14790 254 mohor
      begin
14791 260 mohor
        `TIME; $display("*E Wrong data of the received packet");
14792
        test_fail("Wrong data of the received packet");
14793
        fail = fail + 1;
14794 254 mohor
      end
14795
      // check WB INT signal
14796 260 mohor
      if (i_length[1:0] == 2'h0)
14797 254 mohor
      begin
14798
        if (wb_int !== 1'b1)
14799
        begin
14800
          `TIME; $display("*E WB INT signal should be set");
14801
          test_fail("WB INT signal should be set");
14802
          fail = fail + 1;
14803
        end
14804
      end
14805
      else
14806
      begin
14807
        if (wb_int !== 1'b0)
14808
        begin
14809
          `TIME; $display("*E WB INT signal should not be set");
14810
          test_fail("WB INT signal should not be set");
14811
          fail = fail + 1;
14812
        end
14813
      end
14814 260 mohor
      // check RX buffer descriptor of a packet
14815
      check_rx_bd(127, data);
14816
      if (i_length[1] == 1'b0) // interrupt enabled no_carrier_sense_rx_fd_detect
14817 254 mohor
      begin
14818 260 mohor
        if ( ((data[15:0] !== 16'h6000) && (i_length[0] == 1'b0)) ||
14819
             ((data[15:0] !== 16'h6000) && (i_length[0] == 1'b1)) )
14820 254 mohor
        begin
14821 260 mohor
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
14822
          test_fail("RX buffer descriptor status is not correct");
14823
          fail = fail + 1;
14824 254 mohor
        end
14825
      end
14826 260 mohor
      else // interrupt not enabled
14827 254 mohor
      begin
14828 260 mohor
        if ( ((data[15:0] !== 16'h2000) && (i_length[0] == 1'b0)) ||
14829
             ((data[15:0] !== 16'h2000) && (i_length[0] == 1'b1)) )
14830 254 mohor
        begin
14831 260 mohor
          `TIME; $display("*E RX buffer descriptor status is not correct: %0h", data[15:0]);
14832
          test_fail("RX buffer descriptor status is not correct");
14833 254 mohor
          fail = fail + 1;
14834
        end
14835
      end
14836 260 mohor
      // clear RX buffer descriptor for first 4 frames
14837
      if (i_length < min_tmp)
14838
        clear_rx_bd(127, 127);
14839 254 mohor
      // check interrupts
14840
      wbm_read(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14841 260 mohor
      if ((i_length[1:0] == 2'h0) || (i_length[1:0] == 2'h1))
14842 254 mohor
      begin
14843 260 mohor
        if ((data & `ETH_INT_RXB) !== `ETH_INT_RXB)
14844 254 mohor
        begin
14845 260 mohor
          `TIME; $display("*E Interrupt Receive Buffer was not set, interrupt reg: %0h", data);
14846
          test_fail("Interrupt Receive Buffer was not set");
14847 254 mohor
          fail = fail + 1;
14848
        end
14849 260 mohor
        if ((data & (~`ETH_INT_RXB)) !== 0)
14850 254 mohor
        begin
14851 260 mohor
          `TIME; $display("*E Other interrupts (except Receive Buffer) were set, interrupt reg: %0h", data);
14852
          test_fail("Other interrupts (except Receive Buffer) were set");
14853 254 mohor
          fail = fail + 1;
14854
        end
14855
      end
14856
      else
14857
      begin
14858
        if (data !== 0)
14859
        begin
14860 260 mohor
          `TIME; $display("*E Any of interrupts (except Receive Buffer) was set, interrupt reg: %0h, len: %0h", data, i_length[1:0]);
14861
          test_fail("Any of interrupts (except Receive Buffer) was set");
14862 254 mohor
          fail = fail + 1;
14863
        end
14864
      end
14865
      // clear interrupts
14866
      wbm_write(`ETH_INT, data, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14867
      // check WB INT signal
14868
      if (wb_int !== 1'b0)
14869
      begin
14870
        test_fail("WB INT signal should not be set");
14871
        fail = fail + 1;
14872
      end
14873
      // INTERMEDIATE DISPLAYS
14874 260 mohor
      if ((i_length + 4) == (min_tmp + 64))
14875 254 mohor
      begin
14876 260 mohor
        // starting length is min_tmp, ending length is (min_tmp + 64)
14877
        $display("    receive small packets is NOT selected");
14878
        $display("    ->packets with lengths from %0d (MINFL) to %0d are checked (length increasing by 1 byte)",
14879
                 min_tmp, (min_tmp + 64));
14880
        // set receive small, remain the rest
14881
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
14882
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
14883
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14884 254 mohor
      end
14885 260 mohor
      else if ((i_length + 4) == (max_tmp - 16))
14886 254 mohor
      begin
14887 260 mohor
        // starting length is for +128 longer than previous ending length, while ending length is tmp_data
14888
        $display("    receive small packets is selected");
14889
        $display("    ->packets with lengths from %0d to %0d are checked (length increasing by 128 bytes)",
14890
                 (min_tmp + 64 + 128), tmp_data);
14891
        // reset receive small, remain the rest
14892
        wbm_write(`ETH_MODER, `ETH_MODER_RXEN | `ETH_MODER_FULLD | `ETH_MODER_IFG |
14893
                  `ETH_MODER_PRO | `ETH_MODER_BRO,
14894
                  4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14895 254 mohor
      end
14896 260 mohor
      else if ((i_length + 4) == max_tmp)
14897 254 mohor
      begin
14898 260 mohor
        $display("    receive small packets is NOT selected");
14899
        $display("    ->packets with lengths from %0d to %0d (MAXFL) are checked (length increasing by 1 byte)",
14900
                 (max_tmp - (4 + 16)), max_tmp);
14901 254 mohor
      end
14902 260 mohor
      // set length (loop variable)
14903
      if ((i_length + 4) < (min_tmp + 64))
14904
        i_length = i_length + 1;
14905
      else if ( ((i_length + 4) >= (min_tmp + 64)) && ((i_length + 4) <= (max_tmp - 256)) )
14906 254 mohor
      begin
14907 260 mohor
        i_length = i_length + 128;
14908
        tmp_data = i_length + 4; // last tmp_data is ending length
14909 254 mohor
      end
14910 260 mohor
      else if ( ((i_length + 4) > (max_tmp - 256)) && ((i_length + 4) < (max_tmp - 16)) )
14911
        i_length = max_tmp - (4 + 16);
14912
      else if ((i_length + 4) >= (max_tmp - 16))
14913
        i_length = i_length + 1;
14914
      else
14915 254 mohor
      begin
14916 260 mohor
        $display("*E TESTBENCH ERROR - WRONG PARAMETERS IN TESTBENCH");
14917
        #10 $stop;
14918 254 mohor
      end
14919
    end
14920 260 mohor
    // disable RX
14921
    wbm_write(`ETH_MODER, `ETH_MODER_FULLD | `ETH_MODER_RECSMALL | `ETH_MODER_IFG |
14922
              `ETH_MODER_PRO | `ETH_MODER_BRO,
14923 254 mohor
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
14924
    if(fail == 0)
14925
      test_ok;
14926
    else
14927
      fail = 0;
14928 209 tadejm
  end
14929
 
14930
 
14931
end   //  for (test_num=start_task; test_num <= end_task; test_num=test_num+1)
14932
 
14933 260 mohor
 
14934 209 tadejm
end
14935
endtask // test_mac_full_duplex_flow
14936
 
14937
 
14938 169 mohor
//////////////////////////////////////////////////////////////
14939
// WB Behavioral Models Basic tasks
14940
//////////////////////////////////////////////////////////////
14941
 
14942
task wbm_write;
14943
  input  [31:0] address_i;
14944
  input  [((`MAX_BLK_SIZE * 32) - 1):0] data_i;
14945
  input  [3:0]  sel_i;
14946
  input  [31:0] size_i;
14947
  input  [3:0]  init_waits_i;
14948
  input  [3:0]  subseq_waits_i;
14949
 
14950
  reg `WRITE_STIM_TYPE write_data;
14951
  reg `WB_TRANSFER_FLAGS flags;
14952
  reg `WRITE_RETURN_TYPE write_status;
14953
  integer i;
14954
begin
14955
  write_status = 0;
14956
 
14957
  flags                    = 0;
14958
  flags`WB_TRANSFER_SIZE   = size_i;
14959
  flags`INIT_WAITS         = init_waits_i;
14960
  flags`SUBSEQ_WAITS       = subseq_waits_i;
14961
 
14962
  write_data               = 0;
14963
  write_data`WRITE_DATA    = data_i[31:0];
14964
  write_data`WRITE_ADDRESS = address_i;
14965
  write_data`WRITE_SEL     = sel_i;
14966
 
14967
  for (i = 0; i < size_i; i = i + 1)
14968
  begin
14969
    wb_master.blk_write_data[i] = write_data;
14970
    data_i                      = data_i >> 32;
14971
    write_data`WRITE_DATA       = data_i[31:0];
14972
    write_data`WRITE_ADDRESS    = write_data`WRITE_ADDRESS + 4;
14973 116 mohor
  end
14974
 
14975 169 mohor
  wb_master.wb_block_write(flags, write_status);
14976 116 mohor
 
14977 169 mohor
  if (write_status`CYC_ACTUAL_TRANSFER !== size_i)
14978
  begin
14979
    `TIME;
14980
    $display("*E WISHBONE Master was unable to complete the requested write operation to MAC!");
14981
  end
14982
end
14983
endtask // wbm_write
14984 116 mohor
 
14985 169 mohor
task wbm_read;
14986
  input  [31:0] address_i;
14987
  output [((`MAX_BLK_SIZE * 32) - 1):0] data_o;
14988
  input  [3:0]  sel_i;
14989
  input  [31:0] size_i;
14990
  input  [3:0]  init_waits_i;
14991
  input  [3:0]  subseq_waits_i;
14992
 
14993
  reg `READ_RETURN_TYPE read_data;
14994
  reg `WB_TRANSFER_FLAGS flags;
14995
  reg `READ_RETURN_TYPE read_status;
14996
  integer i;
14997
begin
14998
  read_status = 0;
14999
  data_o      = 0;
15000
 
15001
  flags                  = 0;
15002
  flags`WB_TRANSFER_SIZE = size_i;
15003
  flags`INIT_WAITS       = init_waits_i;
15004
  flags`SUBSEQ_WAITS     = subseq_waits_i;
15005
 
15006
  read_data              = 0;
15007
  read_data`READ_ADDRESS = address_i;
15008
  read_data`READ_SEL     = sel_i;
15009
 
15010
  for (i = 0; i < size_i; i = i + 1)
15011 116 mohor
  begin
15012 169 mohor
    wb_master.blk_read_data_in[i] = read_data;
15013
    read_data`READ_ADDRESS        = read_data`READ_ADDRESS + 4;
15014
  end
15015
 
15016
  wb_master.wb_block_read(flags, read_status);
15017
 
15018
  if (read_status`CYC_ACTUAL_TRANSFER !== size_i)
15019
  begin
15020
    `TIME;
15021
    $display("*E WISHBONE Master was unable to complete the requested read operation from MAC!");
15022
  end
15023
 
15024
  for (i = 0; i < size_i; i = i + 1)
15025
  begin
15026
    data_o       = data_o << 32;
15027
    read_data    = wb_master.blk_read_data_out[(size_i - 1) - i]; // [31 - i];
15028
    data_o[31:0] = read_data`READ_DATA;
15029
  end
15030
end
15031
endtask // wbm_read
15032
 
15033
 
15034
//////////////////////////////////////////////////////////////
15035
// Ethernet Basic tasks
15036
//////////////////////////////////////////////////////////////
15037
 
15038
task hard_reset; //  MAC registers
15039
begin
15040
  // reset MAC registers
15041
  @(posedge wb_clk);
15042
  #2 wb_rst = 1'b1;
15043
  repeat(2) @(posedge wb_clk);
15044
  #2 wb_rst = 1'b0;
15045
end
15046
endtask // hard_reset
15047
 
15048
task reset_mac; //  MAC module
15049
  reg [31:0] tmp;
15050
  reg [31:0] tmp_no_rst;
15051
begin
15052
  // read MODER register first
15053
  wbm_read(`ETH_MODER, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15054
  // set reset bit - write back to MODER register with RESET bit
15055
  wbm_write(`ETH_MODER, (`ETH_MODER_RST | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15056
  // clear reset bit - write back to MODER register without RESET bit
15057
  tmp_no_rst = `ETH_MODER_RST;
15058
  tmp_no_rst = ~tmp_no_rst;
15059
  wbm_write(`ETH_MODER, (tmp_no_rst & tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15060
end
15061
endtask // reset_mac
15062
 
15063
task set_tx_bd;
15064
  input  [6:0]  tx_bd_num_start;
15065
  input  [6:0]  tx_bd_num_end;
15066
  input  [15:0] len;
15067
  input         irq;
15068
  input         pad;
15069
  input         crc;
15070
  input  [31:0] txpnt;
15071
 
15072
  integer       i;
15073
  integer       bd_status_addr, bd_ptr_addr;
15074
//  integer       buf_addr;
15075
begin
15076
  for(i = tx_bd_num_start; i <= tx_bd_num_end; i = i + 1)
15077
  begin
15078
//    buf_addr = `TX_BUF_BASE + i * 32'h600;
15079
    bd_status_addr = `TX_BD_BASE + i * 8;
15080
    bd_ptr_addr = bd_status_addr + 4;
15081
    // initialize BD - status
15082
//    wbm_write(bd_status_addr, 32'h00005800, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // IRQ + PAD + CRC
15083
    wbm_write(bd_status_addr, {len, 1'b0, irq, 1'b0, pad, crc, 11'h0},
15084
              4'hF, 1, wbm_init_waits, wbm_subseq_waits); // IRQ + PAD + CRC
15085
    // initialize BD - pointer
15086
//    wbm_write(bd_ptr_addr, buf_addr, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // Initializing BD-pointer
15087
    wbm_write(bd_ptr_addr, txpnt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // Initializing BD-pointer
15088
  end
15089
end
15090
endtask // set_tx_bd
15091
 
15092
task set_tx_bd_wrap;
15093
  input  [6:0]  tx_bd_num_end;
15094
  integer       bd_status_addr, tmp;
15095
begin
15096
  bd_status_addr = `TX_BD_BASE + tx_bd_num_end * 8;
15097
  wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15098
  // set wrap bit to this BD - this BD should be last-one
15099
  wbm_write(bd_status_addr, (`ETH_TX_BD_WRAP | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15100
end
15101
endtask // set_tx_bd_wrap
15102
 
15103
task set_tx_bd_ready;
15104
  input  [6:0]  tx_nd_num_strat;
15105
  input  [6:0]  tx_bd_num_end;
15106
  integer       i;
15107
  integer       bd_status_addr, tmp;
15108
begin
15109
  for(i = tx_nd_num_strat; i <= tx_bd_num_end; i = i + 1)
15110
  begin
15111
    bd_status_addr = `TX_BD_BASE + i * 8;
15112
    wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15113
    // set empty bit to this BD - this BD should be ready
15114
    wbm_write(bd_status_addr, (`ETH_TX_BD_READY | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15115
  end
15116
end
15117
endtask // set_tx_bd_ready
15118
 
15119
task check_tx_bd;
15120
  input  [6:0]  tx_bd_num_end;
15121
  output [31:0] tx_bd_status;
15122
  integer       bd_status_addr, tmp;
15123
begin
15124
  bd_status_addr = `TX_BD_BASE + tx_bd_num_end * 8;
15125
  wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15126
  #1 tx_bd_status = tmp;
15127
  #1;
15128
end
15129
endtask // check_tx_bd
15130
 
15131
task clear_tx_bd;
15132
  input  [6:0]  tx_nd_num_strat;
15133
  input  [6:0]  tx_bd_num_end;
15134
  integer       i;
15135
  integer       bd_status_addr, bd_ptr_addr;
15136
begin
15137
  for(i = tx_nd_num_strat; i <= tx_bd_num_end; i = i + 1)
15138
  begin
15139
    bd_status_addr = `TX_BD_BASE + i * 8;
15140
    bd_ptr_addr = bd_status_addr + 4;
15141
    // clear BD - status
15142
    wbm_write(bd_status_addr, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15143
    // clear BD - pointer
15144
    wbm_write(bd_ptr_addr, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15145
  end
15146
end
15147
endtask // clear_tx_bd
15148
 
15149
task set_rx_bd;
15150
  input  [6:0]  rx_bd_num_strat;
15151
  input  [6:0]  rx_bd_num_end;
15152
  input         irq;
15153
  input  [31:0] rxpnt;
15154
//  input  [6:0]  rxbd_num;
15155
  integer       i;
15156
  integer       bd_status_addr, bd_ptr_addr;
15157
//  integer       buf_addr;
15158
begin
15159
  for(i = rx_bd_num_strat; i <= rx_bd_num_end; i = i + 1)
15160
  begin
15161
//    buf_addr = `RX_BUF_BASE + i * 32'h600;
15162 209 tadejm
//    bd_status_addr = `RX_BD_BASE + i * 8;
15163
//    bd_ptr_addr = bd_status_addr + 4; 
15164
    bd_status_addr = `TX_BD_BASE + i * 8;
15165
    bd_ptr_addr = bd_status_addr + 4;
15166 116 mohor
 
15167 169 mohor
    // initialize BD - status
15168
//    wbm_write(bd_status_addr, 32'h0000c000, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // IRQ + PAD + CRC
15169
    wbm_write(bd_status_addr, {17'h0, irq, 14'h0},
15170
              4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15171
    // initialize BD - pointer
15172
//    wbm_write(bd_ptr_addr, buf_addr, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // Initializing BD-pointer
15173
    wbm_write(bd_ptr_addr, rxpnt, 4'hF, 1, wbm_init_waits, wbm_subseq_waits); // Initializing BD-pointer
15174
  end
15175
end
15176
endtask // set_rx_bd
15177 116 mohor
 
15178 169 mohor
task set_rx_bd_wrap;
15179
  input  [6:0]  rx_bd_num_end;
15180
  integer       bd_status_addr, tmp;
15181
begin
15182 209 tadejm
//  bd_status_addr = `RX_BD_BASE + rx_bd_num_end * 8;
15183
  bd_status_addr = `TX_BD_BASE + rx_bd_num_end * 8;
15184 169 mohor
  wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15185
  // set wrap bit to this BD - this BD should be last-one
15186
  wbm_write(bd_status_addr, (`ETH_RX_BD_WRAP | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15187
end
15188
endtask // set_rx_bd_wrap
15189 116 mohor
 
15190 169 mohor
task set_rx_bd_empty;
15191
  input  [6:0]  rx_bd_num_strat;
15192
  input  [6:0]  rx_bd_num_end;
15193
  integer       i;
15194
  integer       bd_status_addr, tmp;
15195
begin
15196
  for(i = rx_bd_num_strat; i <= rx_bd_num_end; i = i + 1)
15197
  begin
15198 209 tadejm
//    bd_status_addr = `RX_BD_BASE + i * 8;
15199
    bd_status_addr = `TX_BD_BASE + i * 8;
15200 169 mohor
    wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15201
    // set empty bit to this BD - this BD should be ready
15202
    wbm_write(bd_status_addr, (`ETH_RX_BD_EMPTY | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15203 116 mohor
  end
15204 169 mohor
end
15205
endtask // set_rx_bd_empty
15206 116 mohor
 
15207 169 mohor
task check_rx_bd;
15208
  input  [6:0]  rx_bd_num_end;
15209
  output [31:0] rx_bd_status;
15210
  integer       bd_status_addr, tmp;
15211
begin
15212 209 tadejm
//  bd_status_addr = `RX_BD_BASE + rx_bd_num_end * 8;
15213
  bd_status_addr = `TX_BD_BASE + rx_bd_num_end * 8;
15214 169 mohor
  wbm_read(bd_status_addr, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15215
  #1 rx_bd_status = tmp;
15216
  #1;
15217
end
15218
endtask // check_rx_bd
15219 116 mohor
 
15220 169 mohor
task clear_rx_bd;
15221
  input  [6:0]  rx_bd_num_strat;
15222
  input  [6:0]  rx_bd_num_end;
15223
  integer       i;
15224
  integer       bd_status_addr, bd_ptr_addr;
15225
begin
15226
  for(i = rx_bd_num_strat; i <= rx_bd_num_end; i = i + 1)
15227
  begin
15228 209 tadejm
//    bd_status_addr = `RX_BD_BASE + i * 8;
15229
    bd_status_addr = `TX_BD_BASE + i * 8;
15230 169 mohor
    bd_ptr_addr = bd_status_addr + 4;
15231
    // clear BD - status
15232
    wbm_write(bd_status_addr, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15233
    // clear BD - pointer
15234
    wbm_write(bd_ptr_addr, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
15235
  end
15236
end
15237
endtask // clear_rx_bd
15238 116 mohor
 
15239 169 mohor
task set_tx_packet;
15240
  input  [31:0] txpnt;
15241
  input  [15:0] len;
15242
  input  [7:0]  eth_start_data;
15243
  integer       i, sd;
15244
  integer       buffer;
15245
  reg           delta_t;
15246
begin
15247
  buffer = txpnt;
15248
  sd = eth_start_data;
15249
  delta_t = 0;
15250 116 mohor
 
15251 169 mohor
  // First write might not be word allign.
15252
  if(buffer[1:0] == 1)
15253
  begin
15254
    wb_slave.wr_mem(buffer - 1, {8'h0, sd[7:0], sd[7:0] + 3'h1, sd[7:0] + 3'h2}, 4'h7);
15255
    sd = sd + 3;
15256
    i = 3;
15257
  end
15258
  else if(buffer[1:0] == 2)
15259
  begin
15260
    wb_slave.wr_mem(buffer - 2, {16'h0, sd[7:0], sd[7:0] + 3'h1}, 4'h3);
15261
    sd = sd + 2;
15262
    i = 2;
15263
  end
15264
  else if(buffer[1:0] == 3)
15265
  begin
15266
    wb_slave.wr_mem(buffer - 3, {24'h0, sd[7:0]}, 4'h1);
15267
    sd = sd + 1;
15268
    i = 1;
15269
  end
15270
  else
15271
    i = 0;
15272
  delta_t = !delta_t;
15273 116 mohor
 
15274 169 mohor
  for(i = i; i < (len - 4); i = i + 4) // Last 0-3 bytes are not written
15275
  begin
15276
    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);
15277
    sd = sd + 4;
15278
  end
15279
  delta_t = !delta_t;
15280
 
15281
  // Last word
15282
  if((len - i) == 3)
15283 116 mohor
  begin
15284 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);
15285
  end
15286
  else if((len - i) == 2)
15287
  begin
15288
    wb_slave.wr_mem(buffer + i, {sd[7:0], sd[7:0] + 3'h1, 16'h0}, 4'hC);
15289
  end
15290
  else if((len - i) == 1)
15291
  begin
15292
    wb_slave.wr_mem(buffer + i, {sd[7:0], 24'h0}, 4'h8);
15293
  end
15294
  else if((len - i) == 4)
15295
  begin
15296
    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);
15297
  end
15298
  else
15299
    $display("(%0t)(%m) ERROR", $time);
15300
  delta_t = !delta_t;
15301
end
15302
endtask // set_tx_packet
15303
 
15304
task check_tx_packet;
15305
  input  [31:0] txpnt_wb;  // source
15306
  input  [31:0] txpnt_phy; // destination
15307
  input  [15:0] len;
15308
  output [31:0] failure;
15309
  integer       i, data_wb, data_phy;
15310
  reg    [31:0] addr_wb, addr_phy;
15311
  reg    [31:0] failure;
15312
  reg           delta_t;
15313
begin
15314
  addr_wb = txpnt_wb;
15315
  addr_phy = txpnt_phy;
15316
  delta_t = 0;
15317
  failure = 0;
15318 209 tadejm
  #1;
15319 169 mohor
  // First write might not be word allign.
15320
  if(addr_wb[1:0] == 1)
15321
  begin
15322
    wb_slave.rd_mem(addr_wb - 1, data_wb, 4'h7);
15323
    data_phy[31:24] = 0;
15324
    data_phy[23:16] = eth_phy.tx_mem[addr_phy[21:0]];
15325
    data_phy[15: 8] = eth_phy.tx_mem[addr_phy[21:0] + 1];
15326
    data_phy[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0] + 2];
15327
    i = 3;
15328
    if (data_phy[23:0] !== data_wb[23:0])
15329
    begin
15330
      `TIME;
15331 209 tadejm
      $display("*E Wrong 1. word (3 bytes) of TX packet! phy: %0h, wb: %0h", data_phy[23:0], data_wb[23:0]);
15332
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15333 169 mohor
      failure = 1;
15334
    end
15335
  end
15336
  else if (addr_wb[1:0] == 2)
15337
  begin
15338
    wb_slave.rd_mem(addr_wb - 2, data_wb, 4'h3);
15339
    data_phy[31:16] = 0;
15340
    data_phy[15: 8] = eth_phy.tx_mem[addr_phy[21:0]];
15341
    data_phy[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0] + 1];
15342
    i = 2;
15343
    if (data_phy[15:0] !== data_wb[15:0])
15344
    begin
15345
      `TIME;
15346 209 tadejm
      $display("*E Wrong 1. word (2 bytes) of TX packet! phy: %0h, wb: %0h", data_phy[15:0], data_wb[15:0]);
15347
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15348 169 mohor
      failure = 1;
15349
    end
15350
  end
15351
  else if (addr_wb[1:0] == 3)
15352
  begin
15353
    wb_slave.rd_mem(addr_wb - 3, data_wb, 4'h1);
15354
    data_phy[31: 8] = 0;
15355
    data_phy[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0]];
15356
    i = 1;
15357
    if (data_phy[7:0] !== data_wb[7:0])
15358
    begin
15359
      `TIME;
15360 209 tadejm
      $display("*E Wrong 1. word (1 byte) of TX packet! phy: %0h, wb: %0h", data_phy[7:0], data_wb[7:0]);
15361
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15362 169 mohor
      failure = 1;
15363
    end
15364
  end
15365
  else
15366
    i = 0;
15367
  delta_t = !delta_t;
15368 209 tadejm
  #1;
15369 169 mohor
  for(i = i; i < (len - 4); i = i + 4) // Last 0-3 bytes are not checked
15370
  begin
15371
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hF);
15372
    data_phy[31:24] = eth_phy.tx_mem[addr_phy[21:0] + i];
15373
    data_phy[23:16] = eth_phy.tx_mem[addr_phy[21:0] + i + 1];
15374
    data_phy[15: 8] = eth_phy.tx_mem[addr_phy[21:0] + i + 2];
15375
    data_phy[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0] + i + 3];
15376
    if (data_phy[31:0] !== data_wb[31:0])
15377
    begin
15378
      `TIME;
15379 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]);
15380
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15381 169 mohor
      failure = failure + 1;
15382
    end
15383
  end
15384
  delta_t = !delta_t;
15385 209 tadejm
  #1;
15386 169 mohor
  // Last word
15387
  if((len - i) == 3)
15388
  begin
15389
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hE);
15390
    data_phy[31:24] = eth_phy.tx_mem[addr_phy[21:0] + i];
15391
    data_phy[23:16] = eth_phy.tx_mem[addr_phy[21:0] + i + 1];
15392
    data_phy[15: 8] = eth_phy.tx_mem[addr_phy[21:0] + i + 2];
15393
    data_phy[ 7: 0] = 0;
15394
    if (data_phy[31:8] !== data_wb[31:8])
15395
    begin
15396
      `TIME;
15397 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]);
15398
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15399 169 mohor
      failure = failure + 1;
15400
    end
15401
  end
15402
  else if((len - i) == 2)
15403
  begin
15404
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hC);
15405
    data_phy[31:24] = eth_phy.tx_mem[addr_phy[21:0] + i];
15406
    data_phy[23:16] = eth_phy.tx_mem[addr_phy[21:0] + i + 1];
15407
    data_phy[15: 8] = 0;
15408
    data_phy[ 7: 0] = 0;
15409
    if (data_phy[31:16] !== data_wb[31:16])
15410
    begin
15411
      `TIME;
15412 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]);
15413
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15414 169 mohor
      failure = failure + 1;
15415
    end
15416
  end
15417
  else if((len - i) == 1)
15418
  begin
15419
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'h8);
15420
    data_phy[31:24] = eth_phy.tx_mem[addr_phy[21:0] + i];
15421
    data_phy[23:16] = 0;
15422
    data_phy[15: 8] = 0;
15423
    data_phy[ 7: 0] = 0;
15424
    if (data_phy[31:24] !== data_wb[31:24])
15425
    begin
15426
      `TIME;
15427 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]);
15428
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15429 169 mohor
      failure = failure + 1;
15430
    end
15431
  end
15432
  else if((len - i) == 4)
15433
  begin
15434
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hF);
15435
    data_phy[31:24] = eth_phy.tx_mem[addr_phy[21:0] + i];
15436
    data_phy[23:16] = eth_phy.tx_mem[addr_phy[21:0] + i + 1];
15437
    data_phy[15: 8] = eth_phy.tx_mem[addr_phy[21:0] + i + 2];
15438
    data_phy[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0] + i + 3];
15439
    if (data_phy[31:0] !== data_wb[31:0])
15440
    begin
15441
      `TIME;
15442 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]);
15443
      $display("     address phy: %0h, address wb: %0h", addr_phy, addr_wb);
15444 169 mohor
      failure = failure + 1;
15445
    end
15446
  end
15447
  else
15448
    $display("(%0t)(%m) ERROR", $time);
15449
  delta_t = !delta_t;
15450
end
15451
endtask // check_tx_packet
15452
 
15453
task set_rx_packet;
15454
  input  [31:0] rxpnt;
15455
  input  [15:0] len;
15456
  input         plus_dribble_nibble; // if length is longer for one nibble
15457
  input  [47:0] eth_dest_addr;
15458
  input  [47:0] eth_source_addr;
15459
  input  [15:0] eth_type_len;
15460
  input  [7:0]  eth_start_data;
15461
  integer       i, sd;
15462
  reg    [47:0] dest_addr;
15463
  reg    [47:0] source_addr;
15464
  reg    [15:0] type_len;
15465
  reg    [21:0] buffer;
15466
  reg           delta_t;
15467
begin
15468
  buffer = rxpnt[21:0];
15469
  dest_addr = eth_dest_addr;
15470
  source_addr = eth_source_addr;
15471
  type_len = eth_type_len;
15472
  sd = eth_start_data;
15473
  delta_t = 0;
15474
  for(i = 0; i < len; i = i + 1)
15475
  begin
15476
    if (i < 6)
15477
    begin
15478
      eth_phy.rx_mem[buffer] = dest_addr[47:40];
15479
      dest_addr = dest_addr << 8;
15480
    end
15481
    else if (i < 12)
15482
    begin
15483
      eth_phy.rx_mem[buffer] = source_addr[47:40];
15484
      source_addr = source_addr << 8;
15485
    end
15486
    else if (i < 14)
15487
    begin
15488
      eth_phy.rx_mem[buffer] = type_len[15:8];
15489
      type_len = type_len << 8;
15490
    end
15491
    else
15492
    begin
15493
      eth_phy.rx_mem[buffer] = sd[7:0];
15494
      sd = sd + 1;
15495
    end
15496
    buffer = buffer + 1;
15497
  end
15498
  delta_t = !delta_t;
15499
  if (plus_dribble_nibble)
15500
    eth_phy.rx_mem[buffer] = {4'h0, 4'hD /*sd[3:0]*/};
15501
  delta_t = !delta_t;
15502
end
15503
endtask // set_rx_packet
15504
 
15505 243 tadejm
task set_rx_addr_type;
15506
  input  [31:0] rxpnt;
15507
  input  [47:0] eth_dest_addr;
15508
  input  [47:0] eth_source_addr;
15509
  input  [15:0] eth_type_len;
15510
  integer       i;
15511
  reg    [47:0] dest_addr;
15512
  reg    [47:0] source_addr;
15513
  reg    [15:0] type_len;
15514
  reg    [21:0] buffer;
15515
  reg           delta_t;
15516
begin
15517
  buffer = rxpnt[21:0];
15518
  dest_addr = eth_dest_addr;
15519
  source_addr = eth_source_addr;
15520
  type_len = eth_type_len;
15521
  delta_t = 0;
15522
  for(i = 0; i < 14; i = i + 1)
15523
  begin
15524
    if (i < 6)
15525
    begin
15526
      eth_phy.rx_mem[buffer] = dest_addr[47:40];
15527
      dest_addr = dest_addr << 8;
15528
    end
15529
    else if (i < 12)
15530
    begin
15531
      eth_phy.rx_mem[buffer] = source_addr[47:40];
15532
      source_addr = source_addr << 8;
15533
    end
15534
    else // if (i < 14)
15535
    begin
15536
      eth_phy.rx_mem[buffer] = type_len[15:8];
15537
      type_len = type_len << 8;
15538
    end
15539
    buffer = buffer + 1;
15540
  end
15541
  delta_t = !delta_t;
15542
end
15543
endtask // set_rx_addr_type
15544
 
15545 169 mohor
task check_rx_packet;
15546
  input  [31:0] rxpnt_phy; // source
15547
  input  [31:0] rxpnt_wb;  // destination
15548
  input  [15:0] len;
15549
  input         plus_dribble_nibble; // if length is longer for one nibble
15550
  input         successful_dribble_nibble; // if additional nibble is stored into memory
15551
  output [31:0] failure;
15552
  integer       i, data_wb, data_phy;
15553
  reg    [31:0] addr_wb, addr_phy;
15554
  reg    [31:0] failure;
15555
  reg    [21:0] buffer;
15556
  reg           delta_t;
15557
begin
15558
  addr_phy = rxpnt_phy;
15559
  addr_wb = rxpnt_wb;
15560
  delta_t = 0;
15561
  failure = 0;
15562
 
15563
  // First write might not be word allign.
15564
  if(addr_wb[1:0] == 1)
15565
  begin
15566
    wb_slave.rd_mem(addr_wb - 1, data_wb, 4'h7);
15567
    data_phy[31:24] = 0;
15568
    data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0]];
15569
    data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0] + 1];
15570
    data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0] + 2];
15571
    i = 3;
15572
    if (data_phy[23:0] !== data_wb[23:0])
15573
    begin
15574
      `TIME;
15575 243 tadejm
      $display("   addr_phy = %h, addr_wb = %h", rxpnt_phy, rxpnt_wb);
15576
      $display("*E Wrong 1. word (3 bytes) of RX packet! phy = %h, wb = %h", data_phy[23:0], data_wb[23:0]);
15577 169 mohor
      failure = 1;
15578
    end
15579
  end
15580
  else if (addr_wb[1:0] == 2)
15581
  begin
15582
    wb_slave.rd_mem(addr_wb - 2, data_wb, 4'h3);
15583
    data_phy[31:16] = 0;
15584
    data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0]];
15585
    data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0] + 1];
15586
    i = 2;
15587
    if (data_phy[15:0] !== data_wb[15:0])
15588
    begin
15589
      `TIME;
15590 243 tadejm
      $display("   addr_phy = %h, addr_wb = %h", rxpnt_phy, rxpnt_wb);
15591
      $display("*E Wrong 1. word (2 bytes) of RX packet! phy = %h, wb = %h", data_phy[15:0], data_wb[15:0]);
15592 169 mohor
      failure = 1;
15593
    end
15594
  end
15595
  else if (addr_wb[1:0] == 3)
15596
  begin
15597
    wb_slave.rd_mem(addr_wb - 3, data_wb, 4'h1);
15598
    data_phy[31: 8] = 0;
15599
    data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0]];
15600
    i = 1;
15601
    if (data_phy[7:0] !== data_wb[7:0])
15602
    begin
15603
      `TIME;
15604 243 tadejm
      $display("   addr_phy = %h, addr_wb = %h", rxpnt_phy, rxpnt_wb);
15605
      $display("*E Wrong 1. word (1 byte) of RX packet! phy = %h, wb = %h", data_phy[7:0], data_wb[7:0]);
15606 169 mohor
      failure = 1;
15607
    end
15608
  end
15609
  else
15610
    i = 0;
15611
  delta_t = !delta_t;
15612
 
15613
  for(i = i; i < (len - 4); i = i + 4) // Last 0-3 bytes are not checked
15614
  begin
15615
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hF);
15616
    data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i];
15617
    data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0] + i + 1];
15618
    data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0] + i + 2];
15619
    data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0] + i + 3];
15620
    if (data_phy[31:0] !== data_wb[31:0])
15621
    begin
15622
      `TIME;
15623 243 tadejm
      if (i == 0)
15624
        $display("   addr_phy = %h, addr_wb = %h", rxpnt_phy, rxpnt_wb);
15625
      $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]);
15626 169 mohor
      failure = failure + 1;
15627
    end
15628
  end
15629
  delta_t = !delta_t;
15630
 
15631
  // Last word
15632
  if((len - i) == 3)
15633
  begin
15634
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hF);
15635
    data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i];
15636
    data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0] + i + 1];
15637
    data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0] + i + 2];
15638
    if (plus_dribble_nibble)
15639
      data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0] + i + 3];
15640
    else
15641
      data_phy[ 7: 0] = 0;
15642
    if (data_phy[31:8] !== data_wb[31:8])
15643
    begin
15644
      `TIME;
15645 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]);
15646 169 mohor
      failure = failure + 1;
15647
    end
15648
    if (plus_dribble_nibble && successful_dribble_nibble)
15649
    begin
15650
      if (data_phy[3:0] !== data_wb[3:0])
15651 116 mohor
      begin
15652 169 mohor
        `TIME;
15653 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (3 bytes) of RX packet!", ((i/4)+1));
15654 169 mohor
        failure = failure + 1;
15655 116 mohor
      end
15656 169 mohor
    end
15657
    else if (plus_dribble_nibble && !successful_dribble_nibble)
15658
    begin
15659
      if (data_phy[3:0] === data_wb[3:0])
15660 116 mohor
      begin
15661 169 mohor
        `TIME;
15662 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (3 bytes) of RX packet!", ((i/4)+1));
15663 169 mohor
        failure = failure + 1;
15664 116 mohor
      end
15665 169 mohor
    end
15666
  end
15667
  else if((len - i) == 2)
15668
  begin
15669
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hE);
15670
    data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i];
15671
    data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0] + i + 1];
15672
    if (plus_dribble_nibble)
15673
      data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0] + i + 2];
15674
    else
15675
      data_phy[15: 8] = 0;
15676
    data_phy[ 7: 0] = 0;
15677
    if (data_phy[31:16] !== data_wb[31:16])
15678
    begin
15679
      `TIME;
15680 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]);
15681 169 mohor
      failure = failure + 1;
15682
    end
15683
    if (plus_dribble_nibble && successful_dribble_nibble)
15684
    begin
15685
      if (data_phy[11:8] !== data_wb[11:8])
15686
      begin
15687
        `TIME;
15688 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (2 bytes) of RX packet!", ((i/4)+1));
15689 169 mohor
        failure = failure + 1;
15690
      end
15691
    end
15692
    else if (plus_dribble_nibble && !successful_dribble_nibble)
15693
    begin
15694
      if (data_phy[11:8] === data_wb[11:8])
15695
      begin
15696
        `TIME;
15697 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (2 bytes) of RX packet!", ((i/4)+1));
15698 169 mohor
        failure = failure + 1;
15699
      end
15700
    end
15701
  end
15702
  else if((len - i) == 1)
15703
  begin
15704
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hC);
15705
    data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i];
15706
    if (plus_dribble_nibble)
15707
      data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0] + i + 1];
15708
    else
15709
      data_phy[23:16] = 0;
15710
    data_phy[15: 8] = 0;
15711
    data_phy[ 7: 0] = 0;
15712
    if (data_phy[31:24] !== data_wb[31:24])
15713
    begin
15714
      `TIME;
15715 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]);
15716 169 mohor
      failure = failure + 1;
15717
    end
15718
    if (plus_dribble_nibble && successful_dribble_nibble)
15719
    begin
15720
      if (data_phy[19:16] !== data_wb[19:16])
15721
      begin
15722
        `TIME;
15723 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (1 byte) of RX packet!", ((i/4)+1));
15724 169 mohor
        failure = failure + 1;
15725
      end
15726
    end
15727
    else if (plus_dribble_nibble && !successful_dribble_nibble)
15728
    begin
15729
      if (data_phy[19:16] === data_wb[19:16])
15730
      begin
15731
        `TIME;
15732 243 tadejm
        $display("*E Wrong dribble nibble in %0d. word (1 byte) of RX packet!", ((i/4)+1));
15733 169 mohor
        failure = failure + 1;
15734
      end
15735
    end
15736
  end
15737
  else if((len - i) == 4)
15738
  begin
15739
    wb_slave.rd_mem(addr_wb + i, data_wb, 4'hF);
15740
    data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i];
15741
    data_phy[23:16] = eth_phy.rx_mem[addr_phy[21:0] + i + 1];
15742
    data_phy[15: 8] = eth_phy.rx_mem[addr_phy[21:0] + i + 2];
15743
    data_phy[ 7: 0] = eth_phy.rx_mem[addr_phy[21:0] + i + 3];
15744
    if (data_phy[31:0] !== data_wb[31:0])
15745
    begin
15746
      `TIME;
15747 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]);
15748 169 mohor
      failure = failure + 1;
15749
    end
15750
    if (plus_dribble_nibble)
15751
    begin
15752
      wb_slave.rd_mem(addr_wb + i + 4, data_wb, 4'h8);
15753
      data_phy[31:24] = eth_phy.rx_mem[addr_phy[21:0] + i + 4];
15754
      if (successful_dribble_nibble)
15755
      begin
15756
        if (data_phy[27:24] !== data_wb[27:24])
15757
        begin
15758
          `TIME;
15759 243 tadejm
          $display("*E Wrong dribble nibble in %0d. word (0 bytes) of RX packet!", ((i/4)+2));
15760 169 mohor
          failure = failure + 1;
15761
        end
15762
      end
15763
      else
15764
      begin
15765
        if (data_phy[27:24] === data_wb[27:24])
15766
        begin
15767
          `TIME;
15768 243 tadejm
          $display("*E Wrong dribble nibble in %0d. word (0 bytes) of RX packet!", ((i/4)+2));
15769 169 mohor
          failure = failure + 1;
15770
        end
15771
      end
15772
    end
15773
  end
15774
  else
15775
    $display("(%0t)(%m) ERROR", $time);
15776
  delta_t = !delta_t;
15777
end
15778
endtask // check_rx_packet
15779 116 mohor
 
15780 169 mohor
//////////////////////////////////////////////////////////////
15781
// Ethernet CRC Basic tasks
15782
//////////////////////////////////////////////////////////////
15783
 
15784
task append_tx_crc;
15785
  input  [31:0] txpnt_wb;  // source
15786
  input  [15:0] len; // length in bytes without CRC
15787
  input         negated_crc; // if appended CRC is correct or not
15788
  reg    [31:0] crc;
15789
  reg    [31:0] addr_wb;
15790
  reg           delta_t;
15791
begin
15792 243 tadejm
  addr_wb = txpnt_wb + {16'h0, len};
15793 169 mohor
  delta_t = 0;
15794
  // calculate CRC from prepared packet
15795
  paralel_crc_mac(txpnt_wb, {16'h0, len}, 1'b0, crc);
15796
  if (negated_crc)
15797
    crc = ~crc;
15798
  delta_t = !delta_t;
15799
 
15800
  // Write might not be word allign.
15801
  if (addr_wb[1:0] == 1)
15802
  begin
15803
    wb_slave.wr_mem(addr_wb - 1, {8'h0, crc[7:0], crc[15:8], crc[23:16]}, 4'h7);
15804
    wb_slave.wr_mem(addr_wb + 3, {crc[31:24], 24'h0}, 4'h8);
15805 116 mohor
  end
15806 169 mohor
  else if (addr_wb[1:0] == 2)
15807
  begin
15808
    wb_slave.wr_mem(addr_wb - 2, {16'h0, crc[7:0], crc[15:8]}, 4'h3);
15809
    wb_slave.wr_mem(addr_wb + 2, {crc[23:16], crc[31:24], 16'h0}, 4'hC);
15810
  end
15811
  else if (addr_wb[1:0] == 3)
15812
  begin
15813
    wb_slave.wr_mem(addr_wb - 3, {24'h0, crc[7:0]}, 4'h1);
15814
    wb_slave.wr_mem(addr_wb + 1, {crc[15:8], crc[23:16], crc[31:24], 8'h0}, 4'hE);
15815
  end
15816
  else
15817
  begin
15818
    wb_slave.wr_mem(addr_wb, {crc[7:0], crc[15:8], crc[23:16], crc[31:24]}, 4'hF);
15819
  end
15820
  delta_t = !delta_t;
15821
end
15822
endtask // append_tx_crc
15823 116 mohor
 
15824 169 mohor
task check_tx_crc; // used to check crc added to TX packets by MAC
15825
  input  [31:0] txpnt_phy; // destination
15826
  input  [15:0] len; // length in bytes without CRC
15827
  input         negated_crc; // if appended CRC is correct or not
15828
  output [31:0] failure;
15829
  reg    [31:0] failure;
15830
  reg    [31:0] crc_calc;
15831
  reg    [31:0] crc;
15832
  reg    [31:0] addr_phy;
15833
  reg           delta_t;
15834
begin
15835
  addr_phy = txpnt_phy;
15836
  failure = 0;
15837
  // calculate CRC from sent packet
15838
//  serial_crc_phy_tx(addr_phy, {16'h0, len}, 1'b0, crc_calc);
15839
//#10;
15840
  paralel_crc_phy_tx(addr_phy, {16'h0, len}, 1'b0, crc_calc);
15841 209 tadejm
  #1;
15842 169 mohor
  addr_phy = addr_phy + len;
15843
  // Read CRC - BIG endian
15844
  crc[31:24] = eth_phy.tx_mem[addr_phy[21:0]];
15845
  crc[23:16] = eth_phy.tx_mem[addr_phy[21:0] + 1];
15846
  crc[15: 8] = eth_phy.tx_mem[addr_phy[21:0] + 2];
15847
  crc[ 7: 0] = eth_phy.tx_mem[addr_phy[21:0] + 3];
15848
 
15849
  delta_t = !delta_t;
15850
  if (negated_crc)
15851
  begin
15852
    if ((~crc_calc) !== crc)
15853
    begin
15854
      `TIME;
15855
      $display("*E Negated CRC was not successfuly transmitted!");
15856
      failure = failure + 1;
15857
    end
15858
  end
15859
  else
15860
  begin
15861
    if (crc_calc !== crc)
15862
    begin
15863
      `TIME;
15864 209 tadejm
      $display("*E Transmitted CRC was not correct; crc_calc: %0h, crc_mem: %0h", crc_calc, crc);
15865 169 mohor
      failure = failure + 1;
15866
    end
15867
  end
15868
  delta_t = !delta_t;
15869
end
15870
endtask // check_tx_crc
15871
 
15872
task append_rx_crc;
15873
  input  [31:0] rxpnt_phy; // source
15874
  input  [15:0] len; // length in bytes without CRC
15875
  input         plus_dribble_nibble; // if length is longer for one nibble
15876
  input         negated_crc; // if appended CRC is correct or not
15877
  reg    [31:0] crc;
15878
  reg    [7:0]  tmp;
15879
  reg    [31:0] addr_phy;
15880
  reg           delta_t;
15881
begin
15882
  addr_phy = rxpnt_phy + len;
15883
  delta_t = 0;
15884
  // calculate CRC from prepared packet
15885
  paralel_crc_phy_rx(rxpnt_phy, {16'h0, len}, plus_dribble_nibble, crc);
15886
  if (negated_crc)
15887
    crc = ~crc;
15888
  delta_t = !delta_t;
15889
 
15890
  if (plus_dribble_nibble)
15891
  begin
15892
    tmp = eth_phy.rx_mem[addr_phy];
15893 209 tadejm
    eth_phy.rx_mem[addr_phy]     = {crc[27:24], tmp[3:0]};
15894
    eth_phy.rx_mem[addr_phy + 1] = {crc[19:16], crc[31:28]};
15895
    eth_phy.rx_mem[addr_phy + 2] = {crc[11:8], crc[23:20]};
15896
    eth_phy.rx_mem[addr_phy + 3] = {crc[3:0], crc[15:12]};
15897
    eth_phy.rx_mem[addr_phy + 4] = {4'h0, crc[7:4]};
15898 169 mohor
  end
15899
  else
15900
  begin
15901 209 tadejm
    eth_phy.rx_mem[addr_phy]     = crc[31:24];
15902
    eth_phy.rx_mem[addr_phy + 1] = crc[23:16];
15903
    eth_phy.rx_mem[addr_phy + 2] = crc[15:8];
15904
    eth_phy.rx_mem[addr_phy + 3] = crc[7:0];
15905 169 mohor
  end
15906
end
15907
endtask // append_rx_crc
15908
 
15909
// paralel CRC checking for PHY TX
15910
task paralel_crc_phy_tx;
15911
  input  [31:0] start_addr; // start address
15912
  input  [31:0] len; // length of frame in Bytes without CRC length
15913
  input         plus_dribble_nibble; // if length is longer for one nibble
15914
  output [31:0] crc_out;
15915
  reg    [21:0] addr_cnt; // only 22 address lines
15916
  integer       word_cnt;
15917
  integer       nibble_cnt;
15918
  reg    [31:0] load_reg;
15919
  reg           delta_t;
15920
  reg    [31:0] crc_next;
15921
  reg    [31:0] crc;
15922
  reg           crc_error;
15923
  reg     [3:0] data_in;
15924
  integer       i;
15925
begin
15926
  #1 addr_cnt = start_addr[21:0];
15927
  word_cnt = 24; // 27; // start of the frame - nibble granularity (MSbit first)
15928
  crc = 32'hFFFF_FFFF; // INITIAL value
15929
  delta_t = 0;
15930
  // length must include 4 bytes of ZEROs, to generate CRC
15931
  // get number of nibbles from Byte length (2^1 = 2)
15932
  if (plus_dribble_nibble)
15933
    nibble_cnt = ((len + 4) << 1) + 1'b1; // one nibble longer
15934
  else
15935
    nibble_cnt = ((len + 4) << 1);
15936
  // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
15937
  load_reg[31:24] = eth_phy.tx_mem[addr_cnt];
15938
  addr_cnt = addr_cnt + 1;
15939
  load_reg[23:16] = eth_phy.tx_mem[addr_cnt];
15940
  addr_cnt = addr_cnt + 1;
15941
  load_reg[15: 8] = eth_phy.tx_mem[addr_cnt];
15942
  addr_cnt = addr_cnt + 1;
15943
  load_reg[ 7: 0] = eth_phy.tx_mem[addr_cnt];
15944
  addr_cnt = addr_cnt + 1;
15945
  while (nibble_cnt > 0)
15946
  begin
15947
    // wait for delta time
15948
    delta_t = !delta_t;
15949
    // shift data in
15950
 
15951
    if(nibble_cnt <= 8) // for additional 8 nibbles shift ZEROs in!
15952
      data_in[3:0] = 4'h0;
15953
    else
15954
 
15955
      data_in[3:0] = {load_reg[word_cnt], load_reg[word_cnt+1], load_reg[word_cnt+2], load_reg[word_cnt+3]};
15956
    crc_next[0]  = (data_in[0] ^ crc[28]);
15957
    crc_next[1]  = (data_in[1] ^ data_in[0] ^ crc[28]    ^ crc[29]);
15958
    crc_next[2]  = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]);
15959
    crc_next[3]  = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]);
15960
    crc_next[4]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[0];
15961
    crc_next[5]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[1];
15962
    crc_next[6]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[ 2];
15963
    crc_next[7]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[3];
15964
    crc_next[8]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[4];
15965
    crc_next[9]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[5];
15966
    crc_next[10] = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[6];
15967
    crc_next[11] = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[7];
15968
    crc_next[12] = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]) ^ crc[8];
15969
    crc_next[13] = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]) ^ crc[9];
15970
    crc_next[14] = (data_in[3] ^ data_in[2] ^ crc[30]    ^ crc[31]) ^ crc[10];
15971
    crc_next[15] = (data_in[3] ^ crc[31])   ^ crc[11];
15972
    crc_next[16] = (data_in[0] ^ crc[28])   ^ crc[12];
15973
    crc_next[17] = (data_in[1] ^ crc[29])   ^ crc[13];
15974
    crc_next[18] = (data_in[2] ^ crc[30])   ^ crc[14];
15975
    crc_next[19] = (data_in[3] ^ crc[31])   ^ crc[15];
15976
    crc_next[20] =  crc[16];
15977
    crc_next[21] =  crc[17];
15978
    crc_next[22] = (data_in[0] ^ crc[28])   ^ crc[18];
15979
    crc_next[23] = (data_in[1] ^ data_in[0] ^ crc[29]    ^ crc[28]) ^ crc[19];
15980
    crc_next[24] = (data_in[2] ^ data_in[1] ^ crc[30]    ^ crc[29]) ^ crc[20];
15981
    crc_next[25] = (data_in[3] ^ data_in[2] ^ crc[31]    ^ crc[30]) ^ crc[21];
15982
    crc_next[26] = (data_in[3] ^ data_in[0] ^ crc[31]    ^ crc[28]) ^ crc[22];
15983
    crc_next[27] = (data_in[1] ^ crc[29])   ^ crc[23];
15984
    crc_next[28] = (data_in[2] ^ crc[30])   ^ crc[24];
15985
    crc_next[29] = (data_in[3] ^ crc[31])   ^ crc[25];
15986
    crc_next[30] =  crc[26];
15987
    crc_next[31] =  crc[27];
15988
 
15989
    crc = crc_next;
15990
    crc_error = crc[31:0] != 32'hc704dd7b;  // CRC not equal to magic number
15991
    case (nibble_cnt)
15992
    9: crc_out = {!crc[24], !crc[25], !crc[26], !crc[27], !crc[28], !crc[29], !crc[30], !crc[31],
15993
                  !crc[16], !crc[17], !crc[18], !crc[19], !crc[20], !crc[21], !crc[22], !crc[23],
15994
                  !crc[ 8], !crc[ 9], !crc[10], !crc[11], !crc[12], !crc[13], !crc[14], !crc[15],
15995
                  !crc[ 0], !crc[ 1], !crc[ 2], !crc[ 3], !crc[ 4], !crc[ 5], !crc[ 6], !crc[ 7]};
15996
    default: crc_out = crc_out;
15997
    endcase
15998
    // wait for delta time
15999
    delta_t = !delta_t;
16000
    // increment address and load new data
16001
    if ((word_cnt+3) == 7)//4)
16002
    begin
16003
      // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16004
      load_reg[31:24] = eth_phy.tx_mem[addr_cnt];
16005
      addr_cnt = addr_cnt + 1;
16006
      load_reg[23:16] = eth_phy.tx_mem[addr_cnt];
16007
      addr_cnt = addr_cnt + 1;
16008
      load_reg[15: 8] = eth_phy.tx_mem[addr_cnt];
16009
      addr_cnt = addr_cnt + 1;
16010
      load_reg[ 7: 0] = eth_phy.tx_mem[addr_cnt];
16011
      addr_cnt = addr_cnt + 1;
16012
    end
16013
    // set new load bit position
16014
    if((word_cnt+3) == 31)
16015
      word_cnt = 16;
16016
    else if ((word_cnt+3) == 23)
16017
      word_cnt = 8;
16018
    else if ((word_cnt+3) == 15)
16019
      word_cnt = 0;
16020
    else if ((word_cnt+3) == 7)
16021
      word_cnt = 24;
16022
    else
16023
      word_cnt = word_cnt + 4;// - 4;
16024
    // decrement nibble counter
16025
    nibble_cnt = nibble_cnt - 1;
16026
    // wait for delta time
16027
    delta_t = !delta_t;
16028
  end // while
16029
  #1;
16030
end
16031
endtask // paralel_crc_phy_tx
16032
 
16033
// paralel CRC calculating for PHY RX
16034
task paralel_crc_phy_rx;
16035
  input  [31:0] start_addr; // start address
16036
  input  [31:0] len; // length of frame in Bytes without CRC length
16037
  input         plus_dribble_nibble; // if length is longer for one nibble
16038 209 tadejm
  output [31:0] crc_out;
16039 169 mohor
  reg    [21:0] addr_cnt; // only 22 address lines
16040
  integer       word_cnt;
16041 209 tadejm
  integer       nibble_cnt;
16042 169 mohor
  reg    [31:0] load_reg;
16043
  reg           delta_t;
16044 209 tadejm
  reg    [31:0] crc_next;
16045
  reg    [31:0] crc;
16046
  reg           crc_error;
16047
  reg     [3:0] data_in;
16048
  integer       i;
16049 169 mohor
begin
16050
  #1 addr_cnt = start_addr[21:0];
16051 209 tadejm
  word_cnt = 24; // 27; // start of the frame - nibble granularity (MSbit first)
16052
  crc = 32'hFFFF_FFFF; // INITIAL value
16053 169 mohor
  delta_t = 0;
16054
  // length must include 4 bytes of ZEROs, to generate CRC
16055 209 tadejm
  // get number of nibbles from Byte length (2^1 = 2)
16056 169 mohor
  if (plus_dribble_nibble)
16057 209 tadejm
    nibble_cnt = ((len + 4) << 1) + 1'b1; // one nibble longer
16058 169 mohor
  else
16059 209 tadejm
    nibble_cnt = ((len + 4) << 1);
16060
  // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16061 169 mohor
  load_reg[31:24] = eth_phy.rx_mem[addr_cnt];
16062
  addr_cnt = addr_cnt + 1;
16063
  load_reg[23:16] = eth_phy.rx_mem[addr_cnt];
16064
  addr_cnt = addr_cnt + 1;
16065 209 tadejm
  load_reg[15: 8] = eth_phy.rx_mem[addr_cnt];
16066 169 mohor
  addr_cnt = addr_cnt + 1;
16067 209 tadejm
  load_reg[ 7: 0] = eth_phy.rx_mem[addr_cnt];
16068
  addr_cnt = addr_cnt + 1;
16069
  while (nibble_cnt > 0)
16070 169 mohor
  begin
16071
    // wait for delta time
16072
    delta_t = !delta_t;
16073
    // shift data in
16074 209 tadejm
 
16075
    if(nibble_cnt <= 8) // for additional 8 nibbles shift ZEROs in!
16076
      data_in[3:0] = 4'h0;
16077 169 mohor
    else
16078 209 tadejm
 
16079
      data_in[3:0] = {load_reg[word_cnt], load_reg[word_cnt+1], load_reg[word_cnt+2], load_reg[word_cnt+3]};
16080
    crc_next[0]  = (data_in[0] ^ crc[28]);
16081
    crc_next[1]  = (data_in[1] ^ data_in[0] ^ crc[28]    ^ crc[29]);
16082
    crc_next[2]  = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]);
16083
    crc_next[3]  = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]);
16084
    crc_next[4]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[0];
16085
    crc_next[5]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[1];
16086
    crc_next[6]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[ 2];
16087
    crc_next[7]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[3];
16088
    crc_next[8]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[4];
16089
    crc_next[9]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[5];
16090
    crc_next[10] = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[6];
16091
    crc_next[11] = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[7];
16092
    crc_next[12] = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]) ^ crc[8];
16093
    crc_next[13] = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]) ^ crc[9];
16094
    crc_next[14] = (data_in[3] ^ data_in[2] ^ crc[30]    ^ crc[31]) ^ crc[10];
16095
    crc_next[15] = (data_in[3] ^ crc[31])   ^ crc[11];
16096
    crc_next[16] = (data_in[0] ^ crc[28])   ^ crc[12];
16097
    crc_next[17] = (data_in[1] ^ crc[29])   ^ crc[13];
16098
    crc_next[18] = (data_in[2] ^ crc[30])   ^ crc[14];
16099
    crc_next[19] = (data_in[3] ^ crc[31])   ^ crc[15];
16100
    crc_next[20] =  crc[16];
16101
    crc_next[21] =  crc[17];
16102
    crc_next[22] = (data_in[0] ^ crc[28])   ^ crc[18];
16103
    crc_next[23] = (data_in[1] ^ data_in[0] ^ crc[29]    ^ crc[28]) ^ crc[19];
16104
    crc_next[24] = (data_in[2] ^ data_in[1] ^ crc[30]    ^ crc[29]) ^ crc[20];
16105
    crc_next[25] = (data_in[3] ^ data_in[2] ^ crc[31]    ^ crc[30]) ^ crc[21];
16106
    crc_next[26] = (data_in[3] ^ data_in[0] ^ crc[31]    ^ crc[28]) ^ crc[22];
16107
    crc_next[27] = (data_in[1] ^ crc[29])   ^ crc[23];
16108
    crc_next[28] = (data_in[2] ^ crc[30])   ^ crc[24];
16109
    crc_next[29] = (data_in[3] ^ crc[31])   ^ crc[25];
16110
    crc_next[30] =  crc[26];
16111
    crc_next[31] =  crc[27];
16112
 
16113
    crc = crc_next;
16114
    crc_error = crc[31:0] != 32'hc704dd7b;  // CRC not equal to magic number
16115
    case (nibble_cnt)
16116
    9: crc_out = {!crc[24], !crc[25], !crc[26], !crc[27], !crc[28], !crc[29], !crc[30], !crc[31],
16117
                  !crc[16], !crc[17], !crc[18], !crc[19], !crc[20], !crc[21], !crc[22], !crc[23],
16118
                  !crc[ 8], !crc[ 9], !crc[10], !crc[11], !crc[12], !crc[13], !crc[14], !crc[15],
16119
                  !crc[ 0], !crc[ 1], !crc[ 2], !crc[ 3], !crc[ 4], !crc[ 5], !crc[ 6], !crc[ 7]};
16120
    default: crc_out = crc_out;
16121
    endcase
16122 169 mohor
    // wait for delta time
16123
    delta_t = !delta_t;
16124
    // increment address and load new data
16125 209 tadejm
    if ((word_cnt+3) == 7)//4)
16126 169 mohor
    begin
16127 209 tadejm
      // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16128 169 mohor
      load_reg[31:24] = eth_phy.rx_mem[addr_cnt];
16129
      addr_cnt = addr_cnt + 1;
16130
      load_reg[23:16] = eth_phy.rx_mem[addr_cnt];
16131
      addr_cnt = addr_cnt + 1;
16132 209 tadejm
      load_reg[15: 8] = eth_phy.rx_mem[addr_cnt];
16133 169 mohor
      addr_cnt = addr_cnt + 1;
16134 209 tadejm
      load_reg[ 7: 0] = eth_phy.rx_mem[addr_cnt];
16135
      addr_cnt = addr_cnt + 1;
16136 169 mohor
    end
16137
    // set new load bit position
16138 209 tadejm
    if((word_cnt+3) == 31)
16139 169 mohor
      word_cnt = 16;
16140 209 tadejm
    else if ((word_cnt+3) == 23)
16141 169 mohor
      word_cnt = 8;
16142 209 tadejm
    else if ((word_cnt+3) == 15)
16143 169 mohor
      word_cnt = 0;
16144 209 tadejm
    else if ((word_cnt+3) == 7)
16145 169 mohor
      word_cnt = 24;
16146
    else
16147 209 tadejm
      word_cnt = word_cnt + 4;// - 4;
16148
    // decrement nibble counter
16149
    nibble_cnt = nibble_cnt - 1;
16150 169 mohor
    // wait for delta time
16151
    delta_t = !delta_t;
16152
  end // while
16153
  #1;
16154
end
16155
endtask // paralel_crc_phy_rx
16156
 
16157
// paralel CRC checking for MAC
16158
task paralel_crc_mac;
16159
  input  [31:0] start_addr; // start address
16160
  input  [31:0] len; // length of frame in Bytes without CRC length
16161
  input         plus_dribble_nibble; // if length is longer for one nibble
16162 209 tadejm
  output [31:0] crc_out;
16163
 
16164
  reg    [21:0] addr_cnt; // only 22 address lines
16165 169 mohor
  integer       word_cnt;
16166 209 tadejm
  integer       nibble_cnt;
16167 169 mohor
  reg    [31:0] load_reg;
16168
  reg           delta_t;
16169 209 tadejm
  reg    [31:0] crc_next;
16170
  reg    [31:0] crc;
16171
  reg           crc_error;
16172
  reg     [3:0] data_in;
16173
  integer       i;
16174 169 mohor
begin
16175
  #1 addr_cnt = start_addr[19:0];
16176
  // set starting point depending with which byte frame starts (e.g. if addr_cnt[1:0] == 0, then
16177
  //   MSB of the packet must be written to the LSB of Big ENDIAN Word [31:24])
16178
  if (addr_cnt[1:0] == 2'h1)
16179
    word_cnt = 16; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16180
  else if (addr_cnt[1:0] == 2'h2)
16181
    word_cnt = 8; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16182
  else if (addr_cnt[1:0] == 2'h3)
16183
    word_cnt = 0; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16184
  else
16185
    word_cnt = 24; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16186 209 tadejm
  crc = 32'hFFFF_FFFF; // INITIAL value
16187 169 mohor
  delta_t = 0;
16188
  // length must include 4 bytes of ZEROs, to generate CRC
16189 209 tadejm
  // get number of nibbles from Byte length (2^1 = 2)
16190 169 mohor
  if (plus_dribble_nibble)
16191 209 tadejm
    nibble_cnt = ((len + 4) << 1) + 1'b1; // one nibble longer
16192 169 mohor
  else
16193 209 tadejm
    nibble_cnt = ((len + 4) << 1);
16194 169 mohor
  load_reg = wb_slave.wb_memory[{12'h0, addr_cnt}];
16195 209 tadejm
  addr_cnt = addr_cnt + 4;
16196
  while (nibble_cnt > 0)
16197 169 mohor
  begin
16198
    // wait for delta time
16199
    delta_t = !delta_t;
16200
    // shift data in
16201 209 tadejm
 
16202
    if(nibble_cnt <= 8) // for additional 8 nibbles shift ZEROs in!
16203
      data_in[3:0] = 4'h0;
16204 169 mohor
    else
16205 209 tadejm
 
16206
      data_in[3:0] = {load_reg[word_cnt], load_reg[word_cnt+1], load_reg[word_cnt+2], load_reg[word_cnt+3]};
16207
    crc_next[0]  = (data_in[0] ^ crc[28]);
16208
    crc_next[1]  = (data_in[1] ^ data_in[0] ^ crc[28]    ^ crc[29]);
16209
    crc_next[2]  = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]);
16210
    crc_next[3]  = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]);
16211
    crc_next[4]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[0];
16212
    crc_next[5]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[1];
16213
    crc_next[6]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[ 2];
16214
    crc_next[7]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[3];
16215
    crc_next[8]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[4];
16216
    crc_next[9]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[5];
16217
    crc_next[10] = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[6];
16218
    crc_next[11] = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[7];
16219
    crc_next[12] = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]) ^ crc[8];
16220
    crc_next[13] = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]) ^ crc[9];
16221
    crc_next[14] = (data_in[3] ^ data_in[2] ^ crc[30]    ^ crc[31]) ^ crc[10];
16222
    crc_next[15] = (data_in[3] ^ crc[31])   ^ crc[11];
16223
    crc_next[16] = (data_in[0] ^ crc[28])   ^ crc[12];
16224
    crc_next[17] = (data_in[1] ^ crc[29])   ^ crc[13];
16225
    crc_next[18] = (data_in[2] ^ crc[30])   ^ crc[14];
16226
    crc_next[19] = (data_in[3] ^ crc[31])   ^ crc[15];
16227
    crc_next[20] =  crc[16];
16228
    crc_next[21] =  crc[17];
16229
    crc_next[22] = (data_in[0] ^ crc[28])   ^ crc[18];
16230
    crc_next[23] = (data_in[1] ^ data_in[0] ^ crc[29]    ^ crc[28]) ^ crc[19];
16231
    crc_next[24] = (data_in[2] ^ data_in[1] ^ crc[30]    ^ crc[29]) ^ crc[20];
16232
    crc_next[25] = (data_in[3] ^ data_in[2] ^ crc[31]    ^ crc[30]) ^ crc[21];
16233
    crc_next[26] = (data_in[3] ^ data_in[0] ^ crc[31]    ^ crc[28]) ^ crc[22];
16234
    crc_next[27] = (data_in[1] ^ crc[29])   ^ crc[23];
16235
    crc_next[28] = (data_in[2] ^ crc[30])   ^ crc[24];
16236
    crc_next[29] = (data_in[3] ^ crc[31])   ^ crc[25];
16237
    crc_next[30] =  crc[26];
16238
    crc_next[31] =  crc[27];
16239
 
16240
    crc = crc_next;
16241
    crc_error = crc[31:0] != 32'hc704dd7b;  // CRC not equal to magic number
16242
    case (nibble_cnt)
16243
    9: crc_out = {!crc[24], !crc[25], !crc[26], !crc[27], !crc[28], !crc[29], !crc[30], !crc[31],
16244
                  !crc[16], !crc[17], !crc[18], !crc[19], !crc[20], !crc[21], !crc[22], !crc[23],
16245
                  !crc[ 8], !crc[ 9], !crc[10], !crc[11], !crc[12], !crc[13], !crc[14], !crc[15],
16246
                  !crc[ 0], !crc[ 1], !crc[ 2], !crc[ 3], !crc[ 4], !crc[ 5], !crc[ 6], !crc[ 7]};
16247
    default: crc_out = crc_out;
16248
    endcase
16249 169 mohor
    // wait for delta time
16250
    delta_t = !delta_t;
16251 209 tadejm
    // increment address and load new data
16252
    if ((word_cnt+3) == 7)//4)
16253 169 mohor
    begin
16254 209 tadejm
      // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16255
      load_reg = wb_slave.wb_memory[{12'h0, addr_cnt}];
16256 169 mohor
      addr_cnt = addr_cnt + 4;
16257
    end
16258 209 tadejm
    // set new load bit position
16259
    if((word_cnt+3) == 31)
16260 169 mohor
      word_cnt = 16;
16261 209 tadejm
    else if ((word_cnt+3) == 23)
16262 169 mohor
      word_cnt = 8;
16263 209 tadejm
    else if ((word_cnt+3) == 15)
16264 169 mohor
      word_cnt = 0;
16265 209 tadejm
    else if ((word_cnt+3) == 7)
16266 169 mohor
      word_cnt = 24;
16267
    else
16268 209 tadejm
      word_cnt = word_cnt + 4;// - 4;
16269
    // decrement nibble counter
16270
    nibble_cnt = nibble_cnt - 1;
16271 169 mohor
    // wait for delta time
16272
    delta_t = !delta_t;
16273
  end // while
16274
  #1;
16275
end
16276
endtask // paralel_crc_mac
16277
 
16278
// serial CRC checking for PHY TX
16279
task serial_crc_phy_tx;
16280
  input  [31:0] start_addr; // start address
16281
  input  [31:0] len; // length of frame in Bytes without CRC length
16282
  input         plus_dribble_nibble; // if length is longer for one nibble
16283
  output [31:0] crc;
16284
  reg    [21:0] addr_cnt; // only 22 address lines
16285
  integer       word_cnt;
16286
  integer       bit_cnt;
16287
  reg    [31:0] load_reg;
16288
  reg    [31:0] crc_shift_reg;
16289
  reg    [31:0] crc_store_reg;
16290
  reg           delta_t;
16291
begin
16292
  #1 addr_cnt = start_addr[21:0];
16293
  word_cnt = 24; // 27; // start of the frame - nibble granularity (MSbit first)
16294
  crc_store_reg = 32'hFFFF_FFFF; // INITIAL value
16295
  delta_t = 0;
16296
  // length must include 4 bytes of ZEROs, to generate CRC
16297
  // get number of bits from Byte length (2^3 = 8)
16298
  if (plus_dribble_nibble)
16299
    bit_cnt = ((len + 4) << 3) + 3'h4; // one nibble longer
16300
  else
16301
    bit_cnt = ((len + 4) << 3);
16302
  // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16303
  load_reg[31:24] = eth_phy.tx_mem[addr_cnt];
16304
  addr_cnt = addr_cnt + 1;
16305
  load_reg[23:16] = eth_phy.tx_mem[addr_cnt];
16306
  addr_cnt = addr_cnt + 1;
16307
  load_reg[15: 8] = eth_phy.tx_mem[addr_cnt];
16308
  addr_cnt = addr_cnt + 1;
16309
  load_reg[ 7: 0] = eth_phy.tx_mem[addr_cnt];
16310
  addr_cnt = addr_cnt + 1;
16311
#1;
16312
  while (bit_cnt > 0)
16313
  begin
16314
    // wait for delta time
16315
    delta_t = !delta_t;
16316
#1;
16317
    // shift data in
16318
 
16319
    if(bit_cnt <= 32) // for additional 32 bits shift ZEROs in!
16320
     crc_shift_reg[0] = 1'b0               ^ crc_store_reg[31];
16321
    else
16322
 
16323
     crc_shift_reg[0] = load_reg[word_cnt] ^ crc_store_reg[31];
16324
    crc_shift_reg[1]  = crc_store_reg[0]   ^ crc_store_reg[31];
16325
    crc_shift_reg[2]  = crc_store_reg[1]   ^ crc_store_reg[31];
16326
    crc_shift_reg[3]  = crc_store_reg[2];
16327
    crc_shift_reg[4]  = crc_store_reg[3]   ^ crc_store_reg[31];
16328
    crc_shift_reg[5]  = crc_store_reg[4]   ^ crc_store_reg[31];
16329
    crc_shift_reg[6]  = crc_store_reg[5];
16330
    crc_shift_reg[7]  = crc_store_reg[6]   ^ crc_store_reg[31];
16331
    crc_shift_reg[8]  = crc_store_reg[7]   ^ crc_store_reg[31];
16332
    crc_shift_reg[9]  = crc_store_reg[8];
16333
    crc_shift_reg[10] = crc_store_reg[9]   ^ crc_store_reg[31];
16334
    crc_shift_reg[11] = crc_store_reg[10]  ^ crc_store_reg[31];
16335
    crc_shift_reg[12] = crc_store_reg[11]  ^ crc_store_reg[31];
16336
    crc_shift_reg[13] = crc_store_reg[12];
16337
    crc_shift_reg[14] = crc_store_reg[13];
16338
    crc_shift_reg[15] = crc_store_reg[14];
16339
    crc_shift_reg[16] = crc_store_reg[15]  ^ crc_store_reg[31];
16340
    crc_shift_reg[17] = crc_store_reg[16];
16341
    crc_shift_reg[18] = crc_store_reg[17];
16342
    crc_shift_reg[19] = crc_store_reg[18];
16343
    crc_shift_reg[20] = crc_store_reg[19];
16344
    crc_shift_reg[21] = crc_store_reg[20];
16345
    crc_shift_reg[22] = crc_store_reg[21]  ^ crc_store_reg[31];
16346
    crc_shift_reg[23] = crc_store_reg[22]  ^ crc_store_reg[31];
16347
    crc_shift_reg[24] = crc_store_reg[23];
16348
    crc_shift_reg[25] = crc_store_reg[24];
16349
    crc_shift_reg[26] = crc_store_reg[25]  ^ crc_store_reg[31];
16350
    crc_shift_reg[27] = crc_store_reg[26];
16351
    crc_shift_reg[28] = crc_store_reg[27];
16352
    crc_shift_reg[29] = crc_store_reg[28];
16353
    crc_shift_reg[30] = crc_store_reg[29];
16354
    crc_shift_reg[31] = crc_store_reg[30];
16355
    // wait for delta time
16356
    delta_t = !delta_t;
16357
 
16358
    // store previous data
16359
    crc_store_reg = crc_shift_reg;
16360
 
16361
    // put CRC out
16362
    case (bit_cnt)
16363
    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:
16364
    begin
16365
      crc = crc_store_reg;
16366
      crc = {!crc[24], !crc[25], !crc[26], !crc[27], !crc[28], !crc[29], !crc[30], !crc[31],
16367
             !crc[16], !crc[17], !crc[18], !crc[19], !crc[20], !crc[21], !crc[22], !crc[23],
16368
             !crc[ 8], !crc[ 9], !crc[10], !crc[11], !crc[12], !crc[13], !crc[14], !crc[15],
16369
             !crc[ 0], !crc[ 1], !crc[ 2], !crc[ 3], !crc[ 4], !crc[ 5], !crc[ 6], !crc[ 7]};
16370
    end
16371
    default: crc = crc;
16372
    endcase
16373
 
16374
    // increment address and load new data
16375
#1;
16376
    if (word_cnt == 7)//4)
16377
    begin
16378
      // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
16379
      load_reg[31:24] = eth_phy.tx_mem[addr_cnt];
16380
//      load_reg[31:24] = {load_reg[28], load_reg[29], load_reg[30], load_reg[31], 
16381
//                         load_reg[24], load_reg[25], load_reg[26], load_reg[27]};
16382
      addr_cnt = addr_cnt + 1;
16383
      load_reg[23:16] = eth_phy.tx_mem[addr_cnt];
16384
//      load_reg[23:16] = {load_reg[20], load_reg[21], load_reg[22], load_reg[23], 
16385
//                         load_reg[16], load_reg[17], load_reg[18], load_reg[19]};
16386
      addr_cnt = addr_cnt + 1;
16387
      load_reg[15: 8] = eth_phy.tx_mem[addr_cnt];
16388
//      load_reg[15: 8] = {load_reg[12], load_reg[13], load_reg[14], load_reg[15], 
16389
//                         load_reg[ 8], load_reg[ 9], load_reg[10], load_reg[11]};
16390
      addr_cnt = addr_cnt + 1;
16391
      load_reg[ 7: 0] = eth_phy.tx_mem[addr_cnt];
16392
//      load_reg[ 7: 0] = {load_reg[ 4], load_reg[ 5], load_reg[ 6], load_reg[ 7], 
16393
//                         load_reg[ 0], load_reg[ 1], load_reg[ 2], load_reg[ 3]};
16394
      addr_cnt = addr_cnt + 1;
16395
    end
16396
#1;
16397
    // set new load bit position
16398
    if(word_cnt == 31)
16399
      word_cnt = 16;
16400
    else if (word_cnt == 23)
16401
      word_cnt = 8;
16402
    else if (word_cnt == 15)
16403
      word_cnt = 0;
16404
    else if (word_cnt == 7)
16405
      word_cnt = 24;
16406
 
16407
//   if(word_cnt == 24)
16408
//     word_cnt = 31;
16409
//   else if (word_cnt == 28)
16410
//     word_cnt = 19;
16411
//   else if (word_cnt == 16)
16412
//     word_cnt = 23;
16413
//   else if (word_cnt == 20)
16414
//     word_cnt = 11;
16415
//   else if(word_cnt == 8)
16416
//     word_cnt = 15;
16417
//   else if (word_cnt == 12)
16418
//     word_cnt = 3;
16419
//   else if (word_cnt == 0)
16420
//     word_cnt = 7;
16421
//   else if (word_cnt == 4)
16422
//     word_cnt = 27;
16423
    else
16424
      word_cnt = word_cnt + 1;// - 1;
16425
#1;
16426
    // decrement bit counter
16427
    bit_cnt = bit_cnt - 1;
16428
#1;
16429
    // wait for delta time
16430
    delta_t = !delta_t;
16431
  end // while
16432
 
16433
  #1;
16434
end
16435
endtask // serial_crc_phy_tx
16436
 
16437
// serial CRC calculating for PHY RX
16438
task serial_crc_phy_rx;
16439
  input  [31:0] start_addr; // start address
16440
  input  [31:0] len; // length of frame in Bytes without CRC length
16441
  input         plus_dribble_nibble; // if length is longer for one nibble
16442
  output [31:0] crc;
16443
  reg    [21:0] addr_cnt; // only 22 address lines
16444
  integer       word_cnt;
16445
  integer       bit_cnt;
16446
  reg    [31:0] load_reg;
16447
  reg    [31:0] crc_shift_reg;
16448
  reg    [31:0] crc_store_reg;
16449
  reg           delta_t;
16450
begin
16451
  #1 addr_cnt = start_addr[21:0];
16452
  word_cnt = 24; // start of the frame
16453
  crc_shift_reg = 0;
16454
  delta_t = 0;
16455
  // length must include 4 bytes of ZEROs, to generate CRC
16456
  // get number of bits from Byte length (2^3 = 8)
16457
  if (plus_dribble_nibble)
16458
    bit_cnt = ((len + 4) << 3) + 3'h4; // one nibble longer
16459
  else
16460
    bit_cnt = ((len + 4) << 3);
16461
  load_reg[31:24] = eth_phy.rx_mem[addr_cnt];
16462
  addr_cnt = addr_cnt + 1;
16463
  load_reg[23:16] = eth_phy.rx_mem[addr_cnt];
16464
  addr_cnt = addr_cnt + 1;
16465
  load_reg[15:8]  = eth_phy.rx_mem[addr_cnt];
16466
  addr_cnt = addr_cnt + 1;
16467
  load_reg[7:0]   = eth_phy.rx_mem[addr_cnt];
16468
 
16469
  while (bit_cnt > 0)
16470
  begin
16471
    // wait for delta time
16472
    delta_t = !delta_t;
16473
    // store previous data
16474
    crc_store_reg = crc_shift_reg;
16475
    // shift data in
16476
    if(bit_cnt <= 32) // for additional 32 bits shift ZEROs in!
16477
     crc_shift_reg[0] = 1'b0               ^ crc_store_reg[31];
16478
    else
16479
     crc_shift_reg[0] = load_reg[word_cnt] ^ crc_store_reg[31];
16480
    crc_shift_reg[1]  = crc_store_reg[0]   ^ crc_store_reg[31];
16481
    crc_shift_reg[2]  = crc_store_reg[1]   ^ crc_store_reg[31];
16482
    crc_shift_reg[3]  = crc_store_reg[2];
16483
    crc_shift_reg[4]  = crc_store_reg[3]   ^ crc_store_reg[31];
16484
    crc_shift_reg[5]  = crc_store_reg[4]   ^ crc_store_reg[31];
16485
    crc_shift_reg[6]  = crc_store_reg[5];
16486
    crc_shift_reg[7]  = crc_store_reg[6]   ^ crc_store_reg[31];
16487
    crc_shift_reg[8]  = crc_store_reg[7]   ^ crc_store_reg[31];
16488
    crc_shift_reg[9]  = crc_store_reg[8];
16489
    crc_shift_reg[10] = crc_store_reg[9]   ^ crc_store_reg[31];
16490
    crc_shift_reg[11] = crc_store_reg[10]  ^ crc_store_reg[31];
16491
    crc_shift_reg[12] = crc_store_reg[11]  ^ crc_store_reg[31];
16492
    crc_shift_reg[13] = crc_store_reg[12];
16493
    crc_shift_reg[14] = crc_store_reg[13];
16494
    crc_shift_reg[15] = crc_store_reg[14];
16495
    crc_shift_reg[16] = crc_store_reg[15]  ^ crc_store_reg[31];
16496
    crc_shift_reg[17] = crc_store_reg[16];
16497
    crc_shift_reg[18] = crc_store_reg[17];
16498
    crc_shift_reg[19] = crc_store_reg[18];
16499
    crc_shift_reg[20] = crc_store_reg[19];
16500
    crc_shift_reg[21] = crc_store_reg[20];
16501
    crc_shift_reg[22] = crc_store_reg[21]  ^ crc_store_reg[31];
16502
    crc_shift_reg[23] = crc_store_reg[22]  ^ crc_store_reg[31];
16503
    crc_shift_reg[24] = crc_store_reg[23];
16504
    crc_shift_reg[25] = crc_store_reg[24];
16505
    crc_shift_reg[26] = crc_store_reg[25]  ^ crc_store_reg[31];
16506
    crc_shift_reg[27] = crc_store_reg[26];
16507
    crc_shift_reg[28] = crc_store_reg[27];
16508
    crc_shift_reg[29] = crc_store_reg[28];
16509
    crc_shift_reg[30] = crc_store_reg[29];
16510
    crc_shift_reg[31] = crc_store_reg[30];
16511
    // wait for delta time
16512
    delta_t = !delta_t;
16513
    // increment address and load new data
16514
    if (word_cnt == 7)
16515
    begin
16516
      addr_cnt = addr_cnt + 1;
16517
      load_reg[31:24] = eth_phy.rx_mem[addr_cnt];
16518
      addr_cnt = addr_cnt + 1;
16519
      load_reg[23:16] = eth_phy.rx_mem[addr_cnt];
16520
      addr_cnt = addr_cnt + 1;
16521
      load_reg[15:8]  = eth_phy.rx_mem[addr_cnt];
16522
      addr_cnt = addr_cnt + 1;
16523
      load_reg[7:0]   = eth_phy.rx_mem[addr_cnt];
16524
    end
16525
    // set new load bit position
16526
    if(word_cnt == 31)
16527
      word_cnt = 16;
16528
    else if (word_cnt == 23)
16529
      word_cnt = 8;
16530
    else if (word_cnt == 15)
16531
      word_cnt = 0;
16532
    else if (word_cnt == 7)
16533
      word_cnt = 24;
16534
    else
16535
      word_cnt = word_cnt + 1;
16536
    // decrement bit counter
16537
    bit_cnt = bit_cnt - 1;
16538
    // wait for delta time
16539
    delta_t = !delta_t;
16540
  end // while
16541
 
16542
  // put CRC out
16543
  crc = crc_shift_reg;
16544
  #1;
16545
end
16546
endtask // serial_crc_phy_rx
16547
 
16548
// serial CRC checking for MAC
16549
task serial_crc_mac;
16550
  input  [31:0] start_addr; // start address
16551
  input  [31:0] len; // length of frame in Bytes without CRC length
16552
  input         plus_dribble_nibble; // if length is longer for one nibble
16553
  output [31:0] crc;
16554
  reg    [19:0] addr_cnt; // only 20 address lines
16555
  integer       word_cnt;
16556
  integer       bit_cnt;
16557
  reg    [31:0] load_reg;
16558
  reg    [31:0] crc_shift_reg;
16559
  reg    [31:0] crc_store_reg;
16560
  reg           delta_t;
16561
begin
16562
  #1 addr_cnt = start_addr[19:0];
16563
  // set starting point depending with which byte frame starts (e.g. if addr_cnt[1:0] == 0, then
16564
  //   MSB of the packet must be written to the LSB of Big ENDIAN Word [31:24])
16565
  if (addr_cnt[1:0] == 2'h1)
16566
    word_cnt = 16; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16567
  else if (addr_cnt[1:0] == 2'h2)
16568
    word_cnt = 8; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16569
  else if (addr_cnt[1:0] == 2'h3)
16570
    word_cnt = 0; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16571
  else
16572
    word_cnt = 24; // start of the frame for Big ENDIAN Bytes (Litle ENDIAN bits)
16573
 
16574
  crc_shift_reg = 0;
16575
  delta_t = 0;
16576
  // length must include 4 bytes of ZEROs, to generate CRC
16577
  // get number of bits from Byte length (2^3 = 8)
16578
  if (plus_dribble_nibble)
16579
    bit_cnt = ((len + 4) << 3) + 3'h4; // one nibble longer
16580
  else
16581
    bit_cnt = ((len + 4) << 3);
16582
  load_reg = wb_slave.wb_memory[{12'h0, addr_cnt}];
16583
 
16584
  while (bit_cnt > 0)
16585
  begin
16586
    // wait for delta time
16587
    delta_t = !delta_t;
16588
    // store previous data
16589
    crc_store_reg = crc_shift_reg;
16590
    // shift data in
16591
    if(bit_cnt <= 32) // for additional 32 bits shift ZEROs in!
16592
     crc_shift_reg[0] = 1'b0               ^ crc_store_reg[31];
16593
    else
16594
     crc_shift_reg[0] = load_reg[word_cnt] ^ crc_store_reg[31];
16595
    crc_shift_reg[1]  = crc_store_reg[0]   ^ crc_store_reg[31];
16596
    crc_shift_reg[2]  = crc_store_reg[1]   ^ crc_store_reg[31];
16597
    crc_shift_reg[3]  = crc_store_reg[2];
16598
    crc_shift_reg[4]  = crc_store_reg[3]   ^ crc_store_reg[31];
16599
    crc_shift_reg[5]  = crc_store_reg[4]   ^ crc_store_reg[31];
16600
    crc_shift_reg[6]  = crc_store_reg[5];
16601
    crc_shift_reg[7]  = crc_store_reg[6]   ^ crc_store_reg[31];
16602
    crc_shift_reg[8]  = crc_store_reg[7]   ^ crc_store_reg[31];
16603
    crc_shift_reg[9]  = crc_store_reg[8];
16604
    crc_shift_reg[10] = crc_store_reg[9]   ^ crc_store_reg[31];
16605
    crc_shift_reg[11] = crc_store_reg[10]  ^ crc_store_reg[31];
16606
    crc_shift_reg[12] = crc_store_reg[11]  ^ crc_store_reg[31];
16607
    crc_shift_reg[13] = crc_store_reg[12];
16608
    crc_shift_reg[14] = crc_store_reg[13];
16609
    crc_shift_reg[15] = crc_store_reg[14];
16610
    crc_shift_reg[16] = crc_store_reg[15]  ^ crc_store_reg[31];
16611
    crc_shift_reg[17] = crc_store_reg[16];
16612
    crc_shift_reg[18] = crc_store_reg[17];
16613
    crc_shift_reg[19] = crc_store_reg[18];
16614
    crc_shift_reg[20] = crc_store_reg[19];
16615
    crc_shift_reg[21] = crc_store_reg[20];
16616
    crc_shift_reg[22] = crc_store_reg[21]  ^ crc_store_reg[31];
16617
    crc_shift_reg[23] = crc_store_reg[22]  ^ crc_store_reg[31];
16618
    crc_shift_reg[24] = crc_store_reg[23];
16619
    crc_shift_reg[25] = crc_store_reg[24];
16620
    crc_shift_reg[26] = crc_store_reg[25]  ^ crc_store_reg[31];
16621
    crc_shift_reg[27] = crc_store_reg[26];
16622
    crc_shift_reg[28] = crc_store_reg[27];
16623
    crc_shift_reg[29] = crc_store_reg[28];
16624
    crc_shift_reg[30] = crc_store_reg[29];
16625
    crc_shift_reg[31] = crc_store_reg[30];
16626
    // wait for delta time
16627
    delta_t = !delta_t;
16628
    // increment address and load new data for Big ENDIAN Bytes (Litle ENDIAN bits)
16629
    if (word_cnt == 7)
16630
    begin
16631
      addr_cnt = addr_cnt + 4;
16632
      load_reg = wb_slave.wb_memory[{12'h0, addr_cnt}];
16633
    end
16634
    // set new load bit position for Big ENDIAN Bytes (Litle ENDIAN bits)
16635
    if(word_cnt == 31)
16636
      word_cnt = 16;
16637
    else if (word_cnt == 23)
16638
      word_cnt = 8;
16639
    else if (word_cnt == 15)
16640
      word_cnt = 0;
16641
    else if (word_cnt == 7)
16642
      word_cnt = 24;
16643
    else
16644
      word_cnt = word_cnt + 1;
16645
    // decrement bit counter
16646
    bit_cnt = bit_cnt - 1;
16647
    // wait for delta time
16648
    delta_t = !delta_t;
16649
  end // while
16650
 
16651
  // put CRC out
16652
  crc = crc_shift_reg;
16653
  #1;
16654
end
16655
endtask // serial_crc_mac
16656
 
16657
//////////////////////////////////////////////////////////////
16658
// MIIM Basic tasks
16659
//////////////////////////////////////////////////////////////
16660
 
16661
task reset_mii; //  MII module
16662
  reg [31:0] tmp;
16663
  reg [31:0] tmp_no_rst;
16664
begin
16665
  // read MII mode register first
16666
  wbm_read(`ETH_MIIMODER, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16667
  // set reset bit - write back to MII mode register with RESET bit
16668
  wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_RST | tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16669
  // clear reset bit - write back to MII mode register without RESET bit
16670
  tmp_no_rst = `ETH_MIIMODER_RST;
16671
  tmp_no_rst = ~tmp_no_rst;
16672
  wbm_write(`ETH_MIIMODER, (tmp_no_rst & tmp), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16673
end
16674
endtask // reset_mii
16675
 
16676
task mii_set_clk_div; // set clock divider for MII clock
16677
  input [7:0]  clk_div;
16678
begin
16679
  // MII mode register
16680
  wbm_write(`ETH_MIIMODER, (`ETH_MIIMODER_CLKDIV & clk_div), 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16681
end
16682
endtask // mii_set_clk_div
16683
 
16684
 
16685
task check_mii_busy; // MII - check if BUSY
16686
  reg [31:0] tmp;
16687
begin
16688
  @(posedge wb_clk);
16689
  // MII read status register
16690
  wbm_read(`ETH_MIISTATUS, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16691
  while(tmp[`ETH_MIISTATUS_BUSY] !== 1'b0) //`ETH_MIISTATUS_BUSY
16692
  begin
16693
    @(posedge wb_clk);
16694
    wbm_read(`ETH_MIISTATUS, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16695
  end
16696
end
16697
endtask // check_mii_busy
16698
 
16699
 
16700
task check_mii_scan_valid; // MII - check if SCAN data are valid
16701
  reg [31:0] tmp;
16702
begin
16703
  @(posedge wb_clk);
16704
  // MII read status register
16705
  wbm_read(`ETH_MIISTATUS, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16706
  while(tmp[`ETH_MIISTATUS_NVALID] !== 1'b0) //`ETH_MIISTATUS_NVALID
16707
  begin
16708
    @(posedge wb_clk);
16709
    wbm_read(`ETH_MIISTATUS, tmp, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16710
  end
16711
end
16712
endtask // check_mii_scan_valid
16713
 
16714
 
16715
task mii_write_req; // requests write to MII
16716
  input [4:0]  phy_addr;
16717
  input [4:0]  reg_addr;
16718
  input [15:0] data_in;
16719
begin
16720
  // MII address, PHY address = 1, command register address = 0
16721
  wbm_write(`ETH_MIIADDRESS, (`ETH_MIIADDRESS_FIAD & phy_addr) | (`ETH_MIIADDRESS_RGAD & (reg_addr << 8)),
16722
            4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16723
  // MII TX data
16724
  wbm_write(`ETH_MIITX_DATA, {16'h0000, data_in}, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16725
  // MII command
16726
  wbm_write(`ETH_MIICOMMAND, `ETH_MIICOMMAND_WCTRLDATA, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16727
  @(posedge wb_clk);
16728
end
16729
endtask // mii_write_req
16730
 
16731
 
16732
task mii_read_req; // requests read from MII
16733
  input [4:0]  phy_addr;
16734
  input [4:0]  reg_addr;
16735
begin
16736
  // MII address, PHY address = 1, command register address = 0
16737
  wbm_write(`ETH_MIIADDRESS, (`ETH_MIIADDRESS_FIAD & phy_addr) | (`ETH_MIIADDRESS_RGAD & (reg_addr << 8)),
16738
            4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16739
  // MII command
16740
  wbm_write(`ETH_MIICOMMAND, `ETH_MIICOMMAND_RSTAT, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16741
  @(posedge wb_clk);
16742
end
16743
endtask // mii_read_req
16744
 
16745
 
16746
task mii_scan_req; // requests scan from MII
16747
  input [4:0]  phy_addr;
16748
  input [4:0]  reg_addr;
16749
begin
16750
  // MII address, PHY address = 1, command register address = 0
16751
  wbm_write(`ETH_MIIADDRESS, (`ETH_MIIADDRESS_FIAD & phy_addr) | (`ETH_MIIADDRESS_RGAD & (reg_addr << 8)),
16752
            4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16753
  // MII command
16754
  wbm_write(`ETH_MIICOMMAND, `ETH_MIICOMMAND_SCANSTAT, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16755
  @(posedge wb_clk);
16756
end
16757
endtask // mii_scan_req
16758
 
16759
 
16760
task mii_scan_finish; // finish scan from MII
16761
begin
16762
  // MII command
16763
  wbm_write(`ETH_MIICOMMAND, 32'h0, 4'hF, 1, wbm_init_waits, wbm_subseq_waits);
16764
  @(posedge wb_clk);
16765
end
16766
endtask // mii_scan_finish
16767
 
16768
//////////////////////////////////////////////////////////////
16769
// Log files and memory tasks
16770
//////////////////////////////////////////////////////////////
16771
 
16772
task clear_memories;
16773
  reg    [22:0]  adr_i;
16774
  reg            delta_t;
16775
begin
16776
  delta_t = 0;
16777
  for (adr_i = 0; adr_i < 4194304; adr_i = adr_i + 1)
16778
  begin
16779
    eth_phy.rx_mem[adr_i[21:0]] = 0;
16780
    eth_phy.tx_mem[adr_i[21:0]] = 0;
16781
    wb_slave.wb_memory[adr_i[21:2]] = 0;
16782
    delta_t = !delta_t;
16783
  end
16784
end
16785
endtask // clear_memories
16786
 
16787 243 tadejm
task clear_buffer_descriptors;
16788
  reg    [8:0]  adr_i;
16789
  reg            delta_t;
16790
begin
16791
  delta_t = 0;
16792
  for (adr_i = 0; adr_i < 256; adr_i = adr_i + 1)
16793
  begin
16794
    wbm_write((`TX_BD_BASE + {adr_i[7:0], 2'b0}), 32'h0, 4'hF, 1, 4'h1, 4'h1);
16795
    delta_t = !delta_t;
16796
  end
16797
end
16798
endtask // clear_buffer_descriptors
16799
 
16800 169 mohor
task test_note;
16801
  input [799:0] test_note ;
16802
  reg   [799:0] display_note ;
16803
begin
16804
  display_note = test_note;
16805
  while ( display_note[799:792] == 0 )
16806
    display_note = display_note << 8 ;
16807
  $fdisplay( tb_log_file, " " ) ;
16808
  $fdisplay( tb_log_file, "NOTE: %s", display_note ) ;
16809
  $fdisplay( tb_log_file, " " ) ;
16810
end
16811
endtask // test_note
16812
 
16813
task test_heading;
16814
  input [799:0] test_heading ;
16815
  reg   [799:0] display_test ;
16816
begin
16817
  display_test = test_heading;
16818
  while ( display_test[799:792] == 0 )
16819
    display_test = display_test << 8 ;
16820
  $fdisplay( tb_log_file, "  ***************************************************************************************" ) ;
16821
  $fdisplay( tb_log_file, "  ***************************************************************************************" ) ;
16822
  $fdisplay( tb_log_file, "  Heading: %s", display_test ) ;
16823
  $fdisplay( tb_log_file, "  ***************************************************************************************" ) ;
16824
  $fdisplay( tb_log_file, "  ***************************************************************************************" ) ;
16825
  $fdisplay( tb_log_file, " " ) ;
16826
end
16827
endtask // test_heading
16828
 
16829
 
16830
task test_fail ;
16831
  input [7999:0] failure_reason ;
16832
//  reg   [8007:0] display_failure ;
16833
  reg   [7999:0] display_failure ;
16834
  reg   [799:0] display_test ;
16835
begin
16836
  tests_failed = tests_failed + 1 ;
16837
 
16838
  display_failure = failure_reason; // {failure_reason, "!"} ;
16839
  while ( display_failure[7999:7992] == 0 )
16840
    display_failure = display_failure << 8 ;
16841
 
16842
  display_test = test_name ;
16843
  while ( display_test[799:792] == 0 )
16844
    display_test = display_test << 8 ;
16845
 
16846
  $fdisplay( tb_log_file, "    *************************************************************************************" ) ;
16847
  $fdisplay( tb_log_file, "    At time: %t ", $time ) ;
16848
  $fdisplay( tb_log_file, "    Test: %s", display_test ) ;
16849
  $fdisplay( tb_log_file, "    *FAILED* because") ;
16850
  $fdisplay( tb_log_file, "    %s", display_failure ) ;
16851
  $fdisplay( tb_log_file, "    *************************************************************************************" ) ;
16852
  $fdisplay( tb_log_file, " " ) ;
16853
 
16854
 `ifdef STOP_ON_FAILURE
16855
    #20 $stop ;
16856
 `endif
16857
end
16858
endtask // test_fail
16859
 
16860
 
16861
task test_ok ;
16862
  reg [799:0] display_test ;
16863
begin
16864
  tests_successfull = tests_successfull + 1 ;
16865
 
16866
  display_test = test_name ;
16867
  while ( display_test[799:792] == 0 )
16868
    display_test = display_test << 8 ;
16869
 
16870
  $fdisplay( tb_log_file, "    *************************************************************************************" ) ;
16871
  $fdisplay( tb_log_file, "    At time: %t ", $time ) ;
16872
  $fdisplay( tb_log_file, "    Test: %s", display_test ) ;
16873
  $fdisplay( tb_log_file, "    reported *SUCCESSFULL*! ") ;
16874
  $fdisplay( tb_log_file, "    *************************************************************************************" ) ;
16875
  $fdisplay( tb_log_file, " " ) ;
16876
end
16877
endtask // test_ok
16878
 
16879
 
16880
task test_summary;
16881
begin
16882
  $fdisplay(tb_log_file, "**************************** Ethernet MAC test summary **********************************") ;
16883
  $fdisplay(tb_log_file, "Tests performed:   %d", tests_successfull + tests_failed) ;
16884
  $fdisplay(tb_log_file, "Failed tests   :   %d", tests_failed) ;
16885
  $fdisplay(tb_log_file, "Successfull tests: %d", tests_successfull) ;
16886
  $fdisplay(tb_log_file, "**************************** Ethernet MAC test summary **********************************") ;
16887
  $fclose(tb_log_file) ;
16888
end
16889
endtask // test_summary
16890
 
16891
 
16892 116 mohor
endmodule

powered by: WebSVN 2.1.0

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