home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v1.2 / amidev_cd_12.iso / reference / amiga_mail_vol1 / serparcia / wastetime < prev    next >
Text File  |  1990-01-26  |  4KB  |  135 lines

  1. (c)  Copyright 1989 Commodore-Amiga, Inc.   All rights reserved.
  2. The information contained herein is subject to change without notice, and 
  3. is provided "as is" without warranty of any kind, either expressed or implied.  
  4. The entire risk as to the use of this information is assumed by the user.
  5.  
  6.  
  7.  
  8.                        How To Waste Time
  9.  
  10.                         by Bryce Nesbitt
  11.         
  12.  
  13.  
  14. The worst way to insert a delay in an Amiga program is with a busy-loop
  15. like this:
  16.  
  17.          move.w #2000,d0
  18.     loop dbra   loop,d0
  19.  
  20.  
  21. If you are running under the multitasking operating system, the timer.device 
  22. can provide a delay that still lets other tasks run.  If you are taking over 
  23. the machine, then a loop like this is far better:
  24.  
  25.     loop btst.b #0,$bfed01
  26.          beq.s  loop
  27.  
  28. This uses one of the high speed timer chips to provide the delay.  A complete 
  29. example is shown below.  As you can see, the timers are easy to use - it only
  30. takes a few lines of code to run them.  This is a better way to delay for
  31. several reasons:
  32.  
  33. o  The software loop method fails to produce accurate timings under
  34.    many circumstances.  The speed depends on what CPU is installed 
  35.    in the system, what video mode is selected, what type of memory 
  36.    the program is in, the position of the vertical blank at run
  37.    time, what the blitter is doing, what interrupts are enabled and 
  38.    more other factors than you want to think about.
  39.  
  40. o  The timer chip can be "set and forgotten".  The chip does the
  41.    counting.  Your code can continue to get other things done while
  42.    the chip timer runs.
  43.  
  44. o   The timer can produce an interrupt when it is finished, or it
  45.     can set a bit you can examine at any time.
  46.  
  47. o   The timer can be set to automatically reload the count and
  48.     start again.  This gives even pulses, even if your software can't
  49.     respond to them immediately.
  50.  
  51.  
  52. Calculating the Time
  53. --------------------
  54.  
  55.  
  56. First some definitions:
  57.  
  58.     1 millisecond  (ms)  = 1/1,000 second
  59.     1 microsecond (us)  = 1/1,000,000 second
  60.     1 nanosecond  (ns)  = 1/1,000,000,000 second
  61.  
  62. On a stock 68000 based Amiga with no extra memory, the "DBRA"
  63. instruction listed above will take about 1.5 microseconds per
  64. loop.  So the loop above will waste about 3000 microseconds 
  65. (2000 * 1.5 = 3000)  This is the same as 3 miliseconds, or .003
  66. seconds.
  67.  
  68. Each 8520 chip has two 16 bit timers counting down at .715909
  69. Mhz, or 1.3968255 microseconds per tick.  To get the same 3
  70. milisecond delay with the 8520, we need to divide the desired
  71. time by the rate;  3000 / 1.3968255 = 2148.
  72.  
  73.  
  74. A Complete Example
  75.  
  76.  
  77. ;
  78. ; A complete 8520 timing example.  This blinks the power light at
  79. ; (exactly) 3 milisecond intervals.  It takes over the machine,
  80. ; so watch out!
  81. ;
  82. ;
  83. ; The base Amiga crytal frequecies are:
  84. ;        NTSC    28.63636  Mhz
  85. ;        PAL     28.37516  Mhz
  86. ;
  87. ;
  88. ;
  89. ; The two 16 bit timers on the 8520 chips each count down at 1/10
  90. ; the CPU clock, or .715909 Mhz.  That works out to 1.3968255
  91. ; microseconds per count.  Under PAL the countdown is a hair
  92. ; slower, .709379 Mhz.
  93. ;
  94. ; To wait 1/100 second would require waiting 10,000 microseconds.
  95. ; The timer register would be set to (10,000 / 1.3968255 =
  96. ; 7159).
  97. ;
  98. ; To wait 3 miliseconds would require waiting 3000 microsecsonds.
  99. ; The register would be set to (3000 / 1.3968255 = 2148).
  100. ;
  101. ; See the hardware manual for more information on the 8520 chips.
  102. ;
  103. ciaatalo    EQU $bfe401    ;Timer A low
  104. ciaatahi    EQU $bfe501    ;Timer A high
  105. ciaaicr     EQU $bfed01    ;Interrupt control register
  106. ciaacra     EQU $bfee01    ;Timer A control
  107.  
  108.         move.w    #$7fff,$dff09a        ;Kill all custom chip interrupts
  109.  
  110. ;----Setup, only do once
  111. ;----This sets timer A to one-shot mode.
  112.         move.b    ciaacra,d0        ;Set control register A on CIAA
  113.         and.b    #%11000000,d0        ;Don't trash the 60/50Hz flag
  114.         or.b    #%00001000,d0        ;or serial direction bits
  115.         move.b    d0,ciaacra
  116.         move.b    #%01111111,ciaaicr  ;Clear all 8520 interrupts
  117.  
  118. ;----Set time (low byte THEN high byte)
  119. ;----And the low order with $ff
  120. ;----Shift the high order by 8
  121.         move.b    #(2148&255),ciaatalo
  122.         move.b    #(2148>>8),ciaatahi
  123.  
  124. ;----Wait for the timer to count down
  125. busy_wait:
  126.         btst.b    #0,ciaaicr        ;Wait for timer expired flag
  127.         beq.s    busy_wait
  128.         bchg.b    #1,$bfe001        ;Blink light
  129.         bset.b    #0,ciaacra        ;Restart timer
  130.         bra.s    busy_wait
  131.  
  132.          END
  133.  
  134.  
  135.