This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

Problems with Background update

Problems with Background update
by on (#198818)
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:
    /* 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
Re: Problems with Background update
by on (#198819)
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.
Re: Problems with Background update
by on (#198832)
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