1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - EL - demos - simple-2
3   File:     main.c
4 
5   Copyright 2007-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-12-08#$
14   $Rev: 9570 $
15   $Author: kitase_hirotake $
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_LinkFile( "rom:/data/dllA.a", lib_bufA);
141 
142         OS_TPrintf( "dll loaded 0x%x - 0x%x\n", (u32)lib_bufA, (u32)lib_bufA + EL_GetLibSize( my_dlldA));
143 
144         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlldA));
145 
146         // Next, link dllB
147 #ifndef SDK_FINALROM // Because this demo uses the main memory expanded arena, it will not work in FINALROM mode
148         lib_bufB = (u32*)OS_AllocFromMainExArenaLo(8192, 32);
149 #else
150         lib_bufB = MY_Alloc(8192);
151 #endif // SDK_FINALROM
152         my_dlldB = EL_LinkFile( "rom:/data/dllB.a", lib_bufB);
153 
154         OS_TPrintf( "dll loaded 0x%x - 0x%x\n", (u32)lib_bufB, (u32)lib_bufB + EL_GetLibSize( my_dlldB));
155 
156         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlldB));
157 
158         /* Export the static-side symbols*/
159         EL_AddStaticSym();
160 
161         /* Resolve the DLL symbols*/
162         (void)EL_ResolveAll();
163 
164         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlldA));
165         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlldB));
166 
167         OS_TPrintf( "LINK : dynamic\n");
168         global_funcA = (global_func_p)EL_GetGlobalAdr( my_dlldA, "global_func_A\0");
169         OS_TPrintf( "global_func_A : 0x%x\n", global_funcA);
170         global_funcB = (global_func_p)EL_GetGlobalAdr( my_dlldB, "global_func_B\0");
171         OS_TPrintf( "global_func_B : 0x%x\n", global_funcB);
172 
173         OS_TPrintf( "----- global_func_A execution -----\n");
174         (*global_funcA)();
175         OS_TPrintf( "----- global_func_B execution -----\n");
176         (*global_funcB)();
177 
178         OS_TPrintf( "----- dll execution end -----\n");
179 
180         /* Unlink*/
181         OS_TPrintf("EL_Unlink ... dllA\n");
182         (void)EL_Unlink( my_dlldA);
183         OS_TPrintf( "EL_Unlink success.\n");
184 
185         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlldA));
186         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlldB));
187 
188     }
189 
190     OS_TPrintf("\n");
191     OS_TPrintf("===================================\n");
192     OS_TPrintf("Finish\n");
193     OS_TPrintf("===================================\n");
194     OS_Terminate();
195 }
196 
197 /*====== End of main.c ======*/
198