Advertisement
nicx321

Untitled

Apr 15th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.32 KB | None | 0 0
  1. import sys
  2. import os
  3. from time import sleep
  4. if __name__ == "__main__":
  5.     try:
  6.         #register Definition
  7.         Final = []
  8.         DISP = 0x7FF
  9.         Input1 = 0x7FE
  10.         Input2 = 0x7FD
  11.         Input3 = 0x7FC
  12.         AluA = 0x7FB
  13.         AluB = 0x7FA
  14.         Ro1 = 0x7F9
  15.         Ro2 = 0x7F8
  16.         Ro3 = 0x7F7
  17.         Ro4 = 0x7F6
  18.         IfCR = 0x7F5
  19.         IfRR = 0x7F4
  20.         Clocks1 = 0x7F3
  21.         Clocks2 = 0x7F2
  22.         Clocks3 = 0x7F1
  23.         OneR = 0x7F0
  24.         GSR = 0x7EF
  25.         IntR2 = 0x7EE
  26.         Decd = 0x7ED
  27.  
  28.         RomSize = 256
  29.         WD = 1
  30.  
  31.         try:
  32.             f = open("Input.txt", "r")
  33.             G = f.readline()
  34.             if "RomSize" in G:
  35.                 exec(G ,globals())
  36.             f.close()
  37.         except:
  38.             pass
  39.  
  40.  
  41.         #Compiler definition
  42.         Final = []
  43.         WA = []
  44.         Whiles = []
  45.  
  46.         for i in range(RomSize*2):
  47.             Final.append(0)
  48.  
  49.         StatePO = RomSize-1
  50.         StatePA = RomSize
  51.         StateI = 0x000
  52.  
  53.         def DecD(A, Y):
  54.             MV(A, AluA)
  55.             MV(Decd, Y)
  56.  
  57.         def Derf(Val, Addr):
  58.             MV(Addr, IntR2)
  59.             GI(1, Val)
  60.             GI(16, 0)
  61.            
  62.         def DO():
  63.             WA.append(GetJumpPos())
  64.             Whiles.append(len(WA)-1)
  65.  
  66.         def WhilE(A, B, Conf):
  67.             If(A, B, WA[Whiles.pop()], Conf)
  68.  
  69.         def start():
  70.             return GetROM(0)
  71.  
  72.         def Halt():
  73.             GI(4, 0)
  74.  
  75.         def GetROM(Pres = None):
  76.             global StatePO
  77.             MP = StatePO
  78.             StatePO -= 1
  79.             if Pres != None:
  80.                 setVar(MP, Pres)
  81.             return MP
  82.  
  83.         def GetRAM(Pres = None):
  84.             global StatePA
  85.             MP = StatePA
  86.             StatePA += 1
  87.             if Pres != None:
  88.                 setVar(MP, Pres)
  89.             return MP
  90.  
  91.         def GetJumpPos():
  92.             global StateI
  93.             return GetROM(StateI)
  94.  
  95.         def setVar(ADDR, what):
  96.             global Final
  97.             Final[ADDR] = format(what, '04x')
  98.  
  99.         def CalcIns(INS, ADDR):
  100.             return hex((INS<<11)|ADDR)
  101.  
  102.         def Neg(A, ADDR):
  103.             GI(9, ADDR)
  104.  
  105.         def Add(A, B, ADDR):
  106.             MV(A, AluA)
  107.             MV(B, AluB)
  108.             GI(5, ADDR)
  109.  
  110.         def Sub(A, B, ADDR):
  111.             MV(A, AluA)
  112.             MV(B, AluB)
  113.             GI(6, ADDR)
  114.  
  115.         def Mul(A, B, ADDR):
  116.             MV(A, AluA)
  117.             MV(B, AluB)
  118.             GI(8, ADDR)
  119.  
  120.         def Div(A, B, ADDR):
  121.             MV(A, AluA)
  122.             MV(B, AluB)
  123.             GI(7, ADDR)
  124.  
  125.         def And(A, B, ADDR):
  126.             MV(A, AluA)
  127.             MV(B, AluB)
  128.             GI(10, ADDR)
  129.  
  130.         def Or(A, B, ADDR):
  131.             MV(A, AluA)
  132.             MV(B, AluB)
  133.             GI(11, ADDR)
  134.  
  135.         def Not(A, ADDR):
  136.             MV(A, AluA)
  137.             GI(12, ADDR)
  138.  
  139.         def Xor(A, B, ADDR):
  140.             MV(A, AluA)
  141.             MV(B, AluB)
  142.             GI(13, ADDR)
  143.  
  144.         def Goto(ADDR):
  145.             return GI(3, ADDR)
  146.  
  147.         def GI(INS, ADDR):
  148.             global Final, StateI
  149.             if WD == 1:
  150.                 if INS == 15:
  151.                     Final[StateI] = CalcIns(INS, ADDR)
  152.                     StateI += 1
  153.                 else:
  154.                     Final[StateI] = CalcIns(INS, ADDR)
  155.                     StateI += 1
  156.                     if StateI % 5 == 0:
  157.                         GI(15, 0)
  158.             else:
  159.                 Final[StateI] = CalcIns(INS, ADDR)
  160.                 StateI += 1
  161.  
  162.         def MV(A, B):
  163.             global Final
  164.             GI(1, A)
  165.             GI(2, B)
  166.  
  167.         def OneP(X):
  168.             MV(X, AluA)
  169.             MV(0x7F0, X)
  170.  
  171.         def If(A, B, Gotop, Conf):
  172.             if type(Conf) == type(""):
  173.                 Intp = MkIC(Conf)
  174.                 MV(Intp, IfCR)
  175.             else:
  176.                 MV(Conf, IfCR)
  177.             MV(A, AluA)
  178.             MV(B, AluB)
  179.             if Gotop != "":
  180.                 GI(14, Gotop)
  181.  
  182.         def MkIC(Inp):
  183.             if Inp == ">2":
  184.                 G = 1
  185.             if Inp == "=2":
  186.                 G = 2
  187.             if Inp == "<2":
  188.                 G = 4
  189.             if Inp == ">u":
  190.                 G = 8
  191.             if Inp == "=u":
  192.                 G = 16
  193.             if Inp == "<u":
  194.                 G = 32
  195.             return GetROM(G)
  196.  
  197.         #Code
  198.         f = open("Input.txt", "r")
  199.         G = f.read()
  200.         #print(G)
  201.         exec(G ,globals())
  202.         f.close()
  203.  
  204.         #print('\n\n\n\n', end='')
  205.  
  206.         #Render
  207.         f =  open("Output.txt", "w")
  208.         f.write("ROM: \n")
  209.  
  210.         #print("ROM: \n", end='')
  211.         for i in range(RomSize):
  212.             if i%16 == 0 and i != 0:
  213.                 f.write('\n')
  214.                 #print('\n', end='')
  215.             G = format(int(str(Final[i]), 16), '04x').upper()
  216.             f.write(G+' ')
  217.            
  218.         #f.write("\n\nSize: "+str(StateI+(RomSize-StatePO))+"W")
  219.         f.write("\n\nSize: "+str(round(((StateI+(RomSize-StatePO))/RomSize)*100,2))+"%")
  220.  
  221.         f.flush()
  222.         f.close()
  223.  
  224.     except Exception as e:
  225.         OutSt = str(e);
  226.         Location = "Output.txt"
  227.         text_file = open(Location, "w")
  228.         text_file.write(OutSt)
  229.         text_file.close()
  230.  
  231.     sys.exit(1234)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement