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

Subversion Repositories openrisc_me

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

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
      reg [7:0]    sdram_byte;
468
      reg [31:0]   tx_len_bd;
469
 
470
      integer      i;
471
      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
`ifdef VERSATILE_SDRAM
500
         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
              sdram0.get_byte(txpnt_sdram,sdram_byte);
505
 
506
              phy_byte = eth_phy0.tx_mem[buffer];
507
              // Debugging output
508
              //$display("txpnt_sdram = 0x%h, sdram_byte = 0x%h, buffer = 0x%h, phy_byte = 0x%h", txpnt_sdram,  sdram_byte, buffer, phy_byte);
509
              if (phy_byte !== sdram_byte)
510
                begin
511
                   `TIME;
512
                   $display("*E Wrong byte (%d) of TX packet! ram = %h, phy = %h",buffer, sdram_byte, phy_byte);
513
                   failure = 1;
514
                end
515
 
516
              buffer = buffer + 1;
517
 
518
              txpnt_sdram = txpnt_sdram+1;
519
 
520
           end // for (i=0;i<tx_len_bd;i=i+1)
521
 
522
`else
523
         $display("SET ME UP TO LOOK IN ANOTHER MEMORY!");
524
         $display("RAM pointer for BD is 0x%h, bank offset we'll use is 0x%h",
525
                  tx_bd_addr, txpnt_wb);
526
         $finish;
527
`endif // !`ifdef VERSATILE_SDRAM
528
         if (failure)
529
           begin
530
              #100
