3. Sega Master System

The NES's 8-bit rival from Sega

The Sega Master System launched in Japan in 1985 (as the Sega Mark III) and reached North America in 1986. While it never outsold the NES in Japan or the US, it dominated in Europe and Brazil and sold over 13 million units worldwide. Games like Alex Kidd, Phantasy Star, Wonder Boy, and Sonic the Hedgehog (the 8-bit version) showed that the hardware could hold its own against Nintendo's machine.

From a programmer's perspective, the Master System is a very different beast from the NES despite targeting the same generation. Where the NES uses a 6502 variant, the SMS runs a Zilog Z80 — a more complex 8-bit CPU with more registers, a richer instruction set, and 16-bit arithmetic built in. The graphics chip is a derivative of the TMS9918, significantly enhanced with a larger palette and more flexible tile handling. And where the NES relies on mapper chips for bank switching, the SMS has its own standard paging system built into the console.

The System at a Glance

Sega Master System Specifications

CPUZilog Z80A @ 3.58 MHz (NTSC) / 3.55 MHz (PAL)
RAM8 KB main + 16 KB VRAM
GraphicsVDP (315-5124) — 256x192 (or 256x224/256), 32 colors on screen from 64
Sprites64 sprites, 8 per scanline, 8x8 or 8x16
SoundSN76489 PSG — 3 tone + 1 noise channel
CartridgeUp to 512 KB ROM with built-in paging hardware
Video outputNTSC or PAL composite + RGB

The Z80 CPU

The Zilog Z80 is a major step up from the 6502. It has more registers, a richer instruction set, 16-bit arithmetic instructions, block operations, and a relative jump that makes relocatable code easier. Running at 3.58 MHz — roughly 3.5 times the C64's clock speed — it has considerably more processing power available per frame.

Registers

The Z80 has a much larger register file than the 6502:

Z80 Register Set

AAccumulator (8-bit)Main working register, used in most arithmetic and logic
FFlags (8-bit)S (sign), Z (zero), H (half-carry), P/V (parity/overflow), N (subtract), C (carry)
B, CGeneral purpose (8-bit each)Can pair as BC (16-bit) — B is often used as a counter
D, EGeneral purpose (8-bit each)Can pair as DE (16-bit) — often a destination pointer
H, LGeneral purpose (8-bit each)Can pair as HL (16-bit) — the primary memory pointer
IX, IYIndex registers (16-bit)Used for indexed addressing with displacement
SPStack Pointer (16-bit)Points to top of stack, grows downward
PCProgram Counter (16-bit)Address of next instruction
IInterrupt vector (8-bit)High byte of interrupt vector table address
RRefresh counter (8-bit)DRAM refresh — also used as a quick random seed

The Z80 also has a shadow register set — a second copy of A, F, B, C, D, E, H, and L. You can swap between the main and shadow sets instantly with EXX (swap BC/DE/HL) and EX AF, AF' (swap A/F). This is extremely useful in interrupt handlers — swap in the shadow set, do your work, swap back, and you haven't disturbed a single main register.

Register Pairs and 16-Bit Arithmetic

Unlike the 6502, the Z80 can treat pairs of 8-bit registers as 16-bit values and operate on them directly:

; 16-bit addition — trivial on the Z80
    LD HL, $1234     ; load 16-bit immediate into HL
    LD DE, $0010     ; load 16-bit immediate into DE
    ADD HL, DE       ; HL = HL + DE = $1244
                     ; (one instruction, no carry chain needed)

; Compare: the same operation on the 6502 takes 7 instructions

Key Instructions

The Z80 has over 700 instructions (including all prefix variants). Here are the ones you'll use most on the SMS:

Essential Z80 Instructions

