Main Content

Run MATLAB-HDL Cosimulation

Process for Running MATLAB Cosimulation

To start and control the execution of a simulation in the MATLAB® environment, perform the following steps:

Check MATLAB Cosimulation Server's Link Status

The first step to starting an HDL simulator and MATLAB test bench or component function session is to check the link status of the MATLAB server. Is the server running? If the server is running, what mode of communication and, if applicable, what TCP/IP socket port is the server using for its links? You can retrieve this information by using the MATLAB function hdldaemon with the 'status' option. For example:

hdldaemon('status')

The function displays a message that indicates whether the server is running and, if it is running, the number of connections it is handling. For example:

HDLDaemon socket server is running on port 4449 with 0 connections

If the server is not running, the message reads

HDLDaemon is NOT running

See the Options: Inputs section in the hdldaemon reference documentation for information on determining the mode of communication and the TCP/IP socket in use.

Run Cosimulation

You can run a cosimulation session using both the MATLAB and HDL simulator GUIs (typical) or, to reduce memory demand, you can run the cosimulation using the command line interface (CLI) or in batch mode.

Cosimulation with MATLAB Using the HDL Simulator GUI

These steps describe a typical sequence for running a simulation interactively from the main HDL simulator window:

  1. Set breakpoints in the HDL and MATLAB code to verify and analyze simulation progress.

    How you set breakpoints in the HDL simulator will vary depending on what simulator application you are using.

    In MATLAB, there are several ways you can set breakpoints; for example, by using the Set/Clear Breakpoint button on the toolbar.

  2. Issue matlabtb command at the HDL simulator prompt.

    When you begin a specific test bench or component session, you specify parameters that identify the following information:

    For example:

    hdlsim> matlabtb osc_top -sensitivity /osc_top/sine_out
          -socket 4448 -mfunc hosctb
  3. Start the simulation by entering the HDL simulator run command.

    The run command offers a variety of options for applying control over how a simulation runs (refer to your HDL simulator documentation for details). For example, you can specify that a simulation run for several time steps.

    The following command instructs the HDL simulator to run the loaded simulation for 50000 time steps:

    run 50000
    
  4. Step through the simulation and examine values.

    How you step through the simulation in the HDL simulator will vary depending on what simulator application you are using.

    In MATLAB, there are several ways you can step through code; for example, by clicking the Step toolbar button.

  5. When you block execution of the MATLAB function, the HDL simulator also blocks and remains blocked until you clear all breakpoints in the function's code.

  6. Resume the simulation, as desired.

    How you resume the simulation in the HDL simulator will vary depending on what simulator application you are using.

    In MATLAB, there are several ways you can resume the simulation; for example, by clicking the Continue toolbar button.

    The following HDL simulator command resumes a simulation:

    run -continue
    

For more information on HDL simulator and MATLAB debugging features, see the HDL simulator documentation and MATLAB online help or documentation.

Cosimulation with MATLAB Using the Command Line Interface (CLI)

Running your cosimulation session using the command-line interface allows you to interact with the HDL simulator during cosimulation, which can be helpful for debugging.

To use the CLI, specify "CLI" as the property value for the run mode parameter of the HDL Verifier™ HDL simulator launch command.

The Tcl command you build to pass to the HDL simulator launch command must contain the run command or no cosimulation will take place.

Caution

Close the terminal window by entering quit -f at the command prompt. Do not close the terminal window by clicking the "X" in the upper right-hand corner. This causes a memory-type error to be issued from the system. This is not a bug with HDL Verifier but just the way the HDL simulator behaves in this context.

You can type CTRL+C to interrupt and terminate the simulation in the HDL simulator but this action also causes the memory-type error to be displayed.

 Specify CLI mode with nclaunch (Cadence Xcelium)

 Specify CLI mode with vsim (Mentor Graphics ModelSim)

Cosimulation with MATLAB Using Batch Mode

Running your cosimulation session in batch mode allows you to keep the process in the background, reducing demand on memory by disengaging the GUI.

To use the batch mode, specify "Batch" as the property value for the run mode parameter of the HDL Verifier HDL simulator launch command. After you issue the HDL Verifier HDL simulator launch command with batch mode specified, start the simulation in Simulink®. To stop the HDL simulator before the simulation is completed, issue the breakHdlSim command.

 Specify Batch mode with nclaunch (Cadence Xcelium)

 Specify Batch mode with vsim (Mentor Graphics ModelSim)

Apply Stimuli to Cosimulation Session with force Command

After you establish a connection between the HDL simulator and MATLAB, you can then apply stimuli to the test bench or component cosimulation environment. One way of applying stimuli is through the iport parameter of the linked MATLAB function. This parameter forces signal values by deposit.

Other ways to apply stimuli include issuing force commands in the HDL simulator main window (for ModelSim, you can also use the Edit > Clock option in the ModelSim Signals window).

For example, consider the following sequence of force commands:

  • Xcelium™

    force osc_top.clk_enable 1 -after 0ns
    force osc_top.reset 0 -after 0ns 1 -after 40ns 0 -after 120ns
    force osc_top.clk 1 -after 0ns 0 -after 40ns -repeat 80ns
    
  • ModelSim

    VSIM n> force clk 0 0 ns, 1 5 ns -repeat 10 ns
    VSIM n> force clk_en 1 0
    VSIM n> force reset 0 0
    

These commands drive the following signals:

  • The clk signal to 0 at 0 nanoseconds after the current simulation time and to 1 at 5 nanoseconds after the current HDL simulation time. This cycle repeats starting at 10 nanoseconds after the current simulation time, causing transitions from 1 to 0 and 0 to 1 every 5 nanoseconds, as the following diagram shows.

    For example,

    force /foobar/clk   0 0, 1 5 -repeat 10
  • The clk_en signal to 1 at 0 nanoseconds after the current simulation time.

  • The reset signal to 0 at 0 nanoseconds after the current simulation time.

Xcelium Users: Using HDL to Code Clock Signals Instead of the force Command

You should consider using HDL to code clock signals as force is a lower performance solution in the current version of Cadence® Xcelium simulators.

The following are ways that a periodic force might be introduced:

  • Via the Clock pane in the HDL Cosimulation block

  • Via pre/post Tcl commands in the HDL Cosimulation block

  • Via a user-input Tcl script to xmsim

All three approaches may lead to performance degradation.

Restart Simulation

Because the HDL simulator issues the service requests during a MATLAB cosimulation session, you must restart the session from the HDL simulator. To restart a session, perform the following steps:

  1. Make the HDL simulator your active window, if your input focus was not already set to that application.

  2. Reload HDL design elements and reset the simulation time to zero.

  3. Reissue the matlabtb or matlabcp command.

Note

To restart a simulation that is in progress, issue a break command and end the current simulation session before restarting a new session.

Stop Simulation

When you are ready to stop a test bench or component session, it is best to do so in an orderly way to avoid possible corruption of files and to see that all application tasks shut down cleanly. You should stop a session as follows:

  1. Make the HDL simulator your active window, if your input focus was not already set to that application.

  2. Halt the simulation. You must quit the simulation at the HDL simulator side or MATLAB may hang until the simulator is quit.

  3. Close your project.

  4. Exit the HDL simulator, if you are finished with the application.

  5. Quit MATLAB, if you are finished with the application. If you want to shut down the server manually, stop the server by calling hdldaemon with the 'kill' option:

    hdldaemon('kill')

For more information on closing HDL simulator sessions, see the HDL simulator documentation.