Anes wrote:
Another question i have is about palette:
i set the "cgram_addr" writing to $2121 and then send data to $2122. We are talking in a byte fashion way.
Can somebody explain me (altough i have read a lot of docs) how palette and the 3 15-bit b,g,r are interpreted? Every 2 bytes (word fashioned) do i have to put those 3 15-bit word? And how does the ppu look for palette in mode 0? According to Snes Dev Wiki is "ppp*4 + (BG#-1)*32"... ppp is what i got from the "tile map" and since i only have BG1 -> BG#-1 will be 0. I suppouse the "*32" is in case we are using more than 1 BG.
There are thing unclear for me such as the palette number, and that stuff.
The CGRAM data port ($2122) works like the scroll registers, or like $2005 and $2006 on the NES. You write two halves of a word to the same address. Unlike the NES, you write the least significant byte first. The format of a CGRAM entry is xBBBBBGG GGGRRRRR, so first you write the GGGRRRRR to $2122 (the red and the least significant bits of green) and then you write the xBBBBBGG (the most significant bits of green and the blue) to $2122. $2121 auto-increments after every two bytes written to $2122.
So if you want to write black, red, blue, and green to CGRAM entries 0 to 3, 32 to 35, 64 to 67 and 96 to 99, you'd do the following:
black is %0 00000 00000 00000, or hex $0000
red is %0 00000 00000 11111, or hex $001F
blue is %0 11111 00000 00000, or hex $7C00
green is %0 00000 11111 00000, or hex $03E0
Code:
sep #$20 ; accumulator must be 8-bit
lda #$00 ; CGRAM entry 0
jsr writecolors
lda #$20 ; CGRAM entry 32
jsr writecolors
lda #$40 ; CGRAM entry 64
jsr writecolors
lda #$60 ; CGRAM entry 96
; fall through
writecolors:
sta $2121 ; CGRAM address
stz $2122 ; low byte of black (zero)
stz $2122 ; high byte of black (zero)
lda #$1F
sta $2122 ; low byte of red
stz $2122 ; high byte of red (zero)
stz $2122 ; low byte of blue (zero)
lda #$7C
sta $2122 ; high byte of blue
lda #$E0
sta $2122 ; low byte of green
lda #$03
sta $2122 ; high byte of green
rts
Obviously in real code you'd use a data table rather than a bunch of lda immediates.
In Mode 0, BG1 uses entries 0 through 31. A tile with attribute bits 000 will use colors 0-3, a tile with attribute bits 001 will use colors 4-7, etc. Note that 0 pixels are always transparent, so only three out of each four palette entries are actually usable. In mode 0 CGRAM entry 4, 8, 12, 16, etc. will never be displayed, just like the color entries divisible by 4 are effectively unused on the NES.
Likewise BG2 uses entries 32 through 63, BG3 uses entries 64 through 95 and BG4 uses entries 96 through 127. Mode 0 is the only mode where each BG uses different palette entries; in the other modes every BG starts at 0. Sprites always use entries 128 through 255, regardless of the mode.