/*
 * Test_OPTOS.cpp
 *
 * Academic License - for use in teaching, academic research, and meeting
 * course requirements at degree granting institutions only.  Not for
 * government, commercial, or other organizational use.
 *
 * Code generation for model "Test_OPTOS".
 *
 * Model version              : 1.46
 * Simulink Coder version : 24.2 (R2024b) 21-Jun-2024
 * C++ source code generated on : Wed Jul 23 10:51:14 2025
 *
 * Target selection: speedgoat.tlc
 * Note: GRT includes extra infrastructure and instrumentation for prototyping
 * Embedded hardware selection: Intel->x86-64 (Linux 64)
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "Test_OPTOS.h"
#include "Test_OPTOS_cal.h"
#include "Test_OPTOS_private.h"
#include <cstring>

extern "C"
{

#include "rt_nonfinite.h"

}

/* Block signals (default storage) */
B_Test_OPTOS_T Test_OPTOS_B;

/* Block states (default storage) */
DW_Test_OPTOS_T Test_OPTOS_DW;

/* Real-time model */
RT_MODEL_Test_OPTOS_T Test_OPTOS_M_ = RT_MODEL_Test_OPTOS_T();
RT_MODEL_Test_OPTOS_T *const Test_OPTOS_M = &Test_OPTOS_M_;

/* Model step function */
void Test_OPTOS_step(void)
{
  /* Constant: '<Root>/Constant' */
  Test_OPTOS_B.Va = Test_OPTOS_cal->Constant_Value;

  /* Constant: '<Root>/Constant1' */
  Test_OPTOS_B.Va_d = Test_OPTOS_cal->Constant1_Value;

  /* Constant: '<Root>/Constant2' */
  Test_OPTOS_B.Vb = Test_OPTOS_cal->Constant2_Value;

  /* Constant: '<Root>/Constant3' */
  Test_OPTOS_B.Vb_k = Test_OPTOS_cal->Constant3_Value;

  /* Constant: '<Root>/Constant4' */
  Test_OPTOS_B.Vc = Test_OPTOS_cal->Constant4_Value;

  /* Constant: '<Root>/Constant5' */
  Test_OPTOS_B.Vc_i = Test_OPTOS_cal->Constant5_Value;

  /* S-Function (sg_fpga_do_sf_a2): '<Root>/Digital output' */

  /* Level2 S-Function Block: '<Root>/Digital output' (sg_fpga_do_sf_a2) */
  {
    SimStruct *rts = Test_OPTOS_M->childSfunctions[0];
    sfcnOutputs(rts,0);
  }

  /* user code (Output function Trailer) */
  {
  }

  /* Update absolute time for base rate */
  /* The "clockTick0" counts the number of times the code of this task has
   * been executed. The absolute time is the multiplication of "clockTick0"
   * and "Timing.stepSize0". Size of "clockTick0" ensures timer will not
   * overflow during the application lifespan selected.
   * Timer of this task consists of two 32 bit unsigned integers.
   * The two integers represent the low bits Timing.clockTick0 and the high bits
   * Timing.clockTickH0. When the low bit overflows to 0, the high bits increment.
   */
  if (!(++Test_OPTOS_M->Timing.clockTick0)) {
    ++Test_OPTOS_M->Timing.clockTickH0;
  }

  Test_OPTOS_M->Timing.t[0] = Test_OPTOS_M->Timing.clockTick0 *
    Test_OPTOS_M->Timing.stepSize0 + Test_OPTOS_M->Timing.clockTickH0 *
    Test_OPTOS_M->Timing.stepSize0 * 4294967296.0;
}

