]> git.sur5r.net Git - cc65/blob - src/sim65/chiplib.c
support for .zeropage segment in GEOS
[cc65] / src / sim65 / chiplib.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                   chiplib.c                               */
4 /*                                                                           */
5 /*              Chip library handling for the sim65 6502 simulator           */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2002      Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <dlfcn.h>
37
38 /* common */
39 #include "print.h"
40 #include "xmalloc.h"
41
42 /* sim65 */
43 #include "chippath.h"
44 #include "error.h"
45 #include "chiplib.h"
46
47
48
49 /*****************************************************************************/
50 /*                                     Data                                  */
51 /*****************************************************************************/
52
53
54
55 /* Forwards */
56 struct ChipData;
57
58 /* A collection containing all libraries */
59 Collection ChipLibraries = STATIC_COLLECTION_INITIALIZER;
60
61
62
63 /*****************************************************************************/
64 /*                                   Code                                    */
65 /*****************************************************************************/
66
67
68
69 static ChipLibrary* NewChipLibrary (const char* LibName)
70 /* Create, initialize and return a new ChipLibrary structure */
71 {
72     /* Allocate memory */
73     ChipLibrary* L = xmalloc (sizeof (ChipLibrary));
74
75     /* Initialize the fields */
76     L->LibName   = xstrdup (LibName);
77     L->PathName  = 0;
78     L->Handle    = 0;
79     L->Data      = 0;
80     L->ChipCount = 0;
81     L->Chips     = EmptyCollection;
82
83     /* Return the allocated structure */
84     return L;
85 }
86
87
88
89 static void FreeChipLibrary (ChipLibrary* L)
90 /* Free a ChipLibrary structure */
91 {
92     /* Free the names */
93     xfree (L->LibName);
94     xfree (L->PathName);
95
96     /* If the library is open, close it. Discard any errors. */
97     if (L->Handle) {
98         dlclose (L->Handle);
99         (void) dlerror ();
100     }
101
102     /* We may have to handle the Chip pointers, but currently the function
103      * is never called with a non empty Chips collection, so we don't care
104      * for now.
105      */
106     xfree (L);
107 }
108
109
110
111 void LoadChipLibrary (const char* LibName)
112 /* Load a chip library . This includes loading the shared libary, allocating
113  * and initializing the data structure.
114  */
115 {
116     const char* Msg;
117     int (*GetChipData) (const struct ChipData**, unsigned*);
118     int ErrorCode;
119
120     /* Allocate a new ChipLibrary structure */
121     ChipLibrary* L = NewChipLibrary (LibName);
122
123     /* Locate the library */
124     L->PathName = FindChipLib (LibName);
125     if (L->PathName == 0) {
126         /* Library not found */
127         Error ("Cannot find chip plugin library `%s'", LibName);
128         FreeChipLibrary (L);
129         return;
130     }
131
132     /* Open the library */
133     L->Handle = dlopen (L->PathName, RTLD_GLOBAL | RTLD_LAZY);
134
135     /* Check for errors */
136     Msg = dlerror ();
137     if (Msg) {
138         Error ("Cannot open `%s': %s", L->PathName, Msg);
139         FreeChipLibrary (L);
140         return;
141     }
142
143     /* Locate the GetChipData function */
144     GetChipData = dlsym (L->Handle, "GetChipData");
145
146     /* Check the error message */
147     Msg = dlerror ();
148     if (Msg) {
149         /* We had an error */
150         Error ("Cannot find export `GetChipData' in `%s': %s", L->LibName, Msg);
151         FreeChipLibrary (L);
152         return;
153     }
154
155     /* Call the function to read the chip data */
156     ErrorCode = GetChipData (&L->Data, &L->ChipCount);
157     if (ErrorCode != 0) {
158         Error ("Function `GetChipData' in `%s' returned error %d", L->LibName, ErrorCode);
159         FreeChipLibrary (L);
160         return;
161     }
162
163     /* Remember the library */
164     CollAppend (&ChipLibraries, L);
165
166     /* Print some information */
167     Print (stderr, 1, "Opened chip library `%s'\n", L->PathName);
168 }
169
170
171