-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcross emu.lua
More file actions
365 lines (323 loc) · 11.7 KB
/
cross emu.lua
File metadata and controls
365 lines (323 loc) · 11.7 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
local xemu = {}
xemu.emuId_bizhawk = 0
xemu.emuId_snes9x = 1
xemu.emuId_lsnes = 2
xemu.emuId_mesen = 3
local is_atleast_bizhawk_v29 = false -- Detect BizHawk v2.9+, which changes how bitwise operations are written
if memory then
if memory.usememorydomain then
xemu.emuId = xemu.emuId_bizhawk
if bizstring.pad_start then
is_atleast_bizhawk_v29 = true
end
elseif memory.readshort then
xemu.emuId = xemu.emuId_snes9x
else
xemu.emuId = xemu.emuId_lsnes
end
else
xemu.emuId = xemu.emuId_mesen
end
-- Bitwise operations
if xemu.emuId == xemu.emuId_mesen or is_atleast_bizhawk_v29 then
-- [[
xemu_mesen = require("cross emu - mesen")
xemu.rshift = xemu_mesen.rshift
xemu.lshift = xemu_mesen.lshift
xemu.not_ = xemu_mesen.not_
xemu.and_ = xemu_mesen.and_
xemu.or_ = xemu_mesen.or_
xemu.xor = xemu_mesen.xor
--]]
else
if xemu.emuId == xemu.emuId_lsnes then
xemu.rshift = bit.lrshift -- (logical) right shift
else
xemu.rshift = bit.rshift
end
xemu.lshift = bit.lshift
xemu.not_ = bit.bnot
xemu.and_ = bit.band
xemu.or_ = bit.bor
xemu.xor = bit.bxor
end
-- Converts from SNES address model to flat address model (for ROM access)
function snes2pc(p)
return xemu.and_(xemu.rshift(p, 1), 0x3F8000) + xemu.and_(p, 0x7FFF)
end
-- Define memory access functions
if xemu.emuId == xemu.emuId_bizhawk then
if memory.getmemorydomainsize("CARTRIDGE_ROM") ~= memory.getcurrentmemorydomainsize() then
romDomainName = "CARTRIDGE_ROM" -- used by BSNES core
elseif memory.getmemorydomainsize("CARTROM") ~= memory.getcurrentmemorydomainsize() then
romDomainName = "CARTROM" -- used by snes9x core
end
function makeMemoryReader(f)
return function(p)
if p < 0x800000 then
return f(xemu.and_(p, 0x1FFFF), "WRAM")
else
return f(snes2pc(p), romDomainName)
end
end
end
function makeMemoryWriter(f)
return function(p, v)
if p < 0x800000 then
return f(xemu.and_(p, 0x1FFFF), v, "WRAM")
else
print(string.format('Error: trying to write to ROM address %X', p))
end
end
end
function makeAramReader(f)
return function(p)
return f(p, "APURAM")
end
end
xemu.read_u8 = makeMemoryReader(memory.read_u8)
xemu.read_u16_le = makeMemoryReader(memory.read_u16_le)
xemu.read_s8 = makeMemoryReader(memory.read_s8)
xemu.read_s16_le = makeMemoryReader(memory.read_s16_le)
xemu.write_u8 = makeMemoryWriter(memory.write_u8)
xemu.write_u16_le = makeMemoryWriter(memory.write_u16_le)
xemu.read_aram_u8 = makeAramReader(memory.read_u8)
xemu.read_aram_u16_le = makeAramReader(memory.read_u16_le)
xemu.read_aram_s8 = makeAramReader(memory.read_s8)
xemu.read_aram_s16_le = makeAramReader(memory.read_s16_le)
elseif xemu.emuId == xemu.emuId_snes9x then
xemu.read_u8 = memory.readbyte
xemu.read_u16_le = memory.readshort
xemu.read_s8 = memory.readbytesigned
xemu.read_s16_le = memory.readshortsigned
xemu.write_u8 = memory.writebyte
xemu.write_u16_le = memory.writeshort
elseif xemu.emuId == xemu.emuId_lsnes then
function makeMemoryReader(f)
return function(p)
if p < 0x800000 then
return f(p)
else
return f("ROM", snes2pc(p))
end
end
end
function makeMemoryWriter(f)
return function(p, v)
if p < 0x800000 then
return f(p, v)
else
print(string.format('Error: trying to write to ROM address %X', p))
end
end
end
xemu.read_u8 = makeMemoryReader(memory.readbyte)
xemu.read_u16_le = makeMemoryReader(memory.readword)
xemu.read_s8 = makeMemoryReader(memory.readsbyte)
xemu.read_s16_le = makeMemoryReader(memory.readsword)
xemu.write_u8 = makeMemoryWriter(memory.writebyte)
xemu.write_u16_le = makeMemoryWriter(memory.writeword)
else -- xemu.emuId == xemu.emuId_mesen
function makeMemoryReader(f, isSigned)
return function(p)
if p < 0x800000 then
return f(p, emu.memType.workRam, isSigned)
else
return f(p, emu.memType.prgRom, isSigned)
end
end
end
function makeMemoryWriter(f)
return function(p, v)
if p < 0x800000 then
return f(xemu.and_(p, 0x1FFFF), v, emu.memType.workRam)
else
print(string.format('Error: trying to write to ROM address %X', p))
end
end
end
xemu.read_u8 = makeMemoryReader(emu.read, false)
xemu.read_u16_le = makeMemoryReader(emu.readWord, false)
xemu.read_s8 = makeMemoryReader(emu.read, true)
xemu.read_s16_le = makeMemoryReader(emu.readWord, true)
xemu.write_u8 = makeMemoryWriter(emu.write)
xemu.write_u16_le = makeMemoryWriter(emu.writeWord)
end
-- GUI functions
if xemu.emuId == xemu.emuId_bizhawk then
xemu.drawPixel = function(x, y, fg)
if fg ~= nil and type(fg) ~= "string" then
fg = xemu.rshift(fg, 8) + xemu.lshift(xemu.and_(fg, 0xFF), 0x18)
end
gui.drawPixel(x, y, fg)
end
xemu.drawBox = function(x0, y0, x1, y1, fg, bg)
if fg ~= nil and type(fg) ~= "string" then
fg = xemu.rshift(fg, 8) + xemu.lshift(xemu.and_(fg, 0xFF), 0x18)
end
if bg ~= nil and type(bg) ~= "string" then
bg = xemu.rshift(bg, 8) + xemu.lshift(xemu.and_(bg, 0xFF), 0x18)
end
gui.drawBox(x0, y0, x1, y1, fg, bg)
end
xemu.drawLine = function(x0, y0, x1, y1, fg)
if fg ~= nil and type(fg) ~= "string" then
fg = xemu.rshift(fg, 8) + xemu.lshift(xemu.and_(fg, 0xFF), 0x18)
end
-- BizHawk fails to draw 1px x 1px lines
if x0 == x1 and y0 == y1 then
xemu.drawPixel(x0, y0, fg)
return
end
gui.drawLine(x0, y0, x1, y1, fg)
end
xemu.drawText = function(x, y, text, fg, bg)
if fg ~= nil and type(fg) ~= "string" then
fg = xemu.rshift(fg, 8) + xemu.lshift(xemu.and_(fg, 0xFF), 0x18)
end
if bg ~= nil and type(bg) ~= "string" then
bg = xemu.rshift(bg, 8) + xemu.lshift(xemu.and_(bg, 0xFF), 0x18)
end
gui.pixelText(x, y, text, fg, bg)
end
elseif xemu.emuId == xemu.emuId_snes9x then
xemu.drawPixel = gui.pixel
xemu.drawBox = function(x0, y0, x1, y1, fg, bg) gui.box(x0, y0, x1, y1, bg or 0, fg) end
xemu.drawLine = gui.line
xemu.drawText = gui.text
elseif emuId == xemu.emuId_lsnes then
function decodeColour(colour)
if colour == nil then
return nil
end
if type(colour) == "string" then
if colour == "red" then
return 0xFF0000
elseif colour == "orange" then
return 0x808000
elseif colour == "yellow" then
return 0xFFFF00
elseif colour == "white" then
return 0xFFFFFF
elseif colour == "black" then
return 0x000000
elseif colour == "green" then
return 0x00FF00
elseif colour == "purple" then
return 0xFF00FF
elseif colour == "cyan" then
return 0x00FFFF
elseif colour == "blue" then
return 0x0000FF
elseif colour == "clear" then
return 0xFF000000
else
print(string.format("Colour = %s", colour))
return
end
end
return xemu.and_(colour, 0xFFFFFF)
end
xemu.drawPixel = function(x, y, fg)
local n_x, n_y = gui.resolution()
local s_x = n_x / 256
local s_y = n_y / 224
x = math.floor(x * s_x)
y = math.floor(y * s_y)
gui.pixel(x, y, decodeColour(fg))
end
xemu.drawBox = function(x0, y0, x1, y1, fg, bg)
local n_x, n_y = gui.resolution()
local s_x = n_x / 256
local s_y = n_y / 224
x0, x1 = math.min(x0, x1), math.max(x0, x1)
y0, y1 = math.min(y0, y1), math.max(y0, y1)
x0 = math.floor(x0 * s_x)
y0 = math.floor(y0 * s_y)
x1 = math.floor(x1 * s_x)
y1 = math.floor(y1 * s_y)
gui.rectangle(x0, y0, x1 - x0, y1 - y0, 1, decodeColour(fg), decodeColour(bg))
end
xemu.drawLine = function(x0, y0, x1, y1, fg)
local n_x, n_y = gui.resolution()
local s_x = n_x / 256
local s_y = n_y / 224
x0 = math.floor(x0 * s_x)
y0 = math.floor(y0 * s_y)
x1 = math.floor(x1 * s_x)
y1 = math.floor(y1 * s_y)
gui.line(x0, y0, x1, y1, decodeColour(fg))
end
xemu.drawText = function(x, y, text, fg, bg)
local n_x, n_y = gui.resolution()
local s_x = n_x / 256
local s_y = n_y / 224
x = math.floor(x * s_x)
y = math.floor(y * s_y)
gui.text(x, y, text, decodeColour(fg), decodeColour(bg))
end
else -- xemu.emuId == xemu.emuId_mesen
function decodeColour(colour)
if colour == nil then
return nil
end
if type(colour) == "string" then
if colour == "red" then
return 0xFF0000
elseif colour == "orange" then
return 0x808000
elseif colour == "yellow" then
return 0xFFFF00
elseif colour == "white" then
return 0xFFFFFF
elseif colour == "black" then
return 0x000000
elseif colour == "green" then
return 0x00FF00
elseif colour == "purple" then
return 0xFF00FF
elseif colour == "cyan" then
return 0x00FFFF
elseif colour == "blue" then
return 0x0000FF
elseif colour == "clear" then
return 0xFF000000
else
print(string.format("Colour = %s", colour))
return
end
end
return xemu.and_(colour, 0xFFFFFF)
end
xemu.drawPixel = function(x, y, fg)
if fg ~= nil and type(fg) ~= "string" then
fg = xemu.rshift(fg, 8) + xemu.lshift(0xFF - xemu.and_(fg, 0xFF), 0x18)
end
emu.drawPixel(x, y + 7, decodeColour(fg))
end
xemu.drawBox = function(x0, y0, x1, y1, fg, bg)
if fg ~= nil and type(fg) ~= "string" then
fg = xemu.rshift(fg, 8) + xemu.lshift(0xFF - xemu.and_(fg, 0xFF), 0x18)
end
if bg ~= nil and type(bg) ~= "string" then
bg = xemu.rshift(bg, 8) + xemu.lshift(0xFF - xemu.and_(bg, 0xFF), 0x18)
end
emu.drawRectangle(x0, y0 + 6, x1 - x0 + 1, y1 - y0 + 1, decodeColour(fg), bg == fg)
end
xemu.drawLine = function(x0, y0, x1, y1, fg)
if fg ~= nil and type(fg) ~= "string" then
fg = xemu.rshift(fg, 8) + xemu.lshift(0xFF - xemu.and_(fg, 0xFF), 0x18)
end
emu.drawLine(x0, y0 + 6, x1, y1 + 6, decodeColour(fg))
end
xemu.drawText = function(x, y, text, fg, bg)
if fg ~= nil and type(fg) ~= "string" then
fg = xemu.rshift(fg, 8) + xemu.lshift(0xFF - xemu.and_(fg, 0xFF), 0x18)
end
if bg ~= nil and type(bg) ~= "string" then
bg = xemu.rshift(bg, 8) + xemu.lshift(0xFF - xemu.and_(bg, 0xFF), 0x18)
end
emu.drawString(x, y + 7, text, decodeColour(fg), decodeColour(bg))
end
end
return xemu