Lua Scripting (Mesen)

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Sour
Posts: 796
Joined: Sun Feb 07, 2016 6:16 pm

Re: Lua Scripting (Mesen)

Post by Sour » Fri Aug 25, 2017 5:17 pm

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
For the API, so far I've got this roughly planned:

Code: Select all

//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.)
emu.read(address, 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

//Drawing-related
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]

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

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

//Input
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.

dullahan
Posts: 96
Joined: Mon Dec 07, 2009 11:08 am
Location: USA

Re: Lua Scripting (Mesen)

Post by dullahan » Fri Aug 25, 2017 7:11 pm

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

Sour
Posts: 796
Joined: Sun Feb 07, 2016 6:16 pm

Re: Lua Scripting (Mesen)

Post by Sour » Sun Aug 27, 2017 3:32 pm

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
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.

tepples
Posts: 21946
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Lua Scripting (Mesen)

Post by tepples » Sun Aug 27, 2017 6:00 pm

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?
Attachments
BaseSeven_vx8.png
BaseSeven_vx8.png (1.12 KiB) Viewed 5625 times

Sour
Posts: 796
Joined: Sun Feb 07, 2016 6:16 pm

Re: Lua Scripting (Mesen)

Post by Sour » Sun Aug 27, 2017 6:59 pm

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
font.png (3.09 KiB) Viewed 5616 times

User avatar
metalmight
Posts: 2
Joined: Tue Oct 09, 2018 1:09 am

Re: Lua Scripting (Mesen)

Post by metalmight » Tue Aug 13, 2019 2:25 pm

I am posting here because most message boards/forums frown on posting a new topic if it is already being discussed in an existing thread. I am wondering if Mesen could be modified to be compatible with LUA scripts made with FCEUX for NES games specifically the Neill Corlett LUA Script for Metroid. If so it would allow playing a version of Metroid with the HD pack and the LUA script which would be the ultimate NES Metroid experience. I also asked the FCEUX maintainers if a version of FCEUX could be made that would be compatible with HD packs. Thank you.

Post Reply