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