// Automatically generated C++ file on Thu Feb 5 23:09:50 2026 // // To build with Digital Mars C++ Compiler: // // dmc -mn -WD -o my_first_block.cpp kernel32.lib #include 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 IN #undef OUT #undef CLK struct sMY_FIRST_BLOCK { // declare the structure here bool lastCLK; // Previous clock state for edge detection float Z1; // First delay element (y[n-1]) float Z2; // Second delay element (y[n-2]) }; extern "C" __declspec(dllexport) void my_first_block(struct sMY_FIRST_BLOCK **opaque, double t, union uData *data) { double IN = data[0].d; // input bool CLK = data[1].b; // input double &OUT = data[2].d; // output if(!*opaque) { *opaque = (struct sMY_FIRST_BLOCK *) malloc(sizeof(struct sMY_FIRST_BLOCK)); bzero(*opaque, sizeof(struct sMY_FIRST_BLOCK)); } struct sMY_FIRST_BLOCK *inst = *opaque; // Implement module evaluation code here: // IIR filter coefficients (hard-coded) double A0 = -0.5; double A1 = 0.9; // Process on clock edge if (CLK & CLK != inst->lastCLK){ // 2nd-order IIR filter difference equation: y[n] = x[n] - a1*y[n-1] - a2*y[n-2] OUT = IN - inst->Z1 * A0 - inst->Z2 * A1; // Update delay line: shift previous outputs inst->Z2 = inst->Z1; // Move y[n-1] to y[n-2] inst->Z1 = OUT; // Store current output as y[n-1] } // Store current clock state for next edge detection inst->lastCLK = CLK; } extern "C" __declspec(dllexport) void Destroy(struct sMY_FIRST_BLOCK *inst) { free(inst); }