Main Content

Simulate and Verify Generated C Code

In this step of the tutorial, you verify that when executed, the generated code is numerically equivalent to the algorithm modeled in Simulink®. You use a test harness model to simulate ComponentDeploymentFcn in normal mode and in SIL mode, and then compare the simulations by using the Simulation Data Inspector.

To test generated code, you can run software-in-the-loop (SIL) and processor-in-the-loop (PIL) simulations. A SIL simulation compiles and runs the generated code on your development computer. A PIL simulation cross-compiles source code on your development computer. The PIL simulation then downloads and runs the object code on a target processor or an equivalent instruction set simulator. You can use SIL and PIL simulations to:

  • Verify whether simulation and code execution results are numerically equivalent.

  • Collect code coverage and execution-time metrics.

  • Meet IEC 61508, IEC 62304, ISO 26262, EN 50128, or DO-178 certification objectives.

This tutorial shows you how to use test harness ComponentDeploymentFcnHarness to verify whether simulation and code execution results for component model ComponentDeploymentFcn are equivalent. However, if you have a Simulink Test™ license, you can use the Test Manager to create a test harness from the component model. See Create or Import Test Harnesses and Select Properties (Simulink Test).

Inspect and Configure Test Harness Model

The model ComponentDeploymentFcnHarness references the model-under-test, ComponentDeploymentFcn, by using a Model block. The harness model generates test inputs for the referenced model. You can easily switch the Model block between the normal, SIL, and PIL simulation modes.

  1. Save copies of the models ComponentDeploymentFcn and ComponentDeploymentFcnHarness in your current working folder.

  2. Open the model ComponentDeploymentFcnHarness.

    openExample('ComponentDeploymentFcnHarness');

    Test harness model for component model ComponentDeploymentFcn.

  3. Open the model ComponentDeploymentFcn.

    openExample('ComponentDeploymentFcn');
  4. In the ComponentDeploymentFcnHarness model, right-click the Model block and select Subsystem & Model Reference > Refresh Selected Model Block.

  5. Configure both models for use with the Simulation Data Inspector by adjusting these model configuration parameter settings in the Data Import/Export pane of the Configuration Parameters dialog box:

    • Select States.

    • Set Format to Dataset.

    • Select Record logged workspace data in Simulation Data Inspector.

  6. Click OK. Then, save the models.

Simulate the Model in Normal Mode

Run the test harness model in normal mode and capture the results in the Simulation Data Inspector.

  1. In model ComponentDeploymentFcnHarness, right-click the Model block for ComponenDeploymentFcn. From the context menu, select Block Parameters.

  2. In the Block Parameters dialog box, verify that the parameter Simulation mode is set to Normal. Click OK.

  3. Simulate ComponentDeploymentFcnHarness.

  4. When the simulation is done, view the simulation results in the Simulation Data Inspector. If the Simulation Data Inspector is not already open, on the Simulation tab, click Data Inspector.

  5. For the most recent (current) run, in the Properties pane, double-click the Run field and then rename the run as ComponentDeploymentFcn: Normal mode.

  6. Change the Simulation Data Inspector visualization to sparklines. Click the Visualizations icon and click Sparklines. Then, in the Filter Signals pane, expand the States node and then select UnitDelay (10).

  7. In the Multidimensional Signal dialog box, select Convert to channels.

  8. Expand the UnitDelay (10) node.

  9. Select one or more of the UnitDelay signal lines. The Simulation Data Inspector displays the sparklines for the selected channels.

This figure shows sparklines for UnitDelay (1) and UnitDelay (2).

Simulate the Model in SIL Mode

The SIL simulation compiles and executes the generated code on your development computer. The Simulation Data Inspector logs results.

  1. In the ComponentDeploymentFcnHarness model window, right-click the ComponenDeploymentFcn Model block and select Block Parameters.

  2. In the Block Parameters dialog box, make these parameter changes:

    • Set Simulation mode to Software-in-the-loop (SIL).

    • Set Code interface to Top model.

    Click OK.

  3. For the component model ComponenDeploymentFcn, in the Configuration Parameters dialog box, make these model configuration parameter changes:

    • In the Code Generation pane, set Toolchain to Automatically locate an installed toolchain.

    • In the Code Generation > Optimization pane, clear Remove internal data zero initialization.

    Save the configuration changes.

  4. Simulate the ComponentDeploymentFcnHarness model. The Diagnostic Viewer panel opens and displays build and code generation status. If a code generation report window opens, minimize it for exploration later in this tutorial.

  5. When the simulation is complete, in the Simulation Data Inspector Properties pane, double-click the Run field and then rename the new run as ComponentDeploymentFcn: SIL mode.

  6. Change the Simulation Data Inspector visualization to sparklines. Click the Visualizations icon and click Sparklines. Then, in the Filter Signals pane, expand the States node and then select delay (10).

  7. In the Multidimensional Signal dialog box, select Convert to channels.

  8. Expand the delay (10) node.

  9. Select one or more of the UnitDelay signal lines. The Simulation Data Inspector displays the sparklines for the selected channels.

Compare Simulation Results

In the Simulation Data Inspector:

  1. Click the Compare tab.

  2. From the Baseline list, select ComponentDeploymentFcn: Normal mode.

  3. From the Compare To list, select ComponentDeploymentFcn: SIL mode.

  4. Click Compare.

Simulation Data Inspector comparison of normal mode and SIL mode results.

This Simulation Data Inspector shows that the normal mode and SIL mode results match. Comparing the results of normal mode simulation with SIL and PIL simulations can help you verify that the generated code performs as expected.

Alternatively, you can use the SIL/PIL Manager app for a simplified workflow of verifying code generated from a model.

Next, explore ways that you can deploy generated code.