; patches for exec.library
; last update 13/12/2003

; DoIO(ioRequest)(a1)
DoIO:      move.l     A1,-(A7)
           move.l     A6,-(a7)
           move.b     #1,IO_FLAGS(A1)
           movea.l    IO_DEVICE(A1),A6
            jsr        -$1E(A6)
           movea.l    (A7)+,A6
           movea.l    (A7)+,A1
WaitIO:    btst       #0,IO_FLAGS(A1)
            bne.b      doio_62
           movea.l    MN_REPLYPORT(A1),A0
           moveq.l    #1,D0
           move.b     $F(A0),D1
           lsl.l      D1,D0
           move.w     #$4000,_custom+INTENA
           addq.b     #1,IDNestCnt(A6)
           move.l     a1,-(a7)
doio_38:   cmpi.b     #7,$8(A1)
            beq.b      doio_48
            bsr.w Wait ;jsr        _LVOWait(A6)
           movea.l    (A7),A1
          bra.b      doio_38
doio_48:   movea.l    (A1)+,A0
           movea.l    (A1),A1
           move.l     A0,(A1)
           move.l     A1,$4(A0)
           subq.b     #1,IDNestCnt(A6)
            bge.b      doio_60
           move.w     #$C000,_custom+INTENA
doio_60:   movea.l    (A7)+,A1
doio_62:   move.b     IO_ERROR(A1),D0
           extb.l     D0
          rtS

ContextRestore:
    moveq.l #$20,d1
    move.b  (a5),d0
     beq.b .22
    addq.l #2,a5
    fmovem.l (a5)+,fpiar/fpsr/fpcr
    fmovem.x (a5)+,fp0/fp1/fp2/fp3/fp4/fp5/fp6/fp7
    cmp.b #$90,d0
     bne.b .20
    move.l (a5)+,-(a7)
    move.l (a5)+,-(a7)
    move.l (a5)+,-(a7)
    move.w #$9020,d1
.20 addq.l #2,a5
.22 frestore (a5)+
    lea $42(a5),a2
    move.l a2,usp
    move.l (a5)+,d2
    moveq.l #$10,d3
    move.l d2,d0
    lsl.l d3,d2
    move.w (a5)+,d0
    move.w d1,d2
    rol.l d3,d0
    movem.l d0/d2,-(a7)
    movem.l (a5),d0-d7/a0-a6
   rte

SwitchFPU:
    move.l a5,-(a7)
    move.l usp,a5
    movem.l d0-d7/a0-a6,-(a5)
    movea.l PExecBase(pc),a6
    moveq.l #-$1,d4
    move.w $126(a6),d0
    move.w d4,$126(a6)
    move.w #$c000,_custom+intena
    move.l (a7)+,$34(a5)
    moveq.l #$10,d3
    movem.l (a7)+,d1-d2
    rol.l d3,d1
    rol.l d3,d2
    move.w d1,-(a5)
    move.w d2,d1
    lsr.l d3,d2
    move.l d1,-(a5)
    fsave -(a5)
    tst.b (a5)
     beq.b aa      ;$f812aa
    andi.w #$F000,d2
    subq.l #2,a5
    cmp.w #-$7000,d2
     bne.b .143c
    move.l (a7)+,-(a5)
    move.l (a7)+,-(a5)
    move.l (a7)+,-(a5)
    move.w d2,d4
.143c fmovem.x fp0/fp1/fp2/fp3/fp4/fp5/fp6/fp7,-(a5)
    fmovem.l fpiar/fpsr/fpcr,-(a5)
    move.w d4,-(a5)
   bra.b aa       ;$f812aa

SwitchNOFPU:
  move.l A5,-(A7)
  move.l usp,A5
  movem.l D0-D7/A0-A6,-(A5)
  movea.l PExecBase(PC),A6
  move.w $126(A6),D0
  move.w #$FFFF,$126(A6)
  move.w #$C000,_custom+intena
  move.l (A7)+,$34(A5)
  move.w (A7)+,-(A5)
  move.l (A7)+,-(A5)
aa:
  movea.l $114(A6),A3
  movea.l $230(A6),A4 ; should be the context restore routine
  move.w D0,$10(A3)
  move.l A5,$36(A3)
  btst #$6,$E(A3)
   beq.b JL_0_18CA
  movea.l $42(A3),A5
