verilog - 检查所有设置/未设置的位

标签 verilog system-verilog

我是 Verilog 新手,正在迈出 FPGA“编程”的第一步。 我有一个类似于以下的参数化模块定义:

module foobar #(
    parameter BITS = 4
) (...);

   reg[BITS - 1:0] counter = 0;
   ...
   if (counter == |) begin
      ...
   end
   ...
endmodule

并想在|处测试counter的所有位是否已设置(或未设置)。如何实现这一目标?如果没有参数化,通过指定会很容易,例如4'b1111(或4'b0000)。

最佳答案

使用复制串联运算符。检查是否设置了所有位:

   if (counter == {BITS{1'b1}}) begin

检查是否所有位均已清除:

   if (counter == {BITS{1'b0}}) begin

或者,启用 SystemVerilog 功能:

   if (counter == '1) begin // All set
   if (counter == '0) begin // All cleared

关于verilog - 检查所有设置/未设置的位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77511343/

相关文章:

verilog - 具有独特参数的 SystemVerilog 接口(interface)数组

tcl - Modelsim - 为没有 GUI 的文件设置 "compile to library"

verilog - 为什么我们在 FPGA/VHDL/VIVADO 中使用 REG?

Verilog 中的字符串操作

for-loop - 在 systemverilog 中实现 for 循环

c - 硬件建模的正确语言

verilog - Verilog 中的 $fopen 函数支持用户参数吗?

system-verilog - Always block 中的 continue 有何作用?

verilog - 在 assign 语句中使用长嵌套的 if-else 是一种不好的做法吗?

c++ - 如何在 C++ 中进行 SystemVerilog 风格的位 vector 切片赋值?