Actions

SCHG

Mega Drive Processing

From Sonic Retro

The purpose of this page is to give a basic overview of how the Mega Drive handles the processing of code, to help aid in understanding how code may work in a Sonic game. This page will not teach you how to program, however. There are plenty of resources out there to help you learn the fundamentals of programming which can then be applied here.

Motorola 68000

The Motorola 68000 (often just referred to as the "m68k", "68000", or "68k") is the primary processor of the Mega Drive, which is used to run the game logic. It is a 16-bit/32-bit CPU that is clocked at 7.67MHz in NTSC regions and 7.6MHz in PAL regions. Its instruction set can allow for some more complex logic to be more easily used and can even natively do 32-bit arithmetic, and ultimately does the delivering of the "blast processing". Take a look at MarkeyJester's 68000 instruction set guide for a good overview of the most commonly used instructions.

The 68000 has access to the cartridge ROM, the VDP, 65536 bytes of RAM, I/O registers for interfacing attached peripherals/controllers, PSG sound chip, and upon request, the Z80's RAM and YM2612 (more on this later).

The VDP can also access 68000 memory when it uses DMA to quickly transfer data from RAM or cartridge ROM into VDP memory. While this happens, the 68000 is temporarily halted until all the data is transferred.

Zilog Z80

The Zilog Z80 (often just referred to as the "Z80") is the secondary processor of the Mega Drive. It is an 8-bit CPU that is clocked at 3.58MHz in NTSC regions and 3.55MHz in PAL regions. It's main purpose on the Mega Drive is to interface the sound hardware, but it also helps provide backwards compatibility with the Master System. The Z80 has direct access to the YM2612. Its instruction set has more limitations imposed than the 68000 and can only natively do up to 16 bit arithmetic, but for what it's designed to do, it's perfectly fine. Some games will handle all sound handling through this processor, but since it is possible, others may handle FM and PSG sound on the 68000, which allows the Z80 to have more resources for manually streaming higher quality PCM/DAC samples.

The Z80 has access to 8192 bytes of RAM in which its code is stored and run from. As mentioned before, the 68000 can also request for the Z80 to be halted so that it can access this RAM and also access to the YM2612.

The Z80 also can access the cartridge ROM, 32768 bytes at one time, and read its contents via cycle stealing (it basically makes the 68000 halt for a bit to request the data). However, the 68000 has to be able to respond to the request before that can happen, and if something like a DMA from the VDP already has the 68000 halted, then the Z80 it also halted until the 68000 resumes processing so that it can respond and fulfill the request. If the Z80 is responsible for streaming PCM/DAC samples from ROM, then is one of the most common causes of the resulting quality being terrible. Most emulators don't emulate the cycle stealing and halting, however, which explains why samples may sound fine on an emulator, but horrible on real hardware.

Program Flow

The program flow for both processors are pretty simple. They execute one instruction at a time, from top to bottom. You can use branch/jump instructions to change where the program continues running code at (and also have it return back to where you branched out from). The program flow can also be "interrupted". For instance, the VDP can interrupt the 68000 and Z80 once a frame is fully drawn on the screen so that they go to a special function for handling updating for the next frame ("vertical (blank) interrupt" as it's called). The 68000 can also be interrupted after a specific line of pixels in the frame is drawn to go to a special function for quickly updating the VDP midframe for some neat effects ("horizontal (blank) interrupt" as it's called). Other 68000 interrupts include when an error happens and when a controller like a light gun is fired ("external interrupt" as it's called). You can return back to where you were running code before from an interrupt function.

Mega Drive Sonic Community Hacking Guide
Introduction
Beginner Mistakes | Mega Drive Graphics | Mega Drive Audio | Mega Drive Processing