Emulation speed vs actual NES speed (solved)

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

wendelscardua
Posts: 7
Joined: Tue Jun 09, 2020 5:55 pm

Emulation speed vs actual NES speed (solved)

Post by wendelscardua » Fri Jun 26, 2020 9:57 am

Update: I'm dumb, my problem was an older, debug version of my ROM. Ignore this post.



Hi, I've began learning NES dev for a few months, and my games were running mostly on emulators like Fceux and NintendulatorDX. But yesterday I finally got an old NES (and a few days before a flashcart for it) and tried running my games on it. Most of them ran fine (including a small rhythm game I made for a gamejam this month), but one of them was really slow. This one was fine on the emulators I tried, and as I understood emulators have to take care of speed / cycles in order to make CPU and PPU match, so I didn't expect that much of a slowdown on the real thing.

My question is: is there an emulator that I can use that matches NES speed more perfectly? Or at least a rule of thumb for predicting when an emulated game will have performance issues running on the NES? I'd like to avoid going back and forth with the flashcart to debug performance issues during development.
Last edited by wendelscardua on Sat Jun 27, 2020 10:37 am, edited 2 times in total.

User avatar
Dwedit
Posts: 4328
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: Emulation speed vs actual NES speed

Post by Dwedit » Fri Jun 26, 2020 10:05 am

Sounds like you may be polling PPUSTATUS to check for the end of a frame? That can cause missed frames, don't do that (outside of the boot code that waits one frame).
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!

lidnariq
Posts: 9495
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: Emulation speed vs actual NES speed

Post by lidnariq » Fri Jun 26, 2020 10:12 am

Emulators are extremely accurate in this regard; if you don't hear the audio drop out (a "garbly" sound), you are probably trying to emulate a game for one region (PAL/NTSC/PAL famiclone) in the wrong region.

wendelscardua
Posts: 7
Joined: Tue Jun 09, 2020 5:55 pm

Re: Emulation speed vs actual NES speed

Post by wendelscardua » Fri Jun 26, 2020 11:52 am

Dwedit wrote:
Fri Jun 26, 2020 10:05 am
Sounds like you may be polling PPUSTATUS to check for the end of a frame? That can cause missed frames, don't do that (outside of the boot code that waits one frame).
Yes, there may be some missed frames. In this game I rarely poll PPUSTATUS (only when loading a new nametable), but my main loop waits until an "nmis" counter changes (my nmi subroutine only refreshes OAM then increments this counter), which I suppose is prone to cause the same problem.

As my code is severely lacking optimization it being slow isn't much of a surprise, but I still don't understand why the same thing doesn't happen on emulators.

User avatar
Dwedit
Posts: 4328
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: Emulation speed vs actual NES speed

Post by Dwedit » Fri Jun 26, 2020 11:57 am

If you think your code might be slow, a cheap way to measure CPU usage is to use PPUMASK. At start of frame, do something that you can see (such as monochrome mode, color emphasis, masking left 8 pixels, etc), then at the end of frame, turn that off. You can count your CPU load in scanlines. But unless you are reaching the bottom, or seeing a frame with full load followed by a frame with low load, it's not CPU usage.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!

User avatar
rainwarrior
Posts: 7822
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Emulation speed vs actual NES speed

Post by rainwarrior » Fri Jun 26, 2020 11:59 am

wendelscardua wrote:
Fri Jun 26, 2020 11:52 am
Yes, there may be some missed frames. In this game I rarely poll PPUSTATUS (only when loading a new nametable), but my main loop waits until an "nmis" counter changes (my nmi subroutine only refreshes OAM then increments this counter), which I suppose is prone to cause the same problem.
No, the NMI is reliable, and that method should not have a problem. It's only spinning on a $2002 read loop that really causes the problem Dwedit was talking about.

Unfortunately, with the vague details given there's not much to speculate on here. There are many possibilities. A ROM would help diagnosis tremendously, if it's something you could share.

wendelscardua
Posts: 7
Joined: Tue Jun 09, 2020 5:55 pm

Re: Emulation speed vs actual NES speed

Post by wendelscardua » Fri Jun 26, 2020 12:10 pm

rainwarrior wrote:
Fri Jun 26, 2020 11:59 am
wendelscardua wrote:
Fri Jun 26, 2020 11:52 am
Yes, there may be some missed frames. In this game I rarely poll PPUSTATUS (only when loading a new nametable), but my main loop waits until an "nmis" counter changes (my nmi subroutine only refreshes OAM then increments this counter), which I suppose is prone to cause the same problem.
No, the NMI is reliable, and that method should not have a problem. It's only spinning on a $2002 read loop that really causes the problem Dwedit was talking about.

Unfortunately, with the vague details given there's not much to speculate on here. There are many possibilities. A ROM would help diagnosis tremendously, if it's something you could share.
I can share both the ROM and the (really ugly, sorry) source code:

Source: https://github.com/wendelscardua/1jam-retro
ROM: https://wendelscardua.itch.io/retropia

User avatar
Controllerhead
Posts: 89
Joined: Tue Nov 13, 2018 4:58 am
Location: $4016
Contact:

Re: Emulation speed vs actual NES speed

Post by Controllerhead » Fri Jun 26, 2020 7:12 pm

It worked fine on my Everdrive and 07 NTSC toaster. Fun game! Id love to see the concept more developed.
Image
...Are you running this on a European PAL NES by chance? I tried 50hz PAL mode in Mesen and it behaved like you described.

wendelscardua
Posts: 7
Joined: Tue Jun 09, 2020 5:55 pm

