FPGA Verilog 设计中避免生成锁存器

A common source of errors: How to avoid making latches

Syntactically-correct code does not necessarily result in a reasonable circuit (combinational logic + flip-flops). The usual reason is: “What happens in the cases other than those you specified?”. Verilog’s answer is: Keep the outputs unchanged.
语法正确的代码并不一定会产生合理的电路(组合逻辑+触发器)。通常的理由是:“在你指定的情况之外会发生什么?”Verilog的答案是:保持输出不变。

This behaviour of “keep outputs unchanged” means the current state needs to be remembered, and thus produces a latch. Combinational logic (e.g., logic gates) cannot remember any state. Watch out for Warning (10240): … inferring latch(es)" messages. Unless the latch was intentional, it almost always indicates a bug. Combinational circuits must have a value assigned to all outputs under all conditions. This usually means you always need else clauses or a default value assigned to the outputs
这种“保持输出不变”的行为意味着需要记住当前状态,从而产生一个锁存器。组合逻辑(例如,逻辑门)不能记住任何状态。注意警告“Warning (10240): … inferring latch(es)”消息。除非锁存器是故意的,否则它几乎总是表明有漏洞。在所有条件下,组合电路必须为所有输出分配一个值。这通常意味着您总是需要为输出分配else子句或默认值

以如下两种情况为例
未正确使用if语句
未正确使用case语句,缺少default项

示例:
A 错误示例

//How to avoid making latches
//make lathes

module NO_latches (
    input      d,
	input      c,
    output reg q,
    input [1:0]a,
    output reg b
	 );

    always @(*) begin //保持原来的值产生锁存器
        if (d)
           q = c;
    end
    

    always @(*) begin
        case(a)
				2'b00:b = 1'b0;
				2'b11:b = 1'b1;
		  endcase
    end

endmodule

RTL Viewer

if 语句和 case语句都综合得到了锁存器

B 没有锁存器示例

//How to avoid making latches
//NO lathes

module NO_latches (
    input      d,
	input      c,
    output reg q,
    input [1:0]a,
    output reg b
	);

    always @(*) begin //保持原来的值产生锁存器
        if (d)
           q = c;
        else 
           q = 1'b0;
    end
    

    always @(*) begin
        case(a)
				2'b00:b = 1'b0;
				2'b11:b = 1'b1;
				default:b = 1'b0;
		  endcase
    end

endmodule

RTL Viewer
在这里插入图片描述
if 语句和 case语句都没有综合得到锁存器

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值