Mesen - NES Emulator

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

tomaitheous
Posts: 592
Joined: Thu Aug 28, 2008 1:17 am
Contact:

Re: Mesen - NES Emulator

Post by tomaitheous »

Sour wrote:
tomaitheous wrote:...
Isn't most/all of this achievable by parsing a trace log file? It contains all the information you would need to get those statistics. I'd imagine writing a script in python or anything else to parse the output would be fairly easy. Of course, if you wanted to analyze the execution over a long period of time, the trace log could become large relatively quickly, though.
If the trace log contains time stamp entries, then this would work. Though less efficient because of the gigantic bloat. Similar to a CDL, it would be less space to have a binary output of each 'frame' - 256 entries with a occurrence precision rate of 32bits -> 1024bytes per frame. Clear the results on the next frame, recount, save frame of data to binary file. For pairs and triplets, it'd be double or triple the size, which is still tiny in comparison to a trace log.

Anyway, the problem I'm having is that even for just a second of trace log, Mesen's "open" function instead of "saving" or automatically saving, appears to be leaving "notepad" completely unresponsive when it tries to open it (I have other utilities for better handing huge text files in the gigabyte range, associated with txt files in windows, but Mesen doesn't see to use the windows associated app). Unless it's not crashing, but just stalled for like 30 minutes.

I dunno. Maybe I can try messing with mednafen's source code.
__________________________
http://pcedev.wordpress.com
lidnariq
Posts: 11429
Joined: Sun Apr 13, 2008 11:12 am

Re: Mesen - NES Emulator

Post by lidnariq »

This is a tangent, but let's see if I can wrangle a simple example for the static analysis:

Say I had a certain "game" that was released as an Atari 2600 ROM embedded in an emulator that's shipped as a Windows binary. It hasn't been encrypted at all, I just don't know where it is. I also don't know the right questions to ask to find tools to take PE binaries apart.

I happen to have already written a program that will let me look at any given file on disk as a series of histograms: X axis is byte #, Y axis is Nth slice.

Because I know what x86 and 6502 code "looks" like in a histogram:
histograms-of-bytes-of-different-ISAs.png
histograms-of-bytes-of-different-ISAs.png (4.54 KiB) Viewed 5981 times
I can use my brain as a crude (but fast) means of pattern recognition.

Using that, I can find the embedded 6502 code inside the x86 code.

And all of this is a tangent. You won't find x86 code in an NES game (unless it's there by accident because the assembler didn't clear its memory). That's not my point. My point is that these histograms let you find oblique things you didn't know. And they let you find things you didn't know you didn't know. (q.v. #1, q.v. #2)
rainwarrior wrote:Could you [really identify whether a binary is 6502 or x86]? Wouldn't the noise of data and operand-data completely drown the opcode signal?
Well, I can tell the difference between the two in the above image.
User avatar
rainwarrior
Posts: 8731
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Mesen - NES Emulator

Post by rainwarrior »

lidnariq wrote:
rainwarrior wrote:Could you [really identify whether a binary is 6502 or x86]? Wouldn't the noise of data and operand-data completely drown the opcode signal?
Well, I can tell the difference between the two in the above image.
Okay, I can buy that.
Sour
Posts: 890
Joined: Sun Feb 07, 2016 6:16 pm

Re: Mesen - NES Emulator

Post by Sour »

tomaitheous wrote:Similar to a CDL, it would be less space to have a binary output of each 'frame' - 256 entries with a occurrence precision rate of 32bits -> 1024bytes per frame. Clear the results on the next frame, recount, save frame of data to binary file. For pairs and triplets, it'd be double or triple the size, which is still tiny in comparison to a trace log.
This should be relatively easy to implement and would require little to no UI, so I'll see what I can do.
tomaitheous wrote: but Mesen doesn't see to use the windows associated app
The button correctly uses Notepad++ for me. The file's extension is "log" though, so you may have log files associated to notepad. Maybe I should rename the files to be .txt instead.
You can open the file manually too, it'll be in Documents\Mesen\Debugger. The open button is just a shortcut to get the file opened.
tomaitheous
Posts: 592
Joined: Thu Aug 28, 2008 1:17 am
Contact:

Re: Mesen - NES Emulator

Post by tomaitheous »

lidnariq wrote: My point is that these histograms let you find oblique things you didn't know. And they let you find things you didn't know you didn't know.
^ This. I mean, I already have an idea of what I'm looking for (or suspect), but it's also about didn't know you didn't know.

koitsu: What I'm looking for, goes way beyond the NES. The NES just happens to be something I'm looking at in relation to other system, setups, environments, and processors. *If* I find something interesting, I want to be able to turn this into an independent research project for my undergrad thesis (yeah.. undergrad thesis. My uni offers it for senior years for honors program. My plans are grad school, so early research opportunities with faculty mentors looks good on resume. Plus, it might make for an interesting read - just the nes and certain related facts/patterns/etc).
__________________________
http://pcedev.wordpress.com
User avatar
koitsu
Posts: 4201
Joined: Sun Sep 19, 2004 9:28 pm
Location: A world gone mad

Re: Mesen - NES Emulator

Post by koitsu »

Foremost: okay, a colleague has explained to me what the actual usability of (a kind of) a spectrogram for code/data would be. I think for me, no, I wouldn't find this helpful, but I can see (distantly -- because again, outside my pay grade) how it'd be useful. However, I would imagine something like this could be incredibly useful for people who are doing something like, say, ripping NSFs. It's a bit of a stretch, but that's what I got out of it anyway.

As for this:
rainwarrior wrote:
koitsu wrote:How this is done now is a bit painstaking; rainwarrior mentions it this Lizard Kickstarter update/blog (tweaking the R/G/B intensity bits based on start/end of routines, but there's also Lua if you're into that). If you ask me, I'd rather this be done through actual opcodes in a program (i.e. BRK $40 might start the tracking of cycles for performance counter #0 (say, support up to 16 of them), BRK $80 might turn it off, and then correlate that visually on-screen in some way -- allowing the programmer to move the BRK statements around to "narrow down" what takes up the most time in their routine). Again, this would be super useful for VBlank.
"STA $2001" is using an "actual opcode". ;P I don't really understand the advantage of BRK here (it saves 3 bytes but requires an IRQ handler?).
I'll start a separate thread, because the explanation I'll give is too long-winded and I'd feel like was hijacking existing subject.

Edit: said thread: viewtopic.php?f=3&t=15254
User avatar
rainwarrior
Posts: 8731
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Mesen - NES Emulator

Post by rainwarrior »

Trying to identify code by binary visualization isn't the request being made, though, just a digression on something that is somewhat similar and useful.

I don't think you could use an opcode execution profile (at least not what seems to be requested) to identify 6502 code from other types of code, really. Like, running x86 code on a 6502 will immediately engage in degenerate behaviour anyway; even 6502 code will probably "jump to junk" pretty quick unless you know where it's supposed to be run from too. There's probably a lot of better ways to identify it (lidnariq's binary histogram example seems like one way already).

