import sys
import warnings

import matplotlib.pyplot as plt
from parsets import IMACC, IMG, PROGC, REGFLPC, ExecE, plot

warnings.filterwarnings("ignore")

MEM = IMACC(sys.stdin.read())  # Load memory from stdin
PC = PROGC(0)  # Start from the first instruction
RF = REGFLPC()  # initialize register and flags
EE = ExecE(MEM)
IM = IMG()
halted = False
cycle = 0

if MEM.inst_mem == ["0" * 16 for i in range(256)]:
    halted = True

while not halted:
    Instruction = MEM.getData(PC)  # Get current instruction
    IM.imgx.append(cycle)
    IM.imgy.append(PC.PC)
    halted, new_PC, new_regs = EE.execute(Instruction, RF.asdct(), IM, cycle)
    # Update RF compute new_PC
    RF.update(new_regs, new_PC)
    PC.dump()
    # Print PC
    RF.dump()
    # Print RF state
    PC.update(new_PC)
    # Update PC
    cycle += 1

MEM.dump()  # Print memory state

# plotting
plot(plt, IM)

[((7, 0, 7, 33), 'warnings.filterwarnings', 'warnings.filterwarnings', ({(7, 24, 7, 32): '"""ignore"""'}, {}), "('ignore')", False, 'import warnings\n'), ((10, 5, 10, 13), 'parsets.PROGC', 'PROGC', ({(10, 11, 10, 12): '0'}, {}), '(0)', False, 'from parsets import IMACC, IMG, PROGC, REGFLPC, ExecE, plot\n'), ((11, 5, 11, 14), 'parsets.REGFLPC', 'REGFLPC', ({}, {}), '()', False, 'from parsets import IMACC, IMG, PROGC, REGFLPC, ExecE, plot\n'), ((12, 5, 12, 15), 'parsets.ExecE', 'ExecE', ({(12, 11, 12, 14): 'MEM'}, {}), '(MEM)', False, 'from parsets import IMACC, IMG, PROGC, REGFLPC, ExecE, plot\n'), ((13, 5, 13, 10), 'parsets.IMG', 'IMG', ({}, {}), '()', False, 'from parsets import IMACC, IMG, PROGC, REGFLPC, ExecE, plot\n'), ((38, 0, 38, 13), 'parsets.plot', 'plot', ({(38, 5, 38, 8): 'plt', (38, 10, 38, 12): 'IM'}, {}), '(plt, IM)', False, 'from parsets import IMACC, IMG, PROGC, REGFLPC, ExecE, plot\n'), ((9, 12, 9, 28), 'sys.stdin.read', 'sys.stdin.read', ({}, {}), '()', False, 'import sys\n')]