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

相关推荐
小韩博2 分钟前
小迪笔记45课之-PHP应用&SQL二次注入&堆叠执行&DNS带外&功能点&黑白盒条件
笔记·sql·网络安全·php
智嵌电子3 分钟前
【笔记篇】【硬件基础篇】模拟电子技术基础 (童诗白) 第6章 信号的运算和处理
笔记
virtual_k1smet17 分钟前
梧桐·鸿鹄-中移链assistant-level
笔记·区块链
丝斯20111 小时前
AI学习笔记整理(36)——自然语言处理
人工智能·笔记·学习
94621931zyn62 小时前
观影统计 - Cordova 与 OpenHarmony 混合开发实战
笔记
virtual_k1smet2 小时前
梧桐·鸿鹄-大数据professional
大数据·笔记
stars-he2 小时前
单相双半波可控整流电路的MATLAB仿真设计
笔记·学习·matlab
im_AMBER2 小时前
Leetcode 87 等价多米诺骨牌对的数量
数据结构·笔记·学习·算法·leetcode
今儿敲了吗2 小时前
计算机网络第一章笔记
笔记·计算机网络
yuxb733 小时前
Jenkins 流水线:镜像仓库与自动化部署
笔记·jenkins