Sunday, September 15, 2013

Sega Console Programming

In the previous post, we checked out Sega Assembly Programming to generate machine code routines that target Sega hardware; specifically Sega SC-3000: an 8-bit home computer based on the Z80 chip.

As an example, we wrote the main loop of a simple "Shoot 'em up" game that simply moves a target sprite around the screen based on user input; preferably the joystick.

However, all assembly programming and machine code written thus far require a BASIC program driver; Cartridge-based video games built for 8-bit Sega Retro Gaming consoles were written in pure assembly!

In this post, we will focus on the Sega Master System which was the last ever 8-bit video game console manufactured by Sega built using the Z80 chip. Techniques here also apply to the SG-1000, however.
Follow all instructions from Maxim's World of Stuff: this documents how to setup the dev environment.
Download and configure the following software:

Text Editor
Download and install ConTEXT. Download the syntax highlighter file. Save to the Highlighters folder.

Recommended is the WLA DX assembler. Download the compiled binaries and extract.
Remember where you extract the contents as unzipped Compile.bat file is used below.

If you've been following the Sega Retro Gaming series then you will have Fusion and Meka installed:
- Fusion: popular emulator used to play Sega retro games. Download and extract.
- Meka: emulator with some excellent debugging features. Download and extract.

Includes: Sega Master System documents (SMS docs), Z80 User Manual and WLA DX Manual.

Hello World
Download and extract the obligatory "Hello World":
Launch ConTEXT, that is, double click ConTEXT.exe. Setup ConTEXT to compile, link and run assembler (.asm) files:
Options menu, Environment Options, Execute Keys, click "Add".

F9 - Compile Execute: C:\PathToCompile.bat
Start in: %p      Parameters: %n
Check "Capture console output" box
Check "Scroll console to the last line"
Compile output parser rule: *:%n:%l: *

F10 - Fusion Execute: Fusion.exe
Parameters: "%poutput.sms"
Start in: C:\PathTo\Fusion364

F11 - Meka Execute: mekaw.exe
Parameters: "%poutput.sms"
Start in: C:\PathTo\mekaw073

In ConTEXT, choose File menu, Open, "Hello World.asm":
Press F9 to compile and link "Hello World.asm".
This generates the "output.sms" binary file.

Press F10 to run Hello World in the Fusion emulator:

Press F11 to run Hello World in the Meka emulator:
Note: type "C" in the debugger to continue program.

Recommended is the SMS Examine disassembler. Download and extract.
Note: output is recompilable by WLA DX; thus perfect to reverse engineer binary output files.

Disassemble the compiled binary file output.sms that was generated from above.
Create directory "HelloWorld". Copy: Opcodes.dat, output.sms, smsexamine.exe: Start, Run, cmd. cd C:\PathTo\HelloWorld. Type: smsexamine.exe output.sms
This action will generate output.sms.asm and any corresponding data files. Note: output.sms.asm file here is the same as the HelloWorld.asm file above.

Retro Games
Armed with this knowledge, we are now able to disassemble the same retro games we played earlier!
Not only that but we are able to re-assemble the disassembled output and debug through the code:

For example, let's debug through Monaco GP. Download Monaco GP (Japan) (Rev 2).zip and extract.
Create directory "MonacoGP". Copy: "Monaco GP (Japan) (Rev 2).sg", Opcodes.dat, smsexamine.exe. Start, Run, cmd. cd C:\PathTo\MonacoGP. Type: smsexamine.exe "Monaco GP (Japan) (Rev 2).sg"
This action will generate "Monaco GP (Japan) (Rev 2).sg.asm" and any corresponding data files.

Launch ConTEXT. Choose File menu, Open, "Monaco GP (Japan) (Rev 2).sg.asm": Press F9 to compile and link "Monaco GP (Japan) (Rev 2).sg.asm".
This generates the "output.sms" binary file.

Press F10 to run Monaco GP in the Fusion emulator:

Press F11 to run Monaco GP in the Meka emulator:

Note: in Meka we are able to debug through the Monaco GP source assembly code:

Compare machine code in the debugger with the raw binary output file: "Monaco GP (Japan) (Rev 2).sg"

Classic Games
Repeat the process above to disassemble more classic 8-bit Sega retro games:
Congo Bongo
The Castle
After Burner
Golden Axe
24 KB
32 KB
32 KB
512 KB
256 KB
128 KB
512 KB
SG-1000 / SC-3000
SG-1000 / SC-3000
SG-1000 / SC-3000
Sega Master System
Sega Master System
Sega Master System
Sega Master System

Homebrew Games
Homebrew games include any video game built for a games console by an independent game developer under no official company contract; although some consoles are harder to program for than others.

Note: Sega Master System benefits from homebrew development because there is no physical lockout.

Therefore, after reverse engineering Sega retro game code (as above) and learning the intricacies of the hardware, it becomes easier for Indies to build their own versions of classic retro games like Monaco GP.

This Sega Retro Gaming series has explored programming options for retro games on Sega platforms:
Concluding with Sega Console Programming and Homebrew games, we see that it is feasible to build independent video games on classic 8-bit hardware and produce custom cartridge-based video games.

In fact, many Indies are reviving this classic video game art-form and managing to make money in this niche market: Check out Astro Dodge (2012): first game released for the SG-1000 in almost 30 years.

Goal: build retro game in Z80 assembler to target the Sega SG-1000, SC-3000, or the Master System!

No comments:

Post a Comment