Logical Functions

About the writer: Harvey Morehouse is a contractor/consultant with many years of experience using circuit analysis programs. His primary activities are in Reliability, Safety, Testability and Circuit Analysis. He may be reached at harvey.annie@verizon.net. Simple questions for which I know the answer are free. Complex questions, especially where I am ignorant of the answers, are costly!!!

Summary: Although not as convenient as in some SPICE implementations, it is possible to implement Boolean algebraic expressions in B2 Spice as part of the non-linear voltage or current source or otherwise. Some methods of doing this are presented here.

Non-linear source implementation:

There is a unit step function which may be used to implement an 'IF…THEN…' function. There is unfortunately an ambiguity in this function, similar to the indeterminate results one gets with digital logic. This can be overcome with a little work.

Specifically, the ustep function returns a logical 'ONE' if its argument is greater than zero, logic 'ZERO' if the argument is less than zero, and an indeterminate if the argument is equal to zero. Thus, one could add an expression such as the following to a non-linear source:

    V = u(v(2,0))

This would produce ALMOST what we expect. However, the source must be bipolar (negative and positive going) otherwise the result will ALWAYS be a logic 'ONE'. However, if we are interested in a logical comparison of the signal to a level, we could write the expression as:

    V = u(v(2,0)-K)

This produces (almost) a greater than function. If the parameter v(2,0) is unipolar, and it is desired to return a logic ZERO when v(2,0) is equal to zero, then K becomes an arbitrarily small number. This value may then be used as a multiplier to provide an IF..Then function (almost). Assume that v(2,0) is unipolar. Then K = K1 if we wish to get a logic ONE when it is (essentially) greater than zero, where K1 is arbitrarily small.

We then may get an AND function by multiplying together two ustep functions, as in:

    V = u(v(2,0)*v(5,4))

Similarly summing the signals produces an OR function. A negate is produced by reversing the nodes of a source, or by use of a '-' operator.

A simple test circuit was produced to examine these functions. The circuit is shown in Figure 1 following:


Figure 1
Logic circuits (first pass)

The netlist for this test circuit is:

 

Circuit2
************************
* B2 Spice
************************
* B2 Spice default format (same as Berkeley Spice 3F format)

***** main circuit
V1 LA 0 DC 0 PULSE( -.0001 1 0 1u 1u 1m 2m)
V2 LB 0 DC 0 PULSE( -.00001 1 .51m 1u 1u 1m 2m)
R1 LA 0 1K
R2 LB 0 1K
B1 AND 0 v=(u(v(LA))*u(v(LB)))*3
B2 OR 0 v=u((u(v(LA))+u(v(LB)))-.000001)*3
B3 AnotB 0 v=u((u(v(LA))*u(-v(LB)))-.0001)*3
B4 notAnotB 0 v=u(-(u(v(LA))+u(v(LB))-.000001))*3

.OPTIONS gmin = 1E-12 reltol = 1E-4 itl1 = 500 itl4 = 500
+ rshunt = 1G
.TRAN 10u 10m 0 1u uic
.IC
.END

 

One can examine the outputs of this circuit by performing a transient analysis.


Figure 2
Transient test of Circuit 1

It should be noted that these signals are 'well behaved', that is to say, they do not dwell at a zero level when the constant is part of the expression or not. Note that for the sources B2, B3 and B4 a unit step of the inner step functions is performed. The reason for this is that those functions WILL produce an indeterminate logic state. Consequently, as this logic state is between a ZERO and a ONE logic state, it can be cleared by subtracting a K1 value and performing a unit step on that output. (Because the logic functions are 'well-behaved' this step was not needed for the 'AND', however in general it would be.)

So, in using the nonlinear source for logical functions, construct the ustep functions using a constant in all cases, either as a comparison or to convert a unipolar signal to a bipolar one.

Combine the ustep functions together as products or sums. Perform another ustep of these functions with a value of K1 sufficient to make the inner ustep function bipolar.

Then, multiply this function by the desired 'THEN' value.

Now if one needs an 'ELSE' condition, one may negate the 'IF' expression, multiply this by the 'OR' result, and then sum the two products together (making sure that the required constant values and ustep of the inner ustep terms is performed. It is not 'clean', but it will work.

One may wish to use several non-linear sources as intermediate elements to keep one's sanity and to aid debugging.

Logical Switch Implementation:

A second approach is to model the logic functions with switches. This has the advantage that the logical ambiguity is NOT present at the switch outputs, although it is at the switch inputs. However the switch toggle levels and hysteresis is controllable in the switch elements. The following shows an IF..THEN implantation of switches.


Figure 3
Switch Logic implementation

In this circuit, the nonlinear generator output is present at R1 if SA AND SB, OR SC AND SD ARE ACTUATED. With levels A, B, C, and D controlling the respective switches, the logic is:

    IF ((A AND NOTB) OR (C AND D)) THEN V(B1) ELSE ZERO

The circuit is so self evident no netlist is required. In this case, if a control voltage were unipolar, then the trip level would be set to some arbitrarily low level. The switches should have a very low 'ON' resistance compared to R1, with a very large 'OFF' resistance compared to R1 as well.

An ELSE condition can be arrived at several ways, but one is shown in Figure 4 following:


Figure 4
IF ..Then..Else circuit

In the above, a voltage across R1 is asserted to control S1 and S2. S1 and S2 have identical conditions. The logic then becomes, IF ((A OR NOTB) AND C AND NOTD) THEN B1 ELSE B2.

Logical gate implementation:

It should be self evident how to construct such logic with gates, comparators and so on. However, the use of other than Xspice devices should be avoided wherever possible UNLESS one is modeling a device where such items as delay times, rise and fall times and so on are required.

Looking Ahead:

It has been established that one CAN implement logical functions into non-linear sources or by switches or gates implement such functions. So what?

One MAJOR use for this is in the modeling of Switching Mode Power Supplies (SMPS). Simple, fast computing models and techniques have been created which allow the effects of a variable duty cycle control to occur, while enabling an operating point to be established. Within a given switch cycle, one state occurs while the switching transistor in 'ON' and another while it is off. And, there is a third state which can be modeled; however this shall not be described here. The point is, that these conditions can best be modeled by logical equations rather than by circuitry with many state changes occurring rapidly, all the while performing extensive analog computations.

Rapid, yet realistic modeling of SMPS control IC's as well as the switching elements that allows stability determinations to be made is what logical equation capability brings to the table. While this is NOT a total replacement for detailed device modeling in some instances, it can produce results and insights into SMPS operation impossible in any other manner.

References:

No specific references are given, as the ideas, though not original to me, have been discussed and used for several years. One could not however discuss SMPS modeling without mentioning the pioneering work of Middlebrook, Ridley, Sandler, Bello and many others. Not mentioning any of them would be a crime, yet mentioning all of them and their contributions would be a major paper in itself. SMPS analysts are in debt to dozens of pioneers.


 

 

 

 


Beige Bag Software, Inc., 623 W. Huron, Suite 2, Ann Arbor, MI, 48103
(phone) 734.332.0487 (fax) 734.332.0392 (email) info@beigebag.com
© 2005-2008 Beige Bag Software, Inc