我正在調試一段Verilog代碼,特別是從FX2LP(Cypress CY7C68016A)USB控制器發送和接收字節。沒有進入許多細節,數據在每個週期中按字節發送和傳輸。對於我的測試,我使用了一個16字節的緩衝區,我首先填充然後傳回(回波測試)。順序模塊中的位移失敗,組合不成功。爲什麼?
我的代碼的顯著部分看起來像:
reg [127:0] dataBuf; // 16 byte buffer for USB data
reg [7:0] cntByte; // counter for number of bytes
reg [7:0] nextCntByte;
reg shiftBufRx, shiftBufTx; // flags whether buffer should be shifted
reg [7:0] currentByte; // current read byte
// in transmit cycle, byte is read from USB_DATAOUT
assign USB_DATAOUT = dataBuf[7:0];
always @(posedge FIFO_CLK) begin
// update state variables
CurrentState <= NextState;
cntByte <= nextCntByte;
if(shiftBufRx) begin // cycle was a receive
dataBuf <= { currentByte , dataBuf[127:8] };
end
if(shiftBufTx) begin // cycle was a transmit
dataBuf <= { dataBuf[127-8:0] , 8'h00 };
end
end
always @(*) begin
// avoid race conditions
NextState = CurrentState;
nextCntByte = cntByte;
nextDataBuf = dataBuf;
currentByte = 0;
shiftBufRx = 0;
shiftBufTx = 0;
case(CurrentState)
[...]
STATE_USBRX: begin
if(cntByte < 16) begin
nextCntByte = cntByte + 1;
currentByte = USB_DATAIN; // contains received byte in receive cycle
shiftBufRx = 1; // shift buffer after this cycle
end
[...]
end
STATE_USBTX: begin
if(cntByte < 15) begin
shiftBufTx = 1; // shift buffer after this cycle
nextCntByte = cntByte + 1;
end
[...]
end
[...]
endcase
end
此代碼工作完美地模擬(iVerilog)。但是當在Altera Cyclone上進行綜合和執行時,我會遇到非常奇怪的錯誤。例如,大多數情況下,每個字節讀取傳輸到FPGA的第一個字節。例如,發送11 22 33 44 55 66 ...
將收到11 11 11 11 11 11 ...
。
現在,當我代替引入新的變量:
reg [127:0] nextDataBuf;
和在順序always @(posedge FIFO_CLK)
塊與更換部分:
if(shiftBufRx) begin
dataBuf <= nextDataBuf;
end
if(shiftBufTx) begin
dataBuf <= nextDataBuf;
end
,並在組合部分:
STATE_USBRX: begin
if(cntByte < 16) begin
nextCntByte = cntByte + 1;
//currentByte = FIFO_DATAIN;
nextDataBuf = { dataBuf[127-8:0] , FIFO_DATAIN };
shiftBufRx = 1;
end
[...]
end
STATE_USBTX: begin
if(cntByte < 15) begin
shiftBufTx = 1;
nextCntByte = cntByte + 1;
nextDataBuf = { 8'h00 , dataBuf[127:8] };
end
[...]
end
然後它工作!
這意味着:我所做的只是將寄存器從順序塊移到組合塊。
我的代碼和仿真(iVerilog)中看不到任何競態條件,兩個版本都是相同的。
可能是什麼原因?
檢查Tx/Rx標誌之間的關係。如果兩者同時出現,可能會發生奇怪的唱歌。 – Serge
您的數據結構不匹配。 '{dataBuf [127-8:0],8'h00}'!='{8'h00,dataBuf [127:8]}' – Greg