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