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.

Flash Equipment > GBAMP firmware reprogramming... help me! [resolved]

#96074 - Dwedit - Tue Aug 01, 2006 7:44 am

I'm trying to write a function that erases an arbitrary 16k page from the GBAMP's firmware, but it's not working. Here's my code: (two different versions that both failed)

Code:

int FlashErase16k(unsigned int addr)
{
   //addr is in bytes
   //sector = 2048 words = 4096 bytes
   //need to erase 4 sectors
   int block=addr/16384;
   int checkByte;
   int sector;
   
   for (sector = 0; sector<4; sector++)
   {
      // First Halfword of four
      CartWrite(0x213C2, 0xAA);   //FlashWrite(0x5555, 0xAA);
      CartWrite(0x10C3D, 0x55);   //FlashWrite(0x2AAA, 0x55);
      CartWrite(0x213C2, 0x80);   //FlashWrite(0x5555, 0x80);
      CartWrite(0x213C2, 0xAA);   //FlashWrite(0x5555, 0xAA);
      CartWrite(0x10C3D, 0x55);   //FlashWrite(0x2AAA, 0x55);
      CartWrite((block*16384*8)+(sector*4) , 0x30);
      Delay100ms();
   }
   return 0;
}
int FlashErase16k(unsigned int addr)
{
   //addr is in bytes
   //sector = 2048 words = 4096 bytes
   //need to erase 4 sectors
   int block=addr/16384;
   int checkByte;
   int sector;
   int sectorlimit=block*4+4;
   
   for (sector = block*4;sector<sectorlimit;sector++)
   {
      // First Halfword of four
      CartWrite(0x213C2, 0xAA);   //FlashWrite(0x5555, 0xAA);
      CartWrite(0x10C3D, 0x55);   //FlashWrite(0x2AAA, 0x55);
      CartWrite(0x213C2, 0x80);   //FlashWrite(0x5555, 0x80);
      CartWrite(0x213C2, 0xAA);   //FlashWrite(0x5555, 0xAA);
      CartWrite(0x10C3D, 0x55);   //FlashWrite(0x2AAA, 0x55);
      FlashWrite(sector << 11, 0x30);
      Delay100ms();
   }
   return 0;
}


_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."


Last edited by Dwedit on Tue Aug 01, 2006 9:38 pm; edited 1 time in total

#96082 - chishm - Tue Aug 01, 2006 8:59 am

Because of the way the ROM chip is hooked up to the GBA bus, 4 physical ROM sectors appear to be interleaved into 1 logical block, which is why it has to erase 4 sectors at once. Each sector is 2KWord, with each word being 16 bits, so it is necessary to erase 16KiB at a time (as you probably know).

The offset formula you want is

0 <= sector < 4
Write to addresses ((block * 16384) + (sector * 2))

Also, make sure to run the unlock sequence first.
_________________
http://chishm.drunkencoders.com
http://dldi.drunkencoders.com

#96085 - Dwedit - Tue Aug 01, 2006 9:15 am

Flashwrite, or Cartwrite to do the address with that formula? Then you can just write to the rompage as normal, using the other functions from flashmp?

Edit:

This is the current code I'm using (edited into flashmp's flash.cpp). I've tried both multiplying block by 16384 and 8192, but both functions return -1.
Code:
int FlashErase16k(unsigned int addr)
{
   //addr is in bytes
   //sector = 2048 words = 4096 bytes
   //need to erase 4 sectors
   int block=addr/16384;
   int checkByte;
   int sector;
   int sectorlimit=block*4+4;

   for (sector = 0;sector<4;sector++)
   {
      // First Halfword of four
      CartWrite(0x213C2, 0xAA);   //FlashWrite(0x5555, 0xAA);
      CartWrite(0x10C3D, 0x55);   //FlashWrite(0x2AAA, 0x55);
      CartWrite(0x213C2, 0x80);   //FlashWrite(0x5555, 0x80);
      CartWrite(0x213C2, 0xAA);   //FlashWrite(0x5555, 0xAA);
      CartWrite(0x10C3D, 0x55);   //FlashWrite(0x2AAA, 0x55);
      FlashWrite( ((block * 8192) + (sector * 2))  , 0x30);
      Delay100ms();
      Delay100ms();
   }
   //verify the erase
   int cart_addr=0x08000000+block*16384*8+addr&16383;
   unsigned short *p=(unsigned short*)cart_addr;
   int i;
   for (i=0;i<8192;i++)
   {
      if (*p!=0xFFFF) return -1;
      p++;
   }
   return 0;
}

_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#96213 - Dwedit - Tue Aug 01, 2006 9:38 pm

Okay, now I've gotten it to work perfectly!
Here's the code:
Code:

/*
 * FlashErase16k
 */
int FlashErase16k(unsigned int addr)
{
   //addr is in bytes
   //sector = 2048 words = 4096 bytes
   //need to erase 4 sectors
   int block=addr/16384;
   int checkByte;
   int sector;
   int sectorlimit=block*4+4;

   for (sector = 0;sector<4;sector++)
   {
      // First Halfword of four
      CartWrite(0x213C2, 0xAA);   //FlashWrite(0x5555, 0xAA);
      CartWrite(0x10C3D, 0x55);   //FlashWrite(0x2AAA, 0x55);
      CartWrite(0x213C2, 0x80);   //FlashWrite(0x5555, 0x80);
      CartWrite(0x213C2, 0xAA);   //FlashWrite(0x5555, 0xAA);
      CartWrite(0x10C3D, 0x55);   //FlashWrite(0x2AAA, 0x55);
      FlashWrite( ((block * 8192) + (sector * 2048))  , 0x30);
      Delay100ms();
      Delay100ms();
   }
   return 0;
}

_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#97840 - spinal_cord - Fri Aug 11, 2006 9:12 pm

just being nosey, but why erase some of the gbamps firmware?
[edit]
doesnt matter, just read your earlyer post about the emulators.
_________________
I'm not a boring person, it's just that boring things keep happening to me.
Homepage