函数如下:
/*
* Check memory range for valid RAM. A simple memory test determines
* the actually available RAM size between addresses `base' and
* `base + maxsize'.
*/
long get_ram_size(long *base, long maxsize)
{
volatile long *addr;
long save[32];
long cnt;
long val;
long size;
int i = 0;
for (cnt = (maxsize / sizeof (long)) >> 1; cnt > 0; cnt >>= 1) {
addr = base + cnt; /* pointer arith! */
sync ();
save[i++] = *addr;
sync ();
*addr = ~cnt;
}
addr = base;
sync ();
save[i] = *addr;
sync ();
*addr = 0;
sync ();
if ((val = *addr) != 0) {
/* Restore the original data before leaving the function.
*/
sync ();
*addr = save[i];
for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
addr = base + cnt;
sync ();
*addr = save[--i];
}
return (0);
}
for (cnt = 1; cnt < maxsize / sizeof (long); cnt <<= 1) {
addr = base + cnt; /* pointer arith! */
val = *addr;
*addr = save[--i];
if (val != ~cnt) {
size = cnt * sizeof (long);
/* Restore the original data before leaving the function.
*/
for (cnt <<= 1; cnt < maxsize / sizeof (long); cnt <<= 1) {
addr = base + cnt;
*addr = save[--i];
}
return (size);
}
}
return (maxsize);
}
解释说明:
1.从最大地址>>1开始,依次地址右移1,写入~cnt。
2.从最小地址读出比较。
3.没比较过一个size增大一倍。
具体例子如下:假设设定最大地址为1G,实际地址只有256M。
1.第一次写入512M地址处为:~512M=FFFFFFFFDFFFFFFF,由于没有512M,所以写入的数据到了256M地址处。
2.第二次写入256M地址处为:~256M=FFFFFFFFEFFFFFFF,实际写到256M处的为当前数据。
3.第三次写入128M地址处为:~128M=FFFFFFFFF7FFFFFF
一次类推;
在反向检测时,会出现256M地址处正常,512M地址处有问题。进而可以监测出地址为256M。
阅读(2928) | 评论(0) | 转发(0) |