all repos — mgba @ 9c030fb55315162419cfbde1229860a95a988426

mGBA Game Boy Advance Emulator

src/platform/3ds/ctru-heap.c (view raw)

 1/* This code is mostly from ctrulib, which contains the following license:
 2
 3	This software is provided 'as-is', without any express or implied
 4	warranty. In no event will the authors be held liable for any damages
 5	arising from the use of this software.
 6
 7	Permission is granted to anyone to use this software for any purpose,
 8	including commercial applications, and to alter it and redistribute it
 9	freely, subject to the following restrictions:
10
11	* The origin of this software must not be misrepresented; you must not
12	  claim that you wrote the original software. If you use this software in
13	  a product, an acknowledgment in the product documentation would be
14	  appreciated but is not required.
15
16	* Altered source versions must be plainly marked as such, and must not be
17	  misrepresented as being the original software.
18
19	* This notice may not be removed or altered from any source distribution.
20*/
21
22#include <3ds/types.h>
23#include <3ds/srv.h>
24#include <3ds/gfx.h>
25#include <3ds/sdmc.h>
26#include <3ds/services/apt.h>
27#include <3ds/services/fs.h>
28#include <3ds/services/hid.h>
29#include <3ds/svc.h>
30
31#include "util/common.h"
32
33extern char* fake_heap_start;
34extern char* fake_heap_end;
35extern u32 __ctru_linear_heap;
36extern u32 __ctru_heap;
37extern u32 __ctru_heap_size;
38extern u32 __ctru_linear_heap_size;
39static u32 __custom_heap_size = 0x02400000;
40static u32 __custom_linear_heap_size = 0x01400000;
41
42uint32_t* romBuffer = NULL;
43size_t romBufferSize;
44
45FS_Archive sdmcArchive;
46
47bool allocateRomBuffer(void) {
48	if (romBuffer) {
49		return true;
50	}
51	romBuffer = malloc(0x02000000);
52	if (romBuffer) {
53		romBufferSize = 0x02000000;
54		return true;
55	}
56	romBuffer = malloc(0x01000000);
57	if (romBuffer) {
58		romBufferSize = 0x01000000;
59		return true;
60	}
61	return false;
62}
63
64void __system_allocateHeaps() {
65	u32 tmp=0;
66
67	__ctru_heap_size = __custom_heap_size;
68	__ctru_linear_heap_size = __custom_linear_heap_size;
69
70	// Allocate the application heap
71	__ctru_heap = 0x08000000;
72	svcControlMemory(&tmp, __ctru_heap, 0x0, __ctru_heap_size, MEMOP_ALLOC, MEMPERM_READ | MEMPERM_WRITE);
73
74	// Allocate the linear heap
75	svcControlMemory(&__ctru_linear_heap, 0x0, 0x0, __ctru_linear_heap_size, MEMOP_ALLOC_LINEAR, MEMPERM_READ | MEMPERM_WRITE);
76	// Set up newlib heap
77	fake_heap_start = (char*)__ctru_heap;
78	fake_heap_end = fake_heap_start + __ctru_heap_size;
79}
80
81void __appInit(void) {
82	// Initialize services
83	srvInit();
84	aptInit();
85	hidInit();
86
87	fsInit();
88	sdmcInit();
89
90	FSUSER_OpenArchive(&sdmcArchive, ARCHIVE_SDMC, fsMakePath(PATH_EMPTY, ""));
91	allocateRomBuffer();
92}