-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapu.c
More file actions
141 lines (127 loc) · 4.05 KB
/
apu.c
File metadata and controls
141 lines (127 loc) · 4.05 KB
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
#include "apu.h"
#include "gb.h"
static const bool WAVEFORMS[4][8] = {{0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 1, 1, 1},
{0, 1, 1, 1, 1, 1, 1, 0}};
static u8 ch1_get_sample(GameBoy* gb) {
if (!gb->ch1_timer--) {
gb->ch1_timer = gb->ch1_period;
gb->ch1_index = (gb->ch1_index + 1) & 7;
}
return WAVEFORMS[gb->ch1_duty][gb->ch1_index] ? gb->ch1_env_init : 0;
}
static u8 ch2_get_sample(GameBoy* gb) {
if (!gb->ch2_timer--) {
gb->ch2_timer = gb->ch2_period;
gb->ch2_index = (gb->ch2_index + 1) & 7;
}
return WAVEFORMS[gb->ch2_duty][gb->ch2_index] ? gb->ch2_env_init : 0;
}
static u8 ch3_get_sample(GameBoy* gb) {
if (!gb->ch3_timer--) {
gb->ch3_timer = gb->ch3_period;
gb->ch3_index = (gb->ch3_index + 1) & 31;
}
u8 sample = gb->wave_ram[gb->ch3_index];
if (gb->ch3_vol == 3) {
sample >>= 2;
} else if (gb->ch3_vol == 2) {
sample >>= 1;
} else if (gb->ch3_vol == 0) {
sample = 0;
}
return sample;
}
static u8 ch4_get_sample(GameBoy* gb) {
if (!gb->ch4_timer--) {
gb->ch4_timer = gb->ch4_period;
// Clock LFSR
bool new_bit = (gb->ch4_lfsr & 1) == (gb->ch4_lfsr >> 1 & 1);
gb->ch4_lfsr =
new_bit ? (gb->ch4_lfsr | 0x8000) : (gb->ch4_lfsr & 0x7FFF);
if (gb->ch4_width) {
gb->ch4_lfsr =
new_bit ? (gb->ch4_lfsr | 0x0080) : (gb->ch4_lfsr & 0xFF7F);
}
gb->ch4_lfsr >>= 1;
}
return (gb->ch4_lfsr & 1) ? gb->ch4_env_init : 0;
}
void render_audio_sample(GameBoy* gb) {
u8 ch1_sample = gb->ch1_active ? ch1_get_sample(gb) : 0;
u8 ch2_sample = gb->ch2_active ? ch2_get_sample(gb) : 0;
u8 ch3_sample =
gb->ch3_active ? (ch3_get_sample(gb), ch3_get_sample(gb)) : 0;
u8 ch4_sample = gb->ch4_active ? ch4_get_sample(gb) : 0;
s16 converted =
0x7FFF - (ch1_sample + ch2_sample + ch3_sample + ch4_sample) * 0x444;
play_sample(converted, converted);
}
void ch1_trigger(GameBoy* gb) {
if (gb->ch1_dac) {
gb->ch1_active = true;
if (!gb->ch1_len_ctr) {
gb->ch1_len_ctr = gb->ch1_len;
}
}
}
void ch2_trigger(GameBoy* gb) {
if (gb->ch2_dac) {
gb->ch2_active = true;
if (!gb->ch2_len_ctr) {
gb->ch2_len_ctr = gb->ch2_len;
}
}
}
void ch3_trigger(GameBoy* gb) {
if (gb->ch3_dac) {
gb->ch3_active = true;
if (!gb->ch3_len_ctr) {
gb->ch3_len_ctr = gb->ch3_len;
}
}
}
void ch4_trigger(GameBoy* gb) {
if (gb->ch4_dac) {
gb->ch4_active = true;
gb->ch4_lfsr = 0;
if (!gb->ch4_len_ctr) {
gb->ch4_len_ctr = gb->ch4_len;
}
}
}
void update_ch4_period(GameBoy* gb) {
u32 period = gb->ch4_divider << 2;
if (period == 0) {
period = 2;
}
period << gb->ch4_shift;
gb->ch4_period = period;
}
#define UPDATE_LENGTH(c) \
{ \
if (gb->ch##c##_active && gb->ch##c##_len_en) { \
gb->ch##c##_len_ctr++; \
if (!gb->ch##c##_len_ctr) { \
gb->ch##c##_active = false; \
} \
} \
}
void div_apu_event(GameBoy* gb) {
gb->div_apu_counter++;
gb->div_apu_counter &= 7;
if (!(gb->div_apu_counter & 7)) {
// Envelope sweep
}
if (!(gb->div_apu_counter & 3)) {
// CH1 freq sweep
}
if (!(gb->div_apu_counter & 1)) {
// Sound length
UPDATE_LENGTH(1);
UPDATE_LENGTH(2);
UPDATE_LENGTH(3);
UPDATE_LENGTH(4);
}
}