#include <string.h>
-#include <dlfcn.h>
/* common */
#include "coll.h"
#include "xmalloc.h"
/* sim65 */
-#include "chippath.h"
+#include "chipdata.h"
+#include "chiplib.h"
#include "error.h"
#include "chip.h"
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);
}
-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);
}
+/* 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 */
};
-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
--- /dev/null
+/*****************************************************************************/
+/* */
+/* 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
+
+
+
/* sim65 */
+#include "chipdata.h"
#include "simdata.h"
--- /dev/null
+/*****************************************************************************/
+/* */
+/* 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);
+}
+
+
+
--- /dev/null
+/*****************************************************************************/
+/* */
+/* 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
+
+
+
-const char* GetName (void)
-{
- return "RAM";
-}
-
-
-
-unsigned GetVersion (void)
+int GetChipData (const ChipData** Data, unsigned* Count)
{
+ *Data = 0;
+ *Count = 0;
return 1;
}
#include "print.h"
#include "version.h"
-/* sim65 */
+/* sim65 */
#include "chip.h"
+#include "chiplib.h"
#include "chippath.h"
#include "cpucore.h"
#include "cputype.h"
/* Initialize modules */
AddChipPath ("chips");
- LoadChip ("ram.so");
+ LoadChipLibrary ("ram.so");
+ LoadChips ();
MemInit ();
MemLoad ("uz.bin", 0x200, 0);
CPUInit ();
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
.PHONY: all
ifeq (.depend,$(wildcard .depend))
-all : $(EXECS)
+all: $(EXECS) chips
include .depend
else
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
# ------------------------------------------------------------------------------