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

fpga系列 HDL:verilog latch在fpga中的作用 避免latch的常见做法

目录

    • Latch在FPGA中的作用
    • Quartus中有关latch的警告⚠
    • 避免Latch的常见做法
      • 1. if-else 语句未覆盖所有条件
        • 生成Latch的代码:
        • 修复后的代码:
      • 2. case语句未覆盖所有分支
        • 生成Latch的代码:
        • 修复后的代码:
      • 3. 组合逻辑中缺少默认赋值
        • 生成Latch的代码:
        • 修复后的代码:
      • 4. 多路选择器中未处理所有输入
        • 生成Latch的代码:
        • 修复后的代码:
      • 5. 部分条件未赋值
        • 生成Latch的代码:
        • 修复后的代码:
      • 6. 反馈路径中的Latch
        • 生成Latch的代码:
        • 修复后的代码:
  • CG

Latch在FPGA中的作用

  • Latch是一种电平敏感的存储元件,与边沿触发的触发器(Flip-Flop)不同,它会在使能信号有效期间保持其状态。
  • 一般地,Latch可以用于临时存储数据或作为组合逻辑的一部分来保存某些中间结果。
  • 但在现代FPGA设计中,Latch通常不是首选。FPGA 中应避免使用电平敏感的透明锁存器,而应仅使用边沿敏感的触发器。锁存器使布局和布线工具无法灵活地满足时序要求。
  • 如果设计中某些 latch 在不该传递时还在传递信号,会让电路行为不可预测,调试也更困难。级联触发器的效果是可预测的,与深度无关。相比之下,锁存器输出的时序则更加“模拟”。信号进入锁存器的延迟会导致信号输出的延迟。即使锁存器自身的约束得到满足,这种延迟也可能导致下游问题。

Quartus中有关latch的警告⚠

  • 如果代码中生成了Latch,会收到类似如下的警告:Warning (10240): Verilog HDL Always Construct warning at test.v(8): inferring latch(es) for variable “q”, which holds its previous value in one or more paths through the always construct

在这里插入图片描述

避免Latch的常见做法

  • 当在组合逻辑(如if-elsecase语句)中没有处理所有可能的输入条件时,如果某个信号在某些条件下没有被明确赋值,则综合工具可能会推断出Latch以维持该信号的前一状态。在一些复杂的组合逻辑中,存在从输出到输入的直接反馈路径也可能导致Latch的生成。

1. if-else 语句未覆盖所有条件

生成Latch的代码:
module test (input wire sel,input wire in1,output reg out
);always @(*) beginif (sel == 1'b1) beginout = in1;end// 没有 else 分支end
endmodule

在这里插入图片描述
在这里插入图片描述

修复后的代码:
module test (input wire sel,input wire in1,input wire in2, // 添加额外输入output reg out
);always @(*) beginout = in2; // 默认值if (sel == 1'b1) beginout = in1;endend
endmodule

在这里插入图片描述

2. case语句未覆盖所有分支

生成Latch的代码:
module test (input wire [1:0] state,input wire in1,input wire in2,output reg out
);always @(*) begincase (state)2'b00: out = in1;2'b01: out = in2;// 没有 default 分支endcaseend
endmodule

在这里插入图片描述

修复后的代码:
module test (input wire [1:0] state,input wire in1,input wire in2,output reg out
);always @(*) begincase (state)2'b00: out = in1;2'b01: out = in2;default: out = 1'b0; // 添加默认值endcaseend
endmodule

在这里插入图片描述

3. 组合逻辑中缺少默认赋值

生成Latch的代码:
module test (input wire enable,input wire data_in,output reg out
);always @(*) beginif (enable) beginout = data_in;end// 没有 else 分支end
endmodule

在这里插入图片描述

修复后的代码:
module test (input wire enable,input wire data_in,output reg out
);always @(*) beginout = 1'b0; // 默认值if (enable) beginout = data_in;endend
endmodule

在这里插入图片描述

4. 多路选择器中未处理所有输入

生成Latch的代码:
module test (input wire [1:0] sel,input wire in1,input wire in2,input wire in3,output reg out
);always @(*) begincase (sel)2'b00: out = in1;2'b01: out = in2;2'b10: out = in3;// 没有处理 2'b11endcaseend
endmodule

在这里插入图片描述

修复后的代码:
module test (input wire [1:0] sel,input wire in1,input wire in2,input wire in3,output reg out
);always @(*) begincase (sel)2'b00: out = in1;2'b01: out = in2;2'b10: out = in3;default: out = 1'b0; // 添加默认值endcaseend
endmodule

在这里插入图片描述

5. 部分条件未赋值

生成Latch的代码:
module test (input wire condition1,input wire condition2,input wire in1,input wire in2,output reg out
);always @(*) beginif (condition1) beginout = in1;end else if (condition2) beginout = in2;end// 没有 else 分支end
endmodule

在这里插入图片描述

修复后的代码:
module test (input wire condition1,input wire condition2,input wire in1,input wire in2,output reg out
);always @(*) beginout = 1'b0; // 默认值if (condition1) beginout = in1;end else if (condition2) beginout = in2;endend
endmodule

在这里插入图片描述

6. 反馈路径中的Latch

生成Latch的代码:
module test (input wire reset,input wire enable,input wire d,output reg q
);always @(*) beginif (reset) beginq = 1'b0;end else if (enable) beginq = d;end// 没有 else 分支end
endmodule

在这里插入图片描述

修复后的代码:
module test (input wire reset,input wire enable,input wire d,output reg q
);always @(*) beginq = 1'b0; // 默认值if (reset) beginq = 1'b0;end else if (enable) beginq = d;endend
endmodule

在这里插入图片描述

CG

  • 1.5.3.1. 避免意外锁存生成
http://www.xdnf.cn/news/1784.html

相关文章:

  • 优选算法第十讲:字符串
  • Typecho 访客统计插件最新版-前后台统计图均可显示
  • 220V降24V500mA非隔离恒压芯片WT5110
  • m365是什么,和o365的区别
  • word删除空白页的方面
  • Java技术体系的主要产品线详解
  • YOLOv5改进(十)-- 轻量化模型MobileNetv4
  • 基于javaweb的SpringBoot校园服务平台系统设计与实现(源码+文档+部署讲解)
  • JWT(JSON Web Token)用户认证
  • DeepSeek 部署中的常见问题及解决方案全解析
  • 工业自动化中的高效桥梁:EtherCAT转Profinet网关在封装环节的应用
  • 二叉树的最大深度
  • Godot开发2D冒险游戏——第一节:主角登场!
  • NEWport太阳光模拟器与AVANTES光谱仪与太阳能模拟器光谱匹配度检测应用
  • Python实现异步编程的重要方式【协程(Coroutine)函数】(内含详细案例)
  • 计算机组成与体系结构:直接内存映射(Direct Memory Mapping)
  • [flutter]切换国内源(window)
  • 作用域插槽 父子组件插槽传值
  • 区分指向常量的指针、常指针和指向常量的常指针
  • OCP考试需要注意什么?
  • Python判断文本是不是注释方法
  • SAM12
  • 虚拟机系统介绍
  • 机器学习项目管理:团队协作与版本控制
  • Concepts (C++20)
  • 【Linux】网络基础和socket(4)
  • 访问者模式
  • HOJ.单词统计
  • 系统架构师2025年论文《系统架构风格2》
  • 生成运算树