1 <!doctype linuxdoc system>
5 <title>Atari specific information for cc65
7 <url url="mailto:shawnjefferson@24fightingchickens.com" name="Shawn Jefferson"> and<newline>
8 <url url="mailto:chris@groessler.org" name="Christian Groessler">
12 An overview over the Atari runtime system as it is implemented for the cc65 C
16 <!-- Table of contents -->
19 <!-- Begin the document -->
23 This file contains an overview of the Atari runtime system as it comes
24 with the cc65 C compiler. It describes the memory layout, Atari specific
25 header files, available drivers, and any pitfalls specific to that
28 The Atari runtime support comes in two flavors: <tt/atari/ and <tt/atarixl/.
29 The <tt/atari/ target supports all Atari 8-bit computers, the <tt/atarixl/ only
30 supports XL type or newer machines (excluding the 600XL).
32 The <tt/atarixl/ runtime makes the whole 64K of memory available, with the
33 exception of the I/O area at $D000 - $D7FFF. Since the
34 <tt/atarixl/ runtime has some <ref name="limitations" id="limitations">, it is
35 recommended to use the <tt/atari/ target unless lack of memory dictates the
36 use of the <tt/atarixl/ target.
38 Please note that Atari specific functions are just mentioned here, they are
39 described in detail in the separate <url url="funcref.html" name="function
40 reference">. Even functions marked as "platform dependent" may be available on
41 more than one platform. Please see the function reference for more
45 <sect>Binary format<p>
47 The Atari DOS executable file format supports more than one load block (<it/chunk/).
49 The default binary output format generated by the linker for the
50 Atari target is a machine language program with a standard executable
51 header (FF FF <load chunk #1> ... <load chunk #n>).
52 A load chunk has the format [<2 byte start address> <2 bytes end address>
53 <chunk data>].
54 A run vector is added to the end of the
55 file ($02E0 $02E1 <run vector>) and is calculated using
56 the <tt/start/ label in crt0.s. (Technically the run vector is also a load chunk,
57 but is not regarded as such here.)
59 An <tt/atari/ program has two load chunks, an <tt/atarixl/ program has three load
60 chunks. The load chunks are defined in the linker configuration files. For more
61 detailed information about the load chunks see the chapter
62 <ref name="Technical details" id="techdetail">. For the discussion here it's
63 sufficient to know that the first load chunk(s) do preparation work and the
64 main part of the program is in the last load chunk.
66 The values determining the size of the main part of the program (the only load
67 chunk for <tt/atari/, the third load chunk for <tt/atarixl/) are calculated in
68 the crt0.s file from the __STARTUP_LOAD__ and __BSS_LOAD__ values.
69 Be aware of that if you create a custom linker config file and start moving segments around (see section
70 <ref name="Reserving a memory area inside the program" id="memhole">).
73 <sect>Memory layout<p>
75 <sect1><tt/atari/ target<p>
77 The default linker config file assumes that the BASIC ROM is disabled (or
78 the BASIC cartridge unplugged). This gives a usable memory range of
79 [$2000-$BC1F]. The library startup code examines the
80 current memory configuration, which depends on the size of the
81 installed memory and cartridges. It does so by using the value in
82 the MEMTOP ($2E5) variable as highest memory address the program
83 can use. The initial stack pointer, which is the upper bound of
84 memory used by the program, is set to this value, minus an optionally
85 defined __RESERVED_MEMORY__ value.
87 The default load address of $2000 can be changed by creating a custom
88 linker config file or by using the "--start-addr" cl65 command line
89 argument or the "--start-addr" or "-S" ld65 command line arguments.
91 Please note that the first load chunk (which checks the available memory)
92 will always be loaded at $2E00, regardless of the specified start
93 address. This address can only be changed by a custom linker config file.
99 The text screen depends on the installed memory size and cartridges
100 and can be obtained from the SAVMSC variable ($58).
103 The C runtime stack is located at MEMTOP and grows downwards,
104 regardless of how your linker config file is setup. This
105 accommodates the different memory configurations of the Atari
106 machines, as well as having a cartridge installed. You can override
107 this behaviour by writing your own crt0.s file and linking it to
108 your program (see also <ref name="Final note"
109 id="memhole_final_note">).
112 The C heap is located at the end of the program and grows towards the C
117 <sect1><tt/atarixl/ target<p>
119 The startup code rearranges the memory as follows:
122 <item>Sceen memory and display list are moved below the program start address.
123 <item>The ROM is disabled, making the memory in the areas [$C000-$CFFF]
124 and [$D800-$FFF9] available.
125 <item>Character generator data is copied from ROM to the CHARGEN location specified in the
126 linker config file. This is (in the default <tt/atarixl.cfg/ file) at the same address as
127 where it is in ROM ($E000, it can be changed, see <ref name="atarixl chargen location"
128 id="chargenloc">). With the character generator at $E000, there are two upper memory
129 areas available, [$D800-$DFFF] and [$E400-$FFF9].
132 With the default load address of $2400 this gives a usable memory range of
133 [$2400-$CFFF]. Note that the default load address for <tt/atarixl/ is
134 different (and lower) that the default load address for <tt/atari/. This is no problem since
135 on the <tt/atarixl/ target the first load chunk makes sure that the loaded prgram won't overwrite
136 memory below MEMLO. See <ref name="atarixl load chunks" id="xlchunks">.
143 The text screen depends on the selected load address ($2400
144 by default), and resides directly before that address, rounded to the next
146 The screen memory's start address can be obtained from the SAVMSC variable
150 The C runtime stack is located at end of the RAM memory area ($CFFF)
154 The C heap is located at the end of the program (end of BSS segment) and
155 grows towards the C runtime stack.
159 <sect>Linker configurations<p>
161 The ld65 linker comes with default config files for the Atari. There
162 are two targets for the Atari, <tt/atari/ and <tt/atarixl/.
163 The default config file for <tt/atari/ is selected with
164 <tt/-t atari/, and the default config file for <tt/atarixl/ is selected with
166 The Atari package comes with additional secondary linker config files which
167 can be used via <tt/-t atari -C <configfile>/ (for <tt/atari/ target) or
168 <tt/-t atarixl -C <configfile>/ (for <tt/atarixl/ target).
170 <sect1><tt/atari/ config files<p>
172 <sect2>default config file (<tt/atari.cfg/)<p>
174 The default configuration is tailored to C programs. It creates files
175 which have a default load address of $2000.
177 The files generated by this config file include the
178 <ref name="&dquot;system check&dquot;" id="syschk"> load chunk. It can
179 optionally be left out, see <ref name="Getting rid of the &dquot;system check&dquot; load chunk" id="nosyschk">.
181 <sect2><tt/atari-asm.cfg/<p>
183 This config file aims to give the assembler programmer maximum
184 flexibility. All program segments (<tt/CODE/, <tt/DATA/, etc.) are
187 By default it creates regular DOS executable files, which have a default
188 load address of $2E00. It's also possible to generate an image of
189 just the program data without EXE header, load address, or (auto-)start address.
190 To you so, you have to define the symbols <tt/__AUTOSTART__/ and <tt/__EXEHDR__/
191 when linking the program. Therefore, to generate a "plain" binary file, pass the
192 options "<tt/-D__AUTOSTART__=1 -D__EXEHDR__=1/" to the linker.
193 It's also possible to create a non auto-starting program file, by defining
194 only the <tt/__AUTOSTART__/ symbol. Such a program has to be run manually
195 after being loaded by DOS (for example by using the "M" option of DOS 2.5).
196 Defining only the <tt/__EXEHDR__/ symbol will create a (useless) file which
197 doesn't conform to the DOS executable file format (like a "plain" binary file)
198 but still has the "autostart" load chunk appended.
200 The sections of the file which the defines refer to (<tt/__AUTOSTART__/ for
201 the autostart trailer, <tt/__EXEHDR__/ for the EXE header and load address)
202 is <it/left out/, keep this in mind.
204 The values you assign to the two symbols <tt/__AUTOSTART__/ and <tt/__EXEHDR__/
207 <sect2><tt/atari-cart.cfg/<p>
209 This config file can be used to create 8K or 16K cartridges. It's suited both
210 for C and assembly language programs.
212 By default, an 8K cartridge is generated. To create a 16K cartridge, pass the
213 size of the cartridge to the linker, like "<tt/-D__CARTSIZE__=0x4000/".
214 The only valid values for <tt/__CARTSIZE__/ are 0x2000 and 0x4000.
216 The option byte of the cartridge can be set with the <tt/__CARTFLAGS__/
217 value, passed to the linker. The default value is $01, which means
218 that the cartridge doesn't prevent the booting of DOS.
220 The option byte will be located at address $BFFD. For more information
221 about its use, see e.g. "Mapping the Atari".
223 <sect2><tt/atari-cassette.cfg/<p>
225 This config file can be used to create cassette boot files. It's suited both
226 for C and assembly language programs.
228 The size of a cassette boot file is restricted to 32K. Larger programs
229 would need to be split in more parts and the parts to be loaded manually.
231 To write the generated file to a cassette, a utility to run
232 on an Atari is provided in the <tt/targetutil/ directory (<tt/w2cas.com/).
234 <sect1><tt/atarixl/ config files<p>
236 <sect2>default config file (<tt/atarixl.cfg/)<p>
238 The default configuration is tailored to C programs. It creates files
239 which have a default load address of $2400.
241 The files generated by this config file include the
242 <ref name="&dquot;system check&dquot;" id="syschkxl"> load chunk. It can
243 optionally be left out, see <ref name="Getting rid of the &dquot;system check&dquot; load chunk" id="nosyschk">.
245 <sect2><tt/atarixl-largehimem.cfg/<p>
247 This is the same as the default config file, but it rearranges the
248 high memory beneath the ROM into one large block. In order for this
249 config file to work, the runtime library has to be recompiled with a
250 special define. See the file <tt/libsrc/atari/Makefile.inc/ in the
253 The files generated by this config file include the
254 <ref name="&dquot;system check&dquot;" id="syschkxl"> load chunk. It can
255 optionally be left out, see <ref name="Getting rid of the &dquot;system check&dquot; load chunk" id="nosyschk">.
258 <sect>Platform specific header files<p>
260 Programs containing Atari specific code may use the <tt/atari.h/
264 <sect1>Atari specific functions<p>
266 The functions and global variable listed below are special for the Atari.
267 See the <url url="funcref.html" name="function reference"> for declaration and usage.
285 <sect1>Hardware access<p>
287 The following pseudo variables declared in the <tt/atari.h/ header
288 file do allow access to hardware located in the address space. Some
289 variables are structures, accessing the struct fields will access the
294 <tag><tt/GTIA_READ/ and <tt/GTIA_WRITE/</tag>
295 The <tt/GTIA_READ/ structure allows read access to the GTIA. The
296 <tt/GTIA_WRITE/ structure allows write access to the GTIA.
297 See the <tt/_gtia.h/ header file located in the include directory
298 for the declaration of the structure.
300 <tag><tt/POKEY_READ/ and <tt/POKEY_WRITE/</tag>
301 The <tt/POKEY_READ/ structure allows read access to the POKEY. The
302 <tt/POKEY_WRITE/ structure allows write access to the POKEY.
303 See the <tt/_pokey.h/ header file located in the include directory
304 for the declaration of the structure.
306 <tag><tt/ANTIC/</tag>
307 The <tt/ANTIC/ structure allows read access to the ANTIC.
308 See the <tt/_antic.h/ header file located in the include directory
309 for the declaration of the structure.
312 The <tt/PIA/ structure allows read access to the PIA 6520.
313 See the <tt/_pia.h/ header file located in the include directory
314 for the declaration of the structure.
320 <sect>Loadable drivers<p>
322 The names in the parentheses denote the symbols to be used for static linking of the drivers.
325 <sect1>Graphics drivers<p>
327 <table><tabular ca="rrrr">
328 <tt/atari/|<tt/atarixl/|screen resolution|display pages@<hline>
329 <tt/atr3.tgi (atr3_tgi)/|<tt/atrx3.tgi (atrx3_tgi)/|40x24x4 (CIO mode 3, ANTIC mode 8)|1@
330 <tt/atr4.tgi (atr4_tgi)/|<tt/atrx4.tgi (atrx4_tgi)/|80x48x2 (CIO mode 4, ANTIC mode 9)|1@
331 <tt/atr5.tgi (atr5_tgi)/|<tt/atrx5.tgi (atrx5_tgi)/|80x48x4 (CIO mode 5, ANTIC mode A)|1@
332 <tt/atr6.tgi (atr6_tgi)/|<tt/atrx6.tgi (atrx6_tgi)/|160x96x2 (CIO mode 6, ANTIC mode B)|1@
333 <tt/atr7.tgi (atr7_tgi)/|<tt/atrx7.tgi (atrx7_tgi)/|160x96x4 (CIO mode 7, ANTIC mode D)|1@
334 <tt/atr8.tgi (atr8_tgi)/|<tt/atrx8.tgi (atrx8_tgi)/|320x192x2 (CIO mode 8, ANTIC mode F)|1@
335 <tt/atr8p2.tgi (atr8p2_tgi)/|<tt/atrx8p2.tgi (atrx8p2_tgi)/|320x192x2 (CIO mode 8, ANTIC mode F)|2@
336 <tt/atr9.tgi (atr9_tgi)/|<tt/atrx9.tgi (atrx9_tgi)/|80x192x16b (CIO mode 9, ANTIC mode F, GTIA mode $40)|1@
337 <tt/atr9p2.tgi (atr9p2_tgi)/|<tt/atrx9p2.tgi (atrx9p2_tgi)/|80x192x16b (CIO mode 9, ANTIC mode F, GTIA mode $40)|2@
338 <tt/atr10.tgi (atr10_tgi)/|<tt/atrx10.tgi (atrx10_tgi)/|80x192x9 (CIO mode 10, ANTIC mode F, GTIA mode $80)|1@
339 <tt/atr10p2.tgi (atr10p2_tgi)/|<tt/atrx10p2.tgi (atrx10p2_tgi)/|80x192x9 (CIO mode 10, ANTIC mode F, GTIA mode $80)|2@
340 <tt/atr11.tgi (atr11_tgi)/|<tt/atrx11.tgi (atrx11_tgi)/|80x192x16h (CIO mode 11, ANTIC mode F, GTIA mode $C0)|1@
341 <tt/atr14.tgi (atr14_tgi)/|<tt/atrx14.tgi (atrx14_tgi)/|160x192x2 (CIO mode 14, ANTIC mode C)|1@
342 <tt/atr15.tgi (atr15_tgi)/|<tt/atrx15.tgi (atrx15_tgi)/|160x192x4 (CIO mode 15, ANTIC mode E)|1@
343 <tt/atr15p2.tgi (atr15p2_tgi)/|<tt/atrx15p2.tgi (atrx15p2_tgi)/|160x192x4 (CIO mode 15, ANTIC mode E)|2
345 <!-- <caption>bla bla -->
349 Many graphics modes require more memory than the text screen which is
350 in effect when the program starts up. Therefore the programmer has to
351 tell the program beforehand the memory requirements of the graphics
352 modes the program intends to use.
354 On the <tt/atari/ target his can be done by using the __RESERVED_MEMORY__
355 linker config variable. The number specified there describes the number
356 of bytes to subtract from the top of available memory as seen from the
357 runtime library. This memory is then used by the screen buffer.
359 On the <tt/atarixl/ target the screen memory resides below the program
360 load address. In order to reserve memory for a graphics mode, one
361 simply uses a higher program load address. There are restrictions on
362 selectable load addresses,
363 see <ref name="Selecting a good program load address" id="loadaddr">.
365 The numbers for the different graphics modes presented below should
366 only be seen as a rule of thumb. Since the screen buffer memory needs
367 to start at specific boundaries, the numbers depend on the current top
369 The following numbers were determined by a BASIC program.
373 graphics mode|reserved memory@<hline>
407 <caption>reserved memory required for different graphics modes
410 The values of "1" are needed because the graphics command crashes if
411 it doesn't have at least one byte available. This seems to be a bug of
414 Default drivers: <tt/atr8.tgi (atr8_tgi)/ and <tt/atrx8.tgi (atrx8_tgi)/.
416 <sect1>Extended memory drivers<p>
418 Currently there is only one extended memory driver. It manages the second 64K of a 130XE.
422 <tt/atari/|<tt/atarixl/@<hline>
423 <tt/atr130.emd (atr130_emd)/|<tt/atrx130.emd (atrx130_emd)/
427 <sect1>Joystick drivers<p>
429 Currently there are two joystick drivers available:
433 <tt/atari/|<tt/atarixl/|description@<hline>
434 <tt/atrstd.joy (atrstd_joy)/|<tt/atrxstd.joy (atrxstd_joy)/|Supports up to two/four standard joysticks connected to the joystick ports of the Atari. (Four on the pre-XL systems, two on XL or newer.)@
435 <tt/atrmj8.joy (atrmj8_joy)/|<tt/atrxmj8.joy (atrxmj8_joy)/|Supports up to eight standard joysticks connected to a MultiJoy adapter.
440 Default drivers: <tt/atrstd.joy (atrstd_joy)/ and <tt/atrxstd.joy (atrxstd_joy)/.
442 <sect1>Mouse drivers<p>
444 Currently there are five mouse drivers available:
448 <tt/atari/|<tt/atarixl/|description@<hline>
449 <tt/atrjoy.mou (atrjoy_mou)/|<tt/atrxjoy.mou (atrxjoy_mou)/|Supports a mouse emulated by a standard joystick.@
450 <tt/atrst.mou (atrst_mou)/|<tt/atrxst.mou (atrxst_mou)/|Supports an Atari ST mouse.@
451 <tt/atrami.mou (atrami_mou)/|<tt/atrxami.mou (atrxami_mou)/|Supports an Amiga mouse.@
452 <tt/atrtrk.mou (atrtrk_mou)/|<tt/atrxtrk.mou (atrxtrk_mou)/|Supports an Atari trakball.@
453 <tt/atrtt.mou (atrtt_mou)/|<tt/atrxtt.mou (atrxtt_mou)/|Supports an Atari touch tablet.
458 All mouse devices connect to joystick port #0.
460 Default drivers: <tt/atrst.mou (atrst_mou)/ and <tt/atrxst.mou (atrxst_mou)/.
462 <sect1>RS232 device drivers<p>
464 Currently there is one RS232 driver. It uses the R: device (therefore
465 an R: driver needs to be installed) and was tested with the 850
470 <tt/atari/|<tt/atarixl/@<hline>
471 <tt/atrrdev.ser (atrrdev_ser)/|<tt/atrxrdev.ser (atrxrdev_ser)/
478 <sect1><tt/atarixl/<#if output="info|latex2e"> limitations</#if><label id="limitations"<p>
481 <item>The display is cleared at program start and at program termination. This is a side
482 effect of relocating the display memory below the program start address.
483 <item>Not all possible CIO and SIO functions are handled by the runtime stub code which banks
484 the ROM in and out. All functions used by the runtime library are handled, though.
485 <item>The <tt/_sys()/ function is not supported.
486 <item>It is not compatible with DOSes or other programs using the memory below the ROM.
489 <sect>DIO implementation<label id="dio"><p>
491 The Atari supports disk drives with either 128 or 256 byte sectors.
492 The first three sectors of any disk are always 128 bytes long though. This is
493 because the system can only boot from 128 bytes sectors.
495 Therefore the DIO read and write functions transfer only 128 bytes
496 for sectors 1 to 3, regardless of the type of diskette.
499 <sect>CONIO implementation<label id="conio"><p>
501 The console I/O is speed optimized therefore support for XEP80 hardware
502 or f80.com software is missing. Of course you may use stdio.h functions.
505 <sect>Technical details<label id="techdetail"><p>
507 <sect1><tt/atari/<#if output="info|latex2e"> details</#if><p>
509 <sect2><#if output="info|latex2e"><tt/atari/ </#if>Load chunks<p>
511 An <tt/atari/ program contains two load chunks.
514 <item>"system check"<label id="syschk">&nl;
515 This load chunk is always loaded at address $2E00, and checks if the system has
516 enough memory to run the program. It also checks if the program start address is not
517 below MEMLO. If any of the checks return false, the loading of the program is aborted.&nl;
518 The contents of this chunk come from the SYSCHKCHNK memory area of the linker config file.
519 <item>main program&nl;
520 This load chunk is loaded at the selected program start address (default $2000) and
521 contains all of the code and data of the program.&nl;
522 The contents of this chunk come from the RAM memory area of the linker config file.
526 <sect1><tt/atarixl/<#if output="info|latex2e"> details</#if><p>
528 <sect2>General operation<p>
530 The <tt/atarixl/ target banks out the ROM while the program is running in
531 order to make more memory available to the program.
533 The screen memory is by default located at the top of available memory,
534 $BFFF if BASIC is not enabled, $9FFF if BASIC is enabled.
535 Therefore, in order to create a largest possible continuous memory area,
536 the screen memory is moved below the program load address. This gives
537 a memory area from <program load addr> to $CFFF.
539 The startup code installs wrappers for interrupt handlers and ROM routines.
540 When an interrupt or call to a ROM routine happens, the wrappers enable the
541 ROM, call the handler or routine, and disable the ROM again.
543 The "wrapping" of the ROM routines is done by changing the ROM entry
544 point symbols in <tt/atari.inc/ to point to the wrapper functions.
546 For ROM functions which require input or output buffers, the wrappers
547 copy the data as required to buffers in low memory.
549 <sect2><#if output="info|latex2e"><tt/atarixl/ </#if>Load chunks<label id="xlchunks"><p>
551 An <tt/atarixl/ program contains three load chunks.
554 <item>"system check"<label id="syschkxl">&nl;
555 This load chunk is always loaded at address $2E00, and checks if the system is
556 suitable for running the program. It also checks if there is enough room between MEMLO
557 and the program start address to move the text mode screen buffer there. If any of the
558 checks return false, the loading of the program is aborted.&nl;
559 The contents of this chunk come from the SYSCHKCHNK memory area of the linker config file.
560 <item>"shadow RAM prepare"&nl;
561 The second load chunk gets loaded to the selected program load address (default $2400).
562 It moves the screen memory below the program load address, copies the character generator
563 from ROM to its new place in RAM, and copies the parts of the program which reside in
564 high memory below the ROM to their place. The high memory parts are included in this load chunk.&nl;
565 At the beginning of this load chunk there is a .bss area, which is not part of the
566 EXE file. Therefore the on-disk start address of this load chunk will be higher than the
567 selected start address. This .bss area (segment LOWBSS) contains the buffers for the
568 double buffering of ROM input and output data. If you add contents to this segment be aware
569 that the contents won't be zero initialized by the startup code.&nl;
570 The contents of this chunk come from the SRPREPCHNK memory area of the linker config file.
571 <item>main program&nl;
572 This load chunk is loaded just above the LOWBSS segment, replacing the code of
573 the previous load chunk. It contains all remaining code and data sections of the program,
574 including the startup code.&nl;
575 The contents of this chunk come from the RAM memory area of the linker config file.
578 <sect2>Moving screen memory below the program start address<p>
580 When setting a graphics mode, the ROM looks at the RAMTOP location. RAMTOP
581 describes the amount of installed memory in pages (RAMTOP is only one byte).
582 The screen memory and display list are placed immediately below RAMTOP.
584 Now in order to relocate the screen memory to lower memory, the startup code
585 puts a value into RAMTOP which causes the ROM routines to allocate the display
586 memory below the program start address and then it issues a ROM call to setup
587 the regular text mode.
589 <sect2>Selecting a good program load address<label id="loadaddr"><p>
591 Due to the movement of the screen memory below the program start, there are some
592 load addresses which are sub-optimal because they waste memory or prevent a
593 higher resolution graphics mode from being enabled.
595 There are restrictions at which addresses screen memory (display buffer and display
596 list) can be placed. The display buffer cannot cross a 4K boundary and a display
597 list cannot cross a 1K boundary.
599 The startup code takes this into account when moving the screen memory down.
600 If the program start address (aligned to the next lower page boundary) minus
601 the screen buffer size would result in a screen buffer which spans a 4K
602 boundary, the startup code lowers RAMTOP to this 4K boundary.&nl;
603 The size of the screen buffer in text mode is 960 ($3C0) bytes. So, for
604 example, a selected start address of $2300 would span the 4K boundary
605 at $2000. The startup code would adjust the RAMTOP value in such way that
606 the screen memory would be located just below this boundary (at $1C40).
607 This results in the area [$2000-$22FF] being wasted.
608 Additionally, the program might fail to load since the lowest address used
609 by the screen memory could be below MEMLO. (The lowest address used in this
610 example would be at $1C20, where the display list would allocated.)
612 These calculations are performed by the startup code (in the first two
613 load chunks), but the startup code only takes the default 40x24 text mode
614 into account. If the program later wants to load TGI drivers which set
615 a more memory consuming graphics mode, the user has to pick a higher
617 Using higher resolution modes there is a restriction in the ROM that it
618 doesn't expect RAMTOP to be at arbitrary values. The Atari memory modules
619 came only in 8K or 16K sizes, so the ROM expects RAMTOP to only have
620 values in 8K steps. Therefore, when using the highest resolution modes
621 the program start address must be at an 8K boundary.
624 <sect2>Character generator location<label id="chargenloc"><p>
626 The default <tt/atarixl/ linker config file (<tt/atarixl.cfg/) leaves the
627 character generator location at the same address where it is in ROM
628 ($E000). This has the disadvatage to split the upper memory into
629 two parts ([$D800-$DFFF] and
630 [$E400-$FFF9]). For applications which
631 require a large continuous upper memory area, an alternative linker
632 config file (<tt/atarixl-largehimem.cfg/) is provided. It relocates the
633 character generator to $D800, providing a single big upper
634 memory area at [$DC00-$FFF9].
636 With the character generator at a different address than in ROM, the routines
637 which enable and disable the ROM also have to update the chargen pointer.
638 This code is not enabled by default. In order to enable it,
639 uncomment the line which sets CHARGEN_RELOC in <tt/libsrc/atari/Makefile.inc/
640 and recompile the <tt/atarixl/ runtime library.
645 <sect1>Function keys<p>
647 Function keys are mapped to Atari + number key.
650 <sect1>Passing arguments to the program<p>
652 Command line arguments can be passed to <tt/main()/ when DOS supports it.
655 <item>Arguments are separated by spaces.
656 <item>Leading and trailing spaces around an argument are ignored.
657 <item>The first argument passed to <tt/main/ is the program name.
658 <item>A maximum number of 16 arguments (including the program name) are
665 The runtime for the Atari uses routines marked as <tt/.INTERRUPTOR/ for
666 interrupt handlers. Such routines must be written as simple machine language
667 subroutines and will be called automatically by the VBI handler code
668 when they are linked into a program. See the discussion of the <tt/.CONDES/
669 feature in the <url url="ca65.html" name="assembler manual">.
672 <sect1>Reserving a memory area inside a program<label id="memhole"><p>
674 (This section is primarily applicable to the <tt/atari/ target, but the
675 principles apply to <tt/atatixl/ as well.)
677 The Atari 130XE maps its additional memory into CPU memory in 16K
678 chunks at address $4000 to $7FFF. One might want to
679 prevent this memory area from being used by cc65. Other reasons to
680 prevent the use of some memory area could be to reserve space for the
681 buffers for display lists and screen memory.
683 The Atari executable format allows holes inside a program, e.g. one
684 part loads into $2E00 to $3FFF, going below the reserved
685 memory area (assuming a reserved area from $4000 to
686 $7FFF), and another part loads into $8000 to
689 Each load chunk of the executable starts with a 4 byte header which
690 defines its load address and size. In the following linker config files
691 these headers are named HEADER and SECHDR (for the MEMORY layout), and
692 accordingly NEXEHDR and CHKHDR (for the SEGMENTS layout).
694 <sect2>Low code and high data example<p>
695 Goal: Create an executable with 2 load chunks which doesn't use the
696 memory area from $4000 to $7FFF. The CODE segment of
697 the program should go below $4000 and the DATA and RODATA
698 segments should go above $7FFF.
700 The main problem is that the EXE header generated by the cc65 runtime
701 lib is wrong. It defines a single load chunk with the sizes/addresses
702 of the STARTUP, LOWCODE, INIT, CODE, RODATA, and DATA segments, in
703 fact, the whole user program (we're disregarding the "system check"
706 The contents of the EXE header come from the EXEHDR and MAINHDR segments.
707 The EXEHDR segment just contains the $FFFF value which is required
708 to be the first bytes of the EXE file.&nl;
709 The MAINHDR are defined in in crt0.s. This cannot be changed without
710 modifying and recompiling the cc65 atari runtime library. Therefore
711 the original contents of this segment must be discarded and be
712 replaced by a user created one. This discarding is done by assigning the
713 MAINHDR segment to the (new introduced) DISCARD memory area. The DISCARD memory area is
714 thrown away in the new linker config file (written to file "").
715 We add a new FSTHDR segment for the chunk header of the first chunk.
717 The user needs to create a customized linker config file which adds
718 new memory areas and segments to hold the new header data for the first load
719 chunk and the header data for the second load chunk. Also an assembly source file
720 needs to be created which defines the contents of the new header data
721 for the two load chunks.
724 This is an example of a modified cc65 Atari linker configuration file
728 __STACKSIZE__: value = $800 type = weak; # 2K stack
729 __RESERVED_MEMORY__: value = $0000, type = weak;
732 STARTADDRESS: default = $2E00;
735 ZP: start = $82, size = $7E, type = rw, define = yes;
737 HEADER: start = $0000, size = $2, file = %O; # first load chunk
739 FSTHDR: start = $0000, size = $4, file = %O; # second load chunk
740 RAMLO: start = %S, size = $4000 - %S, file = %O;
742 DISCARD: start = $4000, size = $4000, file = "";
744 SECHDR: start = $0000, size = $4, file = %O; # second load chunk
745 RAM: start = $8000, size = $3C20, file = %O; # $3C20: matches upper bound $BC1F
748 EXEHDR: load = HEADER, type = ro;
750 MAINHDR: load = DISCARD, type = ro;
752 NEXEHDR: load = FSTHDR, type = ro; # first load chunk
753 STARTUP: load = RAMLO, type = ro, define = yes;
754 LOWCODE: load = RAMLO, type = ro, define = yes, optional = yes;
755 INIT: load = RAMLO, type = ro, optional = yes;
756 CODE: load = RAMLO, type = ro, define = yes;
758 CHKHDR: load = SECHDR, type = ro; # second load chunk
759 RODATA: load = RAM, type = ro, define = yes;
760 DATA: load = RAM, type = rw, define = yes;
761 BSS: load = RAM, type = bss, define = yes;
763 ZEROPAGE: load = ZP, type = zp;
764 AUTOSTRT: load = RAM, type = ro; # defines program entry point
767 CONDES: segment = RODATA,
769 label = __CONSTRUCTOR_TABLE__,
770 count = __CONSTRUCTOR_COUNT__;
771 CONDES: segment = RODATA,
773 label = __DESTRUCTOR_TABLE__,
774 count = __DESTRUCTOR_COUNT__;
779 A new memory area DISCARD was added.
780 It gets loaded with the contents of the (now unused) MAINHDR segment. But the
781 memory area isn't written to the output file. This way the contents of
782 the MAINHDR segment get discarded.
784 The newly added NEXEHDR segment defines the correct chunk header for the
785 first intended load chunk. It
786 puts the STARTUP, LOWCODE, INIT, and CODE segments, which are the
787 segments containing only code, into load chunk #1 (RAMLO memory area).
789 The header for the second load chunk comes from the new CHKHDR
790 segment. It puts the RODATA, DATA, BSS, and ZPSAVE segments into load
791 chunk #2 (RAM memory area).
794 The contents of the new NEXEHDR and CHKHDR segments come from this
797 .import __CODE_LOAD__, __BSS_LOAD__, __CODE_SIZE__
798 .import __DATA_LOAD__, __RODATA_LOAD__, __STARTUP_LOAD__
801 .word __STARTUP_LOAD__
802 .word __CODE_LOAD__ + __CODE_SIZE__ - 1
805 .word __RODATA_LOAD__
806 .word __BSS_LOAD__ - 1
811 cl65 -t atari -C split.cfg -o prog.com prog.c split.s
814 <sect2>Low data and high code example<p>
817 Goal: Put RODATA and DATA into low memory and STARTUP, LOWCODE, INIT,
818 CODE, BSS, ZPSAVE into high memory (split2.cfg):
822 __STACKSIZE__: value = $800 type = weak; # 2K stack
823 __RESERVED_MEMORY__: value = $0000, type = weak;
826 STARTADDRESS: default = $2E00;
829 ZP: start = $82, size = $7E, type = rw, define = yes;
831 HEADER: start = $0000, size = $2, file = %O; # first load chunk
833 FSTHDR: start = $0000, size = $4, file = %O; # second load chunk
834 RAMLO: start = %S, size = $4000 - %S, file = %O;
836 DISCARD: start = $4000, size = $4000, file = "";
838 SECHDR: start = $0000, size = $4, file = %O; # second load chunk
839 RAM: start = $8000, size = $3C20, file = %O; # $3C20: matches upper bound $BC1F
842 EXEHDR: load = HEADER, type = ro; # discarded old EXE header
844 MAINHDR: load = DISCARD, type = ro;
846 NEXEHDR: load = FSTHDR, type = ro; # first load chunk
847 RODATA: load = RAMLO, type = ro, define = yes;
848 DATA: load = RAMLO, type = rw, define = yes;
850 CHKHDR: load = SECHDR, type = ro; # second load chunk
851 STARTUP: load = RAM, type = ro, define = yes;
852 INIT: load = RAM, type = ro, optional = yes;
853 CODE: load = RAM, type = ro, define = yes;
854 BSS: load = RAM, type = bss, define = yes;
856 ZEROPAGE: load = ZP, type = zp;
857 AUTOSTRT: load = RAM, type = ro; # defines program entry point
860 CONDES: segment = RODATA,
862 label = __CONSTRUCTOR_TABLE__,
863 count = __CONSTRUCTOR_COUNT__;
864 CONDES: segment = RODATA,
866 label = __DESTRUCTOR_TABLE__,
867 count = __DESTRUCTOR_COUNT__;
871 New contents for NEXEHDR and CHKHDR are needed (split2.s):
873 .import __STARTUP_LOAD__, __BSS_LOAD__, __DATA_SIZE__
874 .import __DATA_LOAD__, __RODATA_LOAD__
877 .word __RODATA_LOAD__
878 .word __DATA_LOAD__ + __DATA_SIZE__ - 1
881 .word __STARTUP_LOAD__
882 .word __BSS_LOAD__ - 1
887 cl65 -t atari -C split2.cfg -o prog.com prog.c split2.s
890 <sect2>Final note<label id="memhole_final_note"><p>
892 There are two other memory areas which don't appear directly in the
893 linker config file. They are the stack and the heap.
895 The cc65 runtime lib places the stack location at the end of available
896 memory. This is dynamically set from the MEMTOP system variable at
897 startup. The heap is located in the area between the end of the BSS
898 segment and the top of the stack as defined by __STACKSIZE__.
900 If BSS and/or the stack shouldn't stay at the end of the program,
901 some parts of the cc65 runtime lib need to be replaced/modified.
903 common/_heap.s defines the location of the heap and atari/crt0.s
904 defines the location of the stack by initializing sp.
907 <sect1>Upgrading from an older cc65 version<p>
909 If you are using a customized linker config file you might get some errors
910 regarding the MAINHDR segment. Like this:
913 ld65: Error: Missing memory area assignment for segment `MAINHDR'
916 The old "HEADER" memory description contained six bytes: $FFFF
917 and the first and last memory addess of the program. For the "system
918 check" load chunk this had to be split into two memory assigments. The
919 "HEADER" now only contains the $FFFF. The main program's first
920 and last memory address were moved to a new segment, called "MAINHDR",
921 which in the new linker config file goes into its own memory area (also
922 called "MAINHDR").&nl;&nl;
923 A simple way to adapt your old linker config file is to add the
924 following line to the "SEGMENTS" section:
927 MAINHDR: load = HEADER, type = ro;
932 <sect1>Getting rid of the "system check" load chunk<label id="nosyschk"><p>
934 If, for some reason, you don't want to include the "system check" load
935 chunk, you can do so by defining the symbol <tt/__SYSTEM_CHECK__/ when linking the
936 program. The "system check" chunk doesn't include vital parts of the
937 program. So if you don't want the system checks, it is save to leave them out.
938 This is probably mostly interesting for debugging.
940 When using cl65, you can leave it out with this command line:
943 cl65 -Wl -D__SYSTEM_CHECK__=1 <arguments>
946 The value you assign to <tt/__SYSTEM_CHECK_/ doesn't matter. If the
947 <tt/__SYSTEM_CHECK__/ symbol is defined, the load chunk won't be included.
952 This software is provided 'as-is', without any expressed or implied
953 warranty. In no event will the authors be held liable for any damages
954 arising from the use of this software.
956 Permission is granted to anyone to use this software for any purpose,
957 including commercial applications, and to alter it and redistribute it
958 freely, subject to the following restrictions:
961 <item> The origin of this software must not be misrepresented; you must not
962 claim that you wrote the original software. If you use this software
963 in a product, an acknowledgment in the product documentation would be
964 appreciated but is not required.
965 <item> Altered source versions must be plainly marked as such, and must not
966 be misrepresented as being the original software.
967 <item> This notice may not be removed or altered from any source