David Binnie said:
I have no idea why this doesn't work,
The output counts up regardless of the value of datain(i),
any help out there ?
Processes only update signals when they exit or when they encounter a wait
statement. Your process gets trigerred when datain changes and then it
enters the loop. Since 'temp' is a signal it won't get updated until the
process completes (i.e. after going through the entire 'width' loops). So
what happens is that if any of the bits in datain is '1', then temp will
increment by '1'. Variables however get updated immediately so if temp were
a variable it would increment once for each bit in datain that is a '1'.
Since variables are only visible from within the process that declares them,
the assignment to count must be put inside the process. Modified code with
my annotations below .
As an aside, since you're trying to add '1' to a std_logic_vector I'm
assuming that you're using std_logic_arith which is a poor practice, use
numeric_std instead.
use ieee.numeric_std.all; -- KJ added, remove std_logic_arith
architecture algorithm of ones_counter is
-- signal temp: std_logic_vector (0 to 3); KJ commented out
begin
process(datain)
variable temp: unsigned(0 to 3); KJ added, use unsigned instead of slv
begin
temp := "0000"; -- KJ change to variable assignment
for i in 0 to (width-1) loop
if datain(i) = '1' then
temp := temp + 1; -- KJ change to variable assign and add 1
else null; -- KJ 'else null' not really needed, doesn't hurt anything
though
end if;
end loop;
count <= std_logic_vector(temp); -- KJ moved inside process, add type
conversion
end process;
end algorithm;
KJ