]> git.sur5r.net Git - cc65/blob - doc/atari.sgml
update date
[cc65] / doc / atari.sgml
1 <!doctype linuxdoc system>
2
3 <article>
4
5 <title>Atari specific information for cc65
6 <author>Shawn Jefferson, <htmlurl
7 url="mailto:shawnjefferson@24fightingchickens.com"
8 name="shawnjefferson@24fightingchickens.com"> and 
9 Christian Groessler, <htmlurl url="mailto:cpg@aladdin.de" name="cpg@aladdin.de">
10 <date>20-Feb-2005
11
12 <abstract>
13 An overview over the Atari runtime system as it is implemented for the cc65 C
14 compiler.
15 </abstract>
16
17 <!-- Table of contents -->
18 <toc>
19
20 <!-- Begin the document -->
21
22 <sect>Overview<p>
23
24 This file contains an overview of the Atari runtime system as it comes
25 with the cc65 C compiler. It describes the memory layout, Atari specific
26 header files, available drivers, and any pitfalls specific to that
27 platform.
28
29 Please note that Atari specific functions are just mentioned here, they are
30 described in detail in the separate <htmlurl url="funcref.html" name="function
31 reference">. Even functions marked as "platform dependent" may be available on
32 more than one platform. Please see the function reference for more
33 information.
34
35
36 <sect>Binary format<p>
37
38 The standard binary output format generated by the linker for the
39 Atari target is a machine language program with a standard executable
40 header (FF FF &lt;2 byte start address&gt; &lt;2 bytes end address&gt;
41 &lsqb;program bytes&rsqb;). These values are calculated in the crt0.s
42 file from the __CODE_LOAD__ and __BSS_LOAD__ values, so keep this in
43 mind if you create a custom linker config file and start moving
44 segments around (see section <ref name="Reserving a memory area inside the program" id="memhole">).  You can
45 override this behaviour by creating your own crt0.s file and linking
46 it into your program.  A run vector is added to the end of the file
47 (&dollar;02E0 &lt;run vector&gt;) and is calculated using
48 __CODE_LOAD__ in crt0.s.
49
50
51 <sect>Memory layout<p>
52
53 The default linker script assumes that the BASIC ROM is disabled (or
54 the BASIC cartridge unplugged). This gives a usable memory range from
55 &dollar;2E00 - &dollar;BC1F. The library startup code examines the
56 current memory configuration, which depends on the size of the
57 installed memory and cartridges present, by inspecting the value in
58 the MEMTOP (&dollar;2E5) variable. Then the initial stack pointer,
59 which indicates the upper bound of memory used, is adjusted. The load
60 address of &dollar;2E00 was chosen to accommodate having a DOS loaded
61 and a driver that resides in low memory such as the 850 R: handler.
62 You can override this behaviour by creating a custom linker config
63 file.
64
65 Special locations:
66
67 <descrip>
68   <tag/Text screen/
69   The text screen depends on the installed memory size and cartridges
70   and can be obtained from the SAVMSC variable (&dollar;58).
71
72   <tag/Stack/
73   The C runtime stack is located at MEMTOP and grows downwards,
74   regardless of how your linker config file is setup.  This
75   accomodates the different memory configurations of the Atari
76   machines, as well as having a cartridge installed.  You can override
77   this behaviour by writing your own crt0.s file and linking it to
78   your program (see also <ref name="Final note"
79   id="memhole_final_note">).
80
81   <tag/Heap/
82   The C heap is located at the end of the program and grows towards the C
83   runtime stack.
84
85 </descrip><p>
86
87
88
89 <sect>Platform specific header files<p>
90
91 Programs containing Atari specific code may use the <tt/atari.h/
92 header file.
93
94
95 <sect1>Atari specific functions<p>
96
97 The functions listed below are special for the Atari. See the <htmlurl
98 url="funcref.html" name="function reference"> for declaration and usage.
99
100 <itemize>
101 <item>get_ostype
102 <item>get_tv
103 <item>_gtia_mkcolor
104 <item>_getcolor
105 <item>_getdefdev
106 <item>_graphics
107 <item>_rest_vecs
108 <item>_save_vecs
109 <item>_scroll
110 <item>_setcolor
111 <item>_setcolor_low
112 </itemize>
113
114
115 <sect1>Hardware access<p>
116
117 The following pseudo variables declared in the <tt/atari.h/ header
118 file do allow access to hardware located in the address space. Some
119 variables are structures, accessing the struct fields will access the
120 chip registers.
121
122 <descrip>
123
124   <tag><tt/GTIA_READ/ and <tt/GTIA_WRITE/</tag>
125   The <tt/GTIA_READ/ structure allows read access to the GTIA. The
126   <tt/GTIA_WRITE/ structure allows write access to the GTIA.
127   See the <tt/_gtia.h/ header file located in the include directory
128   for the declaration of the structure.
129
130   <tag><tt/POKEY_READ/ and <tt/POKEY_WRITE/</tag>
131   The <tt/POKEY_READ/ structure allows read access to the POKEY. The
132   <tt/POKEY_WRITE/ structure allows write access to the POKEY.
133   See the <tt/_pokey.h/ header file located in the include directory
134   for the declaration of the structure.
135
136   <tag><tt/ANTIC/</tag>
137   The <tt/ANTIC/ structure allows read access to the ANTIC.
138   See the <tt/_antic.h/ header file located in the include directory
139   for the declaration of the structure.
140
141   <tag><tt/PIA/</tag>
142   The <tt/PIA/ structure allows read access to the PIA 6520.
143   See the <tt/_pia.h/ header file located in the include directory
144   for the declaration of the structure.
145
146 </descrip><p>
147
148
149
150 <sect>Loadable drivers<p>
151
152 <sect1>Graphics drivers<p>
153
154 Currently there are no graphics drivers available for the Atari platform.
155
156 <sect1>Extended memory drivers<p>
157
158 Currently there are no extended memory drivers available for the Atari
159 platform.
160
161 <sect1>Joystick drivers<p>
162
163 <descrip>
164
165   <tag><tt/atari-stdjoy.joy/</tag>
166   Supports up to four standard joysticks connected to the joystick ports of
167   the Atari.
168
169 </descrip><p>
170
171
172
173 <sect1>Mouse drivers<p>
174
175 Currently no drivers available (in fact, the API for loadable mouse drivers
176 does not exist). There is a static driver you can use.
177
178
179 <sect1>RS232 device drivers<p>
180
181 Currently there are no RS232 loadable drivers available for the Atari
182 platform. There is a static driver you can use.
183
184
185 <sect>Limitations<p>
186
187
188 <sect>DIO implementation<label id="dio"><p>
189
190 The Atari supports disk drives with either 128 or 256 byte sectors.
191 The first three sectors of any disk are always 128 bytes long though. This is
192 because the system can only boot from 128 bytes sectors.
193
194 Therefore the DIO read and write functions transfer only 128 bytes
195 for sectors 1 to 3, regardless of the type of diskette.
196
197
198 <sect>Other hints<p>
199
200 <sect1>Function keys<p>
201
202 These are defined to be Atari + number key.
203
204 <sect1>Reserving a memory area inside a program<label id="memhole"><p>
205
206 The Atari 130XE maps its additional memory into CPU memory in 16K
207 chunks at address &dollar;4000 to &dollar;7FFF. One might want to
208 prevent this memory area from being used by cc65. Other reasons to
209 prevent the use of some memory area could be the buffers for display
210 lists and screen memory.
211 <p>
212 The Atari executable format allows holes inside a program, e.g. one
213 part loads into &dollar;2E00 to &dollar;3FFF, going below the reserved
214 memory area (assuming a reserved area from &dollar;4000 to
215 &dollar;7FFF), and another part loads into &dollar;8000 to
216 &dollar;BC1F.
217 <p>
218 Each load chunk of the executable starts with a 4 byte header which
219 defines its load address and size.
220 <p>
221 <sect2>Low code and high data example<p>
222 Goal: Create an executable with 2 load chunks which doesn't use the
223 memory area from &dollar;4000 to &dollar;7FFF. The CODE segment of
224 the program should go below &dollar;4000 and the DATA and RODATA
225 segments should go above &dollar;7FFF.
226 <p>
227 The main problem is that the EXE header generated by the cc65 runtine
228 lib is wrong. It defines a single load chunk with the sizes/addresses
229 of the CODE, RODATA, and DATA segments (the whole user program).
230 <p>
231 The contents of the EXE header come from the EXEHDR segment, which is
232 defined in crt0.s. This cannot be changed w/o modifiying and
233 recompiling the cc65 atari runtime lib. Therefore the original EXE
234 header must be discarded. It will be replaced by a user created
235 one.
236 <p>
237 The user needs to create a customized linker config file which adds
238 new memory areas and segments to hold the new EXE header and the
239 header data for the second load chunk. Also an assembly source file
240 needs to be created which defines the contents of the new EXE header
241 and the second load chunk header.
242 <p>
243 <p>
244 This is a modified cc65 Atari linker configuration file (split.cfg):
245 <tscreen><verb>
246 MEMORY {
247     ZP: start = $82, size = $7E, type = rw, define = yes;
248
249     HEADER: start = $0000, size = $6, file = %O;        # first load chunk
250     RAMLO: start = $2E00, size = $1200, file = %O;
251
252     BANK: start = $4000, size = $4000, file = "";
253
254     SECHDR: start = $0000, size = $4, file = %O;        # second load chunk
255     RAM: start = $8000, size = $3C20, file = %O;        # $3C20: matches upper bound $BC1F
256 }
257 SEGMENTS {
258     EXEHDR: load = BANK, type = wprot;
259
260     NEXEHDR: load = HEADER, type = wprot;               # first load chunk
261     CODE: load = RAMLO, type = wprot, define = yes;
262
263     CHKHDR: load = SECHDR, type = wprot;                # second load chunk
264     RODATA: load = RAM, type = wprot, define = yes;
265     DATA: load = RAM, type = rw, define = yes;
266     BSS: load = RAM, type = bss, define = yes;
267
268     ZEROPAGE: load = ZP, type = zp;
269     AUTOSTRT: load = RAM, type = wprot;                 # defines program entry point
270 }
271 FEATURES {
272     CONDES: segment = RODATA,
273             type = constructor,
274             label = __CONSTRUCTOR_TABLE__,
275             count = __CONSTRUCTOR_COUNT__;
276     CONDES: segment = RODATA,
277             type = destructor,
278             label = __DESTRUCTOR_TABLE__,
279             count = __DESTRUCTOR_COUNT__;
280 }
281 SYMBOLS {
282     __STACKSIZE__ = $800;       # 2K stack
283 }
284 </verb></tscreen>
285 <p>
286
287 A new memory area BANK was added which describes the reserved area.
288 It gets loaded with the contents of the old EXEHDR segment. But the
289 memory area isn't written to the output file. This way the contents of
290 the EXEHDR segment get discarded.
291 <p>
292 The added NEXEHDR segment defines the correct EXE header. It puts only
293 the CODE segment into load chunk #1 (RAMLO memory area).
294 <p>
295 The header for the second load chunk comes from the new CHKHDR
296 segment. It puts the RODATA and DATA segments into load chunk #2 (RAM
297 memory area).
298 <p>
299 <p>
300 The contents of the new NEXEHDR and CHKHDR segments come from this
301 file (split.s):
302 <tscreen><verb>
303         .import __CODE_LOAD__, __BSS_LOAD__, __CODE_SIZE__
304         .import __DATA_LOAD__, __RODATA_LOAD__
305
306         .segment "NEXEHDR"
307         .word    $FFFF          ; EXE file magic number
308         ; 1st load chunk
309         .word    __CODE_LOAD__
310         .word    __CODE_LOAD__ + __CODE_SIZE__ - 1
311
312         .segment "CHKHDR"
313         ; 2nd load chunk (contains with AUTOSTRT in fact a 3rd load chunk)
314         .word    __RODATA_LOAD__
315         .word    __BSS_LOAD__ - 1
316 </verb></tscreen>
317 <p>
318 Compile with
319 <tscreen><verb>
320 cl65 -t atari -C split.cfg -o prog.com prog.c split.s
321 </verb></tscreen>
322
323 <sect2>Low data and high code example<p>
324
325
326 Goal: Put RODATA and DATA into low memory and CODE with BSS into high
327 memory (split2.cfg):
328
329 <tscreen><verb>
330 MEMORY {
331     ZP: start = $82, size = $7E, type = rw, define = yes;
332
333     HEADER: start = $0000, size = $6, file = %O;        # first load chunk
334     RAMLO: start = $2E00, size = $1200, file = %O;
335
336     BANK: start = $4000, size = $4000, file = "";
337
338     SECHDR: start = $0000, size = $4, file = %O;        # second load chunk
339     RAM: start = $8000, size = $3C20, file = %O;        # $3C20: matches upper bound $BC1F
340 }
341 SEGMENTS {
342     EXEHDR: load = BANK, type = wprot;                  # discarded old EXE header
343
344     NEXEHDR: load = HEADER, type = wprot;               # first load chunk
345     RODATA: load = RAMLO, type = wprot, define = yes;
346     DATA: load = RAMLO, type = rw, define = yes;
347
348     CHKHDR: load = SECHDR, type = wprot;                # second load chunk
349     CODE: load = RAM, type = wprot, define = yes;
350     BSS: load = RAM, type = bss, define = yes;
351
352     ZEROPAGE: load = ZP, type = zp;
353     AUTOSTRT: load = RAM, type = wprot;                 # defines program entry point
354 }
355 FEATURES {
356     CONDES: segment = RODATA,
357             type = constructor,
358             label = __CONSTRUCTOR_TABLE__,
359             count = __CONSTRUCTOR_COUNT__;
360     CONDES: segment = RODATA,
361             type = destructor,
362             label = __DESTRUCTOR_TABLE__,
363             count = __DESTRUCTOR_COUNT__;
364 }
365 SYMBOLS {
366     __STACKSIZE__ = $800;       # 2K stack
367 }
368 </verb></tscreen>
369
370 New contents for NEXEHDR and CHKHDR are needed (split2.s):
371 <tscreen><verb>
372         .import __CODE_LOAD__, __BSS_LOAD__, __DATA_SIZE__
373         .import __DATA_LOAD__, __RODATA_LOAD__
374
375         .segment "NEXEHDR"
376         .word    $FFFF
377         .word    __RODATA_LOAD__
378         .word    __DATA_LOAD__ + __DATA_SIZE__ - 1
379
380         .segment "CHKHDR"
381         .word    __CODE_LOAD__
382         .word    __BSS_LOAD__ - 1
383 </verb></tscreen>
384
385 Compile with
386 <tscreen><verb>
387 cl65 -t atari -C split2.cfg -o prog.com prog.c split2.s
388 </verb></tscreen>
389
390 <sect2>Final note<label id="memhole_final_note"><p>
391
392 There are two other memory areas which don't appear directly in the
393 linker script. They are the stack and the heap.
394
395 The cc65 runtime lib places the stack location at the end of available
396 memory. This is dynamically set from the MEMTOP system variable at
397 startup. The heap is located in the area between the end of the BSS
398 segment and the top of the stack as defined by __STACKSIZE__.
399
400 If BSS and/or the stack shouldn't stay at the end of the program,
401 some parts of the cc65 runtime lib need to be replaced/modified.
402
403 common/_heap.s defines the location of the heap and atari/crt0.s
404 defines the location of the stack by initializing sp.
405
406
407 <sect>Bugs/Feedback<p>
408
409 If you have problems using the library, if you find any bugs, or if you're
410 doing something interesting with it, I would be glad to hear from you. Feel
411 free to contact me by email (<htmlurl url="mailto:uz@cc65.org"
412 name="uz@cc65.org"> or <htmlurl url="mailto:cpg@aladdin.de"
413 name="cpg@aladdin.de">).
414
415
416
417 <sect>License<p>
418
419 This software is provided 'as-is', without any expressed or implied
420 warranty.  In no event will the authors be held liable for any damages
421 arising from the use of this software.
422
423 Permission is granted to anyone to use this software for any purpose,
424 including commercial applications, and to alter it and redistribute it
425 freely, subject to the following restrictions:
426
427 <enum>
428 <item>  The origin of this software must not be misrepresented; you must not
429         claim that you wrote the original software. If you use this software
430         in a product, an acknowledgment in the product documentation would be
431         appreciated but is not required.
432 <item>  Altered source versions must be plainly marked as such, and must not
433         be misrepresented as being the original software.
434 <item>  This notice may not be removed or altered from any source
435         distribution.
436 </enum>
437
438 </article>