qipp
2026-03-18 fd0b5f94939d30fe65a98f9111aee2987d12e41f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * memory.c
 *
 *  Created on: 2021Äê12ÔÂ25ÈÕ
 *      Author: graydon
 */
 
#include <stdio.h>
#include <stdlib_21xxx.h>
 
#include <heapnew>
#include <string.h>
#include <interrupt.h>
#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