531
                `TIME;
532
              $display("*E Error transmitting packet %0d (%0d bytes). Finishing simulation", num_tx_packets, tx_len_bd);
533
              get_bd_lenstat(tx_bd_num, tx_len_bd);
534
              $display("   TXBD lenstat: 0x%0h",tx_len_bd);
535
              $display("   TXBD address: 0x%0h",tx_bd_addr);
536
              $finish;
537
           end
538
         else
539
           begin
540
              #1 $display( "(%0t)(%m) TX packet %0d: %0d bytes in memory OK!",$time,num_tx_packets, tx_len_bd);
541
 
542
           end
543
 
544
 
545
      end
546
   endtask // check_tx_packet
547
 
548
   //
549
   // Task to send a set of packets
550
   //
551
   task send_packet_loop;
552
      input [31:0] num_packets;
553
      input [31:0] length;
554
      input [1:0]  length_change; // 0 = none, 1 = incr, 2 = decrement
555
      input [31:0] length_change_size; // Size to change by
556
      input        speed;
557
      input [31:0] back_to_back_delay; // #delay setting between packets
558
      input [47:0] dst_mac;
559
      input [47:0] src_mac;
560
      input        random_fill;
561
      input        random_errors;
562
      input [31:0] random_error_mod;
563
      integer      j;
564
      reg          error_this_time;
565
      integer      error_type; // 0 = rxerr, 1=bad preamble 2=bad crc 3=TODO
566
      reg [31:0]   rx_bd_lenstat;
567
      begin
568
         error_type = 0;
569
         error_this_time = 0;
570 411 julius
 
571
         if (num_packets == 0)
572
           // Loop forever when num_packets is 0
573
           num_packets = 32'h7fffffff;
574 408 julius
 
575 411 julius
 
576 408 julius
         if (speed & !(eth_phy0.control_bit14_10[13] === 1'b1))
577
           begin
578
              // write to phy's control register for 100Mbps
579
              eth_phy0.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
580
              // Swapping speeds, give some delay
581
              #10000;
582
           end
583
         else if (!speed & !(eth_phy0.control_bit14_10[13] === 1'b0))
584
           begin
585
              eth_phy0.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
586
              // Swapping speeds, give some delay
587
              #10000;
588
           end
589
 
590
         eth_phy0.control_bit8_0   = 9'h1_00;
591
 
592
         for(j=0;j<num_packets | length <32;j=j+1)
593
           begin
594
              eth_stim_rx_packet_length = length[15:0]; // Bytes
595
              st_data = 8'h0F;
596
 
597
              // setup RX packet in buffer - length is without CRC
598
              set_rx_packet(0, eth_stim_rx_packet_length, 1'b0, dst_mac,
599
                            src_mac, 16'h0D0E, st_data, random_fill);
600
 
601
              set_rx_addr_type(0, dst_mac, src_mac, 16'h0D0E);
602
 
603
              // Error type 2 is cause CRC error
604
              append_rx_crc(0, eth_stim_rx_packet_length, 1'b0,
605
                            (error_type==2));
606
 
607
              if (error_this_time)
608
                begin
609
                   if (error_type == 0)
610
                     // RX ERR assert during transmit
611
                     eth_phy0.send_rx_packet(64'h0055_5555_5555_5555, 4'h7,
612
                                             8'hD5, 0,
613
                                             eth_stim_rx_packet_length+4,
614
                                             1'b0, 1'b1);
615
                   else if (error_type == 1)
616
                     // Incorrect preamble
617
                     eth_phy0.send_rx_packet(64'h0055_5f55_5555_5555, 4'h7,
618
                                             8'hD5, 0,
619
                                             eth_stim_rx_packet_length+4,
620
                                             1'b0, 1'b0);
621
                   else
622
                     // Normal datapacket
623
                     eth_phy0.send_rx_packet(64'h0055_5555_5555_5555, 4'h7,
624
                                             8'hD5, 0,
625
                                             eth_stim_rx_packet_length+4,
626
                                             1'b0, 1'b0);
627
                end
628
              else
629
                eth_phy0.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5,
630
                                        0, eth_stim_rx_packet_length+4, 1'b0,
631
                                        1'b0);
632
 
633
 
634
              // if RX enable still set (might have gone low during this packet
635
              if (ethmac_rxen)
636
                begin
637
                   if (error_this_time)
638
                     // Put in dummy length, checking function will skip...
639
                     rx_packet_lengths[(eth_rx_num_packets_sent& 12'h3ff)]=32'heeeeeeee;
640
                   else
641
                     rx_packet_lengths[(eth_rx_num_packets_sent & 12'h3ff)] = length;
642
 
643
                   eth_rx_num_packets_sent = eth_rx_num_packets_sent + 1;
644
 
645
                end // if (ethmac_rxen)
646
              else
647
                begin
648
                   // Force the loop to finish up                  
649
                   j = num_packets;
650
                end
651
 
652
 
653
              // Inter-packet gap
654
              #back_to_back_delay;
655
 
656
              // Update length
657
              if (length_change == 2'b01)
658
                length = length + length_change_size;
659
 
660
              if ((length_change == 2'b10) &&
661
                  ((length - length_change_size) > 32))
662
                length = length - length_change_size;
663
 
664
              // Increment error type
665
              if (error_this_time)
666
                error_type = error_type + 1;
667
              if (error_type > 3)
668
                error_type = 0;
669
 
670
 
671
              // Check if we should put in an error this time
672
              if (j%random_error_mod == 0)
673
                error_this_time = 1;
674
              else
675
                error_this_time = 0;
676
 
677
              eth_phy0.rx_err(0);
678
 
679
              // Now wait to check if we have filled up all the RX BDs and
680
              // the this packet would start writing over them. Only really an
681
              // issue when doing minimum IPG tests.
682
              while(((eth_rx_num_packets_sent+1) - eth_rx_num_packets_checked)
683
                    == num_rx_bds)
684
                #100;
685
 
686
 
687
           end // for (j=0;j<num_packets | length <32;j=j+1)
688
      end
689
   endtask // send_packet_loop
690
 
691
   // Local buffer of "sent" data to the ethernet MAC, we will check against
692
   // Size of our local buffer in bytes
693
   parameter eth_rx_sent_circbuf_size = (16*1024);
694
   parameter eth_rx_sent_circbuf_size_mask = eth_rx_sent_circbuf_size - 1;
695
   integer eth_rx_sent_circbuf_fill_ptr = 0;
696
   integer eth_rx_sent_circbuf_read_ptr = 0;
697
   // The actual buffer
698
   reg [7:0] eth_rx_sent_circbuf [0:eth_rx_sent_circbuf_size-1];
699
 
700
   /*
701
    TASKS for set and check RX packets:
702
    -----------------------------------
703
    set_rx_packet
704
    (rxpnt[31:0], len[15:0], plus_nibble, d_addr[47:0], s_addr[47:0], type_len[15:0], start_data[7:0]);
705
    check_rx_packet
706
    (rxpnt_phy[31:0], rxpnt_wb[31:0], len[15:0], plus_nibble, successful_nibble, failure[31:0]);
707
    */
708
   task set_rx_packet;
709
      input  [31:0] rxpnt; // pointer to place in in the phy rx buffer we'll start at
710
      input [15:0]  len;
711
      input         plus_dribble_nibble; // if length is longer for one nibble
712
      input [47:0]  eth_dest_addr;
713
      input [47:0]  eth_source_addr;
714
      input [15:0]  eth_type_len;
715
      input [7:0]   eth_start_data;
716
      input         random_fill;
717
      integer       i, sd;
718
      reg [47:0]    dest_addr;
719
      reg [47:0]    source_addr;
720
      reg [15:0]    type_len;
721
      reg [21:0]    buffer;
722
      reg           delta_t;
723
 
724
      begin
725
         buffer = rxpnt[21:0];
726
         dest_addr = eth_dest_addr;
727
         source_addr = eth_source_addr;
728
         type_len = eth_type_len;
729
         sd = eth_start_data;
730
         delta_t = 0;
731
         for(i = 0; i < len; i = i + 1)
732
           begin
733
              if (i < 6)
734
                begin
735
                   eth_phy0.rx_mem[buffer] = dest_addr[47:40];
736
                   dest_addr = dest_addr << 8;
737
                end
738
              else if (i < 12)
739
                begin
740
                   eth_phy0.rx_mem[buffer] = source_addr[47:40];
741
                   source_addr = source_addr << 8;
742
                end
743
              else if (i < 14)
744
                begin
745
                   eth_phy0.rx_mem[buffer] = type_len[15:8];
746
                   type_len = type_len << 8;
747
                end
748
              else
749
                begin
750
                   if (random_fill)
751
                     begin
752
                        if (lfsr_last_byte == 0)
753
                          eth_phy0.rx_mem[buffer] = lfsr[15:8];
754
                        if (lfsr_last_byte == 1)
755
                          eth_phy0.rx_mem[buffer] = lfsr[23:16];
756
                        if (lfsr_last_byte == 2)
757
                          eth_phy0.rx_mem[buffer] = lfsr[31:24];
758
                        if (lfsr_last_byte == 3)
759
                          begin
760
                             eth_phy0.rx_mem[buffer] = lfsr[7:0];
761
                             lfsr = {lfsr[30:0],(((lfsr[31] ^ lfsr[6]) ^
762
                                                  lfsr[5]) ^ lfsr[1])};
763
                             lfsr_last_byte =  0;
764
                          end
765
                        else
766
                          lfsr_last_byte = lfsr_last_byte + 1;
767
 
768
                     end // if (random_fill)               
769
                   else
770
                     eth_phy0.rx_mem[buffer] = sd[7:0];
771
                   sd = sd + 1;
772
                end // else: !if(i < 14)
773
 
774
              // Update our local buffer
775
              eth_rx_sent_circbuf[eth_rx_sent_circbuf_fill_ptr]
776
                = eth_phy0.rx_mem[buffer];
777
              eth_rx_sent_circbuf_fill_ptr = (eth_rx_sent_circbuf_fill_ptr+1)&
778
                                             eth_rx_sent_circbuf_size_mask;
779
 
780
              buffer = buffer + 1;
781
           end // for (i = 0; i < len; i = i + 1)
782
 
783
         delta_t = !delta_t;
784
         if (plus_dribble_nibble)
785
           eth_phy0.rx_mem[buffer] = {4'h0, 4'hD /*sd[3:0]*/};
786
         delta_t = !delta_t;
787
      end
788
   endtask // set_rx_packet
789
 
790
 
791
 
792
 
793
   task set_rx_addr_type;
794
      input  [31:0] rxpnt;
795
      input [47:0]  eth_dest_addr;
796
      input [47:0]  eth_source_addr;
797
      input [15:0]  eth_type_len;
798
      integer       i;
799
      reg [47:0]    dest_addr;
800
      reg [47:0]    source_addr;
801
      reg [15:0]    type_len;
802
      reg [21:0]    buffer;
803
      reg           delta_t;
804
      begin
805
         buffer = rxpnt[21:0];
806
         dest_addr = eth_dest_addr;
807
         source_addr = eth_source_addr;
808
         type_len = eth_type_len;
809
         delta_t = 0;
810
         for(i = 0; i < 14; i = i + 1)
811
           begin
812
              if (i < 6)
813
                begin
814
                   eth_phy0.rx_mem[buffer] = dest_addr[47:40];
815
                   dest_addr = dest_addr << 8;
816
                end
817
              else if (i < 12)
818
                begin
819
                   eth_phy0.rx_mem[buffer] = source_addr[47:40];
820
                   source_addr = source_addr << 8;
821
                end
822
              else // if (i < 14)
823
                begin
824
                   eth_phy0.rx_mem[buffer] = type_len[15:8];
825
                   type_len = type_len << 8;
826
                end
827
              buffer = buffer + 1;
828
           end
829
         delta_t = !delta_t;
830
      end
831
   endtask // set_rx_addr_type
832
 
833
 
834 411 julius
   // Check if we're using a synthesized version of eth module
835
`ifdef ethmac_IS_GATELEVEL
836
 
837 408 julius
   // Get the length/status register of the ethernet buffer descriptor
838
   task get_bd_lenstat;
839
      input [31:0] bd_num;// Number of ethernet BD to check
840
      output [31:0] bd_lenstat;
841
 `ifdef ACTEL
842
      reg [8:0]    tmp;
843
      integer      raddr;
844
 `endif
845
      begin
846
 `ifdef ACTEL
847
 
848
         // Pull from the Actel memory model
849
         raddr = `ETH_BD_RAM_PATH.\mem_tile.I_1 .get_address((bd_num*2));
850
 
851
         tmp = `ETH_BD_RAM_PATH.\mem_tile.I_1 .MEM_512_9[(raddr*2)];
852
         bd_lenstat[8:0] = tmp[8:0];
853
 
854
         tmp = `ETH_BD_RAM_PATH.\mem_tile.I_1 .MEM_512_9[(raddr*2)+1];
855
         bd_lenstat[17:9] = tmp[8:0];
856
 
857
         raddr = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .get_address((bd_num*2));
858
 
859
         tmp = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .MEM_512_9[(raddr*2)];
860
         bd_lenstat[26:18] = tmp[8:0];
861
 
862
         tmp = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .MEM_512_9[(raddr*2)+1];
863
         bd_lenstat[31:27] = tmp[4:0];
