Sherlock: annotated disassembly

Routines

Prev: B16D Up: Map Next: B2DF
B191: Match the typed command against the verb patterns
Clears the object ids ($A034, $A035), then asks B39D to match the parsed words against the verb patterns at 8259. On a match, IX points at the matching 8-byte pattern; the loop at B1BA turns that into the verb number by counting 8-byte steps back to 8259, and stores it in $A033.
The line comments were written from reading the code and the descriptions of the routines it calls; the counters ($B170 direct, $B171 indirect) and the saves at $B184/B185 are clear, but which message each failure path finally prints was not run for every path.
How it works:
1. Set both object numbers ($A034, $A035) to $FF and clear the 17 bytes of the parser's work space at B16D.
2. Match the parsed words against the verb patterns (B39D). If none matches, return Z with HL = B84C, the routine that says the verb is not known.
3. Turn the matching record's address into a verb number (records are 8 bytes from 8259, counting from 1) and keep it in $A033, with the record's address at $B18E.
4. For verb 46 (TALK TO) swap the six bytes between A002 and the object slot, as bit 0 of B16D says.
5. Copy the typed objects into the two slots in the verb's order (B405), and count the candidates for each (B308, B32D).
6. Ask which one is meant (B6AE) if there are too many, or none if there are too few; otherwise choose the direct and indirect objects and return NZ.
Input
IY The first parse node
Output
F Z with HL = $B84C when no verb matches; NZ when one does
$A033 The verb number
A034 The direct object, A035 the indirect object
IX The verb record that matched
B191 LD A,$FF Load A with $FF - No direct object and no indirect object yet ($FF)
B193 LD ($A035),A Store A in $A035 in "Game variables"
B196 LD ($A034),A ($A034 = direct, $A035 = indirect)
B199 LD HL,$B16D HL = the parser's work space (B16D)
B19C LD B,$11 17 bytes of it
B19E CALL $A2AB Clear them (A2AB)
B1A1 CALL $B39D Match the words against the verb patterns (B39D)
B1A4 JR NZ,$B1AB Matched: carry on
B1A6 LD HL,$B84C
B1A9 XOR A ...and Z to say so
B1AA RET Return to the caller
B1AB LD A,$01 Matched: work out the verb number from IX
B1AD PUSH IX Offset of the matching pattern from 8259 (HL = IX)...
B1AF POP HL Restore HL from the stack
B1B0 LD DE,$8259 ...
B1B3 SBC HL,DE ...
B1B5 JR Z,$B1BF First record: verb 1
B1B7 LD DE,$0008 Otherwise count 8-byte steps
B1BA INC A One more verb number
B1BB SBC HL,DE Subtract DE and the carry from HL
B1BD JR NZ,$B1BA If the two values differ, go back to B1BA
B1BF LD ($A033),A Save the verb number in $A033
B1C2 LD ($B18E),IX And the pattern's address, for the questions at B5D3
B1C6 CP $2E Compare A with $2E - Verb 46 (TALK TO)?
B1C8 JR NZ,$B1DD If A differs from $2E, skip ahead to B1DD
B1CA LD HL,$B16D Point HL at "Parser variables" (B16D) - If so, copy six bytes between A002 and the object slot $B172; bit 0 of B16D picks the direction
B1CD BIT 0,(HL) Test bit 0 of the byte at "Parser variables" (B16D)
B1CF LD HL,$A002 Point HL at A002 in "Game variables"
B1D2 LD DE,$B172 Point DE at $B172 in "Parser variables"
B1D5 LD BC,$0006 Load BC with $0006
B1D8 JR Z,$B1DB If bit 0 of the byte at A002 in "Game variables" is clear, skip ahead to B1DB
B1DA EX DE,HL Swap DE and HL
B1DB LDIR Copy BC bytes from where HL points to where DE points, counting up
B1DD CALL $C493 Call the routine "Set the direct object's requirement from the parsed..." (C493) - Set the requirement codes for the direct and indirect object from the verb's flags (C493, C4B2)
B1E0 CALL $C4B2 Call the routine "Set the indirect object's requirement from the parsed..." (C4B2)
B1E3 LD A,($A01C) Read $A01C in "Game variables" into A - Did the first word carry the flag in bit 7?
B1E6 AND A Test A for zero (this leaves A as it was and clears carry)
B1E7 JR NZ,$B1F2 If A is not zero, skip ahead to B1F2
B1E9 LD A,($B39C) Read "B39D: bit 7 of the verb's first word reference" (B39C) into A - If not, take it from the saved copy at B39C...
B1EC LD ($A01C),A Store A in $A01C in "Game variables"
B1EF CALL $B34F ...and restart the direct-object search (B34F)
B1F2 LD A,($A01E) Read $A01E in "Game variables" into A - Verb flag $A01E bit 4 kept in $B190 (lets the search stop at the first candidate)
B1F5 AND $10 Isolate bit 4 of A (mask $10)
B1F7 LD ($B190),A Store A in $B190 in "Parser variables"
B1FA JR Z,$B200 If bit 4 of A was clear, skip ahead to B200 - Set: forget the $A01C flag
B1FC XOR A Clear A to zero (and the carry flag)
B1FD LD ($A01C),A Store A in $A01C in "Game variables"
B200 XOR A Clear A to zero (and the carry flag) - Clear $A00B
B201 LD ($A00B),A Store A in $A00B in "Game variables"
B204 LD A,($A01D) Read $A01D in "Game variables" into A - Does the verb take a direct or indirect object ($A01D bits 2-3)?
B207 AND $0C Keep only bits 2, 3 of A (mask $0C)
B209 JR NZ,$B213 Yes: go and find them
B20B CALL $C929 No objects needed (or all found): check the record (C929)
B20E RET NZ Passed: return NZ
B20F LD HL,$B863 Point HL at "JP C929 (check the record)" (B863) - Failed: hand back the 'check the record' stub (B863)
B212 RET Return to the caller
B213 CALL $B387 Does the verb take an indirect object, and was one given (B387)?
B216 JR NZ,$B280 NZ: go and deal with it (B280)
B218 LD A,($A01C) Read $A01C in "Game variables" into A - Was the direct object marked?
B21B AND A Test A for zero (this leaves A as it was and clears carry)
B21C JR NZ,$B224 If A is not zero, skip ahead to B224
B21E CALL $B308 If not, run the direct-object search to the end (B308)
B221 AND A Test A for zero (this leaves A as it was and clears carry) - Found none: look at the fallbacks at B259
B222 JR Z,$B259 If A is zero, jump on to B259
B224 CALL $B4C1 Next direct-object candidate (B4C1)
B227 JR Z,$B244 No more: finish
B229 CALL $C929 Is it here and fit to use (C929)?
B22C JR Z,$B224 No: try the next
B22E LD A,($A034) Read $A034 in "Game variables" into A - Yes: remember it as the one so far ($B184)
B231 LD ($B184),A Store A in $B184 in "Parser variables"
B234 LD HL,$B170 Point HL at $B170 in "Parser variables" - Count candidates in $B170
B237 INC (HL) Add one to the byte at $B170 in "Parser variables"
B238 LD A,($B190) Read $B190 in "Parser variables" into A - Stop at the first if the verb says so ($B190)
B23B AND A Test A for zero (this leaves A as it was and clears carry)
B23C JR NZ,$B244 If A is not zero, skip ahead to B244
B23E LD A,($A01C) Read $A01C in "Game variables" into A - Or if the marked-object flag is clear
B241 AND A Test A for zero (this leaves A as it was and clears carry)
B242 JR Z,$B224 If A is zero, go back to B224
B244 LD A,($B184) Read $B184 in "Parser variables" into A - Restore the first candidate into $A034
B247 LD ($A034),A Store A in $A034 in "Game variables"
B24A LD A,($B170) Read $B170 in "Parser variables" into A - How many candidates were there?
B24D AND A Test A for zero (this leaves A as it was and clears carry)
B24E JR Z,$B259 None: B259
B250 CP $01 Compare A with $01 - Exactly one: the checks at B20B
B252 JR Z,$B20B If A equals $01, go back to B20B
B254 LD HL,$B5E2 More than one: the 'too many' message (B5E2)...
B257 XOR A Set A to zero, which sets the zero flag for the caller - ...with Z
B258 RET Return to the caller
B259 LD A,($A033) Read $A033 in "Game variables" into A - No direct object found: verb TALK TO is the exception
B25C CP $2E Compare A with $2E
B25E JR NZ,$B27B Any other verb: error B65F
B260 LD A,($A01C) Read $A01C in "Game variables" into A - TALK TO with a marked object or words given: error too
B263 AND A Test A for zero (this leaves A as it was and clears carry)
B264 JR NZ,$B27B If A is not zero, skip ahead to B27B
B266 LD HL,$B16D Point HL at "Parser variables" (B16D) - (bit 0 of B16D = the slot already holds words)
B269 BIT 0,(HL) Test bit 0 of the byte at "Parser variables" (B16D)
B26B JR NZ,$B27B If bit 0 of the byte at "Parser variables" (B16D) is set, skip ahead to B27B
B26D LD HL,$0000 TALK TO with nothing said: clear the 6-byte slots...
B270 LD ($B172),HL ...at $B172,
B273 LD ($B174),HL ...$B174
B276 LD ($B176),HL ...and $B176
B279 JR $B213 and go round again
B27B LD HL,$B65F Point HL at B65F in "Ask the player which object, or what" - Error: ask what (message routine B65F), Z
B27E XOR A Set A to zero, which sets the zero flag for the caller
B27F RET Return to the caller
B280 LD A,($A01C) Read $A01C in "Game variables" into A - Indirect object case: was the slot marked?
B283 AND A Test A for zero (this leaves A as it was and clears carry)
B284 JR NZ,$B292 If A is not zero, skip ahead to B292
B286 CALL $B308 If not, run the indirect search to the end (B308)
B289 AND A Test A for zero (this leaves A as it was and clears carry) - Found none?
B28A JR Z,$B259 If A is zero, go back to B259
B28C CALL $B32D ...
B28F AND A ...
B290 JR Z,$B303 ...none: ask which (B303)
B292 CALL $B4C1 Next indirect-object candidate (the slot-2 twin of B4C1)
B295 JR Z,$B2B7 No more: finish
B297 CALL $B2DF Search for the second thing (B2DF)
B29A LD A,($B171) Read $B171 in "Parser variables" into A - Found exactly one second thing?
B29D CP $01 Compare A with $01
B29F JR NZ,$B292 No: look at the next
B2A1 LD A,($A034) Read $A034 in "Game variables" into A - Remember this direct object ($B184)
B2A4 LD ($B184),A Store A in $B184 in "Parser variables"
B2A7 LD HL,$B170 Point HL at $B170 in "Parser variables" - Count it in $B170
B2AA INC (HL) Add one to the byte at $B170 in "Parser variables"
B2AB LD A,($B190) Read $B190 in "Parser variables" into A - Stop at the first if the verb says so
B2AE AND A Test A for zero (this leaves A as it was and clears carry)
B2AF JR NZ,$B2B7 If A is not zero, skip ahead to B2B7
B2B1 LD A,($A01C) Read $A01C in "Game variables" into A - Or if the marked flag is clear
B2B4 AND A Test A for zero (this leaves A as it was and clears carry)
B2B5 JR Z,$B292 If A is zero, go back to B292
B2B7 LD A,($B184) Read $B184 in "Parser variables" into A - Restore the first into $A034
B2BA LD ($A034),A Store A in $A034 in "Game variables"
B2BD LD A,($B170) Read $B170 in "Parser variables" into A - How many direct objects?
B2C0 AND A Test A for zero (this leaves A as it was and clears carry)
B2C1 JR Z,$B259 None: B259
B2C3 CP $01 More than one?
B2C5 JR NZ,$B2DA Yes: 'too many' message
B2C7 CALL $B2DF Call the routine "Choose the indirect object: the candidate that passes..." (B2DF) - Exactly one: count the second things (B2DF)
B2CA LD A,($B171) Read $B171 in "Parser variables" into A
B2CD AND A Test A for zero (this leaves A as it was and clears carry) - None: ask which (B303)
B2CE JR Z,$B303 If A is zero, jump on to the routine "HL = B6AE (ask which one), Z" (B303)
B2D0 CP $01 Compare A with $01
B2D2 JP Z,$B20B Exactly one: check the record (B20B)
B2D5 LD HL,$B5D3 Point HL at "Ask the player which object, or what" (B5D3) - More than one: question routine B5D3
B2D8 XOR A Set A to zero, which sets the zero flag for the caller
B2D9 RET Return to the caller
B2DA LD HL,$B5E2 Point HL at B5E2 in "Ask the player which object, or what" - 'Too many' message routine B5E2, Z
B2DD XOR A Set A to zero, which sets the zero flag for the caller
B2DE RET Return to the caller
Prev: B16D Up: Map Next: B2DF