Main Content

Import HDL Code for MATLAB System Object

Cosimulation Type—MATLAB System Object

If you have not yet done so, invoke the Cosimulation Wizard.

cosimWizard

  1. In the Cosimulation Type pane, select MATLAB System object in the field HDL cosimulation with.

  2. Select ModelSim or Incisive for the HDL Simulator.

  3. Select Use HDL simulator executables on the system path if that is where the files are located. The Cosimulation Wizard assumes by default that they are on the system path.

    If the HDL simulator executables are not on the system path, select Use the following HDL simulator executables at the following location and specify the folder location in the text box below.

    If you click Next and the Cosimulation Wizard does not find the executables, the following occurs:

    • You are returned to this dialog and the Cosimulation Wizard displays an error in the status pane.

    • The Cosimulation Wizard switches the option to Use the following HDL simulator executables at the following location.

    • The Cosimulation Wizard makes the HDL simulation path field editable.

    You must enter a valid path to the HDL simulator executables before you are allowed to continue.

  4. Click Next.

HDL Files—MATLAB System Object

In the HDL Files pane, specify the files to be used in creating the function or block.

  • The Cosimulation Wizard attempts to determine the file type of each file and display the type in the File List next to the file name. If the Cosimulation Wizard cannot determine the type or displays the wrong type, you can change the type directly in the File Type column.

  • If possible, the Cosimulation Wizard will determine the compilation order automatically using HDL simulator provided functionality. If your simulator does not include this functionality, add the files in the order they should be compiled.

  • If you are using ModelSim®, you will see compilation scripts listed as .do files (ModelSim macro file). If you are using Incisive®, you will see compilation scripts listed as system scripts.

  1. Click Add to select one or more file names.

  2. Remove files by first highlighting the file name in the File List, then clicking Remove Selected File.

  3. Click Next.

HDL Compilation—MATLAB System Object

In the HDL Compilation pane, you can review the generated HDL compilation commands. You may override and/or customize those commands, if you wish. If you included compilation scripts instead of HDL files, this pane will show you the command to run those scripts.

  1. Enter any changes to the commands in the Compilation Commands box.

    Note

    Do not include system shell commands; for example:

    set file = a.vhd vcom $file

    When control returns to the Cosimulation Wizard from executing the command, the variable no longer holds the value that was set. If you do try to include this type of command, you will see an error in the Status panel.

  2. Click Restore default commands to go back to the generated HDL compilation commands. You are asked to confirm that you want to discard any changes.

  3. Click Next to proceed.

Simulation Options—MATLAB System Object

In the HDL Module pane, provide the name of the HDL module to be used in cosimulation.

  1. Enter the name of the module at Name of HDL module to cosimulate with.

  2. Specify additional simulation options at Simulation options. For example, in the previous image, the options shown are:

    • HDL simulator resolution

    • Turn off optimizations that remove signals from the simulation view

    Click Restore Defaults to change the options back to the default.

  3. For Connection method, select Shared Memory if your firewall policy does not allow TCP/IP socket communication.

  4. Click Next to proceed to the next step. At this time in the process, the application performs the following actions in a command window:

    • Starts the HDL simulator.

    • Loads the HDL module in the HDL simulator.

    • Starts the HDL server, and waits to receive notice that the server has started.

    • Connects with the HDL server to get the port information.

    • Disconnects and shuts down the HDL server.

Input/Output Ports—MATLAB System Object

  1. In the Input/Output Ports pane, specify the type of each input and output port (Input, Clock, Reset, or Unused).

    • The Cosimulation Wizard attempts to determine the port types for you, but you may override any setting.

    • MATLAB® forces clock and reset signals in the HDL simulator through Tcl commands. You can specify clock and reset signal timing in a later step (see Clock/Reset Details—MATLAB System Object).

  2. Click Next.

Output Port Details—MATLAB System Object

  1. In the Output Port Details pane, set the sample time and data type for all output ports.

    • Sample time default is 1, the data type default is Inherit and Signed. These defaults are consistent with the way the HDL Cosimulation block mask (Ports tab) sets default settings for output ports (Simulink® workflow).

    • If you select Set all sample times and data types to 'Inherit', the ports inherit the times via back propagation (sample times are set to -1). However, back propagation may fail in some circumstances; see Backpropagation in Sample Times (Simulink).

  2. Click Next.

Clock/Reset Details—MATLAB System Object

  1. In the Clock/Reset Details pane, set the clock and reset parameters.

    • The time period specified here refers to time in the HDL simulator.

    • The clock default settings are a rising active edge and a period of 10 ns.

    • The reset default settings are an initial value of 0 and a duration of 15 ns.

    The next screen provides a visual display of the simulation start time where you can review how the clocks and resets line up.

  2. Click Next.

Start Time Alignment—MATLAB System Object

  1. In the Start Time Alignment pane, review the current settings for clocks and resets. The purpose for this dialog is twofold:

    • To make sure the rising or falling edge is set as expected (from the previous step)

      • Examine the start time. If it coincides with the active edge of the clock, you need to adjust the HDL simulator start time.

      • Examine the reset signal. If it is synchronous with the clock active edge, you may have a possible race condition.

        To avoid a race condition, make sure the start time does not coincide with the active edge of any clocks. You can do this by moving the start time or by changing clock active edges in the previous step.

    • To make sure the start time is where you want it.

    The HDL simulator start time is calculated from the clock and reset values on the previous pane. If you want, you can change the HDL simulator start time by entering a new value where you see HDL time to start cosimulation (ns). Click Update plot to see your change applied.

  2. Click Next.

System Object Generation

  1. You can modify the HDL simulator sampling period before the wizard generates the System object™. Enter the new value in the box labeled HDL Simulator sampling period (ns).

    The sampling period determines the elapsed time in the HDL Simulator separating each call to step in MATLAB. Most of the time the sampling period is equal to the clock period.

  2. If your inputs and outputs are frame based (instead of sample based), select Frame based processing.

  3. Click Finish.

After you click Finish, the wizard generates the following HDL files in the current directory:

  • compile_hdl_design_design_name.m — Script for recompiling the HDL design

  • launch_hdl_simulator_design_name.m — Script for relaunching the MATLAB System object server and starting the HDL simulator

  • hdlcosim_design_name.m — Script for creating the HDLCosimulation System object

Write System Object Test Bench

Write the test bench for use with the newly generated HDL cosimulation System object. The test bench you write might look similar to the example shown next.

For the files used in this example, see Cosimulation Wizard for MATLAB System Object.

Run Cosimulation and Verify HDL Design

  1. Launch the HDL simulator by executing the launch script created by the wizard (launch_hdl_simulator_design_name.m)

  2. When the HDL simulator is ready, return to MATLAB and start the simulation by executing the test bench.

  3. Verify the results.