diff --git a/src/spicelib/devices/csw/cswacld.c b/src/spicelib/devices/csw/cswacld.c index e9e21fada..a1bf492e6 100644 --- a/src/spicelib/devices/csw/cswacld.c +++ b/src/spicelib/devices/csw/cswacld.c @@ -23,7 +23,7 @@ CSWacLoad(GENmodel *inModel, CKTcircuit *ckt) current_state = (int) ckt->CKTstate0[here->CSWswitchstate]; - g_now = current_state ? model->CSWonConduct : model->CSWoffConduct; + g_now = (current_state == REALLY_ON || current_state == HYST_ON) ? model->CSWonConduct : model->CSWoffConduct; *(here->CSWposPosPtr) += g_now; *(here->CSWposNegPtr) -= g_now; diff --git a/src/spicelib/devices/csw/cswdefs.h b/src/spicelib/devices/csw/cswdefs.h index 0b1a214c8..5401232ef 100644 --- a/src/spicelib/devices/csw/cswdefs.h +++ b/src/spicelib/devices/csw/cswdefs.h @@ -84,6 +84,13 @@ typedef struct sCSWmodel { /* model structure for a switch */ unsigned CSWhystGiven : 1; /* flag to indicate hysteresis volt was given */ } CSWmodel; +enum { + REALLY_OFF = 0, // switch definitely off + REALLY_ON = 1, // switch definitely on + HYST_OFF = 2, // switch is off and in the hysteresis region + HYST_ON = 3, // switch is on and in the hysteresis region +}; + /* device parameters */ #define CSW_CONTROL 1 #define CSW_IC_ON 2 diff --git a/src/spicelib/devices/csw/cswload.c b/src/spicelib/devices/csw/cswload.c index d3a232176..c63594ff1 100644 --- a/src/spicelib/devices/csw/cswload.c +++ b/src/spicelib/devices/csw/cswload.c @@ -22,10 +22,6 @@ CSWload(GENmodel *inModel, CKTcircuit *ckt) double i_ctrl; double previous_state = -1; double current_state = -1, old_current_state = -1; - double REALLY_OFF = 0, REALLY_ON = 1; - /* switch is on or off, not in hysteresis region. */ - double HYST_OFF = 2, HYST_ON = 3; - /* switch is on or off while control value is in hysteresis region. */ for (; model; model = CSWnextModel(model)) for (here = CSWinstances(model); here; here = CSWnextInstance(here)) { @@ -81,7 +77,7 @@ CSWload(GENmodel *inModel, CKTcircuit *ckt) internalerror("bad value for previous region in swload"); } - if (current_state != old_current_state) { + if ((current_state == REALLY_ON || current_state == HYST_ON) != (old_current_state == REALLY_ON || old_current_state == HYST_ON)) { ckt->CKTnoncon++; /* ensure one more iteration */ ckt->CKTtroubleElt = (GENinstance *) here; } diff --git a/src/spicelib/devices/csw/cswnoise.c b/src/spicelib/devices/csw/cswnoise.c index 732383c9a..62e31b0d1 100644 --- a/src/spicelib/devices/csw/cswnoise.c +++ b/src/spicelib/devices/csw/cswnoise.c @@ -64,7 +64,7 @@ CSWnoise(int mode, int operation, GENmodel *genmodel, CKTcircuit *ckt, Ndata *da current_state = (int) ckt->CKTstate0[inst->CSWswitchstate]; NevalSrc(&noizDens, &lnNdens, ckt, THERMNOISE, inst->CSWposNode, inst->CSWnegNode, - current_state ? model->CSWonConduct : model->CSWoffConduct); + (current_state == REALLY_ON || current_state == HYST_ON) ? model->CSWonConduct : model->CSWoffConduct); *OnDens += noizDens; diff --git a/src/spicelib/devices/csw/cswpzld.c b/src/spicelib/devices/csw/cswpzld.c index 065cefec0..9beb03f27 100644 --- a/src/spicelib/devices/csw/cswpzld.c +++ b/src/spicelib/devices/csw/cswpzld.c @@ -26,7 +26,7 @@ CSWpzLoad(GENmodel *inModel, CKTcircuit *ckt, SPcomplex *s) current_state = (int) ckt->CKTstate0[here->CSWswitchstate]; - g_now = current_state ? model->CSWonConduct : model->CSWoffConduct; + g_now = (current_state == REALLY_ON || current_state == HYST_ON) ? model->CSWonConduct : model->CSWoffConduct; *(here->CSWposPosPtr) += g_now; *(here->CSWposNegPtr) -= g_now; diff --git a/src/spicelib/devices/csw/cswtrunc.c b/src/spicelib/devices/csw/cswtrunc.c index 972dd9c81..8dd199c5d 100644 --- a/src/spicelib/devices/csw/cswtrunc.c +++ b/src/spicelib/devices/csw/cswtrunc.c @@ -25,7 +25,7 @@ CSWtrunc(GENmodel *inModel, CKTcircuit *ckt, double *timeStep) lastChange = ckt->CKTstate0[here->CSWctrlvalue] - ckt->CKTstate1[here->CSWctrlvalue]; - if (ckt->CKTstate0[here->CSWswitchstate] == 0) { + if (ckt->CKTstate0[here->CSWswitchstate] == REALLY_OFF || ckt->CKTstate0[here->CSWswitchstate] == HYST_OFF) { ref = model->CSWiThreshold + fabs(model->CSWiHysteresis); if (ckt->CKTstate0[here->CSWctrlvalue] < ref && lastChange > 0) { maxChange = diff --git a/src/spicelib/devices/sw/swacload.c b/src/spicelib/devices/sw/swacload.c index e8b2c03d7..f0ecf519d 100644 --- a/src/spicelib/devices/sw/swacload.c +++ b/src/spicelib/devices/sw/swacload.c @@ -25,7 +25,7 @@ SWacLoad(GENmodel *inModel, CKTcircuit *ckt) current_state = (int) ckt->CKTstate0[here->SWswitchstate]; - g_now = current_state ? model->SWonConduct : model->SWoffConduct; + g_now = (current_state == REALLY_ON || current_state == HYST_ON) ? model->SWonConduct : model->SWoffConduct; *(here->SWposPosPtr) += g_now; *(here->SWposNegPtr) -= g_now; diff --git a/src/spicelib/devices/sw/swdefs.h b/src/spicelib/devices/sw/swdefs.h index 981abe1e6..fdc9386ce 100644 --- a/src/spicelib/devices/sw/swdefs.h +++ b/src/spicelib/devices/sw/swdefs.h @@ -83,6 +83,13 @@ typedef struct sSWmodel { /* model structure for a switch */ unsigned SWhystGiven : 1; /* flag to indicate hysteresis volt was given */ } SWmodel; +enum { + REALLY_OFF = 0, // switch definitely off + REALLY_ON = 1, // switch definitely on + HYST_OFF = 2, // switch is off and in the hysteresis region + HYST_ON = 3, // switch is on and in the hysteresis region +}; + /* device parameters */ #define SW_IC_ON 1 #define SW_IC_OFF 2 diff --git a/src/spicelib/devices/sw/swload.c b/src/spicelib/devices/sw/swload.c index 5067898bc..0ac3e87b7 100644 --- a/src/spicelib/devices/sw/swload.c +++ b/src/spicelib/devices/sw/swload.c @@ -23,8 +23,6 @@ SWload(GENmodel *inModel, CKTcircuit *ckt) double previous_state = -1; double current_state = -1; double old_current_state = -1; - double REALLY_OFF = 0, REALLY_ON = 1; // switch is on or off, not in hysteresis region. - double HYST_OFF = 2, HYST_ON = 3; // switch is on or off while control value is in hysteresis region. // double previous_region = -1; // double current_region = -1; @@ -83,7 +81,7 @@ SWload(GENmodel *inModel, CKTcircuit *ckt) internalerror("bad value for previous state in swload"); } - if (current_state != old_current_state) { + if ((current_state == REALLY_ON || current_state == HYST_ON) != (old_current_state == REALLY_ON || old_current_state == HYST_ON)) { ckt->CKTnoncon++; /* ensure one more iteration */ ckt->CKTtroubleElt = (GENinstance *) here; } diff --git a/src/spicelib/devices/sw/swnoise.c b/src/spicelib/devices/sw/swnoise.c index ed0ce79c6..9fe7eade8 100644 --- a/src/spicelib/devices/sw/swnoise.c +++ b/src/spicelib/devices/sw/swnoise.c @@ -64,7 +64,7 @@ SWnoise(int mode, int operation, GENmodel *genmodel, CKTcircuit *ckt, Ndata *dat current_state = (int) ckt->CKTstate0[inst->SWswitchstate]; NevalSrc(&noizDens, &lnNdens, ckt, THERMNOISE, inst->SWposNode, inst->SWnegNode, - current_state ? model->SWonConduct : model->SWoffConduct); + (current_state == REALLY_ON || current_state == HYST_ON) ? model->SWonConduct : model->SWoffConduct); *OnDens += noizDens; diff --git a/src/spicelib/devices/sw/swpzload.c b/src/spicelib/devices/sw/swpzload.c index ff23edb16..202da5925 100644 --- a/src/spicelib/devices/sw/swpzload.c +++ b/src/spicelib/devices/sw/swpzload.c @@ -28,7 +28,7 @@ SWpzLoad(GENmodel *inModel, CKTcircuit *ckt, SPcomplex *s) current_state = (int) ckt->CKTstate0[here->SWswitchstate]; - g_now = current_state ? model->SWonConduct : model->SWoffConduct; + g_now = (current_state == REALLY_ON || current_state == HYST_ON) ? model->SWonConduct : model->SWoffConduct; *(here->SWposPosPtr) += g_now; *(here->SWposNegPtr) -= g_now; diff --git a/src/spicelib/devices/sw/swtrunc.c b/src/spicelib/devices/sw/swtrunc.c index d179f0e4c..cb61318ae 100644 --- a/src/spicelib/devices/sw/swtrunc.c +++ b/src/spicelib/devices/sw/swtrunc.c @@ -23,7 +23,7 @@ SWtrunc(GENmodel *inModel, CKTcircuit *ckt, double *timeStep) lastChange = ckt->CKTstate0[here->SWctrlvalue] - ckt->CKTstate1[here->SWctrlvalue]; - if (ckt->CKTstate0[here->SWswitchstate] == 0) { + if (ckt->CKTstate0[here->SWswitchstate] == REALLY_OFF || ckt->CKTstate0[here->SWswitchstate] == HYST_OFF) { ref = model->SWvThreshold + fabs(model->SWvHysteresis); if (ckt->CKTstate0[here->SWctrlvalue] < ref && lastChange > 0) { maxChange =