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 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">
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">.
42 <sect1>Before we start<p>
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.
50 <sect1>The sample modules<p>
52 To explain the development flow, I will use the following example modules:
59 extern const char text[]; /* In text.s */
63 printf ("%s\n", text);
71 _text: .asciiz "Hello world!"
75 <sect1>Translation phases<p>
77 We assume that the target file should be named "hello", and the target system
87 +---------+ +---------+
88 | hello.s | | text.s |
89 +---------+ +---------+
93 +---------+ +---------+ +----------+ +---------+
94 | hello.o | | text.o | | c64.o | | c64.lib |
95 +---------+ +---------+ +----------+ +---------+
99 +----------------------->ld65<-------------------------+
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.
109 <sect>The compiler<p>
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.
115 In the example above, we would use the following command line, to translate
116 <tt/hello.c/ into <tt/hello.s/:
119 cc65 -O -I ../../include -t c64 hello.c
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.
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.
130 The <tt/-t/ switch is followed by the target system name.
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.
136 For more information about the compiler, see <url url="cc65.html">.
140 <sect>The assembler<p>
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
146 Let's translate the "hello.s" and "text.s" files from our example:
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,
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.
163 For more information about the assembler, see <url url="ca65.html">.
169 The linker combines several object and library files into one output file.
170 <bf/ld65/ is very configurable, but fortunately has a built-in configuration
171 for the C64, so we don't need to mess with configuration files, here.
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.
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.
183 Let's link our files to get the final executable:
186 ld65 -t c64 -o hello c64.o hello.o text.o c64.lib
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
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 -I ../../include -L ../../lib hello.c text.s
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. [Those two environment variables should be set to
220 absolute paths.])
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.
230 The one-liner above should give you a C64 executable named "<tt/hello/" in the
233 For more information about the compile & link utility, see <url
238 <sect>Running The Executable<p>
240 <em/Note: this section is incomplete!/
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.
252 <sect2>AppleWin 1.10.4<p>
253 Available at <url url="http://www.jantzer-schmidt.de/applewin/">:
255 Emulates Apple II+/IIe computers, with sound, video, joysticks, serial port,
256 and disk images. Includes monitor. Only for Windows. The package comes with
257 ROM and DOS 3.3 disk (called "master.dsk") images; however, you will need
258 <bf/a2tools/ (available at <url
259 url="http://hotel04.ausys.se/pausch/apple2/#a2tools">).
261 Compile the tutorial with
264 cl65 -O -t apple2 hello.c text.s
266 for the Apple II, or:
268 cl65 -O -t apple2enh hello.c text.s
272 Then, insert the file into an Apple disk image, for use with an emulator. Copy
273 the <tt/master.dsk/ which comes with <bf/Applewin/, and rename it to
274 <tt/cc65.dsk/, then use <bf/a2tools/:
277 a2tools in -r b cc65.dsk TEST hello
280 Note that a convention in the Apple world is that "hello" is the file which is
281 run automatically upon booting a DOS disk, sort of like the "autoexec.bat" of
282 the MSDOS/Windows world. We've avoided that in the example, however. Also,
283 the <tt/TEST/ parameter must be in caps., and is the name of the program as it
284 will appear on the Apple disk.
286 Start the emulator, click on the <bf/Disk 1/ icon, and point to <bf/cc65.dsk/;
287 then, click the big Apple logo, to boot the system. Then, type this on the
294 You will see the "Hello, World!" appear on the same line. Thanks to Oliver
295 Schmidt, <htmlurl url="mailto:oliver@jantzer-schmidt.de"
296 name="oliver@jantzer-schmidt.de"> for his help in completing this section.
301 <sect2>Atari800Win Plus 3.0<p>
302 Available at <url url="http://www.a800win.atari-area.prv.pl">:
304 Emulates Atari 400/800/65XE/130XE/800XL/1200XL/5200, with stereo sound, disk
305 images, scanline-exact NTSC/PAL video, joysticks, mouse, cartridges, and RAM
306 expansions. Includes monitor. Unfortunately, only for Windows. You will need
307 the emulator, "atarixl.rom" or "atariosb.rom"/"ataribas.rom", and "dos25.xfd"
308 files (not supplied).
310 Compile the tutorial with
313 cl65 -O -t atari hello.c text.s
316 Start the emulator, choose <bf/File>Autoboot image/ or <bf/File>Load
317 executable/, and point to the "<bf/hello/" executable. It is customary to
318 rename executables of that type to "<bf/hello.xex/". The file has a 7-byte
319 header meant to be loaded directly from Atari DOS 2/2.5 or compatibles.
321 On a real Atari, you would need a disk drive, and Atari DOS 2.5 or compatible.
322 Turn on the computer, type
328 at the BASIC prompt, then choose <bf/N. CREATE MEM.SAV/,
329 then choose <bf/L. BINARY LOAD/, and enter <tt/HELLO/.
331 The emulation, also, supports that method. Look at <bf/Atari>Settings/, and
332 check <bf/Enable H: Patch for Hard Disk Devices/, then <bf/Atari>Hard
333 disks/, and set the path of <bf/H1:/ to your executables directory, then use
334 "<bf/H0:HELLO.XEX/" in the above procedure (after pressing <tt/L/), to access
335 your harddrive directly.
337 <bf/Note/: There is no delay after the program exits, as you are returned
338 to the DOS menu. Your C program should wait for a keypress if you want to see
346 url="http://www.zimmers.net/anonftp/pub/cbm/crossplatform/emulators/VICE/">,
348 url="http://www.ibiblio.org/pub/micro/commodore/crossplatform/emulators/VICE/">:
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.
356 Compile the tutorial with
358 cl65 -O -t <sys> hello.c text.s
360 Substitute the name of a Commodore computer for that <tt/<sys>/:
372 Start the desired version of the emulator (CBM510 and CBM610 programs run on
373 the CBM II [<tt/xcbm2/] emulator).
375 In the Windows versions of VICE, choose <bf>File>Autoboot disk/tape
376 image...</bf>, choose your executable, and click <bf/OK/.
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/.
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:
389 On a real Commodore with attached disk drive, you would type:
401 on PET/CBM II/C128/C16/Plus 4; then, type
407 On a Commodore 128, you can combine those two commands:
412 The output will appear on a separate line, and you will be returned to a BASIC
417 Available at <it/Click Here Software's/ <url
418 url="http://cmdrkey.com/cbm/geos/geos1.html" name="GEOS download page">:
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:
429 <item>graphics drawing tablets
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/".
437 Compile the tutorial with
439 cl65 -O -t geos hello1.c apphello1.grc
441 Copy the resulting file "<tt/hello1/" onto a (GEOS-format) disk.
443 Boot the GEOS master disk/image.
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
452 VICE even has different ways that depend on which operating system is running
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.
465 Find the <bf/CONVERT/ program on the boot disk [tap the 6-key; then, you
466 should see it's icon in the fourth position on the <bf/deskTop/'s directory
467 notePad]. 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/.
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 [on the right side of the screen].)
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.
486 The output is shown in a GEOS dialog box; click <bf/OK/ when you have finished
490 <sect1>Contributions wanted<p>
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.
498 Finally, emulators which run on Unix or Windows would help to reach a wider