/*
 *   GFLIB_ControllerPIp_SF_F32.c Simple C-MEX S-function for function call.
 *
 *   ABSTRACT:
 *     The purpose of this sfunction is to call a simple legacy
 *     function during simulation:
 *
 *        void GFLIB_ControllerPIp_w_F32(int32 u1, int32 u2, int32 u3, int32 p1, int16 p2, int32 p3, int16 p4, int32 y1[1], int32 y2[1], int32 y3[1])
 *
 *   Simulink version           : 7.9 (R2012a) 29-Dec-2011
 *   C source code generated on : 15-Apr-2014 13:06:54

 * THIS S-FUNCTION IS GENERATED BY THE LEGACY CODE TOOL AND MAY NOT WORK IF MODIFIED

 */

/*
   %%%-MATLAB_Construction_Commands_Start
   def = legacy_code('initialize');
   def.SFunctionName = 'GFLIB_ControllerPIp_SF_F32';
   def.OutputFcnSpec = 'void GFLIB_ControllerPIp_w_F32(int32 u1, int32 u2, int32 u3, int32 p1, int16 p2, int32 p3, int16 p4, int32 y1[1], int32 y2[1], int32 y3[1])';
   def.HeaderFiles = {'GFLIB_ControllerPIp.h', 'GFLIB_ControllerPIp_w.h'};
   def.SourceFiles = {'GFLIB_ControllerPIp.c', 'GFLIB_ControllerPIp_w.c', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\GFLIB\GFLIB_Sqrt\GFLIB_Sqrt.c'};
   def.IncPaths = {'C:\AMMCLIB\MC\MC352\dev\libs\codebase\Common', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_Abs', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_AbsSat', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_Add', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_AddSat', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_Convert', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_ConvertPU', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_Div', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_DivSat', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_Mac', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_MacSat', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_Mul', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_MulSat', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_Neg', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_NegSat', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_Norm', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_Round', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_ShBi', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_ShBiSat', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_ShL', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_ShLSat', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_ShR', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_Sub', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_SubSat', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\MLIB\MLIB_VMac', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\GFLIB\GFLIB_Sqrt', 'C:\AMMCLIB\MC\MC352\dev\libs\codebase\GFLIB\GFLIB_ControllerPIp'};
   def.SrcPaths = {'C:\AMMCLIB\MC\MC352\dev\libs\codebase\GFLIB\GFLIB_ControllerPIp'};
   legacy_code('sfcn_cmex_generate', def);
   legacy_code('compile', def);
   %%%-MATLAB_Construction_Commands_End
 */

/*
 * Must specify the S_FUNCTION_NAME as the name of the S-function.
 */
#define S_FUNCTION_NAME                GFLIB_ControllerPIp_SF_F32
#define S_FUNCTION_LEVEL               2

/*
 * Need to include simstruc.h for the definition of the SimStruct and
 * its associated macro definitions.
 */
#include "simstruc.h"

/*
 * Specific header file(s) required by the legacy code function.
 */
#include "GFLIB_ControllerPIp.h"
#include "GFLIB_ControllerPIp_w.h"
#define EDIT_OK(S, P_IDX) \
 (!((ssGetSimMode(S)==SS_SIMMODE_SIZES_CALL_ONLY) && mxIsEmpty(ssGetSFcnParam(S, P_IDX))))
#define MDL_CHECK_PARAMETERS
#if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)

/* Function: mdlCheckParameters ===========================================
 * Abstract:
 *    mdlCheckParameters verifies new parameter settings whenever parameter
 *    change or are re-evaluated during a simulation. When a simulation is
 *    running, changes to S-function parameters can occur at any time during
 *    the simulation loop.
 */
static void mdlCheckParameters(SimStruct *S)
{
  /*
   * Check the parameter 1
   */
  if EDIT_OK(S, 0) {
    int_T dimsArray[2] = { 1, 1 };

    /* Check the parameter attributes */
    ssCheckSFcnParamValueAttribs(S, 0, "P1", DYNAMICALLY_TYPED, 2, dimsArray, 0);
  }

  /*
   * Check the parameter 2
   */
  if EDIT_OK(S, 1) {
    int_T dimsArray[2] = { 1, 1 };

    /* Check the parameter attributes */
    ssCheckSFcnParamValueAttribs(S, 1, "P2", DYNAMICALLY_TYPED, 2, dimsArray, 0);
  }

  /*
   * Check the parameter 3
   */
  if EDIT_OK(S, 2) {
    int_T dimsArray[2] = { 1, 1 };

    /* Check the parameter attributes */
    ssCheckSFcnParamValueAttribs(S, 2, "P3", DYNAMICALLY_TYPED, 2, dimsArray, 0);
  }

  /*
   * Check the parameter 4
   */
  if EDIT_OK(S, 3) {
    int_T dimsArray[2] = { 1, 1 };

    /* Check the parameter attributes */
    ssCheckSFcnParamValueAttribs(S, 3, "P4", DYNAMICALLY_TYPED, 2, dimsArray, 0);
  }
}

#endif

/* Function: mdlInitializeSizes ===========================================
 * Abstract:
 *    The sizes information is used by Simulink to determine the S-function
 *    block's characteristics (number of inputs, outputs, states, etc.).
 */
static void mdlInitializeSizes(SimStruct *S)
{
  /* Number of expected parameters */
  ssSetNumSFcnParams(S, 4);

#if defined(MATLAB_MEX_FILE)

  if (ssGetNumSFcnParams(S) == ssGetSFcnParamsCount(S)) {
    /*
     * If the number of expected input parameters is not equal
     * to the number of parameters entered in the dialog box return.
     * Simulink will generate an error indicating that there is a
     * parameter mismatch.
     */
    mdlCheckParameters(S);
    if (ssGetErrorStatus(S) != NULL) {
      return;
    }
  } else {
    /* Return if number of expected != number of actual parameters */
    return;
  }

#endif

  /* Set the parameter's tunability */
  ssSetSFcnParamTunable(S, 0, 1);
  ssSetSFcnParamTunable(S, 1, 1);
  ssSetSFcnParamTunable(S, 2, 1);
  ssSetSFcnParamTunable(S, 3, 1);

  /*
   * Set the number of pworks.
   */
  ssSetNumPWork(S, 0);

  /*
   * Set the number of dworks.
   */
  if (!ssSetNumDWork(S, 0))
    return;

  /*
   * Set the number of input ports.
   */
  if (!ssSetNumInputPorts(S, 3))
    return;

  /*
   * Configure the input port 1
   */
  ssSetInputPortDataType(S, 0, SS_INT32);
  ssSetInputPortWidth(S, 0, 1);
  ssSetInputPortComplexSignal(S, 0, COMPLEX_NO);
  ssSetInputPortDirectFeedThrough(S, 0, 1);
  ssSetInputPortAcceptExprInRTW(S, 0, 1);
  ssSetInputPortOverWritable(S, 0, 1);
  ssSetInputPortOptimOpts(S, 0, SS_REUSABLE_AND_LOCAL);
  ssSetInputPortRequiredContiguous(S, 0, 1);

  /*
   * Configure the input port 2
   */
  ssSetInputPortDataType(S, 1, SS_INT32);
  ssSetInputPortWidth(S, 1, 1);
  ssSetInputPortComplexSignal(S, 1, COMPLEX_NO);
  ssSetInputPortDirectFeedThrough(S, 1, 1);
  ssSetInputPortAcceptExprInRTW(S, 1, 1);
  ssSetInputPortOverWritable(S, 1, 1);
  ssSetInputPortOptimOpts(S, 1, SS_REUSABLE_AND_LOCAL);
  ssSetInputPortRequiredContiguous(S, 1, 1);

  /*
   * Configure the input port 3
   */
  ssSetInputPortDataType(S, 2, SS_INT32);
  ssSetInputPortWidth(S, 2, 1);
  ssSetInputPortComplexSignal(S, 2, COMPLEX_NO);
  ssSetInputPortDirectFeedThrough(S, 2, 1);
  ssSetInputPortAcceptExprInRTW(S, 2, 1);
  ssSetInputPortOverWritable(S, 2, 1);
  ssSetInputPortOptimOpts(S, 2, SS_REUSABLE_AND_LOCAL);
  ssSetInputPortRequiredContiguous(S, 2, 1);

  /*
   * Set the number of output ports.
   */
  if (!ssSetNumOutputPorts(S, 3))
    return;

  /*
   * Configure the output port 1
   */
  ssSetOutputPortDataType(S, 0, SS_INT32);
  ssSetOutputPortWidth(S, 0, 1);
  ssSetOutputPortComplexSignal(S, 0, COMPLEX_NO);
  ssSetOutputPortOptimOpts(S, 0, SS_REUSABLE_AND_LOCAL);
  ssSetOutputPortOutputExprInRTW(S, 0, 0);

  /*
   * Configure the output port 2
   */
  ssSetOutputPortDataType(S, 1, SS_INT32);
  ssSetOutputPortWidth(S, 1, 1);
  ssSetOutputPortComplexSignal(S, 1, COMPLEX_NO);
  ssSetOutputPortOptimOpts(S, 1, SS_REUSABLE_AND_LOCAL);
  ssSetOutputPortOutputExprInRTW(S, 1, 0);

  /*
   * Configure the output port 3
   */
  ssSetOutputPortDataType(S, 2, SS_INT32);
  ssSetOutputPortWidth(S, 2, 1);
  ssSetOutputPortComplexSignal(S, 2, COMPLEX_NO);
  ssSetOutputPortOptimOpts(S, 2, SS_REUSABLE_AND_LOCAL);
  ssSetOutputPortOutputExprInRTW(S, 2, 0);

  /*
   * Register reserved identifiers to avoid name conflict
   */
  if (ssRTWGenIsCodeGen(S)) {
    /*
     * Register reserved identifier for OutputFcnSpec
     */
    ssRegMdlInfo(S, "GFLIB_ControllerPIp_w_F32", 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))
   */
  ssSetOptions(S,
               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_SFUNCTION_INLINED_FOR_RTW |
               SS_OPTION_DISALLOW_CONSTANT_SAMPLE_TIME);
}

