; FILENAME      X48T08.ASM
; DESCRIPTION   MK48T08 RTC DEMO PROGRAM
;               USE BACK-KEY TO SETUP
;                   ANY-KEY  TO CHANGE TIME/DATE
; HARDWARE      JAZZ-31 & MK48T08 ON EXPANSION SOCKET (DMEM 0000-1FFFH)
; ASSEMBLER     SXA51
; ENGINEER      KRIANGSAK B.
; COMPANY       SILA RESEARCH CO.,LTD.

; FUNCTION      SET & DISPLAY TIME/DATE FROM MK48T08 ON JAZZ-31 DISPLAY
;               # BACK-KEY = SETUP TIME/DATE (AT CURRENT MODE)
;               # ANY-KEY  = CHANGE TIME/DATE MODE

         $INCLUDE "J31H2.ASM"

TIMBUF   EQU    1FF8H             ;RTC BUFFER

; ********** MAIN **********

         ORG    8100H

MAIN:    CALL   CLEAR             ;CLEAR DISPLAY
MAIN1:   CALL   RTIME             ;DISPLAY HH.MM.SS
         MOV    HEXBUF,R2
         MOV    HEXBUF+1,R3
         MOV    HEXBUF+2,R4
         CALL   DISOUT
         CJNE   A,#13H,$+6
         LJMP   MAIN4
         CJNE   A,#0FFH,MAIN2
         SJMP   MAIN1

MAIN2:   CALL   RDATE             ;DISPLAY DD.MM.YY
         MOV    HEXBUF,R2
         MOV    HEXBUF+1,R3
         MOV    HEXBUF+2,R4
         CALL   DISOUT
         CJNE   A,#13H,$+6
         LJMP   MAIN5
         CJNE   A,#0FFH,MAIN1
         SJMP   MAIN2

MAIN4:   CALL   RTIME             ;SET HH.MM (SS=0)
         MOV    DPH,R2
         MOV    DPL,R3
         MOV    R2,#78H           ;t
         CALL   INW
         MOV    R2,DPH
         MOV    R3,DPL
         MOV    R4,#0
         CALL   WTIME
         CALL   UBEEP
         SJMP   MAIN1

MAIN5:   CALL   RDATE             ;SET DD.MM
         MOV    DPH,R2
         MOV    DPL,R3
         MOV    R2,#5EH           ;d
         CALL   INW
         CALL   RDATE
         MOV    R2,DPH
         MOV    R3,DPL
         CALL   WDATE

         MOV    A,R4              ;SET YY
         MOV    R2,#6EH           ;Y
         CALL   INH
         PUSH   ACC
         CALL   RDATE
         POP    ACC
         MOV    R4,A
         CALL   WDATE
         CALL   UBEEP
         SJMP   MAIN2

; ********** DISOUT SUB **********
; SCAN DISPLAY & KEY

DISOUT:  CALL   HTOS
         MOV    A,DISBUF+3        ;SET DOT 1
         SETB   ACC.7
         MOV    DISBUF+3,A
         MOV    A,DISBUF+5        ;SET DOT 2
         SETB   ACC.7
         MOV    DISBUF+5,A
         CALL   SCANK
         RET

; ********** RTIME SUB **********
; READ MK48T08 TIME
; OUT = R2,R3,R4  (HH,MM,SS)
; REG = A,R2,R3,R4

RTIME:   PUSH   DPH
         PUSH   DPL
         MOV    DPTR,#TIMBUF      ;SET R BIT
         MOVX   A,@DPTR
         SETB   ACC.6
         MOVX   @DPTR,A
         INC    DPTR
         MOVX   A,@DPTR           ;SECOND
         MOV    R4,A
         INC    DPTR
         MOVX   A,@DPTR           ;MINUTE
         MOV    R3,A
         INC    DPTR
         MOVX   A,@DPTR           ;HOUR
         MOV    R2,A
         MOV    DPTR,#TIMBUF      ;CLR R BIT
         MOVX   A,@DPTR
         CLR    ACC.6
         MOVX   @DPTR,A
         POP    DPL
         POP    DPH
         RET

; ********** RDATE SUB **********
; READ MK48T08 DATE
; OUT = R2,R3,R4,R5 (DD,MM,YY,WK)
; REG = A,R2,R3,R4,R5

RDATE:   PUSH   DPH
         PUSH   DPL
         MOV    DPTR,#TIMBUF      ;SET R BIT
         MOVX   A,@DPTR
         SETB   ACC.6
         MOVX   @DPTR,A
         MOV    DPTR,#TIMBUF+4
         MOVX   A,@DPTR           ;WEEK-DAY
         MOV    R5,A
         INC    DPTR
         MOVX   A,@DPTR           ;DATE
         MOV    R2,A
         INC    DPTR
         MOVX   A,@DPTR           ;MONTH
         MOV    R3,A
         INC    DPTR
         MOVX   A,@DPTR           ;YEAR
         MOV    R4,A
         MOV    DPTR,#TIMBUF      ;CLR R BIT
         MOVX   A,@DPTR
         CLR    ACC.6
         MOVX   @DPTR,A
         POP    DPL
         POP    DPH
         RET

; ********** WTIME SUB **********
; WRITE MK48T08 TIME
; IN  = R2,R3,R4  (HH,MM,SS)
; REG = A,R2,R3,R4

WTIME:   PUSH   DPH
         PUSH   DPL
         MOV    DPTR,#TIMBUF      ;SET W BIT
         MOVX   A,@DPTR
         SETB   ACC.7
         MOVX   @DPTR,A
         INC    DPTR
         MOV    A,R4
         CLR    ACC.7
         MOVX   @DPTR,A           ;SECOND
         INC    DPTR
         MOV    A,R3
         CLR    ACC.7
         MOVX   @DPTR,A           ;MINUTE
         INC    DPTR
         MOV    A,R2
         ANL    A,#3FH
         MOVX   @DPTR,A           ;HOUR
         MOV    DPTR,#TIMBUF      ;CLR W BIT
         MOVX   A,@DPTR
         CLR    ACC.7
         MOVX   @DPTR,A
         POP    DPL
         POP    DPH
         RET

; ********** WDATE SUB **********
; WRITE MK48T08 DATE
; IN  = R2,R3,R4,R5 (DD,MM,YY,WK)
; REG = A,R2,R3,R4,R5

WDATE:   PUSH   DPH
         PUSH   DPL
         MOV    DPTR,#TIMBUF      ;SET W BIT
         MOVX   A,@DPTR
         SETB   ACC.7
         MOVX   @DPTR,A
         MOV    DPTR,#TIMBUF+4
         MOV    A,R5
         ANL    A,#07H
         MOVX   @DPTR,A           ;WEEK-DAY
         INC    DPTR
         MOV    A,R2
         ANL    A,#3FH
         MOVX   @DPTR,A           ;DATE
         INC    DPTR
         MOV    A,R3
         ANL    A,#3FH
         MOVX   @DPTR,A           ;MONTH
         INC    DPTR
         MOV    A,R4
         MOVX   @DPTR,A           ;YEAR
         MOV    DPTR,#TIMBUF      ;CLR W BIT
         MOVX   A,@DPTR
         CLR    ACC.7
         MOVX   @DPTR,A
         POP    DPL
         POP    DPH
         RET

         END

