1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - EL - demos - simple-1
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 
24 /*---------------------------------------------------------------------------*
25     static variables
26  *---------------------------------------------------------------------------*/
27 static u32 lib_buf[8192];
28 int        fd;
29 
30 typedef void (*global_func_p)( void);
31 global_func_p   global_func;
32 
33 /*---------------------------------------------------------------------------*
34   Name:         MY_Alloc
35 
36   Description:  The user's memory allocation function passed to the EL library.
37 
38   Arguments:    None.
39 
40   Returns:      None.
41  *---------------------------------------------------------------------------*/
MY_Alloc(size_t size)42 static void *MY_Alloc(size_t size)
43 {
44     void* heap;
45     heap = OS_Alloc( size);
46     if( heap == NULL) { OS_TPanic( "OS_Alloc failed.\n");}
47     return( heap);
48 }
49 
50 /*---------------------------------------------------------------------------*
51   Name:         MY_Free
52 
53   Description:  The user's memory deallocation function passed to the EL library.
54 
55   Arguments:    None.
56 
57   Returns:      None.
58  *---------------------------------------------------------------------------*/
MY_Free(void * ptr)59 static void MY_Free(void *ptr)
60 {
61     OS_Free( ptr);
62 }
63 
64 /*---------------------------------------------------------------------------*
65   Name:         VBlankIntr
66 
67   Description:  V-Blank interrupt handler
68 
69   Arguments:    None.
70 
71   Returns:      None.
72  *---------------------------------------------------------------------------*/
VBlankIntr(void)73 static void VBlankIntr(void)
74 {
75     //---- check interrupt flag
76     OS_SetIrqCheckFlag(OS_IE_V_BLANK);
77 }
78 
79 /*---------------------------------------------------------------------------*
80   Name:         TwlMain
81 
82   Description:  Main
83 
84   Arguments:    None.
85 
86   Returns:      None.
87  *---------------------------------------------------------------------------*/
TwlMain(void)88 void TwlMain(void)
89 {
90     ELDlld my_dlld;
91 
92     /* OS initialization */
93     OS_Init();
94     OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr);
95     (void)OS_EnableIrqMask(OS_IE_V_BLANK);
96     (void)OS_EnableIrq();
97     (void)GX_VBlankIntr(TRUE);
98 
99     // When in NITRO mode, stopped by Panic
100     DEMOCheckRunOnTWL();
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         my_dlld = EL_LinkFileEx( "rom:/data/dllA.a", lib_buf, 8192);
138         if(my_dlld == 0)
139             OS_Panic("failed EL_LinkFileEx\n");
140 
141         OS_TPrintf( "dll loaded 0x%x - 0x%x\n", (u32)lib_buf, (u32)lib_buf + EL_GetLibSize( my_dlld));
142 
143         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlld));
144 
145         /* Export the static-side symbols */
146         EL_AddStaticSym();
147 
148         /* Resolve the DLL symbols */
149         (void)EL_ResolveAll();
150 
151         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlld));
152         if(!EL_IsResolved( my_dlld))
153             OS_Panic("failed EL_ResolveAll\n");
154 
155         OS_TPrintf( "LINK : dynamic\n");
156         global_func = (global_func_p)EL_GetGlobalAdr( my_dlld, "global_func_A\0");
157         OS_TPrintf( "global_func : 0x%x\n", global_func);
158         if(global_func == 0)
159             OS_Panic("failed EL_GetGlobalAdr\n");
160 
161         OS_TPrintf( "----- dll-func1 execution -----\n");
162         (*global_func)();
163 
164         OS_TPrintf( "----- dll execution end -----\n");
165 
166         /* Unlink */
167         (void)EL_Unlink( my_dlld);
168         OS_TPrintf( "EL_Unlink success.\n");
169 
170         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlld));
171 
172     }
173 
174     OS_TPrintf("\n");
175     OS_TPrintf("===================================\n");
176     OS_TPrintf("Finish\n");
177     OS_TPrintf("===================================\n");
178     OS_Terminate();
179 }
180 
181 /*====== End of main.c ======*/
182