# Prince of Persia Password Generator (C#)

This version produces passwords for the Sega Master System and Super Nintendo versions of Prince of Persia. I will explain how the Master System version works first because it is simpler and much easier to explain.

The password is made up of six characters ranging from A to Z. Each letter is assigned a number in sequential order, as shown in the table below.

• I will explain the 5th character first because it affects the rest of the password. This character serves as the password’s baseline value. All other values in the password are added from this value upwards. In the example above, it is set to N, which represents the value of 13.
• The first character represents the level number. The first level is zero, so you need to subtract 1 to get the level you want to go to. The 9th level of the game is given the value of 8, so you need to add 8 to the baseline value (13 in this case) to get the final letter. 8 + 13 = 21 (V).
• N.B. If any of these password characters go above 25, perform a modulo of 26 to the value (which means to divide the value by 26 and keeping the remainder). So if for example the character’s final value ended up being 27, 27 mod 26 = 1, which would make the final character B.
• The second and third characters represent the amount of time remaining, in tens and units respectively. 27 is made up of 2 tenths and 7 units. That means that the second character is 2 + 13 = 15 (P), and the third character is 7 + 13 = 20 (U).
• The fourth character is the maximum health of the Prince. Here, he has six health, so the fourth character is 6 + 13 = 19 (T).

So that just leaves the 6th character. This is a verification character to ensure that the password is genuine. In order to get the correct value, you must perform the following steps:

• Add all the values we’ve gathered so far (VPUTN = 21 + 15 + 20 + 19 + 13 = 88).
• Add 10 to this value (88 + 10 = 98).
• Perform a modulo of 26 to this value (98 / 26 = 3 remainder 20, therefore 98 mod 26 = 20).
• This is the final character in the password (20 = U).

This is more difficult to explain. In order to understand what is happening here requires knowledge of binary numbers and bitwise operations, including XOR.

The password for the SNES version is made up of seven characters. In the table below, each row represents a byte that will eventually become the seven characters of the password.

The three most significant bits in each byte are not used in generating the password, so we can set them to zero and ignore them from here onward.

The first step is to take the level you want to go to. Take the value in the level box and subtract it by 1. In this example, we want to go to level 4 (which becomes 4 – 1 = 3). 3 in binary is 00011, so stitch this value into the password as follows (remember that bit 0 is the least significant bit):

The next step is your health. 15 is the maximum amount of health you can get, so only 4 bits are needed to represent this value. 7 in binary is 0111, and needs to be inserted into the password in the following way (notice that the direction of the bits is going the opposite direction to the level number):

Now comes a tricky bit: the time. The passwords in this game are accurate all the way down to the frame. The game runs at 7FPS, meaning the password changes 7 times per second. I thought it would be too much to give the user the option to set the frame number, so I’ve omitted it from the tool, and the player will get the full second in the final password.

The time and seconds shown in the game and in the tool are how much time the Prince has left to reach the end. However, the password actually wants the amount of time that has passed since the beginning of the game. Furthermore, five frames are dropped from the password every minute. This is starting to get really complicated now, so I am going to convert the time remaining to the final frame counter step by step.

• The values in the tool are given the variable names minutesRemaining and secondsRemaining. The Prince has 120 minutes to reach the Princess.
• The constants mentioned above are FRAMES_PER_SECOND = 7, and FRAMES_LOST_PER_MINUTE = 5.
• framesElapsedInCurrentMinute = (((60 – secondsRemaining) mod 60) * FRAMES_PER_SECOND).
• framesElapsedInCurrentMinute = (((60 – 12) mod 60) * 7)
• framesElapsedInCurrentMinute = ((48 mod 60) * 7)
• framesElapsedInCurrentMinute = (48 * 7)
• framesElapsedInCurrentMinute = 336
• minutesElapsed = (119 – minutesRemaining). If (minutesElapsed < 0), set (minutesElapsed = 0). If (framesElapsedInCurrentMinute == 0), (minutesElapsed++)
• minutesElapsed = (119 – 106)
• minutesElapsed = 13
• framesElapsedInMinutes = ((60 * FRAMES_PER_SECOND + FRAMES_LOST_PER_MINUTE) * minutesElapsed)
• framesElapsedInMinutes = ((60 * 7 + 5) * 13)
• framesElapsedInMinutes = (425 * 13)
• framesElapsedInMinutes = 5,525
• totalFramesElapsed = (framesElapsedInMinutes + framesElapsedInCurrentMinute)
• totalFramesElapsed = (5,525 + 336)
• totalFramesElapsed = 5,861

Hopefully I haven’t lost you at this point. It might take a few tries to understand what is happening there. Ultimately, what we want are the number of frames the game has run, plus five frames for each minute that has elapsed. I don’t know why it is like this, but this is what is necessary to create accurate passwords in this game.

So, now that we have our final frame counter, we need to convert it into a 16-bit binary number and stitch it into the password. 5,861 in binary is 0001 0110 1110 0101. It is implemented into the password as follows.

The fundamentals of the password are now in place. It’s time to take a snapshot of what the current password looks like at this point.

We need to keep hold of these values because there are two tests that the password needs to pass before it becomes valid. I call them the Add Checksum and the XOR Checksum. I will go through these in that order.

The number needed to pass the Add Checksum is obtained by adding all of the values together, then modulo 32 (or if you prefer bitwise functions, feel free to AND 31 instead of modulo 32. It is faster and will give you the same result).

• ADD = (11 + 13 + 7 + 4 + 1 + 10 + 0) mod 32
• ADD = 46 mod 32