1 <!doctype linuxdoc system>
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">
12 How to use the cc65 C language system -- an introduction.
15 <!-- Table of contents -->
18 <!-- Begin the document -->
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">).
28 I do assume that you have downloaded and installed the compiler and
29 target-specific files. Windows users should use the friendly .exe installer
30 (named cc65-2.13.0-1.exe for version 2.13.0 of the package - adjust the
31 version number if necessary). It does not only install the target files, but
32 will also set up necessary environment variables for you.
34 If you're going for the .ZIP archives, please note that there is one file for
35 the host platform (Windows, DOS or OS/2), one file for each target platform
36 (C64 or whatever) and a separate file containing the docs (which include the
37 file you're currently reading). So for most uses, you will need at least 3
38 files and unpack all three into one directory. In case of the .ZIP archives,
39 you will also need to set the environment variables <tt/CC65_INC/,
40 <tt/LD65_LIB/ and <tt/LD65_CFG/ as described below.
42 <bf/Note/: There is a much simpler way to compile this example, by using the
43 <bf/cl65/ compile-and-link utility. However, it makes sense to understand how
44 the separate steps work. How to do the example with the <bf/cl65/ utility is
45 described <ref id="using-cl65" name="later">.
48 <sect1>Before we start<p>
50 You will find a copy of the sample modules, used in the next section, in the
51 "<tt>cc65/samples/tutorial</tt>" directory. If you encounter problems with
52 missing include files and/or libraries, please check the environment variables
53 <tt/CC65_INC/, <tt/LD65_LIB/ and <tt/LD65_CFG/. They should point to the
54 <tt/include/, <tt/lib/ and <tt/cfg/ subdirectories of the directory, where you
58 <sect1>The sample modules<p>
60 To explain the development flow, I will use the following example modules:
67 extern const char text[]; /* In text.s */
71 printf ("%s\n", text);
79 _text: .asciiz "Hello world!"
83 <sect1>Translation phases<p>
85 We assume that the target file should be named "hello", and the target system
95 +---------+ +---------+
96 | hello.s | | text.s |
97 +---------+ +---------+
101 +---------+ +---------+ +----------+ +---------+
102 | hello.o | | text.o | | c64.o | | c64.lib |
103 +---------+ +---------+ +----------+ +---------+
107 +----------------------->ld65<-------------------------+
112 <tt/c64.o/ (the startup code) and <tt/c64.lib/ (the C64 version of the runtime
113 and C library) are provided in binary form in the cc65 package. Actually, the
114 startup code is contained in the library, so you won't need to care about it.
118 <sect>The compiler<p>
120 The compiler translates one C source into one assembly source, for each
121 invocation. It does <em/not/ create object files directly, and it is <em/not/
122 able to translate more than one file per run.
124 In the example above, we would use the following command line, to translate
125 <tt/hello.c/ into <tt/hello.s/:
128 cc65 -O -t c64 hello.c
131 The <tt/-O/ switch tells the compiler to do an additional optimizer run, which
132 is usually a good idea, since it makes the code smaller. If you don't care
133 about the size, but want to have slightly faster code, use <tt/-Oi/ to inline
134 some runtime functions.
136 The <tt/-t/ switch is followed by the target system name.
138 If the compiler does not complain about errors in our "hello world" program, we
139 will have a file named "<tt/hello.s/", in our directory, that contains the
140 assembly source for the <bf/hello/ module.
142 For more information about the compiler, see <url url="cc65.html">.
146 <sect>The assembler<p>
148 The assembler translates one assembly source into an object file, for each
149 invocation. The assembler is <em/not/ able to translate more than one source
152 Let's translate the "hello.s" and "text.s" files from our example:
159 The <tt/-t/ switch is needed when translating the <tt/text.s/ file, so the
160 text is converted from the input character-set (usually ISO-8859-1) into the
161 target character-set (PETSCII, in this example) by the assembler. The
162 compiler-generated file <tt/hello.s/ does not contain any character constants,
163 so specification of a target is not necessary (it wouldn't do any harm,
166 If the assembler does not complain, we should now have two object files (named
167 <tt/hello.o/ and <tt/text.o/) in the current directory.
169 For more information about the assembler, see <url url="ca65.html">.
175 The linker combines several object and library files into one output file.
176 <bf/ld65/ is very configurable, but fortunately has built-in configurations,
177 so we don't need to mess with configuration files, here.
179 The compiler uses small functions to do things that cannot be done inline
180 without a big impact on code size. Those runtime functions, together with the
181 C library, are in an object-file archive named after the system, in this case,
182 "<tt/c64.lib/". We have to specify that file on the command line, so that the
183 linker can resolve those functions.
185 Let's link our files to get the final executable:
188 ld65 -o hello -t c64 hello.o text.o c64.lib
191 The argument after <tt/-o/ specifies the name of the output file, the argument
192 after <tt/-t/ gives the target system. The following arguments are object
193 files or libraries. Since the target library resolves imports in <tt/hello.o/
194 and <tt/text.o/, it must be specified <em/after/ those files.
196 After a successful linker run, we have a file named "<tt/hello/", ready for
199 For more information about the linker, see <url url="ld65.html">.
203 <sect>The easy way (using the cl65 utility)<label id="using-cl65"><p>
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.
209 To compile both files into one executable, enter:
212 cl65 -O hello.c text.s
215 The <bf/cl65/ utility knows how to translate C files into object files (it will
216 call the compiler, and then the assembler). It does know also how to create
217 object files from assembly files (it will call only the assembler, for that).
218 It knows how to build an executable (it will pass all object files to the
219 linker). And finally, it has the C64 as a default target, and will supply the
220 correct startup file and runtime library names to the linker, so you don't
221 have to care about that.
223 The one-liner above should give you a C64 executable named "<tt/hello/" in the
226 For more information about the compile & link utility, see <url
231 <sect>Running The Executable<p>
233 <em/Note: this section is incomplete!/
235 Depending on the target, cc65 chooses several methods of making a program
236 available for execution. Here, we list sample emulators and instructions for
237 running the program. Unless noted, similar instructions would also apply to a
238 real machine. One word of advice: we suggest you clear the screen at the
239 start, and wait for a keypress at the end of your program, as each target
240 varies in its start and exit conditions.
247 url="http://applewin.berlios.de/">:
249 Emulates Apple ][/enhanced Apple //e computers, with
250 sound, video, joysticks, serial port, and disk images. Includes monitor. Only
251 for Windows. The package comes with a DOS 3.3 disk (called "master.dsk") image;
252 however, you will need <bf/AppleCommander 1.3.5/ or later (available at <url
253 url="http://applecommander.sourceforge.net/">).
255 Compile the tutorial with
258 cl65 -O -t apple2 hello.c text.s
260 for the Apple ][, or:
262 cl65 -O -t apple2enh hello.c text.s
264 for the enhanced Apple //e.
266 Then, put the file onto an Apple disk image, for use with an emulator. Copy
267 the <tt/master.dsk/ which comes with <bf/AppleWin/, and rename it to
268 <tt/cc65.dsk/, then use <bf/AppleCommander/:
271 java -jar ac.jar -cc65 cc65.dsk test B < hello
274 Note that a convention in the Apple world is that "hello" is the file which is
275 run automatically upon booting a DOS disk, sort of like the "autoexec.bat" of
276 the MSDOS/Windows world. We've avoided that in the example, however. Also,
277 the <tt/B/ parameter must be in caps., and "test" is the name of the program as
278 it will appear on the Apple disk.
280 Start the emulator, click on the <bf/Disk 1/ icon, and point to <bf/cc65.dsk/;
281 then, click the big Apple logo, to boot the system. Then, type this on the
288 You will see the "Hello, World!" appear on the same line. Thanks to Oliver
289 Schmidt, <htmlurl url="mailto:ol.sc@web.de" name="ol.sc@web.de"> for his help
290 in completing this section.
295 <sect2>Atari800Win Plus<p>
297 url="http://www.a800win.atari-area.prv.pl">:
299 Emulates Atari 400/800/65XE/130XE/800XL/1200XL/5200, with stereo sound, disk
300 images, scanline-exact NTSC/PAL video, joysticks, mouse, cartridges, and RAM
301 expansions. Includes monitor. Unfortunately, only for Windows. You will need
302 the emulator, "atarixl.rom" or "atariosb.rom"/"ataribas.rom", and "dos25.xfd"
303 files (not supplied).
305 Compile the tutorial with
308 cl65 -O -t atari hello.c text.s
311 Start the emulator, choose <bf/File>Autoboot image/ or <bf/File>Load
312 executable/, and point to the "<bf/hello/" executable. It is customary to
313 rename executables of that type to "<bf/hello.xex/". The file has a 7-byte
314 header meant to be loaded directly from Atari DOS 2/2.5 or compatibles.
316 On a real Atari, you would need a disk drive, and Atari DOS 2.5 or compatible.
317 Turn on the computer, type
323 at the BASIC prompt, then choose <bf/N. CREATE MEM.SAV/,
324 then choose <bf/L. BINARY LOAD/, and enter <tt/HELLO/.
326 The emulation, also, supports that method. Look at <bf/Atari>Settings/, and
327 check <bf/Enable H: Patch for Hard Disk Devices/, then <bf/Atari>Hard
328 disks/, and set the path of <bf/H1:/ to your executables directory, then use
329 "<bf/H0:HELLO.XEX/" in the above procedure (after pressing <tt/L/), to access
330 your harddrive directly.
332 <bf/Note/: There is no delay after the program exits, as you are returned
333 to the DOS menu. Your C program should wait for a keypress if you want to see
341 url="http://code.google.com/p/oriculator/">:
343 Emulates Oric-1 and Atmos computers, with sound, disk images,
344 scanline-exact NTSC/PAL video, and movie export. Includes monitor.
345 Fortunately for all SDL platforms. You will just need the emulator, all
348 Compile the tutorial with
351 cl65 -O -t atmos hello.c text.s -o hello.tap
354 Start the emulator, choose <bf/F1/ and <bf/Insert tape.../, and point to
355 the "<bf/hello.tap/" executable. The file has an auto start header meant to
356 be loaded directly from tape.
358 On a real Atmos, you would need a tape drive.
359 Turn on the computer, type
367 The emulation, also, supports that method.
374 url="http://www.viceteam.org/">:
376 Emulates Commodore 64/128/VIC-20/PET/CBM II/Plus 4 computers. Supports
377 printers, serial port and adapters, stereo sound, disk drives and images, RAM
378 expansions, cartridges, ethernet connection, cycle-exact NTSC/PAL video, mice,
379 and joysticks. Includes monitor. Runs on MSDOS/PCDOS, Win9x/ME/NT/2000/XP, OS2,
380 BeOS x86, Acorn RISC OS, and most Unixes.
382 Compile the tutorial with
384 cl65 -O -t <sys> hello.c text.s
386 Substitute the name of a Commodore computer for that <tt/<sys>/:
398 Start the desired version of the emulator (CBM510 and CBM610 programs run on
399 the CBM II [<tt/xcbm2/] emulator).
401 In the Windows versions of VICE, choose <bf>File>Autoboot disk/tape
402 image...</bf>, choose your executable, and click <bf/OK/.
404 In the Unix versions, hold down the mouse's first button. Move the pointer to
405 <bf>Smart-attach disk/tape...</bf>, and release the button. Choose your
406 executable, and click <bf/Autostart/.
408 The file has a 14-byte header which corresponds to a PRG-format BASIC program,
409 consisting of a single line, similar to this:
415 On a real Commodore with attached disk drive, you would type:
427 on PET/CBM II/C128/C16/Plus 4; then, type
433 On a Commodore 128, you can combine those two commands:
438 The output will appear on a separate line, and you will be returned to a BASIC
443 Available at <it/Click Here Software's/ <url
444 url="http://cbmfiles.com/geos/index.html" name="GEOS download section">:
446 <it><bf/G/raphics <bf/E/nvironment <bf/O/perating <bf/S/ystem.</it>
447 It provides a WIMP GUI (Windows, Icons, and Mouse-Pointer Graphical User
448 Interface) for Commodore's computer models <bf/64/ and <bf/128/. It can be
449 controlled by many different types of input devices:
455 <item>graphics drawing tablets
459 The tutorial files are different for GEOS. You will find them "next door," in
460 "<tt>cc65/samples/geos</tt>"; they are called "<tt/hello1.c/" and
461 "<tt/apphello1.grc/".
463 Compile the tutorial with
465 cl65 -O -t geos hello1.c apphello1.grc
467 Copy the resulting file "<tt/hello1/" onto a (GEOS-format) disk.
469 Boot the GEOS master disk/image.
472 When you want to run GEOS in an emulator, you must adjust that emulator so that
473 it does a "true drive" emulation. Each emulator has its own way of turning that
478 VICE even has different ways that depend on which operating system is running
481 <item>In Windows, you must click on <bf/Options/ (in an always visible menu).
482 Then, you must click on <bf/True drive emulation/.
483 <item>In Unix, you must <em/hold down/ the second button on your mouse. Move
484 the pointer down to <bf/Drive settings/. Then, move the pointer over to
485 <bf/Enable true drive emulation/. (If there is a check-mark in front of
486 those words, that feature already is turned on -- then, move the pointer
487 off of that menu.) Release the mouse button.
491 Find the <bf/CONVERT/ program on the boot disk [tap the 6-key; then, you
492 should see its icon in the fourth position on the <bf/deskTop/'s directory
493 notePad]. Move GEOS's pointer over to <bf/CONVERT/'s icon; double-click
494 it to run that program. Click on the <bf/Disk/ icon; put the disk with
495 "<tt/hello1/" into the drive; and, click the <bf/OK/ icon. Use the little
496 icons under the list of file-names to move through that list until you find
497 "<tt/hello1/". Click on it; and then, click on the <bf/Convrt/ icon.
498 <bf/CONVERT/ will ask you to confirm that you choose the correct file; click
499 <bf/YES/ if you did (or, click <bf/NO/ if you made a mistake). After the
500 program has converted "<tt/hello1/" from a CBM file into a GEOS file, it will
501 announce what it did -- click on <bf/OK/. <bf/CONVERT/ will show the file list
502 again. This time, click on <bf/Quit/.
504 (You might need to put the boot disk back into the drive, in order to reload
505 <bf/deskTop/. Then, you must swap back to the disk with the tutorial program
506 on it, and click on its disk icon [on the right side of the screen].)
508 Now, you must find <bf/hello1/. Click on the lower left-hand corner of the
509 directory notePad. Look at the eight file-positions on each page until you see
510 <bf/hello1/. Double-click on its icon.
512 The output is shown in a GEOS dialog box; click <bf/OK/ when you have finished
516 <sect1>Contributions wanted<p>
518 We need your help! Recommended emulators and instructions for other targets
519 are missing. We suggest that you choose emulators with good compatibility.
520 Also, being able to run all computers in the target series is good for
521 target compatibility testing. A machine-language monitor is almost essential
522 for debugging, but a native debugger could be used, as well.
524 Finally, emulators which run on Unix or Windows would help to reach a wider