Skip to content

Commit 6ae4c9e

Browse files
committed
Explain SGB border at a high level, and document "CHR_TRN after PCT_TRN"
Fixes gbdev#199
1 parent c7549a6 commit 6ae4c9e

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/SGB_Command_Border.md

+29-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
# Border and OBJ Commands
22

3+
A SGB border is simply an image displayed on the "host" SNES, and the SNES functions much [like the Game Boy (Color)](./Rendering.md): tiles are arranged on the screen by a tilemap, and then colorized by color palettes.
4+
5+
However, since all of this happens on the SNES side, all of this data—tiles, tilemap, palettes—must be transferred from the GB to the SNES via [SGB VRAM transfers](./SGB_VRAM_Transfer.md).
6+
7+
The process is as follows:
8+
9+
1. Send tiles using one or two [`CHR_TRN`](<#SGB Command $13 — CHR_TRN>) commands
10+
2. Send the tilemap and palettes using a [`PCT_TRN`](<#SGB Command $14 — PCT_TRN>) command
11+
12+
After that, the current border will slowly fade out, and after a few extra frames, the new border will start fading in.
13+
314
## SGB Command $13 — CHR_TRN
415

5-
Used to transfer tile data (characters) to SNES Tile memory in VRAM.
6-
This normally used to define BG tiles for the SGB Border (see PCT_TRN),
7-
but might be also used to define moveable SNES foreground sprites (see
8-
OBJ_TRN).
16+
Used to transfer tile data to the SNES.
17+
This is normally used to write BG tiles for the SGB border (see [`PCT_TRN`](<#SGB Command $14 — PCT_TRN>)), but might be also used to define moveable SNES foreground sprites (see [`OBJ_TRN`](<#SGB Command $18 — OBJ_TRN>)).
918

1019
```
1120
Byte Content
1221
0 Command*8+Length (fixed length=1)
1322
1 Tile Transfer Destination
1423
Bit 0 - Tile Numbers (0=Tiles $00-$7F, 1=Tiles $80-$FF)
1524
Bit 1 - Tile Type (0=BG Tiles, 1=OBJ Tiles)
16-
Bit 2-7 - Not used (zero)
17-
2-F Not used (zero)
25+
Bit 2-7 - Ignored
26+
2-F Ignored
1827
```
1928

2029
The tile data is sent by VRAM transfer (4 KiB).
@@ -99,6 +108,20 @@ The Super NES supports 8 background palettes.
99108
The SGB system software (when run in a LLE such as Mesen 2) has been observed to use background palette 0 for the GB screen, palettes 1, 2, 3, and 7 for the menus, and palettes 4, 5, and 6 for the border.
100109
Thus a border can use three 15-color palettes.
101110

111+
## Quirks
112+
113+
### Sending `CHR_TRN` *after* `PCT_TRN`
114+
115+
Normally, tiles must be uploaded via `CHR_TRN` before `PCT_TRN` is sent.
116+
However, it is actually possible to do the reverse!
117+
118+
This is possible because of how the tile transfer actually works.
119+
Writing the new border's tiles to VRAM while the old border's tilemap is still being displayed would corrupt that border; so instead, the SGB BIOS stores the tiles in SNES WRAM, and copies them to SNES VRAM after the old border has been faded out.
120+
121+
It would seem possible to simply send the `CHR_TRN` right after the `PCT_TRN`, but `CHR_TRN` is ignored by the SGB BIOS during the fade-out.
122+
Therefore, the `CHR_TRN` must be sent right after the fade-out; [this was measured](https://github.com/pinobatch/little-things-gb/blob/master/sgbears/docs/long_story.txt) to be a 5-frame window, but its offset varies at least between SGB and SGB2.
123+
Waiting 61 to 63 (Game Boy) frames seems to be the safest.
124+
102125
## SGB Command $18 — OBJ_TRN
103126

104127
Used to start transferring object attributes to SNES object attribute memory (OAM). Unlike all other

0 commit comments

Comments
 (0)