/* Function: mdlInitializeSampleTimes =====================================
 * Abstract:
 *    This function is used to specify the sample time(s) for your
 *    S-function. You must register the same number of sample times as
 *    specified in ssSetNumSampleTimes.
 */
static void mdlInitializeSampleTimes(SimStruct *S)
{
  ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
  ssSetOffsetTime(S, 0, FIXED_IN_MINOR_STEP_OFFSET);

#if defined(ssSetModelReferenceSampleTimeDefaultInheritance)

  ssSetModelReferenceSampleTimeDefaultInheritance(S);

#endif

}

#define MDL_SET_WORK_WIDTHS
#if defined(MDL_SET_WORK_WIDTHS) && defined(MATLAB_MEX_FILE)

/* Function: mdlSetWorkWidths =============================================
 * Abstract:
 *      The optional method, mdlSetWorkWidths is called after input port
 *      width, output port width, and sample times of the S-function have
 *      been determined to set any state and work vector sizes which are
 *      a function of the input, output, and/or sample times.
 *
 *      Run-time parameters are registered in this method using methods
 *      ssSetNumRunTimeParams, ssSetRunTimeParamInfo, and related methods.
 */
static void mdlSetWorkWidths(SimStruct *S)
{
  /* Set number of run-time parameters */
  if (!ssSetNumRunTimeParams(S, 4))
    return;

  /*
   * Register the run-time parameter 1
   */
  ssRegDlgParamAsRunTimeParam(S, 0, 0, "p1", ssGetDataTypeId(S, "int32"));

  /*
   * Register the run-time parameter 2
   */
  ssRegDlgParamAsRunTimeParam(S, 1, 1, "p2", ssGetDataTypeId(S, "int16"));

  /*
   * Register the run-time parameter 3
   */
  ssRegDlgParamAsRunTimeParam(S, 2, 2, "p3", ssGetDataTypeId(S, "int32"));

  /*
   * Register the run-time parameter 4
   */
  ssRegDlgParamAsRunTimeParam(S, 3, 3, "p4", ssGetDataTypeId(S, "int16"));
}

#endif

/* Function: mdlOutputs ===================================================
 * Abstract:
 *    In this function, you compute the outputs of your S-function
 *    block. Generally outputs are placed in the output vector(s),
 *    ssGetOutputPortSignal.
 */
static void mdlOutputs(SimStruct *S, int_T tid)
{
  /*
   * Get access to Parameter/Input/Output/DWork/size information
   */
  int32_T *p1 = (int32_T *) ssGetRunTimeParamInfo(S, 0)->data;
  int16_T *p2 = (int16_T *) ssGetRunTimeParamInfo(S, 1)->data;
  int32_T *p3 = (int32_T *) ssGetRunTimeParamInfo(S, 2)->data;
  int16_T *p4 = (int16_T *) ssGetRunTimeParamInfo(S, 3)->data;
  int32_T *u1 = (int32_T *) ssGetInputPortSignal(S, 0);
  int32_T *u2 = (int32_T *) ssGetInputPortSignal(S, 1);
  int32_T *u3 = (int32_T *) ssGetInputPortSignal(S, 2);
  int32_T *y1 = (int32_T *) ssGetOutputPortSignal(S, 0);
  int32_T *y2 = (int32_T *) ssGetOutputPortSignal(S, 1);
  int32_T *y3 = (int32_T *) ssGetOutputPortSignal(S, 2);

  /*
   * Call the legacy code function
   */
  GFLIB_ControllerPIp_w_F32( *u1, *u2, *u3, *p1, *p2, *p3, *p4, y1, y2, y3);
}

/* Function: mdlTerminate =================================================
 * Abstract:
 *    In this function, you should perform any actions that are necessary
 *    at the termination of a simulation.
 */
static void mdlTerminate(SimStruct *S)
{
}

/*
 * Required S-function trailer
 */
#ifdef MATLAB_MEX_FILE
# include "simulink.c"
#else
# include "cg_sfun.h"
#endif
