Advertisement
Redxone

[CC - 16bit Computer] Computer Interfacer

Feb 12th, 2017
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.29 KB | None | 0 0
  1.     --]]
  2. --  Commands:
  3. --  MOV[s, d](Register into value or value into register)
  4. --  ADD      (Adds register A and B)
  5. --  SUB      (Subtracts register A with B)
  6. --  BUS[s]   (Puts a decimal value on the BUS)
  7. --  XOR      (XORs A, B)
  8. --  AND      (ANDs A, B)
  9. --  NOT[s]   (NOTs A or B)
  10. --  OR       (ORs A, B)
  11. --  POKE[s]  (Pokes Source of RAM)
  12. --  IN [s]   (Inputs BUS to Source of RAM)
  13. --  OUTA      (Stores Outputted data into register A)
  14. --  OUTB      (Stores Outputted data into register B)
  15. --]]
  16.  
  17. local programtickrate = 1.0
  18.  
  19.  hextobin = function(hex)
  20.     -- Convert to base 10 soooo easy
  21.     local dec = tonumber(hex,16)
  22.     local bin = ""
  23.     local binkey = "01"
  24.     local rim
  25.     while dec > 0 do
  26.        rim = math.floor(dec%2)
  27.        bin = binkey:sub(rim+1,rim+1) .. bin
  28.        dec = math.floor(dec/2)
  29.     end
  30.     return bin
  31.   end
  32.  
  33.  
  34. bintodec = function(bin)
  35.     if(tonumber(bin,2) == nil)then return bin end
  36.     if(bin == 0)then return 0 end
  37.     local dec = 0
  38.     for i = 1, #bin do
  39.         if(bin:sub(i,i) == '1')then
  40.             dec = dec + (2^(i-1))
  41.         end
  42.     end
  43.     return dec
  44. end
  45.  
  46. dectobin = function(dec)
  47.     dec = tonumber(dec)
  48.     local bin = ""
  49.     local binkey = "01"
  50.     local rim
  51.     while dec > 0 do
  52.        rim = math.floor(dec%2)
  53.        bin = binkey:sub(rim+1,rim+1) .. bin
  54.        dec = math.floor(dec/2)
  55.     end
  56.     if(#bin < 4)then
  57.       bin = string.rep("0",4-#bin) .. bin
  58.     end
  59.     return string.reverse(bin)
  60. end
  61.  
  62. dectobin16 = function(dec)
  63.     dec = tonumber(dec)
  64.     local bin = ""
  65.     local binkey = "01"
  66.     local rim
  67.     while dec > 0 do
  68.        rim = math.floor(dec%2)
  69.        bin = binkey:sub(rim+1,rim+1) .. bin
  70.        dec = math.floor(dec/2)
  71.     end
  72.     if(#bin < 16)then
  73.       bin = string.rep("0",16-#bin) .. bin
  74.     end
  75.     return string.reverse(bin)
  76. end
  77.  
  78. bintohex = function(bin)
  79.     local dec = tonumber(tostring(bin),2)
  80.     local hex = ""
  81.     local hexkey = "0123456789ABCDEF"
  82.     local rim
  83.     if(dec == 0)then hex = "0" end
  84.     while dec > 0 do
  85.        rim = math.floor(dec%16)
  86.        hex = hexkey:sub(rim+1,rim+1) .. hex
  87.        dec = math.floor(dec/16)
  88.     end
  89.     return hex
  90.   end
  91.  
  92. getBinary = function(data)
  93.     if(tonumber(data,2) ~= nil and #data == 4)then
  94.          return data
  95.  elseif(tonumber(data) ~= nil)then
  96.    return string.reverse(dectobin(data))
  97.  else
  98.    return "0000"
  99.  end
  100. end
  101.  
  102.  
  103. function getFileLines(file)
  104.     local f = fs.open(file, "r")
  105.     local line = f.readLine()
  106.     local data = {}
  107.     while line do
  108.       table.insert(data, line)
  109.       line = f.readLine()
  110.     end
  111.     f.close()
  112.     return data
  113. end
  114.  
  115. function getBus(side)
  116.     return redstone.getBundledInput(side)
  117. end
  118.  
  119. local busside = 'left'
  120. local opcside = 'left'
  121. local clk = 0
  122.  
  123. pushBus = function(data,side)
  124.     redstone.setBundledOutput(opcside, 0)
  125.     redstone.setBundledOutput(side, 0)
  126.     if(tonumber(data,2) ~= nil and (#data == 4 or #data == 16) )then data = bintodec(data) end
  127.     redstone.setBundledOutput(side, tonumber(data))
  128.     sleep(clk)
  129. end
  130.  
  131. runBinary = function(bin)
  132.     if(bin == 0)then return true end
  133.     for i = 1, #bin do
  134.         redstone.setBundledOutput(opside, bintodec(bin[i]))
  135.         sleep(clk)
  136.     end
  137. end
  138.  
  139. local commands = {
  140.     {c = 'MDA',name="Move Direct Address",args=2,call = function(dest, value)
  141.             pushBus(value, busside)
  142.         return {
  143.             '000000000001' .. getBinary(dest),
  144.             '000000000010' .. getBinary(dest),
  145.         }
  146.  
  147.      end},
  148.     {c = 'CLN',name="Clean RAM",args=0,call = function()
  149.  
  150.         code = {}
  151.         for i = 1, 15 do
  152.             redstone.setBundledOutput(busside, 0)
  153.             local busval = '000000000001' .. getBinary(tostring(i))
  154.             table.insert(code, busval)
  155.         end
  156.         print("Clearing RAM...")
  157.         return code
  158.     end},
  159.     {c = 'MACH',name="Enter machine binary code. ",args=1,call = function(code) return {tostring(code)} end},
  160.     {c = 'ADD',name="Add. ",args=0,call = function()  pushBus('0',busside) return {'0000010000000000'} end},
  161.     {c = 'SUB',name="Subtract. ",args=0,call = function()  pushBus('0',busside) return {'0000100000000000'} end},
  162.     {c = 'XOR',name="Exclusive Or. ",args=0,call = function() pushBus('0',busside) return {'0001000000000000'} end},
  163.     {c = 'AND',name="And. ",args=0,call = function() pushBus('0',busside) return {'0010000000000000'}end},
  164.     {c = 'NOT',name="Invert. ",args=1,call = function(r)
  165.         pushBus('0',busside)
  166.         if(string.upper(r) == 'A')then
  167.             return {'1000001000000000'}
  168.         elseif(string.upper(r) == 'B')then
  169.             return {'1000000100000000'}
  170.         else
  171.             return 0
  172.         end
  173.      end},
  174.     {c = 'OR',name="Or. ",args=0,call = function()   return {'0100000000000000'}end},
  175.     {c = 'POKE',name="Load an address onto the the Bus. ",args=1,call = function(source)
  176.           if(source  == 'A')then return {'0000001000000000'} end
  177.           if(source  == 'B')then return {'0000000100000000'} end
  178.           return {'000000000010' .. getBinary(source)}
  179.     end},
  180.     {c = 'POP',name="Store using Bus value. ",args=1,call = function(source)  
  181.         local oldbus = getBus()
  182.         redstone.setBundledOutput(opcside, 0)
  183.         pushBus(oldbus,busside)
  184.         if(source  == 'A')then
  185.             return {'000000001000' .. getBinary(source)}
  186.         end
  187.         if(source  == 'B')then
  188.             return {'000000000100' .. getBinary(source)}
  189.         end
  190.         return {'000000000001' .. getBinary(source)}
  191.     end},
  192.     {c = 'OP',name="Decimal opcode toggle. ",args=1,call = function(opc)
  193.         local code = {}
  194.         opc = tonumber(opc)
  195.         local prevop = redstone.getBundledInput(opcside)
  196.         if(opc > 16)then return 0 end
  197.         if(opc == 0)then return {'000000000000000'} end -- Clear op codes.
  198.         if(prevop == 0)then
  199.             code[1] = string.rep('0',opc-1) .. '1' .. string.rep('0',16-opc)
  200.         else
  201.             code[1] = dectobin16(bit.bxor(prevop, 2^(opc-1) ))
  202.         end
  203.         return code
  204.     end},
  205.     {c = 'RUN',name="Run RAM segment as code. ",args=2,call = function(min,max)
  206.             local code = {}
  207.             if max == nil then min = max end
  208.             for i = min, max do
  209.                 redstone.setBundledOutput(opcside,bintodec( tostring('000000000010' .. getBinary(tostring(i) ) ) ) )
  210.                 sleep(programtickrate)
  211.                 local busval = (dectobin16(getBus()))
  212.                 sleep(programtickrate)
  213.                 table.insert(code, busval)
  214.                 print( (i) .. ":" .. busval:sub(1,12) .. ":" .. busval:sub(13,16))
  215.                 --redstone.setBundledOutput(opcside,bintodec( tostring('000000000000' .. getBinary(tostring(i) ) ) ) )
  216.             end
  217.             print("Done. ")
  218.             return code
  219.     end},
  220.     {c = 'LOAD',name="Load Machine Binary into RAM. (Broken) ",args=2,call = function(min,binary)
  221.             if( not fs.exists(binary))then print("Failed to load program - " .. binary) end
  222.             local lines = {}
  223.             lines = getFileLines(binary)
  224.             for i = min, #lines+(min-1) do
  225.                 if(i > 15)then
  226.                     print("Out of space. ")
  227.                 end
  228.                 print("Setting: " ..  getBinary(tostring(i)) .. " -> " .. tostring(lines[i-(min-1)]) )
  229.                 redstone.setBundledOutput(opcside,bintodec( tostring('000000000000' .. getBinary(tostring(i) ) ) ) )
  230.                 sleep(programtickrate)
  231.                 redstone.setBundledOutput(busside, bintodec(lines[i-(min-1)]))
  232.                 redstone.setBundledOutput(opcside,bintodec( tostring('000000000001' .. getBinary(tostring(i) ) ) ) )
  233.                 sleep(programtickrate)
  234.                 redstone.setBundledOutput(opcside,bintodec( tostring('000000000000' .. getBinary(tostring(i) ) ) ) )
  235.             end
  236.             print("Done. ")
  237.             return 0
  238.     end},
  239.     {c = 'MOV',name='Move.',args=2,call = function(dest,source)
  240.             local code = {}
  241.             local directval = false
  242.             if(string.upper(source:sub(1,1)) == 'B')then
  243.                 source = source:sub(2,#source)
  244.                 source = bintodec(string.reverse(source))
  245.             end    
  246.             if(string.upper(source:sub(#source,#source)) == 'D')then
  247.                 source = source:sub(1,#source-1)
  248.                 directval = true
  249.             end
  250.             if(not directval and dest == tonumber(dest) and source == tonumber(source))then
  251.                 source = getBinary(source)
  252.                 dest = getBinary(dest)
  253.                 code = {
  254.                     "000000001010" .. source,
  255.                     "000000100001" .. dest,
  256.                 }
  257.                 return code
  258.             end
  259.             if(string.upper(dest) == 'A')then
  260.                 if(string.upper(source) == 'A')then
  261.                     return 0
  262.                 elseif(string.upper(source) == 'B')then
  263.                     code = {
  264.                         "0000001001000000", -- READ A, WRITE B
  265.                     }
  266.                     return code
  267.                 elseif(not directval)then
  268.                     source = getBinary(source)
  269.                     code = {
  270.                         "000000001010" .. source, -- WRITE A, READ RAM
  271.                     }
  272.                     return code
  273.                 elseif(directval)then
  274.                     pushBus('0',busside)
  275.                     sleep(clock)
  276.                     pushBus(tostring(source),busside)
  277.                     dest = getBinary(dest)
  278.                     code = {
  279.                         "0000000010000000", -- WRITE B
  280.                     }
  281.                 end
  282.             elseif(string.upper(dest) == 'B')then
  283.                 if(string.upper(source) == 'B')then
  284.                     return 0
  285.                 elseif(string.upper(source) == 'A')then
  286.                     code = {
  287.                         "0000000110000000",  -- READ B, WRITE A
  288.                     }
  289.                     return code
  290.                 elseif(not directval)then
  291.                     source = getBinary(source)
  292.                     code = {
  293.                         "000000000110" .. source, -- WRITE B, READ RAM
  294.                     }
  295.                     return code
  296.                 elseif(directval)then
  297.                     pushBus('0',busside)
  298.                     sleep(clock)
  299.                     pushBus(tostring(source),busside)
  300.                     code = {
  301.                         "0000000001000000", -- WRITE B
  302.                     }
  303.                 end
  304.             elseif(string.upper(source) == 'A')then
  305.                 dest = getBinary(dest)
  306.                 code = {
  307.                     "000000100001" .. dest,
  308.                 }
  309.             elseif(string.upper(source) == 'B')then
  310.                 dest = getBinary(dest)
  311.                 code = {
  312.                     "000000010001" .. dest,
  313.                 }
  314.             elseif(not directval)then
  315.                 source = getBinary(source)
  316.                 dest = getBinary(dest)
  317.                 code = {
  318.                     "000000001010" .. source,
  319.                     "000000100001" .. dest,
  320.                 }
  321.             elseif(directval)then
  322.                 pushBus('0',busside)
  323.                 sleep(clock)
  324.                 pushBus(tostring(source),busside)
  325.                 dest = getBinary(dest)
  326.                 code = {
  327.                     "000000000001" .. dest,
  328.                 }
  329.             end
  330.             return code
  331.         end,
  332.     },
  333. }
  334.  
  335. execute = function(cmd,targs,opside,bside,delay)
  336.     clk = delay
  337.     busside = bside
  338.     opcside = opside
  339.     if(cmd == "BUS")then
  340.         pushBus((targs[1]),bside)
  341.         return true
  342.     elseif(cmd == "POKEBUS")then
  343.         print("-> " .. redstone.getBundledInput(bside))
  344.         return true
  345.     elseif(cmd == "CLR")then
  346.         local w, h = term.getSize()
  347.         term.setBackgroundColor(colors.black)
  348.         term.setTextColor(colors.lime)
  349.         term.clear()
  350.         paintutils.drawLine(1,1,w,1,colors.gray)
  351.         term.setBackgroundColor(colors.gray)
  352.         term.setCursorPos(1, 1)
  353.         write("Computer Control")
  354.         term.setBackgroundColor(colors.black)
  355.         term.setCursorPos(1,2)
  356.         redstone.setBundledOutput(opside, 0)
  357.         return true
  358.     elseif(cmd == "CMDS")then
  359.             local w, h = term.getSize()
  360.             print("CLR: Clears Bus and Interface. ")
  361.             print("BUS: Outputs value to Bus.")
  362.             print("POKEBUS: Outputs Bus value. ")
  363.             print("CMDS: List Opperation Codes. ")
  364.             local ind = 0
  365.         for k, v in pairs(commands) do
  366.             if(ind+7 > h)then
  367.                 _, oldh = term.getCursorPos()
  368.                 term.setCursorPos(1,h)
  369.                 write("Press any key. ")
  370.                 os.pullEvent("key")
  371.                 term.setCursorPos(1,oldh)
  372.                 term.clearLine()
  373.             end
  374.                 term.clearLine()
  375.                 print(v.c .. ": " .. v.name)
  376.             ind = ind + 1
  377.         end
  378.         return true
  379.     end
  380.     for k, v in pairs(commands) do
  381.         if(v.c == cmd)then
  382.             if(#targs == v.args)then
  383.                 local machinecode = v.call(unpack(targs))
  384.                 if(machinecode == 0)then return true end
  385.                 for i = 1, #machinecode do
  386.                     --print(machinecode[i])
  387.                     redstone.setBundledOutput(opside, bintodec(machinecode[i]))
  388.                     sleep(delay)
  389.                 end
  390.                 --sleep(delay)
  391.                 --redstone.setBundledOutput(opcside,bintodec( '0000000000000000' ))
  392.                 return true -- Done executing
  393.             end
  394.         end
  395.     end
  396.     return false
  397. end
  398.  
  399. local function compileASMFunction(assembly,args,exp)
  400.     for k, v in pairs(commands) do
  401.             if(v.c == assembly[1])then
  402.                 if(#args == v.args)then
  403.                     local machinecode = v.call(unpack(args))
  404.                     if(machinecode == 0)then return true end
  405.                         local ef = fs.open(exp,'a')
  406.                             for i = 1, #machinecode do
  407.                                 print(machinecode[i])
  408.                                 ef.writeLine(machinecode[i])
  409.                             end
  410.                         ef.close()
  411.                     return true -- Done executing
  412.                 end
  413.             end
  414.         end
  415. end
  416.  
  417.  
  418. function CCASM(asm,exp)
  419.     if(not fs.exists(asm))then print("Failed to compile program - " .. asm) end
  420.     if(fs.exists(exp))then print("Cannot export to:  " .. exp) end
  421.     local f = fs.open(asm,'r')
  422.     local lines = getFileLines(asm)
  423.     local ef = fs.open(exp,'w')
  424.     ef.write("")
  425.     ef.close()
  426.     for k, v in pairs(lines) do
  427.         local line = v:gsub(",","")
  428.         local line = string.upper(line)
  429.         local assembly = {}
  430.         local args = {}
  431.         local ind = 0
  432.         for word in line:gmatch("%w+") do
  433.             if(ind >= 1)then
  434.                 table.insert(args, word)
  435.             end
  436.             table.insert(assembly, word)
  437.             ind = ind + 1
  438.         end
  439.         compileASMFunction(assembly,args,exp)
  440.     end
  441. end
  442.  
  443. function BITGUI(opside, bside, clock)
  444.     clk = clock
  445.     busside = bside
  446.     opcside = opside
  447.     local history = {}
  448.     local w, h = term.getSize()
  449.     term.setBackgroundColor(colors.black)
  450.     term.setTextColor(colors.lime)
  451.     term.clear()
  452.     paintutils.drawLine(1,1,w,1,colors.gray)
  453.     term.setBackgroundColor(colors.gray)
  454.     term.setCursorPos(1, 1)
  455.     write("Computer Control")
  456.  term.setBackgroundColor(colors.black)
  457.  term.setCursorPos(1,2)
  458.     while true do
  459.         write(":> ")
  460.         local command = read(nil,history)
  461.         table.insert(history, command)
  462.         local command = string.upper(command)
  463.         local assembly = {}
  464.         local args = {}
  465.         local ind = 0
  466.         command = command:gsub(","," ")
  467.         for word in command:gmatch("%w+") do
  468.             if(ind >= 1)then
  469.                 table.insert(args, word)
  470.             end
  471.             table.insert(assembly, word)
  472.             ind = ind + 1
  473.         end
  474.         if(not execute(assembly[1],args,opside,bside,clock))then
  475.             print("ERROR :> Unrecognized opperation: " .. assembly[1] .. " or Invalid Arguments")
  476.         end
  477.     end
  478. end
  479. BITGUI("bottom","back", 0.8)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement