Go Back   Velocity Reviews > Newsgroups > VHDL
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

VHDL - simple and annoying

 
Thread Tools Search this Thread
Old 09-02-2007, 02:36 PM   #1
Default simple and annoying


I have no idea why this doesn't work,
The output counts up regardless of the value of datain(i),
any help out there ?

architecture algorithm of ones_counter is
signal temp: std_logic_vector (0 to 3);
begin

process(datain)

begin
temp <= "0000";
for i in 0 to (width-1) loop
if datain(i) = '1' then
temp <= temp + '1';
else null;
end if;
end loop;
end process;

count <= temp;

end algorithm;




David Binnie
  Reply With Quote
Old 09-02-2007, 05:06 PM   #2
Shannon
 
Posts: n/a
Default Re: simple and annoying
On Sep 2, 6:36 am, "David Binnie" <td.bin...@blueyonder.co.uk> wrote:
> I have no idea why this doesn't work,
> The output counts up regardless of the value of datain(i),
> any help out there ?
>
> architecture algorithm of ones_counter is
> signal temp: std_logic_vector (0 to 3);
> begin
>
> process(datain)
>
> begin
> temp <= "0000";
> for i in 0 to (width-1) loop
> if datain(i) = '1' then
> temp <= temp + '1';
> else null;
> end if;
> end loop;
> end process;
>
> count <= temp;
>
> end algorithm;


You didn't say much about what datain is or what width is but from the
looks of it, this will count up if datain is any non-zero number.

Shannon




Shannon
  Reply With Quote
Old 09-02-2007, 05:26 PM   #3
David Binnie
 
Posts: n/a
Default Re: simple and annoying
Thanks,

datain is an slv and width is just to make the slv generic.

It is meant to count the number of 1s in the slv. i.e. 0111 would return 3

But it just counts up regardless of the value of datain.

I think there is something I don't understand in the loop statement.


David

"Shannon" <> wrote in message
news: ups.com...
> On Sep 2, 6:36 am, "David Binnie" <td.bin...@blueyonder.co.uk> wrote:
>> I have no idea why this doesn't work,
>> The output counts up regardless of the value of datain(i),
>> any help out there ?
>>
>> architecture algorithm of ones_counter is
>> signal temp: std_logic_vector (0 to 3);
>> begin
>>
>> process(datain)
>>
>> begin
>> temp <= "0000";
>> for i in 0 to (width-1) loop
>> if datain(i) = '1' then
>> temp <= temp + '1';
>> else null;
>> end if;
>> end loop;
>> end process;
>>
>> count <= temp;
>>
>> end algorithm;

>
> You didn't say much about what datain is or what width is but from the
> looks of it, this will count up if datain is any non-zero number.
>
> Shannon
>
>





David Binnie
  Reply With Quote
Old 09-02-2007, 05:34 PM   #4
David Binnie
 
Posts: n/a
Default More simple and annoying
The RTL schematic shows that each bit is of datain is compared with '1'
i.e. the implemetation is that if any bit of datain is '1' then temp will
count.

So it's a zero number detector.

I don't think the logic implies this !

David




David Binnie
  Reply With Quote
Old 09-02-2007, 07:20 PM   #5
Brad Smallridge
 
Posts: n/a
Default Re: simple and annoying
> The output counts up regardless of the value of datain(i),
> any help out there ?
>
> architecture algorithm of ones_counter is
> signal temp: std_logic_vector (0 to 3);
> begin
>
> process(datain)
>
> begin
> temp <= "0000";
> for i in 0 to (width-1) loop
> if datain(i) = '1' then
> temp <= temp + '1';
> else null;
> end if;
> end loop;
> end process;
>
> count <= temp;
>
> end algorithm;


I think the problem has to do with the way VHDL schedules
signals rather than updates them like variables in program
code. I got it to work by using variables instead of
std_logic_vector inside the loop. There are probably better
ways.

Brad Smallridge
AiVision

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

entity top is
Port (
datain : in std_logic_vector(7 downto 0));
end top;

architecture algorithm of top is

signal count : std_logic_vector(3 downto 0);
constant width : positive := 8;

begin

process(datain)
variable temp : std_logic_vector(3 downto 0);
begin
temp := "0000";
for i in 0 to (width-1) loop
if datain(i) = '1' then
temp := temp + '1';
-- else null;
end if;
end loop;
count <= std_logic_vector(unsigned(temp));
end process;


end algorithm;





Brad Smallridge
  Reply With Quote
Old 09-02-2007, 09:35 PM   #6
KJ
 
Posts: n/a
Default Re: simple and annoying

"David Binnie" <> wrote in message
news:N%yCi.115117$. uk...
>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




KJ
  Reply With Quote
Old 09-04-2007, 03:32 PM   #7
ast
 
Posts: n/a
Default Re: simple and annoying

"David Binnie" <> a écrit dans le message de news: N%yCi.115117$...
>I have no idea why this doesn't work,
> The output counts up regardless of the value of datain(i),
> any help out there ?
>
> architecture algorithm of ones_counter is
> signal temp: std_logic_vector (0 to 3);
> begin
>
> process(datain)
>
> begin
> temp <= "0000";
> for i in 0 to (width-1) loop
> if datain(i) = '1' then
> temp <= temp + '1';
> else null;
> end if;
> end loop;
> end process;
>
> count <= temp;
>
> end algorithm;


You simply dont know how signals are handled in VHDL.

If a signal is set many times in a process, only the last setting is done.

ie suppose signal S (type integer) is 0 and then enter the following process

process
begin
for i in 0 to 5 loop
S <= S+1;
end loop;
end;

At the end of the process, S is 1 and not 6 as expected

You need to use a variable (instead of a signal) if you want to work in a process

try this code:

architecture algorithm of ones_counter is
variable temp: std_logic_vector (0 to 3);
begin

process(datain)

begin
temp := "0000";
for i in 0 to (width-1) loop
if datain(i) = '1' then
temp := temp + '1';
end if;
end loop;
end process;

count <= temp;




ast
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
SAY NO TO BLURAY wandrinandz DVD Video 386 10-18-2008 07:04 PM
Discontinued! @#$%^&*!! NYC XYZ DVD Video 202 01-15-2006 08:04 PM
I am disappointed in DVD Walter R. DVD Video 181 01-14-2006 01:02 AM
CYE: Most Annoying Packaging EVER... Jordan Lund DVD Video 12 01-30-2004 02:42 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46