debugging equivalent of printf

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
User avatar
battagline
Posts: 152
Joined: Wed Sep 05, 2018 11:13 am
Location: Colorado
Contact:

debugging equivalent of printf

Post 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
A few of my web games
https://www.embed.com
Or if you're bored at work
https://www.classicsolitaire.com
User avatar
dougeff
Posts: 3078
Joined: Fri May 08, 2015 7:17 pm

Re: debugging equivalent of printf

Post 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.
nesdoug.com -- blog/tutorial on programming for the NES
User avatar
battagline
Posts: 152
Joined: Wed Sep 05, 2018 11:13 am
Location: Colorado
Contact:

Re: debugging equivalent of printf

Post 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?
A few of my web games
https://www.embed.com
Or if you're bored at work
https://www.classicsolitaire.com
User avatar
dougeff
Posts: 3078
Joined: Fri May 08, 2015 7:17 pm

Re: debugging equivalent of printf

Post 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
nesdoug.com -- blog/tutorial on programming for the NES
User avatar
battagline
Posts: 152
Joined: Wed Sep 05, 2018 11:13 am
Location: Colorado
Contact:

Re: debugging equivalent of printf

Post by battagline »

It looks like Mesen also has a LUA api.

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

Will investigate.

Thanks again Doug
A few of my web games
https://www.embed.com
Or if you're bored at work
https://www.classicsolitaire.com
Sour
Posts: 890
Joined: Sun Feb 07, 2016 6:16 pm

Re: debugging equivalent of printf

Post 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!)
User avatar
battagline
Posts: 152
Joined: Wed Sep 05, 2018 11:13 am
Location: Colorado
Contact:

Re: debugging equivalent of printf

Post 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.
A few of my web games
https://www.embed.com
Or if you're bored at work
https://www.classicsolitaire.com
unregistered
Posts: 1318
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

Re: debugging equivalent of printf

Post 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.
Sour
Posts: 890
Joined: Sun Feb 07, 2016 6:16 pm

Re: debugging equivalent of printf

Post 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.
Post Reply