| Routines

6000: Superimpose sprite tiles onto a tile of the play area

Used by the routine at 606C. Checks through B characters, starting at character H. If any part of a character's sprite needs to be printed at the row and column specified by DE, the appropriate UDG is located and superimposed onto the contents of the buffer at E170, which on the first call to this routine contains the appropriate skool UDG (the background).
Input
B | Number of characters to consider
D | Row of play area (0-20)
E | Column of play area (0-191)
H | Number of the first character to consider
6000 | LD L,$01 | Point HL at byte 0x01 of the character's buffer
6002 | LD A,E | A=column of play area (0-191) under consideration
6003 | SUB (HL) | Subtract the character's x-coordinate
6004 | CP $03 | Sprites are three character squares wide
6006 | JR NC,$604C | Jump if no part of this character's sprite is in this column
6008 | DEC L | L=0x00
6009 | BIT 7,(HL) | Is the character facing left?
600B | JR Z,$6010 | Jump if so
600D | CPL | 'Flip' the number in A, so 0 becomes 2, 1 remains 1, and 2 becomes 0 (i.e. A becomes 2-A)
600E | ADD A,$03
6010 | ADD A,A | Now C=0 if the front of the character is in this column, 4 if it's the middle, or 8 if it's the back
6011 | ADD A,A
6012 | LD C,A
6013 | LD L,$02 | Point HL at byte 0x02 of the character's buffer
6015 | LD A,D | A=row of play area (0-20) under consideration
6016 | SUB (HL) | Subtract the character's y-coordinate
6017 | CP $04 | Sprites are 4 character squares tall
6019 | JR NC,$604C | Jump if no part of this character's sprite is in this row
601B | ADD A,C | 0<=A<=11 (index of the sprite tile at this row and column, where 0=top-front, 4=top-middle, 8=top-back, 11=bottom-back)
601C | ADD A,$D7 | 0xD7<=A<=0xE2
601E | EXX | Set H' to the number of the page containing the UDG reference for the sprite tile
601F | LD H,A
6020 | EXX
6021 | LD L,$00 | Point HL at byte 0x00 of the character's buffer
6023 | LD A,(HL) | A=character's animatory state
6024 | OR $80 | Set bit 7 to obtain the LSB of the address that holds the sprite tile UDG reference
6026 | EXX |
6027 | LD L,A | 0x80<=L'<=0xFF (0xD7<=H'<=0xE2)
6028 | LD A,(HL) | A=sprite tile UDG reference (0x49-0xFF)
6029 | AND A | Is this tile the 'null' UDG (a blank square)?
602A | JR Z,$604B | Jump if so (no need to superimpose it)
602C | LD E,A | E'=UDG reference (0x49-0xFF)
602D | LD A,L | A=(normalised) animatory state (0x80-0xFF)
602E | CP $D0 | 0xD0=animatory state of MR WACKER, the first teacher
6030 | EXX |
6031 | LD A,(HL) | A=character's (true) animatory state (0x00-0xFF)
6032 | EXX |
6033 | LD D,$B7 | Point DE' at the graphic data at page 0xB7 onwards (for animatory states 0x00-0x4F and 0x80-0xCF)
6035 | JR C,$6039 | Jump unless we're dealing with animatory states 0x50-0x7F or 0xD0-0xFF (teachers, ALBERT, and water fired from the pistol)
6037 | LD D,$C7 | Point DE' at the graphic data at page 0xC7 onwards (for animatory states 0x50-0x7F and 0xD0-0xFF)
6039 | LD HL,$E170 | Point HL' at the UDG back buffer
603C | RLCA | Push the character's 'direction' bit into the carry flag
603D | JR C,$6050 | Jump if the character is facing right
603F | EX DE,HL |
6040 | LD A,(DE) | A=byte already in the UDG buffer
6041 | OR (HL) | Superimpose the sprite tile byte
6042 | INC H | Point HL' at the outline mask for this tile byte
6043 | AND (HL) | AND with this mask
6044 | INC H | Point HL' at the next sprite tile byte
6045 | LD (DE),A | Place the resultant byte back in the UDG buffer
6046 | INC E | Move DE' along the UDG buffer
6047 | BIT 3,E | Have we done all 8 bytes yet?
6049 | JR Z,$6040 | Jump back if not
604B | EXX |
604C | INC H | Next game character
604D | DJNZ $6000 | Jump back until all characters have been done
604F | RET |
The character is facing right, so we have to produce a mirror image of the graphic data.
6050 | LD B,$B6 | Page 0xB6 contains mirror images of 0x00-0xFF
6052 | LD A,(DE) | A=sprite tile byte
6053 | LD C,A | A=mirror image of this byte
6054 | LD A,(BC)
6055 | OR (HL) | Superimpose what's already in the UDG buffer
6056 | LD (HL),A | Put the result so far back in the UDG buffer
6057 | INC D | A=outline mask for this sprite tile byte
6058 | LD A,(DE)
6059 | LD C,A | A=mirror image of this mask
605A | LD A,(BC)
605B | AND (HL) | AND it with what's in the UDG buffer
605C | LD (HL),A | Place the result in the UDG buffer
605D | INC D | Next sprite tile byte
605E | INC L | Next byte of the UDG buffer
605F | BIT 3,L | Have we done all 8 bytes yet?
6061 | JR Z,$6052 | Jump back if not
6063 | JR $604B |