1 /*---------------------------------------------------------------------------*
2   Project:  NitroCrypto - build - demos
3   File:     main.c
4 
5   Copyright 2005-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   $Date:: 2008-09-18#$
14   $Rev: 8573 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 /*---------------------------------------------------------------------------*
18   This demo confirms that the RC4 algorithm for the CRYPT library works and measures its speed of operation.
19   Measures speed
20  *---------------------------------------------------------------------------*/
21 
22 #include <nitro.h>
23 #include <nitro/crypto.h>
24 
25 
26 
27 static void VBlankIntr(void);
28 static void DisplayInit(void);
29 static void FillScreen(u16 col);
30 static BOOL CompareRC4Speed(void);
31 
32 /*---------------------------------------------------------------------------*
33     Variable Definitions
34  *---------------------------------------------------------------------------*/
35 
36 /*---------------------------------------------------------------------------*
37     Function Definitions
38  *---------------------------------------------------------------------------*/
39 
40 /*---------------------------------------------------------------------------*
41   Name:         NitroMain
42 
43   Description:  Initialization and main loop.
44 
45   Arguments:    None.
46 
47   Returns:      None.
48  *---------------------------------------------------------------------------*/
NitroMain(void)49 void NitroMain(void)
50 {
51     // Various types of initialization
52     OS_Init();
53     OS_InitTick();
54 
55     DisplayInit();
56 
57     if (CompareRC4Speed())
58 	{
59         // Success
60         OS_TPrintf("------ Test Succeeded ------\n");
61         FillScreen(GX_RGB(0, 31, 0));
62 	}
63 	else
64 	{
65         // Failed
66         OS_TPrintf("****** Test Failed ******\n");
67         FillScreen(GX_RGB(31, 0, 0));
68 	}
69 
70     // Main loop
71     while (TRUE)
72     {
73         // Waiting for the V-Blank
74         OS_WaitVBlankIntr();
75     }
76 }
77 
78 /*---------------------------------------------------------------------------*
79   Name:         VBlankIntr
80 
81   Description:  V-Blank interrupt vector.
82 
83   Arguments:    None.
84 
85   Returns:      None.
86  *---------------------------------------------------------------------------*/
VBlankIntr(void)87 static void VBlankIntr(void)
88 {
89     // Sets the IRQ check flag
90     OS_SetIrqCheckFlag(OS_IE_V_BLANK);
91 }
92 
93 /*---------------------------------------------------------------------------*
94   Name:         DisplayInit
95 
96   Description:  Graphics initialization.
97 
98   Arguments:    None.
99 
100   Returns:      None.
101  *---------------------------------------------------------------------------*/
DisplayInit(void)102 static void DisplayInit(void)
103 {
104 
105     GX_Init();
106     FX_Init();
107 
108     GX_DispOff();
109     GXS_DispOff();
110 
111     GX_SetDispSelect(GX_DISP_SELECT_SUB_MAIN);
112 
113     OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr);
114     (void)OS_EnableIrqMask(OS_IE_V_BLANK);
115     (void)GX_VBlankIntr(TRUE);         // To generate V-Blank interrupt request
116     (void)OS_EnableIrq();
117 
118 
119     GX_SetBankForLCDC(GX_VRAM_LCDC_ALL);
120     MI_CpuClearFast((void *)HW_LCDC_VRAM, HW_LCDC_VRAM_SIZE);
121 
122     MI_CpuFillFast((void *)HW_OAM, 192, HW_OAM_SIZE);   // Clear OAM
123     MI_CpuClearFast((void *)HW_PLTT, HW_PLTT_SIZE);     // Clear the standard palette
124 
125     MI_CpuFillFast((void *)HW_DB_OAM, 192, HW_DB_OAM_SIZE);     // Clear OAM
126     MI_CpuClearFast((void *)HW_DB_PLTT, HW_DB_PLTT_SIZE);       // Clear the standard palette
127     MI_DmaFill32(3, (void *)HW_LCDC_VRAM_C, 0x7FFF7FFF, 256 * 192 * sizeof(u16));
128 
129 
130     GX_SetBankForOBJ(GX_VRAM_OBJ_256_AB);       // Set VRAM-A,B for OBJ
131 
132     GX_SetGraphicsMode(GX_DISPMODE_VRAM_C,      // VRAM mode
133                        (GXBGMode)0,    // Dummy
134                        (GXBG0As)0);    // Dummy
135 
136     GX_SetVisiblePlane(GX_PLANEMASK_OBJ);       // Make OBJs visible
137     GX_SetOBJVRamModeBmp(GX_OBJVRAMMODE_BMP_1D_128K);   // 2D mapping OBJ
138 
139     OS_WaitVBlankIntr();              // Waiting for the end of the V-Blank interrupt
140     GX_DispOn();
141 
142 }
143 
144 
145 /*---------------------------------------------------------------------------*
146   Name:         FillScreen
147 
148   Description:  Fills the screen.
149 
150   Arguments:    col: FillColor.
151 
152   Returns:      None.
153  *---------------------------------------------------------------------------*/
FillScreen(u16 col)154 static void FillScreen(u16 col)
155 {
156     MI_CpuFill16((void *)HW_LCDC_VRAM_C, col, 256 * 192 * 2);
157 }
158 
159 /*---------------------------------------------------------------------------*
160   Name:         CompareRC4Speed
161 
162   Description:  RC4 function test routine.
163 
164   Arguments:    None.
165 
166   Returns:      None.
167  *---------------------------------------------------------------------------*/
168 #define DATA_SIZE (1024*1024)
CompareRC4Speed(void)169 static BOOL CompareRC4Speed(void)
170 {
171     static u8 data[DATA_SIZE] ATTRIBUTE_ALIGN(32);
172     static u8 orig[DATA_SIZE] ATTRIBUTE_ALIGN(32);
173 
174     u8 key[] = {
175         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
176         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
177     };
178 
179     int i;
180     MATHRandContext32 rand;
181     OSTick start, end;
182 
183     OS_TPrintf("Initialize Array...\n");
184     // Initializes data
185     MATH_InitRand32(&rand, 0);
186     for (i = 0; i < DATA_SIZE; i++)
187     {
188         data[i] = orig[i] = (u8)MATH_Rand32(&rand, 0x100);
189     }
190 
191     OS_TPrintf("Start...\n");
192 
193     OS_TPrintf("RC4 Encoding:\t\t");
194     start = OS_GetTick();
195     CRYPTO_RC4(key, sizeof(key), data, sizeof(data));
196     end   = OS_GetTick();
197     OS_Printf("%lld us/MB\n", OS_TicksToMicroSeconds(end - start));
198 
199     OS_TPrintf("RC4 Decoding:\t\t");
200     start = OS_GetTick();
201     CRYPTO_RC4(key, sizeof(key), data, sizeof(data));
202     end   = OS_GetTick();
203     OS_Printf("%lld us/MB\n", OS_TicksToMicroSeconds(end - start));
204 
205     for (i = 0; i < DATA_SIZE; i++)
206     {
207         if ( data[i] != orig[i] )
208         {
209             //OS_Panic("Decoding Failed!");
210 			return FALSE;
211         }
212     }
213 
214     OS_TPrintf("RC4Fast Encoding:\t");
215     start = OS_GetTick();
216     CRYPTO_RC4Fast(key, sizeof(key), data, sizeof(data));
217     end   = OS_GetTick();
218     OS_Printf("%lld us/MB\n", OS_TicksToMicroSeconds(end - start));
219 
220     OS_TPrintf("RC4Fast Decoding:\t");
221     start = OS_GetTick();
222     CRYPTO_RC4Fast(key, sizeof(key), data, sizeof(data));
223     end   = OS_GetTick();
224     OS_Printf("%lld us/MB\n", OS_TicksToMicroSeconds(end - start));
225 
226     for (i = 0; i < DATA_SIZE; i++)
227     {
228         if ( data[i] != orig[i] )
229         {
230             //OS_Panic("Decoding Failed!");
231 			return FALSE;
232         }
233     }
234 
235     OS_TPrintf("Done.\n");
236 
237     return TRUE;
238 }
239 
240 /*---------------------------------------------------------------------------*
241   End of file
242  *---------------------------------------------------------------------------*/
243