Ternary computing: basics


I am working on a computer architecture principles lectures for our university; and as an assignment I’d like to propose to my students to build a simple programmable machine working in ternary. The main reason is fun: as a lecturer I must bring a bit of entertainment, otherwise I won’t be listened to. Besides, it is important for historic reasons. Any further «why?!» questions will be answered «Because I can».

This page describes the very basics, it won’t go beyond a simple ternary adder (and its hardware implementation). Stay tuned for more.

I chose the balanced ternary system: every trit represents one of three possible states, -1, 0 or 1. A very extensive description of this system may be found here.

77d6bc8bcbc8b3b0d12873b4a0d4490a.jpg


Abstraction


The only block that will be used in my computer is the ternary multiplexer.

It can be seen as a black box with five pins: the selector pin receives a ternary signal (either -1, 0 or 1), and then there is a little switch inside the box that connects the output pin with one of three input pins inN, inO or inP.

Usually it is depicted as shown here:

d8ab66f7ea82307d8743746c93d48623.png

Ternary demultiplexer works in the similar way: depending on the selector pin one input is connected with one of three possible output pins. Note that in my hardware I use CMOS analog switches. Those are bi-directional, so the hardware can be used both as multiplexer and demultiplexer. But anyways, for now I do not use demultiplexing possibilities.

Hardware implementation


The basic design was proposed by Shaos.
The only thing I did is to create SMD version of the TRIMUX. Surface mount dg403 switches can be bought for 50p per piece in China.

e31da86e53152c6e9898be596e3fbba4.png

Before I found this design I tried to use combinations of cd4007 and cd4016. It worked, but the hardware is cumbersome and ugly. DG403 switches provide true ternary computing possibilities without any redundancy like to use two-bit binary and ban one of four configurations.

One ternary multiplexer can be created with two DG403: one of the ICs receives -5V to 0V power input, while the other one is powered with 0V to 5V, and it allows use ternary signals represented by three tensions -5V, 0V and 5V, respectively. This design uses only half of dg403 pins, therefore it is natural to create a board with two multiplexers.

c5258e0890865b1f512b060f722752e3.png

Do not hesitate to contact Shaos here. Amazing guy, instead of speculating about benefits of the ternary, he designed and fabricated his own ternary ICs!

242bcbadae0d579ab29fdc99aec9b6d0.jpg


Let us omit the identity function that we can obtain by giving -1,0 and 1 to corresponding input pins of the multiplexer.

For the beginnning let us increment input signal A by computing A+1 (of course, in the -1,0,1 ring):

c1481154b39791b35c346651473945e4.png


Here is the way we can decrement the input:

9ab0f4d3ea1ec50b27280e86162e6dc9.png


Let us compute max (A,0):

f36fb235a0dd4cc59b40c1a795f0470f.png


And the min (A,0):

c4d643a59ccf8ad3aefd2381bdccadce.png


A+B


So, one multiplexer allows us to compute any unary function. To compute a function of two arguments we need to use three or four multiplexers. For example, if we want to compute a sum of two signals A and B (still in the ring -1,0,1), then we can use this schema:

d2ab8848d8fcef2b80979473136585fd.png

There are two layers of multiplexers: the first one computes unary functions of A, and the second layer combines them in accordance with B.

Consensus


If we want to compute consensus function of two ternary signals (consensus equals -1 if A=B=-1, equals 1 if A=B=1 and is zero otherwise), then we can do it like this:

a8d7de2ed7955d36a8f7c683d3b1fe75.png

Hardware implementation


In fact, we just created a half-adder. For two inputs A and B it computes two outputs S and C related as A + B = S + 3*C. S is the sum and C is the carry flag.

Let us test the design! Red LED means -1, off means 0, green LED means 1. Thus, this photo tells us that S=-1, C=1, or, in other words, 1+1 = -1 + 3×1:

305c31ff8e795c7618bf39c0222060aa.jpg

Here is the table that lists all nine possible states of our half-adder. Each cell gives corresponding values for S and C. A link to a photo is provided for each cell.

S, C B
-1 0 1
A -1 1,-1 -1,0 0,0
0 -1,0 0,0 1,0
1 0,0 1,0 -1,1


In the contrast with the half-adder, a full-adder receives three inputs A, B, Cin and computes two outputs S and Cout related as A + B + Cin = S + 3*Cout.

Sum of three trits


If we want to compute a sum of A+B+Cin, the idea is the same as before: we use layer-by-layer preparation of inputs. The first layer receives A as an input, the second uses B and the last single-multiplexer layer uses Cin. Here is one possible way to compute the output S:

aaa1e7dd638daeb4df917d7c2afa4de7.png

Note that when Cin=0, it behaves exactly in the same manner as the half-adder, therefore it is natural to see the inclusion (highlighted in green) of the half-adder schematics in the full-adder.

Overflow (carry flag)


The overflow trit can be computed in the same layer-by-layer way. It is also includes the half-adder!

73925a557a45b77f0f626b5b99d6de9a.png

Hardware validation


This time I was too lazy to use breadboards to test the full adder, so I made this one layer PCB:

d145585f9dbf1bb2f9764051dd7ee61b.png

Here it is after the copper was etched:

69aaa5283e303703df8b31787481ca59.jpg

Three following tables list all 27 possible states of the full-adder. As before, the photos are available for each state. Note that the middle table (Cin=0) is exactly the same as for the half-adder.


The advantage of the full adder with respect to the half-adder is the possibility to stack multiple adders until we get enough trits to represent the number we want.

Here is a stack of two adders:

69aaa5283e303703df8b31787481ca59.jpg

And here how it solves -4 + 2 (the least significant trit is on the left):
a87d1f5371c93de26f8e5fdf0b64f6f0.jpg

Of course, we do not need a full adder board for the least significant trit (we do not have carry flag for the input of the least significant trit), half-adder would suffice.


This is the end of the introduction to ternary computing. Stay tuned for clocks, counters, memory and more!

© Habrahabr.ru