soundPlayX crashes randomly?

Post Reply
Dren
Posts: 11
Joined: Wed Mar 16, 2011 10:12 pm

soundPlayX crashes randomly?

Post by Dren » Thu Feb 14, 2013 1:59 pm

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.

elhobbs
Posts: 354
Joined: Thu Jul 02, 2009 1:19 pm

Re: soundPlayX crashes randomly?

Post by elhobbs » Thu Feb 14, 2013 2:17 pm

out of curiosity - can you give a little detail about the context where you are calling these functions? Is this taking place in an interrupt handler do you have any error codes - are you using the default exception handler which would dump the stack and registers?

Dren
Posts: 11
Joined: Wed Mar 16, 2011 10:12 pm

Re: soundPlayX crashes randomly?

Post by Dren » Thu Feb 14, 2013 4:50 pm

The sound code isn't run from an interrupt handler, it's executed when the emulated cpu writes to the corresponding registers. It all descends directly from "int main()".
I didn't know about any exception handlers. I added "defaultExceptionHander()" to the initialization routine, but there was no output when it crashed. I do have the console set up, with stderr directed to it, but nothing happened.

Dren
Posts: 11
Joined: Wed Mar 16, 2011 10:12 pm

Re: soundPlayX crashes randomly?

Post by Dren » Tue Feb 26, 2013 1:30 am

My workaround was unsatisfactory, as it turns out. Putting it into sleep mode seems to sometimes cause the arm7 to crash, perhaps the arm9 was awake and sent too many commands while the arm7 was going into / exiting sleep? Making arm9 wait for a response again seems to fix this particular issue, but then, the arm9 crashes randomly again. (However arm7 is still alive, as the L+R+START+SELECT combo to exit works.) So my options are:

A) Wait for arm7 to send a response to arm9, causing arm9 to crash for an unknown reason
B) Arm9 doesn't wait for a response (and arm7 doesn't send one), in which case arm7 will sometimes crash when going to sleep.

If anyone would be kind enough to take a look at this, the project I'm referring to is gameyob, a gameboy emulator. Its source is all on github. Specifically in "gbsnd.cpp" for arm9 and "sound.c" for arm7. However this occurs even when using libnds's functions rather than my workaround ones.

Dren
Posts: 11
Joined: Wed Mar 16, 2011 10:12 pm

Re: soundPlayX crashes randomly?

Post by Dren » Wed Feb 27, 2013 4:18 pm

I've made yet another workaround where the arm9 detects sleep mode and turns off sound emulation until sleep mode is done... so I guess this isn't an issue anymore. Hopefully it won't come back to haunt me.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest