1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - EL - demos - simple-2
3   File:     main.c
4 
5   Copyright 2007-2009 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:: 2009-09-24#$
14   $Rev: 11063 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 #include <twl.h>
19 #include <twl/el.h>
20 
21 #include "DEMO.h"
22 #include "dllA.h"
23 #include "dllB.h"
24 
25 /*---------------------------------------------------------------------------*
26     static variables
27  *---------------------------------------------------------------------------*/
28 static u32* lib_bufA;
29 static u32* lib_bufB;
30 
31 typedef void (*global_func_p)( void);
32 global_func_p   global_funcA;
33 global_func_p   global_funcB;
34 
35 /*---------------------------------------------------------------------------*
36   Name:         MY_Alloc
37 
38   Description:  The user's memory allocation function passed to the EL library.
39 
40   Arguments:    None.
41 
42   Returns:      None.
43  *---------------------------------------------------------------------------*/
MY_Alloc(size_t size)44 static void *MY_Alloc(size_t size)
45 {
46     void* heap;
47     heap = OS_Alloc( size);
48     if( heap == NULL) { OS_TPanic( "OS_Alloc failed.\n");}
49     return( heap);
50 }
51 
52 /*---------------------------------------------------------------------------*
53   Name:         MY_Free
54 
55   Description:  The user's memory deallocation function passed to the EL library.
56 
57   Arguments:    None.
58 
59   Returns:      None.
60  *---------------------------------------------------------------------------*/
MY_Free(void * ptr)61 static void MY_Free(void *ptr)
62 {
63     OS_Free( ptr);
64 }
65 
66 /*---------------------------------------------------------------------------*
67   Name:         VBlankIntr
68 
69   Description:  V-Blank interrupt handler
70 
71   Arguments:    None.
72 
73   Returns:      None.
74  *---------------------------------------------------------------------------*/
VBlankIntr(void)75 static void VBlankIntr(void)
76 {
77     //---- check interrupt flag
78     OS_SetIrqCheckFlag(OS_IE_V_BLANK);
79 }
80 
81 /*---------------------------------------------------------------------------*
82   Name:         TwlMain
83 
84   Description:  Main
85 
86   Arguments:    None.
87 
88   Returns:      None.
89  *---------------------------------------------------------------------------*/
TwlMain(void)90 void TwlMain(void)
91 {
92     ELDlld my_dlldA, my_dlldB;
93 
94     /* OS initialization */
95     OS_EnableMainExArena();
96     OS_Init();
97     OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr);
98     (void)OS_EnableIrqMask(OS_IE_V_BLANK);
99     (void)OS_EnableIrq();
100     (void)GX_VBlankIntr(TRUE);
101 
102     // When in NITRO mode, stopped by Panic
103     DEMOCheckRunOnTWL();
104 
105     {
106         void   *tempLo;
107         OSHeapHandle hh;
108 
109         // Based on the premise that OS_Init has been already called
110         tempLo = OS_InitAlloc(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi(), 1);
111         OS_SetArenaLo(OS_ARENA_MAIN, tempLo);
112         hh = OS_CreateHeap(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi());
113         if (hh < 0)
114         {
115             OS_Panic("ARM9: Fail to create heap...\n");
116         }
117         hh = OS_SetCurrentHeap(OS_ARENA_MAIN, hh);
118     }
119 
120 
121     OS_TPrintf("\n");
122     OS_TPrintf("===================================\n");
123     OS_TPrintf("EL Library test\n");
124     OS_TPrintf("===================================\n");
125 
126     /* Start testing the EL library */
127     {
128         FS_Init( FS_DMA_NOT_USE );
129 
130         if( EL_Init( MY_Alloc, MY_Free) < 0)
131         {
132             OS_TPanic( "EL_Init failed.\n");
133         }
134         else
135         {
136             OS_TPrintf( "EL_Init success.\n");
137         }
138 
139         // Dynamic modules are in the ROM
140         lib_bufA = MY_Alloc(8192);
141         if(lib_bufA == 0)
142             OS_Panic("failed alloc\n");
143         my_dlldA = EL_LinkFileEx( "rom:/data/dllA.a", lib_bufA, 8192);
144         if(my_dlldA == 0)
145             OS_Panic("failed EL_LinkFileEx(dllA.a)\n");
146 
147         OS_TPrintf( "dll loaded 0x%x - 0x%x\n", (u32)lib_bufA, (u32)lib_bufA + EL_GetLibSize( my_dlldA));
148 
149         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlldA));
150 
151         // Next, link dllB
152 #ifndef SDK_FINALROM // Because this demo uses the main memory expanded arena, it will not work in FINALROM mode
153         lib_bufB = (u32*)OS_AllocFromMainExArenaLo(8192, 32);
154 #else
155         lib_bufB = MY_Alloc(8192);
156 #endif // SDK_FINALROM
157         my_dlldB = EL_LinkFileEx( "rom:/data/dllB.a", lib_bufB, 8192);
158         if(my_dlldB == 0)
159             OS_Panic("failed EL_LinkFileEx(dllB.a)\n");
160 
161         OS_TPrintf( "dll loaded 0x%x - 0x%x\n", (u32)lib_bufB, (u32)lib_bufB + EL_GetLibSize( my_dlldB));
162 
163         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlldB));
164 
165         /* Export the static-side symbols */
166         EL_AddStaticSym();
167 
168         /* Resolve the DLL symbols */
169         (void)EL_ResolveAll();
170 
171         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlldA));
172         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlldB));
173         if(!EL_IsResolved(my_dlldA))
174             OS_Panic("failed EL_IsResolved(my_dlldA)\n");
175         if(!EL_IsResolved(my_dlldB))
176             OS_Panic("failed EL_IsResolved(my_dlldB)\n");
177 
178         OS_TPrintf( "LINK : dynamic\n");
179         global_funcA = (global_func_p)EL_GetGlobalAdr( my_dlldA, "global_func_A\0");
180         OS_TPrintf( "global_func_A : 0x%x\n", global_funcA);
181         global_funcB = (global_func_p)EL_GetGlobalAdr( my_dlldB, "global_func_B\0");
182         OS_TPrintf( "global_func_B : 0x%x\n", global_funcB);
183         if(global_funcA == 0)
184             OS_Panic("failed EL_GetGlobalAdr(my_dlldA)\n");
185         if(global_funcB == 0)
186             OS_Panic("failed EL_GetGlobalAdr(my_dlldB)\n");
187 
188         OS_TPrintf( "----- global_func_A execution -----\n");
189         (*global_funcA)();
190         OS_TPrintf( "----- global_func_B execution -----\n");
191         (*global_funcB)();
192 
193         OS_TPrintf( "----- dll execution end -----\n");
194 
195         /* Unlink */
196         OS_TPrintf("EL_Unlink ... dllA\n");
197         (void)EL_Unlink( my_dlldA);
198         OS_TPrintf( "EL_Unlink success.\n");
199 
200         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlldA));
201         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlldB));
202 
203     }
204 
205     OS_TPrintf("\n");
206     OS_TPrintf("===================================\n");
207     OS_TPrintf("Finish\n");
208     OS_TPrintf("===================================\n");
209     OS_Terminate();
210 }
211 
212 /*====== End of main.c ======*/
213