include ht45f24a.inc 
;--------------------------------------------------------
_SCS_EN         MACRO 
                SET     CSEN            ;SCS Pin  Enable/Disable  1/0...
                ENDM
;--------------------------------------------------------
_SCS_DIS        MACRO 
                CLR     CSEN            ;SCS Pin  Enable/Disable  1/0...
                ENDM
;--------------------------------------------------------
 
_EN_HT82V739    MACRO 
                mov     A,11111100b     ;; Enable HT82V739
                andm    A,PC            ;; Enable Flash ROM
                ENDM
;--------------------------------------------------------
_DIS_HT82V739   MACRO
                mov     A,00000011b     ;; Disable HT82V739
                orm     A,PC            ;; Disable Flash ROM
                ENDM
;========================================================================
data1           .section    'data'
        c_LVD_27             EQU     00000011B;00000001B=2.2V  00000010B=2.4V  00000011B=2.7V
        count                   db      ?
        Temp                    db      ?
        Temp1                   db      ?
        Temp2                   db      ?
        a_LVD_step              db      ?
        a_LVD_MODE1             db      ?
        a_LVD_MODE2             db      ?
        a_LVD_MODE3             db      ?
        a_LED_cycle             db      ?
        a_LED_HI                db      ?
        a_LED_LOW               db      ?
        a_LED_Count             db      ? ;; LED flash time
        a_II0_PFD0              db      ? ;; Timer Freq. divider
        a_II0_PFD1              db      ? ;; Timer Freq. divider
        a_II0_PFD2              db      ? ;; Timer Freq. divider
        #define c_II0_PFD0      002d      ;;  500uS x 2   = 1000uS  (1mS)
        #define c_II0_PFD1      0200d     ;;  1mS x 200   = 200mS  (200mS)
        #define c_II0_PFD2      050d      ;;  200mS x 50  = 10sec  (10sec)
        #define pn_PA1          PA.1
        #define pn_IR_TX        PC.6
        #define pn_Tx           PB.6
        #define fg_CF           STATUS.0
        #define fg_ZF           STATUS.2  
        #define NOP2            JMP     $+1
        #define c_AN7           111b
        #define pn_Key          PA.0
        #define pn_LED          PC.4
        fg_Test_mode            dbit
        fg_Low_Baterry          dbit
        fg_LED                  dbit
        fg_LED_Status           dbit
        fg_Have_Smoke           dbit
        fg_KEY                  dbit
        fg_LOOP_LowBatStep2     dbit
        fg_Toggle_LowBatStep2   dbit
        a_DebounceKey           db      ?
        a_DebounceKey_Long      db      ?
        fg_WaitReleaseKey       dbit
        fg_WithLongKeyIn        dbit
        fg_WithKeyIn            dbit
        fg_10Sec_En             dbit
        #define c_DebounceKey_Long      0150d   
        #define c_DebounceKey           020d                    
        fg_Tx_END       dbit
                ;;--- [2] Timing Flag define ---
        fg_10Sec        dbit
        fg_key_busy     dbit
        fg_set_smoke_value      dbit
        fg_disable_VOICE        dbit
        a_II0_ACC               db      ?
        a_II0_STATUS            db      ?
        a_II1_ACC               db      ?
        a_II1_STATUS            db      ?
        a_low_Baterry_count     db      ?
;==========================================================================================
        a_SR10                  db      ?
        a_SR11                  db      ?
        a_SR12                  db      ?
        a_SR13                  db      ?
        a_SR14                  db      ?
        a_SR15                  db      ?
        a_SR16                  db      ?
        a_SCAN_X                db      ?
        a_SCAN_Y                db      ?
;==========================================================================================
        a_EE_address            db      ?
        a_Reference_H           db      ?       ;;eeprom address 00h
        a_Reference_L           db      ?       ;;eeprom address 01h
        a_smoke_value_H         db      ?       ;;eeprom address 02h
        a_smoke_value_L         db      ?       ;;eeprom address 03h

        a_AN7_BUF_H             db      ?
        a_AN7_BUF_L             db      ?

        DECL_buffer             db      ?
;=====================================================================
        R_OUT                   DB      ?
        R_OUT_BUF               DB      ?
        R_ADDR_L                DB      ?
        R_ADDR_M                DB      ?
        R_ADDR_H                DB      ?
        R_BlockSize_L           DB      ?
        R_BlockSize_M           DB      ?
        R_BlockSize_H           DB      ?
        R_DATA_BUF              DB      ?
;=====================================================================
        latchd                  DB      ?
        DA1bufL_1               DB      ?
        DA1bufL                 DB      ?
        DA1bufH                 DB      ?
        tracknumber01           DB      ?
        tracknumber02           DB      ?
        tracknumber03           DB      ?
        EndSection1             dbit
        F_PlayBusy_Fg           dbit
        F_FirstData_Fg          dbit
        tickCnt                 DB      ?
        Len1L                   DB      ?
        Len1M                   DB      ?
        Len1H                   DB      ?
        CONTROLCODE             DB      ?
;========================================================================

code1           .section   at 00h   'code'

;========================================================================
                org     00h

                SZ      PDF                     ;;;; Only after HALT be set '1'
                SNZ     TO                      ;;;; WDT Timer Out TO = 1
                JMP     POWER_ON_PROC           ;;;; Power On PROC
                JMP     WDT_TIME_OUT_PROC       ;;;; WDT Time out PROC

                org     04h
                RETI

                org     08h
                RETI
 
                org     0ch
                JMP     I_TIMER0

                org     10h
                JMP     I_TIMER1
                org     14h
                RETI

                org     18h
                RETI
;======================================================================
                ORG     020h
POWER_ON_PROC:
                clr     wdt
                CALL    INIT_IO
                CALL    DEFAULT_RAM
                CALL    INIT_WDTC
                CALL    INIT_EEPROM
                CALL    INIT_TIMER
                CALL    INIT_INTERRUPT
                CALL    INIT_ADC
                CALL    INIT_DAC
                CALL    INIT_OPA
                CALL    INIT_VOICE_ROM
                CALL    Disable_VOICE                
                CALL    Smoke_Check
;========================================================================
MAIN_LOOP:
                clr     wdt
                sz      fg_Test_mode
                JMP     Test_mode_Step1
                JMP     NORMAL_MODE1
NORMAL_MODE1:
                clr     wdt
                CALL    Baterry_Detect
                CALL    Smoke_Check
                CALL    VOICE_PROC
                CALL    KEY_IN_PROC
                CALL    LED_PROC
IN_HALT:
                CALL    Enter_HALT_SET
                NOP
                NOP
                HALT
                nop
                nop
WDT_TIME_OUT_PROC:
                CALL    KEY_IN_PROC
                snz     fg_Test_mode
                JMP     NORMAL_MODE1

Test_mode_Step1:
                CALL    Smoke_Check
Test_mode_Step2:
                CALL    SEND_UART

                CLR     WDT
                SNZ     pn_key
                JMP     $-1
                SNZ     fg_WithKeyIn
                JMP     Test_mode_Step3
                clr     fg_disable_VOICE
                clr     fg_WithKeyIn
                clr     fg_Have_Smoke
Test_mode_Step3:
                SZ      fg_disable_VOICE
                JMP     IN_HALT
                SNZ     fg_Have_Smoke
                JMP     IN_HALT
                CALL    INIT_VOICE_ROM

                MOV     A,4                    ;huozaifasheng..........
                CALL    PLAY_VOICE
                SNZ     EndSection1
                JMP     $-1
                CALL    Disable_VOICE
                clr     fg_Have_Smoke
                set     fg_disable_VOICE
                JMP     IN_HALT


KEY_IN_PROC:
                clr     wdt
                SZ      fg_Test_mode
                RET
                SZ      fg_Low_Baterry
                RET
                SNZ     fg_WithLongKeyIn
                JMP     DO_SHORT_KEY
         DO_LONG_KEY:
                CALL    KEY_PRESS_LARGE_3S     ;;>3sec
                clr     fg_WithKeyIn
                clr     fg_WithKeyIn
         DO_SHORT_KEY:
                SZ      fg_WithKeyIn
                CALL    KeySmall3S_RELEASE     ;;<3sec
                SZ      pn_key
                RET
                JMP     KEY_IN_PROC




