// Automatically generated C++ file on Tue Feb 20 10:48:26 2024 // // To build with Digital Mars C++ Compiler: // // dmc -mn -WD delay_x1.cpp kernel32.lib #include union uData { bool b; char c; unsigned char uc; short s; unsigned short us; int i; unsigned int ui; float f; double d; long long int i64; unsigned long long int ui64; char *str; unsigned char *bytes; }; // int DllMain() must exist and return 1 for a process to load the .DLL // See https://docs.microsoft.com/en-us/windows/win32/dlls/dllmain for more // information. int __stdcall DllMain(void *module, unsigned int reason, void *reserved) { return 1; } void bzero(void *ptr, unsigned int count) { unsigned char *first = (unsigned char *)ptr; unsigned char *last = first + count; while (first < last) *first++ = '\0'; } // #undef pin names lest they collide with names in any header file(s) you might // include. #undef Vin #undef Vref #undef Vout #undef Vcc struct sDELAY_X1 { // declare the structure here double timeout_t; // next time to trigger out high }; extern "C" __declspec(dllexport) void delay_x1( struct sDELAY_X1 **opaque, double t, union uData *data) { double Vin = data[0].d; // input double Vref = data[1].d; // input double Vcc = data[2].d; // input double delay = data[3].d; // input parameter double &Vout = data[4].d; // output if (!*opaque) { *opaque = (struct sDELAY_X1 *)malloc(sizeof(struct sDELAY_X1)); bzero(*opaque, sizeof(struct sDELAY_X1)); } struct sDELAY_X1 *inst = *opaque; // Implement module evaluation code here: // if Vin < Vref, advance next trigger time and out is low if (Vin < Vref) { inst->timeout_t = t + delay; Vout = 0; return; } // Vin > Vref, if past next trigger time, out is high if (t > inst->timeout_t) Vout = Vcc; } extern "C" __declspec(dllexport) void Trunc(struct sDELAY_X1 *inst, double t, union uData *data, double *timestep) { // limit the timestep to a tolerance if the circuit // causes a change in struct sDELAY_X1 double ttol = 1e-9; // if proposed next simulation time (t) > next timeout, shorten timestep to // next timeout if (t > inst->timeout_t) ttol = t - inst->timeout_t; *timestep = ttol; } extern "C" __declspec(dllexport) void Destroy(struct sDELAY_X1 *inst) { free(inst); }