1 /*****************************************************************************/
5 /* Disassembler output routines */
9 /* (C) 2000-2009, 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 */
82 GetVersionAsString (),
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) {
186 /* Flush existing output if necessary */
191 /* Output the forward definition */
195 Output (":= * + $%04X", Offs);
197 Output (":= * + %u", Offs);
201 Output ("; %s", Comment);
209 void DefConst (const char* Name, const char* Comment, unsigned Addr)
210 /* Define an address constant */
212 if (Pass == PassCount) {
215 Output (":= $%04X", Addr);
218 Output ("; %s", Comment);
226 void StartSegment (const char* Name, unsigned AddrSize)
227 /* Start a segment */
229 if (Pass == PassCount) {
232 if (AddrSize == ADDR_SIZE_DEFAULT) {
233 Output ("\"%s\"", Name);
235 Output ("\"%s\": %s", Name, AddrSizeToStr (AddrSize));
243 void DataByteLine (unsigned ByteCount)
244 /* Output a line with bytes */
251 for (I = 0; I < ByteCount; ++I) {
253 Output (",$%02X", CodeBuf[PC+I]);
255 Output ("$%02X", CodeBuf[PC+I]);
258 LineComment (PC, ByteCount);
264 void DataDByteLine (unsigned ByteCount)
265 /* Output a line with dbytes */
272 for (I = 0; I < ByteCount; I += 2) {
274 Output (",$%04X", GetCodeDByte (PC+I));
276 Output ("$%04X", GetCodeDByte (PC+I));
279 LineComment (PC, ByteCount);
285 void DataWordLine (unsigned ByteCount)
286 /* Output a line with words */
293 for (I = 0; I < ByteCount; I += 2) {
295 Output (",$%04X", GetCodeWord (PC+I));
297 Output ("$%04X", GetCodeWord (PC+I));
300 LineComment (PC, ByteCount);
306 void DataDWordLine (unsigned ByteCount)
307 /* Output a line with dwords */
314 for (I = 0; I < ByteCount; I += 4) {
316 Output (",$%08lX", GetCodeDWord (PC+I));
318 Output ("$%08lX", GetCodeDWord (PC+I));
321 LineComment (PC, ByteCount);
327 void SeparatorLine (void)
328 /* Print a separator line */
330 if (Pass == PassCount && Comments >= 1) {
331 Output ("; ----------------------------------------------------------------------------");
338 void UserComment (const char* Comment)
339 /* Output a comment line */
341 Output ("; %s", Comment);
347 void LineComment (unsigned PC, unsigned Count)
348 /* Add a line comment with the PC and data bytes */
352 if (Pass == PassCount && Comments >= 2) {
354 Output ("; %04X", PC);
356 for (I = 0; I < Count; ++I) {
357 Output (" %02X", CodeBuf [PC+I]);
361 for (I = 0; I < Count; ++I) {
362 unsigned char C = CodeBuf [PC+I];
375 void OutputSettings (void)
376 /* Output CPU and other settings */
382 Output ("\"%s\"", CPUNames[CPU]);