1 /*****************************************************************************/
5 /* Disassembler output routines */
9 /* (C) 2000-2007 Ullrich von Bassewitz */
10 /* Roemerstrasse 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 /*****************************************************************************/
55 /*****************************************************************************/
57 /*****************************************************************************/
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 */
68 /*****************************************************************************/
70 /*****************************************************************************/
74 static void PageHeader (void)
75 /* Print a page header */
78 "; da65 V%u.%u.%u - (C) Copyright 2000-2005, Ullrich von Bassewitz\n"
82 VER_MAJOR, VER_MINOR, VER_PATCH,
90 void OpenOutput (const char* Name)
91 /* Open the given file for output */
93 /* If we have a name given, open the output file, otherwise use stdout */
95 F = fopen (Name, "w");
97 Error ("Cannot open `%s': %s", Name, strerror (errno));
103 /* Output the header and initialize stuff */
111 void CloseOutput (void)
112 /* Close the output file */
114 if (F != stdout && fclose (F) != 0) {
115 Error ("Error closing output file: %s", strerror (errno));
121 void Output (const char* Format, ...)
122 /* Write to the output file */
124 if (Pass == PassCount) {
126 va_start (ap, Format);
127 Col += vfprintf (F, Format, ap);
134 void Indent (unsigned N)
135 /* Make sure the current line column is at position N (zero based) */
137 if (Pass == PassCount) {
148 /* Add a linefeed to the output file */
150 if (Pass == PassCount) {
152 if (PageLength > 0 && ++Line >= PageLength) {
166 void DefLabel (const char* Name)
167 /* Define a label with the given name */
169 Output ("%s:", Name);
170 /* If the label is longer than the configured maximum, or if it runs into
171 * the opcode column, start a new line.
173 if (Col > LBreak+2 || Col > MCol) {
180 void DefForward (const char* Name, const char* Comment, unsigned Offs)
181 /* Define a label as "* + x", where x is the offset relative to the
185 if (Pass == PassCount) {
189 Output (":= * + $%04X", Offs);
191 Output (":= * + %u", Offs);
195 Output ("; %s", Comment);
203 void DefConst (const char* Name, const char* Comment, unsigned Addr)
204 /* Define an address constant */
206 if (Pass == PassCount) {
209 Output (":= $%04X", Addr);
212 Output ("; %s", Comment);
220 void StartSegment (const char* Name, unsigned AddrSize)
221 /* Start a segment */
223 if (Pass == PassCount) {
226 if (AddrSize == ADDR_SIZE_DEFAULT) {
227 Output ("\"%s\"", Name);
229 Output ("\"%s\": %s", Name, AddrSizeToStr (AddrSize));
237 void DataByteLine (unsigned ByteCount)
238 /* Output a line with bytes */
245 for (I = 0; I < ByteCount; ++I) {
247 Output (",$%02X", CodeBuf[PC+I]);
249 Output ("$%02X", CodeBuf[PC+I]);
252 LineComment (PC, ByteCount);
258 void DataDByteLine (unsigned ByteCount)
259 /* Output a line with dbytes */
266 for (I = 0; I < ByteCount; I += 2) {
268 Output (",$%04X", GetCodeDByte (PC+I));
270 Output ("$%04X", GetCodeDByte (PC+I));
273 LineComment (PC, ByteCount);
279 void DataWordLine (unsigned ByteCount)
280 /* Output a line with words */
287 for (I = 0; I < ByteCount; I += 2) {
289 Output (",$%04X", GetCodeWord (PC+I));
291 Output ("$%04X", GetCodeWord (PC+I));
294 LineComment (PC, ByteCount);
300 void DataDWordLine (unsigned ByteCount)
301 /* Output a line with dwords */
308 for (I = 0; I < ByteCount; I += 4) {
310 Output (",$%08lX", GetCodeDWord (PC+I));
312 Output ("$%08lX", GetCodeDWord (PC+I));
315 LineComment (PC, ByteCount);
321 void SeparatorLine (void)
322 /* Print a separator line */
324 if (Pass == PassCount && Comments >= 1) {
325 Output ("; ----------------------------------------------------------------------------");
332 void UserComment (const char* Comment)
333 /* Output a comment line */
335 Output ("; %s", Comment);
341 void LineComment (unsigned PC, unsigned Count)
342 /* Add a line comment with the PC and data bytes */
346 if (Pass == PassCount && Comments >= 2) {
348 Output ("; %04X", PC);
350 for (I = 0; I < Count; ++I) {
351 Output (" %02X", CodeBuf [PC+I]);
355 for (I = 0; I < Count; ++I) {
356 unsigned char C = CodeBuf [PC+I];
369 void OutputSettings (void)
370 /* Output CPU and other settings */
376 Output ("\"%s\"", CPUNames[CPU]);