]> git.sur5r.net Git - cc65/blob - src/sim65/config.c
Fixed a bug
[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-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 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 "cfgdata.h"
50 #include "chip.h"
51 #include "error.h"
52 #include "global.h"
53 #include "memory.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 ParseMemory (void)
67 /* Parse a MEMORY section */
68 {
69     unsigned I;
70
71
72     while (CfgTok == CFGTOK_INTCON) {
73
74         Location* L;
75
76         /* Remember the start address and skip it */
77         unsigned long Start = CfgIVal;
78         CfgNextTok ();
79
80         /* .. must follow */
81         CfgConsume (CFGTOK_DOTDOT, "`..' expected");
82
83         /* End address must follow and must be greater than start */
84         CfgAssureInt ();
85         if (CfgIVal < Start) {
86             CfgError ("Start address must be greater than end address");
87         }
88
89         /* Create a new location and add it to the list */
90         L = NewLocation (Start, CfgIVal);
91         CollAppend (&Locations, L);
92
93         /* Skip the end address and the following colon */
94         CfgNextTok ();
95         CfgConsumeColon ();
96
97         /* Parse attributes terminated by a semicolon */
98         while (CfgTok == CFGTOK_IDENT) {
99
100             /* Generate a new attribute with the given name, then skip it */
101             CfgData* D = NewCfgData ();
102             CfgNextTok ();
103
104             /* An optional assignment follows */
105             CfgOptionalAssign ();
106
107             /* Check and assign the attribute value */
108             switch (CfgTok) {
109
110                 case CFGTOK_INTCON:
111                     D->Type   = CfgDataNumber;
112                     D->V.IVal = CfgIVal;
113                     break;
114
115                 case CFGTOK_STRCON:
116                     D->Type   = CfgDataString;
117                     D->V.SVal = xstrdup (CfgSVal);
118                     break;
119
120                 case CFGTOK_IDENT:
121                     D->Type   = CfgDataId;
122                     D->V.SVal = xstrdup (CfgSVal);
123                     break;
124
125                 default:
126                     CfgError ("Invalid attribute type");
127             }
128
129             /* Add the attribute to the location */
130             CollAppend (&L->Attributes, D);
131
132             /* Skip the attribute value and an optional comma */
133             CfgNextTok ();
134             CfgOptionalComma ();
135         }
136
137         /* Skip the semicolon */
138         CfgConsumeSemi ();
139     }
140
141     /* Sort all memory locations */
142     LocationSort (&Locations);
143
144     /* Check the locations for overlaps and other problems */
145     LocationCheck (&Locations);
146
147     /* Now create the chip instances. Since we can only mirror existing chips,
148      * we will first create all real chips and the mirrors in a second run.
149      */
150     for (I = 0; I < CollCount (&Locations); ++I) {
151
152         int Index;
153         CfgData* D;
154         unsigned Range;         /* Address range for this chip */
155         ChipInstance* CI;
156
157         /* Get this location */
158         Location* L = CollAtUnchecked (&Locations, I);
159
160         /* Skip mirrors */
161         if (LocationIsMirror (L)) {
162             continue;
163         }
164
165         /* The chip must have an attribute "name" of type string */
166         Index = LocationGetAttr (L, "name");
167         D = CollAt (&L->Attributes, Index);
168         CfgDataCheckType (D, CfgDataString);
169
170         /* Remove the "name" attribute from the attribute list */
171         CollDelete (&L->Attributes, Index);
172
173         /* Create the chip instance for the address range */
174         Range = L->End - L->Start + 1;
175         CI = NewChipInstance (D->V.SVal, L->Start, Range, &L->Attributes);
176
177         /* Delete the "name" attribute */
178         FreeCfgData (D);
179
180         /* Assign the chip instance to memory */
181         MemAssignChip (CI, L->Start, Range);
182     }
183
184     /* Create the mirrors */
185     for (I = 0; I < CollCount (&Locations); ++I) {
186
187         const CfgData* D;
188         unsigned MirrorAddr;    /* Mirror address */
189         unsigned Range;         /* Address range for this chip */
190         unsigned Offs;          /* Offset of the mirror */
191         const ChipInstance* CI; /* Original chip instance */
192         ChipInstance* MCI;      /* Mirrored chip instance */
193
194         /* Get this location */
195         const Location* L = CollAtUnchecked (&Locations, I);
196
197         /* Skip non mirrors */
198         if (!LocationIsMirror (L)) {
199             continue;
200         }
201
202         /* Calculate the address range */
203         Range = L->End - L->Start;
204
205         /* Get the mirror address */
206         D = CollConstAt (&L->Attributes, 0);
207         MirrorAddr = (unsigned) D->V.IVal;
208
209         /* For simplicity, get the chip instance we're mirroring from the
210          * memory, instead of searching for the range in the list.
211          */
212         CI = MemGetChip (MirrorAddr);
213         if (CI == 0) {
214             /* We are mirroring an unassigned address */
215             Error ("%s(%u): Mirroring an unassigned address",
216                    CfgGetName (), L->Line);
217         }
218
219         /* Make sure we're mirroring the correct chip */
220         CHECK (MirrorAddr >= CI->Addr && MirrorAddr < CI->Addr + CI->Size);
221
222         /* Calculate the offset of the mirror */
223         Offs = MirrorAddr - CI->Addr;
224
225         /* Check if the mirror range is ok */
226         if (Offs + Range > CI->Size) {
227             Error ("%s(%u): Mirror range is too large", CfgGetName (), L->Line);
228         }
229
230         /* Clone the chip instance for the new location */
231         MCI = MirrorChipInstance (CI, L->Start - Offs);
232
233         /* Assign the chip instance to memory */
234         MemAssignChip (MCI, L->Start, Range);
235     }
236 }
237
238
239
240 static void ParseConfig (void)
241 /* Parse the config file */
242 {
243     static const IdentTok BlockNames [] = {
244         {   "MEMORY",   CFGTOK_MEMORY   },
245     };
246     cfgtok_t BlockTok;
247
248     do {
249
250         /* Read the block ident */
251         CfgSpecialToken (BlockNames, ENTRY_COUNT (BlockNames), "Block identifier");
252         BlockTok = CfgTok;
253         CfgNextTok ();
254
255         /* Expected a curly brace */
256         CfgConsume (CFGTOK_LCURLY, "`{' expected");
257
258         /* Read the block */
259         switch (BlockTok) {
260
261             case CFGTOK_MEMORY:
262                 ParseMemory ();
263                 break;
264
265             default:
266                 FAIL ("Unexpected block token");
267
268         }
269
270         /* Skip closing brace */
271         CfgConsume (CFGTOK_RCURLY, "`}' expected");
272
273     } while (CfgTok != CFGTOK_EOF);
274 }
275
276
277
278 void CfgRead (void)
279 /* Read the configuration */
280 {
281     /* If we have a config name given, open the file, otherwise we will read
282      * from a buffer.
283      */
284     CfgOpenInput ();
285
286     /* Parse the file */
287     ParseConfig ();
288
289     /* Close the input file */
290     CfgCloseInput ();
291 }
292
293
294