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

相关推荐
客梦7 分钟前
数据结构-树结构
数据结构·笔记
点亮一颗LED(从入门到放弃)1 小时前
C语言学习笔记
笔记·学习
遇到困难睡大觉哈哈2 小时前
Harmony os ——ArkTS 语言笔记(五):泛型、空安全与可选链
前端·笔记·安全·harmonyos·鸿蒙
ljt27249606612 小时前
Compose笔记(五十七)--snapshotFlow
android·笔记·android jetpack
Ziegler Han3 小时前
《升维》阅读笔记:在不确定的世界里,如何做出高确定性的决策
笔记·《升维》
工程师平哥3 小时前
APE-01 新建工程
笔记·嵌入式硬件
泓博3 小时前
Android摇一摇
笔记
wdfk_prog4 小时前
[Linux]学习笔记系列 -- [block]kyber-iosched
linux·笔记·学习
摇滚侠5 小时前
零基础小白自学 Git_Github 教程,分支合并,笔记13
笔记·git·github
灰灰勇闯IT5 小时前
隐语MOOC三期笔记:可信数据空间实战课——从“数据孤岛”到“安全流通”,企业落地的3个关键步骤(附部署脚本)
笔记·安全