verilog计数器中增减操作之资源占用

安路Anlogic TD Release_2026.1_SP2,器件选择:EG4A20BG256

方法1)case语句

复制代码
module test( input clk,input rst_n,input sw,output reg [3:0] led );

always @(posedge clk or negedge rst_n)
   if (!rst_n) 
      led<= 0; 
   else begin   
    case (sw)
      1: led<=led+1;
      default: led<=led-1;
    endcase 
   end
endmodule

方法2)if语句

复制代码
module test( input clk,input rst_n,input sw,output reg [3:0] led  );

always @(posedge clk or negedge rst_n)
   if (!rst_n) 
      led<= 0; 
   else begin   
    if (sw)led<=led+1;
    else led<=led-1;
    end
endmodule

方法3)条件运算符? :

复制代码
module test( input clk,input rst_n,input sw,output reg [3:0] led  );

always @(posedge clk or negedge rst_n)
   if (!rst_n) 
      led<= 0; 
   else begin   
    led<=sw?(led+1):(led-1);
    end
endmodule

方法4)2*sw-1

复制代码
module test( input clk,input rst_n,input sw,output reg [3:0] led  );

always @(posedge clk or negedge rst_n)
   if (!rst_n) 
      led<= 0; 
   else begin   
    led<=led+2*sw-1;
    end
endmodule

资源占用比较:方法4占用资源最少!

|---------|-----|-----|-----------|-----------|------|------|
| 方法 | not | DFF | MACRO_ADD | MACRO_MUX | lut4 | lut5 |
| case | 2 | 4 | 2 | 3 | 3 | 1 |
| if | 2 | 4 | 2 | 3 | 3 | 1 |
| ? : | 2 | 4 | 2 | 3 | 3 | 1 |
| 2*sw-1 | 1 | 4 | 2 | 0 | 3 | 1 |

相关推荐
t_niu_niu3 天前
bilateral_filter_paper
图像处理·verilog·双边滤波器
FPGA小迷弟5 天前
FPGA开发Vivado Block Design/Vitis/SDK/xlinx/开发教程,保姆级别,一看就会!
网络协议·tcp/ip·fpga开发·verilog·fpga
FPGA小迷弟5 天前
基于FPGA实现智能网卡的概念,什么是智能网卡
网络协议·tcp/ip·fpga开发·verilog·fpga
泡泡糖的中文规格书11 天前
HDLbits 1.2 学习笔记
fpga开发·学习笔记·verilog
星夜离尘5617 天前
FPGA实现CRC-8/SMBUS:从参数和位序核对到1041次自检
fpga开发·verilog·fpga·crc
星夜离尘5617 天前
FPGA正交编码器非法跳变检测:把16种状态转移和采样边界测清楚
verilog·fpga·编码器
星夜离尘5618 天前
I²C总线卡死:先分清SCL拉低、SDA拉低,再谈9个时钟恢复
fpga开发·verilog·i2c
星夜离尘5619 天前
Testbench不只看波形:用scoreboard抓住丢包、重复和乱序
fpga开发·verilog·testbench·scoreboard
星夜离尘5619 天前
FIFO深度怎么定?用突发流量和背压算清缓存预算
缓存·fpga开发·verilog·fifo·背压
ChipCamp20 天前
零门槛上手的RISC-V项目:picorv32
ubuntu·verilog·verilator·iverilog·gtkwave·riscv64-unknown