Megadrive Programming : Hello World

In this series I am going to start looking at the Sega Megadrive / Genesis and how to begin programming for it. These tutorials will mostly be focused on the C programming language and will be using the Sega Genesis Development Kit. In this post I will show how to setup your development environment and how to print a simple piece of text on the screen.

What you will Need

  1. The SGDK. This can be found at Github
  2. A Code editor. I use Visual Studio Code
  3. A Sega Genesis Emulator. I use Gens

Setting up Your Development Environment

First we have to set up the SGDK. Please note that this only works on Windows, it also requires Java to be installed on your computer. For the installation of SGDK please check out the tutorial here. This will require you to set up some Windows environment variables (if you have followed my other tutorials this should now be familiar to you).

Beginning to Code

In order to compile and create the files we need to be able to run on the Genesis emulator we need to create a Make file. Create a new file in your project folder called Make.bat and put the following lines into it and save.

This will create the Genesis Rom in your Project folder. The SGDK comes with its own make file generator which helps make this process a lot easier.

Hello World

Creating something on the screen is also the best first step for Programmers. The classic Hello World output is a time honoured tradition. Create a file called main.c and enter the following lines of code.

VCP_drawText(“Hello Mega Drive World!”,8,12); as you can probably tell is a function that draws to the screen. In this case it will write Hello Mega Drive World to the screen. The Megadrive is an alternative name for the Sega Genesis in Europe. It will print this message to the coordinates 8,12.

This function uses the VDP – Video Display Processor, basically any function which starts with the prefix VDP_ does something visual. In This case, its drawing text to the screen.

We then create a game loop, which is just an infinite loop. In this case its a while loop which never exits. Inside this we make another call to the Video Display Processor – VDP_waitVSync();.

The while loop is executed as quickly as possible which may not sync up to your display. In order to avoid display issues you have to tell your Genesis to wait until the screen display has been fully updated before it starts processing more. Thats what the VDP_waitVSync does.

Running your code and loading it in the Emulator

We now need to run our make file. Go to View->Terminal in the Visual Studio Code editor. This will bring up the terminal screen. in here type .\Make.bat. This will build the files needed for your emulator.

When you run the Rom file located in the Out directory of your project folder

You should see :

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s