summaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 30452e1dae418c406538805a82097ae328ef1f22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <string.h>

#include "common.h"
#include "gba-buttons.c"
#include "background-tiles.c"
#include "sprites.h"
#include "text.h"

//
// Main functions.
//

// TODO: Cleanup OBJ/OAM memory copying and access.
//

//
// BIOS calls
//

int hblank_counter = 0;

void
irs_hblank_func() {
    hblank_counter++;
    if (DISP_VCOUNT >= 160) {
        PAL_BUFFER_BG[0] = rgb15(0, 0, 0);
    } else {
        u16 clr = (DISP_VCOUNT / 8);
        PAL_BUFFER_BG[0] = rgb15(clr, 0, 31 - clr);
    }
}

int main(void) {
    // Configure the display in mode 0 to show OBJs, where tile memory is
    // sequential.
    DISP_CTRL = DISP_ENABLE_SPRITES | DISP_MODE_0 | DISP_BG_0;

    // Initialize sprite button overlay.
    init_sprite_pal(0, COLOR_WHITE);
    init_sprites(0);
    init_button_sprites();

    // Initialize text engine.
    txt_init(0, COLOR_RED, 0);

    // Register interrupts.
    irq_init();
    irs_set(IRQ_VBLANK, irs_stub);
    irs_set(IRQ_HBLANK, irs_hblank_func);

    // turn sound on
    SOUND_STATUS = SOUND_ENABLE;
    SOUND_DMG_MASTER = sound_volume(SOUND_SQUARE1 | SOUND_SQUARE2, 7);
    SOUND_DSOUND_MASTER = SOUND_DMG100;

    char *note_names[] = {
        "C"  , "C#" , "D"  , "D#" ,
        "E"  , "F"  , "F#" , "G"  ,
        "G#" , "A"  , "A#" , "B"
    };
    int frame_counter = 0;
    Note active_note = NOTE_C;
    u8 octave = 0;
    bool playing = false;
    bool new_note = false;
    u8 interval = 1;
    while(true) {
        bios_vblank_wait();
        poll_keys();

        txt_position(0, 1);
        txt_clear_line();

        if (key_pressed(KEY_B)) {
            active_note = NOTE_C;
            new_note = true;
            octave = 0;
        } else if (key_pressed(KEY_A)) {
            active_note = NOTE_D;
            new_note = true;
            octave = 0;
        } else if (key_pressed(KEY_LEFT)) {
            active_note = NOTE_E;
            new_note = true;
            octave = 0;
        } else if (key_pressed(KEY_DOWN)) {
            active_note = NOTE_F;
            new_note = true;
            octave = 0;
        } else if (key_pressed(KEY_RIGHT)) {
            active_note = NOTE_G;
            new_note = true;
            octave = 0;
        } else if (key_pressed(KEY_UP)) {
            active_note = NOTE_A;
            new_note = true;
            octave = 0;
        } else if (key_pressed(KEY_L)) {
            active_note = NOTE_B;
            new_note = true;
            octave = 0;
        } else if (key_pressed(KEY_R)) {
            active_note = NOTE_C;
            new_note = true;
            octave = 1;
        }
        if (key_pressed(KEY_START)) {
            interval++;
        }
        if (key_pressed(KEY_SELECT)) {
            interval--;
        }

        if (key_curr > 0) {
            txt_printf(" Playing: %s + %s\n",
                    note_names[active_note],
                    (note_names[(active_note + interval) % 12]));
            playing = true;
        } else {
            playing = false;
        }

        if (playing) {
            if (new_note) {
                SOUND_SQUARE1_CTRL = SOUND_SQUARE_ENV_VOL(4) | SOUND_SQUARE_ENV_TIME(0);
                SOUND_SQUARE2_CTRL = SOUND_SQUARE_ENV_VOL(4) | SOUND_SQUARE_ENV_TIME(0);
                SOUND_SQUARE1_FREQ = SOUND_SQUARE_RESET | sound_rate(active_note, octave);
                SOUND_SQUARE2_FREQ = SOUND_SQUARE_RESET | sound_rate((active_note + interval) % 12, octave);
            }
            new_note = false;
        } else {
            SOUND_SQUARE1_CTRL = SOUND_SQUARE_ENV_VOL(0) | SOUND_SQUARE_ENV_TIME(0);
            SOUND_SQUARE2_CTRL = SOUND_SQUARE_ENV_VOL(0) | SOUND_SQUARE_ENV_TIME(0);
            SOUND_SQUARE1_FREQ = 0;
            SOUND_SQUARE2_FREQ = 0;
        }

        frame_counter++;
        update_button_sprites();
    };

    return 0;
}