Tutorial: How to write a simple program that runs on your N64

sanni
Senior Member
sanni
Senior Member
Joined: February 21st, 2012, 1:47 pm

July 30th, 2012, 1:19 pm #1

Did you ever wanted to create your own program on the N64?

Yes? Then let me invite you to this little tutorial :)
No? Well I hope you still find it entertaining ;)

First of all I have no clue about what I'm going to talk here, I can't program and all that will follow after this warning is a just a documentation of me applying the principles of copy&paste and trial&error.
But this is actually a good thing because I will explain everything from a beginners perspective so it should be easy for everyone to follow.

Disclamer: I only used Open Source Software, everything I discuss or link here is completly legal.
Introduction:
Thanks to Shaun Taylor's amazing work on libdragon it has become very easy to write little programs for your favorite console the N64.

In this tutorial we will use the virtualization software VirtualBox together with Ubuntu and libdragon to create a 100% legal development kit for the N64.

Every step will be documented with a picture.

Step 1: Setting up VirtualBox

A) First you need to download and install Virtualbox: https://www.virtualbox.org/wiki/Downloads

B) Next you need to download and extract my premade hdd image that contains both Ubuntu and libdragon preinstalled.
Since both libdragon and Ubuntu are open source it's absolutly legal to link you to this image: https://mega.nz/#!hENlWAYC!tLgxtEqsRVoN ... SnwhdszZ3A (1000MB)
If you need a program to extract the file I recommend 7zip

C) After you started VirtualBox you need to create a new virtual machine


D) Name it N64DEV and make sure to select Linux and Ubuntu from the dropdown menues


E) In the "Virtual Hard Disk" window click on "Use existing hard disk"(1), then click on the browse icon(2) and select the hdd image you downloaded earlier(3)


F) After setting up your Virtual Machine click on "Start" to run it


Step 2: Inside Ubuntu

A) Log in with password: dev


IMPORTANT: To change the keyboard layout click on the little "Deu" icon on the top bar and choose your keyboard layout.

B) On the top bar click "Places" and then "Home Folder"


C) Browse to libdragon/examples(1) right-click on "spritemap"(2) and select Copy(3) to Desktop(4). Then rename the new folder on the desktop to n64forever.

The examples directory has many coding examples that teach you how to program with libdragon. Instead of creating our little program from scratch we will just copy one of the examples and alter it to our liking.

D) I want to put the N64 Forever logo into our program.
So I downloaded and resized it to approx 200 pixels on my Windows machine, saved it as N64Forever.PNG and then transfered it to Ubuntu.
To transfer the picture to Ubuntu running in the virtual machine I used a service called dropbox. But you could upload it on mediafire or any other online storage site too. Then open Firefox in the virtual machine and download it again.


E) To use the picture in our program we have to convert it first. So copy the picture onto the desktop. Right-click somewhere on the desktop and select "Open in Terminal".
A black window should pop up.
Write the following in it:

Code: Select all

$N64_INST/bin/mksprite 32 N64Forever.png n64f.sprite
Press Return/Enter to execute the command.


F) A new file called n64f.sprite should be on your desktop now. Double click on the n64forever folder on your desktop(the one you created in step 2C) and then open the filesystem directory.
Delete the files that are already in there(1) and move our n64f.sprite in this directory(2).


Step 3: Editing the Makefile

A) First we need to edit the Makefile in our n64forever directory. Since we just copied it from the spritemap example.
Rightclick on Makefile and choose "Open with Geany"


B) We need to replace

Code: Select all

PROG_NAME = spritemap
in line 9 with

Code: Select all

PROG_NAME = n64forever
And

Code: Select all

$(N64TOOL) -b -l 2M -t "Spritemap Test"
in line 20 with

Code: Select all

$(N64TOOL) -b -l 2M -t "n64forever"



C) Save the file and close Geany.

Step 4: Programming

A) In our n64forever directory rename spritemap.c to n64forever.c

Next right-click on it and choose "Open with Geany"

B) Now alter the code like this

Code: Select all

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdint.h>
#include <libdragon.h>
#include <stdlib.h>

int main(void)
{
    /* enable interrupts (on the CPU) */
    init_interrupts();

    /* Initialize Display */
    display_init( RESOLUTION_320x240, DEPTH_32_BPP, 2, GAMMA_NONE, ANTIALIAS_RESAMPLE );
   
/* Initialize Filesystem */
    dfs_init( DFS_DEFAULT_LOCATION );
    
    /* Initialize Controller */
    controller_init();
    
    /* Read in single sprite */
    int fp = dfs_open("/n64f.sprite");
    sprite_t *n64f = malloc( dfs_size( fp ) );
    dfs_read( n64f, 1, dfs_size( fp ), fp );
    dfs_close( fp );
    
    /* define two variables */
    int x = 40;
    int y = 100;

    /* Main loop test */
    while(1) 
    {
        static display_context_t disp = 0;

        /* Grab a render buffer */
        while( !(disp = display_lock()) );
        
        /* Fill the screen */
        graphics_fill_screen( disp, 0 );
       
/* Create Place for Text */
char tStr[256];

        /* Text */
        graphics_draw_text( disp, 10, 10, "N64 FOREVER" );

/* Logo */
graphics_draw_sprite_trans( disp, x, y, n64f );

        /* Scan for User input */
        controller_scan();
        struct controller_data keys = get_keys_down();

/* If Dpad is pressed move Image */
        if( keys.c[0].up )
        {
y = y+5;
        }
        else if( keys.c[0].down )
        {
y = y-5;
        }
        else if( keys.c[0].left )
        {
x = x-5;
        }
        else if( keys.c[0].right )
        {
x = x+5;
        }

sprintf(tStr, "X: %d\n", x );
graphics_draw_text( disp, 10, 20, tStr );
sprintf(tStr, "Y: %d\n", y );
graphics_draw_text( disp, 10, 30, tStr );
        
        /* Update Display */
        display_show(disp);
    }
}

