Jet Set Willy: Routine at 8ED4

| Routines

8ED4: Move Willy (2)

Used by the routine at 8DD3. This routine checks the keyboard and joystick.
Input
HL | Attribute buffer address of the left-hand cell below Willy's sprite (if Willy is not on a rope)
8ED4 | LD E,$FF | Initialise E to 0xFF (all bits set); it will be used to hold keyboard and joystick readings
8ED6 | LD A,($85D6) | Pick up the rope status indicator from 85D6
8ED9 | DEC A | Is Willy on a rope?
8EDA | BIT 7,A
8EDC | JR Z,$8EFA | Jump if so
8EDE | LD A,($85D1) | Pick up the airborne status indicator from 85D1
8EE1 | CP $0C | Has Willy just landed after falling from too great a height?
8EE3 | JP NC,$90B7 | If so, kill him
8EE6 | XOR A | Reset the airborne status indicator at 85D1 (Willy has landed safely)
8EE7 | LD ($85D1),A
8EEA | LD A,($80CD) | Pick up the attribute byte of the conveyor tile for the current room from 80CD
8EED | CP (HL) | Does the attribute byte of the left-hand cell below Willy's sprite match that of the conveyor tile?
8EEE | JR Z,$8EF4 | Jump if so
8EF0 | INC HL | Point HL at the right-hand cell below Willy's sprite
8EF1 | CP (HL) | Does the attribute byte of the right-hand cell below Willy's sprite match that of the conveyor tile?
8EF2 | JR NZ,$8EFA | Jump if not
8EF4 | LD A,($80D6) | Pick up the direction byte of the conveyor definition from 80D6 (0=left, 1=right)
8EF7 | SUB $03 | Now E=0xFD (bit 1 reset) if the conveyor is moving left, or 0xFE (bit 0 reset) if it's moving right
8EF9 | LD E,A
8EFA | LD BC,$DFFE | Read keys P-O-I-U-Y (right, left, right, left, right) into bits 0-4 of A
8EFD | IN A,(C)
8EFF | AND $1F | Set bit 5 and reset bits 6 and 7
8F01 | OR $20
8F03 | AND E | Reset bit 0 if the conveyor is moving right, or bit 1 if it's moving left
8F04 | LD E,A | Save the result in E
8F05 | LD A,($85DF) | Pick up the game mode indicator (0, 1 or 2) from 85DF
8F08 | AND $02 | Now A=1 if Willy is running to the toilet, 0 otherwise
8F0A | RRCA
8F0B | XOR E | Flip bit 0 of E if Willy is running to the toilet, forcing him to move right (unless he's jumped onto the bed, in which case bit 0 of E is now set, meaning that the conveyor does not move him, and the 'P' key has no effect; this is a bug)
8F0C | LD E,A
8F0D | LD BC,$FBFE | Read keys Q-W-E-R-T (left, right, left, right, left) into bits 0-4 of A
8F10 | IN A,(C)
8F12 | AND $1F | Keep only bits 0-4, shift them into bits 1-5, and set bit 0
8F14 | RLC A
8F16 | OR $01
8F18 | AND E | Merge this keyboard reading into bits 1-5 of E
8F19 | LD E,A
8F1A | LD B,$E7 | Read keys 1-2-3-4-5 ('5' is left) and 0-9-8-7-6 (jump, nothing, right, right, left) into bits 0-4 of A
8F1C | IN A,(C)
8F1E | RRCA | Rotate the result right and set bits 0-2 and 4-7; this ignores every key except '5' and '6' (left)
8F1F | OR $F7
8F21 | AND E | Merge this reading of the '5' and '6' keys into bit 3 of E
8F22 | LD E,A
8F23 | LD B,$EF | Read keys 0-9-8-7-6 (jump, nothing, right, right, left) into bits 0-4 of A
8F25 | IN A,(C)
8F27 | OR $FB | Set bits 0, 1 and 3-7; this ignores every key except '8' (right)
8F29 | AND E | Merge this reading of the '8' key into bit 2 of E
8F2A | LD E,A
8F2B | IN A,(C) | Read keys 0-9-8-7-6 (jump, nothing, right, right, left) into bits 0-4 of A
8F2D | RRCA | Rotate the result right and set bits 0, 1 and 3-7; this ignores every key except '7' (right)
8F2E | OR $FB
8F30 | AND E | Merge this reading of the '7' key into bit 2 of E
8F31 | LD E,A
8F32 | LD A,($85CE) | Collect the Kempston joystick indicator from 85CE
8F35 | OR A | Is the joystick connected?
8F36 | JR Z,$8F42 | Jump if not
8F38 | LD BC,$001F | Collect input from the joystick
8F3B | IN A,(C)
8F3D | AND $03 | Keep only bits 0 (right) and 1 (left) and flip them
8F3F | CPL
8F40 | AND E | Merge this reading of the joystick right and left buttons into bits 0 and 1 of E
8F41 | LD E,A
At this point, bits 0-5 in E indicate the direction in which Willy is being moved or trying to move. If bit 0, 2 or 4 is reset, Willy is being moved or trying to move right; if bit 1, 3 or 5 is reset, Willy is being moved or trying to move left.
8F42 | LD C,$00 | Initialise C to 0 (no movement)
8F44 | LD A,E | Copy the movement bits into A
8F45 | AND $2A | Keep only bits 1, 3 and 5 (the 'left' bits)
8F47 | CP $2A | Are any of these bits reset?
8F49 | JR Z,$8F51 | Jump if not
8F4B | LD C,$04 | Set bit 2 of C: Willy is moving left
8F4D | XOR A | Reset the inactivity timer at 85E0
8F4E | LD ($85E0),A
8F51 | LD A,E | Copy the movement bits into A
8F52 | AND $15 | Keep only bits 0, 2 and 4 (the 'right' bits)
8F54 | CP $15 | Are any of these bits reset?
8F56 | JR Z,$8F5E | Jump if not
8F58 | SET 3,C | Set bit 3 of C: Willy is moving right
8F5A | XOR A | Reset the inactivity timer at 85E0
8F5B | LD ($85E0),A
8F5E | LD A,($85D0) | Pick up Willy's direction and movement flags from 85D0
8F61 | ADD A,C | Point HL at the entry in the left-right movement table at 8421 that corresponds to the direction Willy is facing, and the direction in which he is being moved or trying to move
8F62 | LD C,A
8F63 | LD B,$00
8F65 | LD HL,$8421
8F68 | ADD HL,BC
8F69 | LD A,(HL) | Update Willy's direction and movement flags at 85D0 with the entry from the left-right movement table
8F6A | LD ($85D0),A
That is left-right movement taken care of. Now check the jump keys.
8F6D | LD BC,$7EFE | Read keys SHIFT-Z-X-C-V and B-N-M-SS-SPACE
8F70 | IN A,(C)
8F72 | AND $1F | Are any of these keys being pressed?
8F74 | CP $1F
8F76 | JR NZ,$8F8F | Jump if so
8F78 | LD B,$EF | Read keys 6-7-8-9-0
8F7A | IN A,(C)
8F7C | BIT 0,A | Is '0' being pressed?
8F7E | JR Z,$8F8F | Jump if so
8F80 | LD A,($85CE) | Collect the Kempston joystick indicator from 85CE
8F83 | OR A | Is the joystick connected?
8F84 | JR Z,$8FBC | Jump if not
8F86 | LD BC,$001F | Collect input from the joystick
8F89 | IN A,(C)
8F8B | BIT 4,A | Is the fire button being pressed?
8F8D | JR Z,$8FBC | Jump if not
A jump key or the fire button is being pressed. Time to make Willy jump.
8F8F | LD A,($85DF) | Pick up the game mode indicator from 85DF
8F92 | BIT 1,A | Is Willy running to the toilet?
8F94 | JR NZ,$8FBC | Jump if so
8F96 | XOR A | Initialise the jumping animation counter at 85D5 to 0
8F97 | LD ($85D5),A
8F9A | LD ($85E0),A | Reset the inactivity timer at 85E0
8F9D | INC A | Set the airborne status indicator at 85D1 to 0x01: Willy is jumping
8F9E | LD ($85D1),A
8FA1 | LD A,($85D6) | Pick up the rope status indicator from 85D6
8FA4 | DEC A | Is Willy on a rope?
8FA5 | BIT 7,A
8FA7 | JR NZ,$8FBC | Jump if not
8FA9 | LD A,$F0 | Set the rope status indicator at 85D6 to 0xF0
8FAB | LD ($85D6),A
8FAE | LD A,($85CF) | Round down Willy's y-coordinate at 85CF to the nearest multiple of 16; this might move him upwards a little, but ensures that his actual pixel y-coordinate is a multiple of 8 (making his sprite cell-aligned) before he begins the jump off the rope
8FB1 | AND $F0
8FB3 | LD ($85CF),A
8FB6 | LD HL,$85D0 | Set bit 1 at 85D0: during this jump off the rope, Willy will move in the direction he's facing
8FB9 | SET 1,(HL)
8FBB | RET |