1
0
mirror of https://github.com/cc65/cc65.git synced 2024-06-25 13:29:41 +00:00

Working on the plugins

git-svn-id: svn://svn.cc65.org/cc65/trunk@1222 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz 2002-04-07 20:00:39 +00:00
parent 6c3720686b
commit 58b5779f35
9 changed files with 430 additions and 144 deletions

View 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);
}
/* 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);
}

View File

@ -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

76
src/sim65/chipdata.h Normal file
View File

@ -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

View File

@ -39,6 +39,7 @@
/* sim65 */
#include "chipdata.h"
#include "simdata.h"

171
src/sim65/chiplib.c Normal file
View File

@ -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);
}

94
src/sim65/chiplib.h Normal file
View File

@ -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

View File

@ -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;
}

View 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 ();

View 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
# ------------------------------------------------------------------------------