Skip to content

Commit 2512bdf

Browse files
committed
Create rendering overview doc
Move related information there, to avoid burying it in the middle of hardware reg documentation. Will reorganise and clean it up next.
1 parent 11b6276 commit 2512bdf

File tree

5 files changed

+87
-77
lines changed

5 files changed

+87
-77
lines changed

src/Audio.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ The speaker merges back the two channels, losing the stereo aspect entirely.
3636

3737
The Game Boy's sound chip is called the <abbr title="Audio Processing Unit">APU</abbr>.
3838

39-
The APU runs off the same master clock as the rest of the Game Boy, which is to say, it is fully synced with the CPU and [PPU](<#Rendering Overview>).
39+
The APU runs off the same master clock as the rest of the Game Boy, which is to say, it is fully synced with the CPU and [PPU](<#Graphics Overview>).
4040
This also means that the APU runs about 2.4% faster on the SGB1, increasing frequencies by as much and thus sounding slightly higher-pitched.
4141
The SGB2 rectifies this issue.
4242

src/Rendering.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Rendering overview
2+
3+
::: tip TERMINOLOGY
4+
5+
All references to a "dot" are meant as dots (one 4 MiHz time unit).
6+
Dots remain the same regardless of whether the CPU is in [double speed](<#FF4D — KEY1 (CGB Mode only): Prepare speed switch>).
7+
8+
:::
9+
10+
<figure><figcaption>
11+
12+
The following diagram shows how a Game Boy frame is decomposed:
13+
14+
</figcaption>
15+
16+
{{#include imgs/ppu_modes_timing.svg:2:}}
17+
18+
</figure>
19+
20+
## STAT modes
21+
22+
The LCD controller operates on a 2^22 Hz = 4.194 MHz dot clock. An
23+
entire frame is 154 scanlines = 70224 dots = 16.74 ms. On scanlines 0
24+
through 143, the PPU cycles through modes 2, 3, and 0 once
25+
every 456 dots. Scanlines 144 through 153 are mode 1.
26+
27+
The following sequence is typical when the display is enabled:
28+
29+
```
30+
Mode 2 2_____2_____2_____2_____2_____2___________________2____
31+
Mode 3 _33____33____33____33____33____33__________________3___
32+
Mode 0 ___000___000___000___000___000___000________________000
33+
Mode 1 ____________________________________11111111111111_____
34+
```
35+
36+
When the PPU is accessing some video-related memory, that memory is inaccessible
37+
to the CPU: writes are ignored, and reads return garbage values (usually $FF).
38+
39+
- During modes 2 and 3, the CPU cannot access [OAM](<#VRAM Sprite Attribute Table (OAM)>) ($FE00-FE9F).
40+
- During mode 3, the CPU cannot access VRAM or [CGB palette data registers](<#LCD Color Palettes (CGB only)>)
41+
($FF69,$FF6B).
42+
43+
Mode | Action | Duration | Accessible video memory
44+
-----|------------------------------------------------------------------|--------------------------------------------------------------------|-------------------------
45+
2 | Searching OAM for OBJs whose Y coordinate overlap this line | 80 dots | VRAM, CGB palettes
46+
3 | Reading OAM and VRAM to generate the picture | 168 to 291 dots, depending on object count | None
47+
0 | Nothing (HBlank) | 85 to 208 dots, depending on previous mode 3 duration | VRAM, OAM, CGB palettes
48+
1 | Nothing (VBlank) | 4560 dots (10 scanlines) | VRAM, OAM, CGB palettes
49+
50+
## Properties of STAT modes
51+
52+
Unlike most game consoles, the Game Boy can pause the dot clock briefly,
53+
making Mode 3 longer and Mode 0 shorter. It routinely takes a 6 to 11 dot
54+
break to fetch an OBJ's tile between background tile pattern fetches.
55+
On DMG and GBC in DMG mode, mid-scanline writes to [`BGP`](<#FF47 — BGP (Non-CGB Mode only): BG palette data>)
56+
allow observing this behavior, as the delay from drawing an OBJ shifts the
57+
write's effect to the left by that many dots.
58+
59+
Three things are known to pause the dot clock:
60+
61+
- Background scrolling: If `SCX % 8` is not zero at the start of the scanline, rendering is paused for that many dots while the shifter discards that many pixels from the leftmost tile.
62+
- Window: An active window pauses for at least 6 dots, as the background fetching mechanism starts over at the left side of the window.
63+
- Objects: Each object usually pauses for `11 - min(5, (x + SCX) % 8)` dots.
64+
Because object fetch waits for background fetch to finish, an object's cost depends on its position relative to the left side of the background tile under it. It's greater if an object is directly aligned over the background tile, less if the object is to the right. If the object's left side is over the window, use `255 - WX` instead of `SCX` in this formula.
65+
66+
::: warning TO BE VERIFIED
67+
68+
The exact pause duration for window start is
69+
not confirmed; it may have the same background fetch finish delay as
70+
an object. If two objects' left sides are over the same background or
71+
window tile, the second may pause for fewer dots.
72+
73+
:::
74+
75+
A hardware quirk in the monochrome Game Boy makes the LCD interrupt
76+
sometimes trigger when writing to STAT (including writing \$00) during
77+
OAM scan, HBlank, VBlank, or LY=LYC. It behaves as if \$FF were
78+
written for one cycle, and then the written value were written the next
79+
cycle. Because the GBC in DMG mode does not have this quirk, two games
80+
that depend on this quirk (Ocean's *Road Rash* and Vic Tokai's *Xerd
81+
no Densetsu*) will not run on a GBC.
82+
83+
TODO

src/STAT.md

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -33,72 +33,9 @@ Bit 1-0 - Mode Flag (Mode 0-3, see below) (Read Only)
3333
3: Transferring Data to LCD Controller
3434
```
3535

36-
The two lower STAT bits show the current status of the PPU.
36+
The two lower STAT bits show the current [status of the PPU](<#STAT modes>).
3737

3838
Bit 2 is set when [LY](<#FF44 — LY: LCD Y coordinate \[read-only\]>) contains the same value as [LYC](<#FF45 — LYC: LY compare>).
3939
It is constantly updated.
4040

4141
Bits 3-6 select which sources are used for [the STAT interrupt](<#INT $48 — STAT interrupt>).
42-
43-
## STAT modes
44-
45-
The LCD controller operates on a 2^22 Hz = 4.194 MHz dot clock. An
46-
entire frame is 154 scanlines = 70224 dots = 16.74 ms. On scanlines 0
47-
through 143, the PPU cycles through modes 2, 3, and 0 once
48-
every 456 dots. Scanlines 144 through 153 are mode 1.
49-
50-
The following sequence is typical when the display is enabled:
51-
52-
```
53-
Mode 2 2_____2_____2_____2_____2_____2___________________2____
54-
Mode 3 _33____33____33____33____33____33__________________3___
55-
Mode 0 ___000___000___000___000___000___000________________000
56-
Mode 1 ____________________________________11111111111111_____
57-
```
58-
59-
When the PPU is accessing some video-related memory, that memory is inaccessible
60-
to the CPU: writes are ignored, and reads return garbage values (usually $FF).
61-
62-
- During modes 2 and 3, the CPU cannot access [OAM](<#VRAM Sprite Attribute Table (OAM)>) ($FE00-FE9F).
63-
- During mode 3, the CPU cannot access VRAM or [CGB palette data registers](<#LCD Color Palettes (CGB only)>)
64-
($FF69,$FF6B).
65-
66-
Mode | Action | Duration | Accessible video memory
67-
-----|------------------------------------------------------------------|--------------------------------------------------------------------|-------------------------
68-
2 | Searching OAM for OBJs whose Y coordinate overlap this line | 80 dots | VRAM, CGB palettes
69-
3 | Reading OAM and VRAM to generate the picture | 168 to 291 dots, depending on object count | None
70-
0 | Nothing (HBlank) | 85 to 208 dots, depending on previous mode 3 duration | VRAM, OAM, CGB palettes
71-
1 | Nothing (VBlank) | 4560 dots (10 scanlines) | VRAM, OAM, CGB palettes
72-
73-
## Properties of STAT modes
74-
75-
Unlike most game consoles, the Game Boy can pause the dot clock briefly,
76-
making Mode 3 longer and Mode 0 shorter. It routinely takes a 6 to 11 dot
77-
break to fetch an OBJ's tile between background tile pattern fetches.
78-
On DMG and GBC in DMG mode, mid-scanline writes to [`BGP`](<#FF47 — BGP (Non-CGB Mode only): BG palette data>)
79-
allow observing this behavior, as the delay from drawing an OBJ shifts the
80-
write's effect to the left by that many dots.
81-
82-
Three things are known to pause the dot clock:
83-
84-
- Background scrolling: If `SCX % 8` is not zero at the start of the scanline, rendering is paused for that many dots while the shifter discards that many pixels from the leftmost tile.
85-
- Window: An active window pauses for at least 6 dots, as the background fetching mechanism starts over at the left side of the window.
86-
- Objects: Each object usually pauses for `11 - min(5, (x + SCX) % 8)` dots.
87-
Because object fetch waits for background fetch to finish, an object's cost depends on its position relative to the left side of the background tile under it. It's greater if an object is directly aligned over the background tile, less if the object is to the right. If the object's left side is over the window, use `255 - WX` instead of `SCX` in this formula.
88-
89-
::: warning TO BE VERIFIED
90-
91-
The exact pause duration for window start is
92-
not confirmed; it may have the same background fetch finish delay as
93-
an object. If two objects' left sides are over the same background or
94-
window tile, the second may pause for fewer dots.
95-
96-
:::
97-
98-
A hardware quirk in the monochrome Game Boy makes the LCD interrupt
99-
sometimes trigger when writing to STAT (including writing \$00) during
100-
OAM scan, HBlank, VBlank, or LY=LYC. It behaves as if \$FF were
101-
written for one cycle, and then the written value were written the next
102-
cycle. Because the GBC in DMG mode does not have this quirk, two games
103-
that depend on this quirk (Ocean's *Road Rash* and Vic Tokai's *Xerd
104-
no Densetsu*) will not run on a GBC.

src/SUMMARY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
- [LCD Status Registers](./STAT.md)
2525
- [Scrolling](./Scrolling.md)
2626
- [Palettes](./Palettes.md)
27-
- [Pixel FIFO](./pixel_fifo.md)
27+
- [Rendering](./Rendering.md)
28+
- [Pixel FIFO](./pixel_fifo.md)
2829
- [Audio](./Audio.md)
2930
- [Audio Registers](./Audio_Registers.md)
3031
- [Audio Details](./Audio_details.md)

src/pixel_fifo.md

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
# Pixel FIFO
22

3-
::: tip TERMINOLOGY
4-
5-
All references to a dot are meant as dots (4.19 MHz). Dots remain the same regardless of
6-
CGB double speed.
7-
When it is stated that a certain action *lengthens mode 3* it means that mode 0 (HBlank) is
8-
shortened to make up for the additional time in mode 3, as shown in the following diagram.
9-
10-
:::
11-
12-
{{#include imgs/ppu_modes_timing.svg:2:}}
13-
143
## Introduction
154

165
FIFO stands for *First In, First Out*. The first pixel to be pushed to the

0 commit comments

Comments
 (0)