always_ff模块Multiple Driver错误原因及解决建议
Verilog always_ff多驱动错误分析与解决
错误场景
你编写的状态机控制模块代码如下:
module control( output reg[2:0]DiceValue, input Clock, nReset, [1:0]Ran ); timeunit 1ns; timeprecision 10ps; logic clock_count = 0; logic present_stage, next_stage; always_ff @ (posedge Clock, negedge nReset) begin if (!nReset) begin present_stage <= 1; next_stage <= 1; clock_count <= 0; end else begin clock_count <= clock_count+1; end if (clock_count == 1) begin clock_count <= 0; present_stage <= next_stage; end end
运行后触发错误:Multiple drivers to always_ff output variable clock_count detected.
错误原因
clock_count在同一个always_ff块里被两个独立的分支赋值:
else分支里执行clock_count <= clock_count+1;- 独立的
if (clock_count == 1)分支里执行clock_count <= 0;
这两个分支没有用互斥逻辑关联,当clock_count为1时,时钟沿到来后,两个分支的条件会同时成立,导致clock_count被同时赋予两个不同的值,违反了Verilog时序逻辑块的规则——同一个时序变量在一个always_ff块中只能有唯一的赋值路径,或者通过互斥的条件分支确保每次只有一个赋值生效。
解决建议
- 合并互斥条件分支:把两个对
clock_count的赋值逻辑用if-else嵌套,确保每个时钟沿下变量只有一个赋值动作。修改后的always_ff块示例:
always_ff @ (posedge Clock, negedge nReset) begin if (!nReset) begin present_stage <= 1; next_stage <= 1; clock_count <= 0; end else begin if (clock_count == 1) begin clock_count <= 0; present_stage <= next_stage; end else begin clock_count <= clock_count + 1; end end end
- 补充状态转移逻辑:当前代码仅在复位时给
next_stage赋值1,后续没有更新逻辑,会导致状态切换不符合预期,需要根据需求添加next_stage的状态转移赋值代码。
内容的提问来源于stack exchange,提问作者Jingxuan Cai
相关产品推荐
相关产品推荐

