0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

アセンブリでGBAプログラミング:ARM7でVRAM塗り潰し

Last updated at Posted at 2025-10-05

必要な道具の導入

GBA開発専用のソフトは一切使用せず、汎用ソフトのみを使用します。

sudo snap install mgba
sudo apt install binutils-arm-none-eabi gcc-arm-none-eabi

コンパイルして実行

arm-none-eabi-as -mcpu=arm7tdmi -march=armv4t -g main.s -o main.o
arm-none-eabi-ld -nostdlib -Ttext=0x00000000 -o main.elf main.o
arm-none-eabi-objcopy -O binary main.elf main.gba
main.s
    .arm
    .section .text
    .global _start

/* --- 入口 --- */
    .org 0x00000000
_start:
    b ResetHandler       @ ResetHandlerへジャンプ

/* --- 任天堂ロゴ (0x0004〜0x009F, 156バイト) --- */
.byte 0x24,0xff,0xae,0x51,0x69,0x9a,0xa2,0x21
.byte 0x3d,0x84,0x82,0x0a,0x84,0xe4,0x09,0xad
.byte 0x11,0x24,0x8b,0x98,0xc0,0x81,0x7f,0x21
.byte 0xa3,0x52,0xbe,0x19,0x93,0x09,0xce,0x20
.byte 0x10,0x46,0x4a,0x4a,0xf8,0x27,0x31,0xec
.byte 0x58,0xc7,0xe8,0x33,0x82,0xe3,0xce,0xbf
.byte 0x85,0xf4,0xdf,0x94,0xce,0x4b,0x09,0xc1
.byte 0x94,0x56,0x8a,0xc0,0x13,0x72,0xa7,0xfc
.byte 0x9f,0x84,0x4d,0x73,0xa3,0xca,0x9a,0x61
.byte 0x58,0x97,0xa3,0x27,0xfc,0x03,0x98,0x76
.byte 0x23,0x1d,0xc7,0x61,0x03,0x04,0xae,0x56
.byte 0xbf,0x38,0x84,0x00,0x40,0xa7,0x0e,0xfd
.byte 0xff,0x52,0xfe,0x03,0x6f,0x95,0x30,0xf1
.byte 0x97,0xfb,0xc0,0x85,0x60,0xd6,0x80,0x25
.byte 0xa9,0x63,0xbe,0x03,0x01,0x4e,0x38,0xe2
.byte 0xf9,0xa2,0x34,0xff,0xbb,0x3e,0x03,0x44
.byte 0x78,0x00,0x90,0xcb,0x88,0x11,0x3a,0x94
.byte 0x65,0xc0,0x7c,0x63,0x87,0xf0,0x3c,0xaf

/* --- タイトル・ゲームコード・メーカーコード (0x00A0〜0x00BF) --- */
.org 0x000000A0
.ascii "GBADEMO     "       @ タイトル 12文字
.byte 0x00,0x00,0x00,0x00     @ ゲームコード 4バイト
.byte 0x00,0x00               @ メーカーコード 2バイト
.byte 0x96                    @ 固定値
.byte 0x00                    @ メインユニットコード
.byte 0x00                    @ デバイスタイプ
.space 7                     @ 予約領域
.byte 0x00,0x00               @ 補完チェック・マスクROMバージョン
.space 2                     @ 残り予約領域

.align 2

/* --- プログラム本体 --- */
ResetHandler:
    @ モード設定: Mode3 + BG2
    ldr r0, =0x04000000
    ldr r1, =0x0403
    strh r1, [r0]

    @ VRAM 塗り潰し準備
    ldr r0, =0x06000000    @ VRAM 先頭
    ldr r1, =0x07FC0       @ R=0,G=31,B=31
    mov r2, #(240*160)     @ 画素数

FillLoop:
    strh r1, [r0], #2
    subs r2, r2, #1
    bne FillLoop

InfiniteLoop:
    b InfiniteLoop         @ 無限ループで画面保持

image.png

コードの説明

ヘッダ

ヘッダは192バイト分ある。

モード設定

ldr r0, =0x04000000
ldr r1, =0x0403        @ Mode3 + BG2 enable
strh r1, [r0]

0x04000000 → DISPCNT レジスタ(ディスプレイ制御レジスタ)
0x0403 → Mode3 を選択、BG2 を有効にする設定
Mode3 = 240×160 画面に 16bit ビットマップ描画ができるモード
BG2 は描画対象になる背景層

VRAM アドレス準備

ldr r0, =0x06000000    @ VRAM (Mode3)
ldr r1, =0x07FC0       @ R=0,G=31,B=31
mov r2, #(240*160)

r0 = 0x06000000 → VRAM の先頭アドレス(Mode3 のピクセルデータ置き場)
r1 = 0x07FC0 → 書き込む色(R=0,G=31,B=31 → 水色)
r2 = 240*160 → 画素数(画面の大きさ = 240×160)

塗り潰し

FillLoop:
    strh r1, [r0], #2
    subs r2, r2, #1
    bne FillLoop

strh → 16ビット(halfword)をメモリに書き込む
[r0] → 書き込み先アドレス(ここでは VRAM)
r1 → 書き込む値(色)
#2 → 書き込み後にアドレス r0 を 2 バイト進める

bne → ゼロフラグが 0 のとき分岐(Zero flag = 0 → r2 ≠ 0)
r2 がまだ 0 でなければ、ループ先頭 FillLoop に戻る

アンドロイドのエミュレータで確認

9122cb48c9133.jpg

アンドロイドのエミュレータでも実行出来ました。
実家にGBA実機があるので次帰った時に実行できるか試してみます。

開発環境

OS: Ubuntu 22.04.5 LTS (Jammy Jellyfish)
Kernel: Linux 6.8.0-84-generic
CPU: Intel Core i5-8250U, 4 cores, 8 threads, Little Endian
Architecture: x86_64

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?