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