]> git.sur5r.net Git - cc65/blob - src/da65/main.c
c2d4ed563254507c9c57de0cc71d200cf198bd69
[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     const char* Label = GetLabel (PC);
178     if (Label) {
179         DefLabel (Label);
180     }
181
182     /* Check...
183      *   - ...if we have enough bytes remaining for the code at this address.
184      *   - ...if the current instruction is valid for the given CPU.
185      *   - ...if there is no label somewhere between the instruction bytes.
186      * If any of these conditions is true, switch to data mode.
187      */
188     if (GetStyle (PC) == atDefault) {
189         if (D->Size > RemainingBytes) {
190             MarkAddr (PC, atIllegal);
191         } else if ((D->CPU & CPU) != CPU) {
192             MarkAddr (PC, atIllegal);
193         } else {
194             unsigned I;
195             for (I = 1; I < D->Size; ++I) {
196                 if (HaveLabel (PC+I)) {
197                     MarkAddr (PC, atIllegal);
198                     break;
199                 }
200             }
201         }
202     }
203
204     /* Disassemble the line */
205     switch (GetStyle (PC)) {
206
207         case atDefault:
208         case atCode:
209             D->Handler (D);
210             PC += D->Size;
211             break;
212
213         case atByteTab:
214             ByteTable (RemainingBytes);
215             break;
216
217         case atWordTab:
218             WordTable (RemainingBytes);
219             break;
220
221         case atAddrTab:
222             AddrTable (RemainingBytes);
223             break;
224
225         default:
226             DataByteLine (1);
227             ++PC;
228             break;
229
230     }
231 }
232
233
234
235 static void OnePass (void)
236 /* Make one pass through the code */
237 {
238     unsigned Count;
239
240     /* Disassemble until nothing left */
241     while ((Count = GetRemainingBytes()) > 0) {
242         OneOpcode (Count);
243     }
244 }
245
246
247
248 static void Disassemble (void)
249 /* Disassemble the code */
250 {
251     /* Pass 1 */
252     Pass = 1;
253     OnePass ();
254
255     Output ("---------------------------");
256     LineFeed ();
257
258     /* Pass 2 */
259     Pass = 2;
260     ResetCode ();
261     DefOutOfRangeLabels ();
262     OnePass ();
263 }
264
265
266
267 int main (int argc, char* argv [])
268 /* Assembler main program */
269 {
270     /* Program long options */
271     static const LongOpt OptTab[] = {
272         { "--cpu",              1,      OptCPU                  },
273         { "--formfeeds",        0,      OptFormFeeds            },
274         { "--help",             0,      OptHelp                 },
275         { "--pagelength",       1,      OptPageLength           },
276         { "--verbose",          0,      OptVerbose              },
277         { "--version",          0,      OptVersion              },
278     };
279
280     int I;
281
282     /* Initialize the cmdline module */
283     InitCmdLine (argc, argv, "da65");
284
285     /* Check the parameters */
286     I = 1;
287     while (I < argc) {
288
289         /* Get the argument */
290         const char* Arg = argv [I];
291
292         /* Check for an option */
293         if (Arg [0] == '-') {
294             switch (Arg [1]) {
295
296                 case '-':
297                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
298                     break;
299
300                 case 'h':
301                     OptHelp (Arg, 0);
302                     break;
303
304                 case 'o':
305                     OutFile = GetArg (&I, 2);
306                     break;
307
308                 case 'v':
309                     OptVerbose (Arg, 0);
310                     break;
311
312                 case 'V':
313                     OptVersion (Arg, 0);
314                     break;
315
316                 default:
317                     UnknownOption (Arg);
318                     break;
319
320             }
321         } else {
322             /* Filename. Check if we already had one */
323             if (InFile) {
324                 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
325                          ProgName, Arg);
326                 exit (EXIT_FAILURE);
327             } else {
328                 InFile = Arg;
329             }
330         }
331
332         /* Next argument */
333         ++I;
334     }
335
336     /* Must have an input file */
337     if (InFile == 0) {
338         AbEnd ("No input file");
339     }
340
341     /* Make the config file name from the input file if none was given */
342     if (!CfgAvail ()) {
343         CfgSetName (MakeFilename (InFile, CfgExt));
344     }
345
346     /* Try to read the configuration file */
347     CfgRead ();
348
349     /* Make the output file name from the input file name if none was given */
350     if (OutFile == 0) {
351         OutFile = MakeFilename (InFile, OutExt);
352     }
353
354     /* Load the input file */
355     LoadCode (InFile, 0xE000);  /* ### */
356
357     /* Open the output file */
358     OpenOutput (OutFile);
359
360     /* Disassemble the code */
361     Disassemble ();
362
363     /* Close the output file */
364     CloseOutput ();
365
366     /* Done */
367     return EXIT_SUCCESS;
368 }
369
370
371