README
21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
Nintendo64 CONTROLLER PAK
I. Goals
1. The intended use of CONTROLLER PAK is to save and restore game state.
Maximum performance is not critical.
2. The file system need to allow users to switch RAM Packs while playing.
II. Overview
CONTROLLER PAK has the following features.
1. There are two types of CONTROLLER PAK (32k Bytes and 128k Bytes) now,
so we made the format of CONTROLLER PAK deal with up to 128k.
2. Each access to CONTROLLER PAK is 32 bytes with CRC checking.
3. CONTROLLER PAK is divided into pages (one page = 256bytes);
files will be divided into several pages and saved separately.
4. Each applications may have several files in one CONTROLLER PAK.
5. The number of files that can be saved in one CONTROLLER PAK is
specified in ID area. We can support 16 files or even larger file
system.
6. A file system checker which can fix the corrupted file system
on the fly is provided.
III. Data structure of CONTROLLER PAK
3.1 Whole structure
Backup memory is divided into four areas as follows:
+------------+ -------
| | ^
page 0 | | | ID area 1 page
| | v
+------------+ -------
| | ^
1 | | | INODE area 1 page
| | v
+------------+ -------
| | ^
2 | | | INODE mirror area 1page
| | v
+------------+ -------
| | ^
3 | | |
| | |
+------------+ | DIR area dir_size pages
| | |
: : |
: : |
| | v
+------------+ -------
| | ^
dir_size +3 | | |
| | |
+------------+ | DATA area
| | | 126-dir_size pages (32k)
| | | 254-dir_size pages (64k)
: : :
: : :
: : :
: : :
| | |
+------------+ |
| | |
127(if 32k ) | | |
255(if 64k ) | | v
+------------+ -------
ID area: has the CONTROLLER PAK identifier, etc.
DIR area: has the information about each file
INODE area: describes how the pages link
INODE mirror area: copy of INODE area
DATA area: has the file data itself
The structure of the whole pack file system is defined as follows:
typedef struct {
int status;
OSMesgQueue *queue;
int channel;
u8 id[32];
u8 backup_id[32];
u8 label[32];
int pack_size;
int version;
int dir_size;
int inode_start_page;
} OSPfs;
3.2 ID area
The details of ID area is as follows:
Offset +-------------------------+ ---
+00-+1F | | System reserved
+-------------------------+ ---
+20-+3F | | Serial No.(include pack size)
+-------------------------+ ---
+40-+5F | | System reserved
+-------------------------+ ---
+60-+7F | | Serial No.(include pack size)
+-------------------------+ ---
+80-+9F | | Serial No.(include pack size)
+-------------------------+ ---
+A0-+BF | | System reserved
+-------------------------+ ---
+C0-+DF | | Serial No.(include pack size)
+-------------------------+ ---
+E0-+FF | | Symbolic label
+-------------------------+ ---
Serial No.: Serial number of the pack formatted in Nintendo factory.
There are no CONTROLLER PAKs that have the same serial
numbers. If this number is illegal (and the
following backup number is also illegal), that
pack must not be used.
Backup of serial No.: This is for sudden destruction of serial No. part.
Serial Number block format:
+0 +8 +F
------------------------------------------------------------------
| | |
| serial # (High) | serial # (Mid) |
| | |
------------------------------------------------------------------
| | | | | | |
| serial # (Low) |Device |Mem |Ver| 16bit | 16bit|
| | ID |size| |checksum|~check|
------------------------------------------------------------------
Serial Number block format after repaired:
+0 +8 +F
------------------------------------------------------------------
| | | |
| 0xFFFFFFFF | Random # | serial # (Mid) |
| | | |
------------------------------------------------------------------
| | | | | | |
| serial # (Low) |Device |Mem |Ver| 16bit | 16bit|
| | ID |size| |checksum|~check|
------------------------------------------------------------------
typedef struct {
u32 repaired;
u32 random;
u64 serial_mid;
u64 serial_low;
u16 deviceid;
u8 memory_size;
u8 version;
u16 checksum;
u16 inverted_checksum;
} __OSPackId;
3.3 DIR area
This area has the information about each file. System allocates
32bytes for each file. Since we reserve two pages (512 bytes) for this
area, up to 16 files can exist in a CONTROLLER PAK.
The structure of each file is as follows:
typedef struct {
u16 company_code;
u16 game_code;
u8 start_page;
u8 status;
u16 data_sum;
char ext_name[PFS_FILE_EXT_LEN];
char game_name[PFS_FILE_NAME_LEN];
s32 reserved;
} __OSDir;
NOTE: 1. There is no check-sum for each OSDir structure because
there is CRC checking for each 32-bytes-transition.
3.4 INODE area
This area indicates how the pages link. The data in each address
of this area is a kind of code and this code indicates the status of
the corresponding page (e.g., the data in +20 bytes from the beginning
of INODE area indicates the status of page 20). The meanings of the
codes are as follows:
PFS_EOF (01) The file ends at that page.
PFS_PAGE_NOT_EXIST (02) Memory doesn't exist in that page.
PFS_PAGE_NOT_USED (03) This page is not allocated for any file.
04-FF The file continues to the page
indicated by that number.
The following example shows the way a certain file saved in backup
memory at a certain time.
DIR area of the file INODE area
offset| |
+----------+
+77 | 03 | This page is not used.
Start page +----------+
+------+ +78 | 03 | This page is not used
| 7A | +----------+
+------+ +79 | 03 | This page is not used.
| +----------+
+-------------->+7A | 7B |-------+ This page continues to
+----------+ | page 7B.
+7B | 7C |<------+
| |-------+
+----------+ |
+7C | 7D |<------+
| |-------+
+----------+ |
+7D | 7E |<------+
| |-------+
+----------+ |
+7E | 7F |<------+
| |-------+
+----------+ |
+7F | 01 |<------+ The file ends at this
| | page.
+----------+
+80 | 02 |-+
| | | If the CONTROLLER PAK is
+----------+ | 32Kbytes type, there is no
+81 | 02 | | memory corresponding with
| | | these address.
From this example, one can see that this CONTROLLER PAK is 32kbytes
type, this file occupies six pages (pages 7A-7F), and pages
77 to 79 aren't used at that time.
Since pages 0-3 are used by system, the corresponding addresses
in INODE area (+0 to +3 bytes from the beginning of page 3) isn't
needed. So our system uses these four bytes as the check sum of page
3 (INODE area), etc.
typedef struct {
u8 inode_page[PFS_INODE_SIZE];
} __OSInode;
inode_page[0] is used to save check-sum which can be used to detect
for file system corruption.
IV. Interface Functions
4.1. Error codes returned from functions:
PFS_ERR_NOPACK: No CONTROLLER PAK is plugged in. Apps can
just return.
PFS_ERR_ID_FATAL: dead CONTROLLER PAK is plugged in.
Apps can just return.
PFS_ERR_DEVICE: wrong device type is plugged in.
Apps can just return.
PFS_ERR_NEW_PACK: CONTROLLER PAK has been changed to a
different one.
Apps need to call osPfsInit() to initialize
the new CONTROLLER PAK.
PFS_ERR_INCONSISTENT: File system is corrupted. Apps need to call
osPfsChecker() to fix the file system.
PFS_ERR_CONTRFAIL: Crc error or controller failure. Apps should
retry one more time.
PFS_ERR_INVALID: Invalid parameter or the specified file does
not exist. Apps should change parameter and
then retry.
PFS_DIR_FULL: Directory is full, no new files can be created. Apps should ask users to
change to a new pack or loop through all the
files to let users delete files before
allocating a new file.
PFS_DATA_FULL: No free pages on ram pack. No old files can
grow and no new files can be created.
Apps should ask users to
change to a new pack or loop through all the
files to let users delete files before
allocating a new file.
PFS_ERR_BAD_DATA: The data read from pack are bad. Apps should
just ignore the data.
PFS_ERR_EXIST The specified file which will be created exists
4.2. Open file system:Set up file system for the new plugged-in CONTROLLER PAK
s32
osPfsInit(OSMesgQueue *queue, OSPfs *pfs, int channel)
{
/* Check integrity of file system -- call osPfschecker() */
/* Check valid id */
/* marked file system as initialized */
}
Error codes returned: PFS_ERR_NOPACK, PFS_ERR_ID_FATAL,
PFS_ERR_DEVICE, PFS_ERR_CONTRFAIL.
4.3. Allocate a file: We do not allow to grow a file on a write. Apps
should declare pages needed on allocation time.
s32
osPfsAllocateFile(OSPfs *pfs, u16 company_code, u16 game_code,
u8 *game_name, u8 *ext_name, int file_size_in_bytes,
s32 *file_number)
{
/* Check valid ID and parameters */
/* Allocate a file */
/* If the file exists, return the file number */
/*
* If the file doesn't exist and file_size is not zero,
* preallocate a file and declare file pages. The file
* number is returned.
*/
}
Error codes returned: PFS_ERR_NOPACK, PFS_ERR_NEW_PACK,
PFS_ERR_INCONSISTENT, PFS_ERR_CONTRFAIL,
PFS_ERR_INVALID, PFS_DATA_FULL,
PFS_DIR_FULL, PFS_ERR_EXIST.
4.4. Delete a file
s32
osPfsDeleteFile(OSPfs *pfs, u16 company_code, u16 game_code,
u8 *game_name, u8 *ext_name)
{
/* Check valid ID and parameters */
/* get dir structure */
/* release pages */
/* update dir and inode structures */
}
Error codes returned: PFS_ERR_NOPACK, PFS_ERR_NEW_PACK,
PFS_ERR_INCONSISTENT, PFS_ERR_CONTRFAIL,
PFS_ERR_INVALID.
4.5. Read/Write Data to file
s32
osPfsReadWriteFile(OSPfs *pfs, s32 file_no, u8 flag, int offset,
int size_in_bytes, u8 *data_buffer)
{
/* Check valid ID and parameters */
/* Get dir and inode structures */
/* seek to the specified offset */
/* read/write data from/to CONTROLLER PAK */
}
Error codes returned: PFS_ERR_NOPACK, PFS_ERR_NEW_PACK,
PFS_ERR_INCONSISTENT, PFS_ERR_CONTRFAIL,
PFS_ERR_INVALID, PFS_ERR_BAD_DATA.
4.6. Grow or truncate files:We do not allow to grow a file on a write.
Apps should declare pages needed on allocation
time or use osPfsReSizeFile() to grow/truncate
the specified file.
s32
osPfsReSizeFile(OSPfs *pfs, u16 company_code, u16 game_code,
u8 *game_name, u8 *ext_name, int new_file_size_in_bytes)
{
/* Check valid ID and parameters */
/*
* Fine the specified file. If the file doesn't exist,
* return PFS_ERR_INVALID
*/
/* get dir and inode structures */
/* get current file size */
/* If grow file, declear new pages */
/* If truncate file, release the unsed pages */
/* Write back inode structure */
}
4.7. Get file information
s32
osPfsFileState(OSPfs *pfs, s32 file_no, OSPfsState *state)
{
/*
* typedef struct {
* u32 file_size_in_bytes;
* u16 company_code;
* u16 game_code;
* char ext_name[PFS_FILE_EXT_LEN];
* char game_name[PFS_FILE_NAME_LEN];
* } OSPfsState;
*/
/* Check valid ID and parameters */
/* get dir and inode structures */
/* get file status and calculate file size */
}
Error codes returned: PFS_ERR_NOPACK, PFS_ERR_NEW_PACK,
PFS_ERR_INCONSISTENT, PFS_ERR_CONTRFAIL,
PFS_ERR_INVALID.
4.8. Search for a specified file
s32
osPfsFindFile(OSPfs *pfs, u16 company_code, u16 game_code,
u8 *game_name, u8 *ext_name, s32 *file_no)
{
/* Check valid ID and parameters */
/* loop through all files to search the specified
* file and then return file number
*/
}
Error codes returned: PFS_ERR_NOPACK, PFS_ERR_NEW_PACK,
PFS_ERR_CONTRFAIL, PFS_ERR_INVALID.
4.9. Check integegrity of file system
s32
osPfsChecker(OSPfs *pfs)
{
/* check status */
/*
* loop through all files to make sure that there are no
* file's start pages have been marked NOT_USED, no pages
* are allocated to two different files, no inode are infinite
* loop, all inodes need to be terminated by PFS_EOF .
*/
/*
* return all pages that are not in any file back
* to NOT_USED.
*/
}
Error codes returned: PFS_ERR_NOPACK, PFS_ERR_CONTRFAIL.
4.10. Re-initialize the CONTROLLER PAK in case of corruption.
/*
* This function reformat the whole file system except pack id
* which is assigned by Nintendo.
*/
s32
osReFormatPfs(OSMesgQueue *queue, int channel)
{
/* Check ID */
/* Find CONTROLLER PAK size */
/* make dir */
/* make inode */
}
Error codes returned: PFS_ERR_NOPACK, PFS_ERR_CONTRFAIL.
4.11. Set up symbolic label for CONTROLLER PAK
/*
* This function allows users to setup symbolic label which
* can be used for users to identify CONTROLLER PAK.
*/
s32
osPfsSetLabel(OSPfs *pfs, u8 *label)
{
/* Check ID */
/* Copy label to CONTROLLER PAK */
}
Error codes returned: PFS_ERR_NOPACK, PFS_ERR_CONTRFAIL.
4.12. Read symbolic label which is saved in CONTROLLER PAK
/*
* This function allows users to read symbolic label which
* can be used for users to identify CONTROLLER PAK.
*/
s32
osPfsGetLabel(OSPfs *pfs, u8 *label, int *len)
{
/* Check ID */
/* Copy label to specified data array and return length
* of label back to len
*/
}
Error codes returned: PFS_ERR_NOPACK, PFS_ERR_CONTRFAIL.
4.13. Check to see if CONTROLLER PAK is plugged or not
/*
* This function returns a bit pattern to indicate which memory
* packs are plugged in
*/
s32
osPfsIsPlug(OSMesgQueue *queue, u8 *pattern)
{
/* Set up request command format for all channels */
/* trigger pifmacro */
/* reformat the 64 bytes RAM data and save in data array */
/* loop through data array to check error bits */
}
Error codes returned: None
4.14. Return number of free space in bytes
s32
osPfsFreeBlocks(OSPfs *pfs, s32 *bytes_not_used)
{
/* Check ID */
/* loop through inode table to find all the
* free bloacks
*/
}
Error codes returned: PFS_ERR_NOPACK, PFS_ERR_CONTRFAIL.
4.15. Return the number of max files are allowed on the CONTROLLER PAK and
the number of files exist on CONTROLLER PAK
s32
osPfsNumFiles(OSPfs *pfs, s32 *max_files, s32 *files_used)
{
/* Check ID */
/* loop through directory table to calculate
* the total number of files exist.
*/
}
Error codes returned: PFS_ERR_NOPACK, PFS_ERR_CONTRFAIL.
4.16. Notes for developers
1. In order to achieve the second goal to allow users to switch
RAM Packs while playing, no dir structures, inode structures or
data should be cached on memory.
2. Since each interface function requires some SI DMAs which
are slow, the threads that are calling thses functions should
be low priority threads.
V. Example
mainproc(void *arg)
{
osCreateMesgQueue(&pifMesgQueue, pifMesgBuf, NUM_MESSAGE);
osSetEventMesg(OS_EVENT_SI, &pifMesgQueue, dummyMessage);
osContInit(&pifMesgQueue, &p, &sdata[0]);
for (i = 0; i < MAXCONTROLLERS; i++) {
if (p & (1<<i)){
cont_no = i;
break;
}
}
ret = osPfsInit(&pifMesgQueue, &pfs0, cont_no);
if (ret == PFS_ERR_NOPACK)
osExit();
while(1) {
osContStartReadData(&pifMesgQueue);
osRecvMesg(&pifMesgQueue, NULL, OS_MESG_BLOCK);
osContGetReadData(&rdata[0]);
button = rdata[cont_no].button;
if (button & CONT_A) {
ret = osPfsFindFile(&pfs0, state1.company_code,
state1.game_code, state1.game_name, state1.ext_name, &no);
if (ret == 0) {
ret = osPfsReadWriteFile(&pfs0, no, PFS_READ,
0, state1.file_size, backup_buffer);
if (ret == 0) {
draw_puts(" read success");
}
else {
err_handlaer(ret, &pfs0);
}
}
}
else if (button & CONT_B) {
ret = osPfsFindFile(&pfs0, state1.company_code,
state1.game_code, state1.game_name, state1.ext_name, &no);
if (ret == 0) {
ret = osPfsReadWriteFile(&pfs0, no, PFS_WRITE,
0, state1.file_size, backup_buffer);
if (ret != 0) {
err_handlaer(ret, &pfs0);
}
else {
draw_puts("write file success");
}
}
}
else if (button & CONT_C) {
for(i = 0 ; i < (int)(256*dsize[data_no]) ;i++)
backup_buffer[i] = data_no;
ret = osPfsAllocateFile(&pfs0, c_code[data_no], g_code[data_no],
gname[data_no], ename[data_no],
256*dsize[data_no], &no);
if (ret == 0) {
ret = osPfsReadWriteFile(&pfs0, no, PFS_WRITE,
0, 256*dsize[data_no], backup_buffer);
if (ret != 0) {
draw_puts("create file failure");
} else {
draw_puts("create file success");
}
} else {
if (ret == PFS_ERR_FULL) {
draw_puts("file system full");
} else {
draw_puts("create file failure");
}
}
} else if (button & CONT_D) {
ret = osPfsDeleteFile(&pfs0, state1.company_code,
state1.game_code, state1.game_name, state1.ext_name);
if (ret != 0) {
draw_puts("delete file failure");
} else {
draw_puts("delete file success");
}
} else if (button & CONT_E) {
ret = osPfsChecker(&pfs0);
if (ret != 0) {
err_handlaer(ret, &pfs0);
} else {
draw_puts("check file system success");
}
} else if (button & CONT_F) {
ret = osPfsReFormat(&pifMesgQueue, cont_no);
if (ret != 0) {
draw_puts("check file system failure");
} else {
err_handlaer(ret, &pfs0);
}
}
for(i = 0 ; i < NUM_OF_FILE ; i++) {
ret = osPfsFileState(&pfs0, i, &state);
if (ret == 0) {
s = state.game_name;
for(j = 0; j < PFS_FILE_NAME_LEN ; j++)
draw_char( *s++ );
s = state.ext_name;
for(j = 0; j < PFS_FILE_EXT_LEN ; j++)
draw_char( *s++ );
draw_uint(state.file_size);
} else
err_handlaer(ret, &pfs0);
}
}
osExit();
}
err_handlaer(int error, OSPfs *pfs)
{
switch(error) {
case PFS_ERR_NEW_PACK:
osPfsInit(pfs->queue, pfs, pfs->channel);
break;
case PFS_ERR_INCONSISTENT:
osPfsChecker(&pfs0);
break;
default:
break;
}
return(0);
}
VI. TODO list
1. Provide program or format for Nintendo factory to format
ram pack.