From: cuz Date: Sun, 7 Apr 2002 20:00:39 +0000 (+0000) Subject: Working on the plugins X-Git-Tag: V2.12.0~2395 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=58b5779f35e86fade3f980cb97fb76fcc4dd2a3f;p=cc65 Working on the plugins git-svn-id: svn://svn.cc65.org/cc65/trunk@1222 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- diff --git a/src/sim65/chip.c b/src/sim65/chip.c index 19a1c29aa..033ffd54d 100644 --- a/src/sim65/chip.c +++ b/src/sim65/chip.c @@ -34,7 +34,6 @@ #include -#include /* 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); -} - - - -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; - } + /* Cast the object pointers */ + const Chip* Left = (const Chip*) rhs; + const Chip* Right = (const Chip*) lhs; - /* 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; - - /* Locate the library */ - char* PathName = FindChipLib (LibName); - if (PathName == 0) { - /* Library not found */ - Error ("Cannot find chip plugin library `%s'", LibName); - return; - } + unsigned I, J; - /* Open the library */ - H = dlopen (PathName, RTLD_GLOBAL | RTLD_LAZY); + /* Walk through all libraries */ + for (I = 0; I < CollCount (&ChipLibraries); ++I) { - /* Check for errors */ - Msg = dlerror (); - if (Msg) { - Error ("Error opening `%s': %s", PathName, Msg); - } + /* Get the library entry */ + ChipLibrary* L = CollAt (&ChipLibraries, I); - /* Free the path to the library since we don't need it any longer */ - xfree (PathName); + /* Create the chips */ + for (J = 0; J < L->ChipCount; ++J) { - /* 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); -} + /* 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)); + } + } -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); } diff --git a/src/sim65/chip.h b/src/sim65/chip.h index 72f7b5ee8..9959ac31b 100644 --- a/src/sim65/chip.h +++ b/src/sim65/chip.h @@ -38,32 +38,38 @@ +/* common.h */ +#include "coll.h" + +/* sim65 */ +#include "chipdata.h" +#include "simdata.h" + + + /*****************************************************************************/ /* Data */ /*****************************************************************************/ -/* Forward */ -struct SimData; +#if 0 +typedef struct ChipInstance ChipInstance; +struct ChipInstance { + Chip* C; /* Pointer to corresponding chip */ + unsigned Addr; /* Start address of range */ + unsigned Size; /* Size of range */ +}; +#endif + + /* Chip structure */ typedef struct Chip Chip; struct Chip { - char* Name; /* Name - must be unique */ - char* LibName; /* Name of the associated library */ - void* Handle; /* Library handle or pointer to it */ - - /* -- Exported functions -- */ - unsigned (*InitChip) (const struct SimData* Data); - const char* (*GetName) (void); - unsigned (*GetVersion) (void); - - void (*WriteCtrl) (unsigned Addr, unsigned char Val); - void (*Write) (unsigned Addr, unsigned char Val); - - unsigned char (*ReadCtrl) (unsigned Addr); - unsigned char (*Read) (unsigned Addr); + struct ChipLibrary* Library; /* Pointer to library data structure */ + const ChipData* Data; /* Chip data as given by the library */ + Collection Instances; /* Pointer to chip instances */ }; @@ -74,13 +80,8 @@ struct Chip { -void LoadChip (const char* LibName); -/* Load a chip. This includes loading the shared libary, allocating and - * initializing the data structure. - */ - -void InitChips (void); -/* Initialize the chips. Must be called *after* all chips are loaded */ +void LoadChips (void); +/* Load all chips from all libraries */ const Chip* FindChip (const char* Name); /* Find a chip by name. Returns the Chip data structure or NULL if the chip diff --git a/src/sim65/chipdata.h b/src/sim65/chipdata.h new file mode 100644 index 000000000..9872ce258 --- /dev/null +++ b/src/sim65/chipdata.h @@ -0,0 +1,76 @@ +/*****************************************************************************/ +/* */ +/* chipdata.h */ +/* */ +/* Chip description data structure */ +/* */ +/* */ +/* */ +/* (C) 2002 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ + + + +#ifndef CHIPDATA_H +#define CHIPDATA_H + + + +/*****************************************************************************/ +/* Data */ +/*****************************************************************************/ + + + +/* Version information. */ +#define CHIPDATA_VER_MAJOR 1U +#define CHIPDATA_VER_MINOR 0U + +/* Forwards */ +struct SimData; + +/* ChipDesc structure */ +typedef struct ChipData ChipData; +struct ChipData { + const char* ChipName; /* Name of the chip */ + unsigned MajorVersion; /* Version information */ + unsigned MinorVersion; + + /* -- Exported functions -- */ + void* (*Init) (const struct SimData* Data, unsigned Addr, unsigned Range); + void (*WriteCtrl) (void* Data, unsigned Addr, unsigned char Val); + void (*Write) (void* Data, unsigned Addr, unsigned char Val); + unsigned char (*ReadCtrl) (void* Data, unsigned Addr); + unsigned char (*Read) (void* Data, unsigned Addr); +}; + + + +/* End of chipdata.h */ + +#endif + + + diff --git a/src/sim65/chipif.h b/src/sim65/chipif.h index 8359a6f55..1badc3696 100644 --- a/src/sim65/chipif.h +++ b/src/sim65/chipif.h @@ -39,6 +39,7 @@ /* sim65 */ +#include "chipdata.h" #include "simdata.h" diff --git a/src/sim65/chiplib.c b/src/sim65/chiplib.c new file mode 100644 index 000000000..5996178db --- /dev/null +++ b/src/sim65/chiplib.c @@ -0,0 +1,171 @@ +/*****************************************************************************/ +/* */ +/* chiplib.c */ +/* */ +/* Chip library handling for the sim65 6502 simulator */ +/* */ +/* */ +/* */ +/* (C) 2002 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ + + + +#include + +/* common */ +#include "print.h" +#include "xmalloc.h" + +/* sim65 */ +#include "chippath.h" +#include "error.h" +#include "chiplib.h" + + + +/*****************************************************************************/ +/* Data */ +/*****************************************************************************/ + + + +/* Forwards */ +struct ChipData; + +/* A collection containing all libraries */ +Collection ChipLibraries = STATIC_COLLECTION_INITIALIZER; + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +static ChipLibrary* NewChipLibrary (const char* LibName) +/* Create, initialize and return a new ChipLibrary structure */ +{ + /* Allocate memory */ + ChipLibrary* L = xmalloc (sizeof (ChipLibrary)); + + /* Initialize the fields */ + L->LibName = xstrdup (LibName); + L->PathName = 0; + L->Handle = 0; + L->Data = 0; + L->ChipCount = 0; + L->Chips = EmptyCollection; + + /* Return the allocated structure */ + return L; +} + + + +static void FreeChipLibrary (ChipLibrary* L) +/* Free a ChipLibrary structure */ +{ + /* Free the names */ + xfree (L->LibName); + xfree (L->PathName); + + /* If the library is open, close it. Discard any errors. */ + if (L->Handle) { + dlclose (L->Handle); + (void) dlerror (); + } + + /* We may have to handle the Chip pointers, but currently the function + * is never called with a non empty Chips collection, so we don't care + * for now. + */ + xfree (L); +} + + + +void LoadChipLibrary (const char* LibName) +/* Load a chip library . This includes loading the shared libary, allocating + * and initializing the data structure. + */ +{ + const char* Msg; + int (*GetChipData) (const struct ChipData**, unsigned*); + int ErrorCode; + + /* Allocate a new ChipLibrary structure */ + ChipLibrary* L = NewChipLibrary (LibName); + + /* Locate the library */ + L->PathName = FindChipLib (LibName); + if (L->PathName == 0) { + /* Library not found */ + Error ("Cannot find chip plugin library `%s'", LibName); + FreeChipLibrary (L); + return; + } + + /* Open the library */ + L->Handle = dlopen (L->PathName, RTLD_GLOBAL | RTLD_LAZY); + + /* Check for errors */ + Msg = dlerror (); + if (Msg) { + Error ("Cannot open `%s': %s", L->PathName, Msg); + FreeChipLibrary (L); + return; + } + + /* Locate the GetChipData function */ + GetChipData = dlsym (L->Handle, "GetChipData"); + + /* Check the error message */ + Msg = dlerror (); + if (Msg) { + /* We had an error */ + Error ("Cannot find export `GetChipData' in `%s': %s", L->LibName, Msg); + FreeChipLibrary (L); + return; + } + + /* Call the function to read the chip data */ + ErrorCode = GetChipData (&L->Data, &L->ChipCount); + if (ErrorCode != 0) { + Error ("Function `GetChipData' in `%s' returned error %d", L->LibName, ErrorCode); + FreeChipLibrary (L); + return; + } + + /* Remember the library */ + CollAppend (&ChipLibraries, L); + + /* Print some information */ + Print (stderr, 1, "Opened plugin library `%s'", L->PathName); +} + + + diff --git a/src/sim65/chiplib.h b/src/sim65/chiplib.h new file mode 100644 index 000000000..29a5ebf44 --- /dev/null +++ b/src/sim65/chiplib.h @@ -0,0 +1,94 @@ +/*****************************************************************************/ +/* */ +/* chiplib.h */ +/* */ +/* Chip library handling for the sim65 6502 simulator */ +/* */ +/* */ +/* */ +/* (C) 2002 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ + + + +#ifndef CHIPLIB_H +#define CHIPLIB_H + + + +/* common */ +#include "coll.h" + + + +/*****************************************************************************/ +/* Data */ +/*****************************************************************************/ + + + +/* Forward */ +struct ChipData; + +/* ChipLibrary structure */ +typedef struct ChipLibrary ChipLibrary; +struct ChipLibrary { + char* LibName; /* Name of the library as given */ + char* PathName; /* Name of library including path */ + void* Handle; /* Pointer to libary handle */ + const struct ChipData* Data; /* Pointer to chip data */ + unsigned ChipCount; /* Number of chips in this library */ + Collection Chips; /* Chips in this library */ +}; + +/* A collection containing all libraries */ +Collection ChipLibraries; + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +void LoadChipLibrary (const char* LibName); +/* Load a chip library . This includes loading the shared libary, allocating + * and initializing the data structure. + */ + +void* GetChipLibSym (const ChipLibrary* L, const char* SymName); +/* Locate a symbol in a module and return it. Abort on errors (may be modified + * later to return NULL). + */ + + + +/* End of chiplib.h */ + +#endif + + + diff --git a/src/sim65/chips/ram.c b/src/sim65/chips/ram.c index 36df2ea37..2ff2d9afd 100644 --- a/src/sim65/chips/ram.c +++ b/src/sim65/chips/ram.c @@ -50,15 +50,10 @@ -const char* GetName (void) -{ - return "RAM"; -} - - - -unsigned GetVersion (void) +int GetChipData (const ChipData** Data, unsigned* Count) { + *Data = 0; + *Count = 0; return 1; } diff --git a/src/sim65/main.c b/src/sim65/main.c index 5b7d44f9e..ec865bfdb 100644 --- a/src/sim65/main.c +++ b/src/sim65/main.c @@ -44,8 +44,9 @@ #include "print.h" #include "version.h" -/* sim65 */ +/* sim65 */ #include "chip.h" +#include "chiplib.h" #include "chippath.h" #include "cpucore.h" #include "cputype.h" @@ -203,7 +204,8 @@ int main (int argc, char* argv[]) /* Initialize modules */ AddChipPath ("chips"); - LoadChip ("ram.so"); + LoadChipLibrary ("ram.so"); + LoadChips (); MemInit (); MemLoad ("uz.bin", 0x200, 0); CPUInit (); diff --git a/src/sim65/make/gcc.mak b/src/sim65/make/gcc.mak index d04e0e01c..cc2997932 100644 --- a/src/sim65/make/gcc.mak +++ b/src/sim65/make/gcc.mak @@ -11,13 +11,14 @@ EBIND = emxbind LDFLAGS = OBJS = chip.o \ + chiplib.o \ chippath.o \ - cpucore.o \ - cputype.o \ + cpucore.o \ + cputype.o \ error.o \ - global.o \ + global.o \ main.o \ - memory.o \ + memory.o \ simdata.o LIBS = $(COMMON)/common.a @@ -26,7 +27,7 @@ EXECS = sim65 .PHONY: all ifeq (.depend,$(wildcard .depend)) -all : $(EXECS) +all: $(EXECS) chips include .depend else all: depend @@ -34,15 +35,19 @@ all: depend endif - sim65: $(OBJS) $(LIBS) $(CC) $(CFLAGS) -o $@ $(OBJS) $(LIBS) -ldl @if [ $(OS2_SHELL) ] ; then $(EBIND) $@ ; fi +.PHONY: chips +chips: + @$(MAKE) -C chips -f make/gcc.mak + + clean: rm -f *~ core *.lst -zap: clean +zap: clean rm -f *.o $(EXECS) .depend # ------------------------------------------------------------------------------