GP32 4kb Aligned Addresses


mATkEUpON

Certified Guru
Joined
Sep 30, 2003
Messages
276
I'd like to disable caching on my sound buffer, as I heard it would increase the mixing speed. The thing is, I declare my buffer simply like this:

unsigned short sbuffer[sbufsize];

How can I make a 4KB aligned buffer ???

Thanks
 
You need to allocate 4k extra, and then move the start of the buffer to the next 4k boundary after its initial start. For example (using dynamic memory allocation, it's pretty much the same with static allocation):
Code:
unsigned char *pRealBuffer; /* Use char because I get confused with pointer arithmetic :-) */

unsigned short *GetAlignedBuffer ( unsigned int samples )
{
   unsigned char *pAligned;
   unsigned int nTemp;

   pRealBuffer = (unsigned char *)malloc((samples*2) + 4096);
   if (!pRealBuffer)
      return (unsigned short *)0;
   nTemp = (unsigned int)pRealBuffer;
   nTemp += 4095;
   nTemp &= 0xfffff000;
   return (unsigned short *)nTemp;
}

void FreeAlignedBuffer (void)
{
   if (pRealBuffer)
      free (pRealBuffer);
   pRealBuffer = (unsigned char *)0;
}

A couple of notes:
1) This works by getting a pointer, then adding (4096 - 1) to it. That is guaranteed to make the pointer greater than the next 4k boundary, unless it is already on a 4k boundary to start with. Then masking off the bottom 12 bits decreases the pointer to the 4k boundary. Sheesh, I'm making a bloody awful job of explaining this so I think I'll just give up.
2) It's a bit naughty to convert pointers to/from int. Only works on machines where pointer size is the same as int size, which includes GP32 so it's kind-of OK :)
3) If you're using the gamepark sdk, then malloc() and free() should of course be gm_malloc() and gm_free() or whatever.

Hope this helps! :)
 
mATkEUpON posted on Aug 18 2004 at 12:03 AM said:
I'd like to disable caching on my sound buffer, as I heard it would increase the mixing speed. The thing is, I declare my buffer simply like this:

unsigned short sbuffer[sbufsize];

How can I make a 4KB aligned buffer ???

Thanks
> as I heard it would increase the mixing speed.

What ?

This is of couse wrong...

You want to disable the cache, to get a clean sound. And a much lower access to the
memory bus.

>unsigned short sbuffer[sbufsize];
Is a very bad idear for sound on gp32...

Use a static pointer to a memory region in gp32 memory ram, and set this region,
to uncached memory.



How to setup a working Ringbuffer with Mirkos SDK ?

#define SBUFSIZE 8192
#define UNCACHED 0x0C798000 // 8kb for one segment
gp_setMMU( UNCACHED, (UNCACHED +16384)-1, 0xFFA ); //cache off
gp_initSound(22050,16,SBUFSIZE*2); // Start Ringbuffer with space for 2 SBUFFSIZE

while (1) {
// Render your Sounddata to UNCACHED, len = SBUFSIZE
do_sound_calculation();
gp_addRingsegment(UNCACHED); // fill ringbuffer with SBUFSIZE, = one segment.
}


Issent this easy ?
 
Last edited by a moderator:
If you are using gcc then
unsigned short sbuffer[sbufsize] __attribute__ ((aligned (4096)));
does the thing also.
 
Well, thanks mr.spiv !!!!

mr.mirko, I don't really get the difference between a much lower access to the
memory bus, and increasing the speed...

Anyways, I was setting the flag to 0xFF2 as stated on mr.spiv's website, and the 0xFFA should leave the cache on but just disable the writeback (to get the clearer sound I think). What is the best setting ???
 
mATkEUpON posted on Aug 18 2004 at 12:16 PM said:
Well, thanks mr.spiv !!!!

mr.mirko, I don't really get the difference between a much lower access to the
memory bus, and increasing the speed...

Anyways, I was setting the flag to 0xFF2 as stated on mr.spiv's website, and the 0xFFA should leave the cache on but just disable the writeback (to get the clearer sound I think). What is the best setting ???
> What is the best setting ???

It depends on how mutch you use the bus, try both ...

>mr.mirko, I don't really get the difference between a much lower access to the
memory bus, and increasing the speed...

once again, youre disabling the cache on the sound DMA buffer, or any other buffer, with fast memory acces, to get a clean sound, and not to speed up something. Disabling cache on a memory region, will slow down the access. And why do we do this? Couse the design of the gp32 soundhardware ( no real soundchip ), needs this.

There is no A-B-C cook book for a work around on the gp32 soundbug.
I found out, that this helped me, developing the modplayer:

- disable write back cache on main sound loop buffer, and all sound related buffers.
- fill the soundbuffer with soundbuffer[x] = *buffer++;
- dont use malloc/free with newlib, the garbage collection will result in sound crap...
- If you still suffer from soundbug, compile memory expensive part with -O0
- try using a high memory region for the main sampleloop, ( near framebuffer )

Sound on gp32 is a real hell, it took me 3 Month developing a clean modplayer, i had to
rewrite all memory access in source...
 
Last edited by a moderator:
Thanks for your explanations.

Actually, I hadn't any sound glitches before that, but I believe this is more elegant. Anyways, if someone is interested in my asm mixing routine, just tell me.
 
Back
Top