GCC cross compiler for BBC Micro

Building the itszor/puppeh 6502 C cross compiler and development system for the BBC micro on Linux

Please note, I did not write any of these tools, this is simplified and specific tutorial aimed at helping people get started on C coding on the BBC Micro using the tools that I have been using for my own game.

These instructions were written for Ubuntu 19.10/20.04, but should work unaltered on several Debian based linux distributions such as Debian, Ubuntu, Mint etc. It should be possible to use other Linux distributions and even Cygwin under Windows, but that's beyond the scope of this document.

Prerequisites

  • Familiarity with the Linux command line
  • Some experience building programs from source
  • Ubuntu/Debian/Mint Linux box with ~10GB free disk space

Compiler build instructions

We are basically following the instructions on;

https://github.com/itszor/gcc-6502-bits.git

Oddly one of the first things you need to do is install another C compiler for the 6502, cc65, this is because gcc uses its linker.

sudo apt-get install cc65

You compile up the 6502 compiler using your local machine's compiler, make sure it is installed, also grab a few extra tools and libraries.

sudo apt-get install build-essential flex git libboost-dev libboost-regex-dev dejagnu libgmp-dev libmpfr-dev libmpc-dev

Change to your home directory

cd ~

Checkout the code (this is about 6.3 GB so will take a while)

git clone --recursive https://github.com/itszor/gcc-6502-bits.git

Choose which version of gcc to build

cd gcc-6502-bits/gcc-src
git checkout remotes/origin/m65x-gcc6-virt

Build the compiler, this takes an age, make a cup of tea, a four course meal and learn a language

cd .. (into the gcc-6502-bits directory)
./build.sh 2>&1 | tee build.log

Check that the compiler built

cd prefix/bin
ls

You should see the following files here

6502-cpp 6502-gcc-6.4.1 6502-gcc-nm 6502-gcov 6502-gcov-tool
6502-gcc 6502-gcc-ar 6502-gcc-ranlib 6502-gcov-dump

Example Program

Download this example Hello World program and uncompress it somewhere.

You will need to edit the Makefile to change the path to the gcc compiler to the one in your home directory (unless you are also called peter).

The Makefile creates BBC model B compatible programs by default (which also work on the BBC Master). If you want BBC Master compatibility only (to make use of more memory), edit the Makefile and change the LDFLAGS line from bbcb to bbcmaster, also use the hellow.inf.master in the next section.

When compiling programs the default is to compile programs suitable for MODE 7 (where more RAM is available), to compile programs for other modes, change the value of __STACKTOP__ in LDFLAGS in the Makefile to;

  • MODE 0,1,2 to 0x2fff
  • MODE 3 to 0x3fff
  • MODE 4,5 to 0x57ff
  • MODE 6 to 0x5fff
  • MODE 7 to 0x7bff
  • MODE 128-135 (Shadow screen modes), to 0x7fff

After tweaking the Makefile, build the program with;

make

Running the program

Copy the hellow output file to your machine of choice and set the LOAD/EXEC addresses to &1900 or &E00 (for bbcb or bbcmaster compiled programs repectively), and run with *RUN HELLOW or *HELLOW.

Alternatively import the files with the .inf file to the emulator or disk image editor of your choice (rename the .inf.master to .inf if you compiled up a master version).

bbc-lib: Extra API support

These are some extra wrappers I have written that you may find useful.

Click here for libraries README

Click here for the online API documentation

Download v1 20200604

  • stdint.h A C99 like header to add specific named types for 8, 16, 32bit types, int8_t, uint16_t etc, used throughout the other headers.
  • os.h/os.c contains wrappers around the OS functions
  • basic.h/basic.h contains defines that are similar to BBC BASIC keywords, VDU, PRINTTAB, INKEY etc
  • support.c/support.h contains numeric output functions that can be used instead of printf() to save memory

Suggestions and caveats

The main limitation on the BBC is memory as such most optimisations should be targetted to size rather than speed (but smaller code has less instructions so will likely be reasonably fast as well).

  • You really need to use gcc's size optimisation option, -Os
  • Unused functions appear to not be stripped out of the final binary, as such remove any that you are not using.
  • Use the smallest possible numeric type that holds your data, on 6502-gcc an int is 16 bits, but often you can 8 bit values, this will generate considably smaller code.
  • The implementation of printf() seems to be lacking some functionality e.g. outputing 32 bit values, padding etc.
  • Avoid the use of printf(), it is a general purpose function for all formatted output, adding just one call to printf() will add 2.5KB to the size of your binary. You can use some of the helper functions in support.c/support.h in the bbc-lib library as alternatives.
  • There doesn't appear to be a strcat() function in the library. For strcat(string1, string2) use strcpy(strlen(string1), string2)