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

相关推荐
Lorcian2 小时前
web前端12--表单和表格
前端·css·笔记·html5·visual studio code
想拿高薪的韭菜6 小时前
人工智能第2章-知识点与学习笔记
人工智能·笔记·学习
一只码代码的章鱼7 小时前
计算机网络 应用层 笔记 (电子邮件系统,SMTP,POP3,MIME,IMAP,万维网,HTTP,html)
笔记·计算机网络·microsoft
学游戏开发的8 小时前
UE求职Demo开发日志#19 给物品找图标,实现装备增加属性,背包栏UI显示装备
c++·笔记·游戏引擎·unreal engine
云缘若仙8 小时前
directx12 3d+vs2022游戏开发第三章 笔记五 变换
笔记·3d
前端达人10 小时前
「AI学习笔记」深度学习进化史:从神经网络到“黑箱技术”(三)
人工智能·笔记·深度学习·神经网络·学习
wdxylb12 小时前
GIt使用笔记大全
笔记·git·elasticsearch
一只码代码的章鱼13 小时前
计算机网络 应用层 笔记1(C/S模型,P2P模型,FTP协议)
笔记·计算机网络
霸王蟹13 小时前
el-table组件样式如何二次修改?
前端·javascript·vue.js·笔记·学习·前端框架
haidizym13 小时前
(笔记+作业)书生大模型实战营春节卷王班---L1G3000 浦语提示词工程实践
笔记