Vintage Computing
FIGURE 4. Printing the alphabet with
machine code.
FIGURE 5. Verbose CC65 compiler output.
// set background to black (53281)
POKE(0xd021,0);
our first. Here is the source code for the program (hello.c):
#include <stdio.h>
int main()
{
printf(“Hello, world!\n”);
}
// set text color to white
POKE(646,15);
printf(“Hello, world!\n”);
}
From a command prompt, change to the C:\c64\dev
directory. We have provided a batch file called “ CC.BAT”
that will be used to compile and link the given C program.
It will then copy the executable file to C:\c64\drive8 so
that it can be loaded by the emulator.
This program adds an include directive for the header
file “peekpoke.h.” This header file contains macros that
make it easier to deal with fetching and storing eight-bit
and 16-bit values from/to memory. The POKE macro we
used above looks like this:
From the command prompt, enter the following:
cc hello
#define POKE(addr,val) (*(unsigned char*)
(addr) (val))
You should see something like the screen depicted in
We could have written the first expression as:
Figure 5. From a WinVICE window, load the program from
drive 8 and run it. Refer to Figure 6.
*(unsigned char *)(0xd020) 0;
Note that the startup code that CC65 embeds into
every executable program forces the C64 display into
“lower case” mode. CC65 has also automatically
translated our ASCII constant “Hello, world!” into PETSCII
so that we don’t need to convert back and forth ourselves.
The POKE macro provides syntactic sugar for an
expression like the above.
You can take a look at the peekpoke.h header file or
any of the header files in the C:\c64\cc65\include folder.
If you’d like to look at or change any of the code in
hello.c, issue the following from a command prompt:
You’ll need to use an editor that can understand Unix-style
bare linefeed characters to terminate a line.
notepad hello.c
You can then save hello.c after making your changes.
You can invoke the “cc” batch file again to compile your
changes, and copy the executable program to WinVICE’s
drive8 directory.
FIGURE 6. Loading and running a CC65 program.
Let’s try a version of hello.c that takes advantage of
the C64 colors. Here’s colorhello.c:
#include <stdio.h>
#include <peekpoke.h>
int main()
{
// set border to black (53280 )
POKE(0xd020,0);
October 2015 55