1 //////////////////////////////////////////////////////////////////////
3 //// eth_txethmac.v ////
5 //// This file is part of the Ethernet IP core project ////
6 //// http://www.opencores.org/projects/ethmac/ ////
9 //// - Igor Mohor (igorM@opencores.org) ////
10 //// - Novan Hartadi (novan@vlsi.itb.ac.id) ////
11 //// - Mahmud Galela (mgalela@vlsi.itb.ac.id) ////
13 //// All additional information is avaliable in the Readme.txt ////
16 //////////////////////////////////////////////////////////////////////
18 //// Copyright (C) 2001 Authors ////
20 //// This source file may be used and distributed without ////
21 //// restriction provided that this copyright statement is not ////
22 //// removed from the file and that any derivative work contains ////
23 //// the original copyright notice and the associated disclaimer. ////
25 //// This source file is free software; you can redistribute it ////
26 //// and/or modify it under the terms of the GNU Lesser General ////
27 //// Public License as published by the Free Software Foundation; ////
28 //// either version 2.1 of the License, or (at your option) any ////
29 //// later version. ////
31 //// This source is distributed in the hope that it will be ////
32 //// useful, but WITHOUT ANY WARRANTY; without even the implied ////
33 //// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
34 //// PURPOSE. See the GNU Lesser General Public License for more ////
37 //// You should have received a copy of the GNU Lesser General ////
38 //// Public License along with this source; if not, download it ////
39 //// from http://www.opencores.org/lgpl.shtml ////
41 //////////////////////////////////////////////////////////////////////
43 // CVS Revision History
45 // $Log: eth_txethmac.v,v $
46 // Revision 1.9 2005/02/21 11:25:28 igorm
49 // Revision 1.8 2003/01/30 13:33:24 mohor
50 // When padding was enabled and crc disabled, frame was not ended correctly.
52 // Revision 1.7 2002/02/26 16:24:01 mohor
53 // RetryCntLatched was unused and removed from design
55 // Revision 1.6 2002/02/22 12:56:35 mohor
56 // Retry is not activated when a Tx Underrun occured
58 // Revision 1.5 2002/02/11 09:18:22 mohor
59 // Tx status is written back to the BD.
61 // Revision 1.4 2002/01/23 10:28:16 mohor
62 // Link in the header changed.
64 // Revision 1.3 2001/10/19 08:43:51 mohor
65 // eth_timescale.v changed to timescale.v This is done because of the
66 // simulation of the few cores in a one joined project.
68 // Revision 1.2 2001/09/11 14:17:00 mohor
69 // Few little NCSIM warnings fixed.
71 // Revision 1.1 2001/08/06 14:44:29 mohor
72 // A define FPGA added to select between Artisan RAM (for ASIC) and Block Ram (For Virtex).
73 // Include files fixed to contain no path.
74 // File names and module names changed ta have a eth_ prologue in the name.
75 // File eth_timescale.v is used to define timescale
76 // All pin names on the top module are changed to contain _I, _O or _OE at the end.
77 // Bidirectional signal MDIO is changed to three signals (Mdc_O, Mdi_I, Mdo_O
78 // and Mdo_OE. The bidirectional signal must be created on the top level. This
79 // is done due to the ASIC tools.
81 // Revision 1.1 2001/07/30 21:23:42 mohor
82 // Directory structure changed. Files checked and joind together.
84 // Revision 1.3 2001/06/19 18:16:40 mohor
85 // TxClk changed to MTxClk (as discribed in the documentation).
86 // Crc changed so only one file can be used instead of two.
88 // Revision 1.2 2001/06/19 10:38:08 mohor
89 // Minor changes in header.
91 // Revision 1.1 2001/06/19 10:27:58 mohor
92 // TxEthMAC initial release.
97 `include "timescale.v"
100 module eth_txethmac (MTxClk
, Reset
, TxStartFrm
, TxEndFrm
, TxUnderRun
, TxData
, CarrierSense
,
101 Collision
, Pad
, CrcEn
, FullD
, HugEn
, DlyCrcEn
, MinFL
, MaxFL
, IPGT
,
102 IPGR1
, IPGR2
, CollValid
, MaxRet
, NoBckof
, ExDfrEn
,
103 MTxD
, MTxEn
, MTxErr
, TxDone
, TxRetry
, TxAbort
, TxUsedData
, WillTransmit
,
104 ResetCollision
, RetryCnt
, StartTxDone
, StartTxAbort
, MaxCollisionOccured
,
105 LateCollision
, DeferIndication
, StatePreamble
, StateData
112 input MTxClk
; // Transmit clock (from PHY)
113 input Reset
; // Reset
114 input TxStartFrm
; // Transmit packet start frame
115 input TxEndFrm
; // Transmit packet end frame
116 input TxUnderRun
; // Transmit packet under-run
117 input [7:0] TxData
; // Transmit packet data byte
118 input CarrierSense
; // Carrier sense (synchronized)
119 input Collision
; // Collision (synchronized)
120 input Pad
; // Pad enable (from register)
121 input CrcEn
; // Crc enable (from register)
122 input FullD
; // Full duplex (from register)
123 input HugEn
; // Huge packets enable (from register)
124 input DlyCrcEn
; // Delayed Crc enabled (from register)
125 input [15:0] MinFL
; // Minimum frame length (from register)
126 input [15:0] MaxFL
; // Maximum frame length (from register)
127 input [6:0] IPGT
; // Back to back transmit inter packet gap parameter (from register)
128 input [6:0] IPGR1
; // Non back to back transmit inter packet gap parameter IPGR1 (from register)
129 input [6:0] IPGR2
; // Non back to back transmit inter packet gap parameter IPGR2 (from register)
130 input [5:0] CollValid
; // Valid collision window (from register)
131 input [3:0] MaxRet
; // Maximum retry number (from register)
132 input NoBckof
; // No backoff (from register)
133 input ExDfrEn
; // Excessive defferal enable (from register)
135 output [3:0] MTxD
; // Transmit nibble (to PHY)
136 output MTxEn
; // Transmit enable (to PHY)
137 output MTxErr
; // Transmit error (to PHY)
138 output TxDone
; // Transmit packet done (to RISC)
139 output TxRetry
; // Transmit packet retry (to RISC)
140 output TxAbort
; // Transmit packet abort (to RISC)
141 output TxUsedData
; // Transmit packet used data (to RISC)
142 output WillTransmit
; // Will transmit (to RxEthMAC)
143 output ResetCollision
; // Reset Collision (for synchronizing collision)
144 output [3:0] RetryCnt
; // Latched Retry Counter for tx status purposes
147 output MaxCollisionOccured
;
148 output LateCollision
;
149 output DeferIndication
;
150 output StatePreamble
;
151 output [1:0] StateData
;
162 reg StopExcessiveDeferOccured
;
166 reg PacketFinished_q
;
170 wire ExcessiveDeferOccured
;
173 wire [1:0] StartData
;
192 wire [2:0] DlyCrcCnt
;
202 wire RandomEqByteCnt
;
203 wire PacketFinished_d
;
207 assign ResetCollision
= ~(StatePreamble |
(|StateData
) | StatePAD | StateFCS
);
209 assign ExcessiveDeferOccured
= TxStartFrm
& StateDefer
& ExcessiveDefer
& ~StopExcessiveDeferOccured
;
211 assign StartTxDone
= ~Collision
& (StateFCS
& NibCntEq7 | StateData
[1] & TxEndFrm
& (~Pad | Pad
& NibbleMinFl
) & ~CrcEn
);
213 assign UnderRun
= StateData
[0] & TxUnderRun
& ~Collision
;
215 assign TooBig
= ~Collision
& MaxFrame
& (StateData
[0] & ~TxUnderRun | StateFCS
);
217 // assign StartTxRetry = StartJam & (ColWindow & ~RetryMax);
218 assign StartTxRetry
= StartJam
& (ColWindow
& ~RetryMax
) & ~UnderRun
;
220 assign LateCollision
= StartJam
& ~ColWindow
& ~UnderRun
;
222 assign MaxCollisionOccured
= StartJam
& ColWindow
& RetryMax
;
224 assign StateSFD
= StatePreamble
& NibCntEq15
;
226 assign StartTxAbort
= TooBig | UnderRun | ExcessiveDeferOccured | LateCollision | MaxCollisionOccured
;
229 // StopExcessiveDeferOccured
230 always @ (posedge MTxClk
or posedge Reset
)
233 StopExcessiveDeferOccured
<= #Tp
1'b0;
237 StopExcessiveDeferOccured
<= #Tp
1'b0;
239 if(ExcessiveDeferOccured
)
240 StopExcessiveDeferOccured
<= #Tp
1'b1;
246 always @ (posedge MTxClk
or posedge Reset
)
249 ColWindow
<= #Tp
1'b1;
252 if(~Collision
& ByteCnt
[5:0] == CollValid
[5:0] & (StateData
[1] | StatePAD
& NibCnt
[0] | StateFCS
& NibCnt
[0]))
253 ColWindow
<= #Tp
1'b0;
255 if(StateIdle | StateIPG
)
256 ColWindow
<= #Tp
1'b1;
262 always @ (posedge MTxClk
or posedge Reset
)
265 StatusLatch
<= #Tp
1'b0;
269 StatusLatch
<= #Tp
1'b0;
271 if(ExcessiveDeferOccured | StateIdle
)
272 StatusLatch
<= #Tp
1'b1;
277 // Transmit packet used data
278 always @ (posedge MTxClk
or posedge Reset
)
281 TxUsedData
<= #Tp
1'b0;
283 TxUsedData
<= #Tp |StartData
;
287 // Transmit packet done
288 always @ (posedge MTxClk
or posedge Reset
)
294 if(TxStartFrm
& ~StatusLatch
)
303 // Transmit packet retry
304 always @ (posedge MTxClk
or posedge Reset
)
310 if(TxStartFrm
& ~StatusLatch
)
319 // Transmit packet abort
320 always @ (posedge MTxClk
or posedge Reset
)
326 if(TxStartFrm
& ~StatusLatch
& ~ExcessiveDeferOccured
)
336 always @ (posedge MTxClk
or posedge Reset
)
339 RetryCnt
[3:0] <= #Tp
4'h0
;
342 if(ExcessiveDeferOccured | UnderRun | TooBig | StartTxDone | TxUnderRun
343 | StateJam
& NibCntEq7
& (~ColWindow | RetryMax
))
344 RetryCnt
[3:0] <= #Tp
4'h0
;
346 if(StateJam
& NibCntEq7
& ColWindow
& (RandomEq0 | NoBckof
) | StateBackOff
& RandomEqByteCnt
)
347 RetryCnt
[3:0] <= #Tp RetryCnt
[3:0] + 1'b1;
352 assign RetryMax
= RetryCnt
[3:0] == MaxRet
[3:0];
356 always @ (StatePreamble
or StateData
or StateData
or StateFCS
or StateJam
or StateSFD
or TxData
or
360 MTxD_d
[3:0] = TxData
[3:0]; // Lower nibble
363 MTxD_d
[3:0] = TxData
[7:4]; // Higher nibble
366 MTxD_d
[3:0] = {~Crc
[28], ~Crc
[29], ~Crc
[30], ~Crc
[31]}; // Crc
369 MTxD_d
[3:0] = 4'h9
; // Jam pattern
373 MTxD_d
[3:0] = 4'hd
; // SFD
375 MTxD_d
[3:0] = 4'h5
; // Preamble
382 always @ (posedge MTxClk
or posedge Reset
)
387 MTxEn
<= #Tp StatePreamble |
(|StateData
) | StatePAD | StateFCS | StateJam
;
392 always @ (posedge MTxClk
or posedge Reset
)
395 MTxD
[3:0] <= #Tp
4'h0
;
397 MTxD
[3:0] <= #Tp MTxD_d
[3:0];
402 always @ (posedge MTxClk
or posedge Reset
)
407 MTxErr
<= #Tp TooBig | UnderRun
;
412 always @ (posedge MTxClk
or posedge Reset
)
415 WillTransmit
<= #Tp
1'b0;
417 WillTransmit
<= #Tp StartPreamble | StatePreamble |
(|StateData
) | StatePAD | StateFCS | StateJam
;
421 assign PacketFinished_d
= StartTxDone | TooBig | UnderRun | LateCollision | MaxCollisionOccured | ExcessiveDeferOccured
;
425 always @ (posedge MTxClk
or posedge Reset
)
429 PacketFinished
<= #Tp
1'b0;
430 PacketFinished_q
<= #Tp
1'b0;
434 PacketFinished
<= #Tp PacketFinished_d
;
435 PacketFinished_q
<= #Tp PacketFinished
;
440 // Connecting module Counters
441 eth_txcounters
txcounters1 (.
StatePreamble(StatePreamble
), .
StateIPG(StateIPG
), .
StateData(StateData
),
442 .
StatePAD(StatePAD
), .
StateFCS(StateFCS
), .
StateJam(StateJam
), .
StateBackOff(StateBackOff
),
443 .
StateDefer(StateDefer
), .
StateIdle(StateIdle
), .
StartDefer(StartDefer
), .
StartIPG(StartIPG
),
444 .
StartFCS(StartFCS
), .
StartJam(StartJam
), .
TxStartFrm(TxStartFrm
), .
MTxClk(MTxClk
),
445 .
Reset(Reset
), .
MinFL(MinFL
), .
MaxFL(MaxFL
), .
HugEn(HugEn
), .
ExDfrEn(ExDfrEn
),
446 .
PacketFinished_q(PacketFinished_q
), .
DlyCrcEn(DlyCrcEn
), .
StartBackoff(StartBackoff
),
447 .
StateSFD(StateSFD
), .
ByteCnt(ByteCnt
), .
NibCnt(NibCnt
), .
ExcessiveDefer(ExcessiveDefer
),
448 .
NibCntEq7(NibCntEq7
), .
NibCntEq15(NibCntEq15
), .
MaxFrame(MaxFrame
), .
NibbleMinFl(NibbleMinFl
),
449 .
DlyCrcCnt(DlyCrcCnt
)
453 // Connecting module StateM
454 eth_txstatem
txstatem1 (.
MTxClk(MTxClk
), .
Reset(Reset
), .
ExcessiveDefer(ExcessiveDefer
), .
CarrierSense(CarrierSense
),
455 .
NibCnt(NibCnt
[6:0]), .
IPGT(IPGT
), .
IPGR1(IPGR1
), .
IPGR2(IPGR2
), .
FullD(FullD
),
456 .
TxStartFrm(TxStartFrm
), .
TxEndFrm(TxEndFrm
), .
TxUnderRun(TxUnderRun
), .
Collision(Collision
),
457 .
UnderRun(UnderRun
), .
StartTxDone(StartTxDone
), .
TooBig(TooBig
), .
NibCntEq7(NibCntEq7
),
458 .
NibCntEq15(NibCntEq15
), .
MaxFrame(MaxFrame
), .
Pad(Pad
), .
CrcEn(CrcEn
),
459 .
NibbleMinFl(NibbleMinFl
), .
RandomEq0(RandomEq0
), .
ColWindow(ColWindow
), .
RetryMax(RetryMax
),
460 .
NoBckof(NoBckof
), .
RandomEqByteCnt(RandomEqByteCnt
), .
StateIdle(StateIdle
),
461 .
StateIPG(StateIPG
), .
StatePreamble(StatePreamble
), .
StateData(StateData
), .
StatePAD(StatePAD
),
462 .
StateFCS(StateFCS
), .
StateJam(StateJam
), .
StateJam_q(StateJam_q
), .
StateBackOff(StateBackOff
),
463 .
StateDefer(StateDefer
), .
StartFCS(StartFCS
), .
StartJam(StartJam
), .
StartBackoff(StartBackoff
),
464 .
StartDefer(StartDefer
), .
DeferIndication(DeferIndication
), .
StartPreamble(StartPreamble
), .
StartData(StartData
), .
StartIPG(StartIPG
)
472 assign Enable_Crc
= ~StateFCS
;
474 assign Data_Crc
[0] = StateData
[0]? TxData
[3] : StateData
[1]? TxData
[7] : 1'b0;
475 assign Data_Crc
[1] = StateData
[0]? TxData
[2] : StateData
[1]? TxData
[6] : 1'b0;
476 assign Data_Crc
[2] = StateData
[0]? TxData
[1] : StateData
[1]? TxData
[5] : 1'b0;
477 assign Data_Crc
[3] = StateData
[0]? TxData
[0] : StateData
[1]? TxData
[4] : 1'b0;
479 assign Initialize_Crc
= StateIdle | StatePreamble |
(|DlyCrcCnt
);
482 // Connecting module Crc
483 eth_crc
txcrc (.
Clk(MTxClk
), .
Reset(Reset
), .
Data(Data_Crc
), .
Enable(Enable_Crc
), .
Initialize(Initialize_Crc
),
484 .
Crc(Crc
), .
CrcError(CrcError
)
488 // Connecting module Random
489 eth_random
random1 (.
MTxClk(MTxClk
), .
Reset(Reset
), .
StateJam(StateJam
), .
StateJam_q(StateJam_q
), .
RetryCnt(RetryCnt
),
490 .
NibCnt(NibCnt
), .
ByteCnt(ByteCnt
[9:0]), .
RandomEq0(RandomEq0
), .
RandomEqByteCnt(RandomEqByteCnt
));