Prev: 4000 Up: Map Next: 5CB0
5B80: Decryption Routine
The stub that the tape loader runs after loading the game, which arrives scrambled at 6000 (8800 bytes of it). It unscrambles the game in place, moves it down by 0200 bytes so that it starts at GameEntry, and jumps there.
Each pair of bytes is unscrambled by a single RLD: the low nibble of the first byte of the pair is replaced by the high nibble of the second byte, the high nibble of the second byte by its low nibble, and the low nibble of the second byte by the old low nibble of the first byte.
The routine also sets bit 7 of the memory refresh register R. Nothing in the game ever clears that bit, and the game resets the computer if it is clear when the check at BuildRandomTable runs, which defeats any attempt to start the game without going through this stub. The random number generator at RandomNumber also reads R, so that bit keeps those values in the range 80 to FF.
Set bit 7 of the memory refresh register for the random number generator.
Decryption 5B80 DI Disable interrupts.
5B81 LD A,R Fetch the memory refresh register into A.
5B83 OR %10000000 Set bit 7. The Z80 leaves that bit as it is written, so RandomNumber always mixes a value from 80 to FF into the random number.
5B85 LD R,A Write A back to the memory refresh register.
Unscramble the whole game, a pair of bytes at a time.
5B87 LD DE,$6000 Set DE to 6000, the first byte of the first pair.
5B8A LD HL,$6001 Set HL to 6001, the second byte of the first pair.
5B8D LD BC,$4400 Set a pair counter in BC to 4400.
Decryption_Loop 5B90 LD A,(DE) Fetch the first byte of the pair.
5B91 RLD Rotate the nibbles of the pair.
5B93 LD (DE),A Write the unscrambled first byte back.
5B94 INC DE Move DE on to the first byte of the next pair.
5B95 INC DE
5B96 INC HL Move HL on to the second byte of the next pair.
5B97 INC HL
5B98 DEC BC Decrease the pair counter by one.
5B99 LD A,B Jump to Decryption_Loop until every pair has been unscrambled.
5B9A OR C
5B9B JR NZ,Decryption_Loop
Move the unscrambled game down so that it starts at GameEntry.
5B9D LD HL,$6000 Move 8800 bytes from 6000 down to GameEntry.
5BA0 LD DE,$5E00
5BA3 LD BC,$8800
5BA6 LDIR
5BA8 JP GameEntry Jump to GameEntry to start the game.
View the equivalent code in;
Prev: 4000 Up: Map Next: 5CB0