Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve SGB border documentation #494

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions src/SGB_Command_Border.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
# Border and OBJ Commands

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.

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).

The process is as follows:

1. Send tiles using one or two [`CHR_TRN`](<#SGB Command $13 — CHR_TRN>) commands
2. Send the tilemap and palettes using a [`PCT_TRN`](<#SGB Command $14 — PCT_TRN>) command

After that, the current border will slowly fade out, and after a few extra frames, the new border will start fading in.

## SGB Command $13 — CHR_TRN

Used to transfer tile data (characters) to SNES Tile memory in VRAM.
This normally used to define BG tiles for the SGB Border (see PCT_TRN),
but might be also used to define moveable SNES foreground sprites (see
OBJ_TRN).
Used to transfer tile data to the SNES.
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>)).

```
Byte Content
0 Command*8+Length (fixed length=1)
1 Tile Transfer Destination
Bit 0 - Tile Numbers (0=Tiles $00-$7F, 1=Tiles $80-$FF)
Bit 1 - Tile Type (0=BG Tiles, 1=OBJ Tiles)
Bit 2-7 - Not used (zero)
2-F Not used (zero)
Bit 2-7 - Ignored
2-F Ignored
```

The tile data is sent by VRAM transfer (4 KiB).
Expand Down Expand Up @@ -99,6 +108,20 @@ The Super NES supports 8 background palettes.
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.
Thus a border can use three 15-color palettes.

## Quirks

### Sending `CHR_TRN` *after* `PCT_TRN`

Normally, tiles must be uploaded via `CHR_TRN` before `PCT_TRN` is sent.
However, it is actually possible to do the reverse!

This is possible because of how the tile transfer actually works.
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.

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.
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.
Waiting 61 to 63 (Game Boy) frames seems to be the safest.

## SGB Command $18 — OBJ_TRN

Used to start transferring object attributes to SNES object attribute memory (OAM). Unlike all other
Expand Down
10 changes: 5 additions & 5 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@
- [Command Packet Transfers](./SGB_Command_Packet.md)
- [VRAM Transfers](./SGB_VRAM_Transfer.md)
- [Color Palettes Overview](./SGB_Color_Palettes.md)
- [Command Summary](./SGB_Command_Summary.md)
- [Command List](./SGB_Command_Summary.md)
- [Palette Commands](./SGB_Command_Palettes.md)
- [Color Attribute Commands](./SGB_Command_Attribute.md)
- [Sound Functions](./SGB_Command_Sound.md)
- [System Control Commands](./SGB_Command_System.md)
- [Multiplayer Command](./SGB_Command_Multiplayer.md)
- [Border and OBJ Commands](./SGB_Command_Border.md)
- [Undocumented SGB commands](./SGB_Command_Undocumented.md)
- [System Control Commands](./SGB_Command_System.md)
- [Multiplayer Command](./SGB_Command_Multiplayer.md)
- [Border and OBJ Commands](./SGB_Command_Border.md)
- [Undocumented SGB commands](./SGB_Command_Undocumented.md)

# CPU Specifications

Expand Down