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.

Coding > How to write to EReader SRAM

#42190 - Maverick - Sat May 07, 2005 12:13 pm

I am looking to write a simple UART comms program that writes to the ereader sram. Allowing me to write some ereader code without wasting lots and lots of paper.

Does anyone have any information on this or can point me in the right direction?

I need locations and any bit commands if needed.

Many thanks

#42196 - caitsith2 - Sat May 07, 2005 4:07 pm

e-reader uses 1Mbit flash, bankswitched in 2 512Kbit banks.

What you need to know, is that the program storage starts in the upper bank.

This is the code I have to write to the flash, and switch banks. (asm code).

Code:

.GLOBL  check_flashrom
.GLOBL  switchbank0
.GLOBL  switchbank1
.GLOBL  writeflash
.GLOBL  eraseflash

.ARM
@.SECTION .iwram
.ALIGN
.TEXT

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@
@   Flash related functions.
@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

       
@extern void check_flashrom(u16 *v1, u16 *v2);
check_flashrom:


   STMFD   sp!, {r2-r5}
   ldr     r5,=0xE005555
   mov     r2,#0xAA
   strb    r2,[r5]         @ AA => 0800AAAA
   mov     r4,#0xE000000
   ldr     r3,=0xE002AAA
   mov     r2,#0x55
   strb    r2,[r3]         @ 55 => 08005554
   mov     r2,#0x90
   strb    r2,[r5]         @ 90 => 0800AAAA
   ldrh    r2,[r4]         @ 08000000 => v1
   strh    r2,[r0]         
   ldrh    r0,[r4,#0x1]      @ 08000002 => v2
   strh    r0,[r1]
   mov     r0,#0xF0
   strh    r0,[r4]         @ F0 => 08000000
   LDMFD   sp!, {r2-r5}
   bx      lr

@extern void switchbank0(void);
switchbank0:
   STMFD   sp!, {r2-r5}
   ldr     r5,=0xE005555
   mov     r2,#0xAA
   strb    r2,[r5]         @ AA => 0800AAAA
   mov     r4,#0xE000000
   ldr     r3,=0xE002AAA
   mov     r2,#0x55
   strb    r2,[r3]         @ 55 => 08005554
   mov     r2,#0xB0
   strb    r2,[r5]         @ 90 => 0800AAAA
   mov     r2,#0x00
   strb    r2,[r4]
   LDMFD   sp!, {r2-r5}
   bx      lr
   
@extern void switchbank1(void);
switchbank1:
   STMFD   sp!, {r2-r5}
   ldr     r5,=0xE005555
   mov     r2,#0xAA
   strb    r2,[r5]         @ AA => 0800AAAA
   mov     r4,#0xE000000
   ldr     r3,=0xE002AAA
   mov     r2,#0x55
   strb    r2,[r3]         @ 55 => 08005554
   mov     r2,#0xB0
   strb    r2,[r5]         @ 90 => 0800AAAA
   mov     r2,#0x01
   strb    r2,[r4]
   LDMFD   sp!, {r2-r5}
   bx      lr
   

@extern void writeflash(u16 address, u8 data);
writeflash:
   STMFD   sp!, {r2-r5}
   ldr     r5,=0xE005555
   mov     r2,#0xAA
   strb    r2,[r5]         @ AA => 0800AAAA
   mov     r4,#0xE000000
   add     r0, r0, r4
   ldr     r3,=0xE002AAA
   mov     r2,#0x55
   strb    r2,[r3]         @ 55 => 08005554
   mov     r2,#0xA0
   strb    r2,[r5]         @ 90 => 0800AAAA
   strb    r1,[r0]
writeloop:
    ldrb    r1,[r0]
    ldrb    r2,[r0]
    cmp     r1,r2
    bne     writeloop
    ldrb    r1,[r0]
    ldrb    r2,[r0]
    cmp     r1,r2
    bne     writeloop
   LDMFD   sp!, {r2-r5}
   bx      lr
   
@extern void eraseflash(void);
eraseflash:
    STMFD   sp!, {r0-r5}
    ldr     r3,=0xE005555
    ldr     r4,=0xE002AAA
    ldr     r5,=0xE000000
    mov     r2,#0xAA        @ -> E005555
    strb    r2,[r3]
    mov     r2,#0x55        @ -> E002AAA
    strb    r2,[r4]
    mov     r2,#0x80        @ -> E005555
    strb    r2,[r3]
    mov     r2,#0xAA        @ -> E005555
    strb    r2,[r3]
    mov     r2,#0x55        @ -> E002AAA
    strb    r2,[r4]
    mov     r2,#0x10        @ -> E005555
    strb    r2,[r3]
eraseloop:
    ldrb    r0,[r5]
    ldrb    r1,[r5]
    cmp     r0,r1
    bne     eraseloop
    ldrb    r0,[r5]
    ldrb    r1,[r5]
    cmp     r0,r1
    bne     eraseloop
   LDMFD   sp!, {r0-r5}
   bx      lr
   
[/code]

#42208 - Maverick - Sat May 07, 2005 9:06 pm

Thanks

So the EReader save starts at address 0x0E000000

First, erase then write to bank one, then erase and write to bank two

How, when and why do i need to use check flash rom?

To check it is ok, do i just need to print from 0x0E000000 straight after?

Many thanks for all the help

#42226 - caitsith2 - Sun May 08, 2005 3:14 am

You don't really need to use check flash rom at all, unless you wish to know the manufacture/device type bytes.

But yeah, if there is any info in the current sector you wish to save, read it out to a block of memory, make the changes to that block of memory you read the sector out to, erase the sector, write the sector back in.

However, if your not saving any bytes within the sector, then just erase the sector, write what you want to it.

#42251 - wintermute - Sun May 08, 2005 1:31 pm

any reason the bankswitching wasn't done like this?

Code:

@extern void switchbank(int bank);
switchbank:
   STMFD   sp!, {r2-r5}
   ldr     r5,=0xE005555
   mov     r2,#0xAA
   strb    r2,[r5]         @ AA => 0800AAAA
   mov     r4,#0xE000000
   ldr     r3,=0xE002AAA
   mov     r2,#0x55
   strb    r2,[r3]         @ 55 => 08005554
   mov     r2,#0xB0
   strb    r2,[r5]         @ 90 => 0800AAAA
   strb    r0,[r4]
   LDMFD   sp!, {r2-r5}
   bx      lr


Is this code timing sensitive? i.e. does it need to be in arm assembly or would C code suffice?

This would probably make a useful addition to the Ereader support in devkitARM/libgba.