﻿<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="thpdoc.xsl"?>
<page id="port" suffix=" Keyword">
  <subsection>Declares a port.<code>port in/out/inout TypeName PortName;</code></subsection>
  The <kw>port</kw> keyword declares a port in an entity. Ports can be declared in arbitrary order and can be grouped to improve readability. Port semantics in THDL++ is equivalent to VHDL. See examples for more details.

  <section name="Remarks">
	<p>It is recommended to avoid hardcoding port types when you declare entities. Instead, use <kw>typedef</kw> statements or <kw>template</kw> arguments.</p>
	<p>Sometimes you can increase readability of your port declarations using compact entity declaration syntax. See the <kw>entity</kw> page for more details.</p>
	<p>THDL++ ports can have initial values. For input ports initial values will be used if the port is omited in the port map. For output ports the values will be assigned to the ports before the first assignment statement changes it. See examples for details.</p>
	<p>In THDL++ you can read the values of output ports! When generating VHDL, shadow signals will be created automatically.</p>
  </section>
  <examples>
	<example name="Initial port values">
<code>entity Adder
{
	port in logic[8] X = 0, Y = 0;
	port out logic[8] Result;
	
	Result = X + Y;
}</code>
	</example>
	<example name="Arbitrary order"> The following code is also valid, however, is much less readable:
<code>entity Adder
{
	port in logic[8] X, Y;
	
	Result = X + Y;

	port out logic[8] Result;
}</code>
	</example>
	<example name="Grouping ports">
<code>entity Adder
{
	port
	{
		in logic[8] X, Y;
		out logic[8] Result;
	}

	Result = X + Y;
}</code>
	</example>
	<example name="Avoiding type hardcoding"><code>entity Adder
{
	typedef logic[8] DataType;
	port
	{
		in DataType X, Y;
		out DataType Result;
	}

	Result = X + Y;
}</code></example>
	<example name="Shadow signals"><code>entity Counter
{
	port in logic clk, reset;
	port out logic[8] Value = 0;
	
	process sync (clk.rising) autoreset(reset)
	{
		Value++;
	}
}</code>
	The following VHDL code will be generated:
<code>entity Counter is
	Port (
		clk : in std_logic;
		reset : in std_logic;
		Value : out std_logic_vector(7 downto 0)
	);
	
end entity Counter;

architecture Behavioral of Counter is
	signal thp_shadow_Value : std_logic_vector(7 downto 0) := X"00";
	
	begin
		Value &lt;= thp_shadow_Value;
		
		sync : process (clk, reset) is
		begin
			if reset = '1' then
				Value &lt;= X"00";
			elsif rising_edge(clk) then
				thp_shadow_Value &lt;= (thp_shadow_Value + X"01");
			end if;
		end process sync;
		
end architecture Behavioral;</code>
</example>
  </examples>
  <seealso id="entity"/>
  <seealso id="signal"/>
  <seealso id="link"/>
</page>