Add memory.registerwrite to the Lua implementation. Presumably would be the
same concept as it is in FCEUX:
memory.registerwrite(int address, [int size,] function func)
Registers a function to be called immediately whenever the given memory address
range is written to.
size is the number of bytes to "watch". For example, if size is 100 and address
is 0x0200, then you will register the function across all 100 bytes from 0x0200
to 0x0263. A write to any of those bytes will trigger the function. Having
callbacks on a large range of memory addresses can be expensive, so try to use
the smallest range that's necessary for whatever it is you're trying to do. If
you don't specify any size then it defaults to 1.
The callback function will receive two arguments, (address, size) indicating
what write operation triggered the callback. If you don't care about that extra
information then you can ignore it and define your callback function to not
take any arguments. The value that was written is NOT passed into the callback
function, but you can easily use any of the memory.read functions to retrieve
it.
You may use a memory.write function from inside the callback to change the
value that just got written. However, keep in mind that doing so will trigger
your callback again, so you must have a "base case" such as checking to make
sure that the value is not already what you want it to be before writing it.
Another, more drastic option is to de-register the current callback before
performing the write.
If func is nil that means to de-register any memory write callbacks that the
current script has already registered on the given range of bytes.