Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 36 ยท Code Generation

SIL, PIL and Hardware Deployment for Simulink Models

Verify generated code with software-in-the-loop and processor-in-the-loop, compare against simulation, use support packages and real-time targets, and plan a deployment workflow.

Generated code is only trustworthy if it behaves like the model. Back-to-back testing runs the same inputs through the simulation and through the generated code and compares the outputs. Simulink calls the two main variants SIL (software-in-the-loop) and PIL (processor-in-the-loop). After that you deploy to hardware, either as a standalone program or with a real-time system. This lesson shows the steps and the choices. The tools (Embedded Coder, hardware support packages, Simulink Real-Time) do not run on this site, so the examples here are labelled stand-ins that demonstrate the comparison logic.

The verification ladder

flowchart LR
    M["MIL<br/>model in the loop<br/>(normal simulation)"] --> S["SIL<br/>generated code compiled<br/>for the host PC"]
    S --> P["PIL<br/>generated code runs on<br/>the target processor"]
    P --> H["HIL / deployment<br/>real hardware and I/O"]

Each step changes one thing, so a difference points to the cause.

  • MIL (model-in-the-loop): the model runs in Simulink normal mode. This is the reference.
  • SIL: the generated code is compiled for your computer and called from Simulink as if it were a block. It checks the code logic, data types and algebra (for example fixed-point overflow, integer division), not the target's timing.
  • PIL: the code is cross-compiled, downloaded to the real microcontroller (or an instruction-set simulator) and Simulink exchanges data with it over a connection (serial, JTAG, Ethernet). It checks that the compiler, word lengths and processor produce the same results.
  • HIL (hardware-in-the-loop): the real controller runs the code in real time while a real-time computer simulates the plant. This tests timing and I/O. Tools: Simulink Real-Time with Speedgoat hardware, or other vendors.

Running SIL and PIL

Requires Embedded Coder (and, for PIL, a hardware support package or a custom connectivity configuration).

Method 1: Model block. Put the controller in its own model, reference it with a Model block in a test model, then in the block's Block Parameters > Simulation mode choose Software-in-the-loop (SIL) or Processor-in-the-loop (PIL). Press Run: Simulink generates the code (with ert.tlc), builds it and runs your test inputs through it.

Method 2: Top-model verification. In the Apps > Embedded Coder tab, choose Verify Code > Software-in-the-Loop (SIL) or PIL for the top model. The app builds a SIL/PIL block and a test harness.

Method 3: Command line.

MATLAB
set_param('controller', 'SimulationMode', 'software-in-the-loop');   % or 'processor-in-the-loop'
simOut = sim('controller');

Check settings before building: Hardware Implementation (device and word lengths must match the real target), Code Generation > Interface and Verification > Enable portable word sizes (generates code that compiles on the host for SIL with the target's sizes).

Comparing results

In Simulink Simulation Data Inspector (Analysis > Simulation Data Inspector, SDI), log the outputs of the MIL run and of the SIL run and use Compare Runs with a tolerance (absolute and relative; and optionally a time tolerance). Differences in floating-point code are normal at the 1e-6 level for single precision, but integer or fixed-point code should match exactly.

The stand-in below does the same comparison on two simulations, one in double precision and one emulating a 16-bit fixed-point implementation, and flags the samples that exceed a tolerance.

Python Stand-in: back-to-back comparison with absolute and relative tolerance

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The fixed-point version with an 8-bit fraction deviates from the double-precision reference by up to about 0.02, because the coefficients 0.9 and 0.1 are quantized to multiples of 1/256 and the product is cut with a floor shift. With a tolerance this tight the comparison flags many samples; the cures are more fraction bits, a different rounding mode or a wider accumulator, or (if the requirements allow it) a justified wider tolerance. This is the same story as in the fixed-point lesson.

Code coverage on SIL/PIL

With Simulink Coverage you can measure model coverage in MIL and code coverage in SIL/PIL (statement, decision, condition, MC/DC on the generated C). If the code has branches that the model tests never reach (defensive code, saturation branches, error handling), the coverage report shows them. See model coverage.

Deploying to hardware

Options, from easiest to most production-oriented:

  1. Support packages for popular boards: Simulink Support Package for Arduino, Raspberry Pi, Embedded Coder Support Package for ARM Cortex-M (STM32 Nucleo and Discovery, BeagleBone, ...), TI C2000, Xilinx Zynq and others. Install through Home > Add-Ons > Get Hardware Support Packages, choose the board in Model Settings > Hardware Implementation > Hardware board, add I/O blocks (ADC, PWM, GPIO, serial) from the package library and press Deploy to Hardware. External mode lets you tune parameters and view signals while the code runs on the board.
  2. Integrate generated code in your own project: build with Generate code only and add the files to an IDE project (Keil, IAR, Code Composer Studio, STM32CubeIDE, GCC makefile). You write main, drivers and the scheduler. Use Pack and Go (packNGo) to collect all files and dependencies in a zip.
  3. Real-time prototyping: Simulink Real-Time with Speedgoat targets for HIL and rapid control prototyping.
  4. Automotive and aerospace: AUTOSAR (Classic and Adaptive) code and ARXML export, DO-178C and ISO 26262 workflows with IEC Certification Kit. Not needed for most projects, but it explains why teams pay for the product.

Integrating hand-written code

You rarely write everything in blocks. Ways to connect with legacy or hand-written C:

  • C Caller and C Function blocks (call a C function in the model and in the generated code).
  • Legacy Code Tool or S-Function Builder for older workflows.
  • Custom storage classes or Code Mappings so signals become global variables or macros with your naming (important for calibration and linker maps).
  • Code replacement libraries swap math functions (e.g. sin) for optimized target versions.

Practical checklist before deployment

  1. The model passes MIL tests with documented requirements (testing chapter).
  2. Hardware Implementation matches the target compiler.
  3. SIL back-to-back matches within tolerance; PIL confirmed on the real chip.
  4. Static code analysis (MISRA C, Polyspace) has no critical findings.
  5. Execution time measured on the target is lower than the sample time with margin (for example, utilization under 70 percent). Embedded Coder's profiling and PIL execution profiling help.
  6. Stack and RAM/ROM size fits; compile with the final optimization level.
  7. The version of the model, the code and the compiler is recorded (traceability).

Common mistakes

  • Skipping PIL and discovering later that int or alignment differs.
  • Comparing floating-point outputs with exact equality.
  • Tolerances so wide that real bugs hide.
  • Measuring timing in SIL. The PC's speed says nothing about the microcontroller; use PIL profiling or hardware timers.
  • Leaving the example ert_main.c in place and forgetting that no scheduler checks for overruns.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does SIL primarily verify?

    SIL runs generated code on the host; it checks logic, data types and algebra, not target timing.
  2. 2. What does PIL add over SIL?

    PIL cross-compiles and runs on the processor, so word sizes and compiler behavior are tested.
  3. 3. Which tool compares the logged outputs of two runs with tolerances?

    The Simulation Data Inspector's Compare Runs feature supports absolute, relative and time tolerances.
  4. 4. Why should the Hardware Implementation settings match the real target?

    Word lengths, endianness and rounding behavior of the target determine the generated types and results.

Summary

  • Verify generated code step by step: MIL, SIL, PIL, then HIL or deployment.
  • Switch a Model block (or the top model) to SIL or PIL simulation mode and compare outputs in the Simulation Data Inspector.
  • Integer and fixed-point code should match exactly; floating-point comparisons use tolerances.
  • Deploy with hardware support packages, your own IDE project (Pack and Go), or real-time targets; connect existing C code with C Caller and code mappings.
  • Check execution time, memory, MISRA findings and traceability before release.

Next: Test harnesses and the Test Manager