    include "exec/lists.i"
    include "exec/ports.i"
    include "exec/io.i"
    include "exec/memory.i"
    include "exec/libraries.i"
    include "dos/dosextens.i"

OpenLibrary equ -$228
AllocSignal equ -$14a
FindTask equ -$126
GetMsg equ -$174
PutMsg equ -$16e
Wait equ -$13e
DoIO equ -$1c8
AllocMem equ -$c6
FreeMem equ -$d2
CopyMem equ -$270
FindResident equ -$60
OpenDevice equ -$1bc
CloseDevice equ -$1c2
WaitPort equ -$180
SetTaskPri equ -$12c
Forbid equ -$84
Permit equ -$8a

hisa4 equ $24

hisport equ -$7fb0
histask equ -$7fda
inhibitcnt equ -$7faa
diskchngsig equ -$7fac
hiscdio equ -$7f48
rootnode equ -$7f4c
dospacket equ -$7fa6
;otherport equ -$7dcc
hisres1 equ -$7f9c
hisres2 equ -$7fa0


	section     text
start
    move.l  4,a6
    lea     cdfs_name(PC),a1
    jsr     FindResident(a6)
    tst.l   d0
    beq.s   1$
    move.l  d0,a2
    moveq   #24,d0
    lea     cdfs_name(PC),a1
    jsr     OpenLibrary(a6)
    tst.l   d0
    beq.s   1$
    move.l  d0,a0
    moveq   #24,d0
    cmp.w   LIB_VERSION(a0),d0
    bne.s   1$
    moveq   #9,d0
    cmp.w   LIB_REVISION(a0),d0
    beq.s   2$
1$
    moveq   #20,d0
    rts
2$
    addq.w  #1,LIB_REVISION(a0)
    move.l  hisa4(A0),A4
    move.l  hisport(a4),a3
    moveq   #0,d0
    moveq   #1,d2
    move.b  MP_SIGBIT(a3),d0
    asl.l   d0,d2
    move.l  histask(a4),d3
    sub.l   a1,a1
    jsr     FindTask(a6)
    move.l  d0,a5
    move.l  a5,histask(a4)
3$
    move.l  a5,a1
    moveq   #-1,d0
    jsr     SetTaskPri(a6) ; Force Switch if CD0 running
    jsr     Forbid(a6)
    lea     MP_MSGLIST(a3),a0
    TSTLIST a0
    beq.s   4$
    jsr     Permit(a6)
    bra.s   3$
4$
    move.l  a5,MP_SIGTASK(a3)
    jsr     Permit(a6)
;   move.l  d0,MP_SIGTASK+otherport(a4)
;   lea     hiscdio(a4),a1
;   jsr     CloseDevice(a6)
    moveq   #10,d0
    move.l  a5,a1
    jsr     SetTaskPri(a6)
    lea     -$7f6c(a4),a0
    move.l  -$7eec(a4),d0
    lea     hiscdio(a4),a1
    move.l  -$7ee8(a4),d1
    jsr     OpenDevice(a6)
    bra.s   gloop
******
;a2 = cdfs resident
;a3 = cdfs port
;a4 = cdfs geta4()
;a5 = DOS Packet
;a6 = ExecBase
;d2 = sigmask
main
    move.l  d2,d0
    jsr     Wait(a6)
gloop
    tst.b   diskchngsig(A4)
    beq.s   inhibchk
    tst.l   inhibitcnt(a4)
    bne.s   inhibchk
    bsr     dochange
inhibchk
    move.l  a3,a0
    jsr     GetMsg(a6)
    tst.l   d0
    beq.s   main
    move.l  d0,a1
    move.l  LN_NAME(a1),d0
;   beq.s   inhibchk
    move.l  d0,a5
    move.l  d0,dospacket(a4)
    moveq   #ACTION_INHIBIT,d0
    cmp.l   dp_Type(a5),d0
    beq.s   doinhib
    jsr     $674(a2)
;   jsr     RF35BB8(PC)
    tst.l   d0
    beq.s   inhibchk
