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

Subversion Repositories xge_mac

[/] [xge_mac/] [trunk/] [rtl/] [verilog/] [tx_dequeue.v] - Blame information for rev 26

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 antanguay
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  File name "tx_dequeue.v"                                    ////
4
////                                                              ////
5
////  This file is part of the "10GE MAC" project                 ////
6
////  http://www.opencores.org/cores/xge_mac/                     ////
7
////                                                              ////
8
////  Author(s):                                                  ////
9
////      - A. Tanguay (antanguay@opencores.org)                  ////
10
////                                                              ////
11
//////////////////////////////////////////////////////////////////////
12
////                                                              ////
13
//// Copyright (C) 2008 AUTHORS. All rights reserved.             ////
14
////                                                              ////
15
//// This source file may be used and distributed without         ////
16
//// restriction provided that this copyright statement is not    ////
17
//// removed from the file and that any derivative work contains  ////
18
//// the original copyright notice and the associated disclaimer. ////
19
////                                                              ////
20
//// This source file is free software; you can redistribute it   ////
21
//// and/or modify it under the terms of the GNU Lesser General   ////
22
//// Public License as published by the Free Software Foundation; ////
23
//// either version 2.1 of the License, or (at your option) any   ////
24
//// later version.                                               ////
25
////                                                              ////
26
//// This source is distributed in the hope that it will be       ////
27
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
28
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
29
//// PURPOSE.  See the GNU Lesser General Public License for more ////
30
//// details.                                                     ////
31
////                                                              ////
32
//// You should have received a copy of the GNU Lesser General    ////
33
//// Public License along with this source; if not, download it   ////
34
//// from http://www.opencores.org/lgpl.shtml                     ////
35
////                                                              ////
36
//////////////////////////////////////////////////////////////////////
37
 
38
 
39
`include "defines.v"
40
 
41
module tx_dequeue(/*AUTOARG*/
42
  // Outputs
43 12 antanguay
  txdfifo_ren, txhfifo_ren, txhfifo_wdata, txhfifo_wstatus,
44
  txhfifo_wen, xgmii_txd, xgmii_txc, status_txdfifo_udflow_tog,
45 24 antanguay
  txsfifo_wen, txsfifo_wdata,
46 2 antanguay
  // Inputs
47 12 antanguay
  clk_xgmii_tx, reset_xgmii_tx_n, ctrl_tx_enable_ctx,
48
  status_local_fault_ctx, status_remote_fault_ctx, txdfifo_rdata,
49
  txdfifo_rstatus, txdfifo_rempty, txdfifo_ralmost_empty,
50
  txhfifo_rdata, txhfifo_rstatus, txhfifo_rempty,
51 2 antanguay
  txhfifo_ralmost_empty, txhfifo_wfull, txhfifo_walmost_full
52
  );
53
`include "CRC32_D64.v"
54
`include "CRC32_D8.v"
55
`include "utils.v"
56
 
57
input         clk_xgmii_tx;
58
input         reset_xgmii_tx_n;
59
 
60
input         ctrl_tx_enable_ctx;
61 20 antanguay
 
62 2 antanguay
input         status_local_fault_ctx;
63
input         status_remote_fault_ctx;
64 20 antanguay
 
65 2 antanguay
input  [63:0] txdfifo_rdata;
66
input  [7:0]  txdfifo_rstatus;
67
input         txdfifo_rempty;
68
input         txdfifo_ralmost_empty;
69
 
70
input  [63:0] txhfifo_rdata;
71
input  [7:0]  txhfifo_rstatus;
72
input         txhfifo_rempty;
73
input         txhfifo_ralmost_empty;
74
 
75
input         txhfifo_wfull;
76
input         txhfifo_walmost_full;
77
 
78
output        txdfifo_ren;
79
 
80
output        txhfifo_ren;
81
 
82
output [63:0] txhfifo_wdata;
83
output [7:0]  txhfifo_wstatus;
84 20 antanguay
output        txhfifo_wen;
85 2 antanguay
 
86
output [63:0] xgmii_txd;
87
output [7:0]  xgmii_txc;
88
 
89
output        status_txdfifo_udflow_tog;
90
 
91 24 antanguay
output        txsfifo_wen;
92
output [13:0] txsfifo_wdata;
93 2 antanguay
 
94
 
95
/*AUTOREG*/
96
// Beginning of automatic regs (for this module's undeclared outputs)
97
reg                     status_txdfifo_udflow_tog;
98
reg                     txdfifo_ren;
99
reg                     txhfifo_ren;
100
reg [63:0]              txhfifo_wdata;
101
reg                     txhfifo_wen;
102
reg [7:0]               txhfifo_wstatus;
103 24 antanguay
reg [13:0]              txsfifo_wdata;
104
reg                     txsfifo_wen;
105 2 antanguay
reg [7:0]               xgmii_txc;
106
reg [63:0]              xgmii_txd;
107
// End of automatics
108
 
109
/*AUTOWIRE*/
110
 
111
 
112
reg   [63:0]    xgxs_txd;
113
reg   [7:0]     xgxs_txc;
114
 
115
reg   [63:0]    next_xgxs_txd;
116
reg   [7:0]     next_xgxs_txc;
117
 
