/*
 * SMTOEPR   A SIMULINK real Toeplix matrix constructor.
 *
 *           Syntax:  [sys, x0] = sbuffer(t,x,u,flag,Length)
 *
 *  Copyright 1995-2000 The MathWorks, Inc.
 *  $Revision: 1.11 $  $Date: 2000/06/14 14:28:01 $
 */

#define S_FUNCTION_NAME smtoepr

#include "dsp_sim.h"

/*
 * Defines for easy access of the input parameters
 */
#define NUM_ARGS 1
#define IN_LEN   ssGetArg(S,0)


static void mdlInitializeSizes(SimStruct *S)
{
  /*
   * Set-up size information.
   */
  if (ssGetNumArgs(S) == NUM_ARGS) {
    int Length;

#ifdef MATLAB_MEX_FILE
    if ((mxGetM(IN_LEN) != 1) || (mxGetN(IN_LEN) != 1))
      mexErrMsgTxt("Length must be a scalar.");
#endif
    Length = *mxGetPr(IN_LEN);     /* Length of input vector */

    ssSetNumContStates(    S, 0);
    ssSetNumDiscStates(    S, 0);
    ssSetNumInputs(        S, Length);
    ssSetNumOutputs(       S, Length*Length);
    ssSetDirectFeedThrough(S, 1);
    ssSetNumInputArgs(     S, NUM_ARGS);
    ssSetNumSampleTimes(   S, 1);
    ssSetNumRWork(         S, 0);
    ssSetNumIWork(         S, 0);
    ssSetNumPWork(         S, 0);
  }
#ifdef MATLAB_MEX_FILE
  else {
    mexErrMsgTxt("Wrong number of input arguments");
  }
#endif
}


static void mdlInitializeSampleTimes(SimStruct *S)
{
    /*
     * Blocks that are continuous in nature have a single
     * sample time of 0.0
     */
    ssSetSampleTimeEvent(S, 0, INHERITED_SAMPLE_TIME);
    ssSetOffsetTimeEvent(S, 0, 0.0);
}


static void mdlInitializeConditions(real_T *x0, SimStruct *S)
{
#ifdef MATLAB_MEX_FILE
  int Length, num_inputs;

  if ((mxGetM(IN_LEN) != 1) || (mxGetN(IN_LEN) != 1))
    mexErrMsgTxt("Length must be a scalar.");

  Length = *mxGetPr(IN_LEN);     /* Length of input vector */

  /* Check that input vector size is appropriate: */
  num_inputs = ssGetNumInputs(S);
  if(num_inputs != Length) {
     mexErrMsgTxt("Input to C-MEX S-Function 'smtoepr' is not consistent with Length.");
  }
#endif
}


static void mdlOutputs(real_T *y, const real_T *x, const real_T *u, 
                       SimStruct *S, int_T tid)
{
    /* Inputs are u, Outputs are y */
    int Length = *mxGetPr(IN_LEN);     /* Length of input vector */
    int i;

    for(i=0; i<Length; i++, u++) {
        const real_T *u1 = u;
        int          idx = i;
        int          j   = Length;
        while(j-- != 0) {
            *y++ = *u1;
            ((--idx)<0) ? (u1++) : (u1--);
        }
    }
}

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
