]> git.sur5r.net Git - cc65/commitdiff
Working on the plugins
authorcuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Sun, 7 Apr 2002 20:00:39 +0000 (20:00 +0000)
committercuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Sun, 7 Apr 2002 20:00:39 +0000 (20:00 +0000)
git-svn-id: svn://svn.cc65.org/cc65/trunk@1222 b7a2c559-68d2-44c3-8de9-860c34a00d81

src/sim65/chip.c
src/sim65/chip.h
src/sim65/chipdata.h [new file with mode: 0644]
src/sim65/chipif.h
src/sim65/chiplib.c [new file with mode: 0644]
src/sim65/chiplib.h [new file with mode: 0644]
src/sim65/chips/ram.c
src/sim65/main.c
src/sim65/make/gcc.mak

index 19a1c29aad7a28f2a6bb1408eb96f494c11f0971..033ffd54dc89159fba0c38de281680e1fe5c3ce6 100644 (file)
@@ -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);
-}
-
-
-
-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);
 }
 
index 72f7b5ee8a3d40f593aed9b2d05655034d55f7ba..9959ac31bdb2b14154a6c8162b2b747744e5e7b9 100644 (file)
 
 
 
+/* 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 (file)
index 0000000..9872ce2
--- /dev/null
@@ -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
+
+
+
index 8359a6f559da95ab90ee255703a41e40ca2b604f..1badc3696b5283c988e1e5394af91c4e020e1c73 100644 (file)
@@ -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 (file)
index 0000000..5996178
--- /dev/null
@@ -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 <dlfcn.h>
+
+/* 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 (file)
index 0000000..29a5ebf
--- /dev/null
@@ -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
+
+
+
index 36df2ea37c89902fadff6a5e0f4280c1d53510ed..2ff2d9afdea5017565f95741208360caecbc675f 100644 (file)
 
 
 
-const char* GetName (void)
-{
-    return "RAM";
-}
-
-
-
-unsigned GetVersion (void)
+int GetChipData (const ChipData** Data, unsigned* Count)
 {
+    *Data = 0;
+    *Count = 0;
     return 1;
 }
 
index 5b7d44f9ed32c7a2b7b592be1255c772fc8ec991..ec865bfdb3a126708581012c315d7a7eae98478a 100644 (file)
@@ -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 ();
index d04e0e01ce6d3f179e73a44744044c9e6bafad23..cc299793228ed380514c94ab6bd10ab37ae2a81a 100644 (file)
@@ -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
 
 # ------------------------------------------------------------------------------