19

The objective of Dig Dug is to eliminate underground monsters by inflating them until they pop, or by squashing them with large rocks. Once all of the monsters have escaped or been defeated, the level progresses.

Once the level number reaches 256, however, a monster appears precisely on Dig Dug's spawn point, no tunnels appear, the rocks are arranged strangely and the player loses all of the remaining lives almost instantly.

Why did the developers program an impossible level into the game?

  • 4
    Perhaps someone can chime in with a more specific answer, but the general reason is that the programmer never expected anyone to get that far, and contains a machine-language loop equivalent to something like unsigned char i=level; if (i > 100) i=100; do {...} while(--i); and didn't expect that the loop would ever run more than 100 times, even though it runs 256 times if level is zero. That's what caused the Pac-Man kill-screen, and something similar could be at play here. – supercat Nov 1 '16 at 23:51
  • 1
    @supercat This glitch is caused by something different; I'll see if I can get an answer posted today. – wizzwizz4 Nov 2 '16 at 7:11
  • The principle seems to be the same, but without the loop: code checks if a values is greater than X, clips it if so, and then decrements it regardless. For Pac-Man, the decrement feeds a loop which runs N more times, but the key point is pegging a value before a decrement. – supercat Nov 2 '16 at 14:45
  • 3
    As for why programmers didn't do anything about that bug, I'd say that as far as the customers are concerned the real bug isn't that the game kills the player on level 256, but rather that it doesn't kill the player off sooner. – supercat Nov 2 '16 at 14:46
28

Dig Dug stores the positions of objects in tables in memory. There are two tables for this: an enemy table containing the positions of up to four Pookas and four Fygars, and a rock table containing the positions of up to six rocks. There is also a table containing tunnel data, but this is stored in a different way.

The game divides the playable area into this grid:

Dig Dug Memory Grid
Image sourced from DonHodges.com

This image shows the mapping between on-screen positions and internal representations. These "position numbers" are stored in the enemy and rock tables to show where to place the enemies and rocks. For example, the enemy table for level 1 might look like this:

32 3C DA 00    00 B4 00 00

and the rock table might look like this:

00 45 AB C4 00 00

The enemy table is eight bytes long, but is divided into two sections of four bytes each, for the Pookas and Fygars respectively. The byte value 00 is used for enemies that should not be placed.

There are fifteen different levels in Dig Dug. These are stored in sequential tables, like a C array. There is a piece of code designed to limit the level value to between 00 and 0e, which is as follows:

  1. LD A,($8627): Load the round number into A.
  2. CP 16: Is A < 16?
  3. JR C,$1BE9: If yes, go to instruction 6.
  4. AND #00000011: Mask A with 00000011, performing modulo 4.
  5. ADD A,12: Add 12 to A, bringing it to between 12 and 15 (inclusive).
  6. DEC A: Subtract 1 from A.
  7. RET: Return.

This value is then used to calculate the offset of the current level's data. But what happens if we put 0 into it?

  1. LD A,($8627): Load 0 into A.
  2. CP 16: Is A < 16?
  3. JR C,$1BE9: Yes, so go to instruction 6.
  4. (skipped because of jump)
  5. (skipped because of jump)
  6. DEC A: Subtract 1 from A, underflowing to 255.
  7. RET: Return.

With an offset of 255 the game vastly overshoots the tables, setting and using what is known as a "garbage pointer". The game then interprets code and other memory values as a layout, which coincidently results in two enemies being placed on Dig Dug's starting position, rendering the level impossible.

  • 1
    Hey wizzwizz4, I approved Cactus' suggested edit to your answer because I believe it makes the assembly clearer. I know I just brought up a question about suggested edits on meta, but I don't feel like this puts words in your mouth. If you have any questions or feel as if my approval was wrong, feel free to ping me. Thanks. – JAL Nov 2 '16 at 13:08
  • 1
    I think your approval was correct; this edit makes the answer much clearer. (Thanks also to @Cactus for suggesting it in the first place!) – wizzwizz4 Nov 2 '16 at 16:39
  • 1
    This was an absolutely excellent answer. Well done! – Geo... Nov 2 '16 at 19:47
  • and this is the same style of bug that Pac-Man has. – peter ferrie Feb 12 '17 at 4:15
  • @peterferrie It's not quite as dramatic, but yes it is. – wizzwizz4 Mar 7 '18 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.