Verilog Procedural Interface - Example

Example

As an example, consider the following Verilog code fragment:

val = 41; $increment(val); $display("After $increment, val=%d", val);

Suppose the increment system task increments its first parameter by one. Using C and the VPI mechanism, the increment task can be implemented as follows:

// Implements the increment system task static int increment(char *userdata) { vpiHandle systfref, args_iter, argh; struct t_vpi_value argval; int value; // Obtain a handle to the argument list systfref = vpi_handle(vpiSysTfCall, NULL); args_iter = vpi_iterate(vpiArgument, systfref); // Grab the value of the first argument argh = vpi_scan(args_iter); argval.format = vpiIntVal; vpi_get_value(argh, &argval); value = argval.value.integer; vpi_printf("VPI routine received %d\n", value); // Increment the value and put it back as first argument argval.value.integer = value + 1; vpi_put_value(argh, &argval, NULL, vpiNoDelay); // Cleanup and return vpi_free_object(args_iter); return 0; }

Also, a function that registers this system task is necessary. This function is invoked prior to elaboration or resolution of references when it is placed in the externally visible vlog_startup_routines array.

// Registers the increment system task void register_increment { s_vpi_systf_data data = {vpiSysTask, 0, "$increment", increment, 0, 0, 0}; vpi_register_systf(&data); } // Contains a zero-terminated list of functions that have to be called at startup void (*vlog_startup_routines) = { register_increment, 0 };

The C code is compiled into a shared object that will be used by the Verilog simulator. A simulation of the earlier mentioned Verilog fragment will now result in the following output:

VPI routine received 41 After $increment, val=42

Read more about this topic:  Verilog Procedural Interface

Famous quotes containing the word example:

    Our intellect is not the most subtle, the most powerful, the most appropriate, instrument for revealing the truth. It is life that, little by little, example by example, permits us to see that what is most important to our heart, or to our mind, is learned not by reasoning but through other agencies. Then it is that the intellect, observing their superiority, abdicates its control to them upon reasoned grounds and agrees to become their collaborator and lackey.
    Marcel Proust (1871–1922)