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