﻿<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="thpdoc.xsl"?>
<page id="const" suffix=" Keyword">
  <subsection>Declares a constant.<code>const Type ConstName = Value;</code></subsection>
  <section name="Remarks">
	<p>Note that you cannot declare global constants. Every constant should be declared inside an entity, class, namespace, process or function.</p>
  </section>
  <examples>
	<example><code>entity Adder
{
	const int DataWidth = 32;
	
	port
	{
		in logic[DataWidth] A, B;
		out logic[DataWidth] Sum;
	}
}
</code></example>
	<example name="Entity as a constant">
	<code>entity MyEntity
{
	const int DataWidth = 32;
	
	port in logic[DataWidth] X;
}

entity MyTestbench
{
	const any TheEntity = MyEntity;

	signal logic[TheEntity.DataWidth] Sig = 0;
	
	TheEntity uut(
		X = Sig
	);
}</code>
	</example>
	<example name="A policy class"><code>class DefaultPolicy
{
	const int WordSize = 32;
	const int MemorySize = 512;
}

class CompactModePolicy
{
	const int WordSize = 16;
	const int MemorySize = 128;
}

entity SomeEntity
{
	const any Policy = DefaultPolicy;

	Core.RAMs.Synchronous.SimpleRAM&lt;Policy.MemorySize, Policy.WordSize&gt; RAM(
		...
	);
}</code>
	</example>
	<example name="Non-trivial initializer">
	Constants can be initialized with arbitrary expressions that can be computed during compilation (e.g. function calls).
	<code>int square(int a)
{
	return a * a;
}

entity SomeEntity
{
	const int SquaredSize = square(Size);
	const int Size = 4;
	
	signal logic[SquaredSize] MySig;
}</code>
	Note that the order of the declaration is not important. When THDL++ compiler starts synthesizing MySig signal it will first find the SquaredSize constant, then find the square() function and Size constant and will finally execute square(4) to get the value.
	</example>
  </examples>
  <seealso id="typedef"/>
</page>