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.
Setup
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.

Assembler
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.

Emulators
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 Fusion364.zip and extract.
- Meka: emulator with some excellent debugging features. Download mekaw073.zip and extract.

Documentation
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

ConTEXT
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.

Disassembler
Recommended is the SMS Examine disassembler. Download smsexamine1_2a.zip 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:
Year
1983
1984
1986
1987
1987
1987
1989
Game
Congo Bongo
Flicky
The Castle
After Burner
Shinobi
Wonderboy
Golden Axe
Size
24 KB
32 KB
32 KB
512 KB
256 KB
128 KB
512 KB
Console
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.

Summary
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!

15 comments:

johnwick said...

Thanks for sharing such amazing content . If you want to know about xbox 360 in Pakistan click on the link

waqas waqas said...

Thanks for sharing this quality information with us. I really enjoyed reading. Will surely going to share this URL with my friends
3d tv price in pakistan

Emma Watson said...

Thank you for sharing updated information.Keep up your information. Great job
dqfansurvey

Unknown said...

Hey I loved the way you shared the valuable information with the community. I would say that please continue these efforts and we want to hear mor e from you. krogerfeedback

Unknown said...

Hey I loved the way you shared the valuable information with the community. I would say that please continue these efforts and we want to hear more from you.pandaexpress.com/feedback

Anonymous said...

Hey Thanks for sharing this valuable information with us. I will come back to your site and keep sharing this information with us. Win Prizes by taking surveys

Muslim Rose Welfare said...

Donate Sadaqah Online to Muslims charity through Muslim Rose Today to help extremely poor people around the world. Your online sadaqah can help those less fortunate. So we can donate zakat online with the help of Muslim rose welfare organization.
Donate Ramadan Zakat to Muslims charity through Muslim Rose Welfare to help Muslim. Your zakat donations can help those less fortunate. During this holy A Perfect Chance to Donate Ramadan Donations

Wania Sheikh said...


Great article with remarkable idea. It is so informative. Kindly write more about mens clothing including formal shirts for men, mens jeans pants, womens clothing and more

Wania Sheikh said...

Its really amazing to write on such type of topic, I’ll be looking forward for your next blog on mobile accessories , Power Banks , Bluetooth handsfree and more

Speedy Sprint said...

Speedysprint makes finding the right Online Courier service for you really easy. As the Best Courier Service across London comparison website, we compare all parcel prices and postal rates from the most reliable parcel London Courier delivery companies for you –saving you time, effort and money! At Speedysprint, we guarantee to have the cheapest parcel delivery quotes in the UK. Because we work with all of the industry’s biggest and best Online Courier including UPS, Hermes, and Parcel force we can provide the widest range of top-quality service options to our customers at the lowest prices Online Courier Service .We offer a wide range of parcel collection with Same Day Pickup and Same Day Delivery services, including expedited and premium options to ensure maximum flexibility for our customers.

Angelina John said...

I feel happy about learning more about this topic. Keep sharing your information regularly for my future reference. This content creates new hope and inspiration within me. Thanks for sharing an article like this. The information which you have provided is better than another blog. Please do write more about web hosting companies in Pakistan 2019 and Cheap web hosting in Pakistan 2019 .

Aliza Solutions said...


If you are looking for serious business growth Aliza solutions will help you to grow your business rapidly. Aliza Solutions provide digital marketing service all around the World based in Pakistan & UK. We are the best digital marketing agency in Pakistan and a social media marketing agency in Pakistan & UK. Our core competencies are digital marketing and technology solutions. We help you to outline, evolve and employ proficient, best priced and top-notch Digital Marketing Services, including Search Engine Optimization (SEO), Pay per Click (PPC), App & Mobile Marketing, Reputation Management, Social Media, Content Marketing and more related services to empower your business digitally.

thulannguyen said...

Thanks for sharing, nice post! Post really provice useful information!

Giaonhan247 chuyên dịch vụ mua hàng mỹ từ dịch vụ order hàng mỹ hay nhận mua nước hoa pháp từ website nổi tiếng hàng đầu nước Mỹ mua hàng ebay ship về VN uy tín, giá rẻ.

Speedy Sprint said...

Awesome and interesting article Great things you've always shared with us delivery service

All Car Removal said...

Great post! This Article are very helpful for us, Thank you for this informative article car removal brisbane | cash for cars logan | cash for cars sunshine coast | cash for cars caboolture | cash for cars brisbane

Post a Comment