| Routines

66CE: Get the next character of a message being spoken or written

Used by the routines at 6791, 6A08 and 6B1B. Returns with the next character in A.
Input
H | Number of the character speaking or writing (0xB7-0xD1)
66CE | LD L,$10 | Bytes 0x0F and 0x10 hold the address of the next character in the sub-submessage being written/spoken (if any)
66D0 | LD A,(HL)
66D1 | AND A | Are we working on a sub-submessage?
66D2 | JR NZ,$66DC | Jump if so
66D4 | LD L,$0E | Bytes 0x0D and 0x0E hold the address of the next character in the submessage being written/spoken (if any)
66D6 | LD A,(HL)
66D7 | AND A | Are we working on a submessage?
66D8 | JR NZ,$66DC | Jump if so
66DA | LD L,$0C | We're working on a top-level message
66DC | LD D,(HL) | DE=address of the next character in the (sub)(sub)message
66DD | DEC L
66DE | LD E,(HL)
66DF | LD A,(DE) | Pick up the character code in A
66E0 | INC DE | Move the pointer to the next character and store the address for future reference
66E1 | LD (HL),E
66E2 | INC L
66E3 | LD (HL),D
66E4 | AND A | Has the end of the (sub)(sub)message been reached?
66E5 | JR NZ,$66F0 | Jump if not
We've reached the end of the (sub)(sub)message.
66E7 | LD (HL),$00 | Signal: end of (sub)(sub)message
66E9 | DEC L | L=0x0B (top-level message), 0x0D (submessage) or 0x0F (sub-submessage)
66EA | BIT 2,L | Has the end of the top-level message been reached (L=0x0B)?
66EC | RET Z | Return if so
66ED | DEC L | Otherwise go up to the submessage or the top-level message and jump back to deal with it
66EE | JR $66DC
We haven't reached the end of the (sub)(sub)message yet. Determine whether the next code is a regular character code or a pointer to another message.
66F0 | SUB $20 |
66F2 | CP $60 |
66F4 | JR NC,$66F9 | Jump with character codes 0x01-0x1F or 0x80 onwards
66F6 | ADD A,$20 |
66F8 | RET | Return with the standard ASCII code in A
66F9 | ADD A,$20 |
66FB | CP $03 |
66FD | RET C | Return with character codes 0x01 and 0x02 (end of line)
The next character in the message is actually a pointer to another message. Find the address of that message, and store it for future reference.
66FE | LD E,A | DE points to the LSB of the start address of the (sub)submessage
66FF | LD D,$FE
6701 | INC L | L=0x0D (submessage) or 0x0F (sub-submessage)
6702 | LD A,(DE) | A=LSB of the start address of the (sub)submessage
6703 | LD (HL),A | Store it in byte 0x0D or 0x0F of the character's buffer
6704 | INC L | L=0x0E (submessage) or 0x10 (sub-submessage)
6705 | INC D | A=MSB of the address of the (sub)submessage
6706 | LD A,(DE)
6707 | LD (HL),A | Store it in byte 0x0E or 0x10 of the character's buffer
6708 | JR $66DC | Jump back to collect a character from this (sub)submessage