]> git.sur5r.net Git - cc65/blob - src/da65/output.c
6f3e3f963a9e96f61a6f34cc9ccfb184865e93ce
[cc65] / src / da65 / output.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 output.c                                  */
4 /*                                                                           */
5 /*                       Disassembler output routines                        */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 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 <stdarg.h>
38 #include <string.h>
39 #include <ctype.h>
40 #include <errno.h>
41
42 /* common */
43 #include "version.h"
44
45 /* da65 */
46 #include "code.h"
47 #include "error.h"
48 #include "global.h"
49 #include "output.h"
50
51
52
53 /*****************************************************************************/
54 /*                                   Data                                    */
55 /*****************************************************************************/
56
57
58
59 static FILE*    F       = 0;            /* Output stream */
60 static unsigned Col     = 1;            /* Current column */
61 static unsigned Line    = 0;            /* Current line on page */
62 static unsigned Page    = 1;            /* Current output page */
63
64
65
66 /*****************************************************************************/
67 /*                                   Code                                    */
68 /*****************************************************************************/
69
70
71
72 static void PageHeader (void)
73 /* Print a page header */
74 {
75     fprintf (F,
76              "; da65 V%u.%u.%u - (C) Copyright 2000 Ullrich von Bassewitz\n"
77              "; Input file: %s\n"
78              "; Page:       %u\n\n",
79              VER_MAJOR, VER_MINOR, VER_PATCH,
80              InFile,
81              Page);
82 }
83
84
85
86 void OpenOutput (const char* Name)
87 /* Open the given file for output */
88 {
89     /* Open the output file */
90     F = fopen (Name, "w");
91     if (F == 0) {
92         Error ("Cannot open `%s': %s", Name, strerror (errno));
93     }
94     PageHeader ();
95     Line = 4;
96     Col  = 1;
97 }
98
99
100
101 void CloseOutput (void)
102 /* Close the output file */
103 {
104     if (fclose (F) != 0) {
105         Error ("Error closing output file: %s", strerror (errno));
106     }
107 }
108
109
110
111 void Output (const char* Format, ...)
112 /* Write to the output file */
113 {
114     if (Pass == PassCount) {
115         va_list ap;
116         va_start (ap, Format);
117         Col += vfprintf (F, Format, ap);
118         va_end (ap);
119     }
120 }
121
122
123
124 void Indent (unsigned N)
125 /* Make sure the current line column is at position N (zero based) */
126 {
127     if (Pass == PassCount) {
128         while (Col < N) {
129             fputc (' ', F);
130             ++Col;
131         }
132     }
133 }
134
135
136
137 void LineFeed (void)
138 /* Add a linefeed to the output file */
139 {
140     if (Pass == PassCount) {
141         fputc ('\n', F);
142         if (++Line >= PageLength) {
143             if (FormFeeds) {
144                 fputc ('\f', F);
145             }
146             ++Page;
147             PageHeader ();
148             Line = 4;
149         }
150         Col = 1;
151     }
152 }
153
154
155
156 void DefLabel (const char* Name)
157 /* Define a label with the given name */
158 {
159     Output ("%s:", Name);
160     /* Don't start a new line if the label is fully in the left column */
161     if (Col > MIndent) {
162         LineFeed ();
163     }
164 }
165
166
167
168 void DataByteLine (unsigned ByteCount)
169 /* Output a line with bytes */
170 {
171     unsigned I;
172
173     Indent (MIndent);
174     Output (".byte");
175     Indent (AIndent);
176     for (I = 0; I < ByteCount; ++I) {
177         if (I > 0) {
178             Output (",$%02X", CodeBuf[PC+I]);
179         } else {
180             Output ("$%02X", CodeBuf[PC+I]);
181         }
182     }
183     LineComment (PC, ByteCount);
184     LineFeed ();
185 }
186
187
188
189 void DataWordLine (unsigned ByteCount)
190 /* Output a line with words */
191 {
192     unsigned I;
193
194     Indent (MIndent);
195     Output (".word");
196     Indent (AIndent);
197     for (I = 0; I < ByteCount; I += 2) {
198         if (I > 0) {
199             Output (",$%04X", GetCodeWord (PC+I));
200         } else {
201             Output ("$%04X", GetCodeWord (PC+I));
202         }
203     }
204     LineComment (PC, ByteCount);
205     LineFeed ();
206 }
207
208
209
210 void DataDWordLine (unsigned ByteCount)
211 /* Output a line with dwords */
212 {
213     unsigned I;
214
215     Indent (MIndent);
216     Output (".dword");
217     Indent (AIndent);
218     for (I = 0; I < ByteCount; I += 4) {
219         if (I > 0) {
220             Output (",$%08lX", GetCodeDWord (PC+I));
221         } else {
222             Output ("$%08lX", GetCodeDWord (PC+I));
223         }
224     }
225     LineComment (PC, ByteCount);
226     LineFeed ();
227 }
228
229
230
231 void SeparatorLine (void)
232 /* Print a separator line */
233 {
234     if (Pass == PassCount && Verbosity >= 1) {
235         Output ("; ----------------------------------------------------------------------------");
236         LineFeed ();
237     }
238 }
239
240
241
242 void LineComment (unsigned PC, unsigned Count)
243 /* Add a line comment with the PC and data bytes */
244 {
245     unsigned I;
246
247     if (Pass == PassCount && Verbosity >= 2) {
248         Indent (CIndent);
249         Output ("; %04X", PC);
250         if (Verbosity >= 3) {
251             for (I = 0; I < Count; ++I) {
252                 Output (" %02X", CodeBuf [PC+I]);
253             }
254             if (Verbosity >= 4) {
255                 Indent (TIndent);
256                 for (I = 0; I < Count; ++I) {
257                     unsigned char C = CodeBuf [PC+I];
258                     if (!isprint (C)) {
259                         C = '.';
260                     }
261                     Output ("%c", C);
262                 }
263             }
264         }
265     }
266 }
267
268
269