gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Clearing a BG

#99216 - psycorpse - Sun Aug 20, 2006 3:12 am

How do you clear a bank of ram? I need to clear BG_BMP_RAM(0).

You don't even have to answer it if you can point me to a place that I can read up on it.


Thanks,
Mike

#99217 - tepples - Sun Aug 20, 2006 3:16 am

The simple way, given the base of a bank and its size in bytes:
Code:

void clearMemory(u32 *dst, unsigned int size) {
  for(; size >= sizeof(u32); size -= sizeof(u32)) {
    *dst++ = 0;
  }
}

The following equivalent code has been manually unrolled to encourage the compiler to emit efficient 'stmia' instructions and fewer branches:
Code:

void clearMemory(u32 *dst, unsigned int size) {
  for (; size >= 4 * sizeof(u32); size -= 4 * sizeof(u32)) {
    dst[0] = 0;
    dst[1] = 0;
    dst[2] = 0;
    dst[3] = 0;
    dst += 4;
  }
  for(; size >= sizeof(u32); size -= sizeof(u32)) {
    *dst++ = 0;
  }
}

_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#99219 - knight0fdragon - Sun Aug 20, 2006 3:37 am

memset(dst,0,size);?
_________________
http://www.myspace.com/knight0fdragonds

MK DS FC: Dragon 330772 075464
AC WW FC: Anthony SamsClub 1933-3433-9458
MPFH: Dragon 0215 4231 1206

#99230 - tepples - Sun Aug 20, 2006 5:16 am

memset() works a byte at a time, which is 1. slow and 2. not compatible with DS VRAM.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#99262 - LiraNuna - Sun Aug 20, 2006 10:40 am

That's wrong. It has been proven that memset aligns itself to the size of the memory and it's the second fastest method after stmia clear. (dsboi's testings )

Edit: Oh and, you can use memset() to clear a 16bit rotoscale BG, it does 4byte writes.
_________________
Private property.
Violators will be shot, survivors will be shot again.

#99433 - psycorpse - Mon Aug 21, 2006 5:40 pm

LiraNuna wrote:
That's wrong. It has been proven that memset aligns itself to the size of the memory and it's the second fastest method after stmia clear. (dsboi's testings )

Edit: Oh and, you can use memset() to clear a 16bit rotoscale BG, it does 4byte writes.


I gave it a shot and it worked. I also found www.double.co.nz/nintendo_ds/nds_develop10.html
which uses it to clear some memory.