864
 
865
         //$display("(%t) read eth bd lenstat %h",$time, bd_lenstat);
866
 `endif
867
      end
868
   endtask // get_bd_lenstat
869
 
870
   // Get the length/status register of the ethernet buffer descriptor
871
   task get_bd_addr;
872
      input [31:0] bd_num;// Number of the ethernet BD to check
873
      output [31:0] bd_addr;
874
 `ifdef ACTEL
875
      reg [8:0]    tmp;
876
      integer       raddr;
877
 `endif
878
      begin
879
 `ifdef ACTEL
880
         // Pull from the Actel memory model
881
         raddr = `ETH_BD_RAM_PATH.\mem_tile.I_1 .get_address((bd_num*2)+1);
882
 
883
         tmp = `ETH_BD_RAM_PATH.\mem_tile.I_1 .MEM_512_9[(raddr*2)];
884
         bd_addr[8:0] = tmp[8:0];
885
 
886
         tmp = `ETH_BD_RAM_PATH.\mem_tile.I_1 .MEM_512_9[(raddr*2)+1];
887
         bd_addr[17:9] = tmp[8:0];
888
 
889
         raddr = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .get_address((bd_num*2)+1);
890
 
891
         tmp = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .MEM_512_9[(raddr*2)];
892
         bd_addr[26:18] = tmp[8:0];
893
 
894
         tmp = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .MEM_512_9[(raddr*2)+1];
895
         bd_addr[31:27] = tmp[4:0];
896
 
897
         //$display("(%t) read eth bd%d addr %h",$time,bd_num, bd_addr);
898
 `endif
899
      end
900
   endtask // get_bd_addr
901
 
902 411 julius
`else // !`ifdef ethmac_IS_GATELEVEL
903 408 julius
 
904
   // Get the length/status register of the ethernet buffer descriptor
905
   task get_bd_lenstat;
906
      input [31:0] bd_num;// Number of ethernet BD to check
907
      output [31:0] bd_lenstat;
908
      begin
909
         bd_lenstat = `ETH_BD_RAM_PATH.mem[(bd_num*2)];
910
      end
911
   endtask // get_bd_lenstat
912
 
913
   // Get the length/status register of the ethernet buffer descriptor
914
   task get_bd_addr;
915
      input [31:0] bd_num;// Number of the ethernet BD to check
916
      output [31:0] bd_addr;
917
      begin
918
         bd_addr = `ETH_BD_RAM_PATH.mem[((bd_num*2)+1)];
919
         //$display("(%t) read eth bd%d addr %h",$time,bd_num, bd_addr);
920
      end
921
   endtask // get_bd_addr
