/**
 * sminvcsf.c
 *
 *    ABSTRACT:
 *      The purpose of this sfunction is to call a simple legacy
 *      function during simulation:
 *
 *         void vsInv(int32 size(u1,1)*size(u1,2), single u1[][], single y1[size(u1,1)][size(u1,2)])
 *
 *    Simulink version           : 10.6 (R2022b) 13-May-2022
 *    C source code generated on : 27-Jan-2023 10:44:48
 *
 * 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 = 'sminvcsf';
     def.OutputFcnSpec = 'void vsInv(int32 size(u1,1)*size(u1,2), single u1[][], single y1[size(u1,1)][size(u1,2)])';
     def.HeaderFiles = {'meq.h'};
     def.HostLibFiles = {'meqld('host')'};
     def.TargetLibFiles = {'meqld('target')'};
     def.IncPaths = {'C:\Users\amerle\codes\meq'};
     def.LibPaths = {'C:\Users\amerle\codes\meq'};
     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  sminvcsf
#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 "meq.h"


/* 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, 0);


    /* Set the number of work vectors */
    if (!ssSetNumDWork(S, 0)) return;
    ssSetNumPWork(S, 0);

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

    /* Configure the input port 1 */
    ssSetInputPortDataType(S, 0, SS_SINGLE);
    {
        int_T u1NumRows = DYNAMICALLY_SIZED;
        int_T u1NumCols = DYNAMICALLY_SIZED;
        ssSetInputPortMatrixDimensions(S, 0, u1NumRows, u1NumCols);
    }
    ssSetInputPortComplexSignal(S, 0, COMPLEX_NO);
    ssSetInputPortDirectFeedThrough(S, 0, 1);
    ssSetInputPortAcceptExprInRTW(S, 0, 0);
    ssSetInputPortOverWritable(S, 0, 0);
    ssSetInputPortOptimOpts(S, 0, SS_REUSABLE_AND_LOCAL);
    ssSetInputPortRequiredContiguous(S, 0, 1);
    ssSetInputPortDimensionsMode(S, 0, FIXED_DIMS_MODE);

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

    /* Configure the output port 1 */
    ssSetOutputPortDataType(S, 0, SS_SINGLE);
    {
        int_T y1NumRows = DYNAMICALLY_SIZED;
        int_T y1NumCols = DYNAMICALLY_SIZED;
        ssSetOutputPortMatrixDimensions(S, 0, y1NumRows, y1NumCols);
    }
    ssSetOutputPortComplexSignal(S, 0, COMPLEX_NO);
    ssSetOutputPortOptimOpts(S, 0, SS_REUSABLE_AND_LOCAL);
    ssSetOutputPortOutputExprInRTW(S, 0, 0);
    ssSetOutputPortDimensionsMode(S, 0, FIXED_DIMS_MODE);

    /* Register reserved identifiers to avoid name conflict */
    if (ssRTWGenIsCodeGen(S) || ssGetSimMode(S)==SS_SIMMODE_EXTERNAL) {

        /* Register reserved identifier for  */
        ssRegMdlInfo(S, "vsInv", MDL_INFO_ID_RESERVED, 0, 0, ssGetPath(S));
    } /* if */

    /* 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);

    /* Set the compliance for the operating point save/restore. */
    ssSetOperatingPointCompliance(S, USE_DEFAULT_OPERATING_POINT);

    ssSetArrayLayoutForCodeGen(S, SS_COLUMN_MAJOR);

    /* Set the Simulink version this S-Function has been generated in */
    ssSetSimulinkVersionGeneratedIn(S, "10.6");

    /**
     * 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_INPUT_PORT_DIMENSION_INFO
#if defined(MDL_SET_INPUT_PORT_DIMENSION_INFO) && defined(MATLAB_MEX_FILE)
/* Function: mdlSetInputPortDimensionInfo =================================
 * Abstract:
 *   This method is called with the candidate dimensions for an input port
 *   with unknown dimensions. If the proposed dimensions are acceptable, the
 *   method should go ahead and set the actual port dimensions.
 *   If they are unacceptable an error should be generated via
 *   ssSetErrorStatus.
 *   Note that any other input or output ports whose dimensions are
 *   implicitly defined by virtue of knowing the dimensions of the given
 *   port can also have their dimensions set.
 */
static void mdlSetInputPortDimensionInfo(SimStruct *S, int_T portIndex, const DimsInfo_T *dimsInfo)
{
    /* Set input port dimension */
    if(!ssSetInputPortDimensionInfo(S, portIndex, dimsInfo)) return;

    /* Set output port 1 dimension */
    {
        int_T y1NumRows = ssGetInputPortDimensions(S, 0)[0];
        int_T y1NumCols = ssGetInputPortDimensions(S, 0)[1];
        if (y1NumRows!=DYNAMICALLY_SIZED && y1NumRows < 1) { ssSetErrorStatus(S, "Output 1: the current number of rows is < 1"); return; }
        if (y1NumCols!=DYNAMICALLY_SIZED && y1NumCols < 1) { ssSetErrorStatus(S, "Output 1: the current number of columns is < 1"); return; }
        if ((y1NumRows != DYNAMICALLY_SIZED) && (y1NumCols != DYNAMICALLY_SIZED)) {
            ssSetOutputPortMatrixDimensions(S, 0, y1NumRows, y1NumCols);
        } /* if */
    }
}
#endif

