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

相关推荐
丝斯201138 分钟前
AI学习笔记整理(22)—— AI核心技术(深度学习6)
人工智能·笔记·学习
koo36442 分钟前
pytorch深度学习笔记1
pytorch·笔记·深度学习
jimmyleeee1 小时前
人工智能基础知识笔记二十一:Function Calling
人工智能·笔记
丝斯20112 小时前
AI学习笔记整理(21)—— AI核心技术(深度学习5)
人工智能·笔记·学习
风123456789~2 小时前
【健康管理】第5章 流行病学、医学统计学 1/2
笔记·考证·健康管理
closejudge4 小时前
部署siyuan笔记docker问题记录
笔记
摇滚侠4 小时前
零基础小白自学Git_Github教程,GitHubDeskTop安装,笔记10
笔记·git·github
摇滚侠4 小时前
零基础小白自学 Git_Github 教程,GitHub 是如何工作的,笔记08
笔记·git·github
googleccsdn4 小时前
ENSP Pro Lab笔记:配置STP/RSTP/MSTP(7)
网络·笔记·网络协议
Anesthesia丶5 小时前
Tesla P40显卡使用 nunchaku 部署 Fluex.1-dev笔记
笔记