Installing binutils and GCC as cross-compiler for the Motorolla 68000

Introduction

This short document discusses how to install and set up the GNU's compiler collection for cross compiling to the M68000 plattform so it can be used to develop Sega MegaDrive/Genesis games (and for other consoles that use this processor as well). While this document assumes the use of Linux as a host OS it should work the exact same way on all other UNIX systems, including Mac OS X and propably Cygwin on Windows (please write me to tell me whether this is true if you tried it on one of those two plattforms, thank you).

Although gcc supports several programming languages it seems that only C is supported on the plain 68000 target. C++ would need some functions implemented (particular for printing strings) that the platform simply doesn't provide.

When I wrote this (12.9.2004) the current versions were binutils 2.15 and gcc 3.4.2. Dave Shepperd reported that the last binutils version to support the m68k-coff target is 2.16.1. Using GCC >= 4.3 also seems to be a bit trickier as it requires additional software to be installed (e.g. GMP). If all you want to do is write code for the Sega MegaDrive then binutils 2.16.1 and GCC 3.4.6 should be perfectly fine for you. Maybe I get the time to try building a cross-compile toolchain with current binutils and GCC some time, then I will update this document.

Getting the sources

We need to install two packages: binutils, containing the assembler and linker, and gcc, the C compiler. You can find the sources at these FTP servers:

ftp://ftp.gnu.org/pub/gnu/binutils/
ftp://ftp.gnu.org/pub/gnu/gcc/

But I suggest you use a mirror because the nearest mirror will propably be a lot faster than ftp.gnu.org itself.

http://www.gnu.org/software/gcc/mirrors.html

You need to get the binutils package with the highest version number (note that 2.15 is bigger than 2.2 !). The gcc packages are in a subdirectory which is named gcc-<version>, enter the directory with the highest version number. You'll then see several packages, one simply named gcc-<version>.tar.bz containing really everything and a lot of others named gcc-<subpackage>-<version>.tar.bz2 where <subpackage> is something like ada, core, g++, etc. You can either get the full blown package or just download the -core package which is a lot smaller and contains everything we need (the size is difference is 26M vs 12M for 3.4.2, so you propably really want to get the -core package :-)

Setting a convenience variable

First we'll set an environment variable to make it easier for me to explain what we'll do.

Since we propably don't want to install the cross-compiler into the main directory tree we'll install it into a separate directory. This also makes it easier to get rid of the cross-compiler if you don't want it anymore.

I've chosen /opt/m68k as the target directory for me. I really suggest using a directory in the /opt or /usr/local tree as these are used exactly for these kind of things.

We call the variable CROSSDIRECTORY and set it like this:

export CROSSDIRECTORY=/opt/m68k

Extracting the binutils sources

First you obviously need to unpackage the downloaded tarballs. I have a directory called "src" in my home directory for this purpose in which I extract all source packages. Helps to keep your directory clean.

So assuming you have a directory /home/me/src you'll extract the binutils sources like this:

cd /home/me/src
tar -xjf /path/to/binutils-<version>.tar.bz2

Compiling and installing binutils

Next, change into the binutils source directory:

cd /home/me/src/binutils-<version>

Then we have to make the package ready for being compiled through the "configure" script:

./configure --prefix=$CROSSDIRECTORY --target=m68k-coff

The --prefix option tells configure where we'll install the final executables, see the "Setting a convenience variable" section above. The --target option tells binutils that we'd like binutils to create and handle M68000 instructions with no operating system installed.

Finally compile:

make

This takes a few minutes. After you've successfully compiled binutils you need to install the package. If you're already logged in as root do:

make install

I hope you're logged in as a normal user instead. You really should be, working as root makes it very easy to shoot yourself into the foot accidently and damage your OS installation. Anyways, if you're a normal user then you need to do:

sudo make install

You get asked for your root password because this command will execute with root permissions so you can install into directories which you normally can't write to.

Extracting the gcc sources

Unpackaging the gcc sources is done the same way as with binutils:

cd /home/me/src
tar -xjf /path/to/gcc-<version>.tar.bz2

-OR-

cd /home/me/src
tar -xjf /path/to/gcc-core-<version>.tar.bz2

Compiling and installing gcc

The first step is a bit different from what you may be used to when compiling source packages: we need to create a separate directory in which gcc will com- pile everything:

mkdir -p /home/me/src/gcc-build
cd /home/me/src/gcc-build

Next we'll call "configure" again:

../gcc-<version>/configure --prefix=$CROSSDIRECTORY --target=m68k-coff --enable-languages=c

Again we specify where the final executables should be installed with the --prefix option and that we want a cross-compile with the --target option. The --enable-languages option tells gcc which languages to compile. As far as I know no languages but C are supported on the m68k-coff plattform. Please correct me if I err here.

Next, compile:

PATH=$PATH:$CROSSDIRECTORY/bin make

Now this needs a bit of explanation: gcc will need a few tools from the binutils package (both the version for your host system and the cross-compile version). We need to tune the executable search path so that "make" finds our cross- compiled binutils, they are now in the $CROSSDIRECTORY/bin directory. So if you've set $CROSSDIRECTORY is set to /opt/m68k then this will expand to /opt/m68k/bin. So this line adds $CROSSDIRECTORY/bin to the PATH only for this single call to make and all commands called by make.

Depending on the speed of your machine you can now go make yourself a coffee or something, it'll take a few minutes. After the compilation was successful do:

PATH=$PATH:$CROSSDIRECTORY/bin make install

if you're logged in as root or do:

sudo "PATH=$PATH:$CROSSDIRECTORY make install"

if you're logged in as a normal user.

Finally

Congratulations, you now have a complete gcc toolchain for use with the m68k plattform ! Mind you, all tools (as, ld, gcc, ...) have a prefix m68k-coff-, so you'll use m68k-coff-as instead of as. And since all these programs are in the $CROSSDIRECTORY/bin directory you need to add that path to your PATH variable, either in your global /etc/profile or your users' ~/.profile.

For example, log in as root and edit /etc/profile with your favourite editor. Easiest thing to do that will work on all distributions is to go to the end of the /etc/profile and add these two lines:

# Add cross-compiler to path
export PATH=$PATH:/opt/m68k/bin

Obviously you have to replace /opt/m68k with whatever you've set the CROSSDIRECTORY variable above. Then save the profile, log out and log in again and type m68k-coff-gcc. You should get an error message:

m68k-coff-gcc: no input files

If you get this one then everything went fine. Instead of adding the CROSSCOMPILE directory to PATH you could as well always use absolut paths, but that's a bit annoying, IMHO.

Credits

This mini-howto is written and © 2004 by Marc Haisenko. Thanks to Dave Shepperd for updates on which binutils/GCC versions start to cause trouble.

If you have comments/content corrections/grammar corrections please mail them to me. I'm not a native english speaker so I'm pretty sure there are several spelling and grammar errors.

I do not take any responsibility for damages done to your system through the use of this mini-howto, USE AT OWN RISK !