Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to count number of '1's in std_logic_vector? (VHDL)

Status
Not open for further replies.

uoficowboy

Full Member level 3
Joined
Apr 4, 2009
Messages
169
Helped
6
Reputation
12
Reaction score
5
Trophy points
1,298
Location
Seattle, Wa, USA
Activity points
2,964
Hi - in VHDL, if I have a std_logic_vector of an unknown size (set via a generic) - is there a way to count how many of the elements are equal to '1'?

Thanks!

-Michael
 

You can always use a variable and a loop. (or a function).

in hardware, it should be similar to N 1-bit additions. the complexity should scale with around log2(N) levels of logic. I suspect an actual implementation on an FPGA would scale around log4(N) for most cases.
 

    uoficowboy

    Points: 2
    Helpful Answer Positive Rating
permute said:
You can always use a variable and a loop. (or a function).

in hardware, it should be similar to N 1-bit additions. the complexity should scale with around log2(N) levels of logic. I suspect an actual implementation on an FPGA would scale around log4(N) for most cases.
That would take at least N clock cycles to compute, right? Is there any way to do it instantly?
 

@uoficowboy : No, it wont take N clock cycles. What permute wrote is a correct method . This method will take N clock cycles only when the counting is done inside a process at a clock edge.
But if you use a function or a loop with a variable(note that you have to use variable here and not signal), then it will take place within one clock cycle.
But the number of resources used here will be more.And also the maximum operating freq may decrease depending on the size of the std_logic_vector.

See this link for logic resource usage for combinational loops:
https://vhdlguru.blogspot.com/2010/04/recursive-functions-in-vhdl.html


--vipin
https://vhdlguru.blogspot.com/
 
Not sure the link is really the best example. I find far too many articles on that site to be incorrect, misleading, confusing, or dangerously misleading for the FPGA case.

in this case, the author compares the complexity of using a recursive function to something (it didn't seem to be specified). The logic generated seemed ideal for a v5/v6, but was deemed to use "more resources".
 

Just write VHDL to do what you want - let the synthesisor do all the work. Here is the function you want:

Code:
function count_ones(s : std_logic_vector) return integer is
  variable temp : natural := 0;
begin
  for i in s'range loop
    if s(i) = '1' then temp := temp + 1; 
    end if;
  end loop;
  
  return temp;
end function count_ones;

now you just need to call this function in a clocked process:

Code:
process(clk)
begin
  if rising_edge(clk) then
    n_bits <= count_ones(s);
  end if;
end process;
 

    uoficowboy

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top