Re: Emulation speed vs actual NES speed

Post by wendelscardua » Fri Jun 26, 2020 9:03 pm

Controllerhead wrote:
Fri Jun 26, 2020 7:12 pm
It worked fine on my Everdrive and 07 NTSC toaster. Fun game! Id love to see the concept more developed.
Image
...Are you running this on a European PAL NES by chance? I tried 50hz PAL mode in Mesen and it behaved like you described.
I bought it recently, and it was advertised as an American NTSC NES (it came with a separate Brazilian PAL-M board as well).

Image
Image

I'm using an EverDrive N8 as well ("Stone Age Gamer EverDrive-N8 Deluxe Kit").

I tried running as PAL on Mesen and it doesn't seem to be the same kind of "slow" (Mesen as PAL just sounds slightly slower, playing on the console is way worse).

Also, this other game of mine runs perfectly on the NES: https://wendelscardua.itch.io/raddio (and it runs the same on Mesen as NTSC, so I think it's safe to discard the PAL hypothesis).

Wait a minute, I have an hypothesis - I may have tried with an older, debug version of my ROM by mistake. If that's the case I'll be very sorry for bothering all of you :(

User avatar
Controllerhead
Posts: 89
Joined: Tue Nov 13, 2018 4:58 am
Location: $4016
Contact:

Re: Emulation speed vs actual NES speed

Post by Controllerhead » Fri Jun 26, 2020 9:11 pm

wendelscardua wrote:
Fri Jun 26, 2020 9:03 pm
it was advertised as an American NTSC NES (it came with a separate Brazilian PAL-M board as well).
That sounds strange. I would open it up and check the board / revision anyway, there could be some kind of counterfeit / wackiness going on. Unlike the carts, i think a standard phillips head should do the trick.

User avatar
tokumaru
Posts: 11745
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Emulation speed vs actual NES speed

Post by tokumaru » Fri Jun 26, 2020 10:11 pm

wendelscardua wrote:
Fri Jun 26, 2020 9:03 pm
I bought it recently, and it was advertised as an American NTSC NES (it came with a separate Brazilian PAL-M board as well).
Authentic NES consoles sold in Brazil were regular NTSC units, Playtronic just swapped the crystal and added that pesky transcoder board, which means that they can be converted back to NTSC. PAL-M timing is still the same as NTSC though, as far as software is concerned, so that surely can't be what's causing the timing issues.

wendelscardua
Posts: 7
Joined: Tue Jun 09, 2020 5:55 pm

Re: Emulation speed vs actual NES speed

Post by wendelscardua » Sat Jun 27, 2020 9:22 am

Sorry for bothering you people, I'm dumb, it was a bad ROM after all.

(I think it was the one I was using to try debugging a javascript NES emulator; said emulator would hang whenever I entered a specific minigame, so I had put lots of debug stuff on each frame, causing the slowdown)

User avatar
Controllerhead
Posts: 89
Joined: Tue Nov 13, 2018 4:58 am
Location: $4016
Contact:

Re: Emulation speed vs actual NES speed

Post by Controllerhead » Sat Jun 27, 2020 9:43 am

Well, since you're here, if you aren't using one of them them already, FCEUX and especially Mesen have great step through debuggers with breakpoints that support labeling with compilers like ASM6F. It sure beats Trace and Pray!
Image

wendelscardua
Posts: 7
Joined: Tue Jun 09, 2020 5:55 pm

Re: Emulation speed vs actual NES speed

Post by wendelscardua » Sat Jun 27, 2020 11:45 am

Controllerhead wrote:
Sat Jun 27, 2020 9:43 am
Well, since you're here, if you aren't using one of them them already, FCEUX and especially Mesen have great step through debuggers with breakpoints that support labeling with compilers like ASM6F. It sure beats Trace and Pray!
Image
I'm currently using NintendulatorDX for that kind of debugging, but by your screenshot it seems FCEUX may be nicer, I'll check it later, thanks :)

My "Trace and Pray" was a failed attempt at debugging the combination of my ROM and this JS NES emulator - https://github.com/bfirsh/jsnes - because there's a point where the game hangs, but only in that emulator. (and I use that emulator to make my ROMs available for playing on the browser at https://nes.scardua.net/ )

User avatar
Controllerhead
Posts: 89
Joined: Tue Nov 13, 2018 4:58 am
Location: $4016
Contact:

Re: Emulation speed vs actual NES speed

Post by Controllerhead » Sat Jun 27, 2020 2:53 pm

wendelscardua wrote:
Sat Jun 27, 2020 11:45 am
My "Trace and Pray" was a failed attempt at debugging the combination of my ROM and this JS NES emulator - https://github.com/bfirsh/jsnes - because there's a point where the game hangs, but only in that emulator. (and I use that emulator to make my ROMs available for playing on the browser at https://nes.scardua.net/ )
Hmm i see. It does indeed hang at that Minesweeper part. It doesn't look like JSNES has any debug functions that i can tell or know about; but, since its in Javascript, it is possible to run the Chrome step through debugger and see how the emulator is executing step by step. I'm not sure how far you've looked into this, but i would set a button trigger in your NES ASM code to trigger the minesweeper game start instead of a timer, trigger it in JSNES and a diffrent emulator while each is paused, and compare step by step how each instruction is being interpreted in CPU.js against how the successful emulator is doing it, if you haven't tried that already.
Image

PS- i also commented out the sound code because of, you know, horrible buzzing noises
Image

Post Reply