from os import system
from time import sleep
from js import setInterval, document
from pyodide.ffi import create_proxy
# Get the DOM element
output_div = document.getElementById('output')
MEMORY_SIZE = 326
memory = [0]*MEMORY_SIZE
instruction = 0
a = 0
b = 0
c = 0
i = 0
j = 0
k = 0
x = 0
y = 0
z = 0
def reset(event):
global instruction
global a,b,c,i,j,k,x,y,z
global memory
memory[0] = 'k = 0'
memory[1] = 'i = 0'
memory[2] = 'i = i + 1'
memory[3] = 'j = i + k'
memory[4] = 'x = j + 37'
memory[5] = 'y = j + 38'
memory[6] = 'z = j + 39'
memory[7] = 'a = memory[x]'
memory[8] = 'b = memory[y]'
memory[9] = 'c = memory[z]'
memory[10] = 'y = y + 24'
memory[11] = 'if a == 0: goto(23)'
memory[12] = 'if b == 0: goto(18)'
memory[13] = 'if c == 0: goto(16)'
memory[14] = 'memory[y] = 0'
memory[15] = 'goto(34)'
memory[16] = 'memory[y] = 1'
memory[17] = 'goto(34)'
memory[18] = 'if c == 0: goto(21)'
memory[19] = 'memory[y] = 1'
memory[20] = 'goto(34)'
memory[21] = 'memory[y] = 0'
memory[22] = 'goto(34)'
memory[23] = 'if b == 0: goto(29)'
memory[24] = 'if c == 0: goto(27)'
memory[25] = 'memory[y] = 1'
memory[26] = 'goto(34)'
memory[27] = 'memory[y] = 1'
memory[28] = 'goto(34)'
memory[29] = 'if c == 0: goto(32)'
memory[30] = 'memory[y] = 1'
memory[31] = 'goto(34)'
memory[32] = 'memory[y] = 0'
memory[33] = 'goto(34)'
memory[34] = 'if i < 23: goto(2)'
memory[35] = 'k = k + 24'
memory[36] = 'if k < 264: goto(1)'
memory[37] = 'halt'
memory[51] = 1
instruction=0
a=b=c=i=j=k=x=y=z=0
print_computer()
def goto(cell):
global instruction
instruction = cell-1
def print_computer():
out = '╔═══ Processor ══════════════╗' + '
'
out += ' instruction: %d' % instruction + '
'
out += ' Short Term Memory' + '
'
out += ' a: %4d b: %4d c: %4d' % (a,b,c) + '
'
out += ' i: %4d j: %4d k: %4d' % (i,j,k) + '
'
out += ' x: %4d y: %4d z: %4d' % (x,y,z) + '
'
out += '╚════════════════════════════╝' + '
'
out += '╔═══ Memory ═════════════════╗' + '
'
for cell in range(len(memory)):
marker = "→ " if cell==instruction else " "
T = type(memory[cell])
if T == float:
formatted_cell = ("%2.2f" % memory[cell])
elif T==int:
formatted_cell = ("%d" % memory[cell])
else:
formatted_cell = memory[cell]
out += marker + "%2d" % cell + " : " + formatted_cell + '
'
out += '╚════════════════════════════╝' + '
'
output_div.innerHTML = out
def compute():
global instruction
if memory[instruction] != 'halt':
exec(memory[instruction], globals())
instruction = instruction + 1
print_computer()
reset(None)
_ = setInterval(create_proxy(compute), 100)
document.getElementById("resetbutton").addEventListener("click", create_proxy(reset))