// memtest_boot.c
#include "memtest_boot.h"
// 简单的随机数生成器
static ul rand_seed = 1;
static ul simple_rand(void) {
rand_seed = rand_seed * 1103515245 + 12345;
return rand_seed;
}
// 比较两块内存区域
static int compare_regions(ulv *bufa, ulv *bufb, ul count) {
ul i;
ulv *p1 = bufa;
ulv *p2 = bufb;
for (i = 0; i < count; i++, p1++, p2++) {
if (*p1 != *p2) {
return TEST_FAIL;
}
}
return TEST_SUCCESS;
}
// 地址线测试
int test_addr_lines(ulv *addr, ul count) {
ul pattern = 1;
ul j;
for (j = 0; j < sizeof(ul) * 8; j++) {
ulv *p = addr;
ulv *p1 = (ulv *)(((ul)addr) + pattern);
if (((ul)p1) < ((ul)addr) + count) {
*p = 0;
*p1 = ~0UL;
if (*p != 0) {
return TEST_FAIL;
}
}
pattern = pattern << 1;
}
return TEST_SUCCESS;
}
// 随机值测试
int test_random_value(ulv *addr, ul count) {
ul i;
for (i = 0; i < count; i++) {
ul pattern = simple_rand();
*addr = pattern;
if (*addr != pattern) {
return TEST_FAIL;
}
addr++;
}
return TEST_SUCCESS;
}
// 固定位模式测试
int test_solid_bits(ulv *addr, ul count) {
ul i;
// 测试全1模式
for (i = 0; i < count; i++) {
*addr = ~0UL;
if (*addr != ~0UL) {
return TEST_FAIL;
}
addr++;
}
addr -= count;
// 测试全0模式
for (i = 0; i < count; i++) {
*addr = 0;
if (*addr != 0) {
return TEST_FAIL;
}
addr++;
}
return TEST_SUCCESS;
}
// 棋盘模式测试
int test_checkerboard(ulv *addr, ul count) {
ul i;
ul pattern1 = 0x55555555;
ul pattern2 = 0xAAAAAAAA;
// 写入pattern1
for (i = 0; i < count; i++) {
*addr = pattern1;
addr++;
}
addr -= count;
// 验证pattern1
for (i = 0; i < count; i++) {
if (*addr != pattern1) {
return TEST_FAIL;
}
addr++;
}
addr -= count;
// 写入pattern2
for (i = 0; i < count; i++) {
*addr = pattern2;
addr++;
}
addr -= count;
// 验证pattern2
for (i = 0; i < count; i++) {
if (*addr != pattern2) {
return TEST_FAIL;
}
addr++;
}
return TEST_SUCCESS;
}
// 行走1测试
int test_walking_one(ulv *addr, ul count) {
ul pattern = 1;
ul i, j;
for (i = 0; i < sizeof(ul) * 8; i++) {
for (j = 0; j < count; j++) {
addr[j] = pattern;
if (addr[j] != pattern) {
return TEST_FAIL;
}
}
pattern = pattern << 1;
}
return TEST_SUCCESS;
}
// 主测试函数
int do_memtest(void *start, ul size, ul test_mask) {
ulv *addr = (ulv *)start;
ul count = size / sizeof(ul);
int result = TEST_SUCCESS;
// 地址线测试
if (test_mask & TEST_ADDR_LINE) {
if (test_addr_lines(addr, size) != TEST_SUCCESS) {
result |= TEST_ADDR_LINE;
}
}
// 随机值测试
if (test_mask & TEST_RANDOM) {
if (test_random_value(addr, count) != TEST_SUCCESS) {
result |= TEST_RANDOM;
}
}
// 固定位模式测试
if (test_mask & TEST_SOLID_BITS) {
if (test_solid_bits(addr, count) != TEST_SUCCESS) {
result |= TEST_SOLID_BITS;
}
}
// 棋盘模式测试
if (test_mask & TEST_CHECKER) {
if (test_checkerboard(addr, count) != TEST_SUCCESS) {
result |= TEST_CHECKER;
}
}
// 行走1测试
if (test_mask & TEST_WALKING_1) {
if (test_walking_one(addr, count) != TEST_SUCCESS) {
result |= TEST_WALKING_1;
}
}
return result;
}
注意:头文件参考上篇
标签:count,memtest,addr,++,return,ul,TEST,uboot,--------- From: https://blog.csdn.net/u011608645/article/details/145037245