Table 2: OAM state flags
OAM
OAM (Object Attribute Memory) is management data region for all sprites rendered to the screen. OAM is constituted by lower table (512 bytes) and upper table (32 bytes).
Each table can contain 128 entries so PPU can render up to 128 sprites.

The data assigned to the variable $12 is the second half of the its OAM lower table entry. However, you should not access this variable directly.
To changing OAM state, use command $8500 or $8B00.

OAM lower table entry is structured as follows:
Byte Content
0
Lower 8 bit of X location
1
Y location
2
Tile address
3
Vertical/ Horizontal flip, priority, palette, and name table select

The data operated by the command $8500 or $8B00 is a 4-bit area of the forth byte which specified vertical and horizontal flip and sprite priority.
All other data is automatically assigned appropriately by sprite rendering logic so the users do not have to be aware of its behavior.
The forth byte of lower table entry is structured as follows:
Bit Content
7
Vertical flip flag
If this is true, the sprite graphic will be flipped vertically.
6
Horizontal flip flag
If this is true, the sprite graphic will be flipped horizontally.
4-5
Sprite priority
The variable $02 (depth) affects to sprites with the same priority.
The sprite priority of OAM directly defines the rendering order of the BG layers and the sprite layers.
Normally, priority 2 is used but when drawing at the same layer as the player character, set 3.
1-3
Palette select
Specify index of CGRAM palette for sprite.
16 colors from CGRAM $100 + (p << 5) will be assigned to the sprite (p is the value of these 3 bits).
This value is assigned automatically when sprites are rendered.
0
Name table select
Specifies which of two character tables existing in the VRAM to use.
This value is assigned automatically when sprites are rendered.
Rendering priority of BG layers and sprite layers
This rendering order depends on PPU I/O $2015 (BGMODE). Normally, mode 1 is used in each level.
Mode Number of colors of each layer Order (front -> back)
0
4 colors each
3AB2ab1CD0cd
1
16 colors for BG1, 2. 4 colors for BG3. BG4 is not available.
3AB2ab1C0c
C3AB2ab10c
2
16 colors for BG1, 2. BG3, 4 are not available.
3A2B1a0b
3
256 colors for BG1. 16 colors for BG2. BG3, 4 are not available.
3A2B1a0b
4
256 colors for BG1. 4 colors for BG2. BG3, 4 are not available.
3A2B1a0b
5
16 colors for BG1. 4 colors for BG2. BG3, 4 are not available.
3A2B1a0b
6
16 colors for BG1. BG2, 3, 4 are not available.
3A21a0
7
256 colors for BG1. BG2, 3, 4 are not available.
Zoom and rotate functions are available.
321a0
7 Extend BG
256 colors for BG1. 128 colors for BG2. BG3, 4 are not available.
Zoom and rotate functions are available. Character data and tilemap are common to the BG1 and BG2.
32B1a0b

Note
1. ABCD abcd is correspond to BG1 - 4. Captial letter is prioritized tile, small letter is the other.
2. 0123 means sprite layer and each number means its priority.
3. The second order of mode 1 is for mode 1 BG3 prioritize mode. In this case, prioritized BG3 tiles are renderd on front.
4. Priorities of BG tiles are specified in mapchip data or foreground/ background tilemap.
5. Number of colors of each layer means the maximum number of colors for each one tile. Each BG layer can use 8 palettes so actual maximum colors is multipled by 8.
6. There is no concept of a palette for layers that use 128 or 256 color tiles.
7. Since the number of colors that can be held in CGRAM is 256 colors, the maximum simultaneous coloring number is 256 including sprites in every mode.