Skip to content

Instantly share code, notes, and snippets.

@EmmaEwert
Created February 5, 2015 16:19
Show Gist options
  • Save EmmaEwert/c7038b8ac86ed638ba43 to your computer and use it in GitHub Desktop.
Save EmmaEwert/c7038b8ac86ed638ba43 to your computer and use it in GitHub Desktop.
# Ternary CPU
Balanced ternary trits of `-`, `0` and `+`
1 tribble = 6 trits (729 states)
CPU memory map addresses are 1 tribble (------ to ++++++), operations operate on tribbles
Three special registers (p = program counter, s = stack pointer, q = query processor)
Three general registers (a, x, y)
## Memory map
### ------ to -+++++: RAM (3^5 = 243 addresses = 177147 states)
* ------ upwards: Heap
* -+++++ downwards: Stack
### 0----- to 0--+++: IO (3^3 = 27 addresses = 19683 states)
### 0-0--- to 0+++++: Reserved (2 * 3^4 = 162 addresses = 118098 states)
### +----- to ++++++: ROM (3^5 = 243 addresses = 177147 states)
## Registers
### p: Program counter
6-trit register holding the address of the next instruction to be executed.
### q: Query processor
6-trit stateholder register states:
* .....-: carry
* .....+: overflow
* ....-.: negative
* ....+.: zero
* ...X..: compare
* XXX...: reserved
### s: Stack pointer
The stack shares memory locations ------ to -+++++ with the heap.
The stack pointer is a 6-trit (5 absolute, 1 state) register serving as negative offset from -+++++.
IDEA: Three pointers from state trit.
### x|y|z: General purpose
## Instructions
Asterisk next to size indicates redundant instruction
Sz Instr Result
2 add 13 x
2* add 13 y
2* add 13 z
1* add x x
1 add x y
1* add x z
1* add y x
1* add y y
1 add y z
1 add z x
1* add z y
1* add z z
2 add &13 x
2* add &13 y
2* add &13 z
1* add &x x
1 add &x y
1* add &x z
1* add &y x
1 add &y y
1* add &y z
1* add &z x
1 add &z y
1* add &z z
--|repeat for all binary operations|--
* nand: logic building block
* bne: operands are offsets for compare flag of - and of +
* cmp: alpha < beta ? - : alpha > beta ? + : 0
* mov: beta := alpha
* ld: load tribble into register
* st: store tribble into register
Sz Instr
1 clr x
1* clr y
1* clr z
1* clr p
1* clr q
1* clr s
1* jmp 13
1 jmp x
1* jmp y
1* jmp z
1* jmp p
1* jmp q
1* jmp s
1* jmp &13
1 jmp &x
1* jmp &y
1* jmp &z
1* jmp &p
1* jmp &q
1* jmp &s
--|repeat for all unary operations|--
* psh: push alpha onto stack
* pop: pop alpha off stack
1 nop
## Septemvigesimal numeral system
--- = a
--0 = b
--+ = c
-0- = d
-00 = e
-0+ = f
-+- = g
-+0 = h
-++ = i
0-- = 1
0-0 = 2
0-+ = 3
00- = 4
000 = 5
00+ = 6
0+- = 7
0+0 = 8
0++ = 9
+-- = A
+-0 = B
+-+ = C
+0- = D
+00 = E
+0+ = F
++- = G
++0 = H
+++ = I
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment