📄 memory.c
字号:
/*************************************************************************/
/* */
/* FILE NAME VERSION */
/* */
/* memory.c KS32C50100 : version 1.0 */
/* */
/* COMPONENT */
/* */
/* */
/* DESCRIPTION */
/* */
/* */
/* AUTHOR */
/* */
/* */
/* DATA STRUCTURES */
/* */
/* */
/* FUNCTIONS */
/* */
/* Evaluation & debugging utility for memory. */
/* */
/* DEPENDENCIES */
/* */
/*************************************************************************/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "snds.h"
#include "uart.h"
#include "isr.h"
#include "std.h"
#include "memory.h"
#include "pollio.h"
#include "sysconf.h"
extern char Image$$RW$$Limit[];
void *mallocPt=Image$$RW$$Limit;
//#define HEAPEND 0x13fffff /* DATA Area:0x1000000~0x13fffff */
void *nmallocPt = (void *)AllocNonCacheAreaBase ;
// Memory Test
void MemoryPartTest(void)
{
char TestItemSel ;
IOPDATA = 0x0 ; // All LED On
do {
Print("\n+---------------------------------------------------+\r") ;
Print("| >>>>> Memory Test <<<<< |\r") ;
Print("+---------------------------------------------------+\r") ;
Print("| Memory Copy Test - [C] |\r") ;
Print("| Memory Dump - [R] |\r") ;
Print("| Memory Pattern Fill - [F] |\r") ;
Print("| Memory Pattern Search - [S] |\r") ;
Print("| External I/O Bank Test - [E] |\r") ;
Print("| Quit - [Q] |\r") ;
Print("+---------------------------------------------------+\r") ;
Print("\rSelect Test Item : ") ;
TestItemSel = get_byte() ;
switch(TestItemSel)
{
case 'C' : case 'c' : MemoryTest() ; break ;
case 'R' : case 'r' : MemoryDump() ; break ;
case 'F' : case 'f' : MemPatternFill() ; break ;
case 'S' : case 's' : MemPatternSearch(); break ;
case 'E' : case 'e' : ExtIOBankTest(); break ;
case 'Q' : case 'q' : break ;
default : Print("\nInvalid Test Item Selected") ;
break ;
}
Print("\n Press Any Key To Continue ....") ; get_byte() ;
} while ( (TestItemSel != 'Q') && (TestItemSel != 'q') ) ;
IOPDATA = 0x0F ;
}
// Memory Dump top function
void MemoryDump(void)
{
U32 DumpInitAdd, DumpSize ;
U32 *base; // Dump Memory base address
U32 *end; // Dump Memory end address
Print("\n >> Input Memory Dump Base Address (0x1000000) : 0x") ;
DumpInitAdd = get_num() ;
if (DumpInitAdd==0) DumpInitAdd=0x1000000 ;
Print("\r >> Input Memory Dump Size (0x100) : 0x") ;
DumpSize = get_num() ;
if (DumpSize==0) DumpSize=0x100 ;
base = (U32 *)DumpInitAdd;
end = (U32 *)(DumpInitAdd + DumpSize) ;
Print("\nMemory Dump from %x to %x",base, end) ;
MemDump(base, end);
}
/*
* Memory dump program
* ~~~~~~~~~~~~~~~~~~~~
*/
void MemDump(U32 *base, U32 *end)
{
int i;
int j;
int k;
int Range;
U32 *DumpBase;
U32 *DumpEnd ;
DumpBase = base;
DumpEnd = end;
Range=DumpEnd-DumpBase ;
k=0; j=0;
for (i=0;i < Range+4;i+=j)
if (k==20) {
Print("\nPress Any Key");
get_byte();k=0;}
else {
k++;
{
Print("\r%8x : ",DumpBase) ;
j=0;
for (j=0; j<4 ; j++)
{
Print(" %08x ",*DumpBase) ;
DumpBase++ ;
}
}
}
}
/*
* Memory Clear
* ~~~~~~~~~~~~~~~~~~~
*/
void MemClear(U32 *startaddr, U32 *endaddr)
{
int cnt ;
cnt = (int)(endaddr - startaddr) ;
while(cnt--) *startaddr++ = 0x00000000 ;
}
/*
* Memory Pattern Fill
* ~~~~~~~~~~~~~~~~~~~
*/
void MemPatternFill(void)
{
U32 *StartAddr, *EndAddr ;
U32 Pattern ;
int cnt ;
Print("\n >>> Memory Pattern Fill <<< ") ;
Print("\r $$ Memory Pattern Fill Start Address : 0x") ;
StartAddr = (U32 *)get_num() ;
Print("\r $$ Memory Pattern Fill End Address : 0x") ;
EndAddr = (U32 *)get_num() ;
Print("\r $$ Memor Fill Pattern : 0x") ;
Pattern = get_num() ;
cnt = (int)(EndAddr - StartAddr) ;
Print("\n >> Memory Fill %x to %x, Pattern : %x",\
StartAddr, EndAddr,Pattern) ;
while(cnt--) *StartAddr++ = Pattern ;
}
/*
* Memory Pattern Search
* ~~~~~~~~~~~~~~~~~~~~~
*/
void MemPatternSearch(void)
{
U32 *StartAddr, *EndAddr ;
U32 Pattern ;
int cnt ;
Print("\n >>> Memory Pattern Search <<< ") ;
Print("\r $$ Memory Pattern Search Start Address : 0x") ;
StartAddr = (U32 *)get_num() ;
Print("\r $$ Memory Pattern Search End Address : 0x") ;
EndAddr = (U32 *)get_num() ;
Print("\r $$ Memory Search Pattern : 0x") ;
Pattern = get_num() ;
cnt = (int)(EndAddr - StartAddr) ;
Print("\n >> Memory Pattern Search 0x%x to 0x%x, Pattern : %x\n", \
StartAddr, EndAddr,Pattern) ;
while(cnt--)
{
if (*StartAddr == Pattern)
Print("\r +-- Pattern is Searched : %x (%x)",\
StartAddr, *StartAddr);
StartAddr++ ;
}
}
/*
* Memory Test Top Program
* ~~~~~~~~~~~~~~~~~~~~~~~~
*/
void MemoryTest(void)
{
U32 MemCopyDest ; // Input Memory Test Address
U32 MemTestSize ; // Input Memory Test Size
U32 *src; // Memory source address
U32 *dst; // Memory destination address
int tsize; // Memory Test size
int lsize; // memory test loop size
Print("\n >> Input Memory Test Location(0x1200000) : 0x") ;
MemCopyDest = get_num() ;
if (MemCopyDest==0) MemCopyDest=0x1200000 ;
Print("\n >> Input Memory Test Size(0x10000) : 0x") ;
MemTestSize = get_num() ;
if (MemTestSize==0) MemTestSize=0x500 ;
Print("\n >> Input Memory Test repeat number(0x10000) : 0x") ;
lsize = get_num();
if (lsize==0) lsize=(int)MemTestLoop;
src = (U32 *)MemCopySrc;
dst = (U32 *)MemCopyDest;
tsize = (int)MemTestSize;
Print("\n - Source Memory Location : 0x%08x",src) ;
Print("\r - Destination Memory Location : 0x%08x",dst) ;
Print("\r - Memory Test Size : 0x%08x",tsize) ;
Print("\r - Memory Test Loop Count : %d\r",lsize) ;
if (!MemTest(src,dst,tsize,lsize))
Print("\n $$$ Memory Test Fail !!! ") ;
else Print("\n $$$ Memory Test Success !!! ") ;
}
/*
* Memory word, halfword, byte copy & Compare test
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
int MemTest(U32 *src,U32 *dst,int tsize,int lsize)
{
int i;
int j;
// Word Test
Print("\r ++ Memory Test (Long) .... ");
for (i=0 ; i < lsize ; i++)
{
for (j=0 ; j < 4 ; j++)
{
MemTestInit(src, tsize);
if ( !WordCopyTest(src, dst, tsize) )
{
Print(" Fail !!") ;
return MemTestFail ;
}
PrintMemTestStatus(j) ;
}
}
Print("Ok") ;
// Half Word Test
Print("\r ++ Memory Test (Short) ... ") ;
for (i=0 ; i < lsize ; i++)
{
for (j=0 ; j < 4 ; j++)
{
MemTestInit(src, tsize);
if ( !SWordCopyTest(src, dst, tsize) )
{
Print(" Fail !!") ;
return MemTestFail ;
}
PrintMemTestStatus(j) ;
}
}
Print("Ok") ;
// Byte Test
Print("\r ++ Memory Test (Byte) .... ") ;
for (i=0 ; i < lsize ; i++)
{
for (j=0 ; j < 4 ; j++)
{
MemTestInit(src, tsize);
if ( !ByteCopyTest(src, dst, tsize) )
{
Print(" Fail !!") ;
return MemTestFail ;
}
PrintMemTestStatus(j) ;
}
}
Print("Ok") ;
return MemTestOk ;
}
// Initialize Memory Test, Test Pattern Generation
void MemTestInit(U32 *src, int tsize)
{
U32 *InitSrcAddr;
int cnt ;
int pat ;
InitSrcAddr = src ;
cnt = tsize ;
pat = 0;
while(cnt--) {
*InitSrcAddr++ = (U32)PatternGen(pat++);
}
}
int ByteCopyTest(U32 *src,U32 *dst,int tsize)
{
bcopy(src, dst, tsize) ;
if (!bcomp(src, dst, tsize)) return MemTestFail ;
return MemTestOk ;
}
int SWordCopyTest(U32 *src,U32 *dst,int tsize)
{
scopy(src, dst, tsize) ;
if (!scomp(src, dst, tsize)) return MemTestFail ;
return MemTestOk ;
}
int WordCopyTest(U32 *src,U32 *dst,int tsize)
{
wcopy(src, dst, tsize) ;
if (!wcomp(src, dst, tsize)) return MemTestFail ;
return MemTestOk ;
}
// Byte Copy Function
void bcopy(void *src, void *dst, int tsize)
{
U8 *SrcAddr;
U8 *DstAddr;
int cnt ;
SrcAddr = (U8 *)src ;
DstAddr = (U8 *)dst ;
cnt = tsize;
while(cnt--)
*DstAddr++ = *SrcAddr++ ;
}
// Half Word Copy Function
void scopy(void *src, void *dst, int tsize)
{
U16 *SrcAddr;
U16 *DstAddr;
int cnt ;
SrcAddr = (U16 *)src;
DstAddr = (U16 *)dst;
cnt = tsize ;
while(cnt--)
*DstAddr++ = *SrcAddr++ ;
}
// Word Copy Function
void wcopy(void *src, void *dst, int tsize)
{
U32 *SrcAddr;
U32 *DstAddr;
int cnt ;
SrcAddr = (U32 *)src ;
DstAddr = (U32 *)dst ;
cnt = tsize ;
while(cnt--)
*DstAddr++ = *SrcAddr++ ;
}
// Byte Compare Function
int bcomp(void *src, void *dst, int tsize)
{
U8 *SrcAddr;
U8 *DstAddr;
int cnt ;
SrcAddr = (U8 *)src ;
DstAddr = (U8 *)dst ;
cnt = tsize ;
while(cnt--)
{
if ( *DstAddr != *SrcAddr )
{
Print("\rMemory Byte Test Error") ;
Print("\rSource Data is %02x at Addr %08x",*SrcAddr,SrcAddr) ;
Print("\rDestin Data is %02x at Addr %08x",*DstAddr,DstAddr) ;
return MemTestFail ;
}
DstAddr++;
SrcAddr++;
}
return MemTestOk ;
}
// Half Word Compare Function
int scomp(void *src, void *dst, int tsize)
{
U16 *SrcAddr;
U16 *DstAddr;
int cnt ;
SrcAddr = (U16 *)src ;
DstAddr = (U16 *)dst ;
cnt = tsize ;
while(cnt--)
{
if ( *DstAddr != *SrcAddr )
{
Print("\rMemory Short Test Error") ;
Print("\rSource Data is %04x at Addr %08x",*SrcAddr,SrcAddr) ;
Print("\rDestin Data is %04x at Addr %08x",*DstAddr,DstAddr) ;
return MemTestFail ;
}
DstAddr++;
SrcAddr++;
}
return MemTestOk ;
}
// Word Compare Function
int wcomp(void *src, void *dst, int tsize)
{
U32 *SrcAddr;
U32 *DstAddr;
int cnt ;
SrcAddr = (U32 *)src ;
DstAddr = (U32 *)dst ;
cnt = tsize ;
while(cnt--)
{
if ( *DstAddr != *SrcAddr )
{
Print("\rMemory Long Test Error") ;
Print("\rSource Data is %08x at Addr %08x",*SrcAddr,SrcAddr) ;
Print("\rDestin Data is %08x at Addr %08x",*DstAddr,DstAddr) ;
return MemTestFail ;
}
DstAddr++;
SrcAddr++;
}
return MemTestOk ;
}
// External I/O Bank Test
void ExtIOBankTest(void)
{
U32 *ExtIoBase0, *ExtIoBase1, *ExtIoBase2, *ExtIoBase3 ;
U32 *src ;
U32 ExtIoTestSize = 0xFF0 ;
int lsize = 5 ; // Ext IO test loop count
char TestBankSel ;
EXTACON0 = TCOS0 | TACS0 | TCOH0 | TACC0 ;
EXTACON0 |= TCOS1 | TACS1 | TCOH1 | TACC1 ;
EXTACON1 = TCOS2 | TACS2 | TCOH2 | TACC2 ;
EXTACON1 |= TCOS3 | TACS3 | TCOH3 | TACC3 ;
Print("\n >> External I/O Bank Test <<") ;
ExtIoBase0 = (U32 *)EXTIOBASE ;
ExtIoBase1 = ExtIoBase0 + 4096 ;
ExtIoBase2 = ExtIoBase1 + 4096 ;
ExtIoBase3 = ExtIoBase2 + 4096 ;
Print("\r +- External I/O Bank #0 Base Addr : 0x%x",ExtIoBase0) ;
Print("\r +- External I/O Bank #1 Base Addr : 0x%x",ExtIoBase1) ;
Print("\r +- External I/O Bank #2 Base Addr : 0x%x",ExtIoBase2) ;
Print("\r +- External I/O Bank #3 Base Addr : 0x%x",ExtIoBase3) ;
src = (U32 *)MemCopySrc;
Print("\n $$ Select Test External I/O Bank (0,1,2,3,Q) ? ") ;
TestBankSel = get_byte() ;
if (TestBankSel == '0' )
{
Print("\n <<>> External I/O Bank #0 Test ") ;
MemTest(src,ExtIoBase0,ExtIoTestSize,lsize);
}
else if (TestBankSel == '1' )
{
Print("\n <<>> External I/O Bank #1 Test ") ;
MemTest(src,ExtIoBase1,ExtIoTestSize,lsize);
}
else if (TestBankSel == '2' )
{
Print("\n <<>> External I/O Bank #2 Test ") ;
MemTest(src,ExtIoBase2,ExtIoTestSize,lsize);
}
else if (TestBankSel == '3' )
{
Print("\n <<>> External I/O Bank #3 Test ") ;
MemTest(src,ExtIoBase3,ExtIoTestSize,lsize);
}
else Print("\r +- Invalid External I/O Bank Selected") ;
}
// Print Memory Test Status
void PrintMemTestStatus(int j)
{
if (j==0) PrintStatus(0xE,'/') ;
else if (j==1) PrintStatus(0xD,'-') ;
else if (j==2) PrintStatus(0xB,'\\') ;
else PrintStatus(0x7,'|') ;
}
int PatternGen(unsigned int seed)
{
if(!(seed%0x100)) srand(seed);
return(rand());
}
// Allocation for NonCacheable Area
void *nmalloc(unsigned nbyte)
/*Very simple; Use nmalloc() & free() like Stack*/
{
void *returnPt=nmallocPt;
nmallocPt= (int *)nmallocPt+nbyte/4+((nbyte%4)>0);
if( (int)nmallocPt > AllocNonCacheAreaEnd)return NULL;
return returnPt;
}
// Allocation for Cacheable Area
/*
* Function : *malloc
* Input : Number of byte for necessary
* Output : Start pointer of allocated memory location.
*/
void *malloc(unsigned nbyte)
{
void *returnPt=mallocPt;
mallocPt= (int *)mallocPt+nbyte/4+((nbyte%4)>0);
if( (int)mallocPt > HEAPEND)return NULL;
return returnPt;
}
/*
* Function : free
* Input : Start pointer for freeze memory location
* Output : No
*/
void free(void *pt)
{
mallocPt=pt;
}
void nmfree(void *pt)
{
nmallocPt=pt;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -