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

Reply

VHDL - Adding elements of an array

 
Thread Tools Search this Thread
Old 05-29-2004, 10:26 PM   #1
Default Adding elements of an array


Hi:

I have a 8x8 matrix and I want to add all the 64 elements.I tried
doing this using two FOR loops,one for row and one for column.It
didn't work.
Can anyone suggest a method to do this?

Thanks,
Modukuri


Modukuri
  Reply With Quote
Old 05-30-2004, 12:19 AM   #2
Jim Lewis
 
Posts: n/a
Default Re: Adding elements of an array
Create a two dimensional array of type unsigned or
signed as is appropriate for your problem. Include the
package:
use ieee.numeric_std.all ;

If you still have problems, provide your code and
so people can point you in the right direction.

Cheers,
Jim

> Hi:
>
> I have a 8x8 matrix and I want to add all the 64 elements.I tried
> doing this using two FOR loops,one for row and one for column.It
> didn't work.
> Can anyone suggest a method to do this?
>
> Thanks,
> Modukuri



--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~
Jim Lewis
Director of Training private.php?do=newpm&u=
SynthWorks Design Inc. http://www.SynthWorks.com
1-503-590-4787

Expert VHDL Training for Hardware Design and Verification
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~


Jim Lewis
  Reply With Quote
Old 05-30-2004, 04:18 PM   #3
Ralf Hildebrandt
 
Posts: n/a
Default Re: Adding elements of an array
Modukuri wrote:

> I have a 8x8 matrix and I want to add all the 64 elements.


=> 63 additions (quite a lot...)

> I tried
> doing this using two FOR loops,one for row and one for column.It
> didn't work.
> Can anyone suggest a method to do this?


First decide, if it is really nessecary to add them in parallel. (->
Serialized addition; Pipelining?)

Next, you should think about a tree-based or an array-based adder. The
simplest form can be described this way:

sum1<=((a+b) + (c+d)) + ((e+f) + (g+h)); -- tree
sum2<=(((((((a+b)+c)+d)+e)+f)+g)+h); -- array

(I did not care for the carry-out (-> overflow).)



Ralf



Ralf Hildebrandt
  Reply With Quote
Old 05-30-2004, 10:17 PM   #4
Modukuri
 
Posts: n/a
Default Re: Adding elements of an array
Hi:

This is the code,I'm using to sum all the elements of an array.But,at
the end of simulation,signal "sum" has only the last element of the
matrix,not the sum of all the 64 elements.I would really appreciate
any corrections/suggesstions to the code.

type diff_matrix is array (0 to 7,0 to 7) of integer;
signal diff : diff_matrix;
signal sum : integer;

process(clk,reset)
begin
if reset = '1 then
sum <= 0;
elsif (clk'event and clk = '1') then
for i in 0 to 7 loop
for j in 0 to 7 loop
sum <= sum_ini + diff(i,j);
end loop;
end loop;
end process;


Thanks,
Modukuri



Jim Lewis <> wrote in message news:<>...
> Create a two dimensional array of type unsigned or
> signed as is appropriate for your problem. Include the
> package:
> use ieee.numeric_std.all ;
>
> If you still have problems, provide your code and
> so people can point you in the right direction.
>
> Cheers,
> Jim
>
> > Hi:
> >
> > I have a 8x8 matrix and I want to add all the 64 elements.I tried
> > doing this using two FOR loops,one for row and one for column.It
> > didn't work.
> > Can anyone suggest a method to do this?
> >
> > Thanks,
> > Modukuri

>
>
> --
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~
> Jim Lewis
> Director of Training private.php?do=newpm&u=
> SynthWorks Design Inc. http://www.SynthWorks.com
> 1-503-590-4787
>
> Expert VHDL Training for Hardware Design and Verification
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~



Modukuri
  Reply With Quote
Old 05-30-2004, 10:33 PM   #5
Christoph M. Wintersteiger
 
Posts: n/a
Default Re: Adding elements of an array

The problem is, that the two loops are actually run through in the
same simulation cycle, you could think of them being concurrent. So
every time you assign something to sum, the driver of sum is updated,
not the signal itself tho. Naturally the last assignment is that of
the last element of the matrix, which then stays in the driver and is
assigned to the signal in the next simulation cycle.
However, the code would work if you used variables instead of signals.
Something like this maybe:

type diff_matrix is array (0 to 7,0 to 7) of integer;
signal diff : diff_matrix;
signal sum : integer;
variable temp : integer;

process(clk,reset)
begin
if reset = '1 then
sum <= 0;
elsif (clk'event and clk = '1') then
for i in 0 to 7 loop
for j in 0 to 7 loop
temp := temp + diff(i,j);
end loop;
end loop;

sum <= temp
end if;
end process;

On 30 May 2004 14:17:11 -0700, (Modukuri) wrote:

>Hi:
>
>This is the code,I'm using to sum all the elements of an array.But,at
>the end of simulation,signal "sum" has only the last element of the
>matrix,not the sum of all the 64 elements.I would really appreciate
>any corrections/suggesstions to the code.
>
>type diff_matrix is array (0 to 7,0 to 7) of integer;
>signal diff : diff_matrix;
>signal sum : integer;
>
>process(clk,reset)
>begin
> if reset = '1 then
> sum <= 0;
> elsif (clk'event and clk = '1') then
> for i in 0 to 7 loop
> for j in 0 to 7 loop
> sum <= sum_ini + diff(i,j);
> end loop;
> end loop;
>end process;
>
>
>Thanks,
>Modukuri
>
>
>
>Jim Lewis <> wrote in message news:<>...
>> Create a two dimensional array of type unsigned or
>> signed as is appropriate for your problem. Include the
>> package:
>> use ieee.numeric_std.all ;
>>
>> If you still have problems, provide your code and
>> so people can point you in the right direction.
>>
>> Cheers,
>> Jim
>>
>> > Hi:
>> >
>> > I have a 8x8 matrix and I want to add all the 64 elements.I tried
>> > doing this using two FOR loops,one for row and one for column.It
>> > didn't work.
>> > Can anyone suggest a method to do this?
>> >
>> > Thanks,
>> > Modukuri

>>
>>
>> --
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~
>> Jim Lewis
>> Director of Training private.php?do=newpm&u=
>> SynthWorks Design Inc. http://www.SynthWorks.com
>> 1-503-590-4787
>>
>> Expert VHDL Training for Hardware Design and Verification
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~




Christoph M. Wintersteiger
  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
constants as of array of integers, for loops octavsly Hardware 0 04-25-2009 11:53 AM
How to retrieve array parameter ( JAVA ) naruponk Software 1 04-16-2009 10:20 AM
Array Programme rits Software 2 03-04-2009 05:18 PM
How To Access HTML elements in code behind??? nedums_b Software 1 02-07-2008 07:15 PM
Paramount postpones Tuesday release of Star Trek DVD, elements missing. Allan DVD Video 2 09-09-2004 03:50 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