1 /*****************************************************************************/
5 /* sim65 main program */
9 /* (C) 2002-2009, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
54 /*****************************************************************************/
56 /*****************************************************************************/
60 /* Name of program file */
61 const char* ProgramFile;
63 /* exit simulator after MaxCycles Cycles */
64 unsigned long MaxCycles;
66 /*****************************************************************************/
68 /*****************************************************************************/
72 static void Usage (void)
74 printf ("Usage: %s [options] file [arguments]\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"
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",
92 static void OptHelp (const char* Opt attribute ((unused)),
93 const char* Arg attribute ((unused)))
94 /* Print usage information and exit */
102 static void OptVerbose (const char* Opt attribute ((unused)),
103 const char* Arg attribute ((unused)))
104 /* Increase verbosity */
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 */
120 static void OptVersion (const char* Opt attribute ((unused)),
121 const char* Arg attribute ((unused)))
122 /* Print the simulator version */
124 fprintf (stderr, "%s V%s\n", ProgName, GetVersionAsString ());
128 static void OptQuitXIns (const char* Opt attribute ((unused)),
129 const char* Arg attribute ((unused)))
130 /* quit after MaxCycles cycles */
132 MaxCycles = strtoul(Arg, NULL, 0);
135 static void ReadProgramFile (void)
136 /* Load program into memory */
139 unsigned Addr = 0x0200;
142 FILE* F = fopen (ProgramFile, "rb");
144 Error ("Cannot open `%s': %s", ProgramFile, strerror (errno));
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);
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);
160 MemWriteByte (Addr++, (unsigned char) Val);
163 /* Check for errors */
165 Error ("Error reading from `%s': %s", ProgramFile, strerror (errno));
171 Print (stderr, 1, "Loaded `%s' at $0200-$%04X\n", ProgramFile, Addr - 1);
176 int main (int argc, char* argv[])
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 },
188 /* Initialize the cmdline module */
189 InitCmdLine (&argc, &argv, "sim65");
191 /* Parse the command line */
193 while (I < ArgCount) {
195 /* Get the argument */
196 const char* Arg = ArgVec[I];
198 /* Check for an option */
199 if (Arg [0] == '-') {
204 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
225 OptQuitXIns (Arg, GetArg (&I, 2));
241 /* Do we have a program file? */
242 if (ProgramFile == 0) {
243 AbEnd ("No program file");
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 */
263 /* Return an apropriate exit code */