]> git.sur5r.net Git - cc65/blob - src/sim65/config.c
Working
[cc65] / src / sim65 / config.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 config.c                                  */
4 /*                                                                           */
5 /*            Configuration file parsing for the sim65 6502 simulator        */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2002 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40
41 /* common */
42 #include "check.h"
43 #include "bitops.h"
44 #include "print.h"
45 #include "xmalloc.h"
46
47 /* sim65 */
48 #include "chip.h"
49 #include "error.h"
50 #include "global.h"
51 #include "scanner.h"
52 #include "config.h"
53
54
55
56 /*****************************************************************************/
57 /*                                   Data                                    */
58 /*****************************************************************************/
59
60
61
62 static void FlagAttr (unsigned* Flags, unsigned Mask, const char* Name)
63 /* Check if the item is already defined. Print an error if so. If not, set
64  * the marker that we have a definition now.
65  */
66 {
67     if (*Flags & Mask) {
68         CfgError ("%s is already defined", Name);
69     }
70     *Flags |= Mask;
71 }
72
73
74
75 static void AttrCheck (unsigned Attr, unsigned Mask, const char* Name)
76 /* Check that a mandatory attribute was given */
77 {
78     if ((Attr & Mask) == 0) {
79         CfgError ("%s attribute is missing", Name);
80     }
81 }
82
83
84
85 static void ParseChips (void)
86 /* Parse a CHIPS section */
87 {
88     static const IdentTok Attributes [] = {
89         {   "ADDR",     CFGTOK_ADDR     },
90         {   "RANGE",    CFGTOK_RANGE    },
91     };
92
93     /* Bits and stuff to remember which attributes we have read */
94     enum {
95         CA_ADDR  = 0x01,
96         CA_RANGE = 0x02
97     };
98     unsigned Attr;
99
100     /* Attribute values. Initialize to make gcc happy. */
101     const Chip* C;
102     unsigned Addr  = 0;
103     unsigned Range = 0;
104
105     while (CfgTok == CFGTOK_IDENT) {
106
107         /* Search the chip with the given name */
108         C = FindChip (CfgSVal);
109         if (C == 0) {
110             CfgError ("No such chip: `%s'", CfgSVal);
111         }
112
113         /* Skip the name plus the following colon */
114         CfgNextTok ();
115         CfgConsumeColon ();
116
117         /* Read the attributes */
118         Attr = 0;
119         while (CfgTok == CFGTOK_IDENT) {
120
121             /* Map the identifier to a token */
122             cfgtok_t AttrTok;
123             CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
124             AttrTok = CfgTok;
125
126             /* An optional assignment follows */
127             CfgNextTok ();
128             CfgOptionalAssign ();
129
130             /* Check which attribute was given */
131             switch (AttrTok) {
132
133                 case CFGTOK_ADDR:
134                     CfgAssureInt ();
135                     CfgRangeCheck (0, 0xFFFF);
136                     FlagAttr (&Attr, CA_ADDR, "ADDR");
137                     Addr = (unsigned) CfgIVal;
138                     break;
139
140                 case CFGTOK_RANGE:
141                     CfgAssureInt ();
142                     CfgRangeCheck (0, 0xFFFF);
143                     FlagAttr (&Attr, CA_RANGE, "RANGE");
144                     Range = (unsigned) CfgIVal;
145                     break;
146
147                 default:
148                     FAIL ("Unexpected attribute token");
149
150             }
151
152             /* Skip the attribute value and an optional comma */
153             CfgNextTok ();
154             CfgOptionalComma ();
155         }
156
157         /* Skip the semicolon */
158         CfgConsumeSemi ();
159
160         /* Check for mandatory parameters */
161         AttrCheck (Attr, CA_ADDR, "ADDR");
162         AttrCheck (Attr, CA_RANGE, "RANGE");
163
164         /* Address + Range may not exceed 16 bits */
165         if (((unsigned long) Range) > 0x10000UL - Addr) {
166             CfgError ("Range error");
167         }
168
169         /* Create the chip ## */
170
171     }
172 }
173
174
175
176 static void ParseConfig (void)
177 /* Parse the config file */
178 {
179     static const IdentTok BlockNames [] = {
180         {   "CHIPS",    CFGTOK_CHIPS    },
181     };
182     cfgtok_t BlockTok;
183
184     do {
185
186         /* Read the block ident */
187         CfgSpecialToken (BlockNames, ENTRY_COUNT (BlockNames), "Block identifier");
188         BlockTok = CfgTok;
189         CfgNextTok ();
190
191         /* Expected a curly brace */
192         CfgConsume (CFGTOK_LCURLY, "`{' expected");
193
194         /* Read the block */
195         switch (BlockTok) {
196
197             case CFGTOK_CHIPS:
198                 ParseChips ();
199                 break;
200
201             default:
202                 FAIL ("Unexpected block token");
203
204         }
205
206         /* Skip closing brace */
207         CfgConsume (CFGTOK_RCURLY, "`}' expected");
208
209     } while (CfgTok != CFGTOK_EOF);
210 }
211
212
213
214 void CfgRead (void)
215 /* Read the configuration */
216 {
217     /* If we have a config name given, open the file, otherwise we will read
218      * from a buffer.
219      */
220     CfgOpenInput ();
221
222     /* Parse the file */
223     ParseConfig ();
224
225     /* Close the input file */
226     CfgCloseInput ();
227 }
228
229
230