User File #49090278993160476

Upload All User Files

#49090278993160476 - smb lua 2

smb.lua
789 downloads
Uploaded 8/11/2018 6:39 PM by xy2_ (see all 139)
mword = memory.readdwordsigned
mwordu = memory.readdwordunsigned

local ds_width = 256
local ds_height = 192

function toBits(num,bits)
	-- Simple binary packing function
	-- To unpack, tonumber(num, 2)
	-- Returns a table of bits, most significant first.
	bits = bits or math.max(1, select(2, math.frexp(num)))
	local t = {} -- will contain the bits        
	for b = bits, 1, -1 do
		t[b] = math.fmod(num, 2)
		num = math.floor((num - t[b]) / 2)
	end
	return table.concat(t)
end

-- https://en.wikipedia.org/wiki/Q_(number_format)#Characteristics
-- Here, the sign bit is included in m, because n+m = 32, which is equal to
-- the length of the DS adress bus
-- Unpacking function
local function Q(number, m)
	local n = 32-m
	local max = m+n
	local packed = toBits(number, 32)
	
	local sign = tonumber(string.sub(packed, 1, 1))
	local unsignedpacked = string.sub(packed, 2, max)
	
	-- If the number is signed: NOT the number, add 1 (one's complement)
	if sign == 1 then
		packed = toBits(bit.bnot(tonumber(packed,2))+1,32)
	end
	
	local integer = tonumber(string.sub(packed, 2, m),2) -- As usual, Lua indexes start at 1..
	local fractional = (tonumber(string.sub(packed, m+1, max), 2))/(2^n)
	local total = integer + fractional
	
	if sign == 1 then
		total = total * -1
	end
	
	return total
end

local function main()
	local pointer1 = mwordu(0x021FBA14)
	local zpos = Q(mwordu(pointer1 + 0xC),20)
	local ypos = Q(mwordu(pointer1 + 0x10),20)
	local xpos = Q(mwordu(pointer1 + 0x14),20)
	
	local zdelta = Q(mwordu(pointer1 + 0x24),20)
	local ydelta = Q(mwordu(pointer1 + 0x28),20)
	local xdelta = Q(mwordu(pointer1 + 0x2C),20)
	
	local zspeed = Q(mwordu(pointer1 + 0x139),20)
	local yspeed = Q(mwordu(pointer1 + 0x13C),20)
	local xspeed = Q(mwordu(pointer1 + 0x140),20)
	
	local time = memory.readwordunsigned(0x021FBA20)
	local minutes = math.floor(time/60)
	local seconds = string.format("%02d", math.floor(((time%60)*100/60)))
	local frame = math.floor(time%60)
	
	local speed = Q(mwordu(0x020DDBAC),16)

	gui.text(0,0,xpos)
	gui.text(0,8,ypos)
	gui.text(0,16,zpos)
	
	gui.text(0,28,xdelta,"green")
	gui.text(0,36,ydelta,"green")
	gui.text(0,44,zdelta,"green")
	
	gui.text(100,28,xspeed,"teal")
	gui.text(100,36,yspeed,"teal")
	gui.text(100,44,zspeed,"teal")
	
	gui.text(0,60,speed,"teal")

	gui.text(ds_width - 82,0,time)
	gui.text(ds_width - 52,0,minutes..":"..seconds.."["..frame.."]","yellow")

end

gui.register(main)