I'm reminded of the computer studies of Shakespeare and contemporary authors where they used word frequencies and markov chains to identify authorial "signatures" to attribute works to their authors (and also resolve questions about "disputed" works of Shakespeare). I'm sure there's similar applications for code analysis and identification of coding styles, etc. (especially on work from the hand-coded assembly era), though you probably want a lot better information than just opcode frequency here.

I don't think "we don't know what we don't know" is justification to add a feature to a debugger, though. (e.g. every single feature of MSVC's debugger has a pretty deliberate purpose.) I think gathering information about opcodes is an interesting research question-- but it would be better served by a tool that can be modified by the researcher directly for their own purposes, i.e. a one off build, or something they modified themselves, or something scriptable like FCEUX's lua scripts (which can probably do this task, actually).

I guarantee once you've collected some data you're going to have a hundred "what if" extensions for that feature (what if I tracked pairs of consecutive opcodes, what about detecting hardware wait loops, etc.). You'd get the best results if you were prepared to modify it yourself for a personal build.
Sour
Posts: 890
Joined: Sun Feb 07, 2016 6:16 pm

Re: Mesen - NES Emulator

Post by Sour »

rainwarrior wrote:I guarantee once you've collected some data you're going to have a hundred "what if" extensions for that feature (what if I tracked pairs of consecutive opcodes, what about detecting hardware wait loops, etc.). You'd get the best results if you were prepared to modify it yourself for a personal build.
I agree on this - this sort of very specific feature would be better off as a custom build or a script.
I do plan on adding C# scripting in the future - I should probably take a look at that soon, it should be relatively simple to implement.
User avatar
rainwarrior
Posts: 8731
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Mesen - NES Emulator

Post by rainwarrior »

tomaitheous wrote:Would it be possible to add a feature to the debugger where you could get the rate of occurence of each opcode? Similar to the CDL, but record the number of times all instructions are executed.
rainwarrior wrote:FCEUX's lua scripts ... can probably do this task, actually
Just to follow up, as a demonstration of how useful scripting languages can be for stuff like this, here's a very simple FCEUX LUA script that does that.