Load / Store
LD A, nLoad immediate into A
LD A, (HL)Load from address in HL
LD (HL), AStore A to address in HL
LD HL, nnLoad 16-bit immediate into HL
LD (nn), AStore A to absolute address
Arithmetic
ADD A, nAdd to A (8-bit)
SUB nSubtract from A
ADD HL, rr16-bit add to HL
INC r / DEC rIncrement / decrement register
INC rr / DEC rrIncrement / decrement register pair
Logic & Shifts
AND n / OR n / XOR nBitwise operations with A
SLA r / SRA r / SRL rShift left / arithmetic right / logical right
BIT b, rTest bit b of register (sets Z flag)
SET b, r / RES b, rSet / reset individual bit
Compare & Branch
CP nCompare A with n (sets flags, A unchanged)
JR cc, offsetRelative jump (signed 8-bit offset)
JP cc, nnAbsolute jump
CALL nn / RETCall subroutine / return
DJNZ offsetDecrement B, jump if not zero (loop instruction)
Block Operations
LDIRCopy BC bytes from (HL) to (DE), incrementing both
LDDRSame but decrementing (copy backward)
CPIRSearch: compare A with (HL), increment HL, decrement BC, repeat until match or BC=0
I/O
IN A, (n)Read from I/O port
OUT (n), AWrite to I/O port
OUTI / OTIROutput (HL) to port C, increment HL — OTIR repeats B times

LDIR and OTIR — Hardware Loops

The Z80's block instructions are a huge advantage over the 6502. LDIR copies a block of memory in a single instruction — no loop counter, no branch, no index management. OTIR sends a block of data to an I/O port, which is exactly how you write data to the VDP. These instructions make the Z80 extremely efficient at bulk data transfers.

I/O Ports vs. Memory-Mapped I/O

