Article #20632 (20848 is last): From: rigter@cafe.net (wilf rigter) Newsgroups: comp.sys.sinclair Subject: Re: Z80 Interrupts, what do they push? Date: Mon Mar 4 06:43:57 1996 In article <96030315051400631@vanilla.nbg.sub.org>, Guenter Woigk wrote: > >In article <4er2f2$qon@yama.mcc.ac.uk>, Simon Cooke writes: > >>: Also I would like to know if interrupts can occur in the middle of block >>: instructions as I currently allow it. >> >>Yep, it can. Effectively an LDIR, for example is this: >> >>Decode ED prefix, PC=PC+1 >>Decode LDIR instruction (176 ... can't remember the hex), PC=PC+1 >>(HL)->(DE) >>INC HL >>INC DE >>DEC BC >>if BC<>0 then PC=PC-2 >><> > >So if someone could give an approved answer or test it, i would be very pleased! > I tested this on my ZX81 in the (interupt driven) slow mode and I found no problem with executing 256 loops of LDIR of 8192 bytes. The screen is rock steady and the excution time is constant albeit slow! As was suggested, each of the 8192 LDIR itterations appears to be accompanied by a refresh cycle at which time the INT level and NMI latch is tested. Hope this helps. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%: N Article #20652 (20848 is last): From: rjc94@ecs.soton.ac.uk (Robert Cooper) Newsgroups: comp.sys.sinclair Subject: Re: Z80 Interrupts, what do they push? Date: Mon Mar 4 19:14:50 1996 >>>: Also I would like to know if interrupts can occur in the middle of block >>>: instructions as I currently allow it. >>>Yep, it can. Effectively an LDIR, for example is this: >>> [snip] >>So if someone could give an approved answer or test it, i would be >very pleased! >I tested this on my ZX81 in the (interupt driven) slow mode and I >found no problem with executing 256 loops of LDIR of 8192 bytes. The What happened me was that I was trying to copy a picture to the second screen on a 128K. With interrupts enabled, only part of the screen was copied. What seemed to happen was that the LDIR was entered, about 5000 bytes were copied, then the interrupt occured. When the interrupt ended, control was returned to the command after the LDIR, so only part of the screen was copied. Still, it's not too much hassle to disable interrupts, or even to split the LDIR command into 2 or more if interrupts are important. Hmm. Ica %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%: N Article #20658 (20848 is last): Newsgroups: comp.sys.sinclair From: Rob Turner Subject: Re: Z80 Interrupts, what do they push? Date: Mon Mar 4 21:38:13 1996 wilf rigter wrote: > > In article <96030315051400631@vanilla.nbg.sub.org>, > Guenter Woigk wrote: > > > >In article <4er2f2$qon@yama.mcc.ac.uk>, Simon Cooke writes: > > > >>: Also I would like to know if interrupts can occur in the middle > of block > >>: instructions as I currently allow it. > >> > >>Yep, it can. Effectively an LDIR, for example is this: > >> > >>Decode ED prefix, PC=PC+1 > >>Decode LDIR instruction (176 ... can't remember the hex), PC=PC+1 > >>(HL)->(DE) > >>INC HL > >>INC DE > >>DEC BC > >>if BC<>0 then PC=PC-2 > >><> > > > >So if someone could give an approved answer or test it, i would be > very pleased! > > > I tested this on my ZX81 in the (interupt driven) slow mode and I > found no problem with executing 256 loops of LDIR of 8192 bytes. The > screen is rock steady and the excution time is constant albeit slow! > As was suggested, each of the 8192 LDIR itterations appears to be > accompanied by a refresh cycle at which time the INT level and NMI > latch is tested. Hope this helps. > Are you sure interrupts can happen in the middle of an instruction? I thought that they had to wait at least for the end of an instruction. If you know the interrupt that is causing the problem, could you stop it or save all of the vital registers in the interrupt? In particular save the flags. Sounds like you know what you're on about anyway. -- Rob __________________________________________________ Eastwood 42.11 ma2rct@bath.ac.uk Bath University http://www.bath.ac.uk/~ma2rct Claverton Down Bath BA2 7JX (01225) 829006 8103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%: N Article #20679 (20848 is last): From: simonc@jumper.mcc.ac.uk (Simon Cooke) Newsgroups: comp.sys.sinclair Subject: Re: Z80 Interrupts, what do they push? Date: Tue Mar 5 11:49:52 1996 Rob Turner (ma2rct@bath.ac.uk) wrote: : > > Simon Cooke (simonc@jumper.mcc.ac.uk) wrote: : > >>Yep, it can. Effectively an LDIR, for example is this: : > >> : > >>Decode ED prefix, PC=PC+1 : > >>Decode LDIR instruction (176 ... can't remember the hex), PC=PC+1 : > >>(HL)->(DE) : > >>INC HL : > >>INC DE : > >>DEC BC : > >>if BC<>0 then PC=PC-2 : > >><> : Are you sure interrupts can happen in the middle of an : instruction? I thought that they had to wait at least for the : end of an instruction. Well, the way the LDIR works is that it acts like: {start of LDIR} LD (HL),(DE) INC DE INC HL DEC BC IF BC<>0, PC=PC-2 {end of instruction} After the instruction occurs, interrupts are tested. If there's a non-zero value in BC, the program counter will have been decremented by 2, and lo and behold, will be back at the start of the LDIR instruction again... This is why the LDIR takes so long (you'd expect it to be pretty quick if all it had was a read, write, and possible a maths instruction cycle - but it's not. However, they've altered it on the Z380 so that it works pretty much in this way, but added in some more interrupt detection to handle it). : If you know the interrupt that is causing the problem, could you : stop it or save all of the vital registers in the interrupt? In : particular save the flags. That'd do it. The main problem is if the interrupt routine is dodgy in some way, and doesn't ensure that all of the registers are correctly restored afterwards. : Sounds like you know what you're on about anyway. Thanks :) Simon -- +- Email:Simon.Cooke@umist.ac.uk ---- Fidonet: 2:250/124.2 (Simon Cooke) -+ | Snail: 18 Braemar Drive, Sale, Cheshire, M33 4NJ Tel: 0161 282 5051 | | Message Pager: 01426 208084 (55p per min peak, 35ppm offpeak) | +- WWW: http://jumper.mcc.ac.uk/~simonc ----------------------------------+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%: N Article #20762 (20848 is last): From: imc@ecs.ox.ac.uk (Ian Collier) Newsgroups: comp.sys.sinclair Subject: Re: Z80 Interrupts, what do they push? Date: Thu Mar 7 20:06:26 1996 In article <96030315051400631@vanilla.nbg.sub.org>, kio@vanilla.nbg.sub.org wrot e: >are you sure? >Though i agree upon the processing sheme I always thought, that a LDIR/LDDR/INI R/OTIR instruction is >an uninterruptable unit. It's not. It is a bit hard to demonstrate interrupts, but note that I have seen several protection systems (and indeed written one) that contain tricks working on the following principle. LD HL,4000H LD DE,4001H LD BC,0 LD (HL),C LDIR You might think this would wipe the whole memory and crash the machine, but in fact it stops after obliterating the 'ED' of the LDIR instruction. That's because it then turns into an ordinary "OR B" instruction. Ian Collier - imc@comlab.ox.ac.uk - WWW Home Page (including Spectrum section): http://www.comlab.ox.ac.uk/oucl/users/ian.collier/index.html New to this group? Answers to frequently-asked questions can be had from http://www.nvg.unit.no/spectrum/ . Sam Coupi FAQ: http://www.soton.ac.uk/~tsp93/Coupe/FAQ.txt %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%: N Article #20834 (20848 is last): From: rigter@cafe.net (wilf rigter) Newsgroups: comp.sys.sinclair Subject: Re: Z80 Interrupts, what do they push? Date: Sat Mar 9 23:14:02 1996 In article <8019.imc@comlab.ox.ac.uk>, imc@ecs.ox.ac.uk (Ian Collier) wrote: >It is a bit hard to demonstrate (Z80) interrupts, but note that >I have seen several protection systems (and indeed written one) >that contain tricks working on the following principle. > > LD HL,4000H > LD DE,4001H > LD BC,0 > LD (HL),C > LDIR > >You might think this would wipe the whole memory and crash the >machine, but in fact it stops after obliterating the 'ED' of the >LDIR instruction. >That's because it then turns into an ordinary "OR B" instruction. I hope you don't try to run this "protection principle" program using the suggested addresses on a ZX81 since it will most certainly crash after wiping out most of the system variable area. I guess for 4000 etc , read "user memory" or some such. Your example would show that the LDIR opcode is fetched every iteration until BC = 0. If the opcode is (self)modified, the Z80 will execute the new opcode and carry on. OK, but I don't understand how this demonstrates a "protection principle"? Can you give some more details? PS. You got me thinking about "protected mode" execution on a Z80, verrry interesting! regards %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%: N Article #20835 (20848 is last): From: elliott@teaching.physics.ox.ac.uk (John Elliott) Newsgroups: comp.sys.sinclair Subject: Re: Z80 Interrupts, what do they push? Date: Sat Mar 9 23:56:40 1996 In article <4hfbuq$510@wapping.ecs.soton.ac.uk>, Robert Cooper (rjc94@ecs.soton. ac.uk) wrote: : >>>: Also I would like to know if interrupts can occur in the middle of block : >>>: instructions as I currently allow it. : >>>Yep, it can. Effectively an LDIR, for example is this: : >>> [snip] : >>So if someone could give an approved answer or test it, i would be : >very pleased! : >I tested this on my ZX81 in the (interupt driven) slow mode and I : >found no problem with executing 256 loops of LDIR of 8192 bytes. The : What happened me was that I was trying to copy a picture to the second : screen on a 128K. With interrupts enabled, only part of the screen was : copied. Did you set BANKM (23388, or 5B5Ch) correctly? If not, then the interrupt routine will have changed the paging and the other half of the LDIR will have written to (or read from) the wrong memory bank. Correct paging code is: LD A,(5B5Ch) LD BC,1FFDh DI OUT (C),A LD (5B5Ch),A EI -------------------- http://sable.ox.ac.uk/~sjoh0132/ --------------------- John Elliott |BLOODNOK: "But why have you got such a long face?" |SEAGOON: "Heavy dentures, Sir!" - The Goon Show :-------------------------------------------------------------------------) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Path: vanilla!asbach!noris.net!blackbush.xlink.net!tank.news.pipex.net!pipex!sunsite.doc.ic.ac.uk!sunews!suma3!sgusmjam From: "James M. Smith" Newsgroups: comp.sys.sinclair Subject: LDIR - when it can be interrupted Date: Fri, 15 Mar 1996 16:49:42 +0000 Organization: University of Reading, U.K. Lines: 26 Message-ID: References: <4er2f2$qon@yama.mcc.ac.uk> <96030315051400631@vanilla.nbg.sub.org> <8019.imc@comlab.ox.ac.uk> <4ibihp$qqc@unix.sbu.ac.uk> <4ibmbm$ae0@yama.mcc.ac.uk> NNTP-Posting-Host: suma3-e2.reading.ac.uk Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII In-Reply-To: <4ibmbm$ae0@yama.mcc.ac.uk> The LDIR instruction is one of the slowest in the Z80 instruction set. I seem to remember it takes 16/21 T-states depending whether the loop has finished or not. Anyway, an interrupt is only processed once the current instruction has finished. What the LDIR instruction actually does is the following: - transfer (HL) to (DE) - inc HL - inc DE - dec BC - if BC <> 0 then PC=PC-2 where PC is the program counter. LDIR is an EDxx instruction, so it takes 2 bytes. 21 T-states is probably long-enough to upset the video timing on a ZX81 as not even an NMI can stop an instruction half way through. The LDIR instruction is in effect a recursive instruction, as it keeps decreasing the program counter to execute itself again. I got this information from the Zilog Z80 Technical Ref. manual Hope this solves the problem. James Smith. ---- Path: vanilla!asbach!noris.net!blackbush.xlink.net!tank.news.pipex.net!pipex!sunsite.doc.ic.ac.uk!unix.sbu.ac.uk!news From: Mike Child Newsgroups: comp.sys.sinclair Subject: Re: Z80 Interrupts, what do they push? Date: 15 Mar 1996 10:59:05 GMT Organization: Southbank University Lines: 25 Message-ID: <4ibihp$qqc@unix.sbu.ac.uk> References: <4er2f2$qon@yama.mcc.ac.uk> <96030315051400631@vanilla.nbg.sub.org> <8019.imc@comlab.ox.ac.uk> NNTP-Posting-Host: lr101-pc2.sbu.ac.uk Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 1.22 (Windows; I; 16bit) imc@ecs.ox.ac.uk (Ian Collier) wrote: > LD HL,4000H > LD DE,4001H > LD BC,0 > LD (HL),C > LDIR > >You might think this would wipe the whole memory and crash the machine, >but in fact it stops after obliterating the 'ED' of the LDIR instruction. >That's because it then turns into an ordinary "OR B" instruction. Hey thats really neat! What an aesthetically pleasing bit of code. I'm still confused about this interrupt question though - I'm positive that when I was learning to program my zx81 in mc the book I was using stated that use of LDIR etc. could cause a screen flicker in SLOW mode because the display routine couldn't interrupt it. Was this just plain wrong then? Were there differences between early Z80s and later? Or was *one* cycle of LDIR (effectively 6 consecutive instructions that couldn't be interrupted according to the scheme Simon Cooke gave) enough to cause an occaisional screen flicker? I've never tried it out incidentally. Mike Child ---- Path: vanilla!asbach!noris.net!blackbush.xlink.net!tank.news.pipex.net!pipex!warwick!lyra.csx.cam.ac.uk!mgr11 From: mgr11@cus.cam.ac.uk (M.G. Rison) Newsgroups: comp.sys.sinclair Subject: Re: Z80 Interrupts, what do they push? Date: 15 Mar 1996 15:07:28 GMT Organization: University of Cambridge, England Lines: 46 Message-ID: <4ic13g$mle@lyra.csx.cam.ac.uk> References: <4er2f2$qon@yama.mcc.ac.uk> <8019.imc@comlab.ox.ac.uk> <4ibihp$qqc@unix.sbu.ac.uk> <4ibmbm$ae0@yama.mcc.ac.uk> Reply-To: rison@hep.phy.cam.ac.uk NNTP-Posting-Host: ursa.cus.cam.ac.uk En la artikolo <4ibmbm$ae0@yama.mcc.ac.uk>, Simon Cooke skribis: > Mike Child (childm@vax.sbu.ac.uk) wrote: > [LDIRs and how they work] > : I'm still confused about this interrupt question though - I'm positive > : that when I was learning to program my zx81 in mc the book I was using > : stated that use of LDIR etc. could cause a screen flicker in SLOW mode > : because the display routine couldn't interrupt it. Was this just plain > : wrong then? Were there differences between early Z80s and later? Or was > : *one* cycle of LDIR (effectively 6 consecutive instructions that couldn't > : be interrupted according to the scheme Simon Cooke gave) enough to cause > : an occaisional screen flicker? I've never tried it out incidentally. > I think you're about right there - the LDIR instruction is (AFAICR) the > longest instruction on the Z80, so it would probably be correct to say > that this would cause the flicker. I suspect the book Mike is referring to is `Mastering machine code on your ZX81 (or ZX80) '. This does indeed seem to imply that block load instructions cannot be interrupted, and is indeed (AFAICT) plain wrong. As someone else has said, the block load instructions simply do: - do the non-repeating version - if BC<>0 then PC=PC-2 Thus they can be interrupted after every byte copy, and if the copy squashes the &ED of the instruction, then the block load operation will stop. An example is BMX Simulator (on the CPC, at least), which will crash if this is not emulated in the exact way described above. BTW, AFAICT there are a few instructions which take even longer than one step of a block load operation, e.g. EX (SP),IX and RES n,(IX+d). But I never tried to see whether they'd cause flicker on a ZX81. Ah, that book was great. I learnt machine code thanks to it. Did anybody actually type in the draughts program? I couldn't be bothered: it seemed as if a lot of fiddling around was necessary to get the whole thing together... Mark ====================================================================== | rison@hep.phy.cam.ac.uk | Esperanto - lingvo inter-nacia | | rison@vxcern.cern.ch | * Mi estas riisto * | ====================================================================== ---- Path: vanilla!asbach!noris.net!blackbush.xlink.net!tank.news.pipex.net!pipex!warwick!bham!bhamcs!news.ox.ac.uk!news From: imc@ecs.ox.ac.uk (Ian Collier) Newsgroups: comp.sys.sinclair Subject: Re: Z80 Interrupts, what do they push? Date: 15 Mar 1996 15:07:37 GMT Organization: Oxford University Computing Laboratory Lines: 33 Message-ID: <8088.imc@comlab.ox.ac.uk> References: <4er2f2$qon@yama.mcc.ac.uk> <8019.imc@comlab.ox.ac.uk> <4ibihp$qqc@unix.sbu.ac.uk> <4ibmbm$ae0@yama.mcc.ac.uk> NNTP-Posting-Host: boothp2.ecs.ox.ac.uk X-Local-Date: Friday, 15th March 1996 at 3:07pm GMT In article <4ibmbm$ae0@yama.mcc.ac.uk>, simonc@jumper.mcc.ac.uk (Simon Cooke) wrote: >: Or was >: *one* cycle of LDIR (effectively 6 consecutive instructions that couldn't >: be interrupted according to the scheme Simon Cooke gave) enough to cause >: an occaisional screen flicker? I've never tried it out incidentally. >I think you're about right there - the LDIR instruction is (AFAICR) the >longest instruction on the Z80, so it would probably be correct to say >that this would cause the flicker. Well, LDIR takes 21 cycles and is among the longest. But the joint winners with 23 cycles are EX (SP),IX and all the instructions that access (IX+d) twice, for instance INC (IX+d) (and of course the equivalent IY instructions). Incidentally, it seems that you can make an uninteruptable instruction of any length by prefixing it with an arbitrary number of FD or DD bytes; the Z80 can also not be interrupted immediately after an EI or DI so you could have some fun there. :-) Whether this makes any difference to the ZX81 screen depends on what it does between getting an interrupt and displaying the first line. I would think that there must be another interrupt to indicate the first line, because otherwise even a simple instruction like LD DE,10 would move the first line right by one or two character squares. So I don't expect an LDIR would make any difference. Ian Collier - imc@comlab.ox.ac.uk - WWW Home Page (including Spectrum section): http://www.comlab.ox.ac.uk/oucl/users/ian.collier/index.html New to this group? Answers to frequently-asked questions can be had from http://www.nvg.unit.no/spectrum/ . Sam Coupé FAQ: http://www.soton.ac.uk/~tsp93/Coupe/FAQ.txt