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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [boards/] [xilinx/] [ml501/] [bench/] [verilog/] [include/] [eth_stim.v] - Blame information for rev 530

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 44 julius
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3 412 julius
////  Ethernet MAC Stimulus                                       ////
4 44 julius
////                                                              ////
5
////  Description                                                 ////
6
////  Ethernet MAC stimulus tasks. Taken from the project         ////
7
////  testbench in the ethmac core.                               ////
8
////                                                              ////
9
////  To Do:                                                      ////
10
////                                                              ////
11
////                                                              ////
12
////  Author(s):                                                  ////
13
////      - Tadej Markovic, tadej@opencores.org                   ////
14
////      - Igor Mohor,     igorM@opencores.org                   ////
15 412 julius
////      - Julius Baxter   julius.baxter@orsoc.se                ////
16 44 julius
////                                                              ////
17
////                                                              ////
18
//////////////////////////////////////////////////////////////////////
19
////                                                              ////
20
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
21
////                                                              ////
22
//// This source file may be used and distributed without         ////
23
//// restriction provided that this copyright statement is not    ////
24
//// removed from the file and that any derivative work contains  ////
25
//// the original copyright notice and the associated disclaimer. ////
26
////                                                              ////
27
//// This source file is free software; you can redistribute it   ////
28
//// and/or modify it under the terms of the GNU Lesser General   ////
29
//// Public License as published by the Free Software Foundation; ////
30
//// either version 2.1 of the License, or (at your option) any   ////
31
//// later version.                                               ////
32
////                                                              ////
33
//// This source is distributed in the hope that it will be       ////
34
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
35
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
36
//// PURPOSE.  See the GNU Lesser General Public License for more ////
37
//// details.                                                     ////
38
////                                                              ////
39
//// You should have received a copy of the GNU Lesser General    ////
40
//// Public License along with this source; if not, download it   ////
41
//// from http://www.opencores.org/lgpl.shtml                     ////
42
////                                                              ////
43
//////////////////////////////////////////////////////////////////////
44 412 julius
`define TIME $display("Time: %0t", $time)
45 44 julius
 
46 412 julius
// Defines for ethernet test to trigger sending/receiving
47
// Is straight forward when using RTL design, but if using netlist then paths to
48
// the RX/TX enabled bits depend on synthesis tool, etc, but ones here appear to
49
// work with design put through Synplify, with hierarchy maintained.
50
`define ETH_TOP dut.ethmac0
51
`define ETH_BD_RAM_PATH `ETH_TOP.wishbone.bd_ram
52
`define ETH_MODER_PATH `ETH_TOP.ethreg1.MODER_0
53 44 julius
 
54 412 julius
`ifdef RTL_SIM
55
 `ifdef ethmac_IS_GATELEVEL
56
  `define ETH_MODER_TXEN_BIT `ETH_MODER_PATH.r_TxEn;
57
  `define ETH_MODER_RXEN_BIT `ETH_MODER_PATH.r_RxEn;
58
 `else
59
  `define ETH_MODER_TXEN_BIT `ETH_MODER_PATH.DataOut[1];
60
  `define ETH_MODER_RXEN_BIT `ETH_MODER_PATH.DataOut[0];
61
 `endif
62
`endif
63
 
64
`ifdef GATE_SIM
65
 `define ETH_MODER_TXEN_BIT `ETH_MODER_PATH.r_TxEn;
66
 `define ETH_MODER_RXEN_BIT `ETH_MODER_PATH.r_RxEn;