sendmsg
    jsr     $726(a2)
;   jsr     RF35C6A(PC)
    bra.s   inhibchk
doinhib
    tst.l   dp_Arg1(a5)
    beq.s   1$
    move.l  inhibitcnt(A4),D0
    addq.l  #1,inhibitcnt(A4)
    tst.l   D0
    bne.s   2$
    jsr     $1520(a2)
;   jsr     RF36A64(PC)
    bra.s   2$
1$
    subq.l  #1,inhibitcnt(A4)
    bgt.s   2$
    clr.l   inhibitcnt(A4)
    bsr.s   dochange
2$
    moveq   #-1,d0
    move.l  d0,hisres1(a4)
    moveq   #0,d0
    move.l  d0,hisres2(a4)
    bra.s   sendmsg

dochange ; a6 = ExecBase, a2 = resptr
    clr.b   diskchngsig(A4)
    tst.b   -$7FA2(A4)
    beq.s   1$
    moveq   #0,D0
    rts
1$
    link    a5,#0  ; so it can be unlk'd later
    move.b  #1,-$7FA2(A4)
    moveq   #14,d0
    lea     hiscdio(a4),a1
    move.w  d0,$1c(a1)
    jsr     DoIO(a6)
    tst.l   hiscdio+IO_ACTUAL(A4)
    beq.s   2$
    moveq   #15,d0
    move.b  d0,-$7e84(a4)
    jsr     $1520(a2)
;   jsr     RF36A64(PC)
    bra.s   3$
2$
    moveq.l #16,d0
    move.b  d0,-$7e84(a4)
    clr.l   -(SP)
    jsr     chkdisk(PC)
    addq.w  #4,SP
3$
    jmp     $2dbe(a2)
;   bra     lF38302

chkiso ; a2 = resptr
    movem.l D3-D7/A3/A5,-(SP)
    moveq   #0,d3
    moveq.l #16,d7
    move.l  #MEMF_PUBLIC!MEMF_24BITDMA,d1
    move.l  #$8008,d0
    jsr     AllocMem(a6)
    move.l  D0,A3
    tst.l   d0
    bne.s   gotbuf
    moveq.l #2,d7
    move.l  #MEMF_PUBLIC!MEMF_24BITDMA,d1
    move.l  #$1008,d0
    jsr     AllocMem(a6)
    move.l  D0,A3
    tst.l   d0
    bne.s   gotbuf
ret0
    moveq   #0,D0
exit
    movem.l (SP)+,D3-D7/A3/A5
    rts
gotbuf
    moveq   #16,D4
loop1
    move.l  d7,-(SP)
    move.l  D4,-(SP)
    move.l  A3,-(SP)
    jsr     $2ca8(a2) ; read d7 secs into (a3) @ sec d4
;   jsr     RF381EC(PC)
    lea     $0C(SP),SP
    tst.l   D0
    bne     endloop
    moveq   #0,D6
    moveq   #0,D5
loop2
    move.l  D5,a5
    add.l   A3,a5
    move.l  a5,-(SP)
    jsr     $ed4(a2) ; check if CD001 v1
;   jsr     RF36418
    addq.w  #4,SP
    tst.l   D0
    beq     sloop2
    move.b  (a5),D0
    tst.b   D0
    beq.s   case0
    subq.b  #1,D0
    beq.s   case1
    sub.b   #$FE,D0
    beq     endloop
    bra     sloop2
case0
    tst.l   -$7E90(A4)
    bne     sloop2
    lea     $10ff(a2),a0
;   lea     .aCDTV___(PC),a0
    moveq   #31,d0
    lea     7(a5),a1
1$
    cmpm.b  (a1)+,(a0)+
    dbne    d0,1$
    bne.s   sloop2
    move.l  D4,D0
    add.l   D6,D0
    move.l  D0,-$7E90(A4)
    bra     sloop2
case1
    tst.l   d3
    bne.s   0$
    move.l  D4,D3
    add.l   D6,D3
    move.l  #$800,d0
    move.l  rootnode(A4),a1
    move.l  a5,a0
    jsr     CopyMem(A6)
