]> git.sur5r.net Git - cc65/blob - src/sim65/main.c
Formatting fixes.
[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 <string.h>
37 #include <stdlib.h>
38 #include <errno.h>
39
40 /* common */
41 #include "abend.h"
42 #include "cmdline.h"
43 #include "print.h"
44 #include "version.h"
45
46 /* sim65 */
47 #include "6502.h"
48 #include "error.h"
49 #include "memory.h"
50 #include "paravirt.h"
51
52
53
54 /*****************************************************************************/
55 /*                                   Data                                    */
56 /*****************************************************************************/
57
58
59
60 /* Name of program file */
61 const char* ProgramFile;
62
63 /* exit simulator after MaxCycles Cycles */
64 unsigned long MaxCycles;
65
66 /*****************************************************************************/
67 /*                                   Code                                    */
68 /*****************************************************************************/
69
70
71
72 static void Usage (void)
73 {
74     printf ("Usage: %s [options] file [arguments]\n"
75             "Short options:\n"
76             "  -h\t\t\tHelp (this text)\n"
77             "  -c\t\t\tPrint amount of executed CPU cycles\n"
78             "  -v\t\t\tIncrease verbosity\n"
79             "  -V\t\t\tPrint the simulator version number\n"
80             "  -x <num>\t\tExit simulator after <num> cycles\n"
81             "\n"
82             "Long options:\n"
83             "  --help\t\tHelp (this text)\n"
84             "  --cycles\t\tPrint amount of executed CPU cycles\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 OptHelp (const char* Opt attribute ((unused)),
93                      const char* Arg attribute ((unused)))
94 /* Print usage information and exit */
95 {
96     Usage ();
97     exit (EXIT_SUCCESS);
98 }
99
100
101
102 static void OptVerbose (const char* Opt attribute ((unused)),
103                         const char* Arg attribute ((unused)))
104 /* Increase verbosity */
105 {
106     ++Verbosity;
107 }
108
109
110
111 static void OptCycles (const char* Opt attribute ((unused)),
112                        const char* Arg attribute ((unused)))
113 /* Set flag to print amount of cycles at the end */
114 {
115     PrintCycles = 1;
116 }
117
118
119
120 static void OptVersion (const char* Opt attribute ((unused)),
121                         const char* Arg attribute ((unused)))
122 /* Print the simulator version */
123 {
124     fprintf (stderr, "%s V%s\n", ProgName, GetVersionAsString ());
125     exit(EXIT_SUCCESS);
126 }
127
128 static void OptQuitXIns (const char* Opt attribute ((unused)),
129                         const char* Arg attribute ((unused)))
130 /* quit after MaxCycles cycles */
131 {
132     MaxCycles = strtoul(Arg, NULL, 0);
133 }
134
135 static void ReadProgramFile (void)
136 /* Load program into memory */
137 {
138     int Val;
139     unsigned Addr = 0x0200;
140
141     /* Open the file */
142     FILE* F = fopen (ProgramFile, "rb");
143     if (F == 0) {
144         Error ("Cannot open `%s': %s", ProgramFile, strerror (errno));
145     }
146
147     /* Get the CPU type from the file header */
148     if ((Val = fgetc(F)) != EOF) {
149         if (Val != CPU_6502 && Val != CPU_65C02) {
150             Error ("`%s': Invalid CPU type", ProgramFile);
151         }
152         CPU = Val;
153     }
154
155     /* Read the file body into memory */
156     while ((Val = fgetc(F)) != EOF) {
157         if (Addr == 0xFF00) {
158             Error ("`%s': To large to fit into $0200-$FFF0", ProgramFile);
159         }
160         MemWriteByte (Addr++, (unsigned char) Val);
161     }
162
163     /* Check for errors */
164     if (ferror (F)) {
165         Error ("Error reading from `%s': %s", ProgramFile, strerror (errno));
166     }
167
168     /* Close the file */
169     fclose (F);
170
171     Print (stderr, 1, "Loaded `%s' at $0200-$%04X\n", ProgramFile, Addr - 1);
172 }
173
174
175
176 int main (int argc, char* argv[])
177 {
178     /* Program long options */
179     static const LongOpt OptTab[] = {
180         { "--help",             0,      OptHelp                 },
181         { "--cycles",           0,      OptCycles               },
182         { "--verbose",          0,      OptVerbose              },
183         { "--version",          0,      OptVersion              },
184     };
185
186     unsigned I;
187
188     /* Initialize the cmdline module */
189     InitCmdLine (&argc, &argv, "sim65");
190
191     /* Parse the command line */
192     I = 1;
193     while (I < ArgCount) {
194
195         /* Get the argument */
196         const char* Arg = ArgVec[I];
197
198         /* Check for an option */
199         if (Arg [0] == '-') {
200
201             switch (Arg [1]) {
202
203                 case '-':
204                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
205                     break;
206
207                 case 'h':
208                 case '?':
209                     OptHelp (Arg, 0);
210                     break;
211
212                 case 'c':
213                     OptCycles (Arg, 0);
214                     break;
215
216                 case 'v':
217                     OptVerbose (Arg, 0);
218                     break;
219
220                 case 'V':
221                     OptVersion (Arg, 0);
222                     break;
223
224                 case 'x':
225                     OptQuitXIns (Arg, GetArg (&I, 2));
226                     break;
227
228                 default:
229                     UnknownOption (Arg);
230                     break;
231             }
232         } else {
233             ProgramFile = Arg;
234             break;
235         }
236
237         /* Next argument */
238         ++I;
239     }
240
241     /* Do we have a program file? */
242     if (ProgramFile == 0) {
243         AbEnd ("No program file");
244     }
245
246     ParaVirtInit (I);
247
248     MemInit ();
249
250     ReadProgramFile ();
251
252     Reset ();
253
254     while (1) {
255         ExecuteInsn ();
256         if (MaxCycles && (GetCycles () >= MaxCycles)) {
257             Error ("Maximum number of cycles reached.");
258             exit (-99); /* do not use EXIT_FAILURE to avoid conflicts with the
259                            same value being used in a test program */
260         }
261     }
262
263     /* Return an apropriate exit code */
264     return EXIT_SUCCESS;
265 }