Learning Bluespec

Functions

Overview

  • Functions are parameterized expressions.
  • Functions are a way to encapsulate a description.
  • Functions are first class objects and can be passed to other functions or modules.
  • Functions are defined once and can be instantiated many times.
  • Functions return an object.
  • Functions can be used for both static elaboration and to describe combinational circuits.
  • Functions can be an abstraction of a combinational expression where the arguments are the inputs to the circuit and the result is the output of the circuit.
  • When used for static elaboration function arguments and results can have any type, not just restricted types as in Verilog or SystemVerilog.  Types can include rules, interfaces, modules and functions.

Syntax


function type function_name (arguments) provisos;
                  function body statements;
                  return statement;
endfunction: function_name

Examples

The boolean negation function:

 function Bool notFn (Bool x);
     if (x) notFn = False;
     else   notFn = True;
 endfunction: notFn

The boolean negation function, but using return instead:

function Bool notFn (Bool x);
     if (x) return False;
     else   return True;
 endfunction: notFn

The factorial function, using a loop:

 function int factorial (int n);
    int f = 1, j = 0;
    while (j < n)
      begin
        f = f * j;
        j = j + 1;
      end
    factorial = f;
 endfunction: factorial

The factorial function, using recursion:

 function int factorial (int n);
    if (n <= 1) return (1);
    else return (n * factorial (n - 1));
 endfunction: factorial


  Sign in   Home   Sitemap   Terms   Report Abuse   Print  |  Powered by Google Sites