(Just the very basic, logs opcodes, spits out the data to the LUA console when stopped. You could easily modify it to respond to input, log other information, output to file, etc.)


Edit: Lua scripts were later disallowed on this forum. Uploading a ZIP containing what I think was the original script.
Attachments
opcodecounter.zip
(417 Bytes) Downloaded 117 times
opcodecounter.lua
(464 Bytes) Downloaded 63 times
Last edited by rainwarrior on Tue Jun 12, 2018 12:32 pm, edited 1 time in total.
tomaitheous
Posts: 592
Joined: Thu Aug 28, 2008 1:17 am
Contact:

Re: Mesen - NES Emulator

Post by tomaitheous »

Thanks rainwarrior! I had no idea you could do that low level of stuff with lua script. I always though lua was integrated as mostly a just a generic memory interface (and on a frame basis). Guess I've been missing out.
__________________________
http://pcedev.wordpress.com
User avatar
nothingtosay
Posts: 39
Joined: Mon May 19, 2014 11:46 pm

Re: Mesen - NES Emulator

Post by nothingtosay »

Stereo panning is a great feature and I'm happy someone finally implemented it in an emulator! I hope you'll add per-channel panning options for the expansion audio chips. I also hope you'll add shader support in the future.

I have a sound emulation bug to report that I hope you can get to the bottom of. I don't know how widely known this is, but in Super Mario Bros. 3, the underground music is supposed to have a randomized phase cancellation effect. Mesen doesn't reproduce it. I made a recording from my NES to show this does happen on hardware, but the forum won't let me attach a WAV or FLAC file, so I uploaded it to MediaFire. I haven't tested a huge array of emulators, but FCEUX does it pretty well (Nestopia and Nintendulator do it too but seems like it's to a lesser extent). Conveniently, I hear FCEUX has a great debugger that presumably would be able to show what's going on, but I'm not a programmer in any way. To try to figure it out, I paused FCEUX (not the game), saved state, and recorded the two square waves and the triangle wave individually, pausing and reloading the state each time so the recordings start at the exact same time and are perfectly synchronized. Then I compared the waves of two different-sounding passages of the same notes side-by-side. The timing of the waves in relation to each other gets delayed slightly, producing phase cancellations. I haven't done the same kind of recording test with any other emulators, but my guess based on the sound of it is that other emulators don't randomize it and just produce one of the many possible variations. Hopefully you can get it figured out!
User avatar
rainwarrior
Posts: 8731
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Mesen - NES Emulator

Post by rainwarrior »

nothingtosay wrote:The timing of the waves in relation to each other gets delayed slightly, producing phase cancellations. I haven't done the same kind of recording test with any other emulators, but my guess based on the sound of it is that other emulators don't randomize it and just produce one of the many possible variations. Hopefully you can get it figured out!
Phases aren't really random. The squares explicitly have their phase reset every time $4003/$4007 is written, which will happen at specific times during a piece of music (usually whenever a particular octave threshold is crossed, but some music engines reset phase on every note). Sound effects that interrupt the channels will disrupt it though (until the next phase reset). Emulating the phase correctly requires resetting the phase on $4003/4007 but also making sure that the "silent" behaviour also matches what the hardware does.

The triangle's phase is more effectively random; I think it's consistent at power on, but that's about it. Same deal with noise. However, this isn't randomization done by the emulator, this is randomization as a consequence of human input.
User avatar
kode54
Posts: 67
Joined: Mon Jun 06, 2005 12:47 pm
Contact:

Re: Mesen - NES Emulator

Post by kode54 »

I see Game_Music_Emu's Nes_Apu and Nes_Oscs already do this, or close enough. They reset phase on writes to $4003/$4007, and they maintain pitch correct phase during periods of silence or inaudibility. I just don't know what else could be "wrong".
Sour
Posts: 890
Joined: Sun Feb 07, 2016 6:16 pm

Re: Mesen - NES Emulator

Post by Sour »

nothingtosay wrote:Stereo panning is a great feature and I'm happy someone finally implemented it in an emulator! I hope you'll add per-channel panning options for the expansion audio chips. I also hope you'll add shader support in the future.
No problem! It was a feature that was requested by Eugene.S a while ago. I might add per-channel panning for the expansion chips eventually, but that would imply per-channel volume options for them too, and then you'd end up with twice as many sliders as there already are (and there's already too many for comfort!) - so I'm not sure. If I did, I'd have to hide them away in some advanced menu (most users wouldn't ever use this).
Shaders are on my list of things to get done eventually, though.
nothingtosay wrote:[SMB3 BGM explanation]
To be perfectly honest, I can't really tell from the recording (I'm pretty terrible at these sort of things). Also, when reloading a save state, I would expect an emulator to play the exact same sound sequence every single time, so long as no user input is done (with a controller) - if some emulators don't, then that would imply their save states aren't perfect. (But I may be misunderstanding what you meant about reloading from a save state to record & compare)

