Main Content

Import HDL Code for MATLAB Function

Cosimulation Type—MATLAB Function

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


  1. In the Cosimulation Type pane, select MATLAB in the field HDL cosimulation with to create a MATLAB® function template (test bench or component).

  2. Select ModelSim or Xcelium for the HDL Simulator. This workflow is not supported for Vivado® 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 Function

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. This means you can add the files in any order.

  • If you are using ModelSim®, you will see compilation scripts listed as .do files (ModelSim macro file). If you are using Xcelium™, 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 Function

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.


    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.

HDL Modules—MATLAB Function

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.

Callback Schedule—MATLAB Function

  1. In the Callback Schedule pane, enter multiple component or test bench function callbacks from the HDL simulator. Enter the following information for each callback function:

    • Callback type: select matlabcp to create a component function or matlabtb to create a test bench function.

    • Callback function name (optional): Specify the name of component or test bench function, if it is not the same as the HDL component. The default assumption is that the function name is the same as the HDL component name.

    • HDL component: Enter component name manually or browse for it by clicking Browse.

    • Trigger mode: Specify one of the following to trigger the callback function:

      • Repeat

      • Rising Edge

      • Falling Edge

      • Sensitivity

    • Sample time (ns) or Trigger Signal:

      • If you selected trigger Repeat, enter the sample time in nanoseconds.

      • If you selected Rising Edge, Falling Edge, or Sensitivity, Sample time (ns) changes to Trigger Signal. Enter the signal name to be used to trigger the callback.

      You can browse the existing signals in the HDL component you specified by clicking Browse.

  2. Click Add to add the command to the MATLAB Callback Functions list.

    If you have more callback functions you want to schedule, repeat the above steps. If you want to remove any callback functions, highlight the line you want to remove and click Remove.


    If you attempt to add a callback function for the same HDL module as an existing callback function in the MATLAB Callback Functions list, the new callback function will overwrite the existing one (this is true even if you change the callback type). You will see a warning in the Status window:

    Warning: This HDL component already has a scheduled callback function, which is
    replaced by this new one.
  3. Click Next.

Script Generation—MATLAB Function

  1. Click Back to review or change your settings.

  2. Click Finish to generate scripts.

Generated Files—MATLAB Function

The Cosimulation Wizard creates the following files and opens each one in a separate MATLAB Editor windows.

  • launchHDLsimulator — script for launching the HDL simulator for cosimulation with MATLAB.

  • compileHDLDesign — compilation script you can reuse for subsequent compilation of this particular component.

  • Function files (*.m) — component and test bench customized function templates, one for each component specified in the Cosimulation Wizard.

Complete the Component or Test Bench Function

The template that the wizard generates contains some simple port I/O instructions and empty routines where you add your own code, as shown in the example below. For a full example of creating and using a MATLAB function, see Verify HDL Module with MATLAB Component Function Using Cosimulation Wizard.

function osc_top_u_osc_filter1x(obj)
% Automatically generated MATLAB(R) callback function.

% Copyright 2010 The MathWorks, Inc.
% $Revision $

% Initialize state of callback function.
if (strcmp(obj.simstatus,'Init'))
    disp('Initializing states ...');

    % Store port information in userdata
    % The name strings of ports that sends data from HDL simulator to
    % MATLAB callback function
    obj.userdata.FromHdlPortNames = fields(obj.portinfo.out);
    obj.userdata.FromHdlPortNum   = length(fields(obj.portinfo.out));

    % The name strings of ports that sends data from MATLAB callback
    % function to HDL simulator
    obj.userdata.ToHdlPortNames   = fields(;
    obj.userdata.ToHdlPortNum     = length(fields(;

    % Initialize state
    obj.userdata.State = 0;

% Obj.tnow is the current HDL simulation time specified in seconds
disp(['Callback function is executed at time ' num2str(obj.tnow)]);

if(obj.userdata.FromHdlPortNum > 0)
    % The name of the first input port
    portName  = obj.userdata.FromHdlPortNames{1};
    disp(['Reading input port ' portName]);
    % Convert the multi-valued logic value of the first port to decimal
    portValueDec  = mvl2dec( ...
        obj.portvalues.(portName), ...      % Multi-valued logic of the first port
        obj.portinfo.out.(portName).size);  %#ok<NASGU> % Bit width
    % Then perform any necessary operations on this value passed by HDL simulator.
    % ...
    % Optionally, you can translate the port value into fixed point object,
    % e.g.
    % myfiobj = fi(portValueDec,1, 16, 4);

% Update your state(s). In the following example, we use this internal
% state to implement a one-bit counter
obj.userdata.State = ~obj.userdata.State;

if(obj.userdata.ToHdlPortNum > 0)
    % The name of the first output port in HDL
    portName = obj.userdata.ToHdlPortNames{1};
    disp(['Writing output port ' portName]);

    % Assign the first port value to internal state obj.userdata.State.
    % Before assignment, convert decimal value to multi-valued logic.
    % You can change obj.userdata.State to another other valid decimal values.
    obj.portvalues.(portName) = dec2mvl(...
        obj.userdata.State, ...;

    % Operate on other out ports, if there are any.
    % ...

See Also

Related Topics