GBA program development introduction 2

zhaozj2021-02-08  258

3. First GBA program

Now let's start our first GBA program. In this program we will display a picture on the screen.

First of all, build a 240 * 160 size, 256 colors of BMP pictures, such as this:

The name is called helloworld.bmp. Open the KaleID_1-2-3 program just downloaded. Select the menu file in Open, open this helloWorld.bmp file, select the menu file in the Convert and Save. The dialog box appears:

Choose the choice as the figure, pay special attention to "Bitmap Graphics (Mode 4)" and "Save As C Source" and remove "#include . Finally click" Convert and Save, save it as helloworld.h

The image data has been converted, then we start writing code. Create a main.c file and put the helloWorld.h in a directory. Then write the code in main.c.

Typedef unsigned char u8;

Typedef unsigned short u16;

Typedef unsigned long u32;

#define reg_dispcnt * (u16 *) 0x04000000 // Display register address

#define vram 0x06000000 // Image buffer address

#define palette 0x5000000 // Toner Address

#define mode_4 0x04 // mode4 logo

#define bg2_enable 0x0400 // bg_2 flag

#define setmode (mode) reg_dispcnt = (mode) // Set the macro definition of the display mode

#include "helloworld.h" // contains the header file of the image palette and the data

U16 * Palette_mem = (u16 *) Palette; // System palette

U16 * video_buffer = (u16 *) vram; // image buffer

Void Draw (U16 * SRC_PALETTE, U16 * SRC_DATA, U16 * DST_PALETTE, U16 * DST_DATA);

int main ()

{

// Set screen mode, use mode_4

SetMode (MODE_4 | BG2_ENABLE);

// HelloWorld_Pal and HelloWorld_GFX are the number of palette and image data defined in "helloworld.h"

DRAW ((U16 *) HelloWorld_Pal, (U16 *) HelloWorld_GFX, Palette_Mem, Video_Buffer;

// Dead cycle

While (1)

{;

}

// Mode_4 drawing function

Void Draw (U16 * SRC_PALETTE, U16 * SRC_DATA, U16 * DST_PALETTE, U16 * DST_DATA)

{

Int loop, x, y;

// Write a palette

For (loop = 0; loop <256; loop )

DST_PALETTE [loop] = src_palette [loop];

// Write an image buffer

FOR (x = 0; x <120; x )

FOR (y = 0; y <160; y )

DST_DATA [(Y) * 120 (x)] = SRC_DATA [(Y) * 120 (x)];}

Finally, build a compile batch command Make.bat to help us compile the generated target code.

Write the following command to a newly built make.bat file

PATH = C: / devkitadv / bin;% PATH%

gcc -lm -o main.elf main.c

Objcopy -V -O binary main.elf main.bin

PAUSE

If your devkitadv is in the D disk, then it should be pat = d: / devkitadv / bin;% PATH%, my Devkitadv is installed in the C drive. Execute make.bat, if there is no error, you can get compiled Out of main.bin and main.elf. Main.bin is the ROM of the GBA that can run. You can use the simulator VisualBoyAdVance to open it, or burn it to the GBA card.

This is the result of running in the simulator VisualBoyAdvance.

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

New Post(0)