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

相关推荐
zjeweler3 小时前
“网安+护网”终极300多问题面试笔记-3共3-综合题型(最多)
笔记·网络安全·面试·职场和发展·护网行动
Elaine3369 小时前
【软件测试系统学习笔记:从理论基础到接口实战】
软件测试·笔记·学习·接口测试
xuhaoyu_cpp_java9 小时前
Maven学习(二)
java·经验分享·笔记·学习·maven
智者知已应修善业9 小时前
【51单片机4位数循环小数位移数值位移】2023-6-9
c++·经验分享·笔记·算法·51单片机
雾喔10 小时前
【学习笔记3】AI 工程实战
人工智能·笔记·学习
qeen8711 小时前
【算法笔记】时间复杂度与空间复杂度
笔记
_李小白12 小时前
【OSG学习笔记】Day 50: Text与Font
笔记·学习
于慨12 小时前
flutter开发笔记
笔记
jimmyleeee12 小时前
人工智能基础知识笔记三十九:几个Skills的网站
人工智能·笔记·chatgpt
Hello_Embed12 小时前
嵌入式上位机开发入门(二十二):RTU/TCP 双协议互斥访问寄存器
笔记·网络协议·tcp/ip·嵌入式