Verilog查找数组最大值模块输出异常问题咨询
Verilog最大值查找模块错误诊断与修复
核心错误点
- 时序逻辑赋值规则错误:所有由时钟沿触发的时序逻辑
always块中,你全部使用了阻塞赋值=,这会导致仿真过程中逻辑执行顺序不确定,出现竞争冒险,最终仿真结果和FPGA实际运行结果不一致,是输出异常的核心原因。时序逻辑必须统一使用非阻塞赋值<=。 - 常量数组被综合优化:
corr_Output数组的所有元素在复位、工作状态下均为固定常量,没有可变输入修改其数值,综合器会直接将该数组优化为常量连线,不会生成对应寄存器,这就是你综合后只能观测到少量寄存器的原因。 - 同沿采样时序冲突:两个
always块都在同一个时钟上升沿触发,阻塞赋值的存在会导致负责计算最大值的逻辑块采样corr_Output时,无法确定采样到的是更新前还是更新后的值,输出结果完全不可控。
修正后代码
module findmaximum( input clk, input rst, output reg [3:0] max, output reg [3:0] index ); reg [3:0] corr_Output[0:9]; integer i; always@(posedge clk or posedge rst) begin if(rst) begin corr_Output[0] <= 0; corr_Output[1] <= 0; corr_Output[2] <= 0; corr_Output[3] <= 0; corr_Output[4] <= 0; corr_Output[5] <= 0; corr_Output[6] <= 0; corr_Output[7] <= 0; corr_Output[8] <= 0; corr_Output[9] <= 0; end else begin corr_Output[0] <= 0; corr_Output[1] <= 0; corr_Output[2] <= 0; corr_Output[3] <= 0; corr_Output[4] <= 7; corr_Output[5] <= 0; corr_Output[6] <= 0; corr_Output[7] <= 0; corr_Output[8] <= 0; corr_Output[9] <= 0; end end always@(posedge clk or posedge rst) begin if(rst) begin max <= 0; index <= 0; end else begin max <= corr_Output[0]; index <= 0; for (i = 0; i <= 9; i = i + 1) begin if (corr_Output[i] > max) begin max <= corr_Output[i]; index <= i; end end end end endmodule
验证说明
修正后复位释放后的第二个时钟上升沿,输出max会更新为7,index会更新为4,符合预期。如果后续需要适配可变输入数组,只需新增输入端口替换掉corr_Output的固定赋值逻辑即可。
内容的提问来源于stack exchange,提问作者Arslan Majid
相关产品推荐
相关产品推荐

