]> git.sur5r.net Git - cc65/blob - doc/intro.sgml
Add URL for AppleWin
[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> (available at
245 <url url="http://www.jantzer-schmidt.de/applewin/">): Emulates Apple II+/IIe 
246 computer, with sound, video, joysticks, serial port, and disk images. Roms and
247 dos disk included. Includes monitor. Only for Windows. Unfortunately we were
248 unable to find documentation on running programs.  Please help.
249
250 <sect1>Atari<p>
251
252 <bf>Atari800Win Plus 3.0</bf> (available at
253 <url url="http://www.a800win.atari-area.prv.pl">): Emulates Atari
254 400/800/65XE/130XE/800XL/1200XL/5200, with stereo sound, disk images, scanline
255 exact NTSC/PAL video, joysticks, mouse, cartridges and ram expansions.
256 Includes monitor. Unfortunately only for Windows. You will need the emulator
257 only.  Optionally you will need atarixl.rom and/or atariosb.rom/ataribas.rom
258 and dos25.xfd files (not supplied).
259
260 Compile the tutorial with
261
262 <tscreen><verb>
263 cl65 -O -t atari hello.c text.s
264 </verb></tscreen>
265
266 Start the emulator, choose File>Autoboot image or File>Load executable, and point
267 to the hello executable.  It is customary to rename executables of this type to
268 hello.xex.  The file has a 7 byte header meant to be loaded directly from Atari
269 DOS 2/2.5 or compatibles.
270
271 On a real Atari, you would need a disk drive and Atari Dos 2.5 or compatible.
272 Turn on the computer, type
273
274 <tscreen><verb>
275 DOS
276 </verb></tscreen>
277
278 at the basic prompt, then choose N. CREATE MEM.SAV then choose L. BINARY LOAD
279 and enter HELLO.
280
281 The emulation also supports this method.  Look at Atari>Settings and check
282 Enable H: Patch for Hard Disk Devices, then Atari>Hard disks and set the path
283 of H1: to your executables directory, then use H0:HELLO.XEX in the above
284 proceedure to access your hardrive directly.
285
286 <bf>Note:</bf> There is no delay after the program exits.  Your C program
287 should wait for a keypress if you want to see any output.
288
289 <sect1>Commodore<p>
290
291 <bf>Vice 1.9</bf> (available at
292 <url url="ftp://ftp.funet.fi/pub/cbm/crossplatform/emulators/VICE/">):
293 Emulates Commodore 64/128/Vic 20/PET/CBM II computers. Missing is the Plus/4
294 and Commodore 16. Supports printer, serial port, stereo sound, disk drives and
295 images, ram expansions, cartridges, cycle exact NTSC/PAL video, mice,
296 joysticks. Includes monitor. Runs on Win9x/NT/2000/XP/ME/OS2/MSDOS, Beos x86,
297 Acorn RISC OS, and many Unixes.
298
299 Start the desired version of the emulator, choose File>Autoboot disk/tape
300 image, and choose your executable.  The file has a 14 byte header which
301 corresponds to a PRG format BASIC program, consisting of a single line;
302
303 <tscreen><code>
304 1000 sys2061
305 </code></tscreen>
306
307 On a real Commodore with attached disk drive, you would type:
308
309 <tscreen><verb>
310 LOAD "HELLO",8
311 </verb></tscreen>
312
313 for Vic 20/C64, or
314
315 <tscreen><verb>
316 DLOAD "0:HELLO"
317 </verb></tscreen>
318
319 on PET/CBM II/C128, then type
320
321 <tscreen><verb>
322 RUN
323 </verb></tscreen>
324
325 We need your help! Recommended emulators and instructions for other machines
326 are missing. We suggest an emulator with good compatibility. Also, being able
327 to run all computers in the target series is good for target compatibility
328 testing. A machine language monitor is almost essential for debugging, but a
329 native debugger could be used as well.
330
331 Finally, emulators which run on Unix/Windows would help reach a wider audience.
332
333 </article>
334
335
336