3

I am trying to understand SystemVerilog function return values from the Language resource manual(Section 10.3.1), but I am having difficulties in grasping the following section. Can anyone help me interpret it? I tried looking in different sites but the information wasn't that deep.

In SystemVerilog, a function return can be a structure or union. In this case, a hierarchical name used inside the function and beginning with the function name is interpreted as a member of the return value. If the function name is used outside the function, the name indicates the scope of the whole function. If the function name is used within a hierarchical name, it also indicates the scope of the whole function.

a = b + myfunc1(c, d); //call myfunc1 (defined above) as an expression
myprint(a); //call myprint (defined below) as a statement
function void myprint (int a);
...
endfunction

2 Answers 2

8

You can use two different ways to return a value from a function. For example as follows,

function int myfunc1(int c, d);
  myfunc1 = c+d;
endfunction

and

function int myfunc1(int c, d);
  return c+d;
endfunction

So when the function is declared as a structure or union type, the hierarchical name beginning with the function name also means the variable of the return value. But the old LRM description is not right and precise now because the hierarchical name now could also be the function scope, not the return value. For example,

typedef struct { int c, d, n; } ST;

function ST myfunc1(int c, d);
  static int x = 1;
  myfunc1.c = c;          // myfunc1 refers to the return structure
  myfunc1.d = d;          // myfunc1 refers to the return structure
  myfunc1.n = c + d +     // myfunc1 refers to the return structure
              myfunc1.x;  // myfunc1 refers to function scope
endfunction

Another interesting example of using hierarchical name containing the function name.

typedef struct { int c, d; } ST ;

module top;
  function ST myfunc1(int c,d);
    top.myfunc1.c = c;
    myfunc1.c = 1;
    myfunc1.d = d;
  endfunction

  ST x;
  initial begin
    x = myfunc1(3,2);
    #1 $display("%d %d %d", x.c, x.d, top.myfunc1.c);
  end
endmodule

The function call of x = myfunc1(3,2) constructs a call frame for myfunc1 and pass values for evaluation. The scopes of myfunc1 and top.myfunc1 are different. The hierarchical name beginning with myfunc1 refers to current call frame of the function, while top.myfunc1 refers to the scope of function declared inside the module top . So the message will be 1 2 3.

Sign up to request clarification or add additional context in comments.

Comments

5

It looks like you are referencing a really old version of the LRM. Get the latest official version IEEE Std 1800-2012. You'll want to look at § 13.4.1 Return values and void functions. There is a line missing between quoted paragraph and quoted code:

Functions can be declared as type void, which do not have a return value. Function calls may be used as expressions unless of type void, which are statements:

The example code is not referring you your question hierarchical name access, it is an example of the void return type.

The example code below demonstrates hierarchical name access with a struct/union return types. Read about structs and unions in § 7.2 and § 7.3.

function struct { byte a,b; } struct_func (byte a,b);
  byte c;
  c = a ^ b;
  struct_func.a = a; // <== hierarchical name used inside the function
  struct_func.b = ~b;
endfunction

initial begin
  // function name is used within a hierarchical name ( struct member )
  $display("%h", struct_func(8'h42,8'hFE).b ); // returns 01
  // function name is used within a hierarchical name ( function member )
  $display("%h", struct_func.b ); // returns fe (last value of input b)
  // function name is used within a hierarchical name ( function member )
  $display("%h", struct_func.c ); // returns bc (last value of variable c)
end

Most cases you want to reuse struct/union definitions and should be defined as a typedef. The below function with the yeild the same results with the above initial block.

typedef struct { byte a,b; } my_struct;

function my_struct struct_func (byte a,b);
  byte c;
  c = a ^ b;
  struct_func.a = a; // <== hierarchical name used inside the function
  struct_func.b = ~b;
endfunction

1 Comment

In most cases, you are required to use a typedef in order to return a aggregate type. That is because a) the syntax will not allow you to specify an unpacked range in the function header, and b) the return type is anonymous and incompatible with any other type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.