Working on the plugins
git-svn-id: svn://svn.cc65.org/cc65/trunk@1222 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
149
src/sim65/chip.c
149
src/sim65/chip.c
@@ -34,7 +34,6 @@
|
||||
|
||||
|
||||
#include <string.h>
|
||||
#include <dlfcn.h>
|
||||
|
||||
/* common */
|
||||
#include "coll.h"
|
||||
@@ -42,7 +41,8 @@
|
||||
#include "xmalloc.h"
|
||||
|
||||
/* sim65 */
|
||||
#include "chippath.h"
|
||||
#include "chipdata.h"
|
||||
#include "chiplib.h"
|
||||
#include "error.h"
|
||||
#include "chip.h"
|
||||
|
||||
@@ -69,37 +69,12 @@ static int CmpChips (void* Data attribute ((unused)),
|
||||
const void* lhs, const void* rhs)
|
||||
/* Compare function for CollSort */
|
||||
{
|
||||
return strcmp (((const Chip*) lhs)->Name, ((const Chip*) rhs)->Name);
|
||||
}
|
||||
/* Cast the object pointers */
|
||||
const Chip* Left = (const Chip*) rhs;
|
||||
const Chip* Right = (const Chip*) lhs;
|
||||
|
||||
|
||||
|
||||
void* GetSym (const Chip* C, const char* SymName)
|
||||
/* Locate a symbol in a module and return it. Abort on errors (may be modified
|
||||
* later to return NULL).
|
||||
*/
|
||||
{
|
||||
void* Val;
|
||||
const char* Msg;
|
||||
|
||||
/* Fetch the error message and discard it - this will clear pending
|
||||
* errors
|
||||
*/
|
||||
dlerror ();
|
||||
|
||||
/* Fetch the symbol value */
|
||||
Val = dlsym (C->Handle, SymName);
|
||||
|
||||
/* Check the error message */
|
||||
Msg = dlerror ();
|
||||
if (Msg) {
|
||||
/* We had an error */
|
||||
Error ("Error loading `%s' from `%s': %s", SymName, C->LibName, Msg);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Return the symbol value read */
|
||||
return Val;
|
||||
/* Do the compare */
|
||||
return strcmp (Left->Data->ChipName, Right->Data->ChipName);
|
||||
}
|
||||
|
||||
|
||||
@@ -110,100 +85,66 @@ void* GetSym (const Chip* C, const char* SymName)
|
||||
|
||||
|
||||
|
||||
static Chip* NewChip (void* Handle, const char* LibName)
|
||||
static Chip* NewChip (ChipLibrary* Library, const ChipData* Data)
|
||||
/* Allocate a new chip structure, initialize and return it */
|
||||
{
|
||||
/* Allocate memory */
|
||||
Chip* C = xmalloc (sizeof (Chip));
|
||||
|
||||
/* Initialize the fields */
|
||||
C->Name = 0;
|
||||
C->LibName = xstrdup (LibName);
|
||||
C->Handle = Handle;
|
||||
C->InitChip = 0;
|
||||
C->GetVersion = 0;
|
||||
C->WriteCtrl = 0;
|
||||
C->Write = 0;
|
||||
C->ReadCtrl = 0;
|
||||
C->Read = 0;
|
||||
C->Library = Library;
|
||||
C->Data = Data;
|
||||
C->Instances = EmptyCollection;
|
||||
|
||||
/* Return the structure */
|
||||
return C;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void FreeChip (Chip* C)
|
||||
/* Free the given chip structure */
|
||||
|
||||
#if 0
|
||||
static void FreeChip (Chip* C)
|
||||
/* ## Free the given chip structure */
|
||||
{
|
||||
/* Free the strings */
|
||||
xfree (C->Name);
|
||||
xfree (C->LibName);
|
||||
|
||||
/* Free the structure itself */
|
||||
xfree (C);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
void LoadChip (const char* LibName)
|
||||
/* Load a chip. This includes loading the shared libary, allocating and
|
||||
* initializing the data structure.
|
||||
*/
|
||||
void LoadChips (void)
|
||||
/* Load all chips from all libraries */
|
||||
{
|
||||
Chip* C;
|
||||
void* H;
|
||||
const char* Msg;
|
||||
unsigned Ver;
|
||||
const char* Name;
|
||||
unsigned I, J;
|
||||
|
||||
/* Locate the library */
|
||||
char* PathName = FindChipLib (LibName);
|
||||
if (PathName == 0) {
|
||||
/* Library not found */
|
||||
Error ("Cannot find chip plugin library `%s'", LibName);
|
||||
return;
|
||||
/* Walk through all libraries */
|
||||
for (I = 0; I < CollCount (&ChipLibraries); ++I) {
|
||||
|
||||
/* Get the library entry */
|
||||
ChipLibrary* L = CollAt (&ChipLibraries, I);
|
||||
|
||||
/* Create the chips */
|
||||
for (J = 0; J < L->ChipCount; ++J) {
|
||||
|
||||
/* Get a pointer to the chip data */
|
||||
const ChipData* Data = L->Data + J;
|
||||
|
||||
/* Check if the chip data has the correct version */
|
||||
if (Data->MajorVersion != CHIPDATA_VER_MAJOR) {
|
||||
Warning ("Version mismatch for `%s' (%s), expected %u, got %u",
|
||||
Data->ChipName, L->LibName,
|
||||
CHIPDATA_VER_MAJOR, Data->MajorVersion);
|
||||
/* Ignore this chip */
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Generate a new chip and insert it into the collection */
|
||||
CollAppend (&Chips, NewChip (L, Data));
|
||||
}
|
||||
}
|
||||
|
||||
/* Open the library */
|
||||
H = dlopen (PathName, RTLD_GLOBAL | RTLD_LAZY);
|
||||
|
||||
/* Check for errors */
|
||||
Msg = dlerror ();
|
||||
if (Msg) {
|
||||
Error ("Error opening `%s': %s", PathName, Msg);
|
||||
}
|
||||
|
||||
/* Free the path to the library since we don't need it any longer */
|
||||
xfree (PathName);
|
||||
|
||||
/* Allocate the chip structure */
|
||||
C = NewChip (H, LibName);
|
||||
|
||||
/* Read function pointers */
|
||||
/* C->InitChip = GetSym (C, "InitChip"); */
|
||||
C->GetName = GetSym (C, "GetName");
|
||||
C->GetVersion = GetSym (C, "GetVersion");
|
||||
/* C->WriteCtrl = GetSym (C, "WriteCtrl"); */
|
||||
/* C->Write = GetSym (C, "Write"); */
|
||||
/* C->ReadCtrl = GetSym (C, "ReadCtrl"); */
|
||||
/* C->Read = GetSym (C, "Read"); */
|
||||
|
||||
/* Insert the structure into the list of all chips */
|
||||
CollAppend (&Chips, C);
|
||||
|
||||
/* Call the functions */
|
||||
Name = C->GetName ();
|
||||
Ver = C->GetVersion ();
|
||||
printf ("%s version %u\n", Name, Ver);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void InitChips (void)
|
||||
/* Initialize the chips. Must be called *after* all chips are loaded */
|
||||
{
|
||||
/* Sort the chips by name */
|
||||
/* Last act: Sort the chips by name */
|
||||
CollSort (&Chips, CmpChips, 0);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user