VOICE_PROC:
                SZ      fg_Have_Smoke
                CALL    Have_Smoke
                RET
LED_PROC:
                SZ      fg_Low_Baterry
                RET
                sdz     a_LED_cycle
                RET
                CLR     WDT
                set     fg_LED
                clr     pn_LED
                mov     a,100
                mov     a_LED_HI,a
                sz      fg_LED
                JMP     $-1
                mov     A,10d
                mov     a_LED_cycle,A
                RET
LED_PROC1:

LED_PROC_STEP1:
                mov     a,3
                mov     a_LED_Count,a
LED_PROC_STEP2:
                mov     a,50
                mov     a_LED_HI,a
                mov     a,50
                mov     a_LED_LOW,a
                clr     WDT
                set     fg_LED
                clr     pn_LED
                sz      fg_LED
                JMP     $-1
                sz      fg_LED_STATUS
                JMP     $-1
                sdz     a_LED_Count
                JMP     LED_PROC_STEP2
                RET



Enter_HALT_SET:
                set     pawu.0
                SET     PAPU.0
                SET     PAC.0  ;PAC.0
                CLR     pbc
                CLR     pbpu
                set     PC.4     ;;LED OFF
                _DIS_HT82V739
;-------------------------------------------------
                CLR     LVDEN    ;;LVD OFF
;------------------------------------------------------------------
                clr     cmp1en   ;; CMP1 OFF
                clr     cmp2en   ;; CMP2 OFF
;------------------------------------------------------------------
                clr     a1en     ;;OPA1 OFF
                clr     a2en     ;;OPA2 OFF
;------------------------------------------------------------------
                clr     lcden    ;;LCD OFF
;------------------------------------------------------------------
                clr     ldoen
                CLR     REN1     ; Bias voltage Resistor enable/disable : (1/0)
;------------------------------------------------------------------
                clr     simen
;------------------------------------------------------------------
                SET     ADONB    ; 1: ADC module is turn off.
;------------------------------------------------------------------
                CLR     DACEN    ;D/A Off........
;------------------------------------------------------------------
                clr     PCKP1    ;pckpsc1
                clr     PCKP0    ;pckpsc0
                clr     pcken
                clr     idlen
                RET

;================================================================================
KeySmall3S_RELEASE:
;================================================================================
               clr      fg_WithKeyIn           ;; short_key
               CALL     INIT_VOICE_ROM
               MOV      A,2                    ;Bi...Bi.....
               CALL     PLAY_VOICE
               SNZ      EndSection1
               JMP      $-1
               MOV      A,3                  ;xitongzhengchang (Normal system)
               CALL     PLAY_VOICE
               SNZ      EndSection1
               JMP      $-1
               CALL     Disable_VOICE
               RET
;========================================================================
KEY_PRESS_LARGE_3S:
;========================================================================
               CLR      fg_WithLongKeyIn
               CALL     INIT_VOICE_ROM
               MOV      A,1                     ;BB.........(Alarm sound)
               CALL     PLAY_VOICE
               SNZ      EndSection1
               JMP      $-1
               CALL     Disable_VOICE
               RET

;==================================================================================
Baterry_Detect:
               MOV      A,c_LVD_27              ;00000011B LVD = 2.7V
               MOV      LVDC,A
               SET      LVDEN
               mov      a,80
               sdz      ACC                     ; delay 240 cycles
               JMP      $-1
               SNZ      LVDC.5                  ;LVDO   ;2012 06 20
               JMP      L_SKIP_LowBat_ALARM
               JMP      LowBatStep_0
LowBatStep_0:
               snz      fg_WithKeyIn
               JMP      LowBatStep_0_1
               clr      fg_WithKeyIn
               set      fg_LOOP_LowBatStep2
               JMP      LowBatStep_2
LowBatStep_0_1:
               mov      a,a_LVD_step
               addm     a,PCL
               JMP      LowBatStep_1
               JMP      LowBatStep_2
               JMP      LowBatStep_3
LowBatStep_1:

               CALL     INIT_VOICE_ROM
               MOV      A,2                    ;B........
               CALL     PLAY_VOICE
               SNZ      EndSection1
               JMP      $-1
               MOV      A,0                    ;qinggenghuandianchi.......
               CALL     PLAY_VOICE
               SNZ      EndSection1
               JMP      $-1
               CALL     Disable_VOICE
               clr      fg_WithKeyIn
               set      fg_Low_Baterry
               inc      a_LVD_MODE1
               mov      A,a_LVD_MODE1
               sub      A,2
               snz      C
               JMP      L_BaterryCheck_END
               inc      a_LVD_step
               clr      a_LVD_MODE1
               JMP      L_BaterryCheck_END
;-----------------------------------------------------------------------------
LowBatStep_2:
;-----------------------------------------------------------------------------
               snz      fg_Toggle_LowBatStep2
               JMP      LowBatStep_2_1
               clr      fg_Toggle_LowBatStep2
               CALL     LED_PROC1
               sz       fg_LOOP_LowBatStep2
               JMP      L_BaterryCheck_END
               inc      a_LVD_MODE2
               mov      A,a_LVD_MODE2
               sub      A,8
               snz      C
               JMP      L_BaterryCheck_END
               inc      a_LVD_step
               clr      a_LVD_MODE2
               JMP      L_BaterryCheck_END
LowBatStep_2_1:
               set      fg_Toggle_LowBatStep2
               JMP      L_BaterryCheck_END
;-----------------------------------------------------------------------------
LowBatStep_3:
;-----------------------------------------------------------------------------
               CALL     INIT_VOICE_ROM
               MOV      A,2
               CALL     PLAY_VOICE
               SNZ      EndSection1            ; ³]©wEndSection1 = 1
               JMP      $-1
               CALL     Disable_VOICE
               clr      fg_WithKeyIn
               inc      a_LVD_MODE3
               mov      A,a_LVD_MODE3
               sub      A,3
               snz      C
               JMP      L_BaterryCheck_END
               clr      a_LVD_step
               clr      a_LVD_MODE3
               JMP      L_BaterryCheck_END
;=============================================================================

L_SKIP_LowBat_ALARM:
               clr      a_LVD_step
               clr      a_LVD_MODE1
               clr      a_LVD_MODE2
               clr      a_LVD_MODE3
               CLR      fg_Low_Baterry
               JMP      L_BaterryCheck_END           ;MAIN_LOOP   ;21/01--09
;----------------------------------------------------------------------
L_BaterryCheck_END:
               CLR      LVDEN
               RET

;==================================================================================
Smoke_Check:
;================================================================================
               set      pn_PA1          ;; set OPA Input DC Bias Source Voltage
               set      A1EN
               mov      A,02d           ;; M=2
               CALL     Delay_500uS_ACC ;; 2+1+(1+N*3-1+3)*M-1+2
               set      A2EN
               mov      A,04d
               CALL     Delay_500uS_ACC
               clr      EMI
             set      pn_IR_TX        ;; Set IR LED On
;               mov      a,7
;               mov      a_SR10,a
;LOOP_IR:
;               clr      pn_IR_TX
;               nop2
;               set      pn_IR_TX
;               nop2
;               nop2
;               sdz      a_SR10
;               JMP      LOOP_IR
             NOP
             mov      A,028d          ;; Instruction Delay Loop ;;;; 4MHz Fsys
Delay_ACC:                            ;; 1 Loop have 3 Instruction
             sdz      ACC             ;; Delay time = N * 3 * 4 / Fsys
             JMP      Delay_ACC       ;;            = 30 * 3 * 4 / 4MHz = 90uS
               mov      A,c_AN7         ;;
               CALL     SELE_ADC        ;; Read ADC from OPA2 Output,
               RET

;---------------------------------------------------------------------
Have_Smoke:

               CLR      WDT
               SZ       fg_WithKeyIn
               JMP      Have_Smoke_1

               CALL     INIT_VOICE_ROM
               MOV      A,1
               CALL     PLAY_VOICE
               SNZ      EndSection1             ; ??EndSection1 = 1
               JMP      $-1

               MOV      A,4                    ;huozaifasheng..........
               CALL     PLAY_VOICE
               SNZ      EndSection1
               JMP      $-1

               CALL     Disable_VOICE
               CALL     LED_PROC1
               SET      fg_10Sec_En
;----------------------------------------------------------------
;Have  Smoke  begin  Alarm 10Sec   Or  Wait Key  Press......
;----------------------------------------------------------------------
               NOP
               SNZ      fg_10Sec
               JMP      Have_Smoke
Have_Smoke_1:
               CLR      fg_10Sec
               CLR      fg_10Sec_En
               CLR      fg_Have_Smoke
               CLR      fg_WithKeyIn
               RET
;==========================================================================================
Delay_20ms:
               mov      a,50
               mov      Temp1,a
               JMP      L_Goon_Delay_lp
Delay_200ms:
               mov      a,20
               mov      Temp1,a
L_Goon_Delay_lp:
               mov      a,20
               mov      Temp2,a
L_Goon_Delay_2p:
               mov      a,35
               sdz      ACC     ;105 cycles
               JMP      $-1
               SDZ      Temp2
               JMP      L_Goon_Delay_2p
               SDZ      Temp1
               JMP      L_Goon_Delay_lp
               RET
;==========================================================================================
SEND_UART:
               mov      A,00
               mov      a_SCAN_X,A

               mov      A,a_AN7_BUF_H
               mov      a_SR11,A
               mov      A,a_AN7_BUF_L
               CALL     HEX2BCD_16      ;; before : a_SR10,a_SR11,ACC     after : a_SR13,a_SR14,a_SR15
               CALL     DATA_2_PC
               ret
HEX2BCD_8:
               clr      a_SR11
HEX2BCD_16:
               clr      a_SR10
HEX2BCD_24:
               mov      a_SR12,A
               clr      a_SR15
               clr      a_SR14
               clr      a_SR13
               mov      a,024d          ;; 3 Bytes Transfer , 24Bits
               mov      a_SR16,a
  CONVT:
               rlc      a_SR12
               rlc      a_SR11
               rlc      a_SR10
               mov      a,a_SR15
               adc      a,a_SR15
               daa      a_SR15
               mov      a,a_SR14
               adc      a,a_SR14
               daa      a_SR14
               mov      a,a_SR13
               adc      a,a_SR13
               daa      a_SR13
               SDZ      a_SR16
               JMP      CONVT
               ret


DATA_2_PC:
               clr      EMI
               mov      A,002h          ;;¢s Start Code
               CALL     Send_Byte_1152  ;;¢}
               mov      A,a_SCAN_X
               and      A,00001111b
               add      A,30h
               CALL     Send_Byte_1152
               mov      A,a_SCAN_Y
               and      A,00001111b
               add      A,030h
               CALL     Send_Byte_1152
               swapa    a_SR14
               and      A,00001111b
               add      A,030h
               CALL     Send_Byte_1152
               mov      A,a_SR14
               and      A,00001111b
               add      A,030h
               CALL     Send_Byte_1152
               swapa    a_SR15
               and      A,00001111b
               add      A,030h
               CALL     Send_Byte_1152
               mov      A,a_SR15
               and      A,00001111b
               add      A,030h
               CALL     Send_Byte_1152

               mov      A,003h          ;;¢s End Code
               CALL     Send_Byte_1152  ;;¢}
               set      EMI
               ret


;@********** SUBROUTINE[xx]: 115200BPS - 8.68uS (17 Ins. @ 8MHz) **********
;@********** SUBROUTINE[xx]: 115200BPS - 8.68uS (9 Ins. @ 4MHz) **********
Send_Byte_1152:
               mov      a_SR10,A
               mov      A,008d
               mov      a_SR11,A
  ;;-------------------------------
  Send_Start_1152:
               clr      pn_TX
 ;;              mov      A,004d
 ;;              SDZ      ACC
 ;;              JMP      $-1
               NOP2
               NOP
  ;;-------------------------------
  Send_Bit_1152:
               rrc      a_SR10
               SZ       c
               JMP      Send_Bit_1152_1
               nop2
    Send_Bit_1152_0:
               clr      pn_TX
;;               nop
;;               mov      A,002d
;;               SDZ      ACC
;;               JMP      $-1
               SDZ      a_SR11
               JMP      Send_Bit_1152
               JMP      Send_Stop_1152
;;               JMP      Send_Bit_1152_Next;;2

    Send_Bit_1152_1:
               NOP
               set      pn_TX
;;               nop
;;               mov      A,002d
;;               SDZ      ACC
;;               JMP      $-1
;;               JMP      Send_Bit_1152_Next
               SDZ      a_SR11
               JMP      Send_Bit_1152
               JMP      Send_Stop_1152
    Send_Bit_1152_Next:
;;               SDZ      a_SR11             ;;3
;;               JMP      Send_Bit_1152      ;;5
  ;;-------------------------------
  Send_Stop_1152:
               NOP2
               NOP2
;;               nop
               set      pn_TX
               mov      A,040d
               SDZ      ACC
               JMP      $-1
               ret
;==========================================================================================
delay:
               mov      a,0ffh
               mov      count,a
delay_loop:
               sdz      count
               jmp      delay_loop
               ret

INIT_EEPROM:
               mov      A,a_EE_address
               CALL     ReadEEPROM
               mov      a_Reference_H,A

               inc      a_EE_address
               mov      A,a_EE_address
               CALL     ReadEEPROM
               mov      a_Reference_L,A

               inc      a_EE_address
               mov      A,a_EE_address
               CALL     ReadEEPROM
               mov      a_smoke_value_H,A

               inc      a_EE_address
               mov      A,a_EE_address
               CALL     ReadEEPROM
               mov      a_smoke_value_L,A
               RET

ReadEEPROM:            ; In-> ACC=address, Out-> ACC=Read Data
               mov      EEA,A
               mov      A,01h
               mov      BP,A
               mov      A,040h
               mov      mp1,A
               set      IAR1.1 ; set RDEN bit, enable read operations
               set      IAR1.0 ; start Read Cycle - set RD bit
        wait_Read_EEPROM:
               sz       IAR1.0
               JMP      wait_Read_EEPROM
               clr      IAR1.1
               clr      BP
               mov      A,EED
               RET

INIT_IO:
;========================================================================             
               ;;       ;;+--------- PA7: A2E           [O]
               ;;       ;;|+-------- PA6: A2N           [x]
               ;;       ;;||+------- PA5: A2P           [x]
               ;;       ;;|||+------ PA4: A1E           [O]
               ;;       ;;||||+----- PA3: A1N           [I]
               ;;       ;;|||||+---- PA2: A1P           [I]
               ;;       ;;||||||+--- PA1: bias          [O]
               ;;       ;;|||||||+-- PA0: test key      [I]
               ;;       ;;||||||||
               mov      A,00011100b
               mov      PA,A
               mov      A,00011101b
               mov      PAC,A
               mov      A,00000001b
               mov      PAWU,A
               mov      A,00000001b
               mov      PAPU,A
               ;;;;     ;;+--------- PB7: No Pin out    [x]
               ;;       ;;|+-------- PB6: Pn_Tx         [0] AN3
               ;;;;     ;;||+------- PB5: pn_IR_TX(1)   [x] AN2
               ;;;;     ;;|||+------ PB4: No Pin out    [x] AM1
               ;;       ;;||||+----- PB3: SIM (/SCS)    [O] AN0
               ;;;;     ;;|||||+---- PB2: SIM (SCK)     [O]
               ;;;;     ;;||||||+--- PB1: SIM (SDI)     [O]
               ;;;;     ;;|||||||+-- PB0: SIM (SDO)     [I]
               ;;       ;;||||||||
               mov      A,00001101b
               mov      PB,A
               mov      A,00100001b
               mov      PBC,A
               mov      A,00100000b
               mov      PBPU,A
               ;;;;     ;;+---------  PC7: No Pin out   [x]
               ;;;;     ;;|+--------- PC6:              [x]
               ;;;;     ;;||+-------- PC5: No Pin out   [x]
               ;;;;     ;;|||+------- PC4: LED          [O]
               ;;       ;;||||+----- PC3: pn_DC_Bias    [x]
               ;;       ;;|||||+---- PC2: pn_IR_TX(2)   [x]
               ;;       ;;||||||+--- PC1: AN5           [x] AN5
               ;;       ;;|||||||+-- PC0: AN4           [x] AN4
               ;;       ;;||||||||
