diff --git a/src/include/ngspice/mifcmdat.h b/src/include/ngspice/mifcmdat.h index 389cd273c..330831d6e 100644 --- a/src/include/ngspice/mifcmdat.h +++ b/src/include/ngspice/mifcmdat.h @@ -366,6 +366,7 @@ struct Mif_Private { Mif_Param_Data_t **param; /* Information about each parameter */ int num_inst_var; /* Number of instance variables */ Mif_Inst_Var_Data_t **inst_var; /* Information about each inst variable */ + Mif_Callback_t *callback; /* Callback function */ }; diff --git a/src/include/ngspice/mifdefs.h b/src/include/ngspice/mifdefs.h index a961e2151..ae472d91b 100644 --- a/src/include/ngspice/mifdefs.h +++ b/src/include/ngspice/mifdefs.h @@ -79,6 +79,7 @@ struct MIFinstance { Mif_Boolean_t event_driven; /* true if this inst is event-driven or hybrid type */ int inst_index; /* Index into inst_table in evt struct in ckt */ + Mif_Callback_t callback; /* instance callback function */ }; diff --git a/src/include/ngspice/miftypes.h b/src/include/ngspice/miftypes.h index 897966c1f..b18663d9c 100644 --- a/src/include/ngspice/miftypes.h +++ b/src/include/ngspice/miftypes.h @@ -172,6 +172,15 @@ typedef enum { } Mif_Cntl_Src_Type_t; +/* + * The "reason" for a callback invocation + */ + +typedef enum { + MIF_CB_DESTROY = 1, /* MIFdestroy has been invoked, its time to clean up */ +} Mif_Callback_Reason_t; + + /* ***************************************************************************** */ @@ -218,5 +227,7 @@ typedef struct Mif_Private Mif_Private_t; typedef struct MIFinstance MIFinstance; typedef struct MIFmodel MIFmodel; +typedef void (* Mif_Callback_t)(Mif_Private_t *, Mif_Callback_Reason_t); + #endif diff --git a/src/xspice/cmpp/mod_lex.l b/src/xspice/cmpp/mod_lex.l index fe7678886..444a7d2ba 100644 --- a/src/xspice/cmpp/mod_lex.l +++ b/src/xspice/cmpp/mod_lex.l @@ -91,6 +91,7 @@ Z [0-9A-Za-z_] ARGS {return TOK_ARGS;} INIT {return TOK_INIT;} +CALLBACK {return TOK_CALLBACK;} ANALYSIS {return TOK_ANALYSIS;} NEW_TIMEPOINT {return TOK_NEW_TIMEPOINT;} CALL_TYPE {return TOK_CALL_TYPE;} diff --git a/src/xspice/cmpp/mod_yacc.y b/src/xspice/cmpp/mod_yacc.y index bf120cd92..8dcf8f5b0 100644 --- a/src/xspice/cmpp/mod_yacc.y +++ b/src/xspice/cmpp/mod_yacc.y @@ -336,6 +336,7 @@ static void append (char *str) %token TOK_ARGS %token TOK_INIT +%token TOK_CALLBACK %token TOK_ANALYSIS %token TOK_NEW_TIMEPOINT %token TOK_TIME @@ -426,6 +427,8 @@ c_char : TOK_IDENTIFIER {fputs (mod_yytext, mod_yyout);} macro : TOK_INIT {fprintf (mod_yyout, "mif_private->circuit.init");} + | TOK_CALLBACK + {fprintf (mod_yyout, "*(mif_private->callback)");} | TOK_ARGS {fprintf (mod_yyout, "Mif_Private_t *mif_private");} | TOK_ANALYSIS diff --git a/src/xspice/evt/evtload.c b/src/xspice/evt/evtload.c index ca6b718b6..b48b229a9 100644 --- a/src/xspice/evt/evtload.c +++ b/src/xspice/evt/evtload.c @@ -225,6 +225,7 @@ int EVTload( cm_data.param = inst->param; cm_data.num_inst_var = inst->num_inst_var; cm_data.inst_var = inst->inst_var; + cm_data.callback = &(inst->callback); /* ******************* */ diff --git a/src/xspice/mif/mif_inp2.c b/src/xspice/mif/mif_inp2.c index 936b14659..de788499e 100644 --- a/src/xspice/mif/mif_inp2.c +++ b/src/xspice/mif/mif_inp2.c @@ -652,6 +652,7 @@ static void MIFinit_inst( fast->analog = MIF_FALSE; fast->event_driven = MIF_FALSE; fast->inst_index = 0; + fast->callback = NULL; } diff --git a/src/xspice/mif/mifdelete.c b/src/xspice/mif/mifdelete.c index 8c62b7a55..8b145c211 100644 --- a/src/xspice/mif/mifdelete.c +++ b/src/xspice/mif/mifdelete.c @@ -89,6 +89,21 @@ MIFdelete( model = (MIFmodel *) inModel; fast = (MIFinstance **) inst; + if ((*fast)->callback) { + Mif_Private_t cm_data; + + /* Prepare the structure to be passed to the code model */ + cm_data.num_conn = (*fast)->num_conn; + cm_data.conn = (*fast)->conn; + cm_data.num_param = (*fast)->num_param; + cm_data.param = (*fast)->param; + cm_data.num_inst_var = (*fast)->num_inst_var; + cm_data.inst_var = (*fast)->inst_var; + cm_data.callback = &((*fast)->callback); + + (*fast)->callback(&cm_data, MIF_CB_DESTROY); + } + /*******************************************/ /* Cut the instance out of the linked list */ /*******************************************/ diff --git a/src/xspice/mif/mifload.c b/src/xspice/mif/mifload.c index b1901fce3..3ceebe0d9 100644 --- a/src/xspice/mif/mifload.c +++ b/src/xspice/mif/mifload.c @@ -437,6 +437,7 @@ MIFload( cm_data.param = here->param; cm_data.num_inst_var = here->num_inst_var; cm_data.inst_var = here->inst_var; + cm_data.callback = &(here->callback); /* Initialize the auto_partial flag to false */ g_mif_info.auto_partial.local = MIF_FALSE;