2016-04-07 00:03:11 +02:00
|
|
|
local xml = {}
|
|
|
|
|
2017-06-03 00:59:55 +02:00
|
|
|
function xml.filename(name)
|
|
|
|
return name .. ".xml"
|
|
|
|
end
|
|
|
|
|
2016-04-07 00:03:11 +02:00
|
|
|
-- basic xml parser for mamecheat only
|
|
|
|
local function xml_parse(data)
|
2016-04-08 02:38:18 +02:00
|
|
|
local function fix_gt(str)
|
|
|
|
str = str:gsub(">=", " ge ")
|
|
|
|
str = str:gsub(">", " gt ")
|
|
|
|
return str
|
|
|
|
end
|
|
|
|
data = data:gsub("(condition=%b\"\")", fix_gt)
|
2016-04-07 00:03:11 +02:00
|
|
|
local cheat_str = data:match("<mamecheat.->(.*)</ *mamecheat>")
|
|
|
|
|
|
|
|
local function get_tags(str)
|
|
|
|
local arr = {}
|
|
|
|
while str ~= "" do
|
|
|
|
local tag, attr, stop
|
2016-08-19 17:56:31 +02:00
|
|
|
tag, attr, stop, str = str:match("<([%w!%-]+) ?(.-)(/?)[ %-]->(.*)")
|
2016-04-07 00:03:11 +02:00
|
|
|
|
|
|
|
if not tag then
|
|
|
|
return arr
|
|
|
|
end
|
|
|
|
if tag:sub(0, 3) ~= "!--" then
|
|
|
|
local block = {}
|
|
|
|
if stop ~= "/" then
|
|
|
|
local nest
|
|
|
|
nest, str = str:match("(.-)</ *" .. tag .. " *>(.*)")
|
|
|
|
local children = get_tags(nest)
|
|
|
|
if not next(children) then
|
2016-08-19 17:56:31 +02:00
|
|
|
nest = nest:gsub("<!--.-%-%->", "")
|
2016-04-07 00:03:11 +02:00
|
|
|
nest = nest:gsub("^%s*(.-)%s*$", "%1")
|
|
|
|
block["text"] = nest
|
|
|
|
else
|
|
|
|
block = children
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if attr then
|
|
|
|
for name, value in attr:gmatch("(%w-)=\"(.-)\"") do
|
|
|
|
block[name] = value:gsub("^%s*(.-)%s*$", "%1")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not arr[tag] then
|
|
|
|
arr[tag] = {}
|
|
|
|
end
|
|
|
|
arr[tag][#arr[tag] + 1] = block
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return arr
|
|
|
|
end
|
|
|
|
local xml_table = get_tags(cheat_str)
|
|
|
|
return xml_table
|
|
|
|
end
|
|
|
|
|
|
|
|
function xml.conv_cheat(data)
|
|
|
|
local spaces, regions, output
|
|
|
|
data = xml_parse(data)
|
2018-05-02 15:56:00 +02:00
|
|
|
local cpu_spaces = {}
|
|
|
|
|
|
|
|
for tag, device in pairs(manager:machine().devices) do
|
|
|
|
local sp
|
|
|
|
for name, space in pairs(device.spaces) do
|
|
|
|
if not sp then
|
|
|
|
sp = {}
|
|
|
|
cpu_spaces[tag] = sp
|
|
|
|
end
|
|
|
|
sp[space.index] = space.name
|
|
|
|
end
|
|
|
|
end
|
2018-05-02 15:42:56 +02:00
|
|
|
|
2016-04-07 00:03:11 +02:00
|
|
|
local function convert_expr(data)
|
|
|
|
local write = false
|
2016-08-30 07:53:27 +02:00
|
|
|
|
2016-07-03 02:04:32 +02:00
|
|
|
local function convert_memref(cpu, phys, space, width, addr, rw)
|
2018-05-02 15:42:56 +02:00
|
|
|
-- debug expressions address spaces by index not by name
|
|
|
|
local function get_space_name(index)
|
2020-01-04 14:06:13 +01:00
|
|
|
local prefix = cpu:sub(1,1)
|
|
|
|
if prefix == ":" then
|
|
|
|
return cpu_spaces[cpu][index]
|
|
|
|
else
|
|
|
|
return cpu_spaces[":" .. cpu][index]
|
|
|
|
end
|
2018-05-02 15:42:56 +02:00
|
|
|
end
|
|
|
|
|
2016-07-03 02:04:32 +02:00
|
|
|
local mod = ""
|
2016-04-07 17:37:44 +02:00
|
|
|
local count
|
2016-04-07 00:03:11 +02:00
|
|
|
if space == "p" then
|
2018-05-02 15:42:56 +02:00
|
|
|
fullspace = get_space_name(0)
|
2016-04-07 00:03:11 +02:00
|
|
|
elseif space == "d" then
|
2018-05-02 15:42:56 +02:00
|
|
|
fullspace = get_space_name(1)
|
2016-04-07 00:03:11 +02:00
|
|
|
elseif space == "i" then
|
2018-05-02 15:42:56 +02:00
|
|
|
fullspace = get_space_name(2)
|
2018-05-01 00:35:09 +02:00
|
|
|
elseif space == "r" then
|
2018-05-02 15:42:56 +02:00
|
|
|
fullspace = get_space_name(0)
|
2016-07-03 02:04:32 +02:00
|
|
|
mod = "direct_"
|
2016-04-07 00:03:11 +02:00
|
|
|
space = "p"
|
2018-05-01 00:35:09 +02:00
|
|
|
elseif space == "o" then
|
2018-05-02 15:42:56 +02:00
|
|
|
fullspace = get_space_name(3)
|
2018-05-01 00:35:09 +02:00
|
|
|
mod = "direct_"
|
|
|
|
space = "o"
|
2016-04-07 00:03:11 +02:00
|
|
|
end
|
|
|
|
if width == "b" then
|
|
|
|
width = "u8"
|
|
|
|
elseif width == "w" then
|
|
|
|
width = "u16"
|
|
|
|
elseif width == "d" then
|
|
|
|
width = "u32"
|
|
|
|
elseif width == "q" then
|
|
|
|
width = "u64"
|
|
|
|
end
|
2020-01-26 02:36:51 +01:00
|
|
|
|
2020-01-04 14:06:13 +01:00
|
|
|
local prefix = cpu:sub(1,1)
|
|
|
|
if prefix == ":" then
|
|
|
|
cpu = cpu:sub(2,cpu:len())
|
|
|
|
end
|
2020-01-26 02:36:51 +01:00
|
|
|
|
2016-04-07 00:03:11 +02:00
|
|
|
local cpuname = cpu:gsub(":", "_")
|
|
|
|
if space == "m" then
|
|
|
|
regions[cpuname .. space] = ":" .. cpu
|
|
|
|
else
|
|
|
|
spaces[cpuname .. space] = { tag = ":" .. cpu, type = fullspace }
|
2016-07-03 02:04:32 +02:00
|
|
|
if phys ~= "p" and mod == "" then
|
|
|
|
mod = "log_"
|
|
|
|
end
|
2016-04-07 00:03:11 +02:00
|
|
|
end
|
|
|
|
if rw == "=" then
|
|
|
|
write = true
|
2016-07-03 02:04:32 +02:00
|
|
|
ret = cpuname .. space .. ":" .. "write_" .. mod .. width .. "(" .. addr .. ","
|
2016-04-07 00:03:11 +02:00
|
|
|
else
|
2016-07-03 02:04:32 +02:00
|
|
|
ret = cpuname .. space .. ":" .. "read_" .. mod .. width .. "(" .. addr .. ")"
|
2016-04-07 00:03:11 +02:00
|
|
|
end
|
|
|
|
if rw == "==" then
|
|
|
|
ret = ret .. "=="
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
local function frame()
|
|
|
|
output = true
|
|
|
|
return "screen:frame_number()"
|
|
|
|
end
|
|
|
|
|
|
|
|
data = data:lower()
|
2016-04-07 17:37:44 +02:00
|
|
|
data = data:gsub("^[(](.-)[)]$", "%1")
|
2016-04-08 02:38:18 +02:00
|
|
|
data = data:gsub("%f[%w]lt%f[%W]", "<")
|
|
|
|
data = data:gsub("%f[%w]ge%f[%W]", ">=")
|
|
|
|
data = data:gsub("%f[%w]gt%f[%W]", ">")
|
|
|
|
data = data:gsub("%f[%w]le%f[%W]", "<=")
|
|
|
|
data = data:gsub("%f[%w]eq%f[%W]", "==")
|
|
|
|
data = data:gsub("%f[%w]ne%f[%W]", "~=")
|
2016-04-07 00:03:11 +02:00
|
|
|
data = data:gsub("!=", "~=")
|
2016-04-08 02:38:18 +02:00
|
|
|
data = data:gsub("||", " or ")
|
|
|
|
data = data:gsub("%f[%w]frame%f[%W]", frame)
|
|
|
|
data = data:gsub("%f[%w]band%f[%W]", "&")
|
|
|
|
data = data:gsub("%f[%w]bor%f[%W]", "|")
|
|
|
|
data = data:gsub("%f[%w]rshift%f[%W]", ">>")
|
|
|
|
data = data:gsub("%f[%w]lshift%f[%W]", "<<")
|
|
|
|
data = data:gsub("(%w-)%+%+", "%1 = %1 + 1")
|
2016-04-07 00:03:11 +02:00
|
|
|
data = data:gsub("%f[%w](%x+)%f[%W]", "0x%1")
|
2018-04-18 15:53:11 +02:00
|
|
|
-- 0?x? avoids an issue where db (data region byte) is interepeted as a hex number
|
|
|
|
data = data:gsub("([%w_:]-)%.(p?)0?x?([pmrodi3])([bwdq])@(%w+) *(=*)", convert_memref)
|
2016-04-07 17:37:44 +02:00
|
|
|
repeat
|
2018-04-18 15:53:11 +02:00
|
|
|
data, count = data:gsub("([%w_:]-)%.(p?)0?x?([pmrodi3])([bwdq])@(%b()) *(=*)", convert_memref)
|
2016-04-07 17:37:44 +02:00
|
|
|
until count == 0
|
2016-04-07 00:03:11 +02:00
|
|
|
if write then
|
|
|
|
data = data .. ")"
|
|
|
|
end
|
|
|
|
return data
|
|
|
|
end
|
|
|
|
|
|
|
|
local function convert_output(data)
|
|
|
|
local str = "draw_text(screen,"
|
|
|
|
if data["align"] then
|
|
|
|
str = str .. data["align"]
|
|
|
|
else
|
|
|
|
str = str .. "\"left\""
|
|
|
|
end
|
|
|
|
if data["line"] then
|
|
|
|
str = str .. ",\"" .. data["line"] .. "\""
|
|
|
|
else
|
|
|
|
str = str .. ", \"auto\""
|
|
|
|
end
|
|
|
|
str = str .. ", nil,\"" .. data["format"] .. "\""
|
2016-04-08 02:38:18 +02:00
|
|
|
if data["argument"] then
|
|
|
|
for count, block in pairs(data["argument"]) do
|
|
|
|
local expr = convert_expr(block["text"])
|
|
|
|
if block["count"] then
|
|
|
|
for i = 0, block["count"] - 1 do
|
|
|
|
str = str .. "," .. expr:gsub("argindex", i)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
str = str .. "," .. expr
|
2016-04-07 00:03:11 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return str .. ")"
|
|
|
|
end
|
|
|
|
|
|
|
|
local function convert_script(data)
|
|
|
|
local str = ""
|
|
|
|
local state = "run"
|
|
|
|
for tag, block in pairs(data) do
|
|
|
|
if tag == "state" then
|
|
|
|
state = block
|
|
|
|
elseif tag == "action" then
|
|
|
|
for count, action in pairs(block) do
|
|
|
|
if action["condition"] then
|
2016-04-07 17:37:44 +02:00
|
|
|
str = str .. " if (" .. convert_expr(action["condition"]) .. ") then "
|
2016-04-07 00:03:11 +02:00
|
|
|
for expr in action["text"]:gmatch("([^,]+)") do
|
|
|
|
str = str .. convert_expr(expr) .. " "
|
|
|
|
end
|
|
|
|
str = str .. "end"
|
|
|
|
else
|
|
|
|
for expr in action["text"]:gmatch("([^,]+)") do
|
|
|
|
str = str .. " " .. convert_expr(expr) .. " "
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif tag == "output" then
|
|
|
|
output = true
|
|
|
|
for count, output in pairs(block) do
|
|
|
|
if output["condition"] then
|
|
|
|
str = str .. " if " .. convert_expr(output["condition"]) .. " then "
|
|
|
|
str = str .. convert_output(output) .. " end "
|
|
|
|
else
|
|
|
|
str = str .. " " .. convert_output(output) .. " "
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return state, str
|
|
|
|
end
|
|
|
|
|
|
|
|
for count, cheat in pairs(data["cheat"]) do
|
|
|
|
spaces = {}
|
|
|
|
regions = {}
|
|
|
|
output = false
|
|
|
|
for tag, block in pairs(cheat) do
|
|
|
|
if tag == "comment" then
|
|
|
|
data["cheat"][count]["comment"] = block[1]["text"]
|
|
|
|
elseif tag == "script" then
|
|
|
|
local scripts = {}
|
|
|
|
for count2, script in pairs(block) do
|
|
|
|
local state, str = convert_script(script)
|
|
|
|
scripts[state] = str
|
|
|
|
end
|
|
|
|
data["cheat"][count]["script"] = scripts
|
|
|
|
elseif tag == "parameter" then
|
2016-04-07 17:37:44 +02:00
|
|
|
if block[1]["min"] then
|
|
|
|
block[1]["min"] = block[1]["min"]:gsub("%$","0x")
|
|
|
|
end
|
|
|
|
if block[1]["max"] then
|
|
|
|
block[1]["max"] = block[1]["max"]:gsub("%$","0x")
|
|
|
|
end
|
|
|
|
if block[1]["step"] then
|
|
|
|
block[1]["step"] = block[1]["step"]:gsub("%$","0x")
|
|
|
|
end
|
|
|
|
data["cheat"][count]["parameter"] = block[1]
|
2016-04-07 00:03:11 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if next(spaces) then
|
2016-08-30 07:53:27 +02:00
|
|
|
data["cheat"][count]["space"] = {}
|
2016-04-07 00:03:11 +02:00
|
|
|
for name, space in pairs(spaces) do
|
2016-08-30 07:53:27 +02:00
|
|
|
data["cheat"][count]["space"] = {}
|
2016-04-07 00:03:11 +02:00
|
|
|
data["cheat"][count]["space"][name] = { type = space["type"], tag = space["tag"] }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if next(regions) then
|
2016-08-30 07:53:27 +02:00
|
|
|
data["cheat"][count]["region"] = {}
|
2016-04-07 00:03:11 +02:00
|
|
|
for name, region in pairs(regions) do
|
2016-08-30 07:53:27 +02:00
|
|
|
data["cheat"][count]["region"] = {}
|
2016-04-07 00:03:11 +02:00
|
|
|
data["cheat"][count]["region"][name] = region
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if output then
|
|
|
|
data["cheat"][count]["screen"] = {}
|
|
|
|
data["cheat"][count]["screen"]["screen"] = ":screen"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return data["cheat"]
|
|
|
|
end
|
|
|
|
|
|
|
|
return xml
|