; this is the patches installation code
; last update 10/01/2004

TestProcessor: move.l #uerr,errorbuf
    movea.l 4.w,a6
    lea dosname,a1
    moveq.l #37,d0
     jsr _LVOOpenLibrary(a6)
    tst.l d0
     beq.w FATAL
    move.l d0,DOSBase
    movea.l d0,a6
    move.l #template,d1
    move.l #opt0,d2
    moveq.l #NULL,d3
     jsr _LVOReadArgs(a6)
    move.l #errorstr1,errorbuf
    tst.l d0
     beq.w FAIL
    move.l d0,d1
     jsr _LVOFreeArgs(a6)
    tst.l opt0
     bne.b .nostr
    move.l #VersionStr,d1
     jsr _LVOPutStr(a6)
.nostr:
    tst.l opt1
     beq.b .do
    tst.l opt2
     beq.b .do
    tst.l opt3
     beq.b .do
    tst.l opt4
     beq.b .do
    tst.l opt6
     bne.w OK
.do
    move.l #errorstr2,errorbuf
    movea.l 4.w,a6
    btst #AFB_68020,AttnFlags+1(a6)
     beq.w FAIL
    tst.l opt5
     bne.w MMUInstall
AllocMem: move.l #MEMF_FAST|MEMF_REVERSE,d1
    move.l #EndPatch-StartPatch+15,d0
     jsr _LVOAllocMem(a6)
    move.l #errorstr3,errorbuf
    move.l d0,d6
     beq.w FAIL
CopyPatch: addi.l #15,d6
    andi.b #%11110000,d6
    move.l d6,MemForPatch
    movea.l d6,a1
cp2: lea StartPatch,a0
    move.l #EndPatch-StartPatch,d0
     jsr _LVOCopyMemQuick(a6)
    movea.l a6,a1
    movea.l MemForPatch,a4
    move.l a1,PExecBase-StartPatch(a4)
; patch intuition to open only simple refresh windows
;OpenIntuition: lea intuitionname,a1
;    moveq.l #0,d0
;     jsr _LVOOpenLibrary(a6)
    ; we should make some error check here
;    move.l d0,a5
;    move.l a5,a1
;    PATCH OpenWindow
;    move.l d0,oldopenwindow-StartPatch(a4)
;    move.l a5,a1
;     jsr _LVOCloseLibrary(a6)
OpenQuickLayers: tst.l opt6
     bne.b PatchUtility
    lea quicklayersname,a1
    moveq.l #0,d0
     jsr _LVOOpenLibrary(a6)
    move.l #errorstr5,errorbuf
    tst.l d0
     beq.w FAIL2
    tst.l opt0
     bne.b PatchUtility
    movea.l DOSBase,a6
    move.l #InstalledLayers,d1
     jsr _LVOPutStr(a6)
    movea.l 4.w,a6
PatchUtility: bsr.w Utility
PatchGraphics: tst.l opt3
     bne.w PatchExec
    lea graphname,a1
    moveq.l #39,d0
     jsr _LVOOpenLibrary(a6)
    move.l #errorstr4,errorbuf
    move.l d0,GFXBase
     beq.w FAIL2
    movea.l MemForPatch,a4
    move.l d0,PGfxBase-StartPatch(a4)
     bsr.w PrepareP2C
    movea.l GFXBase,a1
     jsr _LVOForbid(a6)
    tst.l gopt1
     bne.b .1
    PATCH WaitBlit
.1: tst.l gopt2
      bne.b .2
    PATCH QBlit
.2: tst.l gopt3
      bne.b .3
    PATCH WaitTOF
.3: tst.l gopt4
      bne.b .4
    PATCH RectFill
.4: tst.l gopt5
      bne.b .5
    PATCH SetAPen
.5: tst.l gopt6
      bne.b .6
    PATCH SetBPen
.6: tst.l gopt7
      bne.b .7
    PATCH SetDrMd
.7: tst.l gopt8
      bne.b .8
    PATCH OwnBlitter
.8: tst.l gopt9
      bne.b .9
    PATCH DisownBlitter
.9: tst.l gopt10
     bne.b .10
    PATCH SetABPenDrMd
.10: tst.l gopt11
      bne.b .11
     PATCH WritePixel
.11: tst.l gopt12
       bne.b .12
     PATCH LockLayerRom
.12: tst.l gopt13
      bne.b .13
     PATCH UnlockLayerRom
.13: tst.l gopt14
      bne.b .14
     PATCH WritePixelLine8
.14: tst.l gopt15
      bne.b .15
     PATCH WritePixelArray8
.15: tst.l gopt16
      bne.b .16
     PATCH WriteChunkyPixels
.16: tst.l gopt17
      bne.b .17
     PATCH ReadPixelLine8
.17: tst.l gopt21
    bne.b .18
    PATCH InitArea
.18 tst.l gopt22
    bne.b .22
    PATCH GetBitMapAttr
.22: tst.l gopt23
    bne.b .23
    PATCH ClearRegion
.23: tst.l gopt24
    bne.b .24
    PATCH DisposeRegion
.24: tst.l gopt26
      bne.b .27
    PATCH AddBob
