* An example C-file S-function for multiplying an input by 2,
* Real-Time Workshop note:
* This file can be used as is (noninlined) with the Real-Time Workshop
* C rapid prototyping targets, or it can be inlined using the Target
* Language Compiler technology and used with any target. See
* matlabroot/toolbox/simulink/blocks/tlc_c/timestwo.tlc
* matlabroot/toolbox/simulink/blocks/tlc_ada/timestwo.tlc
* the C and Ada TLC code to inline the S-function.
* See simulink/src/sfuntmpl_doc.c
* Copyright 1990-2004 The MathWorks, Inc.
#define S_FUNCTION_NAME bftest
#define S_FUNCTION_LEVEL 2
typedef unsigned int UINT16;
/* Function: mdlInitializeSizes ===============================================
* Setup sizes of the various vectors.
static void mdlInitializeSizes(SimStruct *S)
static boolean_T *inPorts[16];
// check that input parameter i numerical vector
if (mxIsEmpty(ssGetSFcnParam(S, 0)) ||
mxIsSparse(ssGetSFcnParam(S, 0)) ||
mxIsComplex(ssGetSFcnParam(S, 0)) ||
mxIsLogical(ssGetSFcnParam(S, 0)) ||
!mxIsNumeric(ssGetSFcnParam(S, 0)) ||
!mxIsDouble(ssGetSFcnParam(S, 0)))
ssSetErrorStatus(S, "Parameters must be real finite vectors");
pr = mxGetPr(ssGetSFcnParam(S, 0));
nEls = mxGetNumberOfElements(ssGetSFcnParam(S, 0));
for (el = 0; el < nEls; el++)
ssSetErrorStatus(S, "Parameters must be real finite vectors");
ssSetNumSFcnParams(S, NPARAMS);
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S))
return; /* Parameter mismatch will be reported by Simulink */
ssSetSFcnParamNotTunable(S, 0);
if (!ssWriteRTWParamSettings(S, 1,
SSWRITE_VALUE_NUM, "inportVector", pr))
return; /* An error occurred. */
* Configure the input ports
// set specified number of input ports
if (!ssSetNumInputPorts(S, (int_T)nEls))
for (i = 0; i < nEls; i++)
ssSetInputPortDataType(S, i, SS_BOOLEAN);
ssSetInputPortWidth(S, i, 1);
ssSetInputPortComplexSignal(S, i, COMPLEX_NO);
ssSetInputPortDirectFeedThrough(S, i, 1);
ssSetInputPortAcceptExprInRTW(S, i, 1);
ssSetInputPortOverWritable(S, i, 1);
ssSetInputPortOptimOpts(S, i, SS_REUSABLE_AND_LOCAL);
ssSetInputPortRequiredContiguous(S, i, 1);
* Set the number of output ports.
if (!ssSetNumOutputPorts(S, 1))
* Configure the output port 1
ssSetOutputPortDataType(S, 0, SS_UINT16);
ssSetOutputPortWidth(S, 0, 1);
ssSetOutputPortComplexSignal(S, 0, COMPLEX_NO);
ssSetOutputPortOptimOpts(S, 0, SS_REUSABLE_AND_LOCAL);
ssSetOutputPortOutputExprInRTW(S, 0, 1);
// * Register reserved identifiers to avoid name conflict
// if (ssRTWGenIsCodeGen(S) || ssGetSimMode(S)==SS_SIMMODE_EXTERNAL) {
// * Register reserved identifier for OutputFcnSpec
// ssRegMdlInfo(S, "bftest", MDL_INFO_ID_RESERVED, 0, 0, ssGetPath(S));
* This S-function can be used in referenced model simulating in normal mode.
ssSetModelReferenceNormalModeSupport(S, MDL_START_AND_MDL_PROCESS_PARAMS_OK);
* Set the number of sample time.
ssSetNumSampleTimes(S, 1);
* All options have the form SS_OPTION_<name> and are documented in
* matlabroot/simulink/include/simstruc.h. The options should be
* bitwise or'd together as in
* ssSetOptions(S, (SS_OPTION_name1 | SS_OPTION_name2))
SS_OPTION_USE_TLC_WITH_ACCELERATOR |
SS_OPTION_CAN_BE_CALLED_CONDITIONALLY |
SS_OPTION_EXCEPTION_FREE_CODE |
SS_OPTION_WORKS_WITH_CODE_REUSE |
SS_OPTION_DISALLOW_CONSTANT_SAMPLE_TIME);
ssSupportsMultipleExecInstances(S, true);
/* Function: mdlInitializeSampleTimes =========================================
* Specifiy that we inherit our sample time from the driving block.
static void mdlInitializeSampleTimes(SimStruct *S)
ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S, 0, FIXED_IN_MINOR_STEP_OFFSET);
#if defined(ssSetModelReferenceSampleTimeDefaultInheritance)
ssSetModelReferenceSampleTimeDefaultInheritance(S);
/* Function: mdlOutputs ===================================================
* In this function, you compute the outputs of your S-function
* block. Generally outputs are placed in the output vector(s),
static void mdlOutputs(SimStruct *S, int_T tid)
// get number and index of input bits
pr = mxGetPr(ssGetSFcnParam(S, 0));
nEls = mxGetNumberOfElements(ssGetSFcnParam(S, 0));
boolean_T BF[16] = {false};
for (i = 0; i < nEls; i++)
boolean_T *temp = (boolean_T *)ssGetInputPortSignal(S, i);
BF[(int_T)pr[i]] = *temp;
uint16_T *y1 = (uint16_T *)ssGetOutputPortSignal(S, 0);
*y1 = ((UINT16)BF[0]) | ((UINT16)BF[1] << 1U) | ((UINT16)BF[2] << 2U) | ((UINT16)BF[3] << 3U) | ((UINT16)BF[4] << 4U) | ((UINT16)BF[5] << 5U) | ((UINT16)BF[6] << 6U) | ((UINT16)BF[7] << 7U) | ((UINT16)BF[8] << 8U) | ((UINT16)BF[9] << 9U) | ((UINT16)BF[10] << 10U) | ((UINT16)BF[11] << 11U) | ((UINT16)BF[12] << 12U) | ((UINT16)BF[13] << 13U) | ((UINT16)BF[14] << 14U) | ((UINT16)BF[15] << 15U);
/* Function: mdlTerminate =====================================================
* No termination needed, but we are required to have this routine.
static void mdlTerminate(SimStruct *S)
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#include "cg_sfun.h" /* Code generation registration function */
def = legacy_code('initialize');
def.SFunctionName = 'bftest';
def.OutputFcnSpec = 'uint16 y1 = bftest(boolean u1, boolean u2, boolean u3, boolean u4, boolean u5, boolean u6, boolean u7, boolean u8, boolean u9, boolean u10, boolean u11, boolean u12, boolean u13, boolean u14, boolean u15, boolean u16)';
legacy_code('sfcn_tlc_generate', def);
extern void bftest_wrapper_Output( uint16_T* y1, const boolean_T u1, const boolean_T u2, const boolean_T u3, const boolean_T u4, const boolean_T u5, const boolean_T u6, const boolean_T u7, const boolean_T u8, const boolean_T u9, const boolean_T u10, const boolean_T u11, const boolean_T u12, const boolean_T u13, const boolean_T u14, const boolean_T u15, const boolean_T u16);
void bftest_wrapper_Output( uint16_T* y1, const boolean_T u1, const boolean_T u2, const boolean_T u3, const boolean_T u4, const boolean_T u5, const boolean_T u6, const boolean_T u7, const boolean_T u8, const boolean_T u9, const boolean_T u10, const boolean_T u11, const boolean_T u12, const boolean_T u13, const boolean_T u14, const boolean_T u15, const boolean_T u16) {
*y1 = bftest((boolean_T)(u1), (boolean_T)(u2), (boolean_T)(u3), (boolean_T)(u4), (boolean_T)(u5), (boolean_T)(u6), (boolean_T)(u7), (boolean_T)(u8), (boolean_T)(u9), (boolean_T)(u10), (boolean_T)(u11), (boolean_T)(u12), (boolean_T)(u13), (boolean_T)(u14), (boolean_T)(u15), (boolean_T)(u16));