Hacking the fx-CP400 - Part 2.1 (Building a Cross-Compiler)

To be able to write code for the fx-CP400 without having to write machine code directly, we're going to need some type of assembler and/or compiler. Luckily for us, GCC has support for the SH4 architecture, so we don't have to write our own assembler or compiler. Compiling source code into machine code for an architecture other than the one the compiler is running on is known as cross-compilation. As such, we'll be building a cross-compiler for the SH4 architecture.

Using the OSDev Wiki as "inspiration", here are the few simple steps to building a cross-compiler for the SH4 platform.

Setup

We'll define a few environment variables to ensure the process goes smoothly.

# folder to install our cross-compiler into
export PREFIX="$HOME/opt/cross"

# the architecture and executable format we're targeting
export TARGET=sh4-elf

# add the cross-compiler to our PATH
# you'll probably want to put this somewhere like .bash-profile
export PATH="$PREFIX/bin:$PATH"

Building binutils (sh4-elf-as, sh4-elf-objcopy and more)

Download the source code for Binutils from the Binutils website. Extract it into a directory, and cd into the directory you've just extracted it into. Then, run these commands in that directory.

mkdir build
cd build
../configure --target=$TARGET --prefix="$PREFIX" --with-sysroot --disable-nls --disable-werror
make
make install

You'll now have tools such as sh4-elf-as and sh4-elf-objcopy available.

Building gcc (sh4-elf-gcc)

Download the source code for GCC from the GCC website. Extract it into a directory, and cd into the directory you've just extracted it into. Then, run these commands in that directory. Be warned - the make commands may take a long time, depending on your system and the contrib/download_prerequisites will download about 30 MB of archives.

contrib/download_prerequisites
mkdir build
cd build
../configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c,c++ --without-headers
make all-gcc
make all-target-libgcc
make install-gcc
make install-target-libgcc

You'll now have sh4-elf-gcc availiable for your usage.

You might also be interested in:

Learn about: SPI

A graphical introduction to SPI.

Hacking the fx-CP400 - Part 2 (Exploring the Firmware)

Modifying the calculator's firmware by inserting a small test payload.

See all posts