]> git.sur5r.net Git - cc65/blob - src/da65/output.c
New generic hash table module
[cc65] / src / da65 / output.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 output.c                                  */
4 /*                                                                           */
5 /*                       Disassembler output routines                        */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 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 "cpu.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-2003 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     /* If we have a name given, open the output file, otherwise use stdout */
91     if (Name != 0) {
92         F = fopen (Name, "w");
93         if (F == 0) {
94             Error ("Cannot open `%s': %s", Name, strerror (errno));
95         }
96     } else {
97         F = stdout;
98     }
99
100     /* Output the header and initialize stuff */
101     PageHeader ();
102     Line = 4;
103     Col  = 1;
104 }
105
106
107
108 void CloseOutput (void)
109 /* Close the output file */
110 {
111     if (F != stdout && fclose (F) != 0) {
112         Error ("Error closing output file: %s", strerror (errno));
113     }
114 }
115
116
117
118 void Output (const char* Format, ...)
119 /* Write to the output file */
120 {
121     if (Pass == PassCount) {
122         va_list ap;
123         va_start (ap, Format);
124         Col += vfprintf (F, Format, ap);
125         va_end (ap);
126     }
127 }
128
129
130
131 void Indent (unsigned N)
132 /* Make sure the current line column is at position N (zero based) */
133 {
134     if (Pass == PassCount) {
135         while (Col < N) {
136             fputc (' ', F);
137             ++Col;
138         }
139     }
140 }
141
142
143
144 void LineFeed (void)
145 /* Add a linefeed to the output file */
146 {
147     if (Pass == PassCount) {
148         fputc ('\n', F);
149         if (PageLength > 0 && ++Line >= PageLength) {
150             if (FormFeeds) {
151                 fputc ('\f', F);
152             }
153             ++Page;
154             PageHeader ();
155             Line = 4;
156         }
157         Col = 1;
158     }
159 }
160
161
162
163 void DefLabel (const char* Name)
164 /* Define a label with the given name */
165 {
166     Output ("%s:", Name);
167     /* Don't start a new line if the label is fully in the left column */
168     if (Col > MIndent) {
169         LineFeed ();
170     }
171 }
172
173
174
175 void DataByteLine (unsigned ByteCount)
176 /* Output a line with bytes */
177 {
178     unsigned I;
179
180     Indent (MIndent);
181     Output (".byte");
182     Indent (AIndent);
183     for (I = 0; I < ByteCount; ++I) {
184         if (I > 0) {
185             Output (",$%02X", CodeBuf[PC+I]);
186         } else {
187             Output ("$%02X", CodeBuf[PC+I]);
188         }
189     }
190     LineComment (PC, ByteCount);
191     LineFeed ();
192 }
193
194
195
196 void DataDByteLine (unsigned ByteCount)
197 /* Output a line with dbytes */
198 {
199     unsigned I;
200
201     Indent (MIndent);
202     Output (".dbyte");
203     Indent (AIndent);
204     for (I = 0; I < ByteCount; I += 2) {
205         if (I > 0) {
206             Output (",$%04X", GetCodeDByte (PC+I));
207         } else {
208             Output ("$%04X", GetCodeDByte (PC+I));
209         }
210     }
211     LineComment (PC, ByteCount);
212     LineFeed ();
213 }
214
215
216
217 void DataWordLine (unsigned ByteCount)
218 /* Output a line with words */
219 {
220     unsigned I;
221
222     Indent (MIndent);
223     Output (".word");
224     Indent (AIndent);
225     for (I = 0; I < ByteCount; I += 2) {
226         if (I > 0) {
227             Output (",$%04X", GetCodeWord (PC+I));
228         } else {
229             Output ("$%04X", GetCodeWord (PC+I));
230         }
231     }
232     LineComment (PC, ByteCount);
233     LineFeed ();
234 }
235
236
237
238 void DataDWordLine (unsigned ByteCount)
239 /* Output a line with dwords */
240 {
241     unsigned I;
242
243     Indent (MIndent);
244     Output (".dword");
245     Indent (AIndent);
246     for (I = 0; I < ByteCount; I += 4) {
247         if (I > 0) {
248             Output (",$%08lX", GetCodeDWord (PC+I));
249         } else {
250             Output ("$%08lX", GetCodeDWord (PC+I));
251         }
252     }
253     LineComment (PC, ByteCount);
254     LineFeed ();
255 }
256
257
258
259 void SeparatorLine (void)
260 /* Print a separator line */
261 {
262     if (Pass == PassCount && Comments >= 1) {
263         Output ("; ----------------------------------------------------------------------------");
264         LineFeed ();
265     }
266 }
267
268
269
270 void LineComment (unsigned PC, unsigned Count)
271 /* Add a line comment with the PC and data bytes */
272 {
273     unsigned I;
274
275     if (Pass == PassCount && Comments >= 2) {
276         Indent (CIndent);
277         Output ("; %04X", PC);
278         if (Comments >= 3) {
279             for (I = 0; I < Count; ++I) {
280                 Output (" %02X", CodeBuf [PC+I]);
281             }
282             if (Comments >= 4) {
283                 Indent (TIndent);
284                 for (I = 0; I < Count; ++I) {
285                     unsigned char C = CodeBuf [PC+I];
286                     if (!isprint (C)) {
287                         C = '.';
288                     }
289                     Output ("%c", C);
290                 }
291             }
292         }
293     }
294 }
295
296
297
298 void OutputSettings (void)
299 /* Output CPU and other settings */
300 {
301     LineFeed ();
302     Indent (MIndent);
303     Output (".setcpu");
304     Indent (AIndent);
305     Output ("\"%s\"", CPUNames[CPU]);
306     LineFeed ();
307     LineFeed ();
308 }
309
310
311