C++ block question

For executing something every rising edge of an external clock, I do it like this:
(you need to create the template with the Include a struct… setting checked)

struct sTEST2_X1
{
  // declare the structure here
  bool state;
};

extern "C" __declspec(dllexport) void test2_x1(struct sTEST2_X1 **opaque, double t, union uData *data)
{
   double  in  = data[0].d; // input
   bool    clk = data[1].b; // input
   double &out = data[2].d; // output

   if(!*opaque)
   {
      *opaque = (struct sTEST2_X1 *) malloc(sizeof(struct sTEST2_X1));
      bzero(*opaque, sizeof(struct sTEST2_X1));
   }
   struct sTEST2_X1 *inst = *opaque;

// Implement module evaluation code here:
   if(!(inst->state) && clk) {
      inst->state = 1;
      out = in;
   }
   else if ((inst->state) && !clk) {
      inst->state = 0;
   }
}

edit: looking at WinterMimes post - static variables for storage seem to work too. When I tried that a few days ago, it didn’t work correctly. I wish there was a documation of what can/should be done.

1 Like