Advertisement
Redxone

[CC - MemEdit] Memory Editor API

Jan 14th, 2017
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.04 KB | None | 0 0
  1. --- Mem arch
  2. -- mem[start][offs]
  3. function pool()
  4. local memlib = {
  5.   _ASM = {
  6.     ['mem'] = {}
  7.   },
  8.   blank = "0",
  9.   lastmem = "0:0",
  10.  
  11.   setup = function(self,stack,size)
  12.         local mem = {}
  13.     for i = 1, stack do
  14.         mem[i] = string.rep("0",size)
  15.     end
  16.     self._ASM['mem'] = mem
  17.   end,
  18.  
  19.   fill = function(self,v)
  20.       for y = 1, #self._ASM.mem do
  21.          self._ASM.mem[y] = string.rep(v,#self._ASM.mem[y])
  22.       end
  23.       self.blank = v
  24.   end,
  25.  
  26.   hextobin = function(hex)
  27.     -- Convert to base 10 soooo easy
  28.     local dec = tonumber(hex,16)
  29.     local bin = ""
  30.     local binkey = "01"
  31.     local rim
  32.     while dec > 0 do
  33.        rim = math.floor(dec%2)
  34.        bin = binkey:sub(rim+1,rim+1) .. bin
  35.        dec = math.floor(dec/2)
  36.     end
  37.     if(#bin < 4)then
  38.       bin = string.rep("c0",4-#bin) .. bin
  39.     end
  40.     return bin
  41.   end,
  42.  
  43.   bintohex = function(bin)
  44.     local dec = tonumber(tostring(bin),2)
  45.     local hex = ""
  46.     local hexkey = "0123456789ABCDEF"
  47.     local rim
  48.     if(dec == 0)then hex = "0" end
  49.     while dec > 0 do
  50.        rim = math.floor(dec%16)
  51.        hex = hexkey:sub(rim+1,rim+1) .. hex
  52.        dec = math.floor(dec/16)
  53.     end
  54.     return hex
  55.   end,
  56.  
  57.   decode = function(a)
  58.     local mems = {}
  59.     for i in string.gmatch(a,"([^:]+)") do
  60.       mems[#mems+1] = i
  61.     end
  62.     return tonumber(mems[1]), tonumber(mems[2])
  63.   end,
  64.  
  65.   getlast = function(self)
  66.       return self.decode(self.lastmem)
  67.   end,
  68.  
  69.   encode = function(a,b)
  70.     return tostring(a) .. ":" .. tostring(b)
  71.   end,
  72.  
  73.   poke = function(self,a)
  74.     local address, offs = self.decode(a)
  75.     if(offs)then
  76.       return self._ASM.mem[address]:sub(offs,offs)
  77.     else
  78.       return self._ASM.mem[address]
  79.     end
  80.     self.lastmem = a
  81.   end,
  82.  
  83.   heap = function(self,a,offend)
  84.       local address, offs = self.decode(a)
  85.       self.lastmem = a
  86.       return self._ASM.mem[address]:sub(offs,offs+offend-1)
  87.   end,
  88.  
  89.   dup = function(self,a,v,m)
  90.      local address, offs = self.decode(a)
  91.      self._ASM.mem[address] = self._ASM.mem[address]:sub(1,offs-1) .. string.rep(v,m) .. self._ASM.mem[address]:sub(offs+size+1)
  92.      self.lastmem = a
  93.   end,
  94.   invoke = function(self,a,v)
  95.     local address, offs = self.decode(a)
  96.      if(offs-1 > 0)then
  97.       self._ASM.mem[address] = self._ASM.mem[address]:sub(1,offs-1) .. v .. self._ASM.mem[address]:sub(offs+#v,-1)
  98.      else
  99.       self._ASM.mem[address] = v .. self._ASM.mem[address]:sub(offs+#v,-1)
  100.      end
  101.       self.lastmem = a
  102.   end,
  103.   allocate = function(self,a,size)
  104.       local address, offs = self.decode(a)
  105.       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)
  106.       self.lastmem = a
  107.   end,
  108.  
  109.   swp = function(self,dest, source)
  110.      local tdest = self:poke(dest)
  111.      if(dest:find(":"))then
  112.          self:invoke(dest,self:poke(source))
  113.          self:invoke(source,tdest)
  114.          return 1
  115.      end
  116.   end,
  117.  
  118.   mov = function(self,dest, source)
  119.        local lod = 0
  120.        -- if our source is binary
  121.        if(source:sub(#source,#source)=='b')then
  122.          source = source:sub(1,#source-1)
  123.          source = self.bintohex(source)
  124.        -- if our source is hex
  125.        elseif(source:sub(#source,#source)=='h')then
  126.           source = source:sub(1,#source-1)
  127.        -- if our source is a word
  128.        elseif(source:sub(#source,#source)=='w')then
  129.           source = source:sub(1,#source-1)
  130.        else
  131.           -- If our source is memory address
  132.           source = self:poke(source)
  133.        end
  134.        -- If our destination is a memory address
  135.        if(dest:find(":"))then
  136.          self:invoke(dest,source)
  137.          self.lastmem = dest
  138.          return 1
  139.        end
  140.      end,
  141.  
  142.     shift = function(self,a,oe,dir)
  143.       local address, offs = self.decode(a)
  144.       local data = self:heap(a,oe)
  145.       self:allocate(self.encode(address,offs),oe)
  146.       self:invoke(self.encode(address,(offs)+dir),data)
  147.       -- 1: 2 + -1 = 1, invoked 1:1
  148.       self.lastmem = a
  149.     end,
  150. }
  151. return memlib
  152. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement