; FILENAME      XMAG.ASM
; DESCRIPTION   TEST PROGRAM FOR MAGNETIC READER
; HARDWARE      JAZZ-31 (V2) WITH 80C32 CPU & MAGNETIC CARD AT PORT1
; ASSEMBLER     SXA51
; ENGINEER      KRIANGSAK B.
; COMPANY       SILA RESEARCH CO.,LTD.

; FUNCTION      READ MAGNITIC & SEND DATA TO RS232 (ASCII)
;               # READ CARD FORWARD,REVERSE
;               # CHECK PARITY , CHECK DATA , CHANGE TO ASCII

         $INCLUDE "J31H2.ASM"

                             ;I/O PORT
CARDCP   EQU    P1.0         ;CARD CP
CARDTC   EQU    P1.1         ;CARD CLOCK
CARDTD   EQU    P1.2         ;CARD DATA

                             ;BIT ADDRESS
CARDFF   EQU    00H          ;CARDRD - FIRST FLAG , CARDRE - FULL FLAG
CARDER   EQU    01H          ;CARDCK - CHECK ERROR FLAG
CARDRF   EQU    02H          ;CARDRE - CARD REVERSE FLAG

                             ;RAM WORKING AREA
MAGBUF   EQU    80H          ;MAGNETIC BUFFER (80C32)
MAGWRK   EQU    MAGBUF+40    ;MAGMETIC WORKING (80C32)

; ********** MAIN **********

         ORG    8100H

MAIN:    CALL   CARDCL
         CALL   CARDRD
         CALL   CARDAJ
         JB     CARDER,MAIN

         CALL   UBEEP
         CALL   DUMPA
         SJMP   MAIN

; ********** DUMPA SUB **********
; DUMP DATA IN ASCII (MAGBUF)

DUMPA:   MOV    R0,#MAGBUF        ;SEND DATA TO SERIAL PORT (ASCII)
         MOV    R2,#40
DUMPA1:  MOV    A,@R0
         CALL   SBYTE
         INC    R0
         DJNZ   R2,DUMPA1
         CALL   SLF
         RET

; ********** DUMPH SUB **********
; DUMP DATA IN HEX (MAGBUF)

DUMPH:   MOV    R0,#MAGBUF        ;SEND DATA TO SERIAL PORT (ASCII)
         MOV    R7,#20
DUMPH1:  MOV    A,@R0
         CALL   SBYTEH
         MOV    A,#' '
         CALL   SBYTE
         INC    R0
         DJNZ   R7,DUMPH1
         CALL   SLF

         MOV    R7,#20
DUMPH2:  MOV    A,@R0
         CALL   SBYTEH
         MOV    A,#' '
         CALL   SBYTE
         INC    R0
         DJNZ   R7,DUMPH2
         CALL   SLF
         CALL   SLF
         RET

; ********** CARDCL SUB **********
; CLEAR MAGBUF

CARDCL:  MOV    R0,#MAGBUF
         MOV    R1,#40
CARDCL1: MOV    @R0,#0
         INC    R0
         DJNZ   R1,CARDCL1
         RET

; ********** CARDRD SUB **********
; MAGNETIC CARD READ (RAW DATA)
; OUT = MAGBUF
; REG = A,R0,R1,R2

CARDRD:  CALL   WDS
         JB     CARDCP,CARDRD     ;WAIT FOR CP=0
         MOV    R0,#MAGBUF
         SETB   CARDFF            ;SET FISRT FLAG

CARDRD2: MOV    R2,#5             ;5 BIT DATA
         CLR    A
CARDRD4: JB     CARDCP,CARDRD8    ;CHECK CP=1 (END CARD)
         JB     CARDTC,CARDRD4    ;WAIT FOR CLOCK=0
         MOV    R1,#2             ;DELAY
         DJNZ   R1,$
         MOV    C,CARDTD
         JNB    CARDFF,CARDRD5
         JC     CARDRD4           ;CHECK FIRST=0 (IF FIRST=1 NOTHING)
         CLR    CARDFF            ;CHANGE TO NORMAL
CARDRD5: RRC    A                 ;NORMAL
         JNB    CARDTC,$          ;WAIT FOR CLOCK=1
         DJNZ   R2,CARDRD4

         RRC    A                 ;EACH 5 BIT
         RRC    A
         RRC    A
         CPL    A
         ANL    A,#00011111B
         MOV    @R0,A
         INC    R0
         CJNE   R0,#MAGBUF+40,CARDRD2  ;CHECK BUFFER OVER 40 BYTE
CARDRD8: RET

; ********** CARDAJ SUB **********
; MAGNETIC CARD ADJUST DATA (CHECK REVERSE,CHECK PARITY,CHANGE TO ASCII)
; IN  = MAGBUF  RAW DATA
; OUT = MAGBUF  ASCII DATA (40 BYTE)
;       CARDER 0=ERROR 1=OK
; REG = A,R0,R1,R2,R3,R4

