Use Custom Template to Generate DPI Component
R2026bThis example shows how to create a custom target language compiler (TLC) template for a Simulink® model and generate a SystemVerilog DPI component from it.
The example uses a Simulink model of a sine wave generator as the device under test (DUT).
Create Custom TLC Template
To get started, create a custom template that reflects the interface, clocking, and style of your design. A simple way to start is to copy and modify one of the shipping templates available at:
matlabroot/toolbox/hdlverifier/dpigenerator/rtw/
This example includes MyTemplate.tlc, which is a copy of the sequential model template, customized to include a name and a comment in the file header. The following part opens the component file for writing, and defines the header contents:
%% ================================================================================ %% SystemVerilog Module File %% ================================================================================ %openfile sv_file = "%<gtd.ComponentTypeName>.sv" // ------------------------------------------- // File: %<gtd.ComponentTypeName>.sv // Author: %<gtd.author> // Comment: %<gtd.comment> %<SVTLCommonHeader>
Create Custom Template Object
Use the DPICustomTemplateConfiguration object to configure your template.
For more information about custom template syntax, see Custom Template Language Syntax.
cfg = DPICustomTemplateConfiguration; cfg.ComponentTemplateFiles = "MyTemplate.tlc"; cfg.TestBenchTemplateFiles = "MyTBTemplate.tlc"'; cfg.TemplateDictionary = {'author','Jane Doe';'comment','This is a great example!'}
cfg =
DPICustomTemplateConfiguration with properties:
ComponentTemplateFiles: {'C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3275278\hdlverifier-ex86814857\MyTemplate.tlc'}
TestBenchTemplateFiles: {'C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3275278\hdlverifier-ex86814857\MyTBTemplate.tlc'}
TemplateDictionary: {2×2 cell}
PortGroups: []
IsSequentialTemplate: 1
IsUVMTemplate: 0
To use the object definition available as a template in Simulink, save it to a MAT file.
save myCustomTemplate.mat cfg
Use the Template in Simulink
First, open the model.
open_system("svdpi_BitVector.slx")Set Configuration Parameters
In Simulink, in the Apps tab, click HDL Verifier. Open the HDL Verifier tab. In the HDL Verifier Mode pane, select DPI Component Generation. Then, in the toolstrip, click SystemVerilog Settings to open the Configuration Parameters window. In the System Verilog DPI pane:
Set Component Template Type to
Custom.Set Load custom configuration object from MAT file to
myCustomTemplate.mat.
Click OK.
Alternatively, set these parameters by typing this in the MATLAB® command window:
set_param("svdpi_BitVector","DPIComponentTemplateType","Custom") set_param('svdpi_BitVector',"DPICustomTemplateConfigurationFile","myCustomTemplate.mat")
Generate Component
In Simulink, select the SineWaveGenerator subsystem. Verify that it appears in the Generate DPI Component for parameter in the toolstrip. Then, click Generate DPI Component.

