From c7549a61018af395f65a2e0115a4196fc6d18a79 Mon Sep 17 00:00:00 2001 From: ISSOtm Date: Sat, 20 May 2023 13:23:22 +0200 Subject: [PATCH 1/2] Move all commands under correct chapter Also rename the "summary" to something that makes more sense as a category name, without changing the actual page name. --- src/SUMMARY.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/SUMMARY.md b/src/SUMMARY.md index ce625ca1..7b4ab1b4 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -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 From 6ae4c9e6acd14ed712f9f9f2ddd79803cc36fa7e Mon Sep 17 00:00:00 2001 From: ISSOtm Date: Sat, 20 May 2023 13:35:17 +0200 Subject: [PATCH 2/2] Explain SGB border at a high level, and document "CHR_TRN after PCT_TRN" Fixes #199 --- src/SGB_Command_Border.md | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/SGB_Command_Border.md b/src/SGB_Command_Border.md index 578d93eb..1ffd1491 100644 --- a/src/SGB_Command_Border.md +++ b/src/SGB_Command_Border.md @@ -1,11 +1,20 @@ # 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 @@ -13,8 +22,8 @@ OBJ_TRN). 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). @@ -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