67
`endif
68
 
69
reg [15:0] eth_stim_rx_packet_length;
70 44 julius
reg [7:0] st_data;
71 412 julius
reg [31:0] lfsr;
72
integer lfsr_last_byte;
73
 
74
// Is number of ethernet packets to send if doing the eth-rx test.
75 480 julius
parameter eth_stim_num_rx_only_num_packets = 12; // Set to 0 for continuous RX
76
parameter eth_stim_num_rx_only_packet_size = 60;
77 412 julius
parameter eth_stim_num_rx_only_packet_size_change = 2'b01;  // 2'b01: Increment
78 480 julius
parameter eth_stim_num_rx_only_packet_size_change_amount = 127;
79 485 julius
parameter eth_stim_num_rx_only_IPG = 800_000; // ps
80 412 julius
 
81
// Do call/response test
82
reg eth_stim_do_rx_reponse_to_tx;
83 530 julius
reg eth_stim_do_overflow_test;
84 412 julius
 
85
parameter num_tx_bds = 16;
86
parameter num_tx_bds_mask = 4'hf;
87
parameter num_rx_bds = 16;
88
parameter num_rx_bds_mask = 4'hf;
89
parameter max_eth_packet_size = 16'h0600;
90
 
91
// If running eth-rxtxbig test (sending and receiving maximum packets), then
92
// set this parameter to the max packet size, otherwise min packet size
93
//parameter rx_while_tx_min_packet_size = max_eth_packet_size;
94
parameter rx_while_tx_min_packet_size = 32;
95
 
96
// Use the smallest possible IPG
97
parameter eth_stim_use_min_IPG = 0;
98 485 julius
parameter eth_stim_IPG_delay_max = 100_000_000; // Maximum 100 us
99 412 julius
//parameter eth_stim_IPG_delay_max = 100_000_000; // Maximum 100mS between packets
100 480 julius
parameter eth_stim_IPG_min_10mb = 9600_000; // 9.6 uS
101
parameter eth_stim_IPG_min_100mb = 800_000; // 860+~100 = 960 nS 100MBit min IPG
102 412 julius
parameter eth_stim_check_rx_packet_contents = 1;
103
parameter eth_stim_check_tx_packet_contents = 1;
104
 
105
parameter eth_inject_errors = 0;
106
 
107
// When running simulations where you don't want to feed packets to the design
108
// like this...
109
parameter eth_stim_disable_rx_stim = 0;
110
 
111
// Delay between seeing that the buffer descriptor for an RX packet says it's
112
// been received and ending up in the memory.
113
// For 25MHz sdram controller, use following:
114
//parameter  Td_rx_packet_check = (`BOARD_CLOCK_PERIOD * 2000);
115
// For 64MHz sdram controller, use following:
116
parameter  Td_rx_packet_check = (`BOARD_CLOCK_PERIOD * 500);
117
 
118
integer expected_rxbd;// init to 0
119
integer expected_txbd;
120
 
121
wire ethmac_rxen;
122
wire ethmac_txen;
123
assign ethmac_rxen = eth_stim_disable_rx_stim ? 0 : `ETH_MODER_RXEN_BIT;
124
assign ethmac_txen = `ETH_MODER_TXEN_BIT;
125
 
126
integer eth_rx_num_packets_sent = 0;
127
integer eth_rx_num_packets_checked = 0;
128
integer num_tx_packets = 1;
129
 
130
integer rx_packet_lengths [0:1023]; // Array of packet lengths
131
 
132
 
133
integer speed_loop;
134
 
135
// When txen is (re)enabled, the tx bd pointer goes back to 0
136
always @(posedge ethmac_txen)
137
  expected_txbd = 0;
138
 
139
   reg  eth_stim_waiting;
140
 
141 44 julius
initial
142
  begin
143 412 julius
     #1;
144
     //lfsr = 32'h84218421; // Init pseudo lfsr
145
     lfsr = 32'h00700001; // Init pseudo lfsr
146
     lfsr_last_byte = 0;
147
 
148
     eth_stim_waiting = 1;
149
     expected_rxbd = num_tx_bds; // init this here
150 44 julius
 
151 412 julius
     eth_stim_do_rx_reponse_to_tx = 0;
152 530 julius
     eth_stim_do_overflow_test = 0;
153 44 julius
 
154 412 julius
     while (eth_stim_waiting) // Loop, waiting for enabling of MAC by software
155
       begin
156
          #100;
157
          // If RX enable and not TX enable...
158
          if(ethmac_rxen === 1'b1 & !(ethmac_txen===1'b1))
159
            begin
160
               if (eth_inject_errors)
161
                 begin
162
                    do_rx_only_stim(16, 64, 0, 0);
163
                    do_rx_only_stim(128, 64, 1'b1, 8);
164
                    do_rx_only_stim(256, 64, 1'b1, 4);
165
                    eth_stim_waiting = 0;
166
                 end
167
               else
168
                 begin
169
                    //do_rx_only_stim(eth_stim_num_rx_only_num_packets, 
170
                    //eth_stim_num_rx_only_packet_size, 0, 0);
171 44 julius
 
172 412 julius
                    // Call packet send loop directly. No error injection.
173
                    send_packet_loop(eth_stim_num_rx_only_num_packets,
174
                                     eth_stim_num_rx_only_packet_size,
175
                                     eth_stim_num_rx_only_packet_size_change,
176
                                     eth_stim_num_rx_only_packet_size_change_amount,
177
                                     eth_phy0.eth_speed,     // Speed
178
                                     eth_stim_num_rx_only_IPG, // IPG
179
                               48'h0012_3456_789a, 48'h0708_090A_0B0C, 1,
180 530 julius
                               0, 0, 0);
181 412 julius
 
182
                    eth_stim_waiting = 0;
183
                 end
184
            end // if (ethmac_rxen === 1'b1 & !(ethmac_txen===1'b1))
185
          // If both RX and TX enabled
186
          else if (ethmac_rxen === 1'b1 & ethmac_txen===1'b1)
187
            begin
188
               // Both enabled - let's wait for the first packet transmitted
189
               // to see what stimulus we should provide
190
               while (num_tx_packets==1)
191
                 #1000;
192
 
193
               $display("* ethmac RX/TX test request: %x", eth_phy0.tx_mem[0]);
194
 
195
               // Check the first received byte's value
196
                 case (eth_phy0.tx_mem[0])
197
                   0:
198
                     begin
199
                        // kickoff call/response here
200
                        eth_stim_do_rx_reponse_to_tx = 1;
201
                     end
202 530 julius
                   1:
203
                     begin
204
                        // kickoff overflow test here
205
                        eth_stim_do_overflow_test = 1;
206
                     end
207 412 julius
                   default:
208
                     begin
209
                        do_rx_while_tx_stim(1400);
210
                     end
211
                 endcase // case (eth_phy0.tx_mem[0])
212
 
213
               eth_stim_waiting = 0;
214
            end
215
       end // while (eth_stim_waiting)     
216 49 julius
 
217 412 julius
  end // initial begin
218 49 julius
 
219 412 julius
   // Main Ethernet RX testing stimulus task.
220
   // Sends a set of packets at both speeds
221
   task do_rx_only_stim;
222
      input [31:0] num_packets;
223
      input [31:0] start_packet_size;
224
      input        inject_errors;
225
      input [31:0] inject_errors_mod;
226
 
227
      begin
228
 
229
         for(speed_loop=1;speed_loop<3;speed_loop=speed_loop+1)
230
           begin
231
 
232
              send_packet_loop(num_packets, start_packet_size, 2'b01, 1,
233
                               speed_loop[0], 10000,
234
                               48'h0012_3456_789a, 48'h0708_090A_0B0C, 1,
235 530 julius
                               inject_errors, inject_errors_mod, 0);
236 412 julius
 
237
           end
238
 
239
      end
240
   endtask // do_rx_stim
241 44 julius
 
242 412 julius
   // Generate RX packets while there's TX going on
243
   // Sends a set of packets at both speeds
244
   task do_rx_while_tx_stim;
245
      input [31:0] num_packets;
246
      reg [31:0] IPG; // Inter-packet gap
247
      reg [31:0] packet_size;
248 44 julius
 
249 412 julius
      integer    j;
250
      begin
251
 
252
         for(j=0;j<num_packets;j=j+1)
253
           begin
254
              // Determine delay between RX packets:
255
 
256
              if (eth_stim_use_min_IPG)
257
                begin
258
                   // Assign based on whether we're in 100mbit or 10mbit mode
259
                   IPG = eth_phy0.eth_speed ? eth_stim_IPG_min_100mb :
260
                         eth_stim_IPG_min_10mb;
261
                   // Add a little bit of variability
262
                   // Add up to 15
263
                   IPG = IPG + ($random & 32'h000000f);
264
                end
265
              else
266
                begin
267
                   IPG = $random;
268
 
269
                   while (IPG > eth_stim_IPG_delay_max)
270
                     IPG = IPG / 2;
271
 
272
 
273
                end
274
              $display("do_rx_while_tx IPG = %0d", IPG);
275
              // Determine size of next packet:
276
              if (rx_while_tx_min_packet_size == max_eth_packet_size)
277
                // We want to transmit biggest packets possible, easy case
278
                packet_size = max_eth_packet_size - 4;
279
              else
280
                begin
281
                   // Constrained random sized packets
282
                   packet_size = $random;
283
 
284
                   while (packet_size > (max_eth_packet_size-4))
285
                     packet_size = packet_size / 2;
286
 
287
                   // Now divide by least significant bits of j
288
                   packet_size = packet_size / {29'd0,j[1:0],1'b1};
289
                   if (packet_size < 60)
290
                     packet_size = packet_size + 60;
291
                end
292
 
293
              $display("do_rx_while_tx packet_size = %0d", packet_size);
294
              send_packet_loop(1, packet_size, 2'b01, 1, eth_phy0.eth_speed,
295
                               IPG, 48'h0012_3456_789a,
296 530 julius
                               48'h0708_090A_0B0C, 1, 1'b0, 0, 0);
297 44 julius
 
298 412 julius
              // If RX enable went low, wait for it go high again
299
              if (ethmac_rxen===1'b0)
300
                begin
301
 
302
                   while (ethmac_rxen===1'b0)
303
                     begin
304
                        @(posedge ethmac_rxen);
305
                        #10000;
306
                     end
307
 
308
                   // RX disabled and when re-enabled we reset the buffer descriptor number
309
                   expected_rxbd = num_tx_bds;
310 44 julius
 
311 412 julius
                end
312
 
313
           end // for (j=0;j<num_packets;j=j+1)
314
      end
315
   endtask // do_rx_stim
316 44 julius
 
317 412 julius
   // Registers used in detecting transmitted packets
318
   reg eth_stim_tx_loop_keep_polling;
319
   reg [31:0] ethmac_txbd_lenstat, ethmac_last_txbd_lenstat;
320
   reg        eth_stim_detected_packet_tx;
321 44 julius
 
322 412 julius
   // If in call-response mode, whenever we receive a TX packet, we generate
323
   // one and send it back
324
   always @(negedge eth_stim_detected_packet_tx)
325
     begin
326
        if (eth_stim_do_rx_reponse_to_tx & ethmac_rxen)
327
          // Continue if we are enabled
328
          do_rx_response_to_tx();
329
     end
330 530 julius
 
331
 
332
   // If in call-response mode, whenever we receive a TX packet, we generate
333
   // one and send it back
334
   always @(posedge eth_stim_do_overflow_test)
335
     begin
336
          // Continue if we are enabled
337
          do_overflow_stimulus();
338
     end
339 412 julius
 
340
   // Generate RX packet in rsponse to TX packet
341
   task do_rx_response_to_tx;
342
      //input unused;
343
 
344
     reg [31:0] IPG; // Inter-packet gap
345
      reg [31:0] packet_size;
346
 
347
      integer    j;
348
      begin
349 44 julius
 
350 412 julius
         // Get packet size test wants us to send
351
         packet_size = {eth_phy0.tx_mem[0],eth_phy0.tx_mem[1],
352
                        eth_phy0.tx_mem[2],eth_phy0.tx_mem[3]};
353
 
354 44 julius
 
355 412 julius
         IPG = {eth_phy0.tx_mem[4],eth_phy0.tx_mem[5],
356
                eth_phy0.tx_mem[6],eth_phy0.tx_mem[7]};
357
 
358
 
359
         $display("do_rx_response_to_tx IPG = %0d", IPG);
360
         if (packet_size == 0)
361
           begin
362
              // Constrained random sized packets
363
              packet_size = $random;
364
 
365
              while (packet_size > (max_eth_packet_size-4))
366
                packet_size = packet_size / 2;
367
 
368
              if (packet_size < 60)
369
                packet_size = packet_size + 60;
370
           end
371
 
372
         $display("do_rx_response_to_tx packet_size = %0d", packet_size);
373
         send_packet_loop(1, packet_size, 2'b01, 1, eth_phy0.eth_speed,
374
                          IPG, 48'h0012_3456_789a,
375 530 julius
                          48'h0708_090A_0B0C, 1, 1'b0, 0, 0);
376 412 julius
 
377
         // If RX enable went low, wait for it go high again
378
         if (ethmac_rxen===1'b0)
379
           begin
380
 
381
              while (ethmac_rxen===1'b0)
382
                begin
383
                   @(posedge ethmac_rxen);
384
                   #10000;
385
                end
386
 
387
              // RX disabled and when re-enabled we reset the buffer 
388
              // descriptor number
389
              expected_rxbd = num_tx_bds;
390 44 julius
 
391 412 julius
           end
392 44 julius
 
393 412 julius
      end
394
   endtask // do_rx_response_to_tx
395
 
396 530 julius
   // Generate RX packet in rsponse to TX packet
397
   task do_overflow_stimulus;
398
      //input unused;
399
      reg [31:0] IPG; // Inter-packet gap
400
      reg [31:0] packet_size;
401
 
402
      integer    j;
403
 
404
      begin
405 412 julius
 
406 530 julius
         // Maximum packet size
407
         packet_size = 1500;
408
 
409
         // Minimum IPG
410
         IPG = eth_stim_IPG_min_100mb;
411
 
412
         $display("do_overflow_stimulus IPG = %0d", IPG);
413
 
414
 
415
         $display("do_overflow_stimulus packetsize = %0d", packet_size);
416
 
417
         send_packet_loop(num_rx_bds, packet_size, 2'b01, 1,
418
                          eth_phy0.eth_speed,
419
                          IPG, 48'h0012_3456_789a,
420
                          48'h0708_090A_0B0C, 1, 1'b0, 0, 0);
421
 
422
         // This one should cause overflow, don't check it gets there OK
423
         send_packet_loop(1, packet_size, 2'b01, 1,
424
                          eth_phy0.eth_speed,
425
                          IPG, 48'h0012_3456_789a,
426
                          48'h0708_090A_0B0C, 1, 1'b0, 0, 1);
427
 
428
         // Wind back expected RXBD number
429
         if (expected_rxbd == num_tx_bds)
430
           expected_rxbd = num_tx_bds + num_rx_bds - 1;
431
         else
432
           expected_rxbd = expected_rxbd - 1;
433
 
434
         // This one should cause overflow, don't check it gets there OK
435
         send_packet_loop(1, packet_size, 2'b01, 1,
436
                          eth_phy0.eth_speed,
437
                          IPG, 48'h0012_3456_789a,
438
                          48'h0708_090A_0B0C, 1, 1'b0, 0, 1);
439
 
440
         // Wind back expected RXBD number
441
         if (expected_rxbd == num_tx_bds)
442
           expected_rxbd = num_tx_bds + num_rx_bds - 1;
443
         else
444
           expected_rxbd = expected_rxbd - 1;
445
 
446
 
447
         // This one should cause overflow, don't check it gets there OK
448
         send_packet_loop(1, packet_size, 2'b01, 1,
449
                          eth_phy0.eth_speed,
450
                          IPG, 48'h0012_3456_789a,
451
                          48'h0708_090A_0B0C, 1, 1'b0, 0, 1);
452
 
453
         // Wind back expected RXBD number
454
         if (expected_rxbd == num_tx_bds)
455
           expected_rxbd = num_tx_bds + num_rx_bds - 1;
456
         else
457
           expected_rxbd = expected_rxbd - 1;
458
 
459
 
460
         // This one should cause overflow, don't check it gets there OK
461
         send_packet_loop(1, packet_size, 2'b01, 1,
462
                          eth_phy0.eth_speed,
463
                          IPG, 48'h0012_3456_789a,
464
                          48'h0708_090A_0B0C, 1, 1'b0, 0, 1);
465
 
466
         // Wind back expected RXBD number
467
         if (expected_rxbd == num_tx_bds)
468
           expected_rxbd = num_tx_bds + num_rx_bds - 1;
469
         else
470
           expected_rxbd = expected_rxbd - 1;
471
 
472
 
473
         // Wait until a buffer descriptor becomes available
474
         while(`ETH_TOP.wishbone.RxBDRead==1'b1)
475
           #1000;
476
 
477
         $display("%t: RxBDRead gone low",$time);
478
         #10000;
479
 
480
 
481
 
482
         send_packet_loop(1, packet_size, 2'b01, 1, eth_phy0.eth_speed,
483
                          IPG, 48'h0012_3456_789a,
484
                          48'h0708_090A_0B0C, 1, 1'b0, 0, 0);
485
 
486
 
487
         // If RX enable went low, wait for it go high again
488
         if (ethmac_rxen===1'b0)
489
           begin
490
 
491
              while (ethmac_rxen===1'b0)
492
                begin
493
                   @(posedge ethmac_rxen);
494
                   #10000;
495
                end
496
 
497
              // RX disabled and when re-enabled we reset the buffer 
498
              // descriptor number
499
              expected_rxbd = num_tx_bds;
500
 
501
           end
502
 
503
      end
504
   endtask // do_overflow_stimulus
505 412 julius
 
506
 
507
   //
508
   // always@() to check the TX buffer descriptors
509
   //
510
   always @(posedge ethmac_txen)
511
     begin
512
         ethmac_last_txbd_lenstat = 0;
513
         eth_stim_tx_loop_keep_polling=1;
514
         // Wait on the TxBD Ready bit
515
         while(eth_stim_tx_loop_keep_polling)
516
           begin
517
              #10;
518
              get_bd_lenstat(expected_txbd, ethmac_txbd_lenstat);
519
              // Check if we've finished transmitting this BD
520
              if (!ethmac_txbd_lenstat[15] & ethmac_last_txbd_lenstat[15])
521
                // Falling edge of TX BD Ready
522
                eth_stim_detected_packet_tx = 1;
523
 
524
              ethmac_last_txbd_lenstat = ethmac_txbd_lenstat;
525
 
526
              // If TX en goes low then exit
527
              if (!ethmac_txen)
528
                eth_stim_tx_loop_keep_polling = 0;
529
              else if (eth_stim_detected_packet_tx)
530
                begin
531
                   // Wait until the eth_phy has finished receiving it
532
                   while (eth_phy0.mtxen_i === 1'b1)
533
                     #10;
534
 
535
                   $display("(%t) Check TX packet: bd %d: 0x%h",$time,
536
                            expected_txbd, ethmac_txbd_lenstat);
537
 
538
                   // Check the TXBD, see if the packet transmitted OK
539
                   if (ethmac_txbd_lenstat[8] | ethmac_txbd_lenstat[3])
540
                     begin
541
                        // Error occured
542
                        `TIME;
543
                        $display("*E TX Error of packet %0d detected.",
544
                                 num_tx_packets);
545
                        $display(" TX BD %0d = 0x%h", expected_txbd,
546
                                 ethmac_txbd_lenstat);
547
                        if (ethmac_txbd_lenstat[8])
548
                          $display(" Underrun in MAC during TX");
549
                        if (ethmac_txbd_lenstat[3])
550
                          $display(" Retransmission limit hit");
551
 
552
                        $finish;
553
                     end
554
                   else
555
                     begin
556
                        // Packet was OK, let's compare the contents we 
557
                        // received with those that were meant to be transmitted
558
                        if (eth_stim_check_tx_packet_contents)
559
                          begin
560
                             check_tx_packet(expected_txbd);
561
                             expected_txbd = (expected_txbd + 1) &
562
                                             num_tx_bds_mask;
563
                             num_tx_packets = num_tx_packets + 1;
564
                             eth_stim_detected_packet_tx = 0;
565
                          end
566
                     end
567
                end
568
           end // while (eth_stim_tx_loop_keep_polling)
569
     end // always @ (posedge ethmac_txen)
570
 
571
 
572
 
573 415 julius
 
574
`ifdef XILINX_DDR2
575
   // Gets word from correct bank
576
   task get_32bitword_from_xilinx_ddr2;
577
      input [31:0] addr;
578
      output [31:0] insn;
579
      reg [16*8-1:0] ddr2_array_line0,ddr2_array_line1,ddr2_array_line2,
580
                     ddr2_array_line3;
581
      integer        word_in_line_num;
582
      begin
583
        // Get our 4 128-bit chunks (8 half-words in each!! Confused yet?), 
584
        // 16 words total
585
         gen_cs[0].gen[0].u_mem0.memory_read(addr[28:27],addr[26:13],
586
                                             {addr[12:6],3'd0},
587
                                             ddr2_array_line0);
588
         gen_cs[0].gen[1].u_mem0.memory_read(addr[28:27],addr[26:13],
589
                                             {addr[12:6],3'd0},
590
                                             ddr2_array_line1);
591
         gen_cs[0].gen[2].u_mem0.memory_read(addr[28:27],addr[26:13],
592
                                             {addr[12:6],3'd0},
593
                                             ddr2_array_line2);
594
         gen_cs[0].gen[3].u_mem0.memory_read(addr[28:27],addr[26:13],
595
                                             {addr[12:6],3'd0},
596
                                             ddr2_array_line3);
597
         case (addr[5:2])
598
           4'h0:
599
             begin
600
                insn[15:0] = ddr2_array_line0[15:0];
601
                insn[31:16] = ddr2_array_line1[15:0];
602
             end
603
           4'h1:
604
             begin
605
                insn[15:0] = ddr2_array_line2[15:0];
606
                insn[31:16] = ddr2_array_line3[15:0];
607
             end
608
           4'h2:
609
             begin
610
                insn[15:0] = ddr2_array_line0[31:16];
611
                insn[31:16] = ddr2_array_line1[31:16];
612
             end
613
           4'h3:
614
             begin
615
                insn[15:0] = ddr2_array_line2[31:16];
616
                insn[31:16] = ddr2_array_line3[31:16];
617
             end
618
           4'h4:
619
             begin
620
                insn[15:0] = ddr2_array_line0[47:32];
621
                insn[31:16] = ddr2_array_line1[47:32];
622
             end
623
           4'h5:
624
             begin
625
                insn[15:0] = ddr2_array_line2[47:32];
626
                insn[31:16] = ddr2_array_line3[47:32];
627
             end
628
           4'h6:
629
             begin
630
                insn[15:0] = ddr2_array_line0[63:48];
631
                insn[31:16] = ddr2_array_line1[63:48];
632
             end
633
           4'h7:
634
             begin
635
                insn[15:0] = ddr2_array_line2[63:48];
636
                insn[31:16] = ddr2_array_line3[63:48];
637
             end
638
           4'h8:
639
             begin
640
                insn[15:0] = ddr2_array_line0[79:64];
641
                insn[31:16] = ddr2_array_line1[79:64];
642
             end
643
           4'h9:
644
             begin
645
                insn[15:0] = ddr2_array_line2[79:64];
646
                insn[31:16] = ddr2_array_line3[79:64];
647
             end
648
           4'ha:
649
             begin
650
                insn[15:0] = ddr2_array_line0[95:80];
651
                insn[31:16] = ddr2_array_line1[95:80];
652
             end
653
           4'hb:
654
             begin
655
                insn[15:0] = ddr2_array_line2[95:80];
656
                insn[31:16] = ddr2_array_line3[95:80];
657
             end
658
           4'hc:
659
             begin
660
                insn[15:0] = ddr2_array_line0[111:96];
661
                insn[31:16] = ddr2_array_line1[111:96];
662
             end
663
           4'hd:
664
             begin
665
                insn[15:0] = ddr2_array_line2[111:96];
666
                insn[31:16] = ddr2_array_line3[111:96];
667
             end
668
           4'he:
669
             begin
670
                insn[15:0] = ddr2_array_line0[127:112];
671
                insn[31:16] = ddr2_array_line1[127:112];
672
             end
673
           4'hf:
674
             begin
675
                insn[15:0] = ddr2_array_line2[127:112];
676
                insn[31:16] = ddr2_array_line3[127:112];
677
             end
678
         endcase // case (addr[5:2])
679
      end
680
   endtask
681
 
682
   task get_byte_from_xilinx_ddr2;
683
      input [31:0] addr;
684
      output [7:0] data_byte;
685
      reg [31:0]   word;
686
      begin
687
         get_32bitword_from_xilinx_ddr2(addr, word);
688
         case (addr[1:0])
689
           2'b00:
690
             data_byte = word[31:24];
691
           2'b01:
692
             data_byte = word[23:16];
693
           2'b10:
694
             data_byte = word[15:8];
695
           2'b11:
696
             data_byte = word[7:0];
697
         endcase // case (addr[1:0])
698
      end
699
   endtask // get_byte_from_xilinx_ddr2
700
 
701
`endif
702
 
703 480 julius
`ifdef XILINX_DDR2
704
   task sync_controller_cache_xilinx_ddr;
705
      begin
706
         // Sync cache (writeback dirty lines) with external memory
707
         dut.xilinx_ddr2_0.xilinx_ddr2_if0.do_sync;
708
         // Wait for it to occur.
709
         while (dut.xilinx_ddr2_0.xilinx_ddr2_if0.sync)
710
           #100;
711 415 julius
 
712 480 julius
         // Wait just incase writeback of all data hasn't fully occurred.
713
         // 4uS, in case RAM needs to refresh while writing back.
714
         #4_000_000;
715
 
716
 
717
      end
718
   endtask // sync_controller_cache_xilinx_ddr
719
`endif
720
 
721
 
722 412 julius
   //
723
   // Check packet TX'd by MAC was good
724
   // 
725
   task check_tx_packet;
726
      input [31:0] tx_bd_num;
727
 
728
      reg [31:0]   tx_bd_addr;
729
      reg [7:0]    phy_byte;
730
 
731
      reg [31:0]   txpnt_wb; // Pointer in array to where data should be
732
      reg [24:0]   txpnt_sdram; // Index in array of shorts for data in SDRAM 
733
                                // part
734
      reg [21:0]   buffer;
735
      reg [7:0]    sdram_byte;
736
      reg [31:0]   tx_len_bd;
737
 
738
      integer      i;
739
      integer      failure;
740 44 julius
      begin
741 412 julius
         failure = 0;
742 480 julius
 
743 412 julius
         get_bd_lenstat(tx_bd_num, tx_len_bd);
744
 
745
         tx_len_bd = {15'd0,tx_len_bd[31:16]};
746
 
747
         // Check, if length didn't have to be padded, that
748
         // amount transmitted was correct
749
         if ((tx_len_bd > 60)&(tx_len_bd != (eth_phy0.tx_len-4)))
750
           begin
751
              $display("*E TX packet sent length, %0d != length in TX BD, %0d",
752
                       eth_phy0.tx_len-4, tx_len_bd);
753
              #100;
754
              $finish;
755
           end
756 480 julius
 
757
`ifdef XILINX_DDR2
758
         sync_controller_cache_xilinx_ddr;
759
`endif
760 412 julius
 
761
         get_bd_addr(tx_bd_num, tx_bd_addr);
762
 
763
         // We're never going to be using more than about 256K of receive buffer
764
         // so let's lop off the top bit of the address pointer - we only want
765
         // the offset from the base of the memory bank
766
         txpnt_wb = {14'd0,tx_bd_addr[17:0]};
767
         txpnt_sdram = tx_bd_addr[24:0];
768
 
769
         // Variable we'll use for index in the PHY's TX buffer
770
         buffer = 0; // Start of TX data
771 415 julius
 
772 412 julius
         for (i=0;i<tx_len_bd;i=i+1)
773
           begin
774
              //$display("Checking address in tx bd 0x%0h",txpnt_sdram);
775 415 julius
              sdram_byte = 8'hx;
776 439 julius
`ifdef RAM_WB
777 530 julius
              sdram_byte = dut.ram_wb0.ram_wb_b3_0.get_mem8(txpnt_sdram);
778 480 julius
`else
779
 `ifdef VERSATILE_SDRAM
780
              sdram0.get_byte(txpnt_sdram,sdram_byte);
781
 `else
782
  `ifdef XILINX_DDR2
783
              get_byte_from_xilinx_ddr2(txpnt_sdram, sdram_byte);
784
  `else
785
              $display(" * Error: sdram_byte was %x", sdram_byte);
786
 
787
              $display(" * eth_stim needs to be able to access the main memory to check packet rx/tx");
788
              $finish;
789
 
790
  `endif
791
 `endif
792 415 julius
`endif
793 412 julius
 
794
              phy_byte = eth_phy0.tx_mem[buffer];
795
              // Debugging output
796
              //$display("txpnt_sdram = 0x%h, sdram_byte = 0x%h, buffer = 0x%h, phy_byte = 0x%h", txpnt_sdram,  sdram_byte, buffer, phy_byte);
797 480 julius
 
798 412 julius
              if (phy_byte !== sdram_byte)
799
                begin
800
                   `TIME;
801
                   $display("*E Wrong byte (%d) of TX packet! ram = %h, phy = %h",buffer, sdram_byte, phy_byte);
802
                   failure = 1;
803
                end
804
 
805
              buffer = buffer + 1;
806
 
807
              txpnt_sdram = txpnt_sdram+1;
808
 
809
           end // for (i=0;i<tx_len_bd;i=i+1)
810
 
811
         if (failure)
812
           begin
813
              #100
814
                `TIME;
815
              $display("*E Error transmitting packet %0d (%0d bytes). Finishing simulation", num_tx_packets, tx_len_bd);
816
              get_bd_lenstat(tx_bd_num, tx_len_bd);
817
              $display("   TXBD lenstat: 0x%0h",tx_len_bd);
818
              $display("   TXBD address: 0x%0h",tx_bd_addr);
819
              $finish;
820
           end
821
         else
822
           begin
823
              #1 $display( "(%0t)(%m) TX packet %0d: %0d bytes in memory OK!",$time,num_tx_packets, tx_len_bd);
824
 
825
           end
826
 
827
 
828 44 julius
      end
829 412 julius
   endtask // check_tx_packet
830
 
831 530 julius
 
832
   // Local buffer of "sent" data to the ethernet MAC, we will check against
833
   // Size of our local buffer in bytes
834
   parameter eth_rx_sent_circbuf_size = (16*1024);
835
   parameter eth_rx_sent_circbuf_size_mask = eth_rx_sent_circbuf_size - 1;
836
   integer eth_rx_sent_circbuf_fill_ptr = 0;
837
   integer eth_rx_sent_circbuf_read_ptr = 0;
838
   // The actual buffer
839
   reg [7:0] eth_rx_sent_circbuf [0:eth_rx_sent_circbuf_size-1];
840
 
841
 
842 412 julius
   //
843
   // Task to send a set of packets
844
   //
845
   task send_packet_loop;
846
      input [31:0] num_packets;
847
      input [31:0] length;
848
      input [1:0]  length_change; // 0 = none, 1 = incr, 2 = decrement
849
      input [31:0] length_change_size; // Size to change by
850
      input        speed;
851
      input [31:0] back_to_back_delay; // #delay setting between packets
852
      input [47:0] dst_mac;
853
      input [47:0] src_mac;
854
      input        random_fill;
855
      input        random_errors;
856 530 julius
      input [31:0] random_error_mod;
857
      input        dont_confirm_rx;
858
      integer      j, k;
859 412 julius
      reg          error_this_time;
860
      integer      error_type; // 0 = rxerr, 1=bad preamble 2=bad crc 3=TODO
861
      reg [31:0]   rx_bd_lenstat;
862 44 julius
      begin
863 412 julius
         error_type = 0;
864
         error_this_time = 0;
865
 
866
         if (num_packets == 0)
867
           // Loop forever when num_packets is 0
868
           num_packets = 32'h7fffffff;
869
 
870
 
871
         if (speed & !(eth_phy0.control_bit14_10[13] === 1'b1))
872
           begin
873
              // write to phy's control register for 100Mbps
874
              eth_phy0.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
875
              // Swapping speeds, give some delay
876
              #10000;
877
           end
878
         else if (!speed & !(eth_phy0.control_bit14_10[13] === 1'b0))
879
           begin
880
              eth_phy0.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
881
              // Swapping speeds, give some delay
882
              #10000;
883
           end
884
 
885
         eth_phy0.control_bit8_0   = 9'h1_00;
886
 
887
         for(j=0;j<num_packets | length <32;j=j+1)
888
           begin
889
              eth_stim_rx_packet_length = length[15:0]; // Bytes
890
              st_data = 8'h0F;
891
 
892
              // setup RX packet in buffer - length is without CRC
893
              set_rx_packet(0, eth_stim_rx_packet_length, 1'b0, dst_mac,
894
                            src_mac, 16'h0D0E, st_data, random_fill);
895
 
896
              set_rx_addr_type(0, dst_mac, src_mac, 16'h0D0E);
897
 
898
              // Error type 2 is cause CRC error
899
              append_rx_crc(0, eth_stim_rx_packet_length, 1'b0,
900
                            (error_type==2));
901
 
902
              if (error_this_time)
903
                begin
904
                   if (error_type == 0)
905
                     // RX ERR assert during transmit
906
                     eth_phy0.send_rx_packet(64'h0055_5555_5555_5555, 4'h7,
907
                                             8'hD5, 0,
908
                                             eth_stim_rx_packet_length+4,
909
                                             1'b0, 1'b1);
910
                   else if (error_type == 1)
911
                     // Incorrect preamble
912
                     eth_phy0.send_rx_packet(64'h0055_5f55_5555_5555, 4'h7,
913
                                             8'hD5, 0,
914
                                             eth_stim_rx_packet_length+4,
915
                                             1'b0, 1'b0);
916
                   else
917
                     // Normal datapacket
918
                     eth_phy0.send_rx_packet(64'h0055_5555_5555_5555, 4'h7,
919
                                             8'hD5, 0,
920
                                             eth_stim_rx_packet_length+4,
921
                                             1'b0, 1'b0);
922
                end
923
              else
924
                eth_phy0.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5,
925
                                        0, eth_stim_rx_packet_length+4, 1'b0,
926
                                        1'b0);
927
 
928
 
929
              // if RX enable still set (might have gone low during this packet
930
              if (ethmac_rxen)
931
                begin
932 530 julius
                   if (error_this_time || dont_confirm_rx) begin
933 412 julius
                     // Put in dummy length, checking function will skip...
934
                     rx_packet_lengths[(eth_rx_num_packets_sent& 12'h3ff)]=32'heeeeeeee;
935 530 julius
 
936
                      for(k=0;k<length;k=k+1)
937
                      // skip data  in verify buffer
938
                        eth_rx_sent_circbuf_read_ptr = (eth_rx_sent_circbuf_read_ptr+1)&
939
                                                       eth_rx_sent_circbuf_size_mask;
940
 
941
                   end
942 412 julius
                   else
943
                     rx_packet_lengths[(eth_rx_num_packets_sent & 12'h3ff)] = length;
944
 
945
                   eth_rx_num_packets_sent = eth_rx_num_packets_sent + 1;
946
 
947
                end // if (ethmac_rxen)
948
              else
949
                begin
950
                   // Force the loop to finish up                  
951
                   j = num_packets;
952
                end
953
 
954
 
955
              // Inter-packet gap
956
              #back_to_back_delay;
957
 
958
              // Update length
959
              if (length_change == 2'b01)
960
                length = length + length_change_size;
961
 
962
              if ((length_change == 2'b10) &&
963
                  ((length - length_change_size) > 32))
964
                length = length - length_change_size;
965
 
966
              // Increment error type
967
              if (error_this_time)
968
                error_type = error_type + 1;
969
              if (error_type > 3)
970
                error_type = 0;
971
 
972
 
973
              // Check if we should put in an error this time
974
              if (j%random_error_mod == 0)
975
                error_this_time = 1;
976
              else
977
                error_this_time = 0;
978
 
979
              eth_phy0.rx_err(0);
980
 
981
              // Now wait to check if we have filled up all the RX BDs and
982
              // the this packet would start writing over them. Only really an
983
              // issue when doing minimum IPG tests.
984
              while(((eth_rx_num_packets_sent+1) - eth_rx_num_packets_checked)
985
                    == num_rx_bds)
986
                #100;
987
 
988
 
989
           end // for (j=0;j<num_packets | length <32;j=j+1)
990 44 julius
      end
991 412 julius
   endtask // send_packet_loop
992
 
993
   /*
994
    TASKS for set and check RX packets:
995
    -----------------------------------
996
    set_rx_packet
997
    (rxpnt[31:0], len[15:0], plus_nibble, d_addr[47:0], s_addr[47:0], type_len[15:0], start_data[7:0]);
998
    check_rx_packet
999
    (rxpnt_phy[31:0], rxpnt_wb[31:0], len[15:0], plus_nibble, successful_nibble, failure[31:0]);
1000
    */
1001
   task set_rx_packet;
1002
      input  [31:0] rxpnt; // pointer to place in in the phy rx buffer we'll start at
1003
      input [15:0]  len;
1004
      input         plus_dribble_nibble; // if length is longer for one nibble
1005
      input [47:0]  eth_dest_addr;
1006
      input [47:0]  eth_source_addr;
1007
      input [15:0]  eth_type_len;
1008
      input [7:0]   eth_start_data;
1009
      input         random_fill;
1010
      integer       i, sd;
1011
      reg [47:0]    dest_addr;
1012
      reg [47:0]    source_addr;
1013
      reg [15:0]    type_len;
1014
      reg [21:0]    buffer;
1015
      reg           delta_t;
1016
 
1017 44 julius
      begin
1018 412 julius
         buffer = rxpnt[21:0];
1019
         dest_addr = eth_dest_addr;
1020
         source_addr = eth_source_addr;
1021
         type_len = eth_type_len;
1022
         sd = eth_start_data;
1023
         delta_t = 0;
1024
         for(i = 0; i < len; i = i + 1)
1025
           begin
1026
              if (i < 6)
1027
                begin
1028
                   eth_phy0.rx_mem[buffer] = dest_addr[47:40];
1029
                   dest_addr = dest_addr << 8;
1030
                end
1031
              else if (i < 12)
1032
                begin
1033
                   eth_phy0.rx_mem[buffer] = source_addr[47:40];
1034
                   source_addr = source_addr << 8;
1035
                end
1036
              else if (i < 14)
1037
                begin
1038
                   eth_phy0.rx_mem[buffer] = type_len[15:8];
1039
                   type_len = type_len << 8;
1040
                end
1041
              else
1042
                begin
1043
                   if (random_fill)
1044
                     begin
1045
                        if (lfsr_last_byte == 0)
1046
                          eth_phy0.rx_mem[buffer] = lfsr[15:8];
1047
                        if (lfsr_last_byte == 1)
1048
                          eth_phy0.rx_mem[buffer] = lfsr[23:16];
1049
                        if (lfsr_last_byte == 2)
1050
                          eth_phy0.rx_mem[buffer] = lfsr[31:24];
1051
                        if (lfsr_last_byte == 3)
1052
                          begin
1053
                             eth_phy0.rx_mem[buffer] = lfsr[7:0];
1054
                             lfsr = {lfsr[30:0],(((lfsr[31] ^ lfsr[6]) ^
1055
                                                  lfsr[5]) ^ lfsr[1])};
1056
                             lfsr_last_byte =  0;
1057
                          end
1058
                        else
1059
                          lfsr_last_byte = lfsr_last_byte + 1;
1060
 
1061
                     end // if (random_fill)               
1062
                   else
1063
                     eth_phy0.rx_mem[buffer] = sd[7:0];
1064
                   sd = sd + 1;
1065
                end // else: !if(i < 14)
1066
 
1067
              // Update our local buffer
1068
              eth_rx_sent_circbuf[eth_rx_sent_circbuf_fill_ptr]
1069
                = eth_phy0.rx_mem[buffer];
1070
              eth_rx_sent_circbuf_fill_ptr = (eth_rx_sent_circbuf_fill_ptr+1)&
1071
                                             eth_rx_sent_circbuf_size_mask;
1072
 
1073
              buffer = buffer + 1;
1074
           end // for (i = 0; i < len; i = i + 1)
1075
 
1076
         delta_t = !delta_t;
1077
         if (plus_dribble_nibble)
1078
           eth_phy0.rx_mem[buffer] = {4'h0, 4'hD /*sd[3:0]*/};
1079
         delta_t = !delta_t;
1080 44 julius
      end
1081 412 julius
   endtask // set_rx_packet
1082
 
1083
 
1084
 
1085
 
1086
   task set_rx_addr_type;
1087
      input  [31:0] rxpnt;
1088
      input [47:0]  eth_dest_addr;
1089
      input [47:0]  eth_source_addr;
1090
      input [15:0]  eth_type_len;
1091
      integer       i;
1092
      reg [47:0]    dest_addr;
1093
      reg [47:0]    source_addr;
1094
      reg [15:0]    type_len;
1095
      reg [21:0]    buffer;
1096
      reg           delta_t;
1097 44 julius
      begin
1098 412 julius
         buffer = rxpnt[21:0];
1099
         dest_addr = eth_dest_addr;
1100
         source_addr = eth_source_addr;
1101
         type_len = eth_type_len;
1102
         delta_t = 0;
1103
         for(i = 0; i < 14; i = i + 1)
1104
           begin
1105
              if (i < 6)
1106
                begin
1107
                   eth_phy0.rx_mem[buffer] = dest_addr[47:40];
1108
                   dest_addr = dest_addr << 8;
1109
                end
1110
              else if (i < 12)
1111
                begin
1112
                   eth_phy0.rx_mem[buffer] = source_addr[47:40];
1113
                   source_addr = source_addr << 8;
1114
                end
1115
              else // if (i < 14)
1116
                begin
1117
                   eth_phy0.rx_mem[buffer] = type_len[15:8];
1118
                   type_len = type_len << 8;
1119
                end
1120
              buffer = buffer + 1;
1121
           end
1122
         delta_t = !delta_t;
1123 44 julius
      end
1124 412 julius
   endtask // set_rx_addr_type
1125
 
1126
 
1127
   // Check if we're using a synthesized version of eth module
1128
`ifdef ethmac_IS_GATELEVEL
1129
 
1130
   // Get the length/status register of the ethernet buffer descriptor
1131
   task get_bd_lenstat;
1132
      input [31:0] bd_num;// Number of ethernet BD to check
1133
      output [31:0] bd_lenstat;
1134
 `ifdef ACTEL
1135
      reg [8:0]    tmp;
1136
      integer      raddr;
1137
 `endif
1138 44 julius
      begin
1139 412 julius
 `ifdef ACTEL
1140
 
1141
         // Pull from the Actel memory model
1142
         raddr = `ETH_BD_RAM_PATH.\mem_tile.I_1 .get_address((bd_num*2));
1143
 
1144
         tmp = `ETH_BD_RAM_PATH.\mem_tile.I_1 .MEM_512_9[(raddr*2)];
1145
         bd_lenstat[8:0] = tmp[8:0];
1146
 
1147
         tmp = `ETH_BD_RAM_PATH.\mem_tile.I_1 .MEM_512_9[(raddr*2)+1];
1148
         bd_lenstat[17:9] = tmp[8:0];
1149
 
1150
         raddr = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .get_address((bd_num*2));
1151
 
1152
         tmp = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .MEM_512_9[(raddr*2)];
1153
         bd_lenstat[26:18] = tmp[8:0];
1154
 
1155
         tmp = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .MEM_512_9[(raddr*2)+1];
1156
         bd_lenstat[31:27] = tmp[4:0];
1157
 
1158
         //$display("(%t) read eth bd lenstat %h",$time, bd_lenstat);
1159
 `endif
1160 44 julius
      end
1161 412 julius
   endtask // get_bd_lenstat
1162
 
1163
   // Get the length/status register of the ethernet buffer descriptor
1164
   task get_bd_addr;
1165
      input [31:0] bd_num;// Number of the ethernet BD to check
1166
      output [31:0] bd_addr;
1167
 `ifdef ACTEL
1168
      reg [8:0]    tmp;
1169
      integer       raddr;
1170
 `endif
1171 44 julius
      begin
1172 412 julius
 `ifdef ACTEL
1173
         // Pull from the Actel memory model
1174
         raddr = `ETH_BD_RAM_PATH.\mem_tile.I_1 .get_address((bd_num*2)+1);
1175
 
1176
         tmp = `ETH_BD_RAM_PATH.\mem_tile.I_1 .MEM_512_9[(raddr*2)];
1177
         bd_addr[8:0] = tmp[8:0];
1178
 
1179
         tmp = `ETH_BD_RAM_PATH.\mem_tile.I_1 .MEM_512_9[(raddr*2)+1];
1180
         bd_addr[17:9] = tmp[8:0];
1181
 
1182
         raddr = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .get_address((bd_num*2)+1);
1183
 
1184
         tmp = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .MEM_512_9[(raddr*2)];
1185
         bd_addr[26:18] = tmp[8:0];
1186
 
1187
         tmp = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .MEM_512_9[(raddr*2)+1];
1188
         bd_addr[31:27] = tmp[4:0];
1189
 
1190
         //$display("(%t) read eth bd%d addr %h",$time,bd_num, bd_addr);
1191
 `endif
1192 44 julius
      end
1193 412 julius
   endtask // get_bd_addr
1194
 
1195
`else // !`ifdef ethmac_IS_GATELEVEL
1196
 
1197
   // Get the length/status register of the ethernet buffer descriptor
1198
   task get_bd_lenstat;
1199
      input [31:0] bd_num;// Number of ethernet BD to check
1200
      output [31:0] bd_lenstat;
1201 44 julius
      begin
1202 412 julius
         bd_lenstat = `ETH_BD_RAM_PATH.mem[(bd_num*2)];
1203 44 julius
      end
1204 412 julius
   endtask // get_bd_lenstat
1205
 
1206
   // Get the length/status register of the ethernet buffer descriptor
1207
   task get_bd_addr;
1208
      input [31:0] bd_num;// Number of the ethernet BD to check
1209
      output [31:0] bd_addr;
1210 44 julius
      begin
1211 412 julius
         bd_addr = `ETH_BD_RAM_PATH.mem[((bd_num*2)+1)];
1212
         //$display("(%t) read eth bd%d addr %h",$time,bd_num, bd_addr);
1213 44 julius
      end
1214 412 julius
   endtask // get_bd_addr
1215
`endif
1216
 
1217
   // Always block triggered by finishing of transmission of new packet from 
1218
   // send_packet_loop
1219
   integer eth_rx_packet_length_to_check;
1220 44 julius
 
1221 412 julius
   always @*
1222
     begin
1223
        // Loop here until:
1224
        // 1 - packets sent is not equal to packets checked (ie. some to check)
1225
        // 2 - we're explicitly disabled for some reason
1226
        // 3 - Receive has been disabled in the MAC
1227
        while((eth_rx_num_packets_sent == eth_rx_num_packets_checked) ||
1228
              !eth_stim_check_rx_packet_contents || !(ethmac_rxen===1'b1))
1229
          #1000;
1230 44 julius
 
1231 412 julius
        eth_rx_packet_length_to_check
1232
          = rx_packet_lengths[(eth_rx_num_packets_checked & 12'h3ff)];
1233
 
1234
        if ( eth_rx_packet_length_to_check !==  32'heeeeeeee)
1235
          check_rx_packet(expected_rxbd, 0, eth_rx_packet_length_to_check);
1236
 
1237
        eth_rx_num_packets_checked = eth_rx_num_packets_checked + 1;
1238
 
1239
        expected_rxbd = expected_rxbd + 1;
1240
 
1241
        // Wrap
1242
        if (expected_rxbd == (num_tx_bds + num_rx_bds))
1243
          expected_rxbd = num_tx_bds;
1244
     end
1245
 
1246
   task check_rx_packet;
1247
 
1248
      input [31:0] rx_bd_num;
1249
      input [31:0] rxpnt_phy; // Pointer in array of data in PHY
1250
      input [31:0] len;
1251
 
1252
      reg [31:0]   rx_bd_lenstat;
1253
      reg [31:0]   rx_bd_addr;
1254
      reg [7:0]    phy_byte;
1255
 
1256
      reg [31:0]   rxpnt_wb; // Pointer in array to where data should be
1257
      reg [24:0]   rxpnt_sdram; // byte address from CPU in RAM
1258
      reg [15:0]   sdram_short;
1259
      reg [7:0]    sdram_byte;
1260
 
1261
      integer      i;
1262
      integer      failure;
1263
 
1264
      begin
1265 44 julius
 
1266 412 julius
         failure = 0;
1267
 
1268
         // Wait until the buffer descriptor indicates the packet has been 
1269
         // received...
1270
         get_bd_lenstat(rx_bd_num, rx_bd_lenstat);
1271
         while (rx_bd_lenstat & 32'h00008000)// Check Empty bit
1272
           begin
1273
              #10;
1274
              get_bd_lenstat(rx_bd_num, rx_bd_lenstat);
1275
              //$display("(%t) check_rx_packet: poll bd %d: 0x%h",$time,
1276
                //        rx_bd_num, rx_bd_lenstat);
1277
           end
1278
 
1279 44 julius
 
1280 412 julius
         // Delay some time - takes a bit for the Wishbone FSM to pipe out the
1281
         // packet over Wishbone and into whatever memory it's going into
1282
         #Td_rx_packet_check;
1283 480 julius
 
1284
`ifdef XILINX_DDR2
1285
         sync_controller_cache_xilinx_ddr;
1286
`endif
1287 412 julius
 
1288
         // Ok, buffer filled, let's get its offset in memory
1289
         get_bd_addr(rx_bd_num, rx_bd_addr);
1290 44 julius
 
1291 412 julius
         $display("(%t) Check RX packet: bd %d: 0x%h, addr 0x%h",$time,
1292
                  rx_bd_num, rx_bd_lenstat, rx_bd_addr);
1293 44 julius
 
1294 412 julius
 
1295
         // We're never going to be using more than about 256KB of receive buffer
1296
         // so let's lop off the top bit of the address pointer - we only want
1297
         // the offset from the base of the memory bank
1298
 
1299
         rxpnt_wb = {14'd0,rx_bd_addr[17:0]};
1300
         rxpnt_sdram = rx_bd_addr[24:0];
1301 415 julius
 
1302
 
1303 412 julius
         //$display("RAM pointer for BD is 0x%h, SDRAM addr is 0x%h", rx_bd_addr, rxpnt_sdram);
1304 44 julius
 
1305
 
1306 412 julius
         for (i=0;i<len;i=i+1)
1307
           begin
1308 44 julius
 
1309 415 julius
              sdram_byte = 8'hx;
1310 530 julius
 
1311
`ifdef RAM_WB
1312
              sdram_byte = dut.ram_wb0.ram_wb_b3_0.get_mem8(rxpnt_sdram);
1313
`else
1314
 `ifdef XILINX_DDR2
1315 415 julius
              get_byte_from_xilinx_ddr2(rxpnt_sdram, sdram_byte);
1316 530 julius
 `else
1317 480 julius
              $display(" * Error:");
1318
 
1319
              $display(" * eth_stim needs to be able to access the main memory to check packet rx/tx");
1320
              $finish;
1321 530 julius
 `endif
1322 480 julius
`endif
1323
 
1324
              phy_byte = eth_rx_sent_circbuf[eth_rx_sent_circbuf_read_ptr];
1325 44 julius
 
1326 412 julius
              if (phy_byte !== sdram_byte)
1327
                begin
1328
//                 `TIME;                 
1329 480 julius
                   $display("*E Wrong byte (%5d) of RX packet %5d. phy mem = %h, ram = %h",
1330 412 julius
                            i, eth_rx_num_packets_checked, phy_byte, sdram_byte);
1331
                   failure = 1;
1332
                end
1333
 
1334
              eth_rx_sent_circbuf_read_ptr = (eth_rx_sent_circbuf_read_ptr+1)&
1335
                                             eth_rx_sent_circbuf_size_mask;
1336
 
1337
              rxpnt_sdram = rxpnt_sdram+1;
1338
 
1339
           end // for (i=0;i<len;i=i+2)
1340
 
1341
         if (failure)
1342
           begin
1343
              #100
1344
                `TIME;
1345
              $display("*E Recieved packet %0d, length %0d bytes, had an error. Finishing simulation.", eth_rx_num_packets_checked, len);
1346
              $finish;
1347
           end
1348
         else
1349
           begin
1350
              #1 $display( "(%0t)(%m) RX packet %0d: %0d bytes in memory OK!",$time,eth_rx_num_packets_checked, len);
1351
 
1352
           end
1353
      end
1354
   endtask // check_rx_packet
1355
 
1356
 
1357
   //////////////////////////////////////////////////////////////
1358
   // Ethernet CRC Basic tasks
1359
   //////////////////////////////////////////////////////////////
1360
 
1361
   task append_rx_crc;
1362
      input  [31:0] rxpnt_phy; // source
1363
      input [15:0]  len; // length in bytes without CRC
1364
      input         plus_dribble_nibble; // if length is longer for one nibble
1365
      input         negated_crc; // if appended CRC is correct or not
1366
      reg [31:0]    crc;
1367
      reg [7:0]     tmp;
1368
      reg [31:0]    addr_phy;
1369
      reg           delta_t;
1370
      begin
1371
         addr_phy = rxpnt_phy + len;
1372
         delta_t = 0;
1373
         // calculate CRC from prepared packet
1374
         paralel_crc_phy_rx(rxpnt_phy, {16'h0, len}, plus_dribble_nibble, crc);
1375
         if (negated_crc)
1376
           crc = ~crc;
1377
         delta_t = !delta_t;
1378
 
1379
         if (plus_dribble_nibble)
1380
           begin
1381
              tmp = eth_phy0.rx_mem[addr_phy];
1382
              eth_phy0.rx_mem[addr_phy]     = {crc[27:24], tmp[3:0]};
1383
              eth_phy0.rx_mem[addr_phy + 1] = {crc[19:16], crc[31:28]};
1384
              eth_phy0.rx_mem[addr_phy + 2] = {crc[11:8], crc[23:20]};
1385
              eth_phy0.rx_mem[addr_phy + 3] = {crc[3:0], crc[15:12]};
1386
              eth_phy0.rx_mem[addr_phy + 4] = {4'h0, crc[7:4]};
1387
           end
1388
         else
1389
           begin
1390
              eth_phy0.rx_mem[addr_phy]     = crc[31:24];
1391
              eth_phy0.rx_mem[addr_phy + 1] = crc[23:16];
1392
              eth_phy0.rx_mem[addr_phy + 2] = crc[15:8];
1393
              eth_phy0.rx_mem[addr_phy + 3] = crc[7:0];
1394
           end
1395
      end
1396
   endtask // append_rx_crc
1397
 
1398
   task append_rx_crc_delayed;
1399
      input  [31:0] rxpnt_phy; // source
1400
      input [15:0]  len; // length in bytes without CRC
1401
      input         plus_dribble_nibble; // if length is longer for one nibble
1402
      input         negated_crc; // if appended CRC is correct or not
1403
      reg [31:0]    crc;
1404
      reg [7:0]     tmp;
1405
      reg [31:0]    addr_phy;
1406
      reg           delta_t;
1407
      begin
1408
         addr_phy = rxpnt_phy + len;
1409
         delta_t = 0;
1410
         // calculate CRC from prepared packet
1411
         paralel_crc_phy_rx(rxpnt_phy+4, {16'h0, len}-4, plus_dribble_nibble, crc);
1412
         if (negated_crc)
1413
           crc = ~crc;
1414
         delta_t = !delta_t;
1415
 
1416
         if (plus_dribble_nibble)
1417
           begin
1418
              tmp = eth_phy0.rx_mem[addr_phy];
1419
              eth_phy0.rx_mem[addr_phy]     = {crc[27:24], tmp[3:0]};
1420
              eth_phy0.rx_mem[addr_phy + 1] = {crc[19:16], crc[31:28]};
1421
              eth_phy0.rx_mem[addr_phy + 2] = {crc[11:8], crc[23:20]};
1422
              eth_phy0.rx_mem[addr_phy + 3] = {crc[3:0], crc[15:12]};
1423
              eth_phy0.rx_mem[addr_phy + 4] = {4'h0, crc[7:4]};
1424
           end
1425
         else
1426
           begin
1427
              eth_phy0.rx_mem[addr_phy]     = crc[31:24];
1428
              eth_phy0.rx_mem[addr_phy + 1] = crc[23:16];
1429
              eth_phy0.rx_mem[addr_phy + 2] = crc[15:8];
1430
              eth_phy0.rx_mem[addr_phy + 3] = crc[7:0];
1431
           end
1432
      end
1433
   endtask // append_rx_crc_delayed
1434
 
1435
 
1436
   // paralel CRC calculating for PHY RX
1437
   task paralel_crc_phy_rx;
1438
      input  [31:0] start_addr; // start address
1439
      input [31:0]  len; // length of frame in Bytes without CRC length
1440
      input         plus_dribble_nibble; // if length is longer for one nibble
1441
      output [31:0] crc_out;
1442
      reg [21:0]    addr_cnt; // only 22 address lines
1443
      integer       word_cnt;
1444
      integer       nibble_cnt;
1445
      reg [31:0]    load_reg;
1446
      reg           delta_t;
1447
      reg [31:0]    crc_next;
1448
      reg [31:0]    crc;
1449
      reg           crc_error;
1450
      reg [3:0]     data_in;
1451
      integer       i;
1452
      begin
1453
         #1 addr_cnt = start_addr[21:0];
1454
         word_cnt = 24; // 27; // start of the frame - nibble granularity (MSbit first)
1455
         crc = 32'hFFFF_FFFF; // INITIAL value
1456
         delta_t = 0;
1457
         // length must include 4 bytes of ZEROs, to generate CRC
1458
         // get number of nibbles from Byte length (2^1 = 2)
1459
         if (plus_dribble_nibble)
1460
           nibble_cnt = ((len + 4) << 1) + 1'b1; // one nibble longer
1461
         else
1462
           nibble_cnt = ((len + 4) << 1);
1463
         // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
1464
         load_reg[31:24] = eth_phy0.rx_mem[addr_cnt];
1465
         addr_cnt = addr_cnt + 1;
1466
         load_reg[23:16] = eth_phy0.rx_mem[addr_cnt];
1467
         addr_cnt = addr_cnt + 1;
1468
         load_reg[15: 8] = eth_phy0.rx_mem[addr_cnt];
1469
         addr_cnt = addr_cnt + 1;
1470
         load_reg[ 7: 0] = eth_phy0.rx_mem[addr_cnt];
1471
         addr_cnt = addr_cnt + 1;
1472
         while (nibble_cnt > 0)
1473
           begin
1474
              // wait for delta time
1475
              delta_t = !delta_t;
1476
              // shift data in
1477
 
1478
              if(nibble_cnt <= 8) // for additional 8 nibbles shift ZEROs in!
1479
                data_in[3:0] = 4'h0;
1480
              else
1481
 
1482
                data_in[3:0] = {load_reg[word_cnt], load_reg[word_cnt+1], load_reg[word_cnt+2], load_reg[word_cnt+3]};
1483
              crc_next[0]  = (data_in[0] ^ crc[28]);
1484
              crc_next[1]  = (data_in[1] ^ data_in[0] ^ crc[28]    ^ crc[29]);
1485
              crc_next[2]  = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]);
1486
              crc_next[3]  = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]);
1487
              crc_next[4]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[0];
1488
              crc_next[5]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[1];
1489
              crc_next[6]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[ 2];
1490
              crc_next[7]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[3];
1491
              crc_next[8]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[4];
1492
              crc_next[9]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[5];
1493
              crc_next[10] = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[6];
1494
              crc_next[11] = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[7];
1495
              crc_next[12] = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]) ^ crc[8];
1496
              crc_next[13] = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]) ^ crc[9];
1497
              crc_next[14] = (data_in[3] ^ data_in[2] ^ crc[30]    ^ crc[31]) ^ crc[10];
1498
              crc_next[15] = (data_in[3] ^ crc[31])   ^ crc[11];
1499
              crc_next[16] = (data_in[0] ^ crc[28])   ^ crc[12];
1500
              crc_next[17] = (data_in[1] ^ crc[29])   ^ crc[13];
1501
              crc_next[18] = (data_in[2] ^ crc[30])   ^ crc[14];
1502
              crc_next[19] = (data_in[3] ^ crc[31])   ^ crc[15];
1503
              crc_next[20] =  crc[16];
1504
              crc_next[21] =  crc[17];
1505
              crc_next[22] = (data_in[0] ^ crc[28])   ^ crc[18];
1506
              crc_next[23] = (data_in[1] ^ data_in[0] ^ crc[29]    ^ crc[28]) ^ crc[19];
1507
              crc_next[24] = (data_in[2] ^ data_in[1] ^ crc[30]    ^ crc[29]) ^ crc[20];
1508
              crc_next[25] = (data_in[3] ^ data_in[2] ^ crc[31]    ^ crc[30]) ^ crc[21];
1509
              crc_next[26] = (data_in[3] ^ data_in[0] ^ crc[31]    ^ crc[28]) ^ crc[22];
1510
              crc_next[27] = (data_in[1] ^ crc[29])   ^ crc[23];
1511
              crc_next[28] = (data_in[2] ^ crc[30])   ^ crc[24];
1512
              crc_next[29] = (data_in[3] ^ crc[31])   ^ crc[25];
1513
              crc_next[30] =  crc[26];
1514
              crc_next[31] =  crc[27];
1515
 
1516
              crc = crc_next;
1517
              crc_error = crc[31:0] != 32'hc704dd7b;  // CRC not equal to magic number
1518
              case (nibble_cnt)
1519
                9: crc_out = {!crc[24], !crc[25], !crc[26], !crc[27], !crc[28], !crc[29], !crc[30], !crc[31],
1520
                              !crc[16], !crc[17], !crc[18], !crc[19], !crc[20], !crc[21], !crc[22], !crc[23],
1521
                              !crc[ 8], !crc[ 9], !crc[10], !crc[11], !crc[12], !crc[13], !crc[14], !crc[15],
1522
                              !crc[ 0], !crc[ 1], !crc[ 2], !crc[ 3], !crc[ 4], !crc[ 5], !crc[ 6], !crc[ 7]};
1523
                default: crc_out = crc_out;
1524
              endcase
1525
              // wait for delta time
1526
              delta_t = !delta_t;
1527
              // increment address and load new data
1528
              if ((word_cnt+3) == 7)//4)
1529
                begin
1530
                   // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
1531
                   load_reg[31:24] = eth_phy0.rx_mem[addr_cnt];
1532
                   addr_cnt = addr_cnt + 1;
1533
                   load_reg[23:16] = eth_phy0.rx_mem[addr_cnt];
1534
                   addr_cnt = addr_cnt + 1;
1535
                   load_reg[15: 8] = eth_phy0.rx_mem[addr_cnt];
1536
                   addr_cnt = addr_cnt + 1;
1537
                   load_reg[ 7: 0] = eth_phy0.rx_mem[addr_cnt];
1538
                   addr_cnt = addr_cnt + 1;
1539
                end
1540
              // set new load bit position
1541
              if((word_cnt+3) == 31)
1542
                word_cnt = 16;
1543
              else if ((word_cnt+3) == 23)
1544
                word_cnt = 8;
1545
              else if ((word_cnt+3) == 15)
1546
                word_cnt = 0;
1547
              else if ((word_cnt+3) == 7)
1548
                word_cnt = 24;
1549
              else
1550
                word_cnt = word_cnt + 4;// - 4;
1551
              // decrement nibble counter
1552
              nibble_cnt = nibble_cnt - 1;
1553
              // wait for delta time
1554
              delta_t = !delta_t;
1555
           end // while
1556
         #1;
1557
      end
1558
   endtask // paralel_crc_phy_rx
1559
 
1560
 
1561
 
1562 44 julius
 

powered by: WebSVN 2.1.0

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