; FILENAME      XBCODE.ASM
; DESCRIPTION   BAR CODE EXAMPLE (CODE39)
; HARDWARE      JAZZ-31
; ASSEMBLER     SXA51
; START-DATE    01/12/39
; SOFTWARE ENG  KRIANGSAK B.
; COMPANY       SILA RESEARCH CO.,LTD.
;               TEL 712-2850-2 FAX 381-1447

; ********** VARIABLE SET **********

         $INCLUDE "J31H2.ASM"

BARBIT   EQU    P1.0              ;DATA FROM BARCODE READER
BAROUT   EQU    10H               ;BAR CODE DIGIT BUFFER (LENGTH+1)
BARREV   EQU    20H               ;SAME AS BAROUT
BARBUF   EQU    9000H             ;RAW-DATA BUFFER (LENGTH+2)*10+1 BYTE

; ********** MAIN **********

         ORG    8100H

MAIN:    SETB   BARBIT
         CALL   BARIN
         CALL   BARCV
         JC     MAINE

         MOV    R0,#BAROUT        ;SEND DATA TO SERIAL
MAIN2:   MOV    A,@R0
         CJNE   A,#0DH,MAIN4
         MOV    A,#0AH
         CALL   SBYTE
         MOV    A,#0DH
         CALL   SBYTE
         CALL   UBEEP
         LJMP   MAIN
MAIN4:   CALL   SBYTE
         INC    R0
         SJMP   MAIN2

MAINE:   MOV    R2,#80H           ;READ ERROR
         MOV    R3,#0
         CALL   SOUND
         SJMP   MAIN

; ********** BARCV SUB **********
; CONVERT RAW-DATA TO ASCII
; IN  = BARBUF
; OUT = BAROUT,CY
; REG = A,B,R0,R2,R3,DPTR

BARCV:   MOV    DPTR,#BARBUF
         MOVX   A,@DPTR
         MOV    B,#2
         MUL    AB
         MOV    B,A               ;STD. COMPARE

         MOV    DPTR,#BARBUF+1    ;CHECK FORWARD OR REVERSE
         MOVX   A,@DPTR
         CJNE   A,B,$+3
         JC     BARRV

         MOV    DPTR,#BARBUF      ;**** FORWARD ****
         MOV    R0,#BAROUT
BARCV1:  MOV    R2,#9             ;DIGIT LOOP
         MOV    R3,#0
BARCV2:  MOVX   A,@DPTR
         CJNE   A,#0FFH,BARCV3
         SJMP   BARCVE
BARCV3:  CJNE   A,B,$+3
         CPL    C
         MOV    A,R3
         RLC    A
         MOV    R3,A
         INC    DPTR
         DJNZ   R2,BARCV2
         INC    DPTR              ;SKIP 0 BETWEEN DIGIT
         CALL   BARTB
         JC     BARCVE

         CJNE   A,#'*',BARCV4
         CJNE   R0,#BAROUT,BARCV35
         SJMP   BARCV1            ;IS * START
BARCV35: MOV    @R0,#0DH
         CLR    C
         RET                      ;EXIT OK

BARCV4:  MOV    @R0,A
         INC    R0
         SJMP   BARCV1

BARCVE:  SETB   C
         RET                      ;EXIT ERROR

BARRV:   MOV    DPTR,#BARBUF      ;**** REVERSE ****
         MOV    R0,#BARREV
BARRV1:  MOV    R2,#9             ;DIGIT LOOP
         MOV    R3,#0
BARRV2:  MOVX   A,@DPTR
         CJNE   A,#0FFH,BARRV3
         SJMP   BARRVE
BARRV3:  CJNE   A,B,$+3
         CPL    C
         MOV    A,R3
         RRC    A
         MOV    R3,A
         INC    DPTR
         DJNZ   R2,BARRV2
         INC    DPTR              ;SKIP 0 BETWEEN DIGIT
         MOV    A,R3
         RLC    A
         MOV    R3,A
         CALL   BARTB
         JC     BARRVE

         CJNE   A,#'*',BARRV4
         CJNE   R0,#BARREV,BARRV35
         SJMP   BARRV1            ;IS * START