0$
    lea     $10ff(a2),a0
;   lea     .aCDTV___(PC),a0
    moveq   #31,d0
    lea     8(a5),a1
1$
    cmpm.b  (a1)+,(a0)+
    dbne    d0,1$
    beq.s   comstd
    lea     $1141(a2),a0
;   lea     .aCOM_A___(PC),a0
    moveq   #31,d0
    lea     8(a5),a1
2$
    cmpm.b  (a1)+,(a0)+
    dbne    d0,2$
    bne.s   sloop2
comstd
    move.l  D4,D0
    add.l   D6,D0
    move.l  D0,-$7FCA(A4)
    move.l  #$800,d0
    move.l  rootnode(A4),a1
    move.l  a5,a0
    jsr     CopyMem(A6)
    move.l  A3,a1
    moveq   #11,d0
    asl.l   d0,d7
    move.l  d7,d0
    addq.l  #8,d0
    jsr     FreeMem(a6)
    move.l  rootnode(A4),D0
    bra     exit
sloop2
    addq.l  #1,D6
    add.l   #$000800,D5
    cmp.l   d7,D6
    blt     loop2
eloop2
    add.l   d7,D4
    cmp.w   #256,D4
    blt     loop1
endloop
    move.l  A3,a1
    moveq   #11,d0
    asl.l   d0,d7
    move.l  d7,d0
    addq.l  #8,d0
    jsr     FreeMem(a6)
    tst.l   d3
    beq     ret0
;   pea     1
;   move.l  d3,-(SP)
;   move.l  rootnode(A4),-(SP)
;   jsr     $2ca8(a2) ; read boot sec
;   bsr     RF381EC
;   lea     $0C(SP),SP
;   tst.l   D0
;   bne     ret0
    move.l  rootnode(A4),D0
    bra     exit

chkdisk ; a2 = resptr, a6 = ExecBase
    link    A5,#-$36 ; for the ROM
    movem.l A2/A3,-(SP)
    jsr     chkiso(PC)
    move.l  D0,A3
    tst.l   D0
    bne.s   1$
    moveq   #0,D0
    movem.l (SP)+,A2/A3
    unlk    A5
    rts
1$
    tst.w   $0A(A5)
    beq.s   2$
    move.l  A3,-(SP)
    jsr     $11ea(a2)
;   jsr     RF3672E(PC)
    addq.w  #4,SP
2$
    pea     32
    lea     $28(A3),A0 ; volume_id
    move.l  A0,-(SP)
    pea     -$36(A5)
    jsr     $2f32(a2)
;   jsr     RF38476
    lea     12(SP),SP
    lea     $32D(A3),A1 ; creation_date
    lea     -8(a5),a0
    bsr.s   GetDate
    jmp     $13a6(a2)
;   pea     -8(A5)
;   pea     -$14(A5)
;   jsr     RF385B6(PC)
;   addq.w  #8,SP
;   pea     -$14(A5)
;   pea     -$36(A5)
;   jsr     RF363AA(PC)
;   addq.w  #8,SP
;   move.l  D0,A2
;   move.l  A2,D0
;   bne     lF36A00
;   pea     $6E
;   jsr     Rmalloc(PC)
;   addq.w  #4,SP
;   move.l  D0,A2
;   move.l  A2,D0
;   bne     lF36924
;   moveq   #0,D0
;   bra     lF368AC

GetDate
    movem.l D2/A3,-(SP)
    move.l  a0,a3
    moveq   #4,D1
    jsr     $3176(a2)
;   bsr     RF386BA
    tst.w   D0
    beq.s   2$
    sub.w   #1900,D0
    move.b  D0,(A3)+
    moveq   #4,D2
1$
    moveq   #2,D1
    jsr     $3176(a2)
;   bsr     RF386BA
    move.b  D0,(A3)+
    dbra    D2,1$
    clr.b   (A3)+
2$
    movem.l (SP)+,D2/A3
    rts

cdfs_name
    dc.b    'cdfs.library',0

    END
*******************************************
