Sherlock: annotated disassembly

Routines

Prev: A1AF Up: Map Next: A20C
A1C7: Free node IX back to the heap
Puts the block back in the free list in address order and joins it to free neighbours (A20C).
How it works:
1. Save HL and DE, and work out where the freed block starts: three bytes before the node in IX.
2. Walk the list of free blocks from its head at 9FE3. The list is kept in address order, so stop at the first free block that lies above this one, or at the end of the list.
3. Link the block in at that point: the previous link now points at it, and its own link points at the block that came after.
4. Try to join it to the free block that follows it (A20C). If the freed block joined onto the one before it instead, step back to that block and try again to join it to what follows.
Input
IX The node to free
IX is not preserved
A1C7 PUSH HL Save HL on the stack
A1C8 PUSH DE Save DE on the stack
A1C9 PUSH IX Save IX on the stack - HL = the block's size byte, three bytes before the node
A1CB POP HL Restore HL from the stack
A1CC DEC HL Step HL back one byte
A1CD DEC HL Step HL back one byte
A1CE DEC HL Step HL back one byte
A1CF LD IX,$9FE3 Point IX at the heap's free list (9FE3) - Walk the free list from 9FE3 to the first block above this one
A1D3 LD E,(IX+$00) Read the first byte of the record at IX into E
A1D6 LD D,(IX+$01) Read byte +1 of the record at IX into D
A1D9 LD A,D Copy D into A
A1DA OR E Set in A the bits that are set in E
A1DB JR Z,$A1EB If the result is zero, skip ahead to A1EB - (end of the list: put it last)
A1DD PUSH HL Save HL on the stack
A1DE AND A Clear the carry flag, so that the subtraction that follows is a plain one (a compare)
A1DF SBC HL,DE Subtract DE from HL (a compare: carry = HL was smaller, zero = equal)
A1E1 POP HL Restore HL from the stack
A1E2 JR C,$A1EB If the carry flag is set, skip ahead to A1EB
A1E4 INC DE Step DE on to the next byte
A1E5 INC DE Step DE on to the next byte
A1E6 PUSH DE Save DE on the stack
A1E7 POP IX Restore IX from the stack
A1E9 JR $A1D3 Go back to A1D3
A1EB LD (IX+$00),L Store L in the first byte of the record at IX - Link it in: the previous link now points here,
A1EE LD (IX+$01),H Store H in byte +1 of the record at IX
A1F1 INC HL Step HL on to the next byte - its size's high byte cleared, and its own link to the next free block
A1F2 LD (HL),$00 Store $00 at the address HL points to
A1F4 INC HL Step HL on to the next byte
A1F5 LD (HL),E Store E at the address HL points to
A1F6 INC HL Step HL on to the next byte
A1F7 LD (HL),D Store D at the address HL points to
A1F8 CALL $A20C Call the routine "Join free block IX to the next one if they touch" (A20C) - Join it to the block after it if they touch (A20C)...
A1FB JR Z,$A206 If the zero flag is set, skip ahead to A206
A1FD LD L,(IX+$00) Read the first byte of the record at IX into L - ...and the block before it to this one
A200 LD H,(IX+$01) Read byte +1 of the record at IX into H
A203 PUSH HL Save HL on the stack
A204 POP IX Restore IX from the stack
A206 CALL $A20C Call the routine "Join free block IX to the next one if they touch" (A20C)
A209 POP DE Restore DE from the stack
A20A POP HL Restore HL from the stack
A20B RET Return to the caller
Prev: A1AF Up: Map Next: A20C