/*---------------------------------------------------------------------------* Project: crc demo File: crcdemo.c Copyright (C) 2008 Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Log: crcdemo.c,v $ Revision 1.2 2008/05/19 08:49:08 nakano_yoshinobu Transplanted from RevoEX. $NoKeywords: $ *---------------------------------------------------------------------------*/ #include #include #include #define LINES 24 #define STACK_SIZE 4000 static void DoReport(void); static void* DoTest(void* arg); GXColor Black = { 0, 0, 0, 0 }; GXColor Blue = { 0, 0, 192, 0 }; GXColor Red = { 255, 0, 0, 0 }; GXColor Green = { 0, 224, 0, 0 }; GXColor White = { 255, 255, 255, 0 }; GXColor SucceededGreen = { 0, 128, 0, 0 }; GXColor FailedRed = { 128, 0, 0, 0 }; static char ReportBuffer[4096]; static char PrintBuffer[4096]; static OSThread testThread; static u8 testThreadStack[STACK_SIZE] ATTRIBUTE_ALIGN(32); /*---------------------------------------------------------------------------* Application main loop *---------------------------------------------------------------------------*/ void main(void) { DEMOInit(NULL); if ( ! DEMOInitROMFont() ) { OSHalt("DEMOInitROMFont()"); } // Clear EFB GXSetCopyClear(Blue, GX_MAX_Z24); GXCopyDisp(DEMOGetCurrentBuffer(), GX_TRUE); (void)OSReport("CRC Demo: Test CRC functions\n"); // Launch the test thread if( ! OSCreateThread( &testThread, DoTest, NULL, (void*)((u32)testThreadStack + STACK_SIZE), STACK_SIZE, 20, 0 ) ) { OSHalt( "OSCrateThread()" ); } if ( OSResumeThread( &testThread ) < 0 ) { OSHalt( "OSResumeThread()" ); } while ( ! OSIsThreadTerminated(&testThread) ) { DEMOBeforeRender(); DoReport(); DEMODoneRender(); } { BOOL result; if( ! OSJoinThread( &testThread, (void**)&result ) ) { OSHalt( "OSJoinThread()" ); } if ( result ) { // succeeded OSReport("------ Test Succeeded ------\n"); GXSetCopyClear(SucceededGreen, GX_MAX_Z24); GXCopyDisp(DEMOGetCurrentBuffer(), GX_TRUE); } else { // failed OSReport("****** Test Failed ******\n"); GXSetCopyClear(FailedRed, GX_MAX_Z24); GXCopyDisp(DEMOGetCurrentBuffer(), GX_TRUE); } } while ( TRUE ) { DEMOBeforeRender(); DoReport(); DEMODoneRender(); } } /*---------------------------------------------------------------------------* Display Settings *---------------------------------------------------------------------------*/ static void DoReport(void) { GXRenderModeObj* rmp; rmp = DEMOGetRenderModeObj(); DEMOInitCaption(DM_FT_XLU, (s16) rmp->fbWidth, (s16) rmp->efbHeight); DEMOSetROMFontSize(16, -1); (void)DEMORFPuts(25, 25, 0, ReportBuffer); } // Define program-specific OSReport() function. void OSReport(const char* msg, ...) { BOOL enabled; va_list marker; u32 len; int c; char* p; char* q; va_start(marker, msg); (void)vsnprintf(PrintBuffer, sizeof(PrintBuffer)-1, msg, marker); va_end(marker); PrintBuffer[sizeof(PrintBuffer)-1] = 0; (void)printf("%s", PrintBuffer); enabled = OSDisableInterrupts(); (void)strncat(ReportBuffer, PrintBuffer, sizeof(ReportBuffer)-1); ReportBuffer[sizeof(ReportBuffer)-1] = 0; len = strlen(ReportBuffer); c = 0; q = ReportBuffer; for (p = ReportBuffer; p < ReportBuffer + len; ++p) { if (*p == '\n') { ++c; if (LINES <= c) { q = strchr(q, '\n'); } } } if (LINES <= c) { (void)memmove(ReportBuffer, q + 1, len - (q - ReportBuffer)); } (void)OSRestoreInterrupts(enabled); } /*---------------------------------------------------------------------------* Tests *---------------------------------------------------------------------------*/ #define PrintResultEq( a, b, f ) \ do { OSReport( ((a) == (b)) ? "[--OK--] " : "[**NG**] " ); (f) = (f) && ((a) == (b)); } while( FALSE ) #define PrintResultDigestEq( a, b, l, f ) \ do { OSReport( (memcmp((a), (b), (l)) == 0) ? "[--OK--] " : "[**NG**] " ); (f) = (f) && (memcmp((a), (b), (l)) == 0); } while( FALSE ) static void* DoTest(void* arg) { BOOL flag = TRUE; int i; (void)arg; // Test CRC-16 { char *a[] = { "123456789", }; u16 result_crc16[] = { 0xbb3d, }; for (i = 0; i < sizeof(a) / sizeof(char *); i++) { u16 result; result = OSCalcCRC16(a[i], strlen(a[i])); PrintResultEq(result, result_crc16[i], flag); OSReport("OSCalcCRC16(%s) = %04x\n", a[i], result); } } // Test CRC-32 { char *a[] = { "123456789", }; u32 result_crc32[] = { 0xcbf43926, }; for (i = 0; i < sizeof(a) / sizeof(char *); i++) { u32 result; result = OSCalcCRC32(a[i], strlen(a[i])); PrintResultEq(result, result_crc32[i], flag); OSReport("OSCalcCRC32(%s) = %04x\n", a[i], result); } } return (void*)flag; }