; FILENAME      XDK328.ASM
; DESCRIPTION   DK-328 DEMO PROGRAM
; HARDWARE      JAZZ-31 & DK-328 (CONNECT AT 8255 PORT)
; ASSEMBLER     SXA51
; START-DATE    18/02/37
; SOFTWARE ENG. KRIANGSAK B.
; COMPANY       SILA RESEARCH CO.,LTD.

; ********** VARIABLE **********

PORTA    EQU    0FC00H       ;SEGMENT
PORTB    EQU    0FC01H
PORTC    EQU    0FC02H       ;0-2 DIGIT, 4-7 KEYIN
PORTX    EQU    0FC03H

DISBUF   EQU    18H          ;8 BYTE DISPLAY BUFFER
HEXBUF   EQU    21H          ;4 BYTE HEX BUFFER
SCANPF   EQU    00H          ;BIT ADDRESS (AT INT-RAM ADDRESS 20H)

; ********** MAIN **********

         ORG    8100H

MAIN:    MOV    A,#88H            ;SET A=OUT,B=OUT,C0-3=OUT,C4-7=IN
         MOV    DPTR,#PORTX
         MOVX   @DPTR,A

MAIN1:   MOV    A,#1              ;SHIFT SEGMENT
         MOV    R1,#16
MAIN12:  PUSH   ACC               ;--\
         MOV    R0,#DISBUF
         MOV    R2,#8
MAIN14:  MOV    @R0,A
         INC    R0
         DJNZ   R2,MAIN14
         MOV    R2,#20H
         LCALL  SCAND
         POP    ACC               ;--/
         RL     A
         DJNZ   R1,MAIN12

MAIN2:   MOV    A,#88H            ;MAIN LOOP
MAIN4:   MOV    HEXBUF,A
         MOV    HEXBUF+1,A
         MOV    HEXBUF+2,A
         MOV    HEXBUF+3,A
         LCALL  HTOS
         LCALL  SCAN
         SJMP   MAIN4

; ********** CLEAR SUB **********
; CLEAR DISBUF
; REG = R0,R2

CLEAR:   MOV    R0,#DISBUF
         MOV    R2,#8
CLEAR1:  MOV    @R0,#0
         INC    R0
         DJNZ   R2,CLEAR1
         RET

; ********** SCAN SUB **********
; SCAN DISPLAY AND WAIT FOR KEY
; IN  = DISBUF
;     = SCANPF (KEY PRESS FLAG)
; OUT = A
; REG = A,R0,R2,R3,R4,R5,R6,R7,DPTR

SCAN:    MOV    R4,#8             ;LOOP
         MOV    R5,#8             ;RELEASE KEY COUNT
         MOV    R6,#0             ;KEY COUNT
         MOV    R7,#DISBUF

SCAN1:   LCALL  SCANS             ;OUT SEGMENT
         MOV    DPTR,#PORTC       ;CHECK KEY
         MOVX   A,@DPTR
         ANL    A,#0F0H
         CJNE   A,#0F0H,SCAN3
SCAN11:  DJNZ   R5,SCAN2          ;CHECK KEY RELEASE
         CLR    SCANPF
SCAN2:   DJNZ   R4,SCAN1
         SJMP   SCAN              ;LOOP FOREVER

SCAN3:   JB     SCANPF,SCAN2      ;KEY NOT RELEASE
         SETB   SCANPF
         ORL    A,R6
         MOV    B,A

         MOV    R2,#32            ;FIND TABLE
         MOV    R3,#0
         MOV    DPTR,#SCANTX
SCAN4:   MOV    A,R3
         MOVC   A,@A+DPTR
         CJNE   A,B,SCAN5
         MOV    A,R3
         RET                      ;EXIT NEW PRESS
SCAN5:   INC    R3
         DJNZ   R2,SCAN4
         MOV    A,#0FFH
         RET                      ;EXIT ERROR
            
SCANTX:  DB     0E1H,0E2H,0E3H,0E4H
         DB     0D1H,0D2H,0D3H,0D4H
         DB     0B1H,0B2H,0B3H,0B4H
         DB     071H,072H,073H,074H
         DB     0E5H,0E6H,0E7H,0E8H
         DB     0D5H,0D6H,0D7H,0D8H
         DB     0B5H,0B6H,0B7H,0B8H
         DB     075H,076H,077H,078H

; ********** SCANS SUB **********
; OUT ONE DIGIT & DELAY
; IN  = R6 DIGIT
;     = R7 INDEX TO DISBUF
; OUT = UPDATE R6 R7 + 1
; REG = A,R0,R2,R3,R6,R7,DPTR

