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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [boards/] [actel/] [ordb1a3pe1500/] [bench/] [verilog/] [include/] [eth_stim.v] - Blame information for rev 439

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

Line No. Rev Author Line
1 408 julius
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  Ethernet MAC Stimulus                                       ////
4
////                                                              ////
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
////      - Julius Baxter   julius.baxter@orsoc.se                ////
16
////                                                              ////
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
`define TIME $display("Time: %0t", $time)
45
 
46
// 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 409 julius
`define ETH_TOP dut.ethmac0
51 408 julius
`define ETH_BD_RAM_PATH `ETH_TOP.wishbone.bd_ram
52
`define ETH_MODER_PATH `ETH_TOP.ethreg1.MODER_0
53
 
54
`ifdef RTL_SIM
55 411 julius
 `ifdef ethmac_IS_GATELEVEL
56 408 julius
  `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
reg [7:0] st_data;
71
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 411 julius
parameter eth_stim_num_rx_only_num_packets = 500; // Set to 0 for continuous RX
76
parameter eth_stim_num_rx_only_packet_size = 512;
77
parameter eth_stim_num_rx_only_packet_size_change = 2'b01;  // 2'b01: Increment
78
parameter eth_stim_num_rx_only_packet_size_change_amount = 1;
79
parameter eth_stim_num_rx_only_IPG = 800000; // ns
80 408 julius
 
81 411 julius
// Do call/response test
82
reg eth_stim_do_rx_reponse_to_tx;
83
 
84 408 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 411 julius
 
91 408 julius
// 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
parameter eth_stim_IPG_delay_max = 500_000; // Maximum 500uS ga
99
//parameter eth_stim_IPG_delay_max = 100_000_000; // Maximum 100mS between packets
100
parameter eth_stim_IPG_min_10mb = 9600; // 9.6 uS
101
parameter eth_stim_IPG_min_100mb = 800; // 860+~100 = 960 nS 100MBit min IPG
102
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
 
119
 
120
integer expected_rxbd;// init to 0
121
integer expected_txbd;
122
 
123
wire ethmac_rxen;
124
wire ethmac_txen;
125
assign ethmac_rxen = eth_stim_disable_rx_stim ? 0 : `ETH_MODER_RXEN_BIT;
126
assign ethmac_txen = `ETH_MODER_TXEN_BIT;
127
 
128
integer eth_rx_num_packets_sent = 0;
129
integer eth_rx_num_packets_checked = 0;
130
integer num_tx_packets = 1;
131
 
132
integer rx_packet_lengths [0:1023]; // Array of packet lengths
133
 
134
 
135
integer speed_loop;
136
 
137
// When txen is (re)enabled, the tx bd pointer goes back to 0
138
always @(posedge ethmac_txen)
139
  expected_txbd = 0;
140
 
141
   reg  eth_stim_waiting;
142
 
143
initial
144
  begin
145
     #1;
146
     //lfsr = 32'h84218421; // Init pseudo lfsr
147
     lfsr = 32'h00700001; // Init pseudo lfsr
148
     lfsr_last_byte = 0;
149
 
150
     eth_stim_waiting = 1;
151 411 julius
     expected_rxbd = num_tx_bds; // init this here
152
 
153
     eth_stim_do_rx_reponse_to_tx = 0;
154 408 julius
 
155 411 julius
 
156 408 julius
     while (eth_stim_waiting) // Loop, waiting for enabling of MAC by software
157
       begin
158
          #100;
159
          // If RX enable and not TX enable...
160
          if(ethmac_rxen === 1'b1 & !(ethmac_txen===1'b1))
161
            begin
162
               if (eth_inject_errors)
163
                 begin
164 411 julius
                    do_rx_only_stim(16, 64, 0, 0);
165
                    do_rx_only_stim(128, 64, 1'b1, 8);
166
                    do_rx_only_stim(256, 64, 1'b1, 4);
167 408 julius
                    eth_stim_waiting = 0;
168
                 end
169
               else
170
                 begin
171 411 julius
                    //do_rx_only_stim(eth_stim_num_rx_only_num_packets, 
172
                    //eth_stim_num_rx_only_packet_size, 0, 0);
173
 
174
                    // Call packet send loop directly. No error injection.
175
                    send_packet_loop(eth_stim_num_rx_only_num_packets,
176
                                     eth_stim_num_rx_only_packet_size,
177
                                     eth_stim_num_rx_only_packet_size_change,
178
                                     eth_stim_num_rx_only_packet_size_change_amount,
179
                                     eth_phy0.eth_speed,     // Speed
180
                                     eth_stim_num_rx_only_IPG, // IPG
181
                               48'h0012_3456_789a, 48'h0708_090A_0B0C, 1,
182
                               0, 0);
183
 
184 408 julius
                    eth_stim_waiting = 0;
185
                 end
186
            end // if (ethmac_rxen === 1'b1 & !(ethmac_txen===1'b1))
187
          // If both RX and TX enabled
188
          else if (ethmac_rxen === 1'b1 & ethmac_txen===1'b1)
189
            begin
190 411 julius
               // Both enabled - let's wait for the first packet transmitted
191
               // to see what stimulus we should provide
192
               while (num_tx_packets==1)
193
                 #1000;
194
 
195
               $display("* ethmac RX/TX test request: %x", eth_phy0.tx_mem[0]);
196
 
197
               // Check the first received byte's value
198
                 case (eth_phy0.tx_mem[0])
199
                   0:
200
                     begin
201
                        // kickoff call/response here
202
                        eth_stim_do_rx_reponse_to_tx = 1;
203
                     end
204
                   default:
205
                     begin
206
                        do_rx_while_tx_stim(1400);
207
                     end
208
                 endcase // case (eth_phy0.tx_mem[0])
209
 
210 408 julius
               eth_stim_waiting = 0;
211
            end
212
       end // while (eth_stim_waiting)     
213
 
214
  end // initial begin
215
 
216
   // Main Ethernet RX testing stimulus task.
217
   // Sends a set of packets at both speeds
218
   task do_rx_only_stim;
219
      input [31:0] num_packets;
220 411 julius
      input [31:0] start_packet_size;
221 408 julius
      input        inject_errors;
222
      input [31:0] inject_errors_mod;
223
 
224
      begin
225
 
226
         for(speed_loop=1;speed_loop<3;speed_loop=speed_loop+1)
227
           begin
228
 
229 411 julius
              send_packet_loop(num_packets, start_packet_size, 2'b01, 1,
230
                               speed_loop[0], 10000,
231 408 julius
                               48'h0012_3456_789a, 48'h0708_090A_0B0C, 1,
232
                               inject_errors, inject_errors_mod);
233
 
234
           end
235
 
236
      end
237
   endtask // do_rx_stim
238
 
239
   // Generate RX packets while there's TX going on
240
   // Sends a set of packets at both speeds
241
   task do_rx_while_tx_stim;
242
      input [31:0] num_packets;
243
      reg [31:0] IPG; // Inter-packet gap
244
      reg [31:0] packet_size;
245
 
246
      integer    j;
247
      begin
248
 
249
         for(j=0;j<num_packets;j=j+1)
250
           begin
251
              // Determine delay between RX packets:
252
 
253
              if (eth_stim_use_min_IPG)
254
                begin
255
                   // Assign based on whether we're in 100mbit or 10mbit mode
256
                   IPG = eth_phy0.eth_speed ? eth_stim_IPG_min_100mb :
257
                         eth_stim_IPG_min_10mb;
258
                   // Add a little bit of variability
259
                   // Add up to 15
260
                   IPG = IPG + ($random & 32'h000000f);
261
                end
262
              else
263
                begin
264
                   IPG = $random;
265
 
266
                   while (IPG > eth_stim_IPG_delay_max)
267
                     IPG = IPG / 2;
268
 
269
 
270
                end
271
              $display("do_rx_while_tx IPG = %0d", IPG);
272
              // Determine size of next packet:
273
              if (rx_while_tx_min_packet_size == max_eth_packet_size)
274
                // We want to transmit biggest packets possible, easy case
275
                packet_size = max_eth_packet_size - 4;
276
              else
277
                begin
278
                   // Constrained random sized packets
279
                   packet_size = $random;
280
 
281
                   while (packet_size > (max_eth_packet_size-4))
282
                     packet_size = packet_size / 2;
283
 
284
                   // Now divide by least significant bits of j
285
                   packet_size = packet_size / {29'd0,j[1:0],1'b1};
286
                   if (packet_size < 60)
287
                     packet_size = packet_size + 60;
288
                end
289
 
290
              $display("do_rx_while_tx packet_size = %0d", packet_size);
291
              send_packet_loop(1, packet_size, 2'b01, 1, eth_phy0.eth_speed,
292
                               IPG, 48'h0012_3456_789a,
293
                               48'h0708_090A_0B0C, 1, 1'b0, 0);
294
 
295
              // If RX enable went low, wait for it go high again
296
              if (ethmac_rxen===1'b0)
297
                begin
298
 
299
                   while (ethmac_rxen===1'b0)
300
                     begin
301
                        @(posedge ethmac_rxen);
302
                        #10000;
303
                     end
304
 
305
                   // RX disabled and when re-enabled we reset the buffer descriptor number
306
                   expected_rxbd = num_tx_bds;
307
 
308
                end
309
 
310
           end // for (j=0;j<num_packets;j=j+1)
311
      end
312
   endtask // do_rx_stim
313
 
314 411 julius
   // Registers used in detecting transmitted packets
315
   reg eth_stim_tx_loop_keep_polling;
316
   reg [31:0] ethmac_txbd_lenstat, ethmac_last_txbd_lenstat;
317
   reg        eth_stim_detected_packet_tx;
318
 
319
   // If in call-response mode, whenever we receive a TX packet, we generate
320
   // one and send it back
321
   always @(negedge eth_stim_detected_packet_tx)
322
     begin
323
        if (eth_stim_do_rx_reponse_to_tx & ethmac_rxen)
324
          // Continue if we are enabled
325
          do_rx_response_to_tx();
326
     end
327
 
328
   // Generate RX packet in rsponse to TX packet
329
   task do_rx_response_to_tx;
330
      //input unused;
331
 
332
     reg [31:0] IPG; // Inter-packet gap
333
      reg [31:0] packet_size;
334
 
335
      integer    j;
336
      begin
337
 
338
         // Get packet size test wants us to send
339
         packet_size = {eth_phy0.tx_mem[0],eth_phy0.tx_mem[1],
340
                        eth_phy0.tx_mem[2],eth_phy0.tx_mem[3]};
341
 
342
 
343
         IPG = {eth_phy0.tx_mem[4],eth_phy0.tx_mem[5],
344
                eth_phy0.tx_mem[6],eth_phy0.tx_mem[7]};
345
 
346
 
347
         $display("do_rx_response_to_tx IPG = %0d", IPG);
348
         if (packet_size == 0)
349
           begin
350
              // Constrained random sized packets
351
              packet_size = $random;
352
 
353
              while (packet_size > (max_eth_packet_size-4))
354
                packet_size = packet_size / 2;
355
 
356
              if (packet_size < 60)
357
                packet_size = packet_size + 60;
358
           end
359
 
360
         $display("do_rx_response_to_tx packet_size = %0d", packet_size);
361
         send_packet_loop(1, packet_size, 2'b01, 1, eth_phy0.eth_speed,
362
                          IPG, 48'h0012_3456_789a,
363
                          48'h0708_090A_0B0C, 1, 1'b0, 0);
364
 
365
         // If RX enable went low, wait for it go high again
366
         if (ethmac_rxen===1'b0)
367
           begin
368
 
369
              while (ethmac_rxen===1'b0)
370
                begin
371
                   @(posedge ethmac_rxen);
372
                   #10000;
373
                end
374
 
375
              // RX disabled and when re-enabled we reset the buffer 
376
              // descriptor number
377
              expected_rxbd = num_tx_bds;
378
 
379
           end
380
 
381
      end
382
   endtask // do_rx_response_to_tx
383
 
384
 
385
 
386
 
387
 
388 408 julius
   //
389
   // always@() to check the TX buffer descriptors
390
   //
391
   always @(posedge ethmac_txen)
392
     begin
393 411 julius
         ethmac_last_txbd_lenstat = 0;
394
         eth_stim_tx_loop_keep_polling=1;
395 408 julius
         // Wait on the TxBD Ready bit
396 411 julius
         while(eth_stim_tx_loop_keep_polling)
397 408 julius
           begin
398
              #10;
399 411 julius
              get_bd_lenstat(expected_txbd, ethmac_txbd_lenstat);
400 408 julius
              // Check if we've finished transmitting this BD
401 411 julius
              if (!ethmac_txbd_lenstat[15] & ethmac_last_txbd_lenstat[15])
402 408 julius
                // Falling edge of TX BD Ready
403 411 julius
                eth_stim_detected_packet_tx = 1;
404 408 julius
 
405 411 julius
              ethmac_last_txbd_lenstat = ethmac_txbd_lenstat;
406 408 julius
 
407
              // If TX en goes low then exit
408
              if (!ethmac_txen)
409 411 julius
                eth_stim_tx_loop_keep_polling = 0;
410
              else if (eth_stim_detected_packet_tx)
411 408 julius
                begin
412
                   // Wait until the eth_phy has finished receiving it
413
                   while (eth_phy0.mtxen_i === 1'b1)
414
                     #10;
415
 
416
                   $display("(%t) Check TX packet: bd %d: 0x%h",$time,
417 411 julius
                            expected_txbd, ethmac_txbd_lenstat);
418 408 julius
 
419
                   // Check the TXBD, see if the packet transmitted OK
420 411 julius
                   if (ethmac_txbd_lenstat[8] | ethmac_txbd_lenstat[3])
421 408 julius
                     begin
422
                        // Error occured
423
                        `TIME;
