First color in background palette is always 0F

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

Post Reply
User avatar
kikutano
Posts: 112
Joined: Sat May 26, 2018 6:14 am
Location: Italy

First color in background palette is always 0F

Post by kikutano » Wed Apr 15, 2020 7:14 am

Hi everyone,
I'm trying to load my background palette in this simple way:

Code: Select all

.LoadPalettes:
    lda $2002             
    lda #$3F
    sta $2006             
    lda #$00
    sta $2006             
    ldx #$00              
.LoadPalettesLoop:
    lda Palettes, x   
    sta $2007                     
    inx                           
    cpx #$20          
    bne .LoadPalettesLoop
    
.LoadAttribute:
  lda $2002             
  lda #$23
  sta $2006             
  lda #$C0
  sta $2006
               
  ldx #$00              
.LoadAttributeLoop:
  lda Attribute_Level, x      
  sta $2007             
  inx                   
  cpx #$40            
  bne .LoadAttributeLoop
  
  
My Attribute Table:

Code: Select all

Attribute_Level: 
  .db %10101010, %10101010, %10101010, %00000000, %00000000, %00000000, %00000000, %00000000
  .db %10101010, %10101010, %10101010, %00000000, %00000000, %00000000, %00000000, %00000000
  .db %00000000, %00000000, %00000000, %00000000, %00000000, %10101010, %10101010, %10101010
  .db %00000000, %00000000, %00000000, %00000000, %00000000, %10101010, %10101010, %10101010
  .db %10101010, %00000000, $00000001, %00000001, %00000000, %10101010, %10101010, %10101010
  .db %00000000, %00000000, %10101010, %00000000, %00000000, %10101010, %10101010, %10101010
  .db %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000
  .db %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000, %00000000
This is my palette:

Code: Select all

 .db $0C,$2D,$00,$10,  $0C,$15,$38,$20,  $0C,$27,$07,$30,  $0C,$05,$15,$35  ;;background palette
 .db $0F,$0C,$2C,$38,  $0F,$15,$0D,$10,  $0F,$10,$21,$30,  $0F,$00,$06,$26  ;;sprite palette
I can't understand why, no matter what palette I try to load, but the first value is always $0F and not $0C. I can't understand why. Any idea?

Image

User avatar
nesrocks
Posts: 461
Joined: Thu Aug 13, 2015 4:40 pm
Location: Rio de Janeiro - Brazil
Contact:

Re: First color in background palette is always 0F

Post by nesrocks » Wed Apr 15, 2020 7:27 am

IIRC the bg palette is taken from the first color from the sprites palettes, so make that one $0C and the others whatever value.

Code: Select all

 .db $0C,$2D,$00,$10,  $0C,$15,$38,$20,  $0C,$27,$07,$30,  $0C,$05,$15,$35  ;;background palette
 .db $0C,$0C,$2C,$38,  $0F,$15,$0D,$10,  $0F,$10,$21,$30,  $0F,$00,$06,$26  ;;sprite palette
https://twitter.com/bitinkstudios <- Follow me on twitter! Thanks!

User avatar
kikutano
Posts: 112
Joined: Sat May 26, 2018 6:14 am
Location: Italy

Re: First color in background palette is always 0F

Post by kikutano » Wed Apr 15, 2020 7:44 am

nesrocks wrote:
Wed Apr 15, 2020 7:27 am
IIRC the bg palette is taken from the first color from the sprites palettes, so make that one $0C and the others whatever value.

Code: Select all

 .db $0C,$2D,$00,$10,  $0C,$15,$38,$20,  $0C,$27,$07,$30,  $0C,$05,$15,$35  ;;background palette
 .db $0C,$0C,$2C,$38,  $0F,$15,$0D,$10,  $0F,$10,$21,$30,  $0F,$00,$06,$26  ;;sprite palette
Uh ok, so the first color of the first sprite palette represent the "transparency".

Thanks! :)

lidnariq
Posts: 9509
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: First color in background palette is always 0F

