]> git.sur5r.net Git - cc65/blob - src/da65/main.c
3164c189e44e1978d4a28c89422fc77dae72bc3f
[cc65] / src / da65 / main.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  main.c                                   */
4 /*                                                                           */
5 /*                  Main program for the da65 disassembler                   */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2000 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
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 <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <ctype.h>
40 #include <time.h>
41
42 /* common */
43 #include "abend.h"
44 #include "cmdline.h"
45 #include "fname.h"
46 #include "version.h"
47
48 /* da65 */
49 #include "attrtab.h"
50 #include "code.h"
51 #include "config.h"
52 #include "cpu.h"
53 #include "data.h"
54 #include "error.h"
55 #include "global.h"
56 #include "opctable.h"
57 #include "output.h"
58 #include "scanner.h"
59
60
61
62 /*****************************************************************************/
63 /*                                   Code                                    */
64 /*****************************************************************************/
65
66
67
68 static void Usage (void)
69 /* Print usage information and exit */
70 {
71     fprintf (stderr,
72              "Usage: %s [options] file\n"
73              "Short options:\n"
74              "  -g\t\t\tAdd debug info to object file\n"
75              "  -h\t\t\tHelp (this text)\n"
76              "  -o name\t\tName the output file\n"
77              "  -v\t\t\tIncrease verbosity\n"
78              "  -F\t\t\tAdd formfeeds to the output\n"
79              "  -V\t\t\tPrint the assembler version\n"
80              "\n"
81              "Long options:\n"
82              "  --cpu type\t\tSet cpu type\n"
83              "  --formfeeds\t\tAdd formfeeds to the output\n"
84              "  --help\t\tHelp (this text)\n"
85              "  --pagelength n\tSet the page length for the listing\n"
86              "  --verbose\t\tIncrease verbosity\n"
87              "  --version\t\tPrint the assembler version\n",
88              ProgName);
89 }
90
91
92
93 static void OptCPU (const char* Opt, const char* Arg)
94 /* Handle the --cpu option */
95 {
96     if (Arg == 0) {
97         NeedArg (Opt);
98     }
99     if (strcmp (Arg, "6502") == 0) {
100         SetCPU (CPU_6502);
101     } else if (strcmp (Arg, "65C02") == 0) {
102         SetCPU (CPU_65C02);
103     } else if (strcmp (Arg, "65816") == 0) {
104         SetCPU (CPU_65816);
105 #ifdef SUNPLUS
106     } else if (strcmp (Arg, "sunplus") == 0) {
107         SetCPU (CPU_SUNPLUS);
108 #endif
109     } else {
110         AbEnd ("Invalid CPU: `%s'", Arg);
111     }
112 }
113
114
115
116 static void OptFormFeeds (const char* Opt, const char* Arg)
117 /* Add form feeds to the output */
118 {
119     FormFeeds = 1;
120 }
121
122
123
124 static void OptHelp (const char* Opt, const char* Arg)
125 /* Print usage information and exit */
126 {
127     Usage ();
128     exit (EXIT_SUCCESS);
129 }
130
131
132
133 static void OptPageLength (const char* Opt, const char* Arg)
134 /* Handle the --pagelength option */
135 {
136     int Len;
137     if (Arg == 0) {
138         NeedArg (Opt);
139     }
140     Len = atoi (Arg);
141     if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
142         AbEnd ("Invalid page length: %d", Len);
143     }
144     PageLength = Len;
145 }
146
147
148
149 static void OptVerbose (const char* Opt, const char* Arg)
150 /* Increase verbosity */
151 {
152     ++Verbosity;
153 }
154
155
156
157 static void OptVersion (const char* Opt, const char* Arg)
158 /* Print the disassembler version */
159 {
160     fprintf (stderr,
161              "da65 V%u.%u.%u - (C) Copyright 2000 Ullrich von Bassewitz\n",
162              VER_MAJOR, VER_MINOR, VER_PATCH);
163 }
164
165
166
167 static void OneOpcode (unsigned RemainingBytes)
168 /* Disassemble one opcode */
169 {
170     /* Get the opcode from the current address */
171     unsigned char OPC = GetCodeByte (PC);
172
173     /* Get the opcode description for the opcode byte */
174     const OpcDesc* D = &OpcTable[OPC];
175
176     /* If we have a label at this address, output the label */
177     if (MustDefLabel (PC)) {
178         DefLabel (GetLabel (PC));
179     }
180
181     /* Check...
182      *   - ...if we have enough bytes remaining for the code at this address.
183      *   - ...if the current instruction is valid for the given CPU.
184      *   - ...if there is no label somewhere between the instruction bytes.
185      * If any of these conditions is true, switch to data mode.
186      */
187     if (GetStyleAttr (PC) == atDefault) {
188         if (D->Size > RemainingBytes) {
189             MarkAddr (PC, atIllegal);
190         } else if ((D->CPU & CPU) != CPU) {
191             MarkAddr (PC, atIllegal);
192         } else {
193             unsigned I;
194             for (I = 1; I < D->Size; ++I) {
195                 if (HaveLabel (PC+I)) {
196                     MarkAddr (PC, atIllegal);
197                     break;
198                 }
199             }
200         }
201     }
202
203     /* Disassemble the line */
204     switch (GetStyleAttr (PC)) {
205
206         case atDefault:
207         case atCode:
208             D->Handler (D);
209             PC += D->Size;
210             break;
211
212         case atByteTab:
213             ByteTable ();
214             break;
215
216         case atWordTab:
217             WordTable ();
218             break;
219
220         case atDWordTab:
221             DWordTable ();
222             break;
223
224         case atAddrTab:
225             AddrTable ();
226             break;
227
228         case atRtsTab:
229             RtsTable ();
230             break;
231
232         default:
233             DataByteLine (1);
234             ++PC;
235             break;
236
237     }
238 }
239
240
241
242 static void OnePass (void)
243 /* Make one pass through the code */
244 {
245     unsigned Count;
246
247     /* Disassemble until nothing left */
248     while ((Count = GetRemainingBytes()) > 0) {
249         OneOpcode (Count);
250     }
251 }
252
253
254
255 static void Disassemble (void)
256 /* Disassemble the code */
257 {
258     /* Pass 1 */
259     Pass = 1;
260     OnePass ();
261
262     Output ("---------------------------");
263     LineFeed ();
264
265     /* Pass 2 */
266     Pass = 2;
267     ResetCode ();
268     DefOutOfRangeLabels ();
269     OnePass ();
270 }
271
272
273
274 int main (int argc, char* argv [])
275 /* Assembler main program */
276 {
277     /* Program long options */
278     static const LongOpt OptTab[] = {
279         { "--cpu",              1,      OptCPU                  },
280         { "--formfeeds",        0,      OptFormFeeds            },
281         { "--help",             0,      OptHelp                 },
282         { "--pagelength",       1,      OptPageLength           },
283         { "--verbose",          0,      OptVerbose              },
284         { "--version",          0,      OptVersion              },
285     };
286
287     int I;
288
289     /* Initialize the cmdline module */
290     InitCmdLine (argc, argv, "da65");
291
292     /* Check the parameters */
293     I = 1;
294     while (I < argc) {
295
296         /* Get the argument */
297         const char* Arg = argv [I];
298
299         /* Check for an option */
300         if (Arg [0] == '-') {
301             switch (Arg [1]) {
302
303                 case '-':
304                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
305                     break;
306
307                 case 'h':
308                     OptHelp (Arg, 0);
309                     break;
310
311                 case 'o':
312                     OutFile = GetArg (&I, 2);
313                     break;
314
315                 case 'v':
316                     OptVerbose (Arg, 0);
317                     break;
318
319                 case 'V':
320                     OptVersion (Arg, 0);
321                     break;
322
323                 default:
324                     UnknownOption (Arg);
325                     break;
326
327             }
328         } else {
329             /* Filename. Check if we already had one */
330             if (InFile) {
331                 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
332                          ProgName, Arg);
333                 exit (EXIT_FAILURE);
334             } else {
335                 InFile = Arg;
336             }
337         }
338
339         /* Next argument */
340         ++I;
341     }
342
343     /* Must have an input file */
344     if (InFile == 0) {
345         AbEnd ("No input file");
346     }
347
348     /* Make the config file name from the input file if none was given */
349     if (!CfgAvail ()) {
350         CfgSetName (MakeFilename (InFile, CfgExt));
351     }
352
353     /* Try to read the configuration file */
354     CfgRead ();
355
356     /* Make the output file name from the input file name if none was given */
357     if (OutFile == 0) {
358         OutFile = MakeFilename (InFile, OutExt);
359     }
360
361     /* Load the input file */
362     LoadCode (InFile, 0xE000);  /* ### */
363
364     /* Open the output file */
365     OpenOutput (OutFile);
366
367     /* Disassemble the code */
368     Disassemble ();
369
370     /* Close the output file */
371     CloseOutput ();
372
373     /* Done */
374     return EXIT_SUCCESS;
375 }
376
377
378