home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / programs / system / cpuclear / source.lha / sources / CPUClr.asm next >
Assembly Source File  |  1992-10-20  |  16KB  |  423 lines

  1. ;****** Auto-Revision Header (do not edit) *******************************
  2. ;*
  3. ;* © This is Freeware (All rights are reserved by Peter Simons!)
  4. ;*
  5. ;* Filename         : CpuClr.asm
  6. ;* Created on       : 23-Jul-92
  7. ;* Created by       : Peter Simons
  8. ;* Current revision : V3.100
  9. ;*
  10. ;*
  11. ;* Purpose: This program replaces the system-routine BltClear() with a
  12. ;*          highly optimized 68020/30/40 routine.
  13. ;*
  14. ;*
  15. ;* V3.100 : released in: Kickstart-Magazin, Germany
  16. ;*                                               Peter Simons (20-Okt-92)
  17. ;*
  18. ;* V3.005 : added a "40" in the version-string, if the SPECIAL40-flag
  19. ;*          is set.
  20. ;*
  21. ;* V3.004 : rescuing registers before WaitBlit() is unnecessary :-)
  22. ;*
  23. ;* V3.003 : added special-68040-switch for safety (When set, the copyback
  24. ;*          is flushed after the clear-loop.)
  25. ;*
  26. ;* V3.002 : didn't rescue the registers before jumping into WaitBlit()
  27. ;*
  28. ;* V3.001 : used the wrong address-register in the copy-routine for
  29. ;*          long-aligned blocksize and word-aligned blockaddress
  30. ;*          Thanks to Stefan Thielscher for reporting.
  31. ;*
  32. ;* V3.000 : The new "perfect" version has been sold to the Kickstart-
  33. ;*          computer-magazin, Germany. Thanks for allowing me to
  34. ;*          distribute it via Fred Fish!
  35. ;*                                               Peter Simons (17-Okt-92)
  36. ;*
  37. ;* V2.104 : Fixed nasty bug, that caused NewBltClear() to crash with very
  38. ;*          small block-sizes.
  39. ;*          Thanks to Stefan Thielscher for reporting.
  40. ;*
  41. ;* V2.103 : Rewrote NewBltClear() completely! The clear-loop now clears
  42. ;*          as many long-words in a row as possible. This should maximize
  43. ;*          performance.
  44. ;*
  45. ;* V2.102 : CPUClear used to trash the window, if the size was chosen
  46. ;*          very small, so I added a WaitBlit().
  47. ;*          Thanks to Franz Schwarz for reporting the bug and Chris Hames
  48. ;*          for giving me the blitter-problem-hint!
  49. ;*
  50. ;* V2.101 : NewBltClr() has been rewritten completely for even more
  51. ;*          performance. NewBltClr() now uses the longword-loop only, if
  52. ;*          the address of the memoryblock is longword-aligned, not if
  53. ;*          the size is.
  54. ;*
  55. ;* V2.100 : released: Peter Simons (03-Okt-92)
  56. ;*
  57. ;* V2.024 : changed short branches from ".s" to Motorola's ".b"-style
  58. ;*
  59. ;* V2.023 : added security-check to catch odd numbers of byte to clear
  60. ;*
  61. ;* V2.022 : changed the way, the end of the "blit" is recognized from
  62. ;*          >BCC< to >BNE<. This saved 8 byte.
  63. ;*
  64. ;* V2.021 : test for zero-length-calls works now with bytes/row-mode, too
  65. ;*
  66. ;* V2.020 : Added a delay before the memory of NewBltClr() will be freed,
  67. ;*          because this could possibly crash the machine if the new
  68. ;*          routine is used while CPUClear tries to remove it.
  69. ;*
  70. ;* V2.011 : The last version included a trashed revision-date!
  71. ;*                                               Peter Simons (23-Aug-92)
  72. ;*
  73. ;* V2.010 : added cache-flushing-routines for 68040 compatiblity
  74. ;*
  75. ;* V2.000 : Final version! (??)        released: Peter Simons (24-Jul-92)
  76. ;*
  77. ;* V1.042 : fixed bug in CPU-recognition
  78. ;*
  79. ;* V1.040 : Changed addressing mode in clear-loop! Again saved a few
  80. ;*          bytes and cyles...
  81. ;*
  82. ;* V1.034 : I corrected a few typing-errors in the sourcecode
  83. ;*
  84. ;* V1.033 : I called SetFunction() with wrong parameters, which caused
  85. ;*          the machine to crash immediately. (I'll never find out why it
  86. ;*          worked on my Amiga all the time?? Don't ask! It's magic!)
  87. ;*
  88. ;* V1.032 : Added secure/unsecure switch
  89. ;*
  90. ;* V1.031 : released: Peter Simons (24-Jul-92)
  91. ;*
  92. ;* V1.030 : saved again 4 bytes by changing the addressing mode in the
  93. ;*          clear loop
  94. ;*
  95. ;* V1.028 : NewBltClr won't crash the machine any longer if a blocksize
  96. ;*          of ZERO is specified
  97. ;*
  98. ;* V1.027 : NewBltClr has only one exit-point now --> saves some bytes
  99. ;*
  100. ;* V1.026 : saved one swap-command in long-align-routine
  101. ;*
  102. ;* V1.025 : fixed minor bug: SIZE % 4 = 0 was NOT recognized by NewBltClr
  103. ;*
  104. ;* V1.020 : deleted opcodes in size-calculation: clearing the upper word
  105. ;*          was a waste, because MULU works only on WORD-size
  106. ;*
  107. ;* V1.011 : optimized output-routines and saved some bytes
  108. ;*
  109. ;* V1.010 : the way how the patch is installed, has been completely re-
  110. ;*          written and is shorter and, in my eyes, system-friendlier...
  111. ;*
  112. ;* V1.006 : program won't crash, if no standard-output is available!
  113. ;*          (for example, if started out of DME...)
  114. ;*
  115. ;* V1.005 : program checks for at least OS 2.04 and 68020 or higher
  116. ;*
  117. ;* V1.004 : changed program-output and added OS 2.0-version-string
  118. ;*
  119. ;* V1.003 : minor changes to the source-code
  120. ;*          revision header added
  121. ;*          changed to Macro68k-Assembler
  122. ;*
  123. ;* V1.002 : merged hunks --> program is position independent
  124. ;*
  125. ;* V1.001 : I continue development: Peter Simons (23-Jul-92)
  126. ;*
  127. ;* V1.000 : written by Oliver Wagner
  128. ;*
  129. ;* V0.000 : --- Initial release ---
  130. ;*
  131. ;*************************************************************************
  132. ;
  133. ;
  134.  
  135. ;***************************************************************************
  136. ;*                                                                         *
  137. ;* SEKTION: Labels, Macros, Switches, Structures                           *
  138. ;*                                                                         *
  139. ;***************************************************************************
  140.  
  141. ;-------------------------------------- Switches -----------
  142.                 MC68000
  143.                 EXEOBJ
  144.                 OBJFILE "RAM:CPUClr"
  145.                 NEWSYNTAX
  146.  
  147. SECURECODE      set     1       ; test everything that could possibly
  148.                                 ; crash the machine
  149.  
  150. SPECIAL40       set     0       ; flush copyback after clearing the memory
  151.  
  152. ;-------------------------------------- Exec ---------------
  153. eb_AttnFlags    equ     $0128
  154. lib_Version     equ     $0014
  155.  
  156. MEMF_PUBLIC     equ     1<<0
  157. AF_68020        equ     1
  158.  
  159. ;-------------------------------------- Labels -------------
  160.                 MACLIB  "DATAS:Offsets.preass"
  161. KICKVERSION     equ     37
  162.  
  163. _DOSBase        equr    A5
  164. _GfxBase        equr    A4
  165. _NewBltClr      equr    A3
  166. _OutputHandle   equr    D7
  167.  
  168. ;-------------------------------------- Macros -------------
  169. REVISION        MACRO
  170.                 dc.b "3.100"
  171.                 ENDM
  172. REVDATE         MACRO
  173.                 dc.b "20-Okt-92"
  174.                 ENDM
  175.  
  176.  
  177. ;***************************************************************************
  178. ;*                                                                         *
  179. ;* SEKTION: Program                                                        *
  180. ;*                                                                         *
  181. ;***************************************************************************
  182.  
  183. START:          move.l  ($4).w,a6
  184.         IFNE SECURECODE
  185.                 cmp.w   #KICKVERSION,(lib_Version,a6)   ; Kickstart 2.04+ ?
  186.                 bmi     .ErrorExit
  187.                 btst    #AF_68020,(eb_AttnFlags+1,a6)   ; 68020+ ?
  188.                 beq     .ErrorExit
  189.         ENDC
  190.                 lea     (dosname,PC),a1
  191.                 jsr     (_LVOOldOpenLibrary,a6)         ; open Dos-Library
  192.         IFNE SECURECODE
  193.                 tst.l   d0
  194.                 beq     .ErrorExit
  195.         ENDC
  196.                 move.l  d0,_DOSBase
  197.  
  198.                 lea     (gfxname,PC),a1
  199.                 jsr     (_LVOOldOpenLibrary,a6)         ; open Graphics-Lib
  200.         IFNE SECURECODE
  201.                 tst.l   d0
  202.                 beq     .CloseDosLib
  203.         ENDC
  204.                 move.l  d0,_GfxBase
  205.  
  206.                 move.l  _DOSBase,a6
  207.                 jsr     (_LVOOutput,a6)                 ; get standard-
  208.                 move.l  d0,_OutputHandle                ; outputhandle
  209.  
  210.                 move.l  _OutputHandle,d1
  211.         IFNE SECURECODE
  212.                 beq.b   .SkipWrite
  213.         ENDC
  214.                 lea     (Header,PC),a0                  ; write standard-
  215.                 move.l  a0,d2                           ; header
  216.                 moveq   #Header_len,d3
  217.                 jsr     (_LVOWrite,a6)
  218.  
  219. .SkipWrite      move.l  (_LVOBltClear+2,_GfxBase),_NewBltClr ; is the patch
  220.                 cmp.l   #'FAST',(new_ID,_NewBltClr)     ; already installed?
  221.                 beq.b   .RemovePatch                    ; Yep->remove
  222.  
  223. ;-------------------------------------- install patch ------
  224.  
  225.                 move.l  #NewBltClr_len,d0
  226.                 moveq   #MEMF_PUBLIC,d1
  227.                 move.l  ($4).W,a6
  228.                 jsr     (_LVOAllocVec,a6)
  229.         IFNE SECURECODE
  230.                 tst.l   d0                              ; Can't allocate
  231.                 beq.b   .CloseGfxLib                    ; buffer for new
  232.         ENDC                                            ; routine
  233.                 move.l  d0,_NewBltClr
  234.  
  235.                 lea     (NewBltClr,PC),a0
  236.                 move.l  _NewBltClr,a1                   ; copy patch into
  237.                 move.l  #NewBltClr_len,d0               ; reserved buffer
  238.                 jsr     (_LVOCopyMem,a6)
  239.  
  240.                 jsr     (_LVOForbid,a6)
  241.                 move.l  _GfxBase,a1
  242.                 move.w  #_LVOBltClear,a0                ; patch OS-function
  243.                 move.l  _NewBltClr,d0
  244.                 jsr     (_LVOSetFunction,a6)
  245.                 move.l  d0,(new_OldBltClr,_NewBltClr)   ; save old vector
  246.                 jsr     (_LVOCacheClearU,a6)            ; flush caches for
  247.                 jsr     (_LVOPermit,a6)                 ; '40 compatiblity
  248.  
  249.                 lea     (Install,PC),a0                 ; give feedback :-)
  250.                 moveq   #Install_len,d3
  251.                 bra.b   .exit
  252.  
  253. ;-------------------------------------- remove patch -------
  254. .RemovePatch    move.l  _GfxBase,a1
  255.                 move.w  #_LVOBltClear,a0
  256.                 move.l  (new_OldBltClr,_NewBltClr),d0
  257.                 move.l  ($4).W,a6
  258.                 jsr     (_LVOSetFunction,a6)            ; replace old
  259.                 move.l  d0,a2                           ; function and
  260.                                                         ; release memory
  261.                 move.l  _DOSBase,a6
  262.                 moveq   #100,d1
  263.                 jsr     (_LVODelay,a6)
  264.  
  265.                 move.l  a2,a1
  266.                 move.l  ($4).W,a6
  267.                 jsr     (_LVOFreeVec,a6)
  268.  
  269.                 move.l  _DOSBase,a6
  270.                 lea     (Remove,PC),a0
  271.                 moveq   #Remove_len,d3
  272.  
  273. ;-------------------------------------- exit gracefully ----
  274.  
  275. .exit           move.l  _OutputHandle,d1
  276.         IFNE SECURECODE
  277.                 beq.b   .CloseGfxLib
  278.         ENDC
  279.                 move.l  a0,d2
  280.                 move.l  _DOSBase,a6
  281.                 jsr     (_LVOWrite,a6)
  282.  
  283.                 move.l  _OutputHandle,d1
  284.                 lea     (Returns,PC),a0
  285.                 move.l  a0,d2
  286.                 moveq   #Returns_len,d3
  287.                 jsr     (_LVOWrite,a6)
  288.  
  289. .CloseGfxLib    move.l  _GfxBase,a1
  290.                 move.l  ($4).W,a6                       ; close resources
  291.                 jsr     (_LVOCloseLibrary,a6)           ; and leave
  292.  
  293. .CloseDosLib    move.l  _DOSBase,a1
  294.                 jsr     (_LVOCloseLibrary,a6)
  295.  
  296. .ErrorExit      moveq   #0,d0
  297.                 rts
  298.  
  299.  
  300. ;***************************************************************************
  301. ;*                                                                         *
  302. ;* SEKTION: new BltClear()-Routine                                         *
  303. ;*                                                                         *
  304. ;***************************************************************************
  305. NewBltClr:                      ; A1=&MemBlock A6=&GraphicsLibraryBase
  306.                                 ; D0=bytecount D1=flags
  307.  
  308.                 move.l  d2,a0                   ; save register
  309.                 btst    #1,d1                   ; bytes per row?
  310.                 beq.b   .ByteSize               ; Nope -> length is alright
  311.                 move.l  d0,d2                   ; Yep  -> calc size of block
  312.                 swap    d2
  313.                 mulu.w  d2,d0
  314. .ByteSize       bclr    #0,d0                   ; only even number of bytes
  315.                 tst.l   d0                      ; bytecount > 0 ?
  316.                 beq.b   .finished               ; Nope -> Exit!
  317.                 moveq   #0,d2                   ; clear memory or fill with
  318.                 btst    #2,d1                   ; pattern??
  319.                 beq.b   .DataOK
  320.                 move.l  d1,d2
  321.                 swap    d1                      ; --> fill with pattern
  322.                 move.w  d1,d2
  323. .DataOK         jsr     (_LVOWaitBlit,a6)       ; syncronisation!
  324.  
  325.                 btst    #1,d0
  326.                 beq.b   .LongSize
  327.                 move.w  a1,d1
  328.                 btst    #1,d1
  329.                 bne.b   .WordNotEasy
  330.  
  331.                 subq.l  #2,d0
  332.                 beq.b   .GoOn1
  333. ...             move.l  d2,(a1)+                ; blocksize word-aligned
  334.                 subq.l  #4,d0                   ; memoryblock long-aligned
  335.                 bne.b   ...
  336. .GoOn1          move.w  d2,(a1)+
  337.                 bra.b   .finished
  338.  
  339. .WordNotEasy    move.w  d2,(a1)+                ; blocksize word-aligned
  340.                 subq.l  #2,d0                   ; memoryblock word-aligned
  341.                 beq.b   .finished
  342. ...             move.l  d2,(a1)+
  343.                 subq.l  #4,d0
  344.                 bne.b   ...
  345.                 bra.b   .finished
  346.  
  347. .LongSize       move.w  a1,d1
  348.                 btst    #1,d1
  349.                 bne.b   .LongNotEasy
  350.  
  351. ...             move.l  d2,(a1)+                ; blocksize and memoryblock
  352.                 subq.l  #4,d0                   ; both long-aligned
  353.                 bne.b   ...
  354.  
  355. .finished       move.l  a0,d2
  356.         IFNE SPECIAL40
  357.                 pea     (a6)
  358.                 move.l  ($4).w,a6
  359.                 jsr     (_LVOCacheClearU,a6)    ; write back memory on '40
  360.                 move.l  (SP)+,a6
  361.         ENDC
  362.                 rts
  363.  
  364. .LongNotEasy    move.w  d2,(a1)+
  365.                 subq.l  #4,d0                   ; long-aligned blocksize
  366.                 bmi.b   .finished               ; word-aligned memoryblock
  367. ...             move.l  d2,(a1)+
  368.                 subq.l  #4,d0
  369.                 bne.b   ...
  370.                 move.w  d2,(a1)+
  371.                 bra.b   .finished
  372.  
  373.                 cnop    0,4
  374. new_ID          equ     (*-NewBltClr)           ; Offsets for routine-ID and
  375. .ID             dc.l    "FAST"                  ; buffer for OldBltClr
  376. new_OldBltClr   equ     (*-NewBltClr)
  377. .OldBltClr      dc.l    0
  378.  
  379. NewBltClr_len   equ     (*-NewBltClr)           ; length of routine
  380.  
  381. ;***************************************************************************
  382. ;*                                                                         *
  383. ;* SEKTION: Daten, Zeiger                                                  *
  384. ;*                                                                         *
  385. ;***************************************************************************
  386.  
  387. gfxname         dc.b    "graphics.library",0
  388. dosname         dc.b    "dos.library",0
  389.         IFNE SECURECODE
  390. Header          dc.b    $9B,"1;33",$6D
  391.                 dc.b    "$VER: CPUClr"
  392.            IFNE SPECIAL40
  393.                 dc.b    "40"
  394.            ENDC
  395.                 dc.b    " "
  396.                 REVISION
  397.                 dc.b    " ("
  398.                 REVDATE
  399.                 dc.b    ")"
  400.                 dc.b    $9B,$6D
  401.                 dc.b    " coding by Peter Simons",$0A
  402. Header_len      equ     (*-Header)
  403.         ELSE
  404.                 dc.b    "$VER: "
  405. Header          dc.b    "CPUClr"
  406.            IFNE SPECIAL40
  407.                 dc.b    "40"
  408.            ENDC
  409.                 dc.b    " "
  410.                 REVISION
  411.                 dc.b    ": "
  412. Header_len      equ     (*-Header)
  413.         ENDC
  414.  
  415. Install         dc.b    "Installed"
  416. Install_len     equ     (*-Install)
  417. Remove          dc.b    "Removed"
  418. Remove_len      equ     (*-Remove)
  419. Returns         dc.b    "...",$0A,$0A
  420. Returns_len     equ     (*-Returns)
  421.  
  422.                 END
  423.