From a93a2186c03c040a12ea04611b62870836f2d33e Mon Sep 17 00:00:00 2001 From: libretroadmin Date: Tue, 22 Nov 2022 06:08:17 +0100 Subject: [PATCH] (WiiU) Cleanups in aux code --- wiiu/hbl.c | 59 ++++++------------- wiiu/main.c | 9 +-- wiiu/shader_utils.c | 140 +++++++++++++++----------------------------- wiiu/shader_utils.h | 3 - 4 files changed, 68 insertions(+), 143 deletions(-) diff --git a/wiiu/hbl.c b/wiiu/hbl.c index 5024fe1746..e6c11c5aa8 100644 --- a/wiiu/hbl.c +++ b/wiiu/hbl.c @@ -83,7 +83,7 @@ static inline void memoryAddArea(int start, int end, int cur_index) void *getApplicationEndAddr(void) { extern u32 _end[]; - if((u32)_end >= 0x01000000) + if ((u32)_end >= 0x01000000) return APP_BASE_MEM; return _end; } @@ -91,6 +91,7 @@ void *getApplicationEndAddr(void) /* Create memory areas arrays */ static void memoryInitAreaTable(u32 args_size) { + int i = 0; u32 ApplicationMemoryEnd = (u32)getApplicationEndAddr() + args_size; /* This one seems to be available on every firmware and therefore its our code area but also our main RPX area behind our code */ @@ -100,7 +101,6 @@ static void memoryInitAreaTable(u32 args_size) const memory_values_t * mem_vals = mem_vals_540; /* Fill entries */ - int i = 0; while (mem_vals[i].start_address) { memoryAddArea(mem_vals[i].start_address, mem_vals[i].end_address, i + 1); @@ -124,8 +124,6 @@ static int HomebrewCopyMemory(u8 *address, u32 bytes, u32 args_size) if (*(u16 *)&address[7] != 0xCAFE) { /* assume ELF */ - printf("loading ELF file \n"); - ELF_DATA_ADDR = (u32)getApplicationEndAddr() + args_size; if (ELF_DATA_ADDR >= 0x01000000) return -1; @@ -133,8 +131,6 @@ static int HomebrewCopyMemory(u8 *address, u32 bytes, u32 args_size) else { /* RPX */ - printf("loading RPX file \n"); - ELF_DATA_ADDR = MEM_AREA_TABLE->address; } @@ -150,13 +146,14 @@ static int HomebrewCopyMemory(u8 *address, u32 bytes, u32 args_size) } else { + u32 done = 0; + u32 mapPosition = 0; + DCFlushRange(address, bytes); - u32 done = 0; u32 addressPhysical = (u32)OSEffectiveToPhysical(address); s_mem_area *mem_map = MEM_AREA_TABLE; - u32 mapPosition = 0; while ((done < bytes) && mem_map) { @@ -200,17 +197,17 @@ static int HomebrewCopyMemory(u8 *address, u32 bytes, u32 args_size) */ void log_rpx(const char *filepath, unsigned char *buf, size_t len) { + int i; unsigned int line_buffer[LINE_LEN]; - int i, offset; RARCH_LOG("=== BEGIN file=%s size=%d ===\n", filepath, len); for (i = 0; i < len; i++) { - offset = i % LINE_LEN; + int offset = i % LINE_LEN; line_buffer[offset] = buf[i]; - if(offset == (LINE_LEN-1)) + if (offset == (LINE_LEN-1)) { RARCH_LOG("%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n", line_buffer[0], line_buffer[1], line_buffer[2], line_buffer[3], @@ -223,7 +220,7 @@ void log_rpx(const char *filepath, unsigned char *buf, size_t len) line_buffer[28], line_buffer[29], line_buffer[30], line_buffer[31]); } } - if((len % LINE_LEN) != 0) + if ((len % LINE_LEN) != 0) { for (i = (LINE_LEN - (len % LINE_LEN)); i < LINE_LEN; i++) line_buffer[i] = 0; @@ -239,86 +236,66 @@ void log_rpx(const char *filepath, unsigned char *buf, size_t len) } RARCH_LOG("=== END %s ===\n", filepath); - } #endif int HBL_loadToMemory(const char *filepath, u32 args_size) { + int ret; + FILE *fp; + u32 bytesRead = 0; + u3 fileSize = 0; if (!filepath || !*filepath) return -1; - printf("Loading file %s\n", filepath); - - FILE *fp = fopen(filepath, "rb"); - - if (!fp) - { - printf("failed to open file %s\n", filepath); + if (!(fp = fopen(filepath, "rb"))) return -1; - } - u32 bytesRead = 0; fseek(fp, 0, SEEK_END); - u32 fileSize = ftell(fp); + fileSize = ftell(fp); fseek(fp, 0, SEEK_SET); size_t buffer_size = (fileSize + 0x3f) & ~0x3f; unsigned char *buffer = (unsigned char *) memalign(0x40, buffer_size); if (!buffer) - { - printf("Not enough memory\n"); return -1; - } memset(buffer, 0, buffer_size); /* Copy rpl in memory */ while (bytesRead < fileSize) { - printf("progress: %f \r", 100.0f * (f32)bytesRead / (f32)fileSize); - + int ret; u32 blockSize = 0x8000; if (blockSize > (fileSize - bytesRead)) blockSize = fileSize - bytesRead; - int ret = fread(buffer + bytesRead, 1, blockSize, fp); + ret = fread(buffer + bytesRead, 1, blockSize, fp); if (ret <= 0) - { - printf("Failure on reading file %s\n", filepath); break; - } bytesRead += ret; } - printf("progress: %f \n", 100.0f * (f32)bytesRead / (f32)fileSize); - if (bytesRead != fileSize) { free(buffer); - printf("File loading not finished for file %s, finished %" PRIi32 " of %" PRIi32 " bytes\n", filepath, bytesRead, - fileSize); - printf("File read failure"); return -1; } #ifdef WIIU_LOG_RPX log_rpx(filepath, buffer, bytesRead); #endif - int ret = HomebrewCopyMemory(buffer, bytesRead, args_size); + ret = HomebrewCopyMemory(buffer, bytesRead, args_size); free(buffer); if (ret < 0) - { - printf("Not enough memory"); return -1; - } return fileSize; } diff --git a/wiiu/main.c b/wiiu/main.c index 48150a7569..3377628f1c 100644 --- a/wiiu/main.c +++ b/wiiu/main.c @@ -163,13 +163,10 @@ static bool try_init_iosuhax(void) int result = IOSUHAX_Open(NULL); if (result < 0) result = MCPHookOpen(); - - if (result < 0) - return false; - return true; -#else /* don't HAVE_IOSUHAX */ + if (result >= 0) + return true; +#endif /* HAVE_IOSUHAX */ return false; -#endif } static void try_shutdown_iosuhax(void) diff --git a/wiiu/shader_utils.c b/wiiu/shader_utils.c index a06d1826fd..846e54f138 100644 --- a/wiiu/shader_utils.c +++ b/wiiu/shader_utils.c @@ -98,7 +98,8 @@ void GX2SetShader(GX2Shader *shader) GX2SetGeometryShader(&shader->gs); } -void dump_vs_data(GX2VertexShader* vs) +#if 0 +static void dump_vs_data(GX2VertexShader* vs) { unsigned i; @@ -153,7 +154,7 @@ void dump_vs_data(GX2VertexShader* vs) } } -void dump_ps_data(GX2PixelShader* ps) +static void dump_ps_data(GX2PixelShader* ps) { unsigned i; @@ -197,10 +198,9 @@ void dump_ps_data(GX2PixelShader* ps) DEBUG_INT(ps->samplerVars[i].type); DEBUG_INT(ps->samplerVars[i].location); } - } -void check_shader_verbose(u32 *shader, u32 shader_size, u32 *org, u32 org_size, const char *name) +static void check_shader_verbose(u32 *shader, u32 shader_size, u32 *org, u32 org_size, const char *name) { unsigned i; @@ -225,7 +225,8 @@ void check_shader_verbose(u32 *shader, u32 shader_size, u32 *org, u32 org_size, __builtin_bswap32(org[i])); } } -void check_shader(const void *shader_, u32 shader_size, const void *org_, u32 org_size, const char *name) + +static void check_shader(const void *shader_, u32 shader_size, const void *org_, u32 org_size, const char *name) { unsigned i; bool different = false; @@ -255,6 +256,7 @@ void check_shader(const void *shader_, u32 shader_size, const void *org_, u32 or printf("\n"); } +#endif #define MAKE_MAGIC(c0,c1,c2,c3) ((c0 << 24) |(c1 << 16) |(c2 << 8) |(c3 << 0)) @@ -326,7 +328,7 @@ typedef struct void gfd_free(GFDFile* gfd) { - if(gfd) + if (gfd) { MEM2_free(gfd->data); free(gfd); @@ -339,36 +341,24 @@ static bool gfd_relocate_block(GFDBlock* block) GFDRelocationHeader* rel = (GFDRelocationHeader*)(block->data + block->header.dataSize) - 1; if (rel->magic != GFD_BLOCK_RELOCATIONS_MAGIC) - { - printf("wrong relocations magic number.\n"); return false; - } - if((rel->patchOffset & GFD_RELOCATIONS_TYPE_MASK) != GFD_RELOCATIONS_DATA) - { - printf("wrong data relocations mask.\n"); + if ((rel->patchOffset & GFD_RELOCATIONS_TYPE_MASK) != GFD_RELOCATIONS_DATA) return false; - } u32* patches = (u32*)(block->data + (rel->patchOffset & GFD_RELOCATIONS_VALUE_MASK)); for (i = 0; i < rel->patchCount; i++) { - if(patches[i]) + if (patches[i]) { - if((patches[i] & GFD_RELOCATIONS_TYPE_MASK) != GFD_RELOCATIONS_DATA) - { - printf("wrong patch relocations mask.\n"); + if ((patches[i] & GFD_RELOCATIONS_TYPE_MASK) != GFD_RELOCATIONS_DATA) return false; - } u32* ptr = (u32*)(block->data + (patches[i] & GFD_RELOCATIONS_VALUE_MASK)); - if((((*ptr) & GFD_RELOCATIONS_TYPE_MASK) != GFD_RELOCATIONS_DATA) && + if ((((*ptr) & GFD_RELOCATIONS_TYPE_MASK) != GFD_RELOCATIONS_DATA) && (((*ptr) & GFD_RELOCATIONS_TYPE_MASK) != GFD_RELOCATIONS_TEXT)) - { - printf("wrong relocations mask.\n"); return false; - } *ptr = (u32)block->data + ((*ptr) & GFD_RELOCATIONS_VALUE_MASK); } } @@ -378,14 +368,15 @@ static bool gfd_relocate_block(GFDBlock* block) GFDFile *gfd_open(const char *filename) { - GFDFile* gfd = calloc(1, sizeof(*gfd)); + int size; + GFDFile* gfd = (GFDFile*)calloc(1, sizeof(*gfd)); FILE *fp = fopen(filename, "rb"); if (!fp) goto error; fseek(fp, 0, SEEK_END); - int size = ftell(fp); + size = ftell(fp); fseek(fp, 0, SEEK_SET); gfd->data = MEM2_alloc(size, GX2_SHADER_ALIGNMENT); fread(gfd->data, 1, size, fp); @@ -394,130 +385,93 @@ GFDFile *gfd_open(const char *filename) GFDFileHeader *header = (GFDFileHeader *)gfd->data; if (header->magic != GFD_FILE_MAGIC) - { - printf("wrong file magic number.\n"); goto error; - } if (header->headerSize != sizeof(GFDFileHeader)) - { - printf("wrong file header size.\n"); goto error; - } if (header->majorVersion != GFD_FILE_MAJOR_VERSION) - { - printf("file version not supported.\n"); goto error; - } if (header->gpuVersion != GFD_FILE_GPU_VERSION) - { - printf("gpu version not supported.\n"); goto error; - } if (!header->align) - { - printf("data is not aligned.\n"); goto error; - } GFDBlock *block = (GFDBlock *)(gfd->data + header->headerSize); while (block->header.type != GFD_BLOCK_TYPE_END_OF_FILE) { if (block->header.magic != GFD_BLOCK_MAGIC) - { - printf("wrong block magic number.\n"); goto error; - } if (block->header.headerSize != sizeof(GFDBlockHeader)) - { - printf("wrong block header size.\n"); goto error; - } if (block->header.majorVersion != GFD_BLOCK_MAJOR_VERSION) - { - printf("block version not supported.\n"); goto error; - } switch (block->header.type) { - case GFD_BLOCK_TYPE_VERTEX_SHADER_HEADER: - if (gfd->vs) - continue; + case GFD_BLOCK_TYPE_VERTEX_SHADER_HEADER: + if (gfd->vs) + continue; - gfd->vs = (GX2VertexShader*)block->data; - if(!gfd_relocate_block(block)) - goto error; + gfd->vs = (GX2VertexShader*)block->data; + if (!gfd_relocate_block(block)) + goto error; - break; + break; - case GFD_BLOCK_TYPE_VERTEX_SHADER_PROGRAM: - if(gfd->vs->program) - continue; + case GFD_BLOCK_TYPE_VERTEX_SHADER_PROGRAM: + if (gfd->vs->program) + continue; - GX2Invalidate(GX2_INVALIDATE_MODE_CPU_SHADER, block->data, block->header.dataSize); - gfd->vs->program = block->data; - break; + GX2Invalidate(GX2_INVALIDATE_MODE_CPU_SHADER, block->data, block->header.dataSize); + gfd->vs->program = block->data; + break; - case GFD_BLOCK_TYPE_PIXEL_SHADER_HEADER: - if (gfd->ps) - continue; + case GFD_BLOCK_TYPE_PIXEL_SHADER_HEADER: + if (gfd->ps) + continue; - gfd->ps = (GX2PixelShader*)block->data; - if(!gfd_relocate_block(block)) - goto error; + gfd->ps = (GX2PixelShader*)block->data; + if (!gfd_relocate_block(block)) + goto error; - break; + break; - case GFD_BLOCK_TYPE_PIXEL_SHADER_PROGRAM: - if(gfd->ps->program) - continue; + case GFD_BLOCK_TYPE_PIXEL_SHADER_PROGRAM: + if (gfd->ps->program) + continue; - GX2Invalidate(GX2_INVALIDATE_MODE_CPU_SHADER, block->data, block->header.dataSize); - gfd->ps->program = block->data; - break; + GX2Invalidate(GX2_INVALIDATE_MODE_CPU_SHADER, block->data, block->header.dataSize); + gfd->ps->program = block->data; + break; - default: - break; + default: + break; } block = (GFDBlock *)((u8 *)block + block->header.headerSize + block->header.dataSize); } - if(!gfd->vs) - { - printf("vertex shader is missing.\n"); + if (!gfd->vs) goto error; - } - if(!gfd->vs->program) - { - printf("vertex shader program is missing.\n"); + if (!gfd->vs->program) goto error; - } - if(!gfd->ps) - { - printf("pixel shader is missing.\n"); + if (!gfd->ps) goto error; - } - if(!gfd->ps->program) - { - printf("pixel shader program is missing.\n"); + if (!gfd->ps->program) goto error; - } return gfd; error: - printf("failed to open file : %s\n", filename); gfd_free(gfd); return NULL; diff --git a/wiiu/shader_utils.h b/wiiu/shader_utils.h index 847030c051..1d44aa48fc 100644 --- a/wiiu/shader_utils.h +++ b/wiiu/shader_utils.h @@ -73,9 +73,6 @@ void GX2InitShader(GX2Shader* shader); void GX2DestroyShader(GX2Shader* shader); void GX2SetShader(GX2Shader* shader); -void check_shader(const void* shader_, u32 shader_size, const void* org_, u32 org_size, const char* name); -void check_shader_verbose(u32* shader, u32 shader_size, u32* org, u32 org_size, const char* name); - typedef struct { GX2VertexShader* vs;