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 /*****************************************************************************/
40 #include <sys/types.h>
65 /*****************************************************************************/
67 /*****************************************************************************/
71 static void Usage (void)
73 printf ("Usage: %s [options] file\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"
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",
95 static void OptChipDir (const char* Opt attribute ((unused)), const char* Arg)
96 /* Handle the --chipdir option */
100 /* Get the length of the directory name */
101 unsigned DirLen = strlen (Arg);
103 /* Open the directory */
104 DIR* D = opendir (Arg);
106 AbEnd ("Cannot read directory `%s': %s", Arg, strerror (errno));
109 /* Read in all files and treat them as libraries */
110 while ((E = readdir (D)) != 0) {
115 /* ### Ignore anything buy *.so files */
116 unsigned NameLen = strlen (E->d_name);
120 if (strcmp (E->d_name + NameLen - 3, ".so") != 0) {
124 /* Create the full file name */
125 Name = xmalloc (DirLen + 1 + NameLen + 1);
127 strcpy (Name + DirLen, "/");
128 strcpy (Name + DirLen + 1, E->d_name);
131 if (stat (Name, &S) != 0) {
132 Warning ("Cannot stat `%s': %s", Name, strerror (errno));
137 /* Check if this is a regular file */
138 if (S_ISREG (S.st_mode)) {
139 /* Treat it as a library */
140 LoadChipLibrary (Name);
147 /* Close the directory */
153 static void OptCPU (const char* Opt, const char* Arg)
154 /* Handle the --cpu option */
156 if (strcmp (Arg, "6502") == 0) {
158 } else if (strcmp (Arg, "65C02") == 0) {
161 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
167 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
168 /* Define the config file */
171 Error ("Cannot use -C twice");
178 static void OptDebug (const char* Opt attribute ((unused)),
179 const char* Arg attribute ((unused)))
180 /* Simulator debug mode */
187 static void OptHelp (const char* Opt attribute ((unused)),
188 const char* Arg attribute ((unused)))
189 /* Print usage information and exit */
197 static void OptVerbose (const char* Opt attribute ((unused)),
198 const char* Arg attribute ((unused)))
199 /* Increase verbosity */
206 static void OptVersion (const char* Opt attribute ((unused)),
207 const char* Arg attribute ((unused)))
208 /* Print the assembler version */
210 fprintf (stderr, "sim65 V%s\n", GetVersionAsString ());
215 int main (int argc, char* argv[])
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 },
230 /* Initialize the output file name */
231 const char* InputFile = 0;
233 /* Initialize the cmdline module */
234 InitCmdLine (&argc, &argv, "sim65");
236 /* Parse the command line */
238 while (I < ArgCount) {
240 /* Get the argument */
241 const char* Arg = ArgVec[I];
243 /* Check for an option */
244 if (Arg [0] == '-') {
249 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
266 OptConfig (Arg, GetArg (&I, 2));
270 OptChipDir (Arg, GetArg (&I, 2));
283 fprintf (stderr, "additional file specs ignored\n");
293 /* Sort the already loaded chips */
296 /* Check if we have a valid configuration */
298 Error ("Simulator configuration missing");
301 /* Initialize the simulated CPU memory */
304 /* Read the config file */
313 /* Return an apropriate exit code */