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-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 
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:  VBlank 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     {
100         void   *tempLo;
101         OSHeapHandle hh;
102 
103         // Based on the premise that OS_Init has been already called
104         tempLo = OS_InitAlloc(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi(), 1);
105         OS_SetArenaLo(OS_ARENA_MAIN, tempLo);
106         hh = OS_CreateHeap(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi());
107         if (hh < 0)
108         {
109             OS_Panic("ARM9: Fail to create heap...\n");
110         }
111         hh = OS_SetCurrentHeap(OS_ARENA_MAIN, hh);
112     }
113 
114 
115     OS_TPrintf("\n");
116     OS_TPrintf("===================================\n");
117     OS_TPrintf("EL Library test\n");
118     OS_TPrintf("===================================\n");
119 
120     /* Start testing the EL library*/
121     {
122         FS_Init( FS_DMA_NOT_USE );
123 
124         if( EL_Init( MY_Alloc, MY_Free) < 0)
125         {
126             OS_TPanic( "EL_Init failed.\n");
127         }
128         else
129         {
130             OS_TPrintf( "EL_Init success.\n");
131         }
132 
133         // Dynamic modules are in the ROM
134         my_dlld = EL_LinkFileEx( "rom:/data/dllA.a", lib_buf, 8192);
135         if(my_dlld == 0)
136             OS_Panic("failed EL_LinkFileEx\n");
137 
138         OS_TPrintf( "dll loaded 0x%x - 0x%x\n", (u32)lib_buf, (u32)lib_buf + EL_GetLibSize( my_dlld));
139 
140         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlld));
141 
142         /* Export the static-side symbols*/
143         EL_AddStaticSym();
144 
145         /* Resolve the DLL symbols*/
146         (void)EL_ResolveAll();
147 
148         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlld));
149         if(!EL_IsResolved( my_dlld))
150             OS_Panic("failed EL_ResolveAll\n");
151 
152         OS_TPrintf( "LINK : dynamic\n");
153         global_func = (global_func_p)EL_GetGlobalAdr( my_dlld, "global_func_A\0");
154         OS_TPrintf( "global_func : 0x%x\n", global_func);
155         if(global_func == 0)
156             OS_Panic("failed EL_GetGlobalAdr\n");
157 
158         OS_TPrintf( "----- dll-func1 execution -----\n");
159         (*global_func)();
160 
161         OS_TPrintf( "----- dll execution end -----\n");
162 
163         /* Unlink*/
164         (void)EL_Unlink( my_dlld);
165         OS_TPrintf( "EL_Unlink success.\n");
166 
167         OS_TPrintf( "object resolved flag = %d\n", EL_IsResolved( my_dlld));
168 
169     }
170 
171     OS_TPrintf("\n");
172     OS_TPrintf("===================================\n");
173     OS_TPrintf("Finish\n");
174     OS_TPrintf("===================================\n");
175     OS_Terminate();
176 }
177 
178 /*====== End of main.c ======*/
179