From 8fb90af8ffebd06a1e5b1997cdfe7e3cb3c92ebb Mon Sep 17 00:00:00 2001 From: cuz Date: Thu, 1 May 2003 22:38:47 +0000 Subject: [PATCH] Working git-svn-id: svn://svn.cc65.org/cc65/trunk@2110 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/sim65/addrspace.c | 190 +++++++++++++++++++++++++++++++++++++++++ src/sim65/addrspace.h | 99 +++++++++++++++++++++ src/sim65/cfgdata.c | 16 +++- src/sim65/cfgdata.h | 5 ++ src/sim65/chip.c | 3 +- src/sim65/chip.h | 24 +++--- src/sim65/chipif.h | 8 +- src/sim65/chips/ram.c | 19 ++++- src/sim65/config.c | 141 +----------------------------- src/sim65/cpucore.h | 6 +- src/sim65/cputype.c | 8 +- src/sim65/cputype.h | 10 +-- src/sim65/error.c | 8 +- src/sim65/error.h | 8 +- src/sim65/global.c | 6 +- src/sim65/global.h | 6 +- src/sim65/location.c | 174 +++++++++++++++++++++++++++++++++++++ src/sim65/location.h | 97 +++++++++++++++++++++ src/sim65/make/gcc.mak | 4 +- src/sim65/simdata.h | 8 +- 20 files changed, 653 insertions(+), 187 deletions(-) create mode 100644 src/sim65/addrspace.c create mode 100644 src/sim65/addrspace.h create mode 100644 src/sim65/location.c create mode 100644 src/sim65/location.h diff --git a/src/sim65/addrspace.c b/src/sim65/addrspace.c new file mode 100644 index 000000000..1eda9f76a --- /dev/null +++ b/src/sim65/addrspace.c @@ -0,0 +1,190 @@ +/*****************************************************************************/ +/* */ +/* addrspace.c */ +/* */ +/* CPU address space for the 6502 simulator */ +/* */ +/* */ +/* */ +/* (C) 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. */ +/* */ +/*****************************************************************************/ + + + +/* common */ +#include "check.h" +#include "xmalloc.h" + +/* sim65 */ +#include "chip.h" +#include "cpucore.h" +#include "error.h" +#include "addrspace.h" + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +AddressSpace* NewAddressSpace (unsigned Size) +/* Allocate a new address space and return it */ +{ + unsigned I; + + /* Allocate memory */ + AddressSpace* AS = xmalloc (sizeof (AddressSpace) + + (Size - 1) * sizeof (ChipInstance*)); + + /* Initialize the struct */ + AS->CPU = 0; + AS->Size = Size; + for (I = 0; I < Size; ++I) { + AS->Data[I] = 0; + } + + /* Return the new struct */ + return AS; +} + + + +void ASWrite (AddressSpace* AS, unsigned Addr, unsigned char Val) +/* Write a byte to a given location */ +{ + const ChipInstance* CI; + + /* Make sure, the addresses are in a valid range */ + PRECONDITION (Addr < AS->Size); + + /* Get the instance of the chip at this address */ + CI = AS->Data[Addr]; + + /* Check if the memory is mapped */ + if (CI == 0) { + Break ("Writing to unassigned memory at $%06X", Addr); + } else { + CI->C->Data->Write (CI->Data, Addr - CI->Addr, Val); + } +} + + + +unsigned char ASRead (AddressSpace* AS, unsigned Addr) +/* Read a byte from a location */ +{ + const ChipInstance* CI; + + /* Make sure, the addresses are in a valid range */ + PRECONDITION (Addr < AS->Size); + + /* Get the instance of the chip at this address */ + CI = AS->Data[Addr]; + + /* Check if the memory is mapped */ + if (CI == 0) { + Break ("Reading from unassigned memory at $%06X", Addr); + return 0xFF; + } else { + return CI->C->Data->Read (CI->Data, Addr - CI->Addr); + } +} + + + +void ASWriteCtrl (AddressSpace* AS, unsigned Addr, unsigned char Val) +/* Write a byte to a given location */ +{ + const ChipInstance* CI; + + /* Make sure, the addresses are in a valid range */ + PRECONDITION (Addr < AS->Size); + + /* Get the instance of the chip at this address */ + CI = AS->Data[Addr]; + + /* Check if the memory is mapped */ + if (CI == 0) { + Break ("Writing to unassigned memory at $%06X", Addr); + } else { + CI->C->Data->WriteCtrl (CI->Data, Addr - CI->Addr, Val); + } +} + + + +unsigned char ASReadCtrl (AddressSpace* AS, unsigned Addr) +/* Read a byte from a location */ +{ + const ChipInstance* CI; + + /* Make sure, the addresses are in a valid range */ + PRECONDITION (Addr < AS->Size); + + /* Get the instance of the chip at this address */ + CI = AS->Data[Addr]; + + /* Check if the memory is mapped */ + if (CI == 0) { + Break ("Reading from unassigned memory at $%06X", Addr); + return 0xFF; + } else { + return CI->C->Data->ReadCtrl (CI->Data, Addr - CI->Addr); + } +} + + +void ASAssignChip (AddressSpace* AS, ChipInstance* CI, + unsigned Addr, unsigned Range) +/* Assign a chip instance to memory locations */ +{ + /* Make sure, the addresses are in a valid range */ + PRECONDITION (Addr + Range <= AS->Size); + + /* Assign the chip instance */ + while (Range--) { + CHECK (AS->Data[Addr] == 0); + AS->Data[Addr++] = CI; + } + + /* Set the backpointer to us */ + CI->AS = AS; +} + + +ChipInstance* ASGetChip (const AddressSpace* AS, unsigned Addr) +/* Get the chip that is located at the given address (may return NULL). */ +{ + /* Make sure, the addresses are in a valid range */ + PRECONDITION (Addr < AS->Size); + + /* Return the chip instance */ + return AS->Data[Addr]; +} + + + diff --git a/src/sim65/addrspace.h b/src/sim65/addrspace.h new file mode 100644 index 000000000..b353068e4 --- /dev/null +++ b/src/sim65/addrspace.h @@ -0,0 +1,99 @@ +/*****************************************************************************/ +/* */ +/* addrspace.h */ +/* */ +/* CPU address space for the 6502 simulator */ +/* */ +/* */ +/* */ +/* (C) 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. */ +/* */ +/*****************************************************************************/ + + + +#ifndef ADDRSPACE_H +#define ADDRSPACE_H + + + +/*****************************************************************************/ +/* Data */ +/*****************************************************************************/ + + + +/* Forwards */ +struct CPUData; +struct ChipInstance; + +/* Forwards */ +typedef struct AddressSpace AddressSpace; +struct AddressSpace { + + struct CPU* CPU; /* Backpointer to CPU */ + unsigned Size; /* Address space size */ + struct ChipInstance* Data[1]; /* Pointer to chips, dynamically! */ + +}; + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +AddressSpace* NewAddressSpace (unsigned Size); +/* Allocate a new address space and return it */ + +void ASWrite (AddressSpace* AS, unsigned Addr, unsigned char Val); +/* Write a byte to a given location */ + +unsigned char ASRead (AddressSpace* AS, unsigned Addr); +/* Read a byte from a location */ + +void ASWriteCtrl (AddressSpace* AS, unsigned Addr, unsigned char Val); +/* Write a byte to a given location */ + +unsigned char ASReadCtrl (AddressSpace* AS, unsigned Addr); +/* Read a byte from a location */ + +void ASAssignChip (AddressSpace* AS, struct ChipInstance* CI, + unsigned Addr, unsigned Range); +/* Assign a chip instance to memory locations */ + +struct ChipInstance* ASGetChip (const AddressSpace* AS, unsigned Addr); +/* Get the chip that is located at the given address (may return NULL). */ + + + +/* End of addrspace.h */ + +#endif + + + + diff --git a/src/sim65/cfgdata.c b/src/sim65/cfgdata.c index f603c8e28..fc1d428bd 100644 --- a/src/sim65/cfgdata.c +++ b/src/sim65/cfgdata.c @@ -40,6 +40,7 @@ #include "xmalloc.h" /* sim65 */ +#include "error.h" #include "scanner.h" #include "cfgdata.h" @@ -87,6 +88,19 @@ void FreeCfgData (CfgData* D) +void CfgDataCheckType (const CfgData* D, unsigned Type) +/* Check the config data type and print an error message if it has the wrong + * type. + */ +{ + if (D->Type != Type) { + Error ("%s(%u): Attribute `%s' has invalid type", + CfgGetName (), D->Line, D->Attr); + } +} + + + int CfgDataFind (const Collection* Attributes, const char* AttrName) /* Find the attribute with the given name and return its index. Return -1 if * the attribute was not found. @@ -108,7 +122,7 @@ int CfgDataFind (const Collection* Attributes, const char* AttrName) } /* Not found */ - return -1; + return -1; } diff --git a/src/sim65/cfgdata.h b/src/sim65/cfgdata.h index b31234e64..c86522f72 100644 --- a/src/sim65/cfgdata.h +++ b/src/sim65/cfgdata.h @@ -82,6 +82,11 @@ CfgData* NewCfgData (void); void FreeCfgData (CfgData* D); /* Free a config data structure */ +void CfgDataCheckType (const CfgData* D, unsigned Type); +/* Check the config data type and print an error message if it has the wrong + * type. + */ + int CfgDataFind (const Collection* Attributes, const char* AttrName); /* Find the attribute with the given name and return its index. Return -1 if * the attribute was not found. diff --git a/src/sim65/chip.c b/src/sim65/chip.c index 80dd42cb8..5d33c141b 100644 --- a/src/sim65/chip.c +++ b/src/sim65/chip.c @@ -246,7 +246,7 @@ static Chip* NewChip (ChipLibrary* Library, const ChipData* Data) Chip* C = xmalloc (sizeof (Chip)); /* Initialize the fields */ - C->Library = Library; + C->Lib = Library; C->Data = Data; C->Instances = EmptyCollection; @@ -275,6 +275,7 @@ ChipInstance* NewChipInstance (const char* ChipName, unsigned Addr, /* Initialize the fields */ CI->C = C; + CI->AS = 0; CI->Addr = Addr; CI->Size = Size; CI->Data = C->Data->InitInstance (Addr, Size, Attributes); diff --git a/src/sim65/chip.h b/src/sim65/chip.h index 12817b28c..f614c0ced 100644 --- a/src/sim65/chip.h +++ b/src/sim65/chip.h @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 2002 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (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 */ @@ -54,6 +54,7 @@ /* Forwards */ +struct AddressSpace; struct CfgData; typedef struct ChipInstance ChipInstance; typedef struct Chip Chip; @@ -61,17 +62,18 @@ typedef struct ChipLibrary ChipLibrary; /* One instance of a chip */ struct ChipInstance { - Chip* C; /* Pointer to corresponding chip */ - unsigned Addr; /* Start address of range */ - unsigned Size; /* Size of range */ - void* Data; /* Chip instance data */ + Chip* C; /* Pointer to corresponding chip */ + struct AddressSpace* AS; /* Pointer to address space */ + unsigned Addr; /* Start address of range */ + unsigned Size; /* Size of range */ + void* Data; /* Chip instance data */ }; /* Chip structure */ struct Chip { - struct ChipLibrary* Library; /* Pointer to library data structure */ - const ChipData* Data; /* Chip data as given by the library */ - Collection Instances; /* Pointer to chip instances */ + struct ChipLibrary* Lib; /* Pointer to library data structure */ + const ChipData* Data; /* Chip data as given by the library */ + Collection Instances; /* Pointer to chip instances */ }; /* ChipLibrary structure */ diff --git a/src/sim65/chipif.h b/src/sim65/chipif.h index 1badc3696..c9111aeb7 100644 --- a/src/sim65/chipif.h +++ b/src/sim65/chipif.h @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 2002 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (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 */ diff --git a/src/sim65/chips/ram.c b/src/sim65/chips/ram.c index 5a77bc744..91ebc421f 100644 --- a/src/sim65/chips/ram.c +++ b/src/sim65/chips/ram.c @@ -148,6 +148,8 @@ static int InitChip (const struct SimData* Data) static void* InitInstance (unsigned Addr, unsigned Range, void* CfgInfo) /* Initialize a new chip instance */ { + long Val; + /* Allocate a new instance structure */ InstanceData* D = Sim->Malloc (sizeof (InstanceData)); @@ -157,9 +159,22 @@ static void* InitInstance (unsigned Addr, unsigned Range, void* CfgInfo) D->MemAttr = Sim->Malloc (Range * sizeof (D->MemAttr[0])); D->Mem = Sim->Malloc (Range * sizeof (D->Mem[0])); - /* Clear the RAM and attribute memory */ + /* Check if a value is given that should be used to clear the RAM. + * Otherwise use random values. + */ + if (Sim->GetCfgNum (CfgInfo, "fill", &Val) == 0) { + /* No "fill" attribute */ + unsigned I; + for (I = 0; I < Range; ++I) { + D->Mem[I] = (unsigned char) rand (); + } + } else { + /* Got a "fill" attribute */ + memset (D->Mem, (int) Val, Range); + } + + /* Clear the 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; diff --git a/src/sim65/config.c b/src/sim65/config.c index ce1a272f8..764319297 100644 --- a/src/sim65/config.c +++ b/src/sim65/config.c @@ -51,117 +51,12 @@ #include "error.h" #include "global.h" #include "memory.h" +#include "location.h" #include "scanner.h" #include "config.h" -/*****************************************************************************/ -/* Data */ -/*****************************************************************************/ - - - -/* List of all memory locations */ -static Collection Locations; - -/* One memory location */ -typedef struct Location Location; -struct Location { - unsigned Start; /* Start of memory location */ - unsigned End; /* End memory location */ - Collection Attributes; /* Attributes given */ - unsigned Line; /* Line in config file */ - unsigned Col; /* Column in config file */ -}; - - - -/*****************************************************************************/ -/* struct CfgData */ -/*****************************************************************************/ - - - -static void CfgDataCheckType (const CfgData* D, unsigned Type) -/* Check the config data type */ -{ - if (D->Type != Type) { - Error ("%s(%u): Attribute `%s' has invalid type", - CfgGetName (), D->Line, D->Attr); - } -} - - - -/*****************************************************************************/ -/* struct Location */ -/*****************************************************************************/ - - - -static Location* NewLocation (unsigned long Start, unsigned long End) -/* Create a new location, initialize and return it */ -{ - /* Allocate memory */ - Location* L = xmalloc (sizeof (Location)); - - /* Initialize the fields */ - L->Start = Start; - L->End = End; - L->Attributes = EmptyCollection; - L->Line = CfgErrorLine; - L->Col = CfgErrorCol; - - /* Return the new struct */ - return L; -} - - - -static int CmpLocations (void* Data attribute ((unused)), - const void* lhs, const void* rhs) -/* Compare function for CollSort */ -{ - /* Cast the object pointers */ - const Location* Left = (const Location*) rhs; - const Location* Right = (const Location*) lhs; - - /* Do the compare */ - if (Left->Start < Right->Start) { - return 1; - } else if (Left->Start > Right->Start) { - return -1; - } else { - return 0; - } -} - - - -static int LocationGetAttr (const Location* L, const char* AttrName) -/* Find the attribute with the given name and return it. Call Error() if the - * attribute was not found. - */ -{ - int I = CfgDataFind (&L->Attributes, AttrName); - if (I < 0) { - Error ("%s(%u): Attribute `%s' missing", CfgGetName(), L->Line, AttrName); - } - return I; -} - - - -static int LocationIsMirror (const Location* L) -/* Return true if the given location is a mirror of another one. */ -{ - /* Find the "mirror" attribute */ - return (CfgDataFind (&L->Attributes, "mirror") >= 0); -} - - - /*****************************************************************************/ /* Code */ /*****************************************************************************/ @@ -172,7 +67,6 @@ static void ParseMemory (void) /* Parse a MEMORY section */ { unsigned I; - const Location* Last; while (CfgTok == CFGTOK_INTCON) { @@ -245,37 +139,10 @@ static void ParseMemory (void) } /* Sort all memory locations */ - CollSort (&Locations, CmpLocations, 0); + LocationSort (&Locations); - /* Check for overlaps and other problems */ - Last = 0; - for (I = 0; I < CollCount (&Locations); ++I) { - - /* Get this location */ - const Location* L = CollAtUnchecked (&Locations, I); - - /* Check for an overlap with the following location */ - if (Last && Last->End >= L->Start) { - Error ("%s(%u): Address range overlap (overlapping entry is in line %u)", - CfgGetName(), L->Line, Last->Line); - } - - /* If the location is a mirror, it must not have other attributes, - * and the mirror attribute must be an integer. - */ - if (LocationIsMirror (L)) { - const CfgData* D; - if (CollCount (&L->Attributes) > 1) { - Error ("%s(%u): Location at address $%06X is a mirror " - "but has attributes", CfgGetName(), L->Line, L->Start); - } - D = CollConstAt (&L->Attributes, 0); - CfgDataCheckType (D, CfgDataNumber); - } - - /* Remember this entry */ - Last = L; - } + /* Check the locations for overlaps and other problems */ + LocationCheck (&Locations); /* Now create the chip instances. Since we can only mirror existing chips, * we will first create all real chips and the mirrors in a second run. diff --git a/src/sim65/cpucore.h b/src/sim65/cpucore.h index f44431199..6d98ea9ac 100644 --- a/src/sim65/cpucore.h +++ b/src/sim65/cpucore.h @@ -6,9 +6,9 @@ /* */ /* */ /* */ -/* (C) 2002 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ +/* (C) 2002-2003 Ullrich von Bassewitz */ +/* Römerstrasse 52 */ +/* D-70794 Filderstadt */ /* EMail: uz@cc65.org */ /* */ /* */ diff --git a/src/sim65/cputype.c b/src/sim65/cputype.c index 76903a174..5d56557f4 100644 --- a/src/sim65/cputype.c +++ b/src/sim65/cputype.c @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 2002 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (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 */ diff --git a/src/sim65/cputype.h b/src/sim65/cputype.h index c83e0e6fa..b66b060f7 100644 --- a/src/sim65/cputype.h +++ b/src/sim65/cputype.h @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 2002 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (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 */ @@ -61,4 +61,4 @@ extern CPUType CPU; - + diff --git a/src/sim65/error.c b/src/sim65/error.c index 99d460da9..d5b9524b8 100644 --- a/src/sim65/error.c +++ b/src/sim65/error.c @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 2002 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (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 */ diff --git a/src/sim65/error.h b/src/sim65/error.h index e67036d37..92d4cb253 100644 --- a/src/sim65/error.h +++ b/src/sim65/error.h @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 2002 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (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 */ diff --git a/src/sim65/global.c b/src/sim65/global.c index dac0c27b4..d52d506f1 100644 --- a/src/sim65/global.c +++ b/src/sim65/global.c @@ -6,9 +6,9 @@ /* */ /* */ /* */ -/* (C) 2002 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ +/* (C) 2002-2003 Ullrich von Bassewitz */ +/* Römerstrasse 52 */ +/* D-70794 Filderstadt */ /* EMail: uz@cc65.org */ /* */ /* */ diff --git a/src/sim65/global.h b/src/sim65/global.h index 5d08aaeb1..29c22aa29 100644 --- a/src/sim65/global.h +++ b/src/sim65/global.h @@ -6,9 +6,9 @@ /* */ /* */ /* */ -/* (C) 2002 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ +/* (C) 2002-2003 Ullrich von Bassewitz */ +/* Römerstrasse 52 */ +/* D-70794 Filderstadt */ /* EMail: uz@cc65.org */ /* */ /* */ diff --git a/src/sim65/location.c b/src/sim65/location.c new file mode 100644 index 000000000..f235bd99d --- /dev/null +++ b/src/sim65/location.c @@ -0,0 +1,174 @@ +/*****************************************************************************/ +/* */ +/* location.c */ +/* */ +/* Memory location description */ +/* */ +/* */ +/* */ +/* (C) 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. */ +/* */ +/*****************************************************************************/ + + + +/* common.h */ +#include "coll.h" +#include "xmalloc.h" + +/* sim65 */ +#include "cfgdata.h" +#include "error.h" +#include "scanner.h" +#include "location.h" + + + +/*****************************************************************************/ +/* Data */ +/*****************************************************************************/ + + + +/* List of all memory locations */ +Collection Locations = STATIC_COLLECTION_INITIALIZER; + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +Location* NewLocation (unsigned long Start, unsigned long End) +/* Create a new location, initialize and return it */ +{ + /* Allocate memory */ + Location* L = xmalloc (sizeof (Location)); + + /* Initialize the fields */ + L->Start = Start; + L->End = End; + L->Attributes = EmptyCollection; + L->Line = CfgErrorLine; + L->Col = CfgErrorCol; + + /* Return the new struct */ + return L; +} + + + +static int CmpLocations (void* Data attribute ((unused)), + const void* lhs, const void* rhs) +/* Compare function for CollSort */ +{ + /* Cast the object pointers */ + const Location* Left = (const Location*) rhs; + const Location* Right = (const Location*) lhs; + + /* Do the compare */ + if (Left->Start < Right->Start) { + return 1; + } else if (Left->Start > Right->Start) { + return -1; + } else { + return 0; + } +} + + + +int LocationGetAttr (const Location* L, const char* AttrName) +/* Find the attribute with the given name and return it. Call Error() if the + * attribute was not found. + */ +{ + int I = CfgDataFind (&L->Attributes, AttrName); + if (I < 0) { + Error ("%s(%u): Attribute `%s' missing", CfgGetName(), L->Line, AttrName); + } + return I; +} + + + +int LocationIsMirror (const Location* L) +/* Return true if the given location is a mirror of another one. */ +{ + /* Find the "mirror" attribute */ + return (CfgDataFind (&L->Attributes, "mirror") >= 0); +} + + + +void LocationSort (Collection* Locations) +/* Sort all locations by address */ +{ + /* Sort all memory locations */ + CollSort (Locations, CmpLocations, 0); +} + + + +void LocationCheck (const Collection* Locations) +/* Check all locations for problems */ +{ + unsigned I; + + /* Check for overlaps and other problems */ + const Location* Last = 0; + for (I = 0; I < CollCount (Locations); ++I) { + + /* Get this location */ + const Location* L = CollConstAt (Locations, I); + + /* Check for an overlap with the following location */ + if (Last && Last->End >= L->Start) { + Error ("%s(%u): Address range overlap (overlapping entry is in line %u)", + CfgGetName(), L->Line, Last->Line); + } + + /* If the location is a mirror, it must not have other attributes, + * and the mirror attribute must be an integer. + */ + if (LocationIsMirror (L)) { + const CfgData* D; + if (CollCount (&L->Attributes) > 1) { + Error ("%s(%u): Location at address $%06X is a mirror " + "but has attributes", CfgGetName(), L->Line, L->Start); + } + D = CollConstAt (&L->Attributes, 0); + CfgDataCheckType (D, CfgDataNumber); + } + + /* Remember this entry */ + Last = L; + } +} + + + + diff --git a/src/sim65/location.h b/src/sim65/location.h new file mode 100644 index 000000000..a695bf210 --- /dev/null +++ b/src/sim65/location.h @@ -0,0 +1,97 @@ +/*****************************************************************************/ +/* */ +/* location.h */ +/* */ +/* Memory location description */ +/* */ +/* */ +/* */ +/* (C) 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. */ +/* */ +/*****************************************************************************/ + + + +#ifndef LOCATION_H +#define LOCATION_H + + + +/* common.h */ +#include "coll.h" + + + +/*****************************************************************************/ +/* Data */ +/*****************************************************************************/ + + + +/* List of all memory locations */ +extern Collection Locations; + +/* One memory location */ +typedef struct Location Location; +struct Location { + unsigned Start; /* Start of memory location */ + unsigned End; /* End memory location */ + Collection Attributes; /* Attributes given */ + unsigned Line; /* Line in config file */ + unsigned Col; /* Column in config file */ +}; + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +Location* NewLocation (unsigned long Start, unsigned long End); +/* Create a new location, initialize and return it */ + +int LocationGetAttr (const Location* L, const char* AttrName); +/* Find the attribute with the given name and return it. Call Error() if the + * attribute was not found. + */ + +int LocationIsMirror (const Location* L); +/* Return true if the given location is a mirror of another one. */ + +void LocationSort (Collection* Locations); +/* Sort all locations by address */ + +void LocationCheck (const Collection* Locations); +/* Check all locations for problems */ + + + +/* End of location.h */ + +#endif + + + diff --git a/src/sim65/make/gcc.mak b/src/sim65/make/gcc.mak index b51eecf3c..b4ccf2c2a 100644 --- a/src/sim65/make/gcc.mak +++ b/src/sim65/make/gcc.mak @@ -10,7 +10,8 @@ CC = gcc EBIND = emxbind LDFLAGS = -OBJS = cfgdata.o \ +OBJS = addrspace.o \ + cfgdata.o \ chip.o \ chippath.o \ config.o \ @@ -18,6 +19,7 @@ OBJS = cfgdata.o \ cputype.o \ error.o \ global.o \ + location.o \ main.o \ memory.o \ scanner.o diff --git a/src/sim65/simdata.h b/src/sim65/simdata.h index c4d80ccb1..3a727b975 100644 --- a/src/sim65/simdata.h +++ b/src/sim65/simdata.h @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 2002 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (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 */