;****************************************************************************
;
; Sample demonstration program for use with FlexScan
;
; (c)2000 Oliver Scholz
; all rights reserved
;
;
;****************************************************************************
;
;
;  Memory Model (Harvard Architecture):
;
;  Program Memory:
;    ROM Scan OS  (internal)        $0000 - $1FFF (8K)
;    reserved                       $2000 - $7FFF (24K)
;    ROM Program  (external EPROM)  $8000 - $FFFF (32K), mapped from EPROM 0-$7FFF
;
;  Data Memory:
;    EEPROM Data  (internal, opt.)  $0000 - $07FF (2K)
;    reserved                       $0800 - $7FFF (30K)
;    ROM Data     (external EPROM)  $8000 - $BFFF (16K), mapped from EPROM 0-$3FFF
;    Peripherals (LCD)              $E000 - $FFFF (16K)
;---------------------------------------------------------------------
;
;  von Neumann style mapping of Harvard memories
;  Memory model for accesses through the OS pointer access routine:
;
;  linear model:                  $0000 - $00FF interne 256 Byte RAM
;                                 $0100 - $1FFF interne 8K Programm ROM
;                                 $2000 - $27FF interne 2k EEPROM
;                                 $2800 - $3FFF reserved
;                                 $4000 - $7FFF externe 16K EPROM (0-$3FFF)
;                                 $8000 - $FFFF externe 32K EPROM (0-$7FFF)
;
; 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)
;
; RAM Definitions
;
        DSEG
        ORG     048H            ;User RAM starts at location $48

;------------------------------------------------------------------
;Application Variables
aldl_mode:      DS      1           ;remember current ALDL mode
default:        DS      1
header:         DS      3           ;reserve space for header
buffer:         DS      64          ;reserve space for data buffer

;
        CSEG
        ORG     08000H

;Program header
        DB      0A5H        ;Magic
        LJMP    LOAD        ;Jump to load address
        DB      013H,0A6H   ;Program length (HH:LL) starting at load addr
        DB      0B3H,000H   ;CRC, if used   (HH:LL)
        DB      001H        ;Flag byte: Bit 0 set: check CRC
        DB      0           ;decompression mode
        DB      002H,031H   ;Date 17.1.2001
        DB      000H,000H   ;Address of next directory entry (0=none)
        DB      0,0         ;reserved
        DB      'Fiero Scantool',0,0

        ORG     08020H

LOAD:   SJMP    MAIN

MODE_MENU:
        DB 2,0
        DB '10K Mode',0
        DB 'Open Mode',0

VEH_MENU:
        DB 3,0
        DB '85-88 V6 Fiero',0
        DB '84-86 L4 Fiero',0
        DB '87-88 L4 Fiero',0

FONT:
        DB 055H,0AAH,055H,0AAH,055H,0AAH,055H,0AAH ;checkered block
        DB 0,0,0,24,4,8,16,28           ;indexed 2
        DB 6,9,9,6,0,0,0,0              ;degrees character

; defines for special characters

BLOCK   EQU 0
LOW2    EQU 1
DEGREE  EQU 2

;--------------------------------
; Select ALDL mode
; Note: Not all vehicles will transmit data in all modes,
; and the kind of data may vary!
;
MAIN:   LCALL   CLS

        MOV     DPTR,#FONT
        MOV     A,#3
        LCALL   LOAD_CHARSET

        MOV     DPTR,#MODE_MENU
        MOV     A,#0            ;10K mode is default
        LCALL   SEL_MENU        ;Show mode menu
        MOV     aldl_mode,A     ;remember mode

        CJNE    A,#0,NO_10K

        LCALL   MODE_10K
        SJMP    MODE_SELECTED

NO_10K:
        CJNE    A,#1,MAIN       ;try again

        LCALL   MODE_OPEN
;--------------------------------
MODE_SELECTED:
        LCALL   CLS

        MOV     DPTR,#VEH_MENU
        MOV     A,#0             ;set default
        LCALL   SEL_MENU         ;Show vehicle menu

        CJNE    A,#0FFH,VEH_SELECTED
        SJMP    MODE_SELECTED    ;retry if 0FFH

VEH_SELECTED:
        MOV     DPTR,#JMPTAB     ;use Jumptable to branch to vehicle
        LJMP    SWITCH

JMPTAB: LJMP    C3_25            ;25 Byte per Frame in C3 mode
        LJMP    C3_20            ;20 Byte per Frame in C3 mode
        LJMP    UART_67          ;67 Byte per Frame in UART mode

;#######################################################################
; 25 Byte per Frame C3 ECM
;#######################################################################
C325_ABORT:
        LJMP    C320_ABORT

C3_25:  MOV     DPTR,#buffer
        MOV     A,#25                ;25 bytes per frame
        LCALL   C3_INIT
        CJNE    A,#0,C325_ABORT      ;init failed: abort!

        LCALL   CLS                  ;Print string: waiting
        MOV     DPTR,#T_WAITING
        LCALL   STROUT

        MOV     A,#5
        LCALL   DELAY
