Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- Mem arch
- -- mem[start][offs]
- function pool()
- local memlib = {
- _ASM = {
- ['mem'] = {}
- },
- blank = "0",
- lastmem = "0:0",
- setup = function(self,stack,size)
- local mem = {}
- for i = 1, stack do
- mem[i] = string.rep("0",size)
- end
- self._ASM['mem'] = mem
- end,
- fill = function(self,v)
- for y = 1, #self._ASM.mem do
- self._ASM.mem[y] = string.rep(v,#self._ASM.mem[y])
- end
- self.blank = v
- end,
- 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
- if(#bin < 4)then
- bin = string.rep("c0",4-#bin) .. bin
- end
- return 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,
- decode = function(a)
- local mems = {}
- for i in string.gmatch(a,"([^:]+)") do
- mems[#mems+1] = i
- end
- return tonumber(mems[1]), tonumber(mems[2])
- end,
- getlast = function(self)
- return self.decode(self.lastmem)
- end,
- encode = function(a,b)
- return tostring(a) .. ":" .. tostring(b)
- end,
- poke = function(self,a)
- local address, offs = self.decode(a)
- if(offs)then
- return self._ASM.mem[address]:sub(offs,offs)
- else
- return self._ASM.mem[address]
- end
- self.lastmem = a
- end,
- heap = function(self,a,offend)
- local address, offs = self.decode(a)
- self.lastmem = a
- return self._ASM.mem[address]:sub(offs,offs+offend-1)
- end,
- dup = function(self,a,v,m)
- local address, offs = self.decode(a)
- self._ASM.mem[address] = self._ASM.mem[address]:sub(1,offs-1) .. string.rep(v,m) .. self._ASM.mem[address]:sub(offs+size+1)
- self.lastmem = a
- end,
- invoke = function(self,a,v)
- local address, offs = self.decode(a)
- if(offs-1 > 0)then
- self._ASM.mem[address] = self._ASM.mem[address]:sub(1,offs-1) .. v .. self._ASM.mem[address]:sub(offs+#v,-1)
- else
- self._ASM.mem[address] = v .. self._ASM.mem[address]:sub(offs+#v,-1)
- end
- self.lastmem = a
- end,
- allocate = function(self,a,size)
- local address, offs = self.decode(a)
- self._ASM.mem[address] = self._ASM.mem[address]:sub(1,offs-1) .. string.rep(self.blank,size) .. self._ASM.mem[address]:sub(offs+size+1)
- self.lastmem = a
- end,
- swp = function(self,dest, source)
- local tdest = self:poke(dest)
- if(dest:find(":"))then
- self:invoke(dest,self:poke(source))
- self:invoke(source,tdest)
- return 1
- end
- end,
- mov = function(self,dest, source)
- local lod = 0
- -- if our source is binary
- if(source:sub(#source,#source)=='b')then
- source = source:sub(1,#source-1)
- source = self.bintohex(source)
- -- if our source is hex
- elseif(source:sub(#source,#source)=='h')then
- source = source:sub(1,#source-1)
- -- if our source is a word
- elseif(source:sub(#source,#source)=='w')then
- source = source:sub(1,#source-1)
- else
- -- If our source is memory address
- source = self:poke(source)
- end
- -- If our destination is a memory address
- if(dest:find(":"))then
- self:invoke(dest,source)
- self.lastmem = dest
- return 1
- end
- end,
- shift = function(self,a,oe,dir)
- local address, offs = self.decode(a)
- local data = self:heap(a,oe)
- self:allocate(self.encode(address,offs),oe)
- self:invoke(self.encode(address,(offs)+dir),data)
- -- 1: 2 + -1 = 1, invoked 1:1
- self.lastmem = a
- end,
- }
- return memlib
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement