How to compile ccode on the c64

10 min read

OSCAR64 Credit: https://github.com/drmortalwombat/oscar64

David Bolton takes you back to the 1980s and helps you develop Commodore 64 games faster by coding in C instead of 6502.

Previously in Linux Format… We covered how to write P 6502 (LXF312) first on a website and then using an assembler, and then how to develop for the Commodore 64 (LXF313). In this article, we’re continuing with the C64 but moving way from 6502 assembly language and instead we’re going with coding in C.

Originally, we were going to use the CC65 C compiler but eventually chose an alternative, Oscar64 (https://github.com/drmortalwombat/oscar64), instead. The main reason being that it appears to be more powerful, implementing most of C99 plus a lot of C++ features, and comes with a number of sample programs, including games. These include full source code, so if you want to write games or other programs, there are plenty of examples. The original idea for the Oscar64 compiler was to translate the C source to an intermediate 16-bit byte code, with the option to use native machine code for crucial functions. This enables it do some extra optimisation.

Another benefit of coding in C is that the compiler includes code for doing multiplication and division. This isn’t provided for 6502 assembly language, so if you need either, you’ll have to write your own.

We’ll look at a few examples in C, compiling them and running them on the VICE emulator.

So, you want to develop a game for the Commodore 64? There are certain features that you need in every game. Here’s a list:

Game elements

Read the joystick

Output text

Manipulate hardware sprites

Play sounds

The easiest way to cover these is to take one of the sample games and see how it’s done there. It’s actually made easier because each of these has its own .c source file and header .h file.

Nineteen multiplexed sprites on a Commodore 64 screen.

The samples provided are very nicely structured and very well commented, making it fairly easy to understand the code.

The start of missile.c has these seven lines, plus a few words added to explain what each does: #include <c64/vic.h> – defines enums plus various utility functions. #include <c64/memmap.h> – some defines for char ROM (or not) #include <c64/sprites.h> – functions for manipulating sprites. #include <c64/joystick.h> – function for polling joystick #include <c64/rasterirq.h> – raster interrupt functions #include <c64/cia.h> – data for accessing parallel port (cia) #include <gfx/mcbitmap.h> – functions for drawing, filling and so on, in hi-res mode

These refer to the files in the Include folder and specifically the C64. There are similar folders for Vic- 20, C128, Nes and others. If you want to understand what the addresses mean, load the Project 64 text file (https://bit.ly/l