1 /*****************************************************************************/
5 /* sim65 main program */
9 /* (C) 2002-2009, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
64 /*****************************************************************************/
66 /*****************************************************************************/
70 static void Usage (void)
72 printf ("Usage: %s [options] file\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"
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",
94 static void OptChipDir (const char* Opt attribute ((unused)), const char* Arg)
95 /* Handle the --chipdir option */
99 /* Get the length of the directory name */
100 unsigned DirLen = strlen (Arg);
102 /* Open the directory */
103 DIR* D = opendir (Arg);
105 AbEnd ("Cannot read directory `%s': %s", Arg, strerror (errno));
108 /* Read in all files and treat them as libraries */
109 while ((E = readdir (D)) != 0) {
114 /* ### Ignore anything but *.so files */
115 unsigned NameLen = strlen (E->d_name);
119 if (strcmp (E->d_name + NameLen - 3, ".so") != 0) {
123 /* Create the full file name */
124 Name = xmalloc (DirLen + 1 + NameLen + 1);
126 strcpy (Name + DirLen, "/");
127 strcpy (Name + DirLen + 1, E->d_name);
130 if (FileStat (Name, &S) != 0) {
131 Warning ("Cannot stat `%s': %s", Name, strerror (errno));
136 /* Check if this is a regular file */
137 if (S_ISREG (S.st_mode)) {
138 /* Treat it as a library */
139 LoadChipLibrary (Name);
146 /* Close the directory */
152 static void OptCPU (const char* Opt, const char* Arg)
153 /* Handle the --cpu option */
155 if (strcmp (Arg, "6502") == 0) {
157 } else if (strcmp (Arg, "65C02") == 0) {
160 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
166 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
167 /* Define the config file */
170 Error ("Cannot use -C twice");
177 static void OptDebug (const char* Opt attribute ((unused)),
178 const char* Arg attribute ((unused)))
179 /* Simulator debug mode */
186 static void OptHelp (const char* Opt attribute ((unused)),
187 const char* Arg attribute ((unused)))
188 /* Print usage information and exit */
196 static void OptVerbose (const char* Opt attribute ((unused)),
197 const char* Arg attribute ((unused)))
198 /* Increase verbosity */
205 static void OptVersion (const char* Opt attribute ((unused)),
206 const char* Arg attribute ((unused)))
207 /* Print the assembler version */
209 fprintf (stderr, "sim65 V%s\n", GetVersionAsString ());
214 int main (int argc, char* argv[])
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 },
229 /* Initialize the output file name */
230 const char* InputFile = 0;
232 /* Initialize the cmdline module */
233 InitCmdLine (&argc, &argv, "sim65");
235 /* Initialize the chip library search paths */
238 /* Parse the command line */
240 while (I < ArgCount) {
242 /* Get the argument */
243 const char* Arg = ArgVec[I];
245 /* Check for an option */
246 if (Arg [0] == '-') {
251 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
268 OptConfig (Arg, GetArg (&I, 2));
272 OptChipDir (Arg, GetArg (&I, 2));
285 fprintf (stderr, "additional file specs ignored\n");
295 /* Sort the already loaded chips */
298 /* Check if we have a valid configuration */
300 Error ("Simulator configuration missing");
303 /* Initialize the simulated CPU memory */
306 /* Read the config file */
315 /* Return an apropriate exit code */