1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-27 00:29:31 +00:00

Moved the chiplib module into chip.c

git-svn-id: svn://svn.cc65.org/cc65/trunk@2093 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz 2003-04-28 06:19:33 +00:00
parent 8bff858b09
commit 94e6e73089
7 changed files with 156 additions and 306 deletions

View File

@ -34,16 +34,17 @@
#include <string.h>
#include <dlfcn.h>
/* common */
#include "coll.h"
#include "fname.h"
#include "print.h"
#include "xmalloc.h"
/* sim65 */
#include "cfgdata.h"
#include "chipdata.h"
#include "chiplib.h"
#include "error.h"
#include "chip.h"
@ -58,6 +59,9 @@
/* Sorted list of all chip data structures */
static Collection Chips = STATIC_COLLECTION_INITIALIZER;
/* A collection containing all libraries */
static Collection ChipLibraries = STATIC_COLLECTION_INITIALIZER;
/* SimData instance */
static const SimData Sim65Data = {
1, /* MajorVersion */
@ -99,7 +103,7 @@ static Chip* FindChip (const char* Name)
/* ## We do a linear search for now */
for (I = 0; I < CollCount (&Chips); ++I) {
/* Get the chip at this position */
/* Get the chip at this position */
Chip* C = CollAt (&Chips, I);
/* Compare the name */
@ -121,7 +125,47 @@ static Chip* FindChip (const char* Name)
Chip* NewChip (ChipLibrary* Library, const ChipData* Data)
static ChipLibrary* NewChipLibrary (const char* PathName)
/* Create, initialize and return a new ChipLibrary structure */
{
/* Allocate memory */
ChipLibrary* L = xmalloc (sizeof (ChipLibrary));
/* Initialize the fields */
L->LibName = xstrdup (FindName (PathName));
L->PathName = xstrdup (PathName);
L->Handle = 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);
}
static Chip* NewChip (ChipLibrary* Library, const ChipData* Data)
/* Allocate a new chip structure, initialize and return it */
{
/* Allocate memory */
@ -142,7 +186,7 @@ Chip* NewChip (ChipLibrary* Library, const ChipData* Data)
ChipInstance* NewChipInstance (const char* ChipName, unsigned Addr,
unsigned Size, const Collection* Attributes)
unsigned Size, Collection* Attributes)
{
ChipInstance* CI;
@ -159,9 +203,7 @@ ChipInstance* NewChipInstance (const char* ChipName, unsigned Addr,
CI->C = C;
CI->Addr = Addr;
CI->Size = Size;
CI->Data = C->Data->InitInstance (Addr, Size,
(const CfgData**) Attributes->Items,
CollCount (Attributes));
CI->Data = C->Data->InitInstance (Addr, Size, Attributes);
/* Assign the chip instance to the chip */
CollAppend (&C->Instances, CI);
@ -202,3 +244,92 @@ void SortChips (void)
void LoadChipLibrary (const char* LibName)
/* Load a chip library. This includes loading the shared libary, allocating
* and initializing the data structure, and loading all chip data from the
* library.
*/
{
const char* Msg;
int (*GetChipData) (const struct ChipData**, unsigned*);
int ErrorCode;
const ChipData* Data; /* Pointer to chip data */
unsigned ChipCount; /* Number of chips in this library */
unsigned I;
/* Allocate a new ChipLibrary structure */
ChipLibrary* L = NewChipLibrary (LibName);
/* 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 (&Data, &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 chip library `%s'\n", L->PathName);
/* Create the chips */
for (I = 0; I < ChipCount; ++I) {
Chip* C;
/* Get a pointer to the chip data */
const ChipData* D = Data + I;
/* 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",
D->ChipName, L->LibName,
CHIPDATA_VER_MAJOR, D->MajorVersion);
/* Ignore this chip */
continue;
}
/* Generate a new chip */
C = NewChip (L, D);
/* Insert a reference to the chip into the library exporting it */
CollAppend (&L->Chips, C);
/* Output chip name and version to keep the user happy */
Print (stdout, 1,
" Found `%s', version %u.%u in library `%s'\n",
Data->ChipName,
Data->MajorVersion,
Data->MinorVersion,
L->LibName);
}
}

View File

@ -55,9 +55,9 @@
/* Forwards */
struct CfgData;
struct ChipLibrary;
typedef struct Chip Chip;
typedef struct ChipInstance ChipInstance;
typedef struct Chip Chip;
typedef struct ChipLibrary ChipLibrary;
/* One instance of a chip */
struct ChipInstance {
@ -74,6 +74,14 @@ struct Chip {
Collection Instances; /* Pointer to chip instances */
};
/* ChipLibrary structure */
struct ChipLibrary {
char* LibName; /* Name of the library as given */
char* PathName; /* Name of library including path */
void* Handle; /* Pointer to libary handle */
Collection Chips; /* Chips in this library */
};
/*****************************************************************************/
@ -82,11 +90,8 @@ struct Chip {
Chip* NewChip (struct ChipLibrary* Library, const ChipData* Data);
/* Allocate a new chip structure, initialize and return it */
ChipInstance* NewChipInstance (const char* ChipName, unsigned Addr,
unsigned Size, const Collection* Attributes);
unsigned Size, Collection* Attributes);
/* Allocate a new chip instance for the chip. */
ChipInstance* MirrorChipInstance (const ChipInstance* Orig, unsigned Addr);
@ -95,6 +100,12 @@ ChipInstance* MirrorChipInstance (const ChipInstance* Orig, unsigned Addr);
void SortChips (void);
/* Sort all chips by name. Called after loading */
void LoadChipLibrary (const char* LibName);
/* Load a chip library. This includes loading the shared libary, allocating
* and initializing the data structure, and loading all chip data from the
* library.
*/
/* End of chip.h */
@ -103,4 +114,3 @@ void SortChips (void);

View File

@ -61,8 +61,7 @@ struct ChipData {
/* -- Exported functions -- */
int (*InitChip) (const struct SimData* Data);
void* (*InitInstance) (unsigned Addr, unsigned Range,
const struct CfgData** Data, unsigned CfgDataCount);
void* (*InitInstance) (unsigned Addr, unsigned Range, void* CfgInfo);
void (*WriteCtrl) (void* Data, unsigned Offs, unsigned char Val);
void (*Write) (void* Data, unsigned Offs, unsigned char Val);
unsigned char (*ReadCtrl) (void* Data, unsigned Offs);

View File

@ -1,196 +0,0 @@
/*****************************************************************************/
/* */
/* chiplib.c */
/* */
/* Chip library handling for the sim65 6502 simulator */
/* */
/* */
/* */
/* (C) 2002-2003 Ullrich von Bassewitz */
/* Römerstrasse 52 */
/* D-70794 Filderstadt */
/* EMail: uz@cc65.org */
/* */
/* */
/* 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 "fname.h"
#include "print.h"
#include "xmalloc.h"
/* sim65 */
#include "chip.h"
#include "chippath.h"
#include "error.h"
#include "chiplib.h"
/*****************************************************************************/
/* Data */
/*****************************************************************************/
/* A collection containing all libraries */
Collection ChipLibraries = STATIC_COLLECTION_INITIALIZER;
/*****************************************************************************/
/* Code */
/*****************************************************************************/
static ChipLibrary* NewChipLibrary (const char* PathName)
/* Create, initialize and return a new ChipLibrary structure */
{
/* Allocate memory */
ChipLibrary* L = xmalloc (sizeof (ChipLibrary));
/* Initialize the fields */
L->LibName = xstrdup (FindName (PathName));
L->PathName = xstrdup (PathName);
L->Handle = 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, and loading all chip data from the
* library.
*/
{
const char* Msg;
int (*GetChipData) (const struct ChipData**, unsigned*);
int ErrorCode;
const ChipData* Data; /* Pointer to chip data */
unsigned ChipCount; /* Number of chips in this library */
unsigned I;
/* Allocate a new ChipLibrary structure */
ChipLibrary* L = NewChipLibrary (LibName);
/* 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 (&Data, &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 chip library `%s'\n", L->PathName);
/* Create the chips */
for (I = 0; I < ChipCount; ++I) {
Chip* C;
/* Get a pointer to the chip data */
const ChipData* D = Data + I;
/* 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",
D->ChipName, L->LibName,
CHIPDATA_VER_MAJOR, D->MajorVersion);
/* Ignore this chip */
continue;
}
/* Generate a new chip */
C = NewChip (L, D);
/* Insert a reference to the chip into the library exporting it */
CollAppend (&L->Chips, C);
/* Output chip name and version to keep the user happy */
Print (stdout, 1,
" Found `%s', version %u.%u in library `%s'\n",
Data->ChipName,
Data->MajorVersion,
Data->MinorVersion,
L->LibName);
}
}

View File

@ -1,91 +0,0 @@
/*****************************************************************************/
/* */
/* 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"
/* sim65 */
#include "chipdata.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 */
Collection Chips; /* Chips in this library */
};
/* A collection containing all libraries */
extern Collection ChipLibraries;
/*****************************************************************************/
/* Code */
/*****************************************************************************/
void LoadChipLibrary (const char* LibName);
/* Load a chip library. This includes loading the shared libary, allocating
* and initializing the data structure, and loading all chip data from the
* library.
*/
/* End of chiplib.h */
#endif

View File

@ -51,7 +51,6 @@
/* sim65 */
#include "chip.h"
#include "chiplib.h"
#include "chippath.h"
#include "config.h"
#include "cpucore.h"
@ -298,7 +297,6 @@ int main (int argc, char* argv[])
/* Read the config file */
CfgRead ();
/* Initialize the CPU */
CPUInit ();
#if 0
CPURun ();

View File

@ -11,7 +11,6 @@ EBIND = emxbind
LDFLAGS =
OBJS = chip.o \
chiplib.o \
chippath.o \
config.o \
cpucore.o \