Last active
December 4, 2021 03:19
-
-
Save oleavr/245a11bd01e4a241b1c94df9f2c5139c to your computer and use it in GitHub Desktop.
VICE bridge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const vice = Process.getModuleByName('/usr/lib/c64emu.rgl'); | |
const mainloopOuterLoop = vice.getExportByName('maincpu_mainloop').add(0xf4); | |
const memStore = new NativeFunction(vice.getExportByName('mem_store'), 'void', ['uint16', 'uint8'], { exceptions: 'propagate' }); | |
const ioPending = Memory.alloc(4); | |
const ioCallbacks = []; | |
function poke(address, value) { | |
schedule(() => { memStore(address, value); }); | |
} | |
function schedule(callback) { | |
ioCallbacks.push(callback); | |
ioPending.writeU32(1); | |
} | |
const cm = new CModule(` | |
#include <glib.h> | |
extern gboolean io_pending; | |
extern void process_io (void); | |
void | |
on_tick (void) | |
{ | |
if (io_pending) | |
process_io (); | |
} | |
`, { | |
io_pending: ioPending, | |
process_io: new NativeCallback(() => { | |
let callback; | |
while ((callback = ioCallbacks.shift()) !== undefined) { | |
try { | |
callback(); | |
} catch (e) { | |
Script.nextTick(() => { throw e; }); | |
} | |
} | |
ioPending.writeU32(0); | |
}, 'void', []), | |
}); | |
Interceptor.attach(mainloopOuterLoop, cm.on_tick); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment