### B2 Spice V5 Logical Expressions

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 regarding my articles for which I know the answer are free. Complex questions, especially where I am ignorant of the answers, are costly!!!

Summary: B2 Spice v5 has incorporated a logical expression feature. While there are a few problems still remaining in the implementation, or lack thereof, these can be overcome without much difficulty. And hopefully the remaining problems/lacks will soon be remedied.

Logical Expressions:

Logical expressions are equations/expressions where the result/outputs are binary in nature. That is to say, the result after evaluation of the expression/equation will be bipolar in nature, such as one value or another, or one function/expression will be create an output or a second one will.

By combining these logical expressions, one can create complex functions, where, dependent of the results of variables or expressions taking on different values, different outputs may be provided.

Where is this useful? Well, consider a voltage comparator. Using normal SPICE type elements, especially differential amplifiers, this can involve hundreds of transistors, and require large amounts of time to compute the answer. And, the result will essentially be binary in nature. The same result can be achieved (albeit it without some device effects due to parasitics and non-ideal elements) in a much shorter computational time using logical expressions.

However, if the comparator responds rapidly compared to the remainder of the circuit, the non-ideal effects of the comparator become negligible in most regards and the idealized implementation can be useful.

In the earlier articles, both in the Logical Expressions and in other articles, this was gone into in some length and many (hopefully) models and functions were created.

Logical Expression Primitives:

To be really useful the instruction set should include several items such as operators as well as expressions. Some of the operators are unary. Meaning, they are effective with just one operand. This would be the case for a logical negate. Others are useful with two operands. Such would be the case for a logical AND.

Some of the operators that a full-featured SPICE with a logical expression format would include are shown in the following:

 Type Possible Symbol Provided Alternate Comments Logical Negate '/' or '`' no (1- lval) If the variable is logical with just unity and zero possible values, subtracting this variable from unity will be the negated value. Logical AND '&' no * Multiplication of two logical values will only produce a unity value when both (or all) are true. Logical OR '|' no See comments A second operation is required to produce this function. Add the logical values to be 'or'ed and test to produce a unity value when their sum is non-zero. Equal '==' no See comments This operator takes logical, integer or real arguments. If the logical, integer or real arguments are equal, a logical one output is produced. Alternatives include subtracting the arguments and testing for a non-zero value. GTR '>' yes See comments This operator for integer and real values would compare the operand to the left of the '>' with that to the right, and produce a unity output if the left were greater, otherwise returning a zero. LT '<' yes See comments This operator produces the logical inverse of the GTR function. XOR '||' no See comments This operator would produce an EXCLUSIVE OR of the two LOGICAL arguments, returning a unity value when just one of the two arguments is true, and zero otherwise.

Table 1
Possible Logical Operators

Some other logical operators could be defined. But as you can see, none of these operators are directly provided. Also, the 'logical' values should be usable as real integers as well. That is to say, a function should be capable of being 'switched on' and 'switched off' by means of multiplying it with a logical variable.

Thus, if A is a logical variable, then the function, V = A*f(t) is equal to the value of f(t) when A is true, and zero otherwise.

In summary, there are few logical operators provided as yet in Version 5. So for the moment, it would seem nothing has changed from Ver 4. To implement logical operators one is forced to use the Ver 4 method, with ustep and uramp operators. So nothing is new? Wrong. The next section will detail what is new in Version 5.

Logical Functions:

What is new and very powerful in Version 5 is the incorporation of the logical "IF … THAN …. ELSE …" function into the instruction set.

Recalling from the previous articles on logical functions, I devised a method to use
the uramp() and ustep() functions with the nonlinear function generators to produce logical expressions. As an example, if we wished to create a function which returned v2(t) when v1(t) was greater than zero, and v3(t) when it was not, it could be done in a somewhat contorted manner, using a nonlinear voltage source as:

V = u(v1(t))*(v2(t) - v3(t)) + v3(t)

Recalling, the u() function returns a '1' when the argument is greater than zero, and zero otherwise. Thus, when v1(t) > 0, u(v1(t)) =1 and the equation becomes:

