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 Constant Declaration

Status
Not open for further replies.

hareeshP

Member level 3
Member level 3
Joined
Jul 19, 2017
Messages
59
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
491
Hi,
i want to declare a constant 5 bit value in vhdl and i want to access that constant value throughout the program.
 

constant x: std_logic_vector ( 4 downto 0 ) := "10011" ; -- declare this between "architecture" and "begin"
 
If you want it available in multiple places - declare it in a package.
 
Thanks for your reply,
Currently i have done this way..
 

thanks for your valuable comments,
but when i declare it as a package like,
/////////////////////////////////
package package_name is
constant name: type ;
end package_name;
////////////////////////////////
do i need anything else to declare for the package declaration?
 

Code:
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

package vhdl_pkg is
. 
constant x: std_logic_vector ( 4 downto 0 ) := "10011" ; 
.
end vhdl_pkg;

-- If package body is required
package body vhdl_pkg is
.
.
.
end package body;

In places where you need to use the package put this before the entity declaration: use work.vhdl_pkg.all;
 

Follow-up question,

Is it possible to declare 2 (or more) constants of the same name in different packages ?
And be able to access both (somewhat like structures in C) ?

if package_1.x = some_value ...
if package_2.x = some_value ...
 

You can do this with libraries if you are willing to deal with the "work" library weirdness. Not sure about packages as I've never had a need for this.
 
Follow-up question,

Is it possible to declare 2 (or more) constants of the same name in different packages ?
And be able to access both (somewhat like structures in C) ?

if package_1.x = some_value ...
if package_2.x = some_value ...

Yes. In any name conflict situation, you must use the full path to use then item, otherwise neither are visible. The common situation is when people include both std_logic_arith and numeric_std in the same file. Apart from deleting std_logic_arith, they can work around it by using the full path:

Code:
signal a : ieee.numeric_sd.unsigned(7 downto 0);
signal b : ieee.std_logic_arith.unsigned(7 downto 0);

Similarly, the common quoted "you cant use std_logic_unsigned and std_logic_signed" in the same file is also not technically true. You can just provide the path to the "+" function you wanted:

Code:
my_output0 <= a ieee.std_logic_unsigned."+" b;
my_output2 <= a ieee.std_logic_signed."+" b;

- - - Updated - - -

You can do this with libraries if you are willing to deal with the "work" library weirdness. Not sure about packages as I've never had a need for this.

Its not really weird, work is just the default library, and acts like any other library. Though many people use it as a local library, its not true. What people often do is compile everything into work. The from another project, map "my_other_library" to the existing work library. Mapping is a tool defined construct, not a VHDL one.
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
when declare like this it is showing an error
'object "std_logic" is used but not declared '
 

you must have forgotten:

library ieee;
use ieee.std_logic_1164.all;
 

no...i have written the library.
but still it is showing the error.
 

no...i have written the library.
but still it is showing the error.
////////////////////////////
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.vhdl_pkg.all; // is this correct??
package vhdl_pkg is
constant CHIPID2: std_logic_vector(4 downto 0):= "00001";
end vhdl_pkg;
entity test is
port( clk: in std_logic
------------
//////////////
 

If you put the package and entity definition in one file, you still need separate library clauses for both design units. The library reference in your code works for the package only. Should look like below


Code VHDL - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
library ieee;
 use ieee.std_logic_1164.all;
 
 package vhdl_pkg is
 constant CHIPID2: std_logic_vector(4 downto 0):= "00001";
 end vhdl_pkg;
 
library ieee;
 use ieee.std_logic_1164.all;
 use ieee.numeric_std.all;
library work;
 use work.vhdl_pkg.all;
 
 entity test; -- .....

 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top