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

Setting attribute values according to x and y tile positions

Setting attribute values according to x and y tile positions
by on (#186952)
Alright, filling the screen with tiles from compressed data is working in my program.

Now I encounter the problem that I was able to circumvent in my previous game by simply hardcoding the upper static background graphics and by only using one and the same palette for the lower, dynamic part of the screen:

How do I set a palette attribute value if I know the x and y position of the corresponding tiles?

Let me elaborate:

I understand that each attribute value refers to a 16 x 16 pixels (2 x 2 tiles) area. Alright, fair enough.
But why did they also have to align each attribute byte in a square format?

Why couldn't one attributes byte, which contains the palette information for four 16 x 16 areas, simply refer to a 64 x 16 area? Why does it have to be a 32 x 32 area?

This makes connecting the PPU address offset of a certain tile with the PPU address offset of the corresponding attribute value a pain in the ass.

So, is there a good function that can change the attributes value according to an x and y position?

What I need is basically a function like this:

void ChangeAttributeBits(byte x, byte y, byte palette, ref byte[64] attributes);

x: Meta tile x position from 0 to 15. (Tile position would be x * 2 and pixel position would be x * 16.)
y: Same as x, only for y.
palette: Palette index value from 0 to 3.

attributes:
An array that is a copy of the 64 attribute bytes from a single name table from the PPU.
This value gets changed:
The palette index value is written to the correct two bits in this array. So, after writing the changed array back into the PPU to $23C0, $27C0, $2BC0 or $2FC0, the 2 * 2 tiles at location x * 16, y * 16 use this new palette value.
Re: Setting attribute values according to x and y tile posit
by on (#186955)
So if you have X and Y as 16x16 pixel tile locations:

X/2 and Y/2 give you the 32x32 attribute region location.
X mod 2 and Y mod 2 give you the 4 quadrants of that location (i.e. which bits to select)

Since there are 8 attribute regions per row: ((Y/2)*8) + (X/2) = attribute byte offset


Alternatively if you have a PPU tile address and want to convert to its corresponding attribute, you can use the formula here: PPU_scrolling#Tile_and_attribute_fetching
Re: Setting attribute values according to x and y tile posit
by on (#186960)
DRW wrote:
Why couldn't one attributes byte, which contains the palette information for four 16 x 16 areas, simply refer to a 64 x 16 area? Why does it have to be a 32 x 32 area?

An uniform distribution makes more sense, I guess, and several games took advantage of the format by using 32x32-pixel metatiles. There's isn't much you can do with 64x16.

Quote:
This makes connecting the PPU address offset of a certain tile with the PPU address offset of the corresponding attribute value a pain in the ass.

It's indeed a pain in the ass, specially if you scroll vertically and have to deal with the bottomost attribute row being only 16 pixels high.

Quote:
So, is there a good function that can change the attributes value according to an x and y position?

The math I use is the one rainwarrior posted. Divide the coordinates by 2 to find the address of the attribute byte, use mod 2 to index a set of masks that can be used to manipulate the bits.

For each possible quadrant of an attribute byte, I have a pair of masks, one to clear the relevant bits in a pre-existing attribute byte, and another (the inverse of the other) to clear the unwanted bits of a byte fetched from the metatile's attributes that contains the palette index repeated 4 times. After clearing the bytes separately, I OR them together to form the final byte.
Re: Setting attribute values according to x and y tile posit
by on (#186965)
You can also mask the bits this way.

lda attribute_byte
eor attribute_data
and mask
eor attribute_byte
sta attribute_byte
Re: Setting attribute values according to x and y tile posit
by on (#186969)
psycopathicteen wrote:
You can also mask the bits this way.

lda attribute_byte
eor attribute_data
and mask
eor attribute_byte
sta attribute_byte

Nice trick! You only need one mask and there's no need for a temporary variable. Here's an example of how this works:

Code:
;%00111011 (attribute_byte)
;%11111111 (attribute_data) (%11 4 times)
;%00001100 (mask)

lda attribute_byte ;%00111011
eor attribute_data ;%11000100
and mask           ;%00000100
eor attribute_byte ;%00111111
sta attribute_byte
Re: Setting attribute values according to x and y tile posit
by on (#186998)
I think you can also flag the attribute bytes to update so you don't end up rewriting the same tiles more than once during vblank.
Re: Setting attribute values according to x and y tile posit
by on (#186999)
With attributes, since you have to read-modify-write each byte, it's often more convenient to keep a copy of the attribute data in RAM for unrestricted manipulation, and then upload rows or columns to VRAM as necessary.
Re: Setting attribute values according to x and y tile posit
by on (#187018)
rainwarrior wrote:
X/2 and Y/2 give you the 32x32 attribute region location.
X mod 2 and Y mod 2 give you the 4 quadrants of that location (i.e. which bits to select)

Since there are 8 attribute regions per row: ((Y/2)*8) + (X/2) = attribute byte offset

Thanks. This was exactly what I was looking for.

I also included the EOR hint by psycopathicteen and this is the C function that I came up with:

Code:
byte Attributes[8 * 8];

/* x: 0-15. y: 0-14. paletteIndex: 0-3. */
void ChangeAttributeBits(byte x, byte y, byte paletteIndex)
{
    byte attributesIndex = ((y >> 1) << 3) | (x >> 1);
    byte attribute = Attributes[attributesIndex];

    Attributes[attributesIndex] =
        ((attribute ^ 0xFF) & (paletteIndex << ((((y & 1) << 1) | (x & 1)) << 1))) ^ attribute;
}

Apart from the fact that on the NES, you should rather work with static and global variables instead of local ones, would you say the function is efficient enough or is there something that can be optimized?
Re: Setting attribute values according to x and y tile posit
by on (#187102)
Not in C, but in asm certainly. I think my equivalent function got four times faster and half the space when asm-optimized.
Re: Setting attribute values according to x and y tile posit
by on (#187107)
Yeah, Assembly is surely better.
But I managed to write a complete scrolling platformer in C with only the low level stuff (everything that is in vblank, access to the PPU etc.) and some general purpose functions (copy array, fill array, randomizer) written in Assembly.
Therefore, unless I really get into trouble, I continue writing the functions in C for now. Makes it 10 times easier for me to write code.
Re: Setting attribute values according to x and y tile posit
by on (#187150)
Code:
byte Attributes[8 * 8];

/* x: 0-15. y: 0-14. paletteIndex: 0-3. */
void ChangeAttributeBits(byte x, byte y, byte paletteIndex)
{
    byte attributesIndex = ((y >> 1) << 3) | (x >> 1);
    byte attribute = Attributes[attributesIndex];

    Attributes[attributesIndex] =
        ((attribute ^ 0xFF) & (paletteIndex << ((((y & 1) << 1) | (x & 1)) << 1))) ^ attribute;
}

I just found out that my above function produces incorrect results in certain constellations.

So, either I didn't implement psycopathicteen's suggestion correctly or the algorithm with the EOR itself is incorrect.

The following function seems to work correctly:
Code:
void ChangeAttributeBits(byte x, byte y, byte paletteIndex)
{
   byte attributesIndex = ((y >> 1) << 3) | (x >> 1);
   byte shifts = (((y & 1) << 1) | (x & 1)) << 1;

   Attributes[attributesIndex] =
      (Attributes[attributesIndex] & (~(0x03 << shifts))) | (paletteIndex << shifts);
}

If someone is interested, maybe he likes to check the error in the old function and whether it's just my implemenation or the whole idea of using EOR here.
Re: Setting attribute values according to x and y tile posit
by on (#187180)
Looking at the two ends of your equation, and thinking transitively:

Code:
// original
((attribute ^ 0xFF) & (paletteIndex << ((((y & 1) << 1) | (x & 1)) << 1))) ^ attribute;

// simplified (mask = the whole & term)
((attribute ^ 0xFF) & mask) ^ attribute;

// some truths about XOR
(attribute ^ attribute) = 0x00;
0x00 ^ 0xFF = 0xFF;
(attribute ^ 0xFF) ^ attribute = 0xFF;

// what the statement really does
((attribute ^ 0xFF) & mask) ^ attribute = mask | attribute;


All your mask does here is select which bits to set. It's incapable, however, of clearing any bits. You need to do both to replace the palette index.

i.e. paletteIndex of 3 will work, 1/2 will set one correct bit and fail to clear the other, and 0 will fail to clear either bit.

(By the way, you can use the bitwise complement operator ~a as a shorter form of a ^ 0xFF.)
Re: Setting attribute values according to x and y tile posit
by on (#187184)
rainwarrior wrote:
Code:
// simplified (mask = the whole & term)
((attribute ^ 0xFF) & mask) ^ attribute;

[...]

All your mask does here is select which bits to set. It's incapable, however, of clearing any bits.


O.k., but where exactly is the error? I thought that I simply implemented a C version of the following code:
tokumaru wrote:
Code:
;%00111011 (attribute_byte)
;%11111111 (attribute_data) (%11 4 times)
;%00001100 (mask)

lda attribute_byte ;%00111011
eor attribute_data ;%11000100
and mask           ;%00000100
eor attribute_byte ;%00111111
sta attribute_byte


Isn't this exactly the same?

lda attribute_byte
eor attribute_data

-->
attribute = Attributes[attributesIndex]
attribute ^ 0xFF


and mask
-->
& mask

eor attribute_byte
-->
^ attribute;

sta attribute_byte
-->
Attributes[attributesIndex] =

So, is it my code that's still in error because I didn't convert the algorithm correctly from Assembly to C? Or is the algorithm itself as written down by psychopaticteen and tokumaru that's incorrect/insufficient?
Re: Setting attribute values according to x and y tile posit
by on (#187189)
Code:
attribute                      // lda attribute_byte
^ (palette << shift)           // eor attribute_data
& (3 << shift)                 // and mask
^ attribute                    // eor attribute_byte
                               // sta attribute_byte

result = ((attribute ^ (palette << shift)) & (3 << shift)) ^ attribute;

If you want to compare it to what your second statement would have looked like in the same form, here's the reverse transformation for that:

Code:
result = (attribute & (~(3 << shift))) | (palette << shift);

lda mask                       ;
eor #$FF                       ; ~ (3 << shift)
and attribute_byte             ; & attribute
ora attribute_data             ; | (palette << shift)
sta attribute_byte             ;


We're probably comparing apples to oranges here, there's a lot of unspoken overhead here about how you prepare the mask/shift/palette values, and that's all part of doing it efficiently.
Re: Setting attribute values according to x and y tile posit
by on (#187190)
rainwarrior wrote:
Code:
^ (palette << shift)           // eor attribute_data

Now you totally lost me.

Why does eor attribute_data transform to ^ (palette << shift) instead of ^ 0xFF?

In the example, tokumaru declared attribute_data as %11111111:
tokumaru wrote:
Code:
;%11111111 (attribute_data) (%11 4 times)

So I assumed it's a constant for negation.

Even if attribute_data was a variable, how could palette << shift have ever been %11111111 in the first place if the palette value is only a value from 0 to 3?

I'm confused.
Re: Setting attribute values according to x and y tile posit
by on (#187191)
There are no constants in psychopathicteen's code (a constant would be preceded by an #).

tokumaru's binary number annotation is trying to illustrate an example of how it works step by step, filling in the various variables with an example as he would have generated them (which is different from how you have generated them in your program, but not in an important way)
Re: Setting attribute values according to x and y tile posit
by on (#187194)
rainwarrior wrote:
tokumaru's binary number annotation is trying to illustrate an example of how it works step by step, filling in the various variables with an example as he would have generated them

Exactly, it was just a test to see if the trick really worked, so I picked some arbitrary values for the variables.

As for shifting or not shifting the metatile's attribute data, that comes down to how you store it in the ROM. Many engines will dedicate only 2 bits for defining a metatile's palette index, but I personally prefer to store 4 copies of the same palette index in a byte, so I don't need to spend any time shifting (or deciding how many times to shift) those 2 bits into the correct place. ROM is not so scarce these days, I think I can afford 6 redundant bits per metatile if that will simplify and speed up attribute processing.
Re: Setting attribute values according to x and y tile posit
by on (#187270)
I set aside a whole byte for attributes too. Makes filling the attributes for scrolling really easy:
Code:
  ; top left corner of attribute byte
  lda (LeftPointer),y
  tax
  lda MetatilePalettes,x
  and #%00000011
  sta Temp

  ; top right corner of attribute byte
  lda (RightPointer),y
  tax
  lda MetatilePalettes,x
  and #%00001100
  ora Temp
  sta Temp
  iny

  ; bottom left corner of attribute byte
  lda (LeftPointer),y
  tax
  lda MetatilePalettes,x
  and #%00110000
  ora Temp
  sta Temp

  ; bottom right corner of attribute byte
  lda (RightPointer),y
  tax
  lda MetatilePalettes,x
  and #%11000000
  ora Temp
  iny