数组中的个数

Oma*_*rif 3 verilog dataflow hdl

我试图在 Verilog 中计算 4 位二进制数中 1 的数量,但我的输出出乎意料。我尝试了几种方法;这是我认为应该工作的一个,但它没有。

module ones(one,in);
input [3:0]in;
output [1:0]one;

assign one = 2'b00; 
assign one = one+in[3]+in[2]+in[1]+in[0] ;

endmodule
Run Code Online (Sandbox Code Playgroud)

N8T*_*TRO 5

首先,您不能两次分配变量。

其次,您的范围是关闭的,2 位只能从 0 到 3。您需要一个 3 位输出才能数到 4。

这更像是您需要的:

module ones(
  output wire [2:0] one,
  input wire [3:0] in
);

assign one = in[3]+in[2]+in[1]+in[0] ;

endmodule
Run Code Online (Sandbox Code Playgroud)