Reading Time: 16 minutes

Sonic the Hedgehog on the Game Gear came out in 1991. This week, while my computers were searching it for faster routes, Sonic did something impossible. Swimming up through a flooded corridor in Labyrinth 1, he moved 1,362 pixels to the right in a single frame, from x 621 to x 1983, the very edge of the level. Then he sank slowly through empty space outside the level until he died.

This post is the full story of that glitch. It turned out to be a race between the game's main code and its 60-times-a-second interrupt, of a kind that has sat in the cartridge for 35 years. It goes down to the individual Z80 instructions, so expect hex. Along the way the processor ends up running the developers' own copyright notice as if it were program code. After that I built tools to find every race of that kind in the game without playing it. They found a second race that turns empty air into a spring, and maps of every act show where each spring and current would appear.

The real thing, replayed from the button presses the search saved. Sonic is swimming up at x 621, y 676. One frame later he's at x 1983, the level's right edge, and the camera races to catch up. He's outside the level now, so there's nothing to stand on and he sinks.

How it was found

My Sonic Solver is a search program that plays the game on a team of computers, looking for the fastest button presses through every act. It knows nothing about how Sonic works: it only watches a few numbers in the game's memory, such as position, speed and the clock. One thing it does watch for is an impossible move: Sonic's position changing by much more than his speed allows. Normal play never moves him more than 84 pixels in one frame; anything over 150 is flagged, and the team's computers save the button presses that led to it.