;;               mov      A,00000011b
;;               mov      PC,A
               mov      A,00000000b
               mov      PCC,A
               mov      A,00010011b
               mov      PC,A
               mov      A,00000000b
               mov      PCPU,A
               _DIS_HT82V739
               ret

DEFAULT_RAM:
               clr      BP
  CLEAR_RAM:
               mov      A,040h          ;;
               mov      MP0,A           ;;
               mov      A,192d          ;;
  CLEAR_RAM_1:                          ;;
               clr      IAR0            ;;
               inc      MP0             ;;
               SDZ      ACC             ;;
               JMP      CLEAR_RAM_1     ;;
               nop
  ;@----- Default Flag -----
  ;@----- Default RAM value -----
  DEF_RAM:
               mov      A,c_II0_PFD0
               mov      a_II0_PFD0,A
               mov      A,c_II0_PFD1
               mov      a_II0_PFD1,A
               mov      A,c_II0_PFD2
               mov      a_II0_PFD2,A
               clr      a_SCAN_X
               clr      fg_Low_Baterry
               clr      fg_Have_Smoke
               clr      fg_set_smoke_value
               snz      pn_Key
               set      fg_Test_mode
               NOP
               mov      a,c_DebounceKey_Long   ;; 150
               mov      a_DebounceKey_Long,a
               mov      a,c_DebounceKey        ;; 20
               mov      a_DebounceKey,a
               mov      A,10d
               mov      a_LED_cycle,A          ;; 4*10=40sec
               ret


INIT_WDTC:
               SZ       fg_Test_mode
               JMP      INIT_WDTC_1Sec
        ;;--- Set WDT Control register WDTC ---
        ;;;; Bit7         +----------- FSYSON : Fsys control in IDLE Mode
        ;;                |           >  0 : Disable
        ;;                |              1 : Enable
        ;;;; Bit6,5,6     |+++-------- WS2,1,0 ( Defines the Operating Mode )
        ;;                ||||           0 0 0 : 2^13 / Fs
        ;;                ||||           0 0 1 : 2^14 / Fs
        ;;                ||||           0 1 0 : 2^15 / Fs     Fs=32K  1sec
        ;;                ||||           0 1 1 : 2^16 / Fs             2sec
        ;;                ||||        >  1 0 0 : 2^17 / Fs             4sec
        ;;                ||||           1 0 1 : 2^18 / Fs
        ;;                ||||           1 1 0 : 2^19 / Fs
        ;;                ||||           1 1 1 : 2^20 / Fs
        ;;;; Bit3,2,1,0   ||||++++---- TWDTEN3,2,1,0 : WDT Software Control
        ;;                ||||||||       1010 : Disable
        ;;                ||||||||    >  Other: Enable
               mov      A,11000101B
               mov      WDTC,A            ;; WDTC / WCTL
               RET

INIT_WDTC_1Sec:
        ;;--- Set WDT Control register WDTC ---
        ;;;; Bit7         +----------- FSYSON : Fsys control in IDLE Mode
        ;;                |           >  0 : Disable
        ;;                |              1 : Enable
        ;;;; Bit6,5,6     |+++-------- WS2,1,0 ( Defines the Operating Mode )
        ;;                ||||           0 0 0 : 2^13 / Fs
        ;;                ||||           0 0 1 : 2^14 / Fs
        ;;                ||||        >  0 1 0 : 2^15 / Fs  Fs=32K  1sec
        ;;                ||||           0 1 1 : 2^16 / Fs          2sec
        ;;                ||||           1 0 0 : 2^17 / Fs          4sec
        ;;                ||||           1 0 1 : 2^18 / Fs
        ;;                ||||           1 1 0 : 2^19 / Fs
        ;;                ||||           1 1 1 : 2^20 / Fs
        ;;;; Bit3,2,1,0   ||||++++---- TWDTEN3,2,1,0 : WDT Software Control
        ;;                ||||||||       1010 : Disable
        ;;                ||||||||    >  Other: Enable
               mov      A,10100101B
               mov      WDTC,A            ;; WDTC / WCTL
               RET

INIT_TIMER:
        ;;--- Set Timer0 Control register TMR0C ---
        ;;;; Bit7,6       ++---------- T0M1,0 ( Defines the Operating Mode )
        ;;                ||            0 0 : Unused
        ;;                ||            0 1 : Event count mode ( External Clock )
        ;;                ||          > 1 0 : Timer Mode ( Internal Clock )
        ;;                ||            1 1 : Pulse Width measurement mode ( External Pulse )
        ;;;; Bit5         ||+--------- Unused
        ;;;; Bit4         |||+-------- T0ON ( Enable/Disable Timer 0 )
        ;;                ||||          0 : Disable
        ;;                ||||        > 1 : Enable
        ;;;; Bit3         ||||+------- T0E ( Define Timer0 active edge of the timer/event counter )
        ;;                |||||         0 : rising edge
        ;;                |||||       > 1 : falling edge
        ;;;; Bit2,1,0     |||||+++---- T0PSC2,1,0 ( Define PreScaler Stages )
        ;;                ||||||||      0 0 0 : Fint=Fsys       0 0 1 : Fint=Fsys/2
        ;;                ||||||||      0 1 0 : Fint=Fsys/4     0 1 1 : Fint=Fsys/8
        ;;                ||||||||    > 1 0 0 : Fint=Fsys/16    1 0 1 : Fint=Fsys/32
        ;;                ||||||||      1 1 0 : Fint=Fsys/64    1 1 1 : Fint=Fsys/128
               mov      A,10010100B
               mov      TMR0C,A         ;;;; Fsys =  4MHz /16= 250KHz = 4.000uS

        ;;--- Set Timer0 Value register ---
               mov      A,131d  ;; 4.000uS * 125  (256-131) = 500.00uS ( 500uS )
               mov      TMR0,A  ;;                     ^^ Timer0 Value

        ;; Timer1 Used to
        ;;--- Set Timer1 Control register TMR1C ---
        ;;;; Bit7,6       ++---------- T1M1,0 ( Defines the Operating Mode )
        ;;                ||            0 0 : Unused
        ;;                ||            0 1 : Event count mode ( External Clock )
        ;;                ||          > 1 0 : Timer Mode ( Internal Clock )
        ;;                ||            1 1 : Pulse Width measurement mode ( External Pulse )
        ;;;; Bit5         ||+--------- T1S ( Define Internal Clock Source )
        ;;                |||         > 0 : Fsys/4
        ;;                |||           1 : Fsub, 32K RC OSC
        ;;;; Bit4         |||+-------- T1ON ( Enable/Disable Timer 0 )
        ;;                ||||          0 : Disable
        ;;                ||||        > 1 : Enable
        ;;;; Bit3         ||||+------- T1E ( Define Timer0 active edge of the timer/event counter )
        ;;                |||||         0 : rising edge
        ;;                |||||       > 1 : falling edge
        ;;;; Bit2,1,0     |||||+++---- Unused
        ;;                ||||||||
;              mov      A,10010000B
;              mov      TMR1C,A

;timer1
;========================================================================
               mov      a,low (65536-90)
               mov      TMR1L,a
               mov      a,high (65536-90)
               mov      TMR1H,a
               mov      a,10000000B
               mov      TMR1C,a

               RET

INIT_DAC:
;========================================================================
;DAC.......
;========================================================================
               mov      a,0E1h
               mov      DACTRL,a
               CLR      DACTRL.0                ;DAC Off......
               RET
INIT_ADC:
;========================================================================
;ADC.......
;========================================================================
        ;;--- Set ACSR(xxh) register ---
        ;;;; Bit7         +----------- Unused
        ;;;; Bit6         |+---------- ADONB : ADC module power on/off control bit
        ;;                ||          > 0 : ADC power on
        ;;                ||            1 : ADC power off
        ;;;; Bit5         ||+--------- VRSEL : Select ADC reference voltage
        ;;                |||           0 : Internal ADC power (Vdd)
        ;;                |||           1 : VREF pin
        ;;;; Bit4,3       |||++------- Unused
        ;;;; Bit2,1,0     |||||+++---- ADCS 2,1,0 : Select ADC clock source
        ;;                ||||||||          0 0 0 : Fsys / 2
        ;;                ||||||||          0 0 1 : Fsys / 8
        ;;                ||||||||          0 1 0 : Fsys / 32
        ;;                ||||||||          0 1 1 : Undefined
        ;;                ||||||||          1 0 0 : Fsys
        ;;                ||||||||    >>    1 0 1 : Fsys / 4
        ;;                ||||||||          1 1 0 : Fsys / 16
        ;;                ||||||||          1 1 1 : Undefined
               mov      A,00000101b
               mov      ACSR,A

        ;;--- Set ADPCR(xxh) register ---
        ;;;; Bit7,6       ++---------- Unused
        ;;                ||
        ;;;; Bit5         ||+--------- PCR5 : Define PC1 is A/D input or mot
        ;;                |||           0 : Not A/D input (PC1)
        ;;                |||           1 : A/D input, AN5
        ;;;; Bit4         |||+-------- PCR4 : Define PC0 is A/D input or mot
        ;;                ||||          0 : Not A/D input (PC0)
        ;;                ||||          1 : A/D input, AN4
        ;;;; Bit3         ||||+------- PCR3 : Define PB6 is A/D input or mot
        ;;                |||||         0 : Not A/D input (PB6)
        ;;                |||||         1 : A/D input, AN3
        ;;;; Bit2         |||||+------ PCR2 : Define PB5 is A/D input or mot
        ;;                ||||||        0 : Not A/D input (PB5)
        ;;                ||||||        1 : A/D input, AN2
        ;;;; Bit1         ||||||+----- PCR1 : Define PB4 is A/D input or mot
        ;;                |||||||       0 : Not A/D input (PB4)
        ;;                |||||||       1 : A/D input, AN1
        ;;;; Bit0         |||||||+---- PCR0 : Define PB3 is A/D input or mot
        ;;                ||||||||      0 : Not A/D input (PB3)
        ;;                ||||||||      1 : A/D input, AN0
        ;;                ||||||||
               mov      A,00000000b
               mov      ADPCR,A
;----------------------------------------------------------------
;       Label(ADCR)    Bits    Function
;----------------------------------------------------------------
        ;;--- Set ADCR0(xxh) register ---
        ;;;; Bit7         +----------- START Start the A/D conversion
        ;;                |           > 0 -> 1 -> 0 : Start ADC
        ;;                |             0 -> 1      : Reset ADC and set EOCB to 1
        ;;;; Bit6         |+---------- EOCB End of A/D Conversion bit
        ;;                ||          > 0 : End of ADC
        ;;                ||            1 : not End of ADC
        ;;;; Bit5,4,3     ||+++------- Unused
        ;;                |||||
        ;;;; Bit2,1,0     |||||+++---- ACS 2,1,0 Select ADC clock source
        ;;                ||||||||      000 : AN0
        ;;                ||||||||      001 : AN1
        ;;                ||||||||      010 : AN2
        ;;                ||||||||      011 : AN3
        ;;                ||||||||      100 : AN4
        ;;                ||||||||      101 : AN5
        ;;                ||||||||      110 : A1E OPA 1 output
        ;;                ||||||||    > 111 : A2E OPA 2 output
               mov      A,00010111b
               mov      ADCR,A
               RET
INIT_INTERRUPT:
;========================================================================
;Interrupt.......
;========================================================================
        ;;--- Set INTC0 Register ---
               ;;         +------------- For Test Only Must be writen as 0
               ;;;;       |+------------ T0F,   Timer0-Int request Flag (1=Act)
               ;;;;       ||+----------- EIF1,  Ext-Int 1 request Flag  (1=Act)
               ;;;;       |||+---------- EIF0,  Ext-Int 0 request Flag  (1=Act)
               ;;;;       ||||+--------- ET0I,  Timer 0-Int Control      (1=En)
               ;;;;       |||||+-------- EEI0,  Ext-Int 1 Control       (1=En)
               ;;;;       ||||||+------- EEI0,  Ext-Int 0 Control       (1=En)
               ;;;;       |||||||+------ EMI,   Control Int             (1=En)
               ;;         ||||||||
               mov      A,00001001B
               mov      INTC0,A
        ;;--- Set INTC1 Register ---
               ;;;;       +------------- Unused, read as 0
               ;;;;       |+------------ MFF,   Multi-function request Flag (1=Act)
               ;;;;       ||+----------- SIMF,  SPI/I2C request Flag        (1=Act)
               ;;;;       |||+---------- T1F,   Timer0-Int request Flag (1=Act)
               ;;;;       ||||+--------- Unused, read as 0
               ;;;;       |||||+-------- EMFI,  Control the Multi-function interrupt (1=En)
               ;;;;       ||||||+------- ESIM,  Control SPI/I2C Interrupt (1=En)
               ;;;;       |||||||+------ ET1I,  Timer 1-Int Control      (1=En))
               ;;         ||||||||
               mov      A,00000000B
               mov      INTC1,A
        ;;--- Set MFIC0 Register ---
               ;;;;       +------------- PEF,  External peripheral interrupt request flag
               ;;;;       |+------------ TB1F, Time Base 1 interrupt request Flag
               ;;;;       ||+----------- TB0F, Time Base 0 interrupt request Flag
               ;;;;       |||+---------- ADF,  A/D converter interrupt request Flag
               ;;;;       ||||+--------- EPI,  External peripheral interrupt Enable
               ;;;;       |||||+-------- TB1E, Time Base 1 interrupt Enable
               ;;;;       ||||||+------- TB0E, Time Base 0 interrupt Enable
               ;;;;       |||||||+------ EADI, A/D converter interrupt Enable
               ;;         ||||||||
               mov      A,00000000B
               mov      MFIC0,A
        ;;--- Set MFIC1 Register ---
               ;;;;       +------------- Unused, read as 0
               ;;;;       |+------------ LVDF,  LVD interrupt request Flag
               ;;;;       ||+----------- E2F,   EEPROM interrupt request Flag
               ;;;;       |||+---------- CF,    Comparator interrupt request Flag
               ;;;;       ||||+--------- Unused, read as 0
               ;;;;       |||||+-------- ELVDI, LVD interrupt Enable
               ;;;;       ||||||+------- EE2I,  EEPROM interrupt Enable
               ;;;;       |||||||+------ ECI,   Comparator interrupt Enable
               ;;         ||||||||
               mov      A,00000000B
               mov      MFIC1,A
        ;;--- Set INTEDGE Register ---
               ;;;;       ++++---------- Unused, read as 0
               ;;;;       ||||++-------- INT1S1,INT1S0 : INT1 Edge select
               ;;;;       ||||||              0 0 : disable
               ;;;;       ||||||              0 1 : rising edge tigger
               ;;;;       ||||||              1 0 : falling edge tigger
               ;;;;       ||||||              1 1 : dual  edge tigger
               ;;;;       ||||||
               ;;;;       ||||||++------ INT0S1,INT0S0 : INT0 Edge select
               ;;;;       ||||||              0 0 : disable
               ;;;;       ||||||              0 1 : rising edge tigger
               ;;;;       ||||||              1 0 : falling edge tigger
               ;;;;       ||||||              1 1 : dual  edge tigger
               ;;         ||||||||
               mov      A,00000000B
               mov      INTEDGE,A
               RET