922
`endif
923
 
924
   // Always block triggered by finishing of transmission of new packet from 
925
   // send_packet_loop
926
   integer eth_rx_packet_length_to_check;
927
 
928
   always @*
929
     begin
930 411 julius
        // Loop here until:
931
        // 1 - packets sent is not equal to packets checked (ie. some to check)
932
        // 2 - we're explicitly disabled for some reason
933
        // 3 - Receive has been disabled in the MAC
934 408 julius
        while((eth_rx_num_packets_sent == eth_rx_num_packets_checked) ||
935 411 julius
              !eth_stim_check_rx_packet_contents || !(ethmac_rxen===1'b1))
936 408 julius
          #1000;
937 411 julius
 
938 408 julius
        eth_rx_packet_length_to_check
939
          = rx_packet_lengths[(eth_rx_num_packets_checked & 12'h3ff)];
940
 
941
        if ( eth_rx_packet_length_to_check !==  32'heeeeeeee)
942
          check_rx_packet(expected_rxbd, 0, eth_rx_packet_length_to_check);
943
 
944
        eth_rx_num_packets_checked = eth_rx_num_packets_checked + 1;
945
 
946
        expected_rxbd = expected_rxbd + 1;
947
 
948
        // Wrap
949
        if (expected_rxbd == (num_tx_bds + num_rx_bds))
950
          expected_rxbd = num_tx_bds;
951
     end
952
 
953
   task check_rx_packet;
954
 
955
      input [31:0] rx_bd_num;
956
      input [31:0] rxpnt_phy; // Pointer in array of data in PHY
957
      input [31:0] len;
958
 
959
      reg [31:0]   rx_bd_lenstat;
960
      reg [31:0]   rx_bd_addr;
961
      reg [7:0]    phy_byte;
962
 
963
      reg [31:0]   rxpnt_wb; // Pointer in array to where data should be
964
      reg [24:0]   rxpnt_sdram; // byte address from CPU in RAM
965
      reg [15:0]   sdram_short;
966
      reg [7:0]    sdram_byte;
967
      //reg [7:0]    phy_rx_mem [0:2000];
968
 
969
      integer      i;
970
      integer      failure;
971
 
972
      begin
973
 
974
         failure = 0;
975
 
976
         // Wait until the buffer descriptor indicates the packet has been 
977
         // received...
978
         get_bd_lenstat(rx_bd_num, rx_bd_lenstat);
979
         while (rx_bd_lenstat & 32'h00008000)// Check Empty bit
980
           begin
981
              #10;
982
              get_bd_lenstat(rx_bd_num, rx_bd_lenstat);
983
              //$display("(%t) check_rx_packet: poll bd %d: 0x%h",$time,
984
                //        rx_bd_num, rx_bd_lenstat);
985
           end
986
 
987
 
988
         // Delay some time - takes a bit for the Wishbone FSM to pipe out the
989
         // packet over Wishbone and into whatever memory it's going into
990
         #Td_rx_packet_check;
991
 
992
         // Ok, buffer filled, let's get its offset in memory
993
         get_bd_addr(rx_bd_num, rx_bd_addr);
994
 
995
         $display("(%t) Check RX packet: bd %d: 0x%h, addr 0x%h",$time,
996
                  rx_bd_num, rx_bd_lenstat, rx_bd_addr);
997
 
998
 
999
         // We're never going to be using more than about 256KB of receive buffer
1000
         // so let's lop off the top bit of the address pointer - we only want
1001
         // the offset from the base of the memory bank
1002
 
1003
         rxpnt_wb = {14'd0,rx_bd_addr[17:0]};
1004
         rxpnt_sdram = rx_bd_addr[24:0];
1005
 
1006
`ifdef VERSATILE_SDRAM
1007
         // We'll look inside the SDRAM array
1008
         // Hard coded for the SDRAM buffer area to be from the halfway mark in
1009
         // memory (so starting in Bank2)
1010
         // We'll be passed the offset from the beginning of the buffer area
1011
         // in rxpnt_wb. This value will be in bytes.
1012
 
1013
         //$display("RAM pointer for BD is 0x%h, SDRAM addr is 0x%h", rx_bd_addr, rxpnt_sdram);
1014
 
1015
 
1016
         for (i=0;i<len;i=i+1)
1017
           begin
1018
 
1019
              sdram0.get_byte(rxpnt_sdram,sdram_byte);
1020
 
1021
              phy_byte = eth_rx_sent_circbuf[eth_rx_sent_circbuf_read_ptr];//phy_rx_mem[buffer]; //eth_phy0.rx_mem[buffer];