V = 1*(v2(t) - v3(t)) + v3(t) = v2(t)

Whereas, if v1(t) were equal to or less that zero, the result is:

V = 0*(v2(t) - v3(t)) + v3(t) = v3(t)

Now, what Version 5 does is precisely the same thing, but the syntax is cleaned up. Using a different syntax, the same functions have been used to produce the output. A ustep function is still used, but it becomes invisible to the user.

There are actually two different ways the "IF … THAN …. ELSE …" function has been incorporated into the instruction set. The reasons why are for another reason I will not reveal here, but the two means are described in the following.

Method 1:

v = IF ( test, THEN_true_test_output, ELSE_false test output)

The preceding equation should be rather easy to understand. Within the brackets are three items, separated by commas. The first is the actual test expression. IF this expression results in a true value, the second expression is the voltage that 'v' will present as an output. If the test presents a false output, then the third expression is the voltage output. This is best seen by an illustration. Figure 1 following is a small test circuit.

Figure 1
Method 1 test ckt 1

A netlist for the circuit follows:

if_then_else.cp~

***** main circuit
B1 1 0 v = IF ( v(3)<1, 0, 1)
R1 3 4 1K
V1 3 0 SIN( 0.000000000000e+000 5.000000000000e+000 1.000000000000e+006 0.000000000000e+000 0.000000000000e+000)
R2 4 0 1K
R3 1 0 1K

.TRAN 1E-11 1E-7 0 1E-11 uic

.end

The leftmost generator V1, is a sine wave source with an amplitude of 5V peak, centered about zero volts. B1 generator is used to create a logical output voltage. The requirement for B1 is that, IF the voltage output of generator V1 is less than one volt, that the B1 output will be zero volts. And if the output of generator V1 is equal to greater than 1 volt, then the B1 generator will be 1 volt. (Note that the output probe for generator V1 is voltage V2, and the output probe for generator B1 is V3. Whereas in the equation for generator B1, the node voltage at node 3 with respect to node 0 is understood.)

An output graph showing the input voltage v2 and the output voltage v3 is shown in Figure 2 following:

Figure 2
Method 1 test ckt 1 graph

Here we see that when the green input voltage at v2 is less than 1v, the output is zero, and 1 volt otherwise. Now the test was:

B1 1 0 v = IF ( v(3)<1, 0, 1)

This could have been performed in nearly the same manner with a B1 source equation of:

B1 1 0 v = IF ( v(3)>1, 1, 0)

This would for all practical purposes be the same circuit, IF the voltage generator V1 did not dwell at zero. For, the first equation is equivalent to:

B1 1 0 v = IF ( v(3)>=1, 1, 0)

But there is no >= operator, so they are NOT precisely the same even were that operator available.

The point that should be remembered is that for real variables the use of an ‘=’ operator for a test, other than was done through inclusion in an else condition by implication, is problematic at best. Remembering that SPICE produces a numerical output, at solution times that might not EVER be precisely right to produce a unitary value, let alone have two real solution values both meet at the same time, it is best NEVER to use an equality test save for logical values.

Now of course some care must always be used in creating tests, however I believe it is clear that the logical expressions are far, far easier to produce and understand than the ustep method.

The next question that arises is, can these expressions be nested? Suppose we wished to create a voltage under the conditions that:

IF v1 > 1 then v2 else (if v3>v4 then v5)

The answer right now is no, you cannot nest these conditions. Now they will work, sorta, but NOT as intended. This is on the list to be fixed. To do this at this time you will have to resort to using several B type generators. Or, one could do this by including ustep functions within the IF THEN ELSE expressions. For example, suppose one wished to get an output of 1 for the conditions that v1 was greater than 1 volt AND less that 2v.
The output will be zero otherwise.

The expression could be:

V = IF (u(v1-1)*u(2-v1),1,0)

The test is the product of the two ustep functions. The product will be true only if both ustep functions return a 1. This will occur if both (v1>1) and (2>v1). To get more information on using the ustep (and uramp) functions please refer to earlier articles about logical expressions.