BARRV35: MOV    R1,#BAROUT        ;LOAD BARREV TO BAROUT
         DEC    R0
BARRV37: CJNE   R0,#BARREV-1,BARRV38
         MOV    @R1,#0DH
         CLR    C
         RET                      ;EXIT OK
BARRV38: MOV    A,@R0
         MOV    @R1,A
         DEC    R0
         INC    R1
         SJMP   BARRV37

BARRV4:  MOV    @R0,A
         INC    R0
         SJMP   BARRV1

BARRVE:  SETB   C
         RET                      ;EXIT ERROR

; ********** BARTB SUB **********
; BARCODE LOOKUP TABLE
; IN  = R3,CY  (CODE39)
; OUT = A,CY   (ASCII)
; REG = A

BARTB:   PUSH   DPH
         PUSH   DPL
         MOV    DPTR,#BARTBX
         JC     BARTB1
         MOV    DPTR,#BARTBY

BARTB1:  CLR    A                 ;TABLE
         MOVC   A,@A+DPTR
         CJNE   A,#0FFH,BARTB2
         SJMP   BARTBE
BARTB2:  CLR    C                 ;COMPARE
         SUBB   A,R3
         JZ     BARTB5
         INC    DPTR
         INC    DPTR
         SJMP   BARTB1

BARTBE:  SETB   C                 ;EXIT ERROR
         POP    DPL
         POP    DPH
         RET

BARTB5:  INC    DPTR              ;COMPARE OK
         CLR    A
         MOVC   A,@A+DPTR
         CLR    C
         POP    DPL
         POP    DPH
         RET                      ;EXIT OK

BARTBX:  DB     00100001B,'1'     ;BIT 9 = 1 TABLE
         DB     01100000B,'3'
         DB     00110000B,'5'
         DB     00100100B,'8'
         DB     00001001B,'A'
         DB     01001000B,'C'
         DB     00011000B,'E'
         DB     00001100B,'H'
         DB     00000011B,'K'
         DB     01000010B,'M'
         DB     00010010B,'O'
         DB     00000110B,'R'
         DB     10000001B,'U'
         DB     11000000B,'W'
         DB     10010000B,'Y'
         DB     10000100B,'.'
         DB     0FFH

BARTBY:  DB     00110100B,'0'     ;BIT 9 = 0 TABLE
         DB     01100001B,'2'
         DB     00110001B,'4'
         DB     01110000B,'6'
         DB     00100101B,'7'
         DB     01100100B,'9'
         DB     01001001B,'B'
         DB     00011001B,'D'
         DB     01011000B,'F'
         DB     00001101B,'G'
         DB     01001100B,'I'
         DB     00011100B,'J'
         DB     01000011B,'L'
         DB     00010011B,'N'
         DB     01010010B,'P'
         DB     00000111B,'Q'
         DB     01000110B,'S'
         DB     00010110B,'T'
         DB     11000001B,'V'
         DB     10010001B,'X'
         DB     11010000B,'Z'
         DB     10000101B,'-'
         DB     11000100B,' '
         DB     10010100B,'*'
         DB     10101000B,'$'
         DB     10100010B,'/'
         DB     10001010B,'+'
         DB     00101010B,'%'
         DB     0FFH

; ********** BARIN SUB **********
; READ BAR CODE TO BARBUF
; OUT = BATBUF
; REG = A,R2,DPTR

BARIN:   MOV    DPTR,#BARBUF
         JB     BARBIT,$          ;WAIT FOR FIRST PULSE
         JNB    BARBIT,$

BARIN1:  CLR    A                 ;BACK BAR
BARIN2:  CALL   BARDL
         INC    A
         JZ     BARIN5
         JB     BARBIT,BARIN2
         MOVX   @DPTR,A
         INC    DPTR

         CLR    A                 ;WHITE BAR
BARIN3:  CALL   BARDL
         INC    A
         JZ     BARIN5
         JNB    BARBIT,BARIN3
         MOVX   @DPTR,A
         INC    DPTR
         SJMP   BARIN1

BARIN5:  MOV    A,#0FFH
         MOVX   @DPTR,A
         RET

BARDL:   MOV    R2,#8             ;DELAY SUB.
         DJNZ   R2,$
         RET

         END

