bool StartROM(char* path) { char temppath[300]; Result res; // load the ROM strncpy(temppath, "/snes/", 6); strncpy(&temppath[6], path, 0x106); temppath[6+0x106] = '\0'; bprintf("Loading %s...\n", temppath); if (!SNES_LoadROM(temppath)) return false; running = 1; framecount = 0; CPU_Reset(); SPC_Reset(); // SPC700 thread (running on syscore) res = svcCreateThread(&spcthread, SPCThread, 0, (u32*)(spcthreadstack+0x400), 0x3F, 1); if (res) { bprintf("Failed to create SPC700 thread:\n -> %08X\n", res); spcthread = NULL; } bprintf("ROM loaded, running...\n"); return true; }
Result aptInit(void) { Result ret=0; if (aptInitialised) return ret; aptStatusMutex = 0; // Initialize APT stuff, escape load screen. ret = __apt_initservicehandle(); if(ret!=0)return ret; if((ret=APT_GetLockHandle(&aptuHandle, 0x0, &aptLockHandle)))return ret; svcCloseHandle(aptuHandle); currentAppId = __apt_appid; svcCreateEvent(&aptStatusEvent, 0); svcCreateEvent(&aptSleepSync, 0); if(!aptIsCrippled()) { aptOpenSession(); if((ret=APT_Initialize(NULL, currentAppId, &aptEvents[0], &aptEvents[1])))return ret; aptCloseSession(); aptOpenSession(); if((ret=APT_Enable(NULL, 0x0)))return ret; aptCloseSession(); // create APT close event svcCreateEvent(&aptEvents[2], 0); // After a cycle of APT_Finalize+APT_Initialize APT thinks the // application is suspended, so we need to tell it to unsuspend us. if (aptIsReinit()) { aptOpenSession(); APT_PrepareToJumpToApplication(NULL, 0x0); aptCloseSession(); aptOpenSession(); APT_JumpToApplication(NULL, 0x0, 0x0, 0x0); aptCloseSession(); } aptOpenSession(); if((ret=APT_NotifyToWait(NULL, currentAppId)))return ret; aptCloseSession(); // create APT event handler thread svcCreateThread(&aptEventHandlerThread, aptEventHandler, 0x0, (u32*)(&aptEventHandlerStack[APT_HANDLER_STACKSIZE/8]), 0x31, 0xfffffffe); } else aptAppStarted(); aptInitialised = true; return 0; }
Result ndspInit(void) { Result rc = 0; if (AtomicPostIncrement(&ndspRefCount)) return 0; if (!componentBin && !ndspFindAndLoadComponent()) { rc = MAKERESULT(RL_PERMANENT, RS_NOTFOUND, 41, RD_NOT_FOUND); goto _fail0; } LightLock_Init(&ndspMutex); ndspInitMaster(); ndspiInitChn(); rc = cfguInit(); if (R_SUCCEEDED(rc)) { u8 outMode; rc = CFGU_GetConfigInfoBlk2(sizeof(outMode), 0x70001, &outMode); if (R_SUCCEEDED(rc)) ndspMaster.outputMode = outMode; cfguExit(); } rc = dspInit(); if (R_FAILED(rc)) return rc; rc = ndspInitialize(false); if (R_FAILED(rc)) goto _fail1; rc = svcCreateEvent(&sleepEvent, 0); if (R_FAILED(rc)) goto _fail2; rc = svcCreateThread(&ndspThread, ndspThreadMain, 0x0, (u32*)(&ndspThreadStack[NDSP_THREAD_STACK_SIZE/8]), 0x18, 1); if (R_FAILED(rc)) goto _fail3; aptHook(&aptCookie, ndspAptHook, NULL); return 0; _fail3: svcCloseHandle(sleepEvent); _fail2: ndspFinalize(false); _fail1: dspExit(); if (componentFree) { free((void*)componentBin); componentBin = NULL; } _fail0: AtomicDecrement(&ndspRefCount); return rc; }
int main(int argc, char** argv) { gfxInitDefault(); consoleInit(GFX_TOP, NULL); svcCreateEvent(&threadRequest,0); u32 *threadStack = memalign(32, STACKSIZE); Result ret = svcCreateThread(&threadHandle, threadMain, 0, &threadStack[STACKSIZE/4], 0x3f, 0); printf("thread create returned %x\n", ret); // Main loop while (aptMainLoop()) { gspWaitForVBlank(); hidScanInput(); printf("\x1b[5;0H"); printf("thread counter = %d\n",threadcount); u32 kDown = hidKeysDown(); if (kDown & KEY_START) break; // break in order to return to hbmenu if (kDown & KEY_A) svcSignalEvent(threadRequest); // Flush and swap framebuffers gfxFlushBuffers(); gfxSwapBuffers(); } // tell thread to exit threadExit = true; // signal the thread svcSignalEvent(threadRequest); // give it time to exit svcSleepThread(10000000ULL); // close handles and free allocated stack svcCloseHandle(threadRequest); svcCloseHandle(threadHandle); free(threadStack); gfxExit(); return 0; }
void initPhysicsThread(physicsThread_s* p) { if(!p)return; initRequestQueue(&p->privateList); initRequestQueue(&p->requestList); p->exit=false; svcCreateMutex(&p->requestMutex, false); Result val = svcCreateThread(&p->thread, physicsThreadMain, (u32)p, (u32*)&p->stack[PHYSICSTHREAD_STACKSIZE/8], 0x18, 1); printf("%08X (%08X)\n",(unsigned int)val,(unsigned int)p->thread); if(val) { //thread creation failed ! what do we do ?! } }
Thread threadCreate(ThreadFunc entrypoint, void* arg, size_t stack_size, int prio, int affinity, bool detached) { size_t stackoffset = (sizeof(struct Thread_tag)+7)&~7; size_t allocsize = stackoffset + ((stack_size+7)&~7); size_t tlssize = __tls_end-__tls_start; size_t tlsloadsize = __tdata_lma_end-__tdata_lma; size_t tbsssize = tlssize-tlsloadsize; // Guard against overflow if (allocsize < stackoffset) return NULL; if ((allocsize-stackoffset) < stack_size) return NULL; if ((allocsize+tlssize) < allocsize) return NULL; Thread t = (Thread)memalign(8,allocsize+tlssize); if (!t) return NULL; t->ep = entrypoint; t->arg = arg; t->detached = detached; t->finished = false; t->stacktop = (u8*)t + allocsize; if (tlsloadsize) memcpy(t->stacktop, __tdata_lma, tlsloadsize); if (tbsssize) memset((u8*)t->stacktop+tlsloadsize, 0, tbsssize); // Set up child thread's reent struct, inheriting standard file handles _REENT_INIT_PTR(&t->reent); struct _reent* cur = getThreadVars()->reent; t->reent._stdin = cur->_stdin; t->reent._stdout = cur->_stdout; t->reent._stderr = cur->_stderr; Result rc; rc = svcCreateThread(&t->handle, _thread_begin, (u32)t, (u32*)t->stacktop, prio, affinity); if (R_FAILED(rc)) { free(t); return NULL; } return t; }
Result aptInit(void) { Result ret=0; if (aptInitialised) return ret; // Initialize APT stuff, escape load screen. ret = __apt_initservicehandle(); if(ret!=0)return ret; if((ret=APT_GetLockHandle(&aptuHandle, 0x0, &aptLockHandle)))return ret; svcCloseHandle(aptuHandle); currentAppId = __apt_appid; svcCreateEvent(&aptStatusEvent, 0); svcCreateEvent(&aptSleepSync, 0); if(!(__system_runflags&RUNFLAG_APTWORKAROUND)) { aptOpenSession(); if((ret=APT_Initialize(NULL, currentAppId, &aptEvents[0], &aptEvents[1])))return ret; aptCloseSession(); aptOpenSession(); if((ret=APT_Enable(NULL, 0x0)))return ret; aptCloseSession(); aptOpenSession(); if((ret=APT_NotifyToWait(NULL, currentAppId)))return ret; aptCloseSession(); // create APT event handler thread svcCreateThread(&aptEventHandlerThread, aptEventHandler, 0x0, (u32*)(&aptEventHandlerStack[APT_HANDLER_STACKSIZE/8]), 0x31, 0xfffffffe); } else aptAppStarted(); aptInitialised = true; return 0; }
void Thread::launch() { wait(); svcCreateThread(&m_thread, &Thread::entryPoint, (u32)this, &threadStack[THREAD_STACK_SIZE/sizeof(u32)], 0x3F, 0); }
/*----------------*/ int main(int argc, char *argv[]) { APP_STATUS status; srvInit(); aptInit(APPID_APPLICATION); gfxInit(); hidInit(NULL); fsInit(); svcCreateEvent(&new_cmd_event, 0); svcCreateEvent(&cmd_done_event, 0); svcCreateThread(&thread, cmd_thread_func, 0x0, (u32*)((char*)thread_stack + sizeof(thread_stack)), 0x31, 0xfffffffe); int where = 0; u32 ret = SOC_Initialize((u32*)0x08000000, 0x48000); if(ret == 0) { listen_socket = socket(AF_INET, SOCK_STREAM, 0); if(listen_socket == -1) { where = 1; ret = SOC_GetErrno(); } else { u32 tmp = fcntl(listen_socket, F_GETFL); fcntl(listen_socket, F_SETFL, tmp | O_NONBLOCK); struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = __swap16(PORT); addr.sin_addr.s_addr = INADDR_ANY; ret = bind(listen_socket, (struct sockaddr *)&addr, sizeof(addr)); if(ret != 0) { where = 2; ret = SOC_GetErrno(); } else { ret = listen(listen_socket, 1); if(ret == -1) { ret = SOC_GetErrno(); where = 3; } } } } u32 it = 0; int accept_errno = 0; int first = 1; while((status = aptGetStatus()) != APP_EXITING) { hidScanInput(); consoleClear(&top); print(&top, "newver\n"); print(&top, "ret: %08x, where: %d\n", ret, where); print(&top, "frame: %08x\n", it); u32 ip = gethostid(); print(&top, "ip: %d.%d.%d.%d\n", ip & 0xFF, (ip>>8)&0xFF, (ip>>16)&0xFF, (ip>>24)&0xFF); if(accept_errno != 0) print(&top, "accept returned errno %d\n", accept_errno); if(!first) { int s = accept(listen_socket, NULL, NULL); if(s == -1) { int err = SOC_GetErrno(); if(err != -EWOULDBLOCK) accept_errno = err; } else { sock = s; conn_main(); closesocket(sock); } } it++; first = 0; if(enable_draw) renderFrame(); u32 keys = hidKeysUp(); if(keys & KEY_A) break; } thread_exit = 1; svcSignalEvent(new_cmd_event); svcWaitSynchronization(thread, U64_MAX); svcCloseHandle(thread); svcCloseHandle(new_cmd_event); svcCloseHandle(cmd_done_event); SOC_Shutdown(); fsExit(); hidExit(); gfxExit(); aptExit(); srvExit(); return 0; }
int main(int argc, char** argv) { gfxInitDefault(); consoleInit(GFX_TOP, NULL); Handle threadRequest; svcCreateEvent(&threadRequest, 0); Handle threadHandle; volatile bool threadShouldExit = false; volatile int threadCount = 0; ThreadArgs threadArgs = { &threadRequest, &threadShouldExit, &threadCount }; const u32 STACKSIZE_BYTES = 4 * 1024; u32 *const threadStack = memalign(32, STACKSIZE_BYTES); const s32 threadPriority = 0x3f; const s32 threadProcessorId = 0; const Result ret = svcCreateThread(&threadHandle, threadMain, (u32)&threadArgs, &threadStack[STACKSIZE_BYTES / 4], threadPriority, threadProcessorId); printf("thread create returned %x\n", (unsigned int)ret); // Loop until the user signals an exit from the program. while (aptMainLoop()) { gspWaitForVBlank(); hidScanInput(); printf("\x1b[5;0H"); printf("thread counter = %d\n", threadCount); u32 kDown = hidKeysDown(); if (kDown & KEY_START) break; // Break to exit to the launcher. if (kDown & KEY_A) svcSignalEvent(threadRequest); // Flush and swap framebuffers. gfxFlushBuffers(); gfxSwapBuffers(); } // Tell thread to exit. threadShouldExit = true; // Signal the thread so that it can run to the exit condition check. svcSignalEvent(threadRequest); // Give the thread time to exit. svcSleepThread(10000000ULL); // Close thread and event handles and free the allocated stack. svcCloseHandle(threadRequest); svcCloseHandle(threadHandle); free(threadStack); gfxExit(); return 0; }
int main() { touchPosition touch; sf2d_init(); sftd_init(); sftd_font *text = sftd_load_font_mem(Roboto_ttf, Roboto_ttf_size); sftd_font *title = sftd_load_font_mem(RobotoThin_ttf, RobotoThin_ttf_size); sf2d_texture *logo = sfil_load_PNG_buffer(logo_png, SF2D_PLACE_RAM); sf2d_texture *record = sfil_load_PNG_buffer(record_png, SF2D_PLACE_RAM); sf2d_texture *stop = sfil_load_PNG_buffer(stop_png, SF2D_PLACE_RAM); sf2d_set_clear_color(RGBA8(0xFA, 0xFA, 0xFA, 0xFF)); sharedmem = (u32*)memalign(0x1000, sharedmem_size); audiobuf = linearAlloc(audiobuf_size); MIC_Initialize(sharedmem, sharedmem_size, control, 0, 3, 1, 1);//See mic.h. // Threading stuff svcCreateEvent(&threadRequest,0); u32 *threadStack = memalign(32, STACKSIZE); svcCreateThread(&threadHandle, threadMic, 0, &threadStack[STACKSIZE/4], 0x3f, 0); while(aptMainLoop()) { hidScanInput(); hidTouchRead(&touch); u32 kDown = hidKeysDown(); if (kDown & KEY_START) break; // break in order to return to hbmenu sf2d_start_frame(GFX_TOP, GFX_LEFT); sf2d_draw_texture(logo, 60, 70); sftd_draw_text(title, 177, 80, RGBA8(0, 0, 0, 222), 40, "Audio"); sftd_draw_text(title, 175, 120, RGBA8(0, 0, 0, 222), 40, "Recorder"); sf2d_end_frame(); sf2d_start_frame(GFX_BOTTOM, GFX_LEFT); sf2d_draw_texture(record, 85, 85); sf2d_draw_texture(stop, 165, 85); sf2d_end_frame(); svcSignalEvent(threadRequest); if(print == 1) { sf2d_start_frame(GFX_TOP, GFX_LEFT); sf2d_draw_texture(logo, 60, 70); sftd_draw_text(title, 177, 80, RGBA8(0, 0, 0, 222), 40, "Audio"); sftd_draw_text(title, 175, 120, RGBA8(0, 0, 0, 222), 40, "Recorder"); sftd_draw_text(text, 130, 209, RGBA8(0, 0, 0, 222), 16, "Recording audio..."); sf2d_end_frame(); } if(recording == 2) { sf2d_start_frame(GFX_TOP, GFX_LEFT); sf2d_draw_texture(logo, 60, 70); sftd_draw_text(title, 177, 80, RGBA8(0, 0, 0, 222), 40, "Audio"); sftd_draw_text(title, 175, 120, RGBA8(0, 0, 0, 222), 40, "Recorder"); sftd_draw_text(text, 130, 209, RGBA8(0, 0, 0, 222), 16, "Saving audio..."); sf2d_end_frame(); } sf2d_swapbuffers(); } MIC_Shutdown(); sftd_free_font(text); sftd_free_font(title); sf2d_free_texture(logo); sf2d_free_texture(record); sf2d_free_texture(stop); // tell thread to exit threadExit = true; // signal the thread svcSignalEvent(threadRequest); // give it time to exit svcSleepThread(10000000ULL); // close handles and free allocated stack svcCloseHandle(threadRequest); svcCloseHandle(threadHandle); free(threadStack); free(sharedmem); linearFree(audiobuf); linearFree(nomute_audiobuf); sf2d_fini(); sftd_fini(); return 0; }
int main() { gfxInitDefault(); consoleInit(GFX_TOP, &LuaBox_MainConsole); // Initialize console LuaBox_Running = 1; LuaBox_EventList = NULL; // Clear event list svcCreateMutex(&LuaBox_ConsoleMutex, 0); // Create the console mutex svcCreateMutex(&LuaBox_EventMutex, 0); // Create event list mutex svcCreateEvent(&LuaBox_EventSignal, 0); // Create event list mutex printf("%s version %s\ninitializing lua state...\n", LUABOX_NAME, LUABOX_VERSION); L = luaL_newstate(); luaL_openlibs(L); // Install our apis lua_newtable(L); lua_pushstring(L, "exists"); lua_pushcfunction(L, Api_fs_exists); lua_settable(L, -3); lua_setglobal(L, "fs"); // Check if boot.lua exists FILE* fp = fopen("lua/bios.lua", "r"); if(fp == NULL) { printf("lua/bios.lua does not exist\nplease create and place lua code in it for this to be useful.\npress start to exit.\n"); while(1) { gspWaitForVBlank(); hidScanInput(); u32 kDown = hidKeysDown(); if (kDown & KEY_START) break; // break in order to return to hbmenu // Flush and swap framebuffers gfxFlushBuffers(); gfxSwapBuffers(); } gfxExit(); return 0; } fclose(fp); printf("initialized.\ninitializing keyboard...\n"); SoftKb_Setup(GFX_BOTTOM, 4, 0); SoftKb_Draw(); printf("initialized. use dpad and A button to type.\n"); // Start lua thread. printf("starting lua thread...\n"); Handle threadHandle; u32 *threadStack = memalign(32, LUABOX_THREAD_STACKSIZE); svcCreateThread(&threadHandle, LuaThread, 0, &threadStack[LUABOX_THREAD_STACKSIZE/4], 0x3f, 0); // Main loop while(LuaBox_Running) { gspWaitForVBlank(); hidScanInput(); u32 kDown = hidKeysDown(); char out = SoftKb_Handle(kDown); if(out >= 32 || out == 10 || out == 7) { LuaBox_PushEvent(LUABOX_EVENT_CHAR, out); } if (kDown & KEY_START) break; // break in order to return to hbmenu // Flush and swap framebuffers gfxFlushBuffers(); gfxSwapBuffers(); } gfxExit(); return 0; }