// Automatically generated C++ file on Thu Sep 5 14:24:00 2024 // // To build with Digital Mars C++ Compiler: // // dmc -mn -WD d_flop_cd4013b.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 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 Q #undef _Q #undef D #undef CLK #undef SET #undef RESET struct sD_FLOP_CD4013B { // declare the structure here bool lastCLK; bool lastSET; bool lastRESET; }; extern "C" __declspec(dllexport) void d_flop_cd4013b(struct sD_FLOP_CD4013B **opaque, double t, union uData *data) { bool D = data[0].b; // input bool CLK = data[1].b; // input bool SET = data[2].b; // input bool RESET = data[3].b; // input bool &Q = data[4].b; // output bool &_Q = data[5].b; // output if(!*opaque) { *opaque = (struct sD_FLOP_CD4013B *) malloc(sizeof(struct sD_FLOP_CD4013B)); bzero(*opaque, sizeof(struct sD_FLOP_CD4013B)); // initialization _Q=!Q; } struct sD_FLOP_CD4013B *inst = *opaque; // Implement module evaluation code here: if (!SET & !RESET) { if (inst->lastCLK != CLK && !inst->lastCLK) { if (D) { Q=1; _Q=0; } else { Q=0; _Q=1; } } } else if (SET & RESET) { Q=1; _Q=1; } else if (SET) { Q=1; _Q=0; } else if (RESET) { Q=0; _Q=1; } inst->lastCLK = CLK; inst->lastSET = SET; inst->lastRESET = RESET; } extern "C" __declspec(dllexport) double MaxExtStepSize(struct sD_FLOP_CD4013B *inst) { return 1e308; // implement a good choice of max timestep size that depends on struct sD_FLOP_CD4013B } extern "C" __declspec(dllexport) void Trunc(struct sD_FLOP_CD4013B *inst, double t, union uData *data, double *timestep) { // limit the timestep to a tolerance if the circuit causes a change in struct sD_FLOP_CD4013B const double ttol = 1e-12; // 1ns default tolerance if(*timestep > ttol) { struct sD_FLOP_CD4013B tmp = *inst; d_flop_cd4013b(&(&tmp), t, data); // if(tmp != *inst) // implement a meaningful way to detect if the state has changed // *timestep = ttol; if(tmp.lastCLK != inst->lastCLK & !inst->lastCLK) // CLK rising edge - force to reduce timestep *timestep = ttol; if((tmp.lastSET != inst->lastSET) | (tmp.lastRESET != inst->lastRESET)) *timestep = ttol; } } extern "C" __declspec(dllexport) void Destroy(struct sD_FLOP_CD4013B *inst) { free(inst); free(inst); }