Sherlock: annotated disassembly

Routines

Prev: A040 Up: Map Next: A0F7
A064: The main loop: one minute of game time per pass
Each pass is one minute. The clock moves on (BFD9) and is printed (BF79); then every actor in the table at 9860 whose next-turn time (+6/+7) has come takes its turn. A turn starts with CC37 (find the actor, check for orders or remarks waiting for it) and then runs plan-script steps (CC78) until one takes time ($A01F non-zero); that many minutes are added to the actor's next-turn time, and the actor's own after-turn routine, if it has one, runs (9C77). The player is an actor like the rest: the player's script step is the one that waits for a typed command.
Before the walk, if the player is in the middle of WAIT UNTIL (plan verb 50) and ENTER is pressed, the player's next-turn time is pulled back to now, so a long wait can be cut short.
The player's +6/+7 equals the minute counter (9FDD) whenever the game waits for a command, and moves on by 5 for WAIT and by 1 for TAKE LAMP or EXAMINE LAMP; Watson's and Lestrade's move on in step. The per-actor call counts in the B91F note (CC37 once per actor per turn) agree.
How it works:
1. Clear the ending flag $A0DA. If it is set on a later pass, go to the ending code at A0E7.
2. Move the clock on one minute (BFD9) and print it (BF79).
3. If the player is waiting until a time (plan verb 50) and ENTER is held down, make the player's next turn now.
4. Walk the actor table from 9860 in 8-byte steps until the $FF marker. For each actor whose time has come (A0DB): start its turn (CC37), then run plan steps (CC78) until one takes time ($A01F non-zero).
5. Add that time to the actor's next-turn time and run the actor's after-turn routine (9C77).
6. At the end of the table, loop for the next minute. When the ending flag is 2 the game is saved (B0E4) and the loop continues; otherwise it is loaded (B0D7) and the game restarts at A040.
Output
F None: the loop never ends by itself; $A0DA stops it
A064 XOR A Clear A to zero (and the carry flag) - Not ending yet
A065 LD ($A0DA),A Store A in $A0DA in "The main loop: one minute of game time per..."
A068 LD A,($A0DA) Read $A0DA in "The main loop: one minute of game time per..." into A - Is the game ending?
A06B AND A Test A for zero (this leaves A as it was and clears carry)
A06C JR NZ,$A0E7 Yes: restart or reload
A06E CALL $BFD9 A minute passes
A071 CALL $BF79 Print the clock
A074 LD IY,$9860 The player (first actor)...
A078 LD A,(IY+$03) Read byte +3 of the record at IY into A - ...waiting until a time (plan verb 50)?
A07B CP $32 Compare A with $32
A07D JR NZ,$A093 If A differs from $32, skip ahead to A093
A07F CALL $C41F Call the routine "GET-KEY: has the player pressed a key?" (C41F) - Is a key down...
A082 BIT 7,A Test bit 7 of A
A084 JR Z,$A093 If bit 7 of A is clear, skip ahead to A093
A086 CP $8D Compare A with $8D - ...and is it ENTER?
A088 JR NZ,$A093 If A differs from $8D, skip ahead to A093
A08A LD HL,($9FDD) Read minutes since the start (9FDD) into HL - Then the player acts now: next-turn time = this minute
A08D LD (IY+$06),L Store L in byte +6 of the record at IY
A090 LD (IY+$07),H Store H in byte +7 of the record at IY
A093 LD IY,$9860 Walk the actor table
A097 LD A,(IY+$00) Read the first byte of the record at IY into A - End of the table?
A09A INC A Add one to A
A09B JR Z,$A068 Yes: the next minute
A09D LD HL,($9FDD) Read minutes since the start (9FDD) into HL - Has this actor's time come?
A0A0 CALL $A0DB Call A0DB (inside "The main loop: one minute of game time per...")
A0A3 JR C,$A0D1 Not yet: next actor
A0A5 LD A,(IY+$00) Read the first byte of the record at IY into A - Start its turn
A0A8 PUSH IY Save IY on the stack
A0AA PUSH AF Save AF on the stack
A0AB CALL $CC37 Call the routine "Start an actor's turn" (CC37)
A0AE POP AF Restore AF from the stack - One plan step
A0AF PUSH AF Save AF on the stack
A0B0 CALL $CC78 Call the routine "Run one step of the current actor's plan script" (CC78)
A0B3 LD HL,($A01F) Read $A01F in "Game variables" into HL - Did it take any time?
A0B6 LD A,L Copy L into A
A0B7 OR H Set in A the bits that are set in H
A0B8 JR Z,$A0AE If the result is zero, go back to A0AE - No: another step
A0BA POP AF Restore AF from the stack
A0BB POP IY Restore IY from the stack
A0BD LD E,(IY+$06) Read byte +6 of the record at IY into E - Add the time to its next-turn time
A0C0 LD D,(IY+$07) Read byte +7 of the record at IY into D
A0C3 ADD HL,DE Add DE to HL
A0C4 LD (IY+$06),L Store L in byte +6 of the record at IY
A0C7 LD (IY+$07),H Store H in byte +7 of the record at IY
A0CA PUSH IY Save IY on the stack - Its own after-turn routine, if any
A0CC CALL $9C77 Call the routine "Run the current actor's own after-turn routine" (9C77)
A0CF POP IY Restore IY from the stack
A0D1 LD DE,$0008 Load DE with $0008, the amount that is added next - Next actor (8 bytes on)
A0D4 ADD IY,DE Add DE to IY
A0D6 JR $A097 Go back to A097
A0D8 INC C Add one to C
A0D9 OR $00 Set bits of A (mask $00)
A0DB LD E,(IY+$06) Read byte +6 of the record at IY into E - Compare the minute counter (HL) with the actor's next-turn time
A0DE LD D,(IY+$07) Read byte +7 of the record at IY into D
A0E1 LD A,H Copy H into A
A0E2 CP D Compare A with D
A0E3 RET NZ If A differs from D, return
A0E4 LD A,L Copy L into A
A0E5 CP E Compare A with E
A0E6 RET Return to the caller
A0E7 CP $02 Compare A with $02 - Ending: 2 = carry on (reloaded?), 1 = start again
A0E9 JR Z,$A0F1 If A equals $02, skip ahead to A0F1
This entry point is used by the routine at C1CB.
A0EB CALL $B0D7 Load: restore the saved game (B0D7)...
A0EE JP $A040 ...and carry on at A040
A0F1 CALL $B0E4 Save: write the game out (B0E4)...
A0F4 JP $A064 ...and carry on at A064
Prev: A040 Up: Map Next: A0F7