/* * memory.c * * Created on: 2021Äê12ÔÂ25ÈÕ * Author: graydon */ #include #include #include #include #include #include "memory.h" #include "config.h" //#define INTERNALBUFFER_SIZE1 (20*1024-128) //static int mInternalBuffer1 = 0x000B9000; #define INTERNALBUFFER_SIZE2 (24*1024-128) static int mInternalBuffer2 = 0x000C2000; static int mSRAMIndex[SRAM_CNT] = {0, 1, 2, 3, 4}; static int mL2_Index = 4; static int L2_block0_bankbase = 0x00602c00; void * operator new( size_t size ) { void* ptr = NULL; // ptr = heap_malloc(mSRAMIndex[SRAM_CM],size); // if(ptr){ // return ptr; // } ptr = heap_malloc(mSRAMIndex[SRAM_DM],size); if(ptr){ return ptr; } ptr = heap_malloc(mSRAMIndex[SRAM_PM],size); if(ptr){ return ptr; } ptr = heap_malloc(mSRAMIndex[SRAM_DDR],size); if(ptr){ return ptr; } ptr = heap_malloc(mSRAMIndex[SRAM_L2], size); if(ptr){ return ptr; } return NULL; } void * operator new(size_t size,int heapID) { void* ptr = NULL; if((heapID < 0 || heapID > 2) && heapID != 4) { return NULL; } ptr = heap_malloc(mSRAMIndex[heapID],size); return ptr; } void operator delete( void *ptr) { free(ptr); } extern "C" void* sqe_alloc(int size) { void* p = new int[size]; memset(p, 0 , size); return p; } extern "C" void sqe_free (void *ptr) { delete[] ptr; } #ifdef __cplusplus extern "C" { #endif int sram_init() { int i; int passed = 1; mSRAMIndex[SRAM_DM] = heap_lookup(0); //mSRAMIndex[SRAM_PM] = heap_lookup(1); mSRAMIndex[SRAM_PM] = heap_install((void*)mInternalBuffer2,INTERNALBUFFER_SIZE2, 1); mSRAMIndex[SRAM_DDR] = heap_lookup(2); //mSRAMIndex[SRAM_CM] = heap_install((void*)mInternalBuffer1,INTERNALBUFFER_SIZE1, 3); mSRAMIndex[SRAM_L2] = heap_lookup(4); dbg_printf("Space on default heap in internal memory: %d words\n" , space_unused()); dbg_printf("Space on custom heap in internal memory: %d words\n" , heap_space_unused(mSRAMIndex[SRAM_PM])); dbg_printf("Space on custom heap in external memory: %d words\n " , heap_space_unused(mSRAMIndex[SRAM_DDR])); dbg_printf("Space on custom heap in block1 memory: %d words\n " , heap_space_unused(mSRAMIndex[SRAM_CM])); //L2 Test //int *memtest = (int*)malloc(19*1024); //int *memtest2 = (int*)heap_malloc(mSRAMIndex[SRAM_PM],19*1024);//new int[19*1024];// int* source = (int*)DDRMalloc(127*1024); int* source1 = new(4) int[1024*60]; for(i=0;i < 60*1024;i ++) { source[i] = i; source1[i] = i; if(source[i] != i || source1[i] != i) { passed =0; break; } // if(i < 19*1024){ // memtest2[i] = i; // if(memtest2[i] != i){ // passed = 0; // break; // } // } /*if(source1[i] % 1024 == 0){ printf("%d\n",source1[i]); }*/ } DDRFree(source); delete[] source1; return passed; } void* sram_malloc(MemoryType memtype , int size) { void* p = NULL; if(memtype >= SRAM_CNT) { return NULL; } if(memtype == SRAM_AUTO){ // p = heap_malloc(mSRAMIndex[SRAM_CM],size); // if(p){ // return p; // } p = heap_malloc(mSRAMIndex[SRAM_DM],size); if(p){ return p; } p = heap_malloc(mSRAMIndex[SRAM_PM],size); if(p){ return p; } p = heap_malloc(mSRAMIndex[SRAM_DDR],size); if(p){ return p; } p = heap_malloc(mSRAMIndex[SRAM_L2], size); if(p){ return p; } } else{ p = heap_malloc(mSRAMIndex[memtype],size); } return p; } void sram_free(MemoryType memtype ,void* ptr) { if(memtype < SRAM_CNT) { if(memtype == SRAM_AUTO){ free(ptr); } else{ heap_free(mSRAMIndex[memtype], ptr); } } } int sram_free_space(MemoryType memtype) { int size = 0; if(memtype >= SRAM_CNT) { return 0; } size = heap_space_unused(mSRAMIndex[memtype]); return size; } int sram_unused_space() { int size = 0; size += heap_space_unused(mSRAMIndex[SRAM_DM]); size += heap_space_unused(mSRAMIndex[SRAM_PM]); size += heap_space_unused(mSRAMIndex[SRAM_CM]); return size; } void* DDRMalloc(int size) { //return sram_malloc(SRAM_DDR, size); void *p = NULL; p = heap_malloc(mSRAMIndex[SRAM_L2],size); if(p == NULL){ p = heap_malloc(mSRAMIndex[SRAM_DDR],size); } return p; } void DDRFree(void* ptr) { //sram_free(SRAM_DDR, ptr); free(ptr); } int DDRUnUsedSpace() { return sram_free_space(SRAM_DDR)+sram_free_space(SRAM_L2); } #ifdef __cplusplus } #endif