diff --git a/src/sim65/chip.c b/src/sim65/chip.c index 033ffd54d..e09ee2599 100644 --- a/src/sim65/chip.c +++ b/src/sim65/chip.c @@ -57,6 +57,15 @@ /* Sorted list of all chip data structures */ static Collection Chips = STATIC_COLLECTION_INITIALIZER; +/* SimData instance */ +static const SimData Sim65Data = { + 1, /* MajorVersion */ + 1, /* MinorVersion */ + xmalloc, /* void* (*Malloc) (size_t Size); */ + Warning, /* void (*Warning) (const char* Format, ...); */ + Error /* void (*Error) (const char* Format, ...); */ +}; + /*****************************************************************************/ @@ -101,14 +110,14 @@ static Chip* NewChip (ChipLibrary* Library, const ChipData* Data) } - + #if 0 static void FreeChip (Chip* C) /* ## Free the given chip structure */ { /* Free the structure itself */ xfree (C); -} +} #endif diff --git a/src/sim65/chipdata.h b/src/sim65/chipdata.h index 9872ce258..5f21045bc 100644 --- a/src/sim65/chipdata.h +++ b/src/sim65/chipdata.h @@ -46,7 +46,7 @@ /* Version information. */ #define CHIPDATA_VER_MAJOR 1U -#define CHIPDATA_VER_MINOR 0U +#define CHIPDATA_VER_MINOR 0U /* Forwards */ struct SimData; @@ -59,7 +59,8 @@ struct ChipData { unsigned MinorVersion; /* -- Exported functions -- */ - void* (*Init) (const struct SimData* Data, unsigned Addr, unsigned Range); + int (*InitChip) (const struct SimData* Data); + void* (*InitInstance) (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); diff --git a/src/sim65/chips/ram.c b/src/sim65/chips/ram.c index 2ff2d9afd..9e5f3ebd4 100644 --- a/src/sim65/chips/ram.c +++ b/src/sim65/chips/ram.c @@ -33,29 +33,198 @@ +#include +#include + /* sim65 */ #include "chipif.h" +/*****************************************************************************/ +/* Forwards */ +/*****************************************************************************/ + + + +int InitChip (const struct SimData* Data); +/* Initialize the chip, return an error code */ + +static void* InitInstance (unsigned Addr, unsigned Range); +/* Initialize a new chip instance */ + +static void WriteCtrl (void* Data, unsigned Addr, unsigned char Val); +/* Write control data */ + +static void Write (void* Data, unsigned Addr, unsigned char Val); +/* Write user data */ + +static unsigned char ReadCtrl (void* Data, unsigned Addr); +/* Read control data */ + +static unsigned char Read (void* Data, unsigned Addr); +/* Read user data */ + + + /*****************************************************************************/ /* Data */ /*****************************************************************************/ +/* Control data passed to the main program */ +static const struct ChipData RAMData[1] = { + { + "RAM", /* Name of the chip */ + CHIPDATA_VER_MAJOR, /* Version information */ + CHIPDATA_VER_MINOR, + + /* -- Exported functions -- */ + InitChip, + InitInstance, + WriteCtrl, + Write, + ReadCtrl, + Read + } +}; + +/* The SimData pointer we get when InitChip is called */ +static const SimData* Sim; + +/* Possible RAM attributes */ +#define ATTR_INITIALIZED 0x01 /* RAM cell is intialized */ +#define ATTR_WPROT 0x02 /* RAM cell is write protected */ + +/* Data for one RAM instance */ +typedef struct InstanceData InstanceData; +struct InstanceData { + unsigned BaseAddr; /* Base address */ + unsigned Range; /* Memory range */ + unsigned char* MemAttr; /* Memory attributes */ + unsigned char* Mem; /* The memory itself */ +}; + + + /*****************************************************************************/ -/* Code */ +/* Exported function */ /*****************************************************************************/ int GetChipData (const ChipData** Data, unsigned* Count) { - *Data = 0; - *Count = 0; - return 1; + /* Pass the control structure to the caller */ + *Data = RAMData; + *Count = sizeof (Data) / sizeof (Data[0]); + + /* Call was successful */ + return 0; } +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +int InitChip (const struct SimData* Data) +/* Initialize the chip, return an error code */ +{ + /* Remember the pointer */ + Sim = Data; + + /* Always successful */ + return 0; +} + + + +static void* InitInstance (unsigned Addr, unsigned Range) +/* Initialize a new chip instance */ +{ + /* Allocate a new instance structure */ + InstanceData* D = Sim->Malloc (sizeof (InstanceData)); + + /* Initialize the structure, allocate RAM and attribute memory */ + D->BaseAddr = Addr; + D->Range = Range; + D->MemAttr = Sim->Malloc (Range * sizeof (D->MemAttr[0])); + D->Mem = Sim->Malloc (Range * sizeof (D->Mem[0])); + + /* Clear the RAM and attribute memory */ + memset (D->MemAttr, 0, Range * sizeof (D->MemAttr[0])); + memset (D->Mem, 0, Range * sizeof (D->Mem[0])); + + /* Done, return the instance data */ + return D; +} + + + +static void WriteCtrl (void* Data, unsigned Addr, unsigned char Val) +/* Write control data */ +{ + /* Cast the data pointer */ + InstanceData* D = (InstanceData*) Data; + + /* Do the write and remember the cell as initialized */ + D->Mem[Addr] = Val; + D->MemAttr[Addr] |= ATTR_INITIALIZED; +} + + + +static void Write (void* Data, unsigned Addr, unsigned char Val) +/* Write user data */ +{ + /* Cast the data pointer */ + InstanceData* D = (InstanceData*) Data; + + /* Check for a write to a write protected cell */ + if (D->MemAttr[Addr] & ATTR_WPROT) { + Sim->Warning ("Writing to write protected memory at $%04X", Addr); + } + + /* Do the write and remember the cell as initialized */ + D->Mem[Addr] = Val; + D->MemAttr[Addr] |= ATTR_INITIALIZED; +} + + + +static unsigned char ReadCtrl (void* Data, unsigned Addr) +/* Read control data */ +{ + /* Cast the data pointer */ + InstanceData* D = (InstanceData*) Data; + + /* Read the cell and return the value */ + return D->Mem[Addr]; +} + + + +static unsigned char Read (void* Data, unsigned Addr) +/* Read user data */ +{ + /* Cast the data pointer */ + InstanceData* D = (InstanceData*) Data; + + /* Check for a read from an uninitialized cell */ + if ((D->MemAttr[Addr] & ATTR_INITIALIZED) == 0) { + /* We're reading a memory cell that was never written to */ + Sim->Warning ("Reading from uninitialized memory at $%04X", Addr); + } + + /* Read the cell and return the value */ + return D->Mem[Addr]; +} + + + + diff --git a/src/sim65/make/gcc.mak b/src/sim65/make/gcc.mak index cc2997932..aab8d3974 100644 --- a/src/sim65/make/gcc.mak +++ b/src/sim65/make/gcc.mak @@ -18,8 +18,7 @@ OBJS = chip.o \ error.o \ global.o \ main.o \ - memory.o \ - simdata.o + memory.o LIBS = $(COMMON)/common.a @@ -31,7 +30,7 @@ all: $(EXECS) chips include .depend else all: depend - @$(MAKE) -f make/gcc.mak all + @$(MAKE) -f make/gcc.mak all endif @@ -45,9 +44,11 @@ chips: clean: + @$(MAKE) -C chips -f make/gcc.mak clean rm -f *~ core *.lst zap: clean + @$(MAKE) -C chips -f make/gcc.mak zap rm -f *.o $(EXECS) .depend # ------------------------------------------------------------------------------ diff --git a/src/sim65/simdata.c b/src/sim65/simdata.c deleted file mode 100644 index 4f8321c05..000000000 --- a/src/sim65/simdata.c +++ /dev/null @@ -1,54 +0,0 @@ -/*****************************************************************************/ -/* */ -/* simdata.c */ -/* */ -/* Simulator data passed to the chip plugins */ -/* */ -/* */ -/* */ -/* (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. */ -/* */ -/*****************************************************************************/ - - - -/* sim65 */ -#include "simdata.h" - - - -/*****************************************************************************/ -/* Data */ -/*****************************************************************************/ - - - -/* SimData instance */ -const SimData Sim65Data = { - 1, /* MajorVersion */ - 1 /* MinorVersion */ -}; - - - diff --git a/src/sim65/simdata.h b/src/sim65/simdata.h index cac6da7aa..72424ebf2 100644 --- a/src/sim65/simdata.h +++ b/src/sim65/simdata.h @@ -49,10 +49,12 @@ typedef struct SimData SimData; struct SimData { unsigned MajorVersion; unsigned MinorVersion; -}; -/* SimData instance */ -extern const SimData Sim65Data; + /* -- Callback functions -- */ + void* (*Malloc) (size_t Size); + void (*Warning) (const char* Format, ...); + void (*Error) (const char* Format, ...); +};