Flop
Jump to navigation
Jump to search
Programs
Simple binary 3 bit counter
add a add b add c clock c to c b to b a when c 0 when b 0
Locked values
to 0
add a clock a to a a when a 1
to 1
add a clock a to a a when a 0
Interpreter
pflops = {}
flops = {}
transitions = {}
whens = {}
clocks = []
program = """
add a
add b
add c
add d
clock d
to d c
to c b
to b a
when d 0
when c 0
when b 0
when a 0
""".split("\n")
def floop(flopper):
flops[flopper] = 1-flops[flopper]
if flops[flopper] == whens[flopper] and flops[flopper] != pflops[flopper]:
try:
floop(transitions[flopper])
except KeyError:
pass
for commands in program:
parts = commands.split(" ")
if parts[0] == "add":
flops[parts[1]] = 0
pflops[parts[1]] = 0
elif parts[0] == "to":
transitions[parts[1]] = parts[2]
elif parts[0] == "when":
whens[parts[1]] = int(parts[2])
elif parts[0] == "clock":
clocks.append(parts[1])
i = 0
while i < 16: #dependent on program length
for flop in flops:
print(flop+": "+str(flops[flop]))
print("")
for flop in flops:
if flop in clocks:
flops[flop] = 1-flops[flop]
if flops[flop] == whens[flop] and flops[flop] != pflops[flop]:
try:
floop(transitions[flop])
except KeyError:
pass
pflops = flops.copy()
i += 1
Better syntax would be this
c to b when c is 0