Main Content

Get Started with MATLAB Based SystemVerilog DPI Generation

This example shows you how to generate a SystemVerilog DPI component for a programmable square-wave generator written in MATLAB®, and export it to an HDL simulator.

For demonstrative purposes, this example uses Modelsim® 10.3c in 64-bit Windows® 7. However, this same procedure can be easily replicated for other systems and simulators.

Requirements and Prerequisites

Products required for this example:

  • MATLAB Coder™

  • Simulators: Mentor Graphics® ModelSim®/QuestaSim® or Cadence® Xcelium™

  • One of the supported C compilers: Microsoft® Visual C++, or GNU GCC


The MATLAB code used in this example demonstrates a simple programmable square wave generator. This example also provides a MATLAB test bench that exercises the design.

The following image illustrates the DPI component that is generated in this example:

  • The 'On_time' and 'Off_time' control the output signal duty cycle.

For example, if On_time=1 and Off_time=1, the square wave generated will have 50% duty cycle.

Simulate Design with Supplied MATLAB Testbench

To make sure there are no run-time errors, and that the design meets the requirements, simulate the design with the supplied testbench prior to code generation. Enter the following command in MATLAB:


It should plot the figure below:

Note that the plot shows the output signal that was programmed to behave as follows:

  • Clock cycles<100: on=1,off=1

  • 100<Clock cycles<250: on=2,off=1

  • 250<Clock cycles: on=1,off=2

Generate the DPI component using DPIGEN command

Using DPIGEN, generate the DPI component.

Execute the DPIGEN command in MATLAB as follows:

dpigen -testbench ProgSWGenerator_tb ProgSWGenerator -args {0,0}

The following directory structure is generated:

dpi_tb: Folder where all the testbench related files are. Generated DPI component. Generated SystemVerilog package.

libProgSWGenerator_dpi.dll: Library containing the definitions of all imported functions.

Note: DPIGEN will automatically try to compile the library. In order to just generate the files without compiling you should use the -c option. For example:

dpigen -c -testbench ProgSWGenerator_tb  ProgSWGenerator -args {0,0}

Run Generated Testbench in HDL Simulator

For ModelSim/QuestaSim, perform the following steps:

  • Start ModelSim/QuestaSim in GUI mode.

  • In the HDL Simulator,Change your current directory to "dpi_tb" under the code generation directory.

  • Enter the following command to start your simulation


For example:

The following wave form are generated:

Note that the output wave (the bottom one) behaves in exactly the same way as the signal the was plotted in the MATLAB testbench.

For Xcelium and VCS simulator:

  • Start your terminal shell

  • Change the current directory to "dpi_tb" under the code generation directory

  • For Xcelium enter the following command in your shell.

  • For VCS enter the following command in your shell.

  • When the simulation finishes, you should see the following text printed in your console:

**************TEST COMPLETED (PASSED)**************

This ends the Getting Started with MATLAB based SystemVerilog DPI Component Generation example.