-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.c
167 lines (133 loc) · 4.11 KB
/
sound.c
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "gba.h"
#include "sound.h"
void setupSounds() {
REG_SOUNDCNT_X = SND_ENABLED;
REG_SOUNDCNT_H = SND_OUTPUT_RATIO_100 |
DSA_OUTPUT_RATIO_100 |
DSA_OUTPUT_TO_BOTH |
DSA_TIMER0 |
DSA_FIFO_RESET |
DSB_OUTPUT_RATIO_100 |
DSB_OUTPUT_TO_BOTH |
DSB_TIMER1 |
DSB_FIFO_RESET;
REG_SOUNDCNT_L = 0;
}
void playSoundA(const signed char* sound, int length, int looping) {
// Set DMA channel to 1
dma[1].cnt = 0;
DMANow(1, sound, REG_FIFO_A, DMA_DESTINATION_FIXED | DMA_AT_REFRESH | DMA_REPEAT | DMA_32);
// Set up timer 0
REG_TM0CNT = 0;
int ticks = (PROCESSOR_CYCLES_PER_SECOND / SOUND_FREQ);
REG_TM0D = -ticks;
REG_TM0CNT = TIMER_ON;
// typedef struct{
// const signed char* data;
// int length;
// int frequency;
// int isPlaying;
// int looping;
// int duration;
// int priority;
// int vBlankCount;
// } SOUND;
// Initialize struct members of soundA
soundA.data = sound;
soundA.length = length;
soundA.frequency = SOUND_FREQ;
soundA.isPlaying = 1; //true
soundA.looping = looping;
soundA.duration = ((VBLANK_FREQ * length) / SOUND_FREQ);
soundA.vBlankCount = 0;
}
void playSoundB(const signed char* sound, int length, int looping) {
// Set DMA channel to 2
dma[2].cnt = 0;
DMANow(2, sound, REG_FIFO_B, DMA_DESTINATION_FIXED | DMA_AT_REFRESH | DMA_REPEAT | DMA_32);
// Set up timer 1
REG_TM1CNT = 0;
int ticks = PROCESSOR_CYCLES_PER_SECOND / SOUND_FREQ;
REG_TM1D = -ticks;
REG_TM1CNT = TIMER_ON;
// Initialize struct members of soundB
soundB.data = sound;
soundB.length = length;
soundB.frequency = SOUND_FREQ;
soundB.isPlaying = 1;
soundB.looping = looping;
soundB.duration = ((VBLANK_FREQ * length) / SOUND_FREQ);
soundB.vBlankCount = 0;
}
void pauseSounds() {
soundA.isPlaying = 0;
soundB.isPlaying = 0;
REG_TM0CNT = TIMER_OFF;
REG_TM1CNT = TIMER_OFF;
}
void unpauseSounds() {
soundA.isPlaying = 1;
soundB.isPlaying = 1;
REG_TM0CNT = TIMER_ON;
REG_TM1CNT = TIMER_ON;
}
void stopSounds() {
soundA.isPlaying = 0;
soundB.isPlaying = 0;
dma[1].cnt = 0;
dma[2].cnt = 0;
REG_TM0CNT = 0;
REG_TM1CNT = 0;
}
// TODO 2.1: Write this function
void setupInterrupts() {
// Disable interrupt master enable register
REG_IME = 0;
// Enable VBlank interrupts
REG_IE |= INT_VBLANK;
REG_DISPSTAT |= INT_VBLANK_ENABLE;
// TODO 5.3: Add bits for timer interrupts (timer 2 and timer 3)
REG_IE |= INT_TM2 | INT_TM3;
// Set interrupt handling function
REG_INTERRUPT = interruptHandler;
// Re-enable interrupt master enable register
REG_IME = 1;
}
// TODO 2.2: Write this function
void interruptHandler() {
// Disable interrupt master enable register
REG_IME = 0;
// If interrupt flag register identifies a VBlank interrupt
if (REG_IF & INT_VBLANK) {
// Handle soundA
if (soundA.isPlaying) {
soundA.vBlankCount++;
if (soundA.vBlankCount >= soundA.duration) {
if (soundA.looping) {
playSoundA(soundA.data, soundA.length, soundA.looping);
} else {
soundA.isPlaying = 0;
dma[1].cnt = 0;
REG_TM0CNT = 0;
}
}
}
// Handle soundB
if (soundB.isPlaying) {
soundB.vBlankCount++;
if (soundB.vBlankCount >= soundB.duration) {
if (soundB.looping) {
playSoundA(soundB.data, soundB.length, soundB.looping);
} else {
soundB.isPlaying = 0;
dma[2].cnt = 0;
REG_TM1CNT = 0;
}
}
}
// Notify GBA that the interrupt has been handled
REG_IF = REG_IF;
}
// Re-enable interrupt master enable register
REG_IME = 1;
}