; FILENAME      XCLOCK.ASM
; DESCRIPTION   REAL-TIME-CLOCK FROM TIMER0
; HARDWARE      JAZZ-31 BOARD
; ASSEMBLER     SXA51
; START-DATE    14/08/93
; SOFTWARE ENG. KRIANGSAK B.
; COMPANY       SILA RESEARCH CO.,LTD.

; FUNCTION      REAL-TIME-CLOCK FROM TIMER-0
;               DISPLAY TIME TO SERIAL PORT (ASCII)

BRAT12   EQU    0E8H         ;1200 BAUD RATE VAR.
BRAT24   EQU    0F4H         ;2400
BRAT48   EQU    0FAH         ;4800
BRAT96   EQU    0FDH         ;9600
TIMVAR   EQU    4100         ;TIME BASE VARIABLE

; ********** RAM WORKING **********

         ORG    0000H
         DS     8
SYSSTK:  DS     24
TIMBUF:  DS     3            ;HH,MM,SS
TIMDIV:  DS     1            ;15Hz COUNTER
TIMMEM:  DS     1
ENDRAM:

; ********** POWER **********

         ORG    8100H
         LJMP   RES

         DB     0FFH,0FFH,0FFH,0FFH
         DB     0FFH,0FFH,0FFH,0FFH

; ********** TIMUP ********** (TIMER 0 INTERRUPT)

TIMUP:   PUSH   ACC
         PUSH   PSW

         MOV    TH0,#HIGH TIMVAR  ;RESET TIMER
         MOV    TL0,#LOW TIMVAR
         INC    TIMDIV
         MOV    A,TIMDIV
         CJNE   A,#15,TIMUP9
         MOV    TIMDIV,#0

         MOV    A,TIMBUF+2        ;SECOND
         ADD    A,#1
         DA     A
         MOV    TIMBUF+2,A
         CJNE   A,#60H,TIMUP9
         MOV    TIMBUF+2,#0

         MOV    A,TIMBUF+1        ;MINUTE
         ADD    A,#1
         DA     A
         MOV    TIMBUF+1,A
         CJNE   A,#60H,TIMUP9
         MOV    TIMBUF+1,#0

         MOV    A,TIMBUF          ;HOUR
         ADD    A,#1
         DA     A
         MOV    TIMBUF,A

TIMUP9:  POP    PSW
         POP    ACC
         RETI

; ********** RES **********

RES:     MOV    R2,#40H           ;POWER UP DELAY
         DJNZ   R2,$
         MOV    SP,#SYSSTK

         MOV    TMOD,#21H         ;TIMER1 MODE2 / TIMER 0 MODE1
         MOV    SCON,#52H         ;SERIAL 8 BIT UART MODE
         MOV    TH1,#BRAT96       ;9600 BPS
         SETB   TR1               ;TIMER1 ON

         MOV    TH0,#HIGH TIMVAR  ;15 Hz INTERRUPT
         MOV    TL0,#LOW TIMVAR
         MOV    TIMDIV,#0
         SETB   ET0
         SETB   EA

         MOV    TIMBUF,#09H       ;START TIME
         MOV    TIMBUF+1,#30H
         MOV    TIMBUF+2,#30H
         SETB   TR0

; ********** MAIN **********

MAIN1:   MOV    A,TIMBUF+2
         CJNE   A,TIMMEM,MAIN2
         SJMP   MAIN1

MAIN2:   MOV    TIMMEM,A
         MOV    A,TIMBUF
         LCALL  SBYTEH
         MOV    A,#':'
         LCALL  SBYTE
         MOV    A,TIMBUF+1
         LCALL  SBYTEH
         MOV    A,#':'
         LCALL  SBYTE
         MOV    A,TIMBUF+2
         LCALL  SBYTEH
         LCALL  SLF
         SJMP   MAIN1

; ********** HTOA SUB **********
; CONVERT HEX TO ASCII
; IN  = A
; OUT = R2,R3
; REG = A,R2,R3

HTOA:    PUSH   ACC
         SWAP   A
         LCALL  HTOAS
         MOV    R2,A
         POP    ACC
         LCALL  HTOAS
         MOV    R3,A
         RET

HTOAS:   ANL    A,#0FH
         CJNE   A,#0AH,$+3
         JNC    HTOAS1
         ORL    A,#30H
         RET
HTOAS1:  SUBB   A,#9
         ORL    A,#40H
         RET

; ********** ATOH SUB **********
; ASCII TO HEX CONVERT
; IN  = R2,R3   30H,41H
; OUT = A       0AH
; REG = A,R2

ATOH:    MOV    A,R2
         LCALL  ATOHS
         SWAP   A
         MOV    R2,A
         MOV    A,R3
         LCALL  ATOHS
         ORL    A,R2
         RET

ATOHS:   CJNE   A,#'A',$+3
         JC     ATOHS1
         ADD    A,#9
ATOHS1:  ANL    A,#0FH
         RET

; ********** SBYTE SUB **********
; SEND BYTE
; IN  = A
; REG = NO

SBYTE:   JNB    TI,$              ;WAIT FOR SEND OK
         CLR    TI
         MOV    SBUF,A
         RET

; ********** SBYTEH SUB **********
; SEND 2 BYTE HEX FROM A
; IN  = A
; REG = A,R2,R3

SBYTEH:  LCALL  HTOA
         MOV    A,R2
         LCALL  SBYTE
         MOV    A,R3
         LCALL  SBYTE
         RET

; ********** SBYTEW SUB **********
; SEND 4 BYTE FROM SPTR
; IN  = DPTR
; REG = A,R2,R3

SBYTEW:  MOV    A,DPH
         LCALL  SBYTEH
         MOV    A,DPL
         LCALL  SBYTEH
         RET

; ********** SBLOCK SUB **********
; SEND BLOCK
; IN  = DPTR ROM-ADDRESS (END BY 0 OR 0DH)
; OUT = DPTR (NEXT)
; REG = A,DPTR

SBLOCK:  CLR    A
         MOVC   A,@A+DPTR
         CJNE   A,#0,SBLOCK1
         INC    DPTR              ;NEXT ADDRESS
         RET                      ;EXIT BY 0
SBLOCK1: CJNE   A,#0DH,SBLOCK2
         LCALL  SLF               ;CR/LF
         INC    DPTR              ;NEXT ADDRESS
         RET                      ;EXIT BY ODH
SBLOCK2: LCALL  SBYTE
         INC    DPTR
         SJMP   SBLOCK

; ********** SBLANK SUB **********
; SEND BLANK
; IN  = R2 (NO OF BLANK)
; REG = A,R2

SBLANK:  MOV    A,#' '
         LCALL  SBYTE
         DJNZ   R2,SBLANK
         RET

; ********** SLF SUB **********
; PRINT CR/LF
; REG = A

SLF:     MOV    A,#0DH
         LCALL  SBYTE
         MOV    A,#0AH
         LCALL  SBYTE
         RET

; ********** Sxxx SUB **********

S1B:     MOV    A,#' '
         LCALL  SBYTE
         RET

; ********** RBYTE SUB **********
; OUT = A
; REG = A

RBYTE:   JNB    RI,$              ;WAIT FOR RECEIVE OK
         CLR    RI
         MOV    A,SBUF
         RET

; ********** RBYTEH SUB **********
; READ HEX-ASCII FROM SERIAL (2 BYTE)
; OUT = A
; REG = A,R2,R3

RBYTEH:  LCALL  RBYTE
         MOV    R2,A
         LCALL  RBYTE
         MOV    R3,A
         LCALL  ATOH
         RET

; ********** RBYTEW SUB **********
; READ BYTE WORD FROM SERIAL (4 BYTE)
; OUT = DPTR
; REG = A,R2,R3,DPTR

RBYTEW:  LCALL  RBYTEH
         MOV    DPH,A
         LCALL  RBYTEH
         MOV    DPL,A
         RET

; ********** DTSEC SUB **********
; DELAY 1/10 SECOND
; IN  = R2
; REG = R2,R3,R4

DTSEC:   MOV    R3,#179
DTSEC1:  MOV    R4,#0
         DJNZ   R4,$
         NOP
         NOP
         DJNZ   R3,DTSEC1
         DJNZ   R2,DTSEC
         RET

         END

