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で文字表示

0
Posted at

目的

GBAでの開発に当たり、文字表示機能が無いとデバッグが困難な為、まずは16進数の0~Fまでを表示する機能を実装する。

効果

c2f7c42ef1a9e8

検証

ビルドの手順は筆者の以下の記事を参考
GBA自作コードを実機で動作させる手順

main.c
typedef unsigned char  uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int   uint32_t;

#define REG_DISPCNT (*(volatile unsigned int*)0x04000000)
#define MODE3 3
#define BG2_ENABLE (1 << 10)

#define VRAM ((volatile uint16_t*)0x06000000)

volatile const uint8_t font[16][8] = {
// 0
    {0b00111100,  //   ████
     0b01100110,  // ██    ██
     0b01100110,  // ██    ██
     0b01100110,  // ██    ██
     0b01100110,  // ██    ██
     0b01100110,  // ██    ██
     0b00111100,  //   ████
     0b00000000}, // 

    // 1
    {0b00011000,  //    ██
     0b00111000,  //   ███
     0b00011000,  //    ██
     0b00011000,  //    ██
     0b00011000,  //    ██
     0b00011000,  //    ██
     0b00111100,  //   ████
     0b00000000}, // 
    // 2
    {0b00111100,  //  ██████
     0b01100110,  // ██    ██
     0b00000110,  //      ██
     0b00011100,  //    ██
     0b00110000,  //   ██
     0b01100000,  //  ██
     0b01111110,  // ████████
     0b00000000}, //
    // 3
    {0b00111100,  //  ██████
     0b01100110,  // ██    ██
     0b00000110,  //      ██
     0b00011100,  //    ███
     0b00000110,  //      ██
     0b01100110,  // ██    ██
     0b00111100,  //  ██████
     0b00000000}, // 
// 4
    {0b00001100,  //     ██
     0b00011100,  //    ███
     0b00111100,  //   ████
     0b01101100,  //  ██ ██
     0b01111110,  // ████████
     0b00001100,  //     ██
     0b00001100,  //     ██
     0b00000000}, // 
    
    // 5
    {0b01111110,  // ████████
     0b01100000,  // ██
     0b01111100,  // ██████
     0b00000110,  //       ██
     0b00000110,  //       ██
     0b01100110,  // ██   ██
     0b00111100,  //  ██████
     0b00000000}, // 
    
    // 6
    {0b00011100,  //    ████
     0b00110000,  //   ██
     0b01100000,  //  ██
     0b01111100,  // ██████
     0b01100110,  // ██    ██
     0b01100110,  // ██    ██
     0b00111100,  //  ██████
     0b00000000}, // 
    
    // 7
    {0b01111110,  // ████████
     0b00000110,  //      ██
     0b00001100,  //     ██
     0b00011000,  //    ██
     0b00110000,  //   ██
     0b00110000,  //   ██
     0b00110000,  //   ██
     0b00000000}, //
// 8
    {0b00111100,  //  ██████
     0b01100110,  // ██    ██
     0b01100110,  // ██    ██
     0b00111100,  //  ██████
     0b01100110,  // ██    ██
     0b01100110,  // ██    ██
     0b00111100,  //  ██████
     0b00000000}, // 
    
    // 9
    {0b00111100,  //  ██████
     0b01100110,  // ██    ██
     0b01100110,  // ██    ██
     0b00111110,  //  ██████
     0b00000110,  //      ██
     0b00001100,  //     ██
     0b00111000,  //  ████
     0b00000000}, // 
    
    // A
    {0b00011000,  //   ██
     0b00111100,  //  ████
     0b01100110,  // ██  ██
     0b01100110,  // ██  ██
     0b01111110,  // ██████
     0b01100110,  // ██  ██
     0b01100110,  // ██  ██
     0b00000000}, // 
    
    // B
    {0b01111100,  // █████
     0b01100110,  // ██  ██
     0b01100110,  // ██  ██
     0b01111100,  // █████
     0b01100110,  // ██  ██
     0b01100110,  // ██  ██
     0b01111100,  // █████
     0b00000000}, // 
    
    // C
    {0b00111100,  //  ██████
     0b01100110,  // ██    ██
     0b01100000,  // ██
     0b01100000,  // ██
     0b01100000,  // ██
     0b01100110,  // ██    ██
     0b00111100,  //  ██████
     0b00000000}, //
// D
    {0b01111000,  // █████
     0b01101100,  // ██   ██
     0b01100110,  // ██    ██
     0b01100110,  // ██    ██
     0b01100110,  // ██    ██
     0b01101100,  // ██  ██
     0b01111000,  // █████
     0b00000000}, // 
    
    // E
    {0b01111110,  // ████████
     0b01100000,  // ██
     0b01100000,  // ██
     0b01111100,  // ██████
     0b01100000,  // ██
     0b01100000,  // ██
     0b01111110,  // ████████
     0b00000000}, // 
    
    // F
    {0b01111110,  // ████████
     0b01100000,  // ██
     0b01100000,  // ██
     0b01111100,  // ██████
     0b01100000,  // ██
     0b01100000,  // ██
     0b01100000,  // ██
     0b00000000}, //
};

static inline void putpixel(int x, int y, uint16_t c) {
    VRAM[y * 240 + x] = c;
}

void draw_hex(int x, int y, int v, uint16_t color) {
    for (int row = 0; row < 8; row++) {
        uint8_t bits = font[v][row];

        for (int col = 0; col < 8; col++) {
            if (bits & (1 << (7 - col))) {
                putpixel(x + col, y + row, color);
            }
        }
    }
}

int main(void) {
    REG_DISPCNT = MODE3 | BG2_ENABLE;

    for (int i = 0; i < 240 * 160; i++)
        VRAM[i] = 0;

    int x = 10;
    int y = 10;

    for (int i = 0; i < 16; i++) {
        draw_hex(x + i * 9, y, i, 0xFFFF);
    }

    while (1);
}
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?