;----------------------------------------------------------------------------

; SFR definitions
;
PSW   EQU 0D0H
P1    EQU 090H
P2    EQU 0A0H
P3    EQU 0B0H
DPL   EQU 082H
DPH   EQU 083H
IE    EQU 0A8H
IP    EQU 0B8H
ACC   EQU 0E0H
BCC   EQU 0F0H
SP    EQU 081H
;
PCON  EQU 087H
TCON  EQU 088H
TMOD  EQU 089H
TL0   EQU 08AH
TH0   EQU 08CH
TL1   EQU 08BH
TH1   EQU 08DH
;
WMCON EQU 096H
SCON  EQU 098H
SBUF  EQU 099H
;
T2CON  EQU 0C8H
RCAP2L EQU 0CAH
RCAP2H EQU 0CBH


                $include(flexscan.inc)

                DSEG
                ORG 050H

buffer:         DS      22

        CSEG
        ORG     08000H          ;The program memory starts at address $8000

;Application program header structure
        DB      0A5H            ;"Magic" value, must be $A5
        LJMP    MAIN            ;this is the starting address of your sample code
        DB      0,05FH          ;program length, only used for CRC calculation and loading
        DB      00DH,00FH       ;CRC, $0D0F
        DB      1               ;flag byte 1, set to 0 to disable CRC calculation
        DB      0               ;decompression mode: reserved
        DB      002H,052H       ;18-02-01
        DB      0,0             ;no more files present
        DB      0,0             ;2 reserved bytes
        DB      'J1850 Scanner',0  ;up to 15 character program name plus nullbyte

        ORG     08020H

MAIN:
        LCALL   CLS             ;clear screen

        MOV     DPTR,#buffer    ;pass buffer and buffer length
        MOV     A,#22
        LCALL   DLC_INIT        ;init the DLC

        MOV     DPTR,#0F060H    ;Our ID ($F0) / Peer ID(don't care)
        MOV     A,#000H         ;no filtering
        LCALL   DLC_FILTER

MSG_LOOP:
        LCALL   DLC_RX_READY
        JNB     ACC.0,MSG_LOOP      ;wait until message is ready in buffer
        PUSH    ACC                 ;save result

        LCALL   CRLF                ;scroll up

        MOV     A,#buffer           ;buffer start address
        MOV     R0,A
        MOV     A,@R0               ;get length byte from buffer
        MOV     R7,A                ;store length byte
        LCALL   LCDHEX              ;print length byte in hex

        MOV     A,#'-'
        LCALL   CHROUT              ;print separator character

        MOV     A,#buffer
        MOV     R0,A
        INC     R0                  ;skip length byte
        CJNE    R7,#0,MSGOUT        ;print until end of message
        SJMP    MSGEND

MSGOUT: MOV     A,R0
        PUSH    ACC                 ;save pointer

        MOV     A,@R0               ;get byte from buffer
        LCALL   LCDHEX              ;and print it in hex

        POP     ACC
        MOV     R0,A                ;get pointer back
        INC     R0                  ;advance pointer
        DJNZ    R7,MSGOUT           ;continue for rest of message

MSGEND: POP     ACC                 ;get back status of buffer
        JB      ACC.1,MSG_OVF       ;check for overflow

        MOV     A,#'.'
        SJMP    NO_OVL
MSG_OVF:
        MOV     A,#'!'
NO_OVL: LCALL   CHROUT

        LCALL   DLC_UNLOAD_MSG      ;remove message from buffer and prepare for next message

        LJMP    MSG_LOOP

CRLF:   MOV     DPTR,#T_CRLF
        LCALL   STROUT
        RET

T_CRLF:  DB     13,10,0

        END


