RAM Hack

From wiki.gp2x.org

By tweaking the RAM timings one can gain a moderate speed increase in their applications. The following source code shows how it's done.

static const unsigned short MEMTIMEX0 = 0x3802;
static const unsigned short MEMTIMEX1 = 0x3804;

union memtimex0_t {
    struct {
        unsigned int tRCD : 4; // default = 2
        unsigned int tRP  : 4; // default = 15
        unsigned int tRFC : 4; // default = 4
        unsigned int tMRD : 4; // default = 4
    } s;
    unsigned short i;
};

union memtimex1_t {
    struct {
        unsigned int tWR  : 4; // default = 2
        unsigned int tRAS : 4; // default = 7
        unsigned int tRC  : 4; // default = 10
        unsigned int LAT  : 1; // default = 1; apparently readonly
        unsigned int reserved : 2; // readonly
        
        // Write: Set 1 to set new SDRAM mode parameter;
        // Read: 0 = complete, 1 = busy
        unsigned int ModeStatusSet : 1;
    } s;
    unsigned short i;
};

void ramhack()
{
    memtimex0_t t0;
    t0.i = memreg16(MEMTIMEX0);
    t0.s.tRCD = 1;
    t0.s.tRP = 1;
    t0.s.tRFC = 0;
    t0.s.tMRD = 0;
    memreg16(MEMTIMEX0) = t0.i;
    
    memtimex1_t t1;
    t1.i = memreg16(MEMTIMEX1);
    t1.s.tWR = 0;
    t1.s.tRAS = 3;
    t1.s.tRC = 5;
    
    // The docs say to write 1 to set new mode
    // parameter but seems to work without?
    t1.s.ModeStatusSet = 1;
    memreg16(MEMTIMEX1) = t1.i;
    
    // Wait until mode is set. May not be necessary?
    do {
        t1.i = memreg16(MEMTIMEX1);
    } while (t1.s.ModeStatusSet);
}

The above source code it taken from the gp2x board and sets new RAM timings that differ from the default values. What the above code doesn't do is restoring the preexisting values in those register. This should always be done on application termination.

The stability of this solution is uncertain especially when the CPU is overclocked. Some fast but stable values still need to be gathered.

Personal tools