; a program to set and read the real time clock
;
;
cpm     equ     0h
sys     equ     05h
conout  equ     2
prtmsg  equ     9
cr      equ     0dh
lf      equ     0ah
dollar  equ     24h
sec     equ     0ff64h
min     equ     0ff63h
hour    equ     0ff62h
day     equ     0ff5fh
month   equ     0ff60h
year    equ     0ff61h
;
;
        org     100h
start:  lxi     h,day           ; check to see if the clock is installed
        mov     a,m             ; put day in 'a'
        ora     a               ; test day
        jnz     m1              ; clock is installed
        mvi     c,prtmsg
        lxi     d,warn
        call    sys
        mvi     c,prtmsg
        lxi     d,crlf
        call    sys
        jmp     cpm
;
; check if there was an argument on the command line
;
m1:     lxi     h,128
        mov     a,m
        ora     a
        jnz     m2              ; read clock not requested
        call    read            ; read and format clock
        lxi     d,line          ; print time
        mvi     c,prtmsg
        call    sys
        jmp     cpm
;
m2:     lxi     h,130           ; check if format requested
        mov     a,m
        cpi     '?'
        jnz     setclk          ; no format so set clock
        mvi     c,prtmsg
        lxi     d,format
        call    sys
        jmp     cpm
;
; use argument on command line to set clock
;
setclk: lxi     h,130
loop1:  mov     a,m             ; skip blanks
        cpi     ' '
        jnz     s1              ; no blank
        inx     h               ; blank, so skip it
        jmp     loop1
;
; get characters of month in b and c and convert to integer
;
s1:     mov     b,a
        inx     h
        mov     c,m
        inx     h
        inx     h
        call    ctoi            ; convert to integer
        sta     month           ; and put in month
;
; repeat for day
;
        mov     b,m
        inx     h
        mov     c,m
        inx     h
        inx     h
        call    ctoi
        sta     day
;
; repeat for year
;
        mov     b,m
        inx     h
        mov     c,m
        inx     h
        inx     h
        call    ctoi
        sta     year
;
; skip blanks and repeat for hour, minute, and second
;
loop2:  mov     a,m
        cpi     ' '
        jnz     s2
        inx     h
        jmp     loop2
;
; get hour
;
s2:     mov     b,a
        inx     h
        mov     c,m
        inx     h
        inx     h
        call    ctoi
        sta     hour
;
; get minute
;
        mov     b,m
        inx     h
        mov     c,m
        inx     h
        inx     h
        call    ctoi
        sta     min
;
; get second
;
        mov     b,m
        inx     h
        mov     c,m
        inx     h
        inx     h
        call    ctoi
        sta     sec
        jmp     cpm
;
; a subroutine to convert the characters in b and c into an
;   integer in a
;
ctoi:   mov     a,b
        sui     '0'
        add     a
        add     a
        add     b
        sui     '0'
        add     a
        add     c
        sui     '0'
        ret
;
format: db      'clock input format -  mm/dd/yy hh:mm:ss'
crlf:   db      cr,lf,dollar
warn:   db      'real time clock is not installed$'
;
; read and format date and time
;
;    code from here to end of the program
;    can be used as a subroutine in your
;    programs to read and format the time
;
read:   lxi     h,line          ; get address of "line" in hl
        lda     day             ; get day (in integer form)
        call    itoc            ; convert it to ascii characters
        mov     m,e             ; output the first digit
        inx     h
        mov     m,d             ; output second digit
        inx     h
        mvi     m,'-'
        inx     h
        push    h
        lda     month           ; get month
        dcr     a               ; get (month-1)
        add     a               ; get 2*(month-1)
        add     a               ; get 4*(month-1)
        lxi     h,months        ; get address of months in hl
        mvi     b,0             ; put offset in a into b
        mov     c,a             ;   and c
        dad     b               ; add bc to hl
        xchg                    ; put hl into de
        pop     h
        ldax    d               ; get first letter of month
        mov     m,a
        inx     h
        inx     d
        ldax    d               ; get second letter of month
        mov     m,a
        inx     h
        inx     d
        ldax    d               ; get third letter of month
        mov     m,a
        inx     h
        mvi     m,'-'
        inx     h
;
; output year
;
        lda     year
        call    itoc
        mov     m,e
        inx     h
        mov     m,d
        inx     h
        mvi     m,' '
        inx     h
;
; output hour,
;
        lda     hour
        call    itoc
        mov     m,e
        inx     h
        mov     m,d
        inx     h
        mvi     m,':'
        inx     h
;
; and minute,
;
        lda     min
        call    itoc
        mov     m,e
        inx     h
        mov     m,d
        inx     h
        mvi     m,':'
        inx     h
;
; and second
;
        lda     sec
        call    itoc
        mov     m,e
        inx     h
        mov     m,d
        ret
;
; a subroutine to convert the integer in a into two characters
;   in d and e
;
itoc:   mvi     e,0
loop3:  inr     e
        sui     10
        jp      loop3
        adi     10
        dcr     e
        mov     d,a
        mvi     a,'0'
        add     e
        cpi     '0'
        jnz     it1
        mvi     a,' '
it1:    mov     e,a
        mvi     a,'0'
        add     d
        mov     d,a
        ret
;
months: db      'Jan '
        db      'Feb '
        db      'Mar '
        db      'Apr '
        db      'May '
        db      'Jun '
        db      'Jul '
        db      'Aug '
        db      'Sep '
        db      'Oct '
        db      'Nov '
        db      'Dec '
line:   ds      18
        db      cr,lf,dollar
;
        end     start



