Main Content

Generate Event-Based SystemVerilog DPI Component

SystemVerilog events are static objects used for synchronization between concurrently active processes. This example shows how to generate an event-based SystemVerilog DPI component from Simulink® and verify the generated component using an HDL simulator.

This example uses the proportional-integral-derivative (PID) controller from the example Get Started with SystemVerilog DPI Component Generation.

Set Up Model

To open the design, run the following command:

open_system("svdpi_pid.slx");

To set up the model to generate event-based DPI module, open the HDL Verifier app from the Apps tab:

  1. Select DPI Component Generation in the Mode section.

  2. Select SystemVerilog Settings in the Prepare section.

  3. Set Component template type to Event-Based Module and click OK.

Alternatively, run the following commands to set up the model:

set_param("svdpi_pid","SystemTargetFile","systemverilog_dpi_grt.tlc");
set_param("svdpi_pid","DPIComponentTemplateType","Event-Based Module");

Generate Event-Based SystemVerilog Module

To generate the event-based DPI module, select the Controller subsystem in the model, and in the Simulink toolstrip click Generate DPI Component in the Generate section.

Alternatively, run the following command to generate DPI:

slbuild("svdpi_pid/Controller");
### Searching for referenced models in model 'Controller'.
### Total of 1 models to build.
### Starting build procedure for model: Controller
### Generated code for 'Controller' is up to date because no structural, parameter or code replacement library changes were found.
### Starting SystemVerilog DPI Component Generation
### Successful completion of build procedure for model: Controller

Build Summary

Top model targets:

Model       Build Reason                             Status          Build Duration
===================================================================================
Controller  Compilation artifacts were out of date.  Code compiled.  0h 0m 13.491s

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 19.208s

Open the generated component file,

edit("Controller_build/Controller_dpi.sv");

In the file, note that five events are declared and defined in the module.

generated module

Verify Event-Based SystemVerilog Module

To use the events in your code, import the package DUT_dpi_pkg and instantiate the DPI module DUT_dpi. Trigger an event using the -> or ->> operator. To wait for an event, use the @ operator or specify <event_name>.triggered.

Open the testbench file provided with this example.

edit("Controller_dpi_event_tb.sv");

The testbench triggers DPI_INIT at time 0. Then, every 10 ns, it triggers DPI_OUTPUT and DPI_UPDATE, and compares the DPI output with the Simulink output. Finally, the testbench triggers the DPI_TERMINATE event when the simulation completes.

To simulate the testbench, make sure that Questa™ simulator is on the system path, and then run the following command:

[~, result] = system('vsim -c -do run_event_tb.do') 
result = 
    'Reading pref.tcl
     
     # 2024.3_1
     
     # do run_event_tb.do
     # QuestaSim-64 vlog 2024.3_1 Compiler 2024.10 Oct 17 2024
     # Start time: 09:28:32 on Nov 14,2025
     # vlog -sv ./Controller_build/Controller_dpi_pkg.sv ./Controller_build/Controller_dpi.sv ./Controller_dpi_event_tb.sv 
     # -- Compiling package Controller_dpi_pkg
     # -- Compiling package Controller_dpi_sv_unit
     # -- Importing package Controller_dpi_pkg
     # -- Compiling module Controller_dpi
     # -- Compiling package Controller_dpi_event_tb_sv_unit
     # -- Compiling module Controller_dpi_event_tb
     # 
     # Top level modules:
     # 	Controller_dpi_event_tb
     # End time: 09:28:32 on Nov 14,2025, Elapsed time: 0:00:00
     # Errors: 0, Warnings: 0
     # vsim -sv_lib ./Controller_build/Controller work.Controller_dpi_event_tb 
     # Start time: 09:28:32 on Nov 14,2025
     # ** Note: (vsim-3812) Design is being optimized...
     # //  Questa Sim-64
     # //  Version 2024.3_1 linux_x86_64 Oct 17 2024
     # //
     # // Unpublished work. Copyright 2024 Siemens
     # //
     # // This material contains trade secrets or otherwise confidential information
     # // owned by Siemens Industry Software Inc. or its affiliates (collectively,
     # // "SISW"), or its licensors. Access to and use of this information is strictly
     # // limited as set forth in the Customer's applicable agreements with SISW.
     # //
     # // This material may not be copied, distributed, or otherwise disclosed outside
     # // of the Customer's facilities without the express written permission of SISW,
     # // and may not be used in any way not expressly authorized by SISW.
     # //
     # Loading sv_std.std
     # Loading work.Controller_dpi_pkg(fast)
     # Loading work.Controller_dpi_sv_unit(fast)
     # Loading work.Controller_dpi_event_tb_sv_unit(fast)
     # Loading work.Controller_dpi_event_tb(fast)
     # Loading ././Controller_build/Controller.so
     # Vector compare count = 60001
     # **************TEST COMPLETED (PASSED)**************
     # ** Note: $finish    : ./Controller_dpi_event_tb.sv(60)
     #    Time: 600030 ns  Iteration: 1  Instance: /Controller_dpi_event_tb
     # End time: 09:28:35 on Nov 14,2025, Elapsed time: 0:00:03
     # Errors: 0, Warnings: 0
     '

See Also

(Simulink)

Topics