From ad0571d4205a35bf4b238c2b8453beec08830bf9 Mon Sep 17 00:00:00 2001 From: Ry Date: Thu, 23 Jun 2022 14:15:52 -0700 Subject: [PATCH] Add ryfs_get_size --- fox32rom.def | 1 + main.asm | 1 + ryfs.asm | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/fox32rom.def b/fox32rom.def index eb01e0d..82fdd79 100644 --- a/fox32rom.def +++ b/fox32rom.def @@ -61,6 +61,7 @@ ryfs_open: jmp [0xF0045008] ryfs_seek: jmp [0xF004500C] ryfs_read: jmp [0xF0045010] ryfs_read_whole_file: jmp [0xF0045014] +ryfs_get_size: jmp [0xF0045018] ; memory copy/compare jump table copy_memory_bytes: jmp [0xF0046000] diff --git a/main.asm b/main.asm index f058851..74c619a 100644 --- a/main.asm +++ b/main.asm @@ -208,6 +208,7 @@ get_rom_version: data.32 ryfs_seek data.32 ryfs_read data.32 ryfs_read_whole_file + data.32 ryfs_get_size ; memory copy/compare jump table org.pad 0xF0046000 diff --git a/ryfs.asm b/ryfs.asm index 3e66c9c..75571e7 100644 --- a/ryfs.asm +++ b/ryfs.asm @@ -267,3 +267,53 @@ ryfs_read_whole_file_last_sector: pop r1 pop r0 ret + +; get the exact size of a file +; inputs: +; r0: pointer to file struct +; outputs: +; r0: file size in bytes +ryfs_get_size: + push r1 + push r2 + push r10 + push r11 + + mov r10, r0 + mov r11, 0 ; byte counter + + ; read the first sector into the temp buffer + movz.8 r1, [r0] ; file_disk + inc r0 + movz.16 r0, [r0] ; file_first_sector +ryfs_get_size_sector_loop: + mov r2, TEMP_SECTOR_BUF + call read_sector + + ; check to see if this is the last sector + ; if this is the last sector, read the sector size field in the header + mov r0, TEMP_SECTOR_BUF + add r0, 2 + cmp.16 [r0], 0 + ifz jmp ryfs_get_size_last_sector + + ; there are more sectors left, load them + movz.16 r0, [r0] ; sector number + mov r1, [r10] ; file_disk + add r11, 506 + jmp ryfs_get_size_sector_loop +ryfs_get_size_last_sector: + ; this is the last sector, read the header to get the reamining number of bytes + mov r0, TEMP_SECTOR_BUF + add r0, 4 + movz.16 r0, [r0] + add r11, r0 + + ; return final size + mov r0, r11 + + pop r11 + pop r10 + pop r2 + pop r1 + ret