diff --git a/Makefile b/Makefile index 4fec1c36..ea40eaaf 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ PROC_S := arm5te PROC_LD := v5te LCF_TEMPLATE := ARM9-TS.lcf.template LIBS := -Llib -lsyscall -nostdlib -OPTFLAGS := -O4,p +OPTFLAGS := -O4,s include config.mk diff --git a/asm/include/main.inc b/asm/include/main.inc index bd851dd3..59e4c503 100644 --- a/asm/include/main.inc +++ b/asm/include/main.inc @@ -5,9 +5,7 @@ .public DecompressAtFromMemoryPointer .public DivideInt .public FileInit -.public GetFileLengthInPack .public IntToFloat -.public LoadFileInPack .public MultiplyFloat .public Sprintf .public Sqrtf diff --git a/asm/include/main_0200C484.inc b/asm/include/main_0200C4CC.inc similarity index 99% rename from asm/include/main_0200C484.inc rename to asm/include/main_0200C4CC.inc index a4d21970..0bdd8514 100644 --- a/asm/include/main_0200C484.inc +++ b/asm/include/main_0200C4CC.inc @@ -195,4 +195,4 @@ .public sub_0200B768 .public sub_0200C020 .public SVC_WaitByLoop -.public SVC_CpuSet +.public SVC_CpuSet \ No newline at end of file diff --git a/asm/main.s b/asm/main.s index bca9ccb7..448ea054 100644 --- a/asm/main.s +++ b/asm/main.s @@ -14402,131 +14402,3 @@ _0200C2D0: .word 0x02094B54 _0200C2D4: .word 0x02094B58 _0200C2D8: .word 0x02094B60 arm_func_end FatalError - - arm_func_start OpenAllPackFiles -OpenAllPackFiles: ; 0x0200C2DC - stmdb sp!, {r3, r4, r5, r6, r7, lr} - mov r0, #0x1f8 - mov r1, #0 - bl MemAlloc - ldr r2, _0200C334 ; =0x020AF69C - mov r1, #0x1f8 - str r0, [r2] - bl MemZero - ldr r6, _0200C338 ; =0x020AF6A0 - ldr r5, _0200C334 ; =0x020AF69C - mov r7, #0 - mov r4, #0x54 -_0200C30C: - ldr r0, [r5] - ldr r1, [r6, r7, lsl #2] - mla r0, r7, r4, r0 - bl OpenPackFile - add r0, r7, #1 - mov r0, r0, lsl #0x10 - mov r7, r0, asr #0x10 - cmp r7, #6 - blt _0200C30C - ldmia sp!, {r3, r4, r5, r6, r7, pc} - .align 2, 0 -_0200C334: .word 0x020AF69C -_0200C338: .word 0x020AF6A0 - arm_func_end OpenAllPackFiles - - arm_func_start GetFileLengthInPackWithPackNb -GetFileLengthInPackWithPackNb: ; 0x0200C33C - ldr r3, _0200C354 ; =0x020AF69C - mov r2, #0x54 - ldr r3, [r3] - ldr ip, _0200C358 ; =GetFileLengthInPack - mla r0, r2, r0, r3 - bx ip - .align 2, 0 -_0200C354: .word 0x020AF69C -_0200C358: .word GetFileLengthInPack - arm_func_end GetFileLengthInPackWithPackNb - - arm_func_start LoadFileInPackWithPackId -LoadFileInPackWithPackId: ; 0x0200C35C - stmdb sp!, {r3, lr} - ldr r3, _0200C384 ; =0x020AF69C - mov ip, r1 - ldr r3, [r3] - mov r1, #0x54 - mla r0, r1, r0, r3 - mov r1, r2 - mov r2, ip - bl LoadFileInPack - ldmia sp!, {r3, pc} - .align 2, 0 -_0200C384: .word 0x020AF69C - arm_func_end LoadFileInPackWithPackId - - arm_func_start AllocAndLoadFileInPack -AllocAndLoadFileInPack: ; 0x0200C388 - stmdb sp!, {r3, r4, r5, r6, r7, lr} - mov ip, #0x54 - mul r4, r0, ip - ldr r0, _0200C3DC ; =0x020AF69C - mov r6, r2 - ldr r0, [r0] - mov r5, r3 - add r0, r0, r4 - mov r7, r1 - bl GetFileLengthInPack - str r0, [r6, #4] - mov r1, r5 - bl MemAlloc - str r0, [r6] - ldr r0, _0200C3DC ; =0x020AF69C - ldr r1, [r6] - ldr r0, [r0] - mov r2, r7 - add r0, r0, r4 - bl LoadFileInPack - ldmia sp!, {r3, r4, r5, r6, r7, pc} - .align 2, 0 -_0200C3DC: .word 0x020AF69C - arm_func_end AllocAndLoadFileInPack - - arm_func_start OpenPackFile -OpenPackFile: ; 0x0200C3E0 - stmdb sp!, {r3, r4, r5, r6, lr} - sub sp, sp, #4 - mov r6, r0 - mov r5, r1 - bl DataTransferInit - add r0, r6, #0xc - bl FileInitVeneer - mov r1, r5 - add r0, r6, #0xc - bl FileOpen - add r0, r6, #0xc - add r1, r6, #4 - mov r2, #8 - bl FileRead - add r0, r6, #0xc - mov r1, #8 - mov r2, #0 - bl FileSeek - ldr r0, [r6, #8] - mov r1, #0 - mov r4, r0, lsl #3 - mov r0, r4 - bl MemAlloc - str r0, [r6] - ldr r1, [r6] - add r0, r6, #0xc - mov r2, r4 - bl FileRead - str r5, [sp] - ldr r0, _0200C470 ; =0x02094BE4 - mov r3, r4 - ldmib r6, {r1, r2} - bl DebugPrint0__0200C1FC - bl DataTransferStop - add sp, sp, #4 - ldmia sp!, {r3, r4, r5, r6, pc} - .align 2, 0 -_0200C470: .word 0x02094BE4 - arm_func_end OpenPackFile diff --git a/asm/main_0200C484.s b/asm/main_0200C4CC.s similarity index 99% rename from asm/main_0200C484.s rename to asm/main_0200C4CC.s index fbfeab24..6f694e1f 100644 --- a/asm/main_0200C484.s +++ b/asm/main_0200C4CC.s @@ -1,30 +1,8 @@ .include "asm/macros.inc" - .include "main_0200C484.inc" + .include "main_0200C4CC.inc" .text - arm_func_start LoadFileInPack -LoadFileInPack: ; 0x0200C484 - stmdb sp!, {r4, r5, r6, lr} - mov r6, r0 - mov r5, r1 - mov r4, r2 - bl DataTransferInit - ldr r2, [r6] - add r0, r6, #0xc - ldr r1, [r2, r4, lsl #3] - add r4, r2, r4, lsl #3 - mov r2, #0 - bl FileSeek - ldr r2, [r4, #4] - mov r1, r5 - add r0, r6, #0xc - bl FileRead - bl DataTransferStop - ldr r0, [r4, #4] - ldmia sp!, {r4, r5, r6, pc} - arm_func_end LoadFileInPack - arm_func_start sub_0200C4CC sub_0200C4CC: ; 0x0200C4CC stmdb sp!, {r4, lr} @@ -163265,17 +163243,43 @@ _020908D0: .word 0x020AEEF0 .byte 0x20, 0x20, 0x50, 0x72, 0x69, 0x6E, 0x74, 0x20, 0x20, 0x00, 0x00, 0x00, 0x21, 0x21, 0x21, 0x21 .byte 0x21, 0x20, 0x46, 0x61, 0x74, 0x61, 0x6C, 0x20, 0x21, 0x21, 0x21, 0x21, 0x21, 0x0A, 0x00, 0x00 .byte 0x0A, 0x00, 0x00, 0x00, 0x28, 0x4E, 0x55, 0x4C, 0x4C, 0x29, 0x0A, 0x00, 0x25, 0x73, 0x0A, 0x00 - .byte 0x45, 0x46, 0x46, 0x45, 0x43, 0x54, 0x2F, 0x65, 0x66, 0x66, 0x65, 0x63, 0x74, 0x2E, 0x62, 0x69 - .byte 0x6E, 0x00, 0x00, 0x00, 0x4D, 0x4F, 0x4E, 0x53, 0x54, 0x45, 0x52, 0x2F, 0x6D, 0x6F, 0x6E, 0x73 - .byte 0x74, 0x65, 0x72, 0x2E, 0x62, 0x69, 0x6E, 0x00, 0x42, 0x41, 0x4C, 0x41, 0x4E, 0x43, 0x45, 0x2F - .byte 0x6D, 0x5F, 0x6C, 0x65, 0x76, 0x65, 0x6C, 0x2E, 0x62, 0x69, 0x6E, 0x00, 0x44, 0x55, 0x4E, 0x47 - .byte 0x45, 0x4F, 0x4E, 0x2F, 0x64, 0x75, 0x6E, 0x67, 0x65, 0x6F, 0x6E, 0x2E, 0x62, 0x69, 0x6E, 0x00 - .byte 0x4D, 0x4F, 0x4E, 0x53, 0x54, 0x45, 0x52, 0x2F, 0x6D, 0x5F, 0x61, 0x74, 0x74, 0x61, 0x63, 0x6B - .byte 0x2E, 0x62, 0x69, 0x6E, 0x00, 0x00, 0x00, 0x00, 0x4D, 0x4F, 0x4E, 0x53, 0x54, 0x45, 0x52, 0x2F - .byte 0x6D, 0x5F, 0x67, 0x72, 0x6F, 0x75, 0x6E, 0x64, 0x2E, 0x62, 0x69, 0x6E, 0x00, 0x00, 0x00, 0x00 - .byte 0x66, 0x69, 0x6C, 0x65, 0x20, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x6F, 0x72, 0x79, 0x20, 0x69 - .byte 0x6E, 0x69, 0x74, 0x20, 0x25, 0x34, 0x64, 0x20, 0x25, 0x34, 0x64, 0x20, 0x25, 0x30, 0x38, 0x78 - .byte 0x20, 0x25, 0x73, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + + .global STRING_EFFECT_EFFECT_BIN ; 0x02094b64 + STRING_EFFECT_EFFECT_BIN: + .string "EFFECT/effect.bin" + .align 4 + + .global STRING_MONSTER_MONSTER_BIN ; 0x02094b78 + STRING_MONSTER_MONSTER_BIN: + .string "MONSTER/monster.bin" + .align 4 + + .global STRING_BALANCE_M_LEVEL_BIN ; 0x02094b8c + STRING_BALANCE_M_LEVEL_BIN: + .string "BALANCE/m_level.bin" + .align 4 + + .global STRING_DUNGEON_DUNGEON_BIN ; 0x02094ba0 + STRING_DUNGEON_DUNGEON_BIN: + .string "DUNGEON/dungeon.bin" + .align 4 + + .global STRING_MONSTER_M_ATTACK_BIN ; 0x02094bb4 + STRING_MONSTER_M_ATTACK_BIN: + .string "MONSTER/m_attack.bin" + .align 4 + + .global STRING_MONSTER_M_GROUND_BIN ; 0x02094bcc + STRING_MONSTER_M_GROUND_BIN: + .string "MONSTER/m_ground.bin" + .align 4 + + .global STRING_FILE_DIRECTORY_INIT ; 0x02094be4 + STRING_FILE_DIRECTORY_INIT: + .string "file directory init %4d %4d %08x %s\n" + + ; 0x02094c09 + .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 .byte 0x01, 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 .byte 0x00, 0x00, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 @@ -170100,9 +170104,25 @@ _020908D0: .word 0x020AEEF0 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 - .byte 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x4B, 0x09, 0x02 - .byte 0xB4, 0x4B, 0x09, 0x02, 0xCC, 0x4B, 0x09, 0x02, 0x64, 0x4B, 0x09, 0x02, 0xA0, 0x4B, 0x09, 0x02 - .byte 0x8C, 0x4B, 0x09, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4D, 0x09, 0x02, 0x00, 0x00, 0x00, 0x00 + .byte 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + .global PACK_FILES_OPENED ; 0x20af69c + PACK_FILES_OPENED: + ; [Runtime] A pointer to the 6 opened Pack files (listed at PACK_FILE_PATHS_TABLE) + .word 0x0 + + ; List of pointers to path strings to all known pack files. + ; The game uses this table to load its resources when launching dungeon mode. + .global PACK_FILE_PATHS_TABLE ; 0x020af6a0 + PACK_FILE_PATHS_TABLE: + .word STRING_MONSTER_MONSTER_BIN + .word STRING_MONSTER_M_ATTACK_BIN + .word STRING_MONSTER_M_GROUND_BIN + .word STRING_EFFECT_EFFECT_BIN + .word STRING_DUNGEON_DUNGEON_BIN + .word STRING_BALANCE_M_LEVEL_BIN + + ; 0x020af6b8 + .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x4D, 0x09, 0x02, 0x00, 0x00, 0x00, 0x00 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 .byte 0x00, 0x00, 0x00, 0x00, 0xAC, 0x8D, 0x09, 0x02, 0xC8, 0x8D, 0x09, 0x02, 0x00, 0x00, 0x00, 0x00 diff --git a/common.mk b/common.mk index 659bde70..07b17ce1 100644 --- a/common.mk +++ b/common.mk @@ -117,7 +117,7 @@ XMAP := $(NEF).xMAP EXCCFLAGS := -Cpp_exceptions off -MWCFLAGS = $(DEFINES) $(OPTFLAGS) -enum int -lang c99 $(EXCCFLAGS) -gccext,on -proc $(PROC) -msgstyle gcc -gccinc -i ./include -i ./include/library -i $(WORK_DIR)/files -I$(WORK_DIR)/lib/include -ipa file -interworking -inline on,noauto -char signed -W all -W pedantic -W noimpl_signedunsigned -W noimplicitconv -W nounusedarg -W nomissingreturn -W error +MWCFLAGS = $(DEFINES) $(OPTFLAGS) -enum int -lang c99 $(EXCCFLAGS) -gccext,on -proc $(PROC) -msgstyle gcc -gccinc -i ./include -i ./include/library -i $(WORK_DIR)/files -I$(WORK_DIR)/lib/include -interworking -inline on,noauto -char signed -W all -W pedantic -W noimpl_signedunsigned -W noimplicitconv -W nounusedarg -W nomissingreturn -W error MWASFLAGS = $(DEFINES) -proc $(PROC_S) -gccinc -i . -i ./include -i $(WORK_DIR)/asm/include -i $(WORK_DIR)/files -i $(WORK_DIR)/lib/asm/include -i $(WORK_DIR)/lib/syscall/asm/include -I$(WORK_DIR)/lib/include -DSDK_ASM MWLDFLAGS := -proc $(PROC) -nopic -nopid -interworking -map closure,unused -symtab sort -m _start -msgstyle gcc diff --git a/include/files/pack.h b/include/files/pack.h index c13065b9..337c6df3 100644 --- a/include/files/pack.h +++ b/include/files/pack.h @@ -21,4 +21,20 @@ struct pack_file_opened { struct file_stream opened_file; }; +// Correspond to a .bin pack archive +enum pack_file_id { + PACK_ARCHIVE_MONSTER = 0, + PACK_ARCHIVE_M_ATTACK = 1, + PACK_ARCHIVE_M_GROUND = 2, + PACK_ARCHIVE_EFFECT = 3, + PACK_ARCHIVE_DUNGEON = 4, + PACK_ARCHIVE_M_LEVEL = 5 +}; + +// Contain return result of the AllocAndLoadFileInPack function +struct pack_alloc_and_load_result { + void* data; + u32 length; +}; + #endif \ No newline at end of file diff --git a/include/main_pack.h b/include/main_pack.h index 1afc1e9d..aef5f17f 100644 --- a/include/main_pack.h +++ b/include/main_pack.h @@ -3,6 +3,12 @@ #include +void OpenAllPackFiles(void); +u32 GetFileLengthInPackWithPackNb(enum pack_file_id pack_id, u32 file_index); +u32 LoadFileInPackWithPackId(enum pack_file_id pack_id, u32 file_index, void* output_buffer); +void AllocAndLoadFileInPack(enum pack_file_id pack_id, u32 file_index, struct pack_alloc_and_load_result *output, u32 malloc_flags); +void OpenPackFile(struct pack_file_opened *pack_file,char *file_path); u32 GetFileLengthInPack(struct pack_file_opened* pack_file, u32 file_index); +u32 LoadFileInPack(struct pack_file_opened *pack_file,void *output_buffer,u32 file_index); #endif //PMDSKY_MAIN_PACK_H diff --git a/main.lsf b/main.lsf index 15680326..2dc489cc 100644 --- a/main.lsf +++ b/main.lsf @@ -11,7 +11,7 @@ Static main StackSize 0 2048 Object asm/main.o Object src/main_pack.o - Object asm/main_0200C484.o + Object asm/main_0200C4CC.o } Autoload ITCM diff --git a/src/main_pack.c b/src/main_pack.c index 7da99718..60ad6eee 100644 --- a/src/main_pack.c +++ b/src/main_pack.c @@ -1,6 +1,72 @@ #include "main_pack.h" -u32 GetFileLengthInPack(struct pack_file_opened* pack_file, u32 file_index) { - struct pack_file_table_of_content* element = &pack_file->table_of_content[file_index]; - return element->length; +extern void DataTransferInit(void); +extern void DataTransferStop(void); +extern void FileSeek(struct file_stream* file, int offset, int whence); +extern u32 FileRead(struct file_stream* file, void* buf, u32 size); +extern void FileInitVeneer(struct file_stream* file); +extern char STRING_FILE_DIRECTORY_INIT; +extern void DebugPrint0__0200C1FC(const char* fmt, ...); +extern void FileOpen(struct file_stream* file, const char* filepath); +extern void* MemAlloc(u32 len, u32 flags); +extern void MemZero(void* ptr, u32 len); + +extern struct pack_file_opened *PACK_FILES_OPENED; +extern char* PACK_FILE_PATHS_TABLE[6]; + +void OpenAllPackFiles(void) { + u32 total_size = sizeof(struct pack_file_opened[6]); + PACK_FILES_OPENED = MemAlloc(total_size, 0); + MemZero(PACK_FILES_OPENED, total_size); + for (s16 i = 0; i < 6; i++) { + OpenPackFile(&PACK_FILES_OPENED[i], PACK_FILE_PATHS_TABLE[i]); + }; +} + +u32 GetFileLengthInPackWithPackNb(enum pack_file_id pack_id, u32 file_index) { + struct pack_file_opened* pack_file = &PACK_FILES_OPENED[pack_id]; + return GetFileLengthInPack(pack_file, file_index); +} + +u32 LoadFileInPackWithPackId(enum pack_file_id pack_id, u32 file_index, void* output_buffer) { + return LoadFileInPack(&PACK_FILES_OPENED[pack_id], output_buffer, file_index); +} + +void AllocAndLoadFileInPack(enum pack_file_id pack_id, u32 file_index, struct pack_alloc_and_load_result *output, u32 malloc_flags) { + u32 entry_length = GetFileLengthInPack(&PACK_FILES_OPENED[pack_id], file_index); + output->length = entry_length; + void * data_dest = MemAlloc(output->length, malloc_flags); + output->data = data_dest; + LoadFileInPack(&PACK_FILES_OPENED[pack_id], output->data, file_index); + return; +} + +void OpenPackFile(struct pack_file_opened *pack_file, char *file_path) { + DataTransferInit(); + FileInitVeneer(&pack_file->opened_file); + FileOpen(&pack_file->opened_file,file_path); + FileRead(&pack_file->opened_file,&pack_file->zero,8); + FileSeek(&pack_file->opened_file,8,0); + u32 toc_total_size = pack_file->nb_entry_table_of_content * sizeof(struct pack_file_table_of_content); + struct pack_file_table_of_content *toc_alloc = (struct pack_file_table_of_content *) MemAlloc(toc_total_size, 0); + pack_file->table_of_content = toc_alloc; + FileRead(&pack_file->opened_file,pack_file->table_of_content,toc_total_size); + DebugPrint0__0200C1FC(&STRING_FILE_DIRECTORY_INIT,pack_file->zero, + pack_file->nb_entry_table_of_content,toc_total_size,file_path); + DataTransferStop(); + return; +} + +u32 GetFileLengthInPack(struct pack_file_opened* pack_file, u32 file_index) { + struct pack_file_table_of_content* element = &pack_file->table_of_content[file_index]; + return element->length; +} + +u32 LoadFileInPack(struct pack_file_opened *pack_file,void *output_buffer,u32 file_index) { + DataTransferInit(); + struct pack_file_table_of_content *toc_entry = &pack_file->table_of_content[file_index]; + FileSeek(&pack_file->opened_file,toc_entry->offset,0); + FileRead(&pack_file->opened_file,output_buffer,toc_entry->length); + DataTransferStop(); + return toc_entry->length; }