Bluespec $display within function

957 views Asked by At

Bluespec complains when I try to use $display within a function definition.

It is only allowed within a rule definition.

Is there a way that I can get to display variable names within a function?

I think it might be using the ActionValue# but I cannot get the syntax right. I also cannot find examples online.

Thanks

3

There are 3 answers

0
user2482849 On BEST ANSWER

One correct syntax is -

function ActionValue#(rtype) func(...)
= actionvalue
    do something;
    $display(...);
    return val;
endactionvalue;
0
Aninhumer On

Bluespec functions are statically elaborated at compile time, so in general it does not make sense to use $display statements inside them. However, if the function returns an Action or ActionValue, the action it returns can contain a $display statement, which will then be elaborated inside any rules or methods which call it.

The syntax for creating an ActionValue is:

actionvalue
    //Anything that can go in a rule body can go here.
    $display(...);
    return value;
endactionvalue

The syntax for a plain Action is similar, but with action/endaction and no return statement.

This can then be used in a function definition like so:

function ActionValue#(Type) funcName(Type arg);
    ...
    return actionvalue
        ...
    endactionvalue;
endfunction

If you only need the return statement, you can also use the alternative syntax:

function ActionValue#(Type1) funcName(Type2 arg) =
actionvalue
    ...
endactionvalue;

One final point, if you actually want to generate output from functions at compile time, you can use the message function. However, note that this only takes a simple String argument, rather than handling formatting like $display does.

1
Janus Troelsen On

A Bluespec function is pure by default. If you want to debug it, split it up into several pieces, save the function result in a field and extract it to your testbench with a method.

Check out the book Bluespec by Example.

$display is a function with a side effect, so it has to be called within an "action" block.