*******************************************************************************
*                                                                             *
*                       Settime V0.9 © 1991 by Acki                           *
*                                                                             *
* This useful utility takes the time from the internal hardware clock and     *
* sets it as the System-time. No Workbench-usage !!. The clock is supposed    *
* to be at $dc0000; no clock in this area and running settime will result     *
* in an invalid System-time. I wrote this programm because I wanted a really  *
* fast and small timeset-utility different from Commodores Setclock which     *
* always takes such a long loading time.                                      *
* Use it at your own risk !!! I didn't test it extensively, so there still    *
* might be bugs. If you figure out one, please let me know !!!. It worked     *
* quite smoothly from  29-aug to 30-sep.                                      *
*                                                                             *
* This directory contains the executable and the same thing as a *bootblock*! *
* Settime.bbl is the custom-bootblock as it is used by BBChampion and many    *
* other utilities; Settime.zv is the same thing in Zerovirus-Format (I used   *
* the Version on Fish 412).                                                   *
* Each Bootblock is complete (Checksum,Bootcode etc.), simply load it with    *
* your favourite Virushunter or Bootblocktool (Zerovirus, BootBlockChampion   *
* etc.) and save it on your startup-disk.                                     *
* Whenever a disk containing this bootblock is booted up, the screen will     *
* turn black and show green stripes for a moment to indicate that you         *
* have installed Settime.                                                     *
*                                                                             *
*                                                                             *
* If you reassemble the file like it is, you'll get the Cli-Version.          *
* To get a bootblock, simply remove the ';' at each line and act as if you    *
* wanted to get a normal, runable programm. Then take BootBlockChampion III,  *
* select 'Load Bootblock' ,choose 'Bootblock Code' and load your              *
* 'executable'. Select 'Ok' and the Bootblock will be installed on your Disk. *
*                                                                             *
* Are you interested in writing Custom-Bootblocks yourself ?                  *
* On Fish 242 there is a detailed description how it works and what you       *
* should take care of !                                                       *
*                                                                             *
* This Programm is FREEWARE. Use it at your own risk etc. !!!                 *
* If you want to release it with your Anti-virus-utility or BootblockTool     *
* as Custom-Bootblock, please let me know !!!                                 *
*                                                                             *
* All this stuff was written using the Devpac V2.14 Assembler, Kickstart 1.2  *
*                                                                             *
*                           Thanks in advance for your comments !             *
*                                                                             *
*                                     Andreas Ackermann                       *
*                                     Lorenz-Summa-Str. 10                    *
*                                     W-8679 Oberkotzau                       *
*                                     Germany                                 *
*                                     Phone: 09286/6399                       *
*                                                                             *
*                                     03-Oct-91 10:24:14                      *                  *
*                                                                             *
*******************************************************************************

            rsreset
ln          rs.b    0
ln_succ     rs.l    1
ln_pred     rs.l    1
ln_type     rs.b    1
ln_pri      rs.b    1
ln_name     rs.l    1
ln_size     rs.l    0

nt_msgport  equ     4
nt_message  equ     5
            rsreset
mp          rs.b    ln_size
mp_flags    rs.b    1
mp_sigbit   rs.b    1
mp_sigtask  rs.l    1
mp_msglist  rs.b    14
            rsreset
mn          rs.b    ln_size
mn_replyport    rs.l    1
mn_length       rs.w    1
mn_size         rs.w    0
            rsreset

iostdrequest    rs.l    0
io_message      rs.b    mn_size
io_device       rs.l    1
io_unit         rs.l    1
io_command      rs.w    1
io_flags        rs.b    1
io_error        rs.b    1
io_size         rs.l    0
io_actual       rs.l    1
io_length       rs.l    1
io_data         rs.l    1
io_offest       rs.l    1
iostd_size      rs.l    0

            rsreset

td          rs.l    0
td_io       rs.b    io_size
tv_secs     rs.l    1
tv_micro    rs.l    1

        include "exec/exec_lib.i"

;anfang  move.l  4.w,a6           ;standard routine at the beginning
;        lea.l   dosname(pc),a1   ;of each bootblock
;        jsr     -$60(A6)
;        move.l  d0,a0
;        move.l  $16(a0),a0
;        bsr     _main
;        moveq   #0,d0
;        rts

;dosname dc.b    'dos.library',0
;        even

_main
        movem.l d0-7/a0-6,-(sp)
;        moveq   #$ff,d0           ; that's all to get the gfx
;alert   add.w   #$111,$dff180     ; when we boot up !
;        dbf     d0,alert

        moveq   #1,d2
        moveq   #0,d1
        lea     devname(pc),a0
        moveq   #60,d0
        bsr     open_a_device     ;open timer.device
        move.l  d0,d7
        move.l  d0,a1

        move.w  #11,io_command(a1)
                                  ;command: setsystime
        move.l  #$dc0000,a0       ;format: number of seconds since
        moveq   #$28,d1           ;1-jan-78
        bsr     getdez            ;number of years

        move.w  d0,d3

4$
        sub.w   #78,d0
        move.w  d0,d1
        addq    #1,d1
        lsr.w   #2,d1

2$      add.l   #60*60*24,d2
        dbf     d1,2$
        sub.l   #60*60*24,d2

