News
Documents
Source Code
Downloads
Message Board


Sega Megadrive Assembly Programming Basics

Copyright, Lewis Bassett, December 2005


Chapter 2 - Setting up the environment

Okay, before we can do anything we need to set up our enviroment. A programming enviroment is simply a virtual workshop which we'll use to write our games. This chapter will go through all the steps to set up a working enviroment which we can use to write Megadrive games.

First of all, make a new folder anywhere on your computer, and give it a relevant name, like 'Megadrive', or something similar. This will be where all our work will be done. All of the software used in this document will be installed here.

As an example, I'll go through assembling and running one of the demo programs from this website (available for download in the source code section), so that you can get familiar with the process of assembing and testing a ROM. Download this ASCII Text Demo and unzip the files into the 'Megadrive' directory you just made, using WinZip, or another decompression tool. You should have the following files:

	ASCII.asm
	Header.asm
	Font.dat
	Readme.txt
Don't worry about what each of these files are for just yet, I'll go through each one as we go through this chapter.


The Text Editor

Games are written in assembly, which is a programming language that uses simple english keywords to represent the different operations which the Megadrive can do. These assembly instructions are written into a source file. A source file is simply a normal text file, except it has the extension '.asm', instead of '.txt'. To write these sourcefiles, we'll need a text editor.

I prefer to use the Crimson Text Editor. This particular text editor has a lot of useful features, which I'll explain in a minute. First of all, let's install the software.

Download Crimson Text Editor from this site, and un-zip it, using WinZip or some other decompression tool. Inside the file, there's an installer program called 'cedt370r.exe'. Run this, and follow the steps to install the software onto your computer. After the installation has finished, the software can be found in the programs section of the Windows start menu. When you run the program, you'll be presented with a warning box. Ignore this.

The Crimson text editor works just like any other text editor, except is has some rather cool features. More than one file can be open and edited, which is useful for when we start writing larger programs later on. Each different file that is open has a tab at the top of the page. This tab will also have a green light next to it. Whenever we edit the file, the green light will go red until the file is saved. The most useful feature of Crimson Text Editor though, is that it numbers each line of our code, which makes it MUCH easier for us to find errors later on. The Crimson Text Editor also uses syntax colouring, which marks key words which are used in assembly using different colours. This allows us to identify different parts of our program's code easier.

Using Crimson Text Editor is very straight forward. To open a new file, select 'new' from the file menu at the top. To save select 'save as...' from the file menu. Make sure you save all your source files with a '.asm' extension, and save them into the 'Megadrive' directory you created earlier.

Okay, let's take a little look at the main source code file for the demo we downloaded earlier. Open up the file named 'ASCII.asm', which is in the 'Megadrive\' directory, by selecting 'Open' from the file menu at the top of the screen.


Although any text editor such as Notepad can be used to edit assembly source files, I think Crimson Text Editor provides many useful tools.

This file is the main source code file for the demo. All of the other files which came with the package are included into this file. I'll explain about this in detail in Chapter 4, but for now, I just I want you to take a little look at the file, so that you can familiarise yourself with the way an assembly source file is layed out.


The Assembler

Assemblers are used to convert our assembly source file into a working Sega game. Remember, we write programs using english keywords, but the Megadrive only understands machine code instructions, which is why we need to translate our source files. That's all an assembler is basically, a translator, plus it also does some other things, all of which are explained in greater detail in Chapter 4.

There are many different assemblers which can be used to write Sega programs. I prefer the AS Assembler, which is very flexable and provides some very useful functions.

First of all, download the AS Assembler from this website. Un-zip the whole file into your 'Megadrive\' directory, using WinZip, or some other decompression tool. You'll find the following directories have been added to your 'Megadrive\' folder:

	bin\
	doc\
	include\
	lib\
	man\
The only two directories that really concern us are the 'bin\' and 'lib\' folders.

The 'lib\' directory is used to store library source files, which are source code files that are used in many different programs. Remember the file named 'Header.asm', which was part of the ASCII Text Demo package? This is a library file, as we'll use it for many different projects. Move this file into the 'lib/' directory that was just created.

The 'bin\' directory contains the assembler itself, and some other different programs. The only two programs that concern us are 'as.exe' and 'p2bin.exe'.

The first program, 'as.exe', converts all the code from our source files into a 'list'. A list is basically assembled code, that isn't yet fully formatted. To format the assembled code into proper machine code that can be executed by the assembler, we use the second program, 'p2bin'.

Both programs are DOS programs, which means that they are run from a DOS command line, with the program, any switches, the input file and the output file specified. Since we're working in Windows, we have to write a MS-DOS batch file to run the two programs. A DOS batch file is simply a text file containing a list of typed DOS commands, which is then saved with the extension '.bat'. Batch files are simply clicked on, and run like normal executables.

