1 /*---------------------------------------------------------------------------*
2 Project: crc demo
3 File: crcdemo.c
4
5 Copyright (C) 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: crcdemo.c,v $
14 Revision 1.2 2008/05/19 08:49:08 nakano_yoshinobu
15 Transplanted from RevoEX.
16
17
18 $NoKeywords: $
19 *---------------------------------------------------------------------------*/
20 #include <demo.h>
21 #include <revolution/os.h>
22 #include <string.h>
23
24 #define LINES 24
25 #define STACK_SIZE 4000
26
27 static void DoReport(void);
28 static void* DoTest(void* arg);
29
30 GXColor Black = { 0, 0, 0, 0 };
31 GXColor Blue = { 0, 0, 192, 0 };
32 GXColor Red = { 255, 0, 0, 0 };
33 GXColor Green = { 0, 224, 0, 0 };
34 GXColor White = { 255, 255, 255, 0 };
35
36 GXColor SucceededGreen = { 0, 128, 0, 0 };
37 GXColor FailedRed = { 128, 0, 0, 0 };
38
39 static char ReportBuffer[4096];
40 static char PrintBuffer[4096];
41
42 static OSThread testThread;
43 static u8 testThreadStack[STACK_SIZE] ATTRIBUTE_ALIGN(32);
44
45
46 /*---------------------------------------------------------------------------*
47 Application main loop
48 *---------------------------------------------------------------------------*/
main(void)49 void main(void)
50 {
51 DEMOInit(NULL);
52 if ( ! DEMOInitROMFont() )
53 {
54 OSHalt("DEMOInitROMFont()");
55 }
56
57 // Clear EFB
58 GXSetCopyClear(Blue, GX_MAX_Z24);
59 GXCopyDisp(DEMOGetCurrentBuffer(), GX_TRUE);
60
61 (void)OSReport("CRC Demo: Test CRC functions\n");
62
63 // Launch the test thread
64 if( ! OSCreateThread( &testThread, DoTest, NULL,
65 (void*)((u32)testThreadStack + STACK_SIZE), STACK_SIZE,
66 20, 0 ) )
67 {
68 OSHalt( "OSCrateThread()" );
69 }
70 if ( OSResumeThread( &testThread ) < 0 )
71 {
72 OSHalt( "OSResumeThread()" );
73 }
74
75 while ( ! OSIsThreadTerminated(&testThread) )
76 {
77 DEMOBeforeRender();
78 DoReport();
79 DEMODoneRender();
80 }
81
82 {
83 BOOL result;
84
85 if( ! OSJoinThread( &testThread, (void**)&result ) )
86 {
87 OSHalt( "OSJoinThread()" );
88 }
89 if ( result )
90 {
91 // succeeded
92 OSReport("------ Test Succeeded ------\n");
93 GXSetCopyClear(SucceededGreen, GX_MAX_Z24);
94 GXCopyDisp(DEMOGetCurrentBuffer(), GX_TRUE);
95 }
96 else
97 {
98 // failed
99 OSReport("****** Test Failed ******\n");
100 GXSetCopyClear(FailedRed, GX_MAX_Z24);
101 GXCopyDisp(DEMOGetCurrentBuffer(), GX_TRUE);
102 }
103 }
104
105 while ( TRUE )
106 {
107 DEMOBeforeRender();
108 DoReport();
109 DEMODoneRender();
110 }
111 }
112
113 /*---------------------------------------------------------------------------*
114 Display Settings
115 *---------------------------------------------------------------------------*/
116
DoReport(void)117 static void DoReport(void)
118 {
119 GXRenderModeObj* rmp;
120
121 rmp = DEMOGetRenderModeObj();
122 DEMOInitCaption(DM_FT_XLU, (s16) rmp->fbWidth, (s16) rmp->efbHeight);
123 DEMOSetROMFontSize(16, -1);
124 (void)DEMORFPuts(25, 25, 0, ReportBuffer);
125 }
126
127 // Define program-specific OSReport() function.
OSReport(const char * msg,...)128 void OSReport(const char* msg, ...)
129 {
130 BOOL enabled;
131 va_list marker;
132 u32 len;
133 int c;
134 char* p;
135 char* q;
136
137 va_start(marker, msg);
138 (void)vsnprintf(PrintBuffer, sizeof(PrintBuffer)-1, msg, marker);
139 va_end(marker);
140 PrintBuffer[sizeof(PrintBuffer)-1] = 0;
141
142 (void)printf("%s", PrintBuffer);
143
144 enabled = OSDisableInterrupts();
145
146 (void)strncat(ReportBuffer, PrintBuffer, sizeof(ReportBuffer)-1);
147 ReportBuffer[sizeof(ReportBuffer)-1] = 0;
148 len = strlen(ReportBuffer);
149
150 c = 0;
151 q = ReportBuffer;
152 for (p = ReportBuffer; p < ReportBuffer + len; ++p)
153 {
154 if (*p == '\n')
155 {
156 ++c;
157 if (LINES <= c)
158 {
159 q = strchr(q, '\n');
160 }
161 }
162 }
163 if (LINES <= c)
164 {
165 (void)memmove(ReportBuffer, q + 1, len - (q - ReportBuffer));
166 }
167
168 (void)OSRestoreInterrupts(enabled);
169 }
170
171
172 /*---------------------------------------------------------------------------*
173 Tests
174 *---------------------------------------------------------------------------*/
175
176 #define PrintResultEq( a, b, f ) \
177 do { OSReport( ((a) == (b)) ? "[--OK--] " : "[**NG**] " ); (f) = (f) && ((a) == (b)); } while( FALSE )
178 #define PrintResultDigestEq( a, b, l, f ) \
179 do { OSReport( (memcmp((a), (b), (l)) == 0) ? "[--OK--] " : "[**NG**] " ); (f) = (f) && (memcmp((a), (b), (l)) == 0); } while( FALSE )
180
DoTest(void * arg)181 static void* DoTest(void* arg)
182 {
183 BOOL flag = TRUE;
184 int i;
185
186 (void)arg;
187
188 // Test CRC-16
189 {
190 char *a[] = {
191 "123456789",
192 };
193 u16 result_crc16[] = {
194 0xbb3d,
195 };
196 for (i = 0; i < sizeof(a) / sizeof(char *); i++)
197 {
198 u16 result;
199
200 result = OSCalcCRC16(a[i], strlen(a[i]));
201 PrintResultEq(result, result_crc16[i], flag);
202 OSReport("OSCalcCRC16(%s) = %04x\n", a[i], result);
203 }
204 }
205
206 // Test CRC-32
207 {
208 char *a[] = {
209 "123456789",
210 };
211 u32 result_crc32[] = {
212 0xcbf43926,
213 };
214 for (i = 0; i < sizeof(a) / sizeof(char *); i++)
215 {
216 u32 result;
217
218 result = OSCalcCRC32(a[i], strlen(a[i]));
219 PrintResultEq(result, result_crc32[i], flag);
220 OSReport("OSCalcCRC32(%s) = %04x\n", a[i], result);
221 }
222 }
223 return (void*)flag;
224 }
225