My EMU is running at half the speed

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
babai
Posts: 10
Joined: Thu Aug 03, 2017 7:15 am
Location: India

My EMU is running at half the speed

Post by babai »

Hello All,

This is my first post @ nesdev. I'm building my first emulator,and have finished the 6502 CPU core & a very basic sprite renderer. Below is the flow I'm taking:

1. Start:
2. Run the CPU
3. Check if total no. of cycles exceed 341 if yes goto 4, else back to 2
4. Render one scanline by the ppu
5. Adjust any excess cycles the cpu has rendered by adding excess cycles to step 3.
6. Go back to 2 if 262 scanlines haven't been rendered
6. Once all 262 scanlines have been rendered complete the frame.
7. render the frame
8. Stop

Loop the above 60 times a second

But I see the sprite animations that are happening are roughly at half the actual rate.
I counted the no. of avg CPU cycles I'm doing per frame and its coming as 29870 which I think is close to the actual NES.
Can anyone suggest what wrong I could be doing?

Thanks,
Babai
User avatar
Dwedit
Posts: 4924
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: My EMU is running at half the speed

Post by Dwedit »

Do you have the basic frame timing correct?
262 total scanlines:
* 20 vblank scanlines (Trigger NMI at start of this time, set vblank flag)
* 1 prerender scanline (clear vblank, sprite 0 hit, sprite overflow at start of this time)
* 240 visible scalines
* 1 postrender/pre-vblank scanline

If games are getting messed up by lack of sprite 0 hit, you can temporarily cheat and use the timing of Sprite 0's pixels appearing on the screen as the trigger for that instead.
Also note that sprites are drawn one pixel below their Y coordinate.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
User avatar
thefox
Posts: 3134
Joined: Mon Jan 03, 2005 10:36 am
Location: 🇫🇮
Contact:

Re: My EMU is running at half the speed

Post by thefox »

babai wrote:3. Check if total no. of cycles exceed 341 if yes goto 4, else back to 2
Are those CPU cycles or PPU cycles?
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
babai
Posts: 10
Joined: Thu Aug 03, 2017 7:15 am
Location: India

Re: My EMU is running at half the speed

Post by babai »

Below is order I'm doing it:

scanline 0 - 239:

render sprites
render background //stub for now

post render scanline 240:
I'm doing nothing

scanline 241-260:

@ line 241 set Vblank in PPUSTATUS, and serve "NMI" ISR
for the rest lines, I'm doing nothing

scanline 261:
Clear PPUSTATUS Vblank and sprite hit & overflow
babai
Posts: 10
Joined: Thu Aug 03, 2017 7:15 am
Location: India

Re: My EMU is running at half the speed

Post by babai »

thefox wrote:
babai wrote:3. Check if total no. of cycles exceed 341 if yes goto 4, else back to 2
Are those CPU cycles or PPU cycles?

those are CPU cycles * 3
babai
Posts: 10
Joined: Thu Aug 03, 2017 7:15 am
Location: India

Re: My EMU is running at half the speed

Post by babai »

Dwedit wrote:Do you have the basic frame timing correct?
262 total scanlines:
* 20 vblank scanlines (Trigger NMI at start of this time, set vblank flag)
* 1 prerender scanline (clear vblank, sprite 0 hit, sprite overflow at start of this time)
* 240 visible scalines
* 1 postrender/pre-vblank scanline

If games are getting messed up by lack of sprite 0 hit, you can temporarily cheat and use the timing of Sprite 0's pixels appearing on the screen as the trigger for that instead.
Also note that sprites are drawn one pixel below their Y coordinate.
Oh, I'm not calculating sprite 0 hit at all. I'm going to do what you suggested, and report back.

And yes I'm drawing sprites 1 pixel below.
Post Reply