1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - RTC - demos - convert-1
3   File:     main.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 /*---------------------------------------------------------------------------*
18   Demo to confirm operation of RTC conversion function
19  *---------------------------------------------------------------------------*/
20 
21 #ifdef SDK_TWL
22 #include <twl.h>
23 #else
24 #include <nitro.h>
25 #endif
26 #include    <string.h>
27 
28 
29 static void VBlankIntr(void);
30 static void DisplayInit(void);
31 static void FillScreen(u16 col);
32 static BOOL ConvTest(void);
33 static void FormatDateTime(char *str, const RTCDate *date, const RTCTime *time);
34 
35 /*---------------------------------------------------------------------------*
36     Variable Definitions
37  *---------------------------------------------------------------------------*/
38 
39 /*---------------------------------------------------------------------------*
40     Function Definitions
41  *---------------------------------------------------------------------------*/
42 
43 /*---------------------------------------------------------------------------*
44   Name:         NitroMain
45 
46   Description:  Initialization and main loop.
47 
48   Arguments:    None.
49 
50   Returns:      None.
51  *---------------------------------------------------------------------------*/
NitroMain(void)52 void NitroMain(void)
53 {
54     // Various types of initialization
55     OS_Init();
56 
57     DisplayInit();
58 
59     if (ConvTest())
60     {
61         // Success
62         OS_TPrintf("------ Test Succeeded ------\n");
63         FillScreen(GX_RGB(0, 31, 0));
64     }
65     else
66     {
67         // Failed
68         OS_TPrintf("****** Test Failed ******\n");
69         FillScreen(GX_RGB(31, 0, 0));
70     }
71 
72     // Main loop
73     while (TRUE)
74     {
75         // Waiting for the V-Blank
76         OS_WaitVBlankIntr();
77     }
78 }
79 
80 /*---------------------------------------------------------------------------*
81   Name:         VBlankIntr
82 
83   Description:  V-Blank interrupt vector.
84 
85   Arguments:    None.
86 
87   Returns:      None.
88  *---------------------------------------------------------------------------*/
VBlankIntr(void)89 static void VBlankIntr(void)
90 {
91     // Sets the IRQ check flag
92     OS_SetIrqCheckFlag(OS_IE_V_BLANK);
93 }
94 
95 /*---------------------------------------------------------------------------*
96   Name:         DisplayInit
97 
98   Description:  Graphics initialization.
99 
100   Arguments:    None.
101 
102   Returns:      None.
103  *---------------------------------------------------------------------------*/
DisplayInit(void)104 static void DisplayInit(void)
105 {
106 
107     GX_Init();
108     FX_Init();
109 
110     GX_DispOff();
111     GXS_DispOff();
112 
113     GX_SetDispSelect(GX_DISP_SELECT_SUB_MAIN);
114 
115     OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr);
116     (void)OS_EnableIrqMask(OS_IE_V_BLANK);
117     (void)GX_VBlankIntr(TRUE);         // To generate V-Blank interrupt request
118     (void)OS_EnableIrq();
119 
120 
121     GX_SetBankForLCDC(GX_VRAM_LCDC_ALL);
122     MI_CpuClearFast((void *)HW_LCDC_VRAM, HW_LCDC_VRAM_SIZE);
123 
124     MI_CpuFillFast((void *)HW_OAM, 192, HW_OAM_SIZE);   // Clear OAM
125     MI_CpuClearFast((void *)HW_PLTT, HW_PLTT_SIZE);     // Clear the standard palette
126 
127     MI_CpuFillFast((void *)HW_DB_OAM, 192, HW_DB_OAM_SIZE);     // Clear OAM
128     MI_CpuClearFast((void *)HW_DB_PLTT, HW_DB_PLTT_SIZE);       // Clear the standard palette
129     MI_DmaFill32(3, (void *)HW_LCDC_VRAM_C, 0x7FFF7FFF, 256 * 192 * sizeof(u16));
130 
131 
132     GX_SetBankForOBJ(GX_VRAM_OBJ_256_AB);       // Set VRAM-A,B for OBJ
133 
134     GX_SetGraphicsMode(GX_DISPMODE_VRAM_C,      // VRAM mode
135                        (GXBGMode)0,    // Dummy
136                        (GXBG0As)0);    // Dummy
137 
138     GX_SetVisiblePlane(GX_PLANEMASK_OBJ);       // Make OBJs visible
139     GX_SetOBJVRamModeBmp(GX_OBJVRAMMODE_BMP_1D_128K);   // 2D mapping OBJ
140 
141     OS_WaitVBlankIntr();               // Waiting for the end of the V-Blank interrupt
142     GX_DispOn();
143 
144 }
145 
146 
147 /*---------------------------------------------------------------------------*
148   Name:         FillScreen
149 
150   Description:  Fills the screen.
151 
152   Arguments:    col: FillColor
153 
154   Returns:      None.
155  *---------------------------------------------------------------------------*/
FillScreen(u16 col)156 static void FillScreen(u16 col)
157 {
158     MI_CpuFill16((void *)HW_LCDC_VRAM_C, col, 256 * 192 * 2);
159 }
160 
161 /*---------------------------------------------------------------------------*
162   Name:         ConvTest
163 
164   Description:  RTC conversion function test routine.
165 
166   Arguments:    None.
167 
168   Returns:      TRUE if test succeeds.
169  *---------------------------------------------------------------------------*/
170 static char *sWeekName[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
171 #define PrintResultEq( a, b, f ) \
172     { OS_TPrintf( ((a) == (b)) ? "[--OK--] " : "[**NG**] " ); (f) = (f) && ((a) == (b)); }
173 #define PrintResultStringEq( a, b, f ) \
174     { OS_TPrintf( (strcmp((a), (b)) == 0) ? "[--OK--] " : "[**NG**] " ); (f) = (f) && (strcmp((a), (b)) == 0); }
175 
ConvTest(void)176 static BOOL ConvTest(void)
177 {
178     int     i;
179     BOOL    flag = TRUE;
180 
181     {
182         s64     a[] = {
183             0, 3155759999LL, -1, 3155760000LL,
184             5097600, 5184000, 154335600,
185         };
186         char   *result_str[] = {
187             "2000/01/01(Sat) 00:00:00", "2099/12/31(Thu) 23:59:59",
188             "2000/01/01(Sat) 00:00:00", "2099/12/31(Thu) 23:59:59",
189             "2000/02/29(Tue) 00:00:00", "2000/03/01(Wed) 00:00:00",
190             "2004/11/21(Sun) 07:00:00",
191         };
192         s64     result_sec[] = {
193             0, 3155759999LL, 0, 3155759999LL,
194             5097600, 5184000, 154335600,
195         };
196         for (i = 0; i < sizeof(a) / sizeof(a[0]); i++)
197         {
198             RTCDate date;
199             RTCTime time;
200             char    datestr[64];
201             s64     result;
202 
203             RTC_ConvertSecondToDateTime(&date, &time, a[i]);
204             FormatDateTime(datestr, &date, &time);
205             PrintResultStringEq(datestr, result_str[i], flag);
206             OS_TPrintf("RTC_ConvertSecondToDateTime(%lld) = %s\n", a[i], datestr);
207             result = RTC_ConvertDateTimeToSecond(&date, &time);
208             PrintResultEq(result, result_sec[i], flag);
209             OS_TPrintf("RTC_ConvertDateTimeToSecond(%s) = %lld\n", datestr, result);
210         }
211     }
212 
213 
214     return flag;
215 }
216 
FormatDateTime(char * str,const RTCDate * date,const RTCTime * time)217 static void FormatDateTime(char *str, const RTCDate *date, const RTCTime *time)
218 {
219     (void)OS_SPrintf(str, "%04d/%02d/%02d(%s) %02d:%02d:%02d",
220                      date->year + 2000, date->month, date->day, sWeekName[date->week],
221                      time->hour, time->minute, time->second);
222 }
223 
224 /*---------------------------------------------------------------------------*
225   End of file
226  *---------------------------------------------------------------------------*/
227