// Automatically generated C++ file on Mon Mar 17 16:55:20 2025 // // To build with Digital Mars C++ Compiler: // // dmc -mn -WD trunc_bug_x1.cpp kernel32.lib #include #include #include #define MAXLINES 200 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 vin #undef vout struct sTRUNC_BUG_X1 { double t_last; int line_count; }; FILE *ofile = fopen("debug.txt", "w"); extern "C" __declspec(dllexport) void trunc_bug_x1( struct sTRUNC_BUG_X1 **opaque, double t, union uData *data) { double vin = data[0].d; // input double &vout = data[1].d; // output double clock = 5e-9; if (!*opaque) { *opaque = (struct sTRUNC_BUG_X1 *)malloc(sizeof(struct sTRUNC_BUG_X1)); bzero(*opaque, sizeof(struct sTRUNC_BUG_X1)); (*opaque)->line_count = 0; } struct sTRUNC_BUG_X1 *inst = *opaque; // Implement module evaluation code here: double t_next = (round(inst->t_last / clock - 0.5) + 1) * clock; if (t_next < inst->t_last) fprintf(ofile, "*** err1 ***\n"); if ((t_next > 0) && ((t - t_next) > 1e-12) && (inst->line_count < MAXLINES)) { fprintf(ofile, "**** Trunc Bug! eval t = %f, t_next = %f , Actual Timestep = " "%f****\n", t * 1e9, t_next * 1e9, (t - inst->t_last) * 1e9); Display( "**** Trunc Bug! eval t = %f, t_next = %f , Actual Timestep = " "%f****\n", t * 1e9, t_next * 1e9, (t - inst->t_last) * 1e9); inst->line_count = inst->line_count + 1; } // else if (abs(t - t_next) < 1e-12) // { // Display(">>>> @%f: Clock Eval OK. %f\n", t*1e9, abs(t - t_next) *1e9); // } else if (inst->line_count < MAXLINES) { fprintf(ofile, "eval @%f: Next clock at %f, Actual Timestep = %f\n", t * 1e9, t_next * 1e9, (t - inst->t_last) * 1e9); inst->line_count = inst->line_count + 1; } vout = t_next <= t; inst->t_last = t; } extern "C" __declspec(dllexport) double MaxExtStepSize( struct sTRUNC_BUG_X1 *inst, double t) { return 1e308; // implement a good choice of max timestep size that depends // on struct sTRUNC_BUG_X1 } extern "C" __declspec(dllexport) void Trunc(struct sTRUNC_BUG_X1 *inst, double t, union uData *data, double *timestep) { // limit the timestep to next clock edge double clock = 5e-9; double t_next = (round(inst->t_last / clock - 0.5) + 1) * clock; if ((t > 0) && (t > t_next)) { *timestep = t_next - inst->t_last; if (*timestep <= 0) { fprintf(ofile, "*** err2 ***\n"); *timestep = 1e-10; } if (inst->line_count < MAXLINES) { fprintf(ofile, "trunc @%f: sim_timestep = %f, my_timestep = %f\n", t * 1e9, (t - inst->t_last) * 1e9, *timestep * 1e9); inst->line_count = inst->line_count + 1; } } } extern "C" __declspec(dllexport) void Destroy(struct sTRUNC_BUG_X1 *inst) { free(inst); }