Problems with Background update

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

Post Reply
User avatar
-Basti-
Posts: 40
Joined: Sun Sep 26, 2010 10:29 pm

Problems with Background update

Post by -Basti- »

Hi,
I am facing an issue while I update a background once a player (two-player Pong game) hits a score. Somehow, nearly randomly after a couple of scores, one digit of the score is drawn beside the last digit of the three digits, which display the score (1-9). This is my update routine:

Code: Select all

    /* player1 score */
    PPU_ADDRESS = MSB(0x204b);
    PPU_ADDRESS = LSB(0x204b);
    PPU_DATA = 0x10+(player1.score >> 10);               /* 0x10 is the tile address of digit 0 in .chr file*/
    PPU_ADDRESS = MSB(0x204c);
    PPU_ADDRESS = LSB(0x204c);
    PPU_DATA = 0x10+((player1.score / 10) % 10);
    PPU_ADDRESS = MSB(0x204d);
    PPU_ADDRESS = LSB(0x204d);
    PPU_DATA = 0x10+(player1.score % 10);

    /* player2 score */
    PPU_ADDRESS = MSB(0x205b);
    PPU_ADDRESS = LSB(0x205b);
    PPU_DATA = 0x10+(player2.score >> 10);
    PPU_ADDRESS = MSB(0x205c);
    PPU_ADDRESS = LSB(0x205c);
    PPU_DATA = 0x10+((player2.score / 10) % 10);
    PPU_ADDRESS = MSB(0x205d);
    PPU_ADDRESS = LSB(0x205d);
    PPU_DATA = 0x10+(player2.score % 10);
Does someone has a glimpse why sometimes digits are placed at 0x204e or 0x205e?

Thanks in advance,
Sebastian
User avatar
thefox
Posts: 3134
Joined: Mon Jan 03, 2005 10:36 am
Location: 🇫🇮
Contact:

Re: Problems with Background update

Post by thefox »

Those divisions (and modulos) by 10 are potentially quite expensive operations. You should not be doing them in the middle of your VBlank updates. Instead, calculate the results outside VBlank, store them in some kind of a buffer (or a set of variables), and copy from there during VBlank.

If your updates take longer than the VBlank lasts (about 20 scanlines on NTSC machines), all kinds of crazy things can happen.
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
User avatar
-Basti-
Posts: 40
Joined: Sun Sep 26, 2010 10:29 pm

Re: Problems with Background update

Post by -Basti- »

thefox wrote:Those divisions (and modulos) by 10 are potentially quite expensive operations. You should not be doing them in the middle of your VBlank updates. Instead, calculate the results outside VBlank, store them in some kind of a buffer (or a set of variables), and copy from there during VBlank.

If your updates take longer than the VBlank lasts (about 20 scanlines on NTSC machines), all kinds of crazy things can happen.
Thank you, transferring the calculation of digits out of the NMI routine seems to solve my problem :D
Post Reply