118 10 antanguay
reg   [2:0]     curr_state_enc;
119
reg   [2:0]     next_state_enc;
120 2 antanguay
 
121 10 antanguay
reg   [0:0]     curr_state_pad;
122
reg   [0:0]     next_state_pad;
123 2 antanguay
 
124
reg             start_on_lane0;
125
reg             next_start_on_lane0;
126
 
127
reg   [2:0]     ifg_deficit;
128
reg   [2:0]     next_ifg_deficit;
129
 
130
reg             ifg_4b_add;
131
reg             next_ifg_4b_add;
132
 
133
reg             ifg_8b_add;
134
reg             next_ifg_8b_add;
135
 
136
reg             ifg_8b2_add;
137
reg             next_ifg_8b2_add;
138
 
139 6 antanguay
reg   [7:0]     eop;
140
reg   [7:0]     next_eop;
141
 
142 2 antanguay
reg   [63:32]   xgxs_txd_barrel;
143
reg   [7:4]     xgxs_txc_barrel;
144
 
145
reg   [63:0]    txhfifo_rdata_d1;
146
 
147 26 antanguay
reg   [13:0]    add_cnt;
148 2 antanguay
reg   [13:0]    byte_cnt;
149
 
150
reg   [31:0]    crc32_d64;
151
reg   [31:0]    crc32_d8;
152
reg   [31:0]    crc32_tx;
153
 
154
reg   [63:0]    shift_crc_data;
155 6 antanguay
reg   [3:0]     shift_crc_eop;
156
reg   [3:0]     shift_crc_cnt;
157 2 antanguay
 
158
reg   [31:0]    crc_data;
159
 
160
reg             frame_available;
161
reg             next_frame_available;
162
 
163
reg   [63:0]    next_txhfifo_wdata;
164
reg   [7:0]     next_txhfifo_wstatus;
165 20 antanguay
reg             next_txhfifo_wen;
166 2 antanguay
 
167 6 antanguay
reg             txdfifo_ren_d1;
168
 
169 23 antanguay
reg             frame_end;
170
 
171 2 antanguay
parameter [2:0]
172
             SM_IDLE      = 3'd0,
173
             SM_PREAMBLE  = 3'd1,
174
             SM_TX        = 3'd2,
175
             SM_EOP       = 3'd3,
176
             SM_TERM      = 3'd4,
177
             SM_TERM_FAIL = 3'd5,
178
             SM_IFG       = 3'd6;
179
 
180 6 antanguay
parameter [0:0]
181 10 antanguay
             SM_PAD_EQ    = 1'd0,
182
             SM_PAD_PAD   = 1'd1;
183 2 antanguay
 
184
 
185
//---
186
// RC layer
187
 
188
always @(posedge clk_xgmii_tx or negedge reset_xgmii_tx_n) begin
189
 
