; *** KickChkSum 1.1 *** by Leonardo Fei *** Feb 11,1988 ***

ExecBase = 4
Forbid   = -132                 ;Forbid
SuperSt  = -150                 ;SuperState
AllocMem = -198                 ;AllocMem
FreeMem  = -210                 ;FreeMem
FindTask = -294                 ;FindTask
AddPort  = -354                 ;AddPort
RemPort  = -360                 ;RemovePort
OpenLib  = -408                 ;OpenLibrary
CloseLib = -414                 ;CloseLibrary
OpenDev  = -444                 ;OpenDevice
CloseDev = -450                 ;CloseDevice
DoIo     = -456                 ;DoIo

start:
            
    lea     $fc000c,a0          ;inizio dell'area chksum
    move.l  #$fffd,d1           ;contatore di LONGs    
    clr.l   d0                  ;pulisce chksum base
    bra.s   cont2               ;inizia checksum

cont1:
    add.l   (a0)+,d0            ;somma LONGs in D0
    bcc.s   cont2               ;Carry Clear? salta 
    addi.l  #1,d0               ;altrimenti addiziona #1

cont2:
    dbra    d1,cont1            ;continua fino alla fine
    add.l   $fc0000,d0          ;aggiungi la prima long  
    bcc.s   cont3               ;Carry Clear? salta
    addi.l  #1,d0               ;altrimenti addiziona #1

cont3:
    add.l   $fc0004,d0          ;aggiungi la seconda long
    bcc.s   cont4               ;Carry Clear? salta
    addi.l  #1,d0               ;altrimenti addiziona #1

cont4:
    subi.l  #0,d0           
    not.l   d0                  ;ecco la LONG match-word!    
    move.l  d0,d5               ;salvataggio temporaneo
         
modifica:
    move.l  ExecBase,a6
    move.l  #0,a1               ;questo task
    jsr     FindTask(a6)        ;cerca task
    move.l  d0,readreply+$10    ;SigTask: questo task
    
    lea     readreply,a1
    jsr     AddPort(a6)         ;monta replyport
    
    lea     diskio,a1           ;struct I/O
    move.l  #0,d0               ;drive DF0:
    clr.l   d1                  ;nessun flag
    lea     tddevice,a0         ;device name
    jsr     OpenDev(a6)         ;apri trackdisk.device
    tst.l   d0                  ;OK?
    bne     error               ;no: exit
    
    move.l  #512,d0             ;byteSize
    move.l  #$10002,d1          ;MEMF_CHIP,MEMF_CLEAR
    jsr     AllocMem(a6)        ;alloca il buffer       
    tst.l   d0                  ;OK?
    beq     error               ;no: exit
    move.l  d0,a5               ;salvataggio temporaneo
    
    lea     diskio,a1           ;struct I/O inizializzata
    move.l  #readreply,14(a1)   ;setta replyport
    move.l  a5,40(a1)           ;buffer
    move.l  #1*512,36(a1)       ;lunghezza dati (1 block)
    move.l  #1*512,44(a1)       ;offset: block 1
    move    #2,28(a1)           ;command: CMD_READ
    jsr     DoIo(a6)            ;esegui e aspetta
    
    move.l  d5,8(a5)            ;match-word nel buffer
    
    move    #3,28(a1)           ;command: CMD_WRITE
    jsr     DoIo(a6)            ;esegui ed aspetta
    
    move    #4,28(a1)           ;command: CMD_UPDATE
    jsr     DoIo(a6)            ;esegui ed aspetta
    
    move.l  #0,36(a1)           ;motore: OFF
    move    #9,28(a1)           ;command: TD_MOTOR
    jsr     DoIo(a6)            ;esegui ed aspetta
    
    move.l  #512,d0             ;byteSize
    move.l  a5,a1               ;buffer
    jsr     FreeMem(a6)         ;libera buffer memory
    
    lea     readreply,a1
    jsr     RemPort(a6)         ;rimuovi replyport
            
    lea     diskio,a1
    jsr     CloseDev(a6)        ;chiudi trackdisk.device
    
    jsr     Forbid(a6)          ;disabilita task-switching
    jsr     SuperSt(a6)         ;vai in SuperState !!!
    
    reset                       ;Boot-ROM e' inserita
    move.l  d5,$fc0008          ;match-word in kick WCS
    move.l  4,a0                ;a0 := startaddress in Boot-ROM
    jmp     (a0)                ;reset diretto
    
error:
    rts                         ;end

tddevice:   dc.b    'trackdisk.device',0
even

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