1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "backends/platform/3ds/osystem.h"
24 #include "backends/plugins/3ds/3ds-provider.h"
25 
26 #include <3ds.h>
27 #include <malloc.h>
28 
29 enum {
30 	SYSTEM_MODEL_2DS = 3
31 };
32 
33 struct CommandLine {
34 	int argumentCount;
35 	char** argumentsValue;
36 
CommandLineCommandLine37 	CommandLine(int argc, char** argv): argumentCount(argc), argumentsValue(argv) {}
38 };
39 
mainThreadFunc(void * threadParams)40 static void mainThreadFunc(void *threadParams) {
41 	g_system = new N3DS::OSystem_3DS();
42 	assert(g_system);
43 
44 #ifdef DYNAMIC_MODULES
45 	PluginManager::instance().addPluginProvider(new CTRPluginProvider());
46 #endif
47 
48 	CommandLine *commandLine = static_cast<CommandLine *>(threadParams);
49 	int res = scummvm_main(commandLine->argumentCount, commandLine->argumentsValue);
50 
51 	g_system->destroy();
52 
53 	threadExit(res);
54 };
55 
main(int argc,char * argv[])56 int main(int argc, char *argv[]) {
57 	// Initialize basic libctru stuff
58 	cfguInit();
59 	gfxInitDefault();
60 
61 	// 800px wide top screen is not available on old 2DS systems
62 	u8 systemModel = 0;
63 	CFGU_GetSystemModel(&systemModel);
64 	gfxSetWide(systemModel != SYSTEM_MODEL_2DS);
65 
66 	romfsInit();
67 	osSetSpeedupEnable(true);
68 // 	consoleInit(GFX_TOP, NULL);
69 	gdbHioDevInit();
70 	gdbHioDevRedirectStdStreams(true, true, true);
71 
72 #ifdef USE_LIBCURL
73 	const uint32 soc_sharedmem_size = 0x10000;
74 	void *soc_sharedmem = memalign(0x1000, soc_sharedmem_size);
75 	socInit((u32 *)soc_sharedmem, soc_sharedmem_size);
76 #endif
77 
78 	// Start ScummVM in a separate thread to be able to set the stack size.
79 	// The default stack is not large enough.
80 	CommandLine commandLine(argc, argv);
81 
82 	s32 mainThreadPriority = 0;
83 	svcGetThreadPriority(&mainThreadPriority, CUR_THREAD_HANDLE);
84 
85 	Thread mainThread = threadCreate(&mainThreadFunc, &commandLine, 64 * 1024, mainThreadPriority, -2, false);
86 	threadJoin(mainThread, U64_MAX);
87 	int res = threadGetExitCode(mainThread);
88 	threadFree(mainThread);
89 
90 	// Turn on both screen backlights before exiting.
91 	if (R_SUCCEEDED(gspLcdInit())) {
92 		GSPLCD_PowerOnBacklight(GSPLCD_SCREEN_BOTH);
93 		gspLcdExit();
94 	}
95 
96 #ifdef USE_LIBCURL
97 	socExit();
98 #endif
99 	gdbHioDevExit();
100 	romfsExit();
101 	gfxExit();
102 	cfguExit();
103 	return res;
104 }
105