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

目录

Latch在FPGA中的作用

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的代码:
verilog 复制代码
module test (
    input wire sel,
    input wire in1,
    output reg out
);
    always @(*) begin
        if (sel == 1'b1) begin
            out = in1;
        end
        // 没有 else 分支
    end
endmodule


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

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

生成Latch的代码:
verilog 复制代码
module test (
    input wire [1:0] state,
    input wire in1,
    input wire in2,
    output reg out
);
    always @(*) begin
        case (state)
            2'b00: out = in1;
            2'b01: out = in2;
            // 没有 default 分支
        endcase
    end
endmodule
修复后的代码:
verilog 复制代码
module test (
    input wire [1:0] state,
    input wire in1,
    input wire in2,
    output reg out
);
    always @(*) begin
        case (state)
            2'b00: out = in1;
            2'b01: out = in2;
            default: out = 1'b0; // 添加默认值
        endcase
    end
endmodule

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

生成Latch的代码:
verilog 复制代码
module test (
    input wire enable,
    input wire data_in,
    output reg out
);
    always @(*) begin
        if (enable) begin
            out = data_in;
        end
        // 没有 else 分支
    end
endmodule
修复后的代码:
verilog 复制代码
module test (
    input wire enable,
    input wire data_in,
    output reg out
);
    always @(*) begin
        out = 1'b0; // 默认值
        if (enable) begin
            out = data_in;
        end
    end
endmodule

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

生成Latch的代码:
verilog 复制代码
module test (
    input wire [1:0] sel,
    input wire in1,
    input wire in2,
    input wire in3,
    output reg out
);
    always @(*) begin
        case (sel)
            2'b00: out = in1;
            2'b01: out = in2;
            2'b10: out = in3;
            // 没有处理 2'b11
        endcase
    end
endmodule
修复后的代码:
verilog 复制代码
module test (
    input wire [1:0] sel,
    input wire in1,
    input wire in2,
    input wire in3,
    output reg out
);
    always @(*) begin
        case (sel)
            2'b00: out = in1;
            2'b01: out = in2;
            2'b10: out = in3;
            default: out = 1'b0; // 添加默认值
        endcase
    end
endmodule

5. 部分条件未赋值

生成Latch的代码:
verilog 复制代码
module test (
    input wire condition1,
    input wire condition2,
    input wire in1,
    input wire in2,
    output reg out
);
    always @(*) begin
        if (condition1) begin
            out = in1;
        end else if (condition2) begin
            out = in2;
        end
        // 没有 else 分支
    end
endmodule
修复后的代码:
verilog 复制代码
module test (
    input wire condition1,
    input wire condition2,
    input wire in1,
    input wire in2,
    output reg out
);
    always @(*) begin
        out = 1'b0; // 默认值
        if (condition1) begin
            out = in1;
        end else if (condition2) begin
            out = in2;
        end
    end
endmodule

6. 反馈路径中的Latch

生成Latch的代码:
verilog 复制代码
module test (
    input wire reset,
    input wire enable,
    input wire d,
    output reg q
);
    always @(*) begin
        if (reset) begin
            q = 1'b0;
        end else if (enable) begin
            q = d;
        end
        // 没有 else 分支
    end
endmodule
修复后的代码:
verilog 复制代码
module test (
    input wire reset,
    input wire enable,
    input wire d,
    output reg q
);
    always @(*) begin
        q = 1'b0; // 默认值
        if (reset) begin
            q = 1'b0;
        end else if (enable) begin
            q = d;
        end
    end
endmodule

CG

相关推荐
FPGA之旅1 天前
FPGA从零到一实现FOC(一)之PWM模块设计
fpga开发·dubbo
XMAIPC_Robot1 天前
基于ARM+FPGA的光栅尺精密位移加速度测试解决方案
arm开发·人工智能·fpga开发·自动化·边缘计算
cycf1 天前
状态机的设计
fpga开发
szxinmai主板定制专家1 天前
【精密测量】基于ARM+FPGA的多路光栅信号采集方案
服务器·arm开发·人工智能·嵌入式硬件·fpga开发
千宇宙航1 天前
闲庭信步使用SV搭建图像测试平台:第三十二课——系列结篇语
fpga开发
千宇宙航2 天前
闲庭信步使用SV搭建图像测试平台:第三十一课——基于神经网络的手写数字识别
图像处理·人工智能·深度学习·神经网络·计算机视觉·fpga开发
小眼睛FPGA2 天前
【RK3568+PG2L50H开发板实验例程】FPGA部分/紫光同创 IP core 的使用及添加
科技·嵌入式硬件·ai·fpga开发·gpu算力
forgeda2 天前
如何将FPGA设计验证效率提升1000倍以上(2)
fpga开发·前沿技术·在线调试·硬件断点·时钟断点·事件断点
9527华安3 天前
FPGA实现40G网卡NIC,基于PCIE4C+40G/50G Ethernet subsystem架构,提供工程源码和技术支持
fpga开发·架构·网卡·ethernet·nic·40g·pcie4c
search73 天前
写Verilog 的环境:逻辑综合、逻辑仿真
fpga开发