1 <!doctype linuxdoc system>
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">
13 An overview over the Atari runtime system as it is implemented for the cc65 C
17 <!-- Table of contents -->
20 <!-- Begin the document -->
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
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
36 <sect>Binary format<p>
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 <2 byte start address> <2 bytes end address>
41 [program bytes]). 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 ($02E0 <run vector>) and is calculated using
49 __STARTUP_LOAD__ in crt0.s.
52 <sect>Memory layout<p>
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 $2E00 - $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 ($2E5) variable. Then the initial stack pointer,
60 which indicates the upper bound of memory used, is adjusted. The
61 default load address of $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.
71 The text screen depends on the installed memory size and cartridges
72 and can be obtained from the SAVMSC variable ($58).
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">).
84 The C heap is located at the end of the program and grows towards the C
91 <sect>Platform specific header files<p>
93 Programs containing Atari specific code may use the <tt/atari.h/
97 <sect1>Atari specific functions<p>
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.
118 <sect1>Hardware access<p>
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
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.
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.
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.
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.
153 <sect>Loadable drivers<p>
155 <sect1>Graphics drivers<p>
157 Currently there are no graphics drivers available for the Atari platform.
158 However, the runtime library provides a function named _graphics, with
159 a mode parameter just like the BASIC GRAPHICS command. This function will
160 switch to the requested graphics mode.
161 There are currently no functions available to access the graphics
162 memory. The access must be implemented manually.
164 Many graphics modes require more memory than the text screen which is
165 in effect when the program starts up. Therefore the programmer has to
166 tell the program beforehand the memory requirements of the graphics
167 modes the program intends to use.
168 This can be done by using the __RESERVED_MEMORY__ linker config
169 variable. The number specified there describes the number of bytes to
170 subtract from the top of available memory as seen from the runtime
171 library. This memory is then used by the screen buffer.
173 The numbers for the different graphics modes presented below should
174 only be seen as a rule of thumb. Since the screen buffer memory needs
175 to start at specific boundaries, the numbers depend on the current top
177 The following numbers were determined by a BASIC program.
181 graphics mode|reserved memory@<hline>
215 <caption>reserved memory required for different graphics modes
218 The values of "1" are needed because the graphics command crashes if
219 it doesn't have at least one byte available. This seems to be a bug of
222 <sect1>Extended memory drivers<p>
224 Currently there are no extended memory drivers available for the Atari
227 <sect1>Joystick drivers<p>
231 <tag><tt/ataristd.joy/</tag>
232 Supports up to four standard joysticks connected to the joystick ports of
235 <tag><tt/atarim8.joy/</tag>
236 Supports up to eight standard joysticks connected to a MultiJoy adapter.
242 <sect1>Mouse drivers<p>
244 Currently no drivers available (in fact, the API for loadable mouse drivers
245 does not exist). There is a static driver you can use.
248 <sect1>RS232 device drivers<p>
250 Currently there are no RS232 loadable drivers available for the Atari
251 platform. There is a static driver you can use.
257 <sect>DIO implementation<label id="dio"><p>
259 The Atari supports disk drives with either 128 or 256 byte sectors.
260 The first three sectors of any disk are always 128 bytes long though. This is
261 because the system can only boot from 128 bytes sectors.
263 Therefore the DIO read and write functions transfer only 128 bytes
264 for sectors 1 to 3, regardless of the type of diskette.
267 <sect>CONIO implementation<label id="conio"><p>
269 The console I/O is speed optimized therefore support for XEP80 hardware
270 or f80.com software is missing. Of course you may use stdio.h functions.
276 <sect1>Function keys<p>
278 Function keys are mapped to Atari + number key.
281 <sect1>Passing arguments to the program<p>
283 Command line arguments can be passed to <tt/main()/ when DOS supports it.
286 <item>Arguments are separated by spaces.
287 <item>Leading and trailing spaces around an argument are ignored.
288 <item>The first argument passed to <tt/main/ is the program name.
289 <item>A maximum number of 16 arguments (including the program name) are
296 The runtime for the Atari uses routines marked as <tt/.INTERRUPTOR/ for
297 interrupt handlers. Such routines must be written as simple machine language
298 subroutines and will be called automatically by the VBI handler code
299 when they are linked into a program. See the discussion of the <tt/.CONDES/
300 feature in the <htmlurl url="ca65.html" name="assembler manual">.
303 <sect1>Reserving a memory area inside a program<label id="memhole"><p>
305 The Atari 130XE maps its additional memory into CPU memory in 16K
306 chunks at address $4000 to $7FFF. One might want to
307 prevent this memory area from being used by cc65. Other reasons to
308 prevent the use of some memory area could be to reserve space for the
309 buffers for display lists and screen memory.
311 The Atari executable format allows holes inside a program, e.g. one
312 part loads into $2E00 to $3FFF, going below the reserved
313 memory area (assuming a reserved area from $4000 to
314 $7FFF), and another part loads into $8000 to
317 Each load chunk of the executable starts with a 4 byte header which
318 defines its load address and size. In the following linker scripts
319 these headers are named HEADER and SECHDR (for the MEMORY layout), and
320 accordingly NEXEHDR and CHKHDR (for the SEGMENTS layout).
322 <sect2>Low code and high data example<p>
323 Goal: Create an executable with 2 load chunks which doesn't use the
324 memory area from $4000 to $7FFF. The CODE segment of
325 the program should go below $4000 and the DATA and RODATA
326 segments should go above $7FFF.
328 The main problem is that the EXE header generated by the cc65 runtime
329 lib is wrong. It defines a single load chunk with the sizes/addresses
330 of the STARTUP, LOWCODE, INIT, CODE, RODATA, and DATA segments (the whole user
333 The contents of the EXE header come from the EXEHDR segment, which is
334 defined in crt0.s. This cannot be changed without modifying and
335 recompiling the cc65 atari runtime lib. Therefore the original EXE
336 header must be discarded. It will be replaced by a user created
337 one. The discarding is done by assigning the EXEHDR segment to the
338 BANK memory area. The BANK memory area is discarded in the new linker
339 script (written to file "").
341 The user needs to create a customized linker config file which adds
342 new memory areas and segments to hold the new EXE header and the
343 header data for the second load chunk. Also an assembly source file
344 needs to be created which defines the contents of the new EXE header
345 and the second load chunk header.
348 This is an example of a modified cc65 Atari linker configuration file
352 __STACKSIZE__ = $800; # 2K stack
353 __RESERVED_MEMORY__: value = $0000, weak = yes;
356 STARTADDRESS: default = $2E00;
359 ZP: start = $82, size = $7E, type = rw, define = yes;
361 HEADER: start = $0000, size = $6, file = %O; # first load chunk
362 RAMLO: start = %S, size = $4000 - %S, file = %O;
364 BANK: start = $4000, size = $4000, file = "";
366 SECHDR: start = $0000, size = $4, file = %O; # second load chunk
367 RAM: start = $8000, size = $3C20, file = %O; # $3C20: matches upper bound $BC1F
370 EXEHDR: load = BANK, type = ro;
372 NEXEHDR: load = HEADER, type = ro; # first load chunk
373 STARTUP: load = RAMLO, type = ro, define = yes;
374 LOWCODE: load = RAMLO, type = ro, define = yes, optional = yes;
375 INIT: load = RAMLO, type = ro, optional = yes;
376 CODE: load = RAMLO, type = ro, define = yes;
378 CHKHDR: load = SECHDR, type = ro; # second load chunk
379 RODATA: load = RAM, type = ro, define = yes;
380 DATA: load = RAM, type = rw, define = yes;
381 BSS: load = RAM, type = bss, define = yes;
382 ZPSAVE: load = RAM, type = bss, define = yes;
384 ZEROPAGE: load = ZP, type = zp;
385 AUTOSTRT: load = RAM, type = ro; # defines program entry point
388 CONDES: segment = RODATA,
390 label = __CONSTRUCTOR_TABLE__,
391 count = __CONSTRUCTOR_COUNT__;
392 CONDES: segment = RODATA,
394 label = __DESTRUCTOR_TABLE__,
395 count = __DESTRUCTOR_COUNT__;
400 A new memory area BANK was added which describes the reserved area.
401 It gets loaded with the contents of the old EXEHDR segment. But the
402 memory area isn't written to the output file. This way the contents of
403 the EXEHDR segment get discarded.
405 The newly added NEXEHDR segment defines the correct EXE header. It
406 puts the STARTUP, LOWCODE, INIT, and CODE segments, which are the
407 segments containing only code, into load chunk #1 (RAMLO memory area).
409 The header for the second load chunk comes from the new CHKHDR
410 segment. It puts the RODATA, DATA, BSS, and ZPSAVE segments into load
411 chunk #2 (RAM memory area).
414 The contents of the new NEXEHDR and CHKHDR segments come from this
417 .import __CODE_LOAD__, __BSS_LOAD__, __CODE_SIZE__
418 .import __DATA_LOAD__, __RODATA_LOAD__, __STARTUP_LOAD__
422 .word __STARTUP_LOAD__
423 .word __CODE_LOAD__ + __CODE_SIZE__ - 1
426 .word __RODATA_LOAD__
427 .word __BSS_LOAD__ - 1
432 cl65 -t atari -C split.cfg -o prog.com prog.c split.s
435 <sect2>Low data and high code example<p>
438 Goal: Put RODATA and DATA into low memory and STARTUP, LOWCODE, INIT,
439 CODE, BSS, ZPSAVE into high memory (split2.cfg):
443 __STACKSIZE__ = $800; # 2K stack
444 __RESERVED_MEMORY__: value = $0000, weak = yes;
447 STARTADDRESS: default = $2E00;
450 ZP: start = $82, size = $7E, type = rw, define = yes;
452 HEADER: start = $0000, size = $6, file = %O; # first load chunk
453 RAMLO: start = %S, size = $4000 - %S, file = %O;
455 BANK: start = $4000, size = $4000, file = "";
457 SECHDR: start = $0000, size = $4, file = %O; # second load chunk
458 RAM: start = $8000, size = $3C20, file = %O; # $3C20: matches upper bound $BC1F
461 EXEHDR: load = BANK, type = ro; # discarded old EXE header
463 NEXEHDR: load = HEADER, type = ro; # first load chunk
464 RODATA: load = RAMLO, type = ro, define = yes;
465 DATA: load = RAMLO, type = rw, define = yes;
467 CHKHDR: load = SECHDR, type = ro; # second load chunk
468 STARTUP: load = RAM, type = ro, define = yes;
469 INIT: load = RAM, type = ro, optional = yes;
470 CODE: load = RAM, type = ro, define = yes;
471 ZPSAVE: load = RAM, type = bss, define = yes;
472 BSS: load = RAM, type = bss, define = yes;
474 ZEROPAGE: load = ZP, type = zp;
475 AUTOSTRT: load = RAM, type = ro; # defines program entry point
478 CONDES: segment = RODATA,
480 label = __CONSTRUCTOR_TABLE__,
481 count = __CONSTRUCTOR_COUNT__;
482 CONDES: segment = RODATA,
484 label = __DESTRUCTOR_TABLE__,
485 count = __DESTRUCTOR_COUNT__;
489 New contents for NEXEHDR and CHKHDR are needed (split2.s):
491 .import __STARTUP_LOAD__, __ZPSAVE_LOAD__, __DATA_SIZE__
492 .import __DATA_LOAD__, __RODATA_LOAD__
496 .word __RODATA_LOAD__
497 .word __DATA_LOAD__ + __DATA_SIZE__ - 1
500 .word __STARTUP_LOAD__
501 .word __ZPSAVE_LOAD__ - 1
506 cl65 -t atari -C split2.cfg -o prog.com prog.c split2.s
509 <sect2>Final note<label id="memhole_final_note"><p>
511 There are two other memory areas which don't appear directly in the
512 linker script. They are the stack and the heap.
514 The cc65 runtime lib places the stack location at the end of available
515 memory. This is dynamically set from the MEMTOP system variable at
516 startup. The heap is located in the area between the end of the BSS
517 segment and the top of the stack as defined by __STACKSIZE__.
519 If BSS and/or the stack shouldn't stay at the end of the program,
520 some parts of the cc65 runtime lib need to be replaced/modified.
522 common/_heap.s defines the location of the heap and atari/crt0.s
523 defines the location of the stack by initializing sp.
526 <sect>Bugs/Feedback<p>
528 If you have problems using the library, if you find any bugs, or if you're
529 doing something interesting with it, I would be glad to hear from you. Feel
530 free to contact me by email (<htmlurl url="mailto:uz@cc65.org"
531 name="uz@cc65.org"> or <htmlurl url="mailto:chris@groessler.org"
532 name="chris@groessler.org"> ).
538 This software is provided 'as-is', without any expressed or implied
539 warranty. In no event will the authors be held liable for any damages
540 arising from the use of this software.
542 Permission is granted to anyone to use this software for any purpose,
543 including commercial applications, and to alter it and redistribute it
544 freely, subject to the following restrictions:
547 <item> The origin of this software must not be misrepresented; you must not
548 claim that you wrote the original software. If you use this software
549 in a product, an acknowledgment in the product documentation would be
550 appreciated but is not required.
551 <item> Altered source versions must be plainly marked as such, and must not
552 be misrepresented as being the original software.
553 <item> This notice may not be removed or altered from any source