Let's write a batch file to assemble the ASCII demo. Remember, the files 'ASCII.asm' and 'Font.dat' should be in the 'Megadrive\' directory, and the file 'Header.asm' should be in the 'lib\' subdirectory.

bin\as.exe ASCII.asm
bin\p2bin.exe ASCII.p

pause
The first command tells DOS to use as.exe (the assembler) to assemble the source code file into a list file. The second command tells DOS to use p2bin.exe to convert the list into a binary file, ie, a working Megadrive ROM. The last command, 'pause' tells DOS to wait until the user presses a key. In Windows XP, if this line isn't there, the DOS window will close and you won't be able to see the output messages from the two programs.

Save this batch file (make sure it has the extension '.bat') and double click to run it. If everything went okay, you should have a new file named 'ASCII.bin'. This is our final ROM file, which can now be run on a Megadrive!

Note: 'Header.asm' and 'Font.dat' are included into the 'ASCII.asm' file, which contains the main source code. This is done by the assembler itself, which is explained properly in Chapter 4.


The AS assembler will report any error to you, and the exact line of code where the error occured.

Keep this batch file, and modify it to make new batch files for every project you write. In Chapter 4, I'll explain in detail how the assembler works, and how you can write assembly language instructions which can be converted.


The Emulator

Many years ago, programmers who worked for Sega used special hardware to write their assembled ROMs ('.bin' files) to a special cartridge, so that they could test them on a real Megadrive.

Nowadays, we can run our ROMs on a PC, using an emulator. An Emulator is a special program which simulates all of the working hardware inside the Megadrive. Everything that the Megadrive would normally output to a TV screen is output to the computer screen instead.

Many different good emulators have been written, but in my opinion the best one is Gens, developed by Stéphane Dallongeville. The Windows XP version of Gens can be downloaded from this website (available in the downloads section).

Kaneda, a very active member of this community (his homepage is here: http://www.consoledev.fr.st/), has written a modified version of Gens. His version is specially designed for programmers like us, and allows us to not only run ROMs, but also to see everything that's happening 'behind the scenes'. This is very useful for later on, when we'll need to find out where a problem is.

Let's have a go at running the demo we just assembled!

First, download Kaneda's version of Gens, named KMod, from this web site. Unzip all of the files into a new directory named 'Emulator\', which should be inside the main 'Megadrive\' directory. Inside the 'Emulator\' sub-directory, you should have the following files:

	gens_05.exe
	kailleraclient.dll
	kmod.chm
The emulator is the file named 'gens_05.exe'. Run this by double-clicking on it. An empty window should appear, with funny coloured lines scrolling accross the screen diagonally.

Open up 'ASCII.bin' (our assembled ROM file, located in the main 'Megadrive\' directory) by selecting 'Open' from the 'File' menu. You should now see this:


Emulators allow us to test our games on a PC, without the need to buy expensive hardware.

That's it! That's the ASCII demo, which you downloaded and assembled earlier. If you like, why not edit the text message that's in the source file ('ASCII.asm') and re-assemble the ROM again. Try it!

The most useful feature of KMod is that it allows us to see what's happening in all of the different parts of the Megadrive's hardware, so we can find problems more easily.

Select 'Debug', from the 'CPU' menu. Then select 'Genesis' from the new sub-menu that appears. You should see the following options:

	Genesis - 68k
	Z80
	VDP
	VDP - Registers
	VDP - Sprites
	Sound - YM2612
	Sound - PSG
These are all the main parts of the Megadrive, all of which are explained in Chapter 3. We can see what's happening in all of them!

Select 'Genesis - 68k' from the options above. The following screen should appear:


KMod allows us to see the innner workings of the main processor. This screen shows us everything that is happening inside the main processor of the Megadrive. All of the components listed here are explained properly in Chapter 4.

Now, select 'VDP' from the same menu. You should now see the following screen too:


KMod allows us to see the inner workings of all the video hardware.This screen shows us the contents of all the video hardware, including each indivual letter than is used to write the ASCII string. In Chapter 6, I'll explain all about the video hardware, and this screen.

Now that I've gone through all the steps to assemble and run an working Megadrive ROM, you should be ready to move on the Chapter 3. At this point, you may want to have a little play with the source code files, take a good look at them and get familiar with the layout. In Chapter 3, I'll explain about the hardware that the Megadrive uses, and then after that, I can explain how to write working assembly code.



Chapter 1 Contents Chapter 3


Designed & maintained by Lewis AS Bassett
SEGA, Megadrive, Genesis, Sonic the Hedgehog, etc are all owned by Sega Enterprises Ltd