blocksds / sdk

Main BlocksDS SDK repository
https://blocksds.github.io/docs/
155 stars 8 forks source link

NitroFS fread() returning 0 with feof() and ferror() both zero #173

Closed PoroCYon closed 3 months ago

PoroCYon commented 3 months ago

I'm having this situation where, using fread() (for a file stored on NitroFS), the first call (reading 487 bytes) returns successfully, but subsequent calls end up with an invalid file handle state: the return value is zero (which normally indicates either EOF or an error, cf. here and here), but feof() and ferror() both return zero as well.

Some context: I'm porting my old code from devkitARM, this includes an MP3 streaming and playback engine. Decoding the audio stream happens on the ARM7, but the file I/O calls are done on the ARM9 (because that's how it's done with dkA). FIFO communications are done to synchronize everything:

  1. The ARM9 sends play/pause/... commands to the ARM7
  2. The ARM7 sends a FIFO message to the ARM9 whenever it needs a new MP3 frame to decode and play back
  3. The ARM9 does the file I/O calls and sends another FIFO message back to the ARM7 to inform the latter the buffer is ready

Interestingly, other file accesses (for e.g. reading 3D models) seem to work, but it's the MP3 file I/O that's not working here. Since it involves IPC FIFO stuff, I tried checking if changing/forcing DLDI mode changes much, but it doesn't affect the result at all. So I'm not sure what's causing this, hence this issue.

asiekierka commented 3 months ago

After triage with the user's code, it was discovered that the ARM7 was telling the ARM9 to write past the MP3 streaming buffer's maximum length, overwriting the 1-3 bytes past it - which just so happened to be related to the file structure.