CARDAJ:  MOV    R0,#MAGBUF        ;BACKUP TO MAGWRK
         MOV    R1,#MAGWRK
         MOV    R2,#40
CARDAJ1: MOV    A,@R0
         MOV    @R1,A
         INC    R0
         INC    R1
         DJNZ   R2,CARDAJ1

         CLR    CARDRF            ;FORWARD
         CALL   CARDCK
         JB     CARDER,CARDAJ8
         CALL   CARDAX
         JB     CARDER,CARDAJ8
         RET

CARDAJ8: SETB   CARDRF            ;REVERSE
         CALL   CARDRE
         CALL   CARDCK
         CALL   CARDAX
         RET

CARDAX:  CLR    CARDER            ;CHECK DATA (B)
         MOV    R0,#MAGBUF
         MOV    A,@R0
         CJNE   A,#'B',CARDAX5

         MOV    R0,#MAGBUF+39     ;CHECK DATA (F)
CARDAX1: MOV    A,@R0
         CJNE   A,#0,CARDAX2
         DEC    R0
         CJNE   R0,#MAGBUF-1,CARDAX1
         SJMP   CARDAX5

CARDAX2: DEC    R0
         MOV    A,@R0
         CJNE   A,#'F',CARDAX5
         RET                      ;EXIT OK

CARDAX5: SETB   CARDER            ;EXIT DATA ERROR
         RET

; ********** CARDRE SUB **********
; MAGNETIC CARD REVERSE CHECK
; IN  = MAGWRK  RAW DATA (REVERSE)
; OUT = MAGBUF  RAW DATA (FORWARD)
; REG = A,R0,R1,R2,R3,R4

CARDRE:  CALL   CARDCL
         MOV    R0,#MAGWRK+39     ;REVERSE DATA
         MOV    R1,#MAGBUF        ;FORWARD DATA
         MOV    R2,#5             ;MAGWRK BIT COUNT
         MOV    R3,#5             ;MAGBUF BIT COUNT
         MOV    R4,#0             ;WORK DATA
         CLR    CARDFF

CARDRE1: MOV    A,@R0             ;SKIP ZERO BYTE
         CJNE   A,#0,CARDRE2
         DEC    R0
         CJNE   R0,#MAGWRK-1,CARDRE1
         RET                      ;ERROR (ALL MAGWRK ZERO)

CARDRE2: MOV    A,@R0
         RLC    A
         RLC    A
         RLC    A
CARDRE3: RLC    A                 ;FIND FIRST BIT = 1
         JC     CARDRE5
         DJNZ   R2,CARDRE3
         RET                      ;ERROR (NOTHING)

CARDRE5: PUSH   ACC               ;SHIFT BIT DATA **** LOOP ****
         MOV    A,R4
         RRC    A
         MOV    R4,A
         POP    ACC

         DJNZ   R2,CARDRE6        ;NEXT BYTE OF MAGWRK
         DEC    R0
         CJNE   R0,#MAGWRK-1,CARDRE51
         SETB   CARDFF
         CLR    A
         SJMP   CARDRE52
CARDRE51:MOV    A,@R0
         RLC    A                 ;DON'T CARE 3 BIT LEFT
         RLC    A
         RLC    A
CARDRE52:MOV    R2,#5

CARDRE6: DJNZ   R3,CARDRE8        ;NEXT BYTE OF MAGBUF
         PUSH   ACC               ;--\
         MOV    A,R4
         RRC    A
         RRC    A
         RRC    A
         ANL    A,#1FH
         MOV    @R1,A
         INC    R1
         POP    ACC               ;--/
         JNB    CARDFF,CARDRE61
         RET                      ;EXIT OK
CARDRE61:MOV    R3,#5

CARDRE8: RLC    A                 ;NEXT BIT
         SJMP   CARDRE5

; ********** CARDCK SUB **********
; MAGNETIC CARD CHECK PARITY & CONVERT TO ASCII
; IN  = MAGBUF  RAW DATA
; OUT = MAGBUF  ASCII DATA (40 BYTE)
;       CARDER 0=ERROR 1=OK
; REG = A,R0,R1

CARDCK:  CLR    CARDER            ;ERROR=0
         MOV    R0,#MAGBUF
         MOV    R1,#40
CARDCK2: MOV    A,@R0
         CJNE   A,#0,CARDCK3
         RET                      ;EXIT NO ERROR (UNDER 40 BYTE)

CARDCK3: JNB    PSW.0,CARDCK8
         ANL    A,#0FH            ;CONVERT TO ASCII
         CJNE   A,#0AH,$+3
         JNC    CARDCK4
         ORL    A,#30H
         SJMP   CARDCK6
CARDCK4: SUBB   A,#9
         ORL    A,#40H

CARDCK6: MOV    @R0,A             ;NEXT
         INC    R0
         DJNZ   R1,CARDCK2
         RET                      ;EXIT NO ERROR

CARDCK8: SETB   CARDER            ;ERROR=1
         RET

         END

