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

相关推荐
xzal124 小时前
python中,turtle基础知识笔记1
笔记·python·turtle
鱼鳞_6 小时前
Java学习笔记_Day29(异常)
java·笔记·学习
九成宫7 小时前
IT项目管理期末复习——Chapter 8 项目质量管理
笔记·项目管理·软件工程
Flittly7 小时前
【SpringSecurity新手村系列】(3)自定义登录页与表单认证
java·笔记·安全·spring·springboot
Stella Blog8 小时前
狂神Java基础学习笔记Day04
java·笔记·学习
一只机电自动化菜鸟8 小时前
一建机电备考笔记(17) 常用设备—通用设备1(含考频+题型)
笔记·学习·职场和发展·生活·学习方法
bekote8 小时前
笔记|数据库
数据库·笔记
深蓝海拓8 小时前
基于QtPy (PySide6) 的PLC-HMI工程项目(十)框架初成的阶段总结
网络·笔记·python·学习·ui·plc
戏舟的嵌入式开源笔记8 小时前
LVGL部件应用笔记(基于正点原子教程,持续更新)
笔记
九成宫8 小时前
IT项目管理期末复习——Chapter 7 项目成本管理
笔记·项目管理·软件工程