#include <string.h>

// The array of all boolean signals.
bool b[1];

// The array of all integer signals.
int i[6];


// The RTflow model names of all boolean signals, terminated by NULL.
const char* const boolNames[1+1] =
{
   "enable",
   NULL
};

// The RTflow model names of all integer signals, terminated by NULL.
const char* const intNames[6+1] =
{
   "count",
   "sum",
   "pre_count",
   "v1",
   "v0",
   "Pre1",
   NULL
};

// The RTflow model names of all real-valued signals, terminated by NULL.
const char* const realNames[0+1] =
{
   NULL
};

// Returns the array of all boolean signals.
bool* getBoolArray() { return b; }

// Returns the array of all integer signals.
int* getIntArray() { return i; }


// Returns the real time in seconds corresponding to one execution cycle.
float GetSampleTime(void)
{
   return (float)0.001;
}

// Initializes the state of the system. Call this function once before
// calling run().
void Initialize(void)
{
   i[5] = 0;                /* Pre1 = 0; */
}

// Executes one cycle. Every cycle, the calling code should:
// 1) Provide the input signal values by writing into the arrays.
// 2) Call the run() funcstion.
// 3) Collect the output signal values by reading from the arrays.
void Run(void)
{
   // Output update equations
   i[2] = i[5];             /* pre_count = Pre1; */
   i[3] = 1;                /* v1 = 1; */
   i[4] = 0;                /* v0 = 0; */
   i[1] = i[2] + i[3];      /* sum = pre_count + v1; */
   i[0] = b[0] ? i[1] : i[4];/* count = enable ? sum : v0; */

   // State update equations
   i[5] = i[0];             /* Pre1 = count; */
}

// Finds the index of a signal given its name. The calling code can
// use this function to retrieve the index to be used when reading
// or writing in an array for a specific signal.
// Parameter names: The array of names to search; either boolNames, intNames or realNames.
// Parameter name: The name of the signal to find the index for.
// Returns the index of the requested signal, or -1 if not found.
int FindName(const char* const* names, const char* name)
{
   for (int i = 0; names[i] != NULL; i++)
   {
      if (strcmp(names[i], name) == 0)
      {
         return i;
      }
   }
   return -1;
}