]> git.sur5r.net Git - cc65/blob - doc/internal.txt
Update comments about the stack, add CBM 500 info
[cc65] / doc / internal.txt
1
2
3                             Internals doc for CC65
4
5
6
7 Stacks:
8 -------
9
10 The program stack used by programs compiled with CC65 is located in high
11 memory.  The stack starts there and grows down.  Arguments to functions, local
12 data etc are allocated on this stack, and deallocated when functions exit.
13
14 The program code and data is located in low memory. The heap is located
15 between the program code and the stack. The default size for the parameter
16 stack is 2K, you may change this for most platforms in the linker
17 configuration.
18
19 Note: The size of the stack is only needed if you use the heap, or if you
20 call the stack checking routine (_stkcheck) from somewhere in your program.
21
22 When calling other functions, the return address goes on the normal 6502
23 stack, *not* on the parameter stack.
24
25
26
27 Registers:
28 ----------
29
30 Since CC65 is a member of the Small-C family of compilers, it uses the notion
31 of a 'primary register'.  In the CC65 implementation, I used the AX register
32 pair as the primary register.  Just about everything interesting that the
33 library code does is done by somehow getting a value into AX, and then calling
34 some routine or other.  In places where Small-C would use a secondary
35 register, top-of-stack is used, so for instance two argument function like
36 integer-multiply work by loading AX, pushing it on the stack, loading the
37 second value, and calling the internal function.  The stack is popped, and the
38 result comes back in AX.
39
40
41
42 Calling sequences:
43 ------------------
44
45 C functions are called by pushing their args on the stack, and JSR'ing to the
46 entry point.  (See ex 1, below) If the function returns a value, it comes back
47 in AX.  NOTE!!!  A potentially significant difference between the CC65
48 environment and other C environments is that the CALLEE pops arguments, not
49 the CALLER.  (This is done so as to generate more compact code) In normal use,
50 this doesn't cause any problems, as the normal function entry/exit conventions
51 take care of popping the right number of things off the stack, but you may
52 have to worry about it when doing things like writing hand-coded assembly
53 language routines that take variable numbers of arguments.  More about that
54 later.
55
56 Ex 1:  Function call:  Assuming 'i' declared int and 'c' declared
57        char, the following C code
58
59         i = baz(i, c);
60
61        in absence of a prototype generates this assembler code.  I've added
62        the comments.
63
64         lda     _i              ; get 'i', low byte
65         ldx     _i+1            ; get 'i', hi byte
66         jsr     pushax          ; push it
67         lda     _c              ; get 'c'
68         ldx     #0              ; fill hi byte with 0
69         jsr     pushax          ; push it
70         ldy     #4              ; arg size
71         jsr     _baz            ; call the function
72         sta     _i              ; store the result
73         stx     _i+1
74
75        In presence of a prototype, the picture changes slightly, since the
76        compiler is able to do some optimizations:
77
78         lda     _i              ; get 'i', low byte
79         ldx     _i+1            ; get 'i', hi byte
80         jsr     pushax          ; push it
81         lda     _c              ; get 'c'
82         jsr     pusha           ; push it
83         jsr     _baz            ; call the function
84         sta     _i              ; store the result
85         stx     _i+1
86
87
88 Note that the two words of arguments to baz were popped before it exitted.
89 The way baz could tell how much to pop was by the argument count in Y at call
90 time.  Thus, even if baz had been called with 3 args instead of the 2 it was
91 expecting, that would not cause stack corruption.
92
93 There's another tricky part about all this, though.  Note that the args to baz
94 are pushed in FORWARD order, ie the order they appear in the C statement.
95 That means that if you call a function with a different number of args than it
96 was expecting, they wont end up in the right places, ie if you call baz, as
97 above, with 3 args, it'll operate on the LAST two, not the first two.
98
99
100
101 Symbols:
102 --------
103
104 CC65 does the usual trick of prepending an underbar ('_') to symbol names when
105 compiling them into assembler.  Therefore if you have a C function named
106 'bar', CC65 will define and refer to it as '_bar'.
107
108
109
110 Systems:
111 --------
112
113 Supported systems at this time are: C64, C128, Plus/4, CBM 500, CBM 600/700, 
114 the newer PET machines (not 2001), Atari 8bit, and the Apple ][ (thanks to 
115 Kevin Ruland, who did the port).
116
117 C64:    The program runs in a memory configuration, where only the kernal ROM
118         is enabled. The text screen is expected at the usual place ($400), so
119         50K of memory are available to the program.
120
121 C128:   The startup code will reprogram the MMU, so that only the kernal ROM
122         is enabled. This means, there are 41K of memory available to the
123         program.
124
125 Plus/4: Unfortunately, the Plus/4 is not able to disable only part of it's
126         ROM, it's an all or nothing approach. So, on the Plus/4, the program
127         has only 28K available (16K machines are detected and the amount of
128         free memory is reduced to 12K).
129
130 CBM 500:
131         The C program runs in bank #0 and has about 61K memory available. The
132         memory available on the CBM5x0 is slightly less than that available
133         on its bigger brothers (CBM 600/700) because the video ram is also
134         placed into bank #0 to allow sprites.
135
136 CBM 600/700:
137         The C program runs in a separate segment and has almost full 64K of
138         memory available.
139
140 PET:    The startup code will adjust the upper memory limit to the installed
141         memory. However, only linear memory is used, this limits the top to
142         $8000, so on a 8032 or similar machine, 31K of memory are available to
143         the program.
144
145 APPLE2: The program starts at $800, end of RAM is $8E00, so 33.5K of memory
146         (including stack) are available.
147
148 Atari:  The startup code will adjust the upper memory limit to the installed
149         memory, considering future graphics memory usage (which is allocated
150         at top of RAM). The programmer can specify which graphics mode is
151         about to be used by defining a variable _graphmode_used, unsigned
152         char, to the mode value (mode values like Atari DOS, 0-31).
153         (Please note that graphics mode selection isn't supported in the
154         Atari runtime lib yet!)
155         In the default case the upper memory limit will be $8035 (with Basic
156         cartridge) and $A035 (without cartridge). This is the default which
157         leaves room for the biggest possible graphics mode. If only standard
158         text mode is used (_graphmode_used = 0), the values are $9C1F (with
159         Basic) and $BC1F (no cartridge).
160         The program starts at $1F00 (to leave room for DOS), and the free
161         memory values are $6135 (24K, Basic, default mode), $8135 (32K, no
162         Basic, default mode), $7D1F (31K, Basic, mode 0) and $9D1F (39K,
163         no Basic, mode 0).
164         These values are for a 48K or 64K machine.
165
166 Note: The above numbers do not mean that the remaining memory is unusable.
167 However, it is not linear memory and must be accessed by other, nonportable
168 methods. I'm thinking about a library extension that allows access to the
169 additional memory as a far heap, but these routines do not exist until now.
170
171
172
173 Inline Assembly:
174 ----------------
175
176 CC65 allows inline assembly by a special keyword named "asm". Inline assembly
177 looks like a function call. The string in parenthesis is output in the
178 assembler file.
179
180 Example, insert a break instruction into the code:
181
182         asm ("\t.byte\t$00")
183
184 Note: The \t in the string is replaced by the tab character, as in all other
185 strings.
186
187 Beware: Be careful when inserting inline code since this may collide with
188 the work of the optimizer.
189
190
191
192 Pseudo variables:
193 -----------------
194
195 There are two special variables available named __AX__ and __EAX__. These
196 variables must never be declared (this gives an error), but may be used as any
197 other variable. However, accessing these variables will access the primary
198 register that is used by the compiler to evaluate expressions, return
199 functions results and pass parameters.
200
201 This feature is useful with inline assembly and macros. For example, a macro
202 that reads a CRTC register may be written like this:
203
204 #define wr(idx) (__AX__=(idx),                                  \
205                 asm("\tsta\t$2000\n\tlda\t$2000\n\tldx\t#$00"), \
206                 __AX__)
207