]> git.sur5r.net Git - cc65/blob - doc/da65.sgml
Added a sentence about unnamed labels
[cc65] / doc / da65.sgml
1 <!doctype linuxdoc system>
2
3 <article>
4 <title>da65 Users Guide
5 <author>Ullrich von Bassewitz, <htmlurl url="mailto:uz@cc65.org" name="uz@cc65.org">
6 <date>2003-08-08
7
8 <abstract>
9 da65 is a 6502/65C02 disassembler that is able to read user supplied
10 information about its input data for better results. The output is ready for
11 feeding into ca65, the macro assembler supplied with the cc65 C compiler.
12 </abstract>
13
14 <!-- Table of contents -->
15 <toc>
16
17 <!-- Begin the document -->
18
19 <sect>Overview<p>
20
21 da65 is a disassembler for 6502/65C02 code. It is supplied as a utility with
22 the cc65 C compiler and generates output that is suitable for the ca65
23 macro assembler.
24
25 Besides generating output for ca65, one of the design goals was that the user
26 is able to feed additional information about the code into the disassembler
27 for improved results. This information may include the location and size of
28 tables, and their format.
29
30 One nice advantage of this concept is that disassembly of copyrighted binaries
31 may be handled without problems: One can just pass the information file for
32 disassembling the binary, so everyone with a legal copy of the binary can
33 generate a nicely formatted disassembly with readable labels and other
34 information.
35
36
37 <sect>Usage<p>
38
39
40 <sect1>Command line option overview<p>
41
42 The assembler accepts the following options:
43
44 <tscreen><verb>
45 ---------------------------------------------------------------------------
46 Usage: da65 [options] [inputfile]
47 Short options:
48   -g                    Add debug info to object file
49   -h                    Help (this text)
50   -i name               Specify an info file
51   -o name               Name the output file
52   -v                    Increase verbosity
53   -F                    Add formfeeds to the output
54   -S addr               Set the start/load address
55   -V                    Print the disassembler version
56
57 Long options:
58   --comments n          Set the comment level for the output
59   --cpu type            Set cpu type
60   --debug-info          Add debug info to object file
61   --formfeeds           Add formfeeds to the output
62   --help                Help (this text)
63   --info name           Specify an info file
64   --label-break n       Add newline if label exceeds length n
65   --pagelength n        Set the page length for the listing
66   --start-addr addr     Set the start/load address
67   --verbose             Increase verbosity
68   --version             Print the disassembler version
69 ---------------------------------------------------------------------------
70 </verb></tscreen>
71
72
73 <sect1>Command line options in detail<p>
74
75 Here is a description of all the command line options:
76
77 <descrip>
78
79   <label id="option--comments">
80   <tag><tt>--comments n</tt></tag>
81
82   Set the comment level for the output. Valid arguments are 0..4. Greater
83   values will increase the level of additional information written to the
84   output file in form of comments.
85
86
87   <label id="option--cpu">
88   <tag><tt>--cpu type</tt></tag>
89
90   Set the CPU type. The option takes a parameter, which may be one of
91
92         6502, 65SC02, 65C02
93
94   Support for the 65816 is currently not available.
95
96
97   <label id="option--formfeeds">
98   <tag><tt>-F, --formfeeds</tt></tag>
99
100   Add formfeeds to the generated output. This feature is useful together
101   with the <tt><ref id="option--pagelength" name="--pagelength"></tt> option.
102   If <tt/--formfeeds/ is given, a formfeed is added to the output after each
103   page.
104
105
106   <tag><tt>-g, --debug-info</tt></tag>
107
108   This option adds the <tt/.DEBUGINFO/ command to the output file, so the
109   assembler will generate debug information when reassembling the generated
110   output.
111
112
113   <tag><tt>-h, --help</tt></tag>
114
115   Print the short option summary shown above.
116
117
118   <label id="option--info">
119   <tag><tt>-i name, --info name</tt></tag>
120
121   Specify an info file. The info file contains global options that may
122   override or replace command line options plus informations about the code
123   that has to be disassembled. See the separate section <ref id="infofile"
124   name="Info File Format">.
125
126
127   <label id="option-o">
128   <tag><tt>-o name</tt></tag>
129
130   Specify a name for an output file. The default is to use <tt/stdout/, so
131   without this switch or the corresponding <ref id="global-options"
132   name="global option"> <tt><ref id="OUTPUTNAME" name="OUTPUTNAME"></tt>,
133   the output will go to the terminal.
134
135
136   <label id="option--label-break">
137   <tag><tt>--label-break n</tt></tag>
138
139   Adds a newline if the length of a label exceeds the given length.
140   Note: If the label would run into the code in the mid column, a
141   linefeed is always inserted regardless of this setting.
142
143   This option overrides the <ref id="global-options" name="global option">
144   <tt><ref id="LABELBREAK" name="LABELBREAK"></tt>.
145
146
147   <label id="option--pagelength">
148   <tag><tt>--pagelength n</tt></tag>
149
150   Sets the length of a listing page in lines. After this number of lines, a
151   new page header is generated. If the <tt><ref id="option--formfeeds"
152   name="--formfeeds"></tt> is also given, a formfeed is inserted before
153   generating the page header.
154
155   A value of zero for the page length will disable paging of the output.
156
157
158   <label id="option--start-addr">
159   <tag><tt>-S addr, --start-addr addr</tt></tag>
160
161   Specify the start/load address of the binary code that is going to be
162   disassembled. The given address is interpreted as an octal value if
163   preceded with a '0' digit, as a hexadecimal value if preceded
164   with '0x', '0X', or '$', and as a decimal value in all other cases. If no
165   start address is specified, $10000 minus the size of the input file is used.
166
167
168   <tag><tt>-v, --verbose</tt></tag>
169
170   Increase the disassembler verbosity. Usually only needed for debugging
171   purposes. You may use this option more than one time for even more
172   verbose output.
173
174
175   <tag><tt>-V, --version</tt></tag>
176
177   Print the version number of the assembler. If you send any suggestions
178   or bugfixes, please include the version number.
179
180 </descrip>
181 <p>
182
183
184 <sect>Detailed workings<p>
185
186 <sect1>Supported CPUs<p>
187
188 The default (no CPU given on the command line or in the <tt/GLOBAL/ section of
189 the info file) is the 6502 CPU. The disassembler knows all "official" opcodes
190 for this CPU. Invalid opcodes are translated into <tt/.byte/ commands.
191
192 With the command line option <tt><ref id="option--cpu" name="--cpu"></tt>, the
193 disassembler may be told to recognize either the 65SC02 or 65C02 CPUs. The
194 latter understands the same opcodes as the former, plus 16 additional bit
195 manipulation and bit test-and-branch commands.
196
197 While there is some code for the 65816 in the sources, it is currently
198 unsupported.
199
200
201 <sect1>Attribute map<p>
202
203 The disassembler works by creating an attribute map for the whole address
204 space ($0000 - $FFFF). Initially, all attributes are cleared. Then, an
205 external info file (if given) is read. Disassembly is done in several passes.
206 In all passes with the exception of the last one, information about the
207 disassembled code is gathered and added to the symbol and attribute maps. The
208 last pass generates output using the information from the maps.
209
210 <sect1>Labels<p>
211
212 Some instructions may generate labels in the first pass, while most other
213 instructions do not generate labels, but use them if they are available. Among
214 others, the branch and jump instructions will generate labels for the target
215 of the branch in the first pass. External labels (taken from the info file)
216 have precedence over internally generated ones, They must be valid identifiers
217 as specified for the ca65 assembler. Internal labels (generated by the
218 disassembler) have the form <tt/Labcd/, where <tt/abcd/ is the hexadecimal
219 address of the label in upper case letters. You should probably avoid using
220 such label names for external labels.
221
222
223 <sect1>Info File<p>
224
225 The info file is used to pass additional information about the input code to
226 the disassembler. This includes label names, data areas or tables, and global
227 options like input and output file names. See the <ref id="infofile"
228 name="next section"> for more information.
229
230
231
232 <sect>Info File Format<label id="infofile"><p>
233
234 The info file contains lists of specifications grouped together. Each group
235 directive has an identifying token and an attribute list enclosed in curly
236 braces. Attributes have a name followed by a value. The syntax of the value
237 depends on the type of the attribute. String attributes are places in double
238 quotes, numeric attributes may be specified as decimal numbers or hexadecimal
239 with a leading dollar sign. There are also attributes where the attribute
240 value is a keyword, in this case the keyword is given as is (without quotes or
241 anything). Each attribute is terminated by a semicolon.
242
243 <tscreen><verb>
244         group-name { attribute1 attribute-value; attribute2 attribute-value; }
245 </verb></tscreen>
246
247
248 <sect1>Comments<p>
249
250 Comments start with a hash mark (<tt/#/)  and extend from the position of
251 the mark to the end of the current line. Hash marks inside of strings will
252 of course <em/not/ start a comment.
253
254
255 <sect1>Specifying global options<label id="global-options"><p>
256
257 Global options may be specified in a group with the name <tt/GLOBAL/. The
258 following attributes are recognized:
259
260 <descrip>
261
262   <tag><tt>COMMENTS</tt></tag>
263   This attribute may be used instead of the <tt><ref id="option--comments"
264   name="--comments"></tt> option on the command line. It takes a numerical
265   parameter between 0 and 4. Higher values increase the amount of information
266   written to the output file in form of comments.
267
268
269   <tag><tt>CPU</tt></tag>
270   This attribute may be used instead of the <tt><ref id="option--cpu"
271   name="--cpu"></tt> option on the command line. It takes a string parameter.
272
273
274   <tag><tt>INPUTNAME</tt></tag>
275   The attribute is followed by a string value, which gives the name of the
276   input file to read. If it is present, the disassembler does not accept an
277   input file name on the command line.
278
279
280   <tag><tt>INPUTOFFS</tt></tag>
281   The attribute is followed by a numerical value that gives an offset into
282   the input file which is skipped before reading data. The attribute may be
283   used to skip headers or unwanted code sections in the input file.
284
285
286   <tag><tt>INPUTSIZE</tt></tag>
287   <tt/INPUTSIZE/ is followed by a numerical value that gives the amount of
288   data to read from the input file. Data beyond <tt/INPUTOFFS + INPUTSIZE/
289   is ignored.
290
291
292   <label id="LABELBREAK">
293   <tag><tt>LABELBREAK</tt></tag>
294   <tt/LABELBREAK/ is followed by a numerical value that specifies the label
295   length that will force a newline. To have all labels on their own lines,
296   you may set this value to zero.
297
298   See also the <tt><ref id="option--label-break" name="--label-break"></tt>
299   command line option. A <tt/LABELBREAK/ statement in the info file will
300   override any value given on the command line.
301
302
303   <label id="OUTPUTNAME">
304   <tag><tt>OUTPUTNAME</tt></tag>
305   The attribute is followed by string value, which gives the name of the
306   output file to write. If it is present, specification of an output file on
307   the command line using the <tt><ref id="option-o" name="-o"></tt> option is
308   not allowed.
309
310   The default is to use <tt/stdout/ for output, so without this attribute or
311   the corresponding command line option <tt/<ref id="option-o" name="-o">/
312   the output will go to the terminal.
313
314
315   <tag><tt>PAGELENGTH</tt></tag>
316   This attribute may be used instead of the <tt><ref id="option--pagelength"
317   name="--pagelength"></tt> option on the command line. It takes a numerical
318   parameter. Using zero as page length (which is the default) means that no
319   pages are generated.
320
321
322   <tag><tt>STARTADDR</tt></tag>
323   This attribute may be used instead of the <tt><ref id="option--start-addr"
324   name="--start-addr"></tt> option on the command line. It takes a numerical
325   parameter. The default for the start address is $10000 minus the size of
326   the input file (this assumes that the input file is a ROM that contains the
327   reset and irq vectors).
328
329 </descrip>
330
331
332 <sect1>Specifying Ranges<p>
333
334 The <tt/RANGE/ directive is used to give information about address ranges. The
335 following attributes are recognized:
336
337 <descrip>
338
339   <tag><tt>COMMENT</tt></tag>
340   This attribute is only allowed if a label is also given. It takes a string
341   as argument. See the description of the <tt><ref id="infofile-label"
342   name="LABEL"></tt> directive for an explanation.
343
344   <tag><tt>END</tt></tag>
345   This gives the end address of the range. The end address is inclusive, that
346   means, it is part of the range. Of course, it may not be smaller than the
347   start address.
348
349   <tag><tt>NAME</tt></tag>
350   This is a convenience attribute. It takes a string argument and will cause
351   the disassembler to define a label for the start of the range with the
352   given name. So a separate <tt><ref id="infofile-label" name="LABEL"></tt>
353   directive is not needed.
354
355   <tag><tt>START</tt></tag>
356   This gives the start address of the range.
357
358   <tag><tt>TYPE</tt></tag>
359   This attribute specifies the type of data within the range. The attribute
360   value is one of the following keywords:
361
362   <descrip>
363     <tag><tt>ADDRTABLE</tt></tag>
364     The range consists of data and is disassembled as a table of words
365     (16 bit values). The difference to the <tt/WORDTABLE/ type is that
366     a label is defined for each entry in the table.
367
368     <tag><tt>BYTETABLE</tt></tag>
369     The range consists of data and is disassembled as a byte table.
370
371     <tag><tt>CODE</tt></tag>
372     The range consists of code.
373
374     <tag><tt>DBYTETABLE</tt></tag>
375     The range consists of data and is disassembled as a table of dbytes
376     (double byte values, 16 bit values with the low byte containing the
377     most significant byte of the 16 bit value).
378
379     <tag><tt>DWORDTABLE</tt></tag>
380     The range consists of data and is disassembled as a table of double
381     words (32 bit values).
382
383     <tag><tt>RTSTABLE</tt></tag>
384     The range consists of data and is disassembled as a table of words (16 bit
385     values). The values are interpreted as words that are pushed onto the
386     stack and jump to it via <tt/RTS/. This means that they contain
387     <tt/address-1/ of a function, for which a label will get defined by the
388     disassembler.
389
390     <tag><tt>SKIP</tt></tag>
391     The range is simply ignored when generating the output file. Please note
392     that this means that reassembling the output file will <em/not/ generate
393     the original file, not only because the missing piece in between, but also
394     because the following code will be located on wrong addresses. Output
395     generated with <tt/SKIP/ ranges will need manual rework.
396
397     <tag><tt>TEXTTABLE</tt></tag>
398     The range consists of readable text.
399
400     <tag><tt>WORDTABLE</tt></tag>
401     The range consists of data and is disassembled as a table of words
402     (16 bit values).
403
404   </descrip>
405
406 </descrip>
407
408
409 <sect1>Specifying Labels<label id="infofile-label"><p>
410
411 The <tt/LABEL/ directive is used to give names for labels in the disassembled
412 code. The following attributes are recognized:
413
414 <descrip>
415
416   <tag><tt>ADDR</tt></tag>
417   Followed by a numerical value. Specifies the value of the label.
418
419   <tag><tt>COMMENT</tt></tag>
420   Attribute argument is a string. The comment will show up in a separate line
421   before the label, if the label is within code or data range, or after the
422   label if it is outside.
423
424   Example output:
425
426 <tscreen><verb>
427         foo     := $0001        ; Comment for label named "foo"
428
429         ; Comment for label named "bar"
430         bar:
431 </verb></tscreen>
432
433   <tag><tt>NAME</tt></tag>
434   The attribute is followed by a string value which gives the name of the
435   label. Empty names are allowed, in this case the disassembler will create
436   an unnamed label (see the assembler docs for more information about unnamed
437   labels).
438
439   <tag><tt>SIZE</tt></tag>
440   This attribute is optional and may be used to specify the size of the data
441   that follows. If a size greater than 1 is specified, the disassembler will
442   create labels in the form <tt/label+offs/ for all bytes within the given
443   range, where <tt/label/ is the label name given with the <tt/NAME/
444   attribute, and <tt/offs/ is the offset within the data.
445
446 </descrip>
447
448
449 <sect1>Specifying Assembler Includes<label id="infofile-asminc"><p>
450
451 The <tt/ASMINC/ directive is used to give the names of input files containing
452 symbol assignments in assembler syntax:
453
454 <tscreen><verb>
455         Name = value
456         Name := value
457 </verb></tscreen>
458
459 The usual conventions apply for symbol names. Values may be specified as hex
460 (leading &dollar;), binary (leading %) or decimal. The values may optionally
461 be signed.
462
463 NOTE: The include file parser is very simple. Expressions are not allowed, and
464 anything but symbol assignments is flagged as an error (but see the
465 <tt/IGNOREUNKNOWN/ directive below).
466
467 The following attributes are recognized:
468
469 <descrip>
470
471   <tag><tt>FILE</tt></tag>
472   Followed by a string value. Specifies the name of the file to read.
473
474   <tag><tt>COMMENTSTART</tt></tag>
475   The optional attribute is followed by a character constant. It specifies the
476   character that starts a comment. The default value is a semicolon. This
477   value is ignored if <tt/IGNOREUNKNOWN/ is true.
478
479   <tag><tt>IGNOREUNKNOWN</tt></tag>
480   This attribute is optional and is followed by a boolean value. It allows to
481   ignore input lines that don't have a valid syntax. This allows to read in
482   assembler include files that contain more than just symbol assignments.
483   Note: When this attribute is used, the disassembler will ignore any errors
484   in the given include file. This may have undesired side effects.
485
486 </descrip>
487
488
489 <sect1>An Info File Example<p>
490
491 The following is a short example for an info file that contains most of the
492 directives explained above:
493
494 <tscreen><verb>
495         # This is a comment. It extends to the end of the line
496         GLOBAL {
497             OUTPUTNAME      "kernal.s";
498             INPUTNAME       "kernal.bin";
499             STARTADDR       $E000;
500             PAGELENGTH      0;                  # No paging
501             CPU             "6502";
502         };
503
504
505         RANGE { START $E612;    END   $E631; TYPE Code;      };
506         RANGE { START $E632;    END   $E640; TYPE ByteTable; };
507         RANGE { START $EA51;    END   $EA84; TYPE RtsTable;  };
508         RANGE { START $EC6C;    END   $ECAB; TYPE RtsTable;  };
509         RANGE { START $ED08;    END   $ED11; TYPE AddrTable; };
510
511         # Zero page variables
512         LABEL { NAME "fnadr";   ADDR  $90;   SIZE 3;    };
513         LABEL { NAME "sal";     ADDR  $93;   };
514         LABEL { NAME "sah";     ADDR  $94;   };
515         LABEL { NAME "sas";     ADDR  $95;   };
516
517         # Stack
518         LABEL { NAME "stack";   ADDR  $100;  SIZE 255;  };
519
520         # Indirect vectors
521         LABEL { NAME "cinv";    ADDR  $300;  SIZE 2;    };      # IRQ
522         LABEL { NAME "cbinv";   ADDR  $302;  SIZE 2;    };      # BRK
523         LABEL { NAME "nminv";   ADDR  $304;  SIZE 2;    };      # NMI
524
525         # Jump table at end of kernal ROM
526         LABEL { NAME "kscrorg"; ADDR  $FFED; };
527         LABEL { NAME "kplot";   ADDR  $FFF0; };
528         LABEL { NAME "kiobase"; ADDR  $FFF3; };
529         LABEL { NAME "kgbye";   ADDR  $FFF6; };
530
531         # Hardware vectors
532         LABEL { NAME "hanmi";   ADDR  $FFFA; };
533         LABEL { NAME "hares";   ADDR  $FFFC; };
534         LABEL { NAME "hairq";   ADDR  $FFFE; };
535 </verb></tscreen>
536
537
538
539
540
541 <sect>Bugs/Feedback<p>
542
543 If you have problems using the disassembler, if you find any bugs, or if
544 you're doing something interesting with the assembler, I would be glad to hear
545 from you. Feel free to contact me by email (<htmlurl url="mailto:uz@cc65.org"
546 name="uz@cc65.org">).
547
548
549
550 <sect>Copyright<p>
551
552 da65 (and all cc65 binutils) are (C) Copyright 1998-2003 Ullrich von
553 Bassewitz. For usage of the binaries and/or sources the following
554 conditions do apply:
555
556 This software is provided 'as-is', without any expressed or implied
557 warranty.  In no event will the authors be held liable for any damages
558 arising from the use of this software.
559
560 Permission is granted to anyone to use this software for any purpose,
561 including commercial applications, and to alter it and redistribute it
562 freely, subject to the following restrictions:
563
564 <enum>
565 <item>  The origin of this software must not be misrepresented; you must not
566         claim that you wrote the original software. If you use this software
567         in a product, an acknowledgment in the product documentation would be
568         appreciated but is not required.
569 <item>  Altered source versions must be plainly marked as such, and must not
570         be misrepresented as being the original software.
571 <item>  This notice may not be removed or altered from any source
572         distribution.
573 </enum>
574
575
576
577 </article>
578
579
580
581