当前位置: 首页 > article >正文

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.
在这里插入图片描述
解题:

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


http://www.kler.cn/a/231023.html

相关文章:

  • 响应式设计的基本原理和实现方法(超级详细)
  • nginx限制网段访问
  • 鸿蒙开发系列教程(十六)--日志处理
  • java springBoot项目实现数据脱敏的策略
  • qt QMessagbox的按钮的顺序
  • 洛谷_P5461 赦免战俘_python写法
  • Win32 SDK Gui编程系列之--ListView自绘OwnerDraw(续)
  • 黑马Java——集合进阶(List、Set、泛型、树)
  • C++服务器端开发(1):设计服务器架构
  • 使用python启动一个roslaunch文件
  • c#内置委托
  • 最新AI系统ChatGPT网站H5系统源码,支持Midjourney绘画局部编辑重绘,GPT语音对话+ChatFile文档对话总结+DALL-E3文生图
  • 对接快团团,如何快速高效对接快团团大团长?
  • 编程笔记 html5cssjs 075 Javascript 常量和变量
  • 使用 Visual Studio Code 在远程计算机上调试 PostgreSQL
  • FMECA实施步骤——FMEA软件
  • 2.5学习总结9
  • 电商数据采集:选择爬虫工具还是第三方API?
  • HarmonyOS 鸿蒙应用开发(十、第三方开源js库移植适配指南)
  • 构建中国人自己的私人GPT—支持中文