It should be noted, if there are doubts, that the true and false conditions can themselves be real voltages) or currents as the case may be. I have not tested all the conditions where this function will work, but so far there seems to be no reason why the ustep function could not itself be used in the conditional outputs if desired. If in doubt, construct a simple test circuit as I have done.

Method 2:

The second method of creating a logical IF… THEN …. ELSE …. Expression uses the syntax:

v = <exp> ? true : false

Other than the slight format difference, it should be evident that there is very little difference between methods. A slightly more complicated circuit was used to test this expression, as shown in the following schematic:

Figure 3
Method 2 test ckt 1

V1 is a 1k sine source, and V2 is a 1.333K source, both with a 2V amplitude. The B1 and B2 nonlinear voltage sources are a little more complex than in the previous circuit, so before proceeding we will show the netlist as follows:

v5 logic test 1.cpr

***** main circuit
V1 VP1 0 SIN( 0.000000000000e+000 2.000000000000e+000 1.000000000000e+003 0.000000000000e+000 0.000000000000e+000)
V2 VP2 0 SIN( 0.000000000000e+000 2.000000000000e+000 1.333000000000e+003 0.000000000000e+000 0.000000000000e+000)
R1 VP1 0 1K
R2 VP2 0 1K
R3 VP3 0 1K
R4 VP4 0 1K
B1 VP3 0 V = (v(vp1)>0) ? v(vp2) :0
B2 VP4 0 V = v(vp2)>1 ? u(v(vp1)*v(vp2)) : 0

.TRAN 1E-5 0.0025 0 1E-6 uic

.end

Here we have two nonlinear generators, B1 and B2, showing some of the possible operations. Looking first at B1, its equation is:

B1 VP3 0 V = (v(vp1)>0) ? v(vp2) :0

The test is for V1 source being positive. If this is so, then the output will be the vp2 voltage, otherwise it will be zero. In effect, the output at vp3 is the vp2 voltage ‘chopped’ by vp1 when it is non-zero. This is seen in the following graph:

Figure 4
Method 2 test ckt 1 graph 1

The highlighted trace shows that when vp1 voltage is greater than zero, the B1 generator outputs vp2 voltage, and zero volts at other times.

Following is the vp4 output expression:

B2 VP4 0 V = v(vp2)>1 ? u(v(vp1)*v(vp2)) : 0

Here, the test is the v2 generator output voltage at vp2 being greater than one volt. If that is so, then a logical level is output when the product of voltages vp1 and vp2 is greater than zero, and zero is output otherwise. (Note that the test constrains the output to the time when vp2 is greater than positive 1V.) The output with the B2 generator trace highlighted is shown in the following.

Figure 5
Method 2 test ckt 1 graph 2

Here, the violet trace, vp4 voltage, shows that when the red trace (vp1 or v1 generator output) is greater than 1V, the vp4 output of the B2 generator is a logical one when the product of v1 and v2 is greater than zero, and zero otherwise.

More examples could be created showing how to use this capability in modeling logical expressions, however in view of the several articles written for use of the Version 4 capabilities, it should be easy enough to see how this could be used to simplify those devices modeled. It should also be noted that there is no real advantage, other than perhaps clarity, of revising those models as the underlying method in the implementation of the simplified notation is unchanged, namely ustep and uramp functions.

Summary:

Although there are some features lacking in the enhanced logical equation capability in Version 5, nonetheless it represents a SIGNIFICANT improvement in the ease of implementation of logical expressions, consistent with that used by competing products in most cases. There are now only very few things that can done in products costing perhaps 20 times more than B2SPICE that cannot be conveniently done in Version 5.

Parenthetically I must add that before this I was reluctant to go from Version 4 to Version 5 of B2SPICE, as I felt that several problems in Version 5 were not worth the effort to work around, and Version 4 met all my needs nicely. However, with this new feature, as well as the addition of new features to come, in particular nesting of logical expressions, makes it now worthwhile to change.

Consequently, future articles, as the need arises and the mood occur, will be written in the Version 5 format.