]> git.sur5r.net Git - cc65/blob - src/sim65/main.c
Allow to set the ProDOS type and auxtype on creating new files in a similiar way...
[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     /* Initialize the chip library search paths */
237     InitChipPaths ();
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
312     while (1) {
313         CPURun ();
314     }
315
316     /* Return an apropriate exit code */
317     return EXIT_SUCCESS;
318 }
319
320
321