void setClockrate(u8 setting) { int j, i; u32* patchArea = linearAlloc(0x00100000); if(setting != 0) setting = 3; // grab waitLoop stub GSPGPU_FlushDataCache(NULL, (u8*)patchArea, 0x100); gspwn(patchArea, (u32*)(MENU_LOADEDROP_BUFADR-0x100), 0x100); svcSleepThread(20*1000*1000); // patch it for(i=0; i<0x100/4; i++) { if(patchArea[i] == 0x67666E63) // "cnfg" { patchArea[i+1] = (patchArea[i+1] & ~0xFF) | setting; break; } } // copy it back GSPGPU_FlushDataCache(NULL, (u8*)patchArea, 0x100); gspwn((u32*)(MENU_LOADEDROP_BUFADR-0x100), patchArea, 0x100); svcSleepThread(20*1000*1000); // ghetto dcache invalidation // don't judge me for(j=0; j<4; j++) for(i=0; i<0x00100000/0x4; i+=0x4) patchArea[i+j]^=0xDEADBABE; linearFree(patchArea); }
void physicsThreadMain(u32 arg) { physicsThread_s* p=(physicsThread_s*)arg; while(!p->exit) { svcWaitSynchronization(p->requestMutex, U64_MAX); appendRequestQueue(&p->privateList, &p->requestList); svcReleaseMutex(p->requestMutex); // bool debug=false; // u64 val=svcGetSystemTick(); request_s* r=NULL; while((r=unqueueRequest(&p->privateList)) && !p->exit) { handleRequest(p, r); svcSleepThread(1000); } // if(debug)print("%d ticks\n",(int)(svcGetSystemTick()-val)); updateOBBs(); svcSleepThread(1000000); } svcExitThread(); }
static void titles_update(ui_view* view, void* data, linked_list* items, list_item* selected, bool selectedTouched) { titles_data* listData = (titles_data*) data; if(hidKeysDown() & KEY_B) { if(!listData->populateData.finished) { svcSignalEvent(listData->populateData.cancelEvent); while(!listData->populateData.finished) { svcSleepThread(1000000); } } ui_pop(); task_clear_titles(items); list_destroy(view); free(listData); return; } if(hidKeysDown() & KEY_SELECT) { titles_options_open(listData); return; } if(!listData->populated || (hidKeysDown() & KEY_X)) { if(!listData->populateData.finished) { svcSignalEvent(listData->populateData.cancelEvent); while(!listData->populateData.finished) { svcSleepThread(1000000); } } listData->populateData.items = items; Result res = task_populate_titles(&listData->populateData); if(R_FAILED(res)) { error_display_res(NULL, NULL, res, "Failed to initiate title list population."); } listData->populated = true; } if(listData->populateData.finished && R_FAILED(listData->populateData.result)) { error_display_res(NULL, NULL, listData->populateData.result, "Failed to populate title list."); listData->populateData.result = 0; } if(selected != NULL && selected->data != NULL && (selectedTouched || (hidKeysDown() & KEY_A))) { titles_action_open(items, selected); return; } }
Result srvSysInit() { Result rc = 0; if (!initLockinit) { RecursiveLock_Init(&initLock); } RecursiveLock_Lock(&initLock); if (srvRefCount > 0) { RecursiveLock_Unlock(&initLock); return MAKERESULT(RL_INFO, RS_NOP, 25, RD_ALREADY_INITIALIZED); } while (1) { rc = svcConnectToPort(&srvHandle, "srv:"); if (R_LEVEL(rc) != RL_PERMANENT || R_SUMMARY(rc) != RS_NOTFOUND || R_DESCRIPTION(rc) != RD_NOT_FOUND) break; svcSleepThread(500000); } if (R_SUCCEEDED(rc)) { rc = srvSysRegisterClient(); srvRefCount++; } RecursiveLock_Unlock(&initLock); return rc; }
void* ctr_rend_buffer_copy_stride(void *p, int count, int size, int stride) { int len = size * count; int cb = (len + CTR_REND_BUFFER_ALIGN) & (~CTR_REND_BUFFER_ALIGN); if (ctr_state.buffer_pos + cb > CTR_REND_BUFFER_MAX) { printf("-------------\n\nctr_rend_buffer_copy_stride Out of Memory\n\n-------------\n"); ctr_buf_print(cb); do { svcSleepThread(20000); } while (1); return 0; } ctr_buf_cb(cb); GLubyte *addr = &ctr_state.buffer[ctr_state.buffer_pos]; u8 *src = p; u8 *dst = addr; int i,j; //printf("copy: %08x %d %d %d\n", p, count, size, stride); for (i = 0; i < count; i++) { memcpy(dst, src, size); //for (j = 0; j < size; j++) { // printf("%02x", src[j]); //} //printf("%s", (i&1) ? "\n" : " "); src += stride; dst += size; } //if(i&1) printf("\n"); ctr_state.buffer_pos += cb; return addr; }
void wait_for_input(void) { printf("\n\nPress Start.\n\n"); fflush(stdout); while(aptMainLoop()) { u32 kDown; hidScanInput(); kDown = hidKeysDown(); if (kDown & KEY_START) break; if (kDown & KEY_SELECT) exit(0); #if 0 select_pressed = true; #endif svcSleepThread(1000000); } }
void exitServices() { if (titlemenuIsUpdating) { //Stop the title menu loading process, causing the thread to exit cancelTitleLoading(); //Wait a little bit (two seconds) longer to allow the thread to actually terminate svcSleepThread(2000000000ULL); } if (titleThreadNeedsRelease) { releaseTitleThread(); } // cleanup whatever we have to cleanup audio_stop(); csndExit(); freeThemeImages(); netloader_exit(); titlesExit(); ptmuExit(); acExit(); hidExit(); gfxExit(); closeSDArchive(); exitFilesystem(); aptExit(); srvExit(); }
static int lua_regsound(lua_State *L){ int argc = lua_gettop(L); #ifndef SKIP_ERROR_HANDLING if (argc != 2) return luaL_error(L, "wrong number of arguments"); #endif u32 time = luaL_checkinteger(L, 1); u32 samplerate = luaL_checkinteger(L, 2); u32 mem_base; if (samplerate <= 8200){ srate = MICU_SAMPLE_RATE_8180; mem_base = 0x4000; }else if (samplerate <= 12000){ srate = MICU_SAMPLE_RATE_10910; mem_base = 0x6000; }else if (samplerate <= 18000){ srate = MICU_SAMPLE_RATE_16360; mem_base = 0x8000; }else{ srate = MICU_SAMPLE_RATE_32730; mem_base = 0x10000; } u32 micbuf_pos = 0; u32 mem_size = mem_base * time; micbuf = (u32*)memalign(0x1000, mem_size); micInit((u8*)micbuf, mem_size); micsize = micGetSampleDataSize(); bool power; cur_offset = 0; svcSleepThread(1000000000); MICU_StartSampling(MICU_ENCODING_PCM16_SIGNED, srate, 0, micsize, false); return 0; }
static void files_repopulate(files_data* listData) { if(listData->cancelEvent != 0) { svcSignalEvent(listData->cancelEvent); while(svcWaitSynchronization(listData->cancelEvent, 0) == 0) { svcSleepThread(1000000); } listData->cancelEvent = 0; } while(!util_is_dir(&listData->archive, listData->currDir.path)) { char parentPath[PATH_MAX]; util_get_parent_path(parentPath, listData->currDir.path, PATH_MAX); strncpy(listData->currDir.path, parentPath, PATH_MAX); util_get_path_file(listData->currDir.name, listData->currDir.path, NAME_MAX); util_get_parent_path(parentPath, listData->currDir.path, PATH_MAX); strncpy(listData->parentDir.path, parentPath, PATH_MAX); util_get_path_file(listData->parentDir.name, listData->parentDir.path, NAME_MAX); } listData->cancelEvent = task_populate_files(listData->items, &listData->count, FILES_MAX, &listData->currDir); listData->populated = true; }
static void files_repopulate(files_data* listData, linked_list* items) { if(!listData->populateData.finished) { svcSignalEvent(listData->populateData.cancelEvent); while(!listData->populateData.finished) { svcSleepThread(1000000); } } if(listData->dirItem != NULL) { task_free_file(listData->dirItem); listData->dirItem = NULL; } Result res = 0; if(R_SUCCEEDED(res = task_create_file_item(&listData->dirItem, listData->archive, listData->currDir))) { listData->populateData.items = items; listData->populateData.base = (file_info*) listData->dirItem->data; res = task_populate_files(&listData->populateData); } if(R_FAILED(res)) { error_display_res(NULL, NULL, NULL, res, "Failed to initiate file list population."); } listData->populated = true; }
static void files_free_data(files_data* data) { if(!data->populateData.finished) { svcSignalEvent(data->populateData.cancelEvent); while(!data->populateData.finished) { svcSleepThread(1000000); } } if(data->dirItem != NULL) { task_free_file(data->dirItem); data->dirItem = NULL; } if(data->archive != 0) { FSUSER_CloseArchive(data->archive); data->archive = 0; } if(data->archivePath.data != NULL) { free((void*) data->archivePath.data); data->archivePath.data = NULL; } free(data); }
/* _WM_Lock(wmlock) wmlock = a pointer to a value returns nothing Attempts to set a lock on the MDI tree so that only 1 library command may access it at any time. If lock fails the process retries until successful. */ void _WM_Lock(int * wmlock) { LOCK_START: /* Check if lock is clear, if so set it */ if (__builtin_expect(((*wmlock) == 0), 1)) { (*wmlock)++; /* Now that the lock is set, make sure we * don't have a race condition. If so, * decrement the lock by one and retry. */ if (__builtin_expect(((*wmlock) == 1), 1)) { return; /* Lock cleanly set */ } (*wmlock)--; } #ifdef _WIN32 Sleep(10); #elif defined(__OS2__) || defined(__EMX__) DosSleep(10); #elif defined(WILDMIDI_AMIGA) Delay(1); #elif defined(__vita__) sceKernelDelayThread(500); #elif defined(__SWITCH__) svcSleepThread(500 * 1000); #else usleep(500); #endif goto LOCK_START; }
static void files_update(ui_view* view, void* data, list_item** items, u32** itemCount, list_item* selected, bool selectedTouched) { files_data* listData = (files_data*) data; if(hidKeysDown() & KEY_B) { if(strcmp(listData->currDir.path, "/") == 0) { if(listData->archive.handle != 0) { FSUSER_CloseArchive(&listData->archive); listData->archive.handle = 0; } if(listData->archivePath != NULL) { free(listData->archivePath); listData->archivePath = NULL; } if(listData->cancelEvent != 0) { svcSignalEvent(listData->cancelEvent); while(svcWaitSynchronization(listData->cancelEvent, 0) == 0) { svcSleepThread(1000000); } listData->cancelEvent = 0; } ui_pop(); list_destroy(view); free(listData); return; } else { files_navigate(listData, listData->parentDir.path); } } if(hidKeysDown() & KEY_Y) { files_action_open(&listData->currDir, &listData->populated); return; } if(selected != NULL && selected->data != NULL && (selectedTouched || (hidKeysDown() & KEY_A))) { file_info* fileInfo = (file_info*) selected->data; if(util_is_dir(&listData->archive, fileInfo->path)) { files_navigate(listData, fileInfo->path); } else { files_action_open(fileInfo, &listData->populated); return; } } if(!listData->populated || (hidKeysDown() & KEY_X)) { files_repopulate(listData); } if(*itemCount != &listData->count || *items != listData->items) { *itemCount = &listData->count; *items = listData->items; } }
void LuaThread(void *arg) { // Run Lua interpreter // Load the boot.lua file onto the stack as a chunk luaL_loadfile(L, "lua/bios.lua"); // Continuously resume the chunk until it finishes or errors int args = 0; while(LuaBox_Running) { int state = lua_resume(L, NULL, args); args = 0; if(state == LUA_OK) break; // Execution complete if(state != LUA_YIELD) { // Error of some kind printf("# Uncaught lua error (code %i)\nTraceback:", state); luaL_dostring(L, "print(debug.traceback())"); break; } // Handle yield below svcWaitSynchronization(LuaBox_EventMutex, U64_MAX); // Attempt to lock event list if(LuaBox_EventList == NULL) { // Release the event lock and wait for a signal svcReleaseMutex(LuaBox_EventMutex); svcWaitSynchronization(LuaBox_EventSignal, U64_MAX); // Wait for another thread to signal us an event svcClearEvent(LuaBox_EventSignal); // Clear the event svcWaitSynchronization(LuaBox_EventMutex, U64_MAX); // Attempt to lock event list } struct LuaBox_Event * ev = LuaBox_EventList; // Grab end of list if(ev != NULL) { switch(ev->type) { case LUABOX_EVENT_CHAR: lua_pushstring(L, "char"); // Push string 'char' onto stack char *charPressed = malloc(2); charPressed[0] = (char)ev->value; charPressed[1] = 0x0; lua_pushstring(L, charPressed); free(charPressed); args = 2; break; default: break; } struct LuaBox_Event * next = ev->next; free(ev); LuaBox_EventList = next; } // Release the event lock svcReleaseMutex(LuaBox_EventMutex); } // Execution complete past this point printf("# bios.lua execution complete\n"); while (1) { svcSleepThread(10000000ULL);} }
int usleep(useconds_t useconds) { svcSleepThread(useconds * 1000); return 0; }
static void extsavedata_update(ui_view* view, void* data, list_item** items, u32** itemCount, list_item* selected, bool selectedTouched) { extsavedata_data* listData = (extsavedata_data*) data; if(hidKeysDown() & KEY_B) { if(listData->cancelEvent != 0) { svcSignalEvent(listData->cancelEvent); while(svcWaitSynchronization(listData->cancelEvent, 0) == 0) { svcSleepThread(1000000); } listData->cancelEvent = 0; } ui_pop(); list_destroy(view); free(listData); return; } if(!listData->populated || (hidKeysDown() & KEY_X)) { if(listData->cancelEvent != 0) { svcSignalEvent(listData->cancelEvent); while(svcWaitSynchronization(listData->cancelEvent, 0) == 0) { svcSleepThread(1000000); } listData->cancelEvent = 0; } listData->cancelEvent = task_populate_ext_save_data(listData->items, &listData->count, EXTSAVEDATA_MAX); listData->populated = true; } if(selected != NULL && selected->data != NULL && (selectedTouched || (hidKeysDown() & KEY_A))) { extsavedata_action_open((ext_save_data_info*) selected->data, &listData->populated); return; } if(*itemCount != &listData->count || *items != listData->items) { *itemCount = &listData->count; *items = listData->items; } }
void* texAlloc(size_t size, size_t alignment) { void *p; p = mspace_memalign(tex_msp, alignment, size); if (p == 0) { printf("out of texture memory %d\n", size); while (p == 0) { svcSleepThread(20000); } } return p; }
void SDLH_Play(void) { musicMutex = !songs.empty(); while (musicMutex) { HIDUSER_GetSoundVolume(¤tVolume); if (!Mix_PlayingMusic() || (currentVolume == 0 && songs.size() > 1)) { if (song) { Mix_FreeMusic(song); song = nullptr; } if (Configuration::getInstance().randomMusic()) { currentSong = randomNumbers() % songs.size(); } else { currentSong = (currentSong + 1) % songs.size(); } song = Mix_LoadMUS(songs[currentSong].c_str()); Mix_PlayMusic(song, 1); } if (currentVolume == 0) { Mix_PauseMusic(); } while (currentVolume == 0 && musicMutex) { HIDUSER_GetSoundVolume(¤tVolume); svcSleepThread(250000000); } if (Mix_PausedMusic() && musicMutex) { Mix_ResumeMusic(); } svcSleepThread(250000000); } donePlaying = true; }
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; }
static void alloc_thread(void* arg) { u32 tmp; alloc_finished = false; while (lock_alloc) svcSleepThread(100); svcControlMemory(&tmp, alloc_address, 0x0, alloc_size, MEMOP_ALLOC, MEMPERM_READ | MEMPERM_WRITE); alloc_finished = true; svcExitThread(); }
int ui_confirm(const char* question) { consoleSelect(&top_screen); consoleClear(); printf("%s\n\n\n\n\nA = Yes, B = No", question); gfxFlushBuffers(); gfxSwapBuffers(); gspWaitForVBlank(); while (aptMainLoop()) { hidScanInput(); unsigned int keys = hidKeysDown(); if (keys & KEY_A) { consoleClear(); svcSleepThread(100000000); return 1; } else if (keys & KEY_B) { consoleClear(); svcSleepThread(100000000); return 0; } } return 0; }
static void poll_thread(void* arg) { while ((u32) svcArbitrateAddress(arbiter, alloc_address, ARBITRATION_WAIT_IF_LESS_THAN_TIMEOUT, 0, 0) == 0xD9001814) svcSleepThread(10000); __asm__ volatile("cpsid aif \n\t"); memcpy(flush_buffer, flush_buffer + 0x8000, 0x8000); memcpy(flush_buffer, flush_buffer + 0x8000, 0x8000); memcpy(flush_buffer, flush_buffer + 0x8000, 0x8000); memcpy(flush_buffer, flush_buffer + 0x8000, 0x8000); memcpy(flush_buffer, flush_buffer + 0x8000, 0x8000); while (lock_thread); __asm__ volatile("cpsie aif \n\t"); svcExitThread(); }
Result inline downloadPageToSDCard(httpcContext* context, const short* filename, u32 size) { Result ret = 0; u32 pos = 0, sz = 0; IFile_Open(FILE_LOC, filename, FILE_W); *((int *)FILE_LOC + 1) = 0; svcSleepThread(0x400000LL); while(pos < size) { sz = size - pos; sz = sz > BUF_LEN ? BUF_LEN : sz; ret = httpcReceiveData(context, BUF_LOC, sz); if(ret == HTTPC_RESULTCODE_DOWNLOADPENDING) { ret = httpcGetDownloadSizeState(context, &pos, 0); if(ret) return ret; goto filewrite; } else if(ret) return ret; else { pos += sz; filewrite: IFile_Write(FILE_LOC, WRITTEN_LOC, BUF_LOC, sz); svcSleepThread(0x400000LL); } } return 0; }
void ui_pause(const char* message) { consoleSelect(&top_screen); consoleClear(); printf("%s\n\n\n\n\nA = Continue", message); gfxFlushBuffers(); gfxSwapBuffers(); gspWaitForVBlank(); while (aptMainLoop()) { hidScanInput(); if (hidKeysDown() & KEY_A) { consoleClear(); svcSleepThread(100000000); break; } } }
void* ctr_rend_buffer_alloc(int len) { int cb = (len + CTR_REND_BUFFER_ALIGN) & (~CTR_REND_BUFFER_ALIGN); if (ctr_state.buffer_pos + cb > CTR_REND_BUFFER_MAX) { printf("-------------\n\nctr_rend_buffer_alloc Out of Memory\n\n-------------\n"); ctr_buf_print(cb); do { svcSleepThread(20000); } while (1); return 0; } ctr_buf_cb(cb); GLubyte *addr = &ctr_state.buffer[ctr_state.buffer_pos]; ctr_state.buffer_pos += cb; return addr; }
// this is called before main void __appInit() { assertSuccess(svcCreateEvent(&terminationRequestedEvent, RESET_STICKY)); assertSuccess(svcCreateEvent(&sessionManager.sendAllBuffersToArm9Event, RESET_ONESHOT)); assertSuccess(svcCreateSemaphore(&sessionManager.replySemaphore, 0, 9)); assertSuccess(svcCreateEvent(&sessionManager.PXISRV11CommandReceivedEvent, RESET_ONESHOT)); assertSuccess(svcCreateEvent(&sessionManager.PXISRV11ReplySentEvent, RESET_ONESHOT)); initPXI(); for(Result res = 0xD88007FA; res == (Result)0xD88007FA; svcSleepThread(500 * 1000LL)) { res = srvInit(); if(R_FAILED(res) && res != (Result)0xD88007FA) svcBreak(USERBREAK_PANIC); } }
void SND_setup() { sndBuffer *p, *q; cvar_t *cv; int scs; #ifdef _3DS printf("SND_setup\n"); svcSleepThread(2000000000); #endif cv = Cvar_Get( "com_soundMegs", DEF_COMSOUNDMEGS, CVAR_LATCH | CVAR_ARCHIVE ); scs = (cv->integer*1536); if (buffer) { free(buffer); buffer = 0; } if (buffer == 0) { buffer = malloc(scs*sizeof(sndBuffer)); if (buffer == 0) { Sys_Error("Falied to allocate soud buffer %d %d %d\n", scs, sizeof(sndBuffer), scs*sizeof(sndBuffer)); } } if (sfxScratchBuffer) { free(sfxScratchBuffer); sfxScratchBuffer = 0; } // allocate the stack based hunk allocator sfxScratchBuffer = malloc(SND_CHUNK_SIZE * sizeof(short) * 4); //Hunk_Alloc(SND_CHUNK_SIZE * sizeof(short) * 4); sfxScratchPointer = NULL; inUse = scs*sizeof(sndBuffer); p = buffer;; q = p + scs; while (--q > p) *(sndBuffer **)q = q-1; *(sndBuffer **)q = NULL; freelist = p + scs - 1; Com_Printf("Sound memory manager started\n"); }
static void files_repopulate(files_data* listData, linked_list* items) { if(!listData->populateData.finished) { svcSignalEvent(listData->populateData.cancelEvent); while(!listData->populateData.finished) { svcSleepThread(1000000); } } listData->populateData.items = items; listData->populateData.archive = listData->archive; strncpy(listData->populateData.path, listData->currDir, FILE_PATH_MAX); Result res = task_populate_files(&listData->populateData); if(R_FAILED(res)) { error_display_res(NULL, NULL, res, "Failed to initiate file list population."); } listData->populated = true; }
void SDLH_Exit(void) { if (musicMutex) { musicMutex = false; } else { donePlaying = true; } while (!donePlaying) svcSleepThread(125000000); // wait for SDLH_Play to be done if (song) { Mix_FreeMusic(song); } Mix_CloseAudio(); Mix_Quit(); SDL_Quit(); }
static void files_free_data(files_data* data) { if(!data->populateData.finished) { svcSignalEvent(data->populateData.cancelEvent); while(!data->populateData.finished) { svcSleepThread(1000000); } } if(data->archive != 0) { util_close_archive(data->archive); data->archive = 0; } if(data->archivePath.data != NULL) { free((void*) data->archivePath.data); data->archivePath.data = NULL; } free(data); }