.27: jsr _LVOCacheClearU(a6)
     jsr _LVOPermit(a6)
    tst.l opt0
     bne.b .Checks
    movea.l DOSBase,a6
    move.l #InstalledGfx,d1
     jsr _LVOPutStr(a6)
    movea.l 4.w,a6
.Checks:
     jsr _LVOForbid(a6)
    tst.l gopt18
     bne.b .19
    bsr.w BltClearCheck
.19:
     bsr.w FBlitCheck
     jsr _LVOCacheClearU(a6)
     jsr _LVOPermit(a6)
CloseGraphics: movea.l GFXBase,a1
     jsr _LVOCloseLibrary(a6)
PatchExec: tst.l opt2
     bne.w intrs
     bsr.w FasterForbid
     jsr _LVOForbid(a6)
     movea.l a6,a1
    tst.l exopt1
     bne.b .1
    PATCH Schedule
.1:  tst.l exopt2
      bne.b .2
  PATCH Reschedule
.2: tst.l exopt3
    bne.b .3
  PATCH PutMsg
.3: tst.l exopt4
      bne.b .4
 PATCH ReplyMsg
.4: tst.l exopt5
      bne.b .5
  PATCH Enqueue
.5: tst.l exopt6
      bne.b .6
  PATCH Signal
.6: tst.l exopt7
      bne.b .7
  PATCH SetSignal
.7: tst.l exopt8
      bne.b .8
  PATCH ExitIntr
.8: tst.l exopt9
      bne.b .9
  PATCH Dispatch
.9: tst.l exopt10
      bne.b .10
  PATCH Cause
.10: tst.l exopt11
      bne.b .11
    PATCH Exception
.11: tst.l exopt12
      bne.b .12
    PATCH Wait
.12: tst.l exopt13
      bne.b .13
   PATCH InitSemaphore
.13: tst.l exopt14
      bne.b .14
   PATCH AddSemaphore
.14: tst.l exopt15
      bne.b .15
    PATCH ReleaseSemaphore
.15: tst.l exopt16
      bne.b .16
    PATCH AddPort
.16: tst.l exopt17
      bne.b .17
    PATCH WaitPort
.17:  tst.l exopt18
       bne.b .18
   PATCH RemPort
.18:  tst.l exopt19
         bne.b .19
   PATCH RemSemaphore
.19:  tst.l exopt20
         bne.b .20
    PATCH RemResource
.20:  tst.l exopt21
         bne.b .21
    PATCH SetExcept
.21:  tst.l exopt22
         bne.b .22
    PATCH Insert
.22:  tst.l exopt23
         bne.b .23
    PATCH AddHead
.23:  tst.l exopt24
         bne.b .24
   PATCH Remove
.24:  tst.l exopt25
      bne.b .25
    PATCH RemHead
.25:  tst.l exopt26
         bne.b .26
   PATCH RemTail
.26: tst.l exopt28
    bne.b .28
   PATCH GetMsg
.28: tst.l exopt29
    bne.b .29
   PATCH DoIO
.29: tst.l exopt30
    bne.b .30
   PATCH WaitIO
.30: jsr _LVOCacheClearU(a6)
     jsr _LVOPermit(a6)
    tst.l opt0
     bne.b .testswitch
    movea.l DOSBase,a6
    move.l #InstalledExec,d1
     jsr _LVOPutStr(a6)
    movea.l 4.w,a6
.testswitch
    tst.l exopt27
     bne.b intrs
     bsr.w SwitchCheck
intrs: tst.l opt1
     bne.b EXIT
     bsr.w AutoInterrupt
    jsr _LVODisable(a6)
    movea.l MemForPatch,a0
    adda.l #BLIT-StartPatch,a0
    move.l a0,IVBLIT+IV_CODE(a6)
     jsr _LVOCacheClearU(a6)
     jsr _LVOEnable(a6)
    tst.l opt0
     bne.b EXIT
    movea.l DOSBase,a6
    move.l #InstalledInts,d1
     jsr _LVOPutStr(a6)
    movea.l 4.w,a6
EXIT: bra.b ProtectMem
 cnop 0,4
OK: movea.l 4.w,a6
    movea.l DOSBase,a1
     jsr _LVOCloseLibrary(a6)
    moveq.l #RETURN_OK,d0
   rts
  cnop 0,4
PrepareP2C:
    move.l d0,a0
    move.l gb_LayersBase(a0),PLayersBase-StartPatch(a4)
    move.l MemForPatch,d0
    add.l #p2c-StartPatch,d0
    move.l d0,_p2c-StartPatch(a4)
    move.l MemForPatch,d0
    add.l #FillRoutine-StartPatch,d0
    move.l d0,_FillRoutine-StartPatch(a4)
* makes the bitspread table *
make_table:
    movea.l a4,a1
    adda.l  #p2c-StartPatch,a1
    move.l  d2,a0
    moveq   #0,d0
more_table
    move.b  d0,d1

    moveq   #8-1,d2
byteloop
    btst    #7,d1
    beq.s   zero
    move.b  #1,(a1)+
    bra.s   one
zero    clr.b   (a1)+
one add.b   d1,d1
    dbra    d2,byteloop

    addq    #1,d0
    cmp.w   #256,d0
    bne.b   more_table
table_made
    move.l  a0,d2
    rts
