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

相关推荐
Hello_Embed1 小时前
嵌入式上位机开发入门(三):TCP 编程 —— Server 端实现
笔记·单片机·网络协议·tcp/ip·嵌入式
talen_hx2962 小时前
《零基础入门Spark》学习笔记 Day 11
笔记·学习·spark
ZhiqianXia3 小时前
gem5 模拟器学习笔记(1):核心术语整理
笔记·学习
凌波粒4 小时前
D2L学习笔记:安装、张量与数据处理
笔记·python·学习·pandas
taoqick4 小时前
FIPO粗读笔记
笔记
半壶清水5 小时前
[软考网规考点笔记]-局域网之以太网标准
网络·笔记·网络协议·考试
忙什么果5 小时前
transformer学习笔记2
笔记·学习·transformer
ZhiqianXia5 小时前
Gem5 学习笔记(2) : Gem5 建模要点与基本思路
笔记·学习
_李小白6 小时前
【OSG学习笔记】Day 25: OSG 设计架构解析
笔记·学习·架构
后藤十八里6 小时前
维普期刊逆向笔记
javascript·笔记·js逆向