;========================================================================
INIT_OPA:
;================================================================================
; LDOC   for  OPA   Reference........
;------------------------------------------------------------------------------
               MOV      A,00000000B
               MOV      LDOC,A
        ;;--- Set OPA1C1 (xx) register ---
        ;;;; Bit 7        +------------- A1O2CIN --> OPA1 output to comparator control bit
        ;;                |
        ;;;; Bit 6        |+------------ A1O2N   --> OPA1 output to OPA1 inverting input control bit
        ;;;; Bit 5,4      ||++---------- A1PSEL1,0 --> OPA1 Non-inverting input selection bit
        ;;                ||||              >  0 0 : no connection
        ;;                ||||                 0 1 : 0.9 VDD
        ;;                ||||                 1 0 : VM ( 0.5 VDD or 0.5 VLDO )
        ;;                ||||                 1 1 : 0.1 VDD
        ;;;; Bit 3        ||||+--------- A1PS --> A1P to OPA1 + input control bit
        ;;                |||||
        ;;;; Bit 2        |||||+-------- A1NS --> A1N to OPA1 - input control bit
        ;;                ||||||
        ;;;; Bit 1        ||||||+------- A1OEN--> OPA1 output Enable
        ;;                |||||||
        ;;;; Bit 0        |||||||+------ A1EN --> OPA1 Enable
        ;;                ||||||||
               mov      A,00001101B
               mov      OPA1C1,A

 ;;--- Set OPA2C0 (xxh) Register ---
        ;;;; Bit 7        +------------- A2OP  --> Operational amplifier output (read only)
        ;;;; Bit 6        |+------------ A2OFM --> OPA1 Mode Select.
        ;;                ||            >  0 : OPA mode
        ;;                ||               1 : Input offset voltage cancellation mode
        ;;;; Bit 5        ||+----------- A2RS  --> Input offset voltage cancellation reference selection.
        ;;                |||              0 : Select A1N
        ;;                |||           >  1 : Select A1P
        ;;;; Bit 4 ~ 0    |||+++++------ A2OF4,3,4,1,0 --> Input offset voltage cancellation control bits.
        ;;                ||||||||
               mov      A,00010000B
               mov      OPA2C0,A

 ;;--- Set OPA2C1 (xx) register ---
        ;;;; Bit 7        +------------- A2O2CIN --> OPA1 output to comparator control bit
        ;;                |
        ;;;; Bit 6        |+------------ A2O2N   --> OPA1 output to OPA1 inverting input control bit
        ;;;; Bit 5,4      ||++---------- A2PSEL1,0 --> OPA1 Non-inverting input selection bit
        ;;                ||||              >  0 0 : no connection
        ;;                ||||                 0 1 : 0.9 VDD
        ;;                ||||                 1 0 : VM ( 0.5 VDD or 0.5 VLDO )
        ;;                ||||                 1 1 : 0.1 VDD
        ;;;; Bit 3        ||||+--------- A2PS --> A1P to OPA1 + input control bit
        ;;                |||||
        ;;;; Bit 2        |||||+-------- A2NS --> A1N to OPA1 - input control bit
        ;;                ||||||
        ;;;; Bit 1        ||||||+------- A2OEN--> OPA1 output Enable
        ;;                |||||||
        ;;;; Bit 0        |||||||+------ A2EN --> OPA1 Enable
        ;;                ||||||||
               mov      A,00001111B
               mov      OPA2C1,A

 ;;--- Set OPA2C2 (xx) register ---
        ;;;; Bit 7        +------------- A1O2A2N ---> OPA1 Out to OPA2 (-) inverting input control bit
        ;;;; Bit 6        |+------------ A1O2A2P ---> OPA1 Out to OPA2 (+) non-inverting input control bit
        ;;;; Bit 5,4      ||++---------- Unused
        ;;;; Bit 3        ||||+--------- PGAEN   ---> OPA2 PGA gain enable
        ;;;; Bit 2 ~ 0    |||||+++------ PGA2,1,0 --> OPA2 PGA gain control bits.
        ;;                ||||||||          0 0 0 : 1
        ;;                ||||||||          0 0 1 : 8
        ;;                ||||||||          0 1 0 : 16
        ;;                ||||||||          0 1 1 : 24
        ;;                ||||||||          1 0 0 : 32
        ;;                ||||||||          1 0 1 : 40
        ;;                ||||||||          1 1 0 : 48
        ;;                ||||||||      >   1 1 1 : 56
        ;;                ||||||||
               mov      A,00000111B     ;;
               mov      OPA2C2,A
               RET
          
;==========================================================================================
I_TIMER0:
               mov      a_II0_ACC,A     ;; PUSH ACC
               mov      A,STATUS
               mov      a_II0_STATUS,A  ;; PUSH STATUS
         ;     clr      WDT
         ;@===== [1] 1mS   Process ===== ( 1kHz )
               SDZ      a_II0_PFD0
               JMP      I_TIMER0_END    ;;1mS
               mov      A,c_II0_PFD0    ;;
               mov      a_II0_PFD0,A

        LED_HI:
               snz      fg_LED          ;; fg_LED=1 clr pn_LED
               JMP      LED_LOW
               sdz      a_LED_HI
               JMP      key_check
               clr      fg_LED
               set      pn_LED
               set      fg_LED_Status   ;;   0: LED On  1: LED Off
               JMP      key_check
        LED_LOW:
               snz      fg_LED_Status
               JMP      key_check
               sdz      a_LED_LOW       ;; fg_LED=0 set pn_LED
               JMP      key_check
               clr      fg_LED_Status

        key_check:


               sz       pn_Key
               JMP      Rst_Chk_Key     ;;Checking no press key or
                                        ;;release key.
                                        ;;Debounce Key Start.
               set     fg_KEY
               SZ      fg_key_busy
               JMP     Rst_Chk_Key_END


               sdz      a_DebounceKey
               JMP      Rst_Chk_Key_END

               sz       fg_WaitReleaseKey
               JMP      Chk_LongKey
               set      fg_WaitReleaseKey

        Chk_LongKey:
               sdz      a_DebounceKey_Long
               JMP      Rst_Chk_Key_1
               set      fg_WithLongKeyIn      ;; Long_key
               clr      fg_WaitReleaseKey

;;               set      fg_WithKeyIn          ;; short_key

               set      fg_key_busy

               mov      a,c_DebounceKey_Long
               mov      a_DebounceKey_Long,a
               JMP      Rst_Chk_Key_1

        Rst_Chk_Key:
               SZ       fg_key_busy
               clr      fg_key_busy

               SZ       fg_WaitReleaseKey
               set      fg_WithKeyIn           ;; short_key
               clr      fg_WaitReleaseKey
               clr      fg_key_busy

               mov      a,c_DebounceKey_Long   ;; 150
               mov      a_DebounceKey_Long,a
        Rst_Chk_Key_1:
               mov      a,c_DebounceKey        ;; 20
               mov      a_DebounceKey,a
        Rst_Chk_Key_END:

               SNZ      fg_10Sec_En
               JMP      I_TIMER0_END
         ;@===== [2] 200mS   Process ===== ( 5Hz )
               SDZ      a_II0_PFD1
               JMP      I_TIMER0_END
               mov      A,c_II0_PFD1    ;; 1mS*200=200mS
               mov      a_II0_PFD1,A

         ;@===== [3] 1min   Process ===== ( 16.667mHz )
               SDZ      a_II0_PFD2
               JMP      I_TIMER0_END
               set      fg_10Sec        ;; 10Sec
               mov      A,c_II0_PFD2    ;; 200mS*50=10Sec
               mov      a_II0_PFD2,A
I_TIMER0_END:
               mov      A,a_II0_STATUS  ;; POP STATUS
               mov      STATUS,A
               mov      A,a_II0_ACC     ;; POP ACC
               reti

I_TIMER1:
               clr      wdt
               mov      a_II1_ACC,a
               mov      a,status
               mov      a_II1_STATUS,a
               SNZ      F_FirstData_Fg
               JMP      L_SKIP_TO_DAC
;-----------------------------------------------------------------------------
               mov      a,DA1bufL
               mov      DAL,a
               mov      a,DA1bufH
               mov      DAH,a
;-----------------------------------------------------------------------------
L_SKIP_TO_DAC:
               sz       Endsection1      ; END_SECTION
               jmp      L_End_Speech     ; Latch0/2
               sz       LEN1L            ; LEN1L=0?
               jmp      lowlen_1         ;
               sz       LEN1M            ; LEN1H=0?
               jmp      hilen_1          ;
               sz       LEN1H            ; LEN1H=0?
               jmp      hhilen_1         ;
               jmp      ChkCntlCode1     ; VROM byte??control code
hhilen_1:        
               DEC      LEN1H
hilen_1:
               DEC      LEN1M            ; Decrement LEN1H
lowlen_1:
               DEC      LEN1L            ; Decrement LEN1L
               mov      a,CONTROLCODE
               xor      a,00h
               SZ       Z
               JMP      L_End_Speech
               JMP      PCM12_0
