]> git.sur5r.net Git - cc65/blob - src/da65/output.c
Removed (pretty inconsistently used) tab chars from source code base.
[cc65] / src / da65 / output.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 output.c                                  */
4 /*                                                                           */
5 /*                       Disassembler output routines                        */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-2009, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
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 <stdarg.h>
38 #include <string.h>
39 #include <ctype.h>
40 #include <errno.h>
41
42 /* common */
43 #include "addrsize.h"
44 #include "cpu.h"
45 #include "version.h"
46
47 /* da65 */
48 #include "code.h"
49 #include "error.h"
50 #include "global.h"
51 #include "output.h"
52
53
54
55 /*****************************************************************************/
56 /*                                   Data                                    */
57 /*****************************************************************************/
58
59
60
61 static FILE*    F       = 0;            /* Output stream */
62 static unsigned Col     = 1;            /* Current column */
63 static unsigned Line    = 0;            /* Current line on page */
64 static unsigned Page    = 1;            /* Current output page */
65
66
67
68 /*****************************************************************************/
69 /*                                   Code                                    */
70 /*****************************************************************************/
71
72
73
74 static void PageHeader (void)
75 /* Print a page header */
76 {
77     fprintf (F,
78              "; da65 V%s\n"
79              "; Created:    %s\n"
80              "; Input file: %s\n"
81              "; Page:       %u\n\n",
82              GetVersionAsString (),
83              Now,
84              InFile,
85              Page);
86 }
87
88
89
90 void OpenOutput (const char* Name)
91 /* Open the given file for output */
92 {
93     /* If we have a name given, open the output file, otherwise use stdout */
94     if (Name != 0) {
95         F = fopen (Name, "w");
96         if (F == 0) {
97             Error ("Cannot open `%s': %s", Name, strerror (errno));
98         }
99     } else {
100         F = stdout;
101     }
102
103     /* Output the header and initialize stuff */
104     PageHeader ();
105     Line = 5;
106     Col  = 1;
107 }
108
109
110
111 void CloseOutput (void)
112 /* Close the output file */
113 {
114     if (F != stdout && fclose (F) != 0) {
115         Error ("Error closing output file: %s", strerror (errno));
116     }
117 }
118
119
120
121 void Output (const char* Format, ...)
122 /* Write to the output file */
123 {
124     if (Pass == PassCount) {
125         va_list ap;
126         va_start (ap, Format);
127         Col += vfprintf (F, Format, ap);
128         va_end (ap);
129     }
130 }
131
132
133
134 void Indent (unsigned N)
135 /* Make sure the current line column is at position N (zero based) */
136 {
137     if (Pass == PassCount) {
138         while (Col < N) {
139             fputc (' ', F);
140             ++Col;
141         }
142     }
143 }
144
145
146
147 void LineFeed (void)
148 /* Add a linefeed to the output file */
149 {
150     if (Pass == PassCount) {
151         fputc ('\n', F);
152         if (PageLength > 0 && ++Line >= PageLength) {
153             if (FormFeeds) {
154                 fputc ('\f', F);
155             }
156             ++Page;
157             PageHeader ();
158             Line = 5;
159         }
160         Col = 1;
161     }
162 }
163
164
165
166 void DefLabel (const char* Name)
167 /* Define a label with the given name */
168 {
169     Output ("%s:", Name);
170     /* If the label is longer than the configured maximum, or if it runs into
171      * the opcode column, start a new line.
172      */
173     if (Col > LBreak+2 || Col > MCol) {
174         LineFeed ();
175     }
176 }
177
178
179
180 void DefForward (const char* Name, const char* Comment, unsigned Offs)
181 /* Define a label as "* + x", where x is the offset relative to the
182  * current PC.
183  */
184 {
185     if (Pass == PassCount) {
186         /* Flush existing output if necessary */
187         if (Col > 1) {
188             LineFeed ();
189         }
190
191         /* Output the forward definition */
192         Output ("%s", Name);
193         Indent (ACol);
194         if (UseHexOffs) {
195             Output (":= * + $%04X", Offs);
196         } else {
197             Output (":= * + %u", Offs);
198         }
199         if (Comment) {
200             Indent (CCol);
201             Output ("; %s", Comment);
202         }
203         LineFeed ();
204     }
205 }
206
207
208
209 void DefConst (const char* Name, const char* Comment, unsigned Addr)
210 /* Define an address constant */
211 {
212     if (Pass == PassCount) {
213         Output ("%s", Name);
214         Indent (ACol);
215         Output (":= $%04X", Addr);
216         if (Comment) {
217             Indent (CCol);
218             Output ("; %s", Comment);
219         }
220         LineFeed ();
221     }
222 }
223
224
225
226 void StartSegment (const char* Name, unsigned AddrSize)
227 /* Start a segment */
228 {
229     if (Pass == PassCount) {
230         Output (".segment");
231         Indent (ACol);
232         if (AddrSize == ADDR_SIZE_DEFAULT) {
233             Output ("\"%s\"", Name);
234         } else {
235             Output ("\"%s\": %s", Name, AddrSizeToStr (AddrSize));
236         }
237         LineFeed ();
238     }
239 }
240
241
242
243 void DataByteLine (unsigned ByteCount)
244 /* Output a line with bytes */
245 {
246     unsigned I;
247
248     Indent (MCol);
249     Output (".byte");
250     Indent (ACol);
251     for (I = 0; I < ByteCount; ++I) {
252         if (I > 0) {
253             Output (",$%02X", CodeBuf[PC+I]);
254         } else {
255             Output ("$%02X", CodeBuf[PC+I]);
256         }
257     }
258     LineComment (PC, ByteCount);
259     LineFeed ();
260 }
261
262
263
264 void DataDByteLine (unsigned ByteCount)
265 /* Output a line with dbytes */
266 {
267     unsigned I;
268
269     Indent (MCol);
270     Output (".dbyt");
271     Indent (ACol);
272     for (I = 0; I < ByteCount; I += 2) {
273         if (I > 0) {
274             Output (",$%04X", GetCodeDByte (PC+I));
275         } else {
276             Output ("$%04X", GetCodeDByte (PC+I));
277         }
278     }
279     LineComment (PC, ByteCount);
280     LineFeed ();
281 }
282
283
284
285 void DataWordLine (unsigned ByteCount)
286 /* Output a line with words */
287 {
288     unsigned I;
289
290     Indent (MCol);
291     Output (".word");
292     Indent (ACol);
293     for (I = 0; I < ByteCount; I += 2) {
294         if (I > 0) {
295             Output (",$%04X", GetCodeWord (PC+I));
296         } else {
297             Output ("$%04X", GetCodeWord (PC+I));
298         }
299     }
300     LineComment (PC, ByteCount);
301     LineFeed ();
302 }
303
304
305
306 void DataDWordLine (unsigned ByteCount)
307 /* Output a line with dwords */
308 {
309     unsigned I;
310
311     Indent (MCol);
312     Output (".dword");
313     Indent (ACol);
314     for (I = 0; I < ByteCount; I += 4) {
315         if (I > 0) {
316             Output (",$%08lX", GetCodeDWord (PC+I));
317         } else {
318             Output ("$%08lX", GetCodeDWord (PC+I));
319         }
320     }
321     LineComment (PC, ByteCount);
322     LineFeed ();
323 }
324
325
326
327 void SeparatorLine (void)
328 /* Print a separator line */
329 {
330     if (Pass == PassCount && Comments >= 1) {
331         Output ("; ----------------------------------------------------------------------------");
332         LineFeed ();
333     }
334 }
335
336
337
338 void UserComment (const char* Comment)
339 /* Output a comment line */
340 {
341     Output ("; %s", Comment);
342     LineFeed ();
343 }
344
345
346
347 void LineComment (unsigned PC, unsigned Count)
348 /* Add a line comment with the PC and data bytes */
349 {
350     unsigned I;
351
352     if (Pass == PassCount && Comments >= 2) {
353         Indent (CCol);
354         Output ("; %04X", PC);
355         if (Comments >= 3) {
356             for (I = 0; I < Count; ++I) {
357                 Output (" %02X", CodeBuf [PC+I]);
358             }
359             if (Comments >= 4) {
360                 Indent (TCol);
361                 for (I = 0; I < Count; ++I) {
362                     unsigned char C = CodeBuf [PC+I];
363                     if (!isprint (C)) {
364                         C = '.';
365                     }
366                     Output ("%c", C);
367                 }
368             }
369         }
370     }
371 }
372
373
374
375 void OutputSettings (void)
376 /* Output CPU and other settings */
377 {
378     LineFeed ();
379     Indent (MCol);
380     Output (".setcpu");
381     Indent (ACol);
382     Output ("\"%s\"", CPUNames[CPU]);
383     LineFeed ();
384     LineFeed ();
385 }
386
387
388