// Automatically generated C++ file on Fri Mar 27 16:22:36 2026 // // To build with Digital Mars C++ Compiler: // // dmc -mn -WD -o loadcoefficient.cpp kernel32.lib #include extern "C" __declspec(dllexport) void (*Display)(const char *format, ...) = 0; // works like printf() extern "C" __declspec(dllexport) void (*EXIT)(const char *format, ...) = 0; // print message like printf() but exit(0) afterward 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) const void *GUI_HWND = 0; // pointer to Window handle of QUX.exe extern "C" __declspec(dllexport) const double *CKTtime = 0; extern "C" __declspec(dllexport) const double *CKTdelta = 0; extern "C" __declspec(dllexport) const int *IntegrationOrder = 0; extern "C" __declspec(dllexport) const char *InstallDirectory = 0; extern "C" __declspec(dllexport) double (*EngAtof)(const char **string) = 0; extern "C" __declspec(dllexport) const char *(*BinaryFormat)(unsigned int data) = 0; // BinaryFormat(0x1C) returns "0b00011100" extern "C" __declspec(dllexport) const char *(*EngFormat )(double x, const char *units, int numDgts) = 0; // EngFormat(1e-6, "s", 6) returns "1µs" extern "C" __declspec(dllexport) int (*DFFT)(struct sComplex *u, bool inv, unsigned int N, double scale) = 0; // Discrete Fast Fourier Transform extern "C" __declspec(dllexport) void (*bzero)(void *ptr, unsigned int count) = 0; 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; } // #undef pin names lest they collide with names in any header file(s) you might include. #undef coeff #undef out #undef idx #undef in struct sLOADCOEFFICIENT { // declare the structure here bool init; float coeff[5]; }; extern "C" __declspec(dllexport) void loadcoefficient(struct sLOADCOEFFICIENT **opaque, double t, union uData *data) { double coeff = data[0].d; // input double in = data[1].d; // input double &out = data[2].d; // output int &idx = data[3].i; // output if(!*opaque) { *opaque = (struct sLOADCOEFFICIENT *) malloc(sizeof(struct sLOADCOEFFICIENT)); bzero(*opaque, sizeof(struct sLOADCOEFFICIENT)); } struct sLOADCOEFFICIENT *inst = *opaque; // Implement module evaluation code here: const int N=5; // Polynomial order (number of coefficients) // initialization, don't do anything except to load coeff. if(!inst->init && t > 0){ if(idx==0){ // First coefficient being loaded Display("C++ block loading coefficient...\n"); } inst->coeff[idx] = coeff; // Store current coefficient Display("inst->coeff[%d]=%f\n",idx,inst->coeff[idx]); idx++; if(idx == N){ // All coefficients loaded inst->init=true; Display("Loading completed at t=%es",t); } } // main program if(inst->init){ // Only execute once all coefficients are loaded double x[5]; x[0]=1; // x^0 = 1 x[1]=in; // x^1 x[2]=in*in; // x^2 x[3]=in*in*in; // x^3 x[4]=in*in*in*in; // x^4 out = 0; // Initialize output accumulator for(int n=0; n<5; n++){ //sum(coeff[n] * x^n) out = inst->coeff[n]*x[n]+out; // Accumulate weighted terms } } } extern "C" __declspec(dllexport) double MaxExtStepSize(struct sLOADCOEFFICIENT *inst, double t) { return 1e308; // implement a good choice of max timestep size that depends on struct sLOADCOEFFICIENT } extern "C" __declspec(dllexport) void Trunc(struct sLOADCOEFFICIENT *inst, double t, union uData *data, double *timestep) { // limit the timestep to a tolerance if the circuit causes a change in struct sLOADCOEFFICIENT const double ttol = 1e-9; // 1ns default tolerance if(*timestep > ttol) { struct sLOADCOEFFICIENT tmp = *inst; loadcoefficient(&(&tmp), t, data); // if(tmp != *inst) // implement a meaningful way to detect if the state has changed // *timestep = ttol; } } extern "C" __declspec(dllexport) void Destroy(struct sLOADCOEFFICIENT *inst) { free(inst); }