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... it was also known as the Sega Mark III in Japan.

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  / Mark III Game Gear
Cpu Z80 3.5mhz Z80 3.5mhz
Ram 8k 8k
Max Rom 512k (Up to 4 mbyte in theory)
512k
Vram 16k 16k
Resolution 256x192 pixels
(32x24 tiles - 768 tiles total)
160x144 pixels
(20x18 tiles - 360 tiles total)
Max Tiles 448 (8x8 px)
(512 in theory but some vram used by tilemap etc)
448 (8x8 px)
(512 in theory but some vram used by tilemap etc)
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
PSU
Power Supply
Model 3025-1
9V 1A Center NEGATIVE
(both SMS and Mark III)
(10V famicom works OK)
Model MK-2103
10V 1A Center POSITIVE

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!


ChibiAkumas Tutorials


Lesson H8- Hello World on the Sega Master System and GameGear
   
Lesson S10 - Easy Tile bitmaps on the Sega Mastersystem or GameGear

Lesson P8 - Tilemap graphics on the Sega Master System & Game Gear

Lesson P16 - Palette definitions on the Sega Master System and Game Gear

Lesson P29 - Hardware detection and Bank Switching on the Gameboy/GBC and Sega Mastersystem/GameGear

Lesson P31 - Hardware Sprites on the Master System / Game Gear and MSX1!

Lesson P38 - Playing Digital Sound with WAV on the Sega MasterSystem/GameGear, Elan Enterprise and GameBoy/GBC





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.

if the system is a Majesco SGG the �TMR SEGA� bytes must be present (Checksum is ignored)... if it's not the screen will be black

If the system is an Export SMS the checksum must be correct - if it's not you will get the message "Software Error"

My 'BinaryTools' program can build a valid file with the following command:
BinaryTools.exe checksum "\RelSMS\cart.sms" $0000 $7FF0 "\RelSMS\cart.sms" $7FFA 16bit
From To Meaning
0000 0007 RST0 � Entry Point (jump to your Game Code)
0008 000F RST1
0010 0017 RST2
0018 001F RST3
0020 0027 RST4
0028 002F RST5
0030 0037 RST6
0038 003F RST7 � IM1 Interrupt handler
0066 ?
Non Maskable Interrupt handler
0040 7FEF Game Code
7FF0 7FF7 �TMR SEGA� (needed by later SGG)
7FF8 7FF9 Unused
7FFA 7FFB 16 bit Checksum (sum of bytes $0000-$7FEF... Little endian)
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
E000 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 Tilemap

The 2 bytes that define each tile are in the format ---PCVHN NNNNNNNN

Bit Function
- no function
P Priority (1=Tile in front of Sprites... 0=behind)
C Color palette (0=normal 1=Sprite palette)
V Vertical Flip (1=on)
H Horizontal flip (1=on)
NNNNNNNNN Tile number (0-511)

Gamegear & 
Mastersystem Sprite Memory (&3F00)
The SMS/GG memory supports up to 64 sprites, they can be 8x8 or 8x16 pixels.

They use the second palette of 16 colors. if you don't set the palette, all your sprites will be black!

It is not possible to X or Y flip sprites.

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

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

Sprite Numbers can come from memory address &2000 (Tile 256+), or &0000 (Tile 0-255) depending on the setting of register &06

Gamegear & Mastersystem VDP Ports

To Set a register we out the value to the control port, then the register number +128.
For Example: 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 VHLISMXs
V=Lock Right 8 columns, H=Lock Top 16 pixels...  L=hide left 8 pixes
&01 Mode Control No. 2 -EF13-LD
E=Enable screen / F=Frame interrupt / 13 = Mode bits / L=Large 16x8 Sprites size / D=Double size sprites
&02 Name Table Base Address ----NNNM
&FF = TileMap at &3800 &FD = TileMap at &3000
&03 Color Table Base Address 11111111

&04 Pattern Generator Table Base Address PPPPP11

&05 Sprite Attribute Table Base Address -----SMM
&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!
The 'Pause button' on the console causes a NMI call to address &

note:Button TH has no use on the normal SMS gamepads, so is not used in these examples.
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
Sound on the SMS/GG
The Master System & Gamegear use port &7F for their sound control... the data we send is in the format below


Bits
Command Bit Details  7  6  5  4  3  2  1  0
Format Template L=Latch C=Channel T=Type XXXX=Data L C C T D D D D










Tone - Command 1/2 C=Channel L=tone Low data 1 C C 0 L L L L
Tone - Command 2/2 H= High tone data (Higher numbers = lower tone) 0 - H H H H H H
Volume C=Channel (0-2)  V=Volume (15=silent 0=max) 1 C C 1 V V V V
Noise Channel (Channel 3)  M=Noise mode (1=white) R=Rate (3=use tone 2) 1 1 1 0 - M R R


Sega Mastersystem & GameGear Links (Both systems are practically identical)
Fusion - Emulates the Master System, Game Gear and Others!
SMS Power - Sega Master system programming info

My SMS/GG site - A breif overview of the system, I'll be including these systems in my tutorials soon!



 

View Options
Default Dark
Simple (Hide this menu)
Print Mode (white background)

