1 /*---------------------------------------------------------------------------*
2 Project: Dolphin DVD Demo3
3 File: dvddemo3.c
4
5 Copyright 1998, 1999 Nintendo. All rights reserved.
6
7 These coded instructions, statements, and computer programs contain
8 proprietary information of Nintendo of America Inc. and/or Nintendo
9 Company Ltd., and are protected by Federal copyright law. They may
10 not be disclosed to third parties or copied or duplicated in any form,
11 in whole or in part, without the prior written consent of Nintendo.
12
13 $Log: dvddemo3.c,v $
14 Revision 1.2 02/20/2006 04:13:09 mitu
15 changed include path from dolphin/ to revolution/.
16
17 Revision 1.1 2006/01/17 02:43:50 hiratsu
18 Initial check in.
19
20
21 6 2000/05/26 5:45p Hashida
22 #ifdef'ed out the comment "Hit Command+Q to quit this demo, which is
23 only needed for Mac.
24
25 5 2000/03/01 3:27p Hashida
26 Changed #ifndef EPPC to #ifdef MAC to ifdef out DVDSetRoot.
27
28 4 2000/02/29 5:55p Hashida
29 Changed /dolphin/data to /dolphin/dvddata
30
31 3 2000/02/25 5:34p Hashida
32 Changed so that it works on MINNOW_MARLIN
33
34 2 2000/02/16 2:39p Tian
35 Removed DVDSetRoot for EPPC builds
36
37 11 1999/09/13 3:56p Hashida
38 Removed warnings.
39
40 10 1999/08/27 3:35p Yasu
41 Add #pragma unused() to avoid warning messages
42
43 9 1999/08/26 10:45p Hashida
44 Removed DCInvalidate because DVDRead* functions do that now.
45
46 6 1999/06/11 10:50p Hashida
47 Changed to use DVDSetRoot()
48
49 5 1999/06/09 9:22p Hashida
50 Changed to use OSAlloc to allocate the buffer for files.
51 Added "volatile" to the variables that are accessed from two contexts.
52 Used DVDGetLength for demo1 and demo2.
53
54 2 1999/06/07 8:26p Hashida
55 changed the files' path name from "dvdoverview" to "dvddemo"
56
57 1 1999/06/07 2:47p Hashida
58 initial revision
59
60 $NoKeywords: $
61 *---------------------------------------------------------------------------*/
62
63 /*---------------------------------------------------------------------------*
64 This program shows how to use readAsync function
65 *---------------------------------------------------------------------------*/
66
67
68 #include <revolution.h>
69
70 void main ( void );
71 static void MyOSInit( void );
72 static void callback(s32 result, DVDFileInfo* fileInfo);
73
74 // These variables should be declared "volatile" because this is shared
75 // by two contexts.
76 volatile BOOL error = FALSE;
77 volatile s32 readCount = 0;
78
79 // The names of the files we are going to read.
80 char* fileName1 = "texts/test1.txt";
81 char* fileName2 = "texts/test2.txt";
82
83
84 /*---------------------------------------------------------------------------*
85 Name: callback
86
87 Description: callback function for DVDReadAsync
88
89 Arguments: result -1 if read fails, file size if succeeded.
90 fileInfo fileInfo for the file transferred.
91
92 Returns: none
93 *---------------------------------------------------------------------------*/
callback(s32 result,DVDFileInfo * fileInfo)94 static void callback(s32 result, DVDFileInfo* fileInfo)
95 {
96 #pragma unused(fileInfo)
97
98 if (result == -1)
99 {
100 error = TRUE;
101 }
102 else
103 {
104 readCount++;
105 }
106 return;
107 }
108
main(void)109 void main(void)
110 {
111 DVDFileInfo fileInfo1;
112 DVDFileInfo fileInfo2;
113 u32 fileSize1;
114 u32 fileSize2;
115 u8* buffer1; // pointer to the buffer
116 u8* buffer2; // pointer to the buffer
117 u8* ptr1;
118 u8* ptr2;
119 u32 count1;
120 u32 count2;
121 u32 i;
122
123 MyOSInit();
124
125 #ifdef MAC
126 OSReport("\n-----------------------------------");
127 OSReport("\n Hit Command+Q to quit this demo");
128 OSReport("\n-----------------------------------\n\n");
129 #endif
130
131 DVDInit();
132
133 #ifdef MAC
134 DVDSetRoot("DOLPHIN/dvddata");
135 #endif
136
137 // Open the first file
138 if (FALSE == DVDOpen(fileName1, &fileInfo1))
139 {
140 OSReport("Cannot open file \"%s\"", fileName1);
141 OSHalt("");
142 }
143 // Open the second file
144 if (FALSE == DVDOpen(fileName2, &fileInfo2))
145 {
146 OSReport("Cannot open file \"%s\"", fileName2);
147 OSHalt("");
148 }
149
150 // Get the size of the files
151 fileSize1 = DVDGetLength(&fileInfo1);
152 fileSize2 = DVDGetLength(&fileInfo2);
153
154 // Allocate buffers to read the files.
155 // Note that pointers returned by OSAlloc are all 32byte aligned.
156 buffer1 = (u8*)OSAlloc(OSRoundUp32B(fileSize1));
157 buffer2 = (u8*)OSAlloc(OSRoundUp32B(fileSize2));
158
159 if (FALSE == DVDReadAsync(&fileInfo1, (void*)buffer1,
160 (s32)OSRoundUp32B(fileSize1), 0, callback))
161 {
162 OSHalt("Error occurred when issuing read for the first file");
163 }
164
165 // You can issue next asynchronous read without waiting for the
166 // previous read to finish
167 if (FALSE == DVDReadAsync(&fileInfo2, (void*)buffer2,
168 (s32)OSRoundUp32B(fileSize2), 0, callback))
169 {
170 OSHalt("Error occurred when issuing read for the second file");
171 }
172
173 // Counter to measure the time
174 count1 = 0;
175 count2 = 0;
176
177 while (readCount < 2)
178 {
179 if (readCount == 0)
180 {
181 count1++;
182 }
183 else if (readCount == 1)
184 {
185 count2++;
186 }
187
188 if (error)
189 {
190 OSReport("readCount is %d\n", readCount);
191 if (readCount == 0)
192 {
193 OSHalt("Error occurred when reading the first file");
194 }
195 else
196 {
197 OSHalt("Error occurred when reading the second file");
198 }
199 }
200
201 }
202
203 OSReport("read end\n");
204 OSReport(" %d loops to read the first file\n", count1);
205 OSReport(" %d loops to read the second file\n", count2);
206 OSReport("\n");
207
208 // Close the files
209 DVDClose(&fileInfo1);
210 DVDClose(&fileInfo2);
211
212 // Show the files
213 OSReport("First file:\n");
214 ptr1 = buffer1;
215 for (i = 0; i < fileSize1; i++)
216 {
217 OSReport("%c", *ptr1++);
218 }
219 OSReport("\n\n");
220
221 OSReport("Second file:\n");
222 ptr2 = buffer2;
223 for (i = 0; i < fileSize2; i++)
224 {
225 OSReport("%c", *ptr2++);
226 }
227 OSReport("\n\n");
228
229 OSFree(buffer1);
230 OSFree(buffer2);
231
232 OSHalt("End of demo");
233
234 // NOT REACHED HERE
235 }
236
237 /*---------------------------------------------------------------------------*
238 Name: MyOSInit
239
240 Description: Initialize the operating system.
241 Create a heap so we can use OSAlloc().
242
243 Arguments: none
244
245 Returns: none
246 *---------------------------------------------------------------------------*/
MyOSInit(void)247 static void MyOSInit ( void )
248 {
249 void* arenaLo;
250 void* arenaHi;
251
252 OSInit();
253
254 arenaLo = OSGetArenaLo();
255 arenaHi = OSGetArenaHi();
256
257 // OSInitAlloc should only ever be invoked once.
258 arenaLo = OSInitAlloc(arenaLo, arenaHi, 1); // 1 heap
259 OSSetArenaLo(arenaLo);
260
261 // The boundaries given to OSCreateHeap should be 32B aligned
262 OSSetCurrentHeap(OSCreateHeap((void*)OSRoundUp32B(arenaLo),
263 (void*)OSRoundDown32B(arenaHi)));
264 // From here on out, OSAlloc and OSFree behave like malloc and free
265 // respectively
266
267 OSSetArenaLo(arenaLo = arenaHi);
268
269 return;
270 }
271