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

相关推荐
yuxb731 小时前
Kubernetes 核心概念与微服务架构解析
笔记·kubernetes
d111111111d2 小时前
关于STM32的选项字节的问题:如果我通过操作指针把数据写在了单片机的选项字节区域那么换别的程序时候数据会进行变化吗?
笔记·stm32·单片机·嵌入式硬件·学习
ouliten3 小时前
C++笔记:std::stringbuf
开发语言·c++·笔记
安如衫4 小时前
【机器学习基础】Attention in Transformers:注意力机制
笔记·深度学习·学习·机器学习·注意力机制
十安_数学好题速析5 小时前
幂次之争:巧用对称性破解指数不等式
笔记·学习·高考
せいしゅん青春之我5 小时前
【JavaEE进阶】JVM-面试中的高频考点1
java·网络·jvm·笔记·面试·java-ee
一起养小猫5 小时前
《枕边算法书》阅读笔记:一场从热爱到实践的算法启蒙之旅
笔记
IMPYLH6 小时前
Lua 的 pairs 函数
开发语言·笔记·后端·junit·单元测试·lua
摇滚侠7 小时前
Vue 项目实战《尚医通》,利用 Qrcode 获取二维码,笔记51
vue.js·笔记
星星20258 小时前
新能源汽车六大变革重塑中国汽车制造格局
笔记·团队开发