Digimon World 3 decomp
The European version, SLES_039.36
Loading...
Searching...
No Matches
memcard.h
Go to the documentation of this file.
1#ifndef DW3_MEMCARD_H
2#define DW3_MEMCARD_H
3
4/* Memory card saves (memcard/) */
5
6#include "common.h"
7#include <sys/types.h>
8#include <libgte.h>
9#include <libgpu.h>
10
11/* The bytes the card reads or writes at once: one sector */
12#define CARD_SECTOR_SIZE 0x80
13
14/* The blocks of the save file */
15#define SAVE_BLOCKS 4
16
17/* The icon frames a save may have */
18#define SAVE_MAX_ICONS 3
19
20/* CardHeader.type: this flag plus the icon frame count */
21#define CARD_HEADER_ICONS 0x10
22
23/* The files MEMCARD lists from a card's directory */
24#define CARD_MAX_FILES 15
25
26/*
27 * MemCard.result, as libmcrd's MemCardSync reports it (McErr*), plus
28 * CARD_ERR_NOT_STARTED when the command could not even be sent
29 */
30#define CARD_ERR_NONE 0
31#define CARD_ERR_NO_CARD 1
32#define CARD_ERR_INVALID 2 /* McErrCardInvalid: "Cannot access Memory Card" */
33#define CARD_ERR_NEW_CARD 3
34#define CARD_ERR_UNFORMATTED 4
35#define CARD_ERR_NO_FILE 5 /* McErrFileNotExist: "File cannot be found" */
36#define CARD_ERR_FILE_EXISTS 6 /* McErrAlreadyExist: "File already exists" */
37#define CARD_ERR_FULL 7 /* McErrBlockFull: "There are not enough blocks" */
38#define CARD_ERR_NOT_STARTED 8
39
40/* MemCard.state: idle, or waiting for the command each operation sent */
49
50/* readSave and writeSave's sections: the header, the info, then the data copies */
52
53/* memCardCommand's operations */
55
56/* Memory card save header (first CARD_SECTOR_SIZE bytes of a save) */
57typedef struct CardClut {
58 /* 0x00 */ u8 data[0x20];
59} CardClut;
60
61typedef struct CardHeader {
62 /* 0x00 */ char magic[2];
63 /* 0x02 */ u8 type;
64 /* 0x03 */ u8 blocks;
65 /* 0x04 */ char title[64];
66 /* 0x44 */ u8 reserved[28];
67 /* 0x60 */ CardClut clut;
69
70/* Same layout as the kernel's struct DIRENTRY */
71typedef struct CardDirEntry {
72 /* 0x00 */ char name[20];
73 /* 0x14 */ s32 attr;
74 /* 0x18 */ s32 size;
75 /* 0x1C */ struct CardDirEntry *next;
76 /* 0x20 */ s32 head;
77 /* 0x24 */ char system[4];
79
80/*
81 * Memory card access (MEMCARD), one step per call: every operation returns
82 * 0 while it runs, 1 when it succeeds and result + 1 when it fails.
83 * The save file (SAVE_BLOCKS blocks) holds the header and 1-3 icon frames (a
84 * sector each), an info section (infoSize) and data sections (dataSize each).
85 */
86typedef struct MemCard {
87 /* 0x00 */ s32 state; /* MEMCARD_* */
88 /* 0x04 */ u8 pad4[8]; /* never read or written */
89 /* 0x0C */ const char *fileName;
90 /* 0x10 */ CardHeader header;
91 /* 0x90 */ s32 cmd;
92 /* 0x94 */ u32 result;
93 /* 0x98 */ s32 retries;
94 /* 0x9C */ s32 maxRetries;
95 /* 0xA0 */ s32 restart; /* the command failed and must be reissued */
96 /* 0xA4 */ long fileCount; /* long: MemCardGetDirentry writes it */
98 /* 0x300 */ s32 progress;
99 /* 0x304 */ s32 offset;
100 /* 0x308 */ s32 pad308; /* never read or written */
101 /* 0x30C */ s32 dataSize;
102 /* 0x310 */ s32 infoSize;
103 /* 0x314 */ s32 iconCount;
104 /* 0x318 */ void *icons[SAVE_MAX_ICONS]; /* each frame's image (setSaveHeader) */
105 /* 0x324 */ s32 iconIndex; /* the icon a save is writing (STGMCARD_runSaves) */
106} MemCard;
107
108/* The memory card functions (MEMCARD_FUNCS); like MemCard's operations, the
109 card ones return 0 while they run */
110typedef struct MemCardFuncs {
111 /* 0x00 */ void (*init)(void);
112 /* 0x04 */ void (*setFileName)(void);
113 /* 0x08 */ void (*setHeader)(char *title, CardClut *clut, s32 count, void **icons);
114 /* 0x0C */ s32 (*check)(s32 port);
115 /* 0x10 */ s32 (*accept)(s32 port);
116 /* 0x14 */ s32 (*read)(s32 port, void *buf, s32 size, s32 section);
117 /* 0x18 */ s32 (*write)(s32 port, void *buf, s32 size, s32 section);
118 /* 0x1C */ s32 (*list)(s32 port);
119 /* 0x20 */ s32 (*create)(s32 port);
120 /* 0x24 */ s32 (*format)(s32 port);
121 /* 0x28 */ s32 (*unformat)(void);
122 /* 0x2C */ s32 (*verifyChecksum)(void *data, s32 size, char expected);
123 /* 0x30 */ u8 (*computeChecksum)(void *data, s32 size);
125
126void initMemCard(void);
127void setSaveFileName(void);
128void setSaveHeader(char *title, CardClut *clut, s32 count, void **icons);
129s32 memCardCommand(s32 port, s32 op);
130s32 listSaves(s32 port);
131s32 createSave(s32 port);
132s32 formatMemCard(s32 port);
133s32 unformatMemCard(void);
134s32 verifyChecksum(void *data, s32 size, char expected);
135u8 computeChecksum(void *data, s32 size);
136s32 checkMemCard(s32 port);
137s32 acceptMemCard(s32 port);
138s32 readSave(s32 port, void *buf, s32 size, s32 section);
139s32 writeSave(s32 port, void *buf, s32 size, s32 section);
140s32 syncMemCard(void);
141
142/* The memory card library, declared here rather than from libmcrd.h, which
143 takes the buffers as u_long * and MemCardSync's result as a long *, where
144 the game passes u8 buffers and compares the result unsigned */
145void MemCardInit(long val);
146void MemCardStart(void);
147long MemCardSync(long mode, long *cmds, u_long *result);
148long MemCardExist(long chan);
149long MemCardAccept(long chan);
150long MemCardCreateFile(long chan, const char *file, long blocks);
151long MemCardFormat(long chan);
152long MemCardReadFile(long chan, const char *file, void *adrs, long ofs, long bytes);
153long MemCardWriteFile(long chan, const char *file, void *adrs, long ofs, long bytes);
154long MemCardUnformat(long chan);
155long MemCardGetDirentry(long chan, char *name, CardDirEntry *dir, long *files, long ofs, long max);
156
157extern MemCard MEMCARD;
159extern s32 MEMCARD_SYNC_CMDS[]; /* the MemCardSync command of each MEMCARD_OP_* */
160extern char STR_ALL_FILES[]; /* the directory pattern that matches every file */
161
162#endif /* DW3_MEMCARD_H */
signed int s32
Definition common.h:11
unsigned int u32
Definition common.h:12
unsigned char u8
Definition common.h:8
long MemCardCreateFile(long chan, const char *file, long blocks)
long MemCardAccept(long chan)
u8 computeChecksum(void *data, s32 size)
The XOR of every byte, which verifyChecksum checks.
Definition memcard.c:455
SaveSection
readSave and writeSave's sections: the header, the info, then the data copies
Definition memcard.h:51
@ SAVE_SECTION_INFO
Definition memcard.h:51
@ SAVE_SECTION_HEADER
Definition memcard.h:51
@ SAVE_SECTION_DATA
Definition memcard.h:51
s32 writeSave(s32 port, void *buf, s32 size, s32 section)
Writes a section (as readSave; section 0 takes the offset from bits 8 and up)
Definition memcard.c:270
MemCardFuncs MEMCARD_FUNCS
Definition memcard.c:468
s32 memCardCommand(s32 port, s32 op)
Reads the directory, creates the save file, formats or unformats (MEMCARD_OP_*)
Definition memcard.c:351
void MemCardInit(long val)
The memory card library, declared here rather than from libmcrd.h, which takes the buffers as u_long ...
long MemCardReadFile(long chan, const char *file, void *adrs, long ofs, long bytes)
void setSaveHeader(char *title, CardClut *clut, s32 count, void **icons)
The save file header: title (Shift-JIS), icon CLUT and 1-3 icon frames.
Definition memcard.c:86
MemCard MEMCARD
Definition memcard.c:466
void MemCardStart(void)
long MemCardGetDirentry(long chan, char *name, CardDirEntry *dir, long *files, long ofs, long max)
s32 checkMemCard(s32 port)
Checks whether a card is in port port (MemCardExist)
Definition memcard.c:128
long MemCardUnformat(long chan)
s32 formatMemCard(s32 port)
Formats an unformatted card.
Definition memcard.c:428
char STR_ALL_FILES[]
the directory pattern that matches every file
long MemCardExist(long chan)
s32 readSave(s32 port, void *buf, s32 size, s32 section)
Reads a section of the save (SAVE_SECTION_*, then 2-4 for the data), a sector a call.
Definition memcard.c:189
#define SAVE_MAX_ICONS
The icon frames a save may have.
Definition memcard.h:18
s32 listSaves(s32 port)
Reads the card's directory into MEMCARD.files.
Definition memcard.c:408
void setSaveFileName(void)
Definition memcard.c:57
void initMemCard(void)
Starts libmcrd and sets up MEMCARD: the save file name and the section sizes.
Definition memcard.c:7
MemCardOp
memCardCommand's operations
Definition memcard.h:54
@ MEMCARD_OP_LIST
Definition memcard.h:54
@ MEMCARD_OP_UNFORMAT
Definition memcard.h:54
@ MEMCARD_OP_FORMAT
Definition memcard.h:54
@ MEMCARD_OP_CREATE
Definition memcard.h:54
s32 syncMemCard(void)
MemCardSync, retrying a failed command up to maxRetries times.
Definition memcard.c:105
long MemCardWriteFile(long chan, const char *file, void *adrs, long ofs, long bytes)
#define CARD_MAX_FILES
The files MEMCARD lists from a card's directory.
Definition memcard.h:24
long MemCardSync(long mode, long *cmds, u_long *result)
s32 MEMCARD_SYNC_CMDS[]
the MemCardSync command of each MEMCARD_OP_*
Definition memcard.c:474
s32 verifyChecksum(void *data, s32 size, char expected)
XOR of every byte.
Definition memcard.c:443
long MemCardFormat(long chan)
s32 unformatMemCard(void)
memCardCommand's MEMCARD_OP_UNFORMAT, left out: always fails as if there were no card
Definition memcard.c:438
MemCardState
MemCard.state: idle, or waiting for the command each operation sent.
Definition memcard.h:41
@ MEMCARD_WRITING
Definition memcard.h:46
@ MEMCARD_ACCEPTING
Definition memcard.h:44
@ MEMCARD_COMMAND
Definition memcard.h:47
@ MEMCARD_READING
Definition memcard.h:45
@ MEMCARD_IDLE
Definition memcard.h:42
@ MEMCARD_CHECKING
Definition memcard.h:43
s32 acceptMemCard(s32 port)
Checks a card in port port in full, finding out whether it is new or unformatted (MemCardAccept)
Definition memcard.c:160
s32 createSave(s32 port)
Creates the save file on the card.
Definition memcard.c:418
Memory card save header (first CARD_SECTOR_SIZE bytes of a save)
Definition memcard.h:57
u8 data[0x20]
Definition memcard.h:58
Same layout as the kernel's struct DIRENTRY.
Definition memcard.h:71
struct CardDirEntry * next
Definition memcard.h:75
char name[20]
Definition memcard.h:72
char system[4]
Definition memcard.h:77
char magic[2]
Definition memcard.h:62
u8 blocks
Definition memcard.h:64
CardClut clut
Definition memcard.h:67
char title[64]
Definition memcard.h:65
u8 reserved[28]
Definition memcard.h:66
The memory card functions (MEMCARD_FUNCS); like MemCard's operations, the card ones return 0 while th...
Definition memcard.h:110
s32(* create)(s32 port)
Definition memcard.h:119
void(* setHeader)(char *title, CardClut *clut, s32 count, void **icons)
Definition memcard.h:113
s32(* write)(s32 port, void *buf, s32 size, s32 section)
Definition memcard.h:117
s32(* list)(s32 port)
Definition memcard.h:118
s32(* verifyChecksum)(void *data, s32 size, char expected)
Definition memcard.h:122
s32(* check)(s32 port)
Definition memcard.h:114
void(* setFileName)(void)
Definition memcard.h:112
u8(* computeChecksum)(void *data, s32 size)
Definition memcard.h:123
s32(* accept)(s32 port)
Definition memcard.h:115
s32(* format)(s32 port)
Definition memcard.h:120
s32(* unformat)(void)
Definition memcard.h:121
void(* init)(void)
Definition memcard.h:111
s32(* read)(s32 port, void *buf, s32 size, s32 section)
Definition memcard.h:116
Memory card access (MEMCARD), one step per call: every operation returns 0 while it runs,...
Definition memcard.h:86
s32 dataSize
Definition memcard.h:101
s32 iconIndex
the icon a save is writing (STGMCARD_runSaves)
Definition memcard.h:105
u8 pad4[8]
never read or written
Definition memcard.h:88
long fileCount
long: MemCardGetDirentry writes it
Definition memcard.h:96
s32 infoSize
Definition memcard.h:102
s32 cmd
Definition memcard.h:91
u32 result
Definition memcard.h:92
s32 offset
Definition memcard.h:99
s32 pad308
never read or written
Definition memcard.h:100
void * icons[3]
each frame's image (setSaveHeader)
Definition memcard.h:104
CardDirEntry files[15]
Definition memcard.h:97
s32 progress
Definition memcard.h:98
s32 restart
the command failed and must be reissued
Definition memcard.h:95
s32 iconCount
Definition memcard.h:103
const char * fileName
Definition memcard.h:89
s32 maxRetries
Definition memcard.h:94
s32 state
MEMCARD_*.
Definition memcard.h:87
CardHeader header
Definition memcard.h:90
s32 retries
Definition memcard.h:93