/*
 * SZOH   A SIMULINK ZOH without special properties
 * DSP Blockset S-Function to perform "non-special" Zero Order Hold.
 * This SIMULINK S-function samples and holds the input vector.
 *
 * Syntax:  [sys, x0] = szoh(t,x,u,flag, sampleTime)
 *
 *  Copyright (c) 1995-98 by The MathWorks, Inc.
 *  $Revision: 1.9 $ $Date: 1997/11/26 20:24:24 $
 */

#define S_FUNCTION_NAME szoh

#include "simstruc.h"

#define SAMPLETIME_ARG  ssGetArg(S,0)
#define NUM_ARGS        1


#ifdef MATLAB_MEX_FILE
#define MDL_CHECK_PARAMETERS
static void mdlCheckParameters(SimStruct *S) {

    if ((mxGetM(SAMPLETIME_ARG) != 1) ||
        (mxGetN(SAMPLETIME_ARG) == 0) ||
        (mxGetN(SAMPLETIME_ARG) > 2)   ) {
        ssSetErrorStatus(S, "The sample time must be either a scalar or 2-element row vector");
    }
}
#endif


static void mdlInitializeSizes(SimStruct *S)
{
    ssSetNumSFcnParams(S, NUM_ARGS);

#if defined(MATLAB_MEX_FILE)
    if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) return;
    mdlCheckParameters(S);
    if (ssGetErrorStatus(S) != NULL) return;
#endif

    ssSetNumInputs(        S, DYNAMICALLY_SIZED);
    ssSetNumOutputs(       S, DYNAMICALLY_SIZED);
    ssSetDirectFeedThrough(S, 1);
    ssSetNumSampleTimes(   S, 1);
    ssSetOptions(          S, SS_OPTION_EXCEPTION_FREE_CODE |
                              SS_OPTION_USING_ssGetUPtrs);
}    


static void mdlInitializeSampleTimes(SimStruct *S)
{
    real_T *pr = mxGetPr(SAMPLETIME_ARG);
    ssSetSampleTimeEvent(S, 0, pr[0]);
    ssSetOffsetTimeEvent(S, 0, (mxGetN(SAMPLETIME_ARG) > 1) ? pr[1] : 0.0);
}


static void mdlInitializeConditions(real_T *x0, SimStruct *S)
{
}


static void mdlOutputs(real_T *y, const real_T *x, const real_T *u, 
                       SimStruct *S, int_T tid)
{
    UPtrsType uptr = ssGetUPtrs(S);
    int_T     N    = ssGetNumInputs(S);

    while(N-- > 0) {
        *y++ = **(uptr++);
    }
}


static void mdlUpdate(real_T *x, const real_T *u, SimStruct *S, int_T tid)
{
}


static void mdlDerivatives(real_T *dx, const real_T *x, const real_T *u, 
                           SimStruct *S, int_T tid)
{
}


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 */
#else
#include "cg_sfun.h"    /* Code generation registration function */
#endif
