Load code

32815: Fast load the main code block


32815 31FC80 LD SP,33020 Put stack somewhere safe
32818 DD210040 LD IX,16384 Start loading at address 16384 (start of display area)
32822 1100C0 LD DE,49152
32825 3EFF LD A,255 We expect the first (marker) byte loaded to be 255
32827 37 SCF Set the carry flag
32828 14 INC D Reset the zero flag
32829 08 EX AF,AF'
32830 15 DEC D Restore the value of DE
32831 F3 DI Disable interrupts
32832 3E0F LD A,15 Set border to white
32834 D3FE OUT (254),A
32836 DBFE IN A,(254) Collect an initial EAR port reading into bit 6 of A
32838 1F RRA Move it to bit 5 of A
32839 E620 AND 32 Clear the extraneous bits (0-4 and 6-7)
32841 F602 OR 2 Border will turn red when first edge is found
32843 4F LD C,A C will hold the border colour
32844 BF CP A Set the zero flag
32845 C0 RET NZ
32846 CD0F80 CALL 32783 Listen for an edge
32849 30FA JR NC,32845 Jump back to listen again if none found within time limit
32851 211504 LD HL,1045 An edge was found. Now wait a bit
32854 10FE DJNZ 32854
32856 2B DEC HL
32857 7C LD A,H
32858 B5 OR L
32859 20F9 JR NZ,32854
32861 CD0080 CALL 32768 ...and see if the edges are still coming
32864 30EB JR NC,32845 Jump back if they're not
32866 069C LD B,156 256 double edges arriving within a specific time limit constitute a valid leader tone
32868 CD0080 CALL 32768
32871 30E4 JR NC,32845
32873 3EC6 LD A,198
32875 B8 CP B
32876 30E0 JR NC,32846
32878 24 INC H
32879 20F1 JR NZ,32866
32881 06C9 LD B,201 Is the leader tone still there?
32883 CD0F80 CALL 32783
32886 30D5 JR NC,32845 Jump back if not
32888 78 LD A,B Now start looking for the first edge of the data block
32889 FED4 CP 212
32891 30F4 JR NC,32881 Jump back until it's found
32893 CD0F80 CALL 32783 Look for the second edge of the data block
32896 D0 RET NC Reset the Spectrum if it can't be found
32897 79 LD A,C The border will alternate between blue and yellow for the data block
32898 EE03 XOR 3
32900 4F LD C,A
32901 2600 LD H,0
32903 06E1 LD B,225
32905 1818 JR 32931 Jump forward to load the first (marker) byte of the data block
32907 08 EX AF,AF'
32908 2005 JR NZ,32915 Jump if the first (marker) byte has just been collected
32910 DD7500 LD (IX+0),L Load byte read from tape into memory
32913 180A JR 32925
32915 CB11 RL C
32917 AD XOR L L=first byte of data block
32918 C0 RET NZ Reset the Spectrum if it wasn't 255
32919 79 LD A,C
32920 1F RRA
32921 4F LD C,A
32922 13 INC DE Compensate for the DEC DE below
32923 1802 JR 32927 Jump forward to start loading into memory
32925 DD23 INC IX IX=next address to load byte from tape into
32927 1B DEC DE
32928 08 EX AF,AF'
32929 06E3 LD B,227
32931 2E01 LD L,1 Get ready to load eight bits from tape
32933 CD0080 CALL 32768 Load one bit from tape
32936 D0 RET NC Reset the Spectrum if there was a loading error
32937 3EED LD A,237
32939 B8 CP B The carry flag now holds the loaded bit
32940 CB15 RL L Move the loaded bit into the L register
32942 06E1 LD B,225
32944 D2A580 JP NC,32933 Jump unless eight bits have been loaded
32947 7C LD A,H
32948 AD XOR L
32949 67 LD H,A
32950 7A LD A,D DE never actually reaches 0...
32951 B3 OR E
32952 20D1 JR NZ,32907 Jump back to load another byte from tape
32954 C9 RET (We never actually get here)

When the computer has loaded up to 32953, the instruction at 32952 is changed thus:
32952 20C3 JR NZ,32893

At this stage, 32893 reads as follows:
32893 DD75CC LD (IX-52),L POKE 32902,32
32896 D9 EXX
32897 DD09 ADD IX,BC Add 23 to IX
32899 D9 EXX
32900 CBFD SET 7,L

After the poke at 32893:
32902 2019 JR NZ,32929 Jump back to load next byte from tape

The last byte to be loaded is at 32902 (even though there is one more byte, 32925, left on the tape: see the save routine), which then reads:
32902 31195D LD SP,23833
32905 54 LD D,H
32906 C9 RET To (23833) = 33204