Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --]]
- -- Commands:
- -- MOV[s, d](Register into value or value into register)
- -- ADD (Adds register A and B)
- -- SUB (Subtracts register A with B)
- -- BUS[s] (Puts a decimal value on the BUS)
- -- XOR (XORs A, B)
- -- AND (ANDs A, B)
- -- NOT[s] (NOTs A or B)
- -- OR (ORs A, B)
- -- POKE[s] (Pokes Source of RAM)
- -- IN [s] (Inputs BUS to Source of RAM)
- -- OUTA (Stores Outputted data into register A)
- -- OUTB (Stores Outputted data into register B)
- --]]
- local programtickrate = 1.0
- hextobin = function(hex)
- -- Convert to base 10 soooo easy
- local dec = tonumber(hex,16)
- local bin = ""
- local binkey = "01"
- local rim
- while dec > 0 do
- rim = math.floor(dec%2)
- bin = binkey:sub(rim+1,rim+1) .. bin
- dec = math.floor(dec/2)
- end
- return bin
- end
- bintodec = function(bin)
- if(tonumber(bin,2) == nil)then return bin end
- if(bin == 0)then return 0 end
- local dec = 0
- for i = 1, #bin do
- if(bin:sub(i,i) == '1')then
- dec = dec + (2^(i-1))
- end
- end
- return dec
- end
- dectobin = function(dec)
- dec = tonumber(dec)
- local bin = ""
- local binkey = "01"
- local rim
- while dec > 0 do
- rim = math.floor(dec%2)
- bin = binkey:sub(rim+1,rim+1) .. bin
- dec = math.floor(dec/2)
- end
- if(#bin < 4)then
- bin = string.rep("0",4-#bin) .. bin
- end
- return string.reverse(bin)
- end
- dectobin16 = function(dec)
- dec = tonumber(dec)
- local bin = ""
- local binkey = "01"
- local rim
- while dec > 0 do
- rim = math.floor(dec%2)
- bin = binkey:sub(rim+1,rim+1) .. bin
- dec = math.floor(dec/2)
- end
- if(#bin < 16)then
- bin = string.rep("0",16-#bin) .. bin
- end
- return string.reverse(bin)
- end
- bintohex = function(bin)
- local dec = tonumber(tostring(bin),2)
- local hex = ""
- local hexkey = "0123456789ABCDEF"
- local rim
- if(dec == 0)then hex = "0" end
- while dec > 0 do
- rim = math.floor(dec%16)
- hex = hexkey:sub(rim+1,rim+1) .. hex
- dec = math.floor(dec/16)
- end
- return hex
- end
- getBinary = function(data)
- if(tonumber(data,2) ~= nil and #data == 4)then
- return data
- elseif(tonumber(data) ~= nil)then
- return string.reverse(dectobin(data))
- else
- return "0000"
- end
- end
- function getFileLines(file)
- local f = fs.open(file, "r")
- local line = f.readLine()
- local data = {}
- while line do
- table.insert(data, line)
- line = f.readLine()
- end
- f.close()
- return data
- end
- function getBus(side)
- return redstone.getBundledInput(side)
- end
- local busside = 'left'
- local opcside = 'left'
- local clk = 0
- pushBus = function(data,side)
- redstone.setBundledOutput(opcside, 0)
- redstone.setBundledOutput(side, 0)
- if(tonumber(data,2) ~= nil and (#data == 4 or #data == 16) )then data = bintodec(data) end
- redstone.setBundledOutput(side, tonumber(data))
- sleep(clk)
- end
- runBinary = function(bin)
- if(bin == 0)then return true end
- for i = 1, #bin do
- redstone.setBundledOutput(opside, bintodec(bin[i]))
- sleep(clk)
- end
- end
- local commands = {
- {c = 'MDA',name="Move Direct Address",args=2,call = function(dest, value)
- pushBus(value, busside)
- return {
- '000000000001' .. getBinary(dest),
- '000000000010' .. getBinary(dest),
- }
- end},
- {c = 'CLN',name="Clean RAM",args=0,call = function()
- code = {}
- for i = 1, 15 do
- redstone.setBundledOutput(busside, 0)
- local busval = '000000000001' .. getBinary(tostring(i))
- table.insert(code, busval)
- end
- print("Clearing RAM...")
- return code
- end},
- {c = 'MACH',name="Enter machine binary code. ",args=1,call = function(code) return {tostring(code)} end},
- {c = 'ADD',name="Add. ",args=0,call = function() pushBus('0',busside) return {'0000010000000000'} end},
- {c = 'SUB',name="Subtract. ",args=0,call = function() pushBus('0',busside) return {'0000100000000000'} end},
- {c = 'XOR',name="Exclusive Or. ",args=0,call = function() pushBus('0',busside) return {'0001000000000000'} end},
- {c = 'AND',name="And. ",args=0,call = function() pushBus('0',busside) return {'0010000000000000'}end},
- {c = 'NOT',name="Invert. ",args=1,call = function(r)
- pushBus('0',busside)
- if(string.upper(r) == 'A')then
- return {'1000001000000000'}
- elseif(string.upper(r) == 'B')then
- return {'1000000100000000'}
- else
- return 0
- end
- end},
- {c = 'OR',name="Or. ",args=0,call = function() return {'0100000000000000'}end},
- {c = 'POKE',name="Load an address onto the the Bus. ",args=1,call = function(source)
- if(source == 'A')then return {'0000001000000000'} end
- if(source == 'B')then return {'0000000100000000'} end
- return {'000000000010' .. getBinary(source)}
- end},
- {c = 'POP',name="Store using Bus value. ",args=1,call = function(source)
- local oldbus = getBus()
- redstone.setBundledOutput(opcside, 0)
- pushBus(oldbus,busside)
- if(source == 'A')then
- return {'000000001000' .. getBinary(source)}
- end
- if(source == 'B')then
- return {'000000000100' .. getBinary(source)}
- end
- return {'000000000001' .. getBinary(source)}
- end},
- {c = 'OP',name="Decimal opcode toggle. ",args=1,call = function(opc)
- local code = {}
- opc = tonumber(opc)
- local prevop = redstone.getBundledInput(opcside)
- if(opc > 16)then return 0 end
- if(opc == 0)then return {'000000000000000'} end -- Clear op codes.
- if(prevop == 0)then
- code[1] = string.rep('0',opc-1) .. '1' .. string.rep('0',16-opc)
- else
- code[1] = dectobin16(bit.bxor(prevop, 2^(opc-1) ))
- end
- return code
- end},
- {c = 'RUN',name="Run RAM segment as code. ",args=2,call = function(min,max)
- local code = {}
- if max == nil then min = max end
- for i = min, max do
- redstone.setBundledOutput(opcside,bintodec( tostring('000000000010' .. getBinary(tostring(i) ) ) ) )
- sleep(programtickrate)
- local busval = (dectobin16(getBus()))
- sleep(programtickrate)
- table.insert(code, busval)
- print( (i) .. ":" .. busval:sub(1,12) .. ":" .. busval:sub(13,16))
- --redstone.setBundledOutput(opcside,bintodec( tostring('000000000000' .. getBinary(tostring(i) ) ) ) )
- end
- print("Done. ")
- return code
- end},
- {c = 'LOAD',name="Load Machine Binary into RAM. (Broken) ",args=2,call = function(min,binary)
- if( not fs.exists(binary))then print("Failed to load program - " .. binary) end
- local lines = {}
- lines = getFileLines(binary)
- for i = min, #lines+(min-1) do
- if(i > 15)then
- print("Out of space. ")
- end
- print("Setting: " .. getBinary(tostring(i)) .. " -> " .. tostring(lines[i-(min-1)]) )
- redstone.setBundledOutput(opcside,bintodec( tostring('000000000000' .. getBinary(tostring(i) ) ) ) )
- sleep(programtickrate)
- redstone.setBundledOutput(busside, bintodec(lines[i-(min-1)]))
- redstone.setBundledOutput(opcside,bintodec( tostring('000000000001' .. getBinary(tostring(i) ) ) ) )
- sleep(programtickrate)
- redstone.setBundledOutput(opcside,bintodec( tostring('000000000000' .. getBinary(tostring(i) ) ) ) )
- end
- print("Done. ")
- return 0
- end},
- {c = 'MOV',name='Move.',args=2,call = function(dest,source)
- local code = {}
- local directval = false
- if(string.upper(source:sub(1,1)) == 'B')then
- source = source:sub(2,#source)
- source = bintodec(string.reverse(source))
- end
- if(string.upper(source:sub(#source,#source)) == 'D')then
- source = source:sub(1,#source-1)
- directval = true
- end
- if(not directval and dest == tonumber(dest) and source == tonumber(source))then
- source = getBinary(source)
- dest = getBinary(dest)
- code = {
- "000000001010" .. source,
- "000000100001" .. dest,
- }
- return code
- end
- if(string.upper(dest) == 'A')then
- if(string.upper(source) == 'A')then
- return 0
- elseif(string.upper(source) == 'B')then
- code = {
- "0000001001000000", -- READ A, WRITE B
- }
- return code
- elseif(not directval)then
- source = getBinary(source)
- code = {
- "000000001010" .. source, -- WRITE A, READ RAM
- }
- return code
- elseif(directval)then
- pushBus('0',busside)
- sleep(clock)
- pushBus(tostring(source),busside)
- dest = getBinary(dest)
- code = {
- "0000000010000000", -- WRITE B
- }
- end
- elseif(string.upper(dest) == 'B')then
- if(string.upper(source) == 'B')then
- return 0
- elseif(string.upper(source) == 'A')then
- code = {
- "0000000110000000", -- READ B, WRITE A
- }
- return code
- elseif(not directval)then
- source = getBinary(source)
- code = {
- "000000000110" .. source, -- WRITE B, READ RAM
- }
- return code
- elseif(directval)then
- pushBus('0',busside)
- sleep(clock)
- pushBus(tostring(source),busside)
- code = {
- "0000000001000000", -- WRITE B
- }
- end
- elseif(string.upper(source) == 'A')then
- dest = getBinary(dest)
- code = {
- "000000100001" .. dest,
- }
- elseif(string.upper(source) == 'B')then
- dest = getBinary(dest)
- code = {
- "000000010001" .. dest,
- }
- elseif(not directval)then
- source = getBinary(source)
- dest = getBinary(dest)
- code = {
- "000000001010" .. source,
- "000000100001" .. dest,
- }
- elseif(directval)then
- pushBus('0',busside)
- sleep(clock)
- pushBus(tostring(source),busside)
- dest = getBinary(dest)
- code = {
- "000000000001" .. dest,
- }
- end
- return code
- end,
- },
- }
- execute = function(cmd,targs,opside,bside,delay)
- clk = delay
- busside = bside
- opcside = opside
- if(cmd == "BUS")then
- pushBus((targs[1]),bside)
- return true
- elseif(cmd == "POKEBUS")then
- print("-> " .. redstone.getBundledInput(bside))
- return true
- elseif(cmd == "CLR")then
- local w, h = term.getSize()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.lime)
- term.clear()
- paintutils.drawLine(1,1,w,1,colors.gray)
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(1, 1)
- write("Computer Control")
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,2)
- redstone.setBundledOutput(opside, 0)
- return true
- elseif(cmd == "CMDS")then
- local w, h = term.getSize()
- print("CLR: Clears Bus and Interface. ")
- print("BUS: Outputs value to Bus.")
- print("POKEBUS: Outputs Bus value. ")
- print("CMDS: List Opperation Codes. ")
- local ind = 0
- for k, v in pairs(commands) do
- if(ind+7 > h)then
- _, oldh = term.getCursorPos()
- term.setCursorPos(1,h)
- write("Press any key. ")
- os.pullEvent("key")
- term.setCursorPos(1,oldh)
- term.clearLine()
- end
- term.clearLine()
- print(v.c .. ": " .. v.name)
- ind = ind + 1
- end
- return true
- end
- for k, v in pairs(commands) do
- if(v.c == cmd)then
- if(#targs == v.args)then
- local machinecode = v.call(unpack(targs))
- if(machinecode == 0)then return true end
- for i = 1, #machinecode do
- --print(machinecode[i])
- redstone.setBundledOutput(opside, bintodec(machinecode[i]))
- sleep(delay)
- end
- --sleep(delay)
- --redstone.setBundledOutput(opcside,bintodec( '0000000000000000' ))
- return true -- Done executing
- end
- end
- end
- return false
- end
- local function compileASMFunction(assembly,args,exp)
- for k, v in pairs(commands) do
- if(v.c == assembly[1])then
- if(#args == v.args)then
- local machinecode = v.call(unpack(args))
- if(machinecode == 0)then return true end
- local ef = fs.open(exp,'a')
- for i = 1, #machinecode do
- print(machinecode[i])
- ef.writeLine(machinecode[i])
- end
- ef.close()
- return true -- Done executing
- end
- end
- end
- end
- function CCASM(asm,exp)
- if(not fs.exists(asm))then print("Failed to compile program - " .. asm) end
- if(fs.exists(exp))then print("Cannot export to: " .. exp) end
- local f = fs.open(asm,'r')
- local lines = getFileLines(asm)
- local ef = fs.open(exp,'w')
- ef.write("")
- ef.close()
- for k, v in pairs(lines) do
- local line = v:gsub(",","")
- local line = string.upper(line)
- local assembly = {}
- local args = {}
- local ind = 0
- for word in line:gmatch("%w+") do
- if(ind >= 1)then
- table.insert(args, word)
- end
- table.insert(assembly, word)
- ind = ind + 1
- end
- compileASMFunction(assembly,args,exp)
- end
- end
- function BITGUI(opside, bside, clock)
- clk = clock
- busside = bside
- opcside = opside
- local history = {}
- local w, h = term.getSize()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.lime)
- term.clear()
- paintutils.drawLine(1,1,w,1,colors.gray)
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(1, 1)
- write("Computer Control")
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,2)
- while true do
- write(":> ")
- local command = read(nil,history)
- table.insert(history, command)
- local command = string.upper(command)
- local assembly = {}
- local args = {}
- local ind = 0
- command = command:gsub(","," ")
- for word in command:gmatch("%w+") do
- if(ind >= 1)then
- table.insert(args, word)
- end
- table.insert(assembly, word)
- ind = ind + 1
- end
- if(not execute(assembly[1],args,opside,bside,clock))then
- print("ERROR :> Unrecognized opperation: " .. assembly[1] .. " or Invalid Arguments")
- end
- end
- end
- BITGUI("bottom","back", 0.8)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement