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

相关推荐
自小吃多10 小时前
本地部署大模型避坑实录|Ollama+AnythingLLM 一直加载、CPU 爆满、GPU 闲置问题完整解决
笔记
我命由我1234512 小时前
Windows 操作系统 - Windows 查看架构类型
运维·windows·笔记·学习·系统架构·运维开发·系统
金蕊泛流霞12 小时前
dify安装教程
笔记
IOT.FIVE.NO.113 小时前
Codex Skill 内部结构解析:从 SKILL.md 到 scripts、references、assets
前端·javascript·人工智能·笔记·html
AI精钢14 小时前
把 Markdown 笔记变成可问答的知识图谱:本地 Graph RAG 工具 Kwipu 实测
人工智能·笔记·python·aigc·知识图谱
kobesdu14 小时前
【ROS2实战笔记-15】ros2bag 的深度应用:从数据回放到系统级离线分析
人工智能·笔记·移动机器人·ros2
晓梦林14 小时前
Loooower靶场学习笔记
笔记·学习·安全·web安全
我命由我1234514 小时前
前端开发概念 - 无障碍树
javascript·css·笔记·学习·html·html5·js
沉浸式学习ing17 小时前
网课视频里的PPT怎么提取?视频转图文讲义的实操教程
笔记·ai·aigc·学习方法·视频·ppt
今儿敲了吗18 小时前
链表篇(一)——合并两个有序链表
数据结构·笔记·算法·链表