Page 1 of 1

debugging equivalent of printf

Posted: Sat Oct 13, 2018 9:22 am
by battagline
I have a certain animation that is sometimes running when it should, and sometimes not.

I'm currently using Mesen as my emulator, and I can't help but missing the printf or similar console output from higher level programming languages.

I'm not sure if anything like this exists, but I keep thinking it would be nice if there were some way an emulator could watch a chunk of memory for changes, and then write that out to a file whenever it is written to. Mesen already highlights in red parts of memory when it is written to. In the 6502 assembly a little macro called "Log" could be written that would just write a short message to the part of memory being monitored. The emulator would then detect the change and dump the contents to a file.

Does something like this exist?

If not, does it make sense that it should exist?

If so what would I need to go about doing to tweak Mesen or some other emulator to add this feature?

Thanks,
Rick

Re: debugging equivalent of printf

Posted: Sat Oct 13, 2018 9:41 am
by dougeff
Someone wrote a LUA script, I think it was for FCEUX, that put something on the screen depending on a RAM address value.

You could also put a sprite in the top left corner if something happens, or flip the grayscale bit on 2001, or set a sound channel on or off depending if a thing happens.

I don't do any of that. I use breakpoints and step through the code.

Re: debugging equivalent of printf

Posted: Sat Oct 13, 2018 9:44 am
by battagline
dougeff wrote:Someone wrote a LUA script, I think it was for FCEUX, that put something on the screen depending on a RAM address value.
Thanks doug

So do either FCEUX or Mesen support scripting?

Re: debugging equivalent of printf

Posted: Sat Oct 13, 2018 9:53 am
by dougeff
I actually don't know much about the LUA stuff. I talked with a guy who does TAS speed runs, who told me that he wrote a LUA script that printed tha value of a RAM address in the screen, or something like that.

I don't have any example code though.

Here's an example of someone using LUA scripts...

https://youtu.be/L9xZ8b3wg-I

Edit,
This looks like a LUA for FCEUX which displays stats on screen...(river city ransom)

https://github.com/BillyWM/FCEUX-Lua-Sc ... isplay.lua

Re: debugging equivalent of printf

Posted: Sat Oct 13, 2018 12:18 pm
by battagline
It looks like Mesen also has a LUA api.

https://www.mesen.ca/docs/apireference.html

Will investigate.

Thanks again Doug

Re: debugging equivalent of printf

Posted: Sat Oct 13, 2018 3:45 pm
by Sour
For Mesen, you should be able to use the endFrame callback to read the value from ram and print its value on the screen with drawString. There are a number of Lua script examples built into the script editor window, you can take a look at those if you want to get a better understanding of how to write your script. (Don't have access to a computer at the moment, so can't write a proper example, sorry!)

Re: debugging equivalent of printf

Posted: Sun Oct 14, 2018 7:58 am
by battagline
Sour wrote:For Mesen, you should be able to use the endFrame callback to read the value from ram and print its value on the screen with drawString. There are a number of Lua script examples built into the script editor window, you can take a look at those if you want to get a better understanding of how to write your script. (Don't have access to a computer at the moment, so can't write a proper example, sorry!)

I came up with this:

Code: Select all

prev_string = "unknown"
address_start = 0x0300

function logIt() 
  --emu.memType.cpu
  address = address_start
  log_string = ""
  repeat
  byte_number = emu.read( address, emu.memType.cpu)
  log_string = log_string .. string.char(byte_number)
  address = address + 1
  until byte_number == 0 

--  if log_string ~= prev_string then
    emu.log("log=" .. log_string)
--  end
  prev_string = log_string
end

emu.log("begin")
emu.addEventCallback(logIt, emu.eventType.endFrame);
Seems to work(ish). I'd like to have it called when the memory is updated instead of every frame, but I don't see that as a part of the lua api.

Re: debugging equivalent of printf

Posted: Mon Oct 15, 2018 8:47 am
by unregistered
battagline, look at page 97 of this thread where I asked a similar question. Make sure to read the two posts following my question.

Hope this helps. :)


edit: What really helped me to figure out why animations weren't running at the proper time was after I unchecked the View>"Memory Access Highlighting">Writes box in the Memory Tools part of Mesen. That allowed me to only view the Reads (blue) and that was so helpful to me because my reading logic needed much work. :) The reds used to overwrite the blues, for me, at least.

Re: debugging equivalent of printf

Posted: Mon Oct 15, 2018 4:46 pm
by Sour
You can use the addMemoryCallback function to log read/write operation for a given memory range, rather than checking/logging the value a single time per frame.