[comp.sys.amiga.programmer] Fast timing

rrmorris@uokmax.ecn.uoknor.edu (Rodney Raym Morrison) (04/02/91)

  What is the best/fastest way to have a timer execute a task/routine?
(for a real-time game, etc.)

 I wrote a program that adds a function to the VBI chain (Matt Dillon says
this is the worst/slowest way to do this).

mykes@amiga0.SF-Bay.ORG (Mike Schwartz) (04/03/91)

In article <1991Apr1.215106.1911@uokmax.ecn.uoknor.edu> rrmorris@uokmax.ecn.uoknor.edu (Rodney Raym Morrison) writes:
>
>  What is the best/fastest way to have a timer execute a task/routine?
>(for a real-time game, etc.)
>
> I wrote a program that adds a function to the VBI chain (Matt Dillon says
>this is the worst/slowest way to do this).

"timer" implies one of the CIAs, while VBI chain implies vertical interrupts.

The ROM Kernel Manuals tell you how to use timer.device to use the CIAs in
a friendly way.  The low-level disk drive access routine (pieces) I've posted
recently show how to directly bang at the CIAs, but they busy wait.  To make
them generate interrupts, you'd have to install a CIA interrupt handler in
AutoVector #2 and enable the interrupt in the INTENA register.  

The VBI chain is a linked list of interrupt handler structures that the OS
walks through each VBL interrupt and executes the routines.  Matt was only
indicating that this is a SLOW process and costs a bunch of CPU cycles.  He
was also pointing out that it's possible to not even use interrupts at ALL
and that the overhead for the interrupt/RTE alone is significant.  If
you are taking over the machine, you can just install your VBL interrupt
handler in AutoVector #5.  This vector is also used for blitter and copper
interrupts.

The OS provides a routine called SetVector() to allow you to install
interrupt handlers in the "approved" legal OS manner.

Something that I do in programs that I only intend to run on my own machine
(because I have control over what software I run) is very PC-ish.  You can
save the contents of an autovector and install your own handler in the vector.
When the VBL interrupt (for example) happens, you check INTREQR ($dff01e) to
see if indeed it was a VBL interrupt.  If not, you just jump to the old vector.
If it is VBL, you can do whatever you want.  If you jump to the old vector
upon completing your VBL task, the OS works fine.  You can also clear the
INTREQ bit for pending VBLs and RTE, effectively killing the OS until you
want to restore it.  You should restore the vector before the program exits.

Now for the downside of this practice.  You SHOULD NEVER do this in something
you are going to sell that is supposed to be OS compatible.  If you have 2
programs that do this, it works as long as both are running, but the order
in which they exit becomes VITAL (can you say GURU).  This trick does NOT
work for AutoVector #2 (CIAs) because the CIAs themselves are sources of
at least 2 interrupts (keyboard and timer).  You CAN read INTREQR to determine
whether the interrupt source is BLITTER/COPPER/VBL, but when you read the
CIA's interrupt pending bits, it CLEARS the CIA's register.  In other words,
you can't be transparent to the OS' handler, and the keyboard will certainly
stop working.


DISCLAIMER/CAVEAT:

Again, the software that uses these techniques should ONLY be run on your
machine and should never leave it.  These techniques are exactly what TSR
programs on the PC do and is one of the reasons the PC is often troublesome
to use.  I only describe how to do this because it might help ONE person
do work on HIS machine.

--
********************************************************
* Appendix A of the Amiga Hardware Manual tells you    *
* everything you need to know to take full advantage   *
* of the power of the Amiga.  And it is only 10 pages! *
********************************************************