/* Model initialize function */
void Test_OPTOS_initialize(void)
{
  /* Registration code */

  /* initialize non-finites */
  rt_InitInfAndNaN(sizeof(real_T));
  rtsiSetSolverName(&Test_OPTOS_M->solverInfo,"FixedStepDiscrete");
  Test_OPTOS_M->solverInfoPtr = (&Test_OPTOS_M->solverInfo);

  /* Initialize timing info */
  {
    int_T *mdlTsMap = Test_OPTOS_M->Timing.sampleTimeTaskIDArray;
    mdlTsMap[0] = 0;
    Test_OPTOS_M->Timing.sampleTimeTaskIDPtr = (&mdlTsMap[0]);
    Test_OPTOS_M->Timing.sampleTimes = (&Test_OPTOS_M->Timing.sampleTimesArray[0]);
    Test_OPTOS_M->Timing.offsetTimes = (&Test_OPTOS_M->Timing.offsetTimesArray[0]);

    /* task periods */
    Test_OPTOS_M->Timing.sampleTimes[0] = (5.0E-5);

    /* task offsets */
    Test_OPTOS_M->Timing.offsetTimes[0] = (0.0);
  }

  rtmSetTPtr(Test_OPTOS_M, &Test_OPTOS_M->Timing.tArray[0]);

  {
    int_T *mdlSampleHits = Test_OPTOS_M->Timing.sampleHitArray;
    mdlSampleHits[0] = 1;
    Test_OPTOS_M->Timing.sampleHits = (&mdlSampleHits[0]);
  }

  rtmSetTFinal(Test_OPTOS_M, -1);
  Test_OPTOS_M->Timing.stepSize0 = 5.0E-5;
  Test_OPTOS_M->solverInfoPtr = (&Test_OPTOS_M->solverInfo);
  Test_OPTOS_M->Timing.stepSize = (5.0E-5);
  rtsiSetFixedStepSize(&Test_OPTOS_M->solverInfo, 5.0E-5);
  rtsiSetSolverMode(&Test_OPTOS_M->solverInfo, SOLVER_MODE_SINGLETASKING);

  /* block I/O */
  (void) std::memset((static_cast<void *>(&Test_OPTOS_B)), 0,
                     sizeof(B_Test_OPTOS_T));

  /* states (dwork) */
  (void) std::memset(static_cast<void *>(&Test_OPTOS_DW), 0,
                     sizeof(DW_Test_OPTOS_T));

  /* child S-Function registration */
  {
    RTWSfcnInfo *sfcnInfo = &Test_OPTOS_M->NonInlinedSFcns.sfcnInfo;
    Test_OPTOS_M->sfcnInfo = (sfcnInfo);
    rtssSetErrorStatusPtr(sfcnInfo, (&rtmGetErrorStatus(Test_OPTOS_M)));
    Test_OPTOS_M->Sizes.numSampTimes = (1);
    rtssSetNumRootSampTimesPtr(sfcnInfo, &Test_OPTOS_M->Sizes.numSampTimes);
    Test_OPTOS_M->NonInlinedSFcns.taskTimePtrs[0] = (&rtmGetTPtr(Test_OPTOS_M)[0]);
    rtssSetTPtrPtr(sfcnInfo,Test_OPTOS_M->NonInlinedSFcns.taskTimePtrs);
    rtssSetTStartPtr(sfcnInfo, &rtmGetTStart(Test_OPTOS_M));
    rtssSetTFinalPtr(sfcnInfo, &rtmGetTFinal(Test_OPTOS_M));
    rtssSetTimeOfLastOutputPtr(sfcnInfo, &rtmGetTimeOfLastOutput(Test_OPTOS_M));
    rtssSetStepSizePtr(sfcnInfo, &Test_OPTOS_M->Timing.stepSize);
    rtssSetStopRequestedPtr(sfcnInfo, &rtmGetStopRequested(Test_OPTOS_M));
    rtssSetDerivCacheNeedsResetPtr(sfcnInfo, &Test_OPTOS_M->derivCacheNeedsReset);
    rtssSetZCCacheNeedsResetPtr(sfcnInfo, &Test_OPTOS_M->zCCacheNeedsReset);
    rtssSetContTimeOutputInconsistentWithStateAtMajorStepPtr(sfcnInfo,
      &Test_OPTOS_M->CTOutputIncnstWithState);
    rtssSetSampleHitsPtr(sfcnInfo, &Test_OPTOS_M->Timing.sampleHits);
    rtssSetPerTaskSampleHitsPtr(sfcnInfo,
      &Test_OPTOS_M->Timing.perTaskSampleHits);
    rtssSetSimModePtr(sfcnInfo, &Test_OPTOS_M->simMode);
    rtssSetSolverInfoPtr(sfcnInfo, &Test_OPTOS_M->solverInfoPtr);
  }

  Test_OPTOS_M->Sizes.numSFcns = (1);

  /* register each child */
  {
    (void) std::memset(static_cast<void *>
                       (&Test_OPTOS_M->NonInlinedSFcns.childSFunctions[0]), 0,
                       1*sizeof(SimStruct));
    Test_OPTOS_M->childSfunctions =
      (&Test_OPTOS_M->NonInlinedSFcns.childSFunctionPtrs[0]);
    Test_OPTOS_M->childSfunctions[0] =
      (&Test_OPTOS_M->NonInlinedSFcns.childSFunctions[0]);

    /* Level2 S-Function Block: Test_OPTOS/<Root>/Digital output (sg_fpga_do_sf_a2) */
    {
      SimStruct *rts = Test_OPTOS_M->childSfunctions[0];

      /* timing info */
      time_T *sfcnPeriod = Test_OPTOS_M->NonInlinedSFcns.Sfcn0.sfcnPeriod;
      time_T *sfcnOffset = Test_OPTOS_M->NonInlinedSFcns.Sfcn0.sfcnOffset;
      int_T *sfcnTsMap = Test_OPTOS_M->NonInlinedSFcns.Sfcn0.sfcnTsMap;
      (void) std::memset(static_cast<void*>(sfcnPeriod), 0,
                         sizeof(time_T)*1);
      (void) std::memset(static_cast<void*>(sfcnOffset), 0,
                         sizeof(time_T)*1);
      ssSetSampleTimePtr(rts, &sfcnPeriod[0]);
      ssSetOffsetTimePtr(rts, &sfcnOffset[0]);
      ssSetSampleTimeTaskIDPtr(rts, sfcnTsMap);

      {
        ssSetBlkInfo2Ptr(rts, &Test_OPTOS_M->NonInlinedSFcns.blkInfo2[0]);
      }

      _ssSetBlkInfo2PortInfo2Ptr(rts,
        &Test_OPTOS_M->NonInlinedSFcns.inputOutputPortInfo2[0]);

      /* Set up the mdlInfo pointer */
      ssSetRTWSfcnInfo(rts, Test_OPTOS_M->sfcnInfo);

      /* Allocate memory of model methods 2 */
      {
        ssSetModelMethods2(rts, &Test_OPTOS_M->NonInlinedSFcns.methods2[0]);
      }

      /* Allocate memory of model methods 3 */
      {
        ssSetModelMethods3(rts, &Test_OPTOS_M->NonInlinedSFcns.methods3[0]);
      }

      /* Allocate memory of model methods 4 */
      {
        ssSetModelMethods4(rts, &Test_OPTOS_M->NonInlinedSFcns.methods4[0]);
      }

      /* Allocate memory for states auxilliary information */
      {
        ssSetStatesInfo2(rts, &Test_OPTOS_M->NonInlinedSFcns.statesInfo2[0]);
        ssSetPeriodicStatesInfo(rts,
          &Test_OPTOS_M->NonInlinedSFcns.periodicStatesInfo[0]);
      }

      /* inputs */
      {
        _ssSetNumInputPorts(rts, 6);
        ssSetPortInfoForInputs(rts,
          &Test_OPTOS_M->NonInlinedSFcns.Sfcn0.inputPortInfo[0]);
        ssSetPortInfoForInputs(rts,
          &Test_OPTOS_M->NonInlinedSFcns.Sfcn0.inputPortInfo[0]);
        _ssSetPortInfo2ForInputUnits(rts,
          &Test_OPTOS_M->NonInlinedSFcns.Sfcn0.inputPortUnits[0]);
        ssSetInputPortUnit(rts, 0, 0);
        ssSetInputPortUnit(rts, 1, 0);
        ssSetInputPortUnit(rts, 2, 0);
        ssSetInputPortUnit(rts, 3, 0);
        ssSetInputPortUnit(rts, 4, 0);
        ssSetInputPortUnit(rts, 5, 0);
        _ssSetPortInfo2ForInputCoSimAttribute(rts,
          &Test_OPTOS_M->NonInlinedSFcns.Sfcn0.inputPortCoSimAttribute[0]);
        ssSetInputPortIsContinuousQuantity(rts, 0, 0);
        ssSetInputPortIsContinuousQuantity(rts, 1, 0);
        ssSetInputPortIsContinuousQuantity(rts, 2, 0);
        ssSetInputPortIsContinuousQuantity(rts, 3, 0);
        ssSetInputPortIsContinuousQuantity(rts, 4, 0);
        ssSetInputPortIsContinuousQuantity(rts, 5, 0);

        /* port 0 */
        {
          ssSetInputPortRequiredContiguous(rts, 0, 1);
          ssSetInputPortSignal(rts, 0, &Test_OPTOS_B.Va);
          _ssSetInputPortNumDimensions(rts, 0, 1);
          ssSetInputPortWidthAsInt(rts, 0, 1);
        }

        /* port 1 */
        {
          ssSetInputPortRequiredContiguous(rts, 1, 1);
          ssSetInputPortSignal(rts, 1, &Test_OPTOS_B.Va_d);
          _ssSetInputPortNumDimensions(rts, 1, 1);
          ssSetInputPortWidthAsInt(rts, 1, 1);
        }

        /* port 2 */
        {
          ssSetInputPortRequiredContiguous(rts, 2, 1);
          ssSetInputPortSignal(rts, 2, &Test_OPTOS_B.Vb);
          _ssSetInputPortNumDimensions(rts, 2, 1);
          ssSetInputPortWidthAsInt(rts, 2, 1);
        }

        /* port 3 */
        {
          ssSetInputPortRequiredContiguous(rts, 3, 1);
          ssSetInputPortSignal(rts, 3, &Test_OPTOS_B.Vb_k);
          _ssSetInputPortNumDimensions(rts, 3, 1);
          ssSetInputPortWidthAsInt(rts, 3, 1);
        }

        /* port 4 */
        {
          ssSetInputPortRequiredContiguous(rts, 4, 1);
          ssSetInputPortSignal(rts, 4, &Test_OPTOS_B.Vc);
          _ssSetInputPortNumDimensions(rts, 4, 1);
          ssSetInputPortWidthAsInt(rts, 4, 1);
        }

        /* port 5 */
        {
          ssSetInputPortRequiredContiguous(rts, 5, 1);
          ssSetInputPortSignal(rts, 5, &Test_OPTOS_B.Vc_i);
          _ssSetInputPortNumDimensions(rts, 5, 1);
          ssSetInputPortWidthAsInt(rts, 5, 1);
        }
      }

      /* path info */
      ssSetModelName(rts, "Digital output");
      ssSetPath(rts, "Test_OPTOS/Digital output");
      ssSetRTModel(rts,Test_OPTOS_M);
      ssSetParentSS(rts, (NULL));
      ssSetRootSS(rts, rts);
      ssSetVersion(rts, SIMSTRUCT_VERSION_LEVEL2);

      /* parameters */
      {
        mxArray **sfcnParams = (mxArray **)
          &Test_OPTOS_M->NonInlinedSFcns.Sfcn0.params;
        ssSetSFcnParamsCount(rts, 6);
        ssSetSFcnParamsPtr(rts, &sfcnParams[0]);
        ssSetSFcnParam(rts, 0, (mxArray*)Test_OPTOS_cal->Digitaloutput_P1_Size);
        ssSetSFcnParam(rts, 1, (mxArray*)Test_OPTOS_cal->Digitaloutput_P2_Size);
        ssSetSFcnParam(rts, 2, (mxArray*)Test_OPTOS_cal->Digitaloutput_P3_Size);
        ssSetSFcnParam(rts, 3, (mxArray*)Test_OPTOS_cal->Digitaloutput_P4_Size);
        ssSetSFcnParam(rts, 4, (mxArray*)Test_OPTOS_cal->Digitaloutput_P5_Size);
        ssSetSFcnParam(rts, 5, (mxArray*)Test_OPTOS_cal->Digitaloutput_P6_Size);
      }

      /* work vectors */
      ssSetIWork(rts, (int_T *) &Test_OPTOS_DW.Digitaloutput_IWORK);
      ssSetPWork(rts, (void **) &Test_OPTOS_DW.Digitaloutput_PWORK[0]);

      {
        struct _ssDWorkRecord *dWorkRecord = (struct _ssDWorkRecord *)
          &Test_OPTOS_M->NonInlinedSFcns.Sfcn0.dWork;
        struct _ssDWorkAuxRecord *dWorkAuxRecord = (struct _ssDWorkAuxRecord *)
          &Test_OPTOS_M->NonInlinedSFcns.Sfcn0.dWorkAux;
        ssSetSFcnDWork(rts, dWorkRecord);
        ssSetSFcnDWorkAux(rts, dWorkAuxRecord);
        ssSetNumDWorkAsInt(rts, 2);

        /* IWORK */
        ssSetDWorkWidthAsInt(rts, 0, 1);
        ssSetDWorkDataType(rts, 0,SS_INTEGER);
        ssSetDWorkComplexSignal(rts, 0, 0);
        ssSetDWork(rts, 0, &Test_OPTOS_DW.Digitaloutput_IWORK);

        /* PWORK */
        ssSetDWorkWidthAsInt(rts, 1, 2);
        ssSetDWorkDataType(rts, 1,SS_POINTER);
        ssSetDWorkComplexSignal(rts, 1, 0);
        ssSetDWork(rts, 1, &Test_OPTOS_DW.Digitaloutput_PWORK[0]);
      }

      /* registration */
      sg_fpga_do_sf_a2(rts);
      sfcnInitializeSizes(rts);
      sfcnInitializeSampleTimes(rts);

      /* adjust sample time */
      ssSetSampleTime(rts, 0, 5.0E-5);
      ssSetOffsetTime(rts, 0, 0.0);
      sfcnTsMap[0] = 0;

      /* set compiled values of dynamic vector attributes */
      ssSetNumNonsampledZCsAsInt(rts, 0);

      /* Update connectivity flags for each port */
      _ssSetInputPortConnected(rts, 0, 1);
      _ssSetInputPortConnected(rts, 1, 1);
      _ssSetInputPortConnected(rts, 2, 1);
      _ssSetInputPortConnected(rts, 3, 1);
      _ssSetInputPortConnected(rts, 4, 1);
      _ssSetInputPortConnected(rts, 5, 1);

      /* Update the BufferDstPort flags for each input port */
      ssSetInputPortBufferDstPort(rts, 0, -1);
      ssSetInputPortBufferDstPort(rts, 1, -1);
      ssSetInputPortBufferDstPort(rts, 2, -1);
      ssSetInputPortBufferDstPort(rts, 3, -1);
      ssSetInputPortBufferDstPort(rts, 4, -1);
      ssSetInputPortBufferDstPort(rts, 5, -1);
    }
  }

  /* Start for Constant: '<Root>/Constant' */
  Test_OPTOS_B.Va = Test_OPTOS_cal->Constant_Value;

  /* Start for Constant: '<Root>/Constant1' */
  Test_OPTOS_B.Va_d = Test_OPTOS_cal->Constant1_Value;

  /* Start for Constant: '<Root>/Constant2' */
  Test_OPTOS_B.Vb = Test_OPTOS_cal->Constant2_Value;

  /* Start for Constant: '<Root>/Constant3' */
  Test_OPTOS_B.Vb_k = Test_OPTOS_cal->Constant3_Value;

  /* Start for Constant: '<Root>/Constant4' */
  Test_OPTOS_B.Vc = Test_OPTOS_cal->Constant4_Value;

  /* Start for Constant: '<Root>/Constant5' */
  Test_OPTOS_B.Vc_i = Test_OPTOS_cal->Constant5_Value;

  /* Start for S-Function (sg_fpga_do_sf_a2): '<Root>/Digital output' */
  /* Level2 S-Function Block: '<Root>/Digital output' (sg_fpga_do_sf_a2) */
  {
    SimStruct *rts = Test_OPTOS_M->childSfunctions[0];
    sfcnStart(rts);
    if (ssGetErrorStatus(rts) != (NULL))
      return;
  }
}

/* Model terminate function */
void Test_OPTOS_terminate(void)
{
  /* Terminate for S-Function (sg_fpga_do_sf_a2): '<Root>/Digital output' */
  /* Level2 S-Function Block: '<Root>/Digital output' (sg_fpga_do_sf_a2) */
  {
    SimStruct *rts = Test_OPTOS_M->childSfunctions[0];
    sfcnTerminate(rts);
  }

  /* user code (Terminate function Trailer) */
  {
    freeFPGAModuleSgLib((uint32_t)1);
  }
}
