Module:MemoryAddress/SnesToPC/LoROM: Difference between revisions

From ALttPR Wiki
Jump to navigation Jump to search
(Created page with "local floor = math.floor local OR, AND, LSHIFT, RSHIFT = 1, 4, 5, 6 local function blshift(x, bits) return floor(x) * (2^bits) end local function brshift(x, bits) retur...")
 
No edit summary
Line 52: Line 52:
   return
   return
         bitoper(
         bitoper(
                bitoper(
          bitoper(
                        bitoper(
            bitoper(
                                n,
                    n,
                                0x7F0000,
                    0x7F0000,
                                AND
                    AND
                        ),
            ),
                        1,
            1,
                        RSHIFT
            RSHIFT
                ),
          ),
                bitoper(
          bitoper(
                        n,
                  n,
                        0x7FFF,
                  0x7FFF,
                        AND
                  AND
                ),
          ),
                OR
          OR
         )
         )
end
end

Revision as of 17:51, 27 March 2020

local floor = math.floor

local OR, AND, LSHIFT, RSHIFT = 1, 4, 5, 6

local function blshift(x, bits)

 return floor(x) * (2^bits)

end

local function brshift(x, bits)

 return floor(floor(x) / (2^bits))

end

local function BitAND(a,b)--Bitwise and

 local p,c=1,0
 while a>0 and b>0 do
   local ra,rb=a%2,b%2
   if ra+rb>1 then c=c+p end
   a,b,p=(a-ra)/2,(b-rb)/2,p*2
 end
 return c

end

local function BitOR(a,b)--Bitwise or

 local p,c=1,0
 while a+b>0 do
   local ra,rb=a%2,b%2
   if ra+rb>0 then c=c+p end
   a,b,p=(a-ra)/2,(b-rb)/2,p*2
 end
 return c

end

local function bitoper(a, b, oper)

 local r = 0
 if oper == 1 then -- OR
   r = BitOR(a, b)
 elseif oper == 4 then -- AND
   r = BitAND(a, b)
 elseif oper == 5 then -- LSHIFT
   r = blshift(a, b)
 elseif oper == 6 then -- RSHIFT
   r = brshift(a, b)
 end
 return r

end

local p = {}

-- SNES to PC function p._convert(n)

 -- (((n & 0x7F0000) >> 1) | (n & 0x7FFFF))
 return
        bitoper(
          bitoper(
            bitoper(
                    n,
                    0x7F0000,
                    AND
            ),
            1,
            RSHIFT
          ),
          bitoper(
                  n,
                  0x7FFF,
                  AND
          ),
          OR
        )

end

function p.convert(frame)

 -- Allow for invocation via #invoke or directly from another module
 local args
 if frame == mw.getCurrentFrame() then
   args = frame.args
 else
   args = frame
 end
 local n = args[1]
 return p._convert(n)

end

return p