1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
local xml = {}
function xml.filename(name)
return name .. ".xml"
end
-- basic xml parser for mamecheat only
local function xml_parse(data)
local function fix_gt(str)
str = str:gsub(">=", " ge ")
str = str:gsub(">", " gt ")
return str
end
data = data:gsub("(condition=%b\"\")", fix_gt)
local cheat_str = data:match("<mamecheat.->(.*)</ *mamecheat>")
local function get_tags(str)
local arr = {}
while str ~= "" do
local tag, attr, stop
tag, attr, stop, str = str:match("<([%w!%-]+) ?(.-)(/?)[ %-]->(.*)")
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
nest = nest:gsub("<!--.-%-%->", "")
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)
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
local function convert_expr(data)
local write = false
local function convert_memref(cpu, phys, space, width, addr, rw)
-- debug expressions address spaces by index not by name
local function get_space_name(index)
return cpu_spaces[":" .. cpu][index]
end
local mod = ""
local count
if space == "p" then
fullspace = get_space_name(0)
elseif space == "d" then
fullspace = get_space_name(1)
elseif space == "i" then
fullspace = get_space_name(2)
elseif space == "r" then
fullspace = get_space_name(0)
mod = "direct_"
space = "p"
elseif space == "o" then
fullspace = get_space_name(3)
mod = "direct_"
space = "o"
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
local cpuname = cpu:gsub(":", "_")
if space == "m" then
regions[cpuname .. space] = ":" .. cpu
else
spaces[cpuname .. space] = { tag = ":" .. cpu, type = fullspace }
if phys ~= "p" and mod == "" then
mod = "log_"
end
end
if rw == "=" then
write = true
ret = cpuname .. space .. ":" .. "write_" .. mod .. width .. "(" .. addr .. ","
else
ret = cpuname .. space .. ":" .. "read_" .. mod .. width .. "(" .. addr .. ")"
end
if rw == "==" then
ret = ret .. "=="
end
return ret
end
local function frame()
output = true
return "screen:frame_number()"
end
data = data:lower()
data = data:gsub("^[(](.-)[)]$", "%1")
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]", "~=")
data = data:gsub("!=", "~=")
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")
data = data:gsub("%f[%w](%x+)%f[%W]", "0x%1")
-- 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)
repeat
data, count = data:gsub("([%w_:]-)%.(p?)0?x?([pmrodi3])([bwdq])@(%b()) *(=*)", convert_memref)
until count == 0
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"] .. "\""
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
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
str = str .. " if (" .. convert_expr(action["condition"]) .. ") then "
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
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]
end
end
if next(spaces) then
data["cheat"][count]["space"] = {}
for name, space in pairs(spaces) do
data["cheat"][count]["space"] = {}
data["cheat"][count]["space"][name] = { type = space["type"], tag = space["tag"] }
end
end
if next(regions) then
data["cheat"][count]["region"] = {}
for name, region in pairs(regions) do
data["cheat"][count]["region"] = {}
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
|