1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - screenshot test - EXT
3   File:     ext_scrntest.c
4 
5   Copyright 2003-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 #include <nitro.h>
18 
19 //------------------------------------------------------------------------------
20 // These constants will be replaeced, when regular accessor functions are released.
21 typedef u16 CapturedPixel;
22 
23 #define MASK_CAPTURED_PIX_R         0x001f      //  0000 0000 0001 1111
24 #define SHIFT_CAPTURED_PIX_R        0
25 
26 #define MASK_CAPTURED_PIX_G         0x03e0      //  0000 0011 1110 0000
27 #define SHIFT_CAPTURED_PIX_G        5
28 
29 #define MASK_CAPTURED_PIX_B         0x7C00      //  0111 1100 0000 0000
30 #define SHIFT_CAPTURED_PIX_B        10
31 
32 #define MASK_CAPTURED_PIX_ALPHA     0x8000      //  1000 0000 0000 0000
33 #define SHIFT_CAPTURED_PIX_ALPHA    15
34 
35 
36 
37 
38 
39 const static u16 EXT_SCRN_W_ = 256;
40 const static u16 EXT_SCRN_H_ = 192;
41 const static u32 COUNTER_MAX_ = 0xFFFFFFFE;     // = 0xFFFFFFFF - 0x1
42 
43 //------------------------------------------------------------------------------
44 // private func.
45 static u32 calcScreenShotCheckSum_();
46 
47 //------------------------------------------------------------------------------
48 static void startCapture_();
49 static CapturedPixel *getCapturedPixel_(u16 x, u16 y);
50 static CapturedPixel *getCapturedBufferBase_();
51 static u8 getCapturedPixR_(const CapturedPixel * pPix);
52 static u8 getCapturedPixG_(const CapturedPixel * pPix);
53 static u8 getCapturedPixB_(const CapturedPixel * pPix);
54 static BOOL getCapturedPixAlpha_(const CapturedPixel * pPix);
55 
56 static GXDispMode getDispMode_();
57 static GXCaptureMode getCaptureMode_(GXDispMode mode);
58 static GXCaptureSrcB getCaptureSrcB_(GXDispMode mode);
59 static GXCaptureDest getCaptureDest_();
60 
61 
62 
63 
64 //------------------------------------------------------------------------------
65 // private variable
66 static u32 frameCounter = 0x0;
67 static GXVRamLCDC vramForCapture_ = GX_VRAM_LCDC_C;     // default VRAM for cap. is VRAM_C
68 
69 //------------------------------------------------------------------------------
70 
71 /*---------------------------------------------------------------------------*
72   Name:         EXT_Printf
73 
74   Description:  print formatted strings for auto test.
75 
76   Arguments:    fmt : format string
77                 ...   and parameters
78 
79   Returns:      None.
80  *---------------------------------------------------------------------------*/
EXT_Printf(const char * fmt,...)81 void EXT_Printf(const char *fmt, ...)
82 {
83     va_list vlist;
84 
85     OS_Printf("<AUTO-TEST> ");
86     va_start(vlist, fmt);
87     OS_VPrintf(fmt, vlist);
88     va_end(vlist);
89 }
90 
91 
92 /*---------------------------------------------------------------------------*
93   Name:         EXT_CompPrint
94 
95   Description:  compare string to print formatted strings for auto test.
96 
97   Arguments:    src1 : compared src1
98                 fmt  : format string of compared src2
99                 ...    and parameters
100 
101   Returns:      None.
102  *---------------------------------------------------------------------------*/
EXT_CompPrint(const char * src1,const char * src2_fmt,...)103 void EXT_CompPrint(const char *src1, const char *src2_fmt, ...)
104 {
105     va_list vlist;
106     char    src2[256];
107     s32     i;
108 
109     va_start(vlist, src2_fmt);
110     (void)OS_VSPrintf(src2, src2_fmt, vlist);
111     va_end(vlist);
112 
113     for (i = 0;; i++)
114     {
115         if (src1[i] != src2[i])
116         {
117             EXT_Printf("PrintString = \"%s\" : \"%s\"\n", src1, src2);
118             EXT_Printf("PrintCompare Test [Fail]\n");
119             return;
120         }
121 
122         if (src1[i] == '\0' && src2[i] == '\0')
123         {
124             break;
125         }
126     }
127 
128     EXT_Printf("PrintCompare Test [Success]\n");
129 }
130 
131 
132 /*---------------------------------------------------------------------------*
133   Name:         EXT_TestScreenShot
134 
135   Description:  capturing the screen shot and calculate the check sum of it
136                 at specified frame count.
137 
138                 If the result is different from that you expected,
139                 out error message to debug console.
140 
141   Arguments:    testFrame    frame count when the screen shot will be captured.
142                 checkSum     expected check sum value of captured screen shot.
143 
144   Returns:      none
145  *---------------------------------------------------------------------------*/
EXT_TestScreenShot(u32 testFrame,u32 checkSum)146 void EXT_TestScreenShot(u32 testFrame, u32 checkSum)
147 {
148     const u32 prevTestFrame = testFrame - 1;
149     const u32 nextTestFrame = testFrame + 1;
150 
151     SDK_ASSERTMSG((testFrame > 0)
152                   && (testFrame < (u32)COUNTER_MAX_),
153                   "illegal input value for numFrames in EXT_TestScreenShot");
154 
155 
156 
157     // Request for starting capture on the previous frame of checking.
158     if (frameCounter == prevTestFrame)
159     {
160         startCapture_();
161     }
162 
163     // Calculate the checkSum of the frame on the next frame of capturing.
164     // We have to wait one frame for the guarantee of successful capturing.
165     {
166         if (frameCounter == nextTestFrame)
167         {
168             u32     currentSum = 0x0;
169 
170             EXT_Printf("ScreenShot Test at frameCounter = %d\n", testFrame);
171 
172             currentSum = calcScreenShotCheckSum_();
173 
174             EXT_Printf("CheckSum = %X\n", currentSum);
175             if (checkSum == currentSum)
176             {
177                 EXT_Printf("ScreenShot Test [Success]\n");
178             }
179             else
180             {
181                 EXT_Printf("ScreenShot Test [Fail]\n");
182             }
183 
184             //
185             // Currently, I don't terminate the app here.
186             //
187         }
188     }
189 
190 }
191 
192 /*---------------------------------------------------------------------------*
193   Name:         EXT_TestTickCounter
194 
195   Description:  advance the local frame counter for this library.
196 
197   Arguments:    none
198 
199   Returns:      none
200  *---------------------------------------------------------------------------*/
EXT_TestTickCounter()201 void EXT_TestTickCounter()
202 {
203     frameCounter++;
204 }
205 
206 /*---------------------------------------------------------------------------*
207   Name:         EXT_TestResetCounter
208 
209   Description:  reset the local frame counter for this library.
210 
211   Arguments:    none
212 
213   Returns:      none
214  *---------------------------------------------------------------------------*/
EXT_TestResetCounter()215 void EXT_TestResetCounter()
216 {
217     frameCounter = 0;
218 }
219 
220 /*---------------------------------------------------------------------------*
221   Name:         EXT_TestSetVRAMForScreenShot
222 
223   Description:  Set the VRAM for screen shot capturing.
224 
225   Arguments:    vram    a VRAM used for screen shot capturing.
226 
227   Returns:      none
228  *---------------------------------------------------------------------------*/
EXT_TestSetVRAMForScreenShot(GXVRamLCDC vram)229 void EXT_TestSetVRAMForScreenShot(GXVRamLCDC vram)
230 {
231     // TODO: If I could know more about vram usage, I could check the param more strictly.
232     SDK_ASSERTMSG(vram == GX_VRAM_LCDC_A || vram == GX_VRAM_LCDC_B || vram == GX_VRAM_LCDC_C
233                   || vram == GX_VRAM_LCDC_D, "Currentry VRAM_A B C D are supported for capturing.");
234 
235     vramForCapture_ = vram;
236 }
237 
238 /*---------------------------------------------------------------------------*
239   Name:         startCapture_
240 
241   Description:  start screen capturing( and out messages to debug console).
242 
243 
244   Arguments:    none
245 
246   Returns:      none
247  *---------------------------------------------------------------------------*/
startCapture_()248 static void startCapture_()
249 {
250     const GXDispMode dispMode = getDispMode_();
251 
252     GX_SetCapture(GX_CAPTURE_SIZE_256x192,
253                   getCaptureMode_(dispMode), GX_CAPTURE_SRCA_2D3D, getCaptureSrcB_(dispMode),
254                   getCaptureDest_(), 16, 0);
255     // Out Msg
256     {
257         char    vram = '*';
258         switch (vramForCapture_)
259         {
260         case GX_VRAM_LCDC_A:
261             vram = 'A';
262             break;
263         case GX_VRAM_LCDC_B:
264             vram = 'B';
265             break;
266         case GX_VRAM_LCDC_C:
267             vram = 'C';
268             break;
269         case GX_VRAM_LCDC_D:
270             vram = 'D';
271             break;
272         default:
273             SDK_INTERNAL_ERROR("UnExpected VRAM type in startCapture_()");
274             break;
275         }
276         EXT_Printf("Capture to VRAM %c for ScreenShot Test \n", vram);
277     }
278 }
279 
280 /*---------------------------------------------------------------------------*
281   Name:         getDispMode_
282 
283   Description:  get the current display mode from the DISPCNT register.
284 
285 
286   Arguments:    none
287 
288   Returns:      current display mode
289                 ( see GXDispMode for more detail )
290  *---------------------------------------------------------------------------*/
getDispMode_()291 static GXDispMode getDispMode_()
292 {
293     const GXDispMode ret =
294         (GXDispMode)((reg_GX_DISPCNT & REG_GX_DISPCNT_MODE_MASK) >> REG_GX_DISPCNT_MODE_SHIFT);
295     GX_DISPMODE_ASSERT(ret);
296 
297     return ret;
298 }
299 
300 
301 /*---------------------------------------------------------------------------*
302   Name:         getCaptureMode_
303 
304   Description:  get the type of current capture mode.
305 
306   Arguments:    mode        current display mode
307 
308   Returns:      type of current capture mode
309                 ( see GXCaptureMode for more detail )
310  *---------------------------------------------------------------------------*/
getCaptureMode_(GXDispMode mode)311 static GXCaptureMode getCaptureMode_(GXDispMode mode)
312 {
313     if (mode == GX_DISPMODE_GRAPHICS)
314     {
315         return GX_CAPTURE_MODE_A;
316     }
317     else
318     {
319         return GX_CAPTURE_MODE_B;
320     }
321 }
322 
323 /*---------------------------------------------------------------------------*
324   Name:         getCaptureSrcB_
325 
326   Description:  get the type of current capture mode.
327 
328 
329   Arguments:    mode        current display mode
330 
331   Returns:      type of current capture mode.
332 
333  *---------------------------------------------------------------------------*/
getCaptureSrcB_(GXDispMode mode)334 static GXCaptureSrcB getCaptureSrcB_(GXDispMode mode)
335 {
336     if (mode == GX_DISPMODE_GRAPHICS)
337     {
338         // return meanless value, because srcB isn't used.
339         return (GXCaptureSrcB)0;
340     }
341     else
342     {
343         if (mode == GX_DISPMODE_MMEM)
344         {
345             // capture from main mem
346             return GX_CAPTURE_SRCB_MRAM;
347         }
348         else
349         {
350             // capture from VRAM
351             return GX_CAPTURE_SRCB_VRAM_0x00000;
352         }
353     }
354 }
355 
356 
357 /*---------------------------------------------------------------------------*
358   Name:         getCaptureDest_
359 
360   Description:  get the captured data's destination.
361 
362   Arguments:    none
363 
364   Returns:      type of destination.
365 
366  *---------------------------------------------------------------------------*/
getCaptureDest_()367 static GXCaptureDest getCaptureDest_()
368 {
369     switch (vramForCapture_)
370     {
371     case GX_VRAM_LCDC_A:
372         return GX_CAPTURE_DEST_VRAM_A_0x00000;
373     case GX_VRAM_LCDC_B:
374         return GX_CAPTURE_DEST_VRAM_B_0x00000;
375     case GX_VRAM_LCDC_C:
376         return GX_CAPTURE_DEST_VRAM_C_0x00000;
377     case GX_VRAM_LCDC_D:
378         return GX_CAPTURE_DEST_VRAM_D_0x00000;
379 
380     default:
381         SDK_INTERNAL_ERROR("UnExpected VRAM type in getCaptureDest_()");
382         return GX_CAPTURE_DEST_VRAM_C_0x00000;  // Dummy
383     }
384 }
385 
386 
387 
388 
389 /*---------------------------------------------------------------------------*
390   Name:         calcScreenShotCheckSum_
391 
392   Description:  caluclate the check sum value of captured screen shot.
393 
394   Arguments:    none
395 
396   Returns:      check sum value of captured screen shot.
397 
398  *---------------------------------------------------------------------------*/
399 //
400 // NOTICE ME! Screen size is hardcoded. (These should be removed...)
401 //
calcScreenShotCheckSum_()402 static u32 calcScreenShotCheckSum_()
403 {
404     u16     i, j;
405     u32     sum = 0x0;
406     const CapturedPixel *pPx = NULL;
407     for (j = 0; j < EXT_SCRN_H_; j++)
408     {
409         for (i = 0; i < EXT_SCRN_W_; i++)
410         {
411             pPx = getCapturedPixel_(i, j);
412             SDK_NULL_ASSERT(pPx);
413             sum += (u32)(*pPx) * (i + j);
414         }
415     }
416     return sum;
417 }
418 
419 
420 
421 /*---------------------------------------------------------------------------*
422   Name:         getCapturedPixel_
423 
424   Description:  accessor for Captured scrrenshot on VRAM.
425 
426   Arguments:    none
427 
428   Returns:      Captured pixel data on VRAM.
429 
430  *---------------------------------------------------------------------------*/
getCapturedPixel_(u16 x,u16 y)431 static CapturedPixel *getCapturedPixel_(u16 x, u16 y)
432 {
433     SDK_MINMAX_ASSERT(x, 0, EXT_SCRN_W_);
434     SDK_MINMAX_ASSERT(y, 0, EXT_SCRN_H_);
435 
436     return (CapturedPixel *) (getCapturedBufferBase_()) + ((EXT_SCRN_W_ * y) + x);
437 }
438 
439 /*---------------------------------------------------------------------------*
440   Name:         getCapturedBufferBase_
441 
442   Description:  Get the captured data's base adderess.
443 
444   Arguments:    none
445 
446   Returns:      captured data's base adderess.
447 
448  *---------------------------------------------------------------------------*/
getCapturedBufferBase_()449 static CapturedPixel *getCapturedBufferBase_()
450 {
451     switch (vramForCapture_)
452     {
453     case GX_VRAM_LCDC_A:
454         return (CapturedPixel *) HW_LCDC_VRAM_A;
455     case GX_VRAM_LCDC_B:
456         return (CapturedPixel *) HW_LCDC_VRAM_B;
457     case GX_VRAM_LCDC_C:
458         return (CapturedPixel *) HW_LCDC_VRAM_C;
459     case GX_VRAM_LCDC_D:
460         return (CapturedPixel *) HW_LCDC_VRAM_D;
461 
462     default:
463         SDK_INTERNAL_ERROR("UnExpected VRAM type in getCapturedBufferBase_()");
464         return (CapturedPixel *) NULL; // Dummy
465     }
466 }
467 
468 //------------------------------------------------------------------------------
469 // accessors
470 //
getCapturedPixR_(const CapturedPixel * pPix)471 static u8 getCapturedPixR_(const CapturedPixel * pPix)
472 {
473     SDK_NULL_ASSERT(pPix);
474     return (u8)((MASK_CAPTURED_PIX_R & *pPix) >> SHIFT_CAPTURED_PIX_R);
475 }
476 
getCapturedPixG_(const CapturedPixel * pPix)477 static u8 getCapturedPixG_(const CapturedPixel * pPix)
478 {
479     SDK_NULL_ASSERT(pPix);
480     return (u8)((MASK_CAPTURED_PIX_G & *pPix) >> SHIFT_CAPTURED_PIX_G);
481 }
482 
getCapturedPixB_(const CapturedPixel * pPix)483 static u8 getCapturedPixB_(const CapturedPixel * pPix)
484 {
485     SDK_NULL_ASSERT(pPix);
486     return (u8)((MASK_CAPTURED_PIX_B & *pPix) >> SHIFT_CAPTURED_PIX_B);
487 }
488 
getCapturedPixAlpha_(const CapturedPixel * pPix)489 static BOOL getCapturedPixAlpha_(const CapturedPixel * pPix)
490 {
491     SDK_NULL_ASSERT(pPix);
492     return (BOOL)((MASK_CAPTURED_PIX_R & *pPix) >> SHIFT_CAPTURED_PIX_ALPHA);
493 }
494