;----------------------------------------------------------------------------              
ChkCntlCode1:
               CALL     _Get_Byte
               mov      CONTROLCODE,a    ; ?datacode
               mov      a,CONTROLCODE
               xor      a,00h
               SZ       Z
               jmp      ctrlcodeisSilence;CtrlCodeIsSilence
               mov      a,CONTROLCODE
               xor      a,0ffh
               SZ       Z
               JMP      CH1ENDPLAY       ;11111111b(FFH): end of section
               JMP      ctrlcodeisPCM12_0;Fetch_Seg_Lenth

CH1ENDPLAY:
               mov      a,80h
               mov      DA1bufH,a
               clr      DA1bufL
               CLR      F_FirstData_Fg
               set      EndSection1      ; ??EndSection1 = 1
               CLR      F_PlayBusy_Fg
               clr      T1ON
               clr      ET1I
               _SCS_DIS
               jmp      L_End_Speech     ;ENDDECODER_A; ????Latch0/2
;----------------------------------------------------------------------------               
ctrlcodeisSilence:
ctrlcodeisPCM12_0:
               CALL     _Get_Byte
               mov      len1L ,a         ; ? len1L
               CALL     _Get_Byte
               mov      len1M ,a         ; ? len1L
               CLR      LEN1H

               mov      a,0F8H           ;
               andm     a,tickcnt
               jmp      L_End_Speech    ;ENDDECODER_A             ; ??ISR
;----------------------------------------------------------------------------             
PCM12_0:
               inc      tickCnt
               clr      tickCnt.2
               snz      tickCnt.0
               jmp      _pcm12_0_second
;-----------------------------------------------------    
_pcm12_0_first:
               CALL     _Get_Byte
               MOV      DA1bufH,A

               CALL     _Get_Byte
               MOV      DA1bufL_1,A
               MOV      DA1bufL,A
               JMP      ENDDECODER_A
;-----------------------------------------------------        
_pcm12_0_second:        
               CALL     _Get_Byte     ; VROM data
               MOV      DA1bufH,A
               MOV      A,DA1bufL_1
               MOV      latchd,A
               swapa    latchd
               MOV      DA1bufL,A
               JMP      ENDDECODER_A
;-----------------------------------------------------      
ENDDECODER_A:
               SET      F_FirstData_Fg
L_End_Speech:
               mov      a,a_II1_STATUS
               mov      status,a
               mov      a,a_II1_ACC
               RETI

;==========================================================================================
SELE_ADC:
               clr      ADONB           ;; Enable ADC
               nop
               nop
               mov      A,00000111b     ;;
                                        ;; START ADC Converter ADCR.7 (ADC START bit) 0 -> 1 -> 0
               mov      ADCR,A          ;; Select ADC channel to OPA2E & clr ADCR.7
               clr      START           ;; clr ADCR.7
               set      START           ;; set ADCR.7
               clr      START           ;; clr ADCR.7
               nop                      ;; 4 Tad for ADC Sampling Time
               nop                      ;;
               nop                      ;;
               nop                      ;;
               clr      pn_IR_TX        ;; clr IR LED Off
               clr      A2EN            ;;
               clr      A1EN            ;;
               clr      pn_PA1          ;; clr OPA Input DC Bias Source Voltage



    ADC_Wait:
               sz       EOCB
               JMP      ADC_Wait

               set      EMI
               mov      A,ADRH
               mov      a_AN7_BUF_H,A
               mov      a,ADRL
               mov      a_AN7_BUF_L,A

               set      ADONB           ;; Disable ADC

               clr      c
               rrc      a_AN7_BUF_H
               rrc      a_AN7_BUF_L

               clr      c
               rrc      a_AN7_BUF_H
               rrc      a_AN7_BUF_L

               clr      c
               rrc      a_AN7_BUF_H
               rrc      a_AN7_BUF_L

               clr      c
               rrc      a_AN7_BUF_H
               rrc      a_AN7_BUF_L

               sz       fg_set_smoke_value
               JMP      END_ADC

    TEST111:
               mov      A,a_AN7_BUF_L
               mov      a_smoke_value_L,A
               mov      A,a_AN7_BUF_H
               mov      a_smoke_value_H,A
               mov      A,090h
               addm     A,a_smoke_value_L
               mov      A,01h
               adcm     A,a_smoke_value_H
               set      fg_set_smoke_value
       ;;        mov      A,018h                  ;; A - B > 0
       ;;        mov      a_smoke_value_L,A       ;; A : AD Value
       ;;        mov      A,001h                  ;; B : EEPROM Value
       ;;        mov      a_smoke_value_H,A       ;;
        ;;     mov      A,059h
        ;;     mov      a_AN7_BUF_L,A
        ;;     mov      A,000h
        ;;     mov      a_AN7_BUF_H,A
    END_ADC:
               mov      a,a_AN7_BUF_L
               sub      a,a_smoke_value_L      ;;have_smoke=400 AD value*0.8= 320mV(AD 400d= 190h) EEPROM address= 02h~03h(HI LOW)
               mov      decl_buffer,a
               mov      a,a_AN7_BUF_H
               sbc      a,a_smoke_value_H
               snz      C
               ret
               set      fg_Have_Smoke
               ret
;==========================================================================================
;;  2+1+(1+N*3-1+3)*M-1+2
Delay_500uS_ACC:
               mov      a_SR16,A
Loop_500uS_ACC:                                                                 
               mov      a,81d   ;;N
               sdz      acc
               JMP      $-1
               sdz      a_SR16
               JMP      Loop_500uS_ACC
               JMP      $+1
               JMP      $+1
               RET

;---------------------------------------
PLAY_VOICE:
               CLR      ET1I
               mov      tracknumber03,A
               MOV      tracknumber01,A
               CLR      tracknumber02
               CALL     Delay_200ms         ;Delay  200ms..........
               CLR      LEN1L               ; LEN1L=0?
               CLR      LEN1M               ; LEN1H=0?
               CLR      LEN1H               ; LEN1H=0?
;-----------------------------------------------------------------------------
_Fetch_Voice_StartAddress:
;-----------------------------------------------------------------------------
               clr      R_ADDR_H             ; PCM1addrCH = 0
               clr      c
               rlca     TrackNumber01
               mov      R_ADDR_L ,a                     
               rlca     TrackNumber02
               mov      R_ADDR_M ,a
               mov      a,TrackNumber01
               addm     a, R_ADDR_L
               mov      a,TrackNumber02
               adcm     a, R_ADDR_M          ;*3
               mov      a,020h
               addm     a,R_ADDR_L                      ;; 2013/08/06
               
;==========================================================================================
;------------------------------------------------------------------------
;INPUT: Byte_Program
;OUTPUT: 
;------------------------------------------------------------------------
;_HighSpeed_Read_Initial:
;------------------------------------------------------------------------       
               _SCS_EN
               MOV      A,00BH
               CALL     _Send_Byte
               MOV      A,R_ADDR_H
               CALL     _Send_Byte
        
               MOV      A,R_ADDR_M
               CALL     _Send_Byte
        
               MOV      A,R_ADDR_L
               CALL     _Send_Byte
               MOV      A,00               ;Dummy 8 CLK.......
               CALL     _Send_Byte
;-----------------------------------------------------
               CALL     _Get_Byte
               mov      R_ADDR_L,a          ;¨ì R_ADDR_L
        
               CALL     _Get_Byte
               mov      R_ADDR_M,a          ;¨ì R_ADDR_M
        
               CALL     _Get_Byte
               mov      R_ADDR_H,a          ;¨ì R_ADDR_H
;-----------------------------------------------------------------------------
               _SCS_DIS
;=============================================================================
;=============================================================================
               MOV      A,09
               ADDM     A,R_ADDR_L
               CLR      ACC
               ADCM     A,R_ADDR_M
               ADCM     A,R_ADDR_H
;=============================================================================
;=============================================================================
;----------------------------------------------------
;_HighSpeed_Read_Initial:
;----------------------------------------------------       
               _SCS_EN
               MOV      A,00BH
               CALL     _Send_Byte
        
               MOV      A,R_ADDR_H
               CALL     _Send_Byte
        
               MOV      A,R_ADDR_M
               CALL     _Send_Byte
        
               MOV      A,R_ADDR_L
               CALL     _Send_Byte
        
               MOV      A,00               ;Dummy 8 CLK.......
               CALL     _Send_Byte
