1 /*---------------------------------------------------------------------------*
2   Project:  CNT API DEMO
3   File:     cntdemo.c
4 
5   Copyright (C) 2006 - 2008 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: cntdemo.c,v $
14   Revision 1.5  2008/01/10 13:52:47  wada_jumpei
15   Modified along the change of CNTRead.
16 
17   Revision 1.4  2007/10/11 02:12:54  wada_jumpei
18   Rolled back.
19 
20   Revision 1.1  2006/01/17 02:43:50  wada
21   Initial check in.
22 
23  *---------------------------------------------------------------------------*/
24 
25 /*---------------------------------------------------------------------------*
26   This program shows how to use CNT API
27   InitHandle, Open, Read, Seek, Close, and ReleaseHandle.
28   If this program is built with no option, this will be Disc application.
29   If built with "NANDAPP=TRUE CNT_IDX=2" option, this will be NAND application.
30  *---------------------------------------------------------------------------*/
31 
32 #include <revolution.h>
33 #include <revolution/os.h>
34 #include <revolution/dvd.h>
35 #include <revolution/arc.h>
36 #include <revolution/mem.h>
37 #include <demo.h>
38 #include <revolution/cnt.h>
39 
40 
41 // The names of the files we are going to read.
42 char*   fileName1 = "test1.txt";
43 char*   fileName2 = "test2.txt";
44 
45 // This is the number of content index to be used in this program.
46 #define TARGET_CONTENT  2
47 
main(void)48 void main(void)
49 {
50     CNTHandle   Cnt;
51     CNTFileInfo fileInfo1;
52     CNTFileInfo fileInfo2;
53     CNTFileInfo fileInfo3;
54     u32         fileSize1;
55     u32         fileSize2;
56     u32         fileSize3;
57     u8*         buffer1;        // pointer to the buffer
58     u8*         buffer2;        // pointer to the buffer
59     u8*         buffer3;        // pointer to the buffer
60     u8*         ptr1;
61     u8*         ptr2;
62     u8*         ptr3;
63     u32         i;
64     s32         rv;	            // for checking error
65 
66     DEMOInit(NULL);
67     CNTInit();
68 
69     rv = CNTInitHandle(TARGET_CONTENT, &Cnt, &DemoAllocator1);
70 
71     if(rv != CNT_RESULT_OK)
72     {
73         OSReport("Init handle finished: %d\n", rv);
74     }
75 
76     // Open the filename1 to fileInfo1
77     rv = CNTOpen(&Cnt, fileName1, &fileInfo1);
78 
79     if (rv != CNT_RESULT_OK)
80     {
81         OSReport("Cannot open file \"%s\"", fileName1);
82         OSHalt("");
83     }
84 
85     // Open the filename2 to fileInfo2 and fileInfo3
86     // (Then reads half of the file)
87     rv = CNTOpen(&Cnt, fileName2, &fileInfo2);
88 
89     if (rv != CNT_RESULT_OK)
90     {
91         OSReport("Cannot open file \"%s\"", fileName1);
92         OSHalt("");
93     }
94 
95     rv = CNTOpen(&Cnt, fileName2, &fileInfo3);
96 
97     if (rv != CNT_RESULT_OK)
98     {
99         OSReport("Cannot open file \"%s\"", fileName2);
100         OSHalt("");
101     }
102 
103     // Get the size of the files
104     fileSize1 = CNTGetLength(&fileInfo1);
105     fileSize2 = CNTGetLength(&fileInfo2) >> 1; // filesize / 2
106     fileSize3 = fileSize2;
107 
108 
109     // Allocate buffers to read the files.
110     // Note that pointers returned by Allocator are all 32byte aligned.
111     buffer1 = (u8*)MEMAllocFromAllocator(&DemoAllocator1, OSRoundUp32B(fileSize1));
112     buffer2 = (u8*)MEMAllocFromAllocator(&DemoAllocator1, OSRoundUp32B(fileSize2));
113     buffer3 = (u8*)MEMAllocFromAllocator(&DemoAllocator1, OSRoundUp32B(fileSize3));
114 
115     // reads filename1 at one time
116     rv = CNTRead(&fileInfo1, (void*)buffer1, (u32)OSRoundUp32B(fileSize1));
117 
118     // If CNTRead suceeds, it returns length (the number of bytes).
119     if (rv < 0 || rv != (u32)OSRoundUp32B(fileSize1))
120     {
121         OSHalt("Error occurred when issuing read for the first file");
122     }
123 
124     // reads filename2 dividing into two phase.
125     // First, reads the half of filename2 nomally.
126     rv = CNTRead(&fileInfo2, (void*)buffer2, (u32)OSRoundUp32B(fileSize2));
127 
128     if (rv < 0 || rv != (u32)OSRoundUp32B(fileSize2))
129     {
130         OSHalt("Error occurred when issuing read for the second file");
131     }
132 
133     // Next, seeks to the half of filename2 and reads remaining part.
134     rv = CNTSeek(&fileInfo3, (s32)fileSize2, CNT_SEEK_SET);
135 
136     if (rv != CNT_RESULT_OK)
137     {
138         OSHalt("Error occurred when issuing seek for the second file");
139     }
140 
141     rv = CNTRead(&fileInfo3, (void*)buffer3, (u32)OSRoundUp32B(fileSize3));
142 
143     /*----------------------------    **** WARNING **** ----------------------------------
144 
145     Because the end of the archive file is not aligned to 32B,
146     For DVD application, CNTRead returns number of bytes "really" read (return 64).
147     For NAND application, CNTRead returns "real" number of bytes of the file (return 56).
148 
149     When adding some files in dvddata/content2/, check if CNTRead returns
150     "(u32)OSRoundUp32B(fileSize3)".
151 
152 
153     if (rv < 0 || rv != (u32)OSRoundUp32B(fileSize3))
154     {
155         OSHalt("Error occurred when issuing read for the second file");
156     }
157 
158     ------------------------------------------------------------------------------------*/
159 
160 
161     OSReport("read end\n");
162     OSReport("\n");
163 
164     // Close the files
165     CNTClose(&fileInfo1);
166     CNTClose(&fileInfo2);
167     CNTClose(&fileInfo3);
168 
169     // Show first file (filename1)
170     OSReport("First file:\n");
171     ptr1 = buffer1;
172     for (i = 0; i < fileSize1; i++)
173     {
174         OSReport("%c", *ptr1++);
175     }
176 
177     // Show second file (filename2)
178     OSReport("Second file:\n");
179     ptr2 = buffer2;
180     for (i = 0; i < fileSize2; i++)
181     {
182         OSReport("%c", *ptr2++);
183     }
184 
185     ptr3 = buffer3;
186     for (i = 0; i < fileSize3; i++)
187     {
188         OSReport("%c", *ptr3++);
189     }
190     OSReport("\n\n");
191 
192 
193     MEMFreeToAllocator(&DemoAllocator1, buffer1);
194     MEMFreeToAllocator(&DemoAllocator1, buffer2);
195     MEMFreeToAllocator(&DemoAllocator1, buffer3);
196 
197     // make sure to release CNTHandle
198     CNTReleaseHandle(&Cnt);
199 
200     // for safety device shutdown
201     CNTShutdown();
202 
203     OSHalt("End of demo");
204 
205     // NOT REACHED HERE
206 }
207