Tape to Microdrive Colin Carruthers' easy conversion program The introductions of the Sinclair ZX Microdrive system offers many users fast access to programs and data. Pro- grams which once took several minutes to load can now be available in a matter of seconds. In fact, anyone who has used a Microdrive may be discouraged from using cassette- based software. However, there are very few commercial programs available for the Microdrive due to the difficulty in duplicating large numbers of cartridges. Few software houses seem prepared to release details on how to convert their own programs for use on Microdrive for fear of escalating the piracy problem. This is a weak argument since anyone with two cassette recorders and a blank tape can copy any program they wish. Several houses do release details, particularly publishers of serious software such as Tasman (Tasword), Campbell (Masterfile) and Hisoft with its series of compilers and assemblers. Why should loading material from tape and saving on Microdrive cartridge present a problem? To answer that, we need to consider certain aspects of the operation of a Microdrive. If you examine the diagram in Chapter 24 of the standard Spectrum manual showing RAM allocation, you will see that there are two areas labelled "Microdrive Maps" and "Channel Information". Whenever any Microdrive operation is invoked, these two areas grow by 32 and 595 bytes respectively. The Map holds information about the distribution of the 512 byte long blocks on the cartridge, while the Channel contains a 512 byte buffer along with details such as file name and type - see Microdrive and Interface 1 manual, Appendix 3. In operation, consecutive blocks are passed via the buffer from the cartridge to memory or vice versa - depen- ding on whether we are loading or saving. The main point is that the Basic program and variables must be moved up by 627 - ie, 32 + 595 - bytes. To make matters worse, Interface 1 appends 58 new bytes to the existing System Variables area. Thus, the Basic program resides at an address which is some 685 bytes higher up in memory when using the Microdrive. So, why does this present us with a problem? Well, blocks of machine code which once loaded into the free space just above the Basic area might now overwrite part of the Basic program or, worse still, part of the Microdrive Map or Channel. Either way, the system is likely to crash with the Microdrive running, leaving us no option but to unplug the Spectrum and risk corrupting the material stored on the cartridge. Programs can be divided into three categories. Those which are easy to convert to Microdrive - by the virtue of the addresses used, those which can be loaded into a higher memory address and copied down to their proper target address before being executed, and those which need to be divided into two smaller blocks and moved around after loading. The first category includes programs written entirely in Basic - simply break into them and save auto-start on cartridge - and programs that have machine-code blocks which start at high addresses. Any block which starts above 25000 would normally fall into this category, but this does depend on the size of the Basic loader program. Program 1 gives lots of useful information about material stored on tape such as auto-start line number for Basic programs along with length and start address of machine-code blocks. [Program 1 is on the tzx under the name of "Header". By the way, all three programs will run on a 16K Spectrum, too.] To use, simply run it and play back the relevant tape at normal load volume and tone settings. For each block in the program you wish to convert, note down all the details or dump the screen onto a printer. Typical output from program 1 might look like: Name : Superzap Type : Basic Start : 10 Length : 132 bytes Name : Screen Type : Bytes Start : 16384 Length : 6912 bytes Name : Superzap Type : Bytes Start : 32768 Length : 32768 This is a fairly standard format, ie, Basic loader, title screen and main code block. Type New and Merge in the Basic loader. It might look something like this: 10 CLEAR 32767 20 LOAD ""CODE: LOAD ""CODE 30 PRINT USR 35000 It is probably best to forget the title screen. They are designed mainly to keep people amused while the main code loads. Besides, space on a cartridge is relatively expen- sive. Edit the program to make it load the main code from Microdrive thus: 10 CLEAR 32767 20 LOAD *"m";1;"Superzap c"CODE 30 PRINT USR 35000 Note that we have had to change the name of the code block since we cannot have two files with the same name on the cartridge. Save this on Microdrive to auto-run from line 10. Now do: CLEAR 32767: LOAD ""CODE to load in the main code from tape. Save this to cartridge by typing in, for examine: SAVE *"m";1;"Superzap c"CODE 32768,32768 Reset the system by typing PRINT USR 0 and try loading in the program from Microdrive. Should there be any problems, check carefully that all addresses were typed in correctly. We need to adopt a different strategy for programs with machine-code blocks with start below about 25000. For example, imagine a situation where the main block was 900 bytes starting at address 24000. Clearly, this would over- write the Microdrive Channel and Basic program if we were to load it in from Microdrive to this address. The solution is to load the code into a high address and use program 2 to generate a machine-code routine to copy it down to the correct place in memory. Program 2 does this by using the powerful - not to mention fast - LDIR instruc- tion. To continue the above example, we would start by loading in the code from tape to - for example - 30000. Run Program 2 [on the tzx under the name of "Block Copy"] and enter the following values: Block Start : 30000 Block Length : 9000 New Address : 24000 Execute Address : 24000 The execute address can be found by examining the Basic loader for that program. Program 2 places a 14 byte copy routine at "start + length", 39000 in this case. Save this to cartridge with something like SAVE *"m";1;"Fine Name"CODE 30000,9014 Note that the length has been extended by 14 to save both the code and the copy routine. Change the Basic loader to load in the machine-code from Microdrive, and the execute address - PRINT USR nnnnn - to be the start of the copy routine. Save this auto-run on Microdrive, reset the system and try it out. But what about machine-code blocks which are too large to re-locate using the above technique? For example, say we had a block which was 41535 bytes long starting from location 24000. This block fills the memory from the top right down to address 24000, and does not leave space for any Microdrive operations. What we need to do here is split the block into two, load the high address part into its proper location, load the low address part into the display file and then use the copy routine provided by program 2 to move the low block up to its correct address after all Microdrive operations are over. To continue the above example, first type: CLEAR 23999: LOAD ""CODE Copy the bottom 1000 bytes into the display file using a short program such as: 10 FOR x=0 TO 999 20 POKE 16386+x, PEEK (24000+x) 30 NEXT x and save this block to a temporary file on tape with SAVE "temp"CODE 16386,1000 Now do a CLEAR 24999 to make space for the Microdrive system and save the top part to Microdrive cartridge with SAVE *"m";1;"High"CODE 25000,40535 Reset the system, load in program 2 and then the temp code file from tape back into the display file. Start program 2 - by typing GO TO 10, not RUN which would clear the display file - and enter the following values: Block Start : 16386 Block Length : 1000 New Address : 24000 Execute Address : 24000 Save this to Microdrive using SAVE *"m";1;"Low"CODE 163868,1014 noting once again that the length must be increased by 14 to include the copy routine. Finally, alter the Basic loader so that it loads in "High" and "Low" from Microdrive before doing a PRINT USR 17386 Save this on the same cartridge, reset the system and take a deep breath before trying it out. One word of warning. In all cases, think carefully where the stack is - if this is overwritten by anything a system crash is certain. The position of the stack is controlled by the CLEAR statement so many problems like this can be avoided. With the help of the ideas presented in this article, it is possible to convert most programs to load from Micro- drive. The time spent waiting for programs to load becomes negligible which means that the Spectrum owner can spend more time playing games and less time sitting around with nothing to do. It is probably a good idea to have a menu program on each cartridge called "run" which lists the programs available. Program 3 [on the tzx called... guess!] gives an example of the one I use, and reduces program loading to three key presses - RUN, ENTER and n where n is the selected program.