GBA Explore Diary (-)

zhaozj2021-02-08  244

GBA Explore Diary (-)

Mode0-2 BG and VRAM

GBA's VRAM has a total of 96KB. In Mode3-5, Vram is the same as the computer's display memory. The screen is corresponding to a display memory address.

You can take a look at the following two functions, write a writepoint function under MODE4.

Void Plotpixel (int X, int y, unsigned short int C)

{

M_VideoBuffer [(Y) * 120 (x)] = (c);

}

Void SetPixel (Int x, int y, u8 color)

{

Register U16 * TC;

Tc = M_VideoBuffer Y * 120 X / 2;

IF (x & 1)

* TC = ((* TC & 0x00FF) (Color << 8));

Else

* TC = (* TC & 0xFF00) Color;

}

Since the minimum data transfer unit supported by GBA is 16 bits, the 16 bits are information on two points in MODE4 (256 colors).

So the above Plotpixel is a write method for two points. The SetPixel is written to a single point by some simple displacement operations.

It is particularly pointed out that the value of the U16 in a VRAM corresponds to the front point, and the high 8 bit corresponds to the back point.

Said so many mod4, look at the most exciting part of GBA Mode0-2

Set the register of REG_DISPCNT

Don't say more, it is to specify the mode and supported BG you want to display.

For example, see this statement:

* (vu16 *) REG_DISPCNT = DISP_MODE_1 | DISP_BG0_ON;

This statement is to let GBA support MODE1 display mode and support BG0

Of course you can also

* (VU16 *) REG_DISPCNT = DISP_MODE_1 | DISP_BG0_ON | DISP_BG1_ON | DISP_BG2_ON

But you can't add BG3. Because MODE1 can only support BG0, BG1, BG2.

Mode0-2 is all using TILE (Figure) first.

And Mode0-2 has different BGs

A screen can be displayed by a plurality of BG. For example, you can arrange the ground as a BG, the sky is a BG, etc.

Each BG corresponds to a register

TEXT BG: It can't be rotated, it seems to be only 16 colors. Of course, if you use a 16-color palette, you can use up to 16 palette.

Rotation BG: It can be rotated, enlarged. You can only use 256 colors. If a 256-color palette is used, you cannot use a 16-color palette. Because the GBA's BG palette is only 256x2 = 512 So big.

Look at BG allocation in different modes

Mode 0: 4 Text Bg

Mode 1: Text Bg: BG0 BG1

Rotation BG: BG2

Mode 2: raotation BG: BG2, BG3

Next, look at the registers that control these BGs:

TEXT BG

In the official development package provided by Nintendo, Agblib, it is written in this register.

#define reg_base 0x04000000 // Registers

#define reg_bg0cnt (REG_BASE 0x8) // BG 0 Control

#define reg_bg1cnt (REG_BASE 0x8) // BG 1 Control

2.Rotation BG

In the official development package provided by Nintendo, Agblib, it is written in this register.

#define reg_base 0x04000000 // Registers

#define reg_bg2cnt (REG_BASE 0x8) // BG 0 Control

#define reg_bg3cnt (REG_BASE 0x8) // bg 1 controlporiority refers to the priority of the display.

0 up, 4 lowest.

Look at the problem about BG size. ScrEEN SIZE SETTING TEXT Screen Rotation / Scaling Screen Screen Size Screen Data Screen Size Screen Data 00 256 × 256 2 KBytes 128 × 128 256 BYtes 01 512 × 256 4 Kbytes 256 × 256 1 Kbyte 10 256 × 512 4 kbytes 512 × 512 4 KBYTES 11 512 × 512 8 KBYTES 1024 × 1024 16 kbytes

