Bytesize Adventures
Crafting bite-sized digital worlds

Modern Mega Drive Programming – Getting Started


Mega Drive

The Sega Mega Drive is my all time favourite games console. I had one as a kid when they were first introduced. I remember fondly playing Sonic for hours, leaving the console on pause overnight to come back to it the next day (They have no persistent memory on board – although EA later introduced cartridges with a battery and limited memory).

About 5 years ago, I bought a second hand Mega Drive on eBay. I really went to town on buying all the games I wanted as a kid (now priced at a few pounds). I also played through, and completed, every sonic back-to-back. Something I never managed to do as a kid.

Sonic screenshot

That Mega Drive still takes pride of place under my TV and I still have friends over to play 4-player Micro Machine tournaments.

There’s a heavy dose of nostalgia at play when I power on the Mega Drive. It evokes very happy memories for me.

It was when chatting in a pub one night with a very good friend (about Elite and then all things retro), that I suddenly thought “wouldn’t it be cool to write a game for the Mega Drive”. So I set forth to investigate how such a thing could be done, and if it was even possible.

Research

As it turns out, not only is such a thing possible but it has a very active, thriving community around it. Documentation and tutorials are sparse but there is a wealth of knowledge in those forums.

The Mega Drive was originally programmed in Motorola 68000 Assembly language. That looks something like this (Shamelessly borrowed from here):

Loop:
   move.l #0xF, d0 ; Move 15 into register d0
   move.l d0, d1   ; Move contents of register d0 into d1
   jmp Loop        ; Jump back up to 'Loop'

It looks a little overwhelming if its not something you’re used to. However, its possible to understand portions of it with a little effort. I found this incredibly well written blog all about it.

I was actually quite attracted to giving assembler a shot since it looks like something meaty to get my teeth into. However, my desire to produce an actual product is still strong and it was then that I stumbled upon SGDK – A Mega Drive SDK written in C. Much better. I don’t have much C knowledge but I do have knowledge of Objective-C (and a BSc in computer science) so I figure I’ll be able to fathom it with some effort.

It turns out that SGDK is setup for Windows. I only have access to a Mac so what do I do? Gendev to the rescue. Gendev is another library that extends SGDk to allow it to be utilised on Linux. Perfect!

Getting Started

I wanted to isolate my Mega Drive development environment so what follows are the technical steps I took to setup such an environment in a Virtual Machine on a Mac.

Some of this stuff is on the Gendev page itself but I’m writing it here for posterity and because I did have a couple of hickups, the solutions to which others may find useful.

It’s worth mentioning up front that my hardware is a mid-2009 Macbook Pro (Core 2 Duo) with 8GB of RAM.

  1. Download and install VirtualBox. This should be self explanatory.
  2. Once installed, create a new VM instance and select Linux and Ubuntu 32-bit. I had quite a few failed attempts when doing all of this and one of the more recent things that caught me out was hard drive size. The recommended 8GB won’t be enough. Installation of Ubuntu and the Mega Drive tool chain occupies about 12GB. I went for 25GB as a sensible option.
  3. Download the 32-bit Ubuntu 12 iso (it’s the one called ubuntu-12.04.4-desktop-i386.iso). I had speed issues with Ubuntu 14. Its far too slow when virtualised on my hardware. Additionally, the gendev tools were clearly tested on 12 so its a safer bet for compatibility.
  4. Start your Ubuntu instance and select the downloaded iso as the boot media. You should then follow the steps to install Ubuntu.
  5. As an optional step, once installation is complete, install Xfce. This is a much faster desktop environment than Unity (the Ubuntu default). The reason for doing this is to improve performance of the VM on my old-ish Macbook Pro. More recent hardware is unlikely to suffer as much. Run this command:-
    sudo apt-get install xfce4
  6. Install the Gendev dependencies. Run this command:-
    sudo apt-get install subversion build-essential texinfo
  7. Now we need to checkout Gendev. Find a suitable directory. I created a “Dev” folder in home. Then run this command:-
    svn checkout https://gendev.googlecode.com/svn/trunk/ gendev-read-only
  8. Now we need to compile the tools. This was another “fun” bit. I had quite a few failed attempts at this but if you’ve followed the steps above, you’re unlikely to face them. One thing worth noting is that this compilation process took about 15 hours on my hardware configuration. When you’re ready, run the following commands:-
    cd gendev-read-only
    make
  9. Now you need to compile the SGDK tools. This should only take a couple of minutes. Run these commands:-
    . ~/.gendev
    cd gendev-read-only/sgdk
    make install

    (Note:- that first command is just exporting a couple of paths which you can view in the .gendev file in your home directory)

  10. You should be done now. You create a new project by copying the SGDK skeleton directory to any directory of your choice. Here I’m just creating a megadrive-test directory in my “Dev” folder. Run these commands:-
    mkdir megadrive-test
    cp -R gendev-read-only/sgdk/skeleton megadrive-test

That’s it. You’re done.

Creating and compiling a simple program

You’re basically left to your own devices then. If you go to the SGDK Wiki there is a hello world example.

Just open your favourite text editor (I used Sublime Text) and copy the hello world example into a new file:-

#include 

int main()
{
    VDP_drawText("Hello World!", 10, 13);

    while(1)
    {
            VDP_waitVSync();
    }
    return (0);
}

Save the above code as “test.c” (or anything you like provided it has the c file extension). You should save this file in the root project directory “megadrive-test”. Now run these commands:-

cd megadrive-test
make

If you get something like this:-

make: m68k-elf-gcc: Command not found

Its because you need to run the . ~/.gendev command to export the path again. You can of course just add it permanently to your .profile file which can be found in your home directory.

If it worked, you should have an “out.bin” file in your “megadrive-test” test directory. In order to run it, you’ll need a Mega Drive emulator. I’m using one called Gens/GS.

Download the Ubuntu 8.04+ package from here.

Then navigate to the directory you downloaded to and run this command:-

sudo dpkg -i Gens_2.16.7_i386.deb

You should now be able to open Gens (its in “Games” in the “Applications Menu”) and “Open ROM…”. Select the “out.bin” file in “megadrive-test” and voila!

Mega Drive Emulator

Conclusion

I hope that was helpful. I’m not yet 100% sure if I’ll develop anything, I’m just dabbling at the moment. If I do however, then expect a follow up post at some point soon.