]> git.sur5r.net Git - cc65/blob - doc/atari.sgml
5450b29900aa2c31378df1a227727b629ed077fa
[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:chris@groessler.org" name="chris@groessler.org">
10 <date>03-Jan-2006
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 __STARTUP_LOAD__ and __ZPSAVE_LOAD__ values, so keep
43 this in mind if you create a custom linker config file and start
44 moving segments around (see section
45 <ref name="Reserving a memory area inside the program" id="memhole">).
46 You can override this behaviour by creating your own crt0.s file and
47 linking it into your program.  A run vector is added to the end of the
48 file (&dollar;02E0 &lt;run vector&gt;) and is calculated using
49 __STARTUP_LOAD__ in crt0.s.
50
51
52 <sect>Memory layout<p>
53
54 The default linker script assumes that the BASIC ROM is disabled (or
55 the BASIC cartridge unplugged). This gives a usable memory range from
56 &dollar;2E00 - &dollar;BC1F. The library startup code examines the
57 current memory configuration, which depends on the size of the
58 installed memory and cartridges present, by inspecting the value in
59 the MEMTOP (&dollar;2E5) variable. Then the initial stack pointer,
60 which indicates the upper bound of memory used, is adjusted. The
61 default load address of &dollar;2E00 was chosen to accommodate having
62 a DOS loaded and a driver that resides in low memory such as the 850
63 R: handler. You can override this behaviour by creating a custom
64 linker config file or by using the "--start-addr" cl65 command line
65 argument or the "--start-addr" or "-S" ld65 command line arguments.
66
67 Special locations:
68
69 <descrip>
70   <tag/Text screen/
71   The text screen depends on the installed memory size and cartridges
72   and can be obtained from the SAVMSC variable (&dollar;58).
73
74   <tag/Stack/
75   The C runtime stack is located at MEMTOP and grows downwards,
76   regardless of how your linker config file is setup.  This
77   accommodates the different memory configurations of the Atari
78   machines, as well as having a cartridge installed.  You can override
79   this behaviour by writing your own crt0.s file and linking it to
80   your program (see also <ref name="Final note"
81   id="memhole_final_note">).
82
83   <tag/Heap/
84   The C heap is located at the end of the program and grows towards the C
85   runtime stack.
86
87 </descrip><p>
88
89
90
91 <sect>Platform specific header files<p>
92
93 Programs containing Atari specific code may use the <tt/atari.h/
94 header file.
95
96
97 <sect1>Atari specific functions<p>
98
99 The functions and global variable listed below are special for the Atari.
100 See the <htmlurl url="funcref.html" name="function reference"> for declaration and usage.
101
102 <itemize>
103 <item>get_ostype
104 <item>get_tv
105 <item>_dos_type
106 <item>_gtia_mkcolor
107 <item>_getcolor
108 <item>_getdefdev
109 <item>_graphics
110 <item>_rest_vecs
111 <item>_save_vecs
112 <item>_scroll
113 <item>_setcolor
114 <item>_setcolor_low
115 </itemize>
116
117
118 <sect1>Hardware access<p>
119
120 The following pseudo variables declared in the <tt/atari.h/ header
121 file do allow access to hardware located in the address space. Some
122 variables are structures, accessing the struct fields will access the
123 chip registers.
124
125 <descrip>
126
127   <tag><tt/GTIA_READ/ and <tt/GTIA_WRITE/</tag>
128   The <tt/GTIA_READ/ structure allows read access to the GTIA. The
129   <tt/GTIA_WRITE/ structure allows write access to the GTIA.
130   See the <tt/_gtia.h/ header file located in the include directory
131   for the declaration of the structure.
132
133   <tag><tt/POKEY_READ/ and <tt/POKEY_WRITE/</tag>
134   The <tt/POKEY_READ/ structure allows read access to the POKEY. The
135   <tt/POKEY_WRITE/ structure allows write access to the POKEY.
136   See the <tt/_pokey.h/ header file located in the include directory
137   for the declaration of the structure.
138
139   <tag><tt/ANTIC/</tag>
140   The <tt/ANTIC/ structure allows read access to the ANTIC.
141   See the <tt/_antic.h/ header file located in the include directory
142   for the declaration of the structure.
143
144   <tag><tt/PIA/</tag>
145   The <tt/PIA/ structure allows read access to the PIA 6520.
146   See the <tt/_pia.h/ header file located in the include directory
147   for the declaration of the structure.
148
149 </descrip><p>
150
151
152
153 <sect>Loadable drivers<p>
154
155 <sect1>Graphics drivers<p>
156
157 <descrip>
158
159   <tag><tt/atari10.tgi (atari_10)/</tag>
160
161   <tag><tt/atr10p2.tgi (atari_10p2)/</tag>
162
163   <tag><tt/atari11.tgi (atari_11)/</tag>
164
165   <tag><tt/atari14.tgi (atari_14)/</tag>
166
167   <tag><tt/atari15.tgi (atari_15)/</tag>
168
169   <tag><tt/atr15p2.tgi (atari_15p2)/</tag>
170
171   <tag><tt/atari3.tgi (atari_3)/</tag>
172
173   <tag><tt/atari4.tgi (atari_4)/</tag>
174
175   <tag><tt/atari5.tgi (atari_5)/</tag>
176
177   <tag><tt/atari6.tgi (atari_6)/</tag>
178
179   <tag><tt/atari7.tgi (atari_7)/</tag>
180
181   <tag><tt/atari8.tgi (atari_8)/</tag>
182
183   <tag><tt/atr8p2.tgi (atari_8p2)/</tag>
184
185   <tag><tt/atari9.tgi (atari_9)/</tag>
186
187   <tag><tt/atr9p2.tgi (atari_9p2)/</tag>
188
189 </descrip><p>
190
191 Many graphics modes require more memory than the text screen which is
192 in effect when the program starts up. Therefore the programmer has to
193 tell the program beforehand the memory requirements of the graphics
194 modes the program intends to use.
195 This can be done by using the __RESERVED_MEMORY__ linker config
196 variable. The number specified there describes the number of bytes to
197 subtract from the top of available memory as seen from the runtime
198 library. This memory is then used by the screen buffer.
199
200 The numbers for the different graphics modes presented below should
201 only be seen as a rule of thumb. Since the screen buffer memory needs
202 to start at specific boundaries, the numbers depend on the current top
203 of available memory.
204 The following numbers were determined by a BASIC program.
205
206 <table>
207 <tabular ca="rr">
208 graphics mode|reserved memory@<hline>
209 0|1@
210 1|1@
211 2|1@
212 3|1@
213 4|1@
214 5|182@
215 6|1182@
216 7|3198@
217 8|7120@
218 9|7146@
219 10|7146@
220 11|7146@
221 12|162@
222 13|1@
223 14|3278@
224 15|7120@
225 16|1@
226 17|1@
227 18|1@
228 19|1@
229 20|1@
230 21|184@
231 22|1192@
232 23|3208@
233 24|7146@
234 25|7146@
235 26|7146@
236 27|7146@
237 28|162@
238 29|1@
239 30|3304@
240 31|7146
241 </tabular>
242 <caption>reserved memory required for different graphics modes
243 </table>
244
245 The values of "1" are needed because the graphics command crashes if
246 it doesn't have at least one byte available. This seems to be a bug of
247 the Atari ROM code.
248
249 <sect1>Extended memory drivers<p>
250
251 <descrip>
252
253   <tag><tt/atr130xe.emd (atari_130xe)/</tag>
254
255 </descrip><p>
256
257
258 <sect1>Joystick drivers<p>
259
260 <descrip>
261
262   <tag><tt/ataristd.joy (atari_stdjoy)/</tag>
263   Supports up to four standard joysticks connected to the joystick ports of
264   the Atari.
265
266   <tag><tt/atarim8.joy (atari_multijoy)/</tag>
267   Supports up to eight standard joysticks connected to a MultiJoy adapter.
268
269 </descrip><p>
270
271
272 <sect1>Mouse drivers<p>
273
274 Currently no drivers available (in fact, the API for loadable mouse drivers
275 does not exist). There is a static driver you can use.
276
277
278 <sect1>RS232 device drivers<p>
279
280 Currently there are no RS232 loadable drivers available for the Atari
281 platform. There is a static driver you can use.
282
283
284 <sect>Limitations<p>
285
286
287 <sect>DIO implementation<label id="dio"><p>
288
289 The Atari supports disk drives with either 128 or 256 byte sectors.
290 The first three sectors of any disk are always 128 bytes long though. This is
291 because the system can only boot from 128 bytes sectors.
292
293 Therefore the DIO read and write functions transfer only 128 bytes
294 for sectors 1 to 3, regardless of the type of diskette.
295
296
297 <sect>CONIO implementation<label id="conio"><p>
298
299 The console I/O is speed optimized therefore support for XEP80 hardware
300 or f80.com software is missing. Of course you may use stdio.h functions.
301
302
303 <sect>Other hints<p>
304
305
306 <sect1>Function keys<p>
307
308 Function keys are mapped to Atari + number key.
309
310
311 <sect1>Passing arguments to the program<p>
312
313 Command line arguments can be passed to <tt/main()/ when DOS supports it.
314
315 <enum>
316 <item>Arguments are separated by spaces.
317 <item>Leading and trailing spaces around an argument are ignored.
318 <item>The first argument passed to <tt/main/ is the program name.
319 <item>A maximum number of 16 arguments (including the program name) are
320       supported.                                                       
321 </enum>
322
323
324 <sect1>Interrupts<p>
325
326 The runtime for the Atari uses routines marked as <tt/.INTERRUPTOR/ for
327 interrupt handlers. Such routines must be written as simple machine language
328 subroutines and will be called automatically by the VBI handler code
329 when they are linked into a program. See the discussion of the <tt/.CONDES/
330 feature in the <htmlurl url="ca65.html" name="assembler manual">.
331
332
333 <sect1>Reserving a memory area inside a program<label id="memhole"><p>
334
335 The Atari 130XE maps its additional memory into CPU memory in 16K
336 chunks at address &dollar;4000 to &dollar;7FFF. One might want to
337 prevent this memory area from being used by cc65. Other reasons to
338 prevent the use of some memory area could be to reserve space for the
339 buffers for display lists and screen memory.
340 <p>
341 The Atari executable format allows holes inside a program, e.g. one
342 part loads into &dollar;2E00 to &dollar;3FFF, going below the reserved
343 memory area (assuming a reserved area from &dollar;4000 to
344 &dollar;7FFF), and another part loads into &dollar;8000 to
345 &dollar;BC1F.
346 <p>
347 Each load chunk of the executable starts with a 4 byte header which
348 defines its load address and size. In the following linker scripts
349 these headers are named HEADER and SECHDR (for the MEMORY layout), and
350 accordingly NEXEHDR and CHKHDR (for the SEGMENTS layout).
351 <p>
352 <sect2>Low code and high data example<p>
353 Goal: Create an executable with 2 load chunks which doesn't use the
354 memory area from &dollar;4000 to &dollar;7FFF. The CODE segment of
355 the program should go below &dollar;4000 and the DATA and RODATA
356 segments should go above &dollar;7FFF.
357 <p>
358 The main problem is that the EXE header generated by the cc65 runtime
359 lib is wrong. It defines a single load chunk with the sizes/addresses
360 of the STARTUP, LOWCODE, INIT, CODE, RODATA, and DATA segments (the whole user
361 program).
362 <p>
363 The contents of the EXE header come from the EXEHDR segment, which is
364 defined in crt0.s. This cannot be changed without modifying and
365 recompiling the cc65 atari runtime lib. Therefore the original EXE
366 header must be discarded. It will be replaced by a user created
367 one. The discarding is done by assigning the EXEHDR segment to the
368 BANK memory area. The BANK memory area is discarded in the new linker
369 script (written to file "").
370 <p>
371 The user needs to create a customized linker config file which adds
372 new memory areas and segments to hold the new EXE header and the
373 header data for the second load chunk. Also an assembly source file
374 needs to be created which defines the contents of the new EXE header
375 and the second load chunk header.
376 <p>
377 <p>
378 This is an example of a modified cc65 Atari linker configuration file
379 (split.cfg):
380 <tscreen><verb>
381 SYMBOLS {
382     __STACKSIZE__ = $800;                               # 2K stack
383     __RESERVED_MEMORY__: value = $0000, weak = yes;
384 }
385 FEATURES {
386     STARTADDRESS: default = $2E00;
387 }
388 MEMORY {
389     ZP: start = $82, size = $7E, type = rw, define = yes;
390
391     HEADER: start = $0000, size = $6, file = %O;        # first load chunk
392     RAMLO: start = %S, size = $4000 - %S, file = %O;
393
394     BANK: start = $4000, size = $4000, file = "";
395
396     SECHDR: start = $0000, size = $4, file = %O;        # second load chunk
397     RAM: start = $8000, size = $3C20, file = %O;        # $3C20: matches upper bound $BC1F
398 }
399 SEGMENTS {
400     EXEHDR: load = BANK, type = ro;
401
402     NEXEHDR: load = HEADER, type = ro;                  # first load chunk
403     STARTUP: load = RAMLO, type = ro, define = yes;
404     LOWCODE: load = RAMLO, type = ro, define = yes, optional = yes;
405     INIT: load = RAMLO, type = ro, optional = yes;
406     CODE: load = RAMLO, type = ro, define = yes;
407
408     CHKHDR: load = SECHDR, type = ro;                   # second load chunk
409     RODATA: load = RAM, type = ro, define = yes;
410     DATA: load = RAM, type = rw, define = yes;
411     BSS: load = RAM, type = bss, define = yes;
412     ZPSAVE: load = RAM, type = bss, define = yes;
413
414     ZEROPAGE: load = ZP, type = zp;
415     AUTOSTRT: load = RAM, type = ro;                    # defines program entry point
416 }
417 FEATURES {
418     CONDES: segment = RODATA,
419             type = constructor,
420             label = __CONSTRUCTOR_TABLE__,
421             count = __CONSTRUCTOR_COUNT__;
422     CONDES: segment = RODATA,
423             type = destructor,
424             label = __DESTRUCTOR_TABLE__,
425             count = __DESTRUCTOR_COUNT__;
426 }
427 </verb></tscreen>
428 <p>
429
430 A new memory area BANK was added which describes the reserved area.
431 It gets loaded with the contents of the old EXEHDR segment. But the
432 memory area isn't written to the output file. This way the contents of
433 the EXEHDR segment get discarded.
434 <p>
435 The newly added NEXEHDR segment defines the correct EXE header. It
436 puts the STARTUP, LOWCODE, INIT, and CODE segments, which are the
437 segments containing only code, into load chunk #1 (RAMLO memory area).
438 <p>
439 The header for the second load chunk comes from the new CHKHDR
440 segment. It puts the RODATA, DATA, BSS, and ZPSAVE segments into load
441 chunk #2 (RAM memory area).
442 <p>
443 <p>
444 The contents of the new NEXEHDR and CHKHDR segments come from this
445 file (split.s):
446 <tscreen><verb>
447         .import __CODE_LOAD__, __BSS_LOAD__, __CODE_SIZE__
448         .import __DATA_LOAD__, __RODATA_LOAD__, __STARTUP_LOAD__
449
450         .segment "NEXEHDR"
451         .word    $FFFF
452         .word    __STARTUP_LOAD__
453         .word    __CODE_LOAD__ + __CODE_SIZE__ - 1
454
455         .segment "CHKHDR"
456         .word    __RODATA_LOAD__
457         .word    __BSS_LOAD__ - 1
458 </verb></tscreen>
459 <p>
460 Compile with
461 <tscreen><verb>
462 cl65 -t atari -C split.cfg -o prog.com prog.c split.s
463 </verb></tscreen>
464
465 <sect2>Low data and high code example<p>
466
467
468 Goal: Put RODATA and DATA into low memory and STARTUP, LOWCODE, INIT,
469 CODE, BSS, ZPSAVE into high memory (split2.cfg):
470
471 <tscreen><verb>
472 SYMBOLS {
473     __STACKSIZE__ = $800;       # 2K stack
474     __RESERVED_MEMORY__: value = $0000, weak = yes;
475 }
476 FEATURES {
477     STARTADDRESS: default = $2E00;
478 }
479 MEMORY {
480     ZP: start = $82, size = $7E, type = rw, define = yes;
481
482     HEADER: start = $0000, size = $6, file = %O;        # first load chunk
483     RAMLO: start = %S, size = $4000 - %S, file = %O;
484
485     BANK: start = $4000, size = $4000, file = "";
486
487     SECHDR: start = $0000, size = $4, file = %O;        # second load chunk
488     RAM: start = $8000, size = $3C20, file = %O;        # $3C20: matches upper bound $BC1F
489 }
490 SEGMENTS {
491     EXEHDR: load = BANK, type = ro;                     # discarded old EXE header
492
493     NEXEHDR: load = HEADER, type = ro;                  # first load chunk
494     RODATA: load = RAMLO, type = ro, define = yes;
495     DATA: load = RAMLO, type = rw, define = yes;
496
497     CHKHDR: load = SECHDR, type = ro;                   # second load chunk
498     STARTUP: load = RAM, type = ro, define = yes;
499     INIT: load = RAM, type = ro, optional = yes;
500     CODE: load = RAM, type = ro, define = yes;
501     ZPSAVE: load = RAM, type = bss, define = yes;
502     BSS: load = RAM, type = bss, define = yes;
503
504     ZEROPAGE: load = ZP, type = zp;
505     AUTOSTRT: load = RAM, type = ro;                    # defines program entry point
506 }
507 FEATURES {
508     CONDES: segment = RODATA,
509             type = constructor,
510             label = __CONSTRUCTOR_TABLE__,
511             count = __CONSTRUCTOR_COUNT__;
512     CONDES: segment = RODATA,
513             type = destructor,
514             label = __DESTRUCTOR_TABLE__,
515             count = __DESTRUCTOR_COUNT__;
516 }
517 </verb></tscreen>
518
519 New contents for NEXEHDR and CHKHDR are needed (split2.s):
520 <tscreen><verb>
521         .import __STARTUP_LOAD__, __ZPSAVE_LOAD__, __DATA_SIZE__
522         .import __DATA_LOAD__, __RODATA_LOAD__
523
524         .segment "NEXEHDR"
525         .word    $FFFF
526         .word    __RODATA_LOAD__
527         .word    __DATA_LOAD__ + __DATA_SIZE__ - 1
528
529         .segment "CHKHDR"
530         .word    __STARTUP_LOAD__
531         .word    __ZPSAVE_LOAD__ - 1
532 </verb></tscreen>
533
534 Compile with
535 <tscreen><verb>
536 cl65 -t atari -C split2.cfg -o prog.com prog.c split2.s
537 </verb></tscreen>
538
539 <sect2>Final note<label id="memhole_final_note"><p>
540
541 There are two other memory areas which don't appear directly in the
542 linker script. They are the stack and the heap.
543
544 The cc65 runtime lib places the stack location at the end of available
545 memory. This is dynamically set from the MEMTOP system variable at
546 startup. The heap is located in the area between the end of the BSS
547 segment and the top of the stack as defined by __STACKSIZE__.
548
549 If BSS and/or the stack shouldn't stay at the end of the program,
550 some parts of the cc65 runtime lib need to be replaced/modified.
551
552 common/_heap.s defines the location of the heap and atari/crt0.s
553 defines the location of the stack by initializing sp.
554
555
556 <sect>Bugs/Feedback<p>
557
558 If you have problems using the library, if you find any bugs, or if you're
559 doing something interesting with it, I would be glad to hear from you. Feel
560 free to contact me by email (<htmlurl url="mailto:uz@cc65.org"
561 name="uz@cc65.org"> or <htmlurl url="mailto:chris@groessler.org"
562 name="chris@groessler.org"> ).
563
564
565
566 <sect>License<p>
567
568 This software is provided 'as-is', without any expressed or implied
569 warranty.  In no event will the authors be held liable for any damages
570 arising from the use of this software.
571
572 Permission is granted to anyone to use this software for any purpose,
573 including commercial applications, and to alter it and redistribute it
574 freely, subject to the following restrictions:
575
576 <enum>
577 <item>  The origin of this software must not be misrepresented; you must not
578         claim that you wrote the original software. If you use this software
579         in a product, an acknowledgment in the product documentation would be
580         appreciated but is not required.
581 <item>  Altered source versions must be plainly marked as such, and must not
582         be misrepresented as being the original software.
583 <item>  This notice may not be removed or altered from any source
584         distribution.
585 </enum>
586
587 </article>