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