summaryrefslogtreecommitdiffstats
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c87
1 files changed, 80 insertions, 7 deletions
diff --git a/src/main.c b/src/main.c
index ec2150a..30452e1 100644
--- a/src/main.c
+++ b/src/main.c
@@ -17,9 +17,6 @@
17// BIOS calls 17// BIOS calls
18// 18//
19 19
20int bios_vblank_wait();
21int bios_div(int num, int denom);
22
23int hblank_counter = 0; 20int hblank_counter = 0;
24 21
25void 22void
@@ -48,19 +45,95 @@ int main(void) {
48 45
49 // Register interrupts. 46 // Register interrupts.
50 irq_init(); 47 irq_init();
51 irs_set(IRQ_VBLANK, irq_stub); 48 irs_set(IRQ_VBLANK, irs_stub);
52 irs_set(IRQ_HBLANK, irs_hblank_func); 49 irs_set(IRQ_HBLANK, irs_hblank_func);
53 50
51 // turn sound on
52 SOUND_STATUS = SOUND_ENABLE;
53 SOUND_DMG_MASTER = sound_volume(SOUND_SQUARE1 | SOUND_SQUARE2, 7);
54 SOUND_DSOUND_MASTER = SOUND_DMG100;
55
56 char *note_names[] = {
57 "C" , "C#" , "D" , "D#" ,
58 "E" , "F" , "F#" , "G" ,
59 "G#" , "A" , "A#" , "B"
60 };
54 int frame_counter = 0; 61 int frame_counter = 0;
62 Note active_note = NOTE_C;
63 u8 octave = 0;
64 bool playing = false;
65 bool new_note = false;
66 u8 interval = 1;
55 while(true) { 67 while(true) {
56 bios_vblank_wait(); 68 bios_vblank_wait();
57 poll_keys(); 69 poll_keys();
58 70
59 txt_position(0, 1); 71 txt_position(0, 1);
60 txt_clear_line(); 72 txt_clear_line();
61 txt_printf(" HBlank counter: %d\n", hblank_counter); 73
62 txt_clear_line(); 74 if (key_pressed(KEY_B)) {
63 txt_printf(" Frame counter: %d\n", frame_counter); 75 active_note = NOTE_C;
76 new_note = true;
77 octave = 0;
78 } else if (key_pressed(KEY_A)) {
79 active_note = NOTE_D;
80 new_note = true;
81 octave = 0;
82 } else if (key_pressed(KEY_LEFT)) {
83 active_note = NOTE_E;
84 new_note = true;
85 octave = 0;
86 } else if (key_pressed(KEY_DOWN)) {
87 active_note = NOTE_F;
88 new_note = true;
89 octave = 0;
90 } else if (key_pressed(KEY_RIGHT)) {
91 active_note = NOTE_G;
92 new_note = true;
93 octave = 0;
94 } else if (key_pressed(KEY_UP)) {
95 active_note = NOTE_A;
96 new_note = true;
97 octave = 0;
98 } else if (key_pressed(KEY_L)) {
99 active_note = NOTE_B;
100 new_note = true;
101 octave = 0;
102 } else if (key_pressed(KEY_R)) {
103 active_note = NOTE_C;
104 new_note = true;
105 octave = 1;
106 }
107 if (key_pressed(KEY_START)) {
108 interval++;
109 }
110 if (key_pressed(KEY_SELECT)) {
111 interval--;
112 }
113
114 if (key_curr > 0) {
115 txt_printf(" Playing: %s + %s\n",
116 note_names[active_note],
117 (note_names[(active_note + interval) % 12]));
118 playing = true;
119 } else {
120 playing = false;
121 }
122
123 if (playing) {
124 if (new_note) {
125 SOUND_SQUARE1_CTRL = SOUND_SQUARE_ENV_VOL(4) | SOUND_SQUARE_ENV_TIME(0);
126 SOUND_SQUARE2_CTRL = SOUND_SQUARE_ENV_VOL(4) | SOUND_SQUARE_ENV_TIME(0);
127 SOUND_SQUARE1_FREQ = SOUND_SQUARE_RESET | sound_rate(active_note, octave);
128 SOUND_SQUARE2_FREQ = SOUND_SQUARE_RESET | sound_rate((active_note + interval) % 12, octave);
129 }
130 new_note = false;
131 } else {
132 SOUND_SQUARE1_CTRL = SOUND_SQUARE_ENV_VOL(0) | SOUND_SQUARE_ENV_TIME(0);
133 SOUND_SQUARE2_CTRL = SOUND_SQUARE_ENV_VOL(0) | SOUND_SQUARE_ENV_TIME(0);
134 SOUND_SQUARE1_FREQ = 0;
135 SOUND_SQUARE2_FREQ = 0;
136 }
64 137
65 frame_counter++; 138 frame_counter++;
66 update_button_sprites(); 139 update_button_sprites();