DLL with lookup table won't compile for Simple behavioral buck converter

I am trying to make a simple behavioral block (C++) for a buck converter. I want the Pin = Pout/eff, so external circuit elements will determine Vin and Iout, with Vout = 3.3 (a constant).

Error Message:
double interpolate(double Iout, double* Iout_table, double* eff_table, int table_size) {
^
buck_x1.cpp(45) : Error: expected data def of ‘buck_x1::interpolate’, not func def
void main(double IN, double OUT) {
^
buck_x1.cpp(59) : Error: expected data def of ‘buck_x1::main’, not func def
double eff = interpolate(Iout, Iout_table, eff_table, 4);
^
buck_x1.cpp(64) : Error: variable ‘Iout_table’ is not accessible from this function
annotate(“Pin”, Pin); // Input power
^
buck_x1.cpp(85) : Error: undefined identifier ‘annotate’
annotate(“Pout”, Pout); // Output power
^
buck_x1.cpp(86) : Error: undefined identifier ‘annotate’
Fatal error: too many errors
— errorlevel 1

My C++ Code:
// Automatically generated C++ file on Sat Oct 5 18:05:25 2024
//
// To build with Digital Mars C++ Compiler:
//
// dmc -mn -WD buck_x1.cpp kernel32.lib

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 DllMain entry point (Process.h) - Win32 apps | Microsoft Learn 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

extern “C” __declspec(dllexport) void buck_x1(void **opaque, double t, union uData *data)
{
double in = data[0].d; // input
double &out = data[1].d; // output

// Implement module evaluation code here:

// Global lookup tables for all instances
double Iout_table = {0.1, 0.2, 0.5, 1.0}; // Iout values (A)
double eff_table = {0.75, 0.80, 0.90, 0.75}; // Efficiency values (0-1)

// Interpolation function to get efficiency based on Iout
double interpolate(double Iout, double* Iout_table, double* eff_table, int table_size) {
if (Iout <= Iout_table[0]) return eff_table[0];
if (Iout >= Iout_table[table_size - 1]) return eff_table[table_size - 1];

for (int i = 0; i < table_size - 1; i++) {
    if (Iout >= Iout_table[i] && Iout <= Iout_table[i + 1]) {
        // Linear interpolation
        return eff_table[i] + (eff_table[i + 1] - eff_table[i]) *
                              (Iout - Iout_table[i]) / (Iout_table[i + 1] - Iout_table[i]);
    }
}
return 1.0;  // Default in case of an error

}

void main(double IN, double OUT) {
// OUT represents Iout (current load)
double Iout = OUT;

// Pass the lookup tables to the interpolate function
double eff = interpolate(Iout, Iout_table, eff_table, 4);

// Output voltage is fixed at 3.3V
double Vout = 3.3;

// Calculate output power
double Pout = Vout * Iout;

// Calculate input power based on efficiency
double Pin = Pout / eff;

// Input current (Iin) = Pin / VIN
double Iin = Pin / IN;

// Set output for Vout (which is fixed)
OUT = Vout;

// Set input current (Iin) for VIN
IN = Iin;

// Annotate schematic with power values
annotate("Pin", Pin);        // Input power
annotate("Pout", Pout);      // Output power
annotate("Pdiss", Pin - Pout); // Power dissipation

}

Hi, Robert.

First, a tip: When pasting code directly into a message, click on the little gear icon and select “Preformatted text” option. Otherwise, some of the code text gets converted into formatting codes and dropped from the text. Alternatively, attach the *.cpp file. (New users have to wait a few days before they can upload.)

From what I can see, you might misunderstand how C-Blocks work. There should not be a main() function – the DLL is loaded by QSpice and it calls various functions directly. QSpice calls the evaluation function for each simulation point calculation. In your code the evaluation function is:

I have a series of “C-Block Basics” documents on my QSpice GitHub repository that might clear things up a bit.

–robert

1 Like

thank you Robert. I will check it out!