Gameboy Advance Internals ------------------------- This document is an incomplete guide to the general memory map and graphics modes on the Gameboy Advance. It contains details of the data structures and registers that need to be used to display graphics and read the controls. At the time of writing, the Gameboy Advance has yet to be released, so there may be differences between what is written here, and the final hardware. This document was written mainly out of frustration that there is no publicly available documentation on the Gameboy Advance out there. It contains a lot that I am unsure about, and is very incomplete, so if you discover mistakes, email me and let me know. It was made without reference to any of Nintendo's official documentation. If you have any corrections or additions to add to this document, please mail me on agentq "at" millstone.demon.co.uk. (replace the "at" with an @) Hopefully this will be enough to start more amateur coders off writing their own software for the GBA. by AgentQ Revision 1.0 - 08/02/2001 Memory Map ---------- This section lists the general areas of memory as seen by the CPU, and what they are used for. Zero Page: Location: 0x00000000 Size: 255 bytes I'm not sure what this is for. External Work RAM?: Location: 0x02000000 Size: 256Kb This space is available for your game's data and code. Internal Work RAM?: Location: 0x03000000 Size: 32Kb? This space is also available for use. I'm not sure why the work RAM is split into two parts. Perhaps it's for emulation of the Gameboy Color? IO Ram: Location: 0x04000000 Size: 1Kb This area contains all the memory-mapped IO which is used to control the GBAs graphics, sound, DMA, and other features. Palette: Location: 0x05000000 Size: 0x400 bytes This area specifies the 15-bit colour values for the paletted modes. OAM: Location: 0x07000000 Size: 0x400 bytes This is the Object Attribute Memory, and is used to control the GBAs sprites. VRAM: Location: 0x06000000 Size: 0x20000 bytes The video RAM is used to store the frame buffer in bitmapped modes, and the tile data and tile maps for tile based modes. ROM: Start: 0x08000000 Size: The size of the cartridge? The ROM in the game cartridge appears in this area. Video Modes ----------- The GBA has many graphics modes, including bitmapped modes, and tile based modes. The current video mode is set by the bottom three bits of the LCD register (see IO Ram section) At the moment, I only have information about modes 0, 3, 4, and 5. Mode 0: In this mode, four background layers of 8-bit paletted tiles can be shown. These are made up of 8x8 tiles, the bitmaps of which are stored at the tile data address. The address of this data is set using registers BG0 - BG3. As the GBAs resoltion is 240x160, 30x20 tiles can be seen at once. The SCX and SCY registers can be used to scroll around a larger area of up to 64x64 tiles. In this mode, the data for each pixel is stored as 8-bit palette index. The palette at 0x05000000 stores a 15-bit color value for each of the 256 palette entries. (see description of mode 3 for the format) Up to 128 sprites can be displayed as well as the background layers, which can be up to 64x64 pixels in size. These use the second palette which is located at 0x05000200. See the OAM section for details on how to display sprites. Both background tiles and sprites use palette entry 0 as the transparent colour. Pixels in this colour will not be drawn, and allow other background layers and sprites to show through. The tile map, which stores the layout of the tiles on screen, has a selectable size up to 512x512. The tile map has a 16-bit entry for each tile, which has the following format: F E D C B A 9 8 7 6 5 4 3 2 1 0 X X X X V H T T T T T T T T T T T = The tile number V = If this bit is set, the tile is flipped vertically upside down. H = If this bit is set, the tile is flipped horizontally left to right. The address of the tile map can also be changed. Mode 3: Standard bitmapped 240x160 mode. The map starts at 0x06000000 and is 0x12C00 bytes long. It is 16-bits per pixel in the following format: F E D C B A 9 8 7 6 5 4 3 2 1 0 X B B B B B G G G G G R R R R R R = Red, G = Green, B = Blue (unless it isn't obvious!) This allows the full colour range to be displayed at once. Unfortunately, the frame buffer in this mode is too large for page flipping to be possible. One option to get around this would be to copy a frame buffer from work RAM into VRAM during the retrace. I'm not sure whether the GBAs CPU is fast enough to do the transfer quickly enough though. To display any graphics in this mode, BG2 must be enabled, and BG0, BG1 and BG3 must be disabled. Mode 4: 8-Bit paletted bitmapped mode at 240x160. The bitmap starts at either 0x06000000 or 0x0600A000, this is set by bit 3 of the LCD register. Swapping the map and drawing in the one that isn't displayed allows for page flipping techniques to be used. The palette is at 0x5000000, and contains 256 16-bit values (see mode 3 for description) To display any graphics in this mode, BG2 must be enabled, and BG0, BG1 and BG3 must be disabled. Mode 5: This appears to be another 16-bit bitmapped mode, but at a smaller resolution of 160x128. I'm not sure whether this will be stretched to fit the GBAs LCD screen, or displayed in the middle. (which seems the more likely option) The advantage of using this mode is presumably that there are two frame buffers available, and this can be used to perform page flipping effects which cannot be done in mode 3 due to the smaller memory requirements of mode 5. Bit 3 of the LCD Register sets the start of the frame buffer to 0x06000000 when bit 3 is zero, and 0x6005000 when bit 3 is one. This also may be the mode used to emulate the Gameboy Color, as it's resolution is similar to that of mode 5, at 160x144. OAM (sprites) ------------- The GBA supports 128 simultaneous sprites. These can be up to 64x64 pixels in size. The OAM, which starts at 0x07000000, has one entry for each of the 128 sprites. These entries are 8 bytes long and have the following format: Bytes 1 and 2 F E D C B A 9 8 7 6 5 4 3 2 1 0 S S X X X X X X J J J J J J J J J = Y co-ordinate of the sprite (pixels) S = See below Bytes 3 and 4 F E D C B A 9 8 7 6 5 4 3 2 1 0 S S X X X X X I I I I I I I I I S = Size of the sprite. The top two bits of the size value are found in byte 1 and the bottom two bits are in byte 3. This forms a 4-bit value which sets the size of the sprite in the following way: 0000: 8 x 8 1000: 8 x 16 0001: 16 x 16 1001: 8 x 32 0010: 32 x 32 1010: 16 x 32 0011: 64 x 64 1011: 32 x 64 0100: 16 x 8 1100: Not used 0101: 32 x 8 1101: Not used 0110: 32 x 16 1110: Not used 0111: 64 x 32 1111: Not used I = X co-ordinate of the sprite (pixels) Bytes 5 and 6 F E D C B A 9 8 7 6 5 4 3 2 1 0 X X X X P P T T T T T T T T T X P = Priority. This controls which backgrounds are in front of/behind the sprite 11 : Sprite will be drawn behind BG3, and in front of all the other layers 10 : Sprite will be drawn behind BG2 and BG3, and in front of BG0 and BG1 01 : Sprite will be drawn behind BG1, BG2, and BG3, and in front of BG0 00 : Sprite will be drawn behind all background layers NOTE: This seems to prevent a sprite being drawn in front of all the BG layers, which would be useful, therefore I have a feeling that this field is wrong. T = Tile number. This value indexes selects the bitmap of the tile to be displayed by indexing into the tile data area. (see below) Bytes 7 and 8 These bytes control sprite rotation and scaling. I'm not sure of the format of these bytes. Mail me if you know this! The tile data area contains the actual bitmap for each tile. The sprites do not share tile data with the BG layers as on the Gameboy Color. The sprite tile data starts at 0x06010000. All tiles are 8x8 pixels large. In mode 0, there are 64 bytes per tile, one byte per pixel. This is an 8-bit value which is an index into the 256 colour palette. (see the display mode description above) Sprites use the second palette which begins at 0x05000200. When the sprite is larger than 8x8 pixels, multiple tiles are glued together to make the sprite's width horizontally, and then vertically. For example, if you were to set up a 32x32 sprite, and set the tile number to 5, the sprite would be displayed as follows: --------------------- | 5 | 6 | 7 | 8 | | | | | | --------------------- | 9 | 10 | 11 | 12 | | | | | | --------------------- | 13 | 14 | 15 | 16 | | | | | | --------------------- | 17 | 18 | 19 | 20 | | | | | | --------------------- IO Ram ------ Address: 0x4000000 - LCD The screen mode F E D C B A 9 8 7 6 5 4 3 2 1 0 X X X S L K J I X X X X A M M M M = The video mode. See video modes list above. A = This bit controls the starting address of the bitmap in bitmapped modes and is used for page flipping. See the description of the specific graphics mode for details. I = If set, enable display of BG0. J = If set, enable display of BG1. K = If set, enable display of BG2. L = If set, enable display of BG3. S = If set, enable display of OAM (sprites). Address: 0x4000006 - LCY This location stores the current y location of the LCD hardware. It is incremented as the lines are drawn. The 160 lines of display are followed by 69 lines of Vblank period, before the whole thing starts again for the next frame. Waiting for this register to reach 160 is a common way to synchronize a program to 60Hz. Address: 0x4000008 - BG0 Address: 0x400000A - BG1 Address: 0x400000C - BG2 Address: 0x400000E - BG3 These addresses set up the four background layers. The format is: F E D C B A 9 8 7 6 5 4 3 2 1 0 Z Z X M M M M M X X X X S S X X Z = Size of tile map 00 : 256x256 (32x32 tiles) 01 : 512x256 (64x32 tiles) 10 : 256x512 (32x64 tiles) 11 : 512x512 (64x64 tiles) M = Starting address of character tile map 0x6000000 + M * 0x800 S = Starting address of character tile data 0x6000000 + S * 0x4000 NOTE: I know that other bits in these registers are used, but I don't know what they are used for. Mail me if you know. Address: 0x4000010 - BG0 SCX Horizontal scroll co-ordinate for BG0 Address: 0x4000012 - BG0 SCY Vertical scroll co-ordinate for BG0 Address: 0x4000014 - BG1 SCX Horizontal scroll co-ordinate for BG1 Address: 0x4000016 - BG1 SCY Vertical scroll co-ordinate for BG1 Address: 0x4000018 - BG2 SCX Horizontal scroll co-ordinate for BG2 Address: 0x400001A - BG2 SCY Vertical scroll co-ordinate for BG2 Address: 0x400001C - BG3 SCX Horizontal scroll co-ordinate for BG3 Address: 0x400001E - BG3 SCY Vertical scroll co-ordinate for BG3 F E D C B A 9 8 7 6 5 4 3 2 1 0 X X X X X X S S S S S S S S S S S = Scroll value (pixels) These registers set the pixel that is displayed in the top left hand corner of the GBA's display. All four BG planes wrap when they reach their right or bottom edges, as they do on the standard Gameboy. Address: 0x4000098 - KEY The input register F E D C B A 9 8 7 6 5 4 3 2 1 0 X X X X X X J I D U L R S E B A A = A button B = B button E = Select button S = Start button R = D-pad Right L = D-pad Left U = D-pad Up D = D-pad Down I = Right shoulder button J = Left shoulder button This register stores the state of the GBA's buttons. Each of the inputs is active low. This means that a '0' bit indicates that the key is pressed, while a '1' bit indicates that the key is not pressed. Address: 0x40000D4 - DMA3SRC DMA3 Source Address 32-bit source address for DMA transfer. Address: 0x40000D8 - DMA3DST DMA3 Destination Address 32-bit destination address for DMA transfer. Address: 0x40000DC - DMA3CNT DMA3 Control Register This address controls a DMA transfer which allows large amounts of data (up to 1Kb) to be transferred from one area of memory to another. It can also be used to clear memory to a constant value, if the source address is not incremented with each copy. Fist, set the DMA3SRC and DMA3DST registers to point to the addresses you want. Writing to DMA3CNT address with a '1' in the N field will start the transfer. F E D C B A 9 8 7 6 5 4 3 2 1 0 N A A B B S X X L L L L L L L L N = Enable DMA operation A = Type of increment applied to destination address: 00: Increment after each copy 01: Decrement after each copy 10: Leave unchanged 11: Increment after each copy B = Type of increment applied to source address: 00: Increment after each copy 01: Decrement after each copy 10: Leave unchanged 11: Leave unchanged If enabled, the address will be incremented/decremented by 2 or 4 bytes depending on the selected size. S = Size. If set, copy 32-bit quantities (words) If clear, copy 16-bit quantities (half words) L = Number of words or halfwords to copy.