Skip to content

Commit 941c05a

Browse files
authored
Document BG to OBJ priority in CGB mode (#454)
Co-authored-by: Eldred Habert <[email protected]> Closes #443
1 parent f697ea6 commit 941c05a

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

src/LCDC.md

+2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ When Bit 0 is cleared, the background and window lose their priority -
110110
the sprites will be always displayed on top of background and window,
111111
independently of the priority flags in OAM and BG Map attributes.
112112

113+
When Bit 0 is set, pixel priority is resolved [as described here](<#BG-to-OBJ Priority in CGB Mode>).
114+
113115
## Using LCDC
114116

115117
LCDC is a powerful tool: each bit controls a lot of behavior, and can be

src/OAM.md

+2
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,6 @@ This can be exploited to only hide parts of an object behind the background
122122
([video demonstration](https://youtu.be/B8sJGgCVvnk)).
123123
A similar behaviour [can be seen on the NES](https://forums.nesdev.org/viewtopic.php?f=10&t=16861).
124124

125+
**In CGB Mode**, BG vs. OBJ priority is declared in more than one register, [please see this page](<#BG-to-OBJ Priority in CGB Mode>) for more details.
126+
125127
:::

src/Tile_Maps.md

+37-5
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,47 @@ Bit 3 Tile VRAM Bank number (0=Bank 0, 1=Bank 1)
3333
Bit 2-0 Background Palette number (BGP0-7)
3434
```
3535

36-
When Bit 7 is set, the corresponding BG tile will have priority above
37-
all OBJs (regardless of the priority bits in OAM memory). There's also
38-
a Master Priority flag in LCDC register Bit 0 which overrides all other
39-
priority bits when cleared.
40-
4136
Note that, if the map entry at `0:9800` is tile \$2A, the attribute at
4237
`1:9800` doesn't define properties for ALL tiles \$2A on-screen, but only
4338
the one at `0:9800`!
4439

40+
### BG-to-OBJ Priority in CGB Mode
41+
42+
In CGB Mode, the priority between the BG (and window) layer and the OBJ layer is declared in three different places:
43+
- [BG Map Attribute bit 7](<#BG Map Attributes (CGB Mode only)>)
44+
- [LCDC bit 0](<#LCDC.0 — BG and Window enable/priority>)
45+
- [OAM Attributes bit 7](<#Byte 3 — Attributes/Flags>)
46+
47+
We can infer the following rules from the table below:
48+
* If the BG color index is 0, the OBJ will always have priority;
49+
* Otherwise, if LCDC bit 0 is clear, the OBJ will always have priority;
50+
* Otherwise, if both the BG Attributes and the OAM Attributes have bit 7 clear, the OBJ will have priority;
51+
* Otherwise, BG will have priority.
52+
53+
The following table shows the relations between the 3 flags:
54+
55+
LCDC bit 0 | OAM attr bit 7 | BG attr bit 7 | Priority
56+
:---------:|:--------------:|:-------------:|---------
57+
0 | 0 | 0 | OBJ
58+
0 | 0 | 1 | OBJ
59+
0 | 1 | 0 | OBJ
60+
0 | 1 | 1 | OBJ
61+
1 | 0 | 0 | OBJ
62+
1 | 0 | 1 | BG color 1–3, otherwise OBJ
63+
1 | 1 | 0 | BG color 1–3, otherwise OBJ
64+
1 | 1 | 1 | BG color 1–3, otherwise OBJ
65+
66+
[This test ROM](https://github.com/alloncm/MagenTests) can be used to observe the above.
67+
68+
::: warning
69+
70+
Keep in mind that:
71+
* OAM Attributes bit 7 will grant OBJ priority when **clear**, not when **set**.
72+
* Priority between all OBJs is resolved **before** priority with the BG layer is considered.
73+
Please refer [to this page](<#Drawing priority>) for more details.
74+
75+
:::
76+
4577
## Background (BG)
4678

4779
The [SCY and SCX](<#FF42–FF43 — SCY, SCX: Viewport Y position, X position>)

0 commit comments

Comments
 (0)