all repos — mgba @ 74fc29fc68e7ae84f0c27130d1d98299434ce6f2

mGBA Game Boy Advance Emulator

GB Audio: Reduce redundant GB NR52 code
Jeffrey Pfau jeffrey@endrift.com
Thu, 11 Feb 2016 22:45:49 -0800
commit

74fc29fc68e7ae84f0c27130d1d98299434ce6f2

parent

b4af0f64e3e06bd760e516e8220cc98dce887611

4 files changed, 31 insertions(+), 57 deletions(-)

jump to
M src/gb/audio.csrc/gb/audio.c

@@ -27,7 +27,7 @@ static int32_t _updateChannel3(struct GBAudioChannel3* ch);

static int32_t _updateChannel4(struct GBAudioChannel4* ch); static void _sample(struct GBAudio* audio, int32_t cycles); -void GBAudioInit(struct GBAudio* audio, size_t samples) { +void GBAudioInit(struct GBAudio* audio, size_t samples, uint8_t* nr52) { audio->samples = samples; audio->left = blip_new(BLIP_BUFFER_SIZE); audio->right = blip_new(BLIP_BUFFER_SIZE);

@@ -40,6 +40,7 @@ audio->forceDisableCh[1] = false;

audio->forceDisableCh[2] = false; audio->forceDisableCh[3] = false; audio->masterVolume = GB_AUDIO_VOLUME_MAX; + audio->nr52 = nr52; } void GBAudioDeinit(struct GBAudio* audio) {

@@ -93,10 +94,7 @@ bool oldDirection = audio->ch1.direction;

audio->ch1.direction = GBAudioRegisterSquareSweepGetDirection(value); if (audio->ch1.sweepOccurred && oldDirection && !audio->ch1.direction) { audio->playingCh1 = false; - // TODO: Don't need p - if (audio->p) { - audio->p->memory.io[REG_NR52] &= ~0x0001; - } + *audio->nr52 &= ~0x0001; } audio->ch1.sweepOccurred = false; audio->ch1.time = GBAudioRegisterSquareSweepGetTime(value);

@@ -113,10 +111,7 @@

void GBAudioWriteNR12(struct GBAudio* audio, uint8_t value) { if (!_writeSweep(&audio->ch1.envelope, value)) { audio->playingCh1 = false; - // TODO: Don't need p - if (audio->p) { - audio->p->memory.io[REG_NR52] &= ~0x0001; - } + *audio->nr52 &= ~0x0001; } }

@@ -163,15 +158,12 @@ }

} audio->nextEvent = audio->eventDiff; if (audio->p) { - // TODO: Don't need + // TODO: Don't need p audio->p->cpu->nextEvent = audio->eventDiff; } } - // TODO: Don't need p - if (audio->p) { - audio->p->memory.io[REG_NR52] &= ~0x0001; - audio->p->memory.io[REG_NR52] |= audio->playingCh1; - } + *audio->nr52 &= ~0x0001; + *audio->nr52 |= audio->playingCh1; } void GBAudioWriteNR21(struct GBAudio* audio, uint8_t value) {

@@ -182,10 +174,7 @@

void GBAudioWriteNR22(struct GBAudio* audio, uint8_t value) { if (!_writeSweep(&audio->ch2.envelope, value)) { audio->playingCh2 = false; - // TODO: Don't need p - if (audio->p) { - audio->p->memory.io[REG_NR52] &= ~0x0002; - } + *audio->nr52 &= ~0x0002; } }

@@ -229,21 +218,15 @@ // TODO: Don't need p

audio->p->cpu->nextEvent = audio->eventDiff; } } - // TODO: Don't need p - if (audio->p) { - audio->p->memory.io[REG_NR52] &= ~0x0002; - audio->p->memory.io[REG_NR52] |= audio->playingCh2 << 1; - } + *audio->nr52 &= ~0x0002; + *audio->nr52 |= audio->playingCh2 << 1; } void GBAudioWriteNR30(struct GBAudio* audio, uint8_t value) { audio->ch3.enable = GBAudioRegisterBankGetEnable(value); if (!audio->ch3.enable) { audio->playingCh3 = false; - // TODO: Don't need p - if (audio->p) { - audio->p->memory.io[REG_NR52] &= ~0x0004; - } + *audio->nr52 &= ~0x0004; } }

@@ -292,11 +275,8 @@ // TODO: Don't need p

audio->p->cpu->nextEvent = audio->eventDiff; } } - // TODO: Don't need p - if (audio->p) { - audio->p->memory.io[REG_NR52] &= ~0x0004; - audio->p->memory.io[REG_NR52] |= audio->playingCh3 << 2; - } + *audio->nr52 &= ~0x0004; + *audio->nr52 |= audio->playingCh3 << 2; } void GBAudioWriteNR41(struct GBAudio* audio, uint8_t value) {

@@ -307,10 +287,7 @@

void GBAudioWriteNR42(struct GBAudio* audio, uint8_t value) { if (!_writeSweep(&audio->ch4.envelope, value)) { audio->playingCh4 = false; - // TODO: Don't need p - if (audio->p) { - audio->p->memory.io[REG_NR52] &= ~0x0008; - } + *audio->nr52 &= ~0x0008; } }

@@ -358,11 +335,8 @@ // TODO: Don't need p

audio->p->cpu->nextEvent = audio->eventDiff; } } - // TODO: Don't need p - if (audio->p) { - audio->p->memory.io[REG_NR52] &= ~0x0008; - audio->p->memory.io[REG_NR52] |= audio->playingCh4 << 3; - } + *audio->nr52 &= ~0x0008; + *audio->nr52 |= audio->playingCh4 << 3; } void GBAudioWriteNR50(struct GBAudio* audio, uint8_t value) {

@@ -430,8 +404,8 @@ audio->p->memory.io[REG_NR43] = 0;

audio->p->memory.io[REG_NR44] = 0; audio->p->memory.io[REG_NR50] = 0; audio->p->memory.io[REG_NR51] = 0; - audio->p->memory.io[REG_NR52] &= ~0x000F; } + *audio->nr52 &= ~0x000F; } else if (!wasEnable) { audio->frame = 7; }

