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 http://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 <genesis.h>

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.

5 Comments

  1. Johann Harding says:

    Hey, I am 26 years old guy who live in Quebec. I am interested in developping games and I too stumbled upon sgdk. I’ve been able to display an Hello world on an emulator. I’m reading the Wiki tutorial on displaying sprites. I also bought a Mega Everdrive cartridge but I am still waiting for it. I work on a pc. I used to play on Unity with C#, but never completed a project. I am still in the learning curve. Anyway, if you are still interested or have more information to share about displaying graphics or converting BMP in to whatever format Genny takes. Hoping to hear news from you. Have a nice day!

  2. Christopher Waite says:

    @Johann

    Thanks for the comment.

    Unfortunately I didn’t play around with the Mega Drive programming much past this original post. It sounds like you’ve already surpassed my knowledge :)

    Something to remember is that Gendev utilises SGDK so you should be able to use that documentation. This points you at some tools for converting for Mega Drive format and for displaying sprits on screen: https://github.com/Stephane-D/SGDK/wiki/Tuto-Background-%28old%29

    Hope that helps a little. Sorry I don’t have more to share.

  3. yoyz says:

    Hi, thank you for this tutorial which is well made.
    For me which run on debian 8.0 x86_64 I had to do some tweaks :

    Before building gendev :
    $ export CFLAGS=”-Wno-error=unused-value”
    It is an error related to some native gcc which had problem building other gcc.

    Modify the path in the skeleton makefile and replace lib64 by lib :
    ARCHIVES += $(GENDEV)/m68k-elf/lib64/gcc/m68k-elf/$(GCC_VER)/libgcc.a

    Now it works, I have a working helloworld on one of my most favorite gaming device :)

    Thanks a lot

  4. bu95bunny says:

    still can’t get it, cuz i keep getting error 2 with any ways/VMs/Linux distributoons/GCC versions etc. to build it -.-
    Can somebody who have built GenDev upload binaries or .deb? Please. (x64 would be nice, if some1 did it :D)

  5. CheeseBurg says:

    Could you update this for Ubuntu 16.04 and the GitHub instructions? I have no experience installing stuff like this on linux and I have been having issues with the sparse instructions on their wiki

Leave a Reply

Your email address will not be published. Required fields are marked *