Difference between revisions of "C using CC65"

From DenialWIKI
Jump to navigation Jump to search
Line 10: Line 10:
  
 
The CC65 is a cross compiler and is available to run on various host platforms.  This tutorial will attempt to be as platform independent as possible regarding the host platform that the compiler is running on.
 
The CC65 is a cross compiler and is available to run on various host platforms.  This tutorial will attempt to be as platform independent as possible regarding the host platform that the compiler is running on.
 +
 +
== Config File ==
 +
The key to controlling how CC65 creates it's output file is the linker config file.  The linker config file says where to compile the code to, where to allocate variables both static and dynamic.  By changing the linker config file, a program can be made to run in an ''Unexpanded'' memory configuration, a 3K, 8K or even ROM image at $A000. (Although ROM images also need some changes to ensure that the ROM header is included.)
 +
 +
The default linker config that is included for the VIC-20 is for an unexpanded memory model.  The default 1K stack size is likely to be HUGE compared with what's really needed.
 +
 +
<pre>
 +
MEMORY {
 +
    ZP: start =  $0002, size = $001A, type = rw, define = yes;
 +
    RAM: start = $0FFF, size = $0E01, define = yes, file = %O;
 +
}
 +
SEGMENTS {
 +
    STARTUP:  load = RAM, type = ro;
 +
    LOWCODE:  load = RAM, type = ro,              optional = yes;
 +
    INIT:    load = RAM, type = ro, define = yes, optional = yes;
 +
    CODE:    load = RAM, type = ro;
 +
    RODATA:  load = RAM, type = ro;
 +
    DATA:    load = RAM, type = rw;
 +
    BSS:      load = RAM, type = bss, define = yes;
 +
    HEAP:    load = RAM, type = bss, optional = yes; # must sit just below stack
 +
    ZEROPAGE: load = ZP,  type = zp;
 +
}
 +
FEATURES {
 +
    CONDES: segment = INIT,
 +
    type = constructor,
 +
    label = __CONSTRUCTOR_TABLE__,
 +
    count = __CONSTRUCTOR_COUNT__;
 +
    CONDES: segment = RODATA,
 +
    type = destructor,
 +
    label = __DESTRUCTOR_TABLE__,
 +
    count = __DESTRUCTOR_COUNT__;
 +
    CONDES: segment = RODATA,
 +
    type = interruptor,
 +
    label = __INTERRUPTOR_TABLE__,
 +
    count = __INTERRUPTOR_COUNT__;
 +
}
 +
SYMBOLS {
 +
    __STACKSIZE__ = $400; # 1K stack
 +
}
 +
</pre>
  
 
== External Links ==
 
== External Links ==

Revision as of 09:25, 2 July 2009

Introduction

This section is not intended to be a tutorial on how to program in C. There are many tutorials and books on C programming in the internet. This is intended to be information on how to use the freeware C compiler CC65 to write software for the VIC-20.

There are pros and cons associated with using C to program a device with as few resources as the VIC-20. For small tasks, the overhead of the compiled code and associated libraries is significant when compared to the smaller interpreted programs used by BASIC. However, for slightly larger programs, C offers a comprimise between the speed and efficiency of assembly language, and the size of BASIC. However, why you would choose to program in C is like asking "Why program for the VIC-20 at all?" And the only real answer that's needed is "Because I want to."

In many respects, programming in C for the VIC-20 can be considered programming an embedded system when compared with programming for a modern desktop PC. There are additional factors that programmers need to consider when targeting hardware that has severe limitations. In this respect, the articles and tutorials at sites like Embedded.com are more suited than sites that target how to program using Visual C++.

Overview

The CC65 homepage has information regarding the origins of the compiler and it's limitations. Suffice it to say that it's an "almost" ISO C compatible compiler, with many of the features missing from the implementation not appropriate for the target environment anyway. It is well supported by both it's developer, Ullrich von Bassewitz, and it's large user base across all the compatible target platforms. CC65 is more than just a C compiler. It is a suite of tools including a C compiler, assembler, linker and librarian for the 6502 family of microprocessors. Libraries have been written to support the various home computers and games consoles that utilize the 6502.

The CC65 is a cross compiler and is available to run on various host platforms. This tutorial will attempt to be as platform independent as possible regarding the host platform that the compiler is running on.

Config File

The key to controlling how CC65 creates it's output file is the linker config file. The linker config file says where to compile the code to, where to allocate variables both static and dynamic. By changing the linker config file, a program can be made to run in an Unexpanded memory configuration, a 3K, 8K or even ROM image at $A000. (Although ROM images also need some changes to ensure that the ROM header is included.)

The default linker config that is included for the VIC-20 is for an unexpanded memory model. The default 1K stack size is likely to be HUGE compared with what's really needed.

MEMORY {
    ZP: start =  $0002, size = $001A, type = rw, define = yes;
    RAM: start = $0FFF, size = $0E01, define = yes, file = %O;
}
SEGMENTS {
    STARTUP:  load = RAM, type = ro;
    LOWCODE:  load = RAM, type = ro,               optional = yes;
    INIT:     load = RAM, type = ro, define = yes, optional = yes;
    CODE:     load = RAM, type = ro;
    RODATA:   load = RAM, type = ro;
    DATA:     load = RAM, type = rw;
    BSS:      load = RAM, type = bss, define = yes;
    HEAP:     load = RAM, type = bss, optional = yes; # must sit just below stack
    ZEROPAGE: load = ZP,  type = zp;
}
FEATURES {
    CONDES: segment = INIT,
	    type = constructor,
	    label = __CONSTRUCTOR_TABLE__,
	    count = __CONSTRUCTOR_COUNT__;
    CONDES: segment = RODATA,
	    type = destructor,
	    label = __DESTRUCTOR_TABLE__,
	    count = __DESTRUCTOR_COUNT__;
    CONDES: segment = RODATA,
	    type = interruptor,
	    label = __INTERRUPTOR_TABLE__,
	    count = __INTERRUPTOR_COUNT__;
}
SYMBOLS {
    __STACKSIZE__ = $400;	# 1K stack
}

External Links

CC65 Homepage

Embedded.com