Back to Skool Routines
24684: Print a character square
Used by the routines at 25026, 25080 and 25248.

The skool graphic data is laid out across pages 128 to 181:
Page(s) Bytes Contents
128-135 0-255 Skool UDGs
136-143 0-255 Skool UDGs
144-151 0-255 Skool UDGs (except bytes 1, 11, 21, 30)
152-159 0-249 Skool UDGs (except bytes 224-239)
160-180 0-143 LSBs of skool UDG base addresses
144-179 Skool UDB MSB indicator bit-pairs (see below)
180-251 Skool UDG BRIGHT/PAPER attribute nibbles
181 0-191 Q (0<=Q<=143; see below)
For the character square at skool coordinates (X,Y) (0<=X<=191, 0<=Y<=20), the value Q (0<=Q<=143) is collected from byte X of page 181. This Q value is used to determine the location of the attribute byte, and look up the base address of the skool UDG.

The BRIGHT/PAPER bits for row Y are arranged in a set of 144 nibbles in bytes 180-251 of page Y+160, from bits 7-4 of byte 180 (nibble 0) to bits 3-0 of byte 251 (nibble 143). The value of Q is the index of the appropriate nibble in the set of 144. In other words, the BRIGHT/PAPER bits for the skool location (X,Y) can be found in byte 180+INT(Q/2) of page Y+160: bits 4-7 if Q is even, bits 0-3 if Q is odd.

By default, the INK is 0 (black). However, if the BRIGHT/PAPER bits are all 0, this indicates a character square with non-black INK, of which there are only four: the three shields in MR WACKER's study (which are red, blue, and red), and the top of the entrance to the girls' skool (which is blue). In this case the BRIGHT/PAPER bits are taken from byte 180+INT(Q/2) of page Y+161 (i.e. the attribute byte corresponding to the skool location (X,Y+1)); then the INK is 2 (red) if Q is even, 1 (blue) if 4|Q-1, or 3 (magenta) if 4|Q-3.

So much for the attributes. The LSB of the base address of the skool UDG is found in byte Q of page Y+160. Information on which MSB (128, 136, 144 or 152) applies is arranged in a set of 144 bit-pairs in bytes 144-179 of page Y+160. Bit-pair 0 is bits 7 and 3 of byte 144; bit-pair 1 is bits 6 and 2; bit-pair 2 is bits 5 and 1; bit-pair 3 is bits 4 and 0; bit-pair 4 is bits 7 and 3 of byte 145; and so on up to bit-pair 143, which is bits 4 and 0 of byte 179. The value of Q is the index of the appropriate bit-pair in the set of 144. In other words, the UDG MSB indicator for the skool location (X,Y) can be found in byte 144+INT(Q/4): bits 7 and 3 if 4|Q, 6 and 2 if 4|Q-1, 5 and 1 if 4|Q-2, 4 and 0 if 4|Q-3. The bit-pair forms a 2-digit binary number from 0 to 3; if we call this P, then the MSB is 128+8P.

