| Load routines

32815: Load the next code block

Continues from 24055 (to load the fast code block) and 33046 (to load the standard speed code block).
32815 | LD SP,33020 | Put the stack somewhere safe
32818 | LD IX,33195 | The fast code block loads backwards from address 33195; after the fast code block has loaded, this instruction is changed to 'LD IX,16384' for the standard speed code block
32822 | LD DE,49152 | Set DE (the byte counter) to a high enough value that it won't reach 0 before (a) 241 bytes have been loaded from the fast code block, or (b) the first 16572 bytes (16384-32955) have been loaded from the standard speed code block
32825 | LD A,255 | We expect the first byte loaded (the flag byte) to be 255
32827 | SCF | In the analogous ROM routine, setting the carry flag would indicate that we want to LOAD rather than VERIFY; here, this flag is not used
32828 | INC D | Reset the zero flag, indicating that we haven't loaded the first byte of the data block (the flag byte) yet
32829 | EX AF,AF' | Save these flags
32830 | DEC D | Restore the value of DE
32831 | DI | Disable interrupts
32832 | LD A,15 | BORDER 7
32834 | OUT (254),A
32836 | IN A,(254) | Collect an initial EAR port reading into bit 6 of A
32838 | RRA | Move it to bit 5 of A
32839 | AND 32 | Clear the extraneous bits (0-4 and 6-7)
32841 | OR 2 | The border will turn red when the first edge is found
32843 | LD C,A | C will hold the border colour
32844 | CP A | Set the zero flag to avoid returning at the next instruction
32845 | RET NZ | In the analogous ROM routine, this instruction would return if the BREAK key is being pressed; here, the zero flag is always set
32846 | CALL 32783 | Listen for an edge
32849 | JR NC,32845 | Jump back to listen again if no edge was found within the time limit
An edge was found. Wait a bit and then listen again.
32851 | LD HL,1045 | Wait for about one second
32854 | DJNZ 32854
32856 | DEC HL
32857 | LD A,H
32858 | OR L
32859 | JR NZ,32854
32861 | CALL 32768 | Are the edges still coming?
32864 | JR NC,32845 | Jump back if not
Check whether the signal is a leader tone.
32866 | LD B,156 | 256 double edges arriving within a specific time limit constitute a valid leader tone
32868 | CALL 32768
32871 | JR NC,32845
32873 | LD A,198
32875 | CP B
32876 | JR NC,32846
32878 | INC H
32879 | JR NZ,32866
This looks like a leader tone. Now listen for the first edge of the data block.
32881 | LD B,201 | Is the leader tone still there?
32883 | CALL 32783
32886 | JR NC,32845 | Jump back if not
32888 | LD A,B | Have we found the first edge of the data block?
32889 | CP 212
32891 | JR NC,32881 | Jump back if not
The first edge of the data block has been detected.
32893 | CALL 32783 | Look for the second edge of the data block
32896 | RET NC | Reset the Spectrum if it can't be found
Prepare to load the data block.
32897 | LD A,C | The border will alternate between blue and yellow for the data block
32898 | XOR 3
32900 | LD C,A
32901 | LD H,0 | Initialise the parity byte to 0
32903 | LD B,225 | Set the timing constant for the flag byte; after the fast code block has loaded, this instruction is changed to 'LD B,176' for the standard speed code block
32905 | JR 32933 | Jump forward to load the flag byte
This is the byte-loading loop. The first byte loaded is the flag byte.
32907 | NOP |
32908 | NOP |
32909 | EX AF,AF' | Restore the flags
32910 | JR NZ,32917 | Jump if the first byte (the flag byte) has just been collected
32912 | LD (IX+0),L | Load the byte read from tape into memory
32915 | JR 32927 |
32917 | RL C | Save the carry flag in bit 0 of C temporarily
32919 | XOR L | L=first byte of the data block (the flag byte)
32920 | RET NZ | Reset the Spectrum if it wasn't 255
32921 | LD A,C | Restore the carry flag
32922 | RRA
32923 | LD C,A | Restore C
32924 | INC DE | Compensate for the 'DEC DE' below
32925 | JR 32929 | Jump forward to start loading bytes into memory
32927 | DEC IX | IX=next address to load the byte from tape into; after the fast code block has loaded, this instruction is changed to 'INC IX' for the standard speed code block
32929 | DEC DE | Decrease the byte counter
32930 | EX AF,AF' | Save the flags (the zero flag is now set)
32931 | LD B,227 | Set the timing constant; after the fast code block has loaded, this instruction is changed to 'LD B,178' for the standard speed code block
32933 | LD L,1 | Get ready to load eight bits from the tape
32935 | CALL 32768 | Load one bit from the tape
32938 | RET NC | Reset the Spectrum if there was a loading error
32939 | LD A,237 | Set the carry flag if a '1' was read from the tape, or reset it if a '0' was read; after the fast code block has loaded, the 'LD A,237' instruction is changed to 'LD A,203' for the standard speed code block
32941 | CP B
32942 | RL L | Move the bit into the L register
32944 | LD B,225 | Set the timing constant for the next bit; after the fast code block has loaded, this instruction is changed to 'LD B,176' for the standard speed code block
32946 | JP NC,32935 | Jump unless eight bits have been loaded
A full byte has just been read from the tape.
32949 | LD A,H | Update the (ultimately unused) parity byte against the byte just read from the tape
32950 | XOR L
32951 | LD H,A
32952 | LD A,D | Set the zero flag if the the byte counter has reached 0 (which never happens)
32953 | OR E
32954 | JR NZ,32907 | Jump back to load another byte from the tape
When the bytes from 33195 downwards to 32955 have loaded from the fast code block, the instruction at 32954 above becomes:
32954 | JR NZ,32956 |
And the code continues at 32956. When the bytes from 16384 to 32955 have loaded from the standard speed code block, the instruction at 32954 becomes:
32954 | JR NZ,32890 |
At this stage, 32890 reads as follows:
32890 | LD (IX+20),L | Load the byte read from tape into memory
32893 | EXX |
32894 | ADD IX,BC | Add 247 to IX
32896 | EXX |
32897 | JR NC,32901 | Jump unless IX was incremented beyond 65535
32899 | LD IXh,127 | Reset IX to the appropriate range
32902 | JR 32931 | Jump forward to load the next byte from tape
Now 32890 more bytes are loaded, the first at 33223 and the last at 32902 (even though there is one more byte, 33149, left on the tape: see the save routine). Then 32902 reads as follows:
32902 | LD SP,23835 | Point the stack pointer at the game start address that was placed at 23835 by the save routine
32905 | LD D,H |
32906 | RET | To (23835)=33224