Module:Instruction set table builder

From N64brew Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Instruction set table builder/doc

local getArgs = require('Module:Arguments').getArgs
local p = {}

function string.split(str, len)
	local output = {};
	for match in str:gmatch("."..string.rep(".?", len - 1)) do
		if(#match > 0) then 
			table.insert(output, match);
		end
	end
	return output;
end

function string.addSpacing(str, len)
	local tab = string.split(str, len)
	
	local output = ""
	for i = 1, #tab, 1 do
		output = output .. tab[i] .. " "
	end
	
	return output
end

function formatMnemonic(str)
	str = str .. " "
	str = str:gsub("rd ", "r<sub>d</sub> ")
	str = str:gsub("rd,", "r<sub>d</sub>,")
	str = str:gsub("rs", "r<sub>s</sub>")
	str = str:gsub("rt", "r<sub>t</sub>")
	
	str = str:gsub("r'd", "rd")
	str = str:gsub("r't", "rt")
	str = str:gsub("r's", "rs")
	
	return str
end

function formatDescription(str)
	str = formatMnemonic(str)
	str = str:gsub("immediate", "\'\'immediate\'\'")
	str = str:gsub("sa", "\'\'sa\'\'")
	str = str:gsub("offset", "\'\'offset\'\'")
	str = str:gsub("target", "\'\'target\'\'")
	str = str:gsub("LO", "\'\'LO\'\'")
	str = str:gsub("HI", "\'\'HI\'\'")
	
	return str
end

function p.notation( frame )
    local args = getArgs(frame)
    local s = "{| class=\"wikitable\" style=\"text-align: center;\"\n! Symbol !! Description\n"
    
    local arr = {}
    for k,v in pairs(args) do
    	table.insert(arr, v)
    end
    
    for i = 1, #arr, 2 do
    	s = s .. "|-\n"
		s = s .. "| " .. arr[i] .. " || style=\"text-align: left;\"| " .. arr[i + 1] .. "\n"
	end
    
    s = s .. "|}\n"
    
	return s
end

function p.set( frame )
	local args = getArgs(frame)
    local s = "{| class=\"wikitable\" style=\"text-align: center; font-size: 85%;\"\n! Mnemonic !! Description !! 32-bit Instruction Word\n"
    
    local arr = {}
    for k,v in pairs(args) do
    	table.insert(arr, v)
    end
    
    for i = 1, #arr, 4 do
    	local mnemonic = formatMnemonic( string.gsub(string.format("%-10s", arr[i]), " ", "&nbsp;") .. arr[i + 1] )
    	mnemonic = mnemonic:gsub(arr[i], "[[#"..arr[i].."|"..arr[i].."]]")
    	
    	local descrip = formatDescription( arr[i + 2] )
    	local word = string.addSpacing(string.gsub(arr[i + 3], " ", ""), 4)
    	
    	s = s .. "|-\n"
		s = s .. "| style=\"text-align: left; font-family: Consolas,Monaco,Lucida Console,monospace; padding: 1px 7px 1px 7px;\"| " .. mnemonic
			.. " || style=\"text-align: left; font-family: Consolas,Monaco,Lucida Console,monospace; padding: 1px 7px 1px 7px;\"| " .. descrip
			.. " || style=\"font-family: Consolas,Monaco,Lucida Console,monospace; padding: 1px 7px 1px 7px;\"| " .. word
			.. "\n"
	end
    
    s = s .. "|}\n"
    
	return s
end

return p