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

相关推荐
梨子串桃子_13 小时前
推荐系统学习笔记 | PyTorch学习笔记
pytorch·笔记·python·学习·算法
laplace012315 小时前
# 第六章 agent框架开发实践 - 学习笔记
人工智能·笔记·学习·语言模型·agent
坚持不懈的大白16 小时前
Leetcode学习笔记
笔记·学习·leetcode
中屹指纹浏览器16 小时前
双GAN网络驱动的动态指纹生成技术深度解析——原理、实现与抗检测优化
经验分享·笔记
JeffDingAI17 小时前
【Datawhale学习笔记】基于Gensim的词向量实战
人工智能·笔记·学习
FakeOccupational19 小时前
【电路笔记 PCB】Altium Designer : AD使用教程+Altium Designer常见AD操作命令与流程
开发语言·笔记
Hello_Embed19 小时前
RS485 双串口通信 + LCD 实时显示(DMA+IDLE 空闲中断版)
笔记·单片机·学习·操作系统·嵌入式·freertos
小乔的编程内容分享站19 小时前
C语言指针相关笔记
c语言·笔记
逐步前行19 小时前
SolidWorks2024_装配体实例(桌下抽屉)
笔记
_ziva_20 小时前
Miniconda 下载 + 安装 + VS Code 集成使用教程
笔记