/*
 *  com_rst_chk.c
 *
 *    Set the sample times of all input and output ports.
 *
 *    Verify that the sample time for each port is the 
 *    same as that of the first input and output ports
 *
 *    Verify that sample time is discrete and offset is zero.
 *
 *    Copyright 1996-2000 The MathWorks, Inc.
 *    $Revision: 1.6 $  $Date: 2000/05/19 14:41:59 $
 */

#ifdef MATLAB_MEX_FILE

#define MDL_SET_INPUT_PORT_SAMPLE_TIME
static void mdlSetInputPortSampleTime(SimStruct *S,
                                      int_T     portIdx,
                                      real_T    sampleTime,
                                      real_T    offsetTime)
{
    if (sampleTime == CONTINUOUS_SAMPLE_TIME) {
        ssSetErrorStatus(S, "Continuous sample times not allowed.");
		return;
    }

    if (offsetTime != 0.0) {
        ssSetErrorStatus(S, "Non-zero sample time offsets not allowed.");
		return;
    }

	ssSetInputPortSampleTime(S, portIdx, sampleTime);
	ssSetInputPortOffsetTime(S, portIdx, 0.0);

    if (portIdx==0) {

		int_T i, num;

		if (ssGetOutputPortSampleTime(S, 0) == INHERITED_SAMPLE_TIME) {
			ssSetOutputPortSampleTime(S, 0, sampleTime);
			ssSetOutputPortOffsetTime(S, 0, 0.0);
		}

		num = ssGetNumInputPorts(S);
		for (i=0; i<num; i++) {
			if (ssGetInputPortSampleTime(S, i) != INHERITED_SAMPLE_TIME &&
				ssGetInputPortSampleTime(S, i) != sampleTime) {
				ssSetErrorStatus(S, "All input port sample times must match.");
				return;
			}
		}
		
		num = ssGetNumOutputPorts(S);
		for (i=0; i<num; i++) {
			if (ssGetOutputPortSampleTime(S, i) != INHERITED_SAMPLE_TIME &&
				ssGetOutputPortSampleTime(S, i) != sampleTime) {
				ssSetErrorStatus(S, "All output port sample times must match.");
				return;
			}
		}

	} else {
		if (ssGetInputPortSampleTime(S, 0) != INHERITED_SAMPLE_TIME &&
			ssGetInputPortSampleTime(S, 0) != sampleTime) {
			ssSetErrorStatus(S, "All input port sample times must match.");
			return;
		}
	}
}


#define MDL_SET_OUTPUT_PORT_SAMPLE_TIME
static void mdlSetOutputPortSampleTime(SimStruct *S,
                                       int_T     portIdx,
                                       real_T    sampleTime,
                                       real_T    offsetTime)
{
    if (sampleTime == CONTINUOUS_SAMPLE_TIME) {
        ssSetErrorStatus(S, "Continuous sample times not allowed.");
		return;
    }

    if (offsetTime != 0.0) {
        ssSetErrorStatus(S, "Non-zero sample time offsets not allowed.");
		return;
    }

	ssSetOutputPortSampleTime(S, portIdx, sampleTime);
	ssSetOutputPortOffsetTime(S, portIdx, 0.0);

    if (portIdx==0) {

		int_T i, num;

		if (ssGetInputPortSampleTime(S, 0) == INHERITED_SAMPLE_TIME) {
			ssSetInputPortSampleTime(S, 0, sampleTime);
			ssSetInputPortOffsetTime(S, 0, 0.0);
		}
			
		num = ssGetNumInputPorts(S);
		for (i=0; i<num; i++) {
			if (ssGetInputPortSampleTime(S, i) != INHERITED_SAMPLE_TIME &&
				ssGetInputPortSampleTime(S, i) != sampleTime) {
				ssSetErrorStatus(S, "All input port sample times must match.");
				return;
			}
		}
		
		num = ssGetNumOutputPorts(S);
		for (i=0; i<num; i++) {
			if (ssGetOutputPortSampleTime(S, i) != INHERITED_SAMPLE_TIME &&
				ssGetOutputPortSampleTime(S, i) != sampleTime) {
				ssSetErrorStatus(S, "All output port sample times must match.");
				return;
			}
		}

    } else {
		if (ssGetOutputPortSampleTime(S, 0) != INHERITED_SAMPLE_TIME &&
			ssGetOutputPortSampleTime(S, 0) != sampleTime) {
			ssSetErrorStatus(S, "All output port sample times must match.");
			return;
		}
	}
}

#endif /* MATLAB_MEX_FILE */


static void mdlInitializeSampleTimes(SimStruct *S)
{
    /* This function intentionally left blank. */
}