L_0_18C8: jsr (A5)
JL_0_18CA: lea $196(A6),A0
  lea $124(A6),A5
  move.w #$2700,sr
  movea.l (A0),A3
  move.l (A3),D0
   bne.b JL_0_18F4
  move.w #$2700,D1
JL_0_18E0:
  addq.l #1,$118(A6)
  ori.b #$80,(A5)
 stop #$2000
JL_0_18EC:
  move.w D1,sr
  movea.l (A0),A3
  move.l (A3),D0
   beq.b JL_0_18E0
JL_0_18F4:
  subq.l #4,A5
  move.l D0,(A0)
  move.l A3,$114(A6)
  movea.l D0,A1
  move.l A0,$4(A1)
  move.w (A5)+,(A5)+
  andi.w #$BFFF,(A5)+
  move.b #$2,$F(A3)
  move.w $10(A3),(A5)
   bmi.b JL_0_191E
  move.w #$4000,_custom+intena
JL_0_191E:
  move.w #$2000,sr
  moveq.l #-$60,d0
  addq.l #1,$11C(A6)
  and.b $e(a3),D0
   beq.b JL_0_1932
   bsr.b JL_0_1938
JL_0_1932:  movea.l $36(A3),A5
L_0_1936: jmp (A4)

JL_0_1938: move.b d0,d2
   bpl.b JL_0_1948
  movea.l $46(A3),A5
L_0_1944: jsr (A5)
JL_0_1948: lsr.b #6,d2
   bcs.w Exception
 rtS

GetMsg  LEA $14(A0),A0
    moveq.l #0,d0
    CMPA.L  $8(A0),A0
    BEQ.B   .LB_5570
.LB_5548 MOVE.W  #$4000,_custom+intena
    ADDQ.B  #1,$126(A6)
    MOVE.L  (A0),A1
    MOVE.L  (A1),D0
    BEQ.B   .LB_5562
    MOVE.L  D0,(A0)
    EXG.L D0,A1
    MOVE.L  A0,$4(A1)
.LB_5562 SUBQ.B  #1,$126(A6)
    BGE.B   .LB_5570
    MOVE.W  #$C000,_custom+intena
.LB_5570 RTS

Insert:
  move.l A2,D0
   beq.b AddHead
  move.l (A2),D0
   beq.b 281$
  movea.l D0,A0
  movem.l D0/A2,(A1)
  move.l A1,$4(A0)
  move.l A1,(A2)
 rtS

281$:
  move.l A2,(A1)
  movea.l $4(A2),A0
  move.l A0,$4(A1)
  move.l A1,$4(A2)
  move.l A1,(A0)
 rtS

AddHead:
  move.l (A0),D0
  move.l A1,(A0)
  movem.l D0/A0,(A1)
  movea.l D0,A0
  move.l A1,$4(A0)
 rtS

Remove:
  movea.l (A1)+,A0
  movea.l (A1),A1
  move.l A1,$4(A0)
  move.l A0,(A1)
 rtS

RemHead:
  movea.l (A0),A1
  move.l (A1),D0
   beq.b 290$
  move.l D0,(A0)
  exg.l D0,A1
  move.l A0,$4(A1)
290$:
 rtS

; note: original routine trashes a1/d0
;       this routine trashes a0-a1/d0
RemTail:   ;RemTail(list)(a0)
  movea.l 8(A0),A1
  move.l 4(A1),D0
   beq.b 293$
  move.l D0,8(A0)
  exg.l D0,A1
  addq.l #4,a0
  move.l A0,(A1)
  ;addq.l #4,(A1)
293$: rtS

WaitPort:
  movea.l $14(A0),A1
  tst.l (A1)
   bne.b 204$
  move.b $F(A0),D1
  moveq.l #$01,D0
  lsl.l d1,d0
  move.l A2,-(A7)
  lea $14(a0),a2
203$:
   bsr Wait
  movea.l (a2),a1
  tst.l (A1)
   beq.b 203$
  move.l (a7)+,a2
204$:
  move.l A1,D0
 rtS