Alternatively, enter this code in the MATLAB Command Window:
slbuild('svdpi_BitVector/SineWaveGenerator')### Searching for referenced models in model 'SineWaveGenerator'.
### Total of 1 models to build.
### Starting top model code generation target build for: SineWaveGenerator
### Build reason: binary information cache did not exist. The slprj folder containing the binary information cache might have been removed.
### Starting build procedure for model: SineWaveGenerator
### Build folder for generated code: C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3275278\hdlverifier-ex86814857\SineWaveGenerator_build
### Invoking Target Language Compiler on SineWaveGenerator.rtw
### Using System Target File: Z:\55\user.Bhdl_verifier.j3282454\matlab\toolbox\hdlverifier\dpigenerator\rtw\systemverilog_dpi_grt.tlc
### Loading TLC function libraries
........
### Initial pass through model to cache user defined code
### Caching model source code
.........
### Generating TLC interface API.
..
### Writing source file SineWaveGenerator_capi.c
.
### Writing header file SineWaveGenerator_capi.h
...........................
### Writing header file SineWaveGenerator_types.h
### Writing header file SineWaveGenerator.h
### Writing header file rtwtypes.h
### Writing header file builtin_typeid_types.h
### Writing header file multiword_types.h
### Writing source file SineWaveGenerator.c
.
### Writing header file SineWaveGenerator_private.h
### Writing header file rtmodel.h
### TLC code generation complete (took 5.218s).
### Starting SystemVerilog DPI Component Generation
### Saving binary information cache.
### Generating DPI H Wrapper SineWaveGenerator_dpi.h
### Generating DPI-C Wrapper SineWaveGenerator_dpi.c
### Generating file SineWaveGenerator_dpi_pkg.sv
### Generating file SineWaveGenerator_dpi.sv
### Starting test vector capture
### Running simulation to capture input and expected outputs for a standalone testbench.
### This may take some time...
### Running Simulink simulation to capture inputs and expected outputs
### ...DONE with test-vector capture.
### Generating file run_tb_mq.do
### Generating file run_tb_xcelium.sh
### Generating file run_tb_vcs.sh
### Generating file run_tb_vivado.sh
### Generating file run_tb_vivado.bat
### Generating file SineWaveGenerator_dpi_tb.sv
### Generating makefiles for: SineWaveGenerator_dpi
### Invoking make to build the DPI Shared Library
### Recompiling because source files C:/Users/user/OneDrive - MathWorks/Documents/MATLAB/ExampleManager/user.Bdoc.j3275278/hdlverifier-ex86814857\SineWaveGenerator_build\SineWaveGenerator.c, C:/Users/user/OneDrive - MathWorks/Documents/MATLAB/ExampleManager/user.Bdoc.j3275278/hdlverifier-ex86814857\SineWaveGenerator_build\SineWaveGenerator.h, C:/Users/user/OneDrive - MathWorks/Documents/MATLAB/ExampleManager/user.Bdoc.j3275278/hdlverifier-ex86814857\SineWaveGenerator_build\SineWaveGenerator_capi.c, C:/Users/user/OneDrive - MathWorks/Documents/MATLAB/ExampleManager/user.Bdoc.j3275278/hdlverifier-ex86814857\SineWaveGenerator_build\SineWaveGenerator_capi.h, C:/Users/user/OneDrive - MathWorks/Documents/MATLAB/ExampleManager/user.Bdoc.j3275278/hdlverifier-ex86814857\SineWaveGenerator_build\SineWaveGenerator_dpi.c, C:/Users/user/OneDrive - MathWorks/Documents/MATLAB/ExampleManager/user.Bdoc.j3275278/hdlverifier-ex86814857\SineWaveGenerator_build\SineWaveGenerator_dpi.h, C:/Users/user/OneDrive - MathWorks/Documents/MATLAB/ExampleManager/user.Bdoc.j3275278/hdlverifier-ex86814857\SineWaveGenerator_build\SineWaveGenerator_private.h, C:/Users/user/OneDrive - MathWorks/Documents/MATLAB/ExampleManager/user.Bdoc.j3275278/hdlverifier-ex86814857\SineWaveGenerator_build\SineWaveGenerator_types.h, C:/Users/user/OneDrive - MathWorks/Documents/MATLAB/ExampleManager/user.Bdoc.j3275278/hdlverifier-ex86814857\SineWaveGenerator_build\builtin_typeid_types.h, C:/Users/user/OneDrive - MathWorks/Documents/MATLAB/ExampleManager/user.Bdoc.j3275278/hdlverifier-ex86814857\SineWaveGenerator_build\multiword_types.h, C:/Users/user/OneDrive - MathWorks/Documents/MATLAB/ExampleManager/user.Bdoc.j3275278/hdlverifier-ex86814857\SineWaveGenerator_build\rtmodel.h, C:/Users/user/OneDrive - MathWorks/Documents/MATLAB/ExampleManager/user.Bdoc.j3275278/hdlverifier-ex86814857\SineWaveGenerator_build\rtwtypes.h changed.
### Using toolchain: MinGW64 | gmake (64-bit Windows)
### 'C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3275278\hdlverifier-ex86814857\SineWaveGenerator_build\SineWaveGenerator.mk' is up to date.
### Building 'SineWaveGenerator_win64': "%MINGW_ROOT%\mingw32-make.exe" -j 6 -l 6 -Oline -f SineWaveGenerator.mk all
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3275278\hdlverifier-ex86814857\SineWaveGenerator_build>call "setup_mingw.bat"
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3275278\hdlverifier-ex86814857\SineWaveGenerator_build>set "MINGW_ROOT=D:\3rdparty\internal\12933511\win64\MinGW\bin"
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3275278\hdlverifier-ex86814857\SineWaveGenerator_build>cd .
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3275278\hdlverifier-ex86814857\SineWaveGenerator_build>if "all" == "" ("D:\3rdparty\internal\12933511\win64\MinGW\bin\mingw32-make.exe" -j 6 -l 6 -Oline -f SineWaveGenerator.mk all ) else ("D:\3rdparty\internal\12933511\win64\MinGW\bin\mingw32-make.exe" -j 6 -l 6 -Oline -f SineWaveGenerator.mk all )
"D:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -Wno-error=incompatible-pointer-types -Wno-error=stringop-overflow -O0 -DRT -DUSE_RTMODEL -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=1 -DONESTEPFCN=0 -DTERMFCN=1 -DMULTI_INSTANCE_CODE=1 -DINTEGER_CODE=0 -DMT=0 -DRT_MALLOC -DTID01EQ=0 -DMODEL=SineWaveGenerator -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO @SineWaveGenerator_comp.rsp -o "SineWaveGenerator_capi.obj" "C:/Users/user/ONEDRI~1/DOCUME~1/MATLAB/EXAMPL~1/HA309A~1.J32/HDLVER~1/SineWaveGenerator_build/SineWaveGenerator_capi.c"
"D:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -Wno-error=incompatible-pointer-types -Wno-error=stringop-overflow -O0 -DRT -DUSE_RTMODEL -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=1 -DONESTEPFCN=0 -DTERMFCN=1 -DMULTI_INSTANCE_CODE=1 -DINTEGER_CODE=0 -DMT=0 -DRT_MALLOC -DTID01EQ=0 -DMODEL=SineWaveGenerator -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO @SineWaveGenerator_comp.rsp -o "SineWaveGenerator.obj" "C:/Users/user/ONEDRI~1/DOCUME~1/MATLAB/EXAMPL~1/HA309A~1.J32/HDLVER~1/SineWaveGenerator_build/SineWaveGenerator.c"
"D:\3rdparty\internal\12933511\win64\MinGW\bin/gcc" -c -fwrapv -m64 -Wno-error=incompatible-pointer-types -Wno-error=stringop-overflow -O0 -DRT -DUSE_RTMODEL -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=1 -DONESTEPFCN=0 -DTERMFCN=1 -DMULTI_INSTANCE_CODE=1 -DINTEGER_CODE=0 -DMT=0 -DRT_MALLOC -DTID01EQ=0 -DMODEL=SineWaveGenerator -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO @SineWaveGenerator_comp.rsp -o "SineWaveGenerator_dpi.obj" "C:/Users/user/ONEDRI~1/DOCUME~1/MATLAB/EXAMPL~1/HA309A~1.J32/HDLVER~1/SineWaveGenerator_build/SineWaveGenerator_dpi.c"
### Creating shared library "C:/Users/user/ONEDRI~1/DOCUME~1/MATLAB/EXAMPL~1/HA309A~1.J32/HDLVER~1/SINEWA~1/SineWaveGenerator_win64.dll" ...
"D:\3rdparty\internal\12933511\win64\MinGW\bin/g++" -shared -Wl,--no-undefined -Wl,--out-implib,C:/Users/user/ONEDRI~1/DOCUME~1/MATLAB/EXAMPL~1/HA309A~1.J32/HDLVER~1/SINEWA~1/SineWaveGenerator_win64.lib -o C:/Users/user/ONEDRI~1/DOCUME~1/MATLAB/EXAMPL~1/HA309A~1.J32/HDLVER~1/SINEWA~1/SineWaveGenerator_win64.dll @SineWaveGenerator.rsp -Wl,--start-group -lws2_32 -Wl,--end-group
### Created: "C:/Users/user/ONEDRI~1/DOCUME~1/MATLAB/EXAMPL~1/HA309A~1.J32/HDLVER~1/SINEWA~1/SineWaveGenerator_win64.dll"
### Successfully generated all binary outputs.
C:\Users\user\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\user.Bdoc.j3275278\hdlverifier-ex86814857\SineWaveGenerator_build>exit /B 0
### Successful completion of build procedure for model: SineWaveGenerator
Build Summary
Top model targets:
Model Build Reason Status Build Duration
====================================================================================================================
SineWaveGenerator Information cache folder or artifacts were missing. Code generated and compiled. 0h 0m 14.975s
1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 15.37s
Observe the generated SystemVerilog files in the SineWaveGenerator_build folder. The file SineWaveGenerator_dpi.sv includes these lines in the header.
// ------------------------------------------- // File: SineWaveGenerator_dpi.sv // Author: Jane Doe // Comment: This is a great example! // ------------------------------------------- // Created: 2026-06-11 15:03:26 // Tools : Generated by MATLAB 27.1 and HDL Verifier 27.1 // -------------------------------------------
Simulate SystemVerilog
Before simulating, make sure your HDL simulator is on the system path. For supported simulators, see Supported EDA Tools and Hardware.
The DPI build generates configuration and simulation scripts for supported simulators in the folder SineWaveGenerator_build\dpi_tb.
To run a Questa™ simulation, navigate to the folder and enter this code in the MATLAB Command Window:
cd SineWaveGenerator_build\dpi_tb\ ! vsim -c -do run_tb_mq.do
Reading pref.tcl # 2025.3 # do run_tb_mq.do # run -all # .. # ** Warning: (vlib-34) Library already exists at "work". # Errors: 0, Warnings: 1 # QuestaSim-64 vlog 2025.3 Compiler 2025.09 Sep 15 2025 # Start time: 15:39:43 on Jun 17,2026 # vlog "+define+MG_SIM" ../SineWaveGenerator_dpi_pkg.sv ../SineWaveGenerator_dpi.sv ./SineWaveGenerator_dpi_tb.sv # ** Note: (vlog-220) 'D:/share/apps/HDLTools/ModelSim/questasim-2025.3/win64/win64/../modelsim.ini' is used as the ini file. # -- Compiling package SineWaveGenerator_dpi_pkg # -- Compiling package SineWaveGenerator_dpi_sv_unit # -- Importing package SineWaveGenerator_dpi_pkg # -- Compiling module SineWaveGenerator_dpi # -- Compiling package SineWaveGenerator_dpi_tb_sv_unit # -- Compiling module SineWaveGenerator_dpi_tb # # Top level modules: # SineWaveGenerator_dpi_tb # End time: 15:39:43 on Jun 17,2026, Elapsed time: 0:00:00 # Errors: 0, Warnings: 0 # ** Note: (vsim-220) 'D:/share/apps/HDLTools/ModelSim/questasim-2025.3/win64/win64/../modelsim.ini' is used as the ini file. # vsim "+define+MG_SIM" -c -voptargs="+acc" -sv_lib ../SineWaveGenerator_win64 work.SineWaveGenerator_dpi_tb # Start time: 15:39:44 on Jun 17,2026 # ** Note: (vsim-8009) Loading existing optimized design _opt # // Questa Sim-64 # // Version 2025.3 win64 Sep 15 2025 # // # // Unpublished work. Copyright 2025 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.SineWaveGenerator_dpi_pkg(fast) # Loading work.SineWaveGenerator_dpi_tb_sv_unit(fast) # Loading work.SineWaveGenerator_dpi_tb(fast) # Loading work.SineWaveGenerator_dpi_sv_unit(fast) # Loading work.SineWaveGenerator_dpi(fast) # Loading .\../SineWaveGenerator_win64.dll # Tolerance parameter for floating point comparisons is set to: 4.440900e-16. # Override the SystemVerilog parameter using the EXTRA_SVDPI_SIM_ARGS or EXTRA_SVDPI_COMP_ARGS appropriate for your simulator. # **************TEST COMPLETED (PASSED)************** # ** Note: $finish : ./SineWaveGenerator_dpi_tb.sv(69) # Time: 10042 ns Iteration: 0 Instance: /SineWaveGenerator_dpi_tb # Vector compare count = 1001 # End time: 15:39:47 on Jun 17,2026, Elapsed time: 0:00:03 # Errors: 0, Warnings: 0
See Also
DPICustomTemplateConfiguration | slbuild (Simulink)