Digimon World 3 decomp
The European version, SLES_039.36
Loading...
Searching...
No Matches
file.h
Go to the documentation of this file.
1#ifndef DW3_FILE_H
2#define DW3_FILE_H
3
4/* The disc file table, the CD reader, the file cache and the decompressor
5 (file/) */
6
7#include "common.h"
8#include <sys/types.h>
9#include <libgte.h>
10#include <libgpu.h>
11#include <libcd.h>
12#include "engine/task.h"
13
14/* The disc's file table (FILE_TABLE): files are numbered, not named */
15typedef struct FileTableFuncs {
16 /* 0x0 */ s32 (*exists)(s32 file);
17 /* 0x4 */ s32 (*getSectorCount)(s32 file);
18 /* 0x8 */ s32 (*getSector)(s32 file);
19 /* 0xC */ void (*getPos)(s32 file, s32 offset, void *loc);
21
22/* A file in the cache */
23/* FileSlot.state */
25
26/* The files the cache holds at most */
27#define FILE_CACHE_SLOTS 64
28
29typedef struct FileSlot {
30 /* 0x0 */ s16 state; /* FILE_* */
31 /* 0x2 */ s16 marked;
32 /* 0x4 */ s32 file;
33 /* 0x8 */ s32 lastUsed; /* GFX time */
34 /* 0xC */ void *data;
35} FileSlot;
36
37/* What the CD reader waits for (CdReader.state): the command each state
38 sent to complete, or, in CD_READ_SECTORS, the sectors and then CdlPause */
46
47/* The bytes of data in a sector (Mode 2 Form 1), as the reader reads them */
48#define CD_SECTOR_SIZE 0x800
49
50/* Reads whole sectors of a file with CdlReadN, from callbacks */
51typedef struct CdReader {
52 /* 0x00 */ s32 state; /* CD_READ_* */
53 /* 0x04 */ s32 file;
54 /* 0x08 */ s32 offset; /* in sectors */
55 /* 0x0C */ s32 sectorCount;
56 /* 0x10 */ u8 *buffer;
57 /* 0x14 */ s32 *done; /* set to 1 when the read is complete */
58 /* 0x18 */ u8 loc[4];
59 /* 0x1C */ s32 sector;
60 /* 0x20 */ s32 sectorsLeft;
61 /* 0x24 */ u8 *dst;
62 /* 0x28 */ s32 nextSector;
63 /* 0x2C */ s32 (*isBusy)(void);
64 /* 0x30 */ void (*read)(s32 file, s32 offset, s32 size, void *buf, s32 *done);
65} CdReader;
66
67/*
68 * What cdCheckSector reads first of each sector, the 3 words that
69 * CdlModeSize1 puts before its data: the sector's header, whose position it
70 * compares with the one it expects, and the XA subheader, which nothing
71 * reads. The buffer takes 16 bytes in the .bss (the sound buffers follow
72 * 0x10 after it), so 4 more than it reads.
73 */
74typedef struct CdSectorHeader {
75 /* 0x0 */ CdlLOC pos;
76 /* 0x4 */ u_char subheader[8];
77 /* 0xC */ u_char pad[4]; /* never read: the 4 bytes past the header, see above */
79
80/*
81 * The file cache (FILE_CACHE): up to 64 files loaded in the heap (tag 3),
82 * read in the background one at a time and evicted least recently used
83 * first when the heap is full.
84 */
85typedef struct FileCache {
86 /* 0x000 */ s32 pending;
88 /* 0x404 */ s32 (*isLoading)(s32 file);
89 /* 0x408 */ void (*evictOldest)(void);
90 /* 0x40C */ void (*request)(s32 file);
91 /* 0x410 */ void (*update)(void);
92 /* 0x414 */ void *(*load)(u32 file);
93 /* 0x418 */ void (*free)(s32 file);
94 /* 0x41C */ void (*freeAll)(void);
95 /* 0x420 */ void (*freeFrom)(u32 addr);
96 /* 0x424 */ void *(*getEntry)(u32 fileAndIndex);
97 /* 0x428 */ void *(*getArchiveEntry)(s32 index, void *archive);
98 /* 0x42C */ void (*markCached)(void);
99 /* 0x430 */ void (*touchMarked)(void);
100} FileCache;
101
102/* The first word of "RLEN" data, which its unpacked size follows */
103#define RLEN_MAGIC 0x4E454C52
104
105/*
106 * Expands "RLEN" data (a run-length encoding: a byte n < 0x80 copies n
107 * bytes, n | 0x80 repeats the next byte n times, 0 ends), all at once
108 * (run) or chunkSize bytes a frame (start, then substate 1 until done).
109 */
110typedef struct Decompressor {
112 /* 0x50 */ s32 *data;
113 /* 0x54 */ s32 *begin;
114 /* 0x58 */ s32 compressed;
115 /* 0x5C */ s32 size;
116 /* 0x60 */ s32 bufferSize;
117 /* 0x64 */ void *buffer;
118 /* 0x68 */ void *src; /* the next packed byte */
119 /* 0x6C */ void *dst;
120 /* 0x70 */ s32 chunkSize;
121 /* 0x74 */ void *(*run)();
122 /* 0x78 */ void *(*getData)();
123 /* 0x7C */ void (*start)();
124 /* 0x80 */ void (*free)();
126
131void evictOldestFile(void);
132void requestFile(s32);
133void updateFileCache(void);
134void waitForFile(s32 file);
135void *loadFile(u32 file);
136void freeFile(s32 file);
137void freeAllFiles(void);
138void freeFilesFrom(u32 addr);
139void *getFileEntry(u32 fileAndIndex);
140void *getArchiveEntry(s32 index, void *archive);
141void markCachedFiles(void);
142void touchMarkedFiles(void);
143void cdSyncCallback(s32 status, u_char *result);
144s32 cdCheckSector(void);
145void cdReadyCallback(s32 status, u_char *result);
146s32 isCdReading(void);
147void startCdRead(void);
148void readFile(s32 file, s32 offset, s32 size, void *buffer, s32 *done);
149s32 fileExists(s32 file);
151s32 getFileSector(s32 file);
152void getFilePos(s32 file, s32 offset, void *pos);
153
154void *decompressorRun(Decompressor *task, s32 *data);
156void decompressorSetData(Decompressor *task, s32 *data);
160void decompressorStart(Decompressor *task, s32 *data, s32 chunkSize);
163
165extern CdReader CD_READER;
167extern FileCache FILE_CACHE;
168extern s32 FILE_SECTORS[];
169extern u16 FILE_SECTOR_COUNTS[];
170
171/*
172 * Files the engine loads by number. The European disc numbers its files
173 * differently, and has each text file once per language, in a row:
174 * TEXT_FILE() gives the copy of the language the player picked (LANGUAGE,
175 * set by CNTY_SEL), copy 1 being at the USA version's number. Every disc
176 * has all the copies; the Japanese version reads copy 0, Japanese, which
177 * is the European version's LANGUAGE 0 (NAME_ENTRY_JAPANESE), so it is
178 * the European code with LANGUAGE a constant. The Japanese disc otherwise
179 * numbers its files as the USA one does: every file of the USA disc is at
180 * the same number on it (its 55 more stages' files come after), so the
181 * Japanese version takes the USA version's file numbers.
182 */
183#if VERSION_US
184#define FILE_MENU_SPRITES 0x277 /* the menu graphics, a sprite sheet */
185#define FILE_FONT 0x278
186#define TEXT_FILE(file) (file)
187#elif VERSION_EU
188#define FILE_MENU_SPRITES 0x286
189#define FILE_FONT 0x287
190#define TEXT_FILE(file) (LANGUAGE + (file) - 1)
191extern s32 LANGUAGE; /* 2-5 */
192#elif VERSION_JP
193#define FILE_MENU_SPRITES 0x277
194#define FILE_FONT 0x278
195#define TEXT_FILE(file) (LANGUAGE + (file) - 1)
196#define LANGUAGE 0
197#endif
198
199/* The text files, by their USA disc names (US<name>.BIN), for TEXT_FILE() */
200#define TEXT_AMATERASU_MAP 0x02 /* AMTMAP: the map screen's in the second half (STSTATUS_getGameHalf) */
201#define TEXT_ASUKA_MAP 0x09 /* ASKMAP: the map screen's before */
202#define TEXT_CARD_GAME 0x10 /* CARDGM: the card battle's */
203#define TEXT_CARD_NAMES 0x17 /* CARDNM */
204#define TEXT_CARD_EFFECTS 0x1E /* CARDST */
205#define TEXT_CARD_ALBUM 0x25 /* CRDABM: STCRDABM's */
206#define TEXT_DECK_EDITOR 0x2C /* CRDDEK */
207#define TEXT_CARD_SHOP 0x33 /* CRDSHP: STCRDSHP's, and STCRDDEK's */
208#define TEXT_DIGI_LAB 0x3A /* DGLABO: STGDGLAB's */
209#define TEXT_DIGIMON_NAMING 0x41 /* DGNMET: STDGNAME's */
210#define TEXT_DIGIMON_INFO 0x48 /* DIGINF */
211#define TEXT_DIGIMON_NAMES 0x4F /* DIGNAM */
212#define TEXT_FIGHT_REPORT 0x56 /* FGTRPT: STFGTREP's */
213#define TEXT_INN_NAMES 0x5D /* HTLNAM */
214#define TEXT_ITEM_INFO 0x64 /* ITMINF */
215#define TEXT_ITEM_NAMES 0x6B /* ITMNAM */
216#define TEXT_ITEM_SHOP 0x72 /* ITSHOP: STITSHOP's */
217#define TEXT_MEMORY_CARD 0x79 /* MEMCRD */
218#define TEXT_BATTLE_MENU 0x80 /* MFIGHT: WFIGHTMN's */
219#define TEXT_NAME_ENTRY 0x87 /* NAMEDT: the keyboard's */
220#define TEXT_ONLINE 0x8E /* NAMEET: the Digimon Online registration */
221#define TEXT_SHOP_NAMES 0x95 /* SHPNAM */
222#define TEXT_SKILL_INFO 0x9C /* SKLINF */
223#define TEXT_SKILL_NAMES 0xA3 /* SKLNAM */
224#define TEXT_AREA_NAMES 0xAA /* STAREA */
225#define TEXT_STATUS 0xB1 /* STATUS: the field menu's and STSTATUS's */
226#define TEXT_STAGE_NAMES 0xB8 /* STNAME */
227
228/*
229 * The stages' text files, by their USA disc names too: TALKnn hold the
230 * stages' talks (a stage's STAGE_TEXT, the file shared by a set of stages),
231 * DMGnnn the events of the stages from WSTAGnnn on (EVENT_TEXT_FILE, which
232 * counts from TEXT_FILE(1), so is the file - 1), but for DMG260, DMG270 and
233 * DMG305, of one stage each. The European disc numbers the TALK files 7
234 * lower and has its extra chapter's two after them, so the others 7 higher;
235 * the Japanese version has the extra chapter's (MSTALK08, MSTALK09,
236 * MSDMG900) after its stages' files, and reads them by number.
237 */
238#if VERSION_US || VERSION_JP
239#define TEXT_TALKS_00 0xCD /* TALK00 */
240#define TEXT_TALKS_01 0xD4 /* TALK01 */
241#define TEXT_TALKS_02 0xDB /* TALK02 */
242#define TEXT_TALKS_03 0xE2 /* TALK03 */
243#define TEXT_TALKS_04 0xE9 /* TALK04 */
244#define TEXT_TALKS_05 0xF0 /* TALK05 */
245#define TEXT_TALKS_06 0xF7 /* TALK06 */
246#define TEXT_TALKS_07 0xFE /* TALK07 */
247#define TEXT_TRAINING 0x105 /* TRAINI: STGTRAIN's */
248#define TEXT_EVENTS_200 0x10C /* DMG200 */
249#define TEXT_EVENTS_260 0x113 /* DMG260: the story events' (WSTAG260, FIELDSTG_setupField) */
250#define TEXT_EVENTS_270 0x11A /* DMG270: the Yellow Cruiser's (WSTAG270) */
251#define TEXT_EVENTS_300 0x121 /* DMG300 */
252#define TEXT_EVENTS_305 0x128 /* DMG305: the Master Room's (WSTAG305) */
253#define TEXT_EVENTS_400 0x12F /* DMG400 */
254#define TEXT_EVENTS_500 0x136 /* DMG500 */
255#define TEXT_EVENTS_600 0x13D /* DMG600 */
256#define TEXT_EVENTS_700 0x144 /* DMG700 */
257#define TEXT_EVENTS_800 0x14B /* DMG800 */
258#elif VERSION_EU
259#define TEXT_TALKS_00 0xC6
260#define TEXT_TALKS_01 0xCD
261#define TEXT_TALKS_02 0xD4
262#define TEXT_TALKS_03 0xDB
263#define TEXT_TALKS_04 0xE2
264#define TEXT_TALKS_05 0xE9
265#define TEXT_TALKS_06 0xF0
266#define TEXT_TALKS_07 0xF7
267#define TEXT_TALKS_08 0xFE /* TALK08: the extra chapter's */
268#define TEXT_TALKS_09 0x105 /* TALK09: the extra chapter's */
269#define TEXT_TRAINING 0x10C
270#define TEXT_EVENTS_200 0x113
271#define TEXT_EVENTS_260 0x11A
272#define TEXT_EVENTS_270 0x121
273#define TEXT_EVENTS_300 0x128
274#define TEXT_EVENTS_305 0x12F
275#define TEXT_EVENTS_400 0x136
276#define TEXT_EVENTS_500 0x13D
277#define TEXT_EVENTS_600 0x144
278#define TEXT_EVENTS_700 0x14B
279#define TEXT_EVENTS_800 0x152
280#define TEXT_EVENTS_900 0x159 /* DMG900: the extra chapter's (EXTRA_TEXT) */
281#endif
282
283/* The battle menu's files (WFIGHTMN, WFIGHTTS): FILE_BATTLE_IMAGES is an
284 archive whose entries go through FILE_CACHE.getEntry, the four after it
285 are archives of images for VRAM */
286#if VERSION_US || VERSION_JP
287#define FILE_BATTLE_MENU 0x445
288#define FILE_BATTLE_IMAGES 0x446
289#define FILE_BATTLE_IMAGES_1 0x78F
290#define FILE_BATTLE_IMAGES_2 0x79F
291#define FILE_BATTLE_IMAGES_3 0x7D2
292#define FILE_BATTLE_IMAGES_4 0x7D4
293#elif VERSION_EU
294#define FILE_BATTLE_MENU 0x455
295#define FILE_BATTLE_IMAGES 0x456
296#define FILE_BATTLE_IMAGES_1 0x79E
297#define FILE_BATTLE_IMAGES_2 0x7AE
298#define FILE_BATTLE_IMAGES_3 0x7E1
299#define FILE_BATTLE_IMAGES_4 0x7E3
300#endif
301
302#endif /* DW3_FILE_H */
signed int s32
Definition common.h:11
unsigned int u32
Definition common.h:12
signed short s16
Definition common.h:9
unsigned short u16
Definition common.h:10
unsigned char u8
Definition common.h:8
The task system and its registry (task/)
#define TASK_HEADER(Type)
Definition task.h:39
void * getFileEntry(u32 fileAndIndex)
Files are often archives: a table of offsets from the start of the file.
Definition file_cache.c:230
FileSlot * findOldestFile(void)
The loaded file used least recently (the biggest one on a tie)
Definition file_cache.c:66
void freeFilesFrom(u32 addr)
Frees the files that reach past addr (to load something there)
Definition file_cache.c:205
void updateDecompressor(Decompressor *task)
The decompressor task: unpacks a chunk a frame, frees the buffer at the end.
void freeFile(s32 file)
Drops a loaded file from the cache.
Definition file_cache.c:174
void decompressorAllocBuffer(Decompressor *task)
Makes the buffer big enough for the unpacked size.
void decompressorStep(Decompressor *task)
Unpacks at least chunkSize bytes; back to substate 0 at the end.
void updateFileCache(void)
Starts reading the next queued file and marks the one read as loaded.
Definition file_cache.c:115
void * decompressorRun(Decompressor *task, s32 *data)
Decompressor method: unpacks data at once; returns it unpacked (plain data as it is)
FileSlot * findFreeFileSlot(void)
A free cache slot, or NULL.
Definition file_cache.c:38
void decompressorFree(Decompressor *task)
Decompressor method: frees its buffer.
Definition decompressor.c:4
void * decompressorGetData(Decompressor *task)
Decompressor method: the unpacked data, or NULL while it is unpacking.
FileSlotState
FileSlot.state.
Definition file.h:24
@ FILE_READING
Definition file.h:24
@ FILE_FREE
Definition file.h:24
@ FILE_LOADED
Definition file.h:24
@ FILE_QUEUED
Definition file.h:24
s32 isFileLoading(s32)
0 once the file is in the cache; requests it if needed
Definition file_cache.c:51
s32 cdCheckSector(void)
Checks that the sector just read is the next one.
Definition cd_reader.c:17
void cdSyncCallback(s32 status, u_char *result)
The CdSyncCallback: Setloc, Setmode (double speed, whole sectors), ReadN, and CdlPause once the secto...
Definition cd_reader.c:54
s32 fileExists(s32 file)
Whether the disc has file file
void cdReadyCallback(s32 status, u_char *result)
The CdReadyCallback: copies each sector as it arrives, pauses at the end.
Definition cd_reader.c:34
u16 FILE_SECTOR_COUNTS[]
void readFile(s32 file, s32 offset, s32 size, void *buffer, s32 *done)
Starts reading size sectors (0: all) of a file into buffer; ignored while busy.
Definition cd_reader.c:110
Decompressor * createDecompressor(void)
A decompressor task.
s32 isCdReading(void)
Whether the CD reader is busy.
Definition cd_reader.c:95
CdReader CD_READER
The CD reader.
Definition cd_reader.c:10
void * getArchiveEntry(s32 index, void *archive)
Entry index (the low 16 bits) of an archive already in memory.
Definition file_cache.c:239
s32 getFileSectorCount(s32 file)
The sectors of a file.
CdReadState
What the CD reader waits for (CdReader.state): the command each state sent to complete,...
Definition file.h:39
@ CD_READ_SETMODE
Definition file.h:42
@ CD_READ_SECTORS
Definition file.h:44
@ CD_READ_SETLOC
Definition file.h:41
@ CD_READ_IDLE
Definition file.h:40
@ CD_READ_READN
Definition file.h:43
s32 LANGUAGE
2-5
Definition eu_settings.c:13
void markCachedFiles(void)
markCachedFiles + touchMarkedFiles: refresh the files cached before a load
Definition file_cache.c:246
void requestFile(s32)
Queues a file for the cache (just marks it used when it is there)
Definition file_cache.c:98
CdSectorHeader CD_SECTOR_HEADER
file/cd_reader.c's
Definition game_bss.c:28
void freeAllFiles(void)
Drops every file from the cache.
Definition file_cache.c:188
FileTableFuncs FILE_TABLE
The disc's file table.
void decompressorStart(Decompressor *task, s32 *data, s32 chunkSize)
Decompressor method: starts unpacking data chunkSize bytes a frame.
void decompressorSetData(Decompressor *task, s32 *data)
Points the decompressor at data, which is RLEN-packed or plain.
#define FILE_CACHE_SLOTS
The files the cache holds at most.
Definition file.h:27
void waitForFile(s32 file)
Reads a file into the cache before returning.
Definition file_cache.c:152
void evictOldestFile(void)
Frees the loaded file used least recently, to make room in the heap.
Definition file_cache.c:86
s32 getFileSector(s32 file)
The first sector of a file.
s32 FILE_SECTORS[]
Where each file starts on the disc, and its size, in sectors.
Definition file_table.c:838
void * loadFile(u32 file)
Returns the file's data, reading it now if it is not in the cache.
Definition file_cache.c:160
void touchMarkedFiles(void)
Marks the files markCachedFiles saw as just used (the European and Japanese versions,...
Definition file_cache.c:263
FileSlot * findFileSlot(s32 file)
The cache slot of a file, or NULL.
Definition file_cache.c:25
FileCache FILE_CACHE
The file cache.
Definition file_cache.c:7
void getFilePos(s32 file, s32 offset, void *pos)
The CdlLOC of sector offset of a file.
void startCdRead(void)
Starts the read readFile set up: seeks to its first sector.
Definition cd_reader.c:100
Reads whole sectors of a file with CdlReadN, from callbacks.
Definition file.h:51
s32 sector
Definition file.h:59
void(* read)(s32 file, s32 offset, s32 size, void *buf, s32 *done)
Definition file.h:64
s32(* isBusy)(void)
Definition file.h:63
s32 file
Definition file.h:53
s32 sectorCount
Definition file.h:55
s32 nextSector
Definition file.h:62
s32 offset
in sectors
Definition file.h:54
u8 * buffer
Definition file.h:56
s32 state
CD_READ_*.
Definition file.h:52
u8 * dst
Definition file.h:61
u8 loc[4]
Definition file.h:58
s32 sectorsLeft
Definition file.h:60
s32 * done
set to 1 when the read is complete
Definition file.h:57
What cdCheckSector reads first of each sector, the 3 words that CdlModeSize1 puts before its data: th...
Definition file.h:74
u_char pad[4]
never read: the 4 bytes past the header, see above
Definition file.h:77
CdlLOC pos
Definition file.h:75
u_char subheader[8]
Definition file.h:76
Expands "RLEN" data (a run-length encoding: a byte n < 0x80 copies n bytes, n | 0x80 repeats the next...
Definition file.h:110
s32 * begin
Definition file.h:113
void(* free)()
Definition file.h:124
s32 bufferSize
Definition file.h:116
void(* start)()
Definition file.h:123
void * dst
Definition file.h:119
s32 size
Definition file.h:115
void * src
the next packed byte
Definition file.h:118
void * buffer
Definition file.h:117
s32 * data
Definition file.h:112
s32 compressed
Definition file.h:114
s32 chunkSize
Definition file.h:120
The file cache (FILE_CACHE): up to 64 files loaded in the heap (tag 3), read in the background one at...
Definition file.h:85
FileSlot slots[64]
Definition file.h:87
void(* touchMarked)(void)
Definition file.h:99
void(* free)(s32 file)
Definition file.h:93
void(* markCached)(void)
Definition file.h:98
s32(* isLoading)(s32 file)
Definition file.h:88
void(* freeFrom)(u32 addr)
Definition file.h:95
void(* evictOldest)(void)
Definition file.h:89
void(* request)(s32 file)
Definition file.h:90
void(* update)(void)
Definition file.h:91
void(* freeAll)(void)
Definition file.h:94
s32 pending
Definition file.h:86
s16 marked
Definition file.h:31
s16 state
FILE_*.
Definition file.h:30
s32 file
Definition file.h:32
s32 lastUsed
GFX time.
Definition file.h:33
void * data
Definition file.h:34
The disc's file table (FILE_TABLE): files are numbered, not named.
Definition file.h:15
s32(* getSectorCount)(s32 file)
Definition file.h:17
s32(* getSector)(s32 file)
Definition file.h:18
s32(* exists)(s32 file)
Definition file.h:16
void(* getPos)(s32 file, s32 offset, void *loc)
Definition file.h:19