190
    if (reset_xgmii_tx_n == 1'b0) begin
191
 
192
        xgmii_txd <= {8{`IDLE}};
193
        xgmii_txc <= 8'hff;
194
 
195
    end
196
    else begin
197
 
198
 
199
        //---
200
        // RC Layer, insert local or remote fault messages based on status
201
        // of fault state-machine
202
 
203
        if (status_local_fault_ctx) begin
204
 
205
            // If local fault detected, send remote fault message to
206
            // link partner
207 20 antanguay
 
208 2 antanguay
            xgmii_txd <= {`REMOTE_FAULT, 8'h0, 8'h0, `SEQUENCE,
209
                          `REMOTE_FAULT, 8'h0, 8'h0, `SEQUENCE};
210
            xgmii_txc <= {4'b0001, 4'b0001};
211
        end
212
        else if (status_remote_fault_ctx) begin
213
 
214
            // If remote fault detected, inhibit transmission and send
215
            // idle codes
216 20 antanguay
 
217 2 antanguay
            xgmii_txd <= {8{`IDLE}};
218
            xgmii_txc <= 8'hff;
219
        end
220
        else begin
221
            xgmii_txd <= xgxs_txd;
222
            xgmii_txc <= xgxs_txc;
223
        end
224
    end
225
 
226
end
227
 
228
 
229
always @(posedge clk_xgmii_tx or negedge reset_xgmii_tx_n) begin
230
 
231
    if (reset_xgmii_tx_n == 1'b0) begin
232
 
233 10 antanguay
        curr_state_enc <= SM_IDLE;
234 2 antanguay
 
235
        start_on_lane0 <= 1'b1;
236
        ifg_deficit <= 3'b0;
237
        ifg_4b_add <= 1'b0;
238
        ifg_8b_add <= 1'b0;
239
        ifg_8b2_add <= 1'b0;
240
 
241 6 antanguay
        eop <= 8'b0;
242
 
243 2 antanguay
        txhfifo_rdata_d1 <= 64'b0;
244
 
245
        xgxs_txd_barrel <= {4{`IDLE}};
246
        xgxs_txc_barrel <= 4'hf;
247
 
248
        frame_available <= 1'b0;
249
 
250
        xgxs_txd <= {8{`IDLE}};
251
        xgxs_txc <= 8'hff;
252
 
253
        status_txdfifo_udflow_tog <= 1'b0;
254
 
255 24 antanguay
        txsfifo_wen <= 1'b0;
256
        txsfifo_wdata <= 14'b0;
257 23 antanguay
 
258 2 antanguay
    end
259
    else begin
260
 
261 10 antanguay
        curr_state_enc <= next_state_enc;
262 2 antanguay
 
263
        start_on_lane0 <= next_start_on_lane0;
264
        ifg_deficit <= next_ifg_deficit;
265
        ifg_4b_add <= next_ifg_4b_add;
266
        ifg_8b_add <= next_ifg_8b_add;
267
        ifg_8b2_add <= next_ifg_8b2_add;
268
 
269 6 antanguay
        eop <= next_eop;
270
 
271 2 antanguay
        txhfifo_rdata_d1 <= txhfifo_rdata;
272
 
273
        xgxs_txd_barrel <= next_xgxs_txd[63:32];
274
        xgxs_txc_barrel <= next_xgxs_txc[7:4];
275
 
276
        frame_available <= next_frame_available;
277
 
278 24 antanguay
        txsfifo_wen <= 1'b0;
279
        txsfifo_wdata <= byte_cnt;
280
 
281 2 antanguay
        //---
282
        // Barrel shifter. Previous stage always align packet with LANE0.
283
        // This stage allow us to shift packet to align with LANE4 if needed
284
        // for correct inter frame gap (IFG).
285
 
286
        if (next_start_on_lane0) begin
287
 
288
            xgxs_txd <= next_xgxs_txd;
289
            xgxs_txc <= next_xgxs_txc;
290
 
291
        end
292
        else begin
293
 
294
            xgxs_txd <= {next_xgxs_txd[31:0], xgxs_txd_barrel};
295
            xgxs_txc <= {next_xgxs_txc[3:0], xgxs_txc_barrel};
296
 
297
        end
298
 
299
        //---
300
        // FIFO errors, used to generate interrupts.
301 20 antanguay
 
302 2 antanguay
        if (txdfifo_ren && txdfifo_rempty) begin
303
            status_txdfifo_udflow_tog <= ~status_txdfifo_udflow_tog;
304
        end
305
 
306 23 antanguay
        //---
307
        // Frame count and size
308
 
309
        if (frame_end) begin
310 24 antanguay
            txsfifo_wen <= 1'b1;
311 23 antanguay
        end
312
 
313 2 antanguay
    end
314
 
315
end
316
 
317 10 antanguay
always @(/*AS*/crc32_tx or ctrl_tx_enable_ctx or curr_state_enc or eop
318 2 antanguay
         or frame_available or ifg_4b_add or ifg_8b2_add or ifg_8b_add
319
         or ifg_deficit or start_on_lane0 or status_local_fault_ctx
320 21 antanguay
         or status_remote_fault_ctx or txhfifo_ralmost_empty
321
         or txhfifo_rdata_d1 or txhfifo_rempty or txhfifo_rstatus) begin
322 2 antanguay
 
323 10 antanguay
    next_state_enc = curr_state_enc;
324 2 antanguay
 
325
    next_start_on_lane0 = start_on_lane0;
326
    next_ifg_deficit = ifg_deficit;
327
    next_ifg_4b_add = ifg_4b_add;
328
    next_ifg_8b_add = ifg_8b_add;
329
    next_ifg_8b2_add = ifg_8b2_add;
330
 
331 6 antanguay
    next_eop = eop;
332
 
333 2 antanguay
    next_xgxs_txd = {8{`IDLE}};
334
    next_xgxs_txc = 8'hff;
335
 
336
    txhfifo_ren = 1'b0;
337
 
338
    next_frame_available = frame_available;
339
 
340 10 antanguay
    case (curr_state_enc)
341 2 antanguay
 
342
        SM_IDLE:
343
          begin
344
 
345
              // Wait for frame to be available. There should be a least N bytes in the
346
              // data fifo or a crc in the control fifo. The N bytes in the data fifo
347
              // give time to the enqueue engine to calculate crc and write it to the
348
              // control fifo. If crc is already in control fifo we can start transmitting
349
              // with no concern. Transmission is inhibited if local or remote faults
350
              // are detected.
351
 
352
              if (ctrl_tx_enable_ctx && frame_available &&
353 20 antanguay
                  !status_local_fault_ctx && !status_remote_fault_ctx) begin
354 2 antanguay
 
355
                  txhfifo_ren = 1'b1;
356 10 antanguay
                  next_state_enc = SM_PREAMBLE;
357 2 antanguay
 
358
              end
359
              else begin
360
 
361
                  next_frame_available = !txhfifo_ralmost_empty;
362
                  next_ifg_4b_add = 1'b0;
363
 
364
              end
365
 
366
          end
367
 
368
        SM_PREAMBLE:
369
         begin
370
 
371
             // On reading SOP from fifo, send SFD and preamble characters
372
 
373 6 antanguay
             if (txhfifo_rstatus[`TXSTATUS_SOP]) begin
374 2 antanguay
 
375
                 next_xgxs_txd = {`SFD, {6{`PREAMBLE}}, `START};
376
                 next_xgxs_txc = 8'h01;
377
 
378 6 antanguay
                 txhfifo_ren = 1'b1;
379 2 antanguay
 
380 10 antanguay
                 next_state_enc = SM_TX;
381 2 antanguay
 
382 6 antanguay
             end
383
             else begin
384
 
385
                 next_frame_available = 1'b0;
386 10 antanguay
                 next_state_enc = SM_IDLE;
387 6 antanguay
 
388
             end
389
 
390
 
391 2 antanguay
             // Depending on deficit idle count calculations, add 4 bytes
392
             // or IFG or not. This will determine on which lane start the
393
             // next frame.
394 20 antanguay
 
395 2 antanguay
             if (ifg_4b_add) begin
396
                 next_start_on_lane0 = 1'b0;
397
             end
398
             else begin
399
                 next_start_on_lane0 = 1'b1;
400
             end
401
 
402
          end
403
 
404
        SM_TX:
405
          begin
406
 
407
              next_xgxs_txd = txhfifo_rdata_d1;
408
              next_xgxs_txc = 8'h00;
409
 
410
              txhfifo_ren = 1'b1;
411
 
412
 
413
              // Wait for EOP indication to be read from the fifo, then
414
              // transition to next state.
415
 
416 6 antanguay
              if (txhfifo_rstatus[`TXSTATUS_EOP]) begin
