计数器模块提供数据发送过程中需要的所有计数器:DlyCrcCnt 用来在 CRC 校验序列产生过程中计数;按照 4 位传输时采用 NibCnt 计数;按照字节传输时采用 ByteCnt 计数。计数器模块的主要代码如下:
- //四位传输计数器
- always @ (posedge MTxClk or posedge Reset)
- begin
- if(Reset)
- NibCnt <= #Tp 16'h0;
- else
- begin
- if(ResetNibCnt)
- NibCnt <= #Tp 16'h0;
- else
- if(IncrementNibCnt)
- NibCnt <= #Tp NibCnt + 1'b1;
- end
- end
- assign NibCntEq7 = &NibCnt[2:0];
- assign NibCntEq15 = &NibCnt[3:0];
- assign NibbleMinFl = NibCnt >= (((MinFL-3'h4)<<1) -1); // FCS should not be included inNibbleMinFl
- assign ExcessiveDeferCnt = NibCnt[13:0] == 16'h17b7;
- assign ExcessiveDefer = NibCnt[13:0] == 16'h17b7 & ~ExDfrEn; // 6071 nibbles
- assign IncrementByteCnt = StateData[1] & ~ByteCntMax & ~|DlyCrcCnt[2:0]
- | StateBackOff & (&NibCnt[6:0])
- | (StatePAD | StateFCS) & NibCnt[0] & ~ByteCntMax;
- assign ResetByteCnt = StartBackoff | StateIdle & TxStartFrm | PacketFinished_q;
- // 字节传输计数器
- always @ (posedge MTxClk or posedge Reset)
- begin
- if(Reset)
- ByteCnt[15:0] <= #Tp 16'h0;
- else
- begin
- if(ResetByteCnt)
- ByteCnt[15:0] <= #Tp 16'h0;
- else
- if(IncrementByteCnt)
- ByteCnt[15:0] <= #Tp ByteCnt[15:0] + 1'b1;
- end
- end
- assign MaxFrame = ByteCnt[15:0] == MaxFL[15:0] & ~HugEn;
- assign ByteCntMax = &ByteCnt[15:0];
- //CRC 计数器
- always @ (posedge MTxClk or posedge Reset)
- begin
- if(Reset)
- DlyCrcCnt <= #Tp 3'h0;
- else
- begin
- if(StateData[1] & DlyCrcCnt == 3'h4 | StartJam | PacketFinished_q)
- DlyCrcCnt <= #Tp 3'h0;
- else
- if(DlyCrcEn & (StateSFD | StateData[1] & (|DlyCrcCnt[2:0])))
- DlyCrcCnt <= #Tp DlyCrcCnt + 1'b1;
- end
- end