@@ -572,12 +546,13 @@ }

} } + *audio->nr52 &= ~0x000F; + *audio->nr52 |= audio->playingCh1; + *audio->nr52 |= audio->playingCh2 << 1; + *audio->nr52 |= audio->playingCh3 << 2; + *audio->nr52 |= audio->playingCh4 << 3; + if (audio->p) { - audio->p->memory.io[REG_NR52] &= ~0x000F; - audio->p->memory.io[REG_NR52] |= audio->playingCh1; - audio->p->memory.io[REG_NR52] |= audio->playingCh2 << 1; - audio->p->memory.io[REG_NR52] |= audio->playingCh3 << 2; - audio->p->memory.io[REG_NR52] |= audio->playingCh4 << 3; audio->nextSample -= audio->eventDiff; if (audio->nextSample <= 0) { _sample(audio, audio->sampleInterval);
M src/gb/audio.hsrc/gb/audio.h

@@ -166,6 +166,7 @@ bool playingCh1;

bool playingCh2; bool playingCh3; bool playingCh4; + uint8_t* nr52; int32_t nextEvent; int32_t eventDiff;

@@ -186,7 +187,7 @@ bool forceDisableCh[4];

int masterVolume; }; -void GBAudioInit(struct GBAudio* audio, size_t samples); +void GBAudioInit(struct GBAudio* audio, size_t samples, uint8_t* nr52); void GBAudioDeinit(struct GBAudio* audio); void GBAudioReset(struct GBAudio* audio);
M src/gb/gb.csrc/gb/gb.c

@@ -45,7 +45,7 @@ gb->video.p = gb;

GBVideoInit(&gb->video); gb->audio.p = gb; - GBAudioInit(&gb->audio, 2048); // TODO: Remove magic constant + GBAudioInit(&gb->audio, 2048, &gb->memory.io[REG_NR52]); // TODO: Remove magic constant gb->timer.p = gb;
M src/gba/audio.csrc/gba/audio.c

@@ -22,7 +22,11 @@ static void _sample(struct GBAAudio* audio);

void GBAAudioInit(struct GBAAudio* audio, size_t samples) { audio->psg.p = NULL; - GBAudioInit(&audio->psg, 0); + uint8_t* nr52 = (uint8_t*) &audio->p->memory.io[REG_SOUNDCNT_X >> 1]; +#ifdef __BIG_ENDIAN__ + ++n52; +#endif + GBAudioInit(&audio->psg, 0, nr52); audio->samples = samples; audio->psg.clockRate = GBA_ARM7TDMI_FREQUENCY; // Guess too large; we hang producing extra samples if we guess too low

@@ -89,12 +93,6 @@ audio->nextEvent = GBAudioProcessEvents(&audio->psg, audio->eventDiff / 4);

if (audio->nextEvent != INT_MAX) { audio->nextEvent *= 4; } - - audio->p->memory.io[REG_SOUNDCNT_X >> 1] &= ~0x000F; - audio->p->memory.io[REG_SOUNDCNT_X >> 1] |= audio->psg.playingCh1; - audio->p->memory.io[REG_SOUNDCNT_X >> 1] |= audio->psg.playingCh2 << 1; - audio->p->memory.io[REG_SOUNDCNT_X >> 1] |= audio->psg.playingCh3 << 2; - audio->p->memory.io[REG_SOUNDCNT_X >> 1] |= audio->psg.playingCh4 << 3; } audio->nextSample -= audio->eventDiff;