Digimon World 3 decomp
The European version, SLES_039.36
Loading...
Searching...
No Matches
cd_reader.c
Go to the documentation of this file.
1#include "engine/game.h"
2#include <libgs.h>
3#include <libetc.h>
4#include <libsnd.h>
5
6/* -G8 unit: small variables defined here are reached through $gp */
7static u_char CD_MODE[8];
8
9/* The CD reader */
11 0, 0, 0, 0, 0, NULL, {0}, 0, 0, 0, 0,
14};
15
16/* Checks that the sector just read is the next one */
18 s32 pos;
19
20 CdGetSector(&CD_SECTOR_HEADER, 3);
21 pos = CdPosToInt(&CD_SECTOR_HEADER.pos);
22 if (pos == CD_READER.nextSector) {
23 CD_READER.nextSector = pos + 1;
24 return 0;
25 }
26 return -1;
27}
28
29/*
30 * The CdReadyCallback: copies each sector as it arrives, pauses at the end.
31 * The two callbacks take the status as an int, not CdlCB's u_char, which
32 * GCC would truncate on entry (more code), so they are registered with a cast.
33 */
34void cdReadyCallback(s32 status, u_char *result) {
35 if (status == CdlDataReady) {
36 if (cdCheckSector() != 0) {
37 goto error;
38 }
39 CdGetSector(CD_READER.dst, CD_SECTOR_SIZE / sizeof(u_long));
41 if (--CD_READER.sectorsLeft != 0) {
42 return;
43 }
44 } else {
45 error:
47 }
48 CdReadyCallback(NULL);
49 CdControlF(CdlPause, 0);
50}
51
52/* The CdSyncCallback: Setloc, Setmode (double speed, whole sectors), ReadN,
53 and CdlPause once the sectors are in */
54void cdSyncCallback(s32 status, u_char *result) {
55 switch (status) {
56 case CdlDiskError:
58 CdControlF(CdlPause, 0);
59 } else {
61 }
62 break;
63 case CdlComplete:
64 switch (CD_READER.state) {
65 case CD_READ_SETLOC:
66 CD_MODE[0] = CdlModeSpeed | CdlModeSize1;
67 CdControlF(CdlSetmode, CD_MODE);
69 break;
70 case CD_READ_SETMODE:
71 CdReadyCallback((CdlCB)cdReadyCallback);
72 CdControlF(CdlReadN, 0);
74 break;
75 case CD_READ_READN:
77 break;
78 case CD_READ_SECTORS:
79 CdSyncCallback(NULL);
80 if (CD_READER.sectorsLeft == 0) {
82 if (CD_READER.done != NULL) {
83 *CD_READER.done = 1;
84 }
85 } else {
87 }
88 break;
89 }
90 break;
91 }
92}
93
94/* Whether the CD reader is busy */
97}
98
99/* Starts the read readFile set up: seeks to its first sector */
100void startCdRead(void) {
105 CdSyncCallback((CdlCB)cdSyncCallback);
106 CdControlF(CdlSetloc, CD_READER.loc);
107}
108
109/* Starts reading `size` sectors (0: all) of a file into buffer; ignored while busy */
110void readFile(s32 file, s32 offset, s32 size, void *buffer, s32 *done) {
111 if (isCdReading() == 0) {
112 CD_READER.file = file;
113 CD_READER.offset = offset;
114 CD_READER.buffer = buffer;
115 CD_READER.done = done;
116 if (done != NULL) {
117 *done = 0;
118 }
119 if (size == 0) {
121 } else {
122 CD_READER.sectorCount = size;
123 }
124 FILE_TABLE.getPos(file, offset, CD_READER.loc);
125 CD_READER.sector = FILE_TABLE.getSector(file) + offset;
126 startCdRead();
127 }
128}
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
void cdReadyCallback(s32 status, u_char *result)
The CdReadyCallback: copies each sector as it arrives, pauses at the end.
Definition cd_reader.c:34
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
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
static u_char CD_MODE[8]
-G8 unit: small variables defined here are reached through $gp
Definition cd_reader.c:7
void startCdRead(void)
Starts the read readFile set up: seeks to its first sector.
Definition cd_reader.c:100
#define NULL
Definition common.h:17
signed int s32
Definition common.h:11
#define CD_SECTOR_SIZE
The bytes of data in a sector (Mode 2 Form 1), as the reader reads them.
Definition file.h:48
@ 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
FileTableFuncs FILE_TABLE
The disc's file table.
struct CdSectorHeader CD_SECTOR_HEADER
file/cd_reader.c's
Definition game_bss.c:28
Reads whole sectors of a file with CdlReadN, from callbacks.
Definition file.h:51
s32 sector
Definition file.h:59
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
CdlLOC pos
Definition file.h:75
s32(* getSectorCount)(s32 file)
Definition file.h:17
s32(* getSector)(s32 file)
Definition file.h:18
void(* getPos)(s32 file, s32 offset, void *loc)
Definition file.h:19