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语句,覆盖所有可能。

相关推荐
Yang_jie_0311 小时前
笔记:数据结构(顺序表)
数据结构·windows·笔记
Tom@敲代码14 小时前
js学习笔记-01
javascript·笔记·学习
巴巴媛66616 小时前
STM32学习笔记【32.BKP + RTC】
笔记·stm32·学习
能摆一天是一天16 小时前
Spring ai vectorstore 使用本地模型导致只能匹配可行度为1.0的内容的解决方法笔记
java·笔记·spring
进阶的DW17 小时前
Wiki + Graph + RAG 知识库建设笔记2
笔记
AOwhisky17 小时前
Linux(CentOS)系统管理入门笔记(第四期)——文件系统(下篇):文件与目录操作实战——cpmvmkdirrmln
linux·运维·笔记·centos·云计算·文件系统
bmy-happy17 小时前
网安专业课程笔记sale
笔记
浩瀚地学18 小时前
【Java基础复习】IO流(二)
java·开发语言·经验分享·笔记·学习
智者知已应修善业19 小时前
【利用proteus设计微型CPU,完成寻址相加数据的过程。】2025-2-26
驱动开发·经验分享·笔记·硬件架构·proteus·硬件工程
学计算机的计算基20 小时前
二叉树算法下篇:递归核心技巧与高频面试题详解
java·笔记·算法