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.
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:
| Addresses | What the Z80 sees | Switched by writing to |
|---|---|---|
| $0000-$3FFF | slot 0: bank 0 (start-up code, interrupt handler) | fixed in Sonic |
| $4000-$7FFF | slot 1: usually bank 1 (main game code) | $FFFE |
| $8000-$BFFF | slot 2: whatever bank the code needs right now (level data, object code, music) | $FFFF |
| $C000-$DFFF | 8 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.

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.

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.

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),Hwrites $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.

- 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:
- Read the program. Find every place an interrupt could split something that should have been done in one go.
- Force it. For each candidate, make the interrupt happen there in the debugger, and record what changes.
- 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:

- 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.

What each type does, from the routine table at $5BEB (IX points at Sonic's data):
| Type | Routine does |
|---|---|
| $00 | nothing |
| $01, $17, $1C | hurt Sonic ($17/$1C on a timer) |
| $02 | ramp: launches him upward from his speed if he's fast |
| $03 / $05 | sideways spring: x speed = -8 / +8 px per frame (right / left half of the block) |
| $04, $09 | spring up, y speed -12, only on the ground |
| $06 / $07 | conveyor belt -1.5 / +2 |
| $08, $0C | water flag ($0C also pushes) |
| $0B | teleporter check |
| $0D / $0E, $0F / $10 | water current +5 / +6, -5 / -6 px per frame, d-pad locked |
| $11 | flip vertical speed (bounce) |
| $12 / $13 / $14 | spring up -10 / -12 / -14 with no ground check: works in mid-air |
| $15, $16 | bumpers |
| $18-$1A | bridge blocks |
| $1B | clears the buttons |
| $1D and up | no routine at all |
To check the prediction in a real game, I forced the race in the debugger:
- Set a breakpoint at $4BFB.
- 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.
- 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.

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:
- 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.
- Works out each block's wrong-bank type.
- Keeps the useful ones: sideways springs, mid-air springs and currents.
- 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 / -8 | Mid-air spring -10 / -12 / -14 | Currents | On the route |
|---|---|---|---|---|
| Green Hill 1 | 4 / 70 | 0 / 0 / 0 | 12 | 9 |
| Green Hill 2 | 1 / 27 | 0 / 14 / 0 | 2 | 12 |
| Green Hill 3 | 13 / 12 | 0 / 0 / 0 | 12 | 5 |
| Bridge 1 | 74 / 2 | 9 / 8 / 0 | 0 | 5 |
| Bridge 2 | 65 / 0 | 11 / 7 / 0 | 0 | 6 |
| Bridge 3 | 5 / 0 | 2 / 2 / 0 | 0 | 3 |
| Jungle 1 | 23 / 11 | 1 / 0 / 94 | 7 | 16 |
| Jungle 2 | 15 / 4 | 0 / 0 / 59 | 0 | 3 |
| Jungle 3 | 1 / 16 | 0 / 0 / 92 | 4 | 1 |
| Labyrinth 1 | 20 / 10 | 3 / 0 / 1 | 1 | 10 |
| Labyrinth 2 | 10 / 22 | 6 / 0 / 1 | 2 | 5 |
| Labyrinth 3 | 20 / 16 | 3 / 0 / 0 | 0 | 10 |
| Scrap Brain 1 | 30 / 11 | 36 / 16 / 0 | 1 | 38 |
| Scrap Brain 2 (area 1) | 11 / 11 | 43 / 29 / 0 | 8 | 46 |
| Scrap Brain 2 (area 2) | 11 / 17 | 50 / 33 / 0 | 8 | 3 |
| Scrap Brain 2 (area 3) | 13 / 5 | 61 / 34 / 0 | 16 | 30 |
| Scrap Brain 3 | 25 / 4 | 33 / 13 / 0 | 10 | 27 |
| Sky Base 1 | 2994 / 0 | 0 / 0 / 14 | 4 | 41 |
| Sky Base 2 | 3214 / 1 | 4 / 3 / 0 | 26 | 9 |
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.






How likely is any of this?
Every map square needs three things at once:
- Sonic is in that square on a game-logic frame.
- That frame runs late.
- 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.

