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