GBA Explore Diary (-)

zhaozj2021-02-16  65

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.

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.

Let's take a look at the problem of 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

In order to display a BG.

You can convert a BMP file to the BG data that the BMP file can be used through BMP2Map.exe provided in Agblib.

BG data is divided into three pieces.

CHARACTER DATA

As mentioned earlier. The GBA screen undermode0-2 is made up of the tile. These tiles are 8x8 large.

Some of these tiles call it Tile, and some people call it Character (this is called in agblib).

These data are image data for saving these 8x8 pieces.

2.map data

Map Data can be said to be a reference for the tile. It is a recorded data recorded in the screen in the screen.

3.Palatte Data

This is not to say, it is a palette.

Before displaying a BG, the upper part of the above data should be transmitted to the GBA memory.

Take a look at the code below:

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 for (...) byte. But there are 3 DMA channels in GBA (that is, no CPU, specialized to transfer large amounts of data) ), It should be like Memcpy.

The above code is to transfer the E1 image file Character, Map, Palette to the corresponding memory in GBA.

It is specifically pointed that when you use a 16-color palette, you will convert this program in this program. It pointed out which Paletee used in this Map. For example, I am above The BG of that E1 is using -p2, ie the second palette of using it. Then it corresponds to the memory address in GBA should be BG_PLTT 32 * 2 (16 colors of color palette account for 32 bytes) .

You may ask BG_VRAM, what is BG_PLTT.

#define plt 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 the VRAM.

In MODE0-2, VRAM is divided into many blocks, each 2kb (0x800). CHARACTER DATA is relatively large, it is a small bit of 8 block (16kb) .map Data, one accounts for a block.

Or look at this very useful picture

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 0xc000

DmaArrayCopy (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 can only write registers, 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 are symbolic.

X is increased, the BG is moved left, X decreases, and BG is shifted to the right.

Y increase, BG moves up, y is reduced, BG wants to move.

When X or Y is changed by 256, BG moves 1 pixel.

So the above x, y is used << 8

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

New Post(0)