I am trying to come up with a way to add individual bits of a register.
eg, if regA = 111000 then regB = 3
(Sum of bits of regA
).
1) Is there any synthesizable function/operator in Verilog or SystemVerilog which I can directly use to do this operation?
If not, then maybe the problem is a little interesting, especially because the operation has to be done in one clock cycle (pure combinational logic) and the register width is parameterizable.
2) In case there is no inbuilt Verilog or SystemVerilog operator then what can be done?
Thanks, Ujjwal
You can try something like this. I am not exactly sure what it will synthesize to but it should work.
Of course there are more complex ways that may have better performance, depending on on your tool flow and what is available, where you can create your own adders and cascade them in parallel reducing the size each step. Assuming the width was 32-bits, something like:
Verilog (IEEE Std 1364-2001 or newer):
SystemVerilog (IEEE Std 1800-2005 or newer):
Both will synthesize to combination logic. No latches or flops.
SystemVerilog does have
$countones()
, but I'm unsure if it is synthesizable. Ff it is then:always_comb B = $countones(A)