jtothebell / fake-08

A Pico-8 player/emulator for console homebrew
Other
563 stars 49 forks source link

Fixes for memory functions (peek/poke/etc) not working above 0x8000 #144

Closed nckstwrt closed 2 years ago

nckstwrt commented 2 years ago

The peek/poke functions in picoluaapi.cpp are getting the destination address like this: int dest = lua_tonumber(L,1);

However, for values over 0x8000 (32768) dest will be a negative number. So when vm_poke is called it will see it just as a negative number and ignore it. As per https://pico-8.fandom.com/wiki/Memory - over 0x8000 is for general use and therefore "pokeable"

This is why games like Mot's Grand Prix (https://www.lexaloffle.com/bbs/?tid=44700) do not work in fake-08. A crude fix is to change all memory functions (all peeks, pokes, memset/cpy, etc) to read the destination as: unsigned short dest = (unsigned short)lua_tonumber(L,1);

Also, currently PicoRam's Reset function is resetting areas of the memory listed at https://pico-8.fandom.com/wiki/Memory as not being reset when a new cart is loaded. Instead of: memset(data + 0x5600, 0, 0x10000 - 0x5600); It should possibly be doing: memset(data + 0x5f00, 0, (0x8000 - 0x5f00));

With the above fixes Mot's Grand Prix (and I imagine others that use the memory area above 0x8000) work very well.

On a completely separate note, as I am testing this in Windows, the current HexToBytes function in vm.cpp will misread the serialized data. This is because Windows, very helpfully, adds a \r byte along with the \n byte when writing out the file in default text mode (as is done in Host::saveCartData). One, again crude, solution is to add an additional line of hex.erase(std::remove(hex.begin(), hex.end(), '\r'), hex.end()); to HexToBytes to ensure both \r and \n are removed. I doubt it matters as Windows is not a supported target for you, but I leave it here as it perplexed me in my debugging and so might be useful for others :)

jtothebell commented 2 years ago

Thank you for this report! I verified that all the behavior you described except the carriage returns on windows (I don't have a windows build setup) but your crude fix seems reasonable, so I added that in with the fixes as well: https://github.com/jtothebell/fake-08/pull/145