Target Language Compiler | ![]() ![]() |
Fully Inlined S-Function Example
Inlining an S-function provides a mechanism to directly embed code for an S-function block into the generated code for a model. Instead of calling into a separate source file via function pointers and maintaining a separate data structure (SimStruct
) for it, the code appears "inlined" as the diagram below shows.
The S-function timestwo.c
provides a simple example of a fully inlined S-function. This block multiplies its input by 2 and outputs it. The C-MEX version of the block is in matlabroot
/simulink/src/timestwo.c
and the inlining TLC file for the block is in matlabroot
/toolbox/simulink/blocks/tlc_c/timestwo.tlc
.
timestwo.tlc
%implements "timestwo" "C" %% Function: Outputs ========================================== %% %function Outputs(block, system) Output /* %<Type> Block: %<Name> */ %% /* Multiply input by two */ %assign rollVars = ["U", "Y"] %roll idx = RollRegions, lcv = RollThreshold, block, "Roller", rollVars %<LibBlockOutputSignal(0, "", lcv, idx)> = \ %<LibBlockInputSignal(0, "", lcv, idx)> * 2.0; %endroll %endfunction
TLC Block Analysis
The %implements
line is required by all TLC blocks file and is used by the Target Language Compiler to verify correct block type and correct language support by the block. The %function
directive starts a function declaration and shows the name of the function, Outputs
, and the arguments passed to it, block
and system
. These are the relevant records from the model
.rtw
file for this instance of the block.
The last piece to the prototype is Output
. This means that any line that is not a TLC directive is output by the function to the current file that is selected in TLC. So, any nondirective lines in the Outputs
function become generated code for the block.
The most complicated piece of this TLC block example is the %roll
directive. TLC uses this directive to provide for the automatic generation of for
loops depending on input/output widths and whether the inputs are contiguous in memory. This example uses the typical form of accessing outputs and inputs from within the body of the roll, using LibBlockOutputSignal
and LibBlockInputSignal
to access the outputs and inputs and perform the multiplication and assignment. Note that this TLC file supports any signal width.
The only function needed to implement this block is Outputs
. For more complicated blocks, other functions will be declared as well. You can find examples of more complicated inlining TLC files in matlabroot
/toolbox/simulink/blocks
and matlabroot
/toolbox/simulink/blocks/tlc_c
, and by looking at the code for built-in blocks in matlabroot
/rtw/c/tlc/blocks
.
timestwo Model
This simple model uses the timestwo
S-function and shows the MdlOutputs
function from the generated model.c
file, which contains the inlined S-function code.
MdlOutputs Code
void MdlOutputs(int_T tid) { /* S-Function Block: <Root>/S-Function */ /* Multiply input by two */ rtB.S_Function = (rtB.Constant_Value) * 2.0; /* Outport Block: <Root>/Out1 */ rtY.Out1 = rtB.S_Function; }
![]() | Inlining S-Function Concepts | Wrapper Inlined S-Function Example | ![]() |