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

Subversion Repositories openrisc

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

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

Line No. Rev Author Line
1 44 julius
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3 412 julius
////  Ethernet MAC Stimulus                                       ////
4 44 julius
////                                                              ////
5
////  Description                                                 ////
6
////  Ethernet MAC stimulus tasks. Taken from the project         ////
7
////  testbench in the ethmac core.                               ////
8
////                                                              ////
9
////  To Do:                                                      ////
10
////                                                              ////
11
////                                                              ////
12
////  Author(s):                                                  ////
13
////      - Tadej Markovic, tadej@opencores.org                   ////
14
////      - Igor Mohor,     igorM@opencores.org                   ////
15 412 julius
////      - Julius Baxter   julius.baxter@orsoc.se                ////
16 44 julius
////                                                              ////
17
////                                                              ////
18
//////////////////////////////////////////////////////////////////////
19
////                                                              ////
20
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
21
////                                                              ////
22
//// This source file may be used and distributed without         ////
23
//// restriction provided that this copyright statement is not    ////
24
//// removed from the file and that any derivative work contains  ////
25
//// the original copyright notice and the associated disclaimer. ////
26
////                                                              ////
27
//// This source file is free software; you can redistribute it   ////
28
//// and/or modify it under the terms of the GNU Lesser General   ////
29
//// Public License as published by the Free Software Foundation; ////
30
//// either version 2.1 of the License, or (at your option) any   ////
31
//// later version.                                               ////
32
////                                                              ////
33
//// This source is distributed in the hope that it will be       ////
34
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
35
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
36
//// PURPOSE.  See the GNU Lesser General Public License for more ////
37
//// details.                                                     ////
38
////                                                              ////
39
//// You should have received a copy of the GNU Lesser General    ////
40
//// Public License along with this source; if not, download it   ////
41
//// from http://www.opencores.org/lgpl.shtml                     ////
42
////                                                              ////
43
//////////////////////////////////////////////////////////////////////
44 412 julius
`define TIME $display("Time: %0t", $time)
45 44 julius
 
46 412 julius
// Defines for ethernet test to trigger sending/receiving
47
// Is straight forward when using RTL design, but if using netlist then paths to
48
// the RX/TX enabled bits depend on synthesis tool, etc, but ones here appear to
49
// work with design put through Synplify, with hierarchy maintained.
50
`define ETH_TOP dut.ethmac0
51
`define ETH_BD_RAM_PATH `ETH_TOP.wishbone.bd_ram
52
`define ETH_MODER_PATH `ETH_TOP.ethreg1.MODER_0
53 44 julius
 
54 412 julius
`ifdef RTL_SIM
55
 `ifdef ethmac_IS_GATELEVEL
56
  `define ETH_MODER_TXEN_BIT `ETH_MODER_PATH.r_TxEn;
57
  `define ETH_MODER_RXEN_BIT `ETH_MODER_PATH.r_RxEn;
58
 `else
59
  `define ETH_MODER_TXEN_BIT `ETH_MODER_PATH.DataOut[1];
60
  `define ETH_MODER_RXEN_BIT `ETH_MODER_PATH.DataOut[0];
61
 `endif
62
`endif
63
 
64
`ifdef GATE_SIM
65
 `define ETH_MODER_TXEN_BIT `ETH_MODER_PATH.r_TxEn;
66
 `define ETH_MODER_RXEN_BIT `ETH_MODER_PATH.r_RxEn;
67
`endif
68
 
69
reg [15:0] eth_stim_rx_packet_length;
70 44 julius
reg [7:0] st_data;
71 412 julius
reg [31:0] lfsr;
72
integer lfsr_last_byte;
73
 
74
// Is number of ethernet packets to send if doing the eth-rx test.
75
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
 
81
// Do call/response test
82
reg eth_stim_do_rx_reponse_to_tx;
83
 
84
 
85
parameter num_tx_bds = 16;
86
parameter num_tx_bds_mask = 4'hf;
87
parameter num_rx_bds = 16;
88
parameter num_rx_bds_mask = 4'hf;
89
parameter max_eth_packet_size = 16'h0600;
90
 
91
// If running eth-rxtxbig test (sending and receiving maximum packets), then
92
// set this parameter to the max packet size, otherwise min packet size
93
//parameter rx_while_tx_min_packet_size = max_eth_packet_size;
94
parameter rx_while_tx_min_packet_size = 32;
95
 
96
// Use the smallest possible IPG
97
parameter eth_stim_use_min_IPG = 0;
98
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 44 julius
initial
144
  begin
145 412 julius
     #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
     expected_rxbd = num_tx_bds; // init this here
152 44 julius
 
153 412 julius
     eth_stim_do_rx_reponse_to_tx = 0;
154 44 julius
 
155
 
156 412 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
                    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
                    eth_stim_waiting = 0;
168
                 end
169
               else
170
                 begin
171
                    //do_rx_only_stim(eth_stim_num_rx_only_num_packets, 
172
                    //eth_stim_num_rx_only_packet_size, 0, 0);
173 44 julius
 
174 412 julius
                    // 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
                    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
               // 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
               eth_stim_waiting = 0;
211
            end
212
       end // while (eth_stim_waiting)     
213 49 julius
 
214 412 julius
  end // initial begin
215 49 julius
 
216 412 julius
   // 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
      input [31:0] start_packet_size;
221
      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
              send_packet_loop(num_packets, start_packet_size, 2'b01, 1,
230
                               speed_loop[0], 10000,
231
                               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 44 julius
 
239 412 julius
   // 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 44 julius
 
246 412 julius
      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 44 julius
 
295 412 julius
              // 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 44 julius
 
308 412 julius
                end
309
 
310
           end // for (j=0;j<num_packets;j=j+1)
311
      end
312
   endtask // do_rx_stim
313 44 julius
 
314 412 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 44 julius
 
319 412 julius
   // 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 44 julius
 
338 412 julius
         // 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 44 julius
 
343 412 julius
         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 44 julius
 
379 412 julius
           end
380 44 julius
 
381 412 julius
      end
382
   endtask // do_rx_response_to_tx
383
 
384
 
385
 
386
 
387
 
388
   //
389
   // always@() to check the TX buffer descriptors
390
   //
391
   always @(posedge ethmac_txen)
392
     begin
393
         ethmac_last_txbd_lenstat = 0;
394
         eth_stim_tx_loop_keep_polling=1;
395
         // Wait on the TxBD Ready bit
396
         while(eth_stim_tx_loop_keep_polling)
397
           begin
398
              #10;
399
              get_bd_lenstat(expected_txbd, ethmac_txbd_lenstat);
400
              // Check if we've finished transmitting this BD
401
              if (!ethmac_txbd_lenstat[15] & ethmac_last_txbd_lenstat[15])
402
                // Falling edge of TX BD Ready
403
                eth_stim_detected_packet_tx = 1;
404
 
405
              ethmac_last_txbd_lenstat = ethmac_txbd_lenstat;
406
 
407
              // If TX en goes low then exit
408
              if (!ethmac_txen)
409
                eth_stim_tx_loop_keep_polling = 0;
410
              else if (eth_stim_detected_packet_tx)
411
                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
                            expected_txbd, ethmac_txbd_lenstat);
418
 
419
                   // Check the TXBD, see if the packet transmitted OK
420
                   if (ethmac_txbd_lenstat[8] | ethmac_txbd_lenstat[3])
421
                     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
                                 ethmac_txbd_lenstat);
428
                        if (ethmac_txbd_lenstat[8])
429
                          $display(" Underrun in MAC during TX");
430
                        if (ethmac_txbd_lenstat[3])
431
                          $display(" Retransmission limit hit");
432
 
433
                        $finish;
434
                     end
435
                   else
436
                     begin
437
                        // Packet was OK, let's compare the contents we 
438
                        // received with those that were meant to be transmitted
439
                        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
                             eth_stim_detected_packet_tx = 0;
446
                          end
447
                     end
448
                end
449
           end // while (eth_stim_tx_loop_keep_polling)
450
     end // always @ (posedge ethmac_txen)
451
 
452
 
453
 
454 415 julius
 
455
`ifdef XILINX_DDR2
456
   // Gets word from correct bank
