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

    Note active_note = NOTE_C_4;
    int octave_diff = 0;
    bool playing = false;
    bool new_note = false;
    u8 interval = 7;
    u32 sound_sequence[16] = {
        NOTE_C_4,
        NOTE_A_4,
        NOTE_D_5,
        NOTE_D_4,
        NOTE_F_4,
        NOTE_G_4,
        NOTE_C_4,
        NOTE_A_4,
        NOTE_D_5,
        NOTE_D_4,
        NOTE_F_4,
        NOTE_G_4,
        NOTE_A_4,
        NOTE_G_4,
        NOTE_A_4,
        NOTE_F_4,
    };
    int step_counter = 0;
    int step_counter_2 = 0;
    int frame_counter = 0;
    bool trigger_1 = true;
    bool trigger_2 = true;
    while(true) {
        bios_vblank_wait();
        poll_keys();

        txt_position(0, 1);

        if (frame_counter >= 7 * 4) {
            if ((step_counter + 1) % 4 == 0) {
                step_counter_2 = (step_counter_2 + 1) % 16;
                trigger_2 = true;
            }
            trigger_1 = true;
            frame_counter = 0;
            step_counter = (step_counter + 1) % 16;
        }

        if (trigger_1 && playing) {
            active_note = sound_sequence[step_counter];
            txt_clear_line();
            txt_printf("SYNTH 1\n\n");
            txt_clear_line();
            txt_printf("Step: %d\n", step_counter);
            txt_clear_line();
            txt_printf("Playing: %s\n\n\n\n", note_names[active_note]);
            SOUND_SQUARE1_CTRL = SOUND_SQUARE_ENV_VOL(13) | SOUND_SQUARE_ENV_TIME(4) | SOUND_SQUARE_DUTY(1);
            SOUND_SQUARE1_FREQ = SOUND_SQUARE_RESET | sound_rates[active_note];
            trigger_1 = false;
        }

        if (trigger_2 && playing) {
            active_note = sound_sequence[step_counter_2];
            txt_clear_line();
            txt_printf("SYNTH 2\n\n");
            txt_clear_line();
            txt_printf("Step: %d\n", step_counter_2);
            txt_clear_line();
            txt_printf("Playing: %s\n", note_names[active_note]);
            SOUND_SQUARE2_CTRL = SOUND_SQUARE_ENV_VOL(12) | SOUND_SQUARE_ENV_TIME(3) | SOUND_SQUARE_DUTY(3);
            SOUND_SQUARE2_FREQ = SOUND_SQUARE_RESET | sound_rates[active_note];
            trigger_2 = false;
        }

        if (key_pressed(KEY_B)) {
            playing = true;
            step_counter = 0;
            step_counter_2 = 8;
            frame_counter = 0;
            trigger_1 = true;
            trigger_2 = true;
        }

        if (key_pressed(KEY_A)) {
            playing = false;
        }

        frame_counter++;
        // update_button_sprites();
    };

    return 0;
}