Alan McNeil provides an example of the Frenzy code here- http://a9k.net/

CLICK to see all of the mazes created by the game of Berzerk.

Disclaimer- the author is not a programmer, so I have relied on the legendary MAME developer, Sean Riddle, for design assistance with most technical projects you see on the Guidebook site.

  • Two random #s are generated to create a room # to start in, then 16 random #s are generated, 8 of which determine the walls for that room (not sure what the other 8 are used for).
  • Everytime I “cold-started” Berzerk in MAME, the initial seed was $0000, which means the first room was the same.
  • Take the room coordinates and load them as the seed number, Add the number to itself 7 times, Add hex value $3153 to the number, store the value and repeat the process
  • So from Alan’s description of the random # generator, A=7 and B=$3153; a new random # is generated from seed x 7 + $3153. repeated to obtain values for all 8 pillars of the room
  • the random # is actually the high 8 bits of the seed, and the wall direction is the low 2 bits of that.
  • To make the arm directions from the pillars- the random value is  AND’ed with $03 (leaving just the low 2 bits), then creates the directional branches/arms depending on whether the results are %00, %01, %10 or %11

If you reset the game and add a credit and start a game as soon as possible, up until the maze is drawn, 18 random numbers are generated.

  • The sequence starting room 0000- 3153- 8a98- first pillar fb7b- 11b0- second pillar ad23- ed48- 3rd pillar ae4b- f560- 4th pillar e6f3- 81f8- 5th pillar bf1b- 6b10- 6th pillar 1ec3- 08a8- 7th pillar 6deb- 32c0- 8th pillar 9493 (0 1 2 3 1 1 1 2 1 2 1 3 3 2 0 1 2 0) results in walls WSEE WESN.
  • Putting a breakpoint at $25f8 (see Disassembly diagram below) lets us look at the seed and the resulting direction:  fb7b (3=W) ad23 (1=S) ae4b (2=E) e6f3(2=E)  bf1b (3=W) 1ec3 (2=E) 6deb (1=S) 9493 (0=N) – which matches the walls!

BerzerkFirstMazeExample

How to calculate this manually from your own computer

  • Windows Calculator>check Hex>check Word
  • Room number $0000 * 7 = $0000
  • $0000 + $3153 = $3153
  • $3153 * 7 = $5945
  • $5945 + $3153 = $8A98
  • $8A98 * 7 =  $CA28
  • $CA28 + $3153 =  $FB7B
  • the first pillar value!
  • Check Bin to convert FB7B to Binary
  • 111110–>11<–01111011
  • Pick out the 1st and 2nd bit of the upper 8
  • (00 is N, 01 is S, 10 is E, 11 is W)
  • Pillar 1 arm goes West
  • Check Hex to return calculator to Hexadecimal
  • $FB7B * 7 = $E05D
  • $E05D + $3153 = $11B0
  • $11B0 * 7 = $7BD0
  • $7BD0 + $3153 = $AD23
  • The second pillar value!
  • Check Bin to convert AD23 to Binary
  • 101011–>01<–00100011
  • Pillar 2 arm goes South

BerzerkCodeFromSean

Passing it on-  Teaching Maze Generation to Students

  • Across the country we are seeing after-school groups of kids get together to learn game design from dedicated adults in the programming profession.  I could see Berzerk being a great show case of hands-on design and manual programming math lesson.
  • Given the exercise how could a student go about making a maze based on a Hex number?
  • Alan- “The code for all this is up in the frenzy site – Z80 assembler.
  • You have two choices on the kids making their own maze from a hex number
  • a) do it how it should work – use a modern programming language and seed the random number generator with the hex YX.
  • b) deconstruct the frenzy assembly language – that version COULD be done on paper because the random number generator was so light weight. Just a bunch of adds and shifts.

Frenzy source code on Alan’s web site and in demo.asm:

  • ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • ; Random Number Generator
  • ;————————————–
  • RANDOM::
  • push h
  • lhld Seed
  • mov d,h
  • mov e,l
  • dad h
  • dad d
  • dad h
  • dad d
  • lxi d,3153H
  • dad d
  • shld Seed
  • mov a,h
  • pop h
  • ret

Which means the following in 8080/Z80 language

  • preserve register HL,
  • load the seed into register HL,
  • then put that into register DE.
  • Add HL to itself (doubling the initial #),
  • then add DE (tripling it),
  • then add itself again (6 times the original #),
  • then add DE again (7 times the original #).
  • Then add $3153 and store the result back in seed.
  • Put the top 8 bits in register A as the random #,
  • then restore register HL
  • and return.
  • So from Alan’s description of the random # generator, A=7 and B=$3153; a new random # is generated from seed x 7 + $3153.

Simplified Overview of the Maze Generator

Back to the Berzerk main page HERE


Copyright © 2012. All Rights Reserved.