├── .gitignore ├── LICENSE ├── Makefile ├── README.md └── src ├── common ├── common.h ├── fs_defs.h ├── os_defs.h └── types.h ├── dynamic_libs ├── os_functions.c ├── os_functions.h ├── padscore_functions.c ├── padscore_functions.h ├── sys_functions.c ├── sys_functions.h ├── vpad_functions.c └── vpad_functions.h ├── entry.c ├── link.ld ├── main.cpp ├── main.h ├── system ├── memory.c └── memory.h └── utils └── utils.h /.gitignore: -------------------------------------------------------------------------------- 1 | /fs/build 2 | /installer/bin 3 | /loader/build 4 | /menu/build 5 | /server/logs/*.txt 6 | /build 7 | /*.elf 8 | /fs/*.elf 9 | /loader/*.elf 10 | /sd_loader/build 11 | /sd_loader/*.elf 12 | /udp_debug_reader/obj 13 | /udp_debug_reader/GeckoLog.txt -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 FIX94 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | #--------------------------------------------------------------------------------- 2 | # Clear the implicit built in rules 3 | #--------------------------------------------------------------------------------- 4 | .SUFFIXES: 5 | #--------------------------------------------------------------------------------- 6 | ifeq ($(strip $(DEVKITPPC)),) 7 | $(error "Please set DEVKITPPC in your environment. export DEVKITPPC=devkitPPC") 8 | endif 9 | ifeq ($(strip $(DEVKITPRO)),) 10 | $(error "Please set DEVKITPRO in your environment. export DEVKITPRO=devkitPRO") 11 | endif 12 | export PATH := $(DEVKITPPC)/bin:$(PORTLIBS)/bin:$(PATH) 13 | export LIBOGC_INC := $(DEVKITPRO)/libogc/include 14 | export LIBOGC_LIB := $(DEVKITPRO)/libogc/lib/wii 15 | export PORTLIBS := $(DEVKITPRO)/portlibs/ppc 16 | 17 | PREFIX := powerpc-eabi- 18 | 19 | export AS := $(PREFIX)as 20 | export CC := $(PREFIX)gcc 21 | export CXX := $(PREFIX)g++ 22 | export AR := $(PREFIX)ar 23 | export OBJCOPY := $(PREFIX)objcopy 24 | 25 | #--------------------------------------------------------------------------------- 26 | # TARGET is the name of the output 27 | # BUILD is the directory where object files & intermediate files will be placed 28 | # SOURCES is a list of directories containing source code 29 | # INCLUDES is a list of directories containing extra header files 30 | #--------------------------------------------------------------------------------- 31 | TARGET := vidchange 32 | BUILD := build 33 | BUILD_DBG := $(TARGET)_dbg 34 | SOURCES := src \ 35 | src/dynamic_libs \ 36 | src/system \ 37 | src/utils 38 | DATA := 39 | 40 | INCLUDES := src 41 | 42 | #--------------------------------------------------------------------------------- 43 | # options for code generation 44 | #--------------------------------------------------------------------------------- 45 | CFLAGS := -std=gnu11 -mrvl -mcpu=750 -meabi -mhard-float -ffast-math \ 46 | -O3 -Wall -Wextra -Wno-unused-parameter -Wno-strict-aliasing $(INCLUDE) 47 | CXXFLAGS := -std=gnu++11 -mrvl -mcpu=750 -meabi -mhard-float -ffast-math \ 48 | -O3 -Wall -Wextra -Wno-unused-parameter -Wno-strict-aliasing $(INCLUDE) 49 | ASFLAGS := -mregnames 50 | LDFLAGS := -nostartfiles -Wl,-Map,$(notdir $@).map,-wrap,malloc,-wrap,free,-wrap,memalign,-wrap,calloc,-wrap,realloc,-wrap,malloc_usable_size,-wrap,_malloc_r,-wrap,_free_r,-wrap,_realloc_r,-wrap,_calloc_r,-wrap,_memalign_r,-wrap,_malloc_usable_size_r,-wrap,valloc,-wrap,_valloc_r,-wrap,_pvalloc_r,--gc-sections 51 | 52 | #--------------------------------------------------------------------------------- 53 | Q := @ 54 | MAKEFLAGS += --no-print-directory 55 | #--------------------------------------------------------------------------------- 56 | # any extra libraries we wish to link with the project 57 | #--------------------------------------------------------------------------------- 58 | LIBS := 59 | 60 | #--------------------------------------------------------------------------------- 61 | # list of directories containing libraries, this must be the top level containing 62 | # include and lib 63 | #--------------------------------------------------------------------------------- 64 | LIBDIRS := $(CURDIR) \ 65 | $(DEVKITPPC)/lib \ 66 | $(DEVKITPPC)/lib/gcc/powerpc-eabi/4.8.2 67 | 68 | 69 | #--------------------------------------------------------------------------------- 70 | # no real need to edit anything past this point unless you need to add additional 71 | # rules for different file extensions 72 | #--------------------------------------------------------------------------------- 73 | ifneq ($(BUILD),$(notdir $(CURDIR))) 74 | #--------------------------------------------------------------------------------- 75 | export PROJECTDIR := $(CURDIR) 76 | export OUTPUT := $(CURDIR)/$(TARGETDIR)/$(TARGET) 77 | export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ 78 | $(foreach dir,$(DATA),$(CURDIR)/$(dir)) 79 | export DEPSDIR := $(CURDIR)/$(BUILD) 80 | 81 | #--------------------------------------------------------------------------------- 82 | # automatically build a list of object files for our project 83 | #--------------------------------------------------------------------------------- 84 | CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) 85 | CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) 86 | sFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) 87 | SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S))) 88 | BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) 89 | TTFFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.ttf))) 90 | PNGFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.png))) 91 | 92 | #--------------------------------------------------------------------------------- 93 | # use CXX for linking C++ projects, CC for standard C 94 | #--------------------------------------------------------------------------------- 95 | ifeq ($(strip $(CPPFILES)),) 96 | export LD := $(CC) 97 | else 98 | export LD := $(CXX) 99 | endif 100 | 101 | export OFILES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) \ 102 | $(sFILES:.s=.o) $(SFILES:.S=.o) \ 103 | $(PNGFILES:.png=.png.o) $(addsuffix .o,$(BINFILES)) 104 | 105 | #--------------------------------------------------------------------------------- 106 | # build a list of include paths 107 | #--------------------------------------------------------------------------------- 108 | export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ 109 | $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ 110 | -I$(CURDIR)/$(BUILD) -I$(LIBOGC_INC) \ 111 | -I$(PORTLIBS)/include -I$(PORTLIBS)/include/freetype2 112 | 113 | #--------------------------------------------------------------------------------- 114 | # build a list of library paths 115 | #--------------------------------------------------------------------------------- 116 | export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) \ 117 | -L$(LIBOGC_LIB) -L$(PORTLIBS)/lib 118 | 119 | export OUTPUT := $(CURDIR)/$(TARGET) 120 | .PHONY: $(BUILD) clean install 121 | 122 | #--------------------------------------------------------------------------------- 123 | $(BUILD): 124 | @[ -d $@ ] || mkdir -p $@ 125 | @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile 126 | 127 | #--------------------------------------------------------------------------------- 128 | clean: 129 | @echo clean ... 130 | @rm -fr $(BUILD) $(OUTPUT).elf $(OUTPUT).bin $(BUILD_DBG).elf 131 | 132 | #--------------------------------------------------------------------------------- 133 | else 134 | 135 | DEPENDS := $(OFILES:.o=.d) 136 | 137 | #--------------------------------------------------------------------------------- 138 | # main targets 139 | #--------------------------------------------------------------------------------- 140 | $(OUTPUT).elf: $(OFILES) 141 | 142 | #--------------------------------------------------------------------------------- 143 | # This rule links in binary data with the .jpg extension 144 | #--------------------------------------------------------------------------------- 145 | %.elf: link.ld $(OFILES) 146 | @echo "linking ... $(TARGET).elf" 147 | $(Q)$(LD) -n -T $^ $(LDFLAGS) -o ../$(BUILD_DBG).elf $(LIBPATHS) $(LIBS) 148 | $(Q)$(OBJCOPY) -S -R .comment -R .gnu.attributes ../$(BUILD_DBG).elf $@ 149 | 150 | ../data/loader.bin: 151 | $(MAKE) -C ../loader clean 152 | $(MAKE) -C ../loader 153 | #--------------------------------------------------------------------------------- 154 | %.a: 155 | #--------------------------------------------------------------------------------- 156 | @echo $(notdir $@) 157 | @rm -f $@ 158 | @$(AR) -rc $@ $^ 159 | 160 | #--------------------------------------------------------------------------------- 161 | %.o: %.cpp 162 | @echo $(notdir $<) 163 | @$(CXX) -MMD -MP -MF $(DEPSDIR)/$*.d $(CXXFLAGS) -c $< -o $@ $(ERROR_FILTER) 164 | 165 | #--------------------------------------------------------------------------------- 166 | %.o: %.c 167 | @echo $(notdir $<) 168 | @$(CC) -MMD -MP -MF $(DEPSDIR)/$*.d $(CFLAGS) -c $< -o $@ $(ERROR_FILTER) 169 | 170 | #--------------------------------------------------------------------------------- 171 | %.o: %.S 172 | @echo $(notdir $<) 173 | @$(CC) -MMD -MP -MF $(DEPSDIR)/$*.d -x assembler-with-cpp $(ASFLAGS) -c $< -o $@ $(ERROR_FILTER) 174 | 175 | #--------------------------------------------------------------------------------- 176 | %.png.o : %.png 177 | @echo $(notdir $<) 178 | @bin2s -a 32 $< | $(AS) -o $(@) 179 | 180 | #--------------------------------------------------------------------------------- 181 | %.jpg.o : %.jpg 182 | @echo $(notdir $<) 183 | @bin2s -a 32 $< | $(AS) -o $(@) 184 | 185 | #--------------------------------------------------------------------------------- 186 | %.ttf.o : %.ttf 187 | @echo $(notdir $<) 188 | @bin2s -a 32 $< | $(AS) -o $(@) 189 | 190 | #--------------------------------------------------------------------------------- 191 | %.bin.o : %.bin 192 | @echo $(notdir $<) 193 | @bin2s -a 32 $< | $(AS) -o $(@) 194 | 195 | #--------------------------------------------------------------------------------- 196 | %.wav.o : %.wav 197 | @echo $(notdir $<) 198 | @bin2s -a 32 $< | $(AS) -o $(@) 199 | 200 | #--------------------------------------------------------------------------------- 201 | %.mp3.o : %.mp3 202 | @echo $(notdir $<) 203 | @bin2s -a 32 $< | $(AS) -o $(@) 204 | 205 | #--------------------------------------------------------------------------------- 206 | %.ogg.o : %.ogg 207 | @echo $(notdir $<) 208 | @bin2s -a 32 $< | $(AS) -o $(@) 209 | 210 | -include $(DEPENDS) 211 | 212 | #--------------------------------------------------------------------------------- 213 | endif 214 | #--------------------------------------------------------------------------------- 215 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # wiiu-video-mode-changer 2 | temporarily change the displayed video mode 3 | 4 | # Usage 5 | grab the elf from the downloads section, put it into your wiiu/apps folder and every time you want to change the video mode just launch it and set it to whatever you want. -------------------------------------------------------------------------------- /src/common/common.h: -------------------------------------------------------------------------------- 1 | #ifndef COMMON_H 2 | #define COMMON_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include "os_defs.h" 9 | 10 | #define CAFE_OS_SD_PATH "/vol/external01" 11 | #define SD_PATH "sd:" 12 | #define WIIU_PATH "/wiiu" 13 | 14 | #ifndef MEM_BASE 15 | #define MEM_BASE (0x00800000) 16 | #endif 17 | 18 | #define ELF_DATA_ADDR (*(volatile unsigned int*)(MEM_BASE + 0x1300 + 0x00)) 19 | #define ELF_DATA_SIZE (*(volatile unsigned int*)(MEM_BASE + 0x1300 + 0x04)) 20 | #define MAIN_ENTRY_ADDR (*(volatile unsigned int*)(MEM_BASE + 0x1400 + 0x00)) 21 | #define OS_FIRMWARE (*(volatile unsigned int*)(MEM_BASE + 0x1400 + 0x04)) 22 | 23 | #define OS_SPECIFICS ((OsSpecifics*)(MEM_BASE + 0x1500)) 24 | 25 | #ifndef EXIT_SUCCESS 26 | #define EXIT_SUCCESS 0 27 | #endif 28 | #define EXIT_HBL_EXIT 0xFFFFFFFE 29 | #define EXIT_RELAUNCH_ON_LOAD 0xFFFFFFFD 30 | 31 | #ifdef __cplusplus 32 | } 33 | #endif 34 | 35 | #endif /* COMMON_H */ 36 | 37 | -------------------------------------------------------------------------------- /src/common/fs_defs.h: -------------------------------------------------------------------------------- 1 | #ifndef FS_DEFS_H 2 | #define FS_DEFS_H 3 | 4 | #include "types.h" 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | 11 | /* FS defines and types */ 12 | #define FS_MAX_LOCALPATH_SIZE 511 13 | #define FS_MAX_MOUNTPATH_SIZE 128 14 | #define FS_MAX_FULLPATH_SIZE (FS_MAX_LOCALPATH_SIZE + FS_MAX_MOUNTPATH_SIZE) 15 | #define FS_MAX_ARGPATH_SIZE FS_MAX_FULLPATH_SIZE 16 | 17 | #define FS_STATUS_OK 0 18 | #define FS_RET_UNSUPPORTED_CMD 0x0400 19 | #define FS_RET_NO_ERROR 0x0000 20 | #define FS_RET_ALL_ERROR (unsigned int)(-1) 21 | 22 | #define FS_STAT_FLAG_IS_DIRECTORY 0x80000000 23 | 24 | /* max length of file/dir name */ 25 | #define FS_MAX_ENTNAME_SIZE 256 26 | 27 | #define FS_SOURCETYPE_EXTERNAL 0 28 | #define FS_SOURCETYPE_HFIO 1 29 | #define FS_SOURCETYPE_HFIO 1 30 | 31 | #define FS_MOUNT_SOURCE_SIZE 0x300 32 | #define FS_CLIENT_SIZE 0x1700 33 | #define FS_CMD_BLOCK_SIZE 0xA80 34 | 35 | typedef struct 36 | { 37 | uint32_t flag; 38 | uint32_t permission; 39 | uint32_t owner_id; 40 | uint32_t group_id; 41 | uint32_t size; 42 | uint32_t alloc_size; 43 | uint64_t quota_size; 44 | uint32_t ent_id; 45 | uint64_t ctime; 46 | uint64_t mtime; 47 | uint8_t attributes[48]; 48 | } __attribute__((packed)) FSStat; 49 | 50 | typedef struct 51 | { 52 | FSStat stat; 53 | char name[FS_MAX_ENTNAME_SIZE]; 54 | } FSDirEntry; 55 | 56 | 57 | #ifdef __cplusplus 58 | } 59 | #endif 60 | 61 | #endif /* FS_DEFS_H */ 62 | 63 | -------------------------------------------------------------------------------- /src/common/os_defs.h: -------------------------------------------------------------------------------- 1 | #ifndef __OS_DEFS_H_ 2 | #define __OS_DEFS_H_ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | typedef struct _OsSpecifics 9 | { 10 | unsigned int addr_OSDynLoad_Acquire; 11 | unsigned int addr_OSDynLoad_FindExport; 12 | unsigned int addr_OSTitle_main_entry; 13 | 14 | unsigned int addr_KernSyscallTbl1; 15 | unsigned int addr_KernSyscallTbl2; 16 | unsigned int addr_KernSyscallTbl3; 17 | unsigned int addr_KernSyscallTbl4; 18 | unsigned int addr_KernSyscallTbl5; 19 | } OsSpecifics; 20 | 21 | #ifdef __cplusplus 22 | } 23 | #endif 24 | 25 | #endif // __OS_DEFS_H_ 26 | -------------------------------------------------------------------------------- /src/common/types.h: -------------------------------------------------------------------------------- 1 | #ifndef TYPES_H 2 | #define TYPES_H 3 | 4 | #include 5 | 6 | #endif /* TYPES_H */ 7 | 8 | -------------------------------------------------------------------------------- /src/dynamic_libs/os_functions.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #include "common/common.h" 25 | #include "os_functions.h" 26 | 27 | unsigned int coreinit_handle = 0; 28 | 29 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 30 | //! Lib handle functions 31 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 32 | EXPORT_DECL(int, OSDynLoad_Acquire, const char* rpl, unsigned int *handle); 33 | EXPORT_DECL(int, OSDynLoad_FindExport, u32 handle, int isdata, const char *symbol, void *address); 34 | 35 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 36 | //! Thread functions 37 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 38 | EXPORT_DECL(int, OSCreateThread, void *thread, s32 (*callback)(s32, void*), s32 argc, void *args, u32 stack, u32 stack_size, s32 priority, u32 attr); 39 | EXPORT_DECL(int, OSResumeThread, void *thread); 40 | EXPORT_DECL(int, OSSuspendThread, void *thread); 41 | EXPORT_DECL(int, OSIsThreadTerminated, void *thread); 42 | EXPORT_DECL(int, OSIsThreadSuspended, void *thread); 43 | EXPORT_DECL(int, OSSetThreadPriority, void * thread, int priority); 44 | EXPORT_DECL(int, OSJoinThread, void * thread, int * ret_val); 45 | EXPORT_DECL(void, OSDetachThread, void * thread); 46 | EXPORT_DECL(void, OSSleepTicks, u64 ticks); 47 | EXPORT_DECL(u64, OSGetTick, void); 48 | 49 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 50 | //! Mutex functions 51 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 52 | EXPORT_DECL(void, OSInitMutex, void* mutex); 53 | EXPORT_DECL(void, OSLockMutex, void* mutex); 54 | EXPORT_DECL(void, OSUnlockMutex, void* mutex); 55 | EXPORT_DECL(int, OSTryLockMutex, void* mutex); 56 | 57 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 58 | //! System functions 59 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 60 | EXPORT_DECL(u64, OSGetTitleID, void); 61 | EXPORT_DECL(void, __Exit, void); 62 | EXPORT_DECL(void, OSFatal, const char* msg); 63 | EXPORT_DECL(void, OSSetExceptionCallback, u8 exceptionType, exception_callback newCallback); 64 | EXPORT_DECL(void, DCFlushRange, const void *addr, u32 length); 65 | EXPORT_DECL(void, ICInvalidateRange, const void *addr, u32 length); 66 | EXPORT_DECL(void*, OSEffectiveToPhysical, const void*); 67 | EXPORT_DECL(int, __os_snprintf, char* s, int n, const char * format, ...); 68 | 69 | EXPORT_DECL(void, OSScreenInit, void); 70 | EXPORT_DECL(unsigned int, OSScreenGetBufferSizeEx, unsigned int bufferNum); 71 | EXPORT_DECL(int, OSScreenSetBufferEx, unsigned int bufferNum, void * addr); 72 | EXPORT_DECL(int, OSScreenClearBufferEx, unsigned int bufferNum, unsigned int temp); 73 | EXPORT_DECL(int, OSScreenFlipBuffersEx, unsigned int bufferNum); 74 | EXPORT_DECL(int, OSScreenPutFontEx, unsigned int bufferNum, unsigned int posX, unsigned int posY, const char * buffer); 75 | EXPORT_DECL(int, OSScreenEnableEx, unsigned int bufferNum, int enable); 76 | 77 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 78 | //! Memory functions 79 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 80 | EXPORT_VAR(unsigned int *, pMEMAllocFromDefaultHeapEx); 81 | EXPORT_VAR(unsigned int *, pMEMAllocFromDefaultHeap); 82 | EXPORT_VAR(unsigned int *, pMEMFreeToDefaultHeap); 83 | 84 | EXPORT_DECL(int, MEMGetBaseHeapHandle, int mem_arena); 85 | EXPORT_DECL(unsigned int, MEMGetAllocatableSizeForFrmHeapEx, int heap, int align); 86 | EXPORT_DECL(void *, MEMAllocFromFrmHeapEx, int heap, unsigned int size, int align); 87 | EXPORT_DECL(void, MEMFreeToFrmHeap, int heap, int mode); 88 | EXPORT_DECL(void *, MEMAllocFromExpHeapEx, int heap, unsigned int size, int align); 89 | EXPORT_DECL(int , MEMCreateExpHeapEx, void* address, unsigned int size, unsigned short flags); 90 | EXPORT_DECL(void *, MEMDestroyExpHeap, int heap); 91 | EXPORT_DECL(void, MEMFreeToExpHeap, int heap, void* ptr); 92 | 93 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 94 | //! Loader functions (not real rpl) 95 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 96 | EXPORT_DECL(int, LiWaitIopComplete, int unknown_syscall_arg_r3, int * remaining_bytes); 97 | EXPORT_DECL(int, LiWaitIopCompleteWithInterrupts, int unknown_syscall_arg_r3, int * remaining_bytes); 98 | 99 | void InitOSFunctionPointers(void) 100 | { 101 | unsigned int *funcPointer = 0; 102 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 103 | //! Lib handle functions 104 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 105 | EXPORT_FUNC_WRITE(OSDynLoad_Acquire, (int (*)(const char*, unsigned *))OS_SPECIFICS->addr_OSDynLoad_Acquire); 106 | EXPORT_FUNC_WRITE(OSDynLoad_FindExport, (int (*)(u32, int, const char *, void *))OS_SPECIFICS->addr_OSDynLoad_FindExport); 107 | 108 | OSDynLoad_Acquire("coreinit.rpl", &coreinit_handle); 109 | 110 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 111 | //! System functions 112 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 113 | OS_FIND_EXPORT(coreinit_handle, OSFatal); 114 | OS_FIND_EXPORT(coreinit_handle, OSGetTitleID); 115 | OS_FIND_EXPORT(coreinit_handle, OSSetExceptionCallback); 116 | OS_FIND_EXPORT(coreinit_handle, DCFlushRange); 117 | OS_FIND_EXPORT(coreinit_handle, ICInvalidateRange); 118 | OS_FIND_EXPORT(coreinit_handle, OSEffectiveToPhysical); 119 | OS_FIND_EXPORT(coreinit_handle, __os_snprintf); 120 | OSDynLoad_FindExport(coreinit_handle, 0, "_Exit", &__Exit); 121 | 122 | OS_FIND_EXPORT(coreinit_handle, OSScreenInit); 123 | OS_FIND_EXPORT(coreinit_handle, OSScreenGetBufferSizeEx); 124 | OS_FIND_EXPORT(coreinit_handle, OSScreenSetBufferEx); 125 | OS_FIND_EXPORT(coreinit_handle, OSScreenClearBufferEx); 126 | OS_FIND_EXPORT(coreinit_handle, OSScreenFlipBuffersEx); 127 | OS_FIND_EXPORT(coreinit_handle, OSScreenPutFontEx); 128 | OS_FIND_EXPORT(coreinit_handle, OSScreenEnableEx); 129 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 130 | //! Thread functions 131 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 132 | OS_FIND_EXPORT(coreinit_handle, OSCreateThread); 133 | OS_FIND_EXPORT(coreinit_handle, OSResumeThread); 134 | OS_FIND_EXPORT(coreinit_handle, OSSuspendThread); 135 | OS_FIND_EXPORT(coreinit_handle, OSIsThreadTerminated); 136 | OS_FIND_EXPORT(coreinit_handle, OSIsThreadSuspended); 137 | OS_FIND_EXPORT(coreinit_handle, OSJoinThread); 138 | OS_FIND_EXPORT(coreinit_handle, OSSetThreadPriority); 139 | OS_FIND_EXPORT(coreinit_handle, OSDetachThread); 140 | OS_FIND_EXPORT(coreinit_handle, OSSleepTicks); 141 | OS_FIND_EXPORT(coreinit_handle, OSGetTick); 142 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 143 | //! Mutex functions 144 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 145 | OS_FIND_EXPORT(coreinit_handle, OSInitMutex); 146 | OS_FIND_EXPORT(coreinit_handle, OSLockMutex); 147 | OS_FIND_EXPORT(coreinit_handle, OSUnlockMutex); 148 | OS_FIND_EXPORT(coreinit_handle, OSTryLockMutex); 149 | 150 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 151 | //! Memory functions 152 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 153 | OSDynLoad_FindExport(coreinit_handle, 1, "MEMAllocFromDefaultHeapEx", &pMEMAllocFromDefaultHeapEx); 154 | OSDynLoad_FindExport(coreinit_handle, 1, "MEMAllocFromDefaultHeap", &pMEMAllocFromDefaultHeap); 155 | OSDynLoad_FindExport(coreinit_handle, 1, "MEMFreeToDefaultHeap", &pMEMFreeToDefaultHeap); 156 | 157 | OS_FIND_EXPORT(coreinit_handle, MEMGetBaseHeapHandle); 158 | OS_FIND_EXPORT(coreinit_handle, MEMGetAllocatableSizeForFrmHeapEx); 159 | OS_FIND_EXPORT(coreinit_handle, MEMAllocFromFrmHeapEx); 160 | OS_FIND_EXPORT(coreinit_handle, MEMFreeToFrmHeap); 161 | OS_FIND_EXPORT(coreinit_handle, MEMAllocFromExpHeapEx); 162 | OS_FIND_EXPORT(coreinit_handle, MEMCreateExpHeapEx); 163 | OS_FIND_EXPORT(coreinit_handle, MEMDestroyExpHeap); 164 | OS_FIND_EXPORT(coreinit_handle, MEMFreeToExpHeap); 165 | } 166 | 167 | -------------------------------------------------------------------------------- /src/dynamic_libs/os_functions.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #ifndef __OS_FUNCTIONS_H_ 25 | #define __OS_FUNCTIONS_H_ 26 | 27 | #include 28 | #include "common/os_defs.h" 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | #define BUS_SPEED 248625000 35 | #define SECS_TO_TICKS(sec) (((unsigned long long)(sec)) * (BUS_SPEED/4)) 36 | #define MILLISECS_TO_TICKS(msec) (SECS_TO_TICKS(msec) / 1000) 37 | #define MICROSECS_TO_TICKS(usec) (SECS_TO_TICKS(usec) / 1000000) 38 | 39 | #define usleep(usecs) OSSleepTicks(MICROSECS_TO_TICKS(usecs)) 40 | #define sleep(secs) OSSleepTicks(SECS_TO_TICKS(secs)) 41 | 42 | #define FLUSH_DATA_BLOCK(addr) asm volatile("dcbf 0, %0; sync" : : "r"(((addr) & ~31))) 43 | #define INVAL_DATA_BLOCK(addr) asm volatile("dcbi 0, %0; sync" : : "r"(((addr) & ~31))) 44 | 45 | #define EXPORT_DECL(res, func, ...) res (* func)(__VA_ARGS__) __attribute__((section(".data"))) = 0; 46 | #define EXPORT_VAR(type, var) type var __attribute__((section(".data"))); 47 | 48 | 49 | #define EXPORT_FUNC_WRITE(func, val) *(u32*)(((u32)&func) + 0) = (u32)val 50 | 51 | #define OS_FIND_EXPORT(handle, func) funcPointer = 0; \ 52 | OSDynLoad_FindExport(handle, 0, # func, &funcPointer); \ 53 | if(!funcPointer) \ 54 | OSFatal("Function " # func " is NULL"); \ 55 | EXPORT_FUNC_WRITE(func, funcPointer); 56 | 57 | #define OS_FIND_EXPORT_EX(handle, func, func_p) \ 58 | funcPointer = 0; \ 59 | OSDynLoad_FindExport(handle, 0, # func, &funcPointer); \ 60 | if(!funcPointer) \ 61 | OSFatal("Function " # func " is NULL"); \ 62 | EXPORT_FUNC_WRITE(func_p, funcPointer); 63 | 64 | #define OS_MUTEX_SIZE 44 65 | 66 | /* Handle for coreinit */ 67 | extern unsigned int coreinit_handle; 68 | void InitOSFunctionPointers(void); 69 | 70 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 71 | //! Lib handle functions 72 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 73 | extern int (* OSDynLoad_Acquire)(const char* rpl, unsigned int *handle); 74 | extern int (* OSDynLoad_FindExport)(u32 handle, int isdata, const char *symbol, void *address); 75 | 76 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 77 | //! Thread functions 78 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 79 | extern int (* OSCreateThread)(void *thread, s32 (*callback)(s32, void*), s32 argc, void *args, u32 stack, u32 stack_size, s32 priority, u32 attr); 80 | extern int (* OSResumeThread)(void *thread); 81 | extern int (* OSSuspendThread)(void *thread); 82 | extern int (* OSIsThreadTerminated)(void *thread); 83 | extern int (* OSIsThreadSuspended)(void *thread); 84 | extern int (* OSJoinThread)(void * thread, int * ret_val); 85 | extern int (* OSSetThreadPriority)(void * thread, int priority); 86 | extern void (* OSDetachThread)(void * thread); 87 | extern void (* OSSleepTicks)(u64 ticks); 88 | extern u64 (* OSGetTick)(void); 89 | 90 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 91 | //! Mutex functions 92 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 93 | extern void (* OSInitMutex)(void* mutex); 94 | extern void (* OSLockMutex)(void* mutex); 95 | extern void (* OSUnlockMutex)(void* mutex); 96 | extern int (* OSTryLockMutex)(void* mutex); 97 | 98 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 99 | //! System functions 100 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 101 | extern u64 (* OSGetTitleID)(void); 102 | extern void (* __Exit)(void); 103 | extern void (* OSFatal)(const char* msg); 104 | extern void (* DCFlushRange)(const void *addr, u32 length); 105 | extern void (* ICInvalidateRange)(const void *addr, u32 length); 106 | extern void* (* OSEffectiveToPhysical)(const void*); 107 | extern int (* __os_snprintf)(char* s, int n, const char * format, ...); 108 | 109 | extern void (*OSScreenInit)(void); 110 | extern unsigned int (*OSScreenGetBufferSizeEx)(unsigned int bufferNum); 111 | extern int (*OSScreenSetBufferEx)(unsigned int bufferNum, void * addr); 112 | extern int (*OSScreenClearBufferEx)(unsigned int bufferNum, unsigned int temp); 113 | extern int (*OSScreenFlipBuffersEx)(unsigned int bufferNum); 114 | extern int (*OSScreenPutFontEx)(unsigned int bufferNum, unsigned int posX, unsigned int posY, const char * buffer); 115 | extern int (*OSScreenEnableEx)(unsigned int bufferNum, int enable); 116 | 117 | typedef unsigned char (*exception_callback)(void * interruptedContext); 118 | extern void (* OSSetExceptionCallback)(u8 exceptionType, exception_callback newCallback); 119 | 120 | extern int (* LiWaitIopComplete)(int unknown_syscall_arg_r3, int * remaining_bytes); 121 | extern int (* LiWaitIopCompleteWithInterrupts)(int unknown_syscall_arg_r3, int * remaining_bytes); 122 | 123 | 124 | #ifdef __cplusplus 125 | } 126 | #endif 127 | 128 | #endif // __OS_FUNCTIONS_H_ 129 | -------------------------------------------------------------------------------- /src/dynamic_libs/padscore_functions.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #include "os_functions.h" 25 | #include "padscore_functions.h" 26 | 27 | EXPORT_DECL(void, KPADInit, void); 28 | EXPORT_DECL(s32, WPADProbe, s32 chan, u32 * pad_type); 29 | EXPORT_DECL(s32, WPADSetDataFormat, s32 chan, s32 format); 30 | EXPORT_DECL(void, WPADEnableURCC, s32 enable); 31 | EXPORT_DECL(void, WPADRead, s32 chan, void * data); 32 | EXPORT_DECL(s32, KPADRead, s32 chan, void * data, u32 size); 33 | 34 | void InitPadScoreFunctionPointers(void) 35 | { 36 | unsigned int *funcPointer = 0; 37 | unsigned int padscore_handle; 38 | OSDynLoad_Acquire("padscore.rpl", &padscore_handle); 39 | 40 | OS_FIND_EXPORT(padscore_handle, KPADInit); 41 | OS_FIND_EXPORT(padscore_handle, WPADProbe); 42 | OS_FIND_EXPORT(padscore_handle, WPADSetDataFormat); 43 | OS_FIND_EXPORT(padscore_handle, WPADEnableURCC); 44 | OS_FIND_EXPORT(padscore_handle, WPADRead); 45 | OS_FIND_EXPORT(padscore_handle, KPADRead); 46 | 47 | KPADInit(); 48 | WPADEnableURCC(1); 49 | } 50 | 51 | -------------------------------------------------------------------------------- /src/dynamic_libs/padscore_functions.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #ifndef __PAD_SCORE_FUNCTIONS_H_ 25 | #define __PAD_SCORE_FUNCTIONS_H_ 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | #include 32 | 33 | #define WPAD_BUTTON_LEFT 0x0001 34 | #define WPAD_BUTTON_RIGHT 0x0002 35 | #define WPAD_BUTTON_DOWN 0x0004 36 | #define WPAD_BUTTON_UP 0x0008 37 | #define WPAD_BUTTON_PLUS 0x0010 38 | #define WPAD_BUTTON_2 0x0100 39 | #define WPAD_BUTTON_1 0x0200 40 | #define WPAD_BUTTON_B 0x0400 41 | #define WPAD_BUTTON_A 0x0800 42 | #define WPAD_BUTTON_MINUS 0x1000 43 | #define WPAD_BUTTON_Z 0x2000 44 | #define WPAD_BUTTON_C 0x4000 45 | #define WPAD_BUTTON_HOME 0x8000 46 | 47 | #define WPAD_CLASSIC_BUTTON_UP 0x0001 48 | #define WPAD_CLASSIC_BUTTON_LEFT 0x0002 49 | #define WPAD_CLASSIC_BUTTON_ZR 0x0004 50 | #define WPAD_CLASSIC_BUTTON_X 0x0008 51 | #define WPAD_CLASSIC_BUTTON_A 0x0010 52 | #define WPAD_CLASSIC_BUTTON_Y 0x0020 53 | #define WPAD_CLASSIC_BUTTON_B 0x0040 54 | #define WPAD_CLASSIC_BUTTON_ZL 0x0080 55 | #define WPAD_CLASSIC_BUTTON_R 0x0200 56 | #define WPAD_CLASSIC_BUTTON_PLUS 0x0400 57 | #define WPAD_CLASSIC_BUTTON_HOME 0x0800 58 | #define WPAD_CLASSIC_BUTTON_MINUS 0x1000 59 | #define WPAD_CLASSIC_BUTTON_L 0x2000 60 | #define WPAD_CLASSIC_BUTTON_DOWN 0x4000 61 | #define WPAD_CLASSIC_BUTTON_RIGHT 0x8000 62 | 63 | void InitPadScoreFunctionPointers(void); 64 | 65 | 66 | typedef struct _KPADData 67 | { 68 | u32 btns_h; 69 | u32 btns_d; 70 | u32 btns_r; 71 | u32 unused_1[5]; 72 | f32 pos_x; 73 | f32 pos_y; 74 | u32 unused_2[3]; 75 | f32 angle_x; 76 | f32 angle_y; 77 | u32 unused_3[8]; 78 | u8 device_type; 79 | u8 wpad_error; 80 | u8 pos_valid; 81 | u8 unused_4[1]; 82 | 83 | union 84 | { 85 | struct 86 | { 87 | f32 stick_x; 88 | f32 stick_y; 89 | } nunchuck; 90 | 91 | struct 92 | { 93 | u32 btns_h; 94 | u32 btns_d; 95 | u32 btns_r; 96 | f32 lstick_x; 97 | f32 lstick_y; 98 | f32 rstick_x; 99 | f32 rstick_y; 100 | f32 ltrigger; 101 | f32 rtrigger; 102 | } classic; 103 | 104 | u32 unused_6[20]; 105 | }; 106 | u32 unused_7[16]; 107 | } KPADData; 108 | 109 | typedef void (* wpad_connect_callback_t)(s32 chan, s32 status); 110 | 111 | extern void (* KPADInit)(void); 112 | extern s32 (* WPADProbe)(s32 chan, u32 * pad_type); 113 | extern s32 (* WPADSetDataFormat)(s32 chan, s32 format); 114 | extern void (* WPADEnableURCC)(s32 enable); 115 | extern void (* WPADRead)(s32 chan, void * data); 116 | extern s32 (* KPADRead)(s32 chan, void * data, u32 size); 117 | 118 | #ifdef __cplusplus 119 | } 120 | #endif 121 | 122 | #endif // __PAD_SCORE_FUNCTIONS_H_ 123 | -------------------------------------------------------------------------------- /src/dynamic_libs/sys_functions.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #include "os_functions.h" 25 | 26 | EXPORT_DECL(void, _SYSLaunchTitleByPathFromLauncher, const char* path, int len, int zero); 27 | EXPORT_DECL(int, SYSRelaunchTitle, int argc, char* argv); 28 | EXPORT_DECL(int, SYSLaunchMenu, void); 29 | EXPORT_DECL(unsigned long long, _SYSGetSystemApplicationTitleId, int); 30 | 31 | void InitSysFunctionPointers(void) 32 | { 33 | unsigned int *funcPointer = 0; 34 | unsigned int sysapp_handle; 35 | OSDynLoad_Acquire("sysapp.rpl", &sysapp_handle); 36 | 37 | OS_FIND_EXPORT(sysapp_handle, _SYSLaunchTitleByPathFromLauncher); 38 | OS_FIND_EXPORT(sysapp_handle, SYSRelaunchTitle); 39 | OS_FIND_EXPORT(sysapp_handle, SYSLaunchMenu); 40 | OS_FIND_EXPORT(sysapp_handle, _SYSGetSystemApplicationTitleId); 41 | } 42 | 43 | -------------------------------------------------------------------------------- /src/dynamic_libs/sys_functions.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #ifndef __SYS_FUNCTIONS_H_ 25 | #define __SYS_FUNCTIONS_H_ 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | void InitSysFunctionPointers(void); 32 | 33 | extern void(*_SYSLaunchTitleByPathFromLauncher)(const char* path, int len, int zero); 34 | extern int (* SYSRelaunchTitle)(int argc, char* argv); 35 | extern int (* SYSLaunchMenu)(void); 36 | extern unsigned long long (*_SYSGetSystemApplicationTitleId)(int sysApp); 37 | 38 | #ifdef __cplusplus 39 | } 40 | #endif 41 | 42 | #endif // __SYS_FUNCTIONS_H_ 43 | -------------------------------------------------------------------------------- /src/dynamic_libs/vpad_functions.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #include "os_functions.h" 25 | #include "vpad_functions.h" 26 | 27 | EXPORT_DECL(void, VPADRead, int chan, VPADData *buffer, u32 buffer_size, s32 *error); 28 | 29 | void InitVPadFunctionPointers(void) 30 | { 31 | unsigned int *funcPointer = 0; 32 | unsigned int vpad_handle; 33 | OSDynLoad_Acquire("vpad.rpl", &vpad_handle); 34 | 35 | OS_FIND_EXPORT(vpad_handle, VPADRead); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /src/dynamic_libs/vpad_functions.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 3 | * by Dimok 4 | * 5 | * This software is provided 'as-is', without any express or implied 6 | * warranty. In no event will the authors be held liable for any 7 | * damages arising from the use of this software. 8 | * 9 | * Permission is granted to anyone to use this software for any 10 | * purpose, including commercial applications, and to alter it and 11 | * redistribute it freely, subject to the following restrictions: 12 | * 13 | * 1. The origin of this software must not be misrepresented; you 14 | * must not claim that you wrote the original software. If you use 15 | * this software in a product, an acknowledgment in the product 16 | * documentation would be appreciated but is not required. 17 | * 18 | * 2. Altered source versions must be plainly marked as such, and 19 | * must not be misrepresented as being the original software. 20 | * 21 | * 3. This notice may not be removed or altered from any source 22 | * distribution. 23 | ***************************************************************************/ 24 | #ifndef __VPAD_FUNCTIONS_H_ 25 | #define __VPAD_FUNCTIONS_H_ 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | #include 32 | 33 | #define VPAD_BUTTON_A 0x8000 34 | #define VPAD_BUTTON_B 0x4000 35 | #define VPAD_BUTTON_X 0x2000 36 | #define VPAD_BUTTON_Y 0x1000 37 | #define VPAD_BUTTON_LEFT 0x0800 38 | #define VPAD_BUTTON_RIGHT 0x0400 39 | #define VPAD_BUTTON_UP 0x0200 40 | #define VPAD_BUTTON_DOWN 0x0100 41 | #define VPAD_BUTTON_ZL 0x0080 42 | #define VPAD_BUTTON_ZR 0x0040 43 | #define VPAD_BUTTON_L 0x0020 44 | #define VPAD_BUTTON_R 0x0010 45 | #define VPAD_BUTTON_PLUS 0x0008 46 | #define VPAD_BUTTON_MINUS 0x0004 47 | #define VPAD_BUTTON_HOME 0x0002 48 | #define VPAD_BUTTON_SYNC 0x0001 49 | #define VPAD_BUTTON_STICK_R 0x00020000 50 | #define VPAD_BUTTON_STICK_L 0x00040000 51 | #define VPAD_BUTTON_TV 0x00010000 52 | 53 | #define VPAD_STICK_R_EMULATION_LEFT 0x04000000 54 | #define VPAD_STICK_R_EMULATION_RIGHT 0x02000000 55 | #define VPAD_STICK_R_EMULATION_UP 0x01000000 56 | #define VPAD_STICK_R_EMULATION_DOWN 0x00800000 57 | 58 | #define VPAD_STICK_L_EMULATION_LEFT 0x40000000 59 | #define VPAD_STICK_L_EMULATION_RIGHT 0x20000000 60 | #define VPAD_STICK_L_EMULATION_UP 0x10000000 61 | #define VPAD_STICK_L_EMULATION_DOWN 0x08000000 62 | 63 | 64 | typedef struct 65 | { 66 | f32 x,y; 67 | } Vec2D; 68 | 69 | typedef struct 70 | { 71 | u16 x, y; /* Touch coordinates */ 72 | u16 touched; /* 1 = Touched, 0 = Not touched */ 73 | u16 invalid; /* 0 = All valid, 1 = X invalid, 2 = Y invalid, 3 = Both invalid? */ 74 | } VPADTPData; 75 | 76 | typedef struct 77 | { 78 | u32 btns_h; /* Held buttons */ 79 | u32 btns_d; /* Buttons that are pressed at that instant */ 80 | u32 btns_r; /* Released buttons */ 81 | Vec2D lstick, rstick; /* Each contains 4-byte X and Y components */ 82 | char unknown1c[0x52 - 0x1c]; /* Contains accelerometer and gyroscope data somewhere */ 83 | VPADTPData tpdata; /* Normal touchscreen data */ 84 | VPADTPData tpdata1; /* Modified touchscreen data 1 */ 85 | VPADTPData tpdata2; /* Modified touchscreen data 2 */ 86 | char unknown6a[0xa0 - 0x6a]; 87 | uint8_t volume; 88 | uint8_t battery; /* 0 to 6 */ 89 | uint8_t unk_volume; /* One less than volume */ 90 | char unknowna4[0xac - 0xa4]; 91 | } VPADData; 92 | 93 | void InitVPadFunctionPointers(void); 94 | 95 | extern void (* VPADRead)(int chan, VPADData *buffer, u32 buffer_size, s32 *error); 96 | 97 | #ifdef __cplusplus 98 | } 99 | #endif 100 | 101 | #endif // __VPAD_FUNCTIONS_H_ 102 | -------------------------------------------------------------------------------- /src/entry.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dynamic_libs/os_functions.h" 3 | #include "dynamic_libs/sys_functions.h" 4 | #include "common/common.h" 5 | #include "utils/utils.h" 6 | #include "main.h" 7 | 8 | int __entry_menu(int argc, char **argv) 9 | { 10 | //! ******************************************************************* 11 | //! * Jump to our application * 12 | //! ******************************************************************* 13 | return Menu_Main(); 14 | } 15 | -------------------------------------------------------------------------------- /src/link.ld: -------------------------------------------------------------------------------- 1 | OUTPUT(ftpiiu.elf); 2 | 3 | /* Tell linker where our application entry is so the garbage collect can work correct */ 4 | ENTRY(__entry_menu); 5 | 6 | SECTIONS { 7 | . = 0x00802000; 8 | .text : { 9 | *(.text*); 10 | } 11 | .rodata : { 12 | *(.rodata*); 13 | } 14 | .data : { 15 | *(.data*); 16 | 17 | __sdata_start = .; 18 | *(.sdata*); 19 | __sdata_end = .; 20 | 21 | __sdata2_start = .; 22 | *(.sdata2*); 23 | __sdata2_end = .; 24 | } 25 | .bss : { 26 | __bss_start = .; 27 | *(.bss*); 28 | *(.sbss*); 29 | *(COMMON); 30 | __bss_end = .; 31 | } 32 | __CODE_END = .; 33 | 34 | /DISCARD/ : { 35 | *(*); 36 | } 37 | } 38 | 39 | /******************************************************** FS ********************************************************/ 40 | /* coreinit.rpl difference in addresses 0xFE3C00 */ 41 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017 FIX94 3 | * 4 | * This software may be modified and distributed under the terms 5 | * of the MIT license. See the LICENSE file for details. 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "dynamic_libs/os_functions.h" 15 | #include "dynamic_libs/sys_functions.h" 16 | #include "dynamic_libs/padscore_functions.h" 17 | #include "dynamic_libs/vpad_functions.h" 18 | #include "system/memory.h" 19 | #include "utils/utils.h" 20 | #include "common/common.h" 21 | 22 | static const char *verChar = "WiiU Video Mode Changer v1.0 by FIX94"; 23 | 24 | static unsigned int getButtonsDown(); 25 | 26 | #define OSScreenEnable(enable) OSScreenEnableEx(0, enable); OSScreenEnableEx(1, enable); 27 | #define OSScreenClearBuffer(tmp) OSScreenClearBufferEx(0, tmp); OSScreenClearBufferEx(1, tmp); 28 | #define OSScreenPutFont(x, y, buf) OSScreenPutFontEx(0, x, y, buf); OSScreenPutFontEx(1, x, y, buf); 29 | #define OSScreenFlipBuffers() OSScreenFlipBuffersEx(0); OSScreenFlipBuffersEx(1); 30 | 31 | const char *outPortStr[] = { "HDMI", "Component", "Composite/S-Video", "Composite/SCART" }; 32 | const char *outResStr[] = { "720p (fallback?)", "576i PAL50", "480i", "480p", "720p", "720p 3D?", "1080i", "1080p", "720p (again?)", "720p (No Signal from Component?)", "480i PAL60", "720p (yet again?)", "720p (50Hz, glitchy gamepad)", "1080i (50Hz, glitchy gamepad)", "1080p (50Hz, glitchy gamepad)" }; 33 | 34 | extern "C" int Menu_Main(void) 35 | { 36 | InitOSFunctionPointers(); 37 | InitSysFunctionPointers(); 38 | InitPadScoreFunctionPointers(); 39 | InitVPadFunctionPointers(); 40 | 41 | int screen_buf0_size, screen_buf1_size; 42 | uint8_t *screenBuffer; 43 | int redraw = 1; 44 | 45 | unsigned int avm_handle; 46 | OSDynLoad_Acquire("avm.rpl", &avm_handle); 47 | 48 | int (*AVMSetTVVideoRegion)(int r3, int r4, int r5); 49 | int (*AVMSetTVOutPort)(int r3, int r4); 50 | int (*AVMSetTVScanResolution)(int r3); 51 | int (*AVMDebugIsNTSC)(); 52 | int (*AVMGetCurrentPort)(int *port); 53 | 54 | OSDynLoad_FindExport(avm_handle, 0, "AVMSetTVVideoRegion", &AVMSetTVVideoRegion); 55 | OSDynLoad_FindExport(avm_handle, 0, "AVMSetTVOutPort", &AVMSetTVOutPort); 56 | OSDynLoad_FindExport(avm_handle, 0, "AVMSetTVScanResolution", &AVMSetTVScanResolution); 57 | OSDynLoad_FindExport(avm_handle, 0, "AVMDebugIsNTSC", &AVMDebugIsNTSC); 58 | OSDynLoad_FindExport(avm_handle, 0, "AVMGetCurrentPort", &AVMGetCurrentPort); 59 | 60 | bool isNTSC = !!AVMDebugIsNTSC(); 61 | bool wantNTSC = isNTSC; 62 | 63 | int outPort = 0; 64 | AVMGetCurrentPort(&outPort); 65 | if(outPort > 3) outPort = 0; 66 | int wantPort = outPort; 67 | 68 | //int outRes; //still need to get resolution somehow... 69 | int wantRes = 2; //default 480i 70 | if(outPort == 0) wantRes = 4; //720p from HDMI 71 | else if(outPort == 1) wantRes = 3; //480p from Component 72 | else if(outPort == 3) wantRes = 10; //480i PAL60 from Composite/SCART 73 | 74 | memoryInitialize(); 75 | 76 | OSScreenInit(); 77 | screen_buf0_size = OSScreenGetBufferSizeEx(0); 78 | screen_buf1_size = OSScreenGetBufferSizeEx(1); 79 | screenBuffer = (uint8_t*)MEMBucket_alloc(screen_buf0_size+screen_buf1_size, 0x100); 80 | OSScreenSetBufferEx(0, (void*)(screenBuffer)); 81 | OSScreenSetBufferEx(1, (void*)(screenBuffer + screen_buf0_size)); 82 | OSScreenEnable(1); 83 | 84 | KPADInit(); 85 | WPADEnableURCC(1); 86 | 87 | //garbage read 88 | getButtonsDown(); 89 | 90 | int curSel = 0; 91 | bool applyChanges = false; 92 | while(1) 93 | { 94 | usleep(25000); 95 | unsigned int btnDown = getButtonsDown(); 96 | 97 | if( btnDown & VPAD_BUTTON_HOME ) 98 | break; 99 | 100 | if( btnDown & VPAD_BUTTON_RIGHT ) 101 | { 102 | if(curSel == 0) //NTSC/PAL 103 | { 104 | wantNTSC = !wantNTSC; 105 | if(wantNTSC) 106 | { 107 | //no Composite/SCART 108 | if(wantPort == 3) 109 | { 110 | wantPort = 2; //Composite/S-Video 111 | wantRes = 2; //480i 112 | } 113 | } 114 | else //want PAL 115 | { 116 | //no Composite/S-Video 117 | if(wantPort == 2) 118 | { 119 | wantPort = 3; //Composite/SCART 120 | wantRes = 10; //480i PAL60 121 | } 122 | } 123 | } 124 | else if(curSel == 1) //Port 125 | { 126 | wantPort++; 127 | if(wantPort > 3) 128 | wantPort = 0; 129 | if(wantNTSC) 130 | { 131 | //no Composite/SCART 132 | if(wantPort == 3) 133 | wantPort = 0; 134 | } 135 | else //want PAL 136 | { 137 | //no Composite/S-Video 138 | if(wantPort == 2) 139 | wantPort = 3; 140 | } 141 | //Set default res for port 142 | if(wantPort == 0) wantRes = 4; //720p from HDMI 143 | else if(wantPort == 1) wantRes = 3; //480p from Component 144 | else if(wantPort == 2) wantRes = 2; //480i from Composite/S-Video 145 | else if(wantPort == 3) wantRes = 10; //480i PAL60 from Composite/SCART 146 | } 147 | else if(curSel == 2) //Resolution 148 | { 149 | wantRes++; 150 | if(wantRes > 14) 151 | wantRes = 1; 152 | } 153 | redraw = 1; 154 | } 155 | 156 | if( btnDown & VPAD_BUTTON_LEFT ) 157 | { 158 | if(curSel == 0) //NTSC/PAL 159 | { 160 | wantNTSC = !wantNTSC; 161 | if(wantNTSC) 162 | { 163 | //no Composite/SCART 164 | if(wantPort == 3) 165 | { 166 | wantPort = 2; //Composite/S-Video 167 | wantRes = 2; //480i 168 | } 169 | } 170 | else //want PAL 171 | { 172 | //no Composite/S-Video 173 | if(wantPort == 2) 174 | { 175 | wantPort = 3; //Composite/SCART 176 | wantRes = 10; //480i PAL60 177 | } 178 | } 179 | } 180 | else if(curSel == 1) //Port 181 | { 182 | wantPort--; 183 | if(wantPort < 0) 184 | wantPort = 3; 185 | if(wantNTSC) 186 | { 187 | //no Composite/SCART 188 | if(wantPort == 3) 189 | wantPort = 2; 190 | } 191 | else //want PAL 192 | { 193 | //no Composite/S-Video 194 | if(wantPort == 2) 195 | wantPort = 1; 196 | } 197 | //Set default res for port 198 | if(wantPort == 0) wantRes = 4; //720p from HDMI 199 | else if(wantPort == 1) wantRes = 3; //480p from Component 200 | else if(wantPort == 2) wantRes = 2; //480i from Composite/S-Video 201 | else if(wantPort == 3) wantRes = 10; //480i PAL60 from Composite/SCART 202 | } 203 | else if(curSel == 2) //Resolution 204 | { 205 | wantRes--; 206 | if(wantRes < 0) 207 | wantRes = 14; 208 | } 209 | redraw = 1; 210 | } 211 | 212 | if( btnDown & VPAD_BUTTON_DOWN ) 213 | { 214 | curSel++; 215 | if(curSel > 2) curSel = 0; 216 | redraw = 1; 217 | } 218 | 219 | if( btnDown & VPAD_BUTTON_UP ) 220 | { 221 | curSel--; 222 | if(curSel < 0) curSel = 2; 223 | redraw = 1; 224 | } 225 | 226 | if( btnDown & VPAD_BUTTON_A ) 227 | { 228 | applyChanges = true; 229 | break; 230 | } 231 | 232 | if( redraw ) 233 | { 234 | OSScreenClearBuffer(0); 235 | OSScreenPutFont(0, 0, verChar); 236 | char printStr[256]; 237 | sprintf(printStr,"%s Video Region: %s", (curSel==0)?">":" ", wantNTSC ? "NTSC" : "PAL"); 238 | OSScreenPutFont(0, 1, printStr); 239 | sprintf(printStr,"%s Output Port: %s", (curSel==1)?">":" ", outPortStr[wantPort]); 240 | OSScreenPutFont(0, 2, printStr); 241 | sprintf(printStr,"%s Output Resolution: %s", (curSel==2)?">":" ", outResStr[wantRes]); 242 | OSScreenPutFont(0, 3, printStr); 243 | OSScreenPutFont(0, 4, "Press A to apply settings."); 244 | OSScreenPutFont(0, 5, "Press Home to exit without changes."); 245 | OSScreenFlipBuffers(); 246 | redraw = 0; 247 | } 248 | } 249 | 250 | OSScreenClearBuffer(0); 251 | OSScreenFlipBuffers(); 252 | 253 | OSScreenEnable(0); 254 | MEMBucket_free(screenBuffer); 255 | 256 | memoryRelease(); 257 | 258 | if(applyChanges) 259 | { 260 | if((isNTSC && !wantNTSC) || (!isNTSC && wantNTSC)) 261 | AVMSetTVVideoRegion(wantNTSC ? 1 : 2, wantPort, wantRes); 262 | else if(outPort != wantPort) 263 | AVMSetTVOutPort(wantPort, wantRes); 264 | else //only set resolution 265 | AVMSetTVScanResolution(wantRes); 266 | } 267 | return EXIT_SUCCESS; 268 | } 269 | 270 | /* General Input Code */ 271 | 272 | static unsigned int wpadToVpad(unsigned int buttons) 273 | { 274 | unsigned int conv_buttons = 0; 275 | 276 | if(buttons & WPAD_BUTTON_LEFT) 277 | conv_buttons |= VPAD_BUTTON_LEFT; 278 | 279 | if(buttons & WPAD_BUTTON_RIGHT) 280 | conv_buttons |= VPAD_BUTTON_RIGHT; 281 | 282 | if(buttons & WPAD_BUTTON_DOWN) 283 | conv_buttons |= VPAD_BUTTON_DOWN; 284 | 285 | if(buttons & WPAD_BUTTON_UP) 286 | conv_buttons |= VPAD_BUTTON_UP; 287 | 288 | if(buttons & WPAD_BUTTON_PLUS) 289 | conv_buttons |= VPAD_BUTTON_PLUS; 290 | 291 | if(buttons & WPAD_BUTTON_2) 292 | conv_buttons |= VPAD_BUTTON_X; 293 | 294 | if(buttons & WPAD_BUTTON_1) 295 | conv_buttons |= VPAD_BUTTON_Y; 296 | 297 | if(buttons & WPAD_BUTTON_B) 298 | conv_buttons |= VPAD_BUTTON_B; 299 | 300 | if(buttons & WPAD_BUTTON_A) 301 | conv_buttons |= VPAD_BUTTON_A; 302 | 303 | if(buttons & WPAD_BUTTON_MINUS) 304 | conv_buttons |= VPAD_BUTTON_MINUS; 305 | 306 | if(buttons & WPAD_BUTTON_HOME) 307 | conv_buttons |= VPAD_BUTTON_HOME; 308 | 309 | return conv_buttons; 310 | } 311 | 312 | static unsigned int wpadClassicToVpad(unsigned int buttons) 313 | { 314 | unsigned int conv_buttons = 0; 315 | 316 | if(buttons & WPAD_CLASSIC_BUTTON_LEFT) 317 | conv_buttons |= VPAD_BUTTON_LEFT; 318 | 319 | if(buttons & WPAD_CLASSIC_BUTTON_RIGHT) 320 | conv_buttons |= VPAD_BUTTON_RIGHT; 321 | 322 | if(buttons & WPAD_CLASSIC_BUTTON_DOWN) 323 | conv_buttons |= VPAD_BUTTON_DOWN; 324 | 325 | if(buttons & WPAD_CLASSIC_BUTTON_UP) 326 | conv_buttons |= VPAD_BUTTON_UP; 327 | 328 | if(buttons & WPAD_CLASSIC_BUTTON_PLUS) 329 | conv_buttons |= VPAD_BUTTON_PLUS; 330 | 331 | if(buttons & WPAD_CLASSIC_BUTTON_X) 332 | conv_buttons |= VPAD_BUTTON_X; 333 | 334 | if(buttons & WPAD_CLASSIC_BUTTON_Y) 335 | conv_buttons |= VPAD_BUTTON_Y; 336 | 337 | if(buttons & WPAD_CLASSIC_BUTTON_B) 338 | conv_buttons |= VPAD_BUTTON_B; 339 | 340 | if(buttons & WPAD_CLASSIC_BUTTON_A) 341 | conv_buttons |= VPAD_BUTTON_A; 342 | 343 | if(buttons & WPAD_CLASSIC_BUTTON_MINUS) 344 | conv_buttons |= VPAD_BUTTON_MINUS; 345 | 346 | if(buttons & WPAD_CLASSIC_BUTTON_HOME) 347 | conv_buttons |= VPAD_BUTTON_HOME; 348 | 349 | if(buttons & WPAD_CLASSIC_BUTTON_ZR) 350 | conv_buttons |= VPAD_BUTTON_ZR; 351 | 352 | if(buttons & WPAD_CLASSIC_BUTTON_ZL) 353 | conv_buttons |= VPAD_BUTTON_ZL; 354 | 355 | if(buttons & WPAD_CLASSIC_BUTTON_R) 356 | conv_buttons |= VPAD_BUTTON_R; 357 | 358 | if(buttons & WPAD_CLASSIC_BUTTON_L) 359 | conv_buttons |= VPAD_BUTTON_L; 360 | 361 | return conv_buttons; 362 | } 363 | 364 | static unsigned int getButtonsDown() 365 | { 366 | unsigned int btnDown = 0; 367 | 368 | s32 vpadError = -1; 369 | VPADData vpad; 370 | VPADRead(0, &vpad, 1, &vpadError); 371 | if(vpadError == 0) 372 | btnDown |= vpad.btns_d; 373 | 374 | int i; 375 | for(i = 0; i < 4; i++) 376 | { 377 | u32 controller_type; 378 | if(WPADProbe(i, &controller_type) != 0) 379 | continue; 380 | KPADData kpadData; 381 | KPADRead(i, &kpadData, 1); 382 | if(kpadData.device_type <= 1) 383 | btnDown |= wpadToVpad(kpadData.btns_d); 384 | else 385 | btnDown |= wpadClassicToVpad(kpadData.classic.btns_d); 386 | } 387 | 388 | return btnDown; 389 | } 390 | -------------------------------------------------------------------------------- /src/main.h: -------------------------------------------------------------------------------- 1 | #ifndef _MAIN_H_ 2 | #define _MAIN_H_ 3 | 4 | #include "common/types.h" 5 | #include "dynamic_libs/os_functions.h" 6 | 7 | /* Main */ 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | //! C wrapper for our C++ functions 13 | int Menu_Main(void); 14 | 15 | #ifdef __cplusplus 16 | } 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /src/system/memory.c: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 Dimok 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | ****************************************************************************/ 17 | #include 18 | #include 19 | #include "dynamic_libs/os_functions.h" 20 | #include "common/common.h" 21 | #include "memory.h" 22 | 23 | #define MEMORY_ARENA_1 0 24 | #define MEMORY_ARENA_2 1 25 | #define MEMORY_ARENA_3 2 26 | #define MEMORY_ARENA_4 3 27 | #define MEMORY_ARENA_5 4 28 | #define MEMORY_ARENA_6 5 29 | #define MEMORY_ARENA_7 6 30 | #define MEMORY_ARENA_8 7 31 | #define MEMORY_ARENA_FG_BUCKET 8 32 | 33 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 34 | //! Memory functions 35 | //! This is the only place where those are needed so lets keep them more or less private 36 | //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 37 | extern unsigned int * pMEMAllocFromDefaultHeapEx; 38 | extern unsigned int * pMEMAllocFromDefaultHeap; 39 | extern unsigned int * pMEMFreeToDefaultHeap; 40 | 41 | extern int (* MEMGetBaseHeapHandle)(int mem_arena); 42 | extern unsigned int (* MEMGetAllocatableSizeForFrmHeapEx)(int heap, int align); 43 | extern void *(* MEMAllocFromFrmHeapEx)(int heap, unsigned int size, int align); 44 | extern void (* MEMFreeToFrmHeap)(int heap, int mode); 45 | extern void *(* MEMAllocFromExpHeapEx)(int heap, unsigned int size, int align); 46 | extern int (* MEMCreateExpHeapEx)(void* address, unsigned int size, unsigned short flags); 47 | extern void *(* MEMDestroyExpHeap)(int heap); 48 | extern void (* MEMFreeToExpHeap)(int heap, void* ptr); 49 | 50 | static int mem1_heap = -1; 51 | static int bucket_heap = -1; 52 | 53 | void memoryInitialize(void) 54 | { 55 | int mem1_heap_handle = MEMGetBaseHeapHandle(MEMORY_ARENA_1); 56 | unsigned int mem1_allocatable_size = MEMGetAllocatableSizeForFrmHeapEx(mem1_heap_handle, 4); 57 | void *mem1_memory = MEMAllocFromFrmHeapEx(mem1_heap_handle, mem1_allocatable_size, 4); 58 | if(mem1_memory) 59 | mem1_heap = MEMCreateExpHeapEx(mem1_memory, mem1_allocatable_size, 0); 60 | 61 | int bucket_heap_handle = MEMGetBaseHeapHandle(MEMORY_ARENA_FG_BUCKET); 62 | unsigned int bucket_allocatable_size = MEMGetAllocatableSizeForFrmHeapEx(bucket_heap_handle, 4); 63 | void *bucket_memory = MEMAllocFromFrmHeapEx(bucket_heap_handle, bucket_allocatable_size, 4); 64 | if(bucket_memory) 65 | bucket_heap = MEMCreateExpHeapEx(bucket_memory, bucket_allocatable_size, 0); 66 | } 67 | 68 | void memoryRelease(void) 69 | { 70 | MEMDestroyExpHeap(mem1_heap); 71 | MEMFreeToFrmHeap(MEMGetBaseHeapHandle(MEMORY_ARENA_1), 3); 72 | mem1_heap = -1; 73 | 74 | MEMDestroyExpHeap(bucket_heap); 75 | MEMFreeToFrmHeap(MEMGetBaseHeapHandle(MEMORY_ARENA_FG_BUCKET), 3); 76 | bucket_heap = -1; 77 | } 78 | 79 | //!------------------------------------------------------------------------------------------- 80 | //! wraps 81 | //!------------------------------------------------------------------------------------------- 82 | void *__wrap_malloc(size_t size) 83 | { 84 | // pointer to a function resolve 85 | return ((void * (*)(size_t))(*pMEMAllocFromDefaultHeap))(size); 86 | } 87 | 88 | void *__wrap_memalign(size_t align, size_t size) 89 | { 90 | if (align < 4) 91 | align = 4; 92 | 93 | // pointer to a function resolve 94 | return ((void * (*)(size_t, size_t))(*pMEMAllocFromDefaultHeapEx))(size, align); 95 | } 96 | 97 | void __wrap_free(void *p) 98 | { 99 | // pointer to a function resolve 100 | if(p != 0) 101 | ((void (*)(void *))(*pMEMFreeToDefaultHeap))(p); 102 | } 103 | 104 | void *__wrap_calloc(size_t n, size_t size) 105 | { 106 | void *p = __wrap_malloc(n * size); 107 | if (p != 0) { 108 | memset(p, 0, n * size); 109 | } 110 | return p; 111 | } 112 | 113 | size_t __wrap_malloc_usable_size(void *p) 114 | { 115 | //! TODO: this is totally wrong and needs to be addressed 116 | return 0x7FFFFFFF; 117 | } 118 | 119 | void *__wrap_realloc(void *p, size_t size) 120 | { 121 | void *new_ptr = __wrap_malloc(size); 122 | if (new_ptr != 0) 123 | { 124 | memcpy(new_ptr, p, __wrap_malloc_usable_size(p) < size ? __wrap_malloc_usable_size(p) : size); 125 | __wrap_free(p); 126 | } 127 | return new_ptr; 128 | } 129 | 130 | //!------------------------------------------------------------------------------------------- 131 | //! reent versions 132 | //!------------------------------------------------------------------------------------------- 133 | void *__wrap__malloc_r(struct _reent *r, size_t size) 134 | { 135 | return __wrap_malloc(size); 136 | } 137 | 138 | void *__wrap__calloc_r(struct _reent *r, size_t n, size_t size) 139 | { 140 | return __wrap_calloc(n, size); 141 | } 142 | 143 | void *__wrap__memalign_r(struct _reent *r, size_t align, size_t size) 144 | { 145 | return __wrap_memalign(align, size); 146 | } 147 | 148 | void __wrap__free_r(struct _reent *r, void *p) 149 | { 150 | __wrap_free(p); 151 | } 152 | 153 | size_t __wrap__malloc_usable_size_r(struct _reent *r, void *p) 154 | { 155 | return __wrap_malloc_usable_size(p); 156 | } 157 | 158 | void *__wrap__realloc_r(struct _reent *r, void *p, size_t size) 159 | { 160 | return __wrap_realloc(p, size); 161 | } 162 | 163 | //!------------------------------------------------------------------------------------------- 164 | //! some wrappers 165 | //!------------------------------------------------------------------------------------------- 166 | void * MEM2_alloc(unsigned int size, unsigned int align) 167 | { 168 | return __wrap_memalign(align, size); 169 | } 170 | 171 | void MEM2_free(void *ptr) 172 | { 173 | __wrap_free(ptr); 174 | } 175 | 176 | void * MEM1_alloc(unsigned int size, unsigned int align) 177 | { 178 | if (align < 4) 179 | align = 4; 180 | return MEMAllocFromExpHeapEx(mem1_heap, size, align); 181 | } 182 | 183 | void MEM1_free(void *ptr) 184 | { 185 | MEMFreeToExpHeap(mem1_heap, ptr); 186 | } 187 | 188 | void * MEMBucket_alloc(unsigned int size, unsigned int align) 189 | { 190 | if (align < 4) 191 | align = 4; 192 | return MEMAllocFromExpHeapEx(bucket_heap, size, align); 193 | } 194 | 195 | void MEMBucket_free(void *ptr) 196 | { 197 | MEMFreeToExpHeap(bucket_heap, ptr); 198 | } 199 | -------------------------------------------------------------------------------- /src/system/memory.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * Copyright (C) 2015 Dimok 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | ****************************************************************************/ 17 | #ifndef __MEMORY_H_ 18 | #define __MEMORY_H_ 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | #include 25 | 26 | void memoryInitialize(void); 27 | void memoryRelease(void); 28 | 29 | void * MEM2_alloc(unsigned int size, unsigned int align); 30 | void MEM2_free(void *ptr); 31 | 32 | void * MEM1_alloc(unsigned int size, unsigned int align); 33 | void MEM1_free(void *ptr); 34 | 35 | void * MEMBucket_alloc(unsigned int size, unsigned int align); 36 | void MEMBucket_free(void *ptr); 37 | 38 | #ifdef __cplusplus 39 | } 40 | #endif 41 | 42 | #endif // __MEMORY_H_ 43 | -------------------------------------------------------------------------------- /src/utils/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef __UTILS_H_ 2 | #define __UTILS_H_ 3 | 4 | #include 5 | #include "../common/types.h" 6 | 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | #define FlushBlock(addr) asm volatile("dcbf %0, %1\n" \ 12 | "icbi %0, %1\n" \ 13 | "sync\n" \ 14 | "eieio\n" \ 15 | "isync\n" \ 16 | : \ 17 | :"r"(0), "r"(((addr) & ~31)) \ 18 | :"memory", "ctr", "lr", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" \ 19 | ); 20 | 21 | #define LIMIT(x, min, max) \ 22 | ({ \ 23 | typeof( x ) _x = x; \ 24 | typeof( min ) _min = min; \ 25 | typeof( max ) _max = max; \ 26 | ( ( ( _x ) < ( _min ) ) ? ( _min ) : ( ( _x ) > ( _max ) ) ? ( _max) : ( _x ) ); \ 27 | }) 28 | 29 | #define DegToRad(a) ( (a) * 0.01745329252f ) 30 | #define RadToDeg(a) ( (a) * 57.29577951f ) 31 | 32 | #define ALIGN4(x) (((x) + 3) & ~3) 33 | #define ALIGN32(x) (((x) + 31) & ~31) 34 | 35 | // those work only in powers of 2 36 | #define ROUNDDOWN(val, align) ((val) & ~(align-1)) 37 | #define ROUNDUP(val, align) ROUNDDOWN(((val) + (align-1)), align) 38 | 39 | #define le16(i) ((((u16) ((i) & 0xFF)) << 8) | ((u16) (((i) & 0xFF00) >> 8))) 40 | #define le32(i) ((((u32)le16((i) & 0xFFFF)) << 16) | ((u32)le16(((i) & 0xFFFF0000) >> 16))) 41 | #define le64(i) ((((u64)le32((i) & 0xFFFFFFFFLL)) << 32) | ((u64)le32(((i) & 0xFFFFFFFF00000000LL) >> 32))) 42 | 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | 47 | #endif // __UTILS_H_ 48 | --------------------------------------------------------------------------------