Top Menu
***Main Menu***
Youtube channel
Patreon
Introduction to Assembly (Basics for absolute beginners)
Amazon Affiliate Link
Forum
AkuSprite Editor
ChibiTracker
Dec/Bin/Hex/Oct/Ascii Table

Alt Tech
Archive.org
Bitchute
Odysee
Rumble
DailyMotion
Please note: I wlll upload more content to these alt platforms based on the views they bring in

Z80 Content
***Z80 Tutorial List***
Learn Z80 Assembly (2021)
Learn Z80 Assembly (old)
Hello World
Simple Samples
Advanced Series
Multiplatform Series
Platform Specific Series
ChibiAkumas Series
Grime Z80
Z80 Downloads
Z80 Cheatsheet
Sources.7z
DevTools kit
Z80 Platforms
Amstrad CPC
Elan Enterprise
Gameboy & Gameboy Color
Master System & GameGear
MSX & MSX2
Sam Coupe
TI-83
ZX Spectrum
Spectrum NEXT
Camputers Lynx

6502 Content
***6502 Tutorial List***
Learn 6502 Assembly
Advanced Series
Platform Specific Series
Hello World Series
Simple Samples
Grime 6502
6502 Downloads
6502 Cheatsheet
Sources.7z
DevTools kit
6502 Platforms
Apple IIe
Atari 800 and 5200
Atari Lynx
BBC Micro
Commodore 64
Commodore PET
Commander x16
Super Nintendo (SNES)
Nintendo NES / Famicom
PC Engine (Turbografx-16)
Vic 20

68000 Content
***68000 Tutorial List***
Learn 68000 Assembly
Hello World Series
Platform Specific Series
Simple Samples
Grime 68000
68000 Downloads
68000 Cheatsheet
Sources.7z
DevTools kit
68000 Platforms
Amiga 500
Atari ST
Neo Geo
Sega Genesis / Mega Drive
Sinclair QL
X68000 (Sharp x68k)

8086 Content
Learn 8086 Assembly
Platform Specific Series
Hello World Series
Simple Samples
8086 Downloads
8086 Cheatsheet
Sources.7z
DevTools kit
8086 Platforms
Wonderswan
MsDos

ARM Content
Learn ARM Assembly
Learn ARM Thumb Assembly
Platform Specific Series
Hello World
Simple Samples
ARM Downloads
ARM Cheatsheet
Sources.7z
DevTools kit
ARM Platforms
Gameboy Advance
Nintendo DS
Risc Os

Risc-V Content
Learn Risc-V Assembly
Risc-V Downloads
Risc-V Cheatsheet
Sources.7z
DevTools kit

MIPS Content
Learn Risc-V Assembly
Platform Specific Series
Hello World
Simple Samples
MIPS Downloads
MIPS Cheatsheet
Sources.7z
DevTools kit
MIPS Platforms
Playstation
N64

PDP-11 Content
Learn PDP-11 Assembly
Platform Specific Series
Simple Samples
PDP-11 Downloads
PDP-11 Cheatsheet
Sources.7z
DevTools kit
PDP-11 Platforms
PDP-11
UKNC

TMS9900 Content
Learn TMS9900 Assembly
Platform Specific Series
Hello World
TMS9900 Downloads
TMS9900 Cheatsheet
Sources.7z
DevTools kit
TMS9900 Platforms
Ti 99

6809 Content
Learn 6809 Assembly
Learn 6309 Assembly
Platform Specific Series
Hello World Series
Simple Samples
6809 Downloads
6809/6309 Cheatsheet
Sources.7z
DevTools kit
6809 Platforms
Dragon 32/Tandy Coco
Fujitsu FM7
TRS-80 Coco 3
Vectrex

65816 Content
Learn 65816 Assembly
Hello World
Simple Samples
65816 Downloads
65816 Cheatsheet
Sources.7z
DevTools kit
65816 Platforms
SNES

eZ80 Content
Learn eZ80 Assembly
Platform Specific Series
eZ80 Downloads
eZ80 Cheatsheet
Sources.7z
DevTools kit
eZ80 Platforms
Ti84 PCE

IBM370 Content
Learn IBM370 Assembly
Simple Samples
IBM370 Downloads
IBM370 Cheatsheet
Sources.7z
DevTools kit

Super-H Content
Learn SH2 Assembly
Hello World Series
Simple Samples
SH2 Downloads
SH2 Cheatsheet
Sources.7z
DevTools kit
SH2 Platforms
32x
Saturn

PowerPC Content
Learn PowerPC Assembly
Hello World Series
Simple Samples
PowerPC Downloads
PowerPC Cheatsheet
Sources.7z
DevTools kit
PowerPC Platforms
Gamecube

Work in Progress
ChibiAndroids

Misc bits
Ruby programming









Buy my Assembly programming book
on Amazon in Print or Kindle!


Buy my Assembly programming book



Available worldwide!
Search 'ChibiAkumas' on
your local Amazon website!
Click here for more info!

























































































































































Buy my Assembly programming book
on Amazon in Print or Kindle!


Buy my Assembly programming book



Available worldwide!
Search 'ChibiAkumas' on
your local Amazon website!
Click here for more info!