all repos — mgba @ 668c4f68b76a5d9d4ac5345201ef162078d56601

mGBA Game Boy Advance Emulator

src/gba/gba-audio.c (view raw)

  1#include "gba-audio.h"
  2
  3#include "gba.h"
  4#include "gba-io.h"
  5#include "gba-thread.h"
  6
  7#include <limits.h>
  8
  9const unsigned GBA_AUDIO_SAMPLES = 512;
 10const unsigned GBA_AUDIO_FIFO_SIZE = 8 * sizeof(int32_t);
 11#define SWEEP_CYCLES (GBA_ARM7TDMI_FREQUENCY / 128)
 12
 13static int32_t _updateSquareChannel(struct GBAAudioSquareControl* envelope, int duty);
 14static void _updateEnvelope(struct GBAAudioEnvelope* envelope);
 15static int _updateSweep(struct GBAAudioChannel1* ch);
 16static int32_t _updateChannel1(struct GBAAudioChannel1* ch);
 17static int32_t _updateChannel2(struct GBAAudioChannel2* ch);
 18static int32_t _updateChannel3(struct GBAAudioChannel3* ch);
 19static int32_t _updateChannel4(struct GBAAudioChannel4* ch);
 20static void _sample(struct GBAAudio* audio);
 21
 22void GBAAudioInit(struct GBAAudio* audio) {
 23	audio->nextEvent = 0;
 24	audio->nextCh1 = 0;
 25	audio->nextCh2 = 0;
 26	audio->nextCh3 = 0;
 27	audio->nextCh4 = 0;
 28	audio->ch1.sweep.time = 0;
 29	audio->ch1.envelope.nextStep = INT_MAX;
 30	audio->ch1.control.nextStep = 0;
 31	audio->ch1.control.endTime = 0;
 32	audio->ch1.nextSweep = INT_MAX;
 33	audio->ch1.sample = 0;
 34	audio->ch2.envelope.nextStep = INT_MAX;
 35	audio->ch2.control.nextStep = 0;
 36	audio->ch2.control.endTime = 0;
 37	audio->ch2.sample = 0;
 38	audio->ch3.bank.packed = 0;
 39	audio->ch3.control.endTime = 0;
 40	audio->ch3.sample = 0;
 41	audio->ch4.sample = 0;
 42	audio->ch4.envelope.nextStep = INT_MAX;
 43	audio->eventDiff = 0;
 44	audio->nextSample = 0;
 45	audio->sampleRate = 0x8000;
 46	audio->soundcntLo = 0;
 47	audio->soundcntHi = 0;
 48	audio->soundcntX = 0;
 49	audio->sampleInterval = GBA_ARM7TDMI_FREQUENCY / audio->sampleRate;
 50
 51	CircleBufferInit(&audio->left, GBA_AUDIO_SAMPLES * sizeof(int32_t));
 52	CircleBufferInit(&audio->right, GBA_AUDIO_SAMPLES * sizeof(int32_t));
 53	CircleBufferInit(&audio->chA.fifo, GBA_AUDIO_FIFO_SIZE);
 54	CircleBufferInit(&audio->chB.fifo, GBA_AUDIO_FIFO_SIZE);
 55}
 56
 57void GBAAudioDeinit(struct GBAAudio* audio) {
 58	CircleBufferDeinit(&audio->left);
 59	CircleBufferDeinit(&audio->right);
 60	CircleBufferDeinit(&audio->chA.fifo);
 61	CircleBufferDeinit(&audio->chB.fifo);
 62}
 63
 64int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles) {
 65	audio->nextEvent -= cycles;
 66	audio->eventDiff += cycles;
 67	while (audio->nextEvent <= 0) {
 68		audio->nextEvent = INT_MAX;
 69		if (audio->enable) {
 70			if (audio->playingCh1 && !audio->ch1.envelope.dead) {
 71				audio->nextCh1 -= audio->eventDiff;
 72				if (audio->ch1.envelope.nextStep != INT_MAX) {
 73					audio->ch1.envelope.nextStep -= audio->eventDiff;
 74					if (audio->ch1.envelope.nextStep <= 0) {
 75						int8_t sample = audio->ch1.control.hi * 0x10 - 0x8;
 76						_updateEnvelope(&audio->ch1.envelope);
 77						if (audio->ch1.envelope.nextStep < audio->nextEvent) {
 78							audio->nextEvent = audio->ch1.envelope.nextStep;
 79						}
 80						audio->ch1.sample = sample * audio->ch1.envelope.currentVolume;
 81					}
 82				}
 83
 84				if (audio->ch1.nextSweep != INT_MAX) {
 85					audio->ch1.nextSweep -= audio->eventDiff;
 86					if (audio->ch1.nextSweep <= 0) {
 87						audio->playingCh1 = _updateSweep(&audio->ch1);
 88						if (audio->ch1.nextSweep < audio->nextEvent) {
 89							audio->nextEvent = audio->ch1.nextSweep;
 90						}
 91					}
 92				}
 93
 94				if (audio->nextCh1 <= 0) {
 95					audio->nextCh1 += _updateChannel1(&audio->ch1);
 96					if (audio->nextCh1 < audio->nextEvent) {
 97						audio->nextEvent = audio->nextCh1;
 98					}
 99				}
100
101				if (audio->ch1.control.stop) {
102					audio->ch1.control.endTime -= audio->eventDiff;
103					if (audio->ch1.control.endTime <= 0) {
104						audio->playingCh1 = 0;
105					}
106				}
107			}
108
109			if (audio->playingCh2 && !audio->ch2.envelope.dead) {
110				audio->nextCh2 -= audio->eventDiff;
111				if (audio->ch2.envelope.nextStep != INT_MAX) {
112					audio->ch2.envelope.nextStep -= audio->eventDiff;
113					if (audio->ch2.envelope.nextStep <= 0) {
114						int8_t sample = audio->ch2.control.hi * 0x10 - 0x8;
115						_updateEnvelope(&audio->ch2.envelope);
116						if (audio->ch2.envelope.nextStep < audio->nextEvent) {
117							audio->nextEvent = audio->ch2.envelope.nextStep;
118						}
119						audio->ch2.sample = sample * audio->ch2.envelope.currentVolume;
120					}
121				}
122
123				if (audio->nextCh2 <= 0) {
124					audio->nextCh2 += _updateChannel2(&audio->ch2);
125					if (audio->nextCh2 < audio->nextEvent) {
126						audio->nextEvent = audio->nextCh2;
127					}
128				}
129
130				if (audio->ch2.control.stop) {
131					audio->ch2.control.endTime -= audio->eventDiff;
132					if (audio->ch2.control.endTime <= 0) {
133						audio->playingCh2 = 0;
134					}
135				}
136			}
137
138			if (audio->playingCh3) {
139				audio->nextCh3 -= audio->eventDiff;
140				if (audio->nextCh3 <= 0) {
141					audio->nextCh3 += _updateChannel3(&audio->ch3);
142					if (audio->nextCh3 < audio->nextEvent) {
143						audio->nextEvent = audio->nextCh3;
144					}
145				}
146
147				if (audio->ch3.control.stop) {
148					audio->ch3.control.endTime -= audio->eventDiff;
149					if (audio->ch3.control.endTime <= 0) {
150						audio->playingCh3 = 0;
151					}
152				}
153			}
154
155			if (audio->playingCh4 && !audio->ch4.envelope.dead) {
156				audio->nextCh4 -= audio->eventDiff;
157				if (audio->ch4.envelope.nextStep != INT_MAX) {
158					audio->ch4.envelope.nextStep -= audio->eventDiff;
159					if (audio->ch4.envelope.nextStep <= 0) {
160						int8_t sample = (audio->ch4.sample >> 31) * 0x8;
161						_updateEnvelope(&audio->ch4.envelope);
162						if (audio->ch4.envelope.nextStep < audio->nextEvent) {
163							audio->nextEvent = audio->ch4.envelope.nextStep;
164						}
165						audio->ch4.sample = sample * audio->ch4.envelope.currentVolume;
166					}
167				}
168
169				if (audio->nextCh4 <= 0) {
170					audio->nextCh4 += _updateChannel4(&audio->ch4);
171					if (audio->nextCh4 < audio->nextEvent) {
172						audio->nextEvent = audio->nextCh4;
173					}
174				}
175
176				if (audio->ch4.control.stop) {
177					audio->ch4.control.endTime -= audio->eventDiff;
178					if (audio->ch4.control.endTime <= 0) {
179						audio->playingCh4 = 0;
180					}
181				}
182			}
183		}
184
185		audio->nextSample -= audio->eventDiff;
186		if (audio->nextSample <= 0) {
187			_sample(audio);
188			audio->nextSample += audio->sampleInterval;
189		}
190
191		if (audio->nextSample < audio->nextEvent) {
192			audio->nextEvent = audio->nextSample;
193		}
194		audio->eventDiff = 0;
195	}
196	return audio->nextEvent;
197}
198
199void GBAAudioScheduleFifoDma(struct GBAAudio* audio, int number, struct GBADMA* info) {
200	switch (info->dest) {
201	case BASE_IO | REG_FIFO_A_LO:
202		audio->chA.dmaSource = number;
203		break;
204	case BASE_IO | REG_FIFO_B_LO:
205		audio->chB.dmaSource = number;
206		break;
207	default:
208		GBALog(audio->p, GBA_LOG_GAME_ERROR, "Invalid FIFO destination: 0x%08X", info->dest);
209		return;
210	}
211	info->dstControl = DMA_FIXED;
212}
213
214void GBAAudioWriteSOUND1CNT_LO(struct GBAAudio* audio, uint16_t value) {
215	audio->ch1.sweep.packed = value;
216	if (audio->ch1.sweep.time) {
217		audio->ch1.nextSweep = audio->ch1.sweep.time * SWEEP_CYCLES;
218	} else {
219		audio->ch1.nextSweep = INT_MAX;
220	}
221}
222
223void GBAAudioWriteSOUND1CNT_HI(struct GBAAudio* audio, uint16_t value) {
224	audio->ch1.envelope.packed = value;
225	audio->ch1.envelope.dead = 0;
226	if (audio->ch1.envelope.stepTime) {
227		audio->ch1.envelope.nextStep = 0;
228	} else {
229		audio->ch1.envelope.nextStep = INT_MAX;
230		if (audio->ch1.envelope.initialVolume == 0) {
231			audio->ch1.envelope.dead = 1;
232			audio->ch1.sample = 0;
233		}
234	}
235}
236
237void GBAAudioWriteSOUND1CNT_X(struct GBAAudio* audio, uint16_t value) {
238	audio->ch1.control.packed = value;
239	audio->ch1.control.endTime = (GBA_ARM7TDMI_FREQUENCY * (64 - audio->ch1.envelope.length)) >> 8;
240	if (audio->ch1.control.restart) {
241		if (audio->ch1.sweep.time) {
242			audio->ch1.nextSweep = audio->ch1.sweep.time * SWEEP_CYCLES;
243		} else {
244			audio->ch1.nextSweep = INT_MAX;
245		}
246		if (!audio->playingCh1) {
247			audio->nextCh1 = 0;
248		}
249		audio->playingCh1 = 1;
250		if (audio->ch1.envelope.stepTime) {
251			audio->ch1.envelope.nextStep = 0;
252		} else {
253			audio->ch1.envelope.nextStep = INT_MAX;
254		}
255		audio->ch1.envelope.currentVolume = audio->ch1.envelope.initialVolume;
256		if (audio->ch1.envelope.stepTime) {
257			audio->ch1.envelope.nextStep = 0;
258		} else {
259			audio->ch1.envelope.nextStep = INT_MAX;
260		}
261	}
262}
263
264void GBAAudioWriteSOUND2CNT_LO(struct GBAAudio* audio, uint16_t value) {
265	audio->ch2.envelope.packed = value;
266	audio->ch2.envelope.dead = 0;
267	if (audio->ch2.envelope.stepTime) {
268		audio->ch2.envelope.nextStep = 0;
269	} else {
270		audio->ch2.envelope.nextStep = INT_MAX;
271		if (audio->ch2.envelope.initialVolume == 0) {
272			audio->ch2.envelope.dead = 1;
273			audio->ch2.sample = 0;
274		}
275	}
276}
277
278void GBAAudioWriteSOUND2CNT_HI(struct GBAAudio* audio, uint16_t value) {
279	audio->ch2.control.packed = value;
280	audio->ch1.control.endTime = (GBA_ARM7TDMI_FREQUENCY * (64 - audio->ch2.envelope.length)) >> 8;
281	if (audio->ch2.control.restart) {
282		audio->playingCh2 = 1;
283		audio->ch2.envelope.currentVolume = audio->ch2.envelope.initialVolume;
284		if (audio->ch2.envelope.stepTime) {
285			audio->ch2.envelope.nextStep = 0;
286		} else {
287			audio->ch2.envelope.nextStep = INT_MAX;
288		}
289		audio->nextCh2 = 0;
290	}
291}
292
293void GBAAudioWriteSOUND3CNT_LO(struct GBAAudio* audio, uint16_t value) {
294	audio->ch3.bank.packed = value;
295	if (audio->ch3.control.endTime >= 0) {
296		audio->playingCh3 = audio->ch3.bank.enable;
297	}
298}
299
300void GBAAudioWriteSOUND3CNT_HI(struct GBAAudio* audio, uint16_t value) {
301	audio->ch3.wave.packed = value;
302}
303
304void GBAAudioWriteSOUND3CNT_X(struct GBAAudio* audio, uint16_t value) {
305	audio->ch3.control.packed = value;
306	audio->ch3.control.endTime = (GBA_ARM7TDMI_FREQUENCY * (256 - audio->ch3.wave.length)) >> 8;
307	if (audio->ch3.control.restart) {
308		audio->playingCh3 = audio->ch3.bank.enable;
309	}
310}
311
312void GBAAudioWriteSOUND4CNT_LO(struct GBAAudio* audio, uint16_t value) {
313	audio->ch4.envelope.packed = value;
314	audio->ch4.envelope.dead = 0;
315	if (audio->ch4.envelope.stepTime) {
316		audio->ch4.envelope.nextStep = 0;
317	} else {
318		audio->ch4.envelope.nextStep = INT_MAX;
319		if (audio->ch4.envelope.initialVolume == 0) {
320			audio->ch4.envelope.dead = 1;
321			audio->ch4.sample = 0;
322		}
323	}
324}
325
326void GBAAudioWriteSOUND4CNT_HI(struct GBAAudio* audio, uint16_t value) {
327	audio->ch4.control.packed = value;
328	audio->ch4.control.endTime = (GBA_ARM7TDMI_FREQUENCY * (64 - audio->ch4.envelope.length)) >> 8;
329	if (audio->ch4.control.restart) {
330		audio->playingCh4 = 1;
331		audio->ch4.envelope.currentVolume = audio->ch4.envelope.initialVolume;
332		if (audio->ch4.envelope.stepTime) {
333			audio->ch4.envelope.nextStep = 0;
334		} else {
335			audio->ch4.envelope.nextStep = INT_MAX;
336		}
337		if (audio->ch4.control.power) {
338			audio->ch4.lfsr = 0x40;
339		} else {
340			audio->ch4.lfsr = 0x4000;
341		}
342		audio->nextCh4 = 0;
343	}
344}
345
346void GBAAudioWriteSOUNDCNT_LO(struct GBAAudio* audio, uint16_t value) {
347	audio->soundcntLo = value;
348}
349
350void GBAAudioWriteSOUNDCNT_HI(struct GBAAudio* audio, uint16_t value) {
351	audio->soundcntHi = value;
352}
353
354void GBAAudioWriteSOUNDCNT_X(struct GBAAudio* audio, uint16_t value) {
355	audio->soundcntX = (value & 0x80) | (audio->soundcntX & 0x0F);
356}
357
358void GBAAudioWriteWaveRAM(struct GBAAudio* audio, int address, uint32_t value) {
359	audio->ch3.wavedata[address | (!audio->ch3.bank.bank * 4)] = value;
360}
361
362void GBAAudioWriteFIFO(struct GBAAudio* audio, int address, uint32_t value) {
363	struct CircleBuffer* fifo;
364	switch (address) {
365	case REG_FIFO_A_LO:
366		fifo = &audio->chA.fifo;
367		break;
368	case REG_FIFO_B_LO:
369		fifo = &audio->chB.fifo;
370		break;
371	default:
372		GBALog(audio->p, GBA_LOG_ERROR, "Bad FIFO write to address 0x%03x", address);
373		return;
374	}
375	while (!CircleBufferWrite32(fifo, value)) {
376		int32_t dummy;
377		CircleBufferRead32(fifo, &dummy);
378	}
379}
380
381void GBAAudioSampleFIFO(struct GBAAudio* audio, int fifoId, int32_t cycles) {
382	struct GBAAudioFIFO* channel;
383	if (fifoId == 0) {
384		channel = &audio->chA;
385	} else if (fifoId == 1) {
386		channel = &audio->chB;
387	} else {
388		GBALog(audio->p, GBA_LOG_ERROR, "Bad FIFO write to address 0x%03x", fifoId);
389		return;
390	}
391	if (CircleBufferSize(&channel->fifo) <= 4 * sizeof(int32_t)) {
392		struct GBADMA* dma = &audio->p->memory.dma[channel->dmaSource];
393		dma->nextCount = 4;
394		dma->nextEvent = 0;
395		GBAMemoryUpdateDMAs(&audio->p->memory, -cycles);
396	}
397	CircleBufferRead8(&channel->fifo, &channel->sample);
398}
399
400unsigned GBAAudioCopy(struct GBAAudio* audio, void* left, void* right, unsigned nSamples) {
401	GBASyncLockAudio(audio->p->sync);
402	unsigned read = 0;
403	if (left) {
404		unsigned readL = CircleBufferRead(&audio->left, left, nSamples * sizeof(int32_t)) >> 2;
405		if (readL < nSamples) {
406			memset((int32_t*) left + readL, 0, nSamples - readL);
407		}
408		read = readL;
409	}
410	if (right) {
411		unsigned readR = CircleBufferRead(&audio->right, right, nSamples * sizeof(int32_t)) >> 2;
412		if (readR < nSamples) {
413			memset((int32_t*) right + readR, 0, nSamples - readR);
414		}
415		read = read >= readR ? read : readR;
416	}
417	GBASyncConsumeAudio(audio->p->sync);
418	return read;
419}
420
421static int32_t _updateSquareChannel(struct GBAAudioSquareControl* control, int duty) {
422	control->hi = !control->hi;
423	int period = 16 * (2048 - control->frequency);
424	switch (duty) {
425	case 0:
426		return control->hi ? period : period * 7;
427	case 1:
428		return control->hi ? period * 2 : period * 6;
429	case 2:
430		return period * 4;
431	case 3:
432		return control->hi ? period * 6 : period * 2;
433	default:
434		// This should never be hit
435		return period * 4;
436	}
437}
438
439static void _updateEnvelope(struct GBAAudioEnvelope* envelope) {
440	if (envelope->direction) {
441		++envelope->currentVolume;
442	} else {
443		--envelope->currentVolume;
444	}
445	if (envelope->currentVolume >= 15) {
446		envelope->currentVolume = 15;
447		envelope->nextStep = INT_MAX;
448	} else if (envelope->currentVolume <= 0) {
449		envelope->currentVolume = 0;
450		envelope->dead = 1;
451		envelope->nextStep = INT_MAX;
452	} else {
453		envelope->nextStep += envelope->stepTime * (GBA_ARM7TDMI_FREQUENCY >> 6);
454	}
455}
456
457static int _updateSweep(struct GBAAudioChannel1* ch) {
458	if (ch->sweep.direction) {
459		int frequency = ch->control.frequency;
460		frequency -= frequency >> ch->sweep.shift;
461		if (frequency >= 0) {
462			ch->control.frequency = frequency;
463		}
464	} else {
465		int frequency = ch->control.frequency;
466		frequency += frequency >> ch->sweep.shift;
467		if (frequency < 2048) {
468			ch->control.frequency = frequency;
469		} else {
470			return 0;
471		}
472	}
473	ch->nextSweep += ch->sweep.time * SWEEP_CYCLES;
474	return 1;
475}
476
477static int32_t _updateChannel1(struct GBAAudioChannel1* ch) {
478	int timing = _updateSquareChannel(&ch->control, ch->envelope.duty);
479	ch->sample = ch->control.hi * 0x10 - 0x8;
480	ch->sample *= ch->envelope.currentVolume;
481	return timing;
482}
483
484static int32_t _updateChannel2(struct GBAAudioChannel2* ch) {
485	int timing = _updateSquareChannel(&ch->control, ch->envelope.duty);
486	ch->sample = ch->control.hi * 0x10 - 0x8;
487	ch->sample *= ch->envelope.currentVolume;
488	return timing;
489}
490
491static int32_t _updateChannel3(struct GBAAudioChannel3* ch) {
492	int i;
493	int start;
494	int end;
495	int volume;
496	switch (ch->wave.volume) {
497	case 0:
498		volume = 0;
499		break;
500	case 1:
501		volume = 4;
502		break;
503	case 2:
504		volume = 2;
505		break;
506	case 3:
507		volume = 1;
508		break;
509	default:
510		volume = 3;
511		break;
512	}
513	if (ch->bank.size) {
514		start = 7;
515		end = 0;
516	} else if (ch->bank.bank) {
517		start = 7;
518		end = 4;
519	} else {
520		start = 3;
521		end = 0;
522	}
523	uint32_t bitsCarry = ch->wavedata[end] & 0xF0000000;
524	uint32_t bits;
525	for (i = start; i >= end; --i) {
526		bits = ch->wavedata[i] & 0xF0000000;
527		ch->wavedata[i] <<= 4;
528		ch->wavedata[i] |= bitsCarry >> 28;
529		bitsCarry = bits;
530	}
531	ch->sample = ((bitsCarry >> 26) - 0x20) * volume;
532	return 8 * (2048 - ch->control.rate);
533}
534
535static int32_t _updateChannel4(struct GBAAudioChannel4* ch) {
536	int lsb = ch->lfsr & 1;
537	ch->sample = lsb * 0x10 - 0x8;
538	ch->sample *= ch->envelope.currentVolume;
539	ch->lfsr >>= 1;
540	ch->lfsr ^= (lsb * 0x60) << (ch->control.power ? 0 : 8);
541	int timing = ch->control.ratio ? 2 * ch->control.ratio : 1;
542	timing <<= ch->control.frequency;
543	timing *= 32;
544	return timing;
545}
546
547static void _sample(struct GBAAudio* audio) {
548	int32_t sampleLeft = 0;
549	int32_t sampleRight = 0;
550	int psgShift = 6 - audio->volume;
551
552	if (audio->ch1Left) {
553		sampleLeft += audio->ch1.sample;
554	}
555
556	if (audio->ch1Right) {
557		sampleRight += audio->ch1.sample;
558	}
559
560	if (audio->ch2Left) {
561		sampleLeft += audio->ch2.sample;
562	}
563
564	if (audio->ch2Right) {
565		sampleRight += audio->ch2.sample;
566	}
567
568	if (audio->ch3Left) {
569		sampleLeft += audio->ch3.sample;
570	}
571
572	if (audio->ch3Right) {
573		sampleRight += audio->ch3.sample;
574	}
575
576	if (audio->ch4Left) {
577		sampleLeft += audio->ch4.sample;
578	}
579
580	if (audio->ch4Right) {
581		sampleRight += audio->ch4.sample;
582	}
583
584	sampleLeft = (sampleLeft * (1 + audio->volumeLeft)) >> psgShift;
585	sampleRight = (sampleRight * (1 + audio->volumeRight)) >> psgShift;
586
587	if (audio->chALeft) {
588		sampleLeft += (audio->chA.sample << 2) >> !audio->volumeChA;
589	}
590
591	if (audio->chARight) {
592		sampleRight += (audio->chA.sample << 2) >> !audio->volumeChA;
593	}
594
595	if (audio->chBLeft) {
596		sampleLeft += (audio->chB.sample << 2) >> !audio->volumeChB;
597	}
598
599	if (audio->chBRight) {
600		sampleRight += (audio->chB.sample << 2) >> !audio->volumeChB;
601	}
602
603	GBASyncLockAudio(audio->p->sync);
604	CircleBufferWrite32(&audio->left, sampleLeft << 5);
605	CircleBufferWrite32(&audio->right, sampleRight << 5);
606	unsigned produced = CircleBufferSize(&audio->left);
607	GBASyncProduceAudio(audio->p->sync, produced >= GBA_AUDIO_SAMPLES * 3);
608}