]> git.sur5r.net Git - cc65/blob - src/sim65/main.c
support for .zeropage segment in GEOS
[cc65] / src / sim65 / main.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  main.c                                   */
4 /*                                                                           */
5 /*                              sim65 main program                           */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2002      Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
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
41 /* common */
42 #include "abend.h"
43 #include "cmdline.h"
44 #include "print.h"
45 #include "version.h"
46
47 /* sim65 */
48 #include "chip.h"
49 #include "chiplib.h"
50 #include "chippath.h"
51 #include "config.h"
52 #include "cpucore.h"
53 #include "cputype.h"
54 #include "error.h"
55 #include "global.h"
56 #include "memory.h"
57 #include "scanner.h"
58
59
60
61 /*****************************************************************************/
62 /*                                   Code                                    */
63 /*****************************************************************************/
64
65
66
67 static void Usage (void)
68 {
69     fprintf (stderr,
70              "Usage: %s [options] file\n"
71              "Short options:\n"
72              "  -C name\t\tUse simulator config file\n"
73              "  -L dir\t\tSet a chip directory search path\n"
74              "  -V\t\t\tPrint the simulator version number\n"
75              "  -d\t\t\tDebug mode\n"
76              "  -h\t\t\tHelp (this text)\n"
77              "  -v\t\t\tIncrease verbosity\n"
78              "\n"
79              "Long options:\n"
80              "  --chipdir dir\t\tSet a chip directory search path\n"
81              "  --config name\t\tUse simulator config file\n"
82              "  --cpu type\t\tSet cpu type\n"
83              "  --debug\t\tDebug mode\n"
84              "  --help\t\tHelp (this text)\n"
85              "  --verbose\t\tIncrease verbosity\n"
86              "  --version\t\tPrint the simulator version number\n",
87              ProgName);
88 }
89
90
91
92 static void OptChipDir (const char* Opt attribute ((unused)), const char* Arg)
93 /* Handle the --chipdir option */
94 {
95     AddChipPath (Arg);
96 }
97
98
99
100 static void OptCPU (const char* Opt, const char* Arg)
101 /* Handle the --cpu option */
102 {
103     if (strcmp (Arg, "6502") == 0) {
104         CPU = CPU_6502;
105     } else if (strcmp (Arg, "65C02") == 0) {
106         CPU = CPU_65C02;
107     } else {
108         AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
109     }
110 }
111
112
113
114 static void OptConfig (const char* Opt attribute ((unused)), const char* Arg)
115 /* Define the config file */
116 {
117     if (CfgAvail ()) {
118         Error ("Cannot use -C twice");
119     }
120     CfgSetName (Arg);
121 }
122
123
124
125 static void OptDebug (const char* Opt attribute ((unused)),
126                       const char* Arg attribute ((unused)))
127 /* Simulator debug mode */
128 {
129     Debug = 1;
130 }
131
132
133
134 static void OptHelp (const char* Opt attribute ((unused)),
135                      const char* Arg attribute ((unused)))
136 /* Print usage information and exit */
137 {
138     Usage ();
139     exit (EXIT_SUCCESS);
140 }
141
142
143
144 static void OptVerbose (const char* Opt attribute ((unused)),
145                         const char* Arg attribute ((unused)))
146 /* Increase verbosity */
147 {
148     ++Verbosity;
149 }
150
151
152
153 static void OptVersion (const char* Opt attribute ((unused)),
154                         const char* Arg attribute ((unused)))
155 /* Print the assembler version */
156 {
157     fprintf (stderr,
158              "sim65 V%u.%u.%u\n",
159              VER_MAJOR, VER_MINOR, VER_PATCH);
160 }
161
162
163
164 int main (int argc, char* argv[])
165 {
166     /* Program long options */
167     static const LongOpt OptTab[] = {
168         { "--chipdir",          1,      OptChipDir              },
169         { "--config",           1,      OptConfig               },
170         { "--cpu",              1,      OptCPU                  },
171         { "--debug",            0,      OptDebug                },
172         { "--help",             0,      OptHelp                 },
173         { "--verbose",          0,      OptVerbose              },
174         { "--version",          0,      OptVersion              },
175     };
176
177     unsigned I;
178
179     /* Initialize the output file name */
180     const char* InputFile  = 0;
181
182     /* Initialize the cmdline module */
183     InitCmdLine (&argc, &argv, "sim65");
184
185     /* Parse the command line */
186     I = 1;
187     while (I < ArgCount) {
188
189         /* Get the argument */
190         const char* Arg = ArgVec[I];
191
192         /* Check for an option */
193         if (Arg [0] == '-') {
194
195             switch (Arg [1]) {
196
197                 case '-':
198                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
199                     break;
200
201                 case 'd':
202                     OptDebug (Arg, 0);
203                     break;
204
205                 case 'h':
206                 case '?':
207                     OptHelp (Arg, 0);
208                     break;
209
210                 case 'v':
211                     OptVerbose (Arg, 0);
212                     break;
213
214                 case 'C':
215                     OptConfig (Arg, GetArg (&I, 2));
216                     break;
217
218                 case 'L':
219                     OptChipDir (Arg, GetArg (&I, 2));
220                     break;
221
222                 case 'V':
223                     OptVersion (Arg, 0);
224                     break;
225
226                 default:
227                     UnknownOption (Arg);
228                     break;
229             }
230         } else {
231             if (InputFile) {
232                 fprintf (stderr, "additional file specs ignored\n");
233             } else {
234                 InputFile = Arg;
235             }
236         }
237
238         /* Next argument */
239         ++I;
240     }
241
242     /* Check if we have a valid configuration */
243     if (!CfgAvail ()) {
244         Error ("Simulator configuration missing");
245     }
246
247     /* Load the chips */
248     AddChipPath ("chips");
249     LoadChipLibrary ("ram.so");
250     LoadChips ();
251
252     /* Read the config file */
253     CfgRead ();
254
255     MemInit ();
256     CPUInit ();
257 #if 0
258     CPURun ();
259 #endif
260
261     /* Return an apropriate exit code */
262     return EXIT_SUCCESS;
263 }
264
265
266