The first one is synchronous and uses the <ahref=#savesavestate>saveSavestate</a> and <ahref=#loadsavestate>loadSavestate</a> functions. Its main restriction is that it can only be used inside “startFrame” or “cpuExec” callback functions.<br>
The second API is asynchronous and uses an internally-managed “save slot” system to hold states in memory. It uses the following functions: <ahref=#savesavestateasync>saveSavestateAsync</a>, <ahref=#loadsavestateasync>loadSavestateAsync</a>, <ahref=#getsavestatedata>getSavestateData</a> and <ahref=#clearsavestatedata>clearSavestateData</a>.</p>
<h3id=savesavestate>saveSavestate</h3>
<p><strong>Syntax</strong></p>
<pre><code>emu.saveSavestate()
</code></pre>
<p><strong>Return value</strong><br>
<em>String</em> A string containing a binary blob representing the emulation’s current state.</p>
<p><strong>Description</strong><br>
Creates a savestate and returns it as a binary string. (The savestate is not saved on disk)<br>
<strong>Note:</strong> this can only be called from within a “startFrame” event callback or “cpuExec” memory callback.</p>
<h3id=savesavestateasync>saveSavestateAsync</h3>
<p><strong>Syntax</strong></p>
<pre><code>emu.saveSavestateAsync(slotNumber)
</code></pre>
<p><strong>Parameters</strong><br>
slotNumber - <em>Integer</em> A slot number to which the savestate data will be stored (slotNumber must be >= 0)</p>
<p><strong>Return value</strong><br>
<em>None</em></p>
<p><strong>Description</strong><br>
Queues a save savestate request. As soon at the emulator is able to process the request, it will be saved into the specified slot.<br>
This API is asynchronous because save states can only be taken in-between 2 CPU instructions, not in the middle of an instruction.
When called while the CPU is in-between 2 instructions (e.g: inside the callback of an cpuExec or startFrame event), the save state will be taken immediately and its data will be available via <ahref=#getsavestatedata>getSavestateData</a> right after the call to saveSavestateAsync.<br>
The savestate can be loaded by calling the <ahref=#loadsavestateasync>loadSavestateAsync</a> function.</p>
<h3id=loadsavestate>loadSavestate</h3>
<p><strong>Syntax</strong></p>
<pre><code>emu.loadSavestate(savestate)
</code></pre>
<p><strong>Parameters</strong><br>
savestate - <em>String</em> A binary blob representing a savestate, as returned by <ahref=#savesavestate>saveSavestate()</a></p>
<p><strong>Return value</strong><br>
<em>None</em></p>
<p><strong>Description</strong><br>
Loads the specified savestate.<br>
<strong>Note:</strong> this can only be called from within a “startFrame” event callback or “cpuExec” memory callback.</p>
<h3id=loadsavestateasync>loadSavestateAsync</h3>
<p><strong>Syntax</strong></p>
<pre><code>emu.loadSavestateAsync(slotNumber)
</code></pre>
<p><strong>Parameters</strong><br>
slotNumber - <em>Integer</em> The slot number to load the savestate data from (must be a slot number that was used in a preceding <ahref=#savesavestateasync>saveSavestateAsync</a> call)</p>
<p><strong>Return value</strong><br>
<em>Boolean</em> Returns true if the slot number was valid.</p>
<p><strong>Description</strong><br>
Queues a load savestate request. As soon at the emulator is able to process the request, the savestate will be loaded from the specified slot.<br>
This API is asynchronous because save states can only be loaded in-between 2 CPU instructions, not in the middle of an instruction.
When called while the CPU is in-between 2 instructions (e.g: inside the callback of an cpuExec or startFrame event), the savestate will be loaded immediately.</p>
<h3id=getsavestatedata>getSavestateData</h3>
<p><strong>Syntax</strong></p>
<pre><code>emu.getSavestateData(slotNumber)
</code></pre>
<p><strong>Parameters</strong><br>
slotNumber - <em>Integer</em> The slot number to get the savestate data from (must be a slot number that was used in a preceding <ahref=#savesavestateasync>saveSavestateAsync</a> call)</p>
<p><strong>Return value</strong><br>
<em>String</em> A binary string containing the savestate</p>
<p><strong>Description</strong><br>
Returns the savestate stored in the specified savestate slot.</p>
<h3id=clearsavestatedata>clearSavestateData</h3>
<p><strong>Syntax</strong></p>
<pre><code>emu.clearSavestateData(slotNumber)
</code></pre>
<p><strong>Parameters</strong><br>
slotNumber - <em>Integer</em> The slot number to get the savestate data from (must be a slot number that was used in a preceding <ahref=#savesavestateasync>saveSavestateAsync</a> call)</p>
<p><strong>Return value</strong><br>
<em>None</em></p>
<p><strong>Description</strong><br>
Clears the specified savestate slot (any savestate data in that slot will be removed from memory).</p>
<h2id=cheats>Cheats</h2>
<h3id=addcheat>addCheat</h3>
<p><strong>Syntax</strong></p>
<pre><code>emu.addCheat(cheatCode)
</code></pre>
<p><strong>Parameters</strong><br>
cheatCode - <em>String</em> A game genie format cheat code.</p>
<p><strong>Return value</strong><br>
<em>None</em></p>
<p><strong>Description</strong><br>
Activates a game genie cheat code (6 or 8 characters).<br>
<strong>Note:</strong> cheat codes added via this function are not permanent and not visible in the UI.</p>
<h3id=clearcheats>clearCheats</h3>
<p><strong>Syntax</strong></p>
<pre><code>emu.clearCheats()
</code></pre>
<p><strong>Return value</strong><br>
<em>None</em></p>
<p><strong>Description</strong><br>
Removes all active cheat codes (has no impact on cheat codes saved within the UI)</p>
<em>String</em> A string containing the log shown in the log window</p>
<p><strong>Description</strong><br>
Returns the same text as what is shown in the emulator’s Log Window.</p>
<h3id=getrominfo>getRomInfo</h3>
<p><strong>Syntax</strong></p>
<pre><code>emu.getRomInfo()
</code></pre>
<p><strong>Return value</strong><br>
<em>Table</em> Information about the current ROM with the following structure:</p>
<divclass=highlight><pretabindex=0style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><codeclass=language-textdata-lang=text>name: string, Filename of the current ROM
path: string, Full path to the current ROM (including parent compressed archive when needed)
fileCrc32Hash: int, The CRC32 value for the whole ROM file
fileSha1Hash: string, The SHA1 hash for the whole ROM file
prgChrCrc32Hash: int, The CRC32 value for the file, excluding its 16-byte header
prgChrMd5Hash: string, The MD5 hash for the file, excluding its 16-byte header
format: int, Value that represents the ROM format: 1 = iNES, 2 = UNIF, 3 = FDS, 4 = NSF
isChrRam: bool true when the game uses CHR RAM, false otherwise
<em>String</em> The script’s data folder</p>
<p><strong>Description</strong><br>
This function returns the path to a unique folder (based on the script’s filename) where the script should store its data (if any data needs to be saved).<br>
The data will be saved in subfolders inside the LuaScriptData folder in Mesen’s home folder.</p>
<h3id=takescreenshot>takeScreenshot</h3>
<p><strong>Syntax</strong></p>
<pre><code>emu.takeScreenshot()
</code></pre>
<p><strong>Return value</strong><br>
<em>String</em> A binary string containing a PNG image.</p>
<p><strong>Description</strong><br>
Takes a screenshot and returns a PNG file as a string.<br>