Sutherland Logo Graphic Motto Graphic
Home Workshop Descriptions eTutored™ Online Schedule Request Training Workshop Pricing & Terms Books by Stuart Sutherland Papers by Stuart Sutherland Online Reference Guides Links to Other Sites Contact Us

"The instructor was clearly an expert, and extremely good at presenting the material."

Verilog and SystemVerilog Quiz and Tips

This example illustrates a coding error that is easy to make when creating a SystemVerilog Object-Oriented testbench. Only the relevent portions of code are shown, not the full test context. The test program declares handles for a "Generator" object and a "Scoreboard" object, and then passes those handles to a "build_env" function to actually construct those objects and construct a mailbox so that the generator can communicate with the scoreboard. The problem with this code, however, is that after the "build_env" function returns, the generator and scoreboard handles are still null -- the test program (the "calling scope") does not see the objects that the function created.

program test;
  Generator  gen;  // handle for stimulus generator
  Scoreboard sb;   // handle for response scoreboard
  initial begin
    build_env ( .g(gen), .s(sb) );  // pass handles to environment builder
    ... // gen and sb handles should now reference actual objects, but are null
    ...
  end

  function void build_env(Generator g, Scoreboard s);
    mailbox mbx;
    mbx = new;    // construct a mailbox object and store handle in mbx
    g = new(mbx); // construct a Generator object and store handle in g
    s = new(mbx); // construct a Scoreboard object and store handle in s
    return;
  endfunction
endprogram

Why does it appear that the Generator and Scoreboard objects did not get created?

Answer

The code that calls the "build_env" function cannot see the constructed objects because the direction of the "g" and "s" function arguments is not specified, and therefore default to input arguments. Within the function, these input arguments are local variables. The handles created by the "build_env" function are stored in the local "g" and "s" variables, but these handles are not passed back to code that called the "build_env" function.

The error in the code is the declaration of the "build_env" function. In a method that constructs objects, the handle arguments should be declared as "ref" instead of "input" (which is the default direction), "output" or "inout". The correct declaration of the "build_env" function is:

  function void build_env(ref Generator g, ref Scoreboard s);

A "ref" argument is a reference to variable storage in the scope that calls the method. As "ref" arguments, when the "build_env" function assigns to the argument name, it is directly assigning to the handle variable in the scope that called the function.

Coding Tips Sign-Up