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

相关推荐
Gary Studio11 分钟前
simulink simscape(机器人方向)学习笔记
笔记·学习
Zeku12 分钟前
20260111 - Linux驱动学习笔记:异步通知
笔记·stm32·freertos·linux驱动开发·linux应用开发
wdfk_prog32 分钟前
[Linux]学习笔记系列 -- 内存管理与访问
linux·笔记·学习
go_bai33 分钟前
Linux-网络基础
linux·开发语言·网络·笔记·学习方法·笔记总结
崎岖Qiu33 分钟前
【OS笔记38】:设备管理 - I/O 设备原理
笔记·操作系统·os·设备管理·io设备
代码游侠2 小时前
学习笔记——HC-SR04 超声波测距传感器
开发语言·笔记·嵌入式硬件·学习
Abbylolo2 小时前
《Obsidian Excalidraw插件配置与使用指南》
笔记
@zulnger2 小时前
python 学习笔记(闭包)
笔记·python·学习
AomanHao3 小时前
【阅读笔记】Bayer阵列坏点校正-《Adaptive pixel defect correction》
图像处理·笔记·isp·坏点补偿
yewq-cn3 小时前
Joplin 客户端与服务端
笔记