In game palette glitchy re-writing / VRAM access

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

Post Reply
User avatar
Brickman
Posts: 23
Joined: Mon Oct 03, 2011 9:33 pm

In game palette glitchy re-writing / VRAM access

Post by Brickman »

I have a demo with a firery background and i wish to 'flicker' the fire by cycling one of the colors in my palettes.

I've been able to get the colors switched however the screen shakes for a split second when i do this.
(I've put my color change call right after V-blank but it doesn't help.)

Code: Select all

//accessing the 2nd color in the palette (3F01)
//knes
PPU_ADDR(0x3F01); //writes the high and low byte of the address
PPU.data = palette_fire2; //stores the new color
Just for fun I also tried inline asm to just store the value at address 3F01 but that didn't work at all :) :

Code: Select all

__asm__ ("lda %v", palette_fire);
__asm__ ("sta $3F01");
I'm simply trying to do the realtime color cycling super mario bros does with it's coin/question block color. (SMB is glitch free of course.)
User avatar
Jeroen
Posts: 1048
Joined: Tue Jul 03, 2007 1:49 pm

Post by Jeroen »

First of all what are you doing in vblank besides this? It seems SOMETHING is taking too long and spilling into rendering time.

Also after your writes try resetting the scroll by rewriting your current scroll value to $2005. (takes two writes...if not scrolling try using value #$00)
Shiru
Posts: 1161
Joined: Sat Jan 23, 2010 11:41 pm

Post by Shiru »

You need to reset PPU_ADDR to zero before visible part of the screen starts, i.e. right after your VBlank code.
User avatar
tokumaru
Posts: 12106
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: In game palette glitchy re-writing / VRAM access

Post by tokumaru »

Brickman wrote:I've been able to get the colors switched however the screen shakes for a split second when i do this.
(I've put my color change call right after V-blank but it doesn't help.)
I'm guessing the screen jumps because your palette write messes up the scroll (because it modifies the PPU address). Changes to the PPU must happen during VBlank (i.e. they must be part of your VBlank handler), but they must be before the resetting of the scroll (i.e. the writes to $2000 and $2005, which must be the last PPU operation of the Vblank render).
Just for fun I also tried inline asm to just store the value at address 3F01 but that didn't work at all :) :
That's because "STA $3F01" tries to store a value in the CPU addressing space, not the PPU addressing space. Always keep in mind that these two addressing spaces exist, and that the PPU space can only be accessed by the CPU through registers $2006 (address) and $2007 (data).
User avatar
tokumaru
Posts: 12106
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Post by tokumaru »

Shiru wrote:You need to reset PPU_ADDR to zero before visible part of the screen starts, i.e. right after your VBlank code.
Resetting PPU_ADDR (i.e. writing $00 to $2006 twice) works in most cases, but the correct way to fully reset the scroll is to write to $2000 and $2005 instead. Write once to $2000 to select the name table and twice to $2005 to set the horizontal and vertical scroll. If you do that you don't need to reset $2006.
User avatar
thefox
Posts: 3139
Joined: Mon Jan 03, 2005 10:36 am
Location: Tampere, Finland
Contact:

Post by thefox »

To recap what others were saying, in KNES setting the scroll would look something like this:

Code: Select all

// BASE_NT0 defines the highest bit of scroll.
PPU.ctrl = BASE_NT0|ADDRINC_1|SPR_CHR0|BG_CHR1|SPR_8X8|NMI_ON;
// Set the low 8 bits of scroll.
PPU_SCROLL( 0, 0 );
It's a good idea to have code like this at the very end of vblank, after you've done all the PPU related updates.
User avatar
Brickman
Posts: 23
Joined: Mon Oct 03, 2011 9:33 pm

Post by Brickman »

thefox wrote:

Code: Select all

// Set the low 8 bits of scroll.
PPU_SCROLL( 0, 0 );
That did it! (Writing 0 to $2006 twice), no screens bump at all now. Thank you all for your help!
:D
User avatar
Brickman
Posts: 23
Joined: Mon Oct 03, 2011 9:33 pm

Re: In game palette glitchy re-writing / VRAM access

Post by Brickman »

tokumaru wrote:
Brickman wrote:
Just for fun I also tried inline asm to just store the value at address 3F01 but that didn't work at all :) :
That's because "STA $3F01" tries to store a value in the CPU addressing space, not the PPU addressing space. Always keep in mind that these two addressing spaces exist, and that the PPU space can only be accessed by the CPU through registers $2006 (address) and $2007 (data).
Ok, that's good to know -- thank you for clearing that up that mystery. :idea:
User avatar
thefox
Posts: 3139
Joined: Mon Jan 03, 2005 10:36 am
Location: Tampere, Finland
Contact:

Post by thefox »

Brickman wrote:
thefox wrote:

Code: Select all

// Set the low 8 bits of scroll.
PPU_SCROLL( 0, 0 );
That did it! (Writing 0 to $2006 twice), no screens bump at all now. Thank you all for your help!
:D
FYI this code doesn't write to $2006 twice, it writes to $2005.
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
User avatar
tokumaru
Posts: 12106
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Post by tokumaru »

Brickman wrote:

Code: Select all

// Set the low 8 bits of scroll.
PPU_SCROLL( 0, 0 );
Don't forget about PPU.ctrl (which was also in thefox's suggestion), it's also important.
User avatar
Brickman
Posts: 23
Joined: Mon Oct 03, 2011 9:33 pm

Post by Brickman »

tokumaru wrote:
Brickman wrote:

Code: Select all

// Set the low 8 bits of scroll.
PPU_SCROLL( 0, 0 );
Don't forget about PPU.ctrl (which was also in thefox's suggestion), it's also important.
Yeah, I had that implemented (from one of the KNES demo src's.)
Post Reply