]> git.sur5r.net Git - cc65/blob - src/sim65/main.c
Merge pull request #297 from groessler/something_to_pull
[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 = 0;
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             "  -v\t\t\tIncrease verbosity\n"
78             "  -V\t\t\tPrint the simulator version number\n"
79             "  -x <num>\t\tExit simulator after <num> cycles\n"
80             "\n"
81             "Long options:\n"
82             "  --help\t\tHelp (this text)\n"
83             "  --verbose\t\tIncrease verbosity\n"
84             "  --version\t\tPrint the simulator version number\n",
85             ProgName);
86 }
87
88
89
90 static void OptHelp (const char* Opt attribute ((unused)),
91                      const char* Arg attribute ((unused)))
92 /* Print usage information and exit */
93 {
94     Usage ();
95     exit (EXIT_SUCCESS);
96 }
97
98
99
100 static void OptVerbose (const char* Opt attribute ((unused)),
101                         const char* Arg attribute ((unused)))
102 /* Increase verbosity */
103 {
104     ++Verbosity;
105 }
106
107
108
109 static void OptVersion (const char* Opt attribute ((unused)),
110                         const char* Arg attribute ((unused)))
111 /* Print the simulator version */
112 {
113     fprintf (stderr, "sim65 V%s\n", GetVersionAsString ());
114 }
115
116 static void OptQuitXIns (const char* Opt attribute ((unused)),
117                         const char* Arg attribute ((unused)))
118 /* quit after MaxCycles cycles */
119 {
120     MaxCycles = strtoul(Arg, NULL, 0);
121 }
122
123 static void ReadProgramFile (void)
124 /* Load program into memory */
125 {
126     int Val;
127     unsigned Addr = 0x0200;
128
129     /* Open the file */
130     FILE* F = fopen (ProgramFile, "rb");
131     if (F == 0) {
132         Error ("Cannot open `%s': %s", ProgramFile, strerror (errno));
133     }
134
135     /* Get the CPU type from the file header */
136     if ((Val = fgetc(F)) != EOF) {
137         if (Val != CPU_6502 && Val != CPU_65C02) {
138             Error ("`%s': Invalid CPU type", ProgramFile);
139         }
140         CPU = Val;
141     }
142
143     /* Read the file body into memory */
144     while ((Val = fgetc(F)) != EOF) {
145         if (Addr == 0xFF00) {
146             Error ("`%s': To large to fit into $0200-$FFF0", ProgramFile);
147         }
148         MemWriteByte (Addr++, (unsigned char) Val);
149     }
150
151     /* Check for errors */
152     if (ferror (F)) {
153         Error ("Error reading from `%s': %s", ProgramFile, strerror (errno));
154     }
155
156     /* Close the file */
157     fclose (F);
158
159     Print (stderr, 1, "Loaded `%s' at $0200-$%04X\n", ProgramFile, Addr - 1);
160 }
161
162
163
164 int main (int argc, char* argv[])
165 {
166     /* Program long options */
167     static const LongOpt OptTab[] = {
168         { "--help",             0,      OptHelp                 },
169         { "--verbose",          0,      OptVerbose              },
170         { "--version",          0,      OptVersion              },
171     };
172
173     unsigned I;
174
175     /* Initialize the cmdline module */
176     InitCmdLine (&argc, &argv, "sim65");
177
178     /* Parse the command line */
179     I = 1;
180     while (I < ArgCount) {
181
182         /* Get the argument */
183         const char* Arg = ArgVec[I];
184
185         /* Check for an option */
186         if (Arg [0] == '-') {
187
188             switch (Arg [1]) {
189
190                 case '-':
191                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
192                     break;
193
194                 case 'h':
195                 case '?':
196                     OptHelp (Arg, 0);
197                     break;
198
199                 case 'v':
200                     OptVerbose (Arg, 0);
201                     break;
202
203                 case 'V':
204                     OptVersion (Arg, 0);
205                     break;
206
207                 case 'x':
208                     OptQuitXIns (Arg, GetArg (&I, 2));
209                     break;
210
211                 default:
212                     UnknownOption (Arg);
213                     break;
214             }
215         } else {
216             ProgramFile = Arg;
217             break;
218         }
219
220         /* Next argument */
221         ++I;
222     }
223
224     /* Do we have a program file? */
225     if (ProgramFile == 0) {
226         AbEnd ("No program file");
227     }
228
229     ParaVirtInit (I);
230
231     MemInit ();
232
233     ReadProgramFile ();
234
235     Reset ();
236
237     while (1) {
238         ExecuteInsn ();
239         if (MaxCycles && (GetCycles () >= MaxCycles)) {
240             Error ("Maximum number of cycles reached.");
241             exit (-99); /* do not use EXIT_FAILURE to avoid conflicts with the
242                            same value being used in a test program */
243         }
244     }
245
246     /* Return an apropriate exit code */
247     return EXIT_SUCCESS;
248 }