1022
 
1023
              if (phy_byte !== sdram_byte)
1024
                begin
1025 411 julius
//                 `TIME;                 
1026
                   $display("*E Wrong byte (%5d) of RX packet %5d! phy = %h, ram = %h",
1027
                            i, eth_rx_num_packets_checked, phy_byte, sdram_byte);
1028 408 julius
                   failure = 1;
1029
                end
1030
 
1031
              eth_rx_sent_circbuf_read_ptr = (eth_rx_sent_circbuf_read_ptr+1)&
1032
                                             eth_rx_sent_circbuf_size_mask;
1033
 
1034
              rxpnt_sdram = rxpnt_sdram+1;
1035
 
1036
           end // for (i=0;i<len;i=i+2)
1037
`else
1038
 
1039
         $display("SET ME UP TO LOOK IN ANOTHER MEMORY!");
1040
         $display("RAM pointer for BD is 0x%h, bank offset we'll use is 0x%h",
1041
                  rx_bd_addr, rxpnt_wb);
1042
         $finish;
1043
 
1044
 
1045
`endif // !`ifdef VERSATILE_SDRAM
1046
 
1047
         if (failure)
1048
           begin
1049
              #100
1050
                `TIME;
1051
              $display("*E Recieved packet %0d, length %0d bytes, had an error. Finishing simulation.", eth_rx_num_packets_checked, len);
1052
              $finish;
1053
           end
1054
         else
1055
           begin
1056
              #1 $display( "(%0t)(%m) RX packet %0d: %0d bytes in memory OK!",$time,eth_rx_num_packets_checked, len);
1057
 
1058
           end
1059
      end
1060
   endtask // check_rx_packet
1061
 
1062
 
1063
   //////////////////////////////////////////////////////////////
1064
   // Ethernet CRC Basic tasks
1065
   //////////////////////////////////////////////////////////////
1066
 
1067
   task append_rx_crc;
1068
      input  [31:0] rxpnt_phy; // source
1069
      input [15:0]  len; // length in bytes without CRC
1070
      input         plus_dribble_nibble; // if length is longer for one nibble
1071
      input         negated_crc; // if appended CRC is correct or not
1072
      reg [31:0]    crc;
1073
      reg [7:0]     tmp;
1074
      reg [31:0]    addr_phy;
1075
      reg           delta_t;
1076
      begin
1077
         addr_phy = rxpnt_phy + len;
1078
         delta_t = 0;
1079
         // calculate CRC from prepared packet
1080
         paralel_crc_phy_rx(rxpnt_phy, {16'h0, len}, plus_dribble_nibble, crc);
1081
         if (negated_crc)
1082
           crc = ~crc;
1083
         delta_t = !delta_t;
1084
 
1085
         if (plus_dribble_nibble)
1086
           begin
1087
              tmp = eth_phy0.rx_mem[addr_phy];
1088
              eth_phy0.rx_mem[addr_phy]     = {crc[27:24], tmp[3:0]};
1089
              eth_phy0.rx_mem[addr_phy + 1] = {crc[19:16], crc[31:28]};
1090
              eth_phy0.rx_mem[addr_phy + 2] = {crc[11:8], crc[23:20]};
1091
              eth_phy0.rx_mem[addr_phy + 3] = {crc[3:0], crc[15:12]};
1092
              eth_phy0.rx_mem[addr_phy + 4] = {4'h0, crc[7:4]};
1093
           end
1094
         else
1095
           begin
1096
              eth_phy0.rx_mem[addr_phy]     = crc[31:24];
1097
              eth_phy0.rx_mem[addr_phy + 1] = crc[23:16];
1098
              eth_phy0.rx_mem[addr_phy + 2] = crc[15:8];
1099
              eth_phy0.rx_mem[addr_phy + 3] = crc[7:0];
1100
           end
1101
      end
1102
   endtask // append_rx_crc
1103
 
1104
   task append_rx_crc_delayed;
1105
      input  [31:0] rxpnt_phy; // source
1106
      input [15:0]  len; // length in bytes without CRC
1107
      input         plus_dribble_nibble; // if length is longer for one nibble
1108
      input         negated_crc; // if appended CRC is correct or not
1109
      reg [31:0]    crc;
1110
      reg [7:0]     tmp;
1111
      reg [31:0]    addr_phy;
1112
      reg           delta_t;
1113
      begin
1114
         addr_phy = rxpnt_phy + len;
1115
         delta_t = 0;
1116
         // calculate CRC from prepared packet
1117
         paralel_crc_phy_rx(rxpnt_phy+4, {16'h0, len}-4, plus_dribble_nibble, crc);
1118
         if (negated_crc)
1119
           crc = ~crc;
1120
         delta_t = !delta_t;
1121
 
1122
         if (plus_dribble_nibble)
1123
           begin
1124
              tmp = eth_phy0.rx_mem[addr_phy];
1125
              eth_phy0.rx_mem[addr_phy]     = {crc[27:24], tmp[3:0]};
1126
              eth_phy0.rx_mem[addr_phy + 1] = {crc[19:16], crc[31:28]};
1127
              eth_phy0.rx_mem[addr_phy + 2] = {crc[11:8], crc[23:20]};
1128
              eth_phy0.rx_mem[addr_phy + 3] = {crc[3:0], crc[15:12]};
1129
              eth_phy0.rx_mem[addr_phy + 4] = {4'h0, crc[7:4]};
1130
           end
1131
         else
1132
           begin
1133
              eth_phy0.rx_mem[addr_phy]     = crc[31:24];
1134
              eth_phy0.rx_mem[addr_phy + 1] = crc[23:16];
1135
              eth_phy0.rx_mem[addr_phy + 2] = crc[15:8];
1136
              eth_phy0.rx_mem[addr_phy + 3] = crc[7:0];
1137
           end
1138
      end
1139
   endtask // append_rx_crc_delayed
1140
 
1141
 
1142
   // paralel CRC calculating for PHY RX
1143
   task paralel_crc_phy_rx;
1144
      input  [31:0] start_addr; // start address
1145
      input [31:0]  len; // length of frame in Bytes without CRC length
1146
      input         plus_dribble_nibble; // if length is longer for one nibble
1147
      output [31:0] crc_out;
1148
      reg [21:0]    addr_cnt; // only 22 address lines
1149
      integer       word_cnt;
1150
      integer       nibble_cnt;
1151
      reg [31:0]    load_reg;
1152
      reg           delta_t;
1153
      reg [31:0]    crc_next;
1154
      reg [31:0]    crc;
1155
      reg           crc_error;
1156
      reg [3:0]     data_in;
1157
      integer       i;
1158
      begin
1159
         #1 addr_cnt = start_addr[21:0];
1160
         word_cnt = 24; // 27; // start of the frame - nibble granularity (MSbit first)
1161
         crc = 32'hFFFF_FFFF; // INITIAL value
1162
         delta_t = 0;
1163
         // length must include 4 bytes of ZEROs, to generate CRC
1164
         // get number of nibbles from Byte length (2^1 = 2)
1165
         if (plus_dribble_nibble)
1166
           nibble_cnt = ((len + 4) << 1) + 1'b1; // one nibble longer
1167
         else
1168
           nibble_cnt = ((len + 4) << 1);
1169
         // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
1170
         load_reg[31:24] = eth_phy0.rx_mem[addr_cnt];
1171
         addr_cnt = addr_cnt + 1;
1172
         load_reg[23:16] = eth_phy0.rx_mem[addr_cnt];
1173
         addr_cnt = addr_cnt + 1;
1174
         load_reg[15: 8] = eth_phy0.rx_mem[addr_cnt];
1175
         addr_cnt = addr_cnt + 1;
1176
         load_reg[ 7: 0] = eth_phy0.rx_mem[addr_cnt];
1177
         addr_cnt = addr_cnt + 1;
1178
         while (nibble_cnt > 0)
1179
           begin
1180
              // wait for delta time
1181
              delta_t = !delta_t;
1182
              // shift data in
1183
 
1184
              if(nibble_cnt <= 8) // for additional 8 nibbles shift ZEROs in!
1185
                data_in[3:0] = 4'h0;
1186
              else
1187
 
1188
                data_in[3:0] = {load_reg[word_cnt], load_reg[word_cnt+1], load_reg[word_cnt+2], load_reg[word_cnt+3]};
1189
              crc_next[0]  = (data_in[0] ^ crc[28]);
1190
              crc_next[1]  = (data_in[1] ^ data_in[0] ^ crc[28]    ^ crc[29]);
1191
              crc_next[2]  = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]);
1192
              crc_next[3]  = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]);
1193
              crc_next[4]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[0];
1194
              crc_next[5]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[1];
1195
              crc_next[6]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[ 2];
1196
              crc_next[7]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[3];
1197
              crc_next[8]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[4];
1198
              crc_next[9]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[5];
1199
              crc_next[10] = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[6];
1200
              crc_next[11] = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[7];
1201
              crc_next[12] = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]) ^ crc[8];
1202
              crc_next[13] = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]) ^ crc[9];
1203
              crc_next[14] = (data_in[3] ^ data_in[2] ^ crc[30]    ^ crc[31]) ^ crc[10];
1204
              crc_next[15] = (data_in[3] ^ crc[31])   ^ crc[11];
1205
              crc_next[16] = (data_in[0] ^ crc[28])   ^ crc[12];
1206
              crc_next[17] = (data_in[1] ^ crc[29])   ^ crc[13];
1207
              crc_next[18] = (data_in[2] ^ crc[30])   ^ crc[14];
1208
              crc_next[19] = (data_in[3] ^ crc[31])   ^ crc[15];
1209
              crc_next[20] =  crc[16];
1210
              crc_next[21] =  crc[17];
1211
              crc_next[22] = (data_in[0] ^ crc[28])   ^ crc[18];
1212
              crc_next[23] = (data_in[1] ^ data_in[0] ^ crc[29]    ^ crc[28]) ^ crc[19];
1213
              crc_next[24] = (data_in[2] ^ data_in[1] ^ crc[30]    ^ crc[29]) ^ crc[20];
1214
              crc_next[25] = (data_in[3] ^ data_in[2] ^ crc[31]    ^ crc[30]) ^ crc[21];
1215
              crc_next[26] = (data_in[3] ^ data_in[0] ^ crc[31]    ^ crc[28]) ^ crc[22];
1216
              crc_next[27] = (data_in[1] ^ crc[29])   ^ crc[23];
1217
              crc_next[28] = (data_in[2] ^ crc[30])   ^ crc[24];
1218
              crc_next[29] = (data_in[3] ^ crc[31])   ^ crc[25];
1219
              crc_next[30] =  crc[26];
1220
              crc_next[31] =  crc[27];
1221
 
1222
              crc = crc_next;
1223
              crc_error = crc[31:0] != 32'hc704dd7b;  // CRC not equal to magic number
1224
              case (nibble_cnt)
1225
                9: crc_out = {!crc[24], !crc[25], !crc[26], !crc[27], !crc[28], !crc[29], !crc[30], !crc[31],
1226
                              !crc[16], !crc[17], !crc[18], !crc[19], !crc[20], !crc[21], !crc[22], !crc[23],
1227
                              !crc[ 8], !crc[ 9], !crc[10], !crc[11], !crc[12], !crc[13], !crc[14], !crc[15],
1228
                              !crc[ 0], !crc[ 1], !crc[ 2], !crc[ 3], !crc[ 4], !crc[ 5], !crc[ 6], !crc[ 7]};
1229
                default: crc_out = crc_out;
1230
              endcase
1231
              // wait for delta time
1232
              delta_t = !delta_t;
1233
              // increment address and load new data
1234
              if ((word_cnt+3) == 7)//4)
1235
                begin
1236
                   // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
1237
                   load_reg[31:24] = eth_phy0.rx_mem[addr_cnt];
1238
                   addr_cnt = addr_cnt + 1;
1239
                   load_reg[23:16] = eth_phy0.rx_mem[addr_cnt];
1240
                   addr_cnt = addr_cnt + 1;
1241
                   load_reg[15: 8] = eth_phy0.rx_mem[addr_cnt];
1242
                   addr_cnt = addr_cnt + 1;
1243
                   load_reg[ 7: 0] = eth_phy0.rx_mem[addr_cnt];
1244
                   addr_cnt = addr_cnt + 1;
1245
                end
1246
              // set new load bit position
1247
              if((word_cnt+3) == 31)
1248
                word_cnt = 16;
1249
              else if ((word_cnt+3) == 23)
1250
                word_cnt = 8;
1251
              else if ((word_cnt+3) == 15)
1252
                word_cnt = 0;
1253
              else if ((word_cnt+3) == 7)
1254
                word_cnt = 24;
1255
              else
1256
                word_cnt = word_cnt + 4;// - 4;
1257
              // decrement nibble counter
1258
              nibble_cnt = nibble_cnt - 1;
1259
              // wait for delta time
1260
              delta_t = !delta_t;
1261
           end // while
1262
         #1;
1263
      end
1264
   endtask // paralel_crc_phy_rx
1265
 
1266
 
1267
 
1268
 

powered by: WebSVN 2.1.0

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