Sherlock: annotated disassembly

Routines

Prev: ACFB Up: Map Next: AE59
AD57: Count the sentence, then on to the next or finish
Bumps the sentence counter A012 (not for a comma in mode 2 of A01A); a comma ($A0) goes back to read another sentence (AABD).
EXAMINE LAMP AND YOUR ARMCHAIR and DROP LAMP AND YOUR MONEY each run as two commands, the verb carried to the second; but GIVE LAMP AND YOUR MONEY TO WATSON (and with a comma for AND) gets I SEE NOTHING THAT YOU CAN GIVE, and PUT LAMP AND YOUR MONEY IN YOUR ARMCHAIR gets WHAT ?, though GIVE YOUR MONEY TO WATSON on its own works. The backward pass at AE1E, which copies words from a later sentence into an earlier one with the same verb, never compares the last pair - see the Bugs page ('and-to').
How it works:
1. Count the sentence in A012, except for a comma in statement mode.
2. A comma ($A0) goes back to read another sentence (AABD).
3. If a line of several sentences is being worked through ($A01B set): if the second node has no verb of its own, carry its verb flag into the first node and fill the first node's empty word slots from the second (AE59). Then drop the first node of the list.
4. Go through the sentences from the start. In a statement whose third group is empty, take the previous sentence's group. A sentence with no verb is merged into the previous one (AEF1).
5. Then go backwards from the last sentence: where an earlier sentence has the same verb as a later one and no second group of its own, copy the later sentence's second group into it (AF27). So EXAMINE LAMP AND YOUR ARMCHAIR is understood as two commands with the same verb. The loop never compares the last pair; see the Bugs page.
6. Return Z: done.
Input
D The token class last read
E Flags
Output
F Z when the sentences have been tidied
AD57 LD A,($A01A) Read A01A in "Game variables" into A - Count the sentence (A012) - but not a comma in mode 2 (A01A)
AD5A DEC A Subtract one from A
AD5B LD HL,$A012 Point HL at A012 in "Game variables"
AD5E DEC A Subtract one from A
AD5F LD A,D Copy D into A
AD60 JR NZ,$AD66 If A is not zero, skip ahead to AD66
AD62 CP $A0 Compare A with $A0
AD64 JR Z,$AD67 If A equals $A0, skip ahead to AD67
AD66 INC (HL) Add one to the byte HL points at
AD67 CP $A0 Compare A with $A0 - A comma: back to read another sentence (AABD)
AD69 JP Z,$AABD If A equals $A0, go back to AABD
AD6C LD A,($A01B) Read $A01B in "Game variables" into A - $A01B set (the line had sentences waiting)?
AD6F AND A Test A for zero (this leaves A as it was and clears carry)
AD70 JR Z,$ADD4 If A is zero, jump on to ADD4
AD72 LD IY,($9FF0) Read words the player has (9FF0) into IY - Then look at the second node of the sentence list
AD76 CALL $AEA9 Call the routine "IY = the next node in the list (Z at the end)" (AEA9)
AD79 LD A,(IY+$01) Read byte +1 of the record at IY into A - Has it a verb of its own?
AD7C AND $7F Keep only bits 0, 1, 2, 3, 4, 5, 6 of A (mask $7F)
AD7E OR (IY+$00) Set in A the bits that are set in the first byte of the record at IY
AD81 JR NZ,$ADB8 If the result is not zero, jump on to ADB8
AD83 LD A,($A01B) Read $A01B in "Game variables" into A - No: carry the verb's bit 7 into the first node...
AD86 LD E,A Copy A into E
AD87 LD D,$00 Load D with $00
AD89 LD HL,($9FF0) Read words the player has (9FF0) into HL
AD8C LD A,(IY+$01) Read byte +1 of the record at IY into A
AD8F AND $80 Isolate bit 7 of A (mask $80)
AD91 INC HL Step HL on to the next byte
AD92 OR (HL) Set in A the bits that are set in the byte HL points at
AD93 LD (HL),A Store A at the address HL points to
AD94 DEC HL Step HL back one byte
AD95 ADD HL,DE Add DE to HL - ...and fill in the empty word slots at the first node + $A01B from the second node's words (AE59)
AD96 LD DE,$0008 Load DE with $0008, the amount that is added next
AD99 ADD IY,DE Add DE to IY - IY + DE: the slot
AD9B PUSH IY Save IY on the stack
AD9D POP DE Restore DE from the stack
AD9E CALL $AE59 Fill the first word from DE unless it already has one (AE59)
ADA1 CALL $AE59 ...and the second
ADA4 JR Z,$ADAF Nothing to add: skip
ADA6 PUSH DE Save DE on the stack - Else fill the pair two bytes back too
ADA7 PUSH HL Save HL on the stack
ADA8 DEC DE Step DE back one byte
ADA9 DEC DE Step DE back one byte
ADAA CALL $AE59 Call the routine "Fill in a word reference at HL from DE, unless HL..." (AE59)
ADAD POP HL Restore HL from the stack
ADAE POP DE Restore DE from the stack
ADAF CALL $AE59 Call the routine "Fill in a word reference at HL from DE, unless HL..." (AE59)
ADB2 LD IY,($9FF0) Read words the player has (9FF0) into IY - Then drop the first node of the list
ADB6 JR $ADBC Skip ahead to ADBC
ADB8 LD IY,$9FF2 (with a verb: drop the first node of the rest of the line, 9FF2)
ADBC LD L,(IY-$02) Read byte -2 of the record at IY into L - IX = the list's first node; unhook it...
ADBF LD H,(IY-$01) Read byte -1 of the record at IY into H
ADC2 PUSH HL Save HL on the stack
ADC3 POP IX Restore IX from the stack
ADC5 LD L,(IX-$02) Read byte -2 of the record at IX into L
ADC8 LD H,(IX-$01) Read byte -1 of the record at IX into H
ADCB LD (IY-$02),L Store L in byte -2 of the record at IY
ADCE LD (IY-$01),H Store H in byte -1 of the record at IY
ADD1 CALL $A1C7 ...and free it (A1C7)
ADD4 LD IY,$9FF2 Point IY at 9FF2 in "Game variables" - Now go through the sentences (A012 of them) from the start
ADD8 LD A,($A012) Read A012 in "Game variables" into A
ADDB LD B,A Copy A into B
ADDC PUSH BC Save BC on the stack
ADDD CALL $AEB6 Call the routine "Step forward over nodes marked with bit 6 of +1" (AEB6)
ADE0 LD A,B Copy B into A
ADE1 AND A Test A for zero (this leaves A as it was and clears carry)
ADE2 JR Z,$AE1E If A is zero, jump on to AE1E
ADE4 CALL $AEB6 Next sentence's node (skipping those marked by bit 6 of +1)
ADE7 LD A,($A024) Read $A024 in "Game variables" into A - A statement ($A024 = 2) whose third group (+24) is empty...
ADEA CP $02 Compare A with $02
ADEC JR NZ,$AE11 If A differs from $02, skip ahead to AE11
ADEE PUSH IY Save IY on the stack - HL = the node at IY + $18...
ADF0 POP HL Restore HL from the stack
ADF1 LD DE,$0018 Load DE with $0018, the amount that is added next
ADF4 ADD HL,DE Add DE to HL
ADF5 PUSH HL Save HL on the stack
ADF6 LD D,$06 Load D with $06 - ...OR together its 6 bytes
ADF8 XOR A Clear A to zero (and the carry flag)
ADF9 OR (HL) Set in A the bits that are set in the byte HL points at
ADFA INC HL Step HL on to the next byte
ADFB DEC D Subtract one from D
ADFC JR NZ,$ADF9 If D is not zero, go back to ADF9
ADFE POP HL Restore HL from the stack
ADFF AND A Test A for zero (this leaves A as it was and clears carry) - Z if all of them are zero (the slot is empty)
AE00 JR NZ,$AE11 If A is not zero, skip ahead to AE11
AE02 EX DE,HL Swap DE and HL - ...takes the previous sentence's
AE03 PUSH IX Save IX on the stack
AE05 POP HL Restore HL from the stack
AE06 PUSH BC Save BC on the stack
AE07 LD BC,$0018 Load BC with $0018, the amount that is added next
AE0A ADD HL,BC Add BC to HL
AE0B LD BC,$0006 Load BC with $0006
AE0E LDIR Copy BC bytes from where HL points to where DE points, counting up
AE10 POP BC Restore BC from the stack
AE11 LD A,(IY+$01) Read byte +1 of the record at IY into A - A sentence with no verb is merged with the previous one (AEF1)
AE14 AND $7F Keep only bits 0, 1, 2, 3, 4, 5, 6 of A (mask $7F)
AE16 OR (IY+$00) Set in A the bits that are set in the first byte of the record at IY
AE19 CALL Z,$AEF1 If the result is zero, call the routine "Merge parse node IX into node IY" (AEF1)
AE1C JR $ADE0 Go back to ADE0
AE1E POP BC Restore BC from the stack - Then backwards from the last node
AE1F CALL $AEEA Call the routine "IY = the last node of the sentence list" (AEEA)
AE22 CALL $AEC5 Call the routine "IY = the node before IY in the sentence list (9FF0)" (AEC5) - One step back already - so the loop below never compares the last pair (see Bugs)
AE25 LD A,B Copy B into A
AE26 AND A Test A for zero (this leaves A as it was and clears carry)
AE27 JP Z,$AE57 If A is zero, jump on to AE57
AE2A CALL $AEC5 Call the routine "IY = the node before IY in the sentence list (9FF0)" (AEC5) - Each earlier sentence with no second group (+18),
AE2D LD A,(IY+$12) Read byte +18 of the record at IY into A
AE30 OR (IY+$13) Set in A the bits that are set in byte +19 of the record at IY
AE33 JR NZ,$AE25 If the result is not zero, go back to AE25
AE35 LD A,(IY+$00) Read the first byte of the record at IY into A - the same verb as the later one,
AE38 CP (IX+$00) Compare A with the first byte of the record at IX
AE3B JR NZ,$AE25 If A differs from the first byte of the record at IX, go back to AE25
AE3D LD A,(IY+$01) Read byte +1 of the record at IY into A
AE40 CP (IX+$01) Compare A with byte +1 of the record at IX
AE43 JR NZ,$AE25 If A differs from byte +1 of the record at IX, go back to AE25
AE45 LD A,(IX+$0E) Read byte +14 of the record at IX into A - where the later one has words at +14...
AE48 OR (IX+$0F) Set in A the bits that are set in byte +15 of the record at IX
AE4B JR Z,$AE25 If the result is zero, go back to AE25
AE4D EXX Swap BC, DE and HL with their shadow copies - ...has words copied from it (AF27, from +14)
AE4E LD DE,$000E Load DE with $000E
AE51 CALL $AF27 Call AF27 (inside "Merge parse node IX into node IY")
AE54 JP $AE25 Go back to AE25
AE57 XOR A Set A to zero, which sets the zero flag for the caller - Done: Z
AE58 RET Return to the caller
Prev: ACFB Up: Map Next: AE59