457
   task get_32bitword_from_xilinx_ddr2;
458
      input [31:0] addr;
459
      output [31:0] insn;
460
      reg [16*8-1:0] ddr2_array_line0,ddr2_array_line1,ddr2_array_line2,
461
                     ddr2_array_line3;
462
      integer        word_in_line_num;
463
      begin
464
        // Get our 4 128-bit chunks (8 half-words in each!! Confused yet?), 
465
        // 16 words total
466
         gen_cs[0].gen[0].u_mem0.memory_read(addr[28:27],addr[26:13],
467
                                             {addr[12:6],3'd0},
468
                                             ddr2_array_line0);
469
         gen_cs[0].gen[1].u_mem0.memory_read(addr[28:27],addr[26:13],
470
                                             {addr[12:6],3'd0},
471
                                             ddr2_array_line1);
472
         gen_cs[0].gen[2].u_mem0.memory_read(addr[28:27],addr[26:13],
473
                                             {addr[12:6],3'd0},
474
                                             ddr2_array_line2);
475
         gen_cs[0].gen[3].u_mem0.memory_read(addr[28:27],addr[26:13],
476
                                             {addr[12:6],3'd0},
477
                                             ddr2_array_line3);
478
         case (addr[5:2])
479
           4'h0:
480
             begin
481
                insn[15:0] = ddr2_array_line0[15:0];
482
                insn[31:16] = ddr2_array_line1[15:0];
483
             end
484
           4'h1:
485
             begin
486
                insn[15:0] = ddr2_array_line2[15:0];
487
                insn[31:16] = ddr2_array_line3[15:0];
488
             end
489
           4'h2:
490
             begin
491
                insn[15:0] = ddr2_array_line0[31:16];
492
                insn[31:16] = ddr2_array_line1[31:16];
493
             end
494
           4'h3:
495
             begin
496
                insn[15:0] = ddr2_array_line2[31:16];
497
                insn[31:16] = ddr2_array_line3[31:16];
498
             end
499
           4'h4:
500
             begin
501
                insn[15:0] = ddr2_array_line0[47:32];
502
                insn[31:16] = ddr2_array_line1[47:32];
503
             end
504
           4'h5:
505
             begin
506
                insn[15:0] = ddr2_array_line2[47:32];
507
                insn[31:16] = ddr2_array_line3[47:32];
508
             end
509
           4'h6:
510
             begin
511
                insn[15:0] = ddr2_array_line0[63:48];
512
                insn[31:16] = ddr2_array_line1[63:48];
513
             end
514
           4'h7:
515
             begin
516
                insn[15:0] = ddr2_array_line2[63:48];
517
                insn[31:16] = ddr2_array_line3[63:48];
518
             end
519
           4'h8:
520
             begin
521
                insn[15:0] = ddr2_array_line0[79:64];
522
                insn[31:16] = ddr2_array_line1[79:64];
523
             end
524
           4'h9:
525
             begin
526
                insn[15:0] = ddr2_array_line2[79:64];
527
                insn[31:16] = ddr2_array_line3[79:64];
528
             end
529
           4'ha:
530
             begin
531
                insn[15:0] = ddr2_array_line0[95:80];
532
                insn[31:16] = ddr2_array_line1[95:80];
533
             end
534
           4'hb:
535
             begin
536
                insn[15:0] = ddr2_array_line2[95:80];
537
                insn[31:16] = ddr2_array_line3[95:80];
538
             end
539
           4'hc:
540
             begin
541
                insn[15:0] = ddr2_array_line0[111:96];
542
                insn[31:16] = ddr2_array_line1[111:96];
543
             end
544
           4'hd:
545
             begin
546
                insn[15:0] = ddr2_array_line2[111:96];
547
                insn[31:16] = ddr2_array_line3[111:96];
548
             end
549
           4'he:
550
             begin
551
                insn[15:0] = ddr2_array_line0[127:112];
552
                insn[31:16] = ddr2_array_line1[127:112];
553
             end
554
           4'hf:
555
             begin
556
                insn[15:0] = ddr2_array_line2[127:112];
557
                insn[31:16] = ddr2_array_line3[127:112];
558
             end
559
         endcase // case (addr[5:2])
560
      end
561
   endtask
562
 
563
   task get_byte_from_xilinx_ddr2;
564
      input [31:0] addr;
565
      output [7:0] data_byte;
566
      reg [31:0]   word;
567
      begin
568
         get_32bitword_from_xilinx_ddr2(addr, word);
569
         case (addr[1:0])
570
           2'b00:
571
             data_byte = word[31:24];
572
           2'b01:
573
             data_byte = word[23:16];
574
           2'b10:
575
             data_byte = word[15:8];
576
           2'b11:
577
             data_byte = word[7:0];
578
         endcase // case (addr[1:0])
579
      end
580
   endtask // get_byte_from_xilinx_ddr2
581
 
582
`endif
583
 
584
 
585 412 julius
   //
586
   // Check packet TX'd by MAC was good
587
   // 
588
   task check_tx_packet;
589
      input [31:0] tx_bd_num;
590
 
591
      reg [31:0]   tx_bd_addr;
592
      reg [7:0]    phy_byte;
593
 
594
      reg [31:0]   txpnt_wb; // Pointer in array to where data should be
595
      reg [24:0]   txpnt_sdram; // Index in array of shorts for data in SDRAM 
596
                                // part
597
      reg [21:0]   buffer;
598
      reg [7:0]    sdram_byte;
599
      reg [31:0]   tx_len_bd;
600
 
601
      integer      i;
602
      integer      failure;
603 44 julius
      begin
604 412 julius
         failure = 0;
605
 
606
         get_bd_lenstat(tx_bd_num, tx_len_bd);
607
 
608
         tx_len_bd = {15'd0,tx_len_bd[31:16]};
609
 
610
         // Check, if length didn't have to be padded, that
611
         // amount transmitted was correct
612
         if ((tx_len_bd > 60)&(tx_len_bd != (eth_phy0.tx_len-4)))
613
           begin
614
              $display("*E TX packet sent length, %0d != length in TX BD, %0d",
615
                       eth_phy0.tx_len-4, tx_len_bd);
616
              #100;
617
              $finish;
618
           end
619
 
620
         get_bd_addr(tx_bd_num, tx_bd_addr);
621
 
622
         // We're never going to be using more than about 256K of receive buffer
623
         // so let's lop off the top bit of the address pointer - we only want
624
         // the offset from the base of the memory bank
625
         txpnt_wb = {14'd0,tx_bd_addr[17:0]};
626
         txpnt_sdram = tx_bd_addr[24:0];
627
 
628
         // Variable we'll use for index in the PHY's TX buffer
629
         buffer = 0; // Start of TX data
630 415 julius
 
631 412 julius
         for (i=0;i<tx_len_bd;i=i+1)
632
           begin
633
              //$display("Checking address in tx bd 0x%0h",txpnt_sdram);
634 415 julius
              sdram_byte = 8'hx;
635 412 julius
 
636 415 julius
`ifdef VERSATILE_SDRAM
637
              sdram0.get_byte(txpnt_sdram,sdram_byte);
638
`endif
639
`ifdef XILINX_DDR2
640
              get_byte_from_xilinx_ddr2(txpnt_sdram, sdram_byte);
641
`endif
642
              if (sdram_byte === 8'hx)
643
                begin
644
                   $display(" * Error: sdram_byte was %x", sdram_byte);
645
 
646
                   $display(" * eth_stim needs to be able to access the main memory to check packet rx/tx");
647
                   $display(" * RAM pointer for BD is 0x%h, bank offset we'll use is 0x%h",
648
                            tx_bd_addr, txpnt_wb);
649
                   $finish;
650
                end
651 412 julius
 
652 415 julius
 
653 412 julius
              phy_byte = eth_phy0.tx_mem[buffer];
654
              // Debugging output
655
              //$display("txpnt_sdram = 0x%h, sdram_byte = 0x%h, buffer = 0x%h, phy_byte = 0x%h", txpnt_sdram,  sdram_byte, buffer, phy_byte);
656
              if (phy_byte !== sdram_byte)
657
                begin
658
                   `TIME;
659
                   $display("*E Wrong byte (%d) of TX packet! ram = %h, phy = %h",buffer, sdram_byte, phy_byte);
660
                   failure = 1;
661
                end
662
 
663
              buffer = buffer + 1;
664
 
665
              txpnt_sdram = txpnt_sdram+1;
666
 
667
           end // for (i=0;i<tx_len_bd;i=i+1)
668
 
669
         if (failure)
670
           begin
671
              #100
672
                `TIME;
673
              $display("*E Error transmitting packet %0d (%0d bytes). Finishing simulation", num_tx_packets, tx_len_bd);
674
              get_bd_lenstat(tx_bd_num, tx_len_bd);
675
              $display("   TXBD lenstat: 0x%0h",tx_len_bd);
676
              $display("   TXBD address: 0x%0h",tx_bd_addr);
677
              $finish;
678
           end
679
         else
680
           begin
681
              #1 $display( "(%0t)(%m) TX packet %0d: %0d bytes in memory OK!",$time,num_tx_packets, tx_len_bd);
682
 
683
           end
684
 
685
 
686 44 julius
      end
687 412 julius
   endtask // check_tx_packet
688
 
689
   //
690
   // Task to send a set of packets
691
   //
692
   task send_packet_loop;
693
      input [31:0] num_packets;
694
      input [31:0] length;
695
      input [1:0]  length_change; // 0 = none, 1 = incr, 2 = decrement
696
      input [31:0] length_change_size; // Size to change by
697
      input        speed;
698
      input [31:0] back_to_back_delay; // #delay setting between packets
699
      input [47:0] dst_mac;
700
      input [47:0] src_mac;
701
      input        random_fill;
702
      input        random_errors;
703
      input [31:0] random_error_mod;
704
      integer      j;
705
      reg          error_this_time;
706
      integer      error_type; // 0 = rxerr, 1=bad preamble 2=bad crc 3=TODO
707
      reg [31:0]   rx_bd_lenstat;
708 44 julius
      begin
709 412 julius
         error_type = 0;
710
         error_this_time = 0;
711
 
712
         if (num_packets == 0)
713
           // Loop forever when num_packets is 0
714
           num_packets = 32'h7fffffff;
715
 
716
 
717
         if (speed & !(eth_phy0.control_bit14_10[13] === 1'b1))
718
           begin
719
              // write to phy's control register for 100Mbps
720
              eth_phy0.control_bit14_10 = 5'b01000; // bit 13 set - speed 100
721
              // Swapping speeds, give some delay
722
              #10000;
723
           end
724
         else if (!speed & !(eth_phy0.control_bit14_10[13] === 1'b0))
725
           begin
726
              eth_phy0.control_bit14_10 = 5'b00000; // bit 13 reset - speed 10
727
              // Swapping speeds, give some delay
728
              #10000;
729
           end
730
 
731
         eth_phy0.control_bit8_0   = 9'h1_00;
732
 
733
         for(j=0;j<num_packets | length <32;j=j+1)
734
           begin
735
              eth_stim_rx_packet_length = length[15:0]; // Bytes
736
              st_data = 8'h0F;
737
 
738
              // setup RX packet in buffer - length is without CRC
739
              set_rx_packet(0, eth_stim_rx_packet_length, 1'b0, dst_mac,
740
                            src_mac, 16'h0D0E, st_data, random_fill);
741
 
742
              set_rx_addr_type(0, dst_mac, src_mac, 16'h0D0E);
743
 
744
              // Error type 2 is cause CRC error
745
              append_rx_crc(0, eth_stim_rx_packet_length, 1'b0,
746
                            (error_type==2));
747
 
748
              if (error_this_time)
749
                begin
750
                   if (error_type == 0)
751
                     // RX ERR assert during transmit
752
                     eth_phy0.send_rx_packet(64'h0055_5555_5555_5555, 4'h7,
753
                                             8'hD5, 0,
754
                                             eth_stim_rx_packet_length+4,
755
                                             1'b0, 1'b1);
756
                   else if (error_type == 1)
757
                     // Incorrect preamble
758
                     eth_phy0.send_rx_packet(64'h0055_5f55_5555_5555, 4'h7,
759
                                             8'hD5, 0,
760
                                             eth_stim_rx_packet_length+4,
761
                                             1'b0, 1'b0);
762
                   else
763
                     // Normal datapacket
764
                     eth_phy0.send_rx_packet(64'h0055_5555_5555_5555, 4'h7,
765
                                             8'hD5, 0,
766
                                             eth_stim_rx_packet_length+4,
767
                                             1'b0, 1'b0);
768
                end
769
              else
770
                eth_phy0.send_rx_packet(64'h0055_5555_5555_5555, 4'h7, 8'hD5,
771
                                        0, eth_stim_rx_packet_length+4, 1'b0,
772
                                        1'b0);
773
 
774
 
775
              // if RX enable still set (might have gone low during this packet
776
              if (ethmac_rxen)
777
                begin
778
                   if (error_this_time)
779
                     // Put in dummy length, checking function will skip...
780
                     rx_packet_lengths[(eth_rx_num_packets_sent& 12'h3ff)]=32'heeeeeeee;
781
                   else
782
                     rx_packet_lengths[(eth_rx_num_packets_sent & 12'h3ff)] = length;
783
 
784
                   eth_rx_num_packets_sent = eth_rx_num_packets_sent + 1;
785
 
786
                end // if (ethmac_rxen)
787
              else
788
                begin
789
                   // Force the loop to finish up                  
790
                   j = num_packets;
791
                end
792
 
793
 
794
              // Inter-packet gap
795
              #back_to_back_delay;
796
 
797
              // Update length
798
              if (length_change == 2'b01)
799
                length = length + length_change_size;
800
 
801
              if ((length_change == 2'b10) &&
802
                  ((length - length_change_size) > 32))
803
                length = length - length_change_size;
804
 
805
              // Increment error type
806
              if (error_this_time)
807
                error_type = error_type + 1;
808
              if (error_type > 3)
809
                error_type = 0;
810
 
811
 
812
              // Check if we should put in an error this time
813
              if (j%random_error_mod == 0)
814
                error_this_time = 1;
815
              else
816
                error_this_time = 0;
817
 
818
              eth_phy0.rx_err(0);
819
 
820
              // Now wait to check if we have filled up all the RX BDs and
821
              // the this packet would start writing over them. Only really an
822
              // issue when doing minimum IPG tests.
823
              while(((eth_rx_num_packets_sent+1) - eth_rx_num_packets_checked)
824
                    == num_rx_bds)
825
                #100;
826
 
827
 
828
           end // for (j=0;j<num_packets | length <32;j=j+1)
829 44 julius
      end
830 412 julius
   endtask // send_packet_loop
831
 
832
   // Local buffer of "sent" data to the ethernet MAC, we will check against
833
   // Size of our local buffer in bytes
834
   parameter eth_rx_sent_circbuf_size = (16*1024);
835
   parameter eth_rx_sent_circbuf_size_mask = eth_rx_sent_circbuf_size - 1;
836
   integer eth_rx_sent_circbuf_fill_ptr = 0;
837
   integer eth_rx_sent_circbuf_read_ptr = 0;
838
   // The actual buffer
839
   reg [7:0] eth_rx_sent_circbuf [0:eth_rx_sent_circbuf_size-1];
840
 
841
   /*
842
    TASKS for set and check RX packets:
843
    -----------------------------------
844
    set_rx_packet
845
    (rxpnt[31:0], len[15:0], plus_nibble, d_addr[47:0], s_addr[47:0], type_len[15:0], start_data[7:0]);
846
    check_rx_packet
847
    (rxpnt_phy[31:0], rxpnt_wb[31:0], len[15:0], plus_nibble, successful_nibble, failure[31:0]);
848
    */
849
   task set_rx_packet;
850
      input  [31:0] rxpnt; // pointer to place in in the phy rx buffer we'll start at
851
      input [15:0]  len;
852
      input         plus_dribble_nibble; // if length is longer for one nibble
853
      input [47:0]  eth_dest_addr;
854
      input [47:0]  eth_source_addr;
855
      input [15:0]  eth_type_len;
856
      input [7:0]   eth_start_data;
857
      input         random_fill;
858
      integer       i, sd;
859
      reg [47:0]    dest_addr;
860
      reg [47:0]    source_addr;
861
      reg [15:0]    type_len;
862
      reg [21:0]    buffer;
863
      reg           delta_t;
864
 
865 44 julius
      begin
866 412 julius
         buffer = rxpnt[21:0];
867
         dest_addr = eth_dest_addr;
868
         source_addr = eth_source_addr;
869
         type_len = eth_type_len;
870
         sd = eth_start_data;
871
         delta_t = 0;
872
         for(i = 0; i < len; i = i + 1)
873
           begin
874
              if (i < 6)
875
                begin
876
                   eth_phy0.rx_mem[buffer] = dest_addr[47:40];
877
                   dest_addr = dest_addr << 8;
878
                end
879
              else if (i < 12)
880
                begin
881
                   eth_phy0.rx_mem[buffer] = source_addr[47:40];
882
                   source_addr = source_addr << 8;
883
                end
884
              else if (i < 14)
885
                begin
886
                   eth_phy0.rx_mem[buffer] = type_len[15:8];
887
                   type_len = type_len << 8;
888
                end
889
              else
890
                begin
891
                   if (random_fill)
892
                     begin
893
                        if (lfsr_last_byte == 0)
894
                          eth_phy0.rx_mem[buffer] = lfsr[15:8];
895
                        if (lfsr_last_byte == 1)
896
                          eth_phy0.rx_mem[buffer] = lfsr[23:16];
897
                        if (lfsr_last_byte == 2)
898
                          eth_phy0.rx_mem[buffer] = lfsr[31:24];
899
                        if (lfsr_last_byte == 3)
900
                          begin
901
                             eth_phy0.rx_mem[buffer] = lfsr[7:0];
902
                             lfsr = {lfsr[30:0],(((lfsr[31] ^ lfsr[6]) ^
903
                                                  lfsr[5]) ^ lfsr[1])};
904
                             lfsr_last_byte =  0;
905
                          end
906
                        else
907
                          lfsr_last_byte = lfsr_last_byte + 1;
908
 
909
                     end // if (random_fill)               
910
                   else
911
                     eth_phy0.rx_mem[buffer] = sd[7:0];
912
                   sd = sd + 1;
913
                end // else: !if(i < 14)
914
 
915
              // Update our local buffer
916
              eth_rx_sent_circbuf[eth_rx_sent_circbuf_fill_ptr]
917
                = eth_phy0.rx_mem[buffer];
918
              eth_rx_sent_circbuf_fill_ptr = (eth_rx_sent_circbuf_fill_ptr+1)&
919
                                             eth_rx_sent_circbuf_size_mask;
920
 
921
              buffer = buffer + 1;
922
           end // for (i = 0; i < len; i = i + 1)
923
 
924
         delta_t = !delta_t;
925
         if (plus_dribble_nibble)
926
           eth_phy0.rx_mem[buffer] = {4'h0, 4'hD /*sd[3:0]*/};
927
         delta_t = !delta_t;
928 44 julius
      end
929 412 julius
   endtask // set_rx_packet
930
 
931
 
932
 
933
 
934
   task set_rx_addr_type;
935
      input  [31:0] rxpnt;
936
      input [47:0]  eth_dest_addr;
937
      input [47:0]  eth_source_addr;
938
      input [15:0]  eth_type_len;
939
      integer       i;
940
      reg [47:0]    dest_addr;
941
      reg [47:0]    source_addr;
942
      reg [15:0]    type_len;
943
      reg [21:0]    buffer;
944
      reg           delta_t;
945 44 julius
      begin
946 412 julius
         buffer = rxpnt[21:0];
947
         dest_addr = eth_dest_addr;
948
         source_addr = eth_source_addr;
949
         type_len = eth_type_len;
950
         delta_t = 0;
951
         for(i = 0; i < 14; i = i + 1)
952
           begin
953
              if (i < 6)
954
                begin
955
                   eth_phy0.rx_mem[buffer] = dest_addr[47:40];
956
                   dest_addr = dest_addr << 8;
957
                end
958
              else if (i < 12)
959
                begin
960
                   eth_phy0.rx_mem[buffer] = source_addr[47:40];
961
                   source_addr = source_addr << 8;
962
                end
963
              else // if (i < 14)
964
                begin
965
                   eth_phy0.rx_mem[buffer] = type_len[15:8];
966
                   type_len = type_len << 8;
967
                end
968
              buffer = buffer + 1;
969
           end
970
         delta_t = !delta_t;
971 44 julius
      end
972 412 julius
   endtask // set_rx_addr_type
973
 
974
 
975
   // Check if we're using a synthesized version of eth module
976
`ifdef ethmac_IS_GATELEVEL
977
 
978
   // Get the length/status register of the ethernet buffer descriptor
979
   task get_bd_lenstat;
980
      input [31:0] bd_num;// Number of ethernet BD to check
981
      output [31:0] bd_lenstat;
982
 `ifdef ACTEL
983
      reg [8:0]    tmp;
984
      integer      raddr;
985
 `endif
986 44 julius
      begin
987 412 julius
 `ifdef ACTEL
988
 
989
         // Pull from the Actel memory model
990
         raddr = `ETH_BD_RAM_PATH.\mem_tile.I_1 .get_address((bd_num*2));
991
 
992
         tmp = `ETH_BD_RAM_PATH.\mem_tile.I_1 .MEM_512_9[(raddr*2)];
993
         bd_lenstat[8:0] = tmp[8:0];
994
 
995
         tmp = `ETH_BD_RAM_PATH.\mem_tile.I_1 .MEM_512_9[(raddr*2)+1];
996
         bd_lenstat[17:9] = tmp[8:0];
997
 
998
         raddr = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .get_address((bd_num*2));
999
 
1000
         tmp = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .MEM_512_9[(raddr*2)];
1001
         bd_lenstat[26:18] = tmp[8:0];
1002
 
1003
         tmp = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .MEM_512_9[(raddr*2)+1];
1004
         bd_lenstat[31:27] = tmp[4:0];
1005
 
1006
         //$display("(%t) read eth bd lenstat %h",$time, bd_lenstat);
1007
 `endif
1008 44 julius
      end
1009 412 julius
   endtask // get_bd_lenstat
1010
 
1011
   // Get the length/status register of the ethernet buffer descriptor
1012
   task get_bd_addr;
1013
      input [31:0] bd_num;// Number of the ethernet BD to check
1014
      output [31:0] bd_addr;
1015
 `ifdef ACTEL
1016
      reg [8:0]    tmp;
1017
      integer       raddr;
1018
 `endif
1019 44 julius
      begin
1020 412 julius
 `ifdef ACTEL
1021
         // Pull from the Actel memory model
1022
         raddr = `ETH_BD_RAM_PATH.\mem_tile.I_1 .get_address((bd_num*2)+1);
1023
 
1024
         tmp = `ETH_BD_RAM_PATH.\mem_tile.I_1 .MEM_512_9[(raddr*2)];
1025
         bd_addr[8:0] = tmp[8:0];
1026
 
1027
         tmp = `ETH_BD_RAM_PATH.\mem_tile.I_1 .MEM_512_9[(raddr*2)+1];
1028
         bd_addr[17:9] = tmp[8:0];
1029
 
1030
         raddr = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .get_address((bd_num*2)+1);
1031
 
1032
         tmp = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .MEM_512_9[(raddr*2)];
1033
         bd_addr[26:18] = tmp[8:0];
1034
 
1035
         tmp = `ETH_BD_RAM_PATH.\mem_tile_0.I_1 .MEM_512_9[(raddr*2)+1];
1036
         bd_addr[31:27] = tmp[4:0];
1037
 
1038
         //$display("(%t) read eth bd%d addr %h",$time,bd_num, bd_addr);
1039
 `endif
1040 44 julius
      end
1041 412 julius
   endtask // get_bd_addr
1042
 
1043
`else // !`ifdef ethmac_IS_GATELEVEL
1044
 
1045
   // Get the length/status register of the ethernet buffer descriptor
1046
   task get_bd_lenstat;
1047
      input [31:0] bd_num;// Number of ethernet BD to check
1048
      output [31:0] bd_lenstat;
1049 44 julius
      begin
1050 412 julius
         bd_lenstat = `ETH_BD_RAM_PATH.mem[(bd_num*2)];
1051 44 julius
      end
1052 412 julius
   endtask // get_bd_lenstat
1053
 
1054
   // Get the length/status register of the ethernet buffer descriptor
1055
   task get_bd_addr;
1056
      input [31:0] bd_num;// Number of the ethernet BD to check
1057
      output [31:0] bd_addr;
1058 44 julius
      begin
1059 412 julius
         bd_addr = `ETH_BD_RAM_PATH.mem[((bd_num*2)+1)];
1060
         //$display("(%t) read eth bd%d addr %h",$time,bd_num, bd_addr);
1061 44 julius
      end
1062 412 julius
   endtask // get_bd_addr
1063
`endif
1064
 
1065
   // Always block triggered by finishing of transmission of new packet from 
1066
   // send_packet_loop
1067
   integer eth_rx_packet_length_to_check;
1068 44 julius
 
1069 412 julius
   always @*
1070
     begin
1071
        // Loop here until:
1072
        // 1 - packets sent is not equal to packets checked (ie. some to check)
1073
        // 2 - we're explicitly disabled for some reason
1074
        // 3 - Receive has been disabled in the MAC
1075
        while((eth_rx_num_packets_sent == eth_rx_num_packets_checked) ||
1076
              !eth_stim_check_rx_packet_contents || !(ethmac_rxen===1'b1))
1077
          #1000;
1078 44 julius
 
1079 412 julius
        eth_rx_packet_length_to_check
1080
          = rx_packet_lengths[(eth_rx_num_packets_checked & 12'h3ff)];
1081
 
1082
        if ( eth_rx_packet_length_to_check !==  32'heeeeeeee)
1083
          check_rx_packet(expected_rxbd, 0, eth_rx_packet_length_to_check);
1084
 
1085
        eth_rx_num_packets_checked = eth_rx_num_packets_checked + 1;
1086
 
1087
        expected_rxbd = expected_rxbd + 1;
1088
 
1089
        // Wrap
1090
        if (expected_rxbd == (num_tx_bds + num_rx_bds))
1091
          expected_rxbd = num_tx_bds;
1092
     end
1093
 
1094
   task check_rx_packet;
1095
 
1096
      input [31:0] rx_bd_num;
1097
      input [31:0] rxpnt_phy; // Pointer in array of data in PHY
1098
      input [31:0] len;
1099
 
1100
      reg [31:0]   rx_bd_lenstat;
1101
      reg [31:0]   rx_bd_addr;
1102
      reg [7:0]    phy_byte;
1103
 
1104
      reg [31:0]   rxpnt_wb; // Pointer in array to where data should be
1105
      reg [24:0]   rxpnt_sdram; // byte address from CPU in RAM
1106
      reg [15:0]   sdram_short;
1107
      reg [7:0]    sdram_byte;
1108
      //reg [7:0]    phy_rx_mem [0:2000];
1109
 
1110
      integer      i;
1111
      integer      failure;
1112
 
1113
      begin
1114 44 julius
 
1115 412 julius
         failure = 0;
1116
 
1117
         // Wait until the buffer descriptor indicates the packet has been 
1118
         // received...
1119
         get_bd_lenstat(rx_bd_num, rx_bd_lenstat);
1120
         while (rx_bd_lenstat & 32'h00008000)// Check Empty bit
1121
           begin
1122
              #10;
1123
              get_bd_lenstat(rx_bd_num, rx_bd_lenstat);
1124
              //$display("(%t) check_rx_packet: poll bd %d: 0x%h",$time,
1125
                //        rx_bd_num, rx_bd_lenstat);
1126
           end
1127
 
1128 44 julius
 
1129 412 julius
         // Delay some time - takes a bit for the Wishbone FSM to pipe out the
1130
         // packet over Wishbone and into whatever memory it's going into
1131
         #Td_rx_packet_check;
1132
 
1133
         // Ok, buffer filled, let's get its offset in memory
1134
         get_bd_addr(rx_bd_num, rx_bd_addr);
1135 44 julius
 
1136 412 julius
         $display("(%t) Check RX packet: bd %d: 0x%h, addr 0x%h",$time,
1137
                  rx_bd_num, rx_bd_lenstat, rx_bd_addr);
1138 44 julius
 
1139 412 julius
 
1140
         // We're never going to be using more than about 256KB of receive buffer
1141
         // so let's lop off the top bit of the address pointer - we only want
1142
         // the offset from the base of the memory bank
1143
 
1144
         rxpnt_wb = {14'd0,rx_bd_addr[17:0]};
1145
         rxpnt_sdram = rx_bd_addr[24:0];
1146 415 julius
 
1147
 
1148 412 julius
         //$display("RAM pointer for BD is 0x%h, SDRAM addr is 0x%h", rx_bd_addr, rxpnt_sdram);
1149 44 julius
 
1150
 
1151 412 julius
         for (i=0;i<len;i=i+1)
1152
           begin
1153 44 julius
 
1154 415 julius
              sdram_byte = 8'hx;
1155
`ifdef VERSATILE_SDRAM
1156
              sdram0.get_byte(rxpnt_sdram,sdram_byte);
1157
`endif
1158
`ifdef XILINX_DDR2
1159
              get_byte_from_xilinx_ddr2(rxpnt_sdram, sdram_byte);
1160
`endif
1161
              if (sdram_byte === 8'hx)
1162
                begin
1163
                   $display(" * Error:");
1164
 
1165
                   $display(" * eth_stim needs to be able to access the main memory to check packet rx/tx");
1166
                   $display("RAM pointer for BD is 0x%h, bank offset we'll use is 0x%h",
1167
                            rx_bd_addr, rxpnt_wb);
1168
                   $finish;
1169
                end
1170 44 julius
 
1171 412 julius
              phy_byte = eth_rx_sent_circbuf[eth_rx_sent_circbuf_read_ptr];//phy_rx_mem[buffer]; //eth_phy0.rx_mem[buffer];
1172
 
1173
              if (phy_byte !== sdram_byte)
1174
                begin
1175
//                 `TIME;                 
1176
                   $display("*E Wrong byte (%5d) of RX packet %5d! phy = %h, ram = %h",
1177
                            i, eth_rx_num_packets_checked, phy_byte, sdram_byte);
1178
                   failure = 1;
1179
                end
1180
 
1181
              eth_rx_sent_circbuf_read_ptr = (eth_rx_sent_circbuf_read_ptr+1)&
1182
                                             eth_rx_sent_circbuf_size_mask;
1183
 
1184
              rxpnt_sdram = rxpnt_sdram+1;
1185
 
1186
           end // for (i=0;i<len;i=i+2)
1187
 
1188
         if (failure)
1189
           begin
1190
              #100
1191
                `TIME;
1192
              $display("*E Recieved packet %0d, length %0d bytes, had an error. Finishing simulation.", eth_rx_num_packets_checked, len);
1193
              $finish;
1194
           end
1195
         else
1196
           begin
1197
              #1 $display( "(%0t)(%m) RX packet %0d: %0d bytes in memory OK!",$time,eth_rx_num_packets_checked, len);
1198
 
1199
           end
1200
      end
1201
   endtask // check_rx_packet
1202
 
1203
 
1204
   //////////////////////////////////////////////////////////////
1205
   // Ethernet CRC Basic tasks
1206
   //////////////////////////////////////////////////////////////
1207
 
1208
   task append_rx_crc;
1209
      input  [31:0] rxpnt_phy; // source
1210
      input [15:0]  len; // length in bytes without CRC
1211
      input         plus_dribble_nibble; // if length is longer for one nibble
1212
      input         negated_crc; // if appended CRC is correct or not
1213
      reg [31:0]    crc;
1214
      reg [7:0]     tmp;
1215
      reg [31:0]    addr_phy;
1216
      reg           delta_t;
1217
      begin
1218
         addr_phy = rxpnt_phy + len;
1219
         delta_t = 0;
1220
         // calculate CRC from prepared packet
1221
         paralel_crc_phy_rx(rxpnt_phy, {16'h0, len}, plus_dribble_nibble, crc);
1222
         if (negated_crc)
1223
           crc = ~crc;
1224
         delta_t = !delta_t;
1225
 
1226
         if (plus_dribble_nibble)
1227
           begin
1228
              tmp = eth_phy0.rx_mem[addr_phy];
1229
              eth_phy0.rx_mem[addr_phy]     = {crc[27:24], tmp[3:0]};
1230
              eth_phy0.rx_mem[addr_phy + 1] = {crc[19:16], crc[31:28]};
1231
              eth_phy0.rx_mem[addr_phy + 2] = {crc[11:8], crc[23:20]};
1232
              eth_phy0.rx_mem[addr_phy + 3] = {crc[3:0], crc[15:12]};
1233
              eth_phy0.rx_mem[addr_phy + 4] = {4'h0, crc[7:4]};
1234
           end
1235
         else
1236
           begin
1237
              eth_phy0.rx_mem[addr_phy]     = crc[31:24];
1238
              eth_phy0.rx_mem[addr_phy + 1] = crc[23:16];
1239
              eth_phy0.rx_mem[addr_phy + 2] = crc[15:8];
1240
              eth_phy0.rx_mem[addr_phy + 3] = crc[7:0];
1241
           end
1242
      end
1243
   endtask // append_rx_crc
1244
 
1245
   task append_rx_crc_delayed;
1246
      input  [31:0] rxpnt_phy; // source
1247
      input [15:0]  len; // length in bytes without CRC
1248
      input         plus_dribble_nibble; // if length is longer for one nibble
1249
      input         negated_crc; // if appended CRC is correct or not
1250
      reg [31:0]    crc;
1251
      reg [7:0]     tmp;
1252
      reg [31:0]    addr_phy;
1253
      reg           delta_t;
1254
      begin
1255
         addr_phy = rxpnt_phy + len;
1256
         delta_t = 0;
1257
         // calculate CRC from prepared packet
1258
         paralel_crc_phy_rx(rxpnt_phy+4, {16'h0, len}-4, plus_dribble_nibble, crc);
1259
         if (negated_crc)
1260
           crc = ~crc;
1261
         delta_t = !delta_t;
1262
 
1263
         if (plus_dribble_nibble)
1264
           begin
1265
              tmp = eth_phy0.rx_mem[addr_phy];
1266
              eth_phy0.rx_mem[addr_phy]     = {crc[27:24], tmp[3:0]};
1267
              eth_phy0.rx_mem[addr_phy + 1] = {crc[19:16], crc[31:28]};
1268
              eth_phy0.rx_mem[addr_phy + 2] = {crc[11:8], crc[23:20]};
1269
              eth_phy0.rx_mem[addr_phy + 3] = {crc[3:0], crc[15:12]};
1270
              eth_phy0.rx_mem[addr_phy + 4] = {4'h0, crc[7:4]};
1271
           end
1272
         else
1273
           begin
1274
              eth_phy0.rx_mem[addr_phy]     = crc[31:24];
1275
              eth_phy0.rx_mem[addr_phy + 1] = crc[23:16];
1276
              eth_phy0.rx_mem[addr_phy + 2] = crc[15:8];
1277
              eth_phy0.rx_mem[addr_phy + 3] = crc[7:0];
1278
           end
1279
      end
1280
   endtask // append_rx_crc_delayed
1281
 
1282
 
1283
   // paralel CRC calculating for PHY RX
1284
   task paralel_crc_phy_rx;
1285
      input  [31:0] start_addr; // start address
1286
      input [31:0]  len; // length of frame in Bytes without CRC length
1287
      input         plus_dribble_nibble; // if length is longer for one nibble
1288
      output [31:0] crc_out;
1289
      reg [21:0]    addr_cnt; // only 22 address lines
1290
      integer       word_cnt;
1291
      integer       nibble_cnt;
1292
      reg [31:0]    load_reg;
1293
      reg           delta_t;
1294
      reg [31:0]    crc_next;
1295
      reg [31:0]    crc;
1296
      reg           crc_error;
1297
      reg [3:0]     data_in;
1298
      integer       i;
1299
      begin
1300
         #1 addr_cnt = start_addr[21:0];
1301
         word_cnt = 24; // 27; // start of the frame - nibble granularity (MSbit first)
1302
         crc = 32'hFFFF_FFFF; // INITIAL value
1303
         delta_t = 0;
1304
         // length must include 4 bytes of ZEROs, to generate CRC
1305
         // get number of nibbles from Byte length (2^1 = 2)
1306
         if (plus_dribble_nibble)
1307
           nibble_cnt = ((len + 4) << 1) + 1'b1; // one nibble longer
1308
         else
1309
           nibble_cnt = ((len + 4) << 1);
1310
         // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
1311
         load_reg[31:24] = eth_phy0.rx_mem[addr_cnt];
1312
         addr_cnt = addr_cnt + 1;
1313
         load_reg[23:16] = eth_phy0.rx_mem[addr_cnt];
1314
         addr_cnt = addr_cnt + 1;
1315
         load_reg[15: 8] = eth_phy0.rx_mem[addr_cnt];
1316
         addr_cnt = addr_cnt + 1;
1317
         load_reg[ 7: 0] = eth_phy0.rx_mem[addr_cnt];
1318
         addr_cnt = addr_cnt + 1;
1319
         while (nibble_cnt > 0)
1320
           begin
1321
              // wait for delta time
1322
              delta_t = !delta_t;
1323
              // shift data in
1324
 
1325
              if(nibble_cnt <= 8) // for additional 8 nibbles shift ZEROs in!
1326
                data_in[3:0] = 4'h0;
1327
              else
1328
 
1329
                data_in[3:0] = {load_reg[word_cnt], load_reg[word_cnt+1], load_reg[word_cnt+2], load_reg[word_cnt+3]};
1330
              crc_next[0]  = (data_in[0] ^ crc[28]);
1331
              crc_next[1]  = (data_in[1] ^ data_in[0] ^ crc[28]    ^ crc[29]);
1332
              crc_next[2]  = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]);
1333
              crc_next[3]  = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]);
1334
              crc_next[4]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[0];
1335
              crc_next[5]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[1];
1336
              crc_next[6]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[ 2];
1337
              crc_next[7]  = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[3];
1338
              crc_next[8]  = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[4];
1339
              crc_next[9]  = (data_in[2] ^ data_in[1] ^ crc[29]    ^ crc[30]) ^ crc[5];
1340
              crc_next[10] = (data_in[3] ^ data_in[2] ^ data_in[0] ^ crc[28]  ^ crc[30] ^ crc[31]) ^ crc[6];
1341
              crc_next[11] = (data_in[3] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[31]) ^ crc[7];
1342
              crc_next[12] = (data_in[2] ^ data_in[1] ^ data_in[0] ^ crc[28]  ^ crc[29] ^ crc[30]) ^ crc[8];
1343
              crc_next[13] = (data_in[3] ^ data_in[2] ^ data_in[1] ^ crc[29]  ^ crc[30] ^ crc[31]) ^ crc[9];
1344
              crc_next[14] = (data_in[3] ^ data_in[2] ^ crc[30]    ^ crc[31]) ^ crc[10];
1345
              crc_next[15] = (data_in[3] ^ crc[31])   ^ crc[11];
1346
              crc_next[16] = (data_in[0] ^ crc[28])   ^ crc[12];
1347
              crc_next[17] = (data_in[1] ^ crc[29])   ^ crc[13];
1348
              crc_next[18] = (data_in[2] ^ crc[30])   ^ crc[14];
1349
              crc_next[19] = (data_in[3] ^ crc[31])   ^ crc[15];
1350
              crc_next[20] =  crc[16];
1351
              crc_next[21] =  crc[17];
1352
              crc_next[22] = (data_in[0] ^ crc[28])   ^ crc[18];
1353
              crc_next[23] = (data_in[1] ^ data_in[0] ^ crc[29]    ^ crc[28]) ^ crc[19];
1354
              crc_next[24] = (data_in[2] ^ data_in[1] ^ crc[30]    ^ crc[29]) ^ crc[20];
1355
              crc_next[25] = (data_in[3] ^ data_in[2] ^ crc[31]    ^ crc[30]) ^ crc[21];
1356
              crc_next[26] = (data_in[3] ^ data_in[0] ^ crc[31]    ^ crc[28]) ^ crc[22];
1357
              crc_next[27] = (data_in[1] ^ crc[29])   ^ crc[23];
1358
              crc_next[28] = (data_in[2] ^ crc[30])   ^ crc[24];
1359
              crc_next[29] = (data_in[3] ^ crc[31])   ^ crc[25];
1360
              crc_next[30] =  crc[26];
1361
              crc_next[31] =  crc[27];
1362
 
1363
              crc = crc_next;
1364
              crc_error = crc[31:0] != 32'hc704dd7b;  // CRC not equal to magic number
1365
              case (nibble_cnt)
1366
                9: crc_out = {!crc[24], !crc[25], !crc[26], !crc[27], !crc[28], !crc[29], !crc[30], !crc[31],
1367
                              !crc[16], !crc[17], !crc[18], !crc[19], !crc[20], !crc[21], !crc[22], !crc[23],
1368
                              !crc[ 8], !crc[ 9], !crc[10], !crc[11], !crc[12], !crc[13], !crc[14], !crc[15],
1369
                              !crc[ 0], !crc[ 1], !crc[ 2], !crc[ 3], !crc[ 4], !crc[ 5], !crc[ 6], !crc[ 7]};
1370
                default: crc_out = crc_out;
1371
              endcase
1372
              // wait for delta time
1373
              delta_t = !delta_t;
1374
              // increment address and load new data
1375
              if ((word_cnt+3) == 7)//4)
1376
                begin
1377
                   // because of MAGIC NUMBER nibbles are swapped [3:0] -> [0:3]
1378
                   load_reg[31:24] = eth_phy0.rx_mem[addr_cnt];
1379
                   addr_cnt = addr_cnt + 1;
1380
                   load_reg[23:16] = eth_phy0.rx_mem[addr_cnt];
1381
                   addr_cnt = addr_cnt + 1;
1382
                   load_reg[15: 8] = eth_phy0.rx_mem[addr_cnt];
1383
                   addr_cnt = addr_cnt + 1;
1384
                   load_reg[ 7: 0] = eth_phy0.rx_mem[addr_cnt];
1385
                   addr_cnt = addr_cnt + 1;
1386
                end
1387
              // set new load bit position
1388
              if((word_cnt+3) == 31)
1389
                word_cnt = 16;
1390
              else if ((word_cnt+3) == 23)
1391
                word_cnt = 8;
1392
              else if ((word_cnt+3) == 15)
1393
                word_cnt = 0;
1394
              else if ((word_cnt+3) == 7)
1395
                word_cnt = 24;
1396
              else
1397
                word_cnt = word_cnt + 4;// - 4;
1398
              // decrement nibble counter
1399
              nibble_cnt = nibble_cnt - 1;
1400
              // wait for delta time
1401
              delta_t = !delta_t;
1402
           end // while
1403
         #1;
1404
      end
1405
   endtask // paralel_crc_phy_rx
1406
 
1407
 
1408
 
1409 44 julius
 

powered by: WebSVN 2.1.0

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