| Routines

6C18: Alter UDG references in the play area for a door, a window, a cup or the bike

Used by the routines at 5DDC, 6CD4, 7040 and FA4D. The UDG reference tables used by this routine are organised into entries of four bytes each. Each entry corresponds to a single UDG within the matrix of UDGs for the door, window, cup or bike:
Byte | Contents
--- | ---
1 | y-coordinate
2 | x-coordinate
3 | UDG reference
4 (bits 0-3) | BRIGHT/PAPER attributes
4 (bits 6 & 7) | UDG base page identifier
The data tables used are located as follows:
Address | Object
--- | ---
DD3D | Left study door (closed)
DE3D | Left study door (open)
DF00 | Right study door (closed)
E000 | Right study door (open)
DD00 | Science Lab storeroom door (closed)
DE00 | Science Lab storeroom door (open)
DF3D | Boys' skool door (closed)
E03D | Boys' skool door (open)
F900 | Skool gate (closed)
FA00 | Skool gate (open)
DB00 | Drinks cabinet door (closed)
DC00 | Drinks cabinet door (open)
D700 | Top-floor window (closed)
D800 | Top-floor window (open)
D900 | Middle-floor window (closed)
DA00 | Middle-floor window (open)
DB11 | Cups in the boys' skool (empty)
DD76 | Leftmost cup in the boys' skool (containing water)
DD7B | Leftmost cup in the boys' skool (containing sherry)
DE76 | Middle cup in the boys' skool (containing water)
DE7B | Middle cup in the boys' skool (containing sherry)
DF76 | Rightmost cup in the boys' skool (containing water)
DF7B | Rightmost cup in the boys' skool (containing sherry)
DC11 | Cup in the girls' skool (empty)
E076 | Cup in the girls' skool (containing water)
E07B | Cup in the girls' skool (containing sherry)
E100 | Tree (with no bike attached)
E200 | Tree (with bike attached)
Input
HL | UDG reference table address
6C18 | LD A,(HL) | Pick up the y-coordinate (Y) in D
6C19 | LD D,A
6C1A | INC A | Return if we found the end-of-table marker (0xFF)
6C1B | RET Z
6C1C | INC HL | Point HL at byte 2 of this table entry
6C1D | LD E,(HL) | Pick up the x-coordinate (X) in E
6C1E | INC HL | Point HL at byte 3 of this table entry
6C1F | PUSH HL |
6C20 | LD A,($7FFF) | C=leftmost column of the play area on screen
6C23 | LD C,A
6C24 | LD A,E | Jump if (X,Y) is off-screen (no need to update the screen refresh buffer)
6C25 | SUB C
6C26 | JR C,$6C45
6C28 | CP $20
6C2A | JR NC,$6C45
(X,Y) corresponds to a location that is currently on-screen, so we have to update the screen refresh buffer (SRB).
6C2C | LD C,A | 0<=C<=31 (screen x-coordinate)
6C2D | AND $07 | Point HL at an entry in the 8-byte table at E178, whose contents are (0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01)
6C2F | ADD A,$78
6C31 | LD L,A
6C32 | LD H,$E1
6C34 | LD B,(HL) | Pick up this entry in B
6C35 | LD A,C | 0<=A<=31 (screen x-coordinate)
6C36 | RRCA | Point HL at the byte of the SRB corresponding to (X,Y)
6C37 | RRCA
6C38 | RRCA
6C39 | AND $03
6C3B | ADD A,D
6C3C | ADD A,D
6C3D | ADD A,D
6C3E | ADD A,D
6C3F | LD L,A
6C40 | LD H,$7F
6C42 | LD A,(HL) | Set the appropriate bit in the SRB byte
6C43 | OR B
6C44 | LD (HL),A
The UDG reference (i.e. the LSB of the base address of the skool UDG) for the skool location (X,Y) must be modified to reflect the change in status of the door, window, cup or bike.
6C45 | LD L,E | Point HL at the Q value (0x00<=Q<=0x8F) in page 0xB5 for the x-coordinate X (see 606C)
6C46 | LD H,$B5
6C48 | LD E,(HL) | Pick up the Q value in E
6C49 | LD A,D | Set DE to the address where the UDG reference for (X,Y) is stored
6C4A | ADD A,$A0
6C4C | LD D,A
6C4D | POP HL | Point HL at byte 3 of the table entry
6C4E | LD A,(HL) | Pick up the replacement UDG reference in A
6C4F | INC HL | Point HL at byte 4 of the table entry
6C50 | LD (DE),A | Replace the UDG reference for the skool location (X,Y) with the one collected from the table entry
The attribute byte for the skool location (X,Y) must be modified too.
6C51 | LD A,(HL) | Pick up byte 4 of the table entry in A
6C52 | AND $0F | Keep only bits 0-3 (the BRIGHT and PAPER attributes)
6C54 | LD B,A | Copy them to B
6C55 | LD A,E | A=Q (0x00<=Q<=0x8F)
6C56 | LD C,A | Save the Q value in C for later
6C57 | ADD A,$68 | Point DE at byte 0xB4+INT(Q/2) of page Y+0xA0, where the BRIGHT and PAPER attributes for the skool location (X,Y) are stored
6C59 | SCF
6C5A | RRA
6C5B | LD E,A
6C5C | JR C,$6C69 | Jump if Q is odd (BRIGHT and PAPER in bits 0-3)
6C5E | LD A,B | Shift the BRIGHT and PAPER attributes from bits 0-3 to bits 4-7 of B
6C5F | RRCA
6C60 | RRCA
6C61 | RRCA
6C62 | RRCA
6C63 | LD B,A
6C64 | LD A,(DE) | Pick up the current attribute byte for the skool location (X,Y), keeping bits 0-3 as they are
6C65 | AND $0F
6C67 | JR $6C6C | Jump forward to adjust bits 4-7 appropriately
6C69 | LD A,(DE) | Pick up the current attribute byte for the skool location (X,Y), keeping bits 4-7 as they are
6C6A | AND $F0
6C6C | OR B | Adjust bits 0-3 (if Q is odd) or bits 4-7 (Q is even) of the attribute byte for the skool location (X,Y)
6C6D | LD (DE),A
Finally, the base page (0x80, 0x88, 0x90 or 0x98) for the new skool UDG reference must be set.
6C6E | LD A,(HL) | Pick up byte 4 of the table entry in A again
6C6F | AND $C0 | Keep only bits 6 and 7
6C71 | BIT 6,A | Set B=10001000 if these bits are 11, 10000000 (10), 00001000 (01) or 00000000 (00)
6C73 | JR Z,$6C77
6C75 | SUB $38
6C77 | LD B,A
6C78 | LD A,C | A=Q
6C79 | LD C,$88 | C=10001000
6C7B | SRL A | Set A=INT(Q/4), and shift B and C right (Q AND 3) times
6C7D | JR NC,$6C83
6C7F | RRC B
6C81 | RRC C
6C83 | SRL A
6C85 | JR NC,$6C8F
6C87 | RRC B
6C89 | RRC C
6C8B | RRC B
6C8D | RRC C
6C8F | ADD A,$90 | Point DE at byte 0x90+INT(Q/4) of page Y+0xA0, where the UDG MSB identifier bit-pair for skool location (X,Y) is held (see 606C)
6C91 | LD E,A
6C92 | LD A,(DE) | Pick up the current MSB identifier byte in A
6C93 | OR C | Replace bits (0,4), (1,5), (2,6) or (3,7) in this byte with the corresponding bits in B
6C94 | XOR C
6C95 | OR B
6C96 | LD (DE),A | Store the new MSB identifier byte
Move to the next entry in the table.
6C97 | INC HL | Point HL at the first byte of the next entry in the table
6C98 | JP $6C18 | Jump back to process it