gpt4 book ai didi

system-verilog - 数系统verilog

转载 作者:行者123 更新时间:2023-12-04 22:12:01 28 4
gpt4 key购买 nike

我有一个 64 位的线向量;

wire [63:0] sout;

我想计算这些位的总和,或者等效地计算 1 的数量。

做这个的最好方式是什么? (它应该是可合成的)

最佳答案

我更喜欢使用 for 循环,因为它们更容易扩展并且需要更少的输入(因此更不容易出现拼写错误)。

SystemVerilog(IEEE 标准 1800):

logic [$clog2($bits(sout)+1)-1:0] count_ones;

always_comb begin
count_ones = '0;
foreach(sout[idx]) begin
count_ones += sout[idx];
end
end

Verilog(IEEE 标准 1364-2005):
parameter WIDTH = 64;
// NOTE: $clog2 was added in 1364-2005, not supported in 1364-1995 or 1364-2001
reg [$clog2(WIDTH+1)-1:0] count_ones;
integer idx;

always @* begin
count_ones = {WIDTH{1'b0}};
for( idx = 0; idx<WIDTH; idx = idx + 1) begin
count_ones = count_ones + sout[idx];
end
end

关于system-verilog - 数系统verilog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27197177/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com