]> git.sur5r.net Git - cc65/blob - doc/intro.sgml
bc314c090220265c0dcfe627c42e15dd48939f88
[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 extracted to the same directory.
29 If you received the files as a bzip2 archive (extension *.bz2), you will need
30 to get the <htmlurl url="http://sources.redhat.com/bzip2/#bzip2-latest"
31 name="bzip2 package"> to decompress it.
32
33 <bf>Note</bf>: There is a much simpler way to compile this example using the
34 cl65 compiler and link utility. However, it makes sense to understand how the
35 separate steps work. How to do the example with the cl65 utility is described
36 <ref id="using-cl65" name="later">.
37
38
39 <sect1>Before we start<p>
40
41 You will find a copy of the sample modules used in the next section in the
42 samples/tutorial directory. Windows users will also find a small batch file in
43 this directory named "cc65setup.bat". Be sure to examine and understand the
44 commands in this file, then adjust them for your setup, and execute the file.
45
46
47 <sect1>The sample modules<p>
48
49 To explain the development flow, I will use the following example modules:
50
51 hello.c:
52
53 <tscreen><code>
54         #include <stdio.h>
55         #include <stdlib.h>
56
57         extern const char text[];       /* In text.s */
58
59         int main (void)
60         {
61             printf ("%s\n", text);
62             return EXIT_SUCCESS;
63         }
64 </code></tscreen>
65
66 text.s:
67 <tscreen><code>
68         .export _text
69         _text:  .asciiz "Hello world!"
70 </code></tscreen>
71
72
73 <sect1>Translation phases<p>
74
75 We assume that the target file should be named "hello", and the target system
76 is the C64.
77
78 <tscreen><verb>
79     +---------+
80     | hello.c |
81     +---------+
82          |
83         cc65
84          \/
85     +---------+       +---------+
86     | hello.s |       | text.s  |
87     +---------+       +---------+
88          |                 |
89         ca65              ca65
90          \/                \/
91     +---------+       +---------+       +----------+       +---------+
92     | hello.o |       | text.o  |       |  c64.o   |       | c64.lib |
93     +---------+       +---------+       +----------+       +---------+
94          |                     \         /                      |
95          |                      \       /                       |
96          |                       \     /                        |
97          +----------------------->ld65<-------------------------+
98                                    \/
99                                   hello
100 </verb></tscreen>
101
102 <tt/c64.o/ (the startup code) and <tt/c64.lib/ (the c64 version of the runtime
103 and C library) are provided in binary form in the cc65 package.
104
105
106
107 <sect>The compiler<p>
108
109 The compiler translates one C source into one assembler source for each
110 invocation. It does <em/not/ create object files directly, and it is <em/not/
111 able to translate more than one file per run.
112
113 In the example above, we would use the following command line, to translate
114 <tt/hello.c/ into <tt/hello.s/:
115
116 <tscreen><verb>
117         cc65 -O -I ../include -t c64 hello.c
118 </verb></tscreen>
119
120 The <tt/-O/ switch tells the compiler to do an additional optimizer run, which
121 is usually a good idea, since it makes the code smaller. If you don't care
122 about the size, but want to have slightly faster code, use <tt/-Oi/ to inline
123 some runtime functions.
124
125 The <tt/-I/ switch gives a search path for the include files. You may also set
126 the environment variable CC65_INC to the search path.
127
128 The <tt/-t/ switch is followed by the target system.
129
130 If the compiler does not complain about errors in our hello world, we will
131 have a file named "<tt/hello.s/" in our directory that contains the assembler
132 source for the hello module.
133
134 For more information about the compiler see <htmlurl url="cc65.html"
135 name="cc65.html">.
136
137
138
139 <sect>The assembler<p>
140
141 The assembler translates one assembler source into an object file for each
142 invocation. The assembler is <tt/not/ able to translate more than one source
143 file per run.
144
145 Let's translate the hello.s and text.s files from our example:
146
147 <tscreen><verb>
148         ca65 hello.s
149         ca65 -t c64 text.s
150 </verb></tscreen>
151
152 The <tt/-t/ switch is needed when translating the <tt/text.s/ file, so the
153 text is converted from the input character set (usually ISO-8859-1) into the
154 target character set (PETSCII) by the assembler. The compiler generated file
155 <tt/hello.s/ does not contain any character constants, so specification of a
156 target is not necessary (it wouldn't do any harm, however).
157
158 If the assembler does not complain, we should now have two object files (named
159 <tt/hello.o/ and <tt/text.o/) in the current directory.
160
161 For more information about the assembler see <htmlurl url="ca65.html"
162 name="ca65.html">.
163
164
165
166 <sect>The linker<p>
167
168 The linker combines several object and library file into one output file. ld65
169 is very configurable, but fortunately has a builtin configuration for the C64,
170 so we don't need to mess with configuration files here.
171
172 The compiler uses small functions to do things that cannot be done inline
173 without big impact on code size. These runtime functions, together with the C
174 library are in an object file archive named after the system, in this case
175 "<tt/c64.lib/". We have to specify this file on the command line so that the
176 linker can resolve these functions.
177
178 A second file (this time an object file) needed, is the startup code that
179 prepares the grounds for the C program to run. The startup file must be
180 executed first, so it must be the first file on the linker command line.
181
182 Let's link our files to get the final executable:
183
184 <tscreen><verb>
185         ld65 -t c64 -o hello c64.o hello.o text.o c64.lib
186 </verb></tscreen>
187
188 The argument after <tt/-o/ specifies the name of the output file, the argument
189 after <tt/-t/ gives the target system. As discussed, the startup file must be
190 the first file on the command line (you may have to add a path here, if
191 <tt/c64.o/ is not in your current directory). Since the library resolves
192 imports in <tt/hello.o/ and <tt/text.o/, it must be specified <em/after/ these
193 files.
194
195 After a successful linker run, we have a file named "<tt/hello/", ready for
196 our C64!
197
198 For more information about the linker see <htmlurl url="ld65.html"
199 name="ld65.html">.
200
201
202
203 <sect>The easy way (using the cl65 utility)<label id="using-cl65"><p>
204
205 The cl65 utility is able to do all of the steps described above in just one
206 call, and it has defaults for some options that are very well suited for our
207 example.
208
209 To compile both files into one executable enter
210
211 <tscreen><verb>
212         cl65 -O -I ../include hello.c text.s
213 </verb></tscreen>
214
215 (The <tt/-I/ switch is not needed if you are working under Linux with the
216 include files in the default path, or the <tt/CC65_INC/ environment variable
217 is set correctly).
218
219 The cl65 utility knows, how to translate C files into object files (it will
220 call the compiler and then the assembler). It does also know how to create
221 object files from assembler files (it will call the assember for that). It
222 knows how to build an executable (it will pass all object files to the
223 linker). And, finally, it has the C64 as a default target and will supply the
224 correct startup file and runtime library names to the linker, so you don't
225 have to care about that.
226
227 The one-liner above should give you a C64 executable named "<tt/hello/" in the
228 current directory.
229
230 For more information about the compile &amp; link utility see <htmlurl
231 url="cl65.html" name="cl65.html">.
232
233 <sect>Running The Executable<p>
234
235 <bf>Note: this section is incomplete!</bf>
236
237 Depending on the target, the compiler chooses several methods of making a
238 program available for execution.  Here we list sample emulators and
239 instructions for running the program.  Unless noted, similar instructions
240 would also apply to a real machine.
241
242 <sect1>Apple<p>
243
244 <bf>AppleWin 1.10.4</bf>: Emulates Apple II+/IIe computer, with sound, video,
245 joysticks, serial port, and disk images. Roms and dos disk included. Includes
246 monitor. Only for Windows. Unfortunately we were unable to find documentation
247 on running programs.  Please help.
248
249 <sect1>Atari<p>
250
251 <bf>Atari800Win Plus 3.0</bf> (available at
252 <url url="http://www.a800win.atari-area.prv.pl">): Emulates Atari
253 400/800/65XE/130XE/800XL/1200XL/5200, with stereo sound, disk images, scanline
254 exact NTSC/PAL video, joysticks, mouse, cartridges and ram expansions.
255 Includes monitor. Unfortunately only for Windows. You will need the emulator
256 only.  Optionally you will need atarixl.rom and/or atariosb.rom/ataribas.rom
257 and dos25.xfd files (not supplied).
258
259 Compile the tutorial with
260
261 <tscreen><verb>
262 cl65 -O -t atari hello.c text.s
263 </verb></tscreen>
264
265 Start the emulator, choose File>Autoboot image or File>Load executable, and point
266 to the hello executable.  It is customary to rename executables of this type to
267 hello.xex.  The file has a 7 byte header meant to be loaded directly from Atari
268 DOS 2/2.5 or compatibles.
269
270 On a real Atari, you would need a disk drive and Atari Dos 2.5 or compatible.
271 Turn on the computer, type
272
273 <tscreen><verb>
274 DOS
275 </verb></tscreen>
276
277 at the basic prompt, then choose N. CREATE MEM.SAV then choose L. BINARY LOAD
278 and enter HELLO.
279
280 The emulation also supports this method.  Look at Atari>Settings and check
281 Enable H: Patch for Hard Disk Devices, then Atari>Hard disks and set the path
282 of H1: to your executables directory, then use H0:HELLO.XEX in the above
283 proceedure to access your hardrive directly.
284
285 <bf>Note:</bf> There is no delay after the program exits.  Your C program
286 should wait for a keypress if you want to see any output.
287
288 <sect1>Commodore<p>
289
290 <bf>Vice 1.9</bf> (available at
291 <url url="ftp://ftp.funet.fi/pub/cbm/crossplatform/emulators/VICE/">):
292 Emulates Commodore 64/128/Vic 20/PET/CBM II computers. Missing is the Plus/4
293 and Commodore 16. Supports printer, serial port, stereo sound, disk drives and
294 images, ram expansions, cartridges, cycle exact NTSC/PAL video, mice,
295 joysticks. Includes monitor. Runs on Win9x/NT/2000/XP/ME/OS2/MSDOS, Beos x86,
296 Acorn RISC OS, and many Unixes.
297
298 Start the desired version of the emulator, choose File>Autoboot disk/tape
299 image, and choose your executable.  The file has a 14 byte header which
300 corresponds to a PRG format BASIC program, consisting of a single line;
301
302 <tscreen><code>
303 1000 sys2061
304 </code></tscreen>
305
306 On a real Commodore with attached disk drive, you would type:
307
308 <tscreen><verb>
309 LOAD "HELLO",8
310 </verb></tscreen>
311
312 for Vic 20/C64, or
313
314 <tscreen><verb>
315 DLOAD "0:HELLO"
316 </verb></tscreen>
317
318 on PET/CBM II/C128, then type
319
320 <tscreen><verb>
321 RUN
322 </verb></tscreen>
323
324 We need your help! Recommended emulators and instructions for other machines
325 are missing. We suggest an emulator with good compatibility. Also, being able
326 to run all computers in the target series is good for target compatibility
327 testing. A machine language monitor is almost essential for debugging, but a
328 native debugger could be used as well.
329
330 Finally, emulators which run on Unix/Windows would help reach a wider audience.
331
332 </article>
333
334
335