To summarise, then, the attributes and graphic data for the tile at skool location (X,Y) can be found thus, where Q=PEEK(46336+X):
Data Look at...
BRIGHT/PAPER Nibble Q of bytes 180-251 in page Y+160 (or Y+161 if nibble Q in page Y+160 is 0)
INK 0 if nibble Q in page Y+160 is not 0; 1, 2 or 3 as explained above otherwise
UDG LSB Byte Q in page Y+160
UDG MSB Bit-pair Q of bytes 144-179 in page Y+160
H Screen row number (0-20)
L Screen column number (0-31)
24684 PUSH HL
24685 LD A,H Compute the appropriate attribute file address in BC and the appropriate display file address in DE
24686 AND 24
24688 LD D,A
24689 LD A,H
24690 RRCA
24691 RRCA
24692 RRCA
24693 LD H,A
24694 AND 224
24696 ADD A,L
24697 LD E,A
24698 LD C,A
24699 LD A,H
24700 AND 3
24702 ADD A,88
24704 LD B,A
24705 SET 6,D
24707 EX DE,HL Place the display file address on the stack and get (row,col) back in HL
24708 EX (SP),HL
24709 LD A,(32767) A=leftmost column of play area on screen (0-160)
24712 ADD A,L L=X: column of play area corresponding to the character square under consideration (0-191)
24713 LD L,A
24714 PUSH HL Push the skool coordinates (X,Y) onto the stack
24715 LD A,H A=Y (0-20)
24716 LD H,181 HL=46336+X
24718 LD L,(HL) L=Q (0<=Q<=143)
24719 ADD A,160 H=Y+160
24721 LD H,A
24722 LD E,L E=Q (0<=Q<=143)
24723 LD A,L A=Q
24724 RRA Is Q odd?
24725 JR C,24754 Jump if so
Q (the contents of (46336+X)) is even, which means the PAPER colour and BRIGHT status can be found in bits 4-7 of byte 180+Q/2 in page Y+160. At this point A=Q/2.
24727 ADD A,180 180<=A<=251
24729 LD L,A Point HL at the attribute byte
24730 LD A,(HL) Pick up the attribute byte in A
24731 AND 240 The PAPER colour and BRIGHT status are in bits 4-7
24733 RR A Shift them into bits 3-6
24735 JR NZ,24774 Jump if this character square has INK 0
We are dealing with one of the few character squares with non-black INK. In this case the PAPER colour and BRIGHT status can be found in bits 4-7 (since Q is even) of byte 180+Q/2 in page Y+161.
24737 INC H Point HL at the attribute byte in page Y+161
24738 LD A,(HL) Pick up the attribute byte in A
24739 AND 240 The PAPER colour and BRIGHT status are in bits 4-7
24741 RRA Shift them into bits 3-6
24742 LD L,A L holds the PAPER colour and BRIGHT status
24743 LD A,E A=Q (0<=Q<=143)
24744 AND 3 A=0, 1, 2 or 3
24746 JR NZ,24750 Jump if the INK is blue, red, or magenta
24748 ADD A,2 A=2: INK 2 (red)
24750 OR L OR on the PAPER colour and BRIGHT status
24751 DEC H H=Y+160
24752 JR 24774 Jump forward to transfer the attribute byte in A onto the screen
Q (the contents of (46336+X)) is odd, which means the PAPER colour and BRIGHT status can be found in bits 0-3 of byte 180+(Q-1)/2 in page Y+160. At this point A=(Q-1)/2.
24754 ADD A,180 180<=A<=251
24756 LD L,A Point HL at the attribute byte
24757 LD A,(HL) Pick up the attribute byte in A
24758 AND 15 The PAPER colour and BRIGHT status are in bits 0-3
24760 JR NZ,24771 Jump if this character square has INK 0
We are dealing with one of the few character squares with non-black INK. In this case the PAPER colour and BRIGHT status can be found in bits 0-3 (since Q is odd) of byte 180+(Q-1)/2 in page Y+161.
24762 INC H Point HL at the attribute byte in page Y+161
24763 LD A,(HL) Pick up the attribute byte in A
24764 AND 15 The PAPER colour and BRIGHT status are in bits 0-3
24766 ADD A,A Shift the PAPER and BRIGHT bits from bits 0-3 to bits 3-6
24767 ADD A,A
24768 ADD A,A
24769 JR 24742 Jump back to fill in the INK bits
24771 ADD A,A Shift the PAPER and BRIGHT bits from bits 0-3 to bits 3-6, and set the INK to 0
24772 ADD A,A
24773 ADD A,A
24774 LD (BC),A Poke the attribute byte onto the screen (during startup this instruction is NOP; see 21408)
The appropriate attribute byte has been poked onto the screen. Now to find the appropriate skool UDG.
24775 LD L,E L=Q (0<=Q<=143), H=Y+160
24776 LD E,(HL) E=LSB of skool UDG base address
24777 LD A,136 A=10001000
24779 SRL L Shift A right (Q AND 3) times
24781 JR NC,24784
24783 RRCA
24784 SRL L
24786 JR NC,24790
24788 RRCA
24789 RRCA
24790 LD C,A C=10001000, 01000100, 00100010 or 00010001
24791 LD A,L Point HL at the byte containing the MSB indicator bit-pair: H=Y+160, 144<=L=144+INT(Q/4)<=179
24792 ADD A,144
24794 LD L,A
24795 LD A,C A=10001000, 01000100, 00100010 or 00010001
24796 AND (HL) Keep only the bits of the bit-pair
The base page for the skool UDG depends on the bits set in A.
24797 LD D,128
24799 CP 16 Are any of bits 4-7 in A set?
24801 JR C,24805 Jump if not
24803 LD D,144
24805 AND 15 Are any of bits 0-3 in A set?
24807 JR Z,24811 Jump if not
24809 SET 3,D
Now D=128, 136, 144 or 152, and DE points at the first byte of the skool UDG corresponding to the skool coordinates (X,Y).
24811 LD HL,57712 We're going to copy the skool UDG into the 8-byte back buffer at 57712
24814 LD B,8
24816 LD A,(DE) A=skool UDG byte (during startup this instruction is XOR A; see 21408)
24817 INC D Point DE at the next byte of the skool UDG
24818 LD (HL),A Copy the skool UDG byte into the back buffer
24819 INC L Move to the next slot in the back buffer
24820 DJNZ 24816 Jump back until the entire skool UDG has been copied
The appropriate skool UDG has been copied into the back buffer at 57712. Now it's time to superimpose game character UDGs (if any).
24822 LD H,183 183=character number of little girl no. 1
24824 POP DE D=Y (0-20), E=X (0-191)
24825 LD A,(32767) A=leftmost column of play area on screen (0-160)
24828 CP 120 120=roughly halfway between the tree and the gate
24830 JR C,24843 Jump if columns 144 onwards (girls' skool + half the girls' playground) are off-screen
24832 LD B,7 7 little girls
24834 CALL 24576 Superimpose graphic bytes for the little girls if necessary
24837 LD H,198 198=character number of little boy no. 9
24839 LD B,17 2 little boys, 11 main characters, plus 211-214
24841 JR 24862
24843 CP 80 80=assembly hall stage (or thereabouts)
24845 JR C,24858 Jump if columns 104 onwards (everything to the right of the tree, roughly) are off-screen
24847 LD B,3 3 little girls (183-185)
24849 CALL 24576 Superimpose graphic bytes for these little girls if necessary
24852 LD H,193 193=character number of little boy no. 4
24854 LD B,22 7 little boys, 11 main characters, plus bike, frog and pellets
24856 JR 24862
24858 LD H,190 190=character number of little boy no. 1
24860 LD B,25 All 10 little boys, 11 main characters, plus bike, frog and pellets
24862 CALL 24576 Superimpose graphic bytes for these characters if necessary
24865 LD HL,57712 The 8-byte back buffer at 57712 now contains the UDG to be copied to the screen
24868 POP DE Retrieve the appropriate display file address in DE
This entry point is used by the routine at 26941:
24869 LD B,8 There are 8 bytes per character square
24871 LD A,(HL) Transfer the graphic bytes to the screen
24872 LD (DE),A
24873 INC L
24874 INC D
24875 DJNZ 24871
24877 RET