Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
TIS-100 prime detector
Node 2 is the "prime sequencer"
Node 4 subtracts candidate divisor (fed in by node 5) repeatedly and generates control
signal for node 5 based on result.
Node 5 gets candidate primes from node 2 (prime sequencer) and feeds them repeatedly
to node 4 until node 4s accumulator (current value) is <=0.
=0 -> is divisible -> not prime. Output 0, reset prime sequence, start over.
<0 -> not divisble -> possibly prime. Advance to next candidate divisor.
If next candidate divisor is 0 (end of list), it's prime. Output 1, continue.
----
@0
@1
MOV UP,DOWN
@2
#PRIMES P*P<1000
MOV 2,DOWN
MOV 3,DOWN
MOV 5,DOWN
MOV 7,DOWN
MOV 11,DOWN
MOV 13,DOWN
MOV 17,DOWN
MOV 19,DOWN
MOV 23,DOWN
MOV 29,DOWN
MOV 31,DOWN
MOV 0,DOWN
@3
@4
S:MOV UP,ACC
SAV
X:SUB RIGHT
JGZ G
JLZ L
MOV 1,RIGHT #!P
JMP S
G:MOV -1,RIGHT #LP
JMP X
L:MOV -4,RIGHT #+P
SWP
SAV
JRO RIGHT
@5
S:MOV UP,ACC
JMP GO
MOV UP,ACC
JEZ PR
MOV -10,LEFT
GO:MOV ACC,LEFT
JRO LEFT
NOTP:MOV UP,ACC
JNZ NOTP
MOV 0,DOWN
JMP S
PR:MOV 1,DOWN
MOV -12,LEFT
@6
@7
MOV UP,DOWN
@8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment