Debian Codegraph @

src/pt2_paula.c

raw symbols SCIP
1 /* Simple Paula emulator (with BLEP synthesis by aciddose).
2 ** Limitation: The audio output frequency can't be below 31389Hz ( ceil(PAULA_PAL_CLK / 113.0) )
3 **
4 ** WARNING: These functions must not be called while paulaGenerateSamples() is running!
5 ** If so, lock the audio first so that you're sure it's not running.
6 */
7
8 #include <stdint.h>
9 #include <stdbool.h>
10 #include <string.h>
11 #include <math.h>
12 #include "pt2_header.h" // PI
13 #include "pt2_audio.h"
14 #include "pt2_paula.h"
15 #include "pt2_blep.h"
16 #include "pt2_rcfilters.h"
17
18 typedef struct voice_t
19 {
20 volatile bool active;
21
22 // internal registers
23 bool sampleJustStarted, nextSampleStage;
24 int8_t AUD_DAT[2]; // DMA data buffer
25 const int8_t *location; // current location
26 uint16_t lengthCounter; // current length
27 int32_t sampleCounter; // how many bytes left in AUD_DAT
28 float fSample; // currently held sample point (multiplied by volume)
29 float fDelta, fPhase;
30 float fBlepDelta, fBlepPhase;
31
32 // registers modified by Paula functions
33 const int8_t *storedLocation; // data pointer
34 uint16_t storedLength;
35 float fStoredVol, fStoredDelta;
36 } paulaVoice_t;
37
38 static bool useLEDFilter, useLowpassFilter, useHighpassFilter;
39 static int8_t nullSample[0xFFFF*2]; // buffer for NULL data pointer
40 static float fPeriodToDeltaDiv;
41 static double dPaulaOutputFreq;
42 static blep_t blep[PAULA_VOICES];
43 static onePoleFilter_t filterLo, filterHi;
44 static twoPoleFilter_t filterLED;
45 static paulaVoice_t paula[PAULA_VOICES];
46
47 void paulaSetup(double dOutputFreq, uint32_t amigaModel)
48 {
49 ASSERT(dOutputFreq != 0.0);
50 dPaulaOutputFreq = dOutputFreq;
51 fPeriodToDeltaDiv = (float)(PAULA_PAL_CLK / dPaulaOutputFreq);
52
53 clearBlepState();
54
55 useLowpassFilter = useHighpassFilter = true;
56 clearOnePoleFilterState(&filterLo);
57 clearOnePoleFilterState(&filterHi);
58 clearTwoPoleFilterState(&filterLED);
59
60 /*
61 ** Amiga 500/1200 filters
62 **
63 ** RC values for Amiga 500 (rev 6A):
64 ** - 1-pole (6dB/oct) RC low-pass: R=360 ohm, C=0.1uF
65 ** - 2-pole (12dB/oct) Sallen-Key low-pass ("LED"): R1/R2=10k ohm, C1=6800pF, C2=3900pF
66 ** - 1-pole (6dB/oct) RC high-pass: R=1390 ohm (1000+390), C=22.33uF (22+0.33)
67 **
68 ** RC values for Amiga 1200 (rev 1D4):
69 ** - 1-pole (6dB/oct) RC low-pass: R=680 ohm, C=6800pF
70 ** - 2-pole (12dB/oct) Sallen-Key low-pass ("LED"): R1/R2=10k ohm, C1=6800pF, C2=3900pF
71 ** - 1-pole (6dB/oct) RC high-pass: R=1360 ohm (1000+360), C=22uF
72 */
73 double R, C, R1, R2, C1, C2, cutoff, qfactor;
74
75 if (amigaModel == MODEL_A1200)
76 {
77 // Amiga 1200 rev 1D4
78
79 /* Don't handle the A1200 low-pass filter since its cutoff
80 ** is well above human hearable range anyway (~34.4kHz).
81 ** We don't do volume PWM, so we have nothing we need to
82 ** filter away.
83 */
84 useLowpassFilter = false;
85
86 // A1200 1-pole (6dB/oct) RC high-pass filter:
87 R = 1360.0; // R324 (1K ohm resistor) + R325 (360 ohm resistor)
88 C = 2.2e-5; // C334 (22uF capacitor)
89 cutoff = 1.0 / ((2.0 * PI) * R * C); // ~5.319Hz
90 setupOnePoleFilter(dPaulaOutputFreq, cutoff, &filterHi);
91 }
92 else
93 {
94 // Amiga 500 rev 6A
95
96 // A500 1-pole (6dB/oct) RC low-pass filter:
97 R = 360.0; // R321 (360 ohm)
98 C = 1e-7; // C321 (0.1uF)
99 cutoff = 1.0 / ((2.0 * PI) * R * C); // ~4420.971Hz
100 setupOnePoleFilter(dPaulaOutputFreq, cutoff, &filterLo);
101
102 // A500 1-pole (6dB/oct) RC high-pass filter:
103 R = 1390.0; // R324 (1K ohm) + R325 (390 ohm)
104 C = 2.233e-5; // C334 (22uF) + C335 (0.33uF)
105 cutoff = 1.0 / ((2.0 * PI) * R * C); // ~5.128Hz
106 setupOnePoleFilter(dPaulaOutputFreq, cutoff, &filterHi);
107 }
108
109 // 2-pole (12dB/oct) Sallen-Key low-pass filter ("LED" filter, same values on A500/A1200):
110 R1 = 10000.0; // R322 (10K ohm)
111 R2 = 10000.0; // R323 (10K ohm)
112 C1 = 6.8e-9; // C322 (6800pF)
113 C2 = 3.9e-9; // C323 (3900pF)
114 cutoff = 1.0 / ((2.0 * PI) * sqrt(R1 * R2 * C1 * C2)); // ~3090.533Hz
115 qfactor = sqrt(R1 * R2 * C1 * C2) / (C2 * (R1 + R2)); // ~0.660225
116 setupTwoPoleFilter(dPaulaOutputFreq, cutoff, qfactor, &filterLED);
117 }
118
119 void paulaDisableFilters(void) // disables low-pass/high-pass filter ("LED" filter is kept)
120 {
121 useHighpassFilter = false;
122 useLowpassFilter = false;
123 }
124
125 int8_t *paulaGetNullSamplePtr(void)
126 {
127 return nullSample;
128 }
129
130 static inline void refetchPeriod(paulaVoice_t *v) // Paula stage
131 {
132 v->fBlepPhase = v->fPhase;
133 v->fBlepDelta = v->fDelta;
134
135 // Paula only updates period (delta) during period refetching (this stage)
136 v->fDelta = v->fStoredDelta;
137
138 v->nextSampleStage = true;
139 }
140
141 static inline void nextSample(paulaVoice_t *v, blep_t *b) // Paula stage
142 {
143 if (v->sampleCounter == 0)
144 {
145 // it's time to read new samples from DMA
146
147 // don't update AUD_LEN/AUD_LC yet on DMA trigger
148 if (!v->sampleJustStarted)
149 {
150 if (--v->lengthCounter == 0)
151 {
152 v->lengthCounter = v->storedLength;
153 v->location = v->storedLocation;
154 }
155 }
156
157 v->sampleJustStarted = false;
158
159 // fill DMA data buffer
160 v->AUD_DAT[0] = *v->location++;
161 v->AUD_DAT[1] = *v->location++;
162 v->sampleCounter = 2;
163 }
164
165 /* Pre-compute current sample point.
166 ** Output volume is only read from AUDxVOL at this stage,
167 ** and we don't emulate volume PWM anyway, so we can
168 ** pre-multiply by volume here.
169 */
170 v->fSample = v->AUD_DAT[0] * v->fStoredVol; // -128..127 * 0.0f .. 1.0f
171
172 // fill BLEP buffer if the new sample differs from the old one
173 if (v->fSample != b->fLastValue)
174 {
175 if (v->fBlepDelta > v->fBlepPhase) // also checks if v->fBlepDelta > 0.0f
176 {
177 const float fBlepOffset = v->fBlepPhase / v->fBlepDelta;
178 blepAdd(b, fBlepOffset, b->fLastValue - v->fSample);
179 }
180
181 b->fLastValue = v->fSample;
182 }
183
184 // progress AUD_DAT buffer
185 v->AUD_DAT[0] = v->AUD_DAT[1];
186 v->sampleCounter--;
187 }
188
189 static void audxper(int32_t ch, uint16_t period)
190 {
191 paulaVoice_t *v = &paula[ch];
192
193 int32_t realPeriod = period;
194 if (realPeriod == 0)
195 realPeriod = 65536; // On Amiga: period 0 = period 65536 (1+65535)
196 else if (realPeriod < 113)
197 realPeriod = 113; // close to what happens on real Amiga (and low-limit needed for BLEP synthesis)
198
199 // to be read on next sampling step (or on DMA trigger)
200 v->fStoredDelta = fPeriodToDeltaDiv / (float)realPeriod;
201
202 // BLEP synthesis edge-case
203 if (v->fBlepDelta == 0.0f)
204 v->fBlepDelta = v->fDelta;
205 }
206
207 static void audxvol(int32_t ch, uint16_t vol)
208 {
209 int32_t realVol = vol & 127;
210 if (realVol > 64)
211 realVol = 64;
212
213 // multiplying sample point by this also scales the sample from -128..127 -> -1.000 .. ~0.992
214 paula[ch].fStoredVol = realVol * (1.0f / (128.0f * 64.0f));
215 }
216
217 static void audxlen(int32_t ch, uint16_t len)
218 {
219 paula[ch].storedLength = len;
220 }
221
222 static void audxdat(int32_t ch, const int8_t *src)
223 {
224 if (src == NULL)
225 src = nullSample;
226
227 paula[ch].storedLocation = src;
228 }
229
230 static void startDMA(int32_t ch)
231 {
232 paulaVoice_t *v = &paula[ch];
233
234 if (v->storedLocation == NULL)
235 v->storedLocation = nullSample;
236
237 // immediately update these
238 v->location = v->storedLocation;
239 v->lengthCounter = v->storedLength;
240
241 // make Paula fetch new samples immediately
242 v->sampleCounter = 0;
243 v->sampleJustStarted = true;
244 refetchPeriod(v);
245
246 // kludge: must be cleared *after* refetchPeriod()
247 v->fPhase = 0.0f;
248
249 v->active = true;
250 }
251
252 static void stopDMA(int32_t ch)
253 {
254 paula[ch].active = false;
255 }
256
257 void paulaWriteByte(uint32_t address, uint8_t data8)
258 {
259 if (address == 0)
260 return;
261
262 switch (address)
263 {
264 // CIA-A ("LED" filter control only)
265 case 0xBFE001:
266 {
267 const bool oldLedFilterState = useLEDFilter;
268
269 useLEDFilter = !!(data8 & 2);
270 if (useLEDFilter != oldLedFilterState)
271 clearTwoPoleFilterState(&filterLED);
272 }
273 break;
274
275 default:
276 return;
277 }
278 }
279
280 void paulaWriteWord(uint32_t address, uint16_t data16)
281 {
282 if (address == 0)
283 return;
284
285 switch (address)
286 {
287 // DMACON
288 case 0xDFF096:
289 {
290 if (data16 & 0x8000)
291 {
292 // set
293 if (data16 & 1) startDMA(0);
294 if (data16 & 2) startDMA(1);
295 if (data16 & 4) startDMA(2);
296 if (data16 & 8) startDMA(3);
297 }
298 else
299 {
300 // clear
301 if (data16 & 1) stopDMA(0);
302 if (data16 & 2) stopDMA(1);
303 if (data16 & 4) stopDMA(2);
304 if (data16 & 8) stopDMA(3);
305 }
306 }
307 break;
308
309 // AUDxLEN
310 case 0xDFF0A4: audxlen(0, data16); break;
311 case 0xDFF0B4: audxlen(1, data16); break;
312 case 0xDFF0C4: audxlen(2, data16); break;
313 case 0xDFF0D4: audxlen(3, data16); break;
314
315 // AUDxPER
316 case 0xDFF0A6: audxper(0, data16); break;
317 case 0xDFF0B6: audxper(1, data16); break;
318 case 0xDFF0C6: audxper(2, data16); break;
319 case 0xDFF0D6: audxper(3, data16); break;
320
321 // AUDxVOL
322 case 0xDFF0A8: audxvol(0, data16); break;
323 case 0xDFF0B8: audxvol(1, data16); break;
324 case 0xDFF0C8: audxvol(2, data16); break;
325 case 0xDFF0D8: audxvol(3, data16); break;
326
327 default:
328 return;
329 }
330 }
331
332 void paulaWritePtr(uint32_t address, const int8_t *ptr)
333 {
334 if (address == 0)
335 return;
336
337 switch (address)
338 {
339 // AUDxDAT
340 case 0xDFF0A0: audxdat(0, ptr); break;
341 case 0xDFF0B0: audxdat(1, ptr); break;
342 case 0xDFF0C0: audxdat(2, ptr); break;
343 case 0xDFF0D0: audxdat(3, ptr); break;
344
345 default:
346 return;
347 }
348 }
349
350 void clearBlepState(void)
351 {
352 memset(blep, 0, sizeof (blep));
353 }
354
355 // output is -4.00 .. 3.97 (can be louder because of high-pass filter)
356 void paulaGenerateSamples(float *fOutL, float *fOutR, int32_t numSamples)
357 {
358 if (numSamples <= 0)
359 return;
360
361 float *fMixBufSelect[PAULA_VOICES] = { fOutL, fOutR, fOutR, fOutL };
362
363 // clear mix buffer block
364 memset(fOutL, 0, numSamples * sizeof (float));
365 memset(fOutR, 0, numSamples * sizeof (float));
366
367 // mix samples
368
369 paulaVoice_t *v = paula;
370 blep_t *b = blep;
371
372 for (int32_t i = 0; i < PAULA_VOICES; i++, v++, b++)
373 {
374 if (!v->active || v->location == NULL || v->storedLocation == NULL)
375 continue;
376
377 float *fMixBuffer = fMixBufSelect[i]; // what output channel to mix into (L, R, R, L)
378 for (int32_t j = 0; j < numSamples; j++)
379 {
380 if (v->nextSampleStage)
381 {
382 v->nextSampleStage = false;
383 nextSample(v, b);
384 }
385
386 float fSample = v->fSample; // current sample, pre-multiplied by vol, scaled to -1.0 .. 0.992
387 if (b->samplesLeft > 0)
388 fSample = blepRun(b, fSample);
389
390 fMixBuffer[j] += fSample;
391
392 v->fPhase += v->fDelta;
393 if (v->fPhase >= 1.0f)
394 {
395 v->fPhase -= 1.0f;
396 refetchPeriod(v);
397 }
398 }
399 }
400
401 // apply Amiga filters
402 for (int32_t i = 0; i < numSamples; i++)
403 {
404 float fOut[2];
405
406 fOut[0] = fOutL[i];
407 fOut[1] = fOutR[i];
408
409 if (useLowpassFilter)
410 onePoleLPFilterStereo(&filterLo, fOut, fOut);
411
412 if (useLEDFilter)
413 twoPoleLPFilterStereo(&filterLED, fOut, fOut);
414
415 if (useHighpassFilter)
416 onePoleHPFilterStereo(&filterHi, fOut, fOut);
417
418 fOutL[i] = fOut[0];
419 fOutR[i] = fOut[1];
420 }
421 }