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 = 0;
66 /*****************************************************************************/
68 /*****************************************************************************/
72 static void Usage (void)
74 printf ("Usage: %s [options] file [arguments]\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"
82 " --help\t\tHelp (this text)\n"
83 " --verbose\t\tIncrease verbosity\n"
84 " --version\t\tPrint the simulator version number\n",
90 static void OptHelp (const char* Opt attribute ((unused)),
91 const char* Arg attribute ((unused)))
92 /* Print usage information and exit */
100 static void OptVerbose (const char* Opt attribute ((unused)),
101 const char* Arg attribute ((unused)))
102 /* Increase verbosity */
109 static void OptVersion (const char* Opt attribute ((unused)),
110 const char* Arg attribute ((unused)))
111 /* Print the simulator version */
113 fprintf (stderr, "sim65 V%s\n", GetVersionAsString ());
116 static void OptQuitXIns (const char* Opt attribute ((unused)),
117 const char* Arg attribute ((unused)))
118 /* quit after MaxCycles cycles */
120 MaxCycles = strtoul(Arg, NULL, 0);
123 static void ReadProgramFile (void)
124 /* Load program into memory */
127 unsigned Addr = 0x0200;
130 FILE* F = fopen (ProgramFile, "rb");
132 Error ("Cannot open `%s': %s", ProgramFile, strerror (errno));
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);
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);
148 MemWriteByte (Addr++, (unsigned char) Val);
151 /* Check for errors */
153 Error ("Error reading from `%s': %s", ProgramFile, strerror (errno));
159 Print (stderr, 1, "Loaded `%s' at $0200-$%04X\n", ProgramFile, Addr - 1);
164 int main (int argc, char* argv[])
166 /* Program long options */
167 static const LongOpt OptTab[] = {
168 { "--help", 0, OptHelp },
169 { "--verbose", 0, OptVerbose },
170 { "--version", 0, OptVersion },
175 /* Initialize the cmdline module */
176 InitCmdLine (&argc, &argv, "sim65");
178 /* Parse the command line */
180 while (I < ArgCount) {
182 /* Get the argument */
183 const char* Arg = ArgVec[I];
185 /* Check for an option */
186 if (Arg [0] == '-') {
191 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
208 OptQuitXIns (Arg, GetArg (&I, 2));
224 /* Do we have a program file? */
225 if (ProgramFile == 0) {
226 AbEnd ("No program file");
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 */
246 /* Return an apropriate exit code */