Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

VHDL package, for loop, constant array.

Status
Not open for further replies.

HDE

Newbie level 2
Joined
Nov 17, 2011
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,305
Hi everyone,
I have a question in regard to a vhdl package I have created. Hereafter, is the code that works fine:

--------------------------------------------------------------------------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.all;
USE ieee.math_real.log2;
USE ieee.math_real.ceil;

package PKG_globalConstants is

constant Width : integer := 7;
constant FaultyChannels : integer := 3;
constant clockPeriod : INTEGER := 20;
constant pwmPeriod : INTEGER := 320000;
constant dcIintegral : positive := 22;
constant cntWidth : INTEGER := INTEGER(CEIL(LOG2(REAL(pwmPeriod/clockPeriod))))
type pulseShift_array is array (0 to FaultyChannels) of positive;
constant pulseShift : pulseShift_array := ( (2**cntWidth)/(Width-3), (2**cntWidth)/(Width-2), (2**cntWidth)/(Width-1), (2**cntWidth)/(Width) ); ---(line that I need to replace)

end PKG_globalConstants;
--------------------------------------------------------------------------------------------------------------------------------------------------

My question is, is it possible to use the following part of code in order to make pulseShift constant array parametric?
I use Xilinx WEB ise 13.2 and I get a syntax error for the 'loop' example. If what I need to do is possible in VHDL, could you please give me the answer? Thanks in advance.

Hearafter is the code I would like to insert in my package;
----------------------------------------------------------------
constant pulseShift : pulseShift_array;
forLoop: for i in 0 to FaultyChannels generate
pulseShift(i) := (2**cntWidth)/(Width-(FaultyChannels-i));
end generate;
----------------------------------------------------------------
 

yes, you can use a for loop. But a for loop is a procedural bit of VHDL, so needs to be inside a process, function or procedure. (a generate loop must be inside an architecture, and cant be used for constants).

Luckily, you can call a function to initialise a constant:

Code:
function pulseShift_init return pulseShift_array is
  variable temp : pulseShift_array;
begin
  forLoop: for i in 0 to FaultyChannels loop
    temp(i) := (2**cntWidth)/(Width-(FaultyChannels-i));
  end loop;

  return temp;
end function pulseShift_init;

constant pulseShift : pulseShift_array := pulseShift_init;


---------- Post added at 18:47 ---------- Previous post was at 18:42 ----------

another quick thing I just remembered, you cannot put more than a function declaration inside a package, the body of the function has to be inside the package body.

ie:

Code:
package PKG_globalConstants is

.......

  function pulseShift_init return pulseShift_array;
  constant pulseShift : pulseShift_array := pulseShift_init;

end package PKG_globalConstants;


package body PKG_globalConstants is

  function pulseShift_init return pulseShift_array is
    variable temp : pulseShift_array;
  begin
    forLoop: for i in 0 to FaultyChannels loop
      temp(i) := (2**cntWidth)/(Width-(FaultyChannels-i));
    end loop;
  
    return temp;
  end function pulseShift_init;
  
end package body PKG_globalConstants;
 
  • Like
Reactions: HDE

    HDE

    Points: 2
    Helpful Answer Positive Rating
(a generate loop must be inside an architecture, and cant be used for constants).
Constants can be generated in a loop by using an optional "block_declarative_item" and a begin keyword. There are however easier ways to achieve the same, particularly initialization functions, as you explained.
 
  • Like
Reactions: HDE

    HDE

    Points: 2
    Helpful Answer Positive Rating
Thank you for all the valuable information!
TrickiDicky your example worked fine!
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top