Target Language Compiler    

Basic Inlined S-Function Written in TLC

Objective: To understand the differences between a noninlined S-function and an inlined S-function. This will help you develop a better understanding of when to use an inlined or noninlined S-function.

Example directory: tlctutorial/timestwo

Basic Code Generation

  1. Copy the Simulink S-function tlctutorial/timestwo/timestwo.c into your working directory.
  2. Create the MEX-file.

    This is needed to avoid picking up the version shipped with Simulink.

  1. Create the model sfun_x2 using the Simulink S-function called timestwo. Your model should look like this.
  2. Simulate the model using the fixed-step discrete solver with a step size of 0.01 and stop time of 10.0.

    Note that timestwo.c code is a level-2 S-function. Level-2 S-functions are beyond the scope of this training session. See the Simulink book Writing S-Functions for more information.

    Using C coded S-functions with generated code tend to add unnecessary overhead. For simple S-functions where speed is crucial -- in cases such as
    I/O device drivers -- you can rewrite the S-function as an inlined S-function by creating a file with the same S-function name with a .tlc extension (filename.tlc).

  1. To look at the generated code for the noninlined C coded S-function, select Generate code only from the Real-Time Workshop page in the Simulation Parameters dialog and click on the Generate code button to generate C code for the model. Now view the MdlOutputs and MdlTerminate portions of the generated C code, sfun_x2.c. Notice the overhead of calling the S-function. This necessary code allows for a generic API for S-functions. The next step is to inline your S-function by removing the SimStruct associated with your S-function (memory savings) and the generic API (speed up).

 TLC Tutorial Creating an Inlined S-Function