Z80 Assembly programming for the Sega Master System and the Game Gear!

Unlike the Gameboy, the SMS and GG have a real Z80 CPU!... Quite surprisingly the Master System and Game Gear are pretty much the same system!

The Game Gear has a smaller screen than the master system... rather strangely it actually has a better colour palette! but appart from that, they are identical, and can run the same basic games!

Lets take a look at the specs!

Master System Game Gear
Cpu Z80 3.5mhz Z80 3.5mhz
Ram 8k 8k
Vram 16k 16k
Resolution 256x192 160x144
Max Tiles 512 (8x8 px) - 768 onscreen 512 (8x8 px) - 360 onscreen
Max Sprites 64 (8x8 px 8 per line) 64 (8x8 px 8 per line)
Colors 32 onscreen (16 sprite, 16 tile) 32 onscreen (16 sprite, 16 tile)
Palette 64 4096
Sound chip SN76489 SN76489
Joystick 2x 4 directon, 2 fire 1x 4 direction, 2 fire + start

The smaller screen is handled by 'skipping the 6 tiles from the left and right side, and 3 from the top and bottom... So we can write a game for our gamegear, and run it on a mastersystem, we just need to design around the screen size difference, and use some different palette code!



Console graphics hardware - Tiles and Sprites!
This section is a general description, and not SMS/GG specific, skip to the next chapter if you know the concept of tiles and sprite layers!

The Gameboy,Gamegear and Mastersystem screens do not work like they do on computers like the CPC
Graphics are not just 'bytes' in a memory address...  The screen is made up of a 'Tile Layer' and a 'Sprite Layer'

To explain Tiles and sprites, lets look at our imaginary game shown to the right 'The Super Yuusha Siblings', on a theoretical game system the 'GameChibi'... Just to be very clear, we looking at this as a concept, not the actual layout of the gameboy!

Looking at our example, We have a level with some grass, blocks, and some collectable 'stars'... our hero, Yume is controlled by the player...
The screen is made up of the Tile layer, and the Sprite Layer,

Usually Sprites are drawn above the Tiles... but sometimes they may be drawn below.

It's also possible we could use sprites for the stars.. but sprites are very limited, so the object doesn't move, then tiles will do the job... we can even animate the stars by switching the tile between different patterns
The tile array on the systems we'll be looking at is made up of 8x8 tiles... the array is a 'grid' of these tiles, so the tiles must line up, a block cannot be at a 'half way boundary'

