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, "sim65 V%s\n", GetVersionAsString ());
127 static void OptQuitXIns (const char* Opt attribute ((unused)),
128 const char* Arg attribute ((unused)))
129 /* quit after MaxCycles cycles */
131 MaxCycles = strtoul(Arg, NULL, 0);
134 static void ReadProgramFile (void)
135 /* Load program into memory */
138 unsigned Addr = 0x0200;
141 FILE* F = fopen (ProgramFile, "rb");
143 Error ("Cannot open `%s': %s", ProgramFile, strerror (errno));
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);
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);
159 MemWriteByte (Addr++, (unsigned char) Val);
162 /* Check for errors */
164 Error ("Error reading from `%s': %s", ProgramFile, strerror (errno));
170 Print (stderr, 1, "Loaded `%s' at $0200-$%04X\n", ProgramFile, Addr - 1);
175 int main (int argc, char* argv[])
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 },
187 /* Initialize the cmdline module */
188 InitCmdLine (&argc, &argv, "sim65");
190 /* Parse the command line */
192 while (I < ArgCount) {
194 /* Get the argument */
195 const char* Arg = ArgVec[I];
197 /* Check for an option */
198 if (Arg [0] == '-') {
203 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
224 OptQuitXIns (Arg, GetArg (&I, 2));
240 /* Do we have a program file? */
241 if (ProgramFile == 0) {
242 AbEnd ("No program file");
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 */
262 /* Return an apropriate exit code */