C3_25LP:
        LCALL   GETCH                ;abort if any key pressed
        CJNE    A,#0FFH,C325_ABORT

        LCALL   C3_TESTLOCK          ;check if communication has been established
        CJNE    A,#0,C3_25_START
        SJMP    C3_25LP

C3_25_START:
        MOV     DPTR,#T_READING
        LCALL   STROUT

        MOV     A,#5              ;wait 0.5s
        LCALL   DELAY
;-----------------------------------------------------------------
C3_25_MAIN:
        MOV     DPTR,#MENU_C3     ;Show main menu
        MOV     A,#0
        LCALL   SEL_MENU
        CJNE    A,#0FFH,C3_25_M

        LCALL   C3_EXIT
        LJMP    MODE_SELECTED

C3_25_M:
        PUSH    ACC
        LCALL   CLS
        POP     ACC

        MOV     default,#0
        MOV     DPTR,#C325MTAB    ;use Jumptable to branch to vehicle
        LCALL   SWITCH

        SJMP    C3_25_MAIN


C325MTAB:
        LJMP    C3_25_CODES
        LJMP    C3_25_SENSORS
        LJMP    C3_25_FLAGS
;---------------------------------------------------------
C3_25_CODES:
        MOV     A,#buffer+11
        LCALL   DISPL_CODES
        LJMP    C3_25_MAIN
;---------------------------------------------------------
C3_25_SENSORS:
        MOV     DPTR,#SENS_C325   ;Show main menu
        MOV     A,default
        LCALL   SEL_MENU
        CJNE    A,#0FFH,C3_25_DO

        LJMP    C3_25_MAIN

C3_25_DO:
        MOV     default,A

        PUSH    ACC
        LCALL   CLS
        POP     ACC

        MOV     DPTR,#C325TAB     ;use Jumptable to branch to reading
        LCALL   SWITCH

        SJMP    C3_25_SENSORS
;---------------------------------------------------------
C3_25_FLAGS:
        MOV     DPTR,#FLAGS_C325   ;Show main menu
        MOV     A,default
        LCALL   SEL_MENU
        CJNE    A,#0FFH,C3_25_ADO

        LJMP    C3_25_MAIN

C3_25_ADO:
        MOV     default,A

        PUSH    ACC
        LCALL   CLS
        POP     ACC

        MOV     DPTR,#C325TAB2    ;use Jumptable to branch to vehicle
        LCALL   SWITCH

        SJMP    C3_25_FLAGS
;--------------------------------------------------
SENS_C325:
        DB      16,0
        DB      'Prom-Id',0
        DB      'Coolant Temp',0
        DB      'MAT Sensor',0
        DB      'MAP Sensor',0
        DB      'TPS Sensor',0
        DB      'O2 Sensor',0
        DB      'Battery Voltage',0
        DB      'Engine Speed',0
        DB      'Speed Sensor',0
        DB      'IAC Valve',0
        DB      'EGR Duty Cycle',0
        DB      'Integrator',0
        DB      'Block Learn',0
        DB      'O',LOW2,'-Crosscounts',0
        DB      'Injector BPW',0
        DB      'Spark Advance',0


C325TAB:
        LJMP    PROM_ID
        LJMP    COOLANT2
        LJMP    MAT
        LJMP    MAP
        LJMP    TPS
        LJMP    O2
        LJMP    BAT
        LJMP    RPM
        LJMP    VSS
        LJMP    IAC
        LJMP    EGR
        LJMP    INT
        LJMP    BLM
        LJMP    O2X
        LJMP    BPW
        LJMP    ADV

FLAGS_C325:
        DB      10,0
        DB      'Battery Status',0
        DB      'Loop Status',0
        DB      'P/N Switch',0
        DB      'A/C Request',0
        DB      'A/C Clutch',0
        DB      'TCC Solenoid',0
        DB      'Shift Light',0
        DB      'Mixture',0
        DB      'EGR Switch',0
        DB      'Turbo Wastegate',0


C325TAB2:
        LJMP    BATSTAT
        LJMP    LOOP
        LJMP    PN
        LJMP    ACREQ
        LJMP    ACCLU_V6
        LJMP    TCC
        LJMP    SHIFT
        LJMP    MIX
        LJMP    EGRSW
        LJMP    TURBO

;############################################################

C320_ABORT:
        MOV     A,#16
        MOV     DPTR,#T_ABORTED
        LCALL   MESSAGE

        LCALL   C3_EXIT
        LJMP    MAIN

C3_20:  MOV     DPTR,#buffer
        MOV     A,#20                ;20 bytes per frame
        LCALL   C3_INIT
        CJNE    A,#0,C320_ABORT      ;init failed: abort!

        LCALL   CLS                  ;Print string: waiting
        MOV     DPTR,#T_WAITING
        LCALL   STROUT

        MOV     A,#5
        LCALL   DELAY
