I picked up an emulation project for the ds which I hadn't looked at in awhile. For sound emulation I needed low-level sound access. Libnds's "sound.h" functions worked fine... except when it crashed randomly. And I mean
randomly... I couldn't see any pattern to it. It could happen seconds after starting a game, or 10 minutes in. For each sound channel, when a tone was requested, my code would look like this:
Code: Select all
soundKill(channel[i]);
channel[i] = soundPlayPSG(dutyTable[chanDuty[i]], chanFreq[i], 0, 64);
I can't post my full code since it's mixed with irrelevant emulation code. But, simply commenting out the "soundPlayPSG" line would remove the crashes. I downloaded libnds's source code, and I knew when I looked at it that this was the culprit:
Code: Select all
while(!fifoCheckValue32(FIFO_SOUND));
return (int)fifoGetValue32(FIFO_SOUND);
It was caught in the while loop. But, I checked the corresponding arm7 code and I saw no reason why this would happen. Still, I followed my gut and rewrote the "soundPlayPSG" function on both the arm9 and arm7 sides. Actually, the only change I made was that it passes the channel number through FIFO on the arm9 side, so that arm9 doesn't need to wait for a response. Now the sound works, and it doesn't crash randomly.
I'm not sure if this is a problem with my code or with libnds. But, I thought I should give you a heads-up that somehow, code execution can become stuck in that while loop.