RemResource:
RemSemaphore:
RemPort:
  addq.b #1,TDNestCnt(A6)
  movea.l (A1)+,A0
  movea.l (A1),A1
  move.l A0,(A1)
  move.l A1,$4(A0)
 jmp _LVOPermit(a6)

;*********************
ReleaseSemaphore
        ; really must do stuff in Forbid
        addq.b #1,$127(a6)    ; jsr (_LVOForbid,a6)

        ; release once. are we still locked?
        subq.w  #1,(SS_NESTCOUNT,a0)
        bne.b   .nested

        clr.l   (SS_OWNER,a0)       ; no one own it now.

        ; time to wake others up?
        subq.w  #1,(SS_QUEUECOUNT,a0)
        bge.b   .wake

        ; no-one is waiting, so go away immeditially
        jmp (_LVOPermit,a6)     ; exit

.nested
        ; if this condition is true, there has been more
        ; Releases than Obtains.
        bmi.b   .alert

        subq.w  #1,(SS_QUEUECOUNT,a0)
.exit       jmp (_LVOPermit,a6)     ; exit


.alert      movem.l d0-d1/d7/a0-a1,-(sp)
        move.l  #AN_SemCorrupt,d7
        jsr (_LVOAlert,a6)
        move.l  (3*4,sp),a0     ; restore semaphore ptr from stack
        jsr (_LVOInitSemaphore,a6)  ; reinittialize
        movem.l (sp)+,d0-d1/d7/a0-a1
        jmp (_LVOPermit,a6)

.wake
        movem.l d0/d1/a0-a3,-(sp)
        move.l  a0,a2           ; a2 = semaphore

        ; get the first waiter

        lea (SS_WAITQUEUE,a0),a0
        move.l  (a0),a1
        move.l  (a1),d0
        beq.b   .poof

        move.l  d0,(a0)
        exg d0,a0
        move.l  d0,(LN_PRED,a0)

        move.l  (SSR_WAITER,a1),d0  ; d0 = task ptr or NULL
        beq.b   .wakeproc

        bclr    #0,d0
        bne.b   .wakeshrd

        move.l  d0,(SS_OWNER,a2)
.simple
        bsr.b   .replysig
.poof
        movem.l (sp)+,d0/d1/a0-a3
        jmp (_LVOPermit,a6)     ; exit


.wakeproc
        move.l  (SSM_SEMAPHORE,a1),(SS_OWNER,a2)
        move.l  (SS_WAITQUEUE,a2),a3

.wakel      bsr.b   .replymsg

        move.l  (SS_OWNER,a2),d1    ; d1 = owner task

.lookl      move.l  (a3),d0
        beq.b   .poof

        move.l  a3,a1
        move.l  d0,a3
        move.l  (SSR_WAITER,a1),d0  ; d0 = waiting task
        beq.b   .isproc

        cmp.l   d0,d1           ; same task?
        bne.b   .lookl          ; is not, look more

        ; remove the node
        move.l  (LN_PRED,a1),a0
        move.l  a3,(a0)
        move.l  a0,(LN_PRED,a3)
        ; d0 = d1 = task pointer
        bra.b   .simple         ; signal it & exit

.isproc     cmp.l   (SSM_SEMAPHORE,a1),d1   ; is the same task?
        bne.b   .lookl          ; nope, look more

        ; another message with the same task.
        ; remove the node but do *NOT* bump SS_NESTCOUNT
        ; since .wakel will do it.
        move.l  (LN_PRED,a1),a0
        move.l  a3,(a0)
        move.l  a0,(LN_PRED,a3)
        bra.b   .wakel          ; wake it & check more


.wakeshrd
        move.l  (SS_WAITQUEUE,a2),a3

.wakeit     pea (.checkm,pc)        ; pop out in here
        tst.l   d0          ; is it a Procure?
        bne.b   .replysig       ; if not, we do a signal

        clr.l   (SSR_WAITER,a1)     ; clear SSR_WAITER
.replymsg   move.l  a2,(SSM_SEMAPHORE,a1)
        addq.w  #1,(SS_NESTCOUNT,a2)
        bra.w ReplyMsg ;jmp (_LVOReplyMsg,a6)   ; reply it / pop out in .checkm