424
                        $display("*E TX Error of packet %0d detected.",
425
                                 num_tx_packets);
426
                        $display(" TX BD %0d = 0x%h", expected_txbd,
427 411 julius
                                 ethmac_txbd_lenstat);
428
                        if (ethmac_txbd_lenstat[8])
429 408 julius
                          $display(" Underrun in MAC during TX");
430 411 julius
                        if (ethmac_txbd_lenstat[3])
431 408 julius
                          $display(" Retransmission limit hit");
432
 
433
                        $finish;
434
                     end
435
                   else
436
                     begin
437 411 julius
                        // Packet was OK, let's compare the contents we 
438
                        // received with those that were meant to be transmitted
439 408 julius
                        if (eth_stim_check_tx_packet_contents)
440
                          begin
441
                             check_tx_packet(expected_txbd);
442
                             expected_txbd = (expected_txbd + 1) &
443
                                             num_tx_bds_mask;
444
                             num_tx_packets = num_tx_packets + 1;
445 411 julius
                             eth_stim_detected_packet_tx = 0;
446 408 julius
                          end
447
                     end
448
                end
449 411 julius
           end // while (eth_stim_tx_loop_keep_polling)
450 408 julius
     end // always @ (posedge ethmac_txen)
451
 
452
 
453
 
