]> git.sur5r.net Git - cc65/blob - doc/intro.sgml
Oliver added a comment to the Apple2 linker config.
[cc65] / doc / intro.sgml
1 <!doctype linuxdoc system>
2
3 <article>
4
5 <title>cc65 Compiler Intro
6 <author>Ullrich von Bassewitz, <htmlurl url="mailto:uz@cc65.org" name="uz@cc65.org">,
7 <and>CbmNut, <htmlurl url="mailto:cbmnut@hushmail.com" name="cbmnut@hushmail.com">,
8 <and><url name="Greg King" url="mailto:gngking@erols.com">
9 <date>2005-7-22
10
11 <abstract>
12 How to use the cc65 C language system -- an introduction.
13 </abstract>
14
15 <!-- Table of contents -->
16 <toc>
17
18 <!-- Begin the document -->
19
20 <sect>Overview<p>
21
22 This is a short intro of how to use the compiler and the bin-utils. It contains
23 a step-by-step example of how to build a complete application from one C and
24 one assembly modules. This file does <em/not/ contain a complete reference for
25 the tools used in the process. There are separate files describing those tools,
26 in detail (see <url url="index.html">).
27
28 You are assumed to have downloaded and extracted the executables and the
29 target-specific files. For example: for Windows users targeting C64, you need
30 <bf/cc65-win32-2.10.1.zip/ and <bf/cc65-c64-2.10.1.zip/ (or, whatever the
31 current cc65 version is) extracted to the same directory. If you received the
32 files as a bzip2 archive (extension <bf/.bz2/), you will need to get <url
33 url="http://sources.redhat.com/bzip2/#bzip2-latest" name="the bzip2 package">
34 to decompress it.
35
36 <bf/Note/: There is a much simpler way to compile this example, by using the
37 <bf/cl65/ compile-and-link utility. However, it makes sense to understand how
38 the separate steps work. How to do the example with the <bf/cl65/ utility is
39 described <ref id="using-cl65" name="later">.
40
41
42 <sect1>Before we start<p>
43
44 You will find a copy of the sample modules, used in the next section, in the
45 "<tt>cc65/samples/tutorial</tt>" directory. Please make sure that the compiler
46 and linker can find the include and library files, by setting the environment
47 variables <tt/CC65_INC/ and <tt/CC65_LIB/, respectively.
48
49
50 <sect1>The sample modules<p>
51
52 To explain the development flow, I will use the following example modules:
53
54 hello.c:
55 <tscreen><code>
56         #include <stdio.h>
57         #include <stdlib.h>
58
59         extern const char text[];       /* In text.s */
60
61         int main (void)
62         {
63             printf ("%s\n", text);
64             return EXIT_SUCCESS;
65         }
66 </code></tscreen>
67
68 text.s:
69 <tscreen><code>
70         .export _text
71         _text:  .asciiz "Hello world!"
72 </code></tscreen>
73
74
75 <sect1>Translation phases<p>
76
77 We assume that the target file should be named "hello", and the target system
78 is the C64.
79
80 <tscreen><verb>
81     +---------+
82     | hello.c |
83     +---------+
84          |
85         cc65
86          \/
87     +---------+       +---------+
88     | hello.s |       | text.s  |
89     +---------+       +---------+
90          |                 |
91         ca65              ca65
92          \/                \/
93     +---------+       +---------+       +----------+       +---------+
94     | hello.o |       | text.o  |       |  c64.o   |       | c64.lib |
95     +---------+       +---------+       +----------+       +---------+
96          |                    \          /                      |
97          |                     \        /                       |
98          |                      \      /                        |
99          +----------------------->ld65<-------------------------+
100                                    \/
101                                  hello
102 </verb></tscreen>
103
104 <tt/c64.o/ (the startup code) and <tt/c64.lib/ (the C64 version of the runtime
105 and C library) are provided in binary form in the cc65 package.
106
107
108
109 <sect>The compiler<p>
110
111 The compiler translates one C source into one assembly source, for each
112 invocation. It does <em/not/ create object files directly, and it is <em/not/
113 able to translate more than one file per run.
114
115 In the example above, we would use the following command line, to translate
116 <tt/hello.c/ into <tt/hello.s/:
117
118 <tscreen><verb>
119         cc65 -O -I ../../include -t c64 hello.c
120 </verb></tscreen>
121
122 The <tt/-O/ switch tells the compiler to do an additional optimizer run, which
123 is usually a good idea, since it makes the code smaller. If you don't care
124 about the size, but want to have slightly faster code, use <tt/-Oi/ to inline
125 some runtime functions.
126
127 The <tt/-I/ switch gives a search path for the include files. You may also set
128 the environment variable <tt/CC65_INC/ to the search path.
129
130 The <tt/-t/ switch is followed by the target system name.
131
132 If the compiler does not complain about errors in our "hello world" program, we
133 will have a file named "<tt/hello.s/", in our directory, that contains the
134 assembly source for the <bf/hello/ module.
135
136 For more information about the compiler, see <url url="cc65.html">.
137
138
139
140 <sect>The assembler<p>
141
142 The assembler translates one assembly source into an object file, for each
143 invocation. The assembler is <em/not/ able to translate more than one source
144 file per run.
145
146 Let's translate the "hello.s" and "text.s" files from our example:
147
148 <tscreen><verb>
149         ca65 hello.s
150         ca65 -t c64 text.s
151 </verb></tscreen>
152
153 The <tt/-t/ switch is needed when translating the <tt/text.s/ file, so the
154 text is converted from the input character-set (usually ISO-8859-1) into the
155 target character-set (PETSCII, in this example) by the assembler. The
156 compiler-generated file <tt/hello.s/ does not contain any character constants,
157 so specification of a target is not necessary (it wouldn't do any harm,
158 however).
159
160 If the assembler does not complain, we should now have two object files (named
161 <tt/hello.o/ and <tt/text.o/) in the current directory.
162
163 For more information about the assembler, see <url url="ca65.html">.
164
165
166
167 <sect>The linker<p>
168
169 The linker combines several object and library files into one output file.
170 <bf/ld65/ is very configurable, but fortunately has built-in configurations,
171 so we don't need to mess with configuration files, here.
172
173 The compiler uses small functions to do things that cannot be done inline
174 without a big impact on code size. Those runtime functions, together with the
175 C library, are in an object-file archive named after the system, in this case,
176 "<tt/c64.lib/". We have to specify that file on the command line, so that the
177 linker can resolve those functions.
178
179 A second file (this time, an object file) needed is the startup code that
180 prepares the grounds for the C program to run. The startup file must be
181 executed first, so it must be the first file on the linker command line.
182
183 Let's link our files to get the final executable:
184
185 <tscreen><verb>
186         ld65 -t c64 -o hello c64.o hello.o text.o c64.lib
187 </verb></tscreen>
188
189 The argument after <tt/-o/ specifies the name of the output file, the argument
190 after <tt/-t/ gives the target system. As discussed, the startup file must be
191 the first input file on the command line (you may have to add a path here, if
192 <tt/c64.o/ is not in your current directory). Since the library resolves
193 imports in <tt/hello.o/ and <tt/text.o/, it must be specified <em/after/ those
194 files.
195
196 After a successful linker run, we have a file named "<tt/hello/", ready for
197 our C64!
198
199 For more information about the linker, see <url url="ld65.html">.
200
201
202
203 <sect>The easy way (using the cl65 utility)<label id="using-cl65"><p>
204
205 The <bf/cl65/ utility is able to do all of the steps described above, in just
206 one command line, and it has defaults for some options that are very
207 well-suited for our example.
208
209 To compile both files into one executable, enter:
210
211 <tscreen><verb>
212         cl65 -O -I ../../include -L ../../lib hello.c text.s
213 </verb></tscreen>
214
215 (The <tt/-I/ option is not needed if you are working under a Unix-like system
216 with the include files in their default path, or if the <tt/CC65_INC/
217 environment variable is set correctly. The <tt/-L/ option is not needed if the
218 libraries are in their default path, or if the <tt/CC65_LIB/ environment
219 variable is set correctly. &lsqb;Those two environment variables should be set to
220 absolute paths.&rsqb;)
221
222 The <bf/cl65/ utility knows how to translate C files into object files (it will
223 call the compiler, and then, the assembler). It does know also how to create
224 object files from assembly files (it will call only the assembler, for that).
225 It knows how to build an executable (it will pass all object files to the
226 linker). And finally, it has the C64 as a default target, and will supply the
227 correct startup file and runtime library names to the linker, so you don't
228 have to care about that.
229
230 The one-liner above should give you a C64 executable named "<tt/hello/" in the
231 current directory.
232
233 For more information about the compile &amp; link utility, see <url
234 url="cl65.html">.
235
236
237
238 <sect>Running The Executable<p>
239
240 <em/Note: this section is incomplete!/
241
242 Depending on the target, cc65 chooses several methods of making a
243 program available for execution.  Here, we list sample emulators and
244 instructions for running the program.  Unless noted, similar instructions
245 would also apply to a real machine.  One word of advice: we suggest you clear
246 the screen at the start, and wait for a keypress at the end of your program,
247 as each target varies in it's start and exit conditions.
248
249
250 <sect1>Apple
251
252 <sect2>AppleWin<p>
253 Available at <url
254 url="http://applewin.berlios.de/">:
255
256 Emulates Apple&nbsp;&rsqb;&lsqb;/enhanced&nbsp;Apple&nbsp;//e computers, with
257 sound, video, joysticks, serial port, and disk images. Includes monitor. Only
258 for Windows. The package comes with a DOS 3.3 disk (called "master.dsk") image;
259 however, you will need <bf/AppleCommander 1.3.5/ or later (available at <url
260 url="http://applecommander.sourceforge.net/">).
261
262 Compile the tutorial with
263
264 <tscreen><verb>
265 cl65 -O -t apple2 hello.c text.s
266 </verb></tscreen>
267 for the Apple&nbsp;&rsqb;&lsqb;, or:
268 <tscreen><verb>
269 cl65 -O -t apple2enh hello.c text.s
270 </verb></tscreen>
271 for the enhanced&nbsp;Apple&nbsp;//e.
272
273 Then, put the file onto an Apple disk image, for use with an emulator.  Copy
274 the <tt/master.dsk/ which comes with <bf/AppleWin/, and rename it to
275 <tt/cc65.dsk/, then use <bf/AppleCommander/:
276
277 <tscreen><verb>
278 java -jar ac.jar -cc65 cc65.dsk test B < hello
279 </verb></tscreen>
280
281 Note that a convention in the Apple world is that "hello" is the file which is
282 run automatically upon booting a DOS disk, sort of like the "autoexec.bat" of
283 the MSDOS/Windows world.  We've avoided that in the example, however.  Also,
284 the <tt/B/ parameter must be in caps., and "test" is the name of the program as
285 it will appear on the Apple disk.
286
287 Start the emulator, click on the <bf/Disk 1/ icon, and point to <bf/cc65.dsk/;
288 then, click the big Apple logo, to boot the system.  Then, type this on the
289 Apple:
290
291 <tscreen><verb>
292 BRUN TEST
293 </verb></tscreen>
294
295 You will see the "Hello, World!" appear on the same line.  Thanks to Oliver
296 Schmidt, <htmlurl url="mailto:ol.sc@web.de" name="ol.sc@web.de"> for his help
297 in completing this section.
298
299
300 <sect1>Atari
301
302 <sect2>Atari800Win Plus<p>
303 Available at <url
304 url="http://www.a800win.atari-area.prv.pl">:
305
306 Emulates Atari 400/800/65XE/130XE/800XL/1200XL/5200, with stereo sound, disk
307 images, scanline-exact NTSC/PAL video, joysticks, mouse, cartridges, and RAM
308 expansions. Includes monitor. Unfortunately, only for Windows. You will need
309 the emulator, "atarixl.rom" or "atariosb.rom"/"ataribas.rom", and "dos25.xfd"
310 files (not supplied).
311
312 Compile the tutorial with
313
314 <tscreen><verb>
315 cl65 -O -t atari hello.c text.s
316 </verb></tscreen>
317
318 Start the emulator, choose <bf/File&gt;Autoboot image/ or <bf/File&gt;Load
319 executable/, and point to the "<bf/hello/" executable.  It is customary to
320 rename executables of that type to "<bf/hello.xex/".  The file has a 7-byte
321 header meant to be loaded directly from Atari DOS 2/2.5 or compatibles.
322
323 On a real Atari, you would need a disk drive, and Atari DOS 2.5 or compatible.
324 Turn on the computer, type
325
326 <tscreen><verb>
327 DOS
328 </verb></tscreen>
329
330 at the BASIC prompt, then choose <bf/N. CREATE MEM.SAV/,
331 then choose <bf/L. BINARY LOAD/, and enter <tt/HELLO/.
332
333 The emulation, also, supports that method.  Look at <bf/Atari&gt;Settings/, and
334 check <bf/Enable H: Patch for Hard Disk Devices/, then <bf/Atari&gt;Hard
335 disks/, and set the path of <bf/H1:/ to your executables directory, then use
336 "<bf/H0:HELLO.XEX/" in the above procedure (after pressing <tt/L/), to access
337 your harddrive directly.
338
339 <bf/Note/: There is no delay after the program exits, as you are returned
340 to the DOS menu.  Your C program should wait for a keypress if you want to see
341 any output.
342
343
344 <sect1>Commodore
345
346 <sect2>VICE<p>
347 Available at <url
348 url="http://www.viceteam.org/">:
349
350 Emulates Commodore 64/128/VIC-20/PET/CBM II/Plus 4 computers. Supports
351 printers, serial port and adapters, stereo sound, disk drives and images, RAM
352 expansions, cartridges, ethernet connection, cycle-exact NTSC/PAL video, mice,
353 and joysticks. Includes monitor. Runs on MSDOS/PCDOS, Win9x/ME/NT/2000/XP, OS2,
354 BeOS x86, Acorn RISC OS, and most Unixes.
355
356 Compile the tutorial with
357 <tscreen><verb>
358 cl65 -O -t &lt;sys&gt; hello.c text.s
359 </verb></tscreen>
360 Substitute the name of a Commodore computer for that <tt/&lt;sys&gt;/:
361 <itemize>
362 <item><tt/c128/
363 <item><tt/c16/
364 <item><tt/c64/
365 <item><tt/cbm510/
366 <item><tt/cbm610/
367 <item><tt/pet/
368 <item><tt/plus4/
369 <item><tt/vic20/
370 </itemize>
371
372 Start the desired version of the emulator (CBM510 and CBM610 programs run on
373 the CBM II &lsqb;<tt/xcbm2/&rsqb; emulator).
374
375 In the Windows versions of VICE, choose <bf>File&gt;Autoboot disk/tape
376 image...</bf>, choose your executable, and click <bf/OK/.
377
378 In the Unix versions, hold down the mouse's first button. Move the pointer to
379 <bf>Smart-attach disk/tape...</bf>, and release the button. Choose your
380 executable, and click <bf/Autostart/.
381
382 The file has a 14-byte header which corresponds to a PRG-format BASIC program,
383 consisting of a single line, similar to this:
384
385 <tscreen><code>
386 1000 sys2061
387 </code></tscreen>
388
389 On a real Commodore with attached disk drive, you would type:
390
391 <tscreen><verb>
392 LOAD "0:HELLO",8
393 </verb></tscreen>
394
395 for VIC-20/C64, or:
396
397 <tscreen><verb>
398 DLOAD "HELLO"
399 </verb></tscreen>
400
401 on PET/CBM II/C128/C16/Plus 4; then, type
402
403 <tscreen><verb>
404 RUN
405 </verb></tscreen>
406
407 On a Commodore 128, you can combine those two commands:
408 <tscreen><verb>
409 RUN "HELLO"
410 </verb></tscreen>
411
412 The output will appear on a separate line, and you will be returned to a BASIC
413 prompt.
414
415
416 <sect1>GEOS<p>
417 Available at <it/Click Here Software's/ <url
418 url="http://cbmfiles.com/geos/index.html" name="GEOS download section">:
419
420 <it><bf/G/raphics <bf/E/nvironment <bf/O/perating <bf/S/ystem.</it>
421 It provides a WIMP GUI (Windows, Icons, and Mouse-Pointer Graphical User
422 Interface) for Commodore's computer models <bf/64/ and <bf/128/.  It can be
423 controlled by many different types of input devices:
424 <itemize>
425 <item>keyboard
426 <item>joysticks
427 <item>mice
428 <item>trackballs
429 <item>graphics drawing tablets
430 <item>light-pens
431 </itemize>
432
433 The tutorial files are different for GEOS.  You will find them "next door," in
434 "<tt>cc65/samples/geos</tt>"; they are called "<tt/hello1.c/" and
435 "<tt/apphello1.grc/".
436
437 Compile the tutorial with
438 <tscreen><verb>
439 cl65 -O -t geos hello1.c apphello1.grc
440 </verb></tscreen>
441 Copy the resulting file "<tt/hello1/" onto a (GEOS-format) disk.
442
443 Boot the GEOS master disk/image.
444
445 <quote>
446 When you want to run GEOS in an emulator, you must adjust that emulator so that
447 it does a "true drive" emulation. Each emulator has its own way of turning that
448 feature on.
449 </quote>
450
451 <quote>
452 VICE even has different ways that depend on which operating system is running
453 the emulator.
454 <itemize>
455 <item>In Windows, you must click on <bf/Options/ (in an always visible menu).
456       Then, you must click on <bf/True drive emulation/.
457 <item>In Unix, you must <em/hold down/ the second button on your mouse.  Move
458       the pointer down to <bf/Drive settings/.  Then, move the pointer over to
459       <bf/Enable true drive emulation/.  (If there is a check-mark in front of
460       those words, that feature already is turned on -- then, move the pointer
461       off of that menu.) Release the mouse button.
462 </itemize>
463 </quote>
464
465 Find the <bf/CONVERT/ program on the boot disk &lsqb;tap the 6-key; then, you
466 should see it's icon in the fourth position on the <bf/deskTop/'s directory
467 notePad&rsqb;.  Move GEOS's pointer over to <bf/CONVERT/'s icon; double-click
468 it to run that program.  Click on the <bf/Disk/ icon; put the disk with
469 "<tt/hello1/" into the drive; and, click the <bf/OK/ icon.  Use the little
470 icons under the list of file-names to move through that list until you find
471 "<tt/hello1/".  Click on it; and then, click on the <bf/Convrt/ icon.
472 <bf/CONVERT/ will ask you to confirm that you choose the correct file; click
473 <bf/YES/ if you did (or, click <bf/NO/ if you made a mistake).  After the
474 program has converted "<tt/hello1/" from a CBM file into a GEOS file, it will
475 announce what it did -- click on <bf/OK/.  <bf/CONVERT/ will show the file list
476 again.  This time, click on <bf/Quit/.
477
478 (You might need to put the boot disk back into the drive, in order to reload
479 <bf/deskTop/.  Then, you must swap back to the disk with the tutorial program
480 on it, and click on its disk icon &lsqb;on the right side of the screen&rsqb;.)
481
482 Now, you must find <bf/hello1/.  Click on the lower left-hand corner of the
483 directory notePad.  Look at the eight file-positions on each page until you see
484 <bf/hello1/.  Double-click on its icon.
485
486 The output is shown in a GEOS dialog box; click <bf/OK/ when you have finished
487 reading it.
488
489
490 <sect1>Contributions wanted<p>
491
492 We need your help! Recommended emulators and instructions for other targets
493 are missing.  We suggest that you choose emulators with good compatibility.
494 Also, being able to run all computers in the target series is good for
495 target compatibility testing. A machine-language monitor is almost essential
496 for debugging, but a native debugger could be used, as well.
497
498 Finally, emulators which run on Unix or Windows would help to reach a wider
499 audience.
500
501 </article>