417 20 antanguay
 
418 2 antanguay
                  txhfifo_ren = 1'b0;
419
                  next_frame_available = !txhfifo_ralmost_empty;
420 10 antanguay
                  next_state_enc = SM_EOP;
421 6 antanguay
 
422 2 antanguay
              end
423 6 antanguay
              else if (txhfifo_rempty || txhfifo_rstatus[`TXSTATUS_SOP]) begin
424 2 antanguay
 
425
                  // Failure condition, we did not see EOP and there
426 6 antanguay
                  // is no more data in fifo or SOP, force end of packet transmit.
427 2 antanguay
 
428 10 antanguay
                  next_state_enc = SM_TERM_FAIL;
429 2 antanguay
 
430
              end
431
 
432 6 antanguay
              next_eop[0] = txhfifo_rstatus[2:0] == 3'd1;
433
              next_eop[1] = txhfifo_rstatus[2:0] == 3'd2;
434
              next_eop[2] = txhfifo_rstatus[2:0] == 3'd3;
435
              next_eop[3] = txhfifo_rstatus[2:0] == 3'd4;
436
              next_eop[4] = txhfifo_rstatus[2:0] == 3'd5;
437
              next_eop[5] = txhfifo_rstatus[2:0] == 3'd6;
438
              next_eop[6] = txhfifo_rstatus[2:0] == 3'd7;
439
              next_eop[7] = txhfifo_rstatus[2:0] == 3'd0;
440 20 antanguay
 
441 2 antanguay
          end
442
 
443
        SM_EOP:
444
          begin
445
 
446
              // Insert TERMINATE character in correct lane depending on position
447
              // of EOP read from fifo. Also insert CRC read from control fifo.
448
 
449 6 antanguay
              if (eop[0]) begin
450 20 antanguay
                  next_xgxs_txd = {{2{`IDLE}}, `TERMINATE,
451 2 antanguay
                                   crc32_tx[31:0], txhfifo_rdata_d1[7:0]};
452
                  next_xgxs_txc = 8'b11100000;
453
              end
454
 
455 6 antanguay
              if (eop[1]) begin
456 2 antanguay
                  next_xgxs_txd = {`IDLE, `TERMINATE,
457
                                   crc32_tx[31:0], txhfifo_rdata_d1[15:0]};
458
                  next_xgxs_txc = 8'b11000000;
459
              end
460
 
461 6 antanguay
              if (eop[2]) begin
462 2 antanguay
                  next_xgxs_txd = {`TERMINATE, crc32_tx[31:0], txhfifo_rdata_d1[23:0]};
463
                  next_xgxs_txc = 8'b10000000;
464
              end
465
 
466 6 antanguay
              if (eop[3]) begin
467 2 antanguay
                  next_xgxs_txd = {crc32_tx[31:0], txhfifo_rdata_d1[31:0]};
468
                  next_xgxs_txc = 8'b00000000;
469
              end
470
 
471 6 antanguay
              if (eop[4]) begin
472 2 antanguay
                  next_xgxs_txd = {crc32_tx[23:0], txhfifo_rdata_d1[39:0]};
473
                  next_xgxs_txc = 8'b00000000;
474
              end
475
 
476 6 antanguay
              if (eop[5]) begin
477 2 antanguay
                  next_xgxs_txd = {crc32_tx[15:0], txhfifo_rdata_d1[47:0]};
478
                  next_xgxs_txc = 8'b00000000;
479
              end
480
 
481 6 antanguay
              if (eop[6]) begin
482 2 antanguay
                  next_xgxs_txd = {crc32_tx[7:0], txhfifo_rdata_d1[55:0]};
483
                  next_xgxs_txc = 8'b00000000;
484
              end
485
 
486 6 antanguay
              if (eop[7]) begin
487 2 antanguay
                  next_xgxs_txd = {txhfifo_rdata_d1[63:0]};
