Lua Scripting (Mesen)
Page 2 of 2

Author:  Sour [ Fri Aug 25, 2017 5:17 pm ]
Post subject:  Re: Lua Scripting (Mesen)

Got most of it working now - just a matter of implementing the API at this point.
Here's what it looks like in the UI. It's possible to have multiple script windows open, each running in their own independent "sandbox".
scriptwindow.png [ 63.54 KiB | Viewed 787 times ]

For the API, so far I've got this roughly planned:
//read/write have side-effects, debugRead/debugWrite have none.
//Allows you to read all kinds of memory (cpu, ppu, chr rom/ram, prg rom/ram, oam, etc.), type): int
emu.write(address, value, type): void
emu.debugRead(address, type): int
emu.debugWrite(address, value, type): void

//The "state" object should contain most of what is important about the emulator's state
//CPU, PPU, APU & Cartridge
emu.getState(): table
emu.setState(state: table): void

//Callbacks for read/write/exec and events like nmi, irq, reset, start of frame, end of frame
//The bulk of a script's code will need to be in these callbacks
emu.addMemoryCallback(func, type, start, end): void
emu.removeMemoryCallback(func, type, start, end): void
emu.addEventCallback(func, eventtype): void
emu.removeEventCallback(func, eventtype): void

emu.drawString(x, y, string, color, frameCount): void
emu.drawLine(x, y, x2, y2, color, frameCount): void
emu.drawRectangle(x, y, width, height, color, fill, frameCount): void
emu.getPixel(x, y): int
emu.setPixel(x, y, color): void
emu.getMousePosition(): table [x, y]

emu.log(message: string): void  //Log to LUA-specific console
emu.displayMessage(message: string): void  //Print via OSD

emu.addCheatCode(cheat: string, type: enum): void
emu.clearCheatCodes(): void

emu.getInput(port: int): table [a,b,start,select,up,down,left,right]
emu.setInput(port: int, state: table, durationInFrames: int): void

//Controlling the emulation
emu.reset(hardReset: bool): void
emu.break(): void
emu.resume(): void
emu.execute(cycleCount: int, type: int): void
emu.rewind(seconds: int): void

//Screenshots, savestates
emu.takeScreenshot(): string (png)
emu.saveSavestate(): string
emu.loadSavestate(state: string): void

Some stuff is definitely missing, but I think it's a decent start. If you see anything critical missing, or something terribly bad, let me know.

Author:  dullahan [ Fri Aug 25, 2017 7:11 pm ]
Post subject:  Re: Lua Scripting (Mesen)

I am excited for this, thx for your work Sour!

Author:  Sour [ Sun Aug 27, 2017 3:32 pm ]
Post subject:  Re: Lua Scripting (Mesen)

Almost finished implementing the API - pretty much everything except setState/setInput is done.
As a test, I decided to try porting feos' SoundDisplay2.lua script from FCEUX and it pretty much works:
sounddisplay2.png [ 81.8 KiB | Viewed 717 times ]
I used the exact same font/draw logic as FCEUX for the drawString function, so it makes it easier to port something like this over.

Author:  tepples [ Sun Aug 27, 2017 6:00 pm ]
Post subject:  Re: Lua Scripting (Mesen)

Sour wrote:
I used the exact same font/draw logic as FCEUX for the drawString function, so it makes it easier to port something like this over.

In "Triangle", the raised "g" looks distracting. Would it be worth sacrificing one pixel of the capital letters' height to make the descenders look less off-kilter?

BaseSeven_vx8.png [ 1.12 KiB | Viewed 694 times ]

Author:  Sour [ Sun Aug 27, 2017 6:59 pm ]
Post subject:  Re: Lua Scripting (Mesen)

Good point - since I'm not actually restricted by a grid, I cheated and added some code to manually offset some of the characters.
Moved down g/y/p/q & increased the height of the p/q characters (the bottom part seemed too short otherwise)
The background color isn't shifted since that would look odd in most scenarios.
font.png [ 3.09 KiB | Viewed 685 times ]

Page 2 of 2 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group