Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the FOPEN_MAX of the real PSP console? #29

Closed
PalMusicFan opened this issue Jun 21, 2019 · 7 comments
Closed

What is the FOPEN_MAX of the real PSP console? #29

PalMusicFan opened this issue Jun 21, 2019 · 7 comments

Comments

@PalMusicFan
Copy link

Hello!
It seems that only 10 files could be opened simultaneously in the PSP mode of a PSV console. So, is it the limitation of a real PSP console? What is the FOPEN_MAX of a real PSP console?
BTW, in the emulator PPSSPP, much more files could be opened simultaneously.

Thanks!

@yoanlcq
Copy link
Contributor

yoanlcq commented Dec 10, 2019

I was interested in this, too. Here's what I found.

First, it's worth mentioning that PSPSDK's libc has a #define _NFILE 16 which limits the number of fopen() handles, if you use that library.

I wrote a test application which tried to open as many different files as possible via sceIoOpen in read-only mode, until the call would fail, and tested it via PSPLINK (too lazy to test it as an EBOOT.PBP).

The application was able to open 56 different files simultaneously. What I think is most interesting is that the highest file descriptor value it could get was 63 (if we include 0, that makes 64 file descriptors, which makes sense to me since it's a power of two).
The smallest file descriptor value (the first) the application could get was 7. Probably the first 3 are stdin, stdout and sterr, and either the 5 others are reserved by the kernel, or used by PSPLINK.

I guess it's reasonable to assume that the kernel reserves 8 file descriptors, and supports up to 64 (per-module? system-wide? idk). These are nice power of two integers.

(edit) I'm testing on a PSP SLIM 2000, with firmware version 6.35.

@PalMusicFan
Copy link
Author

PalMusicFan commented Dec 18, 2019

Thanks so much for your reply!
Since the application was able to open 56 different files simultaneously, why PSPSDK's libc has a #define _NFILE 16 ? Is it something like early PSP hardware / software limitation? How could PPSSPP ignore the limitation of _NFILE 16? Is it a hack?

@yoanlcq
Copy link
Contributor

yoanlcq commented Dec 18, 2019

why PSPSDK's libc has a #define _NFILE 16 ?

idk, hopefully an expert out there knows, but my I guess is, it could have been a copy-paste or just an educated guess.

How could PPSSPP ignore the limitation of _NFILE 16 ?

Regardless of PPSSPP, the "real" existing PSP games do not use the PSPSDK's libc (which is unofficial), hence I'm pretty sure the limitation is not there (or is way bigger). Not even all homebrew PSP games are guaranteed to use it, because there are various alternatives (e.g newlib).

When building a homebrew PSP game with the current SDK, you can set USE_PSPSDK_LIBC=1 when calling make if you want to use PSPSDK's libc. The alternatives are USE_KERNEL_LIBC=1, or newlib (see src/base/build_prx.mak).

Knowing that, PPSSPP is basically a PSP kernel running on high-end platforms such as PCs. One could look at the source on GitHub if they're so inclined, but I'm guessing at some point they're just using the host's fopen() or equivalent, which has practically no limitation (what's the FOPEN_MAX on PC ? :) ).

@PalMusicFan
Copy link
Author

PalMusicFan commented Dec 18, 2019

Thanks so much! That is very important information for me!

Actually I am maintaining the PSP version of a homebrew classic RPG cross-platform reimplementation. I encountered two main problems. One of them is this FOPEN_MAX limiatation. Though I solved it with conditional compilation, but this makes my code unsuitable for being merged into the main stream (The other one is "If bpp is 8, repeated SDL_SetVideoMode calling will cause Display error." pspdev/psplibraries#37 ) .

Thanks again! :)

@PalMusicFan
Copy link
Author

Sorry, I just finished a file demo:
OpenFileTest.zip
It seems only 10 files could be opened simultaneously (fd number 3~12).
Am I doing something wrong? Thanks!

@PalMusicFan
Copy link
Author

PalMusicFan commented Dec 31, 2019

In addition, how do we achieve suspend-resume? If a file is being read when suspending, it seems that the pointer position can not be saved? Edit: Solved, but do I have to handle it all by myself? I am not sure whether my solution is stable.

for (int i = 3; i < 13; i++) {
printf("PSP_POWER_CB_SUSPENDING i = %d \n", i);
if (__psp_descriptormap[i]) {
fileseekpoints[i] = ftell(i); // It stops here. Edit: Sorry, I confused fd and fp.
}
}

@fjtrujy
Copy link
Member

fjtrujy commented Nov 25, 2021

Hello @PalMusicFan,
About 2 years after, I'm coming for closing the issue and giving you more information. About how it is working after the new POSIX toolchain.

I have modified your example:

int main(int argc, char** argv)
{
	SetupCallbacks();
	atexit(sceKernelExitGame);

	pspDebugScreenInit();
	int fd;
	char pathname[30];
	
	for (int i = 1; i <= 69; i++) {
		sprintf(pathname, "%02d.txt", i);
		printf("Opening %s: ", pathname);
		if ((fd = open(pathname, O_RDONLY)) < 0) {
    		printf("Failed\n");
			break;
		}
		printf("OK, fd: %i\n", fd);
		sceKernelDelayThread(100000);
	}

	sceKernelDelayThread(10000000);

	sceKernelExitGame();

    return 0;
}

I have tried in both places PPSSPP and real PSP, in both it behaves exactly the same, and limitation looks to be a PSP OS limitation:

Screenshot 2021-11-25 at 13 44 31

Looks to be that maxium fd files you can open in PSP are 60, having in mind that:

So fd = 3 would be the first id when an user open a file in his program.

Having in mind that your app needs to open 56 files simultaneously, I think that your problem will be solved with the new toolchain.

Thanks!

@fjtrujy fjtrujy closed this as completed Nov 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants