]> git.sur5r.net Git - cc65/blob - src/da65/handler.c
New generic hash table module
[cc65] / src / da65 / handler.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 handler.c                                 */
4 /*                                                                           */
5 /*               Opcode handler functions for the disassembler               */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 52                                             */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdarg.h>
37
38 /* common */
39 #include "xsprintf.h"
40
41 /* da65 */
42 #include "attrtab.h"
43 #include "code.h"
44 #include "error.h"
45 #include "global.h"
46 #include "handler.h"
47 #include "opctable.h"
48 #include "output.h"
49
50
51
52 /*****************************************************************************/
53 /*                             Helper functions                              */
54 /*****************************************************************************/
55
56
57
58 static void Mnemonic (const char* M)
59 /* Indent and output a mnemonic */
60 {
61     Indent (MIndent);
62     Output ("%s", M);
63 }
64
65
66
67 static void OneLine (const OpcDesc* D, const char* Arg, ...) attribute ((format(printf, 2, 3)));
68 static void OneLine (const OpcDesc* D, const char* Arg, ...)
69 /* Output one line with the given mnemonic and argument */
70 {
71     char Buf [256];
72     va_list ap;
73
74     /* Mnemonic */
75     Mnemonic (D->Mnemo);
76
77     /* Argument */
78     va_start (ap, Arg);
79     xvsprintf (Buf, sizeof (Buf), Arg, ap);
80     va_end (ap);
81     Indent (AIndent);
82     Output (Buf);
83
84     /* Add the code stuff as comment */
85     LineComment (PC, D->Size);
86
87     /* End the line */
88     LineFeed ();
89 }
90
91
92
93 static const char* GetAddrArg (unsigned Flags, unsigned Addr)
94 /* Return an address argument - a label if we have one, or the address itself */
95 {
96     const char* Label = 0;
97     if (Flags & flUseLabel) {
98         Label = GetLabel (Addr);
99     }
100     if (Label) {
101         return Label;
102     } else {
103         static char Buf [32];
104         if (Addr < 0x100) {
105             xsprintf (Buf, sizeof (Buf), "$%02X", Addr);
106         } else {
107             xsprintf (Buf, sizeof (Buf), "$%04X", Addr);
108         }
109         return Buf;
110     }
111 }
112
113
114
115 static void GenerateLabel (unsigned Flags, unsigned Addr)
116 /* Generate a label in pass one if requested */
117 {
118     if (Pass == 1 && !HaveLabel (Addr)) {
119         if ((Flags & flGenLabel) != 0 ||
120             ((Flags & flUseLabel) != 0 && Addr >= CodeStart && Addr <= CodeEnd)) {
121             AddLabel (Addr, atIntLabel, MakeLabelName (Addr));
122         }
123     }
124 }
125
126
127
128 /*****************************************************************************/
129 /*                                   Code                                    */
130 /*****************************************************************************/
131
132
133
134 void OH_Illegal (const OpcDesc* D attribute ((unused)))
135 {
136     DataByteLine (1);
137 }
138
139
140
141 void OH_Accumulator (const OpcDesc* D)
142 {
143     OneLine (D, "a");
144 }
145
146
147
148 void OH_Implicit (const OpcDesc* D)
149 {
150     Mnemonic (D->Mnemo);
151     LineComment (PC, D->Size);
152     LineFeed ();
153 }
154
155
156
157 void OH_Immidiate (const OpcDesc* D)
158 {
159     OneLine (D, "#$%02X", GetCodeByte (PC+1));
160 }
161
162
163
164 void OH_Direct (const OpcDesc* D)
165 {
166     /* Get the operand */
167     unsigned Addr = GetCodeByte (PC+1);
168
169     /* Generate a label in pass 1 */
170     GenerateLabel (D->Flags, Addr);
171
172     /* Output the line */
173     OneLine (D, "%s", GetAddrArg (D->Flags, Addr));
174 }
175
176
177
178 void OH_DirectX (const OpcDesc* D)
179 {
180     /* Get the operand */
181     unsigned Addr = GetCodeByte (PC+1);
182
183     /* Generate a label in pass 1 */
184     GenerateLabel (D->Flags, Addr);
185
186     /* Output the line */
187     OneLine (D, "%s,x", GetAddrArg (D->Flags, Addr));
188 }
189
190
191
192 void OH_DirectY (const OpcDesc* D)
193 {
194     /* Get the operand */
195     unsigned Addr = GetCodeByte (PC+1);
196
197     /* Generate a label in pass 1 */
198     GenerateLabel (D->Flags, Addr);
199
200     /* Output the line */
201     OneLine (D, "%s,y", GetAddrArg (D->Flags, Addr));
202 }
203
204
205
206 void OH_Absolute (const OpcDesc* D)
207 {
208     /* Get the operand */
209     unsigned Addr = GetCodeWord (PC+1);
210
211     /* Generate a label in pass 1 */
212     GenerateLabel (D->Flags, Addr);
213
214     /* Output the line */
215     OneLine (D, "%s", GetAddrArg (D->Flags, Addr));
216 }
217
218
219
220 void OH_AbsoluteX (const OpcDesc* D)
221 {
222     /* Get the operand */
223     unsigned Addr = GetCodeWord (PC+1);
224
225     /* Generate a label in pass 1 */
226     GenerateLabel (D->Flags, Addr);
227
228     /* Output the line */
229     OneLine (D, "%s,x", GetAddrArg (D->Flags, Addr));
230 }
231
232
233
234 void OH_AbsoluteY (const OpcDesc* D)
235 {
236     /* Get the operand */
237     unsigned Addr = GetCodeWord (PC+1);
238
239     /* Generate a label in pass 1 */
240     GenerateLabel (D->Flags, Addr);
241
242     /* Output the line */
243     OneLine (D, "%s,y", GetAddrArg (D->Flags, Addr));
244 }
245
246
247
248 void OH_AbsoluteLong (const OpcDesc* D attribute ((unused)))
249 {
250     Error ("Not implemented");
251 }
252
253
254
255 void OH_AbsoluteLongX (const OpcDesc* D attribute ((unused)))
256 {
257     Error ("Not implemented");
258 }
259
260
261
262 void OH_Relative (const OpcDesc* D)
263 {
264     /* Get the operand */
265     signed char Offs = GetCodeByte (PC+1);
266
267     /* Calculate the target address */
268     unsigned Addr = (((int) PC+2) + Offs) & 0xFFFF;
269
270     /* Generate a label in pass 1 */
271     GenerateLabel (D->Flags, Addr);
272
273     /* Output the line */
274     OneLine (D, "%s", GetAddrArg (D->Flags, Addr));
275 }
276
277
278
279 void OH_RelativeLong (const OpcDesc* D attribute ((unused)))
280 {
281     Error ("Not implemented");
282 }
283
284
285
286 void OH_DirectIndirect (const OpcDesc* D)
287 {
288     /* Get the operand */
289     unsigned Addr = GetCodeByte (PC+1);
290
291     /* Generate a label in pass 1 */
292     GenerateLabel (D->Flags, Addr);
293
294     /* Output the line */
295     OneLine (D, "(%s)", GetAddrArg (D->Flags, Addr));
296 }
297
298
299
300 void OH_DirectIndirectY (const OpcDesc* D)
301 {
302     /* Get the operand */
303     unsigned Addr = GetCodeByte (PC+1);
304
305     /* Generate a label in pass 1 */
306     GenerateLabel (D->Flags, Addr);
307
308     /* Output the line */
309     OneLine (D, "(%s),y", GetAddrArg (D->Flags, Addr));
310 }
311
312
313
314 void OH_DirectXIndirect (const OpcDesc* D)
315 {
316     /* Get the operand */
317     unsigned Addr = GetCodeByte (PC+1);
318
319     /* Generate a label in pass 1 */
320     GenerateLabel (D->Flags, Addr);
321
322     /* Output the line */
323     OneLine (D, "(%s,x)", GetAddrArg (D->Flags, Addr));
324 }
325
326
327
328 void OH_AbsoluteIndirect (const OpcDesc* D)
329 {
330     /* Get the operand */
331     unsigned Addr = GetCodeWord (PC+1);
332
333     /* Generate a label in pass 1 */
334     GenerateLabel (D->Flags, Addr);
335
336     /* Output the line */
337     OneLine (D, "(%s)", GetAddrArg (D->Flags, Addr));
338 }
339
340
341
342 void OH_BitBranch (const OpcDesc* D)
343 {
344     /* Get the operands */
345     unsigned char TestAddr   = GetCodeByte (PC+1);
346     signed char   BranchOffs = GetCodeByte (PC+2);
347
348     /* Calculate the target address for the branch */
349     unsigned BranchAddr = (((int) PC+3) + BranchOffs) & 0xFFFF;
350
351     /* Generate labels in pass 1. The bit branch codes are special in that
352      * they don't really match the remainder of the 6502 instruction set (they
353      * are a Rockwell addon), so we must pass additional flags as direct
354      * value to the second GenerateLabel call.
355      */
356     GenerateLabel (D->Flags, TestAddr);
357     GenerateLabel (flLabel, BranchAddr);
358
359     /* Output the line */
360     OneLine (D, "%s,%s", GetAddrArg (D->Flags, TestAddr), GetAddrArg (flLabel, BranchAddr));
361 }
362
363
364
365 void OH_StackRelative (const OpcDesc* D attribute ((unused)))
366 {
367     Error ("Not implemented");
368 }
369
370
371
372 void OH_DirectIndirectLongX (const OpcDesc* D attribute ((unused)))
373 {
374     Error ("Not implemented");
375 }
376
377
378
379 void OH_StackRelativeIndirectY (const OpcDesc* D attribute ((unused)))
380 {
381     Error ("Not implemented");
382 }
383
384
385
386 void OH_DirectIndirectLong (const OpcDesc* D attribute ((unused)))
387 {
388     Error ("Not implemented");
389 }
390
391
392
393 void OH_DirectIndirectLongY (const OpcDesc* D attribute ((unused)))
394 {
395     Error ("Not implemented");
396 }
397
398
399
400 void OH_BlockMove (const OpcDesc* D attribute ((unused)))
401 {
402     Error ("Not implemented");
403 }
404
405
406
407 void OH_AbsoluteXIndirect (const OpcDesc* D attribute ((unused)))
408 {
409     Error ("Not implemented");
410 }
411
412
413
414 void OH_Rts (const OpcDesc* D)
415 {
416     OH_Implicit (D);
417     SeparatorLine();
418 }
419
420
421
422 void OH_JmpAbsolute (const OpcDesc* D)
423 {
424     OH_Absolute (D);
425     SeparatorLine ();
426 }
427
428
429
430 void OH_JmpAbsoluteIndirect (const OpcDesc* D)
431 {
432     OH_AbsoluteIndirect (D);
433     SeparatorLine ();
434 }
435
436
437