Functions

Functions allow a set of calculations to be isolated and grouped under a user-defined name. This helps to create structured and better readable code. They can only be used to specify the combinational part of RTL descriptions.

Functions in Arx are still under development. More features are likely to be added in the future.

Function Definition

Functions are declared on their own, not inside a component. They should precede the code for the component in which they are called.

A function definition obeys the following syntax:

function <function name> : <return type>
*{ <input argument name> : <type> }*
   <variable declaration>
begin
  <statements>
  return <expression>
end

The <variable declaration> section of the code has the same syntax as variable declarations variable declarations in a component. Also <statements> follow the syntax of statements in a component. The last statement should be the return statement in which the intended return value of the function is given by <expression>. Note that user-defined types, generics, etc. are not (yet) supported in a function definition.

Below, an example of an Arx function is given:

function mylogic: bitvector(8)
  x: bitvector(8)
  y: bitvector(8)
variable
  res: bitvector(8)
begin
  res = (not x) xor y
  return res
end

Function Call

Calling a function is straightforward. It involves passing the function arguments in the order of declaration in the argument list after the function name. The line below shows how the example function mentioned above could be called:

  mem = mylogic(mem, data_in)

If a function does not have arguments, the function call consists of the function name and a pair of parentheses, as illustrated below:

  new_value = function_without_args()

External Function

External functions make it possible to link Arx descriptions to functions in any of the Arx target languages (C/C++ or VHDL) rather than in Arx itself. This is especially useful in early stages of developing algorithms when it is not yet necessary that the entire description is synthesizable. One can e.g. refer to the square-root function sqrt which is part of the C math library.

An Arx description becomes consistent by external-function declarations. It is up to the user to take care of the actual linking in the target language. An external function declaration consists of the first part of an ordinary function definition: the function name, return type and input-argument descriptions. The above-mentioned function sqrt would be declared as:

external function sqrt: real
  x: real

Known Issues

The current version of Arx does not yet generate VHDL code for functions. The workaround for this situation is to augment the VHDL code generated by Arx with hand-written VHDL for the functions.

 
arx/function.txt · Last modified: 2023/07/17 22:40 by 127.0.0.1