Post by lidnariq » Wed Apr 15, 2020 10:07 am

kikutano wrote:
Wed Apr 15, 2020 7:14 am
I can't understand why, no matter what palette I try to load, but the first value is always $0F and not $0C. I can't understand why. Any idea?
When you write to the palette colors at addresses $3F10, $3F14, $3F18, or $3F1C, it replaces the colors you had writen to addresses to $3F00, $3F04, $3F08, or $3F0C.

Super Mario Bros. relies on this, writing black to $3F00, and the desired backdrop color to $3F10.

User avatar
Quietust
Posts: 1567
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: First color in background palette is always 0F

Post by Quietust » Wed Apr 15, 2020 10:09 am

lidnariq wrote:
Wed Apr 15, 2020 10:07 am
When you write to the palette colors at addresses $3F10, $3F14, $3F18, or $3F1C, it replaces the colors you had writen to addresses to $3F00, $3F04, $3F08, or $3F0C.
Do note, however, that $3F04, $3F08, and $3F0C still contain distinct values, though the PPU never renders them under normal circumstances.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.

User avatar
nesrocks
Posts: 461
Joined: Thu Aug 13, 2015 4:40 pm
Location: Rio de Janeiro - Brazil
Contact:

Re: First color in background palette is always 0F

Post by nesrocks » Wed Apr 15, 2020 10:17 am

Ah that's a better explanation :)
https://twitter.com/bitinkstudios <- Follow me on twitter! Thanks!

User avatar
Controllerhead
Posts: 102
Joined: Tue Nov 13, 2018 4:58 am
Location: $4016
Contact:

Re: First color in background palette is always 0F

Post by Controllerhead » Wed Apr 15, 2020 10:11 pm

kikutano wrote:
Wed Apr 15, 2020 7:44 am
Uh ok, so the first color of the first sprite palette represent the "transparency".
Yes, but, to elaborate just a bit, $3F10 mirrors to $3F00, as do $3F20, $3F30, and so on. That will be the background color for All palettes.

I ran into similar trouble when i was writing the background to $3F00 and arbitrary data to $3F10, and wondering why my background color was acting funny.

To get around this, i write the background color to $3F20 after my palette loading loop, as the PPU increment pointer is already there, so i can just write junk data during the loop to those first spaces of each of the 8 palettes to speed things up, and it works properly!

Only writes to $3F00 or $3F10 ($3F20, $3F30, etc...) and so on matter. Just a heads up!

Pokun
Posts: 1492
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: First color in background palette is always 0F

Post by Pokun » Thu Apr 16, 2020 2:22 am

Heh I've done the same mistake. I wrote all palette entries in order and wondered why I couldn't change the backdrop color by changing what's written to sprite color 0 ($3F00). My palette changing routine naturally wrote the BG palette entries after the sprite palette entries, so BG color 0 ($3F10) is written after $3F00 and overwrites $3F00 since they are mirrors of each other. I solved it by simply writing the desired backdrop color to $3F10 as well, but I guess I could have just skipped writes to mirrored palette entries altogether.

Color 0 of the other palettes ($3F04, $3F08 and $3F0C) are unique memory locations but normally unused like Quietust said, but they can be used as the backdrop color during forced blanking, as can any other palette entry, and I guess this is the reason why the palette chan't be changed outside vblank even rendering is off or a rainbow streak will be shown.

User avatar
tokumaru
Posts: 11769
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: First color in background palette is always 0F

Post by tokumaru » Thu Apr 16, 2020 3:15 am

A lot of people run into this problem due to the naive palette update code taught in most tutorials, where a simple loop copies 32 bytes to VRAM, and considering that such tutorials don't mention the peculiar memory mirroring situation of palette RAM, this is not surprising.

In my own programs I usually treat palettes as groups of 3 colors, and the background color is handled separately, since it's more of a global setting. The palette update code is aware of that and will skip updating the mirrored/unused entries using BIT $2007 instead of STA $2007, causing the VRAM addresses to increment without writing anything to VRAM in those spots.

Post Reply