VHDL Functions
VHDL Functions
FunctionsarepartofagroupofstructuresinVHDLcalledsubprograms.Functionsaresmallsectionsofcodethat
performanoperationthatisreusedthroughoutyourcode.
Functionsareusedifallofthefollowingconditionsaretrue:
•Therearenodelay,timing,oreventcontrolconstructsthatarepresent
•Itreturnsasinglevalue
•Thereisatleastoneinputargument
•Therearenooutputorinoutarguments
•Therearenonon-blockingassignments
Inshort,functionsmayimplementonlycombinatorialbehavior,i.e.theycomputeavalueonthebasisofthepresentvalue
oftheinputargumentsandreturnasinglevalue.Eachlineofcodeinthefunctionisexecutedsequentially(theyarenot
non-blocking).Theyareusedintherighthandsideofanassignmentstatement.Hereisanexampleofafunction
definitionandcall.
EXAMPLE:
functionidentifier[ inputportdeclarations]returntypeis
[variabledeclarations]
begin
functionstatements
endidentifier
Tocallafunction,oneneedstousethefunctionidentifier(withinput(s)defined)asanassignment
operandinaprocessblock:
Func_Out<=identifier(input);
Hereiscodesnippetshowinghowfunctionsareused:
functionHAS_FUNCTION(
DIN:STD_LOGIC_VECTOR(7downto0);)
returnSTD_LOGIC_VECTORis
variablek:integer:=0;
variablecount:integer:=0;
variablereverse_bits:STD_LOGIC_VECTOR(7downto0):=“00000000”;
begin
forkin0to7loop
reverse_bits(7–count):=DIN(count);
count:=count+1;
endloop;
returnreverse_bits;
endHAS_TASK;