// Automatically generated C++ file on Tue Nov 5 19:12:22 2024 // // To build with Digital Mars C++ Compiler: // // dmc -mn -WD constantontime.cpp kernel32.lib #include extern "C" __declspec(dllexport) int (*Display)(const char *format, ...) = 0; // works like printf() extern "C" __declspec(dllexport) const double *DegreesC = 0; // pointer to current circuit temperature extern "C" __declspec(dllexport) const int *StepNumber = 0; // pointer to current step number extern "C" __declspec(dllexport) const int *NumberSteps = 0; // pointer to estimated number of steps extern "C" __declspec(dllexport) const char* const *InstanceName = 0; // pointer to address of instance name extern "C" __declspec(dllexport) const char *QUX = 0; // path to QUX.exe extern "C" __declspec(dllexport) const bool *ForKeeps = 0; // pointer to whether being evaluated non-hypothetically extern "C" __declspec(dllexport) const bool *HoldICs = 0; // pointer to whether instance initial conditions are being held extern "C" __declspec(dllexport) int (*DFFT)(struct sComplex *u, bool inv, unsigned int N, double scale) = 0; // discrete Fast Fourier function 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 CLK #undef Q #undef Ton struct sCONSTANTONTIME { // declare the structure here bool lastQ; bool lastCLK; float Tclk; }; extern "C" __declspec(dllexport) void constantontime(struct sCONSTANTONTIME **opaque, double t, union uData *data) { bool CLK = data[0].b; // input double Ton = data[1].d; // input bool &Q = data[2].b; // output if(!*opaque) { *opaque = (struct sCONSTANTONTIME *) malloc(sizeof(struct sCONSTANTONTIME)); bzero(*opaque, sizeof(struct sCONSTANTONTIME)); } struct sCONSTANTONTIME *inst = *opaque; // Implement module evaluation code here: // rising edge : Set Q if ( (CLK != inst->lastCLK) & (CLK) ){ Q = 1; inst->Tclk = t; // record time of rising edge to inst->Tclk } // On time more than Ton (external) : Reset Q float OnTime = t - inst->Tclk; // on time duration if ( OnTime > Ton ){ Q = 0; } // Store current state inst->lastQ = Q; inst->lastCLK = CLK; } extern "C" __declspec(dllexport) double MaxExtStepSize(struct sCONSTANTONTIME *inst) { return 1e308; // implement a good choice of max timestep size that depends on struct sCONSTANTONTIME } extern "C" __declspec(dllexport) void Trunc(struct sCONSTANTONTIME *inst, double t, union uData *data, double *timestep) { // limit the timestep to a tolerance if the circuit causes a change in struct sCONSTANTONTIME const double ttol = 1e-9; // 1ns default tolerance if(*timestep > ttol) { struct sCONSTANTONTIME tmp = *inst; constantontime(&(&tmp), t, data); // if(tmp != *inst) // implement a meaningful way to detect if the state has changed // *timestep = ttol; if (tmp.lastQ != inst->lastQ) *timestep = ttol; } } extern "C" __declspec(dllexport) void Destroy(struct sCONSTANTONTIME *inst) { free(inst); }