.replysig
        move.l  d0,a1           ; a1 = task
        moveq   #SIGF_SINGLE,d0
        addq.w  #1,(SS_NESTCOUNT,a2)
        bra.w Signal ;jmp (_LVOSignal,a6)


.checkm     move.l  (a3),d0
        beq.b   .poof

        move.l  a3,a1
        move.l  d0,a3
        move.l  (SSR_WAITER,a1),d0
        bclr    #0,d0
        beq.b   .checkm         ; check more

        ; remove the node
        move.l  (LN_PRED,a1),a0
        move.l  a3,(a0)
        move.l  a0,(LN_PRED,a3)
        bra.b   .wakeit         ; wake it & check more
;*********************

InitSemaphore:  ;InitSemaphore(sigSem)(a0)
    MOVE.B  #$0F,8(A0)
    CLR 14(A0)
    CLR.L   40(A0)
    MOVE    #$FFFF,44(A0)
    LEA 16(A0),A0
    MOVE.L  A0,8(A0)
    ADDQ.L  #4,A0
    CLR.L   (A0)
    MOVE.L  A0,-(A0)
    RTS

AddSemaphore:
    MOVEA.L A1,A0
    MOVE.L  A1,-(A7)
    MOVE.B  #$0F,8(A0)
    CLR 14(A0)
    CLR.L   40(A0)
    MOVE    #$FFFF,44(A0)
    LEA 16(A0),A0
    MOVE.L  A0,8(A0)
    ADDQ.L  #4,A0
    CLR.L   (A0)
    MOVE.L  A0,-(A0)
    MOVEA.L (A7)+,A1
    LEA 532(A6),A0
    BRA.S   LAB_012D

AddPort:
    LEA 20(A1),A0     ;port->mp_msgList.lh_TailPred=&port->mp_MsgList
    MOVE.L  A0,8(A0)

    ADDQ.L  #4,A0     ;port->mp_MsgList.lh_Tail=NULL;
    CLR.L   (A0)
    MOVE.L  A0,-(A0)  ;port->mp_SigTask=&port->mp_MsgList

    LEA 392(A6),A0         ;SysBase->PortList in a0
LAB_012D: ADDQ.B  #1,295(A6)      ; forbid()

    MOVE.L  (A0),D0         ; enqueue(&SysBase->PortList,&port->mp_Node)
    MOVE.B  9(A1),D1        ;               a0              a1
LAB_012E: MOVEA.L D0,A0
    MOVE.L  (A0)+,D0
    BEQ.S   LAB_012F
    CMP.B   5(A0),D1
    BLE.S   LAB_012E
LAB_012F: MOVE.L  (A0),D0
    MOVE.L  A1,(A0)
    SUBQ.L  #4,A0
    MOVE.L  A0,(A1)
    MOVEA.L D0,A0
    MOVE.L  D0,4(A1)
    MOVE.L  A1,(A0)

    JMP _LVOPermit(A6)

Wait:  ;Wait(signalSet)(d0)
  move.l a2,-(a7)
  lea _custom+intena,a2
  movea.l ThisTask(A6),A1
  move.l D0,TC_SIGWAIT(A1)
  move.w #$4000,(a2)
  and.l TC_SIGRECVD(A1),D0
   bne.b 219$
  addq.b #1,$127(A6)
218$:
  move.b #TS_WAIT,TC_STATE(A1)
  lea $1a8(A6),A0     ;TASKWAIT->LH_TAIL
  move.l 4(A0),D0
  move.l A1,4(A0)
  move.l a0,(a1)
  move.l d0,a0
  move.l d0,$4(a1)
  move.l A1,(A0)
  movea.l A5,A0
  move.b $126(A6),D1
  St $126(A6)
  movea.l _LVOSwitch+2(A6),A5
  move.w #$C000,(a2)
   jsr _LVOSupervisor(A6)
  move.w #$4000,(a2)
  move.b D1,$126(A6)
  movea.l A0,A5
  move.l $16(A1),D0
  and.l $1a(A1),D0
   beq.b 218$
  subq.b #1,$127(A6)
219$:
  eor.l D0,$1a(A1)
  tst.b $126(A6)
   bge.b 220$
  move.w #$C000,(a2)
220$:
  movea.l (a7)+,a2
 rtS

ExitIntr:
    MOVEA.L (a7)+,A6
    BTST    #5,($18,a7)
    BNE.B   .lbC000016
    TST.B   ($127,A6)
    BGE.B   .lbC000016
    TST.W   ($124,A6)
    BMI.B   .lbC000026
.lbC000016   MOVEM.L (a7)+,d0-d1/a0-a1/a5-a6
   RTE

; scedule()
.lbC000026   MOVEA.L ($114,A6),A1    
    MOVE.W  #$2700,SR
    BCLR    #7,($124,A6)
    BTST    #5,(14,A1)
    BNE.B   .lbC00005A
    LEA ($196,A6),A0
    CMPA.L  (8,A0),A0
    BEQ.B   .lbC000016
    MOVEA.L (A0),A0
    MOVE.B  (9,A0),D1
    CMP.B   (9,A1),D1
    BGT.B   .lbC00005A
    BTST    #6,($124,A6)
    BEQ.B   .lbC000016
.lbC00005A   LEA ($196,A6),A0
  move.l (A0),D0 ; enqueue()
  move.b LN_PRI(A1),D1
.1A
  movea.l D0,A0
  move.l (A0)+,D0
   beq.b .1
  cmp.b $5(A0),D1
   ble.b .1A
.1 move.l (A0),D0
  move.l A1,(A0)
  subq.l #4,A0
  move.l A0,(A1)
  movea.l D0,A0
  move.l D0,LN_PRED(A1)
  move.l A1,(A0) ; end enqueue()
    MOVE.B  #3,(15,A1)
    MOVE.W  #$2000,SR
    MOVEM.L (a7)+,d0-d1/a0-a1/a5
    MOVE.L  (SP),-(SP)
    MOVE.L  (_LVOSwitch+2,A6),(4,SP)   ;Switch
    MOVEA.L (SP)+,A6
   RTS

SetExcept:
  movea.l ThisTask(A6),A1
  lea $1E(A1),A0
  bra.b JL_0_1C78

SetSignal:
  movea.l ThisTask(A6),A1
  lea $1A(A1),A0
JL_0_1C78:
  and.l D1,D0
  not.l D1
  move.w #$4000,_custom+intena
  addq.b #1,IDNestCnt(A6)
  move.l (A0),-(A7)
  and.l (A0),D1
  or.l D0,D1
  move.l D1,(A0)
  move.l $1A(A1),D0
 bra.b Signal2

Signal:
    LEA ($1A,A1),A0
    MOVE.W  #$4000,_custom+intena
    ADDQ.B  #1,($126,A6)
    MOVE.L  (A0),-(SP)
    OR.L    D0,(A0)
Signal2:
    MOVE.L  ($1E,A1),D1
    AND.L   D0,D1
     BNE.B   lbC000060
    CMPI.B  #4,(15,A1)
     BNE.B   lbC00004C
    AND.L   ($16,A1),D0
     BEQ.B   lbC00004C
lbC00002A   MOVE.L  A1,D0
    MOVEA.L (A1)+,A0
    MOVEA.L (A1),A1
    MOVE.L  A0,(A1)
    MOVE.L  A1,(4,A0)
    MOVEA.L D0,A1
    LEA ($196,A6),A0
    subq.b  #1,$f(a1)
  move.l (A0),D0 ;enqueue()
  move.b $9(A1),D1
296$:
  movea.l D0,A0
  move.l (A0)+,D0
   beq.b 297$
  cmp.b $5(A0),D1
   ble.b 296$
297$:
  move.l (A0),D0
  move.l A1,(A0)
  subq.l #4,A0
  move.l A0,(A1)
  movea.l D0,A0
  move.l D0,$4(A1)
  move.l A1,(A0) ;end enqueue()
    CMPA.L  ($196,A6),A1
    BEQ.B   lbc80
lbC00004C   SUBQ.B  #1,($126,A6)
    BGE.B   lbC00005A
    MOVE.W  #$C000,_custom+intena
lbC00005A   MOVE.L  (SP)+,D0
    rts

lbC000060   ori.b    #$20,(14,A1)
    CMPI.B  #4,(15,A1)
     BEQ.B   lbC00002A
lbc80 subq.b #1,$126(a6)
     bge.b .1
    move.w #$c000,_custom+intena
