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

相关推荐
卡提西亚26 分钟前
C++笔记-34-map/multimap容器
开发语言·c++·笔记
一个平凡而乐于分享的小比特2 小时前
UCOSIII笔记(十三)CPU利用率及栈检测统计与同时等待多个内核对象
笔记·ucosiii
摇滚侠3 小时前
2025最新 SpringCloud 教程,编写微服务 API,笔记08
笔记·spring cloud·微服务
我的老子姓彭5 小时前
N32WB蓝牙芯片开发
笔记
历程里程碑5 小时前
各种排序法大全
c语言·数据结构·笔记·算法·排序算法
hd51cc6 小时前
MFC多线程学习笔记三:线程间的通信
笔记·学习
hd51cc6 小时前
MFC多线程学习笔记四:线程间的同步
笔记·学习·mfc
星空的资源小屋6 小时前
VNote:程序员必备Markdown笔记神器
javascript·人工智能·笔记·django
wdfk_prog6 小时前
[Linux]学习笔记系列 -- [block]bfq-iosched
linux·笔记·学习
摇滚侠6 小时前
Vue 项目实战《尚医通》,实名认证模块静态的搭建,笔记53
vue.js·笔记