summaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 8b362265a39bb3af016d487584504605dc7c99bd (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
#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.
//

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);

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

    int frame_counter = 0;
    Note active_note = NOTE_C_4;
    int octave_diff = 0;
    bool playing = false;
    bool new_note = false;
    u8 interval = 4;
    while(true) {
        bios_vblank_wait();
        poll_keys();

        txt_position(0, 1);
        txt_clear_line();

        if (key_pressed(KEY_B)) {
            active_note = NOTE_C_4 + 12 * octave_diff;
            new_note = true;
        } else if (key_pressed(KEY_A)) {
            active_note = NOTE_D_4 + 12 * octave_diff;
            new_note = true;
        } else if (key_pressed(KEY_LEFT)) {
            active_note = NOTE_E_4 + 12 * octave_diff;
            new_note = true;
        } else if (key_pressed(KEY_DOWN)) {
            active_note = NOTE_F_4 + 12 * octave_diff;
            new_note = true;
        } else if (key_pressed(KEY_RIGHT)) {
            active_note = NOTE_G_4 + 12 * octave_diff;
            new_note = true;
        } else if (key_pressed(KEY_UP)) {
            active_note = NOTE_A_4 + 12 * octave_diff;
            new_note = true;
        } else if (key_pressed(KEY_L)) {
            active_note = NOTE_B_4 + 12 * octave_diff;
            new_note = true;
        } else if (key_pressed(KEY_R)) {
            active_note = NOTE_C_5 + 12 * octave_diff;
            new_note = true;
        }
        if (key_pressed(KEY_START)) {
            octave_diff = CLAMP(octave_diff + 1, -2, 3);
        }
        if (key_pressed(KEY_SELECT)) {
            octave_diff = CLAMP(octave_diff - 1, -2, 3);
        }

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

        if (playing) {
            if (new_note) {
                // SOUND_SQUARE1_SWEEP = 3 | (1 << 3) | (2 << 4);
                // SOUND_SQUARE1_CTRL = SOUND_SQUARE_ENV_VOL(15) | SOUND_SQUARE_ENV_TIME(0x7);
                // SOUND_SQUARE2_CTRL = SOUND_SQUARE_ENV_VOL(15) | SOUND_SQUARE_ENV_TIME(0);
                SOUND_SQUARE1_CTRL = SOUND_SQUARE_ENV_VOL(15) | SOUND_SQUARE_ENV_TIME(4) | SOUND_SQUARE_ENV_INC | SOUND_SQUARE_DUTY(3);
                // SOUND_SQUARE2_CTRL = SOUND_SQUARE_ENV_VOL(15) | SOUND_SQUARE_ENV_TIME(4) | SOUND_SQUARE_ENV_INC;
                SOUND_SQUARE1_FREQ = SOUND_SQUARE_RESET | sound_rates[active_note];
                // 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;
}