1 /*****************************************************************************/
5 /* Configuration file parsing for the sim65 6502 simulator */
9 /* (C) 1998-2002 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
57 /*****************************************************************************/
59 /*****************************************************************************/
63 static CfgData* NewCfgData (const char* Tok)
64 /* Create and intialize a new CfgData struct, then return it */
67 CfgData* D = xmalloc (sizeof (CfgData));
69 /* Initialize the fields */
70 D->Attr = xstrdup (Tok);
73 /* Return the new struct */
79 /*****************************************************************************/
81 /*****************************************************************************/
85 static void FlagAttr (unsigned* Flags, unsigned Mask, const char* Name)
86 /* Check if the item is already defined. Print an error if so. If not, set
87 * the marker that we have a definition now.
91 CfgError ("%s is already defined", Name);
98 static void AttrCheck (unsigned Attr, unsigned Mask, const char* Name)
99 /* Check that a mandatory attribute was given */
101 if ((Attr & Mask) == 0) {
102 CfgError ("%s attribute is missing", Name);
108 static void ParseChips (void)
109 /* Parse a CHIPS section */
111 static const IdentTok Attributes [] = {
112 { "ADDR", CFGTOK_ADDR },
113 { "RANGE", CFGTOK_RANGE },
116 /* Bits and stuff to remember which attributes we have read */
123 /* Attribute values. Initialize to make gcc happy. */
128 while (CfgTok == CFGTOK_IDENT) {
130 /* Search the chip with the given name */
131 C = FindChip (CfgSVal);
133 CfgError ("No such chip: `%s'", CfgSVal);
136 /* Skip the name plus the following colon */
140 /* Read the attributes */
142 while (CfgTok == CFGTOK_IDENT) {
144 /* Map the identifier to a token */
146 CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
149 /* An optional assignment follows */
151 CfgOptionalAssign ();
153 /* Check which attribute was given */
158 CfgRangeCheck (0, 0xFFFF);
159 FlagAttr (&Attr, CA_ADDR, "ADDR");
160 Addr = (unsigned) CfgIVal;
165 CfgRangeCheck (0, 0xFFFF);
166 FlagAttr (&Attr, CA_RANGE, "RANGE");
167 Range = (unsigned) CfgIVal;
171 FAIL ("Unexpected attribute token");
175 /* Skip the attribute value and an optional comma */
180 /* Skip the semicolon */
183 /* Check for mandatory parameters */
184 AttrCheck (Attr, CA_ADDR, "ADDR");
185 AttrCheck (Attr, CA_RANGE, "RANGE");
187 /* Address + Range may not exceed 16 bits */
188 if (((unsigned long) Range) > 0x10000UL - Addr) {
189 CfgError ("Range error");
192 /* Create the chip ## */
199 static void ParseConfig (void)
200 /* Parse the config file */
202 static const IdentTok BlockNames [] = {
203 { "CHIPS", CFGTOK_CHIPS },
209 /* Read the block ident */
210 CfgSpecialToken (BlockNames, ENTRY_COUNT (BlockNames), "Block identifier");
214 /* Expected a curly brace */
215 CfgConsume (CFGTOK_LCURLY, "`{' expected");
225 FAIL ("Unexpected block token");
229 /* Skip closing brace */
230 CfgConsume (CFGTOK_RCURLY, "`}' expected");
232 } while (CfgTok != CFGTOK_EOF);
238 /* Read the configuration */
240 /* If we have a config name given, open the file, otherwise we will read
248 /* Close the input file */