488
                  next_xgxs_txc = 8'b00000000;
489
              end
490
 
491
              if (!frame_available) begin
492
 
493
                  // If there is not another frame ready to be transmitted, interface
494
                  // will go idle and idle deficit idle count calculation is irrelevant.
495
                  // Set deficit to 0.
496 20 antanguay
 
497 2 antanguay
                  next_ifg_deficit = 3'b0;
498
 
499
              end
500
              else begin
501
 
502
                  // Idle deficit count calculated based on number of "wasted" bytes
503
                  // between TERMINATE and alignment of next frame in LANE0.
504
 
505
                  next_ifg_deficit = ifg_deficit +
506 6 antanguay
                                     {2'b0, eop[0] | eop[4]} +
507
                                     {1'b0, eop[1] | eop[5], 1'b0} +
508
                                     {1'b0, eop[2] | eop[6],
509
                                      eop[2] | eop[6]};
510 2 antanguay
              end
511
 
512
              // IFG corrections based on deficit count and previous starting lane
513
              // Calculated based on following table:
514
              //
515
              //                 DIC=0          DIC=1          DIC=2          DIC=3
516
              //              -------------  -------------  -------------  -------------
517
              // PktLen       IFG      Next  IFG      Next  IFG      Next  IFG      Next
518
              // Modulus      Length   DIC   Length   DIC   Length   DIC   Length   DIC
519
              // -----------------------------------------------------------------------
520
              //    0           12      0      12      1      12      2      12      3
521
              //    1           11      1      11      2      11      3      15      0
522
              //    2           10      2      10      3      14      0      14      1
523
              //    3            9      3      13      0      13      1      13      2
524
              //
525
              //
526
              // In logic it translates into adding 4, 8, or 12 bytes of IFG relative
527
              // to LANE0.
528
              //   IFG and Add columns assume no deficit applied
529
              //   IFG+DIC and Add+DIC assume deficit must be applied
530
              //
531 20 antanguay
              //                        Start lane 0       Start lane 4
532 2 antanguay
              // EOP Pads IFG  IFG+DIC  Add   Add+DIC      Add    Add IFG
533
              // 0   3    11   15        8     12           12     16
534
              // 1   2    10   14        8     12           12     16
535
              // 2   1    9    13        8     12           12     16
536
              // 3   8    12   12        4     4            8      8
537
              // 4   7    11   15        4     8            8      12
538
              // 5   6    10   14        4     8            8      12
539
              // 6   5    9    13        4     8            8      12
540
              // 7   4    12   12        8     8            12     12
541
 
542
              if (!frame_available) begin
543
 
544
                  // If there is not another frame ready to be transmitted, interface
545
                  // will go idle and idle deficit idle count calculation is irrelevant.
546 20 antanguay
 
547 2 antanguay
                  next_ifg_4b_add = 1'b0;
548
                  next_ifg_8b_add = 1'b0;
549
                  next_ifg_8b2_add = 1'b0;
550
 
551
              end
552
              else if (next_ifg_deficit[2] == ifg_deficit[2]) begin
553
 
554
                  // Add 4 bytes IFG
555 20 antanguay
 
556 6 antanguay
                  next_ifg_4b_add = (eop[0] & !start_on_lane0) |
557
                                    (eop[1] & !start_on_lane0) |
558
                                    (eop[2] & !start_on_lane0) |
559
                                    (eop[3] & start_on_lane0) |
560
                                    (eop[4] & start_on_lane0) |
561
                                    (eop[5] & start_on_lane0) |
562
                                    (eop[6] & start_on_lane0) |
563
                                    (eop[7] & !start_on_lane0);
564 2 antanguay
 
565
                  // Add 8 bytes IFG
566 20 antanguay
 
567 6 antanguay
                  next_ifg_8b_add = (eop[0]) |
568
                                    (eop[1]) |
569
                                    (eop[2]) |
570
                                    (eop[3] & !start_on_lane0) |
571
                                    (eop[4] & !start_on_lane0) |
572
                                    (eop[5] & !start_on_lane0) |
573
                                    (eop[6] & !start_on_lane0) |
574
                                    (eop[7]);
575 2 antanguay
 
576
                  // Add another 8 bytes IFG
577
 
578
                  next_ifg_8b2_add = 1'b0;
579
 
580
              end
581
              else begin
582
 
583
                  // Add 4 bytes IFG
584
 
585 6 antanguay
                  next_ifg_4b_add = (eop[0] & start_on_lane0) |
586
                                    (eop[1] & start_on_lane0) |
587
                                    (eop[2] & start_on_lane0) |
588
                                    (eop[3] &  start_on_lane0) |
589
                                    (eop[4] & !start_on_lane0) |
590
                                    (eop[5] & !start_on_lane0) |
591
                                    (eop[6] & !start_on_lane0) |
592
                                    (eop[7] & !start_on_lane0);
593 2 antanguay
 
594
                  // Add 8 bytes IFG
595 20 antanguay
 
596 6 antanguay
                  next_ifg_8b_add = (eop[0]) |
597
                                    (eop[1]) |
598
                                    (eop[2]) |
599
                                    (eop[3] & !start_on_lane0) |
600
                                    (eop[4]) |
601
                                    (eop[5]) |
602
                                    (eop[6]) |
603
                                    (eop[7]);
604 2 antanguay
 
605
                  // Add another 8 bytes IFG
606
 
607 6 antanguay
                  next_ifg_8b2_add = (eop[0] & !start_on_lane0) |
608
                                     (eop[1] & !start_on_lane0) |
609
                                     (eop[2] & !start_on_lane0);
610 2 antanguay
 
611
              end
612
 
613 6 antanguay
              if (|eop[2:0]) begin
614 2 antanguay
 
615
                  if (frame_available) begin
616
 
617
                      // Next state depends on number of IFG bytes to be inserted.
618
                      // Skip idle state if needed.
619
 
620
                      if (next_ifg_8b2_add) begin
621 10 antanguay
                          next_state_enc = SM_IFG;
622 2 antanguay
                      end
623
                      else if (next_ifg_8b_add) begin
624 10 antanguay
                          next_state_enc = SM_IDLE;
625 2 antanguay
                      end
626
                      else begin
627
                          txhfifo_ren = 1'b1;
628 10 antanguay
                          next_state_enc = SM_PREAMBLE;
629 2 antanguay
                      end
630
 
631
                  end
632
                  else begin
633 10 antanguay
                      next_state_enc = SM_IFG;
634 2 antanguay
                  end
635
              end
636
 
637 6 antanguay
              if (|eop[7:3]) begin
638 10 antanguay
                  next_state_enc = SM_TERM;
639 2 antanguay
              end
640
 
641
          end
642
 
643
        SM_TERM:
644
          begin
645
 
646
              // Insert TERMINATE character in correct lane depending on position
647
              // of EOP read from fifo. Also insert CRC read from control fifo.
648
 
649 6 antanguay
              if (eop[3]) begin
650 2 antanguay
                  next_xgxs_txd = {{7{`IDLE}}, `TERMINATE};
651
                  next_xgxs_txc = 8'b11111111;
652
              end
653
 
654 6 antanguay
              if (eop[4]) begin
655 2 antanguay
                  next_xgxs_txd = {{6{`IDLE}}, `TERMINATE, crc32_tx[31:24]};
656
                  next_xgxs_txc = 8'b11111110;
657
              end
658
 
659 6 antanguay
              if (eop[5]) begin
660 2 antanguay
                  next_xgxs_txd = {{5{`IDLE}}, `TERMINATE, crc32_tx[31:16]};
661
                  next_xgxs_txc = 8'b11111100;
662
              end
663
 
664 6 antanguay
              if (eop[6]) begin
665 2 antanguay
                  next_xgxs_txd = {{4{`IDLE}}, `TERMINATE, crc32_tx[31:8]};
666
                  next_xgxs_txc = 8'b11111000;
667
              end
668
 
669 6 antanguay
              if (eop[7]) begin
670 2 antanguay
                  next_xgxs_txd = {{3{`IDLE}}, `TERMINATE, crc32_tx[31:0]};
671
                  next_xgxs_txc = 8'b11110000;
672
              end
673
 
674
              // Next state depends on number of IFG bytes to be inserted.
675
              // Skip idle state if needed.
676
 
677
              if (frame_available && !ifg_8b_add) begin
678
                  txhfifo_ren = 1'b1;
679 10 antanguay
                  next_state_enc = SM_PREAMBLE;
680 2 antanguay
              end
681
              else if (frame_available) begin
682 10 antanguay
                  next_state_enc = SM_IDLE;
683 2 antanguay
              end
684
              else begin
685 10 antanguay
                  next_state_enc = SM_IFG;
686 2 antanguay
              end
687
 
688
          end
689
 
690
        SM_TERM_FAIL:
691
          begin
692
 
693
              next_xgxs_txd = {{7{`IDLE}}, `TERMINATE};
694
              next_xgxs_txc = 8'b11111111;
695 10 antanguay
              next_state_enc = SM_IFG;
696 2 antanguay
 
697
          end
698
 
699
        SM_IFG:
700
          begin
701
 
702 10 antanguay
              next_state_enc = SM_IDLE;
703 2 antanguay
 
704
          end
705
 
706
        default:
707
          begin
708 10 antanguay
              next_state_enc = SM_IDLE;
709 2 antanguay
          end
710
 
711
    endcase
712
 
713
end
714
 
715
 
716 26 antanguay
always @(/*AS*/crc32_d64 or next_txhfifo_wstatus or txhfifo_wen
717
         or txhfifo_wstatus) begin
718 2 antanguay
 
719 6 antanguay
    if (txhfifo_wen && txhfifo_wstatus[`TXSTATUS_SOP]) begin
720 2 antanguay
        crc_data = 32'hffffffff;
721
    end
722
    else begin
723
        crc_data = crc32_d64;
724
    end
725 20 antanguay
 
726 26 antanguay
    if (next_txhfifo_wstatus[`TXSTATUS_EOP] && next_txhfifo_wstatus[2:0] != 3'b0) begin
727
        add_cnt = {11'b0, next_txhfifo_wstatus[2:0]};
728
    end
729
    else begin
730
        add_cnt = 14'd8;
731
    end
732
 
733 2 antanguay
end
734
 
735 10 antanguay
always @(/*AS*/byte_cnt or curr_state_pad or txdfifo_rdata
736 6 antanguay
         or txdfifo_rempty or txdfifo_ren_d1 or txdfifo_rstatus
737 2 antanguay
         or txhfifo_walmost_full) begin
738
 
739 10 antanguay
    next_state_pad = curr_state_pad;
740 2 antanguay
 
741
    next_txhfifo_wdata = txdfifo_rdata;
742
    next_txhfifo_wstatus = txdfifo_rstatus;
743
 
744
    txdfifo_ren = 1'b0;
745
    next_txhfifo_wen = 1'b0;
746
 
747 10 antanguay
    case (curr_state_pad)
748 2 antanguay
 
749 10 antanguay
      SM_PAD_EQ: begin
750 2 antanguay
 
751
 
752 6 antanguay
          //---
753
          // If room availabe in hoding fifo and data available in
754
          // data fifo, transfer data words. If transmit state machine
755
          // is reading from fifo we can assume room will be available.
756 2 antanguay
 
757 6 antanguay
          if (!txhfifo_walmost_full) begin
758
 
759
              txdfifo_ren = !txdfifo_rempty;
760
 
761 2 antanguay
          end
762
 
763
 
764 6 antanguay
          //---
765
          // This logic dependent on read during previous cycle.
766 2 antanguay
 
767 6 antanguay
          if (txdfifo_ren_d1) begin
768
 
769 2 antanguay
              next_txhfifo_wen = 1'b1;
770
 
771 6 antanguay
              // On EOP, decide if padding is required for this packet.
772 2 antanguay
 
773 6 antanguay
              if (txdfifo_rstatus[`TXSTATUS_EOP]) begin
774 2 antanguay
 
775 23 antanguay
                  if (byte_cnt < 14'd60) begin
776 2 antanguay
 
777
                      next_txhfifo_wstatus = `TXSTATUS_NONE;
778 6 antanguay
                      txdfifo_ren = 1'b0;
779 10 antanguay
                      next_state_pad = SM_PAD_PAD;
780 2 antanguay
 
781
                  end
782 23 antanguay
                  else if (byte_cnt == 14'd60 &&
783 6 antanguay
                           (txdfifo_rstatus[2:0] == 3'd1 ||
784
                            txdfifo_rstatus[2:0] == 3'd2 ||
785
                            txdfifo_rstatus[2:0] == 3'd3)) begin
786 2 antanguay
 
787
                      // Pad up to LANE3, keep the other 4 bytes for crc that will
788
                      // be inserted by dequeue engine.
789 20 antanguay
 
790 6 antanguay
                      next_txhfifo_wstatus[2:0] = 3'd4;
791 2 antanguay
 
792
                      // Pad end bytes with zeros.
793
 
794 6 antanguay
                      if (txdfifo_rstatus[2:0] == 3'd1)
795 2 antanguay
                        next_txhfifo_wdata[31:8] = 24'b0;
796 6 antanguay
                      if (txdfifo_rstatus[2:0] == 3'd2)
797 2 antanguay
                        next_txhfifo_wdata[31:16] = 16'b0;
798 6 antanguay
                      if (txdfifo_rstatus[2:0] == 3'd3)
799 2 antanguay
                        next_txhfifo_wdata[31:24] = 8'b0;
800
 
801 6 antanguay
                      txdfifo_ren = 1'b0;
802 2 antanguay
 
803
                  end
804
                  else begin
805
 
806 6 antanguay
                      txdfifo_ren = 1'b0;
807 20 antanguay
 
808 2 antanguay
                  end
809
 
810
              end
811 20 antanguay
 
812 2 antanguay
          end
813
 
814
      end
815
 
816 10 antanguay
      SM_PAD_PAD: begin
817 2 antanguay
 
818 6 antanguay
          //---
819
          // Pad packet to 64 bytes by writting zeros to holding fifo.
820 2 antanguay
 
821 6 antanguay
          if (!txhfifo_walmost_full) begin
822
 
823 2 antanguay
              next_txhfifo_wdata = 64'b0;
824
              next_txhfifo_wstatus = `TXSTATUS_NONE;
825
              next_txhfifo_wen = 1'b1;
826 20 antanguay
 
827 23 antanguay
              if (byte_cnt == 14'd60) begin
828 2 antanguay
 
829
 
830
                  // Pad up to LANE3, keep the other 4 bytes for crc that will
831
                  // be inserted by dequeue engine.
832
 
833 6 antanguay
                  next_txhfifo_wstatus[`TXSTATUS_EOP] = 1'b1;
834
                  next_txhfifo_wstatus[2:0] = 3'd4;
835 2 antanguay
 
836 10 antanguay
                  next_state_pad = SM_PAD_EQ;
837 2 antanguay
 
838
              end
839
 
840
          end
841
 
842
      end
843
 
844
      default:
845
        begin
846 10 antanguay
            next_state_pad = SM_PAD_EQ;
847 2 antanguay
        end
848
 
849
    endcase
850
 
851
end
852
 
853
 
854
always @(posedge clk_xgmii_tx or negedge reset_xgmii_tx_n) begin
855
 
856
    if (reset_xgmii_tx_n == 1'b0) begin
857
 
858 10 antanguay
        curr_state_pad <= SM_PAD_EQ;
859 2 antanguay
 
860 6 antanguay
        txdfifo_ren_d1 <= 1'b0;
861
 
862 2 antanguay
        txhfifo_wdata <= 64'b0;
863
        txhfifo_wstatus <= 8'b0;
864 20 antanguay
        txhfifo_wen <= 1'b0;
865 2 antanguay
 
866
        byte_cnt <= 14'b0;
867
 
868
        shift_crc_data <= 64'b0;
869 6 antanguay
        shift_crc_eop <= 4'b0;
870
        shift_crc_cnt <= 4'b0;
871 2 antanguay
 
872 20 antanguay
        crc32_d64 <= 32'b0;
873
        crc32_d8 <= 32'b0;
874
        crc32_tx <= 32'b0;
875
 
876 23 antanguay
        frame_end <= 1'b0;
877
 
878 2 antanguay
    end
879
    else begin
880
 
881 10 antanguay
        curr_state_pad <= next_state_pad;
882 2 antanguay
 
883 6 antanguay
        txdfifo_ren_d1 <= txdfifo_ren;
884
 
885 2 antanguay
        txhfifo_wdata <= next_txhfifo_wdata;
886
        txhfifo_wstatus <= next_txhfifo_wstatus;
887
        txhfifo_wen <= next_txhfifo_wen;
888
 
889 23 antanguay
        frame_end <= 1'b0;
890 2 antanguay
 
891 6 antanguay
        //---
892
        // Reset byte count on SOP
893 20 antanguay
 
894 6 antanguay
        if (next_txhfifo_wen) begin
895 2 antanguay
 
896 6 antanguay
            if (next_txhfifo_wstatus[`TXSTATUS_SOP]) begin
897
 
898 23 antanguay
                // Init byte count, 8-bytes + 4-bytes for CRC at the end of frame
899 2 antanguay
 
900 23 antanguay
                byte_cnt <= 14'd12;
901
 
902 2 antanguay
            end
903
            else begin
904
 
905 26 antanguay
                byte_cnt <= byte_cnt + add_cnt;
906 2 antanguay
 
907
            end
908
 
909 23 antanguay
            frame_end <= next_txhfifo_wstatus[`TXSTATUS_EOP];
910
 
911 2 antanguay
        end
912
 
913 6 antanguay
 
914
        //---
915
        // Calculate CRC as data is written to holding fifo. The holding fifo creates
916
        // a delay that allow the CRC calculation to complete before the end of the frame
917
        // is ready to be transmited.
918
 
919 2 antanguay
        if (txhfifo_wen) begin
920
 
921
            crc32_d64 <= nextCRC32_D64(reverse_64b(txhfifo_wdata), crc_data);
922
 
923
        end
924
 
925 6 antanguay
        if (txhfifo_wen && txhfifo_wstatus[`TXSTATUS_EOP]) begin
926 2 antanguay
 
927
            // Last bytes calculated 8-bit at a time instead of 64-bit. Start
928
            // this process at the end of the frame.
929 20 antanguay
 
930 2 antanguay
            crc32_d8 <= crc32_d64;
931
 
932
            shift_crc_data <= txhfifo_wdata;
933 6 antanguay
            shift_crc_cnt <= 4'd9;
934 2 antanguay
 
935 6 antanguay
            if (txhfifo_wstatus[2:0] == 3'b0) begin
936
              shift_crc_eop <= 4'd8;
937
            end
938
            else begin
939
                shift_crc_eop <= {1'b0, txhfifo_wstatus[2:0]};
940
            end
941
 
942 2 antanguay
        end
943 6 antanguay
        else if (shift_crc_eop != 4'b0) begin
944 2 antanguay
 
945
            // Complete crc calculation 8-bit at a time until finished. This can
946
            // be 1 to 8 bytes long.
947
 
948
            crc32_d8 <= nextCRC32_D8(reverse_8b(shift_crc_data[7:0]), crc32_d8);
949
 
950
            shift_crc_data <= {8'b0, shift_crc_data[63:8]};
951 6 antanguay
            shift_crc_eop <= shift_crc_eop - 4'd1;
952 20 antanguay
 
953 2 antanguay
        end
954
 
955 6 antanguay
 
956
        //---
957
        // Update CRC register at the end of calculation. Always update after 8
958
        // cycles for deterministic results, even if a single byte was present in
959
        // last data word.
960 20 antanguay
 
961 6 antanguay
        if (shift_crc_cnt == 4'b1) begin
962
 
963 2 antanguay
            crc32_tx <= ~reverse_32b(crc32_d8);
964 6 antanguay
 
965 2 antanguay
        end
966
        else begin
967 6 antanguay
 
968
            shift_crc_cnt <= shift_crc_cnt - 4'd1;
969
 
970 2 antanguay
        end
971
 
972
    end
973
 
974
end
975
 
976
endmodule

powered by: WebSVN 2.1.0

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