Menu

#796 Add `ppu.readoam` to Lua

General
open
nobody
None
5
2018-04-07
2017-05-27
cjoudrey
No

Hello,

As far as I can tell it is currently not possible to read the PPU's OAM via Lua.

Here's a small diff to add ppu.readoam to the Lua engine. While this diff works, I'm not a C++ developer, so the code might be terrible. Feel free to change it if that's the case. :)

--- src/lua-engine.cpp  (revision 3365)
+++ src/lua-engine.cpp  (working copy)
@@ -1439,6 +1439,13 @@
    return 1;
 }

+static int ppu_readoam(lua_State *L) {

+   const char *p = reinterpret_cast<const char*>(SPRAM);
+   lua_pushlstring(L, p, 0x100);
+
+   return 1;
+}
+
 static inline bool isalphaorunderscore(char c)
 {
    return isalpha(c) || c == '_';
@@ -5626,6 +5633,7 @@
 static const struct luaL_reg ppulib [] = {
    {"readbyte", ppu_readbyte},
    {"readbyterange", ppu_readbyterange},
+   {"readoam", ppu_readoam},

    {NULL,NULL}
 };

It might be useful to expose the values of the other PPU registers in Lua, i.e. PPUSCROLL, PPUCTRL, PPUMASK, etc..

  • Christian

Discussion

  • feos

    feos - 2017-05-27

    You're probably able to read those register values from ram by address.

     
  • cjoudrey

    cjoudrey - 2017-05-28

    Thanks for the reply @feos!

    From what I can tell, we currently only expose ppu.readbyte in Lua.

    ppu.readbyte ends up calling FFCEUX_PPURead_Default (depending on mapper) which only exposes the pattern tables (< 0x2000), nametables (< 0x3F00), and palettes (< 0x3FFF).

    The OAM (sprite data) is stored in a separate array SPRAM which is not accessible via ppu.readbyte. This is why I'm proposing we add ppu.readoam as per the diff above.

    For the other registers, it appears like they are stored in an instance of PPUREGS called ppur. As far as I can tell these are not currently accessible in Lua.

    Edit: Hmm, I just noticed the registers might be readable via memory.readbyte as this ends up calling GetMem which does handle 2000-2007. I'll have to try that out.

    • Christian
     

    Last edit: cjoudrey 2017-05-28
  • Alex W. Jackson

    Alex W. Jackson - 2017-05-29

    PPU registers are (depending on the register) either write-only or reading them has some side effect which will probably mess up a game if triggered unexpectedly. If you want to read the PPU's state from Lua, reading 2000-2007 is not the way to do it.

     
  • zeromus

    zeromus - 2017-05-29

    yeah, add new functions if you want to do that and build the values to return with whatever safe logic is required

     
  • spiiin

    spiiin - 2018-04-07

    and please, don't forget readXXXrange functions for all versions or memory reading. Now it's look very strange, that some functions has this variant, and some has not.

     

Log in to post a comment.

MongoDB Logo MongoDB