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