SCANS:   MOV    A,R6              ;OUT DIGIT
         MOV    DPTR,#PORTC
         MOVX   @DPTR,A
         MOV    A,R7              ;OUT SEGMENT
         MOV    R0,A
         MOV    A,@R0
         MOV    DPTR,#PORTA
         MOVX   @DPTR,A
         MOV    R2,#4             ;DELAY
SCANS1:  MOV    R3,#0
         DJNZ   R3,$
         DJNZ   R2,SCANS1
         CLR    A                 ;CLEAR SEGMENT
         MOVX   @DPTR,A
         INC    R6                ;NEXT DIGIT & DISBUF
         INC    R7
         RET

; ********** SCAND SUB **********
; SCAN DISPLAY & DELAY
; IN  = DISBUF
;     = R2 DELAY VAR.
; REG = A,R0,R2,R3,R4,R5,R6,R7,DPTR

SCAND:   MOV    A,R2              ;DELAY
         MOV    R4,A
SCAND1:  MOV    R5,#8             ;LOOP
         MOV    R6,#0             ;DIGIT
         MOV    R7,#DISBUF
SCAND2:  LCALL  SCANS
         DJNZ   R5,SCAND2
         DJNZ   R4,SCAND1
         RET

; ********** HTOSX SUB **********
; ONE BYTE HEX TO SEGMENT
; IN  = R2 HEX 0-F
; OUT = A SEGMENT CODE
; REG = A,DPTR

HTOSX:   MOV    A,R2              ;MAKE SURE 0-F
         ANL    A,#0FH
         MOV    R2,A
         MOV    DPTR,#SEGTAB      ;TABLE
         MOV    A,DPL
         ADD    A,R2
         MOV    DPL,A
         MOV    A,DPH
         ADDC   A,#0
         MOV    DPH,A
         CLR    A
         MOVC   A,@A+DPTR
         RET

SEGTAB:  DB     3FH,06H,5BH,4FH        ;0123
         DB     66H,6DH,7DH,07H        ;4567
         DB     7FH,6FH,77H,7CH        ;89AB
         DB     39H,5EH,79H,71H        ;CDEF

; ********** HTOS SUB **********
; HEX TO SEGMENT CODE
; IN  = HEXBUF
; OUT = DISBUF
; REG = A,R0,R1,R2

HTOS:    MOV    R0,#HEXBUF
         MOV    R1,#DISBUF
         LCALL  HTOSS
         LCALL  HTOSS
         LCALL  HTOSS
         LCALL  HTOSS
         RET

HTOSS:   MOV    A,@R0             ;DIGIT L
         PUSH   ACC
         SWAP   A
         ANL    A,#0FH
         MOV    R2,A
         LCALL  HTOSX
         MOV    @R1,A
         INC    R1
         POP    ACC               ;DIGIT R
         ANL    A,#0FH
         MOV    R2,A
         LCALL  HTOSX
         MOV    @R1,A
         INC    R1
         INC    R0
         RET

; ********** STOHX SUB **********
; ONE BYTE SEGMENT TO HEX
; IN  = A SEGMENT CODE
; OUT = A HEX
; REG = A,R2,R3,DPTR

STOHX:   MOV    DPTR,#SEGTAB      ;TABLE
         ANL    A,#7FH            ;AND BIT 7 (DOT)
         MOV    R3,A              ;MEMO TO R3
         MOV    R2,#0             ;COUNTER
STOHX1:  CLR    A
         MOVC   A,@A+DPTR
         CLR    C
         SUBB   A,R3              ;COMPARE
         JZ     STOHX2
         INC    DPTR
         INC    R2
         CJNE   R2,#16,STOHX1
         MOV    R2,#0
STOHX2:  MOV    A,R2
         RET

; ********** STOH SUB **********
; SEGMENT CODE TO HEX
; IN  = DISBUF
; OUT = HEXBUF
; REG = A,R0,R1,R2,R3,DPTR

STOH:    MOV    R0,#DISBUF
         MOV    R1,#HEXBUF
         LCALL  STOHS
         LCALL  STOHS
         LCALL  STOHS
         LCALL  STOHS
         RET

STOHS:   MOV    A,@R0             ;DIGIT L
         LCALL  STOHX
         ANL    A,#0FH
         SWAP   A
         MOV    @R1,A
         INC    R0
         MOV    A,@R0             ;DIGIT R
         LCALL  STOHX
         ANL    A,#0FH
         ORL    A,@R1
         MOV    @R1,A
         INC    R0
         INC    R1
         RET

         END