#define MDL_SET_OUTPUT_PORT_DIMENSION_INFO
#if defined(MDL_SET_OUTPUT_PORT_DIMENSION_INFO) && defined(MATLAB_MEX_FILE)
/* Function: mdlSetOutputPortDimensionInfo ================================
 * Abstract:
 *   This method is called with the candidate dimensions for an output port
 *   with unknown dimensions. If the proposed dimensions are acceptable, the
 *   method should go ahead and set the actual port dimensions.
 *   If they are unacceptable an error should be generated via
 *   ssSetErrorStatus.
 *   Note that any other input or output ports whose dimensions are
 *   implicitly defined by virtue of knowing the dimensions of the given
 *   port can also have their dimensions set.
 */
static void mdlSetOutputPortDimensionInfo(SimStruct *S, int_T portIndex, const DimsInfo_T *dimsInfo)
{
    /* Set output port dimension */
    if(!ssSetOutputPortDimensionInfo(S, portIndex, dimsInfo)) return;
    {
        int_T y1NumRows = ssGetInputPortDimensions(S, 0)[0];
        int_T y1NumCols = ssGetInputPortDimensions(S, 0)[1];
        if (y1NumRows!=DYNAMICALLY_SIZED && y1NumRows < 1) { ssSetErrorStatus(S, "Output 1: the current number of rows is < 1"); return; }
        if (y1NumCols!=DYNAMICALLY_SIZED && y1NumCols < 1) { ssSetErrorStatus(S, "Output 1: the current number of columns is < 1"); return; }
        if ((y1NumRows != DYNAMICALLY_SIZED) && (y1NumCols != DYNAMICALLY_SIZED)) {
            if ((dimsInfo->numDims != 2) || (dimsInfo->dims[0] != y1NumRows) || (dimsInfo->dims[1] != y1NumCols)) {
               ssSetErrorStatus(S, "Invalid output port dimensions. The output signal 1 must be a 2D (matrix) signal.");
            }
        } /* if */
    }
}
#endif

#define MDL_SET_DEFAULT_PORT_DIMENSION_INFO
#if defined(MDL_SET_DEFAULT_PORT_DIMENSION_INFO) && defined(MATLAB_MEX_FILE)
/* Function: mdlSetDefaultPortDimensionInfo ===============================
 * Abstract:
 *   This method is called when there is not enough information in your
 *   model to uniquely determine the port dimensionality of signals
 *   entering or leaving your block. When this occurs, Simulink's
 *   dimension propagation engine calls this method to ask you to set
 *   your S-functions default dimensions for any input and output ports
 *   that are dynamically sized.
 *   If you do not provide this method and you have dynamically sized ports
 *   where Simulink does not have enough information to propagate the
 *   dimensionality to your S-function, then Simulink will set these unknown
 *   ports to the 'block width' which is determined by examining any known
 *   ports. If there are no known ports, the width will be set to 1.
 */
static void mdlSetDefaultPortDimensionInfo(SimStruct *S)
{

    /* Set input port 1 default dimension */
    if (ssGetInputPortWidth(S, 0) == DYNAMICALLY_SIZED) {
        if (!ssSetInputPortMatrixDimensions(S, 0, 1, 1)) return;
    } /* if */

    /* Set output port 1 default dimension */
    {
        int_T y1NumRows = ssGetInputPortDimensions(S, 0)[0];
        int_T y1NumCols = ssGetInputPortDimensions(S, 0)[1];
        if (y1NumRows!=DYNAMICALLY_SIZED && y1NumRows < 1) { ssSetErrorStatus(S, "Output 1: the current number of rows is < 1"); return; }
        if (y1NumCols!=DYNAMICALLY_SIZED && y1NumCols < 1) { ssSetErrorStatus(S, "Output 1: the current number of columns is < 1"); return; }
        if ((y1NumRows != DYNAMICALLY_SIZED) && (y1NumCols != DYNAMICALLY_SIZED)) {
            ssSetOutputPortMatrixDimensions(S, 0, y1NumRows, y1NumCols);
        } /* if */
    }
}
#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 data */
    int32_T expr1 = (int32_T) (ssGetInputPortDimensions(S, 0)[0]*ssGetInputPortDimensions(S, 0)[1]);
    real32_T* u1 = (real32_T*) ssGetInputPortSignal(S, 0);
    real32_T* y1 = (real32_T*) ssGetOutputPortSignal(S, 0);


    /* Call the legacy code function */
    vsInv(expr1, u1, y1);
}

/* 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

