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-06-04#$
14   $Rev: 10698 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 #include <twl.h>
19 #include <twl/el.h>
20 
21 
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     {
103         void   *tempLo;
104         OSHeapHandle hh;
105 
106         // Based on the premise that OS_Init has been already called
107         tempLo = OS_InitAlloc(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi(), 1);
108         OS_SetArenaLo(OS_ARENA_MAIN, tempLo);
109         hh = OS_CreateHeap(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi());
110         if (hh < 0)
111         {
112             OS_Panic("ARM9: Fail to create heap...\n");
113         }
114         hh = OS_SetCurrentHeap(OS_ARENA_MAIN, hh);
115     }
116 
117 
118     OS_TPrintf("\n");
119     OS_TPrintf("===================================\n");
120     OS_TPrintf("EL Library test\n");
121     OS_TPrintf("===================================\n");
122 
123     /* Start testing the EL library*/
124     {
125         FS_Init( FS_DMA_NOT_USE );
126 
127         if( EL_Init( MY_Alloc, MY_Free) < 0)
128         {
129             OS_TPanic( "EL_Init failed.\n");
130         }
131         else
132         {
133             OS_TPrintf( "EL_Init success.\n");
134         }
135 
136         // Dynamic modules are in the ROM
137         lib_bufA = MY_Alloc(8192);
138         if(lib_bufA == 0)
139             OS_Panic("failed alloc\n");
140         my_dlldA = EL_LinkFileEx( "rom:/data/dllA.a", lib_bufA, 8192);
141         if(my_dlldA == 0)
142             OS_Panic("failed EL_LinkFileEx(dllA.a)\n");
143 
144         OS_TPrintf( "dll loaded 0x%x - 0x%x\n", (u32)lib_bufA, (u32)lib_bufA + EL_GetLibSize( my_dlldA));
145 
146         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlldA));
147 
148         // Next, link dllB
149 #ifndef SDK_FINALROM // Because this demo uses the main memory expanded arena, it will not work in FINALROM mode
150         lib_bufB = (u32*)OS_AllocFromMainExArenaLo(8192, 32);
151 #else
152         lib_bufB = MY_Alloc(8192);
153 #endif // SDK_FINALROM
154         my_dlldB = EL_LinkFileEx( "rom:/data/dllB.a", lib_bufB, 8192);
155         if(my_dlldB == 0)
156             OS_Panic("failed EL_LinkFileEx(dllB.a)\n");
157 
158         OS_TPrintf( "dll loaded 0x%x - 0x%x\n", (u32)lib_bufB, (u32)lib_bufB + EL_GetLibSize( my_dlldB));
159 
160         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlldB));
161 
162         /* Export the static-side symbols*/
163         EL_AddStaticSym();
164 
165         /* Resolve the DLL symbols*/
166         (void)EL_ResolveAll();
167 
168         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlldA));
169         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlldB));
170         if(!EL_IsResolved(my_dlldA))
171             OS_Panic("failed EL_IsResolved(my_dlldA)\n");
172         if(!EL_IsResolved(my_dlldB))
173             OS_Panic("failed EL_IsResolved(my_dlldB)\n");
174 
175         OS_TPrintf( "LINK : dynamic\n");
176         global_funcA = (global_func_p)EL_GetGlobalAdr( my_dlldA, "global_func_A\0");
177         OS_TPrintf( "global_func_A : 0x%x\n", global_funcA);
178         global_funcB = (global_func_p)EL_GetGlobalAdr( my_dlldB, "global_func_B\0");
179         OS_TPrintf( "global_func_B : 0x%x\n", global_funcB);
180         if(global_funcA == 0)
181             OS_Panic("failed EL_GetGlobalAdr(my_dlldA)\n");
182         if(global_funcB == 0)
183             OS_Panic("failed EL_GetGlobalAdr(my_dlldB)\n");
184 
185         OS_TPrintf( "----- global_func_A execution -----\n");
186         (*global_funcA)();
187         OS_TPrintf( "----- global_func_B execution -----\n");
188         (*global_funcB)();
189 
190         OS_TPrintf( "----- dll execution end -----\n");
191 
192         /* Unlink*/
193         OS_TPrintf("EL_Unlink ... dllA\n");
194         (void)EL_Unlink( my_dlldA);
195         OS_TPrintf( "EL_Unlink success.\n");
196 
197         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlldA));
198         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlldB));
199 
200     }
201 
202     OS_TPrintf("\n");
203     OS_TPrintf("===================================\n");
204     OS_TPrintf("Finish\n");
205     OS_TPrintf("===================================\n");
206     OS_Terminate();
207 }
208 
209 /*====== End of main.c ======*/
210