]> git.sur5r.net Git - cc65/blob - doc/intro.sgml
More changes from Lisa Higgins
[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 CyberX
7 <date>06.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. Windows users will also find a small batch file in
44 this directory named "cc65setup.bat". Be sure to examine and understand the
45 commands in this file, then adjust them for your setup, and execute the file.
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.
242
243 <sect1>Apple<p>
244
245 <bf>AppleWin 1.10.4</bf> (available at
246 <url url="http://www.jantzer-schmidt.de/applewin/">): Emulates Apple II+/IIe
247 computer, with sound, video, joysticks, serial port, and disk images. Roms and
248 dos disk included. Includes monitor. Only for Windows. The package comes with
249 roms and dos3.3 disk (called master.dsk), however you will need a2tools
250 (available at <url url="http://hotel04.ausys.se/pausch/apple2/#a2tools">) and
251 applehdr (included in the Apple package at util/apple/)
252
253 Compile the tutorial with
254
255 <tscreen><verb>
256 cl65 -O -t apple2 hello.c text.s
257 </verb></tscreen>
258
259 Then insert the file into an Apple disk image for use with an emulator.  Copy
260 the master.dsk which comes with Applewin and rename it to cc65.dsk, then use
261 a2tools:
262
263 <tscreen><verb>
264 a2tools in -r b cc65.dsk TEST hello
265 </verb></tscreen>
266
267 Note that a convention in the Apple world is that hello is the file which is
268 automatically run upon booting a DOS disk, sort of like the Autoexec.bat of
269 the PC world.  We've avoided this in the example however.  Also, the TEST
270 parameter must be in caps, and is the name of the program as it will appear on
271 the Apple disk.
272
273 Start the emulator, click on the Disk 1 icon, and point to cc65.dsk, then
274 click the big Apple logo to boot the system.  Then type this on the Apple:
275
276 <tscreen><verb>
277 BRUN TEST
278 </verb></tscreen>
279
280 You will see the "Hello, World!" appear on the same line.  We suggest you
281 clear the screen first in your programs. Thanks to Oliver Schmidt, <htmlurl
282 url="mailto:oliver@jantzer-schmidt.de" name="oliver@jantzer-schmidt.de"> for
283 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
293 and dos25.xfd files (not supplied).
294
295 Compile the tutorial with
296
297 <tscreen><verb>
298 cl65 -O -t atari hello.c text.s
299 </verb></tscreen>
300
301 Start the emulator, choose File>Autoboot image or File>Load executable, and point
302 to the hello executable.  It is customary to rename executables of this type to
303 hello.xex.  The file has a 7 byte header meant to be loaded directly from Atari
304 DOS 2/2.5 or compatibles.
305
306 On a real Atari, you would need a disk drive and Atari Dos 2.5 or compatible.
307 Turn on the computer, type
308
309 <tscreen><verb>
310 DOS
311 </verb></tscreen>
312
313 at the basic prompt, then choose N. CREATE MEM.SAV then choose L. BINARY LOAD
314 and enter HELLO.
315
316 The emulation also supports this method.  Look at Atari>Settings and check
317 Enable H: Patch for Hard Disk Devices, then Atari>Hard disks and set the path
318 of H1: to your executables directory, then use H0:HELLO.XEX in the above
319 proceedure to access your hardrive directly.
320
321 <bf>Note:</bf> There is no delay after the program exits.  Your C program
322 should wait for a keypress if you want to see 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 We need your help! Recommended emulators and instructions for other machines
361 are missing. We suggest an emulator with good compatibility. Also, being able
362 to run all computers in the target series is good for target compatibility
363 testing. A machine language monitor is almost essential for debugging, but a
364 native debugger could be used as well.
365
366 Finally, emulators which run on Unix/Windows would help reach a wider audience.
367
368 </article>
369
370
371