]> git.sur5r.net Git - cc65/blob - doc/intro.sgml
Fixed wrong reference
[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"> and CbmNut <htmlurl url="mailto:cbmnut@hushmail.com" name="cbmnut@hushmail.com">
7 <date>07.13.2002
8
9 <abstract>
10 How to use the cc65 C compiler - an introduction.
11 </abstract>
12
13 <!-- Table of contents -->
14 <toc>
15
16 <!-- Begin the document -->
17
18 <sect>Overview<p>
19
20 This is a short intro of how to use the compiler and the binutils. It contains a
21 step-by-step example of how to build a complete application from one C and one
22 assembler module. This file does <em/not/ contain a complete reference for the
23 tools used in the process. There are separate files describing these tools in
24 detail.
25
26 You are assumed to have downloaded and extracted the executables and the
27 target specific files. For example, for Windows users targeting C64, you need
28 cc65-win32-2.8.0.zip and cc65-c64-2.8.0.zip (or whatever the current cc65
29 version is) extracted to the same directory. If you received the files as a
30 bzip2 archive (extension *.bz2), you will need to get the <htmlurl
31 url="http://sources.redhat.com/bzip2/#bzip2-latest" name="bzip2 package"> to
32 decompress it.
33
34 <bf>Note</bf>: There is a much simpler way to compile this example using the
35 cl65 compiler and link utility. However, it makes sense to understand how the
36 separate steps work. How to do the example with the cl65 utility is described
37 <ref id="using-cl65" name="later">.
38
39
40 <sect1>Before we start<p>
41
42 You will find a copy of the sample modules used in the next section in the
43 samples/tutorial directory. Please check that the compiler and linker can
44 find the include library files by setting the environment variables
45 <tt/CC65_INC/ and <tt/CC65_LIB/ respectively.
46
47
48 <sect1>The sample modules<p>
49
50 To explain the development flow, I will use the following example modules:
51
52 hello.c:
53
54 <tscreen><code>
55         #include <stdio.h>
56         #include <stdlib.h>
57
58         extern const char text[];       /* In text.s */
59
60         int main (void)
61         {
62             printf ("%s\n", text);
63             return EXIT_SUCCESS;
64         }
65 </code></tscreen>
66
67 text.s:
68 <tscreen><code>
69         .export _text
70         _text:  .asciiz "Hello world!"
71 </code></tscreen>
72
73
74 <sect1>Translation phases<p>
75
76 We assume that the target file should be named "hello", and the target system
77 is the C64.
78
79 <tscreen><verb>
80     +---------+
81     | hello.c |
82     +---------+
83          |
84         cc65
85          \/
86     +---------+       +---------+
87     | hello.s |       | text.s  |
88     +---------+       +---------+
89          |                 |
90         ca65              ca65
91          \/                \/
92     +---------+       +---------+       +----------+       +---------+
93     | hello.o |       | text.o  |       |  c64.o   |       | c64.lib |
94     +---------+       +---------+       +----------+       +---------+
95          |                     \         /                      |
96          |                      \       /                       |
97          |                       \     /                        |
98          +----------------------->ld65<-------------------------+
99                                    \/
100                                   hello
101 </verb></tscreen>
102
103 <tt/c64.o/ (the startup code) and <tt/c64.lib/ (the c64 version of the runtime
104 and C library) are provided in binary form in the cc65 package.
105
106
107
108 <sect>The compiler<p>
109
110 The compiler translates one C source into one assembler source for each
111 invocation. It does <em/not/ create object files directly, and it is <em/not/
112 able to translate more than one file per run.
113
114 In the example above, we would use the following command line, to translate
115 <tt/hello.c/ into <tt/hello.s/:
116
117 <tscreen><verb>
118         cc65 -O -I ../include -t c64 hello.c
119 </verb></tscreen>
120
121 The <tt/-O/ switch tells the compiler to do an additional optimizer run, which
122 is usually a good idea, since it makes the code smaller. If you don't care
123 about the size, but want to have slightly faster code, use <tt/-Oi/ to inline
124 some runtime functions.
125
126 The <tt/-I/ switch gives a search path for the include files. You may also set
127 the environment variable CC65_INC to the search path.
128
129 The <tt/-t/ switch is followed by the target system.
130
131 If the compiler does not complain about errors in our hello world, we will
132 have a file named "<tt/hello.s/" in our directory that contains the assembler
133 source for the hello module.
134
135 For more information about the compiler see <htmlurl url="cc65.html"
136 name="cc65.html">.
137
138
139
140 <sect>The assembler<p>
141
142 The assembler translates one assembler source into an object file for each
143 invocation. The assembler is <tt/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) by the assembler. The compiler generated file
156 <tt/hello.s/ does not contain any character constants, so specification of a
157 target is not necessary (it wouldn't do any harm, however).
158
159 If the assembler does not complain, we should now have two object files (named
160 <tt/hello.o/ and <tt/text.o/) in the current directory.
161
162 For more information about the assembler see <htmlurl url="ca65.html"
163 name="ca65.html">.
164
165
166
167 <sect>The linker<p>
168
169 The linker combines several object and library file into one output file. ld65
170 is very configurable, but fortunately has a builtin configuration for the C64,
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 big impact on code size. These runtime functions, together with the C
175 library are in an object file archive named after the system, in this case
176 "<tt/c64.lib/". We have to specify this file on the command line so that the
177 linker can resolve these 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 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/ these
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 <htmlurl url="ld65.html"
200 name="ld65.html">.
201
202
203
204 <sect>The easy way (using the cl65 utility)<label id="using-cl65"><p>
205
206 The cl65 utility is able to do all of the steps described above in just one
207 call, and it has defaults for some options that are very well suited for our
208 example.
209
210 To compile both files into one executable enter
211
212 <tscreen><verb>
213         cl65 -O -I ../include hello.c text.s
214 </verb></tscreen>
215
216 (The <tt/-I/ switch is not needed if you are working under Linux with the
217 include files in the default path, or the <tt/CC65_INC/ environment variable
218 is set correctly).
219
220 The cl65 utility knows, how to translate C files into object files (it will
221 call the compiler and then the assembler). It does also know how to create
222 object files from assembler files (it will call the assember for that). It
223 knows how to build an executable (it will pass all object files to the
224 linker). And, finally, it has the C64 as a default target and will supply the
225 correct startup file and runtime library names to the linker, so you don't
226 have to care about that.
227
228 The one-liner above should give you a C64 executable named "<tt/hello/" in the
229 current directory.
230
231 For more information about the compile &amp; link utility see <htmlurl
232 url="cl65.html" name="cl65.html">.
233
234 <sect>Running The Executable<p>
235
236 <bf>Note: this section is incomplete!</bf>
237
238 Depending on the target, the compiler chooses several methods of making a
239 program available for execution.  Here we list sample emulators and
240 instructions for running the program.  Unless noted, similar instructions
241 would also apply to a real machine.  One word of advice: we suggest you clear
242 the screen at the start, and wait for a keypress at the end of your program,
243 as each target varies in it's start and exit conditions.
244
245 <sect1>Apple<p>
246
247 <bf>AppleWin 1.10.4</bf> (available at
248 <url url="http://www.jantzer-schmidt.de/applewin/">): Emulates Apple II+/IIe
249 computer, with sound, video, joysticks, serial port, and disk images. Roms and
250 dos disk included. Includes monitor. Only for Windows. The package comes with
251 roms and dos3.3 disk (called master.dsk), however you will need a2tools
252 (available at <url url="http://hotel04.ausys.se/pausch/apple2/#a2tools">).
253
254 Compile the tutorial with
255
256 <tscreen><verb>
257 cl65 -O -t apple2 hello.c text.s
258 </verb></tscreen>
259
260 Then insert the file into an Apple disk image for use with an emulator.  Copy
261 the master.dsk which comes with Applewin and rename it to cc65.dsk, then use
262 a2tools:
263
264 <tscreen><verb>
265 a2tools in -r b cc65.dsk TEST hello
266 </verb></tscreen>
267
268 Note that a convention in the Apple world is that hello is the file which is
269 automatically run upon booting a DOS disk, sort of like the Autoexec.bat of
270 the PC world.  We've avoided this in the example however.  Also, the TEST
271 parameter must be in caps, and is the name of the program as it will appear on
272 the Apple disk.
273
274 Start the emulator, click on the Disk 1 icon, and point to cc65.dsk, then
275 click the big Apple logo to boot the system.  Then type this on the Apple:
276
277 <tscreen><verb>
278 BRUN TEST
279 </verb></tscreen>
280
281 You will see the "Hello, World!" appear on the same line.  Thanks to Oliver
282 Schmidt, <htmlurl url="mailto:oliver@jantzer-schmidt.de"
283 name="oliver@jantzer-schmidt.de"> for his help in completing this section.
284
285 <sect1>Atari<p>
286
287 <bf>Atari800Win Plus 3.0</bf> (available at
288 <url url="http://www.a800win.atari-area.prv.pl">): Emulates Atari
289 400/800/65XE/130XE/800XL/1200XL/5200, with stereo sound, disk images, scanline
290 exact NTSC/PAL video, joysticks, mouse, cartridges and ram expansions.
291 Includes monitor. Unfortunately only for Windows. You will need the emulator,
292 atarixl.rom or atariosb.rom/ataribas.rom and dos25.xfd files (not supplied).
293
294 Compile the tutorial with
295
296 <tscreen><verb>
297 cl65 -O -t atari hello.c text.s
298 </verb></tscreen>
299
300 Start the emulator, choose File>Autoboot image or File>Load executable, and
301 point to the hello executable.  It is customary to rename executables of this
302 type to hello.xex.  The file has a 7 byte header meant to be loaded directly
303 from Atari DOS 2/2.5 or compatibles.
304
305 On a real Atari, you would need a disk drive and Atari Dos 2.5 or compatible.
306 Turn on the computer, type
307
308 <tscreen><verb>
309 DOS
310 </verb></tscreen>
311
312 at the basic prompt, then choose N. CREATE MEM.SAV then choose L. BINARY LOAD
313 and enter HELLO.
314
315 The emulation also supports this method.  Look at Atari>Settings and check
316 Enable H: Patch for Hard Disk Devices, then Atari>Hard disks and set the path
317 of H1: to your executables directory, then use H0:HELLO.XEX in the above
318 proceedure (after pressing L) to access your hardrive directly.
319
320 <bf>Note:</bf> There is no delay after the program exits, as you are returned
321 to the DOS menu.  Your C program should wait for a keypress if you want to see
322 any output.
323
324 <sect1>Commodore<p>
325
326 <bf>Vice 1.9</bf> (available at
327 <url url="ftp://ftp.funet.fi/pub/cbm/crossplatform/emulators/VICE/">):
328 Emulates Commodore 64/128/Vic 20/PET/CBM II computers. Missing is the Plus/4
329 and Commodore 16. Supports printer, serial port, stereo sound, disk drives and
330 images, ram expansions, cartridges, cycle exact NTSC/PAL video, mice,
331 joysticks. Includes monitor. Runs on Win9x/NT/2000/XP/ME/OS2/MSDOS, Beos x86,
332 Acorn RISC OS, and many Unixes.
333
334 Start the desired version of the emulator, choose File>Autoboot disk/tape
335 image, and choose your executable.  The file has a 14 byte header which
336 corresponds to a PRG format BASIC program, consisting of a single line;
337
338 <tscreen><code>
339 1000 sys2061
340 </code></tscreen>
341
342 On a real Commodore with attached disk drive, you would type:
343
344 <tscreen><verb>
345 LOAD "HELLO",8
346 </verb></tscreen>
347
348 for Vic 20/C64, or
349
350 <tscreen><verb>
351 DLOAD "0:HELLO"
352 </verb></tscreen>
353
354 on PET/CBM II/C128, then type
355
356 <tscreen><verb>
357 RUN
358 </verb></tscreen>
359
360 The output will appear on a separate line, and you will be returned to a BASIC
361 prompt.
362
363 We need your help! Recommended emulators and instructions for other targets
364 are missing. We suggest an emulator with good compatibility. Also, being able
365 to run all computers in the target series is good for target compatibility
366 testing. A machine language monitor is almost essential for debugging, but a
367 native debugger could be used as well.
368
369 Finally, emulators which run on Unix/Windows would help reach a wider audience.
370
371 </article>