To display a BG. You can convert a BMP file into a BG data that can be used in GBA through bg. BG data is divided into GBA. 1.Character Data has been said to .Mode0-2 The GBA screen is made up of the tile. These tiles are 8x8 big. These tiles are called Tile, and some people call it Character (Agblib is called this). These data is to save these 8x8 image of image data. Caracter Data can have 256. Because 16kb = 16384 BYTES = 256 * 8 * 8 Some picture converters can't prompt this maximum limit, such as KaleId, even if your BMP contains more than 256 Character It still still converts you, but once there will be an error once it runs in the GBA program. So it is recommended to use the BMP2Map.exe in AGBLIB .2.map DataMap Data can be said to be the reference block. It is Record the discharged data of Character Data in the screen .3.Palatte Data does not have to say, that is, the palette should transmit the upper part of the above data to GBA memory before displaying a BG. Take a look at the bottom below. Code: DmaArrayCopy (3, E1_CHARACTER, BG_VRAM, 32); DmaArrayCopy (3, E1_MAP, BG_VRAM 0X8000, 32); DmaArrayCopy (3, E1_PALETTE, BG_PLTT 32 * 2, 32); DmaArrayCopy is a function of data transfer. Of course You can also write a byte by word (...) by one byte. But there are 3 DMA channels in GBA (just the CPU, specialized to transmit large amounts of data), it should be like Memcpy Such a function, its transmission speed is much faster than Memcpy. The above code is to transfer the E1 image file Character, Map, Palette to the corresponding memory in GBA. Specially pointed out that when using a 16-color palette, If you convert it with bmp2map.exe. So you should pay attention to this program - Paletee in this program. It points out which PaleteE used in this Map. For example, the E1 I I used to use -p2, even if the second Monograph. Then it corresponds to the memory address in GBA should be BG_PLTT 32 * 2 (16 colors total 32 bytes). You may ask BG_VRAM, BG_PLTT is Shi. # Define pltt 0x05000000 #define BG_PLTT (PLTT 0x0) #define vram 0x06000000 #define BG_VRAM (VRAM 0x0) They are actually vram, Palette in the specified location in memory. Map data, Character Data is placed in VRAM. In MODE0- 2, VRAM is divided into many blocks, each 2kb (0x800) .Character Data is a big bigger, one to account for 8 block (16kb). Map Data is a little bit, it takes a block. Or look at this Very useful picture of the value of Character Base Block is 0-3

Screen Base Block (already equipped with Map Data) is 0-31

Of course, you can't let them repeat, otherwise the data written first is covered.

Let's take a look at the code for setting BG registers:

#define textbg_size_256x256 0x0

#define textbg_size_256x512 0x8000

#define textbg_size_512x256 0x4000

#define textbg_size_512x512 0xC000

#define rotbg_size_128x128 0x0

#define rotbg_size_256x256 0x4000

#define rotbg_size_512x512 0x8000

#define rotbg_size_1024x1024 0xc000dmaArrayCopy (3, E1_CHARACTER, BG_VRAM, 32);

DmaArrayCopy (3, E1_MAP, BG_VRAM 0x8000, 32);

DmaArrayCopy (3, E1_PALETTE, BG_PLTT 32 * 2, 32);

* (vu16 *) REG_BG0CNT =

BG_COLOR_16 | TEXTBG_SIZE_256X256 | BG_PRIORITY_0

| 16 << bg_screen_base_shift | 0 << BG_CHAR_BASE_SHIFT;

You correspond to the front map.

16 is the value of Screen Base Block, 0 is the value of Character Base Block.

0x8000 is the corresponding offset address of Base Block 16, 0 is the corresponding offset site of Base Block 0

GBA hardware has many support for BG processing, such as rotating, moving, enlargement, Alpha, Marcel.

Let's take a look at the movement of BG.

This is also achieved by writing to the register of GBA.

There are two registers in BG2 (Rotation BG) to control its location .reg_bg2x, reg_bg2y

Still look at their definitions

#define reg_base 0x04000000

// registers

#define reg_bg2x (REG_BASE 0x28)

// bg 2 start x coordinate

#define reg_bg2y (reg_base 0x2c)

// bg 2 start y coordinate

They are only written registers, you don't read them, otherwise you can't read what useful data.

But you can write them:

S32 x = 10 << 8;

S32 y = 10 << 8;

* (S32 *) REG_BG2X = x;

* (S32 *) REG_BG2Y = Y;

They need to remember that they are 32-bit data, and it is symbolic .x increases, BG is shifted left, X decreases, BG is shifted to right. Y increases, BG moves up, y reduction, BG want Down. When X or Y is changed 256, BG moves 1 pixel. So the above X, Y is used << 8

转载请注明原文地址:https://www.9cbs.com/read-3171.html

New Post(0)