Debian Codegraph @

src/engine/platform/sound/vic20sound.c

raw
1 /*
2 * vic20sound.c - Implementation of VIC20 sound code.
3 *
4 * Written by
5 * Rami Rasanen <[email protected]>
6 * Ville-Matias Heikkila <[email protected]>
7 *
8 * This file is part of VICE, the Versatile Commodore Emulator.
9 * See README for copyright notice.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 * 02111-1307 USA.
25 *
26 */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "vic20sound.h"
33
34 /* ---------------------------------------------------------------------*/
35
36 static float voltagefunction[] = {
37 0.00f, 148.28f, 296.55f, 735.97f, 914.88f, 1126.89f, 1321.86f, 1503.07f, 1603.50f,
38 1758.00f, 1913.98f, 2070.94f, 2220.36f, 2342.91f, 2488.07f, 3188.98f, 3285.76f, 3382.53f,
39 3479.31f, 3576.08f, 3672.86f, 3769.63f, 3866.41f, 3963.18f, 4059.96f, 4248.10f, 4436.24f,
40 4624.38f, 4812.53f, 5000.67f, 5188.81f, 5192.91f, 5197.00f, 5338.52f, 5480.04f, 5621.56f,
41 5763.07f, 5904.59f, 6046.11f, 6187.62f, 6329.14f, 6609.31f, 6889.47f, 7169.64f, 7449.80f,
42 7729.97f, 7809.36f, 7888.75f, 7968.13f, 8047.52f, 8126.91f, 8206.30f, 8285.69f, 8365.07f,
43 8444.46f, 8523.85f, 8603.24f, 8905.93f, 9208.63f, 9511.32f, 9814.02f, 9832.86f, 9851.70f,
44 9870.54f, 9889.38f, 9908.22f, 9927.07f, 9945.91f, 9964.75f, 9983.59f, 10002.43f, 10021.27f,
45 10040.12f, 10787.23f, 11534.34f, 12281.45f, 12284.98f, 12288.50f, 12292.03f, 12295.56f, 12299.09f,
46 12302.62f, 12306.15f, 12309.68f, 12313.21f, 12316.74f, 12320.26f, 12323.79f, 12327.32f, 13113.05f,
47 13898.78f, 13910.58f, 13922.39f, 13934.19f, 13945.99f, 13957.80f, 13969.60f, 13981.40f, 13993.21f,
48 14005.01f, 14016.81f, 14028.62f, 14040.42f, 14052.22f, 14064.03f, 16926.31f, 16987.04f, 17047.77f,
49 17108.50f, 17169.23f, 17229.96f, 17290.69f, 17351.42f, 17412.15f, 17472.88f, 17533.61f, 17594.34f,
50 17655.07f, 17715.80f, 17776.53f, 17837.26f, 18041.51f, 18245.77f, 18450.02f, 18654.28f, 18858.53f,
51 19062.78f, 19267.04f, 19471.29f, 19675.55f, 19879.80f, 20084.05f, 20288.31f, 20417.74f, 20547.17f,
52 20676.61f, 20774.26f, 20871.91f, 20969.55f, 21067.20f, 21164.85f, 21262.50f, 21360.15f, 21457.80f,
53 21555.45f, 21653.09f, 21750.74f, 21848.39f, 21946.04f, 22043.69f, 22141.34f, 22212.33f, 22283.33f,
54 22354.33f, 22425.33f, 22496.32f, 22567.32f, 22638.32f, 22709.32f, 22780.31f, 22851.31f, 22922.31f,
55 22993.31f, 23064.30f, 23135.30f, 23206.30f, 23255.45f, 23304.60f, 23353.75f, 23402.91f, 23452.06f,
56 23501.21f, 23550.36f, 23599.51f, 23648.67f, 23768.81f, 23888.96f, 24009.11f, 24129.26f, 24249.41f,
57 24369.56f, 24451.92f, 24534.28f, 24616.63f, 24698.99f, 24781.35f, 24863.70f, 24946.06f, 25028.42f,
58 25110.77f, 25193.13f, 25275.49f, 25357.84f, 25440.20f, 25522.56f, 25604.92f, 25658.87f, 25712.83f,
59 25766.79f, 25820.75f, 25874.71f, 25928.66f, 25982.62f, 26036.58f, 26090.54f, 26144.49f, 26198.45f,
60 26252.41f, 26306.37f, 26360.33f, 26414.28f, 26501.23f, 26588.17f, 26675.12f, 26762.06f, 26849.01f,
61 26935.95f, 27022.90f, 27109.84f, 27196.78f, 27283.73f, 27370.67f, 27457.62f, 27544.56f, 27631.51f,
62 27718.45f, 27726.89f, 27735.33f, 27743.78f, 27752.22f, 27760.66f, 27769.10f, 27777.54f, 27785.98f,
63 27794.43f, 27802.87f, 27811.31f, 27819.75f, 27828.19f, 27836.63f, 27845.08f, 27853.52f, 27861.96f,
64 27870.40f, 27878.84f, 27887.28f, 27895.73f, 27904.17f, 27912.61f, 27921.05f, 27929.49f, 27937.93f,
65 27946.38f, 27954.82f, 27963.26f, 27971.70f, 27980.14f, 27988.58f, 27997.03f, 28005.47f, 28013.91f,
66 28022.35f, 28030.79f, 28039.23f, 28047.68f, 28056.12f, 28064.56f, 28073.00f, 28081.44f, 28089.88f,
67 28098.33f, 28106.77f, 28115.21f, 28123.65f, 28132.09f, 28140.53f, 28148.98f, 28157.42f, 28165.86f,
68 28174.30f, 28182.74f, 28191.18f, 28199.63f, 28208.07f, 28216.51f, 28224.95f, 28233.39f, 28241.83f,
69 28250.28f, 28258.72f, 28267.16f, 28275.60f, 28284.04f, 28292.48f, 28300.93f, 28309.37f, 28317.81f,
70 28326.25f, 28334.69f, 28343.13f, 28351.58f, 28360.02f, 28368.46f, 28376.90f, 28385.34f, 28393.78f,
71 28402.23f, 28410.67f, 28419.11f, 28427.55f, 28435.99f, 28444.43f, 28452.88f, 28461.32f, 28469.76f,
72 28478.20f, 28486.64f, 28495.08f, 28503.53f, 28511.97f, 28520.41f, 28528.85f, 28537.29f, 28545.73f,
73 28554.18f, 28562.62f, 28571.06f, 28579.50f, 28587.94f, 28596.38f, 28604.83f, 28613.27f, 28621.71f,
74 28630.15f, 28638.59f, 28647.03f, 28655.48f, 28663.92f, 28672.36f, 28680.80f, 28689.24f, 28697.68f,
75 28706.13f, 28714.57f, 28723.01f, 28731.45f, 28739.89f, 28748.33f, 28756.78f, 28765.22f, 28773.66f,
76 28782.10f, 28790.54f, 28798.98f, 28807.43f, 28815.87f, 28824.31f, 28832.75f, 28841.19f, 28849.63f,
77 28858.08f, 28866.52f, 28874.96f, 28883.40f, 28891.84f, 28900.28f, 28908.73f, 28917.17f, 28925.61f,
78 28934.05f, 28942.49f, 28950.93f, 28959.38f, 28967.82f, 28976.26f, 28984.70f, 28993.14f, 29001.58f,
79 29010.03f, 29018.47f, 29026.91f, 29035.35f, 29043.79f, 29052.23f, 29060.68f, 29069.12f, 29077.56f,
80 29086.00f, 29094.44f, 29102.88f, 29111.33f, 29119.77f, 29128.21f, 29136.65f, 29145.09f, 29153.53f,
81 29161.98f, 29170.42f, 29178.86f, 29187.30f, 29195.74f, 29204.18f, 29212.63f, 29221.07f, 29229.51f,
82 29237.95f, 29246.39f, 29254.83f, 29263.28f, 29271.72f, 29280.16f, 29288.60f, 29297.04f, 29305.48f,
83 29313.93f, 29322.37f, 29330.81f, 29339.25f, 29347.69f, 29356.13f, 29364.58f, 29373.02f, 29381.46f,
84 29389.90f, 29398.34f, 29406.78f, 29415.23f, 29423.67f, 29432.11f, 29440.55f, 29448.99f, 29457.43f,
85 29465.88f, 29474.32f, 29482.76f, 29491.20f
86 };
87
88 void vic_sound_clock(sound_vic20_t *snd, uint32_t cycles);
89
90 int vic_sound_machine_calculate_samples(sound_vic20_t *snd, int16_t *pbuf, int nr, int soc, int scc, uint32_t delta_t)
91 {
92 int s = 0;
93 int i;
94 float o;
95 float v;
96 int16_t vicbuf;
97 int samples_to_do;
98
99 while (s < nr && delta_t >= snd->cycles_per_sample - snd->leftover_cycles) {
100 samples_to_do = (int)(snd->cycles_per_sample - snd->leftover_cycles);
101 snd->leftover_cycles += samples_to_do - snd->cycles_per_sample;
102 vic_sound_clock(snd, samples_to_do);
103
104 v = voltagefunction[(((snd->accum * 7) / snd->accum_cycles) + 1) * snd->volume];
105 if (snd->filter_off) {
106 o = v;
107 } else {
108 o = snd->lowpassbuf - snd->highpassbuf;
109 snd->highpassbuf += snd->highpassbeta * (snd->lowpassbuf - snd->highpassbuf);
110 snd->lowpassbuf += snd->lowpassbeta * (v - snd->lowpassbuf);
111 }
112
113 if (o < -32768) {
114 vicbuf = -32768;
115 } else if (o > 32767) {
116 vicbuf = 32767;
117 } else {
118 vicbuf = (int16_t)o;
119 }
120
121 for (i = 0; i < soc; i++) {
122 pbuf[(s * soc) + i] = vicbuf;
123 }
124 s++;
125 snd->accum = 0;
126 snd->accum_cycles = 0;
127 delta_t -= samples_to_do;
128 }
129 if (delta_t > 0) {
130 snd->leftover_cycles += delta_t;
131 vic_sound_clock(snd, delta_t);
132 delta_t = 0;
133 }
134 return s;
135 }
136
137 void vic_sound_clock(sound_vic20_t *snd, uint32_t cycles)
138 {
139 uint32_t i;
140 int j, enabled;
141
142 if (cycles <= 0) {
143 return;
144 }
145
146 for (j = 0; j < 4; j++) {
147 int chspeed = "\4\3\2\1"[j];
148
149 if (snd->ch[j].ctr > cycles) {
150 snd->accum += snd->ch[j].out * cycles;
151 snd->ch[j].ctr -= cycles;
152 } else {
153 for (i = cycles; i; i--) {
154 snd->ch[j].ctr--;
155 if (snd->ch[j].ctr <= 0) {
156 int a = (~snd->ch[j].reg) & 127;
157 int edge_trigger;
158 a = a ? a : 128;
159 snd->ch[j].ctr += a << chspeed;
160 enabled = (snd->ch[j].reg & 128) >> 7;
161 edge_trigger = (snd->noise_LFSR & 1) & !snd->noise_LFSR0_old;
162
163 if((j != 3) || ((j == 3) && edge_trigger)) {
164 uint8_t shift = snd->ch[j].shift;
165 shift = ((shift << 1) | (((((shift & 128) >> 7)) ^ 1) & enabled));
166 snd->ch[j].shift = shift;
167 }
168 if(j == 3) {
169 int bit3 = (snd->noise_LFSR >> 3) & 1;
170 int bit12 = (snd->noise_LFSR >> 12) & 1;
171 int bit14 = (snd->noise_LFSR >> 14) & 1;
172 int bit15 = (snd->noise_LFSR >> 15) & 1;
173 int gate1 = bit3 ^ bit12;
174 int gate2 = bit14 ^ bit15;
175 int gate3 = (gate1 ^ gate2) ^ 1;
176 int gate4 = (gate3 & enabled) ^ 1;
177 snd->noise_LFSR0_old = snd->noise_LFSR & 1;
178 snd->noise_LFSR = (snd->noise_LFSR << 1) | gate4;
179 }
180 snd->ch[j].out = snd->ch[j].shift & (j == 3 ? enabled : 1);
181 }
182 snd->accum += snd->ch[j].out; /* FIXME: doesn't take DC offset into account */
183 }
184 }
185 }
186
187 snd->accum_cycles += cycles;
188 }
189
190 void vic_sound_machine_store(sound_vic20_t *snd, uint16_t addr, uint8_t value)
191 {
192 switch (addr) {
193 case 0xA:
194 snd->ch[0].reg = value;
195 break;
196 case 0xB:
197 snd->ch[1].reg = value;
198 break;
199 case 0xC:
200 snd->ch[2].reg = value;
201 break;
202 case 0xD:
203 snd->ch[3].reg = value;
204 break;
205 case 0xE:
206 snd->volume = value & 0x0f;
207 break;
208 }
209 }
210
211 int vic_sound_machine_init(sound_vic20_t *snd, int speed, int cycles_per_sec, bool filter_off)
212 {
213 uint32_t i;
214 float dt;
215
216 memset(snd, 0, sizeof(sound_vic20_t));
217
218 snd->cycles_per_sample = (float)cycles_per_sec / speed;
219 snd->leftover_cycles = 0.0f;
220
221 snd->lowpassbuf = 0.0f;
222 snd->highpassbuf = 0.0f;
223 snd->filter_off = filter_off;
224
225 snd->speed = speed;
226
227 dt = 1.f / speed;
228
229 /*
230 Audio output stage
231
232 5V
233 -----+
234 audio| 1k |
235 +---+---R---+--------(K) +-----
236 out | | | | |audio
237 -----+ C .01 C .1 | 1 uF |
238 | uF | uF +-----C-----+ 1K
239 | |
240 GND GND R 470 | amp
241 | +-----
242
243 GND
244
245 */
246
247 /* Low-pass: R = 1 kOhm, C = 100 nF; RC = 1e3*1e-7 = 1e-4 (1591 Hz) */
248 snd->lowpassbeta = dt / ( dt + 1e-4f );
249 /* High-pass: R = 1 kOhm, C = 1 uF; RC = 1e3*1e-6 = 1e-3 ( 159 Hz) */
250 snd->highpassbeta = dt / ( dt + 1e-3f );
251
252 for (i = 0; i < 16; i++) {
253 vic_sound_machine_store(snd, (uint16_t)i, 0);
254 }
255
256 return 1;
257 }