Labyrinth 1's map in the solver had a strange triangle of dots in an empty corner at the bottom right: about 2,500 places "reached" outside the level. Each one was Sonic sinking from the right edge, steering a little left or right as he fell. The team had logged the jump that put him there 63 times, always at the same spot. It's a dead end for speed (the fastest route doesn't use it), but it was clearly something the game shouldn't do, so I went looking for the cause.

To do that I loaded the moment just before the jump into the Gearsystem emulator's debugger and stepped through the frame one instruction at a time. Claude Code drove the debugger through its MCP interface. Before the explanation, here's what you need to know about the hardware.

A two-minute primer: banks, shadows and the frame interrupt

The Game Gear's Z80 processor can only see 64 KB at a time, but the Sonic cartridge holds 256 KB. The cartridge is split into sixteen 16 KB banks. A chip in the cartridge, the mapper, decides which bank appears in each 16 KB window (a slot) of the processor's address space:

AddressesWhat the Z80 seesSwitched by writing to
$0000-$3FFFslot 0: bank 0 (start-up code, interrupt handler)fixed in Sonic
$4000-$7FFFslot 1: usually bank 1 (main game code)$FFFE
$8000-$BFFFslot 2: whatever bank the code needs right now (level data, object code, music)$FFFF
$C000-$DFFF8 KB of RAM, repeated at $E000-$FFFF

The mapper registers can be written but not read back. So the game keeps its own note of which bank is in each slot: a shadow copy in RAM, at $D22F (slot 1) and $D230 (slot 2). Every bank switch in Sonic is two instructions: write the bank number to the mapper, then write the same number to the shadow.

The third ingredient is the frame interrupt. Once per frame, 60 times a second, the video chip interrupts whatever the Z80 is doing and jumps to the handler at $0038. The handler updates the screen and plays the music. It also switches banks itself, because the music driver lives in bank 3. So it starts by saving the shadow bytes and finishes by putting the banks back from what it saved:

0078  LD   HL,($D22F)    ; read both shadows (slot 1 and slot 2)
007B  PUSH HL             ; ...and keep them on the stack
007C  BIT  0,(IY+0)       ; has the game finished this frame's work?
0080  CALL NZ,$0148       ;   no:  short path
0087  CALL Z,$00B8        ;   yes: full screen update
008A  EI
008B  LD   A,$03          ; sound driver is in bank 3
008D  LD   ($FFFE),A
0090  LD   ($D22F),A
0093  CALL $4000          ; play the music
 ...
00A3  POP  HL
00A4  LD   ($FFFE),HL     ; put BOTH slots back from the saved shadows
00A7  LD   ($D22F),HL

That's a perfectly sensible design. The handler puts things back exactly as it found them, as long as the shadows tell the truth.

Why it needs a late frame

Most of the time the interrupt finds the game idle. The main loop has finished the frame's work and is waiting. That's what the bit test at $007C checks, and it's when the handler does the full screen update. But when a lot is going on, such as many objects or water, the game's work for one frame takes longer than 1/60 of a second. Then the interrupt arrives in the middle of it. It takes the short path, keeps the music going, puts the banks back and returns, and the game carries on where it was. Speedrunners call these lag frames: the picture doesn't change because the game didn't finish in time.

Animated timeline: on time, the interrupt always lands while the game is idle; running late, it lands in the middle of the game's work
When the game keeps up, the interrupt always lands while the main loop is waiting. When it runs late, the interrupt lands somewhere in the middle of the game's work, and that is the only time it can split one of the game's two-step bank switches.

The race

Suppose the interrupt arrives between the two steps of a bank switch: after the mapper has been written, but before the shadow. The handler saves the shadow, which still holds the old bank. When it finishes, it "restores" that old bank into the mapper. The game then does its second step and writes the new bank number to the shadow. Now the shadow says one bank and the hardware holds another. For the rest of that frame, every read from that slot returns bytes from the wrong part of the cartridge.

Animation of the bank race: the mapper is set to bank 5, the interrupt fires before the shadow is updated, saves bank 2, and restores bank 2, leaving the game reading the wrong bank
The race step by step, using the bank switch at $4BFB from later in this post. The window is a single 13-clock-cycle instruction, out of roughly 59,700 cycles in each frame.

The fix would have been tiny: write the shadow first, or turn interrupts off around the pair. Most bank switches in the game are never at risk, because they happen while the game is loading or while interrupts are already off. A few run during play, and Sonic's own block check runs every single frame.

Labyrinth 1: the bubble that wasn't

Every frame, the game finds the 32×32 block of level that Sonic is in, looks up that block's type, and runs the routine for that type. Types include springs, spikes, conveyor belts and water currents. Here's the end of that code. It switches slot 2 to bank 2, where the routines live, and jumps to the one for this block:

4C2F  LD   DE,$5BEB       ; table of block-type routines
4C32  ADD  HL,DE
4C33  LD   A,(HL)
4C34  INC  HL
4C35  LD   H,(HL)
4C36  LD   L,A            ; HL = routine for this block type
4C37  LD   DE,$4C44       ; where to come back to
4C3A  LD   A,$02
4C3C  LD   ($FFFF),A      ; step 1: slot 2 = bank 2
4C3F  LD   ($D230),A      ; step 2: shadow = 2      <-- the window is between these
4C42  PUSH DE
4C43  JP   (HL)

Just before this, slot 2 held bank 5 (level data). If the interrupt lands in the window at $4C3C, slot 2 goes back to bank 5 while the shadow says 2. In Labyrinth 1 the block-type routine itself usually doesn't care. A separate piece of Sonic's code does. When the level has water and Sonic is underwater, it makes a breath bubble once every 256 frames of game logic:

54AD  BIT  4,(IX+24)      ; is Sonic underwater?
54B1  RET  Z
54B2  LD   A,($D224)      ; game-logic frame counter
54B5  AND  A
54B6  CALL Z,$91F4        ; every 256th frame: make a bubble (code in bank 2)

That call assumes bank 2 is in slot 2, and normally it is. But on the one frame where the counter rolls over and the game is running late and the interrupt lands in that one instruction, slot 2 still holds bank 5. And at $91F4 in bank 5 there isn't code at all. There's text: the signature the developers, Ancient, left in the ROM.

Table showing the bytes of the text '(C)Ancient.' decoded as Z80 instructions, ending with a write to Sonic's x position
The Z80 runs the letters of "(C)Ancient." as instructions. Most do nothing useful. Four of them build an address out of whatever is in the registers and memory, then write to it. The address they land on is Sonic's x position.

Each letter is a valid Z80 instruction: C is LD B,E, i is LD L,C, t is LD (HL),H. What the text does depends on what's in the registers at that moment:

  • Register C holds $F4 when Sonic is moving up. Earlier code at $4F0A sets it from the sign of his vertical speed. Moving down, it's $0C, and the stray write lands harmlessly in ROM. So only an upward swim can do anything.
  • The text builds HL = $F4F4 and reads a byte from it. The Game Gear repeats its RAM at $E000-$FFFF, so $F4F4 is really $D4F4, a byte inside another object's slot. In Labyrinth 1 at that spot it's 0, so HL becomes $F400.
  • LD (HL),H writes $F4 to $F400, which is really $D400: the high byte of Sonic's x position. x goes from $026D (621) to $F46D, roughly 62,000 pixels to the right.
  • The processor carries on through more of the text and the bytes after it, and by luck finds its way back without crashing. Later in the frame, the game's own "don't leave the level" check sees x is far past the right edge and puts him on the edge: x = 1983. y, speed, rings and everything else are untouched.

Every condition is in that chain: underwater, swimming up, the 256-frame counter at zero, a late frame, the interrupt in a 13-cycle window, and $D4F4 = 0. If $D4F4 holds something else, the $F4 lands on a different byte of Sonic's data instead of x.

Could it happen anywhere else in the act?

The code says yes. Every block type in Labyrinth 1 goes through the bank-2 switch, so the window opens on every frame, wherever Sonic is. The jumps the team found all came from one small area, x 620-665, y 676-681. Was that because the search swims there a lot, or because the spot itself is special?

To find out, I picked 28 underwater starting points spread across the act and ran 200,000 tries, 7,143 from each point, with random buttons. Each try ran on through one bubble check. As a control, the same test on the route that originally found the glitch gave 1 jump in 6,000 tries, so the test can see it.

Map of Labyrinth 1 with 28 test points; all 8 jumps came from one spot in the middle-left flooded room
All eight jumps came from one spot. Click for full size.
  • The glitch needs a late frame exactly at the bubble check. In the original case the check frame took two video frames, and the jump came on the second.
  • At x 620-640, y 680-700, that frame was late almost 100% of the time. At about a dozen other spots it was late 25-80% of the time. Everywhere else it was under 5%.
  • At the hot spot, 8 jumps from 6,597 chances where everything else lined up: about 1 in 800.
  • Everywhere else, 0 jumps from 6,162 such chances. The same rate would have given about 7.5, and the odds of seeing none by luck are about 1 in 1,800.
  • On the right of the act (x 1000-1150), $D4F4 was never 0, so the x-variant can't happen there at all.

So in practice there's one spot. The interrupt always fires at the same moment in the frame; what changes is how far the game has got by then. At that spot, the amount of work in the bubble frame happens to put the end of Sonic's block check right where the interrupt lands. The test can't prove it's impossible elsewhere, only very much rarer.

One side discovery: swimming left past x 1024 at one test point, Sonic jumped 254 pixels to the right in one frame, every time. That's a different bug, a lost "borrow" when the low byte of x wraps around, and nothing to do with interrupts. The solver has logged thousands of mysterious "253 px" moves across 14 acts. This may be what they are, but I haven't checked.

Finding every race without playing: three layers

The solver found the Labyrinth glitch by luck: it happened, and the solver noticed. The obvious question is what else is in there. Playing the game until something odd happens is hopeless for a 13-cycle window that needs a late frame. Instead the search works in three layers, each one narrowing the next:

  1. Read the program. Find every place an interrupt could split something that should have been done in one go.
  2. Force it. For each candidate, make the interrupt happen there in the debugger, and record what changes.
  3. Reach it. Find real button presses that make a late frame line up with the window. This one is still to do.

This work is kept apart from the solver, in a separate project I call the ROM Lab. The solver's rule is that it must find every trick by itself, without being told how the game works. The Lab is the opposite experiment: it is allowed to read the code. Nothing from the Lab goes back into the solver.

Layer 1: a scanner that reads the whole program

I wrote a Z80 decoder in Python and a scanner on top of it. Starting from the reset and interrupt vectors, the scanner follows every jump, call and jump table. As it goes it tracks three things for every instruction: which bank is in slots 1 and 2 at that point, whether interrupts are on, and whether it's main-game code or interrupt code. Then it checks three kinds of hazard:

  • Bank switches where the mapper is written before its shadow while interrupts are on, and the old and new banks differ.
  • Video-port pairs. The video chip takes its addresses as two bytes, one after the other, and the interrupt reads a status port that resets that two-byte sequence.
  • Shared variables that the main code reads, changes and writes back, while the interrupt's short path writes them too.

It takes about two seconds:

Funnel: 15,173 instructions decoded, 766 in the interrupt's short path, 62 bank writes, 18 risky windows, 2 that run every frame of play
From the whole program down to the two windows that matter.
  • 18 risky bank switches out of 62. Each leaves a one-instruction gap. Thirteen are in bank 0 code at $0A97-$1FF7, which looks like level-loading, graphics and sound set-up. Most of them switch slot 1, where the main game code lives, so a hit there would most likely crash the game. Three are inside the interrupt itself, after it re-enables interrupts, but only a line interrupt could land there and that one doesn't touch banks. That leaves the two in Sonic's block check, which run on every frame of play.
  • $4C3C is the Labyrinth 1 glitch. The scanner found it with no hints. It also found that the four calls to $91F4 in the drowning code (at $52B1-$52BA, the burst of bubbles when Sonic drowns) would run the same text.
  • $4BFB is new. It's the switch to bank 5 just before the block-type table is read. More on that below.
  • 33 video-port pairs, 2 of them with interrupts on ($1CFE, $1EA4). Worst case is a glitched tile or palette entry for a frame.
  • 5 shared bytes in the sound driver ($DC04, $DC4E, $DC7B, $DCA8, $DCD5). Worst case is a wrong note.
  • 0 shared scratch variables. The developers clearly thought about the interrupt's short path, which leaves the game's working variables alone. What they missed is that "put the banks back" trusts a shadow that can be one instruction out of date.

A static scan has limits. It merges paths, so its "old bank" lists can include combinations that never happen in practice. And only five jump tables are worked out automatically, so object code reached only through other tables can be missed; I added 29 entry points by hand from a disassembly. It's a list of suspects, not proof.

Layer 2: forcing the race at $4BFB

Here's the code around the new window. It runs every frame, just before the lookup that $4C3C dispatches on:

4BF9  LD   A,$05
4BFB  LD   ($FFFF),A      ; step 1: slot 2 = bank 5 (level data)
4BFE  LD   ($D230),A      ; step 2: shadow = 5      <-- window
 ...                      ; find the block under Sonic
4C17  LD   A,($D2D5)      ; zone number
4C1A  ADD  A,A
4C1D  LD   BC,$A200       ; per-zone pointers to the block-type tables
4C20  ADD  HL,BC
4C21  LD   A,(HL)         ; read the pointer   (from slot 2)
4C23  LD   H,(HL)
4C25  ADD  HL,DE          ; + block number
4C26  ADD  HL,BC
4C27  LD   A,(HL)         ; read the block type (from slot 2)
4C28  CP   $1D            ; types $1D and up: no routine
4C2A  JR   NC,$4C44

Before $4BFB, slot 2 holds bank 2. If the interrupt splits this pair, the lookup reads bank 2's bytes at $A200, both the zone pointer and the type. The pointer is nonsense, so the address wraps around 64 KB and the "type" comes from somewhere in banks 0-2. The key point is that the result is fixed. It depends only on the zone and the block number, never on timing. Every block in the game has a known "wrong" type, and the scanner can work them all out straight from the ROM.

Diagram: in Sky Base 1, block 0 (open air) is type 0 normally, but type 5 (spring right) when read from bank 2
The same block, read from two banks. In Sky Base, block 0 is plain open air. With bank 2 left in slot 2 it becomes type $05, the sideways spring.

What each type does, from the routine table at $5BEB (IX points at Sonic's data):

TypeRoutine does
$00nothing
$01, $17, $1Churt Sonic ($17/$1C on a timer)
$02ramp: launches him upward from his speed if he's fast
$03 / $05sideways spring: x speed = -8 / +8 px per frame (right / left half of the block)
$04, $09spring up, y speed -12, only on the ground
$06 / $07conveyor belt -1.5 / +2
$08, $0Cwater flag ($0C also pushes)
$0Bteleporter check
$0D / $0E, $0F / $10water current +5 / +6, -5 / -6 px per frame, d-pad locked
$11flip vertical speed (bounce)
$12 / $13 / $14spring up -10 / -12 / -14 with no ground check: works in mid-air
$15, $16bumpers
$18-$1Abridge blocks
$1Bclears the buttons
$1D and upno routine at all

To check the prediction in a real game, I forced the race in the debugger:

  1. Set a breakpoint at $4BFB.
  2. When it hits, set register A to 2 and step one instruction, so the mapper gets the old bank, exactly as if the interrupt had restored it.
  3. Set A back to 5, so the shadow gets the new bank, and let the game run.

I tried it twice:

  • Sky Base 1, in the open air after the signpost: the dispatch went to $57FD (type $05) as predicted, and Sonic moved 8 px right in one frame.
  • Green Hill 1, rolling right at 6.1 px per frame with Down held: his speed jumped to 7.98 px per frame straight away. After 1, 5 and 10 frames he was at x 2275, 2299 and 2331, against 2273, 2292 and 2316 in the normal run, and still 1.9 px per frame faster. The two runs then go their own ways.
Side-by-side animation of Green Hill 1: the normal run and the same moment with the race forced, where Sonic gains speed and the runs diverge
Green Hill 1, same buttons, recorded from the debugger at one-third speed. Left: the game as it normally runs. Right: the $4BFB race forced on the first frame. One frame of the wrong bank, and the runs never meet again.

Every place in every act

Since the wrong type depends only on the block, the whole game can be mapped without playing it. For each act, the tool:

  1. Takes the full 4 KB block layout from RAM ($C000) while replaying the solver's route. It keeps every distinct layout, so Scrap Brain 2's three areas are all there.
  2. Works out each block's wrong-bank type.
  3. Keeps the useful ones: sideways springs, mid-air springs and currents.
  4. Paints them on the act's map.

The marked square is where Sonic's check point must be, which is a little below his middle (x+8, y+16). Squares inside solid ground are marked too, but he can't get there, so look for the ones in open space. A white outline means the solver's current route passes through that square.

Act (click for the map)Sideways +8 / -8Mid-air spring -10 / -12 / -14CurrentsOn the route
Green Hill 14 / 700 / 0 / 0129
Green Hill 21 / 270 / 14 / 0212
Green Hill 313 / 120 / 0 / 0125
Bridge 174 / 29 / 8 / 005
Bridge 265 / 011 / 7 / 006
Bridge 35 / 02 / 2 / 003
Jungle 123 / 111 / 0 / 94716
Jungle 215 / 40 / 0 / 5903
Jungle 31 / 160 / 0 / 9241
Labyrinth 120 / 103 / 0 / 1110
Labyrinth 210 / 226 / 0 / 125
Labyrinth 320 / 163 / 0 / 0010
Scrap Brain 130 / 1136 / 16 / 0138
Scrap Brain 2 (area 1)11 / 1143 / 29 / 0846
Scrap Brain 2 (area 2)11 / 1750 / 33 / 083
Scrap Brain 2 (area 3)13 / 561 / 34 / 01630
Scrap Brain 325 / 433 / 13 / 01027
Sky Base 12994 / 00 / 0 / 14441
Sky Base 23214 / 14 / 3 / 0269
Counts are 32×32 blocks whose wrong-bank type gives that effect. Green Hill 1 shows its main layout; Sky Base 3, the final boss, has none.

Three things stand out:

  • Sky Base 1 and 2: the sky itself is a spring. Block 0, empty air, has wrong type $05. A race almost anywhere Sonic is in the air sets his speed to +8 px per frame.
  • Scrap Brain is full of mid-air springs (-10 and -12), and the solver's routes pass through many of them.
  • Jungle has the strongest launch, -14, in 59-94 blocks per act.

Water currents turn up well outside Labyrinth too, for example 26 blocks in Sky Base 2.

Sky Base 1 map with the squares where a $4BFB race would give a spring or current
Sky Base 1: almost all the open air (orange) would be a +8 sideways spring. Purple: mid-air -14 springs. Click for full size.
Scrap Brain 2, first area map with the squares where a $4BFB race would give a spring or current
Scrap Brain 2, first area: mid-air springs (blue) all over, many on the route. Click for full size.
Scrap Brain 1 map with the squares where a $4BFB race would give a spring or current
Scrap Brain 1: 52 mid-air springs (blue), and 38 marked squares on the solver's route. Click for full size.
Jungle 1 map with the squares where a $4BFB race would give a spring or current
Jungle 1: 94 squares of -14 mid-air spring (purple). Click for full size.
Green Hill 1 map with the squares where a $4BFB race would give a spring or current
Green Hill 1: mostly -8 sideways springs (yellow), plus currents. Click for full size.
Labyrinth 1, home of the x-jump. map with the squares where a $4BFB race would give a spring or current
Labyrinth 1, home of the x-jump. Click for full size.

How likely is any of this?

Every map square needs three things at once:

  1. Sonic is in that square on a game-logic frame.
  2. That frame runs late.
  3. The interrupt arrives in the 13-cycle window.

Along the solver's current routes, only 0-68 useful moments per act are followed by a late frame. Scrap Brain 2 has the most. Even then, the Labyrinth test suggests about 1 in 800 of those will land in the window.

But "1 in 800" is an average over random play. The game is completely deterministic: the same presses on the same frames give the same result every time. So for any given route, the race either happens or it doesn't, and changing the route a little moves the end of Sonic's block check against the interrupt. That's the idea behind layer 3: search for presses that put a late frame, the window and a useful square together on purpose.

Two cautions. First, all of this depends on cycle-exact timing, and an emulator is only as exact as its model of the hardware. Gearsystem is very good, but I haven't tried any of this on a real Game Gear, and a different emulator may never see it. Second, a speedrun built on it would need that exact timing too.

What's next

  • Layer 3: reach it. Start with Scrap Brain, where the routes already cross the most usable squares, and search for presses that make the race happen there. Then measure whether a one-frame spring actually saves time.
  • The slot-1 windows. Check whether any of the other 13 ever run during play, or only while a level loads.
  • The solver stays blind. None of this is fed to the solver. If it ever stumbles into one of these races by itself, that's fair game, and the impossible-move detector will catch it.

How it was done

Everything was done with Claude Code, working alongside me:

  • Bulk tests: the Gearsystem emulator core, driven from Python.
  • Investigation and forced races: the Gearsystem 3.9 desktop debugger, through its MCP server. It handled breakpoints, single steps, register writes and the recordings above.
  • Layer 1: a Python Z80 decoder and scanner of about 900 lines.
  • Labyrinth 1 test: 200,000 tries of the emulator core, run in parallel.

The cartridge image is a legally owned copy and is never shared.

All images and animations in this post © Peter J Mayhew. Related on this blog: Teaching a Team of Computers to Speedrun Sonic and Sega Game Gear Sonic the Hedgehog level select. Sonic the Hedgehog is a trademark of SEGA. The Game Gear version was developed by Ancient. This is a fan research project and isn't affiliated with or endorsed by SEGA.

← All articles

Related articles