]> git.sur5r.net Git - cc65/blob - src/sim65/main.c
Fixed a bug
[cc65] / src / sim65 / main.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  main.c                                   */
4 /*                                                                           */
5 /*                              sim65 main program                           */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2002-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 <string.h>
38 #include <stdlib.h>
39 #include <errno.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <dirent.h>
43 #include <unistd.h>
44
45 /* common */
46 #include "abend.h"
47 #include "cmdline.h"
48 #include "print.h"
49 #include "version.h"
50 #include "xmalloc.h"
51
52 /* sim65 */
53 #include "chip.h"
54 #include "chippath.h"
55 #include "config.h"
56 #include "cpucore.h"
57 #include "cputype.h"
58 #include "error.h"
59 #include "global.h"
60 #include "memory.h"
61 #include "scanner.h"
62
63
64
65 /*****************************************************************************/
66 /*                                   Code                                    */
67 /*****************************************************************************/
68
69
70
71 static void Usage (void)
72 {
73     fprintf (stderr,
74              "Usage: %s [options] file\n"
75              "Short options:\n"
76              "  -C name\t\tUse simulator config file\n"
77              "  -L dir\t\tSet a chip directory search path\n"
78              "  -V\t\t\tPrint the simulator version number\n"
79              "  -d\t\t\tDebug mode\n"
80              "  -h\t\t\tHelp (this text)\n"
81              "  -v\t\t\tIncrease verbosity\n"
82              "\n"
83              "Long options:\n"
84              "  --chipdir dir\t\tSet a chip directory search path\n"
85              "  --config name\t\tUse simulator config file\n"
86              "  --cpu type\t\tSet cpu type\n"
87              "  --debug\t\tDebug mode\n"
88              "  --help\t\tHelp (this text)\n"
89              "  --verbose\t\tIncrease verbosity\n"
90              "  --version\t\tPrint the simulator version number\n",
91              ProgName);
92 }
93
94
95
96 static void OptChipDir (const char* Opt attribute ((unused)), const char* Arg)
97 /* Handle the --chipdir option */
98 {
99     struct dirent* E;
100
101     /* Get the length of the directory name */
102     unsigned DirLen = strlen (Arg);
103
104     /* Open the directory */
105     DIR* D = opendir (Arg);
106     if (D == 0) {
107         AbEnd ("Cannot read directory `%s': %s", Arg, strerror (errno));
108     }
109
110     /* Read in all files and treat them as libraries */
111     while ((E = readdir (D)) != 0) {
112
113         char*  Name;
114         struct stat S;
115
116         /* ### Ignore anything buy *.so files */
117         unsigned NameLen = strlen (E->d_name);
118         if (NameLen <= 3) {
119             continue;
120         }
121         if (strcmp (E->d_name + NameLen - 3, ".so") != 0) {
122             continue;
123         }
124
125         /* Create the full file name */
126         Name = xmalloc (DirLen + 1 + NameLen + 1);
127         strcpy (Name, Arg);
128         strcpy (Name + DirLen, "/");
129         strcpy (Name + DirLen + 1, E->d_name);
130
131         /* Stat the file */
132         if (stat (Name, &S) != 0) {
133             Warning ("Cannot stat `%s': %s", Name, strerror (errno));
134             xfree (Name);
135             continue;
136         }
137
138         /* Check if this is a regular file */
139         if (S_ISREG (S.st_mode)) {
140             /* Treat it as a library */
141             LoadChipLibrary (Name);
142         }
143
144         /* Free the name */
145         xfree (Name);
146     }
147
148     /* Close the directory */
149     closedir (D);
150 }
151
152
153
154 static void OptCPU (const char* Opt, const char* Arg)
155 /* Handle the --cpu option */
156 {
157     if (strcmp (Arg, "6502") == 0) {
158         CPU = CPU_6502;
159     } else if (strcmp (Arg, "65C02") == 0) {
160         CPU = CPU_65C02;
161     } else {
162         AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
163     }
164 }
165
166
167
168 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
169 /* Define the config file */
170 {
171     if (CfgAvail ()) {
172         Error ("Cannot use -C twice");
173     }
174     CfgSetName (Arg);
175 }
176
177
178
179 static void OptDebug (const char* Opt attribute ((unused)),
180                       const char* Arg attribute ((unused)))
181 /* Simulator debug mode */
182 {
183     Debug = 1;
184 }
185
186
187
188 static void OptHelp (const char* Opt attribute ((unused)),
189                      const char* Arg attribute ((unused)))
190 /* Print usage information and exit */
191 {
192     Usage ();
193     exit (EXIT_SUCCESS);
194 }
195
196
197
198 static void OptVerbose (const char* Opt attribute ((unused)),
199                         const char* Arg attribute ((unused)))
200 /* Increase verbosity */
201 {
202     ++Verbosity;
203 }
204
205
206
207 static void OptVersion (const char* Opt attribute ((unused)),
208                         const char* Arg attribute ((unused)))
209 /* Print the assembler version */
210 {
211     fprintf (stderr,
212              "sim65 V%u.%u.%u\n",
213              VER_MAJOR, VER_MINOR, VER_PATCH);
214 }
215
216
217
218 int main (int argc, char* argv[])
219 {
220     /* Program long options */
221     static const LongOpt OptTab[] = {
222         { "--chipdir",          1,      OptChipDir              },
223         { "--config",           1,      OptConfig               },
224         { "--cpu",              1,      OptCPU                  },
225         { "--debug",            0,      OptDebug                },
226         { "--help",             0,      OptHelp                 },
227         { "--verbose",          0,      OptVerbose              },
228         { "--version",          0,      OptVersion              },
229     };
230
231     unsigned I;
232
233     /* Initialize the output file name */
234     const char* InputFile  = 0;
235
236     /* Initialize the cmdline module */
237     InitCmdLine (&argc, &argv, "sim65");
238
239     /* Parse the command line */
240     I = 1;
241     while (I < ArgCount) {
242
243         /* Get the argument */
244         const char* Arg = ArgVec[I];
245
246         /* Check for an option */
247         if (Arg [0] == '-') {
248
249             switch (Arg [1]) {
250
251                 case '-':
252                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
253                     break;
254
255                 case 'd':
256                     OptDebug (Arg, 0);
257                     break;
258
259                 case 'h':
260                 case '?':
261                     OptHelp (Arg, 0);
262                     break;
263
264                 case 'v':
265                     OptVerbose (Arg, 0);
266                     break;
267
268                 case 'C':
269                     OptConfig (Arg, GetArg (&I, 2));
270                     break;
271
272                 case 'L':
273                     OptChipDir (Arg, GetArg (&I, 2));
274                     break;
275
276                 case 'V':
277                     OptVersion (Arg, 0);
278                     break;
279
280                 default:
281                     UnknownOption (Arg);
282                     break;
283             }
284         } else {
285             if (InputFile) {
286                 fprintf (stderr, "additional file specs ignored\n");
287             } else {
288                 InputFile = Arg;
289             }
290         }
291
292         /* Next argument */
293         ++I;
294     }
295
296     /* Sort the already loaded chips */
297     SortChips ();
298
299     /* Check if we have a valid configuration */
300     if (!CfgAvail ()) {
301         Error ("Simulator configuration missing");
302     }
303
304     /* Initialize the simulated CPU memory */
305     MemInit ();
306
307     /* Read the config file */
308     CfgRead ();
309
310     CPUInit ();
311 #if 1
312     CPURun ();
313 #endif
314
315     /* Return an apropriate exit code */
316     return EXIT_SUCCESS;
317 }
318
319
320