Search code examples
vhdlghdl

use package in ghdl


I'm trying to use my own package in ghdl. Can someone help me with the structure and the compilation. At the moment my code looks like this:

in ./my_package/my_package.vhd

library IEEE;
use IEEE.std_logic_1164.all;

package my_package is
  constant my_constant : std_logic_vector(3 downto 0) := "1111";
end my_package;

I'm compiling this in my_package as follows:

myname@myrechner my_package$ ghdl -a --work=my_package my_package.vhd

then in ./uses_my_package/uses_my_package.vhd

library IEEE;
use IEEE.std_logic_1164.all;

entity uses_my_package is
  port(
    vector_in : in std_logic_vector(3 downto 0);
    vector_out : out std_logic_vector(3 downto 0));
end uses_my_package;

architecture impl of uses_my_package is
begin
  vector_out <= vector_in;
end impl;

and in ./uses_my_package/testbench.vhd:

library IEEE;
use IEEE.std_logic_1164.all;
library work;
use work.my_package.all;

entity testbench is
end testbench;

architecture tb of testbench is
  component uses_my_package is
    port (
      vector_in : in std_logic_vector(3 downto 0);
      vector_out : out std_logic_vector(3 downto 0));
  end component;

  signal vector_in_signal : std_logic_vector(3 downto 0);
  signal vector_out_signal : std_logic_vector(3 downto 0);
  constant clk_period : time := 1 ms;

begin
  dut : uses_my_package
    port map (vector_in_signal, vector_out_signal);

  process
  begin
    vector_in_signal <= "0000";
    wait for clk_period;
    assert vector_out_signal = my_constant report "fail 0000" severity error;
    vector_in_signal <= "1111";
    wait for clk_period;
    assert vector_out_signal = my_constant report "fail 1111" severity error;
    wait;
  end process;

end tb;

Can anybody tell me how I should compile this program or point me to a tutorial about packages and libraries in ghdl? My google-foo appears to be quiet weak at the moment. Thank-you!


Solution

  • As far as I can see your code is valid (except that uses_my_package.vhd actually doesn't!) so your question is about the compile command lines?

    The little speedbump comes about because you've put the source files in different directories, to keep packages and their clients separate...

    OK, this is good practice, you just have to include the paths to those directories in the command line.

    And in the spirit of keeping the structure tidy, let's not just pile the build objects in the root directory either, but create a folder for them too. So here's one approach...

    mkdir build
    cd build
    ghdl -a ../my_package/my_package.vhd
    ghdl -a ../uses_my_package/uses_my_package.vhd
    ghdl -a ../uses_my_package/testbench.vhd
    ghdl -e testbench
    ghdl -r testbench
    

    This still uses the default library "work" for everything. You don't need a library work; declaration in your testbench to do that, it's implicitly visible. You just need the use clause as you have it.

    If you want to move your package into a different library, mylib, just specify that on the command line when you compile it...

    ghdl -a --work=mylib ../my_package/my_package.vhd
    

    (NOTE: One subtlety. Any references to work inside my_package.vhd are now references to mylib so if my_package.vhd contains the line use work.my_secret_package.all; you also have to compile my_secret_package.vhd into mylib. This keeps the library abstraction clean, everything inside mylib only refers to mylib unless explicitly specified otherwise)

    Using it from that library in the testbench is pretty obvious : replace the current library/use clause with

    library mylib;
    use mylib.my_package.all;
    

    And that's it...