|
1 |
| -# Window |
| 1 | +# Window behavior |
2 | 2 |
|
3 | 3 | ## FF4A–FF4B — WY, WX: Window Y position, X position plus 7
|
4 | 4 |
|
5 |
| -These two registers specify the on-screen coordinates of [the Window](#Window)'s top-left pixel. |
| 5 | +These two registers specify the on-screen coordinates of [the Window]'s top-left pixel. |
6 | 6 |
|
7 |
| -The Window is visible (if enabled) when both coordinates are in the ranges |
8 |
| -WX=0..166, WY=0..143 respectively. Values WX=7, WY=0 place the Window at the |
9 |
| -top left of the screen, completely covering the background. |
| 7 | +The Window is visible (if enabled) when `WX` and `WY` are in the range \[0; 166\] and \[0; 143\] respectively. |
| 8 | +Values `WX`=7, `WY`=0 place the Window at the top left of the screen, completely covering the background. |
10 | 9 |
|
11 |
| -:::warning Warning |
| 10 | +## Window mid-frame behavior |
| 11 | + |
| 12 | +While the Window should work as just mentioned, writing to `WX`, `WY` etc. mid-frame displays more articulated behavior. |
| 13 | +There are several aspects of the window that respond differently to various mid-frame interactions; the **tl;dr** is this: |
| 14 | + |
| 15 | +- For the least glitchy results, only write to `WX`, `WY`, and `LCDC` during VBlank (possibly in your [VBlank interrupt handler]); if mid-frame writes are required, prefer writing during HBlank. |
| 16 | +- If intending to hide the Window for part of the screen (e.g. to have a status bar at the *top* of the screen instead of the bottom), hide it by setting `WX` to a high value rather than writing to `LCDC`. |
| 17 | + |
| 18 | +### Window rendering criteria |
12 | 19 |
|
13 |
| -WX values 0 and 166 are unreliable due to hardware bugs. |
| 20 | +The PPU keeps track of a “**Y condition**” throughout a frame. |
14 | 21 |
|
15 |
| -If WX is set to 0, the window will "stutter" horizontally when SCX changes |
16 |
| -(depending on SCX % 8). |
| 22 | +- On each VBlank, the *Y condition* is cleared (becomes false). |
| 23 | +- At the beginning of each scanline, if the value of `WY` is equal to [`LY`], the *Y condition* becomes true (and remains so for subsequent scanlines). |
17 | 24 |
|
18 |
| -If WX is set to 166, the window will span the entirety of the following |
19 |
| -scanline. |
| 25 | +:::tip Erratum |
| 26 | + |
| 27 | +On GBC, clearing the [Window enable bit] in `LCDC` resets the *Y condition*; `WY` must be set to `LY` or greater for the Window to display again in the current frame. |
20 | 28 |
|
21 | 29 | :::
|
22 | 30 |
|
23 |
| -## Window mid-frame behavior |
| 31 | +Additionally, the PPU maintains a counter, initialized to 0 at the beginning of each scanline. |
| 32 | +The counter is incremented for each pixel rendered; however, it also increments 7 times before the first pixel is actually rendered (this covers pixels discarded during the initial “fine scroll” adjustment). |
| 33 | + |
| 34 | +When this counter is equal to `WX`, if the *Y condition* is true and the [Window enable bit] is set in `LCDC`, background rendering is reset, beginning anew from the active row of the Window's tilemap. |
| 35 | +The coordinate of the active Window row is then incremented. |
24 | 36 |
|
25 |
| -While the Window should work as just mentioned, writing to WX, WY etc. mid-frame shows a more articulated behavior. |
| 37 | +- This process can happen more than once per scanline, making the Window's “tilemap Y coordinate” increase more than once in the scanline. |
| 38 | + (This is demonstrated by the TODO test ROM.) |
26 | 39 |
|
27 |
| -For the window to be displayed on a scanline, the following conditions must be met: |
| 40 | + However, this requires “disabling” the Window by briefly clearing its enable bit from `LCDC` first. |
| 41 | +- If this process doesn't happen, the Window's “tilemap Y coordinate” does not increase; so, if the Window is hidden (by any means) on a given scanline, the row of pixels rendered the next time it's shown will be the same as if it had not been hidden in the first place, producing a sort of vertical striped stretching: |
28 | 42 |
|
29 |
| -- **WY condition was triggered**: i.e. at some point in this frame the value of WY was equal to LY (checked at the start of Mode 2 only) |
30 |
| -- **WX condition was triggered**: i.e. the current X coordinate being rendered + 7 was equal to WX |
31 |
| -- Window enable bit in LCDC is set |
| 43 | +  |
| 44 | +- If `WX` is equal to 0, the Window is switched to before the initial “fine scroll” adjustment, causing it to be shifted left by <math><mi>SCX</mi> <mo>%</mo> <mn>8</mn></math> pixels. |
| 45 | +- On monochrome systems, `WX` = 166 (which would normally show a single Window pixel, along the right edge of the screen) exhibits a bug: the Window spans the entire screen, but offset vertically by one scanline. |
| 46 | +- On monochrome systems, if the Window is disabled via `LCDC`, but the other conditions are met *and* it would have started rendering exactly on a BG tile boundary, then where it would have started rendering, a single pixel with ID 0 (i.e. drawn as the first entry in [the BG palette]) is inserted; this offsets the remainder of the scanline.[^star_trek] |
32 | 47 |
|
33 |
| -If the WY condition has already been triggered and at the start of a row the window enable bit was set, |
34 |
| -then resetting that bit before the WX condition gets triggered on that row yields a nice window glitch pixel where the window would have been activated. |
| 48 | +[^star_trek]: This was discovered as affecting the game *Star Trek 25th anniversary*; more information and a test ROM are available [in this thread](https://github.com/LIJI32/SameBoy/issues/278#issuecomment-1189712129). |
35 | 49 |
|
36 |
| -The way the Window selects which line of its tilemap to render may be surprising: the Y position is selected by an internal counter, which is reset to 0 during VBlank and **only** incremented when the Window starts being rendered on a given scanline. |
37 |
| -In particular, this means that hiding the Window mid-frame in any way (via either `WX` or `LCDC`, usually to display a status bar at the top *and* bottom of the screen) will also inhibit incrementing that Y-position counter. |
| 50 | +[the Window]: #Window |
| 51 | +[VBlank interrupt handler]: <#INT $40 — VBlank interrupt> |
| 52 | +[Window enable bit]: <#LCDC.5 — Window enable> |
| 53 | +[`LY`]: <#FF44 — LY: LCD Y coordinate \[read-only\]> |
| 54 | +[the BG palette]: <#FF47 — BGP (Non-CGB Mode only): BG palette data> |
0 commit comments