Channel Directions in VHDL
When using signals as procedure parameters, you can specify if a signal is meant to only send or receive values. This specificity increases the type-safety of the program.
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_TEXTIO.ALL;
use STD.TEXTIO.ALL;
entity ChannelDirections is
end ChannelDirections;
architecture Behavioral of ChannelDirections is
signal pings : std_logic_vector(7 downto 0);
signal pongs : std_logic_vector(7 downto 0);
procedure ping (signal pings : out std_logic_vector(7 downto 0);
constant msg : in string) is
begin
for i in 1 to msg'length loop
pings <= std_logic_vector(to_unsigned(character'pos(msg(i)), 8));
wait for 10 ns;
end loop;
end procedure;
procedure pong (signal pings : in std_logic_vector(7 downto 0);
signal pongs : out std_logic_vector(7 downto 0)) is
begin
pongs <= pings;
wait for 10 ns;
end procedure;
begin
process
variable l : line;
begin
ping(pings, "passed message");
pong(pings, pongs);
write(l, character'val(to_integer(unsigned(pongs))));
writeline(output, l);
wait;
end process;
end Behavioral;
This ping
procedure only accepts a signal for sending values. It would be a compile-time error to try to read from this signal within the procedure.
The pong
procedure accepts one signal for receives (pings
) and a second for sends (pongs
).
In the main process, we create two signals pings
and pongs
. We then call the ping
procedure with the pings
signal and a message. After that, we call the pong
procedure with both signals. Finally, we print the received message.
To run this VHDL code, you would typically use a VHDL simulator such as ModelSim or GHDL. The exact commands may vary depending on your simulation environment, but it might look something like this:
$ ghdl -a channel_directions.vhd
$ ghdl -e channel_directions
$ ghdl -r channel_directions
passed message
This example demonstrates how VHDL can use signal directions to enforce type safety in signal communication between procedures, similar to channel directions in other languages.