Debian Codegraph @

src/engine/platform/sound/swan.c

raw
1 /*
2 WonderSwan sound core
3
4 Copyright (c) 2025 Adrian "asie" Siekierka
5
6 This software is provided 'as-is', without any express or implied
7 warranty. In no event will the authors be held liable for any damages
8 arising from the use of this software.
9
10 Permission is granted to anyone to use this software for any purpose,
11 including commercial applications, and to alter it and redistribute it
12 freely, subject to the following restrictions:
13
14 1. The origin of this software must not be misrepresented; you must not
15 claim that you wrote the original software. If you use this software
16 in a product, an acknowledgment in the product documentation would be
17 appreciated but is not required.
18 2. Altered source versions must be plainly marked as such, and must not be
19 misrepresented as being the original software.
20 3. This notice may not be removed or altered from any source distribution.
21 */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include "swan.h"
26
27 #define HYPERV_ENABLE 0x0080
28
29 #define SND_NOISE_ENABLE 0x10
30 #define SND_NOISE_RESET 0x08
31
32 #define SND_CH1_ENABLE 0x01
33 #define SND_CH2_ENABLE 0x02
34 #define SND_CH3_ENABLE 0x04
35 #define SND_CH4_ENABLE 0x08
36 #define SND_CH2_VOICE 0x20
37 #define SND_CH3_SWEEP 0x40
38 #define SND_CH4_NOISE 0x80
39
40 #define SND_OUT_HEADPHONES 0x80
41 #define SND_OUT_HEADPHONES_ENABLE 0x08
42 #define SND_OUT_SPEAKER_ENABLE 0x01
43
44 #define SND_VOL_CH2_LEFT_HALF 0x08
45 #define SND_VOL_CH2_LEFT_FULL 0x01
46 #define SND_VOL_CH2_RIGHT_HALF 0x02
47 #define SND_VOL_CH2_RIGHT_FULL 0x01
48
49 #define SND_TEST_CH_VOICE 0x20
50 #define SND_TEST_FAST_SWEEP 0x02
51 #define SND_TEST_HOLD_CH 0x01
52
53 static const uint8_t lfsr_tap_bits[8] = { 14, 10, 13, 4, 8, 6, 9, 11 };
54
55 void swan_sound_init(swan_sound_t *snd, bool headphones) {
56 memset(snd, 0, sizeof(swan_sound_t));
57
58 snd->out_ctrl = headphones ? 0x80 : 0x00;
59 }
60
61 uint8_t swan_sound_in(swan_sound_t *snd, uint16_t port) {
62 switch (port & 0x1FF) {
63 case 0x6A: return snd->hyper_ctrl;
64 case 0x6B: return snd->hyper_ctrl >> 8;
65 case 0x80: return snd->frequency[0];
66 case 0x81: return snd->frequency[0] >> 8;
67 case 0x82: return snd->frequency[1];
68 case 0x83: return snd->frequency[1] >> 8;
69 case 0x84: return snd->frequency[2];
70 case 0x85: return snd->frequency[2] >> 8;
71 case 0x86: return snd->frequency[3];
72 case 0x87: return snd->frequency[3] >> 8;
73 case 0x88: return snd->volume[0];
74 case 0x89: return snd->volume[1];
75 case 0x8A: return snd->volume[2];
76 case 0x8B: return snd->volume[3];
77 case 0x8C: return snd->sweep_amount;
78 case 0x8D: return snd->sweep_ticks;
79 case 0x8E: return snd->noise_ctrl;
80 case 0x8F: return snd->wave_address;
81 case 0x90: return snd->ch_ctrl;
82 case 0x91: return snd->out_ctrl;
83 case 0x92: return snd->noise_lfsr;
84 case 0x93: return snd->noise_lfsr >> 8;
85 case 0x94: return snd->voice_volume;
86 case 0x95: return snd->test_flags;
87 case 0x96: return snd->synth_output_right;
88 case 0x97: return snd->synth_output_right >> 8;
89 case 0x98: return snd->synth_output_left;
90 case 0x99: return snd->synth_output_left >> 8;
91 case 0x9A: return snd->synth_output_mono;
92 case 0x9B: return snd->synth_output_mono >> 8;
93 default: return 0;
94 }
95 }
96
97 void swan_sound_out(swan_sound_t *snd, uint16_t port, uint8_t value) {
98 switch (port & 0x1FF) {
99 case 0x6A: snd->hyper_ctrl = (snd->hyper_ctrl & 0xFF00) | value; break;
100 case 0x6B: snd->hyper_ctrl = (snd->hyper_ctrl & 0x0FFF) | ((value & 0x70) << 8); break;
101 case 0x80: snd->frequency[0] = (snd->frequency[0] & 0xFF00) | value; break;
102 case 0x81: snd->frequency[0] = (snd->frequency[0] & 0xFF) | ((value & 0x7) << 8); break;
103 case 0x82: snd->frequency[1] = (snd->frequency[1] & 0xFF00) | value; break;
104 case 0x83: snd->frequency[1] = (snd->frequency[1] & 0xFF) | ((value & 0x7) << 8); break;
105 case 0x84: snd->frequency[2] = (snd->frequency[2] & 0xFF00) | value; break;
106 case 0x85: snd->frequency[2] = (snd->frequency[2] & 0xFF) | ((value & 0x7) << 8); break;
107 case 0x86: snd->frequency[3] = (snd->frequency[3] & 0xFF00) | value; break;
108 case 0x87: snd->frequency[3] = (snd->frequency[3] & 0xFF) | ((value & 0x7) << 8); break;
109 case 0x88: snd->volume[0] = value; break;
110 case 0x89: snd->volume[1] = value; break;
111 case 0x8A: snd->volume[2] = value; break;
112 case 0x8B: snd->volume[3] = value; break;
113 case 0x8C: snd->sweep_amount = value; break;
114 case 0x8D: snd->sweep_ticks = value & 0x1F; break;
115 case 0x8E:
116 snd->noise_ctrl = value & 0x17;
117 if (value & SND_NOISE_RESET) {
118 snd->noise_lfsr = 0;
119 }
120 break;
121 case 0x8F: snd->wave_address = value; break;
122 case 0x90: snd->ch_ctrl = value; break;
123 case 0x91: snd->out_ctrl = (snd->out_ctrl & 0x80) | (value & 0x0F); break;
124 case 0x94: snd->voice_volume = value; break;
125 case 0x95: snd->test_flags = value; break;
126 }
127 }
128
129 void swan_sound_tick(swan_sound_t *snd, uint32_t cycles) {
130 for (int ch = 0; ch < 4; ch++) {
131 if (snd->ch_ctrl & (1 << ch)) {
132 snd->period_counter[ch] += cycles;
133 uint32_t step = 2048 - snd->frequency[ch];
134 while (snd->period_counter[ch] >= step) {
135 snd->sample_index[ch] = (snd->sample_index[ch] + 1) & 0x1F;
136 snd->period_counter[ch] -= step;
137
138 // Update noise counter
139 if (ch == 3 && (snd->noise_ctrl & SND_NOISE_ENABLE)) {
140 uint8_t lfsr_new = (1 ^ (snd->noise_lfsr >> 7) ^ (snd->noise_lfsr >> lfsr_tap_bits[snd->noise_ctrl & 7])) & 0x1;
141 snd->noise_lfsr = (snd->noise_lfsr << 1) | lfsr_new;
142 }
143 }
144 }
145 }
146
147 if (snd->ch_ctrl & SND_CH3_SWEEP) {
148 snd->sweep_counter += cycles;
149 uint32_t step = ((snd->test_flags & SND_TEST_FAST_SWEEP) ? 1 : 8192) * (snd->sweep_ticks + 1);
150 while (snd->sweep_counter >= step) {
151 snd->frequency[2] = (snd->frequency[2] + snd->sweep_amount) & 0x7FF;
152 snd->sweep_counter -= step;
153 }
154 }
155 }
156
157 static inline void sample_render_channel(swan_sound_t *snd, uint8_t ch, uint8_t sample) {
158 snd->ch_output_right[ch] = sample * (snd->volume[ch] & 0xF);
159 snd->ch_output_left[ch] = sample * (snd->volume[ch] >> 4);
160 }
161
162 static inline void wavetable_render_channel(swan_sound_t *snd, uint8_t ch) {
163 uint8_t index = snd->sample_index[ch];
164 uint8_t addr = (ch << 4) | (index >> 1);
165 sample_render_channel(snd, ch, (index & 1) ? (snd->wave_ram[addr] >> 4) : (snd->wave_ram[addr] & 0xF));
166 }
167
168 static void voice_render_channel2(swan_sound_t *snd) {
169 if (snd->voice_volume & SND_VOL_CH2_RIGHT_FULL) {
170 snd->ch_output_right[1] = snd->volume[1];
171 } else if (snd->voice_volume & SND_VOL_CH2_RIGHT_HALF) {
172 snd->ch_output_right[1] = snd->volume[1] >> 1;
173 }
174 if (snd->voice_volume & SND_VOL_CH2_LEFT_FULL) {
175 snd->ch_output_left[1] = snd->volume[1];
176 } else if (snd->voice_volume & SND_VOL_CH2_LEFT_HALF) {
177 snd->ch_output_left[1] = snd->volume[1] >> 1;
178 }
179 }
180
181 // See https://ws.nesdev.org/wiki/Sound
182 void swan_sound_sample(swan_sound_t *snd) {
183 // Calculate synthesizer channels
184 if (!(snd->test_flags & SND_TEST_HOLD_CH)) {
185 for (int i = 0; i < 4; i++) {
186 snd->ch_output_left[i] = 0;
187 snd->ch_output_right[i] = 0;
188 }
189
190 if (snd->ch_ctrl & SND_CH1_ENABLE) {
191 wavetable_render_channel(snd, 0);
192 }
193
194 if (snd->ch_ctrl & SND_CH2_ENABLE) {
195 if (snd->ch_ctrl & SND_CH2_VOICE) {
196 voice_render_channel2(snd);
197 } else {
198 wavetable_render_channel(snd, 1);
199 }
200 }
201
202 if (snd->ch_ctrl & SND_CH3_ENABLE) {
203 wavetable_render_channel(snd, 2);
204 }
205
206 if (snd->ch_ctrl & SND_CH4_ENABLE) {
207 if (snd->ch_ctrl & SND_CH4_NOISE) {
208 sample_render_channel(snd, 3, (snd->noise_lfsr & 1) * 15);
209 } else {
210 wavetable_render_channel(snd, 3);
211 }
212 }
213 }
214
215 // Sum synthesizer channels
216 if (snd->test_flags & SND_TEST_CH_VOICE) {
217 if (snd->ch_ctrl & SND_CH2_VOICE) {
218 snd->synth_output_left = snd->ch_output_left[1] * 5;
219 snd->synth_output_right = snd->ch_output_right[1] * 5;
220 } else {
221 snd->synth_output_left = 0;
222 snd->synth_output_right = 0;
223 }
224 } else {
225 snd->synth_output_left =
226 snd->ch_output_left[0] +
227 snd->ch_output_left[1] +
228 snd->ch_output_left[2] +
229 snd->ch_output_left[3];
230
231 snd->synth_output_right =
232 snd->ch_output_right[0] +
233 snd->ch_output_right[1] +
234 snd->ch_output_right[2] +
235 snd->ch_output_right[3];
236 }
237
238 // Mix speaker output
239 snd->synth_output_mono = (snd->synth_output_left & 0x3FF) + (snd->synth_output_right & 0x3FF);
240 if (snd->out_ctrl & SND_OUT_SPEAKER_ENABLE) {
241 snd->output_speaker = (snd->synth_output_mono >> ((snd->out_ctrl >> 1) & 3)) & 0xFF;
242 } else {
243 snd->output_speaker = 0;
244 }
245
246 // Mix headphone output
247 if (snd->out_ctrl & SND_OUT_HEADPHONES_ENABLE) {
248 snd->output_left = snd->synth_output_left << 5;
249 snd->output_right = snd->synth_output_right << 5;
250 if (snd->hyper_ctrl & HYPERV_ENABLE) {
251 snd->output_left += snd->hyper_output_left;
252 snd->output_right += snd->hyper_output_right;
253 }
254 } else {
255 snd->output_left = 0;
256 snd->output_right = 0;
257 }
258 }