How do I use Vblank?

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

DementedPurple
Posts: 318
Joined: Mon Jan 30, 2017 5:20 pm
Location: Colorado USA

How do I use Vblank?

Post by DementedPurple »

For the game I'm designing I need to use the Vblank bit, but I don't exactly understand how I'm supposed to access it. Is it a memory address? I don't know.
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: How do I use Vblank?

Post by tepples »

Vertical blanking (vblank) is the period between frames, during which your program can write to video memory through the PPU. It lasts about 2,270 cycles, after which point the rendering logic takes control of video memory again for the next frame. To be notified that vblank has occurred, you need to do three things:
  1. Make an NMI handler, which is a special kind of subroutine that ends with RTI instead of RTS.
  2. Set the vector at $FFFA to point to the NMI handler. Exactly how this is done depends on the assembler you're using.
  3. Set bit 7 of PPUCTRL ($2000) to 1. If bit 7 of PPUCTRL is 1, the CPU will call the NMI handler at the start of every vblank.
The NMI handler usually sets a variable in memory notifying the main program that vblank has occurred. There are two schools of thought on how to organize a game loop:
  1. The main program waits for the variable to change and then proceeds to update video memory.
  2. The main program sets a variable that the updates have been prepared in video memory, the NMI handler does the video memory updates and clears the ready variable, and then the main program waits for the variable to change before beginning the next frame of game logic. This method resembles multithreading, and it's more stable if you're using a raster split such as a status bar.
Which assembler are you using? Is it NESASM, asm6, ca65, or something else?
DementedPurple
Posts: 318
Joined: Mon Jan 30, 2017 5:20 pm
Location: Colorado USA

Re: How do I use Vblank?

Post by DementedPurple »

I don't know, none of the assemblers I've ran into ever seem to work.
User avatar
dustmop
Posts: 136
Joined: Wed Oct 16, 2013 7:55 am

Re: How do I use Vblank?

Post by dustmop »

What was the problem when you tried to use these assemblers?

In a previous thread, it seemed like you were trying to double-click on the assembler executables. If that's what's happening, you need to learn all about the command-line and how to use command-line applications. Read this - http://www.makeuseof.com/tag/a-beginner ... mand-line/ Once you're done with that, you need to "cd" to the directory that has the assembler and run a command such as:

Code: Select all

nesasm3 code.asm
Where "code.asm" is a source file that contains your assembly code.

These steps cannot be skipped. Otherwise, you'll never get anywhere, let alone close to using vblank.
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: How do I use Vblank?

Post by tokumaru »

DementedPurple wrote:I don't know, none of the assemblers I've ran into ever seem to work.
Well, they do work, because many people are making games with them... But they're not magical, you have to know what you're doing. :wink:

Anyway, don't ever use the vblank bit ($2002.7) to control the frame rate of your games. The PPU has a bug that causes a false negative to be returned in that bit if the register happens to be read at the exact time vblank starts, causing the program to constantly miss vblanks. The reliable way to detect the start of the vertical blank is with NMIs. The NMI is an interrupt that reliably fires every time vblank starts, letting you know it's safe to perform video updates for the next frame.
DementedPurple
Posts: 318
Joined: Mon Jan 30, 2017 5:20 pm
Location: Colorado USA

Re: How do I use Vblank?

Post by DementedPurple »

Okay then, so how would I use the NMI?
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: How do I use Vblank?

Post by tokumaru »

tepples explained one way to do it.
Pokun
Posts: 2681
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: How do I use Vblank?

Post by Pokun »

Make sure you understand the basics.

Nerdy Nights covers most of it pretty good, although it's not perfect:
http://nintendoage.com/forum/messagevie ... eadid=7155
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: How do I use Vblank?

Post by tokumaru »

Doesn't Nerdy Nights use the less-than-ideal "everything in the NMI" model?
Pokun
Posts: 2681
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: How do I use Vblank?

Post by Pokun »

Yep, so it's not perfect (and it also has a few other flaws). But I mean it teaches other things, like working the assembler, I think.
User avatar
OmegaMax
Posts: 80
Joined: Wed Sep 21, 2016 8:55 am
Location: Calgary.Alberta,Canada

Re: How do I use Vblank?

Post by OmegaMax »

Nerdy nights isn't perfect but there isn't a lot of other choices.
DementedPurple
Posts: 318
Joined: Mon Jan 30, 2017 5:20 pm
Location: Colorado USA

Re: How do I use Vblank?

Post by DementedPurple »

I can't find a tutorial anywhere that mentions the address of NMI
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: How do I use Vblank?

Post by lidnariq »

It's like the reset address: you set it in the last six bytes of your game.
DementedPurple
Posts: 318
Joined: Mon Jan 30, 2017 5:20 pm
Location: Colorado USA

Re: How do I use Vblank?

Post by DementedPurple »

How do I do that?
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: How do I use Vblank?

Post by lidnariq »

What assembler are you using?
Post Reply