Target Language Compiler | ![]() ![]() |
Creating an Inlined S-Function
The Target Language Compiler creates an inlined S-function whenever it detects a .tlc
file with the same name as an S-function. Assuming the .tlc
file is properly formed, it directs construction of code that functionally duplicates the external S-function without incurring API overhead.
See how this process works by completing the following steps:
rename_timestwo.tlc
. Rename this file to be timestwo.tlc
, so that it will be used when generating code. The executable portion of the file is
%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
Now create the inline version of the S-function.
model: sfun_x2
to model: sfun_x2_ilp
.
sfun_x2_ilp.mdl
.
sfun_x2_ilp_grt_rtw
. Inspect the generated file sfun_x2_ilp.c
, paying attention to the highlighted lines corresponding to those above:
... /* Compute block outputs */ void MdlOutputs(int_T tid) { /* This is a single rate system */ (void)tid; /* Sin: '<Root>/Sin' */ rtB.Sin = 1.0 * sin(1.0 * ssGetT(rtS) + 0.0) + 0.0; /* S-Function Block: <Root>/S-Function */ /* Multiply input by two */rtB.timestwo_output = rtB.Sin * 2.0;
/* Outport: '<Root>/Out' */ rtY.Out = rtB.timestwo_output; } /* Perform model update */ void MdlUpdate(int_T tid) { /* (no update code required) */ } /* Terminate function */ void MdlTerminate(void) {/* (no terminate code required) */
}
Continue the exercise by creating a stand-alone simulation, as follows.
sfun_x2_ilp.exe
(or, on UNIX systems, sfun_x2_ilp
).
timestwo.tlc
file did the right thing by running the stand-alone executable. To run it, in the MATLAB command window, type:
View or plot the contents of the sfun_x2_ilp.mat
file to verify that the stand-alone model generated sine output ranging from -2 to +2. In the MATLAB command window, type
![]() | Why Use TLC to Implement S-functions? | Scalar or Vector, It's the Same | ![]() |