Verilog刷题笔记19

题目:

A common source of errors: How to avoid making latches

When designing circuits, you must think first in terms of circuits:

I want this logic gate

I want a combinational blob of logic that has these inputs and produces these outputs

I want a combinational blob of logic followed by a set of flip-flops

What you must not do is write the code first, then hope it generates a proper circuit.

If (cpu_overheated) then shut_off_computer = 1;

If (~arrived) then keep_driving = ~gas_tank_empty;

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.

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.

解题:

bash 复制代码
module top_module (
    input      cpu_overheated,
    output reg shut_off_computer,
    input      arrived,
    input      gas_tank_empty,
    output reg keep_driving  ); //

    always @(*) begin
        if (cpu_overheated)
           shut_off_computer = 1;
        else
            shut_off_computer=0;
            
    end

    always @(*) begin
        if (~arrived)
          keep_driving = ~gas_tank_empty;
        else
            keep_driving =0;
    end

endmodule

结果正确:

本题注意点:
组合逻辑中,如果if缺少else语句,或是自己赋值给自己的情况就会产生latch。为了避免latch产生,if需要加上else语句,覆盖所有可能。

相关推荐
浅念-14 分钟前
C++ 异常
开发语言·数据结构·数据库·c++·经验分享·笔记·学习
ycjunhua43 分钟前
Gool NoteBookLM 创建无法进入开发界面
笔记·学习
今儿敲了吗1 小时前
python基础学习笔记第四章
c++·笔记·python·学习
【数据删除】3481 小时前
计算机复试学习笔记 Day44
笔记·学习
星轨初途2 小时前
类和对象(上)
开发语言·c++·经验分享·笔记
留院极客离心圆2 小时前
C++ 进阶笔记:宏
开发语言·c++·笔记
蒸蒸yyyyzwd2 小时前
设计模式之美学习笔记
笔记·学习·设计模式
勇气要爆发2 小时前
吴恩达《LangChain LLM 应用开发精读笔记》8-Document Loading 文档加载
笔记·langchain·dreamweaver
中屹指纹浏览器3 小时前
2026年设备指纹追踪与浏览器虚拟环境技术深度解析——从风控原理到安全隔离实践
经验分享·笔记
星轨初途3 小时前
郑州轻工业大学“筑梯杯” 2025级新生程序设计大赛暨省内高校邀请赛——题解
android·c++·经验分享·笔记·算法