Introduction
Packages help keep code neat and easy to use again. They group related parts so you don't repeat yourself.
Packages help keep code neat and easy to use again. They group related parts so you don't repeat yourself.
package package_name is -- declarations like types, constants, functions end package package_name; package body package_name is -- implementations of functions end package body package_name;
The package declares what is available to use.
The package body contains the actual code for functions.
add to add two numbers.package math_pkg is function add(a : integer; b : integer) return integer; end package math_pkg;
add function inside the package body.package body math_pkg is function add(a : integer; b : integer) return integer is begin return a + b; end function add; end package body math_pkg;
This program shows a package utils_pkg with a constant and a function. The test entity uses the package to double a number and print the result.
library ieee; use ieee.std_logic_1164.all; library std; use std.textio.all; package utils_pkg is constant MAX_VALUE : integer := 100; function double(x : integer) return integer; end package utils_pkg; package body utils_pkg is function double(x : integer) return integer is begin return x * 2; end function double; end package body utils_pkg; entity test_pkg is end entity test_pkg; architecture behavior of test_pkg is use work.utils_pkg.all; signal result : integer; begin process begin result <= double(10); wait for 1 ns; report "Double of 10 is " & integer'image(result); wait; end process; end architecture behavior;
Always put reusable code in packages to avoid duplication.
Use use work.package_name.all; to access package contents easily.
Packages group reusable code like functions and constants.
They keep your code clean and easy to maintain.
Use packages to share code across different parts of your design.
package math_pkg is
function add(a : integer; b : integer) return integer;
end package;
package body math_pkg is
function add(a : integer; b : integer) return integer is
begin
return a + b;
end function;
end package body;
architecture behavior of test is
use work.math_pkg.all;
signal result : integer := 0;
begin
process
begin
result <= add(3, 4);
wait;
end process;
end behavior;package constants_pkg is constant MAX_VAL integer := 255; end package constants_pkg;
Pass the test and this pattern moves unknown