You will notice that we deleted quite a bit and added a few lines of our own.

C) Save the changes

Step 5: Compiling

A) Right click somewhere in our n64forever directory and choose "Open in Terminal"

B) In the box that pops up write

Code: Select all

make
And execute by pressing Return/Enter.


C) If everything worked it should look like this:

If not you probably got a typo, check which line it complains about and fix it in the code.

D) Lots of new files should have been created, but we only care for n64forever.v64. Thats our rom we want to run on our N64.
You can use the Mess emulator to test your program on your PC:
http://messui.the-chronicles.org/



Step 6: Upload the program
To get the n64forever.v64 rom to your Windows machine use dropbox or mediafire again. Upload it through Firefox in Ubuntu then download it again from within Windows.



Step 7: Running the program
To run the program on your N64, you need a flashcart. Just copy the n64forever.v64 to the flashcarts SD card and launch it from the flashcarts menu.


And thats how our program looks like:

You can move the N64 Forever logo around the screen with the dpad.


Quote
Like
Share

Mk II
Elite
Joined: November 7th, 2010, 11:49 am

July 30th, 2012, 3:15 pm #2

Post of the week!
Quote
Like
Share

jonebone
Senior Member
jonebone
Senior Member
Joined: March 17th, 2011, 7:13 pm

July 30th, 2012, 3:23 pm #3

Interesting, it looks like this was written in C. I had to write several programs in C / C++ as an undergrad but haven't used that language in years. It's pretty powerful and easier than assembly though (NES-code), so the sky is the limit if everything works as easy as it sounds. Nice write up!
Quote
Like
Share

Mk II
Elite
Joined: November 7th, 2010, 11:49 am

July 30th, 2012, 6:29 pm #4

Now for your next assignment, replace the logo graphic with a Nintendo character like Mario or Zelda.
Then turn the image into a sprite with at least two frames of animation.
Extra credit for a fitting background image.



Quote
Like
Share

stinger9142
Guardian
Joined: December 4th, 2006, 6:53 am

August 2nd, 2012, 2:30 am #5

Impressive work! :D
Quote
Like
Share

PrinceReborn
Newbie
Joined: January 26th, 2014, 11:18 pm

January 27th, 2014, 2:19 am #6

Anyone have working links/images as they are broken?
Quote
Like
Share

sanni
Senior Member
sanni
Senior Member
Joined: February 21st, 2012, 1:47 pm

January 27th, 2014, 3:26 pm #7

I'm sorry but my image hoster deleted all my images. But luckily in this case I had made an complete backup. So enjoy :)


Quote
Like
Share

PrinceReborn
Newbie
Joined: January 26th, 2014, 11:18 pm

January 28th, 2014, 1:49 pm #8

PrinceReborn wrote:Anyone have working links/images as they are broken?
Wow! Thank you so much for the effort! I'm going to have a go.
Quote
Like
Share

Mk II
Elite
Joined: November 7th, 2010, 11:49 am

January 29th, 2014, 3:13 pm #9

Sanni,

the MESS emulator does not support Nintendo 64 without the BIOS Rom files and i can't get the test program to run in Project64 either.
any idea or suggestion?

UPDATE yeah, i've just succesfully compiled and run "my" first N64 program!
Only replaced the sprite art as Sanni suggested for a trial run.



I couldn't get the Windows toolchain working properly so i had to use the Ubuntu VM setup but i'm quite familiar with Linux/Ubuntu so that worked out really well.
I happen to own a Doctor V64 and i'm looking forward to burning my creations to CD-ROM and run them on an actual console

Word of advice: this is not the best platform to learn how to program (probably one of the worst actually).
Get the C programming basics under your belt before you try to tackle the N64
Quote
Like
Share

Kibib
Newbie
Kibib
Newbie
Joined: August 11th, 2016, 2:38 am

August 11th, 2016, 2:56 am #10

Hey everyone, newbie here.
I have followed the tutorial and played around with the code a bit, and it is quite a lot of fun.
Now, what's really bugging me is, that I seem to be incapable of creating sprites with an alpha-channel (converting PNGs with transparent areas).
Mk II seems to got that working, so I was wondering, if there is anything special to do when using mksprite in such a case?
When trying to load the sprites, they simply do not appear.
The functions dfs_read and dfs_open both return negative values.

The same thing happens with libdragons own examples, though...

Any suggestions?
UPDATE: The MESS Emulator is certainly not the cause (I thought it might be), I've got the .n64 running on the cen64 emulator as well - same problem.
Quote
Like
Share