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

typedef struct SeqTrigger {
    bool trigger;
    Note note;
    // TODO: ...
} SeqTrigger;

static SeqTrigger sequence_synth_1 [] = {
    {true, NOTE_D_4},
    {true, NOTE_F_4},
    {true, NOTE_A_4},
    {true, NOTE_C_5},

    {true, NOTE_D_4},
    {false, NOTE_D_4},
    {false, NOTE_D_4},
    {false, NOTE_D_4},

    {true, NOTE_D_4},
    {true, NOTE_F_4},
    {true, NOTE_A_4},
    {true, NOTE_C_5},

    {true, NOTE_D_4},
    {false, NOTE_D_4},
    {true, NOTE_A_4},
    {false, NOTE_A_5},
};

int step_counter = 0;
Note active_note;
void
irq_timer_0(void) {
    active_note = sequence_synth_1[step_counter].note;
    if (sequence_synth_1[step_counter].trigger) {
        SOUND_SQUARE1_CTRL = SOUND_SQUARE_ENV_VOL(13) | SOUND_SQUARE_ENV_TIME(4) | SOUND_SQUARE_DUTY(2);
        SOUND_SQUARE1_FREQ = SOUND_SQUARE_RESET | sound_rates[active_note];
    }

    step_counter = (step_counter + 1) % 16;
}

void
set_time(int bpm) {
    // TIMER_CTRL_0 = TIMER_CTRL_DISABLE;

    // The number of ticks of a 1024 cycle clock in a step based on the BPM can
    // be calculated as:
    //     X bpm -> 60000 / 4 / bpm = Y ms = Ye-3 s
    //     Y ms  -> Ye-3 / 59.99e-9 /1024 = Z ticks
    // We have to operate on integer values, so the numbers have been
    // precalculated to `n_ticks = 244181 / bmp`
    int n_ticks = -244181 / bpm;

    irs_set(IRQ_TIMER_0, irq_timer_0);
    TIMER_DATA_0 = n_ticks;
    TIMER_CTRL_0 = TIMER_CTRL_IRQ | TIMER_CTRL_ENABLE | TIMER_CTRL_FREQ_3;
}

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;

    // Initialize timer.
    int bpm = 120;

    while(true) {
        bios_vblank_wait();
        poll_keys();
        if (key_hold(KEY_UP)) {
            bpm += 1;
            set_time(bpm);
        }
        if (key_hold(KEY_DOWN)) {
            bpm -= 1;
            set_time(bpm);
        }

        if (key_pressed(KEY_START)) {
            step_counter = 0;
            set_time(bpm);
        }
        if (key_pressed(KEY_SELECT)) {
            TIMER_CTRL_0 ^= TIMER_CTRL_ENABLE;
        }

        txt_position(1,6);
        txt_clear_line();
        txt_printf(" BPM: %d\n\n", bpm);

        txt_clear_line();
        txt_printf(" Step: %d\n", step_counter);
        txt_clear_line();
        txt_printf(" Note: %s\n", note_names[active_note]);

        update_button_sprites();
    };

    return 0;
}