N64 Programming/Compiling
The N64 is by no means resource limited, so writing software for it in C is perfectly reasonable. One thing you must keep in mind, though: coding for the N64 requires extensive knowledge of both C and MIPS R4K assembly. However, assembly will only have to be used in small routines that initialize the N64 (or handle exceptions). You also have to be familiar with the GNU toolchain (binutils and gcc namely).
Initial Steps
[edit | edit source]Choose a directory that you want the compiled binaries, set this to $PREFIX, and ensure that it exists. For example,
export PREFIX=/opt/n64
mkdir -p $PREFIX
Next, set $GCC to the compiler you are going to use. For example,
export GCC=gcc
Building binutils
[edit | edit source]Download the binutils source code (we will use version 2.35.1), and extract it.
Create an out-of-tree build directory, and change into it:
mkdir -p build-binutils
cd build-binutils
Then run the following commands:
../binutils-2.35.1/configure \
--target=mips64-elf --prefix=$PREFIX \
--program-prefix=mips64- --with-cpu=vr4300 \
--with-sysroot --disable-nls --disable-werror
make CC=$GCC
make install
Hopefully everything went well, and now you'll have a binutils package targeting MIPS. Move back to your working directory and now it is time to build GCC.
Compiling GCC
[edit | edit source]GCC needs to use some of the binaries that you compiled above, so do the following:
export PATH=$PREFIX/bin:$PATH
This will make GCC be able to find them.
As with binutils, download the the gcc source (we will use version 10.2.0) and extract it.
Create an out-of-tree build directory, and change into it:
mkdir -p build-gcc
cd build-gcc
Then run the following commands:
../gcc-10.2.0/configure \
--target=mips64-elf --prefix=$PREFIX \
--program-prefix=mips64- --with-arch=vr4300 \
-with-languages=c,c++ --disable-threads \
--disable-nls --without-headers
make all-gcc CC=$GCC
make all-target-libgcc CC=$GCC
make install-gcc
make install-target-libgcc
The mips64 toolchain should now be installed in your chosen $PREFIX/bin.
Coding examples
[edit | edit source]While coding C for the N64 is no different than any other platform (except that you don't have any libraries at your disposal), you may, at times, have to write to memory mapped registers. The following example (which utilizes DMA) demonstrates this:
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ **
** N64 DMA **
** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
typedef struct
{
/* Pointers to data */
void *ramp;
void *romp;
/* Filesizes (8-byte aligned) */
u32 size_ramrom; /* RAM -> ROM */
u32 size_romram; /* RAM <- ROM */
/* Status register */
u32 status;
} DMA_REG;
/* DMA status flags */
enum
{
DMA_BUSY = 0x00000001,
DMA_ERROR = 0x00000008
};
/* DMA registers ptr */
static volatile DMA_REG * dmaregs = (DMA_REG*)0xA4600000;
/* Copy data from ROM to RAM */
int dma_write_ram ( void *ram_ptr, void *rom_ptr, u32 length )
{
/* Check that DMA is not busy already */
while( dmaregs->status & DMA_BUSY );
/* Write addresses */
dmaregs->ramp = (u32)ram_ptr & 0x00FFFFFF; /* ram pointer */
dmaregs->romp = (u32)rom_ptr & 0x1FFFFFFF; /* rom pointer */
/* Write size */
dmaregs->size_romram = length - 1;
/* Wait for transfer to finish */
while( dmaregs->status & DMA_BUSY );
/* Return size written */
return length & 0xFFFFFFF8;
}
You may also have to use inline assembly a fair bit. The function below sets a breakpoint on a region of memory:
enum
{
BREAKPOINT_READ = 1,
BREAKPOINT_WRITE = 2
};
/* Set breakpoint */
void bp_set ( u32 addr, u8 flags )
{
addr &= 0x3FFFF8; /* assuming lower 4MB, also doubleword */
flags &= 0x03; /* only lower two bits */
addr |= flags;
asm("mtc0 %0, $18\n" /* WatchLo */
"mtc0 $zero, $19\n" /* WatchHi */
::"r"(addr));
}