我正在调试一段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