1$      add.l   #$1e13380,d2
        dbf     d0,1$
        sub.l   #$1e13380,d2

        moveq   #$20,d1
        bsr     getdez           ;actual month

        lea     monthtable(pc),a1
        move.l  #60*60*24*28,8(a1)

        btst.l  #0,d3
        bne.s   3$
        btst.l  #1,d3
        bne.s   3$               ;is actual year a leap-year ?
        add.l   #60*60*24,8(a1)  ;feb->29 days !

3$      moveq   #$0,d1
7$      add.l   (a1)+,d2
        addq    #1,d1
        cmp.b   d0,d1
        bne.s   7$

        moveq   #$18,d1
        bsr     getdez
5$      add.l   #60*60*24,d2
        dbf     d0,5$
        sub.l   #60*60*24*2,d2

        moveq   #$10,d1
        bsr     getdez       ;actual hour
        mulu    #60*60,d0
        add.l   d0,d2

        moveq   #$8,d1
        bsr     getdez       ;actual minute
        mulu    #60,d0
        add.l   d0,d2

        moveq   #0,d1
        bsr     getdez       ;actual second
        add.l   d0,d2
        bra.s   endskip
getdez
        move.l  4(a0,d1.w),d0   ;here we read the hardware clock. Every entry
        and.l   #%1111,d0       ;(year,month,etc.) consists of two 32-bit
        mulu.w  #10,d0          ;registers the first one containing the 2nd
        move.l  0(a0,d1.w),d1   ;digit and the 2nd one containing the 1st digit
        and.l   #%1111,d1       ;(decimal system !!!)
        add.b   d1,d0           ;result -> d0.l
        rts

monthtable
tg31    equ     60*60*24*31
tg30    equ     60*60*24*30
tg28    equ     60*60*24*28
        dc.l    0,tg31,tg28,tg31,tg30,tg31,tg30,tg31,tg31,tg30,tg31,tg30

endskip
        move.l  d7,a1
        move.l  d2,tv_secs(a1)
        jsr     _lvodoio(a6)   ;now really set the time
        moveq   #60,d0
        bsr     close_a_device ;clean_up
        movem.l (sp)+,d0-7/a0-6
        rts

devname
        dc.b    'timer.device',0
        even

;d0=length
;a0=devname
;d2=unit
;d1=flags

;result=d0 0->error ,else pointer to messageport

open_a_device
        movem.l d4-d7,-(sp)
        move.l  a0,d4
        move.l  d1,d5
        move.l  d0,d6
        move.l  4.w,a6
        move.l  #$10000,d1
        add.l   #34,d0
        jsr     _lvoallocmem(a6)
        move.l  d0,d7
        beq     od_nomsgmem
        move.l  d0,a0
        moveq   #0,d0
        sub.l   a1,a1
        bsr     createport
        tst.l   d0
        beq     od_noport
        add.l   #34,d7
        move.l  d7,a1
        move.l  a0,mn_replyport(a1)
        move.w  d6,mn_length(a1)
        move.b  #nt_message,ln_type(a1)
        move.l  d2,d0
        move.l  d4,a0
        move.l  d5,d1
        jsr     _lvoopendevice(a6)
        tst.l   d0
        beq.s   od_ok

od_error
        move.l  d7,a0
        sub.l   #34,d7
        bsr     deleteport
od_nomsgmem
        moveq   #0,d7
od_ok
        move.l  d7,d0
        movem.l (sp)+,d4-7
        rts

od_noport
        move.l  d7,a1
        sub.l   #34,a1
        move.l  d6,d0
        add.l   #34,d0
        jsr     _lvofreemem(a6)
        bra.s   od_nomsgmem

;a1=msgport
;d0=length

close_a_device
        move.l  4.w,a6
        movem.l d0/a1,-(sp)
        jsr     _lvoclosedevice(a6)
        movem.l (sp)+,d0/a1
        move.l  mn_replyport(a1),a0
        bsr     deleteport
        sub.l   #34,a1
        add.l   #34,d0
        jsr     _lvofreemem(a6)
        rts


;a0=^to messageport
;d0= pri
;a1=^to name
;this function won't create a public message-port (cause we don't addport
;our port !)
createport
        move.l  4.w,a6
        move.b  d0,ln_pri(a0)
        move.l  a1,ln_name(a0)
        move.b  #nt_msgport,ln_type(a0)
        move.l  a0,-(sp)
        moveq   #$ff,d0
        jsr     _lvoallocsignal(a6)
        move.l  (sp),a0
        move.b  d0,mp_sigbit(a0)
        addq    #1,d0
        beq.s   cp_error
        sub.l   a1,a1
        jsr     _lvofindtask(a6)
        move.l  (sp)+,a0
        move.l  d0,mp_sigtask(a0)
        moveq   #$ff,d0
        rts
cp_error
        move.l   (sp)+,a0
        moveq    #0,d0
        rts


;a0=port

deleteport
        movem.l d0/a1,-(sp)
        move.l  4.w,a6
        move.b  #$ff,ln_type(a0)
        moveq   #0,d0
        move.b  mp_sigbit(a0),d0
        jsr     _lvofreesignal(a6)
        movem.l (sp)+,d0/a1
        rts

        dc.b    'This programm sets the system-time from your internal clock.'
        dc.b    ' First published on the Fred-Fish collection >540 !!! '
        dc.b    ' Author: Andreas Ackermann, Lorenz-Summa-Str. 10, W-8679 '
        dc.b    ' Oberkotzau   Germany  Phone: 09286/6399 ',0,0

        END


*******************************************************************************
*                                                                             *
*      Enjoy !!!               Another masterpiece of ACKI                    *
*                                                                             *
*******************************************************************************




