Use Variable-Sized Vector in SystemVerilog DPI Component
This example shows how to configure, generate, and use a SystemVerilog DPI (SVDPI) component with variable-length inputs or outputs.
Design Task
When you generate an SVDPI from a MATLAB® function that includes a variable-sized vector input or output, the result is a SystemVerilog module with a variable-sized input or output. Follow this example to configure, generate, and use the component in a SystemVerilog environment.
MATLAB Function
This example uses the function varSizeVectorSupport, a sine function where the size of the input vec is variable. The output of the function is derived from the input, and therefore the output size is also variable.
Testbench
Use the provided varSizeVectorSupport_tb testbench to stimulate the function with fixed-sized signals. Then, generate a SystemVerilog testbench to test the generated SystemVerilog module. You can later change the port types of the generated SystemVerilog testbench (or write a new testbench) without regenerating a DPI module from the varSizeVectorSupport function.
Configure Generation Options
Create an svdpiConfiguration object for a sequential module with variable-size ports. Then, set the toolchain to a supported simulator (for example, the Questa™ simulator). Make sure that the specified simulator is on the system path.
cfg = svdpiConfiguration('sequential-module-varsize'); if ispc cfg.CoderConfiguration.Toolchain = "Siemens Questa/Modelsim (64-bit Windows)"; else cfg.CoderConfiguration.Toolchain = "Siemens Questa/Modelsim (64-bit Linux)"; end
Generate SystemVerilog DPI Component
To generate the SystemVerilog module, use the dpigen function.
You must pass the configuration object to the
dpigenfunction using the-configargument.Use the
-argsargument to specify the size of the generated ports. To set an upper bound value for the variable-length vector, specify that value in thecoder.Typeobject. For example, to set an upper bound of 20 forvec, enter this code at the MATLAB command prompt.
dpigen -config cfg varSizeVectorSupport -args {coder.typeof(1,[1 20],[0 1]),1,1}
### Generating DPI-C Wrapper varSizeVectorSupport_dpi.c ### Generating DPI-C Wrapper header file varSizeVectorSupport_dpi.h ### Generating file varSizeVectorSupport_pkg.sv ### Generating file varSizeVectorSupport.sv ### Generating vsim makefile/script varSizeVectorSupport.do Code generation successful. ### Executing simulator script using vsim on system path ### Successful script execution.
Use
infto specify that the port size is of variable-length and unbounded in the generated SystemVerilog.To generate a SystemVerilog testbench from a MATLAB testbench function, use the
-testbenchargument.
dpigen -config cfg varSizeVectorSupport -testbench varSizeVectorSupport_tb -args {coder.typeof(1,[1 inf],[0 1]),1,1}
### Generating DPI-C Wrapper varSizeVectorSupport_dpi.c ### Generating DPI-C Wrapper header file varSizeVectorSupport_dpi.h ### Generating file varSizeVectorSupport_pkg.sv ### Generating file varSizeVectorSupport.sv ### Generating vsim makefile/script varSizeVectorSupport.do ### Running simulation to capture input and expected outputs for a standalone testbench. ### This may take some time... Code generation successful. ### ...DONE with test-vector capture. ### Generating file run_tb_mq.do ### Generating file varSizeVectorSupport_tb.sv Code generation successful. ### Executing simulator script using vsim on system path ### Successful script execution.
Generated Interface
Because vec is specified as a variable-sized vector during code generation in this example, the generated SystemVerilog includes the variable-sized input vec and output y. The data type of the output y is derived from the input data type. These variable-sized ports are declared as SystemVerilog open arrays ([ ]). This code shows the generated interface for the varSizeVectorSupport function.
module varSizeVectorSupport_dpi( input bit clk, input bit clk_enable, input bit reset, input real vec [], input real amp, input real freq, output shortint y [] );
Simulate in Questa Simulator
To simulate the testbench and verify the generated component, run the following command:
cd codegen/dll/varSizeVectorSupport/dpi_tb !vsim -c -do run_tb_mq.do
Reading pref.tcl # 2024.3_1 # do run_tb_mq.do # run -all # .. # QuestaSim-64 vlog 2024.3_1 Compiler 2024.10 Oct 16 2024 # Start time: 11:48:15 on Nov 24,2025 # vlog "+define+MG_SIM" ../varSizeVectorSupport_pkg.sv ../varSizeVectorSupport.sv ./varSizeVectorSupport_tb.sv # -- Compiling package varSizeVectorSupport_pkg # -- Compiling package varSizeVectorSupport_sv_unit # -- Importing package varSizeVectorSupport_pkg # -- Compiling interface varSizeVectorSupport_interface # -- Compiling module varSizeVectorSupport # -- Compiling package varSizeVectorSupport_tb_sv_unit # -- Compiling module varSizeVectorSupport_tb # # Top level modules: # varSizeVectorSupport_tb # End time: 11:48:15 on Nov 24,2025, Elapsed time: 0:00:00 # Errors: 0, Warnings: 0 # vsim -c -voptargs="+acc" -L ../work work.varSizeVectorSupport_tb # Start time: 11:48:16 on Nov 24,2025 # ** Note: (vsim-3812) Design is being optimized... # ** Warning: (vopt-10587) Some optimizations are turned off because the +acc switch is in effect. This will cause your simulation to run slowly. Please use -access/-debug to maintain needed visibility. The +acc switch would be deprecated in a future release. # ** Note: (vsim-12126) Error and warning message counts have been restored: Errors=0, Warnings=1. # // Questa Sim-64 # // Version 2024.3_1 win64 Oct 16 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.varSizeVectorSupport_pkg(fast) # Loading work.varSizeVectorSupport_tb_sv_unit(fast) # Loading work.varSizeVectorSupport_tb(fast) # Loading work.varSizeVectorSupport_sv_unit(fast) # Loading work.varSizeVectorSupport_interface(fast__1) # Loading work.varSizeVectorSupport(fast) # Loading C:/Users/hagam/AppData/Local/Temp\hagam@MATHWORKS-GSCFO_dpi_26440\win64_gcc-7.4.0\vsim_auto_compile.dll # Tolerance parameter for floating point comparisons is set to: 4.440892e-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 : ./varSizeVectorSupport_tb.sv(108) # Time: 52 ns Iteration: 0 Instance: /varSizeVectorSupport_tb # Vector compare count = 2 # End time: 11:48:33 on Nov 24,2025, Elapsed time: 0:00:17 # Errors: 0, Warnings: 1
Limitations
This feature does not support:
Variable-sized matrices on the interface. If you have a matrix in your model, convert it to a variable-sized vector before connecting to an input or output.
Structure data types with fields of variable size
Variable-sized arrays of structures
Cross-platform DPI component generation