Results 1 to 6 of 6

Thread: Addition Sum of Array

  1. #1
    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    1

    Default Addition Sum of Array

    Hey @ all,

    i'm new here and i have some questions about the Sum of an array (VHDL), that contains a lot of 8 bit value's of std_logic_vector.

    For example i have declared the array as follow:

    HTML Code:
    type table is Array (natural range <>) of std_logic_vector (7 downto 0);
    constant vec_value: table := ("00000000","00000001","00000010","00000010","00000010","00000110","00010010");
    Now i woult like to calculate the Sum of all elements of the array. Is it possible to realize it with a for loop, because i need the index later to calculate some features, for example the maximum and minimum and it's position or something like this? And this code should be synthesizable.

    I hope somebody can help me. I am very grateful for this.

  2. #2
    Join Date
    Oct 2008
    Posts
    3,406
    Rep Power
    1

    Default Re: Addition Sum of Array

    No, you cannot sum std_logic_vectors with standard VHDL. You can only perform arithmatic on unsigned or signed types. Std_logic_vectors are just an array of bits, so you have no idea if they are signed or unsigned or just a random bus.

    So you need to convert them to signed or unsigned type first. You could easily write a function to sum the table. The question is, will vec_value always be a constant, or is it some kind of memory?

  3. #3
    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    1

    Default Re: Addition Sum of Array

    Thank's for the fast answer. My VHDL experience are not the best. This is a new section for me.

    Yes, that's right. I have to convert std_logic_vector in unsigned / signed. Can i use integer too?

    So, my idea for the implementation:
    Code:
    type table is Array (natural range <>) of std_logic_vector (7 downto 0);
    constant vec_value: table := ("00000000","00000001","00000010","00000010","00000010","00000110","00010010");
    
    begin
     signal tmp : unsigned := "00000000" ;
      
     process(CLK)
        begin
        if clk'event and clk = '1' then
           for i in 0 to 6 loop    
             tmp <= tmp + unsigned(vec_value(i));
            end loop;   
           SUM <= (tmp,8);
       end if;
    end process;
    Is it the right way? But do i need a clk?

    The question is, will vec_value always be a constant, or is it some kind of memory?
    For the first time to lean about arithmetic operations i use constant's but later it will be elements of memory which are frequently change





    Thank's a lot


    regards


    phot

  4. #4
    Join Date
    Aug 2005
    Location
    California
    Posts
    3,557
    Rep Power
    1

    Default Re: Addition Sum of Array

    Hi phot,

    When writing VHDL, you need to think about what logic will be synthesized, and what logic you're just using to make the code more readable.

    In some cases, it makes the code easier to read and maintain by using tables and constants containing integers or real values. This works well, if those tables and constants are only used to initialize synthesizeable logic, eg., to initialize std_logic_vector signals or variables.

    For example, look in the following example code for a linear feedback shift register. The code essentially uses the VHDL compiler to perform matrix operations, the results of which are then used to define hardware:

    http://www.ovro.caltech.edu/~dwh/correlator/pdf/lfsr_tutorial.pdf
    http://www.ovro.caltech.edu/~dwh/correlator/pdf/lfsr_tutorial_src.zip

    The adder_tree.vhd component in that source is what you would need to use if you were adding values together in a parallel operation.

    If you were adding values from RAM, then you would only need one adder, and a clock per RAM location to read the data and then sum it. Keep in mind that your sum will have more bits than the input, so you need to make your adder as wide as needed to store the largest sum, eg., the sum of 100 8-bit values is ceil(log2(100)) = 7-bits wider (eg., 100 x FFh = 639Ch).

    Cheers,
    Dave

  5. #5
    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    1

    Default Re: Addition Sum of Array

    Thank's a lot.

    My FPGA is a Spartan 3AN XC3S1400AN which includes a Block RAM. How can i use it? Do i have to create in the Xilinx Core Generator a Block Memory Generator? Or do i need only a Block RAM Controller, because the Block Ram already exists?

    best regards

    phot

  6. #6
    Join Date
    Aug 2005
    Location
    California
    Posts
    3,557
    Rep Power
    1

    Default Re: Addition Sum of Array

    My FPGA is a Spartan 3AN XC3S1400AN which includes a Block RAM. How can i use it? Do i have to create in the Xilinx Core Generator a Block Memory Generator? Or do i need only a Block RAM Controller, because the Block Ram already exists?
    This is an Altera Forum. You'll need to ask these sorts of questions on a Xilinx forum.

    Cheers,
    Dave

Similar Threads

  1. vhdl addition result size
    By dabg in forum General Altera Discussion
    Replies: 10
    Last Post: January 30th, 2014, 10:30 PM
  2. Addition fixed point
    By newmoon in forum General Altera Discussion
    Replies: 2
    Last Post: September 17th, 2011, 05:33 PM
  3. Custom Instruction addition
    By kalyansrinivas in forum General Discussion Forum
    Replies: 6
    Last Post: December 14th, 2009, 05:27 PM
  4. Addition of 2 numbers
    By xcite in forum Shared Material
    Replies: 1
    Last Post: May 24th, 2008, 11:49 AM
  5. interfacing nios with addition modules
    By Quan in forum General Discussion Forum
    Replies: 3
    Last Post: June 24th, 2004, 08:40 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •