2 // RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
3 // LISTING.C - Listing Output
4 // Copyright (C) 199x Landon Dyer, 2011-2012 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.... Reboot's Macro Assembler N.N.NN (Unknown)
12 // nnnnn aaaaaaaa dddddddddddddddddddd T source code
13 // nnnnn aaaaaaaa dddddddddddddddd
23 char * list_fname; // Listing filename
24 char 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
42 static char * month[16] = { "", "Jan", "Feb", "Mar",
43 "Apr", "May", "Jun", "Jul",
44 "Aug", "Sep", "Oct", "Nov",
48 // Eject the Page (Print Empty Lines), Reset the Line Count and Bump the Page Number
63 // Return GEMDOS Format Date
72 tm = localtime(&tloc);
73 v = ((tm->tm_year - 80) << 9) | ((tm->tm_mon+1) << 5) | tm->tm_mday;
80 // Return GEMDOS Format Time
89 tm = localtime(&tloc);
90 v = (tm->tm_hour << 11) | (tm->tm_min) << 5 | tm->tm_sec;
97 // Generate a Time String
99 void time_string(char * buf, VALUE time)
114 sprintf(buf, "%d:%02d:%02d %s",
115 hour, (int)((time >> 5) & 0x3F), (int)((time & 0x1F) << 1), ampm);
120 // Generate a Date String
122 void date_string(char * buf, VALUE date)
124 sprintf(buf, "%d-%s-%d",
125 (int)(date & 0x1F), month[(date >> 5) & 0xF], (int)((date >> 9) + 1980));
130 // Copy `n' Characters from `src' to `dest' (also stops on EOS in src).
131 // Does not null-terminate dest.
133 void scopy(char *dest, char *src, int len)
136 len = 1000; // Some large number
138 while (len-- && *src)
144 // Transform letters a-f in the address and data columns of the listing to
145 // uppercase. (People seem to like uppercase hex better in assembly-language
148 void uc_ln(char * ln)
153 for(i=LOC_COL; i<SRC_COL; ++i)
155 if ((j = ln[i]) >= 'a' && j <= 'f')
156 ln[i] = (char)(j - 0x20);
162 // Fill Region `dest' with `len' Characters `c' and Null Terminate the Region
164 void lnfill(char * dest, int len, char chr)
174 // Create Listing File with the Appropriate Name
176 void list_setup(void)
180 strcpy(fnbuf, list_fname);
184 strcpy(fnbuf, firstfname);
185 fext(fnbuf, ".prn", 1);
190 if ((list_fd = open(fnbuf, _OPEN_FLAGS, _PERM_MODE)) < 0)
196 // Tag Listing with a Character, Typically for Errors or Warnings
198 void taglist(char chr)
200 lnimage[TAG_COL+1] = chr;
205 // Print a Line to the Listing File
207 void println(char * ln)
211 if (list_fname != NULL) // Create listing file, if necessary
215 write(list_fd, ln, length);
216 write(list_fd, "\n", 1L);
221 // Ship Line `ln' Out; Do Page Breaks and Title Stuff
223 void ship_ln(char * ln)
225 // If listing level is <= 0, then don't print anything
229 // Notice bottom of page
230 if (nlines >= pagelen - BOT_MAR)
233 // Print title, boilerplate, and subtitle at top of page
238 date_string(datestr, dos_date());
239 time_string(timestr, dos_time());
241 "%-40s%-20s Page %-4d %s %s RMAC %01i.%01i.%02i (%s)",
242 title, curfname, pageno, timestr, datestr, MAJOR, MINOR, PATCH, PLATFORM);
244 sprintf(buf, "%s", subttl);
256 // Initialize Listing Generator
260 extern VALUE dos_date(), dos_time();
269 date_string(datestr, dos_date());
270 time_string(timestr, dos_time());
285 DEBUG printf("~list: lsloc=$%ux sloc=$%ux\n", lsloc, sloc);
289 sprintf(buf, "%08ux", lsloc);
290 scopy(lnimage+LOC_COL, buf, 8);
293 if (llineno != curlineno)
295 sprintf(buf, "%5d", llineno);
296 scopy(lnimage+LN_COL, buf, 5);
299 // List bytes only when section stayed the same and the section is not a
300 // "no-data" (SBSS) section. An extra annoyance is caused by "ds.b" in a
301 // microprocessor mode, which prints out bytes of zero as if they had been
302 // deposited with dcb. The fix (kludge) is an extra variable which records
303 // the fact that a 'ds.x' directive generated all the data, and it
304 // shouldn't be listed
305 savsect(); // Update section variables
307 if (lcursect == cursect && (sect[lcursect].scattr & SBSS) == 0
308 && lsloc != sloc && just_bss == 0)
310 ch = sect[lcursect].sfcode;
312 for(; ch!=NULL; ch=ch->chnext)
314 if (lsloc >= ch->chloc && lsloc < (ch->chloc + ch->ch_size))
318 // Fatal: Can't find chunk holding code
322 interror(6); // Can't find generated code in section
325 p = ch->chptr + (lsloc - ch->chloc);
329 for(count=sloc-lsloc; count--; col+=2, ++lsloc)
336 lnfill(lnimage, SRC_COL, SPACE); // Fill with spaces
337 sprintf(buf, "%08ux", lsloc);
338 scopy(lnimage+LOC_COL, buf, 8);
341 if (lsloc >= (ch->chloc + ch->ch_size))
343 if ((ch = ch->chnext) == NULL)
350 fixcount = fixtest(lcursect, lsloc);
356 ++p; // Advance anyway
359 sprintf(buf, "%02x", *p++ & 0xff);
361 scopy(lnimage+col, buf, 2);
364 // Flush partial line
380 // Copy Current (Saved) Line to Output Buffer and Tag it with a Character
382 void lstout(char tag)
391 lnfill(lnimage, SRC_COL, SPACE); // Fill with spaces
392 lnimage[TAG_COL] = tag;
394 // Copy line image and handle control characters
395 d = lnimage + SRC_COL;
397 for(s=lnbuf; *s; ++s)
399 if (*s >= 0x20 || *s == '\t')
404 *d++ = (char)(*s + 0x40);
413 // Output a Value to Listing
415 int listvalue(VALUE v)
417 sprintf(buf, "=%08ux", v);
418 scopy(lnimage + DATA_COL - 1, buf, 9);
424 * .subttl [-] "string"
427 * o leading '-' supresses page eject
428 * o don't .eject on first .subttl, but do so on all other ones,
429 * o arrange not to print the .subttl directive
444 return error("missing string");
446 strcpy(subttl, (char *)tok[1]);
450 if (ejectok && (subflag || pageno > 1)) // Always eject on pages 2+
460 // Set title on titles not on the first page, do an eject and clobber the subtitle
465 return error("missing string");
467 strcpy(title, (char*)tok[1]);