]> git.sur5r.net Git - cc65/blob - src/sim65/main.c
5405af29ff87f2293c3421f472c89ddd124bffd1
[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, "sim65 V%s\n", GetVersionAsString ());
125 }
126
127 static void OptQuitXIns (const char* Opt attribute ((unused)),
128                         const char* Arg attribute ((unused)))
129 /* quit after MaxCycles cycles */
130 {
131     MaxCycles = strtoul(Arg, NULL, 0);
132 }
133
134 static void ReadProgramFile (void)
135 /* Load program into memory */
136 {
137     int Val;
138     unsigned Addr = 0x0200;
139
140     /* Open the file */
141     FILE* F = fopen (ProgramFile, "rb");
142     if (F == 0) {
143         Error ("Cannot open `%s': %s", ProgramFile, strerror (errno));
144     }
145
146     /* Get the CPU type from the file header */
147     if ((Val = fgetc(F)) != EOF) {
148         if (Val != CPU_6502 && Val != CPU_65C02) {
149             Error ("`%s': Invalid CPU type", ProgramFile);
150         }
151         CPU = Val;
152     }
153
154     /* Read the file body into memory */
155     while ((Val = fgetc(F)) != EOF) {
156         if (Addr == 0xFF00) {
157             Error ("`%s': To large to fit into $0200-$FFF0", ProgramFile);
158         }
159         MemWriteByte (Addr++, (unsigned char) Val);
160     }
161
162     /* Check for errors */
163     if (ferror (F)) {
164         Error ("Error reading from `%s': %s", ProgramFile, strerror (errno));
165     }
166
167     /* Close the file */
168     fclose (F);
169
170     Print (stderr, 1, "Loaded `%s' at $0200-$%04X\n", ProgramFile, Addr - 1);
171 }
172
173
174
175 int main (int argc, char* argv[])
176 {
177     /* Program long options */
178     static const LongOpt OptTab[] = {
179         { "--help",             0,      OptHelp                 },
180         { "--cycles",           0,      OptCycles               },
181         { "--verbose",          0,      OptVerbose              },
182         { "--version",          0,      OptVersion              },
183     };
184
185     unsigned I;
186
187     /* Initialize the cmdline module */
188     InitCmdLine (&argc, &argv, "sim65");
189
190     /* Parse the command line */
191     I = 1;
192     while (I < ArgCount) {
193
194         /* Get the argument */
195         const char* Arg = ArgVec[I];
196
197         /* Check for an option */
198         if (Arg [0] == '-') {
199
200             switch (Arg [1]) {
201
202                 case '-':
203                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
204                     break;
205
206                 case 'h':
207                 case '?':
208                     OptHelp (Arg, 0);
209                     break;
210
211                 case 'c':
212                     OptCycles (Arg, 0);
213                     break;
214
215                 case 'v':
216                     OptVerbose (Arg, 0);
217                     break;
218
219                 case 'V':
220                     OptVersion (Arg, 0);
221                     break;
222
223                 case 'x':
224                     OptQuitXIns (Arg, GetArg (&I, 2));
225                     break;
226
227                 default:
228                     UnknownOption (Arg);
229                     break;
230             }
231         } else {
232             ProgramFile = Arg;
233             break;
234         }
235
236         /* Next argument */
237         ++I;
238     }
239
240     /* Do we have a program file? */
241     if (ProgramFile == 0) {
242         AbEnd ("No program file");
243     }
244
245     ParaVirtInit (I);
246
247     MemInit ();
248
249     ReadProgramFile ();
250
251     Reset ();
252
253     while (1) {
254         ExecuteInsn ();
255         if (MaxCycles && (GetCycles () >= MaxCycles)) {
256             Error ("Maximum number of cycles reached.");
257             exit (-99); /* do not use EXIT_FAILURE to avoid conflicts with the
258                            same value being used in a test program */
259         }
260     }
261
262     /* Return an apropriate exit code */
263     return EXIT_SUCCESS;
264 }