I wanted to try writing some C code for my RC2014 Classic II computer, so I thought I’d document how I did this.
I’ve not written C for a few years, so I’m quite rusty. My example code may not be the most efficient, but I have compiled it, and then run it on my RC2014.
I use a Mac, so I installed z88dk. This is a C and Assembly language toolkit especially designed to target Z80 based computers like the RC2014.
One of the simplest programs is “Hello World”. This simply prints the string “Hello World” to the screen. In this case, the screen on the RC2014 is actually a serial terminal.
I’m planning on running this using SCM. This is a simple monitor program that the RC2014 Classic II ships with in ROM. I can use this to load in hex code and execute it from a serial terminal.
This is my example Hello World C program. I saved it as “hello.c”.
#pragma output CRT_ORG_CODE = 0x8000
#pragma output REGISTER_SP = 0xFC00
#include <stdio.h>
int main(void) {
printf("Hello World\n");
return 0;
}
This looks pretty much standard C, but you may have noticed the two #pragma’s at the start.
CRT_ORG_CODE tells the compiler to assemble this code to run at address 0x8000. This is the default for SCM.
REGISTER_SP tells the compiler to set the stack pointer to 0xFC00.
To compile the C code, we have to run the following on the command line.
zcc +rc2014 -subtype=basic -clib=sdcc_iy -v -m -SO3 --max-allocs-per-node200000 hello.c -o hello -create-app
+rc2014 tells the compiler we are targeting the RC2014 when generating code.
-subtype=basic tells the compiler we are building code to run under BASIC or SCM. More advanced RC2014s can run CPM, and we could change the subtype if we wanted to target that.
-clib=sdcc_iy tells the compiler which C library we are using.
-v gives us a verbose output.
-m generates map files when compiling.
-SO3 tells the compiler to use level 3 optimisation. This should be the most efficient.
–max-allocs-per-node 200000 is another optimisation that is recommended in the documentation.
-create-app will create .ihx hex files that can be sent to SCM to load the program.
Once compiled, I use a cat command to send the generated hello.ihx file to SCM over an active serial connection.
cat hello.ihx > /dev/tty.usbXXXXX
Finally, in my terminal (I use minicom) I type the following into SCM to execute my program from address 0x8000.
g 8000
And I magically see the words “Hello World” in my terminal.