Jet Set Willy: Routine at 36564

| Routines

36564: Move Willy (2)

Used by the routine at 36307. 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)
36564 | LD E,255 | Initialise E to 255 (all bits set); it will be used to hold keyboard and joystick readings
36566 | LD A,(34262) | Pick up the rope status indicator from 34262
36569 | DEC A | Is Willy on a rope?
36570 | BIT 7,A
36572 | JR Z,36602 | Jump if so
36574 | LD A,(34257) | Pick up the airborne status indicator from 34257
36577 | CP 12 | Has Willy just landed after falling from too great a height?
36579 | JP NC,37047 | If so, kill him
36582 | XOR A | Reset the airborne status indicator at 34257 (Willy has landed safely)
36583 | LD (34257),A
36586 | LD A,(32973) | Pick up the attribute byte of the conveyor tile for the current room from 32973
36589 | CP (HL) | Does the attribute byte of the left-hand cell below Willy's sprite match that of the conveyor tile?
36590 | JR Z,36596 | Jump if so
36592 | INC HL | Point HL at the right-hand cell below Willy's sprite
36593 | CP (HL) | Does the attribute byte of the right-hand cell below Willy's sprite match that of the conveyor tile?
36594 | JR NZ,36602 | Jump if not
36596 | LD A,(32982) | Pick up the direction byte of the conveyor definition from 32982 (0=left, 1=right)
36599 | SUB 3 | Now E=253 (bit 1 reset) if the conveyor is moving left, or 254 (bit 0 reset) if it's moving right
36601 | LD E,A
36602 | LD BC,57342 | Read keys P-O-I-U-Y (right, left, right, left, right) into bits 0-4 of A
36605 | IN A,(C)
36607 | AND 31 | Set bit 5 and reset bits 6 and 7
36609 | OR 32
36611 | AND E | Reset bit 0 if the conveyor is moving right, or bit 1 if it's moving left
36612 | LD E,A | Save the result in E
36613 | LD A,(34271) | Pick up the game mode indicator (0, 1 or 2) from 34271
36616 | AND 2 | Now A=1 if Willy is running to the toilet, 0 otherwise
36618 | RRCA
36619 | 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)
36620 | LD E,A
36621 | LD BC,64510 | Read keys Q-W-E-R-T (left, right, left, right, left) into bits 0-4 of A
36624 | IN A,(C)
36626 | AND 31 | Keep only bits 0-4, shift them into bits 1-5, and set bit 0
36628 | RLC A
36630 | OR 1
36632 | AND E | Merge this keyboard reading into bits 1-5 of E
36633 | LD E,A
36634 | LD B,231 | 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
36636 | IN A,(C)
36638 | RRCA | Rotate the result right and set bits 0-2 and 4-7; this ignores every key except '5' and '6' (left)
36639 | OR 247
36641 | AND E | Merge this reading of the '5' and '6' keys into bit 3 of E
36642 | LD E,A
36643 | LD B,239 | Read keys 0-9-8-7-6 (jump, nothing, right, right, left) into bits 0-4 of A
36645 | IN A,(C)
36647 | OR 251 | Set bits 0, 1 and 3-7; this ignores every key except '8' (right)
36649 | AND E | Merge this reading of the '8' key into bit 2 of E
36650 | LD E,A
36651 | IN A,(C) | Read keys 0-9-8-7-6 (jump, nothing, right, right, left) into bits 0-4 of A
36653 | RRCA | Rotate the result right and set bits 0, 1 and 3-7; this ignores every key except '7' (right)
36654 | OR 251
36656 | AND E | Merge this reading of the '7' key into bit 2 of E
36657 | LD E,A
36658 | LD A,(34254) | Collect the Kempston joystick indicator from 34254
36661 | OR A | Is the joystick connected?
36662 | JR Z,36674 | Jump if not
36664 | LD BC,31 | Collect input from the joystick
36667 | IN A,(C)
36669 | AND 3 | Keep only bits 0 (right) and 1 (left) and flip them
36671 | CPL
36672 | AND E | Merge this reading of the joystick right and left buttons into bits 0 and 1 of E
36673 | 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.
36674 | LD C,0 | Initialise C to 0 (no movement)
36676 | LD A,E | Copy the movement bits into A
36677 | AND 42 | Keep only bits 1, 3 and 5 (the 'left' bits)
36679 | CP 42 | Are any of these bits reset?
36681 | JR Z,36689 | Jump if not
36683 | LD C,4 | Set bit 2 of C: Willy is moving left
36685 | XOR A | Reset the inactivity timer at 34272
36686 | LD (34272),A
36689 | LD A,E | Copy the movement bits into A
36690 | AND 21 | Keep only bits 0, 2 and 4 (the 'right' bits)
36692 | CP 21 | Are any of these bits reset?
36694 | JR Z,36702 | Jump if not
36696 | SET 3,C | Set bit 3 of C: Willy is moving right
36698 | XOR A | Reset the inactivity timer at 34272
36699 | LD (34272),A
36702 | LD A,(34256) | Pick up Willy's direction and movement flags from 34256
36705 | ADD A,C | Point HL at the entry in the left-right movement table at 33825 that corresponds to the direction Willy is facing, and the direction in which he is being moved or trying to move
36706 | LD C,A
36707 | LD B,0
36709 | LD HL,33825
36712 | ADD HL,BC
36713 | LD A,(HL) | Update Willy's direction and movement flags at 34256 with the entry from the left-right movement table
36714 | LD (34256),A
That is left-right movement taken care of. Now check the jump keys.
36717 | LD BC,32510 | Read keys SHIFT-Z-X-C-V and B-N-M-SS-SPACE
36720 | IN A,(C)
36722 | AND 31 | Are any of these keys being pressed?
36724 | CP 31
36726 | JR NZ,36751 | Jump if so
36728 | LD B,239 | Read keys 6-7-8-9-0
36730 | IN A,(C)
36732 | BIT 0,A | Is '0' being pressed?
36734 | JR Z,36751 | Jump if so
36736 | LD A,(34254) | Collect the Kempston joystick indicator from 34254
36739 | OR A | Is the joystick connected?
36740 | JR Z,36796 | Jump if not
36742 | LD BC,31 | Collect input from the joystick
36745 | IN A,(C)
36747 | BIT 4,A | Is the fire button being pressed?
36749 | JR Z,36796 | Jump if not
A jump key or the fire button is being pressed. Time to make Willy jump.
36751 | LD A,(34271) | Pick up the game mode indicator from 34271
36754 | BIT 1,A | Is Willy running to the toilet?
36756 | JR NZ,36796 | Jump if so
36758 | XOR A | Initialise the jumping animation counter at 34261 to 0
36759 | LD (34261),A
36762 | LD (34272),A | Reset the inactivity timer at 34272
36765 | INC A | Set the airborne status indicator at 34257 to 1: Willy is jumping
36766 | LD (34257),A
36769 | LD A,(34262) | Pick up the rope status indicator from 34262
36772 | DEC A | Is Willy on a rope?
36773 | BIT 7,A
36775 | JR NZ,36796 | Jump if not
36777 | LD A,240 | Set the rope status indicator at 34262 to 240
36779 | LD (34262),A
36782 | LD A,(34255) | Round down Willy's y-coordinate at 34255 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
36785 | AND 240
36787 | LD (34255),A
36790 | LD HL,34256 | Set bit 1 at 34256: during this jump off the rope, Willy will move in the direction he's facing
36793 | SET 1,(HL)
36795 | RET |