]> git.sur5r.net Git - cc65/blob - src/sim65/config.c
Added LOWCODE and STARTUP segments
[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 "cfgdata.h"
49 #include "chip.h"
50 #include "error.h"
51 #include "global.h"
52 #include "scanner.h"
53 #include "config.h"
54
55
56
57 /*****************************************************************************/
58 /*                              struct CfgData                               */
59 /*****************************************************************************/
60
61
62
63 static CfgData* NewCfgData (const char* Tok)
64 /* Create and intialize a new CfgData struct, then return it */
65 {
66     /* Allocate memory */
67     CfgData* D = xmalloc (sizeof (CfgData));
68
69     /* Initialize the fields */
70     D->Attr = xstrdup (Tok);
71     D->Type = Invalid;
72
73     /* Return the new struct */
74     return D;
75 }
76
77
78
79 /*****************************************************************************/
80 /*                                   Data                                    */
81 /*****************************************************************************/
82
83
84
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.
88  */
89 {
90     if (*Flags & Mask) {
91         CfgError ("%s is already defined", Name);
92     }
93     *Flags |= Mask;
94 }
95
96
97
98 static void AttrCheck (unsigned Attr, unsigned Mask, const char* Name)
99 /* Check that a mandatory attribute was given */
100 {
101     if ((Attr & Mask) == 0) {
102         CfgError ("%s attribute is missing", Name);
103     }
104 }
105
106
107
108 static void ParseChips (void)
109 /* Parse a CHIPS section */
110 {
111     static const IdentTok Attributes [] = {
112         {   "ADDR",     CFGTOK_ADDR     },
113         {   "RANGE",    CFGTOK_RANGE    },
114     };
115
116     /* Bits and stuff to remember which attributes we have read */
117     enum {
118         CA_ADDR  = 0x01,
119         CA_RANGE = 0x02
120     };
121     unsigned Attr;
122
123     /* Attribute values. Initialize to make gcc happy. */
124     const Chip* C;
125     unsigned Addr  = 0;
126     unsigned Range = 0;
127
128     while (CfgTok == CFGTOK_IDENT) {
129
130         /* Search the chip with the given name */
131         C = FindChip (CfgSVal);
132         if (C == 0) {
133             CfgError ("No such chip: `%s'", CfgSVal);
134         }
135
136         /* Skip the name plus the following colon */
137         CfgNextTok ();
138         CfgConsumeColon ();
139
140         /* Read the attributes */
141         Attr = 0;
142         while (CfgTok == CFGTOK_IDENT) {
143
144             /* Map the identifier to a token */
145             cfgtok_t AttrTok;
146             CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
147             AttrTok = CfgTok;
148
149             /* An optional assignment follows */
150             CfgNextTok ();
151             CfgOptionalAssign ();
152
153             /* Check which attribute was given */
154             switch (AttrTok) {
155
156                 case CFGTOK_ADDR:
157                     CfgAssureInt ();
158                     CfgRangeCheck (0, 0xFFFF);
159                     FlagAttr (&Attr, CA_ADDR, "ADDR");
160                     Addr = (unsigned) CfgIVal;
161                     break;
162
163                 case CFGTOK_RANGE:
164                     CfgAssureInt ();
165                     CfgRangeCheck (0, 0xFFFF);
166                     FlagAttr (&Attr, CA_RANGE, "RANGE");
167                     Range = (unsigned) CfgIVal;
168                     break;
169
170                 default:
171                     FAIL ("Unexpected attribute token");
172
173             }
174
175             /* Skip the attribute value and an optional comma */
176             CfgNextTok ();
177             CfgOptionalComma ();
178         }
179
180         /* Skip the semicolon */
181         CfgConsumeSemi ();
182
183         /* Check for mandatory parameters */
184         AttrCheck (Attr, CA_ADDR, "ADDR");
185         AttrCheck (Attr, CA_RANGE, "RANGE");
186
187         /* Address + Range may not exceed 16 bits */
188         if (((unsigned long) Range) > 0x10000UL - Addr) {
189             CfgError ("Range error");
190         }
191
192         /* Create the chip ## */
193
194     }
195 }
196
197
198
199 static void ParseConfig (void)
200 /* Parse the config file */
201 {
202     static const IdentTok BlockNames [] = {
203         {   "CHIPS",    CFGTOK_CHIPS    },
204     };
205     cfgtok_t BlockTok;
206
207     do {
208
209         /* Read the block ident */
210         CfgSpecialToken (BlockNames, ENTRY_COUNT (BlockNames), "Block identifier");
211         BlockTok = CfgTok;
212         CfgNextTok ();
213
214         /* Expected a curly brace */
215         CfgConsume (CFGTOK_LCURLY, "`{' expected");
216
217         /* Read the block */
218         switch (BlockTok) {
219
220             case CFGTOK_CHIPS:
221                 ParseChips ();
222                 break;
223
224             default:
225                 FAIL ("Unexpected block token");
226
227         }
228
229         /* Skip closing brace */
230         CfgConsume (CFGTOK_RCURLY, "`}' expected");
231
232     } while (CfgTok != CFGTOK_EOF);
233 }
234
235
236
237 void CfgRead (void)
238 /* Read the configuration */
239 {
240     /* If we have a config name given, open the file, otherwise we will read
241      * from a buffer.
242      */
243     CfgOpenInput ();
244
245     /* Parse the file */
246     ParseConfig ();
247
248     /* Close the input file */
249     CfgCloseInput ();
250 }
251
252
253