SHARE
TWEET

Untitled

asteroidsteam Nov 25th, 2018 (edited) 130 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ARRAY Format: ["Command","Argument"],
  2. #Help Format: Command [Argument] => Description
  3.  
  4. #MEM 8-bit-num => Store a number in a variable
  5. #LD mem_location => Load a variable into RAM
  6. #SUB 8-bit-num => Subtract the RAM by a number
  7. #STO mem_location => Store the current RAM values into a variable
  8. #OUT => Output the current Ram
  9. #JZ mem_location => Jump to memory location IF Zero flag is set
  10. #JMP mem_location => Jump to a memory location
  11. #HLT => Halt the main clock (ALWAYS USE AT END OF PROGRAM)
  12. #JC mem_location => Jump to memory location IF carry flag is set
  13.  
  14. #Count down binary values from 00000111 until 00000000
  15.  
  16. # commands = [
  17. #     ["MEM","00000111"], # 0x00000000
  18. #     ["LD","0x00000000"], # 0x00000001
  19. #     ["SUB","00000001"], # 0x00000010
  20. #     ["STO","0x00000000"], # 0x00000011
  21. #     ["OUT",""], # 0x00000100
  22. #     ["JZ","0x00000111"], # 0x00000101
  23. #     ["JMP","0x00000001"], # 0x00000110
  24. #     ["HLT",""] # 0x00000111
  25. # ]
  26.  
  27. FILE_CODE = "./test.jas"
  28.  
  29. #Do not mess with code below.
  30.  
  31. class Halt(Exception): pass
  32.  
  33. locations = []
  34. ptr = "0x00000000"
  35. mem_load = "00000000"
  36. flags = [
  37.     False, # Zero Flag
  38.     False # Carry Flag
  39. ]
  40. def parseFile(file):
  41.     fileCode = []
  42.     with open(file,"r") as f:
  43.         for line in f.readlines():
  44.             fSplit = line.split(" ")
  45.             if len(fSplit) == 2:
  46.                 fileCode.append([fSplit[0],fSplit[1].replace("\n","")])
  47.            else:
  48.                fileCode.append([fSplit[0].replace("\n",""),""])
  49.    return fileCode
  50. commands = parseFile(FILE_CODE)
  51. def countMem():
  52.    num_mem = 0
  53.    for i in xrange(len(commands)):
  54.        if (commands[i][0] == "MEM"):
  55.            num_mem += 1
  56.    return num_mem
  57. def display():
  58.    n = 8
  59.    print("MEMORY:")
  60.    for i in xrange(len(commands)):
  61.        b = bin(i)[2:]
  62.        l = len(b)
  63.        b = str(0) * (n - l) + b
  64.        cmd = commands[i][0]
  65.        subcmd = commands[i][1]
  66.        if (i == countMem()):
  67.            print "\nPROGRAM:"
  68.        if (commands[i][0] == "MEM"):
  69.            print "0x"+b+" "+subcmd
  70.        else:
  71.            print "0x"+b+" "+cmd+" "+subcmd
  72.        locations.append(["0x"+b,commands[i][0],commands[i][1].replace("0x","")])
  73. def getCmdFromPtr(memloc):
  74.     for i in xrange(len(locations)):
  75.         if (locations[i][0] == memloc):
  76.             return locations[i][1]
  77.     return False
  78. def getValFromPtr(memloc):
  79.     for i in xrange(len(locations)):
  80.         if (locations[i][0] == memloc):
  81.             return locations[i][2]
  82.     return False
  83. def setValFromPtr(memloc,val):
  84.     for i in xrange(len(locations)):
  85.         if (locations[i][0] == memloc):
  86.             locations[i][2] = val
  87.             return
  88. def add(x,y):
  89.         maxlen = max(len(x), len(y))
  90.  
  91.         #Normalize lengths
  92.         x = x.zfill(maxlen)
  93.         y = y.zfill(maxlen)
  94.  
  95.         result = ''
  96.         carry = 0
  97.  
  98.         for i in range(maxlen-1, -1, -1):
  99.             r = carry
  100.             r += 1 if x[i] == '1' else 0
  101.             r += 1 if y[i] == '1' else 0
  102.  
  103.             # r can be 0,1,2,3 (carry + x[i] + y[i])
  104.             # and among these, for r==1 and r==3 you will have result bit = 1
  105.             # for r==2 and r==3 you will have carry = 1
  106.  
  107.             result = ('1' if r % 2 == 1 else '0') + result
  108.             carry = 0 if r < 2 else 1      
  109.  
  110.         if carry !=0 : result = '1' + result
  111.  
  112.         return result.zfill(maxlen)
  113. def sub(s1, s2):
  114.     b = bin(int(s1, 2) - int(s2, 2))[2:]
  115.     l = len(b)
  116.     b = str(0) * (8 - l) + b
  117.     return b  
  118. def runCode(cmd,arg):
  119.     global mem_load
  120.     global ptr
  121.     global flags
  122.    
  123.     if mem_load == "000000b1":
  124.         mem_load = "00000000"
  125.     if mem_load == "00000000":
  126.         flags[0] = True
  127.     else:
  128.         flags[0] = False    
  129.     if len(mem_load) != 8:
  130.         flags[1] = True
  131.     elif len(mem_load) == 8:
  132.         flags[1] = False
  133.    
  134.     if cmd == "JMP":
  135.         ptr = "0x"+arg
  136.     elif cmd == "JZ" and flags[0]:
  137.         ptr = "0x"+arg
  138.     elif cmd == "LD":
  139.         mem_load = getValFromPtr("0x"+arg)
  140.     elif cmd == "OUT":
  141.         print mem_load
  142.     elif cmd == "STO":
  143.         setValFromPtr(arg,mem_load)
  144.     elif cmd == "ADD":
  145.         mem_load = add(mem_load,arg)
  146.     elif cmd == "SUB":
  147.         mem_load = sub(mem_load,arg)
  148.     elif cmd == "HLT":
  149.         raise Halt
  150.     elif cmd == "JC" and flags[1]:
  151.         ptr = "0x"+arg
  152.     ptr = "0x"+add(ptr.replace("0x",""),"00000001")
  153.    return
  154. print("=-=-=-CODE-=-=-=")
  155. display()
  156. print("\nRunning Program...")
  157. try:
  158.    while True:
  159.        runCode(getCmdFromPtr(ptr),getValFromPtr(ptr))
  160. except Halt:
  161.    pass
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top