.1  move.l (sp)+,d0
Reschedule:  bset #$7,SysFlags(A6)  ;$124
  Sne D0
  tst.b TDNestCnt(A6)               ;$127
   bge.b JL_0_20
  tst.b IDNestCnt(A6)               ;$126
   blt.b JL_0_3E
  tst.b D0
   bne.b JL_0_20
  move.w #$8004,_custom+intreq
JL_0_20: rtS

JL_0_3E:  move.l A5,-(A7)
  lea JL_0_4C(PC),A5
   jsr _LVOSupervisor(a6)
  movea.l (A7)+,A5
 rtS

pjl_0_0   movem.l   (sp)+,d0-d1/a0-a1/a5-a6
   rte

JL_0_56: rte

JL_0_4C:  btst #$5,(A7)
   bne.b JL_0_56
Schedule:
  movem.l d0-d1/a0-a1/a5-a6,-(a7)
  movea.l ThisTask(a6),A1
  move.w #$2700,sr
  andi.b #$7F,SysFlags(A6)
  lea TaskReady(a6),a0
  btst #$5,LN_SIZE(A1)
   bne.b JL_0_1B14
  cmpa.l LN_TYPE(A0),A0
   beq.b pjl_0_0
  movea.l (A0),A0
  move.b $9(A0),D1
  cmp.b LN_PRI(A1),D1
   bgt.b .13
  btst #$6,SysFlags(A6)
   beq.b pjl_0_0
.13
  lea TaskReady(a6),a0
JL_0_1B14: ; enqueue
  move.l (A0),D0
  move.b LN_PRI(A1),D1
JL_0_1B1A:
  movea.l D0,A0
  move.l (A0)+,D0
   beq.b JL_0_1B26
  cmp.b $5(A0),D1
   ble.b JL_0_1B1A
JL_0_1B26:
  move.l (A0),D0
  move.l A1,(A0)
  subq.l #4,A0
  move.l A0,(A1)
  movea.l D0,A0
  move.l D0,LN_PRED(A1)
  move.l A1,(A0)
; end enqueue
  move.b #$3,TC_STATE(A1)
  move.w #$2000,sr
  movem.l (sp)+,d0-d1/a0-a1/a5
  move.l (a7),-(a7)
  move.l _LVOSwitch+2(A6),4(A7)
  move.l (a7)+,a6
 rtS

ReplyMsg:
  moveq.l #$07,D0
  move.l $E(A1),a0
  move.l a0,d1
   bne.b yetreplymsg
  move.b #$6,$8(A1)
 rtS 

PutMsg:
  moveq.l #$05,D0
  move.l A0,D1
yetreplymsg
  lea $1C(A0),A0
  move.w #$4000,_custom+intena
  addq.b #1,IDNestCnt(A6)
  move.b D0,$8(A1)
  move.l (A0),D0
  move.l A1,(A0)
  subq.l #4,A0
  exg.l d0,a0
  movem.l d0/a0,(a1)
  move.l A1,(A0)  
  movea.l D1,A0
  move.l $10(A0),D1
   beq.b 185$
  moveq.l #3,d0
  movea.l d1,a1
  and.b $e(a0),D0
   beq.b 189$
  subq.b #1,D0
   bne.b 187$
  bsr.w Cause ;jsr _LVOCause(a6)
185$:
  subq.b #1,IDNestCnt(A6)
   bge.b 186$
  move.w #$C000,_custom+intena
186$: rtS 

187$:
  subq.b #1,D0
   beq.b 185$
  exg.l A1,A0
188$:
   jsr (A0)
 bra.b 185$

189$:
  addq.b #1,TDNestCnt(A6)
  subq.b #1,IDNestCnt(A6)
   bge.b 190$
  move.w #$C000,_custom+intena
190$: move.b $f(a0),d1
  moveq.l #1,d0
  lsl.l d1,d0
  move.l _LVOPermit+2(A6),-(A7)
 bra.w Signal ;jmp _LVOSignal(a6)

Enqueue:
  move.l (A0),D0
  move.b $9(A1),D1
296$:
  movea.l D0,A0
  move.l (A0)+,D0
   beq.b 297$
  cmp.b $5(A0),D1
   ble.b 296$
297$:
  move.l (A0),D0
  move.l A1,(A0)
  subq.l #4,A0
  move.l A0,(A1)
  movea.l D0,A0
  move.l D0,$4(A1)
  move.l A1,(A0)
 rtS

Dispatch:
  movea.l $230(A6),A4        ;context retsore
  move.w #$FFFF,$126(A6)
  move.w #$C000,_custom+intena
  lea $196(A6),A0
  lea $124(A6),A5
  move.w #$2700,sr
  movea.l (A0),A3
  move.l (A3),D0
   bne.b JL_0_199C
  move.w #$2700,D2
JL_0_1988:
  addq.l #1,$118(A6)
  ori.b #$80,(A5)
 stop #$2000
JL_0_1994:
  move.w D2,sr
  movea.l (A0),A3
  move.l (A3),D0
   beq.b JL_0_1988
JL_0_199C:
  subq.l #4,A5
  movea.l D0,A1
  move.l A3,$114(A6)
  move.l D0,(A0)
  move.l A0,$4(A1)
  move.w (A5)+,(A5)+
  andi.w #$BFFF,(A5)+
  move.b #$2,$F(A3)
  move.w $10(A3),(A5)
   bmi.b 254$
  move.w #$4000,_custom+intena
254$:
  move.w #$2000,sr
  addq.l #1,$11c(A6)
  moveq.l #-$60,D0
  and.b $E(A3),D0
   beq.b 255$
   bsr.b 257$
255$:
  movea.l $36(A3),A5
256$:
 jmp (A4)

257$:
  move.b d0,d2
   bpl.b 259$
  movea.l $46(A3),A5
   jsr (A5)
259$:
  lsr.b #6,D2
   bcs.b Exception
L269: rts

Exception:
  andi.b #$DF,$E(A3)
  move.l $2A(A3),D1
   beq.b L269
  lea _custom+intena,a0
  move.w #$4000,(a0)
  lea $126(A6),A5
  lea $1A(A3),A1
  addq.b #1,(A5)
  move.l (A1)+,D0
  and.l (A1),D0
  eor.l D0,(A1)
  eor.l D0,-(A1)
  subq.b #1,(A5)
   bge.b 263$
  move.w #$C000,(a0)
263$:
  movea.l $36(A3),A1
  move.l $E(A3),-(A1)
  tst.b (A5)
   bne.b JL_0_1A4E
  st (A5)
  move.w #$C000,(a0)
JL_0_1A4E:
  lea AJL_0_1A64(pc),a0
  move.l a0,-(a1)
  subq.l #8,a7
  move.l A1,usp
  move.l a7,a0
  clr.w (a0)+
  move.l D1,(A0)+
  movea.l $26(A3),A1
  move.w #$0020,(a0)
 rte

AJL_0_1A64:
  movea.l PExecBase(PC),A6
  lea 267$(PC),A5
 jmp _LVOSupervisor(A6)

267$:
  movea.l $230(A6),A4   ;context restore
  addq.l #8,A7
  movea.l $114(A6),A3
  move.l usp,A1
  or.l D0,$1E(A3)
  move.l (A1)+,D0
  move.l D0,$E(A3)
  move.l A1,$36(A3)
  move.w D0,$126(A6)
   bmi.b L268
  move.w #$4000,_custom+intena
L268: rtS

;void Cause(interrupt) a1
Cause:
  move.w #$4000,_custom+intena
  addq.b #1,$126(A6)
  moveq.l #$0B,D0
  cmp.b $8(A1),D0
   beq.b .JL_0_48
  move.b D0,$8(A1)
  move.b $9(A1),D0
  andi.w #$F0,D0
  ext.w D0
  lea $1D6(A6),A0      ; <- lea $1d2(a6),a0 + addq.l #4,a0
  adda.w D0,A0
  move.l 4(A0),d0
  move.l A1,4(A0)
  exg.l D0,A0
  movem.l D0/A0,(A1)
  move.l A1,(A0)
  move.w #$8004,_custom+intreq
  ori.b #$20,$124(a6)
.JL_0_48:  subq.b #1,$126(A6)
   bge.b .JL_0_56
  move.w #$C000,_custom+intena
.JL_0_56: rtS


