DEV Community

Thomas Leathers
Thomas Leathers

Posted on

I'm the lead developer of SBTCVM, a FOSS balanced ternary VM project, Ask Me Anything!

Top comments (5)

thomasthespacefox profile image
Thomas Leathers • Edited

Basically SBTCVM simulates a ternary computer. It has 19683 words of memory, (all ram) and at the moment programs are primarily loaded as memory images called troms (or ternary roms), but disk-based programs are in future plans. (we just haven't written the disk system yet. :p)

As far as what SBTCVM is actually useful for, learning and experimenting with balanced ternary is what lead to the first prototype in the first place, and remains an important part of the project.

what balanced ternary is

while regular ternary has 0, 1, & 2, balanced ternary has +1, 0, & -1


That negative digit can be quite confusing to beginners. For example:
lets count to 4 in Balanced ternary (p/+=+1 n/-=-1)

center: standard SBTCVM notation, right: alphanumeric notation which is less confusing in written math.

1 0+ 0p
2 +- pnHuh? where's zero?
3 +0 p0
4 ++ pp
Obvious in hindsight: the negative trit (trit: same idea as a bit) comes before zero.

converting to decimal

the second trit (left) is worth 31 (3)
the first is 30 (1)

so taking 2 as an example:

pn = +3-1 = 2

As as result of the negative digit, balanced ternary is never unsigned. So things like addresses start at the Max Negative Integer (or MNI for short.), and zero tends to fall in the middle of things.

github of latest version:
the blog has some guides and such as well:

ben profile image
Ben Halpern

Can you explain the project like I'm five? :)

thomasthespacefox profile image
Thomas Leathers

I suppose i could try. :)

SBTCVM has several major parts:

The VM of SBTCVM uses instruction level simulation to simulate a type of digital computer originally developed in Russia. It uses a 'balanced' base number. A balanced base number has an equal number of positive and negative digits. for example:

Here's a 2 trit(similar to a bit) number table as an example.

-- -4
-0 -3
-+ -2
0- -1
00 0
0+ 1
+- 2
+0 3
++ 4

not only does it have 9 states, 4 of them are negative. This inherit signing causes balanced ternary computing to be quite different from binary in places.

The (latest version's) CPU itself uses 18-trit words divided into a 9-trit instruction word, and a 9-trit data word, along with a 9-trit memory bus. This means it has 19,683 words of memory. It uses an additional 9-trit IO bus, providing 19,683 IO addresses.

The second part of SBTCVM is the programming tools. Currently SBTCVM has a fairly advanced low-level assembler, and a currently numeric, higher level language that provides a basic integer object abstraction system, along with the ability to use some assembly in-line if your feeling brave :)

A trom (memory image) dump utility is also provided, as a standard hex viewer obviously isn't going to help much. :)

The third part is software that runs in the VM. Now, the latest codebase isn't ready for huge interactive programs yet, but we do have some exciting plans... :)

juankortiz profile image

What challenges are you facing in terms of development?

thomasthespacefox profile image
Thomas Leathers • Edited

Here are some of the main ones:


SBTCVM has its own architecture for a reason. one of the key challenges has been translating elements from more modern computers to balanced ternary. You see, the last actual hardware example was built in the late 50s in Russia. So we don't have a massive amount of examples to build on.

Building the whole programming workflow

SBTCVM, having its own architecture, and not even being binary in the first place, can't use any common tools or compilers. So not only is the VM running on a custom math library, everything from SBTCVM assembly to SSTNPL to the romdump utility has been built from scratch.

having to design encodings and devices from scratch.

SBTCVM has involved inventing things from the start. the simplest of these creations is the 2 text encodings it has used: SBTCVM-BTT-6 and SBTCVM-BTT2 (current).

While SBTGA, sbtcvm's graphics system, is yet to be implemented in the new codebase, it can't use standard resolutions or color modes for obvious reasons.

Codebase & implementation

Along with the design challenges, actually implementing the architecture is quite a task. the latest codebase runs the CPU at 6.5Khz. that doesn't sound like much, but for python, it is.

The new codebase uses plenty of python classes in a modular architecture design that keeps the primary components separated (and has allowed us to plan multiple frontends i.e. curses, pygame, tk).

the assembler and SSTNPL's compiler are also class-based, but divide the instruction-specific parsing into class instances in a large list. This has allowed a variety of powerful features, and has somewhat simplified their upkeep.

programming the VM

Another challenge has been writing programs to make the VM more useful, and acting as examples for proper programs. The latest codebase doesn't have much outside the usual barrage of test programs, but the previous codebase did manage to run an RPN calculator.