1 /*****************************************************************************/
5 /* Disassembler output routines */
9 /* (C) 2000-2014, 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 */
66 static const char* SegmentName = 0; /* Name of current segment */
70 /*****************************************************************************/
72 /*****************************************************************************/
76 static void PageHeader (void)
77 /* Print a page header */
84 GetVersionAsString (),
92 void OpenOutput (const char* Name)
93 /* Open the given file for output */
95 /* If we have a name given, open the output file, otherwise use stdout */
97 F = fopen (Name, "w");
99 Error ("Cannot open '%s': %s", Name, strerror (errno));
105 /* Output the header and initialize stuff */
113 void CloseOutput (void)
114 /* Close the output file */
116 if (F != stdout && fclose (F) != 0) {
117 Error ("Error closing output file: %s", strerror (errno));
123 void Output (const char* Format, ...)
124 /* Write to the output file */
126 if (Pass == PassCount) {
128 va_start (ap, Format);
129 Col += vfprintf (F, Format, ap);
136 void Indent (unsigned N)
137 /* Make sure the current line column is at position N (zero based) */
139 if (Pass == PassCount) {
150 /* Add a linefeed to the output file */
152 if (Pass == PassCount) {
154 if (PageLength > 0 && ++Line >= PageLength) {
168 void DefLabel (const char* Name)
169 /* Define a label with the given name */
171 Output ("%s:", Name);
172 /* If the label is longer than the configured maximum, or if it runs into
173 ** the opcode column, start a new line.
175 if (Col > LBreak+2 || Col > MCol) {
182 void DefForward (const char* Name, const char* Comment, unsigned Offs)
183 /* Define a label as "* + x", where x is the offset relative to the
187 if (Pass == PassCount) {
188 /* Flush existing output if necessary */
193 /* Output the forward definition */
197 Output (":= * + $%04X", Offs);
199 Output (":= * + %u", Offs);
203 Output ("; %s", Comment);
211 void DefConst (const char* Name, const char* Comment, unsigned Addr)
212 /* Define an address constant */
214 if (Pass == PassCount) {
217 Output (":= $%04X", Addr);
220 Output ("; %s", Comment);
228 void DataByteLine (unsigned ByteCount)
229 /* Output a line with bytes */
236 for (I = 0; I < ByteCount; ++I) {
238 Output (",$%02X", CodeBuf[PC+I]);
240 Output ("$%02X", CodeBuf[PC+I]);
243 LineComment (PC, ByteCount);
249 void DataDByteLine (unsigned ByteCount)
250 /* Output a line with dbytes */
257 for (I = 0; I < ByteCount; I += 2) {
259 Output (",$%04X", GetCodeDByte (PC+I));
261 Output ("$%04X", GetCodeDByte (PC+I));
264 LineComment (PC, ByteCount);
270 void DataWordLine (unsigned ByteCount)
271 /* Output a line with words */
278 for (I = 0; I < ByteCount; I += 2) {
280 Output (",$%04X", GetCodeWord (PC+I));
282 Output ("$%04X", GetCodeWord (PC+I));
285 LineComment (PC, ByteCount);
291 void DataDWordLine (unsigned ByteCount)
292 /* Output a line with dwords */
299 for (I = 0; I < ByteCount; I += 4) {
301 Output (",$%08lX", GetCodeDWord (PC+I));
303 Output ("$%08lX", GetCodeDWord (PC+I));
306 LineComment (PC, ByteCount);
312 void SeparatorLine (void)
313 /* Print a separator line */
315 if (Pass == PassCount && Comments >= 1) {
316 Output ("; ----------------------------------------------------------------------------");
323 void StartSegment (const char* Name, unsigned AddrSize)
324 /* Start a segment */
326 if (Pass == PassCount) {
331 Output ("\"%s\"", Name);
332 if (AddrSize != ADDR_SIZE_DEFAULT) {
333 Output (": %s", AddrSizeToStr (AddrSize));
342 void EndSegment (void)
346 Output ("; End of \"%s\" segment", SegmentName);
356 void UserComment (const char* Comment)
357 /* Output a comment line */
359 Output ("; %s", Comment);
365 void LineComment (unsigned PC, unsigned Count)
366 /* Add a line comment with the PC and data bytes */
370 if (Pass == PassCount && Comments >= 2) {
372 Output ("; %04X", PC);
374 for (I = 0; I < Count; ++I) {
375 Output (" %02X", CodeBuf [PC+I]);
379 for (I = 0; I < Count; ++I) {
380 unsigned char C = CodeBuf [PC+I];
393 void OutputSettings (void)
394 /* Output CPU and other settings */
400 Output ("\"%s\"", CPUNames[CPU]);