Here is the second of a short series of intermediate-level machine-code articles, for those of you who enjoyed playing with the CLS routines of issue 2:4. Unlike the last installment, however, the routine here is actually useful, not “just another pretty screen!”
One of the nice features of the Z80 is that it has a number of commands which actually are a whole subroutine in a single op-code or machine-language instruction. Quite probably the most powerful of these are the “block-transfer” group of commands. These allow you to easily transfer blocks of memory anywhere you wish, at a rapid rate; 16K can be transferred in about .1 sec. in FAST mode and on the Spectrum/2068, and in about 1/2 sec. in ZX81 SLOW mode. The LDIR (load-increment-repeat) command can be used for filling from the bottom up, and LDDR (load-decrement-repeat) goes the other way (allowing for data-block overlap either way). There are also “semi-automatic” commands (LDI, LDD) which allow you to jump out of the loop if you need to. Similarly, string searches and other jobs requiring comparisons go a lot easier using CPIR (compare-increment-repeat), CPDR, CPI and CPD commands. There are even similar “routines in an op-code” for input/output use (INIR, OTIR, etc.)
These commands all have one thing in common; simplicity of use. For example, the LDI/LDD group of commands only require that you:
1) Fill the BC register with the number of bytes to transfer,
2) Fill the DE register with the destination address (where you want it), and
3) Fill the HL register with the source address (where it is now).
4) Then LDIR or LDDR, and the move takes place.
The source block is left unchanged, unless of course the source and destination blocks overlap.
I’ve mentioned one use for this before, in this magazine and elsewhere; storing multiple programs in RAM and calling any at will. This is especially useful for ZX81’s with 64K, since the vast majority of software for the machine is designed for 16K. You could thus keep two full-16K programs in high memory (8000-BFFF and C000-FFFF), and bring either down into the operating area (4000-7FFF or 16-32K). See Bob Hartung‘s article in this issue for further information on these commands.
MANAGE UP TO “TROIS”
But imagine for a moment if you had a command, like LDIR, that SWAPS or exchanges two blocks of memory. You could have THREE separate programs available at all times! You can swap using LDIR or LDDR, of course, but the larger the block is the more memory you tie up for the temporary storage. To continue the same example, you still could only store two 16K programs at a time in 64K. Well, the short routine of Listing 1 gets around this. In addition to its usefulness, it might help you understand how the block transfer commands actually operate. It is analogous to LDIR; as with LDIR, you load BC with the number of bytes, and DE and HL with the start addresses of the blocks to swap. I call it “SWIR,” to reflect this similarity with the “canned” Z80 commands. Note that there would not be much point in having a separate “SWDR” routine to go the other direction, since you’d normally use this only on non-overlapping blocks. For a brain teaser, though, figure out what happens if the blocks do overlap, how to modify the routine to SWDR, and what this would do with overlapping blocks.
Note also that although this routine is comparatively slow (takes about twice as long as two uses of LDIR), it still swaps 16K in about 1/2 second in FAST mode. Another way of looking at it is that it will swap the contents of the ZX81 display file in about .1 sec. in SLOW mode.
So what can you do with it? Well, Listing 2 shows a couple short drivers to swap programs as mentioned above. You’ll have to use FIL1 and FIL2 to store two of the three programs you’ll be running. This is because the high-memory region will contain “garbage” or all zeros on power-up, and swapping this into the operating region will cause a crash. So here’s what you do:
1) Load the first program, and call FIL1 (RAND USR 8207).
2) Then load the second program and call FIL2 (RAND USR 8222).
3) Now load the third program.
4) Henceforth, instead of using NEW, use SWP1 (RAND USR 8238) and SWP2 (RAND USR 8243) to exchange the current program with the one stored at slot 1 (32-48K) or slot 2 (48-64K) respectively.
No matter how many times you call SWP1 and SWP2, you’ll always have all three 16K programs at your disposal. (Example: your filing program, HotZ II, and a program you’re working on.) Note that all three programs must have the same RAMTOP setting, or you’ll have stack problems after a swap. Also note that the SWP driver only swaps 3F20 bytes instead of the full 4000h (16K). This is to prevent overwriting the stack, while leaving ample room above E_LINE for most “16K” programs. I chose 3F20 because HotZ II’s program area ends at 7F20 (4000+3F20). If you’re dealing with REALLY cram-packed 16K programs, you might have to raise this somewhat. PRINT PEEK 16404+256*PEEK 16405 to get E_LINE of each program. Also PRINT PEEK 16386+256*PEEK 16387 to get ERR_SP. The value you place into 203C-203D (8252-8253d) should be higher than the highest E_LINE, but at least 40 bytes lower than the lowest ERR_SP.
LISTING 1 SWIR SUBROUTINE
2000 7E SWIR LD A,(HL) : GET CHARACTER FROM BLOCK 1
2001 F5 PUSH AF : SAVE IT
2002 1A LD A,(DE) : GET CHARACTER FROM BLOCK 2
2003 77 LD (HL),A : PUT IT INTO BLOCK 1
2004 F1 POP AF : GET CHAR. FROM BLOCK 1
2005 12 LD (DE),A : AND PUT IT INTO BLOCK 2
2006 23 INC HL : NEXT BLOCK 1 LOCATION
2007 13 INC DE : NEXT BLOCK 2 LOCATION
2008 0B DEC BC : DECREMENT COUNTER
2009 78 LD A,B : CHECK HIGH BYTE
200A B1 OR C : AND LOW BYTE
200B C8 RET Z : ALL DONE IF BOTH ARE ZERO
200C 18F2 JR SWIR : ELSE CONTINUE WITH NEXT CHAR.
LISTING 2 3-PROGRAM MANAGER
200F CD230F FIL1 CALL 0F23 : CALL FAST MODE
2012 010040 LD BC,4000 : NO. BYTES=16384
2015 110080 LD DE,8000 : DESTINATION=32768 (32K=SLOT1)
2018 210040 LD HL,4000 : SOURCE=16384
201B EDB0 LDIR : FILE PROGRAM IN SLOT 1
201D C9 RET : RETURN
201E CD230F FIL2 CALL 0F23 : CALL FAST MODE
2021 010040 LD BC,4000 : NO. BYTES=16384
2024 1100C0 LD DE,C000 : DESTINATION=49152 (48K=SLOT2)
2027 210040 LD HL,4000 : SOURCE=16384
202A EDB0 LDIR : FILE PROGRAM IN SLOT 2
202C C9 RET : RETURN
202D 00 NOP
202E 110080 SWP1 LD DE,8000 : SWAP WITH SLOT 1
2031 1803 JR SWAP
2033 1100C0 SWP2 LD DE,C000 : SWAP WITH SLOT 2
2036 D5 SWAP PUSH DE : SAVE SLOT LOCATION
2037 CD230F CALL 0F23 : CALL FAST MODE
203A D1 POP DE : GET SLOT LOCATION
203B 01203F LD BC,3F20 : NO. BYTES=16160
203E 210040 LD HL,4000 : EXCHANGE LOCATION=16384
2041 CD0020 CALL SWIR : SWAP CURRENT PROGRAM WITH SEL
ECTED SLOT. AND RETURN.
2044 C9 RET
Other Applications
For the 2068 (or ZX81 for that matter), you could use this to swap display files (Bob Hartung’s article), swap sets of variables (such as different loads of a file program), even different programs that share the same variables set. Your job will only be determining the source, destination, and number of bytes. Frequently some of this information will be contained in system variables, like D_FILE, VARS, etc.
I used it to create an alternate display file in the middle of the program area which could contain data and must not be overwritten. The problem was that you can’t move the display file into the top 16K of high memory directly, so I used SWIR to help out. Here’s how it works: SWIR exchanges the data in the “alternate display file” area with a screen located in hi-mem, then the D_FILE pointer is moved to the new location. When done, D_FILE is loaded with the original value, and SWIR is called again to restore the positions of the auxiliary screen and the data. There’s no reason you couldn’t take this further and have several video pages in hi-mem, swapping them about at will with a POKE (which slot) followed by a USR call (swap). This would be especially useful for the 2068, with its 6K+ display file.
How about using it within a program to swap two pieces of software that use the same region of memory? Often this is a lot easier than relocating tricky software packages. Simply use the swap routine to boot whichever chunk of code is needed at any given time.
I’m sure you can find applications of your own. Next time we’ll take a quick look over a simple check-sum routine, useful for verifying tape loads.
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.