A fundamental difference from the 6502 family: the Z80 has a separate I/O address space. Instead of memory-mapped registers (like the C64's VIC-II at $D000 or the NES PPU at $2000), the SMS uses IN and OUT instructions to talk to hardware through numbered I/O ports.

Port Direction Function
$7E Read VDP V counter (current scanline)
$7F Read VDP H counter (horizontal position)
$BE Read/Write VDP data port
$BF Read VDP status register
$BF Write VDP control port (address/register writes)
$DC Read I/O port A (controller 1, accent accent accent accent accent)
$DD Read I/O port B (controller 2, accent accent accent accent accent)
$3F Write I/O port control

Memory Map

The Z80's 16-bit address bus gives 64 KB of address space. The SMS fills it with a mix of ROM, RAM, and a clever paging system:

CPU Memory Map

$FFFF ┌─────────────────────┐
      │   System RAM (8 KB) │
      │   mirrored          │ $C000-$DFFF is the actual RAM
$C000 ├─────────────────────┤ $DFFC-$DFFF: paging registers
      │                     │
      │   Cartridge ROM     │
      │   Slot 2 (16 KB)    │ Bankable — controlled by $FFFF
      │                     │
$8000 ├─────────────────────┤
      │                     │
      │   Cartridge ROM     │
      │   Slot 1 (16 KB)    │ Bankable — controlled by $FFFE
      │                     │
$4000 ├─────────────────────┤
      │                     │
      │   Cartridge ROM     │
      │   Slot 0 (16 KB)    │ Bankable — controlled by $FFFD
      │   (first 1 KB fixed)│ $0000-$03FF always maps to ROM page 0
      │                     │
$0000 └─────────────────────┘

Bank Switching

The SMS has built-in paging hardware controlled by writing to three memory addresses in the upper RAM area:

; Switch ROM bank in slot 2 ($8000-$BFFF) to bank 5
    LD A, 5
    LD ($FFFF), A    ; slot 2 now shows 16 KB from ROM offset 5*$4000

; The first 1 KB ($0000-$03FF) is ALWAYS fixed to ROM bank 0
; This ensures the interrupt vectors and boot code are always accessible

Unlike the NES where mapper hardware varies per cartridge, the SMS paging system is standardized. Every cartridge uses the same mechanism — you always know how bank switching works.

The VDP — Video Display Processor

The SMS VDP (315-5124) is derived from the Texas Instruments TMS9918 used in the MSX and ColecoVision, but heavily enhanced. It supports a 256x192 display (with 256x224 and 256x240 modes on some models), 32 on-screen colors from a palette of 64, hardware scrolling, and a more flexible tile system than the NES.

Communicating with the VDP

All VDP access goes through two I/O ports:

  • $BF (control port) — write register values or set the VRAM address
  • $BE (data port) — read/write VRAM data

To set a VRAM address or write to a VDP register, you write two bytes to the control port. The second byte determines the operation:

; Set VRAM write address to $3800
    LD A, $00
    OUT ($BF), A     ; low byte of address
    LD A, $78        ; $38 OR $40 — bit 6 set = write mode
    OUT ($BF), A     ; high byte + mode

; Write to VDP register 0
    LD A, %00000110  ; register value
    OUT ($BF), A     ; value first
    LD A, $80        ; $80 + register number (0)
    OUT ($BF), A     ; $80 = "write register" command

; Write data to VRAM (after setting address)
    LD A, $42
    OUT ($BE), A     ; write byte to VRAM, address auto-increments

Tile Format

SMS tiles are 8x8 pixels with 4 bits per pixel, stored as four interleaved bit planes. Each tile is 32 bytes — twice the NES's 16 bytes per tile, but with 16 colors per tile instead of 4.

Tile Data Format — 4 Bit Planes

For each row of 8 pixels (4 bytes per row, 8 rows = 32 bytes):

Byte 0: bit plane 0 (LSB) ..####.. = $3C Byte 1: bit plane 1 .##..##. = $66 Byte 2: bit plane 2 #..##..# = $99 Byte 3: bit plane 3 (MSB) ........ = $00

Pixel value = (plane3 << 3) | (plane2 << 2) | (plane1 << 1) | plane0 Each pixel is 0-15, indexing into one of two 16-color palettes

The VDP has space for up to 448 tiles in VRAM (in the standard 192-line mode), or 512 in the extended modes. Tiles are stored starting at VRAM address $0000.

The Color Palette

The SMS has two 16-color palettes (32 colors on screen), selected from a master palette of 64 colors. Each palette entry is a single byte encoding 2 bits each for red, green, and blue:

; Palette entry format: --BBGGRR
; Each component: 0-3 (4 levels per channel)
; 4 * 4 * 4 = 64 possible colors

; Set palette entry 0 to bright blue ($30 = %00110000)
    ; First, set CRAM write address to palette 0, entry 0
    LD A, $00
    OUT ($BF), A     ; low byte
    LD A, $C0        ; $C0 = palette write mode
    OUT ($BF), A

    LD A, %00110000  ; BB=11, GG=00, RR=00 = bright blue
    OUT ($BE), A     ; write to CRAM

SMS vs. NES Color Flexibility

The NES has a fixed palette of ~52 colors — you pick from what's available. The SMS lets you define your colors from 64 possibilities (6-bit RGB). And where the NES assigns palettes to 2x2 tile blocks through the attribute table, the SMS assigns a palette to each individual tile via a bit in the tilemap entry. No blocky color boundaries.

Tilemap (Name Table)

The tilemap is the SMS equivalent of the NES nametable. It's a 32x28 grid of 16-bit entries (32x24 visible in 192-line mode), stored in VRAM starting at $3800 by default.

Each tilemap entry is 2 bytes (little-endian):

Tilemap Entry Format (16 bits)

Bits 0-8Tile index (0-511)
Bit 9Horizontal flip
Bit 10Vertical flip
Bit 11Palette select (0 = background palette, 1 = sprite palette)
Bit 12Priority (1 = tile in front of sprites)
Bits 13-15Unused

The per-tile flip and palette selection are a significant advantage over the NES. You can mirror tiles horizontally and vertically in hardware, effectively quadrupling the apparent size of your tile set without using extra VRAM.

; Write a tilemap entry: tile 42, horizontally flipped, palette 1
    ; Set VRAM address to tilemap position (0,0) = $3800
    LD A, $00
    OUT ($BF), A
    LD A, $38 | $40  ; address $3800, write mode
    OUT ($BF), A

    ; Tile 42, H-flip (bit 9), palette 1 (bit 11)
    ; Low byte: tile index bits 0-7 = 42
    LD A, 42
    OUT ($BE), A
    ; High byte: bit 0=tile bit 8, bit 1=H-flip, bit 2=V-flip, bit 3=palette
    LD A, %00001010  ; H-flip + palette 1
    OUT ($BE), A

Scrolling

The VDP has two scroll registers — one for horizontal scroll and one for vertical scroll. Both apply to the entire screen (no per-scanline scroll without tricks).

; Set horizontal scroll to 120 pixels
    LD A, 120
    OUT ($BF), A
    LD A, $80 + 8    ; VDP register 8 = horizontal scroll
    OUT ($BF), A

; Set vertical scroll to 32 pixels
    LD A, 32
    OUT ($BF), A
    LD A, $80 + 9    ; VDP register 9 = vertical scroll
    OUT ($BF), A

A useful feature: VDP register 0 can lock the top 2 rows from horizontal scrolling and lock the right 8 columns from vertical scrolling. This lets you create a fixed status bar at the top or a static column on the right with no software effort — the hardware just doesn't scroll those regions.

Sprites

The SMS supports 64 sprites, each 8x8 or 8x16 pixels (selected globally — you can't mix sizes). Sprite data is stored in a Sprite Attribute Table (SAT) in VRAM, typically at $3F00.

SAT Format

The SAT has an unusual layout — Y positions come first in a block, then X/tile pairs:

Sprite Attribute Table Layout

$3F00-$3F3F: Y positions (1 byte each, 64 entries)
             Value $D0 terminates the sprite list early

$3F80-$3FFF: X position + tile index (2 bytes each, 64 entries) Byte 0: X position Byte 1: Tile index (from tile set)

; Set up sprite 0 at position (100, 80) using tile 10
    ; Set VRAM address to SAT Y table ($3F00)
    LD A, $00
    OUT ($BF), A
    LD A, $3F | $40  ; write mode
    OUT ($BF), A
    LD A, 80         ; Y position (actually displayed at Y+1)
    OUT ($BE), A

    ; Set VRAM address to SAT XN table for sprite 0 ($3F80)
    LD A, $80
    OUT ($BF), A
    LD A, $3F | $40
    OUT ($BF), A
    LD A, 100        ; X position
    OUT ($BE), A
    LD A, 10         ; tile index
    OUT ($BE), A

Sprite Details

  • 8 per scanline limit — same as the NES. Excess sprites are dropped (no priority rotation in hardware — games do this in software to create flickering).
  • Sprite palette only — sprites always use the second 16-color palette.
  • No per-sprite flip — unlike tilemap tiles, sprites cannot be flipped in hardware. You need separate tiles for left-facing and right-facing versions.
  • Shift left by 8 — VDP register 0 bit 3 shifts all sprites 8 pixels to the left, allowing sprites to smoothly enter from the left edge.
  • Zoom and magnify — VDP register 1 can double the sprite size (16x16 or 16x32) and/or magnify sprites to 2x scale. Magnified sprites are pixel-doubled, not additional detail.

Sound — The SN76489 PSG

The SMS uses the SN76489 PSG for audio — a simpler chip than the NES APU or the C64's SID, but capable of recognizable music in skilled hands. It's accessed through I/O port $7F.

Channels

SN76489 Sound Channels

Tone 1Square wave with 10-bit frequency divider
Tone 2Square wave with 10-bit frequency divider
Tone 3Square wave with 10-bit frequency divider
NoiseWhite or periodic noise, 3 frequency presets or follow Tone 3's frequency

Each channel has a 4-bit volume control (16 levels, with 0 = max and 15 = silence).

The SN76489 uses a single write-only register accessed via OUT ($7F), A. Commands are encoded in the data byte itself:

; Command byte format:
;   1 cc t dddd   — LATCH: select channel (cc), type (t=0 freq, t=1 volume), data
;   0 - dddddd   — DATA: continue writing to the latched channel/type

; Play a tone on channel 0 (~440 Hz)
; Frequency divider = 3579545 / (32 * 440) ≈ 254 = $0FE
; Low 4 bits in latch byte, high 6 bits in data byte

    LD A, %10001110  ; latch channel 0 frequency, low 4 bits = $E
    OUT ($7F), A
    LD A, %00001111  ; data byte, high 6 bits = $0F
    OUT ($7F), A

; Set channel 0 volume to maximum (0 = loudest)
    LD A, %10010000  ; latch channel 0 volume, data = 0 (max)
    OUT ($7F), A

; Silence channel 0
    LD A, %10011111  ; latch channel 0 volume, data = 15 (silent)
    OUT ($7F), A

Noise Channel

The noise channel can produce white noise (good for explosions, snare drums) or periodic noise (metallic, buzzy — useful for certain sound effects):

; Set noise to white noise, medium frequency
    LD A, %11100101  ; latch noise channel, white noise (bit 2),
    OUT ($7F), A     ; medium freq preset (bits 0-1 = 01)

; Set noise to follow Tone 3's frequency (bit 0-1 = 11)
; This lets you create tuned noise effects
    LD A, %11100111
    OUT ($7F), A

VBlank and the Frame Loop

The VDP generates an interrupt at the start of VBlank — the period after the last visible scanline. Like the NES, this is the safe window for VRAM updates. The VDP also supports a line interrupt (horizontal interrupt) that fires every N scanlines, configurable via VDP register 10.

; Enable VBlank interrupt
    LD A, %01100000  ; VDP register 1: enable VBlank IRQ (bit 5),
    OUT ($BF), A     ; 8x16 sprites (bit 1)
    LD A, $81        ; register 1
    OUT ($BF), A

    IM 1             ; Z80 interrupt mode 1 (jump to $0038 on IRQ)
    EI               ; enable interrupts

; Interrupt handler at $0038
    .org $0038
irq_handler:
    PUSH AF
    PUSH BC
    PUSH DE
    PUSH HL

    IN A, ($BF)      ; read VDP status — acknowledges interrupt
    ; bit 7 = VBlank flag

    ; ... update VRAM, sprites, scroll, game logic ...

    POP HL
    POP DE
    POP BC
    POP AF
    EI
    RETI

Line Interrupts for Effects

The VDP's line interrupt counter (register 10) fires an IRQ every N scanlines. This is the SMS equivalent of NES raster interrupts. You can use it to change the scroll position mid-screen for parallax effects, create a fixed status bar, or split the screen into independently scrolling regions. Set register 10 to the number of lines between interrupts.

Reading the Controller

The SMS controller has a D-pad and two buttons (1 and 2). Controller state is read from I/O ports $DC and $DD:

; Read controller 1
    IN A, ($DC)
    ; Bit 0 = Up      (0 = pressed)
    ; Bit 1 = Down     (0 = pressed)
    ; Bit 2 = Left     (0 = pressed)
    ; Bit 3 = Right    (0 = pressed)
    ; Bit 4 = Button 1 (0 = pressed)
    ; Bit 5 = Button 2 (0 = pressed)
    ; Bits 6-7 = Controller 2 Up/Down

    BIT 4, A         ; test button 1
    JR Z, button1_pressed  ; Z set = bit was 0 = pressed

Active-low logic, same as the NES — bits are 0 when pressed.

Putting It All Together — Minimal SMS Program

; SMS ROM header and startup

    .org $0000
    DI               ; disable interrupts
    IM 1             ; interrupt mode 1
    JP init          ; jump to initialization

    .org $0038       ; IRQ handler (mode 1)
irq:
    PUSH AF
    IN A, ($BF)      ; acknowledge VDP interrupt
    POP AF
    EI
    RETI

    .org $0066       ; NMI handler (pause button)
nmi:
    RETN

; SMS ROM header (at $7FF0 for 32 KB ROM)
    .org $7FF0
    .db "TMR SEGA"   ; magic string
    .db $00, $00     ; reserved
    .db $00, $00     ; checksum (filled by tools)
    .db $00          ; product code (low)
    .db $00          ; product code (high) + version
    .db $4C          ; region + ROM size (32 KB)
    .db $00          ; padding

    .org $0100
init:
    ; Set up stack
    LD SP, $DFF0

    ; Initialize VDP registers
    LD HL, vdp_init_data
    LD B, vdp_init_end - vdp_init_data
    LD C, $BF        ; VDP control port
    OTIR             ; output B bytes from (HL) to port C

    ; Clear VRAM (16 KB)
    LD A, $00
    OUT ($BF), A
    LD A, $40        ; VRAM address $0000, write mode
    OUT ($BF), A
    LD B, 0          ; 256 iterations
    LD C, $40        ; 64 pages = 16384 bytes
clear_vram:
    XOR A
    OUT ($BE), A     ; write 0 to VRAM
    DJNZ clear_vram  ; inner loop: 256 bytes
    DEC C
    JR NZ, clear_vram ; outer loop: 64 pages

    ; Load palette (just set color 0 to blue)
    LD A, $00
    OUT ($BF), A
    LD A, $C0        ; CRAM write
    OUT ($BF), A
    LD A, %00110000  ; bright blue
    OUT ($BE), A

    ; Enable display
    LD A, %01000000  ; enable display (bit 6)
    OUT ($BF), A
    LD A, $81        ; register 1
    OUT ($BF), A

    EI               ; enable interrupts

main_loop:
    HALT             ; wait for VBlank interrupt
    ; ... game logic here ...
    JR main_loop

vdp_init_data:
    ; Register value, then $80+register number
    .db %00000110, $80  ; reg 0: no line interrupt, mode 4
    .db %10100000, $81  ; reg 1: enable VBlank, 8x16 sprites, enable display
    .db $FF, $82        ; reg 2: name table at $3800 ($FF = $3800)
    .db $FF, $83        ; reg 3: (ignored in mode 4)
    .db $FF, $84        ; reg 4: (ignored in mode 4)
    .db $FF, $85        ; reg 5: SAT at $3F00
    .db $FF, $86        ; reg 6: sprite tiles from $0000-$1FFF
    .db $00, $87        ; reg 7: overscan color = palette entry 0
    .db $00, $88        ; reg 8: horizontal scroll = 0
    .db $00, $89        ; reg 9: vertical scroll = 0
    .db $FF, $8A        ; reg 10: line interrupt counter (disabled)
vdp_init_end:

SMS vs. NES — Key Differences

NES SMS
CPU 6502 @ 1.79 MHz Z80 @ 3.58 MHz
RAM 2 KB 8 KB
Tile depth 2 bits/pixel (4 colors) 4 bits/pixel (16 colors)
Palette Fixed 52 colors Programmable, 64 colors (6-bit RGB)
Palette assignment Per 2x2 tile block Per individual tile
Tile flip No (sprites only) Yes, per tilemap entry (H + V)
Sprites 64 (8x8 or 8x16) 64 (8x8 or 8x16)
Sprite flip H + V flip No flip
Hardware I/O Memory-mapped I/O ports (IN/OUT)
Bank switching Cartridge mapper (varies) Standardized paging
Sound 5 channels (2 pulse, tri, noise, DMC) 4 channels (3 tone, 1 noise)

Different Trade-offs

Neither system is strictly "better." The SMS has more colors, per-tile palette selection, and tile flipping — but the NES has sprite flipping, which the SMS lacks. The NES has a more capable sound chip (especially the DMC sample channel). And while the SMS has standardized bank switching, the NES's cartridge mapper ecosystem enabled more creative hardware extensions like scanline counters and extra sound channels.

Key Takeaways

  • The SMS runs a Z80 at 3.58 MHz — more registers, 16-bit arithmetic, block transfer instructions, and separate I/O ports.
  • The VDP uses 4 bits per pixel (16 colors per tile) vs. the NES's 2 bits per pixel — richer graphics from each tile.
  • Palette colors are programmable (6-bit RGB, 64 possibilities) and assigned per-tile, not per 2x2 block.
  • Tilemap entries include per-tile horizontal and vertical flip — effectively multiplying your usable tile count.
  • Hardware I/O uses Z80 IN/OUT ports, not memory-mapped registers. OTIR makes bulk VRAM writes very efficient.
  • Bank switching is standardized — no mapper zoo like the NES.
  • The SN76489 sound chip is simpler than the NES APU: 3 square waves + noise, no triangle or samples.
  • The VDP's line interrupt counter enables mid-screen effects like parallax scrolling and status bars.

Tools and Emulators

Tool Purpose
Emulicious Accurate SMS emulator with excellent debugger, VRAM viewer, and tile inspector.
Meka Long-standing SMS emulator with built-in debugger and memory viewer.
WLA-DX Cross-assembler supporting Z80 (and many other CPUs). The standard SMS homebrew toolchain.
z80asm Simple Z80 assembler.
SDCC C compiler targeting Z80 — usable for SMS development with devkitSMS.
devkitSMS C library and tools for SMS homebrew development (uses SDCC).
BMP2Tile Converts images to SMS tile format with palette extraction.
SMSPower Community site with technical documentation, homebrew, and ROM hacking resources.

The Master System proved that the Z80 could drive a competitive tile-based console. Next, Nintendo takes a similar Z80-derived CPU and shrinks the entire concept down to a handheld — the Game Boy, with its 160x144 screen and four shades of green.