📄 tstsdram.c
字号:
int t_sdram_walk0(UInt32 *begin_p, UInt32 len) {
UInt32 *end_p ;
UInt32 *ipp ;
UInt32 start_val ;
UInt32 loop_val ;
UInt32 exp_val ;
UInt32 obs_val ;
int errors ;
int lap ;
end_p = (UInt32 *) (((char *) begin_p) + len) ;
errors = 0 ;
lap = 0 ;
start_val = 1 ;
while (start_val != 0) {
printf(" lap %2d\n", lap++) ;
/*
* Fill memory with data
*/
loop_val = start_val ;
ipp = begin_p ;
while (ipp < end_p) {
exp_val = ~loop_val ;
*ipp++ = exp_val ;
loop_val = loop_val << 1 ;
if (loop_val == 0) {
loop_val = 1 ;
}
}
/*
* Check memory
*/
loop_val = start_val ;
ipp = begin_p ;
while (ipp < end_p) {
exp_val = ~loop_val ;
obs_val = *ipp ;
if (obs_val != exp_val) {
errors++ ;
printf("read error: addr %08x exp %08x obs %08x xor %08x\n",
ipp, exp_val, obs_val, exp_val ^ obs_val) ;
dinvalid(0, (UInt32) ipp) ;
obs_val = *ipp ;
if (obs_val != exp_val) {
printf("re-read error: addr %08x exp %08x obs %08x xor %08x\n",
ipp, exp_val, obs_val, exp_val ^ obs_val) ;
}
if (errors > ERROR_MAX) {
printf("errors > %d, exitting...\n", ERROR_MAX) ;
return errors ;
}
}
loop_val = loop_val << 1 ;
if (loop_val == 0) {
loop_val = 1 ;
}
ipp++ ;
}
start_val = start_val << 1 ;
}
return errors ;
}
/*
* Test a single cache load using back to back writes and back to back reads
* Stores pat, ~pat at address, address+1
*/
int t_sdram_back_to_back_cache(UInt32 *base_p, UInt32 pat) {
UInt32 *ipp ;
UInt32 exp_val ;
UInt32 obs_val ;
UInt32 offset ;
UInt32 tag ;
UInt32 address ;
int errors ;
#pragma TCS_break_dtree
/* copyback/invalidate the entire cache */
for (offset = 0; offset < CACHE_SIZE; offset += CACHE_LINE_SIZE) {
tag = rdtag(0, offset) ;
address = tag << 11 ;
dcb(0, address) ;
dinvalid(0, address) ;
}
#pragma TCS_break_dtree
/* Fill cache with data==address */
ipp = base_p ;
exp_val = pat ;
for (offset = 0; offset < CACHE_SIZE; offset += sizeof(UInt32)) {
*ipp = exp_val ;
exp_val = exp_val ^ 0xffffffff ;
ipp++;
}
#pragma TCS_break_dtree
/* Copyback cache to memory */
ipp = base_p ;
for (offset = 0; offset < CACHE_SIZE; offset += CACHE_LINE_SIZE) {
dcb(0, (UInt32) ipp) ;
ipp += CACHE_LINE_SIZE / sizeof(UInt32) ;
}
#pragma TCS_break_dtree
/* Invalidate cache */
ipp = base_p ;
for (offset = 0; offset < CACHE_SIZE; offset += CACHE_LINE_SIZE) {
dinvalid(0, (UInt32) ipp) ;
ipp += CACHE_LINE_SIZE / sizeof(UInt32) ;
}
#pragma TCS_break_dtree
/* Reload memory to cache */
ipp = base_p ;
for (offset = 0; offset < CACHE_SIZE; offset += CACHE_LINE_SIZE) {
ld32d(0, (UInt32) ipp) ;
ipp += CACHE_LINE_SIZE / sizeof(UInt32) ;
}
#pragma TCS_break_dtree
/*
* Check cache with data==address
*/
errors = 0 ;
ipp = base_p ;
exp_val = pat ;
for (offset = 0; offset < CACHE_SIZE; offset += sizeof(UInt32)) {
obs_val = *ipp ;
if (obs_val != exp_val) {
errors++ ;
printf("read error: addr %08x exp %08x obs %08x xor %08x\n",
ipp, exp_val, obs_val, exp_val ^ obs_val) ;
dinvalid(0, (UInt32) ipp) ;
obs_val = *ipp ;
if (obs_val != exp_val) {
printf("re-read error: addr %08x exp %08x obs %08x xor %08x\n",
ipp, exp_val, obs_val, exp_val ^ obs_val) ;
}
if (errors > ERROR_MAX) {
printf("errors > %d, exitting...\n", ERROR_MAX) ;
return errors ;
}
}
exp_val = exp_val ^ 0xffffffff ;
ipp++ ;
}
return errors ;
}
/*
* Test sdram using back to back writes and back to back reads
* Stores pat, ~pat at address, address+1
*/
int t_sdram_back_to_back(UInt32 *begin_p, UInt32 len, UInt32 pat) {
int errors ;
UInt32 *ipp ;
UInt32 *end_p ;
/* get pointers to base/end of cache size aligned areas */
ipp = (UInt32 *) ((((UInt32)begin_p) + (CACHE_SIZE-1)) & ~(CACHE_SIZE-1)) ;
end_p = (UInt32 *) ((((UInt32)begin_p) + len) & ~(CACHE_SIZE-1)) ;
errors = 0 ;
while (ipp < end_p) {
errors += t_sdram_back_to_back_cache(ipp, pat) ;
ipp += CACHE_SIZE / sizeof(UInt32) ;
if (errors > ERROR_MAX) {
printf("errors > %d, exitting...\n", ERROR_MAX) ;
return errors ;
}
}
return errors ;
}
static unsigned int GetMaxAvailableSize(void)
{
char *pBlock ;
unsigned int Size ;
unsigned int Span ;
/*
* Binary search for maximum block size
*/
Size = 0x40000000 ;
Span = Size >> 1 ;
while (Span != 0)
{
pBlock = malloc(Size) ;
if (pBlock == NULL)
{
Size -= Span ;
}
else
{
free(pBlock) ;
Size += Span ;
}
Span = Span >> 1 ;
}
return Size ;
}
//-----------------------------------------------------------------------------
// Main Entry Point
//-----------------------------------------------------------------------------
//
int tstSdram(void) {
unsigned int maxblock;
static UInt32 *start;
static int len;
static int first = 1;
maxblock = GetMaxAvailableSize();
printf("Maximum available block is %d (0x%08X) bytes.\n",maxblock,maxblock);
if(first) {
first = 0;
len = ((maxblock-(1<<10)) & ~63);
start = (UInt32 *) malloc(len) ;
start = (UInt32 *)((((unsigned)(start)) + 128) & ~63); /* Align start to 64 bytes */
len -= 256; /* Safety */
if (len < (CACHE_SIZE*2)) {
printf("only %d bytes available, need at least %d bytes\n",
len, (CACHE_SIZE*2)) ;
return 1 ;
}
if(start == NULL) {
printf("malloc failed\n");
return 1;
}
else printf("SDRAM TEST: Got %d Kbytes, from 0x%.8x to 0x%.8x\n",
len/1024, (UInt32)(start), ((UInt32)(start)+len));
}
sdram_fail = t_sdram_addr(start, len);
printf("sdram_addr %s\n", sdram_fail ? "FAILED" : "passed");
if (sdram_fail) return 1;
sdram_fail = t_sdram_inv_addr(start, len);
printf("sdram_inv_addr %s\n", sdram_fail ? "FAILED" : "passed");
if (sdram_fail) return 1;
sdram_fail |= t_sdram_flipbits(0x5A5A5A5A, (unsigned *)start, len);
printf("sdram_flip 0x5A5A5A5A %s\n", sdram_fail ? "FAILED" : "passed");
if(sdram_fail) return 1;
sdram_fail |= t_sdram_flipbits(0x00000000, (unsigned *)start, len);
printf("sdram_flip 0x00000000 %s\n", sdram_fail ? "FAILED" : "passed");
if(sdram_fail) return 1 ;
sdram_fail = t_sdram_walk1(start, len);
printf("sdram_walk1 %s\n", sdram_fail ? "FAILED" : "passed");
if (sdram_fail) return 1 ;
sdram_fail = t_sdram_walk0(start, len);
printf("sdram_walk0 %s\n", sdram_fail ? "FAILED" : "passed");
if (sdram_fail) return 1 ;
sdram_fail = t_sdram_back_to_back(start, len, 0x00000000);
printf("sdram_back_to_back 0 %s\n", sdram_fail ? "FAILED" : "passed");
if (sdram_fail) return 1 ;
sdram_fail = t_sdram_back_to_back(start, len, 0xffffffff);
printf("sdram_back_to_back f %s\n", sdram_fail ? "FAILED" : "passed");
sdram_fail = t_sdram_back_to_back(start, len, 0xaaaaaaaa);
printf("sdram_back_to_back a %s\n", sdram_fail ? "FAILED" : "passed");
if (sdram_fail) return 1 ;
sdram_fail = t_sdram_back_to_back(start, len, 0x55555555);
printf("sdram_back_to_back 5 %s\n", sdram_fail ? "FAILED" : "passed");
if (sdram_fail) return 1 ;
return 0 ;
}
tmMain() // expands to right framework for all OS with init. See tmMain.h
{
printf("\n") ;
printf("tstSdram start\n") ;
if (tstSdram() != 0) {
printf("tstSdram FAIL\n") ;
tmMain_EXIT (1);
}
else {
printf("tstSdram PASS\n") ;
tmMain_EXIT (0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -