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

相关推荐
深圳卢先生5 小时前
CentOS 安装jenkins笔记
笔记·centos·jenkins
u_topian5 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
WarPigs10 小时前
游戏框架笔记
笔记·游戏·架构
金心靖晨10 小时前
redis汇总笔记
数据库·redis·笔记
遇见尚硅谷11 小时前
C语言:20250714笔记
c语言·开发语言·数据结构·笔记·算法
Norvyn_712 小时前
LeetCode|Day11|557. 反转字符串中的单词 III|Python刷题笔记
笔记·python·leetcode
逼子格12 小时前
权电阻网络DAC实现电压输出型数模转换Multisim电路仿真——硬件工程师笔记
笔记·嵌入式硬件·硬件工程·硬件工程师·adc·硬件工程师真题·权电阻网络dac
Jyywww12112 小时前
慕尚花坊项目笔记
笔记
xd0000213 小时前
ethers.js-5–和solidity的关系
笔记
星辰大海141214 小时前
AI Linux 运维笔记
运维·笔记