2 // RMAC - Renamed Macro Assembler for all Atari computers
3 // LISTING.C - Listing Output
4 // Copyright (C) 199x Landon Dyer, 2011-2021 Reboot and Friends
5 // RMAC derived from MADMAC v1.07 Written by Landon Dyer, 1986
6 // Source utilised with the kind permission of Landon Dyer
8 // --------------------------------------------------------------------------
9 // 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7
10 // 012345678901234567890123456789012345678901234567890123456789012345678901234567
11 // filename.... Renamed Macro Assembler N.N.NN (Unknown)
12 // nnnnn aaaaaaaa dddddddddddddddddddd T source code
13 // nnnnn aaaaaaaa dddddddddddddddd
23 char * list_fname; // Listing filename
24 uint8_t subttl[TITLESIZ]; // Current subtitle
25 int listing; // Listing level
26 int pagelen = 61; // Lines on a page
27 int nlines; // #lines on page so far
28 LONG lsloc; // `sloc' at start of line
31 static int lcursect; // `cursect' at start of line
32 static int llineno; // `curlineno' at start of line
33 static int pageno; // Current page number
34 static int pagewidth; // #columns on a page
35 static int subflag; // 0, don't do .eject on subttl (set 1)
36 static char lnimage[IMAGESIZ]; // Image of output line
37 static char title[TITLESIZ]; // Current title
38 static char datestr[20]; // Current date dd-mon-yyyy
39 static char timestr[20]; // Current time hh:mm:ss [am|pm]
40 static char buf[IMAGESIZ]; // Buffer for numbers
41 static long unused; // For supressing 'write' warnings
43 static char * month[16] = {
44 "", "Jan", "Feb", "Mar",
45 "Apr", "May", "Jun", "Jul",
46 "Aug", "Sep", "Oct", "Nov",
52 // Eject the page, reset the line count and bump the page number
67 // Return GEMDOS format date
69 uint32_t dos_date(void)
76 tm = localtime(&tloc);
77 v = ((tm->tm_year - 80) << 9) | ((tm->tm_mon + 1) << 5) | tm->tm_mday;
84 // Return GEMDOS format time
86 uint32_t dos_time(void)
93 tm = localtime(&tloc);
94 v = (tm->tm_hour << 11) | (tm->tm_min) << 5 | tm->tm_sec;
101 // Generate a time string
103 void time_string(char * buf, uint32_t time)
118 sprintf(buf, "%d:%02d:%02d %s",
119 hour, (int)((time >> 5) & 0x3F), (int)((time & 0x1F) << 1), ampm);
124 // Generate a date string
126 void date_string(char * buf, uint32_t date)
128 sprintf(buf, "%d-%s-%d",
129 (int)(date & 0x1F), month[(date >> 5) & 0xF], (int)((date >> 9) + 1980));
134 // Transform letters a-f in the address and data columns of the listing to
135 // uppercase. (People seem to like uppercase hex better in assembly-language
138 void uc_ln(char * ln)
143 for(i=LOC_COL; i<SRC_COL; ++i)
145 if ((j = ln[i]) >= 'a' && j <= 'f')
146 ln[i] = (char)(j - 0x20);
152 // Fill region 'dest' with 'len' characters 'c' and null terminate the region
154 void lnfill(char * dest, int len, char chr)
164 // Create listing file with the appropriate name
166 void list_setup(void)
170 strcpy(fnbuf, list_fname);
174 strcpy(fnbuf, firstfname);
175 fext(fnbuf, ".prn", 1);
180 if ((list_fd = open(fnbuf, _OPEN_FLAGS, _PERM_MODE)) < 0)
181 CantCreateFile(fnbuf);
186 // Tag listing with a character, typically for errors or warnings
188 void taglist(char chr)
190 lnimage[TAG_COL + 1] = chr;
195 // Print a line to the listing file
197 void println(const char * ln)
201 // Create listing file, if necessary
202 if (list_fname != NULL)
206 unused = write(list_fd, ln, length);
207 unused = write(list_fd, "\n", 1L);
212 // Ship line 'ln' out; do page breaks and title stuff
214 void ship_ln(const char * ln)
216 // If listing level is <= 0, then don't print anything
222 // Notice bottom of page
223 if (nlines >= pagelen - BOT_MAR)
226 // Print title, boilerplate, and subtitle at top of page
231 date_string(datestr, dos_date());
232 time_string(timestr, dos_time());
234 "%-40s%-20s Page %-4d %s %s RMAC %01i.%01i.%02i (%s)",
235 title, curfname, pageno, timestr, datestr, MAJOR, MINOR, PATCH,
238 sprintf(buf, "%s", subttl);
251 // Initialize listing generator
253 void InitListing(void)
262 date_string(datestr, dos_date());
263 time_string(timestr, dos_time());
278 DEBUG printf("~list: lsloc=$%X sloc=$%X\n", lsloc, sloc);
282 sprintf(buf, "%08X", lsloc);
283 strncpy(lnimage + LOC_COL, buf, 8);
286 if (llineno != curlineno)
288 sprintf(buf, "%5d", llineno);
289 strncpy(lnimage + LN_COL, buf, 5);
292 // List bytes only when section stayed the same and the section is not a
293 // "no-data" (SBSS) section. An extra annoyance is caused by "ds.b" in a
294 // microprocessor mode, which prints out bytes of zero as if they had been
295 // deposited with dcb. The fix (kludge) is an extra variable which records
296 // the fact that a 'ds.x' directive generated all the data, and it
297 // shouldn't be listed
298 SaveSection(); // Update section variables
300 if (lcursect == cursect && (sect[lcursect].scattr & SBSS) == 0
301 && lsloc != sloc && just_bss == 0)
303 ch = sect[lcursect].sfcode;
305 if (lcursect != M6502)
307 for(; ch!=NULL; ch=ch->chnext)
309 if (lsloc >= ch->chloc && lsloc < (ch->chloc + ch->ch_size))
314 // Fatal: Can't find chunk holding code
318 interror(6); // Can't find generated code in section
321 p = ch->chptr + (lsloc - ch->chloc);
325 for(count=sloc-lsloc; count--; col+=2, ++lsloc)
333 lnfill(lnimage, SRC_COL, SPACE); // Fill with spaces
334 sprintf(buf, "%08X", lsloc);
335 strncpy(lnimage + LOC_COL, buf, 8);
338 if (lcursect != M6502 &&
339 lsloc >= (ch->chloc + ch->ch_size))
341 if ((ch = ch->chnext) == NULL)
348 fixcount = fixtest(lcursect, lsloc);
354 p++; // Advance anyway
357 sprintf(buf, "%02x", *p++ & 0xff);
359 strncpy(lnimage + col, buf, 2);
362 // Flush partial line
378 // Copy current (saved) line to output buffer and tag it with a character
380 void lstout(char tag)
389 lnfill(lnimage, SRC_COL, SPACE); // Fill with spaces
390 lnimage[TAG_COL] = tag;
392 // Copy line image and handle control characters
393 d = lnimage + SRC_COL;
395 for(s=lnbuf; *s; ++s)
397 if (*s >= 0x20 || *s == '\t')
402 *d++ = (char)(*s + 0x40);
411 // Output a value to listing
413 int listvalue(uint32_t v)
415 sprintf(buf, "=%08X", v);
416 strncpy(lnimage + DATA_COL - 1, buf, 9);
422 * .subttl [-] "string"
425 * o leading '-' supresses page eject
426 * o don't .eject on first .subttl, but do so on all other ones,
427 * o arrange not to print the .subttl directive
442 return error("missing string");
444 // strcpy(subttl, (char *)tok[1]);
445 strcpy(subttl, string[tok[1]]);
449 // Always eject on pages 2+
450 if (ejectok && (subflag || pageno > 1))
460 // Set title on titles not on the first page, do an eject and clobber the
466 return error("missing string");
468 // strcpy(title, (char*)tok[1]);
469 strcpy(title, string[tok[1]]);