C3_20LP:
        LCALL   GETCH                ;abort if any key pressed
        CJNE    A,#0FFH,C320_ABORT

        LCALL   C3_TESTLOCK          ;check if communication has been established
        CJNE    A,#0,C3_20_START
        SJMP    C3_20LP


;-------------------------------------------------------------------

C3_20_START:
        MOV     DPTR,#T_READING
        LCALL   STROUT

        MOV     A,#5              ;wait 0.5s
        LCALL   DELAY
;-----------------------------------------------------------------
C3_20_MAIN:
        MOV     DPTR,#MENU_C3     ;Show main menu
        MOV     A,#0
        LCALL   SEL_MENU
        CJNE    A,#0FFH,C3_20_M

        LCALL   C3_EXIT
        LJMP    MODE_SELECTED     ;Aborted: go to previous menu

C3_20_M:
        PUSH    ACC
        LCALL   CLS
        POP     ACC

        MOV     default,#0
        MOV     DPTR,#C320MTAB    ;use Jumptable to branch to area
        LCALL   SWITCH

        SJMP    C3_20_MAIN

MENU_C3:
        DB      3,0
        DB      'Trouble Codes',0
        DB      'Sensors&Values',0
        DB      'Switches&Flags',0

C320MTAB:
        LJMP    C3_20_CODES
        LJMP    C3_20_SENSORS
        LJMP    C3_20_FLAGS
;---------------------------------------------------------
C3_20_SENSORS:
        MOV     DPTR,#SENSORS_C320   ;Show main menu
        MOV     A,default
        LCALL   SEL_MENU
        CJNE    A,#0FFH,C3_20_DO

        LJMP    C3_20_MAIN

C3_20_DO:
        MOV     default,A

        PUSH    ACC
        LCALL   CLS
        POP     ACC

        MOV     DPTR,#C320TAB     ;use Jumptable to branch to reading
        LCALL   SWITCH

        SJMP    C3_20_SENSORS
;---------------------------------------------------------
C3_20_FLAGS:
        MOV     DPTR,#FLAGSC320   ;Show main menu
        MOV     A,default
        LCALL   SEL_MENU
        CJNE    A,#0FFH,C3_20_ADO

        LJMP    C3_20_MAIN

C3_20_ADO:
        MOV     default,A

        PUSH    ACC
        LCALL   CLS
        POP     ACC

        MOV     DPTR,#C320TAB2    ;use Jumptable to branch to vehicle
        LCALL   SWITCH

        SJMP    C3_20_FLAGS
;---------------------------------------------------------
C3_20_CODES:
        MOV     A,#buffer+11
        LCALL   DISPL_CODES
        LJMP    C3_20_MAIN
;---------------------------------------------------------
SENSORS_C320:
        DB      12,0
        DB      'Prom-Id',0
        DB      'Coolant Temp',0
        DB      'MAP Sensor',0
        DB      'TPS Sensor',0
        DB      'O2 Sensor',0
        DB      'Battery Voltage',0
        DB      'Engine Speed',0
        DB      'Speed Sensor',0
        DB      'IAC Valve',0
        DB      'Integrator',0
        DB      'Block Learn',0
        DB      'O',LOW2,'-Crosscounts',0

C320TAB:
        LJMP    PROM_ID
        LJMP    COOLANT
        LJMP    MAP
        LJMP    TPS
        LJMP    O2
        LJMP    BAT
        LJMP    RPM
        LJMP    VSS
        LJMP    IAC
        LJMP    INT
        LJMP    BLM
        LJMP    O2X


FLAGSC320:
        DB      10,0
        DB      'Battery Status',0
        DB      'Loop Status',0
        DB      'P/N Switch',0
        DB      'A/C Request',0
        DB      'A/C Clutch',0
        DB      'Fan Relay',0
        DB      'TCC Solenoid',0
        DB      'Shift Light',0
        DB      'Mixture',0
        DB      'Clear Flood Md',0


C320TAB2:
        LJMP    BATSTAT
        LJMP    LOOP
        LJMP    PN
        LJMP    ACREQ
        LJMP    ACCLU
        LJMP    FAN
        LJMP    TCC
        LJMP    SHIFT
        LJMP    MIX
        LJMP    FDMODE
;--------------------------------------------------------
BATSTAT:
        MOV     A,#5
        LCALL   DELAY          ; debounce a little

BS_LOOP:
        LCALL   CLS            ; clear screen

        MOV     DPTR,#T_BATSTAT
        LCALL   STROUT

        MOV     DPTR,#T_NORM   ;default: normal

        MOV     R0,#buffer+14
        MOV     A,@R0
        JNB     ACC.2,BAT_NOLOW
        MOV     DPTR,#T_LOW
BAT_NOLOW:
        MOV     R0,#buffer
        MOV     A,@R0
        JNB     ACC.6,BAT_NOHIGH
        MOV     DPTR,#T_HIGH
BAT_NOHIGH:
        LCALL   STROUT

        LCALL   WAIT_OR_ABORT
        CJNE    A,#0,BS_LOOP
        RET


