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

相关推荐
一轮弯弯的明月9 小时前
贝尔数求集合划分方案总数
java·笔记·蓝桥杯·学习心得
航Hang*9 小时前
第3章:Linux系统安全管理——第2节:部署代理服务
linux·运维·服务器·开发语言·笔记·系统安全
zjnlswd10 小时前
tkinter学习案例--笔记代码
笔记·学习
独小乐10 小时前
009.中断实践之实现按键测试|千篇笔记实现嵌入式全栈/裸机篇
linux·c语言·驱动开发·笔记·嵌入式硬件·arm
无聊大侠hello world10 小时前
Yu-AI-Agent 项目(AI 恋爱大师智能体) · 学习笔记
人工智能·笔记·学习
CheerWWW10 小时前
C++学习笔记——箭头运算符、std::vector的使用、静态链接、动态链接
c++·笔记·学习
ZhiqianXia11 小时前
Pytorch 学习笔记(17):decompositions.py —— 算子分解的百科全书
pytorch·笔记·学习
xian_wwq11 小时前
【学习笔记】大模型如何理解图片
笔记·学习
talen_hx29611 小时前
《零基础入门Spark》学习笔记 Day 13
笔记·学习·spark
Flittly11 小时前
【SpringAIAlibaba新手村系列】(15)MCP Client 调用本地服务
java·笔记·spring·ai·springboot