diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 81 |
1 files changed, 12 insertions, 69 deletions
@@ -5,6 +5,7 @@ | |||
5 | #include "background-tiles.c" | 5 | #include "background-tiles.c" |
6 | #include "sprites.h" | 6 | #include "sprites.h" |
7 | #include "text.h" | 7 | #include "text.h" |
8 | #include "sequencer.c" | ||
8 | 9 | ||
9 | // | 10 | // |
10 | // Main functions. | 11 | // Main functions. |
@@ -13,63 +14,6 @@ | |||
13 | // TODO: Cleanup OBJ/OAM memory copying and access. | 14 | // TODO: Cleanup OBJ/OAM memory copying and access. |
14 | // | 15 | // |
15 | 16 | ||
16 | typedef struct SeqTrigger { | ||
17 | bool trigger; | ||
18 | Note note; | ||
19 | // TODO: ... | ||
20 | } SeqTrigger; | ||
21 | |||
22 | static SeqTrigger sequence_synth_1 [] = { | ||
23 | {true, NOTE_D_4}, | ||
24 | {true, NOTE_F_4}, | ||
25 | {true, NOTE_A_4}, | ||
26 | {true, NOTE_C_5}, | ||
27 | |||
28 | {true, NOTE_D_4}, | ||
29 | {false, NOTE_D_4}, | ||
30 | {false, NOTE_D_4}, | ||
31 | {false, NOTE_D_4}, | ||
32 | |||
33 | {true, NOTE_D_4}, | ||
34 | {true, NOTE_F_4}, | ||
35 | {true, NOTE_A_4}, | ||
36 | {true, NOTE_C_5}, | ||
37 | |||
38 | {true, NOTE_D_4}, | ||
39 | {false, NOTE_D_4}, | ||
40 | {true, NOTE_A_4}, | ||
41 | {false, NOTE_A_5}, | ||
42 | }; | ||
43 | |||
44 | int step_counter = 0; | ||
45 | Note active_note; | ||
46 | void | ||
47 | irq_timer_0(void) { | ||
48 | active_note = sequence_synth_1[step_counter].note; | ||
49 | if (sequence_synth_1[step_counter].trigger) { | ||
50 | SOUND_SQUARE1_CTRL = SOUND_SQUARE_ENV_VOL(13) | SOUND_SQUARE_ENV_TIME(4) | SOUND_SQUARE_DUTY(2); | ||
51 | SOUND_SQUARE1_FREQ = SOUND_SQUARE_RESET | sound_rates[active_note]; | ||
52 | } | ||
53 | |||
54 | step_counter = (step_counter + 1) % 16; | ||
55 | } | ||
56 | |||
57 | void | ||
58 | set_time(int bpm) { | ||
59 | // TIMER_CTRL_0 = TIMER_CTRL_DISABLE; | ||
60 | |||
61 | // The number of ticks of a 1024 cycle clock in a step based on the BPM can | ||
62 | // be calculated as: | ||
63 | // X bpm -> 60000 / 4 / bpm = Y ms = Ye-3 s | ||
64 | // Y ms -> Ye-3 / 59.99e-9 /1024 = Z ticks | ||
65 | // We have to operate on integer values, so the numbers have been | ||
66 | // precalculated to `n_ticks = 244181 / bmp` | ||
67 | int n_ticks = -244181 / bpm; | ||
68 | |||
69 | irs_set(IRQ_TIMER_0, irq_timer_0); | ||
70 | TIMER_DATA_0 = n_ticks; | ||
71 | TIMER_CTRL_0 = TIMER_CTRL_IRQ | TIMER_CTRL_ENABLE | TIMER_CTRL_FREQ_3; | ||
72 | } | ||
73 | 17 | ||
74 | int main(void) { | 18 | int main(void) { |
75 | // Configure the display in mode 0 to show OBJs, where tile memory is | 19 | // Configure the display in mode 0 to show OBJs, where tile memory is |
@@ -79,10 +23,10 @@ int main(void) { | |||
79 | // Initialize sprite button overlay. | 23 | // Initialize sprite button overlay. |
80 | init_sprite_pal(0, COLOR_WHITE); | 24 | init_sprite_pal(0, COLOR_WHITE); |
81 | init_sprites(0); | 25 | init_sprites(0); |
82 | init_button_sprites(); | 26 | init_sequencer_sprites(); |
83 | 27 | ||
84 | // Initialize text engine. | 28 | // Initialize text engine. |
85 | txt_init(0, COLOR_RED, 0); | 29 | // txt_init(0, COLOR_RED, 0); |
86 | 30 | ||
87 | // Register interrupts. | 31 | // Register interrupts. |
88 | irq_init(); | 32 | irq_init(); |
@@ -90,7 +34,7 @@ int main(void) { | |||
90 | 34 | ||
91 | // turn sound on | 35 | // turn sound on |
92 | SOUND_STATUS = SOUND_ENABLE; | 36 | SOUND_STATUS = SOUND_ENABLE; |
93 | SOUND_DMG_MASTER = sound_volume(SOUND_SQUARE1 | SOUND_SQUARE2, 7); | 37 | SOUND_DMG_MASTER = sound_volume(SOUND_SQUARE1 | SOUND_SQUARE2, 3); |
94 | SOUND_DSOUND_MASTER = SOUND_DMG100; | 38 | SOUND_DSOUND_MASTER = SOUND_DMG100; |
95 | 39 | ||
96 | // Initialize timer. | 40 | // Initialize timer. |
@@ -116,16 +60,15 @@ int main(void) { | |||
116 | TIMER_CTRL_0 ^= TIMER_CTRL_ENABLE; | 60 | TIMER_CTRL_0 ^= TIMER_CTRL_ENABLE; |
117 | } | 61 | } |
118 | 62 | ||
119 | txt_position(1,6); | 63 | // txt_position(1,6); |
120 | txt_clear_line(); | 64 | // txt_clear_line(); |
121 | txt_printf(" BPM: %d\n\n", bpm); | 65 | // txt_printf(" BPM: %d\n\n", bpm); |
122 | |||
123 | txt_clear_line(); | ||
124 | txt_printf(" Step: %d\n", step_counter); | ||
125 | txt_clear_line(); | ||
126 | txt_printf(" Note: %s\n", note_names[active_note]); | ||
127 | 66 | ||
128 | update_button_sprites(); | 67 | // txt_clear_line(); |
68 | // txt_printf(" Step: %d\n", step_counter); | ||
69 | // txt_clear_line(); | ||
70 | // txt_printf(" Note: %s\n", note_names[active_note]); | ||
71 | render_sequencer_sprites(); | ||
129 | }; | 72 | }; |
130 | 73 | ||
131 | return 0; | 74 | return 0; |