1 /*****************************************************************************/
5 /* sim65 main program */
9 /* (C) 2002-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 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)
74 "Usage: %s [options] file\n"
76 " -C name\t\tUse simulator config file\n"
77 " -L dir\t\tSet a chip directory search path\n"
78 " -V\t\t\tPrint the simulator version number\n"
79 " -d\t\t\tDebug mode\n"
80 " -h\t\t\tHelp (this text)\n"
81 " -v\t\t\tIncrease verbosity\n"
84 " --chipdir dir\t\tSet a chip directory search path\n"
85 " --config name\t\tUse simulator config file\n"
86 " --cpu type\t\tSet cpu type\n"
87 " --debug\t\tDebug mode\n"
88 " --help\t\tHelp (this text)\n"
89 " --verbose\t\tIncrease verbosity\n"
90 " --version\t\tPrint the simulator version number\n",
96 static void OptChipDir (const char* Opt attribute ((unused)), const char* Arg)
97 /* Handle the --chipdir option */
101 /* Get the length of the directory name */
102 unsigned DirLen = strlen (Arg);
104 /* Open the directory */
105 DIR* D = opendir (Arg);
107 AbEnd ("Cannot read directory `%s': %s", Arg, strerror (errno));
110 /* Read in all files and treat them as libraries */
111 while ((E = readdir (D)) != 0) {
116 /* ### Ignore anything buy *.so files */
117 unsigned NameLen = strlen (E->d_name);
121 if (strcmp (E->d_name + NameLen - 3, ".so") != 0) {
125 /* Create the full file name */
126 Name = xmalloc (DirLen + 1 + NameLen + 1);
128 strcpy (Name + DirLen, "/");
129 strcpy (Name + DirLen + 1, E->d_name);
132 if (stat (Name, &S) != 0) {
133 Warning ("Cannot stat `%s': %s", Name, strerror (errno));
138 /* Check if this is a regular file */
139 if (S_ISREG (S.st_mode)) {
140 /* Treat it as a library */
141 LoadChipLibrary (Name);
148 /* Close the directory */
154 static void OptCPU (const char* Opt, const char* Arg)
155 /* Handle the --cpu option */
157 if (strcmp (Arg, "6502") == 0) {
159 } else if (strcmp (Arg, "65C02") == 0) {
162 AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
168 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
169 /* Define the config file */
172 Error ("Cannot use -C twice");
179 static void OptDebug (const char* Opt attribute ((unused)),
180 const char* Arg attribute ((unused)))
181 /* Simulator debug mode */
188 static void OptHelp (const char* Opt attribute ((unused)),
189 const char* Arg attribute ((unused)))
190 /* Print usage information and exit */
198 static void OptVerbose (const char* Opt attribute ((unused)),
199 const char* Arg attribute ((unused)))
200 /* Increase verbosity */
207 static void OptVersion (const char* Opt attribute ((unused)),
208 const char* Arg attribute ((unused)))
209 /* Print the assembler version */
213 VER_MAJOR, VER_MINOR, VER_PATCH);
218 int main (int argc, char* argv[])
220 /* Program long options */
221 static const LongOpt OptTab[] = {
222 { "--chipdir", 1, OptChipDir },
223 { "--config", 1, OptConfig },
224 { "--cpu", 1, OptCPU },
225 { "--debug", 0, OptDebug },
226 { "--help", 0, OptHelp },
227 { "--verbose", 0, OptVerbose },
228 { "--version", 0, OptVersion },
233 /* Initialize the output file name */
234 const char* InputFile = 0;
236 /* Initialize the cmdline module */
237 InitCmdLine (&argc, &argv, "sim65");
239 /* Parse the command line */
241 while (I < ArgCount) {
243 /* Get the argument */
244 const char* Arg = ArgVec[I];
246 /* Check for an option */
247 if (Arg [0] == '-') {
252 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
269 OptConfig (Arg, GetArg (&I, 2));
273 OptChipDir (Arg, GetArg (&I, 2));
286 fprintf (stderr, "additional file specs ignored\n");
296 /* Sort the already loaded chips */
299 /* Check if we have a valid configuration */
301 Error ("Simulator configuration missing");
304 /* Initialize the simulated CPU memory */
307 /* Read the config file */
316 /* Return an apropriate exit code */