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