Adding elements of an array

M

Modukuri

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
 
J

Jim Lewis

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 mailto:[email protected]
SynthWorks Design Inc. http://www.SynthWorks.com
1-503-590-4787

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

Ralf Hildebrandt

Modukuri said:
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
 
M

Modukuri

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
 
C

Christoph M. Wintersteiger

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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top