43

Pac-man is a game where you navigate a small yellow circle around a maze, collecting dots and fruit whilst avoiding brightly-coloured monsters, or ghosts. As long as there was at least one 'man left, the game would play forever.

Except... it doesn't. After a few hundred levels, the level suddenly turns into this:

A big glitchy mess on the right-hand side of a Pac-man level

Why would such a dramatic glitch occur?

user3840170
  • 23,072
  • 4
  • 91
  • 150
wizzwizz4
  • 18,543
  • 10
  • 78
  • 144

1 Answers1

58

There are only seven fruit in Pac-man. The way the game calculates the number of fruit to draw is as follows:

  1. LD A,(#4E13): Load the level number (at memory address 0x4E13) into A.
  2. INC A: Increment A.
  3. CP #08: Is A < 8?
  4. JP NC,#2C2E: If not, jump to large-number fruit handling code.
  5. LD DE,#3B08: Load the address of the cherry into DE.
  6. Start the fruit-drawing loop.

(The assembly code is reproduced and explained in detail here, which also includes a patch to remove the glitch.)

This code works fine if the level number is 1, 2, 3, 4... even if it's 0! But if the level number is 255, the implementation's main flaw is exposed:

  1. LD A,(#4E13): Load 255 into A
  2. INC A: Increment 255 to 256 - this overflows to 0
  3. CP #08: Is 0 less than 8? Yes.
  4. JP NC,#2C2E: Don't jump to large-number fruit handling code
  5. LD DE,#3B08: Load the address of the cherry into DE
  6. Start the fruit-drawing loop

The fruit-drawing loop will then proceed to draw a fruit, subtract from the counter, check if it's equal to 0... but subtracting anything from 0 will cause it to underflow to 255. And 255 is not equal to 0.

The loop will dutifully draw fruit on the screen repeatedly until it has finished, by which time it will have drawn "level number + 1" fruit, which is fine when the level number is less than eight, but not when it is 255.

This attempt to draw 256 fruit on the screen, overwriting many other items - such as the precious Pac-dots required to complete the level - whilst doing so, is the cause of this dramatic glitch. No matter what the player does, the "Pac-dots collected" count will never be large enough, so the game cannot progress and ends here. This is why the kill-screen glitch occurs, and the final level of Pac-man will always be impossible to complete!

wizzwizz4
  • 18,543
  • 10
  • 78
  • 144
  • I plan to add more information about the assembly and algorithm specifics soon, but have not got time today. – wizzwizz4 Oct 31 '16 at 22:06
  • 1
    Why didn't the developer consider the 0 case? – user64742 Nov 01 '16 at 02:08
  • 2
    Perhaps he thought the player never would complete so many levels. In any case, If you start at one and always count upwards, it's easy to forget that you wind up and 0 eventually again. – Omar and Lorraine Nov 01 '16 at 07:09
  • @TheGreatDuck It might also very well be that this approached saved on resources. Programmers didn't think of "how to make this the most obviously correct solution" back then - rather, it was more like "how to squeeze this to as little memory as possible" and "how to make this run as fast as possible". Saving one byte in instruction code was done all the time. Also, don't forget that this simply means that level 255 is unplayable - you didn't lose an "infinite game" through this glitch - just one (non-existent) level. Without the glitch, you would just start over from zero again. – Luaan Nov 01 '16 at 12:59
  • 7
    @TheGreatDuck I'd say they programmer(s) simply made a mistake, which is exactly what a bug is. I don't know too much about the history of this one, but I'm guessing it was too difficult to ship an update for the bug which may have only been found decades after release by which time it was considered a nice little achievement and probably no longer serviced. – Arkiliknam Nov 01 '16 at 15:35
  • I also explained it here, you might like some images I added there: https://tcrf.net/Bugs:Pac-Man_(Arcade) It also explains why it actually distorts the right "half" of the maze and not other places. – Ray Nov 01 '16 at 17:57
  • I moved have the thread of comments (>2 replies deep) to chat. Please continue this discussion there. – wizzwizz4 Nov 02 '16 at 07:07
  • If the cause of the glitch is the game repeatedly drawing fruit...then why isn't the right side of the screen covered in fruit? – Kyralessa Apr 13 '17 at 12:08
  • @Kyralessa See Ray Koopa's comment and the linked TCRF page. – wizzwizz4 Apr 13 '17 at 12:16
  • 1
    @wizzwizz4, ah, got it. So it's not so much that it tries to draw 256 fruits, as that it tries to draw fruit 256 times, runs out of fruit items to draw, and picks up other items to draw instead. – Kyralessa Apr 14 '17 at 06:41
  • @Kyralessa As far as it's concerned, those are fruit items - everything pointed to by memory after the start of the fruit list is a fruit item, regardless of any other meaning it might also have. – wizzwizz4 Apr 14 '17 at 08:28
  • 1
    Here's an excellent video explaining the glitch in detail too https://youtu.be/NKKfW8X9uYk – 640KB Jul 16 '22 at 19:31