Difference between revisions of "Module:Address"

From ALttP Speedrunning Wiki
Jump to: navigation, search
Line 5: Line 5:
 
function p.main(frame)
 
function p.main(frame)
 
local args = frame:getParent().args
 
local args = frame:getParent().args
 +
local addr = args[1] or 'FF'
 +
local size = args[2] or 1
 +
 +
local trimmed, title, len = p._main(addr, size)
 +
 +
local ret = mw.html.create('code')
 +
:attr('title', title)
 +
:wikitext(trimmed)
 +
:wikitext(len)
 +
 +
return ret
 +
end
  
local addr = args[1] or 'FF'
+
function p._main(addr, size, localaddr)
 +
 
local addr10 = tonumber(HEX._convert(addr, 10, 16))
 
local addr10 = tonumber(HEX._convert(addr, 10, 16))
 
local addr10short = addr10 % 0x10000
 
local addr10short = addr10 % 0x10000
 
local bank = (addr10 - addr10short) / 0x10000
 
local bank = (addr10 - addr10short) / 0x10000
local size = args[2] or 1
 
local size10 = tonumber(HEX._convert(size, 10, 16))
 
  
local ret = mw.html.create('code')
+
local size10 = tonumber(HEX._convert(size or 1, 10, 16))
 +
 
 
local title = ''
 
local title = ''
 
local length = 0
 
local length = 0
 +
local len = ''
  
 
if addr10short <= 0xFF then
 
if addr10short <= 0xFF then
Line 24: Line 37:
 
length = 4
 
length = 4
 
elseif addr10short <= 0x7FFF then
 
elseif addr10short <= 0x7FFF then
title = 'Hardware register'
+
if localaddr then
 +
title = 'Local address'
 +
else
 +
title = 'Hardware register'
 +
end
 
length = 4
 
length = 4
 
else
 
else
Line 35: Line 52:
  
 
if size10 > 1 then
 
if size10 > 1 then
ret:wikitext(string.format('[0x%X]', size10))
+
lent = string.format('[0x%X]', size10)
 
title = title .. string.format('; 0x%X bytes (%s bytes) in length', size10, size10)
 
title = title .. string.format('; 0x%X bytes (%s bytes) in length', size10, size10)
 
end
 
end
  
ret:attr('title', title)
+
return ret, title, len
 
 
return ret
 
 
end
 
end
  
 
return p
 
return p

Revision as of 12:09, 9 April 2019

Documentation for this module may be created at Module:Address/doc

local p = {}

local HEX = require('Module:Base convert')

function p.main(frame)
	local args = frame:getParent().args
	local addr = args[1] or 'FF'
	local size = args[2] or 1

	local trimmed, title, len = p._main(addr, size)

	local ret = mw.html.create('code')
		:attr('title', title)
		:wikitext(trimmed)
		:wikitext(len)

	return ret
end

function p._main(addr, size, localaddr)
	
	local addr10 = tonumber(HEX._convert(addr, 10, 16))
	local addr10short = addr10 % 0x10000
	local bank = (addr10 - addr10short) / 0x10000

	local size10 = tonumber(HEX._convert(size or 1, 10, 16))

	local title = ''
	local length = 0
	local len = ''

	if addr10short <= 0xFF then
		title = 'Direct Page address'
		length = 2
	elseif addr10short <= 0x1FFF then
		title = 'Short address'
		length = 4
	elseif addr10short <= 0x7FFF then
		if localaddr then
			title = 'Local address'
		else
			title = 'Hardware register'
		end
		length = 4
	else
		title = string.format('Long address $%06X; Bank %02X', addr10, bank)
		length = 6
	end

	
	ret:wikitext(string.format('$%0'..length..'X', addr10))

	if size10 > 1 then
		lent = string.format('[0x%X]', size10)
		title = title .. string.format('; 0x%X bytes (%s bytes) in length', size10, size10)
	end

	return ret, title, len
end

return p