

;Versie 2.3 Device wordt netter afgesloten en Port wordt 
;       verwijderd als het device niet geopend kan worden

   NOPAGE
   NOLIST
   INCLUDE     "exec/types.i"
   INCLUDE     "exec/lists.i"
   INCLUDE     "exec/io.i"
   INCLUDE     "exec/nodes.i"
   INCLUDE     "exec/ports.i"
   INCLUDE     "exec/memory.i"
   INCLUDE     "libraries/dos.i"
   INCLUDE     "devices/bootblock.i"
   INCLUDE     "devices/trackdisk.i"
   LIST

   XREF        _LVOClose
   XREF        _LVOOpen
   XREF        _LVOOutput
   XREF        _LVORead
   XREF        _LVOWrite
   XREF        _LVOAddPort
   XREF        _LVOAllocMem
   XREF        _LVOAllocSignal
   XREF        _LVOCloseDevice
   XREF        _LVOCloseLibrary
   XREF        _LVODoIO
   XREF        _LVOFindTask
   XREF        _LVOFreeMem
   XREF        _LVOFreeSignal
   XREF        _LVOOpenDevice
   XREF        _LVOOpenLibrary
   XREF        _LVORemPort

execbase    equ   4

run:
   movem.l     d0/a0,-(sp)
   move.l      execbase,a6
   lea.l       DosName,a1
   clr.l       d0
   jsr         _LVOOpenLibrary(a6)
   move.l      d0,DosBase
   bne         goed
   rts
goed:
   move.l      d0,a6
   jsr         _LVOOutput(a6)
   move.l      d0,OutputHandle   

arg:
   movem.l     (sp)+,d0/a0
   lea         argtekst,a2
   lea         argpointers,a3
   move.l      #0,d2      ;arg counter
spaties:
   move.b      (a0)+,d1   ;spaties opruimen tussen
   subq.l      #1,d0      ;argumenten
   ble.s       parmexit
   cmp.b       #' ',d1
   ble.s       spaties   

;Copieer argumenten naar eigen buffer   
   addq.l      #1,d2      ;Weer een argument erbij
   move.l      a2,(a3)+
   bra.s       copyarg

argcopyloop:
   move.b      (a0)+,d1
   subq.l      #1,d0
   cmp.b       #' ',d1
   ble.s       eindcopy
copyarg:
   move.b      d1,(a2)+
   bra         argcopyloop
eindcopy:   
   clr.b       (a2)+      ;arg afsluiten met een 0
   bra.s       spaties
parmexit:
   cmpi.l      #2,d2      ;# arg = 1  ? 
   bne         usage      ;Niet het juiste aantal arg

   move.l      #argpointers,a0
   move.l      4(a0),a0 ;pointer naar het tweede arg
   cmpi.b      #'d',(a0)   
   beq         dgoed
   cmpi.b      #'D',(a0)
   bne         usage
dgoed:         cmpi.b   #'f',1(a0)
   beq         fgoed
   cmpi.b      #'F',1(a0)
   bne         usage
fgoed:         cmpi.b   #':',3(a0)
   bne         usage
   cmpi.b      #'0',2(a0)
   blt         usage
   cmpi.b   #'3',2(a0)
   bhi   usage
;Argument klopt dus doorgaan !!
   clr.l   d0
   move.b   2(a0),d0
   subi.b   #'0',d0
   move.l   d0,drivenummer   

;Geheugen voor file reserveren en file lezen

   move.l      execbase,a6
   move.l      #MEMF_CHIP+MEMF_CLEAR,d1      ;Chip of Fastmem
   move.l      #1048,d0
   jsr         _LVOAllocMem(a6)
   move.l      d0,filemem
   beq         eind1

   move.l      #argpointers,a0
   move.l      (a0),d1 ;pointer naar het eerste arg
   move.l      #MODE_OLDFILE,d2
   move.l      DosBase,a6
   jsr         _LVOOpen(a6)
   move.l      d0,filepointer
   bne.s       OpenSuccessFull

   move.l      #tekst4,d2   ;fout melding kan file niet
   move.w      #endtekst4-tekst4,d3   ;openen
   jsr         print
   bra         eind2
OpenSuccessFull:

   move.l      filepointer,d1
   move.l      filemem,d2
   move.l      #1048,d3
   move.l      DosBase,a6
   jsr         _LVORead(a6)
   move.l      filepointer,d1
   jsr         _LVOClose(a6)


   move.l      #tekst1,d2
   move.w      #endtekst1-tekst1,d3
   jsr         print

   move.l      execbase,a6      ;ExecBase laden
   move.l      #1024,d0   ;aantal bytes
   move.l      #MEMF_CHIP+MEMF_CLEAR,d1      ;ChipMem
   jsr         _LVOAllocMem(a6)
   move.l      d0,bootmem   ;Bootmem komt bootblock in te staan



opnieuw:
   move.l      #tekst2,d2
   move.w      #endtekst2-tekst2,d3
   jsr         print

WaitMouseButton:
   btst        #6,$bfe001
   beq.s       LeftMouseButton
   btst        #10,$dff016
   beq.s       RightMouseButton
   bra.s       WaitMouseButton

RightMouseButton:
   btst        #10,$dff016
   beq.s       RightMouseButton
   bra         eind3


LeftMouseButton:
   btst        #6,$bfe001
   beq.s       LeftMouseButton

   ;Checksum berekenen en filemem -> bootmem   

   move.l      filemem,a0
   move.l      bootmem,a1
   
   add.l       #36,a0   ;Bij seka porg's #36 andere #32
   add.l       #12,a1
   move.w      #1011,d0   ;1012 bytes copieeren
copyloop2:
   move.b      (a0)+,(a1)+
   dbra        d0,copyloop2

   move.l      bootmem,a0
   move.l      #BBNAME_DOS,BB_ID(a0)
   move.l      #0,BB_CHKSUM(a0)
   move.l      #$00000370,BB_DOSBLOCK(a0)
   clr.l       d0
   move.l      #255,d1
checksumloop:
   add.l       (a0)+,d0
   bcc.s       checksumcont
   add.l       #1,d0
checksumcont:
   dbra        d1,checksumloop
   move.l      #$ffffffff,d1
   sub.l       d0,d1
   move.l      bootmem,a0
   move.l      d1,BB_CHKSUM(a0)


   ;Bootblock wegschrijven
   sub.l       a1,a1
   jsr         _LVOFindTask(a6)
   move.l      d0,readreply+$10


   lea         readreply,a1
   jsr         _LVOAddPort(a6)

   lea         diskio,a1
   move.l      drivenummer,d0      ;drive nummer
   clr.l       d1
   lea         trddevice,a0
   jsr         _LVOOpenDevice(a6)
   tst.l       d0
   bne         eind4

   lea         diskio,a1
   move.l      #readreply,14(a1)
   move.w      #TD_PROTSTATUS,IO_COMMAND(a1)      ;TD_PROTSTATUS
   move.l      execbase,a6
   jsr         _LVODoIO(a6)   
   move.l      diskio+32,d0
   beq         schrijven
   
   move.l      #tekst5,d2
   move.w      #endtekst5-tekst5,d3
   jsr         print
   bra         verwijderdev      

schrijven:   
   lea         diskio,a1
   move.w      #CMD_WRITE,IO_COMMAND(a1)
   move.l      bootmem,IO_DATA(a1)
   move.l      #2*512,IO_LENGTH(a1)
   move.l      #0,IO_OFFSET(a1)
   move.l      execbase,a6
   jsr         _LVODoIO(a6)

   move.l      diskio+32,d6
   lea         diskio,a1
   move.w      #CMD_UPDATE,IO_COMMAND(a1)
   move.l      #0,IO_LENGTH(a1)
   jsr         _LVODoIO(a6)

   lea         diskio,a1
   move.w      #TD_MOTOR,IO_COMMAND(a1)
   move.l      #0,IO_LENGTH(a1)
   jsr         _LVODoIO(a6)

verwijderdev:   
   lea         diskio,a1
   jsr         _LVOCloseDevice(a6)

   lea         readreply,a1
   jsr         _LVORemPort(a6)
   
   bra         opnieuw
eind4:
   lea         readreply,a1
   jsr         _LVORemPort(a6)
               

eind3:

   move.l      execbase,a6
   move.l      #1024,d0
   move.l      bootmem,a1
   jsr         _LVOFreeMem(a6)

eind2:
   move.l      execbase,a6
   move.l      #1048,d0
   move.l      filemem,a1
   jsr         _LVOFreeMem(a6)
eind1:
   rts


print:         ;print tekst op scherm d3.w = lengte d2 = tekst
   movem.l     d1/a6,-(sp)
   move.l      DosBase,a6
   move.l      OutputHandle,d1
   jsr         _LVOWrite(a6)
   movem.l     (sp)+,d1/a6
   rts

usage:
   move.l      #tekst3,d2
   move.w      #endtekst3-tekst3,d3
   jsr         print
   rts         ;einde prog

DosName:
   dc.b        'dos.library',0

   CNOP        0,2

DosBase:
   dc.l        0
OutputHandle:
   dc.l        0
bootmem:
   dc.l        0
drivenummer:
   dc.l        0
filemem:   
   dc.l        0
filepointer:
   dc.l        0
argtekst:
   dcb.l       40,0
argpointers:
   dcb.l       10,0
trddevice:   
   dc.b        'trackdisk.device',0

   CNOP        0,2            'Even

diskio:
   dcb.l       20,0
readreply:
   dcb.l       8,0

tekst1:
   dc.b        'Boot V2.3 Spreading by ASS Holland',10
   dc.b        'This program write a SEKA program',10 
   dc.b        'in the bootblock of a disk.',10
   dc.b        'Coding by A.Nonymous',10,10
endtekst1:
tekst2:
   dc.b        'Press [L-MOUSE] to WRITE',10
   dc.b        'Press [R-MOUSE] to CANCEL',10,10
endtekst2:
tekst3:
   dc.b        'USAGE: boot <filename> dfx:',10
endtekst3:
tekst4:
   dc.b        'Error: File not found!',10
endtekst4:
tekst5:
   dc.b        'Error: Disk is writeprotect!',10,10
endtekst5:
   END
