VPI Basics - HACKT PRSIM Manual
Description: HACKT PRSIM Manual

Next: VPI Example, Previous: Verilog PLI Setup, Up: Co-simulation

### 6.2 VPI Basics

Now that you've setup your VCS compilation environment to link in the hacprsim library. It's time to connect your Verilog instances to hacprsim. This section walks you through the basic steps.

The vpihacprsim.so object you linked in to your simulation executable defines some new functions that one can call from Verilog. The first thing you should do is tell VCS what HAC circuit you are co-simulating.

— Function: $prsim obj

> Loads HAC object file obj for hacprsim co-simulation. The object file need not be compiled through the allocation phase, the library will automatically compile it through the allocation phase for you if needed.

         initial
         begin
           $prsim("my_hac_circuit.haco-a");
           ...
         end

The named object file should be compiled from HAC source. Though the object file doesn't not needed to be compiled through the allocation phase, it is recommended to catch errors as early as possible. In any case, the hacprsim library will automatically compile the object file further if needed. It is important to load this in the `initial` block before any further actions are done with hacprsim. Before running the simulation executable, you need to make sure that the object file loaded by $prsim() exists. (Otherwise the simulation will fail with a thrown exception, showing a stack dump.)

Continuing in the `initial` block, you can make connections between Verilog and hacprsim. Suppose your HAC file instantiates the following:

         defproc inv(bool a, b) { prs { a => b- } }
         inv foo, bar;

You could write connections in Verilog with the following functions:

— Function: $to_prsim vname pname

> Establish a connection from Verilog signal vname to hacprsim signal pname. When vname changes value, hacprsim will be updated accordingly. This command should be invoked prior to any events in simulation.

— Function: $from_prsim pname vname

> Establish a connection from hacprsim signal pname to Verilog signal vname. When pname changes value in hacprsim, the Verilog simulator will be notified accordingly.

A signal can go both ways between the simulation environments, as in the following example:

         module TOP;
           reg a, b, c;
         initial
         begin
           $prsim("my_hac_circuit.haco-a");
           $to_prsim("TOP.a", "foo.a");
           $from_prsim("foo.b", "TOP.b");
           $to_prsim("TOP.b", "bar.a");
           $from_prsim("bar.b", "TOP.c");
         end
         endmodule

`"TOP.b"` is both driven by hacprsim and also fans out to circuits in hacprsim.

There are other useful functions in the hacprsim VPI library.

— Function: $prsim_cmd cmd

> Runs an arbitrary command cmd (string) that would normally be interpreted by hacprsim. This is the one command to rule them all, the last command you will ever need.

— Function: $prsim_default_after time

> Set the default delay for unspecified rules to time, in hacprsim's time units (unitless). This is analogous to the command-line -D time option. This command should be invoked before loading the object file ($prsim()) that initializes the state of the simulation.

— Function: $prsim_sync

> Synchronize callbacks with current hacprsim event queue. This is needed because some $prsim_cmd commands may introduce new events into the event queue, which requires re-registration of the callback function with updated times.

> Update: this command is now deprecated because now all $prsim_cmd calls automatically re-synchronize the event queues, as a conservative measure.

— Function: $prsim_set node val

> Sets a node in hacprsim to val. Synonymous with $prsim_cmd("set node val");.

— Function: $prsim_get node

> Prints value of node in hacprsim. Synonymous with $prsim_cmd("get node");.

— Function: $prsim_watch node

> Register a prsim-driven node watch-point. Synonymous with $prsim_cmd("watch node");.

— Function: $prsim_mkrandom v

> For v 1, synonymous with $prsim_cmd("timing random");. For v 0, synonymous with $prsim_cmd("timing after");.

— Function: $prsim_resetmode v

> For v 1, synonymous with $prsim_cmd("mode reset");. For v 0, synonymous with $prsim_cmd("mode run");.

The following sections walk you through examples of co-simulating hacprsim with VCS. The author highly recommends copying these examples to run them and study them.