1 /*****************************************************************************/
5 /* Chip library handling for the sim65 6502 simulator */
9 /* (C) 2002 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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. */
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: */
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 */
32 /*****************************************************************************/
49 /*****************************************************************************/
51 /*****************************************************************************/
58 /* A collection containing all libraries */
59 Collection ChipLibraries = STATIC_COLLECTION_INITIALIZER;
63 /*****************************************************************************/
65 /*****************************************************************************/
69 static ChipLibrary* NewChipLibrary (const char* LibName)
70 /* Create, initialize and return a new ChipLibrary structure */
73 ChipLibrary* L = xmalloc (sizeof (ChipLibrary));
75 /* Initialize the fields */
76 L->LibName = xstrdup (LibName);
81 L->Chips = EmptyCollection;
83 /* Return the allocated structure */
89 static void FreeChipLibrary (ChipLibrary* L)
90 /* Free a ChipLibrary structure */
96 /* If the library is open, close it. Discard any errors. */
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
111 void LoadChipLibrary (const char* LibName)
112 /* Load a chip library . This includes loading the shared libary, allocating
113 * and initializing the data structure.
117 int (*GetChipData) (const struct ChipData**, unsigned*);
120 /* Allocate a new ChipLibrary structure */
121 ChipLibrary* L = NewChipLibrary (LibName);
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);
132 /* Open the library */
133 L->Handle = dlopen (L->PathName, RTLD_GLOBAL | RTLD_LAZY);
135 /* Check for errors */
138 Error ("Cannot open `%s': %s", L->PathName, Msg);
143 /* Locate the GetChipData function */
144 GetChipData = dlsym (L->Handle, "GetChipData");
146 /* Check the error message */
149 /* We had an error */
150 Error ("Cannot find export `GetChipData' in `%s': %s", L->LibName, Msg);
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);
163 /* Remember the library */
164 CollAppend (&ChipLibraries, L);
166 /* Print some information */
167 Print (stderr, 1, "Opened chip library `%s'\n", L->PathName);