1 /*---------------------------------------------------------------------------*
2   Project:  Dolphin
3   File:     create-bnr-4icon.c
4 
5   Copyright 1998, 1999, 2000 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: create-bnr-4icon.c,v $
14   Revision 1.2  02/20/2006 04:13:08  mitu
15   changed include path from dolphin/ to revolution/.
16 
17   Revision 1.1  01/31/2006 10:48:30  mitu
18   (none)
19 
20 
21     1     6/19/01 12:57p Hosogai
22     Initial check-in
23   $NoKeywords: $
24  *---------------------------------------------------------------------------*/
25 
26 /*---------------------------------------------------------------------------*
27    create-bnr-4icon
28 	 A simple program that creates a memory card file with a banner & 4 icons
29  *---------------------------------------------------------------------------*/
30 
31 /*---------------------------------------------------------------------------*
32    Header files
33  *---------------------------------------------------------------------------*/
34 #include <string.h>
35 #include <revolution.h>
36 #include <revolution/card.h>
37 #include <demo.h>
38 
39 #include "create-bnr-4icon.h"
40 
41 /*---------------------------------------------------------------------------*
42    Forward references
43  *---------------------------------------------------------------------------*/
44 void main();
45 static u32 MountCard( s32 chan );
46 static void CreateFile(s32 chan, u32 secSize, u32 blockSize);
47 static void UnmountCard( s32 chan );
48 static void LoadIconRGB5A3( void* buffer, CARDStat* stat );
49 static void DetachCallback( void );
50 static void AttachCallback( void );
51 static void NewFileName( char* fileName );
52 static void PrintResult(s32 result);
53 
54 
55 /*---------------------------------------------------------------------------*
56    Global data
57  *---------------------------------------------------------------------------*/
58 u8 WRAM[CARD_WORKAREA_SIZE] ATTRIBUTE_ALIGN(32);
59 
60 /*---------------------------------------------------------------------------*
61    Application main
62  *---------------------------------------------------------------------------*/
main()63 void main()
64 {
65 	u32 sectorSize;
66 
67 	DEMOInit(NULL);
68 	CARDInit();
69 
70 	sectorSize = MountCard(CARD_CHAN);
71 
72 	CreateFile(CARD_CHAN, sectorSize, FILE_BLOCK_SIZE);
73 
74 	UnmountCard(CARD_CHAN);
75 
76 	OSReport("End of program\n");
77 }
78 
79 /*---------------------------------------------------------------------------*
80 	Name:           LoadIconRGB5A3
81 
82 	Description:    Loads a RGB5A3 icon & banner information into memcard file
83 
84 	Arguments:      file		TPL file name with RGB5A3 data
85 					buffer		memcard data buffer
86 					stat		CARDStat for file
87 
88 	Returns:        none
89  *---------------------------------------------------------------------------*/
LoadIconRGB5A3(void * CARDbuffer,CARDStat * stat)90 static void LoadIconRGB5A3( void* CARDbuffer, CARDStat* stat )
91 {
92 	CARDHeaderRGB5A3 HeaderRGB5A3 ATTRIBUTE_ALIGN(32) =
93 	{
94 		"Yoshi Nintendo\n",
95 		"Banner and 4 icons\n",
96 		0
97 	};
98 	static TPLPalettePtr  tplIcons;
99 	static TPLDescriptorPtr tdpIcons;
100 	static u32 i;
101 
102 	// Load the TPL file
103 	TPLGetPalette(&tplIcons,   "/carddemo/yoshi.tpl");
104 
105 	// Copy the Texels to the structure
106 	for (i=0; i<4; i++)
107 	{
108 		tdpIcons = TPLGet(tplIcons, (u32) i);
109 
110 		// Copy the Texel and CLUT to the structure
111 		memcpy(HeaderRGB5A3.Icons[i], tdpIcons->textureHeader->data,
112 				ICON_RGB5A3_SIZE);
113 	}
114 
115 	CARDSetCommentAddress(stat, 0);
116 	CARDSetIconAddress   (stat, CARD_COMMENT_SIZE);
117 	CARDSetBannerFormat  (stat, CARD_STAT_BANNER_RGB5A3);
118 	CARDSetIconAnim      (stat, CARD_STAT_ANIM_BOUNCE);
119 	for (i=0; i<4; i++)
120 	{
121 		CARDSetIconFormat    (stat, i, CARD_STAT_ICON_RGB5A3);
122 		CARDSetIconSpeed     (stat, i, CARD_STAT_SPEED_SLOW);
123 	}
124 	CARDSetIconFormat    (stat, 4, CARD_STAT_ICON_NONE);
125 	CARDSetIconSpeed     (stat, 4, CARD_STAT_SPEED_END);
126 
127 	// Free the TPLPalette
128 	TPLReleasePalette(&tplIcons);
129 
130 	// Load the banner TPL file
131 	TPLGetPalette(&tplIcons,   "/carddemo/gamecube.tpl");
132 	tdpIcons = TPLGet(tplIcons, (u32) 0);
133 
134 	memcpy(HeaderRGB5A3.Banner, tdpIcons->textureHeader->data, CARD_BANNER_SIZE);
135 	// Free the TPLPalette
136 	TPLReleasePalette(&tplIcons);
137 
138 	// Copy the icon to the buffer
139 	memcpy(CARDbuffer, &HeaderRGB5A3, sizeof(HeaderRGB5A3));
140 }
141 
142 /*---------------------------------------------------------------------------*
143 	Name:           CreateFile
144 
145 	Description:    Creates a file on the memcard
146 
147 	Arguments:      chan		EXI Channel
148 					secSize		memcard sector size
149 					blockSize	memcard block size
150 
151 	Returns:        none
152  *---------------------------------------------------------------------------*/
CreateFile(s32 chan,u32 secSize,u32 blockSize)153 void CreateFile(s32 chan, u32 secSize, u32 blockSize)
154 {
155 	s32 result;
156 	CARDStat stat;
157 	CARDFileInfo FileInfo;
158 	void* CARDData;
159 	char FileName[CARD_FILENAME_MAX]={'\0'};
160 
161 	// Create the new file name
162 	// based on the system clock
163 	NewFileName(FileName);
164 
165 	// Create the file
166 	result = CARDCreate(chan, FileName, secSize*blockSize, &FileInfo);
167 	if (result < 0)
168 	{
169 		OSReport("Failed to create the file.\n");
170 		PrintResult(result);
171 	}
172 	else
173 	{
174 		OSReport("File created successfully name = [%s]\n", FileName);
175 	}
176 
177 	// Get CARDStat of the file that was created
178 	result = CARDGetStatus(chan, FileInfo.fileNo, &stat);
179 	if (result < 0)
180 	{
181 		OSReport("Failed to get CARDStat\n");
182 		PrintResult(result);
183 	}
184 	else
185 	{
186 		OSReport("CARDStat successfully got.\n");
187 	}
188 
189 	// Allocate the temp buffer
190 	// (same size with the file size that was created)
191 	CARDData = OSAlloc(stat.length);
192 
193 	// Load the icon from TPL into the buffer
194 	LoadIconRGB5A3(CARDData, &stat);
195 
196 	// Write the buffer to the memory card
197 	result = CARDWrite(&FileInfo, CARDData, (s32) stat.length, 0);
198 	if (result < 0)
199 	{
200 		OSReport("Failed to CARDWrite.\n");
201 		PrintResult(result);
202 	}
203 	else
204 	{
205 		OSReport("CARDWrite successfully.\n");
206 	}
207 
208 	// Set the CARDStat which was modified.
209 	result = CARDSetStatus(chan, FileInfo.fileNo, &stat);
210 	if (result < 0)
211 	{
212 		OSReport("Failed to set CARDStat\n");
213 		PrintResult(result);
214 	}
215 	else
216 	{
217 		OSReport("CARDStat successfully set.\n");
218 	}
219 
220 	// Free the temp buffer
221 	OSFree(CARDData);
222 }
223 
224 /*---------------------------------------------------------------------------*
225 	Name:           MountCard
226 
227 	Description:    Mounts and checks memory card
228 
229 	Arguments:      chan		EXI Channel
230 
231 	Returns:        none
232  *---------------------------------------------------------------------------*/
MountCard(s32 chan)233 static u32 MountCard( s32 chan )
234 {
235 	u32 sectorSize;
236 	s32 result;
237 
238 	// Probe the card
239 	OSReport("Please insert the memory card in slot B\n");
240 	result = FALSE;
241 	while (result==FALSE)
242 	{
243 		result = CARDProbe(chan);
244 	}
245 
246 	// Mount the card
247 	result = CARDMount(chan, WRAM, (CARDCallback)DetachCallback);
248 	if (result < 0)
249 	{
250 		OSReport("CARD failed to mount\n");
251 		// Do not halt here.
252 		// (needs to check unformatted card.)
253 	}
254 	else
255 	{
256 		OSReport("CARD mounted successfully\n");
257 	}
258 
259 	// If the card is broken, reformat the card
260 	result = CARDCheck(chan);
261 	if (result == CARD_RESULT_BROKEN){
262 
263 		// If the card is broken, format it
264 		result = CARDFormat(chan);
265 		if (result < 0)
266 		{
267 			OSReport("Failed to format\n");
268 			PrintResult(result);
269 		}
270 		else
271 		{
272 			OSReport("CARD formatted successfully\n");
273 		}
274 	}
275 	else if(result==CARD_RESULT_ENCODING)
276 	{
277 		OSReport("Wrong Encoding.\n");
278 		PrintResult(result);
279 	}
280 	else
281 	{
282 		OSReport("CARD is preformatted.\n");
283 	}
284 
285 	// Get the sector size
286 	result = CARDGetSectorSize(chan, &sectorSize);
287 	if (result < 0)
288 	{
289 		OSReport("CARD faile to Get the sector size.\n");
290 		PrintResult(result);
291 	}
292 	else
293 	{
294 		OSReport("sectorSize = %d\n", sectorSize);
295 	}
296 	return sectorSize;
297 }
298 
299 /*---------------------------------------------------------------------------*
300 	Name:           UnmountCard
301 
302 	Description:    Unmounts memory card
303 
304 	Arguments:      chan		EXI Channel
305 
306 	Returns:        none
307  *---------------------------------------------------------------------------*/
UnmountCard(s32 chan)308 static void UnmountCard(s32 chan)
309 {
310 	s32 result;
311 	// Unmount the card
312 	result = CARDUnmount(chan);
313 	if (result < 0)
314 	{
315 		OSReport("CARD failed to unmount\n");
316 		PrintResult(result);
317 	}
318 	else
319 	{
320 		OSReport("CARD Unmounted successfully\n");
321 	}
322 }
323 
324 /*---------------------------------------------------------------------------*
325 	Name:           DetachCallback & AttachCallback
326 
327 	Description:    Simple memory card callbacks
328 
329 	Arguments:      none
330 
331 	Returns:        none
332  *---------------------------------------------------------------------------*/
DetachCallback(void)333 static void DetachCallback( void )
334 {
335 	OSReport("Card was detached.\n");
336 }
AttachCallback(void)337 static void AttachCallback( void )
338 {
339 	OSReport("Card was attached.\n");
340 }
341 
342 /*---------------------------------------------------------------------------*
343 	Name:           NewFileName
344 
345 	Description:    Creates a filename using the date
346 
347 	Arguments:      fileName	pointer to allocated string
348 
349 	Returns:        none
350  *---------------------------------------------------------------------------*/
NewFileName(char * fileName)351 static void NewFileName(char* fileName)
352 {
353 	OSTime         time;
354 	OSCalendarTime ct;
355 
356 	time = OSGetTime();
357 	OSTicksToCalendarTime(time, &ct);
358 	sprintf(fileName, "%04d/%02d/%02d %02d:%02d:%02d",
359 			ct.year, ct.mon + 1, ct.mday, ct.hour, ct.min, ct.sec);
360 }
361 
362 /*---------------------------------------------------------------------------*
363 	Name:           PrintResult
364 
365 	Description:    Prints out the defined result (and halt upon error)
366 
367 	Arguments:      result		CARD API result
368 
369 	Returns:        none
370  *---------------------------------------------------------------------------*/
PrintResult(s32 result)371 static void PrintResult(s32 result)
372 {
373 	switch (result)
374 	{
375 		case CARD_RESULT_READY:
376 			OSReport("CARD_RESULT_READY\n");
377 			break;
378 		case CARD_RESULT_FATAL_ERROR:
379 			OSHalt("CARD_RESULT_FATAL_ERROR\n");
380 			break;
381 		case CARD_RESULT_WRONGDEVICE:
382 			OSHalt("CARD_RESULT_WRONGDEVICE\n");
383 			break;
384 		case CARD_RESULT_NOCARD:
385 			OSHalt("CARD_RESULT_NOCARD\n");
386 			break;
387 		case CARD_RESULT_BUSY:
388 			OSHalt("CARD_RESULT_BUSY\n");
389 			break;
390 		case CARD_RESULT_IOERROR:
391 			OSHalt("CARD_RESULT_IOERROR\n");
392 			break;
393 		case CARD_RESULT_BROKEN:
394 			OSHalt("CARD_RESULT_BROKEN\n");
395 			break;
396 		case CARD_RESULT_EXIST:
397 			OSHalt("CARD_RESULT_EXIST\n");
398 			break;
399 		case CARD_RESULT_NOENT:
400 			OSHalt("CARD_RESULT_NOENT\n");
401 			break;
402 		case CARD_RESULT_INSSPACE:
403 			OSHalt("CARD_RESULT_INSSPACE\n");
404 			break;
405 		case CARD_RESULT_NAMETOOLONG:
406 			OSHalt("CARD_RESULT_NAMETOOLONG\n");
407 			break;
408 		default:
409 			OSHalt("Illegal\n");
410 			break;
411 	}
412 }
413