1 /*****************************************************************************/
5 /* Disassembler output routines */
9 /* (C) 2000-2006 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
54 /*****************************************************************************/
56 /*****************************************************************************/
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 */
67 /*****************************************************************************/
69 /*****************************************************************************/
73 static void PageHeader (void)
74 /* Print a page header */
77 "; da65 V%u.%u.%u - (C) Copyright 2000-2005, Ullrich von Bassewitz\n"
81 VER_MAJOR, VER_MINOR, VER_PATCH,
89 void OpenOutput (const char* Name)
90 /* Open the given file for output */
92 /* If we have a name given, open the output file, otherwise use stdout */
94 F = fopen (Name, "w");
96 Error ("Cannot open `%s': %s", Name, strerror (errno));
102 /* Output the header and initialize stuff */
110 void CloseOutput (void)
111 /* Close the output file */
113 if (F != stdout && fclose (F) != 0) {
114 Error ("Error closing output file: %s", strerror (errno));
120 void Output (const char* Format, ...)
121 /* Write to the output file */
123 if (Pass == PassCount) {
125 va_start (ap, Format);
126 Col += vfprintf (F, Format, ap);
133 void Indent (unsigned N)
134 /* Make sure the current line column is at position N (zero based) */
136 if (Pass == PassCount) {
147 /* Add a linefeed to the output file */
149 if (Pass == PassCount) {
151 if (PageLength > 0 && ++Line >= PageLength) {
165 void DefLabel (const char* Name)
166 /* Define a label with the given name */
168 Output ("%s:", Name);
169 /* If the label is longer than the configured maximum, or if it runs into
170 * the opcode column, start a new line.
172 if (Col > LBreak+2 || Col > MIndent) {
179 void DefForward (const char* Name, const char* Comment, unsigned Offs)
180 /* Define a label as "* + x", where x is the offset relative to the
184 if (Pass == PassCount) {
188 Output (":= * + $%04X", Offs);
190 Output (":= * + %u", Offs);
194 Output ("; %s", Comment);
202 void DefineConst (const char* Name, const char* Comment, unsigned Addr)
203 /* Define an address constant */
205 if (Pass == PassCount) {
208 Output (":= $%04X", Addr);
211 Output ("; %s", Comment);
219 void DataByteLine (unsigned ByteCount)
220 /* Output a line with bytes */
227 for (I = 0; I < ByteCount; ++I) {
229 Output (",$%02X", CodeBuf[PC+I]);
231 Output ("$%02X", CodeBuf[PC+I]);
234 LineComment (PC, ByteCount);
240 void DataDByteLine (unsigned ByteCount)
241 /* Output a line with dbytes */
248 for (I = 0; I < ByteCount; I += 2) {
250 Output (",$%04X", GetCodeDByte (PC+I));
252 Output ("$%04X", GetCodeDByte (PC+I));
255 LineComment (PC, ByteCount);
261 void DataWordLine (unsigned ByteCount)
262 /* Output a line with words */
269 for (I = 0; I < ByteCount; I += 2) {
271 Output (",$%04X", GetCodeWord (PC+I));
273 Output ("$%04X", GetCodeWord (PC+I));
276 LineComment (PC, ByteCount);
282 void DataDWordLine (unsigned ByteCount)
283 /* Output a line with dwords */
290 for (I = 0; I < ByteCount; I += 4) {
292 Output (",$%08lX", GetCodeDWord (PC+I));
294 Output ("$%08lX", GetCodeDWord (PC+I));
297 LineComment (PC, ByteCount);
303 void SeparatorLine (void)
304 /* Print a separator line */
306 if (Pass == PassCount && Comments >= 1) {
307 Output ("; ----------------------------------------------------------------------------");
314 void UserComment (const char* Comment)
315 /* Output a comment line */
317 Output ("; %s", Comment);
323 void LineComment (unsigned PC, unsigned Count)
324 /* Add a line comment with the PC and data bytes */
328 if (Pass == PassCount && Comments >= 2) {
330 Output ("; %04X", PC);
332 for (I = 0; I < Count; ++I) {
333 Output (" %02X", CodeBuf [PC+I]);
337 for (I = 0; I < Count; ++I) {
338 unsigned char C = CodeBuf [PC+I];
351 void OutputSettings (void)
352 /* Output CPU and other settings */
358 Output ("\"%s\"", CPUNames[CPU]);