;--------------------------------------------------------
;############ Display functions for 20 byte C3 ##########
;--------------------------------------------------------
PROM_ID:
        MOV     DPTR,#S_PROMID
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
COOLANT:
        MOV     DPTR,#S_COOL
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
MAP:
        MOV     DPTR,#S_MAP
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
TPS:
        MOV     DPTR,#S_TPS
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
O2:
        MOV     DPTR,#S_O2
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
BAT:
        MOV     DPTR,#S_BAT
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
RPM:
        MOV     DPTR,#S_RPM
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
VSS:
        MOV     DPTR,#S_VSS
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
IAC:
        MOV     DPTR,#S_IAC
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
INT:
        MOV     DPTR,#S_INT
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
BLM:
        MOV     DPTR,#S_BLM
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
O2X:
        MOV     DPTR,#S_O2X
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
COOLANT2:
        MOV     DPTR,#S_COOL2
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
BPW:    MOV     DPTR,#S_BPW
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
ADV:    MOV     DPTR,#S_ADV
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
MAT:    MOV     DPTR,#S_MAT
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
EGR:    MOV     DPTR,#S_EGR
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
LOOP:
        MOV     DPTR,#S_LOOP
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
PN:
        MOV     DPTR,#S_PN
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
ACREQ:
        MOV     DPTR,#S_ACREQ
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
ACCLU:
        MOV     DPTR,#S_ACCLU
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
FAN:
        MOV     DPTR,#S_FAN
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
TCC:
        MOV     DPTR,#S_TCC
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
SHIFT:
        MOV     DPTR,#S_SHIFT
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
MIX:
        MOV     DPTR,#S_MIX
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
FDMODE:
        MOV     DPTR,#S_FDMODE
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
TURBO:
        MOV     DPTR,#S_TURBO
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
EGRSW:
        MOV     DPTR,#S_EGRSW
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
ACCLU_V6:
        MOV     DPTR,#S_ACCLU2
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------

S_PROMID:     DB      buffer+1,13,'PROM-ID: $',0,0
S_COOL:       DB      buffer+4,7,'Coolant Temp:',13,10,0,DEGREE,' Celsius',0
S_MAP:        DB      buffer+6,2,'MAP Sensor',13,10,'Voltage: ',0,' V',0
S_TPS:        DB      buffer+8,2,'TPS Sensor',13,10,'Voltage: ',0,' V',0
S_O2:         DB      buffer+10,10,'Oxygen Sensor:',13,10,0,' V',0
S_BAT:        DB      buffer+17,3,'Battery: ',0,' V',0
S_RPM:        DB      buffer+7,5,'Engine at',13,10,0,' rpm',0
S_VSS:        DB      buffer+5,0,'Vehicle Speed:',13,10,0,' mph',0
S_IAC:        DB      buffer+3,0,'Idle Air Control',13,10,0,' steps',0
S_INT:        DB      buffer+9,0,'Integrator: ',0,0
S_BLM:        DB      buffer+18,0,'Block Learn',13,10,'Multiplier: ',0,0
S_O2X:        DB      buffer+19,0,'O',LOW2,'-Crosscounts:',13,10,0,0
S_COOL2:      DB      buffer+4,6,'Coolant Temp:',13,10,0,DEGREE,' Celsius',0
S_ADV:        DB      buffer+20,9,'Spark Advance:',13,10,0,DEGREE,' from base',0
S_EGR:        DB      buffer+21,4,'EGR Duty Cycle',13,10,0,'%',0
S_BPW:        DB      buffer+23,11,'Base Pulse Width',13,10,0,'ms',0
S_MAT:        DB      buffer+22,7,'Manifold Air',13,10,'Temp: ',0,DEGREE,' C',0


S_LOOP:       DB      buffer+14,128+7+16,'Engine is in',13,10,0,' loop',0
S_MIX:        DB      buffer+14,128+6+32,'Mixture is ',0,0
S_FDMODE:     DB      buffer+14,128+0,'Clear Flood Mode',13,10,'is ',0,0

S_PN:         DB      buffer+15,128+7,'Park/Neutral',13,10,'switch: ',0,0
S_TURBO:      DB      buffer+15,128+6,'Turbo Wastegate',13,10,'Bypass: ',0,0
S_ACREQ:      DB      buffer+15,128+5+8+48,'A/C request: ',0,0
S_EGRSW:      DB      buffer+15,128+4+16,'EGR diagnostic',13,10,'switch: ',0,0

S_ACCLU:      DB      buffer+16,128+5,'A/C clutch: ',0,0
S_FAN:        DB      buffer+16,128+4,'Fan relay: ',0,0
S_TCC:        DB      buffer+16,128+2,'TCC solenoid:',13,10,0,0
S_SHIFT:      DB      buffer+16,128+2,'Shift light: ',0,0
S_ACCLU2:     DB      buffer+16,128+0,'A/C clutch: ',0,0

;###################################################################
;Display Codes starting at RAM address in ACC
DISPL_CODES:
        MOV     R7,A            ;store buffer address
        MOV     A,#0
        MOV     R6,A            ;current byte index
        MOV     R5,#080H        ;Rotating bitmask
        MOV     R4,A            ;Trouble Code Index in Table

        MOV     A,#5
        LCALL   DELAY

DC_LOOP:
        LCALL   GETCH
        CJNE    A,#0FFH,DC_EXIT ;exit if key pressed

        LCALL   C3_TESTLOCK    ; check if no timeout
        CJNE    A,#0,DC_CONT

        LCALL   CLS
        MOV     DPTR,#T_NO_DATA ; print "no data"
        LCALL   STROUT
;-----------------------------------------------------------------
;Wait for next frame or exit
DC_WAITNEXT:
        LCALL   C3_CLEARFLAG
DC_WAIT2:
        LCALL   C3_FRAMERCVD
        CJNE    A,#0,DC_LOOP   ; new frame ready: display it

        LCALL   GETCH
        CJNE    A,#0FFH,DC_EXIT

        SJMP    DC_WAIT2

DC_EXIT:
        MOV     A,#0
        RET                    ; user has pressed a key: return 0!


;-----------------------------------------------------------------
DC_CONT:
        MOV     A,R7
        MOV     R0,A
        MOV     A,@R0          ;check if any Trouble Code is set at all...
        INC     R0
        ORL     A,@R0
        INC     R0
        ORL     A,@R0
        JNZ     CYCLE_CODES

        LCALL   CLS
        MOV     DPTR,#T_NO_CODES
        LCALL   STROUT

        SJMP    DC_WAITNEXT


;------------------------------------------------------------------
CYCLE_CODES:
        MOV     R3,#0           ;flag if a code was output

        MOV     A,R7            ;RAM address
        ADD     A,R6            ;Index in Byte
        MOV     R0,A
        MOV     A,@R0           ;get indirect

        ANL     A,R5            ;mask bit
        JZ      NO_TCODE        ;zero: Code not set

        LCALL   CLS
        MOV     A,R4            ;get Index
        LCALL   SCAN_NAME       ;print description

        MOV     R3,#0FFH        ;set flag: Code printed

NO_TCODE:
        INC     R4              ;increment index in table

        MOV     A,R5            ;Bitmask
        RR      A               ;rotate right
        MOV     R5,A

        CJNE    A,#080H,WT_TROUBLE

        INC     R6              ;next Byte
        MOV     A,R6
        CJNE    A,#3,WT_TROUBLE ;all three bytes checked?

        MOV     R6,#0           ;yes: start over
        MOV     R4,#0

WT_TROUBLE:
        MOV     A,R3            ;Trouble Code printed?
        JZ      CYCLE_CODES     ;no, check next code immediately

        MOV     R2,#20          ;wait 20x 0.1s
WT_TC:   LCALL   GETCH           ;any key aborts
        CJNE    A,#0FFH,DC_EXIT

        MOV     A,#1
        LCALL   DELAY
        DJNZ    R2,WT_TC

        LJMP    DC_LOOP
;----------------------------------------------------------------------------
SCAN_NAME:
        MOV     DPTR,#CODETBL     ;convert index 0-23 to trouble code number
        MOVC    A,@A+DPTR

        MOV     DPTR,#TXT_CODE    ;list of code texts
        LCALL   CODE_TEXT         ;print code text

        RET

; Trouble Code index to number conversion table
CODETBL:
        DB      12,13,14,15,21,22,23,24
        DB      25,31,32,33,34,35,41,42
        DB      43,44,45,51,52,53,54,55
;
TXT_CODE:
        DB 12,'Code 12: No',13,10,'Distrib. Pulses',0
        DB 13,'Code 13:',13,10,'O',LOW2,'Sensor open',0
        DB 14,'Code 14: Coolant',13,10,'temperature high',0
        DB 15,'Code 15: Coolant',13,10,'temperature low',0
        DB 21,'Code 21:',13,10,'TPS voltage high',0
        DB 22,'Code 22:',13,10,'TPS voltage low',0
        DB 23,'Code 23: MAT',13,10,'temperature low',0
        DB 24,'Code 24: Vehicle',13,10,'Speed Sensor',0

        DB 25,'Code 25: MAT',13,10,'temperature high',0
        DB 31,'Code 31:',13,10,'Turbo Wastegate',0
        DB 32,'Code 32: EGR Ckt',13,10,'vacuum switch',0
        DB 33,'Code 33:',13,10,'MAP voltage high',0
        DB 34,'Code 34:',13,10,'MAP voltage low',0
        DB 35,'Code 35:',13,10,'Idle Speed Error',0
        DB 41,'Code 41:',13,10,'Cam Sensor',0
        DB 42,'Code 42: Electr.',13,10,'Spark Timing Ckt',0

        DB 43,'Code 43: Electr.',13,10,'Spark Control',0
        DB 44,'Code 44:',13,10,'Lean exhaust',0
        DB 45,'Code 45:',13,10,'Rich exhaust',0
        DB 51,'Code 51:',13,10,'PROM Error',0
        DB 52,'Code 52:',13,10,'CALPAK Error',0
        DB 53,'Code 53: Battery',13,10,'voltage high',0
        DB 54,'Code 54: Mixture',13,10,'Ctrl Solenoid',0
        DB 55,'Code 55:',13,10,'ECM Error',0
;-----------------------------------------------------------------------
;Display Codes starting at RAM address in ACC
UDISPL_CODES:
        MOV     R7,A            ;store buffer address
        MOV     A,#0
        MOV     R6,A            ;current byte index
        MOV     R5,#080H        ;Rotating bitmask
        MOV     R4,A            ;Trouble Code Index in Table

        MOV     A,#5
        LCALL   DELAY

UDC_LOOP:
        LCALL   GETCH
        CJNE    A,#0FFH,UDC_EXIT ;exit if key pressed

        LCALL   UART_TESTLOCK   ; check if no timeout
        CJNE    A,#0,UDC_CONT

        LCALL   CLS
        MOV     DPTR,#T_NO_DATA ; print "no data"
        LCALL   STROUT
;-----------------------------------------------------------------
;Wait for next frame or exit
UDC_WAITNEXT:
        LCALL   UART_CLEARFLAG
UDC_2WAIT:
        LCALL   UART_FRAMERCVD
        CJNE    A,#0,UDC_LOOP   ; new frame ready: display it

        LCALL   GETCH
        CJNE    A,#0FFH,UDC_EXIT

        SJMP    UDC_2WAIT

UDC_EXIT:
        MOV     A,#0
        RET                    ; user has pressed a key: return 0!

UDC_CONT:
        LJMP    DC_CONT        ; share common code

;#######################################################################
; 64 Byte per Frame P4 ECM
;#######################################################################

P467_ABORT:
        LJMP    C320_ABORT

UART_67:
        MOV     DPTR,#header
        MOV     A,#67                ;67 header+buffer size
        MOV     R0,#0F0H             ;Expect $F0 as first header byte
        LCALL   UART_INIT
        CJNE    A,#0,P467_ABORT      ;init failed: abort!

        LCALL   CLS                  ;Print string: waiting
        MOV     DPTR,#T_WAITING
        LCALL   STROUT

        MOV     A,#5
        LCALL   DELAY
P4_67LP:
        LCALL   GETCH                ;abort if any key pressed
        CJNE    A,#0FFH,P467_ABORT

        LCALL   C3_TESTLOCK          ;check if communication has been established
        CJNE    A,#0,P4_67_START
        SJMP    P4_67LP

P4_67_START:
        MOV     DPTR,#T_READING      ;communication link established, print msg
        LCALL   STROUT

        MOV     A,#5              ;wait 0.5s
        LCALL   DELAY
;-----------------------------------------------------------------
P4_67_MAIN:
        MOV     DPTR,#MENU_C3     ;Show main menu
        MOV     A,#0
        LCALL   SEL_MENU
        CJNE    A,#0FFH,P4_67_M

        LCALL   UART_EXIT
        LJMP    MODE_SELECTED     ;Go back one step up...

P4_67_M:
        PUSH    ACC
        LCALL   CLS
        POP     ACC

        MOV     default,#0
        MOV     DPTR,#P467MTAB    ;use Jumptable to branch to vehicle
        LCALL   SWITCH


P467MTAB:
        LJMP    P4_67_CODES
        LJMP    P4_67_SENSORS
        LJMP    P4_67_FLAGS
;---------------------------------------------------------
P4_67_CODES:
        MOV     A,#buffer+2
        LCALL   UDISPL_CODES
        LJMP    P4_67_MAIN
;---------------------------------------------------------
P4_67_SENSORS:
        MOV     DPTR,#SENS_P467   ;Show main menu
        MOV     A,default
        LCALL   SEL_MENU
        CJNE    A,#0FFH,P4_67_DO

        LJMP    P4_67_MAIN

P4_67_DO:
        MOV     default,A

        PUSH    ACC
        LCALL   CLS
        POP     ACC

        MOV     DPTR,#P467TAB     ;use Jumptable to branch to reading
        LCALL   SWITCH

        SJMP    P4_67_SENSORS
;---------------------------------------------------------
P4_67_FLAGS:
        MOV     DPTR,#FLAGS_P467   ;Show main menu
        MOV     A,default
        LCALL   SEL_MENU
        CJNE    A,#0FFH,P4_67_ADO

        LJMP    P4_67_MAIN

P4_67_ADO:
        MOV     default,A

        PUSH    ACC
        LCALL   CLS
        POP     ACC

        MOV     DPTR,#P467TAB2    ;use Jumptable to branch to flag
        LCALL   SWITCH

        SJMP    P4_67_FLAGS
;----------------------------------------------------------------------------
UBATSTAT:
        MOV     A,#5
        LCALL   DELAY          ; debounce a little

UBS_LOOP:
        LCALL   CLS            ; clear screen

        MOV     DPTR,#T_BATSTAT
        LCALL   STROUT

        MOV     DPTR,#T_NORM   ;default: normal

        MOV     R0,#buffer+54
        MOV     A,@R0
        JNB     ACC.2,UBAT_NOTLOW
        MOV     DPTR,#T_LOW
UBAT_NOTLOW:
        MOV     R0,#buffer+49
        MOV     A,@R0
        JNB     ACC.6,UBAT_NOHIGH
        MOV     DPTR,#T_HIGH
UBAT_NOHIGH:
        LCALL   STROUT

        LCALL   WAIT_OR_ABORT
        CJNE    A,#0,UBS_LOOP
        RET
;-------------------------------------------------------------------
SENS_P467:
        DB      22,0
        DB      'Prom-Id',0
        DB      'Coolant Temp',0
        DB      'Start CLT',0
        DB      'MAT Sensor',0
        DB      'MAP Sensor',0
        DB      'Barom. pressure',0
        DB      'TPS Sensor',0
        DB      'Throttle Pos.',0
        DB      'O2 Sensor',0
        DB      'Battery Voltage',0
        DB      'Engine Speed',0
        DB      'Desired idle',0
        DB      'Speed Sensor',0
        DB      'IAC Valve',0
        DB      'Desired IAC',0
        DB      'Integrator',0
        DB      'Block Learn',0
        DB      'O',LOW2,'-Transitions',0
        DB      'Injector BPW',0
        DB      'Air/Fuel ratio',0
        DB      'Spark Advance',0
        DB      'Engine on time',0
;------------------------------------------------------------------------
P467TAB:                          ;Sensors
        LJMP    UPROM_ID
        LJMP    UCOOLANT
        LJMP    USTRTCOOL
        LJMP    UMAT
        LJMP    UMAP
        LJMP    UBARO
        LJMP    UTPS
        LJMP    UTHR
        LJMP    UO2
        LJMP    UBAT
        LJMP    URPM
        LJMP    UDIDLE
        LJMP    UVSS
        LJMP    UIAC
        LJMP    UDIAC
        LJMP    UINT
        LJMP    UBLM
        LJMP    UO2X
        LJMP    UBPW
        LJMP    UAFR
        LJMP    UADV
        LJMP    UENG

FLAGS_P467:
        DB      15,0
        DB      'Battery Status',0
        DB      'Loop Status',0
        DB      'P/N Switch',0
        DB      'A/C Request',0
        DB      'A/C Clutch',0
        DB      'Fan Relay',0
        DB      'TCC Solenoid',0
        DB      'Shift Light',0
        DB      'Mixture',0
        DB      'Cruise: ON',0
        DB      'Cruise: SET',0
        DB      'Cruise: RES/ACC',0
        DB      'Cruise: Brake',0
        DB      'Clear Flood Md',0
        DB      'Power Steering',0

P467TAB2:                         ;Flags
        LJMP    UBATSTAT
        LJMP    ULOOP
        LJMP    UPN
        LJMP    UACREQ
        LJMP    UACCLU
        LJMP    UFAN
        LJMP    UTCC
        LJMP    USHIFT
        LJMP    UMIX
        LJMP    UCON
        LJMP    UCSET
        LJMP    UCRES
        LJMP    UCBRK
        LJMP    UFDMODE
        LJMP    USTEER


;--------------------------------------------------------
;############ Display functions for 67 byte UART ##########
;--------------------------------------------------------
UPROM_ID:
        MOV     DPTR,#U_PROMID
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UCOOLANT:
        MOV     DPTR,#U_COOL
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
USTRTCOOL:
        MOV     DPTR,#U_STCOOL
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UMAP:
        MOV     DPTR,#U_MAP
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UBARO:
        MOV     DPTR,#U_BARO
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UTPS:
        MOV     DPTR,#U_TPS
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UTHR:
        MOV     DPTR,#U_THR
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UO2:
        MOV     DPTR,#U_O2
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UBAT:
        MOV     DPTR,#U_BAT
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
URPM:
        MOV     DPTR,#U_RPM
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UDIDLE:
        MOV     DPTR,#U_DIDLE
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UVSS:
        MOV     DPTR,#U_VSS
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UIAC:
        MOV     DPTR,#U_IAC
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UDIAC:
        MOV     DPTR,#U_DIAC
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UINT:
        MOV     DPTR,#U_INT
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UBLM:
        MOV     DPTR,#U_BLM
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UO2X:
        MOV     DPTR,#U_O2X
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UBPW:   MOV     DPTR,#U_BPW
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UADV:   MOV     DPTR,#U_ADV
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UMAT:   MOV     DPTR,#U_MAT
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UAFR:   MOV     DPTR,#U_AFR
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UENG:   MOV     DPTR,#U_ENGINE
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
ULOOP:
        MOV     DPTR,#U_LOOP
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UPN:
        MOV     DPTR,#U_PN
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UACREQ:
        MOV     DPTR,#U_ACREQ
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UACCLU:
        MOV     DPTR,#U_ACCLU
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UFAN:
        MOV     DPTR,#U_FAN
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UTCC:
        MOV     DPTR,#U_TCC
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
USHIFT:
        MOV     DPTR,#U_SHIFT
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UMIX:
        MOV     DPTR,#U_MIX
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UFDMODE:
        MOV     DPTR,#U_FDMODE
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
USTEER:
        MOV     DPTR,#U_STEER
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UCON:
        MOV     DPTR,#U_CR_ON
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UCSET:
        MOV     DPTR,#U_CR_SET
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UCRES:
        MOV     DPTR,#U_CR_RES
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------
UCBRK:
        MOV     DPTR,#U_CR_BRK
        LJMP    SLOT_DISPITEM
;--------------------------------------------------------

U_PROMID:     DB      buffer+0, 13,'PROM-ID: $',0,0
U_COOL:       DB      buffer+5,  6,'Coolant Temp:',13,10,0,DEGREE,' Celsius',0
U_STCOOL:     DB      buffer+6,  6,'Startup Coolant',13,10,'Temp: ',0,DEGREE,' C',0
U_MAP:        DB      buffer+25, 2,'MAP Sensor',13,10,'Voltage: ',0,' V',0
U_TPS:        DB      buffer+7,  2,'TPS Sensor',13,10,'Voltage: ',0,' V',0
U_THR:        DB      buffer+8 , 4,'Throttle: ',0,'%',0
U_O2:         DB      buffer+14,10,'Oxygen Sensor:',13,10,0,' V',0
U_BAT:        DB      buffer+31, 3,'Battery: ',0,' V',0
U_RPM:        DB      buffer+9,  5,'Engine at',13,10,0,' rpm',0
U_VSS:        DB      buffer+12, 0,'Vehicle Speed:',13,10,0,' mph',0
U_IAC:        DB      buffer+20, 0,'Idle Air Control',13,10,0,' steps',0
U_DIAC:       DB      buffer+21, 0,'Desired IAC',13,10,0,' steps',0
U_INT:        DB      buffer+19, 0,'Integrator: ',0,0
U_BLM:        DB      buffer+17, 0,'Block Learn',13,10,'Multiplier: ',0,0
U_O2X:        DB      buffer+15, 0,'O',LOW2,'-Transitions:',13,10,0,0
U_ADV:        DB      buffer+34, 9,'Spark Advance:',13,10,0,DEGREE,' from base',0
U_BPW:        DB      buffer+35,11,'Base Pulse Width',13,10,0,'ms',0
U_MAT:        DB      buffer+30, 7,'Manifold Air',13,10,'Temp: ',0,DEGREE,' C',0
U_DIDLE:      DB      buffer+23,15,'Desired idle',13,10,0,'rpm',0
U_BARO:       DB      buffer+24,14,'Barometric',13,10,'press.: ',0,'kPa',0
U_AFR:        DB      buffer+37, 3,'Air/Fuel Ratio',13,10,0,':1',0
U_ENGINE:     DB      buffer+39,12,'Engine running',13,10,0,' sec.',0

U_ACCLU:      DB      buffer+50,128+5+8,'A/C clutch: ',0,0

U_LOOP:       DB      buffer+54,128+7+16,'Engine is in',13,10,0,' loop',0
U_MIX:        DB      buffer+54,128+6+32,'Mixture is ',0,0
U_FDMODE:     DB      buffer+54,128+0,   'Clear Flood Mode',13,10,'is ',0,0

U_PN:         DB      buffer+55,128+0+8,'Park/Neutral',13,10,'switch: ',0,0
U_STEER:      DB      buffer+55,128+2+8,'Power',13,10,'Steering: ',0,0
U_CR_ON:      DB      buffer+55,128+3,   'Cruise ON',13,10,'switch: ',0,0
U_CR_BRK:     DB      buffer+55,128+4+8, 'Cruise brake',13,10,'switch: ',0,0
U_CR_SET:     DB      buffer+55,128+5,   'Cruise SET',13,10,'switch: ',0,0
U_CR_RES:     DB      buffer+55,128+6,   'Cruise RES/ACC',13,10,'switch: ',0,0
U_ACREQ:      DB      buffer+55,128+7+48,'A/C request: ',0,0

U_FAN:        DB      buffer+57,128+0,'Fan relay: ',0,0
U_TCC:        DB      buffer+57,128+7,'TCC solenoid:',13,10,0,0
U_SHIFT:      DB      buffer+57,128+7,'Shift light: ',0,0
;
;-----------------------------------------------------------------------
; misc. Strings
;
T_NO_CODES: DB    'No Trouble Codes', 13, 10, 'Stored!',0
T_NO_DATA:  DB    '*** No data! ***',0
E_NOIMPL:
        DB      'Not Implemented!',0
T_LOW:  DB      'Low/IAC Inhibitd',0
T_HIGH: DB      'High/Overvoltage',0
T_NORM: DB      'Normal',0
T_BATSTAT:
        DB      'Battery Status:',13,10,0
T_ABORTED:
        DB      'Aborted!',0
T_WAITING:
        DB      'Waiting...',13,10,0
T_READING:
        DB      'Reading Data...',0

        END


