Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
File: matlabroot/simulink/src/sfuntmpl_directfeed.txt
Copyright 1990-2003 The MathWorks, Inc.
$Revision: 1.7.4.2 $
It is very common for S-function authors to write incorrect S-functions when
configuring the S-function direct feedthrough flag. We often find that
S-function authors are confused about what the correct setting for the direct
feedthrough flag should be. Part of the confusion is because the term direct
feedthrough is misleading. To reduce the confusion, you can think of the direct
feedthrough setting as a 'needs input' setting. Specifically, if the S-function
access an input signal in either mdlOutputs() or mdlGetTimeOfNextVarHit(), then
the direct feedthrough flag must be set to 1 (true).
For example, if a level 2 C-MEX S-function uses:
ssGetInputPortSignal(S,inputPortIndex)
in its mdlOutputs() or mdlGetTimeOfNextHit() methods, then the S-function
is required to set the direct feedthrough flag to true in its
mdlInitializeSizes() method:
ssSetInputPortDirectFeedThrough(S, 0, 1); /* need direct feedthrough */
If the S-Function fails to specify the direct feedthrough, or
specifes it wrongly as:
ssSetInputPortDirectFeedThrough(S, 0, 0); /* no direct feedthrough */
/* (this is the default) */
then the S-Function is incorreclty written.
Simulink R12 and prior had a special S-function block sorting mode to try and
compensate for S-functions with incorrect direct feedthrough settings. Simulink
would first try sorting blocks for execution by assuming the S-function had
direct feedthrough when the S-function specified that it did not have direct
feedthough. If this resulted in an algebraic loop, a second sorting would be
done with the direct feedthrough flag in its original state (i.e., no direct
feedthrough). The problem with this scheme is that it can lead to incorrect
execution orders for cases involving S-functions within nonvirtual
subsystems. It can also result in slower update diagram (compile) times. In
addition to these problems, accessing an input signal when the corresponding
input port direct feedthough is not set will result in uninitialized memory
reads which can cause crashes.
In Simulink R12.1 we removed the 'special S-funciton block sorting mode' and
NULL'd out the input signals provided to S-functions during mdlOutput()
and mdlGetTimeOfNextVarHit() when the corresponding input port does
not have direct feedthrough. Note, for M-file/Fortran S-functions, Simulink
sets the input signal values to NaN for this condition. Therefore, in R12.1
and later C-MEX S-functions will cause a segmentation violation when
illegally accessing the input signal. On some of our supported platforms
we are able to detect this condition in our signal handler and will
report the following diagnostic message:
<segmentation violation, register state, and stack trace output>
This segmentation violation occurred while executing an S-function.