454
   //
455
   // Check packet TX'd by MAC was good
456
   // 
457
   task check_tx_packet;
458
      input [31:0] tx_bd_num;
459
 
460
      reg [31:0]   tx_bd_addr;
461
      reg [7:0]    phy_byte;
462
 
463
      reg [31:0]   txpnt_wb; // Pointer in array to where data should be
464 411 julius
      reg [24:0]   txpnt_sdram; // Index in array of shorts for data in SDRAM 
465
                                // part
466 408 julius
      reg [21:0]   buffer;
467 439 julius
      reg [7:0]    destram_byte;
468 408 julius
      reg [31:0]   tx_len_bd;
469
 
470 439 julius
      integer      i,j;
471 408 julius
      integer      failure;
472
      begin
473
         failure = 0;
474
 
475
         get_bd_lenstat(tx_bd_num, tx_len_bd);
476
 
477
         tx_len_bd = {15'd0,tx_len_bd[31:16]};
478
 
479
         // Check, if length didn't have to be padded, that
480
         // amount transmitted was correct
481
         if ((tx_len_bd > 60)&(tx_len_bd != (eth_phy0.tx_len-4)))
482
           begin
483
              $display("*E TX packet sent length, %0d != length in TX BD, %0d",
484
                       eth_phy0.tx_len-4, tx_len_bd);
485
              #100;
486
              $finish;
487
           end
488
 
489
         get_bd_addr(tx_bd_num, tx_bd_addr);
490
 
491
         // We're never going to be using more than about 256K of receive buffer
492
         // so let's lop off the top bit of the address pointer - we only want
493
         // the offset from the base of the memory bank
494
         txpnt_wb = {14'd0,tx_bd_addr[17:0]};
495
         txpnt_sdram = tx_bd_addr[24:0];
496
 
497
         // Variable we'll use for index in the PHY's TX buffer
498
         buffer = 0; // Start of TX data
499 439 julius
`ifdef RAM_WB
500 408 julius
         for (i=0;i<tx_len_bd;i=i+1)
501
           begin
502
              //$display("Checking address in tx bd 0x%0h",txpnt_sdram);
503
 
504 439 julius
              destram_byte = dut.ram_wb0.ram_wb_b3_0.get_byte(txpnt_sdram);
505
 
506
              phy_byte = eth_phy0.tx_mem[buffer];
507
 
508
              // Debugging output
509
              //$display("txpnt_sdram = 0x%h, destram_byte = 0x%h, buffer = 0x%h,
510
              //phy_byte = 0x%h", txpnt_sdram,  destram_byte, buffer, phy_byte);
511
 
512
              if (phy_byte !== destram_byte)
513
                begin
514
                   `TIME;
515
                   $display("*E Wrong byte (%d) of TX packet! ram = %h, phy = %h",buffer, destram_byte, phy_byte);
516
                   failure = 1;
517
                end
518
 
519
              buffer = buffer + 1;
520
 
521
              txpnt_sdram = txpnt_sdram+1;
522
 
523
           end // for (i=0;i<tx_len_bd;i=i+1)
524
`else
525
 `ifdef VERSATILE_SDRAM
526
         for (i=0;i<tx_len_bd;i=i+1)
527
           begin
528
              //$display("Checking address in tx bd 0x%0h",txpnt_sdram);
529
 
530
              sdram0.get_byte(txpnt_sdram,destram_byte);
531 408 julius
 
532
              phy_byte = eth_phy0.tx_mem[buffer];
533
              // Debugging output
534 439 julius
              //$display("txpnt_sdram = 0x%h, destram_byte = 0x%h, buffer = 0x%h, phy_byte = 0x%h", txpnt_sdram,  destram_byte, buffer, phy_byte);
535
              if (phy_byte !== destram_byte)
536 408 julius
                begin
537
                   `TIME;
538 439 julius
                   $display("*E Wrong byte (%d) of TX packet! ram = %h, phy = %h",buffer, destram_byte, phy_byte);
539 408 julius
                   failure = 1;
540
                end
541
 
542
              buffer = buffer + 1;
543
 
544
              txpnt_sdram = txpnt_sdram+1;
545
 
546
           end // for (i=0;i<tx_len_bd;i=i+1)
547
 
548 439 julius
 `else // !`ifdef VERSATILE_SDRAM
549
 
550
         $display("eth_stim.v: CANNOT INSPECT RAM. PLEASE CONFIGURE CORRECTLY");
551 408 julius
         $display("RAM pointer for BD is 0x%h, bank offset we'll use is 0x%h",
552
                  tx_bd_addr, txpnt_wb);
553
         $finish;
554 439 julius
 `endif // !`ifdef VERSATILE_SDRAM
555
`endif // !`ifdef RAM_WB         
556
 
557 408 julius
         if (failure)
558
           begin
559
              #100
560
                `TIME;
561
              $display("*E Error transmitting packet %0d (%0d bytes). Finishing simulation", num_tx_packets, tx_len_bd);
562
              get_bd_lenstat(tx_bd_num, tx_len_bd);
563
              $display("   TXBD lenstat: 0x%0h",tx_len_bd);
564
              $display("   TXBD address: 0x%0h",tx_bd_addr);
565
              $finish;
566
           end
567
         else
568
           begin
569
              #1 $display( "(%0t)(%m) TX packet %0d: %0d bytes in memory OK!",$time,num_tx_packets, tx_len_bd);
570
 
571
           end
572
 
573
 
574
      end
575
   endtask // check_tx_packet
576
 
577
   //
578
   // Task to send a set of packets
579
   //
580
   task send_packet_loop;
581
      input [31:0] num_packets;
582
      input [31:0] length;
583
      input [1:0]  length_change; // 0 = none, 1 = incr, 2 = decrement
584
      input [31:0] length_change_size; // Size to change by
585
      input        speed;
586
      input [31:0] back_to_back_delay; // #delay setting between packets
587
      input [47:0] dst_mac;
588
      input [47:0] src_mac;
589
      input        random_fill;
590
      input        random_errors;
591
      input [31:0] random_error_mod;
592
      integer      j;
593
      reg          error_this_time;
594
      integer      error_type; // 0 = rxerr, 1=bad preamble 2=bad crc 3=TODO
595
      reg [31:0]   rx_bd_lenstat;
596
      begin
597
         error_type = 0;
598
         error_this_time = 0;
599 411 julius
 
600
         if (num_packets == 0)
601
           // Loop forever when num_packets is 0
602
           num_packets = 32'h7fffffff;
603 408 julius
 
604 411 julius
 
605 408 julius
         if (speed & !(eth_phy0.control_bit14_10[13] === 1'b1))
606
           begin
607
              // write to phy's control register for 100Mbps
608
              eth_phy0.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
609
              // Swapping speeds, give some delay
610
              #10000;
611
           end
612
         else if (!speed & !(eth_phy0.control_bit14_10[13] === 1'b0))
613
           begin
614
              eth_phy0.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
615
              // Swapping speeds, give some delay
616
              #10000;
617
           end
618
 
619
         eth_phy0.control_bit8_0   = 9'h1_00;
620
 
621
         for(j=0;j<num_packets | length <32;j=j+1)
622
           begin
623
              eth_stim_rx_packet_length = length[15:0]; // Bytes
624
              st_data = 8'h0F;
625
 
626
              // setup RX packet in buffer - length is without CRC
627
              set_rx_packet(0, eth_stim_rx_packet_length, 1'b0, dst_mac,
628
                            src_mac, 16'h0D0E, st_data, random_fill);
629
 
630
              set_rx_addr_type(0, dst_mac, src_mac, 16'h0D0E);
631
 
632
              // Error type 2 is cause CRC error
633
              append_rx_crc(0, eth_stim_rx_packet_length, 1'b0,
634
                            (error_type==2));
635
 
636
              if (error_this_time)
637
                begin
638
                   if (error_type == 0)
639
                     // RX ERR assert during transmit
640
                     eth_phy0.send_rx_packet(64'h0055_5555_5555_5555, 4'h7,
641
                                             8'hD5, 0,
642
                                             eth_stim_rx_packet_length+4,
643
                                             1'b0, 1'b1);
644
                   else if (error_type == 1)
645
                     // Incorrect preamble
646
                     eth_phy0.send_rx_packet(64'h0055_5f55_5555_5555, 4'h7,
647
                                             8'hD5, 0,
648
                                             eth_stim_rx_packet_length+4,
649
                                             1'b0, 1'b0);
650
                   else
651
                     // Normal datapacket
652
                     eth_phy0.send_rx_packet(64'h0055_5555_5555_5555, 4'h7,
653
                                             8'hD5, 0,
654
                                             eth_stim_rx_packet_length+4,
655
                                             1'b0, 1'b0);
656
                end
657
              else
658
                eth_phy0.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5,
659
                                        0, eth_stim_rx_packet_length+4, 1'b0,
660
                                        1'b0);
661
 
662
 
663
              // if RX enable still set (might have gone low during this packet
664
              if (ethmac_rxen)
665
                begin
666
                   if (error_this_time)
667
                     // Put in dummy length, checking function will skip...
668
                     rx_packet_lengths[(eth_rx_num_packets_sent& 12'h3ff)]=32'heeeeeeee;
669
                   else
670
                     rx_packet_lengths[(eth_rx_num_packets_sent & 12'h3ff)] = length;
671
 
672
                   eth_rx_num_packets_sent = eth_rx_num_packets_sent + 1;
673
 
674
                end // if (ethmac_rxen)
675
              else
676
                begin
677
                   // Force the loop to finish up                  
678
                   j = num_packets;
679
                end
680
 
681
 
682
              // Inter-packet gap
683
              #back_to_back_delay;
684
 
685
              // Update length
686
              if (length_change == 2'b01)
687
                length = length + length_change_size;
688
 
689
              if ((length_change == 2'b10) &&
690
                  ((length - length_change_size) > 32))
691
                length = length - length_change_size;
692
 
693
              // Increment error type
694
              if (error_this_time)
695
                error_type = error_type + 1;
696
              if (error_type > 3)
697
                error_type = 0;
698
 
699
 
700
              // Check if we should put in an error this time
701
              if (j%random_error_mod == 0)
702
                error_this_time = 1;
703
              else
704
                error_this_time = 0;
705
 
706
              eth_phy0.rx_err(0);
707
 
708
              // Now wait to check if we have filled up all the RX BDs and
709
              // the this packet would start writing over them. Only really an
710
              // issue when doing minimum IPG tests.
711
              while(((eth_rx_num_packets_sent+1) - eth_rx_num_packets_checked)
712
                    == num_rx_bds)
713
                #100;
714
 
715
 
716
           end // for (j=0;j<num_packets | length <32;j=j+1)
717
      end
718
   endtask // send_packet_loop
719
 
720
   // Local buffer of "sent" data to the ethernet MAC, we will check against
721
   // Size of our local buffer in bytes
722
   parameter eth_rx_sent_circbuf_size = (16*1024);
723
   parameter eth_rx_sent_circbuf_size_mask = eth_rx_sent_circbuf_size - 1;
724
   integer eth_rx_sent_circbuf_fill_ptr = 0;
725
   integer eth_rx_sent_circbuf_read_ptr = 0;
726
   // The actual buffer
727
   reg [7:0] eth_rx_sent_circbuf [0:eth_rx_sent_circbuf_size-1];
728
 
729
   /*
730
    TASKS for set and check RX packets:
731
    -----------------------------------
732
    set_rx_packet
733
    (rxpnt[31:0], len[15:0], plus_nibble, d_addr[47:0], s_addr[47:0], type_len[15:0], start_data[7:0]);
734
    check_rx_packet
735
    (rxpnt_phy[31:0], rxpnt_wb[31:0], len[15:0], plus_nibble, successful_nibble, failure[31:0]);
736
    */
737
   task set_rx_packet;
738
      input  [31:0] rxpnt; // pointer to place in in the phy rx buffer we'll start at
739
      input [15:0]  len;
740
      input         plus_dribble_nibble; // if length is longer for one nibble
741
      input [47:0]  eth_dest_addr;
742
      input [47:0]  eth_source_addr;
743
      input [15:0]  eth_type_len;
744
      input [7:0]   eth_start_data;
745
      input         random_fill;
746
      integer       i, sd;
747
      reg [47:0]    dest_addr;
748
      reg [47:0]    source_addr;
749
      reg [15:0]    type_len;
750
      reg [21:0]    buffer;
751
      reg           delta_t;
752
 
753
      begin
754
         buffer = rxpnt[21:0];
755
         dest_addr = eth_dest_addr;
756
         source_addr = eth_source_addr;
757
         type_len = eth_type_len;
758
         sd = eth_start_data;
759
         delta_t = 0;
760
         for(i = 0; i < len; i = i + 1)
761
           begin
762
              if (i < 6)
763
                begin
764
                   eth_phy0.rx_mem[buffer] = dest_addr[47:40];
765
                   dest_addr = dest_addr << 8;
766
                end
767
              else if (i < 12)
768
                begin
769
                   eth_phy0.rx_mem[buffer] = source_addr[47:40];
770
                   source_addr = source_addr << 8;
771
                end
772
              else if (i < 14)
773
                begin
774
                   eth_phy0.rx_mem[buffer] = type_len[15:8];
775
                   type_len = type_len << 8;
776
                end
777
              else
778
                begin
779
                   if (random_fill)
780
                     begin
781
                        if (lfsr_last_byte == 0)
782
                          eth_phy0.rx_mem[buffer] = lfsr[15:8];
783
                        if (lfsr_last_byte == 1)
784
                          eth_phy0.rx_mem[buffer] = lfsr[23:16];
785
                        if (lfsr_last_byte == 2)
786
                          eth_phy0.rx_mem[buffer] = lfsr[31:24];
787
                        if (lfsr_last_byte == 3)
788
                          begin
789
                             eth_phy0.rx_mem[buffer] = lfsr[7:0];
790
                             lfsr = {lfsr[30:0],(((lfsr[31] ^ lfsr[6]) ^
791
                                                  lfsr[5]) ^ lfsr[1])};
792
                             lfsr_last_byte =  0;
793
                          end
794
                        else
795
                          lfsr_last_byte = lfsr_last_byte + 1;
796
 
797
                     end // if (random_fill)               
798
                   else
799
                     eth_phy0.rx_mem[buffer] = sd[7:0];
800
                   sd = sd + 1;
801
                end // else: !if(i < 14)
802
 
803
              // Update our local buffer
804
              eth_rx_sent_circbuf[eth_rx_sent_circbuf_fill_ptr]
805
                = eth_phy0.rx_mem[buffer];
806
              eth_rx_sent_circbuf_fill_ptr = (eth_rx_sent_circbuf_fill_ptr+1)&
807
                                             eth_rx_sent_circbuf_size_mask;
808
 
809
              buffer = buffer + 1;
810
           end // for (i = 0; i < len; i = i + 1)
811
 
812
         delta_t = !delta_t;
813
         if (plus_dribble_nibble)
814
           eth_phy0.rx_mem[buffer] = {4'h0, 4'hD /*sd[3:0]*/};
815
         delta_t = !delta_t;
816
      end
817
   endtask // set_rx_packet
818
 
819
 
820
 
821
 
822
   task set_rx_addr_type;
823
      input  [31:0] rxpnt;
824
      input [47:0]  eth_dest_addr;
825
      input [47:0]  eth_source_addr;
826
      input [15:0]  eth_type_len;
827
      integer       i;
828
      reg [47:0]    dest_addr;
829
      reg [47:0]    source_addr;
830
      reg [15:0]    type_len;
831
      reg [21:0]    buffer;
832
      reg           delta_t;
833
      begin
834
         buffer = rxpnt[21:0];
835
         dest_addr = eth_dest_addr;
836
         source_addr = eth_source_addr;
837
         type_len = eth_type_len;
838
         delta_t = 0;
839
         for(i = 0; i < 14; i = i + 1)
840
           begin
841
              if (i < 6)
842
                begin
843
                   eth_phy0.rx_mem[buffer] = dest_addr[47:40];
844
                   dest_addr = dest_addr << 8;
845
                end
846
              else if (i < 12)
847
                begin
848
                   eth_phy0.rx_mem[buffer] = source_addr[47:40];
849
                   source_addr = source_addr << 8;
850
                end
851
              else // if (i < 14)
852
                begin
853
                   eth_phy0.rx_mem[buffer] = type_len[15:8];
854
                   type_len = type_len << 8;
855
                end
856
              buffer = buffer + 1;
857
           end
858
         delta_t = !delta_t;
859
      end
860
   endtask // set_rx_addr_type
861
 
862
 
863 411 julius
   // Check if we're using a synthesized version of eth module
864
`ifdef ethmac_IS_GATELEVEL
865
 
866 408 julius
   // Get the length/status register of the ethernet buffer descriptor
867
   task get_bd_lenstat;
868
      input [31:0] bd_num;// Number of ethernet BD to check
869
      output [31:0] bd_lenstat;
870
 `ifdef ACTEL
871
      reg [8:0]    tmp;
872
      integer      raddr;
873
 `endif
874
      begin
875
 `ifdef ACTEL
876
 
877
         // Pull from the Actel memory model
878
         raddr = `ETH_BD_RAM_PATH.\mem_tile.I_1 .get_address((bd_num*2));
879
 
880
         tmp = `ETH_BD_RAM_PATH.\mem_tile.I_1 .MEM_512_9[(raddr*2)];
881
         bd_lenstat[8:0] = tmp[8:0];
882
 
883
         tmp = `ETH_BD_RAM_PATH.\mem_tile.I_1 .MEM_512_9[(raddr*2)+1];
884
         bd_lenstat[17:9] = tmp[8:0];
885
 
886
         raddr = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .get_address((bd_num*2));
887
 
888
         tmp = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .MEM_512_9[(raddr*2)];
889
         bd_lenstat[26:18] = tmp[8:0];
890
 
891
         tmp = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .MEM_512_9[(raddr*2)+1];
892
         bd_lenstat[31:27] = tmp[4:0];
893
 
894
         //$display("(%t) read eth bd lenstat %h",$time, bd_lenstat);
895
 `endif
896
      end
897
   endtask // get_bd_lenstat
898
 
899
   // Get the length/status register of the ethernet buffer descriptor
900
   task get_bd_addr;
901
      input [31:0] bd_num;// Number of the ethernet BD to check
902
      output [31:0] bd_addr;
903
 `ifdef ACTEL
904
      reg [8:0]    tmp;
905
      integer       raddr;
906
 `endif
907
      begin
908
 `ifdef ACTEL
909
         // Pull from the Actel memory model
910
         raddr = `ETH_BD_RAM_PATH.\mem_tile.I_1 .get_address((bd_num*2)+1);
911
 
912
         tmp = `ETH_BD_RAM_PATH.\mem_tile.I_1 .MEM_512_9[(raddr*2)];
913
         bd_addr[8:0] = tmp[8:0];
914
 
915
         tmp = `ETH_BD_RAM_PATH.\mem_tile.I_1 .MEM_512_9[(raddr*2)+1];
916
         bd_addr[17:9] = tmp[8:0];
917
 
918
         raddr = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .get_address((bd_num*2)+1);
919
 
920
         tmp = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .MEM_512_9[(raddr*2)];
921
         bd_addr[26:18] = tmp[8:0];
922
 
923
         tmp = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .MEM_512_9[(raddr*2)+1];
924
         bd_addr[31:27] = tmp[4:0];
925
 
926
         //$display("(%t) read eth bd%d addr %h",$time,bd_num, bd_addr);
927
 `endif
928
      end
929
   endtask // get_bd_addr
930
 
931 411 julius
`else // !`ifdef ethmac_IS_GATELEVEL
932 408 julius
 
933
   // Get the length/status register of the ethernet buffer descriptor
934
   task get_bd_lenstat;
935
      input [31:0] bd_num;// Number of ethernet BD to check
936
      output [31:0] bd_lenstat;
937
      begin
938
         bd_lenstat = `ETH_BD_RAM_PATH.mem[(bd_num*2)];
939
      end
940
   endtask // get_bd_lenstat
941
 
942
   // Get the length/status register of the ethernet buffer descriptor
943
   task get_bd_addr;
944
      input [31:0] bd_num;// Number of the ethernet BD to check
945
      output [31:0] bd_addr;
946
      begin
947
         bd_addr = `ETH_BD_RAM_PATH.mem[((bd_num*2)+1)];
948
         //$display("(%t) read eth bd%d addr %h",$time,bd_num, bd_addr);
949
      end
950
   endtask // get_bd_addr
951
`endif
952
 
953
   // Always block triggered by finishing of transmission of new packet from 
954
   // send_packet_loop
955
   integer eth_rx_packet_length_to_check;
956
 
957
   always @*
958
     begin
959 411 julius
        // Loop here until:
960
        // 1 - packets sent is not equal to packets checked (ie. some to check)
961
        // 2 - we're explicitly disabled for some reason
962
        // 3 - Receive has been disabled in the MAC
963 408 julius
        while((eth_rx_num_packets_sent == eth_rx_num_packets_checked) ||
964 411 julius
              !eth_stim_check_rx_packet_contents || !(ethmac_rxen===1'b1))
965 408 julius
          #1000;
966 411 julius
 
967 408 julius
        eth_rx_packet_length_to_check
968
          = rx_packet_lengths[(eth_rx_num_packets_checked & 12'h3ff)];
969
 
970
        if ( eth_rx_packet_length_to_check !==  32'heeeeeeee)
971
          check_rx_packet(expected_rxbd, 0, eth_rx_packet_length_to_check);
972
 
973
        eth_rx_num_packets_checked = eth_rx_num_packets_checked + 1;
974
 
975
        expected_rxbd = expected_rxbd + 1;
976
 
977
        // Wrap
978
        if (expected_rxbd == (num_tx_bds + num_rx_bds))
979
          expected_rxbd = num_tx_bds;
980
     end
981
 
982
   task check_rx_packet;
983
 
984
      input [31:0] rx_bd_num;
985
      input [31:0] rxpnt_phy; // Pointer in array of data in PHY
986
      input [31:0] len;
987
 
988
      reg [31:0]   rx_bd_lenstat;
989
      reg [31:0]   rx_bd_addr;
990
      reg [7:0]    phy_byte;
991
 
992
      reg [31:0]   rxpnt_wb; // Pointer in array to where data should be
993
      reg [24:0]   rxpnt_sdram; // byte address from CPU in RAM
994
      reg [15:0]   sdram_short;
995 439 julius
      reg [7:0]    destram_byte;
996 408 julius
 
997
      integer      i;
998
      integer      failure;
999
 
1000
      begin
1001
 
1002
         failure = 0;
1003
 
1004
         // Wait until the buffer descriptor indicates the packet has been 
1005
         // received...
1006
         get_bd_lenstat(rx_bd_num, rx_bd_lenstat);
1007
         while (rx_bd_lenstat & 32'h00008000)// Check Empty bit
1008
           begin
1009
              #10;
1010
              get_bd_lenstat(rx_bd_num, rx_bd_lenstat);
1011
              //$display("(%t) check_rx_packet: poll bd %d: 0x%h",$time,
1012
                //        rx_bd_num, rx_bd_lenstat);
1013
           end
1014
 
1015
 
1016
         // Delay some time - takes a bit for the Wishbone FSM to pipe out the
1017
         // packet over Wishbone and into whatever memory it's going into
1018
         #Td_rx_packet_check;
1019
 
1020
         // Ok, buffer filled, let's get its offset in memory
1021
         get_bd_addr(rx_bd_num, rx_bd_addr);
1022
 
1023
         $display("(%t) Check RX packet: bd %d: 0x%h, addr 0x%h",$time,
1024
                  rx_bd_num, rx_bd_lenstat, rx_bd_addr);
1025
 
1026
 
1027
         // We're never going to be using more than about 256KB of receive buffer
1028
         // so let's lop off the top bit of the address pointer - we only want
1029
         // the offset from the base of the memory bank
1030
 
1031
         rxpnt_wb = {14'd0,rx_bd_addr[17:0]};
1032
         rxpnt_sdram = rx_bd_addr[24:0];
1033 439 julius
 
1034
 
1035
`ifdef RAM_WB
1036
         for (i=0;i<len;i=i+1)
1037
           begin
1038
 
1039
              destram_byte = dut.ram_wb0.ram_wb_b3_0.get_byte(rxpnt_sdram);
1040
 
1041
              phy_byte = eth_rx_sent_circbuf[eth_rx_sent_circbuf_read_ptr];
1042
 
1043
              if (phy_byte !== destram_byte)
1044
                begin
1045
                   $display("*E Wrong byte (%5d) of RX packet %5d! phy = %h, ram = %h",
1046
                            i, eth_rx_num_packets_checked, phy_byte, destram_byte);
1047
                   failure = 1;
1048
                end
1049
 
1050
              eth_rx_sent_circbuf_read_ptr = (eth_rx_sent_circbuf_read_ptr+1)&
1051
                                             eth_rx_sent_circbuf_size_mask;
1052
 
1053
              rxpnt_sdram = rxpnt_sdram+1;
1054
 
1055
           end
1056
`else
1057
 `ifdef VERSATILE_SDRAM
1058 408 julius
         // We'll look inside the SDRAM array
1059
         // Hard coded for the SDRAM buffer area to be from the halfway mark in
1060
         // memory (so starting in Bank2)
1061
         // We'll be passed the offset from the beginning of the buffer area
1062
         // in rxpnt_wb. This value will be in bytes.
1063
 
1064
         //$display("RAM pointer for BD is 0x%h, SDRAM addr is 0x%h", rx_bd_addr, rxpnt_sdram);
1065
 
1066
 
1067
         for (i=0;i<len;i=i+1)
1068
           begin
1069
 
1070 439 julius
              sdram0.get_byte(rxpnt_sdram,destram_byte);
1071 408 julius
 
1072
              phy_byte = eth_rx_sent_circbuf[eth_rx_sent_circbuf_read_ptr];//phy_rx_mem[buffer]; //eth_phy0.rx_mem[buffer];
1073
 
1074 439 julius
              if (phy_byte !== destram_byte)
1075 408 julius
                begin
1076 411 julius
//                 `TIME;                 
1077
                   $display("*E Wrong byte (%5d) of RX packet %5d! phy = %h, ram = %h",
1078 439 julius
                            i, eth_rx_num_packets_checked, phy_byte, destram_byte);
1079 408 julius
                   failure = 1;
1080
                end
1081
 
1082
              eth_rx_sent_circbuf_read_ptr = (eth_rx_sent_circbuf_read_ptr+1)&
1083
                                             eth_rx_sent_circbuf_size_mask;
1084
 
1085
              rxpnt_sdram = rxpnt_sdram+1;
1086
 
1087
           end // for (i=0;i<len;i=i+2)
1088 439 julius
 `else
1089 408 julius
 
1090 439 julius
         $display("eth_stim.v: CANNOT INSPECT RAM. PLEASE CONFIGURE CORRECTLY");
1091 408 julius
         $display("RAM pointer for BD is 0x%h, bank offset we'll use is 0x%h",
1092
                  rx_bd_addr, rxpnt_wb);
1093
         $finish;
1094
 
1095
 
1096 439 julius
 `endif // !`ifdef VERSATILE_SDRAM
1097
`endif // !`ifdef RAM_WB
1098
 
1099 408 julius
 
1100
         if (failure)
1101
           begin
1102
              #100
1103
                `TIME;
1104
              $display("*E Recieved packet %0d, length %0d bytes, had an error. Finishing simulation.", eth_rx_num_packets_checked, len);
1105
              $finish;
1106
           end
1107
         else
1108
           begin
1109
              #1 $display( "(%0t)(%m) RX packet %0d: %0d bytes in memory OK!",$time,eth_rx_num_packets_checked, len);
1110
 
1111
           end
1112
      end
1113
   endtask // check_rx_packet
1114
 
1115
 
1116
   //////////////////////////////////////////////////////////////
1117
   // Ethernet CRC Basic tasks
1118
   //////////////////////////////////////////////////////////////
1119
 
1120
   task append_rx_crc;
1121
      input  [31:0] rxpnt_phy; // source
1122
      input [15:0]  len; // length in bytes without CRC
1123
      input         plus_dribble_nibble; // if length is longer for one nibble
1124
      input         negated_crc; // if appended CRC is correct or not
1125
      reg [31:0]    crc;
1126
      reg [7:0]     tmp;
1127
      reg [31:0]    addr_phy;
1128
      reg           delta_t;
1129
      begin
1130
         addr_phy = rxpnt_phy + len;
1131
         delta_t = 0;
1132
         // calculate CRC from prepared packet
1133
         paralel_crc_phy_rx(rxpnt_phy, {16'h0, len}, plus_dribble_nibble, crc);
1134
         if (negated_crc)
1135
           crc = ~crc;
1136
         delta_t = !delta_t;
1137
 
1138
         if (plus_dribble_nibble)
1139
           begin
1140
              tmp = eth_phy0.rx_mem[addr_phy];
1141
              eth_phy0.rx_mem[addr_phy]     = {crc[27:24], tmp[3:0]};
1142
              eth_phy0.rx_mem[addr_phy + 1] = {crc[19:16], crc[31:28]};
1143
              eth_phy0.rx_mem[addr_phy + 2] = {crc[11:8], crc[23:20]};
1144
              eth_phy0.rx_mem[addr_phy + 3] = {crc[3:0], crc[15:12]};
1145
              eth_phy0.rx_mem[addr_phy + 4] = {4'h0, crc[7:4]};
1146
           end
1147
         else
1148
           begin
1149
              eth_phy0.rx_mem[addr_phy]     = crc[31:24];
1150
              eth_phy0.rx_mem[addr_phy + 1] = crc[23:16];
1151
              eth_phy0.rx_mem[addr_phy + 2] = crc[15:8];
1152
              eth_phy0.rx_mem[addr_phy + 3] = crc[7:0];
1153
           end
1154
      end
1155
   endtask // append_rx_crc
1156
 
1157
   task append_rx_crc_delayed;
1158
      input  [31:0] rxpnt_phy; // source
1159
      input [15:0]  len; // length in bytes without CRC
1160
      input         plus_dribble_nibble; // if length is longer for one nibble
1161
      input         negated_crc; // if appended CRC is correct or not
1162
      reg [31:0]    crc;
1163
      reg [7:0]     tmp;
1164
      reg [31:0]    addr_phy;
1165
      reg           delta_t;
1166
      begin
1167
         addr_phy = rxpnt_phy + len;
1168
         delta_t = 0;
1169
         // calculate CRC from prepared packet
1170
         paralel_crc_phy_rx(rxpnt_phy+4, {16'h0, len}-4, plus_dribble_nibble, crc);
1171
         if (negated_crc)
1172
           crc = ~crc;
1173
         delta_t = !delta_t;
1174
 
1175
         if (plus_dribble_nibble)
1176
           begin
1177
              tmp = eth_phy0.rx_mem[addr_phy];
1178
              eth_phy0.rx_mem[addr_phy]     = {crc[27:24], tmp[3:0]};
1179
              eth_phy0.rx_mem[addr_phy + 1] = {crc[19:16], crc[31:28]};
1180
              eth_phy0.rx_mem[addr_phy + 2] = {crc[11:8], crc[23:20]};
1181
              eth_phy0.rx_mem[addr_phy + 3] = {crc[3:0], crc[15:12]};
1182
              eth_phy0.rx_mem[addr_phy + 4] = {4'h0, crc[7:4]};
1183
           end
1184
         else
1185
           begin
1186
              eth_phy0.rx_mem[addr_phy]     = crc[31:24];
1187
              eth_phy0.rx_mem[addr_phy + 1] = crc[23:16];
1188
              eth_phy0.rx_mem[addr_phy + 2] = crc[15:8];
1189
              eth_phy0.rx_mem[addr_phy + 3] = crc[7:0];
1190
           end
1191
      end
1192
   endtask // append_rx_crc_delayed
1193
 
1194
 
1195
   // paralel CRC calculating for PHY RX
1196
   task paralel_crc_phy_rx;
1197
      input  [31:0] start_addr; // start address
1198
      input [31:0]  len; // length of frame in Bytes without CRC length
1199
      input         plus_dribble_nibble; // if length is longer for one nibble
1200
      output [31:0] crc_out;
1201
      reg [21:0]    addr_cnt; // only 22 address lines
1202
      integer       word_cnt;
1203
      integer       nibble_cnt;
1204
      reg [31:0]    load_reg;
1205
      reg           delta_t;
1206
      reg [31:0]    crc_next;
1207
      reg [31:0]    crc;
1208
      reg           crc_error;
1209
      reg [3:0]     data_in;
1210
      integer       i;
1211
      begin
1212
         #1 addr_cnt = start_addr[21:0];
1213
         word_cnt = 24; // 27; // start of the frame - nibble granularity (MSbit first)
1214
         crc = 32'hFFFF_FFFF; // INITIAL value
1215
         delta_t = 0;
1216
         // length must include 4 bytes of ZEROs, to generate CRC
1217
         // get number of nibbles from Byte length (2^1 = 2)
1218
         if (plus_dribble_nibble)
1219
           nibble_cnt = ((len + 4) << 1) + 1'b1; // one nibble longer
1220
         else
1221
           nibble_cnt = ((len + 4) << 1);
1222
         // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
1223
         load_reg[31:24] = eth_phy0.rx_mem[addr_cnt];
1224
         addr_cnt = addr_cnt + 1;
1225
         load_reg[23:16] = eth_phy0.rx_mem[addr_cnt];
1226
         addr_cnt = addr_cnt + 1;
1227
         load_reg[15: 8] = eth_phy0.rx_mem[addr_cnt];
1228
         addr_cnt = addr_cnt + 1;
1229
         load_reg[ 7: 0] = eth_phy0.rx_mem[addr_cnt];
1230
         addr_cnt = addr_cnt + 1;
1231
         while (nibble_cnt > 0)
1232
           begin
1233
              // wait for delta time
1234
              delta_t = !delta_t;
1235
              // shift data in
1236
 
1237
              if(nibble_cnt <= 8) // for additional 8 nibbles shift ZEROs in!
1238
                data_in[3:0] = 4'h0;
1239
              else
1240
 
1241
                data_in[3:0] = {load_reg[word_cnt], load_reg[word_cnt+1], load_reg[word_cnt+2], load_reg[word_cnt+3]};
1242
              crc_next[0]  = (data_in[0] ^ crc[28]);
1243
              crc_next[1]  = (data_in[1] ^ data_in[0] ^ crc[28]    ^ crc[29]);
1244
              crc_next[2]  = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]);
1245
              crc_next[3]  = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]);
1246
              crc_next[4]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[0];
1247
              crc_next[5]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[1];
1248
              crc_next[6]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[ 2];
1249
              crc_next[7]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[3];
1250
              crc_next[8]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[4];
1251
              crc_next[9]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[5];
1252
              crc_next[10] = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[6];
1253
              crc_next[11] = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[7];
1254
              crc_next[12] = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]) ^ crc[8];
1255
              crc_next[13] = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]) ^ crc[9];
1256
              crc_next[14] = (data_in[3] ^ data_in[2] ^ crc[30]    ^ crc[31]) ^ crc[10];
1257
              crc_next[15] = (data_in[3] ^ crc[31])   ^ crc[11];
1258
              crc_next[16] = (data_in[0] ^ crc[28])   ^ crc[12];
1259
              crc_next[17] = (data_in[1] ^ crc[29])   ^ crc[13];
1260
              crc_next[18] = (data_in[2] ^ crc[30])   ^ crc[14];
1261
              crc_next[19] = (data_in[3] ^ crc[31])   ^ crc[15];
1262
              crc_next[20] =  crc[16];
1263
              crc_next[21] =  crc[17];
1264
              crc_next[22] = (data_in[0] ^ crc[28])   ^ crc[18];
1265
              crc_next[23] = (data_in[1] ^ data_in[0] ^ crc[29]    ^ crc[28]) ^ crc[19];
1266
              crc_next[24] = (data_in[2] ^ data_in[1] ^ crc[30]    ^ crc[29]) ^ crc[20];
1267
              crc_next[25] = (data_in[3] ^ data_in[2] ^ crc[31]    ^ crc[30]) ^ crc[21];
1268
              crc_next[26] = (data_in[3] ^ data_in[0] ^ crc[31]    ^ crc[28]) ^ crc[22];
1269
              crc_next[27] = (data_in[1] ^ crc[29])   ^ crc[23];
1270
              crc_next[28] = (data_in[2] ^ crc[30])   ^ crc[24];
1271
              crc_next[29] = (data_in[3] ^ crc[31])   ^ crc[25];
1272
              crc_next[30] =  crc[26];
1273
              crc_next[31] =  crc[27];
1274
 
1275
              crc = crc_next;
1276
              crc_error = crc[31:0] != 32'hc704dd7b;  // CRC not equal to magic number
1277
              case (nibble_cnt)
1278
                9: crc_out = {!crc[24], !crc[25], !crc[26], !crc[27], !crc[28], !crc[29], !crc[30], !crc[31],
1279
                              !crc[16], !crc[17], !crc[18], !crc[19], !crc[20], !crc[21], !crc[22], !crc[23],
1280
                              !crc[ 8], !crc[ 9], !crc[10], !crc[11], !crc[12], !crc[13], !crc[14], !crc[15],
1281
                              !crc[ 0], !crc[ 1], !crc[ 2], !crc[ 3], !crc[ 4], !crc[ 5], !crc[ 6], !crc[ 7]};
1282
                default: crc_out = crc_out;
1283
              endcase
1284
              // wait for delta time
1285
              delta_t = !delta_t;
1286
              // increment address and load new data
1287
              if ((word_cnt+3) == 7)//4)
1288
                begin
1289
                   // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
1290
                   load_reg[31:24] = eth_phy0.rx_mem[addr_cnt];
1291
                   addr_cnt = addr_cnt + 1;
1292
                   load_reg[23:16] = eth_phy0.rx_mem[addr_cnt];
1293
                   addr_cnt = addr_cnt + 1;
1294
                   load_reg[15: 8] = eth_phy0.rx_mem[addr_cnt];
1295
                   addr_cnt = addr_cnt + 1;
1296
                   load_reg[ 7: 0] = eth_phy0.rx_mem[addr_cnt];
1297
                   addr_cnt = addr_cnt + 1;
1298
                end
1299
              // set new load bit position
1300
              if((word_cnt+3) == 31)
1301
                word_cnt = 16;
1302
              else if ((word_cnt+3) == 23)
1303
                word_cnt = 8;
1304
              else if ((word_cnt+3) == 15)
1305
                word_cnt = 0;
1306
              else if ((word_cnt+3) == 7)
1307
                word_cnt = 24;
1308
              else
1309
                word_cnt = word_cnt + 4;// - 4;
1310
              // decrement nibble counter
1311
              nibble_cnt = nibble_cnt - 1;
1312
              // wait for delta time
1313
              delta_t = !delta_t;
1314
           end // while
1315
         #1;
1316
      end
1317
   endtask // paralel_crc_phy_rx
1318
 
1319
 
1320
 
1321
 

powered by: WebSVN 2.1.0

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