Tiles are defined by a number (usually 0-255)... we define the bitmap (image) data for that tile (we'll call it a pattern), then tell the hardware what positions in the tile array to use that pattern.

In our example, the black background is pattern 0 ... the blocks are pattern 1... the grass is pattern 2... and the stars are pattern 3

Our 'GameChibi' console has a tile array of 8x8... and 64 bytes is used to define the tile grid...
So to define the stars, we need to set memory locations 10,20 and 15 of the tile array to byte '3'

A real system usually has a tile array bigger than the screen (maybe just by one row and one coumn)... this is to allow smooth scrolling of the screen, where two tiles are 'half shown'

Now in the case of our 'GameChibi' system, with it's 64 tile screen, and 256 pattern definitions, we could just set every visible tile to a different pattern, and treat the screen as a plain bitmap again... We can do that on the MSX1, but unfortunately the Gameboy and Mastersystem have too few tiles for their screen size, so some parts of the screen must contain the same tile!

Combining Sprites
Because our system is using hardware sprites, we have to design our game sprites in a way that can be drawn with the hardware sprites... for example lets look at our Yume sprite... if our 'GameChibi' used 8x8 hardware sprites, we would have to use 48 of them to make this image!... we can save 5 (marked green)... these have no data, so we can just not draw them...

When it comes to moving our character, the software will have to move the hardware sprites all together, so the user does not realise they are made up of many sprites!... on systems with more onscreen colors than sprite colors, two or more sprites may be overlapped to make the sprite appear more colorful

Most systems will have one color (usually 0) which marks the transparent colour.

but there is a problem! with software sprites on a bitmap screen, we can draw as much as we want, it will just get slow.... but with hardware sprites, we have a fixed limit of how many sprites can be shown onscreen at once! sounds bad? well actually it's worse, even though a system like the master system can show 64 sprites onscreen, there can only be 8 on a line... if more than 10 appear on the same line, some will flicker, or not appear... there's nothing we can do about it, we just have to design our game to avoid this problem!

The Gamegear screen compared to the Mastersystem
The Sega Mastersystem and Sega game gear are almost the same system, the major difference is the screen size, The SMS screen is 256x192, but the GG screen is 160x144...

If we draw the same data to the screen, we'll realize that the gamegear is drawing only the center area of the SMS screen area, with 3 lines missing from the top and bottom, and 6 columns missing from the left and right sides.

While both systems are 16 color, The Gamegear has a better color palette!
The MasterSystem uses a single byte palette in the format: --BBGGRR
The GameGear uses 2 bytes, in the format ----BBBB GGGGRRRR

Gamegear & Mastersystem Cartridge Rom format & System Ram
The Gamegear rom is pretty simple, when the console starts, execution begins at memory address &0000,

The 'Header' which defines the cartridge is 16 bytes, and typically appears at &7FF0
some of the attributes are not a full byte, and use half a byte (a nibble)

if you're using an emulator, you do not need need a header! even without the 'System type' the 'Fusion' emulator will use the file extension (SMS or GG) to detect the system to use.
From To Meaning
0000 0007 RST0 – Entry Point
0008 000F RST1
0010 0017 RST2
0018 001F RST3
0020 0027 RST4
0028 002F RST5
0030 0037 RST6
0038 003F RST7 – IM1 Interrupt handler
0040 7FEF Game Code
7FF0 7FF7 “TMR SEGA”
7FF8 7FF9 Unused
7FFA 7FFB Checksum
7FFC 7FFE.H Product code in BCD (2.5 bytes) eg 12345
7FFE.L 7FFE.L Low Nibble: Rom Version (0)
7FFF.H 7FFF.H High Nibble: Region & System (&3=SMS JP) (&4=SMS EXP) (&5=GG JP) (&6=GG EXP) (&7=GG INT)
7FFF.L 7FFF.L Low Nibble: Rom Size (&C=32k &F=128k, &0=256k &1=512k)
8000 Game Code (Optional)
C000 DFFF System Ram
D000 FFFF Same as &C000-DFFF (Mirror)
FFFC FFFC Ram Bank Swapper
FFFD FFFD Bank &0000-&3FFF
FFFE FFFE Bank &4000-&7FFF
FFFF FFFF Bank &8000-&BFFF

Gamegear & Mastersystem I/O Ports
Note, some of these ports also respond to alternate addresses, so you may see different port numbers in other programs.
Port Function
06 PSG
3E YM2413
3F Joystick IO Control
7E Vertical counter (R) / PSG (W)
7F Horizontal counter (R)/ PSG (W)
BE VDP data (RW)
BF VDP control (RW)
DC I/O port A and B (Joystick Read)
DD I/O port B and miscellaneous (Joystick Read)

Gamegear & Mastersystem VRAM
Vram is separate so must be accessed by OUT commands on the SMS/GG... Note, Palette data is addressed using &C000... however this is actually not a memory address in the VDP, as the VDP only has 16k ram.
From To Function
0000 1FFF Pattern Definitions
2000 2FFF Sprite Pattern Definitions
3800 3EFF Tile Map
3F00 3FFF Sprite Attribute table
C000 C020 (SMS)
CO40 (GG)
Palette Data
GameGear & Mastersystem Palettes
The GameGear and Mastersystem use 2x 16 color palettes...

Background Tiles can use either of the 2 palettes... sprites always use the second one.

The Mastersystem uses just 2 bits per color channel... meaning 1 byte per definition
The GameGear uses 1 nibble per channel.. meaning 2 bytes per definition... this means the memory locations are different on the SMS and GG

SMS Palette Definition:
 7  6  5  4  3  2  1  0
- - B1 B0 G1 G0 R1 R0

GG Palette Definition:

 F  E  D  C  B  A  9  8    7  6  5  4  3  2  1  0
- - - - B3 B2 B1 B0 G3 G2 G1 G0 R3 R2 R1 R0




Palette Color  SMS Addr  GG Addr 
0 (BG) 0 C000 C000
0 (BG) 1 C001 C002
0 (BG) 2 C002 C004
0 (BG) 3 C003 C006
0 (BG) 4 C004 C008
0 (BG) 5 C005 C00A
0 (BG) 6 C006 C00C
0 (BG) 7 C007 C00E
0 (BG) 8 C008 C010
0 (BG) 9 C009 C012
0 (BG) 10 C00A C014
0 (BG) 11 C00B C016
0 (BG) 12 C00C C018
0 (BG) 13 C00D C01A
0 (BG) 14 C00E C01C
0 (BG) 15 C00F C01E
1 (BG / Sprite) 0 C010 C020
1 (BG / Sprite) 1 C011 C022
1 (BG / Sprite) 2 C012 C024
1 (BG / Sprite) 3 C013 C026
1 (BG / Sprite) 4 C014 C028
1 (BG / Sprite) 5 C015 C02A
1 (BG / Sprite) 6 C016 C02C
1 (BG / Sprite) 7 C017 C02E
1 (BG / Sprite) 8 C018 C030
1 (BG / Sprite) 9 C019 C032
1 (BG / Sprite) 10 C01A C034
1 (BG / Sprite) 11 C01B C036
1 (BG / Sprite) 12 C01C C038
1 (BG / Sprite) 13 C01D C03A
1 (BG / Sprite) 14 C01E C03C
1 (BG / Sprite) 15 C01F C03E


Gamegear & Mastersystem Sprite Memory (&3F00)
The SMS/GG memory supports up to 64 sprites,

There are 3 memory bytes for each hardware sprite, but they are not consecutive!

Look at sprite 0 - shown in black... it's X co-ordinate is at &3F00 ... it's Y co-ordinate is at &3F80... it's Sprite Number is at &3F81

Sprite Numbers can come from memory address &2000, or &0000 (The Tile Patterns) depending on the setting of register &06

Gamegear & Mastersystem VDP Ports
To Set a register, eg, lets set register 8  to value 11
    ld a,11            ;New value
    out (&BF),a    ;Send to control port
    ld a,8 + 128    ;Reg number + 128
    out (&BF),a    ;Send to control port

Register Number Purpose Bits Bit meaning
&00 Mode Control No. 1
&01 Mode Control No. 2
&02 Name Table Base Address
&03 Color Table Base Address
&04 Pattern Generator Table Base Address
&05 Sprite Attribute Table Base Address
&06 Sprite Pattern Generator Table Base Address - - - - - S - - S=Sprite Table Address (0= use patterns for sprites, 1=sprites at &2000)
&07 Overscan/Backdrop Color
&08 Background X Scroll  T T T T T P P P T = Tile offset (0-31), P=Pixel offset
&09 Background Y Scroll  T T T T T P P P T = Tile offset (0-27), P=Pixel offset
&0A Line counter

Joypad Ports
The GameGear and Master System use 3 ports in total, but there are differences!

The Gamegear has an extra button! 'Start' accessible from bit 7 of Port &00... The Gamegear does not have any player 2 controls

The Mastersystem has no start button, but it does have a second player paddle!

note:Button TH has no use on the normal SMS gamepads, so is not used in these rexamples.
Port Bit Purpose
&DC 7 Player 2 - Down
6 Player 2 - Up
5 Player 1 - Fire 2 (TR)
4 Player 1 - Fire 1 (TL)
3 Player 1 - Right
2 Player 1 - Left
1 Player 1 - Down
0 Player 1 - Up
Port Bit Purpose
&DD 7 Player 2 - Extra (TH)
6 Player 1 - Extra (TH)
5 Cartridge slot
4 Reset
3 Player 2 - Fire 2 (TR)
2 Player 2 - Fire 1 (TL)
1 Player 2 - Right
0 Player 2 - Left
Port Bit Purpose
&00 7 Gamegear Start
6 unused
5 unused
4 unused
3 unused
2 unused
1 unused
0 unused



Mastersystem and Gamegear Programming Tutorials:
P. Tilemap graphics on the Sega Master System & Game Gear

General Z80 Assembly Tutorials:
B. Beginner series - Learn the basics
A. Advanced series - In more detail
M. Multiplatform series - programming methods that work on all systems




Visit www.ChibiAkumas.com to get my games and their source code! | Support me on patreon