Page 1 of 1

MemCard problem?

Posted: June 20th, 2016, 1:50 am
by NaughtyCow
I'm trying to write some data onto the memory card, and I've followed the etc\makecard example in the PSYQ, lib, properly initializing the memory card, as well as properly opening it with open(...), etc.. Everything according to that example. For writing, I'm just using the Crash Bandicoot PAL file that I ripped from a Save File I downloaded from GameFaqs (I used Shendo's tool, and saved as Raw)... Anyway, the save file gets written, but there is no icon and no title, and the save is not recognized internally by the bios memcard manager, only by MemCardRex.. I used NO$PSX, and it doesn't proceed with the code after it fails (it freezes somehow), and when I used ePSXe emulutaor, it proceeds, but the error is caught and the error screen I created is shown.

Some code:
[C]int isAnyDataOnMC(char* fname) {
long fd;
if ((fd = open(fname, O_RDONLY )) == -1)
return 0;
close(fd);
return 1;
}

int writeDataOnMC(char* fname, Data* data, unsigned char creat, char blocks) {
long fd;
if (creat) {
if ((fd = open( fname, O_CREAT | blocks << 16 )) == -1)
return 1;
close(fd);
}
if (fd = open( fname, O_WRONLY ) == -1)
return 1;
if (write( fd, (unsigned char*)data->ptr, 8192 * blocks) != 8192 * blocks)
return 1;

close(fd);
return 0;
}

void showOverwrite(Data* data) {
// supposed to show "Do you wish to overwrite" screen? For now, just writes without creating the file.
if (writeDataOnMC("bu10:BESCES-00344972726", data, 0, 1))
exitError();
}[/C]

[C]// This triggers the save
Data d_Tempx;
if (!readfile("\\DATA\\CPSX.DAT;1", &d_Tempx, 0)) exitError();
if (isAnyDataOnMC("bu10:BESCES-00344972726")) showOverwrite(&d_Tempx);
else if (writeDataOnMC("bu10:BESCES-00344972726", &d_Tempx, 1, 1)) exitError();
freedata(&d_Tempx);[/C]

ADDENDUM: The file that gets written is completely null (all zeros).

Re: MemCard problem?

Posted: June 20th, 2016, 11:02 pm
by NaughtyCow
anyone?

Re: MemCard problem?

Posted: June 21st, 2016, 12:01 am
by NaughtyCow
Nevermind, I solved it. Turns out I forgot the brackets, which was setting the file descriptor to -1...

[C]if ((fd = open( fname, O_WRONLY )) == -1)
if (write( fd, (unsigned char*)data->ptr, 8192 * blocks) != (8192 * blocks)[/C]

I also have an another question. Is there any way to check if the memory card is in the slot, or if it requires formatting (and also how to format it).

Re: MemCard problem?

Posted: June 21st, 2016, 12:27 am
by Administrator
NaughtyCow wrote:I also have an another question. Is there any way to check if the memory card is in the slot, or if it requires formatting (and also how to format it).
Sony has some examples for that.

Re: MemCard problem?

Posted: June 21st, 2016, 12:45 am
by NaughtyCow
Shadow wrote:
NaughtyCow wrote:I also have an another question. Is there any way to check if the memory card is in the slot, or if it requires formatting (and also how to format it).
Sony has some examples for that.
Thank, I've just found some myself. Also I was wondering (unrelated to this topic) does the CD-ROM have some limitations, as of maximum number of subdirectiries and files in one folder? I remember reading something about that. Something I also read was that the file size of every file must be divisible by 2048 bytes (2 KB) or else the CD wont load on the actual machine? Is that true?

Re: MemCard problem?

Posted: June 21st, 2016, 12:54 am
by Administrator
Yes it is true. I can't remember the exact number, but it's mentioned somewhere in the Sony documents.

Re: MemCard problem?

Posted: June 21st, 2016, 1:00 am
by NaughtyCow
NaughtyCow wrote: ...file size of every file must be divisible by 2048 bytes (2 KB) or else the CD wont load on the actual machine? Is that true?
Is this also true, or will this code (for length of file ceiled to the nearest factor of 2048) eliminate that issue if I use it for read length:
[C]/* num = actual length , factor = 2048 */
num + (((num % factor) > 0) ? factor : 0);[/C]

Sorry if I'm seeming arrogant. I have a limited amount of CD-R's, and I cannot afford to screw up when burning by program.

Re: MemCard problem?

Posted: June 21st, 2016, 2:27 am
by Administrator
I've never ran into problems with padding or reading files to 2048.
One of my read routines however is shown below.

[C]int _read2(long byte, void *sectbuf, int mode)
{
int nsector,cnt;
nsector=(byte+2047)/2048;

// read start
CdRead(nsector, sectbuf, mode);

while((cnt=CdReadSync(1,NULL))>0) VSync(0);
return cnt;
}[/C]

Re: MemCard problem?

Posted: June 23rd, 2016, 8:56 am
by nocash
NaughtyCow wrote:does the CD-ROM have some limitations, as of maximum number of subdirectiries and files in one folder? I remember reading something about that. Something I also read was that the file size of every file must be divisible by 2048 bytes (2 KB) or else the CD wont load on the actual machine? Is that true?
I don't think that the filesize must be a multiple of 2048 bytes, as far as I remember, the SYSTEM.CNF file is much smaller (just check some retail discs if you want to be sure).

But the other restrictions do exist, the PSX BIOS/Kernel is loading only one 800h-byte sector per directory, and only one 800h-byte sector for the path table - even if the directory or path table is bigger than one sector. Moreover, the PSX is extracting the info from the path table and current directory to an array with limited number of entries:
- max 40 files per directory
- max 44 directories per disc

Anyways, the filesize, and and max number of files and directories are just a software issue, nothing related to real hardware. If you are using a copy the original PSX BIOS/Kernel, then you can safely test that stuff in emulators. Or, if you don't have a copy of the original bios: the bios clone included in no$psx should be also reproducing the filesystem behaviour of the original bios quite well.