;-----------------------------------------------------       
;========================================================================
               mov      a,tracknumber03
               xor      a,0
               snz      z
               JMP      L_SampleSet_is_10KHz
               JMP      L_SampleSet_is_11KHz
L_SampleSet_is_11KHz:
;timer1  
;========================================================================
               mov      a,LOW (65536-90)
               mov      TMR1L,a
               mov      a,high (65536-90)
               mov      TMR1H,a
               JMP      L_SampleSet_Com
;========================================================================
L_SampleSet_is_10KHz:
;timer1  
;========================================================================
               mov      a,LOW (65536-90)
               mov      TMR1L,a
               mov      a,high (65536-90)
               mov      TMR1H,a
L_SampleSet_Com: 
               mov      a,10000000B
               mov      TMR1C,a
               SET      T1ON
;========================================================================
               mov      a,00000000B
               mov      INTC1,a
               set      ET1I
;========================================================================
               CLR      EndSection1   ; ³]©wEndSection1 = 1
               SET      F_PlayBusy_Fg
               RET

;==========================================================================================     
INIT_VOICE_ROM:
;-----------------------------------------------------------
L_SPI_INIT:
               clr      SIMD          ;Data Register...
               MOV      A,00000000B
               mov      SIMC0,a
               set      SIMEN         ;enable sim  SIMCTL0.0
               SET      PCKEN
;-----------------------------------------------------------
;SIMCTL2
;-----------------------------------------------------------
               MOV      A,00H         ;;MLS=MSB,CSEN ENABLE
               MOV      SIMA,A
               SET      CKPOLB   ;high Base Level active rising edge
               SET      CKEG
               set      MLS      ;data shift oder from msb to lsb  SIMCTL2.3
               _SCS_EN
;-----------------------------------------------------------
               MOV      A,00H                                        
               MOV      DAL,A                                        
               MOV      DAH,A                                        
               _EN_HT82V739
               SET      DACTRL.0                ;DAC On......
;==========================================================================================
;------------------------------------------------------------------------------
; FUNCTION  : _DAC_RAMP_UP
; INPUT     : NONE
; OUTPUT    : NONE
; REMARK    :
; DESTROY   : ACC, TBLP, INTC, MP0, TMR0,TMR1,DAC,DAH,DAL,datacode
;------------------------------------------------------------------------------         
;------------------------------------------------------------------------------         
_DAC_RAMP_UP:
_VOICEUP:
               CLR      DAL
               CLR      DAh
               CLR      DA1bufL
               CLR      DA1bufH
voiceup1:
               clr      wdt
               clr      wdt2

               mov      a,4;0           ;DELLOOPCNT-1 ;4
               sdz      acc
               jmp      $-1

               sz       DA1bufH.7       ;TO  80H
               jmp      VoiceUpRet

               mov      a,10h
               addm     a, DA1bufL ;dal
               mov      a,0
               adcm     a,DA1bufH  ;dah
               MOV      a, DA1bufL ;dal
               MOV      DAL,A

               MOV      a, DA1bufH ;dal
               MOV      DAH,A
               jmp      VoiceUp1
VoiceUpRet:

               RET

Disable_VOICE:
;------------------------------------------------------------------------------
; FUNCTION  : _DAC_RAMP_DOWN
; INPUT     : NONE
; OUTPUT    : NONE
; REMARK    :
; DESTROY   : ACC, TBLP, INTC, MP0, TMR0,TMR1,DAC,DAH,DAL,datacode
;------------------------------------------------------------------------------                                                                      
_DAC_RAMP_DOWN:
_VOICEDOWN:
               mov      a,DAL    ;-1
               mov      DA1bufL,a; dal
               mov      a,DAH
               mov      DA1bufH,a;dah
;-----------------------------------------
voicedwn1:                               
               clr      wdt
               clr      wdt2
               mov      a,1

               sdz      acc
               jmp      $-1
               nop
               MOV      A,DA1bufL
               SUB      A,10H
               MOV      DA1bufL,A

               CLR      TEMP

               MOV      A,DA1bufH
               SBC      A,TEMP
               MOV      DA1bufH,A

               mov      a,DA1bufL    ;-1
               mov      DAL,a        ; dal
               mov      a,DA1bufH
               mov      DAH,a        ;dah
               MOV      A,DA1bufH    ;dah
               OR       A,DA1bufL
               SNZ      Z
               jmp      voicedwn1
               CALL     Delay_20ms      
               _DIS_HT82V739
               CLR      DACTRL.0                ;DAC Off......
               CLR      SIMEN
               CLR      PCKEN
               RET


;------------------------------------------------------------------------
_Send_Byte:
               CLR      WCOL
               CLR      TRF
               JMP      RW_SPI
;;----------------------
_Get_Byte:
               CLR      WCOL
               CLR      TRF
               JMP      RW_SPI
;;----------------------
RW_SPI:
               MOV      SIMD,A
               SZ       WCOL
               JMP      RW_SPI
CheckTRF:
               CLR      WCOL
               SNZ      TRF
               JMP      CheckTRF
               CLR      TRF
               MOV      A,SIMD
               RET
;------------------------------------------------------------------------
_Byte_Program:
               CALL     _WREN
               _SCS_EN
               MOV      A,002H
               MOV      R_OUT,A
               CALL     _Send_Byte

               MOV      A,R_ADDR_H
               MOV      R_OUT,A
               CALL     _Send_Byte

               MOV      A,R_ADDR_M
               MOV      R_OUT,A
               CALL     _Send_Byte

               MOV      A,R_ADDR_L
               MOV      R_OUT,A
               CALL     _Send_Byte

               MOV      A,R_DATA_BUF
               MOV      R_OUT,A
               CALL     _Send_Byte
               NOP
               NOP
               NOP
               NOP
               _SCS_DIS

               NOP
               NOP
               NOP
               NOP
;-----------------------------------------------------       
L_Wait_Program_End:
               CALL     _RDSR
               AND      A,03H
               XOR      A,00H
               SNZ      Z
               JMP      L_Wait_Program_End
;-----------------------------------------------------            
               RET
;========================================================
L_Goon_AAI_WORD_Write:
               _SCS_EN
;-----------------------------------------------------       
               MOV      A,0ADH
               MOV      R_OUT,A
               CALL     _Send_Byte
;-----------------------------------------------------           
               MOV      A,R_DATA_BUF
               MOV      R_OUT,A
               CALL     _Send_Byte

               MOV      A,R_DATA_BUF
               MOV      R_OUT,A
               CALL     _Send_Byte
;-----------------------------------------------------       
               _SCS_DIS
               MOV      A,R_BlockSize_L
               OR       A,R_BlockSize_M
               OR       A,R_BlockSize_H
               SZ       Z
               JMP      L_AAI_WORD_Write_End

               SDZ      R_BlockSize_L
               JMP      L_Goon_AAI_WORD_Write

               SDZ      R_BlockSize_M
               JMP      L_Goon_AAI_WORD_Write

               DEC      R_BlockSize_H
               JMP      L_Goon_AAI_WORD_Write
;-----------------------------------------------------
L_AAI_WORD_Write_End:
               _SCS_EN
               MOV      A,004H
               MOV      R_OUT,A
               CALL     _Send_Byte
               _SCS_DIS
;-----------------------------------------------------       
L_Wait_AAI_WORDProgram_End:
               CALL     _RDSR
               AND      A,03H
               XOR      A,00H
               SNZ      Z
               JMP      L_Wait_AAI_WORDProgram_End
;-----------------------------------------------------       
               RET
;========================================================
_RDSR:
               _SCS_EN
               MOV      A,05H
               MOV      R_OUT,A
               CALL     _Send_Byte
               CALL     _Get_Byte
               NOP
               NOP
               _SCS_DIS
               RET
_WREN:
_WREN_LP:
               _SCS_EN
               MOV      A,006H
               MOV      R_OUT,A
               CALL     _Send_Byte
               _SCS_DIS

               CALL     _RDSR
               SNZ       ACC.1
               JMP      _WREN_LP
               SZ        ACC.0
               JMP      _WREN_LP
               RET

               END
