]> git.sur5r.net Git - cc65/blob - src/sim65/config.c
Intermediate state - doesn't run as is.
[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-2012, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
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 "strutil.h"
46 #include "xmalloc.h"
47
48 /* sim65 */
49 #include "addrspace.h"
50 #include "cfgdata.h"
51 #include "chip.h"
52 #include "error.h"
53 #include "global.h"
54 #include "location.h"
55 #include "scanner.h"
56 #include "config.h"
57
58
59
60 /*****************************************************************************/
61 /*                                   Code                                    */
62 /*****************************************************************************/
63
64
65
66 static void FlagAttr (unsigned* Flags, unsigned Mask, const char* Name)
67 /* Check if the item is already defined. Print an error if so. If not, set
68  * the marker that we have a definition now.
69  */
70 {
71     if (*Flags & Mask) {
72         CfgError ("%s is already defined", Name);
73     }
74     *Flags |= Mask;
75 }
76
77
78
79 static void AttrCheck (unsigned Attr, unsigned Mask, const char* Name)
80 /* Check that a mandatory attribute was given */
81 {
82     if ((Attr & Mask) == 0) {
83         CfgError ("%s attribute is missing", Name);
84     }
85 }
86
87
88
89 static void ParseCPU (void)
90 /* Parse a CPU section */
91 {
92     static const IdentTok Attributes [] = {
93         {   "TYPE",             CFGTOK_TYPE             },
94         {   "ADDRSPACE",        CFGTOK_ADDRSPACE        },
95     };
96
97     enum {
98         atNone      = 0x0000,
99         atType      = 0x0001,
100         atAddrSpace = 0x0002
101     };
102     unsigned Attr = 0;
103     unsigned long Size = 0;
104
105
106     while (CfgTok == CFGTOK_IDENT) {
107
108         cfgtok_t AttrTok;
109         CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute");
110         AttrTok = CfgTok;
111
112         /* An optional assignment follows */
113         CfgNextTok ();
114         CfgOptionalAssign ();
115
116         /* Check which attribute was given */
117         switch (AttrTok) {
118
119             case CFGTOK_TYPE:
120                 FlagAttr (&Attr, atType, "TYPE");
121                 CfgAssureIdent ();
122                 /* ### */
123                 break;
124
125             case CFGTOK_ADDRSPACE:
126                 FlagAttr (&Attr, atAddrSpace, "ADDRSPACE");
127                 CfgAssureInt ();
128                 CfgRangeCheck (0x1000, 0x1000000);
129                 Size = CfgIVal;
130                 break;
131
132             default:
133                 FAIL ("Unexpected attribute token");
134
135         }
136
137         /* Skip the attribute value and an optional comma */
138         CfgNextTok ();
139         CfgOptionalComma ();
140     }
141
142     /* Must have some attributes */
143     AttrCheck (Attr, atType, "TYPE");
144     AttrCheck (Attr, atAddrSpace, "ADDRSPACE");
145
146     /* Create the CPU */
147     CPUInstance = NewCPU ("6502", Size);
148
149     /* Skip the semicolon */
150     CfgConsumeSemi ();
151 }
152
153
154
155 static void ParseAddrSpace (void)
156 /* Parse a ADDRSPACE section */
157 {
158     unsigned I;
159
160     /* CPU must be defined before the address space */
161     if (CPUInstance == 0) {
162         CfgError ("CPU must be defined before address space definitions");
163     }
164
165     /* Parse addresses */
166     while (CfgTok == CFGTOK_INTCON) {
167
168         Location* L;
169
170         /* Remember the start address and skip it */
171         unsigned long Start = CfgIVal;
172         CfgNextTok ();
173
174         /* .. must follow */
175         CfgConsume (CFGTOK_DOTDOT, "`..' expected");
176
177         /* End address must follow and must be greater than start */
178         CfgAssureInt ();
179         if (CfgIVal < Start) {
180             CfgError ("Start address must be greater than end address");
181         }
182
183         /* Create a new location and add it to the list */
184         L = NewLocation (Start, CfgIVal);
185         CollAppend (&Locations, L);
186
187         /* Skip the end address and the following colon */
188         CfgNextTok ();
189         CfgConsumeColon ();
190
191         /* Parse attributes terminated by a semicolon */
192         while (CfgTok == CFGTOK_IDENT) {
193
194             /* Generate a new attribute with the given name, then skip it */
195             CfgData* D = NewCfgData ();
196             CfgNextTok ();
197
198             /* An optional assignment follows */
199             CfgOptionalAssign ();
200
201             /* Check and assign the attribute value */
202             switch (CfgTok) {
203
204                 case CFGTOK_INTCON:
205                     D->Type   = CfgDataNumber;
206                     D->V.IVal = CfgIVal;
207                     break;
208
209                 case CFGTOK_STRCON:
210                     D->Type   = CfgDataString;
211                     D->V.SVal = xstrdup (CfgSVal);
212                     break;
213
214                 case CFGTOK_IDENT:
215                     D->Type   = CfgDataId;
216                     D->V.SVal = xstrdup (CfgSVal);
217                     break;
218
219                 default:
220                     CfgError ("Invalid attribute type");
221             }
222
223             /* Add the attribute to the location */
224             CollAppend (&L->Attributes, D);
225
226             /* Skip the attribute value and an optional comma */
227             CfgNextTok ();
228             CfgOptionalComma ();
229         }
230
231         /* Skip the semicolon */
232         CfgConsumeSemi ();
233     }
234
235     /* Sort all memory locations */
236     LocationSort (&Locations);
237
238     /* Check the locations for overlaps and other problems */
239     LocationCheck (&Locations);
240
241     /* Now create the chip instances. Since we can only mirror existing chips,
242      * we will first create all real chips and the mirrors in a second run.
243      */
244     for (I = 0; I < CollCount (&Locations); ++I) {
245
246         int Index;
247         CfgData* D;
248         unsigned Range;         /* Address range for this chip */
249         ChipInstance* CI;
250
251         /* Get this location */
252         Location* L = CollAtUnchecked (&Locations, I);
253
254         /* Skip mirrors */
255         if (LocationIsMirror (L)) {
256             continue;
257         }
258
259         /* The chip must have an attribute "name" of type string */
260         Index = LocationGetAttr (L, "name");
261         D = CollAt (&L->Attributes, Index);
262         CfgDataCheckType (D, CfgDataString);
263
264         /* Remove the "name" attribute from the attribute list */
265         CollDelete (&L->Attributes, Index);
266
267         /* Create the chip instance for the address range */
268         Range = L->End - L->Start + 1;
269         CI = NewChipInstance (D->V.SVal, L->Start, Range, &L->Attributes);
270
271         /* Delete the "name" attribute */
272         FreeCfgData (D);
273
274         /* Assign the chip instance to address space */
275         ASAssignChip (CPUInstance->AS, CI, L->Start, Range);
276     }
277
278     /* Create the mirrors */
279     for (I = 0; I < CollCount (&Locations); ++I) {
280
281         const CfgData* D;
282         unsigned MirrorAddr;    /* Mirror address */
283         unsigned Range;         /* Address range for this chip */
284         unsigned Offs;          /* Offset of the mirror */
285         const ChipInstance* CI; /* Original chip instance */
286         ChipInstance* MCI;      /* Mirrored chip instance */
287
288         /* Get this location */
289         const Location* L = CollAtUnchecked (&Locations, I);
290
291         /* Skip non mirrors */
292         if (!LocationIsMirror (L)) {
293             continue;
294         }
295
296         /* Calculate the address range */
297         Range = L->End - L->Start;
298
299         /* Get the mirror address */
300         D = CollConstAt (&L->Attributes, 0);
301         MirrorAddr = (unsigned) D->V.IVal;
302
303         /* For simplicity, get the chip instance we're mirroring from the
304          * memory, instead of searching for the range in the list.
305          */
306         CI = ASGetChip (MirrorAddr);
307         if (CI == 0) {
308             /* We are mirroring an unassigned address */
309             Error ("%s(%u): Mirroring an unassigned address",
310                    CfgGetName (), L->Line);
311         }
312
313         /* Make sure we're mirroring the correct chip */
314         CHECK (MirrorAddr >= CI->Addr && MirrorAddr < CI->Addr + CI->Size);
315
316         /* Calculate the offset of the mirror */
317         Offs = MirrorAddr - CI->Addr;
318
319         /* Check if the mirror range is ok */
320         if (Offs + Range > CI->Size) {
321             Error ("%s(%u): Mirror range is too large", CfgGetName (), L->Line);
322         }
323
324         /* Clone the chip instance for the new location */
325         MCI = MirrorChipInstance (CI, L->Start - Offs);
326
327         /* Assign the chip instance to address space */
328         ASAssignChip (CPUInstance->AS, MCI, L->Start, Range);
329     }
330 }
331
332
333
334 static void ParseConfig (void)
335 /* Parse the config file */
336 {
337     static const IdentTok BlockNames [] = {
338         {   "ADDRSPACE",        CFGTOK_ADDRSPACE        },
339         {   "CPU",              CFGTOK_CPU              },
340     };
341     cfgtok_t BlockTok;
342
343     do {
344
345         /* Read the block ident */
346         CfgSpecialToken (BlockNames, ENTRY_COUNT (BlockNames), "Block identifier");
347         BlockTok = CfgTok;
348         CfgNextTok ();
349
350         /* Expected a curly brace */
351         CfgConsume (CFGTOK_LCURLY, "`{' expected");
352
353         /* Read the block */
354         switch (BlockTok) {
355
356             case CFGTOK_ADDRSPACE:
357                 ParseAddrSpace ();
358                 break;
359
360             case CFGTOK_CPU:
361                 ParseCPU ();
362                 break;
363
364             default:
365                 FAIL ("Unexpected block token");
366
367         }
368
369         /* Skip closing brace */
370         CfgConsumeRCurly ();
371
372     } while (CfgTok != CFGTOK_EOF);
373 }
374
375
376
377 void CfgRead (void)
378 /* Read the configuration */
379 {
380     /* If we have a config name given, open the file, otherwise we will read
381      * from a buffer.
382      */
383     CfgOpenInput ();
384
385     /* Parse the file */
386     ParseConfig ();
387
388     /* Close the input file */
389     CfgCloseInput ();
390 }
391
392
393