Mesen passes all of blargg's audio tests, including the apu_mixer ones that mute (or nearly mute) a channel by using another channel's output (which requires pretty high accuracy). FCEUX, Nintendulator and Nestopia (apparently) all fail these tests according to TASVideos.org's list.

... Or so I thought :) It looks like I inadvertently broke the apu_mixer tests with the panning options in 0.6.0. 0.5.3 works properly, and I just committed a fix for this. (Really need to add sound checking capabilities to my automated tests).

Also, if you use the volume/panning options, the sound emulation is automatically less accurate - you need to keep the volume of individual sound channels at 100%, and no panning (all at 0) for the apu_mixer tests to pass as expected. (Master volume has no impact on the accuracy, though)

If you want, you can give SMB3 a shot with 0.5.3 (you can download it here) and see if it fares any better.
User avatar
nothingtosay
Posts: 39
Joined: Mon May 19, 2014 11:46 pm

Re: Mesen - NES Emulator

Post by nothingtosay »

Sour wrote:I might add per-channel panning for the expansion chips eventually, but that would imply per-channel volume options for them too, and then you'd end up with twice as many sliders as there already are (and there's already too many for comfort!) - so I'm not sure. If I did, I'd have to hide them away in some advanced menu (most users wouldn't ever use this).
You could do like the NSF player NotSoFatso and have tabs for each expansion chip where the sliders are located. It's a little tough to get a good stereo mix on games that just use the regular channels, but expansion chips are where the panning option could really shine so I hope you'll add it. Not meaning to put pressure on you, of course.
Sour wrote:To be perfectly honest, I can't really tell from the recording (I'm pretty terrible at these sort of things).
Notice how the notes stay the same but the tone changes between the first "doo-doo, doo-doo, doo-doo" and the next in my recording? That's due to the waves being similar but out of phase slightly, so some of the frequencies cancel out when the the waves are added together. The game varies the phase of the waves periodically, so different frequencies will cancel out, creating different tones. In Mesen the tone stays the same, so the phase apparently isn't getting changed. In emulators that don't recreate the effect, it sounds to me like they only play one of the possible phase variations but never change to another after that.
Sour wrote:Also, when reloading a save state, I would expect an emulator to play the exact same sound sequence every single time, so long as no user input is done (with a controller) - if some emulators don't, then that would imply their save states aren't perfect. (But I may be misunderstanding what you meant about reloading from a save state to record & compare)
I only used the save states for recording from FCEUX so I could get each channel individually for the exact same play-through of the music, not for testing the sound of any of the emulators. For that, I just went into the pipe in the sky in the first level and listened to see if the tone of the music changed over time.
Sour wrote:Mesen passes all of blargg's audio tests, including the apu_mixer ones that mute (or nearly mute) a channel by using another channel's output (which requires pretty high accuracy). FCEUX, Nintendulator and Nestopia (apparently) all fail these tests according to TASVideos.org's list.
Right. It just must be a function the test ROMs don't cover. Hopefully someone might make a test ROM that tests it and in the process promote awareness of and support for this cool little effect.
Sour wrote:Also, if you use the volume/panning options, the sound emulation is automatically less accurate - you need to keep the volume of individual sound channels at 100%, and no panning (all at 0) for the apu_mixer tests to pass as expected. (Master volume has no impact on the accuracy, though)
In my testing, I kept all the channels at full volume and with no stereo panning. Anything else (besides master volume) would assuredly lessen the effect if it were emulated.
Sour wrote:If you want, you can give SMB3 a shot with 0.5.3 (you can download it here) and see if it fares any better.
I checked and nope, it doesn't work there either, sorry to say.

Thanks for your consideration for all of this, Sour.

Also, to rainwarrior, I don't really have anything I can say in reply to what you wrote, but I appreciate that you did it and want to acknowledge that. Here's hoping this all gets worked out and both Mesen and Game_Music_Emu will get it emulated properly.
Post Reply