Fixed word reversed fixup problem.
[rmac] / debug.c
1 //
2 // RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
3 // DEBUG.C - Debugging Messages
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
7 //
8
9 #include "debug.h"
10 #include "amode.h"
11 #include "direct.h"
12 #include "mark.h"
13 #include "sect.h"
14 #include "token.h"
15
16
17 static int siztab[4] = { 3, 5, 9, 9 };
18
19
20 //
21 // Print 'c' Visibly
22 //
23 int visprt(char c)
24 {
25         if (c < 0x20 || c >= 0x7F)
26                 putchar('.');
27         else
28                 putchar(c);
29
30         return 0;
31 }
32
33
34 //
35 // Print expression, return ptr to just past the ENDEXPR
36 //
37 TOKEN * printexpr(TOKEN * tp)
38 {
39         if (tp != NULL)
40         {
41                 while (*tp != ENDEXPR)
42                 {
43                         switch ((int)*tp++)
44                         {
45                         case SYMBOL:
46 //                              printf("`%s' ", ((SYM *)*tp)->sname);
47                                 printf("`%s' ", symbolPtr[*tp]->sname);
48                                 tp++;
49                                 break;
50                         case CONST:
51                                 printf("$%X ", *tp++);
52                                 break;
53                         case ACONST:
54                                 printf("ACONST=($%X,$%X) ", *tp, tp[1]);
55                                 tp += 2;
56                                 break;
57                         default:
58                                 printf("%c ", (char)tp[-1]);
59                                 break;
60                         }
61                 }
62         }
63
64 //      printf(";\n");
65         return tp + 1;
66 }
67
68
69 //
70 // Dump data in a chunk (and maybe others) in the appropriate format
71 //
72 int chdump(CHUNK * ch, int format)
73 {
74         while (ch != NULL)
75         {
76                 printf("chloc=$%08X, chsize=$%X\n", ch->chloc, ch->ch_size);
77                 mdump(ch->chptr, ch->ch_size, format, ch->chloc);
78                 ch = ch->chnext;
79         }
80
81         return 0;
82 }
83
84
85 //
86 // Dump fixup records in printable format
87 //
88 int fudump(CHUNK * ch)
89 {
90         PTR p;
91         char * ep;
92         WORD attr, esiz;
93         WORD line, file;
94         LONG loc;
95
96         for(; ch!=NULL;)
97         {
98                 p.cp = ch->chptr;
99                 ep = ch->chptr + ch->ch_size;
100
101                 while (p.cp < ep)
102                 {
103                         attr = *p.wp++;
104                         loc = *p.lp++;
105                         file = *p.wp++;
106                         line = *p.wp++;
107
108                         printf("$%04X $%08X %d.%d: ", (int)attr, loc, (int)file, (int)line);
109
110                         if (attr & FU_EXPR)
111                         {
112                                 esiz = *p.wp++;
113                                 printf("(%d long) ", (int)esiz);
114                                 p.tk = printexpr(p.tk);
115                         }
116                         else
117                         {
118                                 printf("`%s' ;", (*p.sy)->sname);
119                                 p.sy++;
120                         }
121
122                         if ((attr & 0x0F00) == FU_JR)
123                         {
124                                 printf(" *=$%X", *p.lp);
125                                 p.lp++;
126                         }
127
128                         printf("\n");
129                 }
130
131                 ch = ch->chnext;
132         }
133
134         return 0;
135 }
136
137
138 //
139 // Dump marks
140 //
141 int mudump(void)
142 {
143         MCHUNK * mch;
144         PTR p;
145         WORD from;
146         WORD w;
147         LONG loc;
148         SYM * symbol;
149
150         from = 0;
151
152         for(mch=firstmch; mch!=NULL; mch=mch->mcnext)
153         {
154                 printf("mch=$%p mcptr=$%08X mcalloc=$%X mcused=$%X\n",
155                         mch, (mch->mcptr.lw), mch->mcalloc, (mch->mcused));
156
157                 p = mch->mcptr;
158                 
159                 for(;;)
160                 {
161                         w = *p.wp++;
162
163                         if (w & MCHEND)
164                                 break;
165
166                         symbol = NULL;
167                         loc = *p.lp++;
168
169                         if (w & MCHFROM)
170                                 from = *p.wp++;
171
172                         if (w & MSYMBOL)
173                                 symbol = *p.sy++;
174
175                         printf("m=$%04X to=%d loc=$%X from=%d siz=%s",
176                                         w, w & 0x00FF, loc, from, (w & MLONG) ? "long" : "word");
177
178                         if (symbol != NULL)
179                                 printf(" sym=`%s'", symbol->sname);
180
181                         printf("\n");
182                 }
183         }
184
185         return 0;
186 }
187
188
189 //
190 // Dump memory from 'start' for 'count' bytes; `flg' is the following ORed together:
191 // 0 - bytes
192 // 1 - words
193 // 2 - longwords
194 // 
195 // if `base' is not -1, then print it at the start of each line, incremented accordingly.
196 //
197 int mdump(char * start, LONG count, int flg, LONG base)
198 {
199         int i, j, k;
200         j = 0;
201
202         for(i=0; i<(int)count;)
203         {
204                 if ((i & 15) == 0)
205                 {
206                         if (j < i)
207                         {
208                                 printf("  ");
209
210                                 while(j < i)
211                                 visprt(start[j++]);
212
213                                 putchar('\n');
214                         }
215
216                         j = i;
217
218                         if (base != -1)
219                                 printf("%08X  ", base);
220                 }
221
222                 switch (flg & 3)
223                 {
224                 case 0:
225                         printf("%02X ", start[i] & 0xff);
226                         ++i;
227                         break;
228                 case 1:
229                         printf("%02X%02X ", start[i] & 0xff, start[i+1] & 0xff);
230                         i += 2;
231                         break;
232                 case 2:
233                         printf("%02X%02X%02X%02X ", start[i] & 0xff, start[i+1] & 0xff,
234                                 start[i+2] & 0xff, start[i+3] & 0xff);
235                         i += 4;
236                         break;
237                 case 3:
238                         break;
239                 }
240
241                 if (base != -1)
242                         base += 1 << (flg & 3);
243         }
244
245         // Print remaining bit of ascii; the hairy expression computes the number of
246         // spaces to print to make the ascii line up nicely.
247         if (j != i)
248         {
249                 k = ((16 - (i - j)) / (1 << (flg & 3))) * siztab[flg & 3];
250
251                 while(k--)
252                         putchar(' ');
253
254                 printf("  ");
255
256                 while(j < i)
257                         visprt(start[j++]);
258
259                 putchar('\n');
260         }
261
262         return 0;
263 }
264
265
266 //
267 // Dump list of tokens on stdout in printable form
268 //
269 int dumptok(TOKEN * tk)
270 {
271         int flg = 0;
272
273         while (*tk != EOL)
274         {
275                 if (flg++)
276                         printf(" ");
277
278                 if (*tk >= 128)
279                 {
280                         printf("REG=%u", *tk++ - 128);
281                         continue;
282                 }
283
284                 switch ((int)*tk++)
285                 {
286                 case CONST:                                        // CONST <value>
287                         printf("CONST=%u", *tk++);
288                         break;
289                 case STRING:                                       // STRING <address>
290                         printf("STRING='%s'", string[*tk++]);
291                         break;
292                 case SYMBOL:                                       // SYMBOL <address> 
293                         printf("SYMBOL='%s'", string[*tk++]);
294                         break;
295                 case EOL:                                          // End of line 
296                         printf("EOL");
297                         break;
298                 case TKEOF:                                        // End of file (or macro)
299                         printf("TKEOF");
300                         break;
301                 case DEQUALS:                                      // == 
302                         printf("DEQUALS");
303                         break;
304                 case DCOLON:                                       // :: 
305                         printf("DCOLON");
306                         break;
307                 case GE:                                           // >= 
308                         printf("GE");
309                         break;
310                 case LE:                                           // <= 
311                         printf("LE");
312                         break;
313                 case NE:                                           // <> or != 
314                         printf("NE");
315                         break;
316                 case SHR:                                          // >> 
317                         printf("SHR");
318                         break;
319                 case SHL:                                          // << 
320                         printf("SHL");
321                         break;
322                 default:
323                         printf("%c", (int)tk[-1]);
324                         break;
325                 }
326         }
327
328         printf("\n");
329
330         return 0;
331 }
332
333
334 //
335 // Dump Everything
336 //
337 int dump_everything(void)
338 {
339         int i;
340
341         for(i=1; i<NSECTS; i++)
342         {
343                 if (sect[i].scattr & SUSED)
344                 {
345                         printf("Section %d sloc=$%X\n", i, sect[i].sloc);
346                         printf("Code:\n");
347                         chdump(sect[i].sfcode, 1);
348
349                         printf("Fixup:\n");
350                         fudump(sect[i].sffix);
351
352                         printf("\n");
353                 }
354         }
355
356         printf("\nMarks:\n");
357         mudump();                                                               // Dump marks
358 //      printf("Total memory allocated=$%X\n", amemtot);
359
360         return 0;
361 }