; ==============================================
; RTC_Alarm_Checker - for every hour signal
.org 0x001391D6
	LDRB  R0, [R7, #0x9]
	CMP   R0, #0
	BNE   0x001391E6
	ADD   R0, SP, #0x70
	MOV   R1, #0x2B
	STRB  R1, [R0, R1]
	BL    RTC_Gen_Alarm



; ==============================================
; Перехват событий органайзера

; Будильник (до сигнала) - mcc_org_alarm_event_ind
; Было: MCC_INIT_RESTORE
.org 0x009B06E0
.mcc MCC_CALL 0 0 0 0 TALKED_ALARM_BEFORE_SIGNAL

; Будильник (после сигнала) - mcc_org_alarm_event_exit
; Не вызывается во время разговора
; Было: MCC_EXIT
.org 0x009B09F0
.mcc MCC_TRANSF 0 0 0 0 TALKED_ALARM_AFTER_SIGNAL

; Автовключение - mcc_org_alarm_by_autopower
; Было: MCC_INIT_RESTORE
.org 0x009B0A50
.mcc MCC_CALL 0 0 0 0 TALKED_ALARM_AUTOPOWER_ON



; ===============================================
; MCC часть патча
.start 0x009899E4
TALKED_ALARM_BEFORE_SIGNAL:
.mcc MCC_IF 0x12 0x19 1 0 _EXIT_BEFORE_SIGNAL   ; InCall State
.mcc MCC_IF 0x10 0x1D 1 0 _EXIT_IF_KUKUNIK      ; Every hour & MMF Player

.mcc MCC_MELODY 0 0x09
.mcc MCC_TASK 0xA 0x6E
.mcc MCC_TASK 0x10 0x1D 0

_EXIT_BEFORE_SIGNAL:
.mcc MCC_IF 0x10 0x1D 0 0 _EXIT_IF_KUKUNIK      ; Every hour
_EXIT_IF_ALARM:
.mcc MCC_INIT_RESTORE
.mcc MCC_RETURN
_EXIT_IF_KUKUNIK:
.mcc MCC_EXIT


TALKED_ALARM_AFTER_SIGNAL:
.mcc MCC_TASK 0xA 0x6E
.mcc MCC_TASK 0x10 0x1D 1
.mcc MCC_EXIT


TALKED_ALARM_AUTOPOWER_ON:
.mcc MCC_TASK 0xA 0x6E
.mcc MCC_TASK 0x10 0x1D 2
.mcc MCC_TRANSF 0 0 0 0 _EXIT_BEFORE_SIGNAL


; ===============================================
; ExtAPI
.org __my0_4if_Table+0x1D*4
	DCD   TALKED_ALARM_IF+1
.org __my0_5task_Table+0x1D*4
	DCD   TALKED_ALARM+1


; ===============================================
.start 0x006FAD00
TALKED_ALARM_IF:
	PUSH  {LR}
	
	LDR   R0, =DspAlarmOut
	LDRB  R0, [R0, #1]
	CMP   R0, #0x2B
	BNE   _EXIT_FALSE

	LDRH  R0, [R1, #4]
	CMP   R0, #1
	BNE   _EXIT_TRUE

	LDR   R0, =smafplaying2
	LDRB  R0, [R0, #1]
	CMP   R0, #2
	BEQ   _EXIT_TRUE

_EXIT_FALSE:
	MOV   R0, #0
	B     _EXIT_IF
_EXIT_TRUE:
	MOV   R0, #1
_EXIT_IF:
	POP   {PC}
; ===============================================
.align 2
TALKED_ALARM:
	PUSH  {R0-R7,LR}
	SUB   SP, SP, #0x14

	LDRH  R7, [R0, #4]
	LSL   R7, #30
	LSR   R7, #30

_READ_CONFIG:	
	MOV   R2, #2            ; R2 - режим (2 - только чтение)
	ADR   R1, fileNameFrm	; R1 - адрес строки с именем
	MOV   R0, SP            ; R0 - адрес указателя на файл
	BL    tlkOpenFile
	CMP   R0, #0
	BNE   _B_EXIT

	ADD   R3, SP, #4        ; R3 - адрес переменной, реально считано
	MOV   R2, #0xA          ; R2 - кол-во читаемых байт
	ADD   R1, SP, #8        ; R1 - адрес буфера
	LDR   R0, [SP, #0]      ; R0 - указатель на файл
	BL    tlkReadFile
	ADD   R4, R0, #0

	LDR   R0, [SP, #0]      ; R0 - указатель на файл
	BL    tlkCloseFile

	CMP   R4, #0
	BNE   _EXIT


_SAVE_QUIET_MODE:
	LDR   R5, =mmi_quiet_mode_onoff
	LDRB  R6, [R5, #0]

	LDR   R4, =DspAlarmOut
	LDRB  R0, [R4, #1]
	CMP   R0, #0x2B
	BNE   _ALARM


	
	; Каждый час
	ADD   R7, SP, #8

	ADD   R0, SP, #0
	BL    tlkRtcGetTime
	ADD   R0, SP, #0
	LDRB  R0, [R0, #2]      ; текущий час
	ADD   R5, R0, #0

	LDRB  R1, [R7, #5]      ; начало ночного режима
	CMP   R0, R1
	BGE   _SWITCH_OFF_IF_NEED

	LDRB  R1, [R7, #6]      ; окончание ночного режима
	CMP   R0, R1
	BLT   _SWITCH_OFF_IF_NEED

	LDRB  R0, [R7, #3]      ; режим выключен
	CMP   R0, #0
	BEQ   _SWITCH_OFF_IF_NEED

	CMP   R6, #0            ; режим "Без звука"
	BNE   _SWITCH_OFF_IF_NEED

	MOV   R2, #2
	ADR   R1, fileNameHour
	MOV   R0, SP
	BL    tlkOpenFile
	CMP   R0, #0
	BNE   _TALK_TIME

	LDR   R4, =addr_MelBuffer
	LDR   R0, =size_MelBuffer
	BL    tlkCalcBuf

	ADD   R2, R0, #0
	ADD   R3, SP, #4
	LDR   R1, [R4, #0]
	LDR   R0, [SP, #0]
	BL    tlkReadFile
	ADD   R6, R0, #0

	LDR   R0, [SP, #0]
	BL    tlkCloseFile

	CMP   R6, #0
	BNE   _TALK_TIME

	LDRB  R0, [R7, #7]      ; громкость
	CMP   R0, #0
	BEQ   _PLAY_MMF

	CMP   R0, #6
	BLT   _SET_VOLUME

	LDR   R1, =keytone_volume
	LDRB  R0, [R1, #0]

_SET_VOLUME:
	LDR   R1, =mel_vol
	STRB  R0, [R1, #0]

_PLAY_MMF:
	MOV   R3, #0
	LDR   R2, [SP, #4]
	LDR   R1, [R4, #0]
	MOV   R0, #0x54
	BL    tlkPlaySmafMelody

	LDR   R1, =v_mel_yamaha_syn_on
	MOV   R0, #1
	STRB  R0, [R1, #0]

	LDR   R1, =0xFFFF9400
	LDR   R0, =0x00003FFF
	STRH  R0, [R1, #10]

	LDR   R1, = _flag
wa:	LDR   R0, [R1, #0]
	CMP   R0, #0
	BEQ   wa

	B     _SWITCH_OFF_IF_NEED

_TALK_TIME:
	BL    fTalker

_SWITCH_OFF_IF_NEED:
	LDRB  R0, [R7, #8]      ; час автовыключения
	CMP   R0, R5
	BNE   _EXIT

	MOV   R0, #1
	BL    hen3_10SendSPPSwitchOffInd

_B_EXIT:
	B     _EXIT






_ALARM:
	ADD   R1, SP, #8
	LDRB  R0, [R1, R7]
	CMP   R0, #0
	BEQ   _EXIT
        
	LDRB  R0, [R1, #4]
	CMP   R0, #0
	BNE   _SET_QUIET_MODE_OFF

	LDRB  R0, [R4, #0]
	CMP   R0, #0
	BNE   _TALK_IF_QUIET_MODE_OFF

	LDRB  R0, [R4, #1]
	CMP   R0, #0
	BNE   _TALK_IF_QUIET_MODE_OFF

_SET_QUIET_MODE_OFF:
	MOV   R0, #0
	STRB  R0, [R5, #0]

_TALK_IF_QUIET_MODE_OFF:
	LDRB  R0, [R5, #0]
	CMP   R0, #0
	BNE   _EXIT

	BL    fTalker

_RESTORE_QUIET_MODE:
	STRB  R6, [R5, #0]

_EXIT:
	ADD   SP, SP, #0x14
	POP  {R0-R7,PC}
; -----------------------------------------------
.align 2
fileNameFrm:     .string     "/user/alarm.cfg"
.align 2
fileNameHour:    .string     "/user/hour.mmf"
; ===============================================
.equ	size_MelBuffer               , 0x00145754
.equ	addr_MelBuffer               , 0x001458A0
.equ	v_mel_yamaha_syn_on          , 0x0142BCC4
.equ	smafplaying2                 , 0x01430874
.equ	mel_vol                      , 0x01656DEC
.equ    mmi_quiet_mode_onoff         , 0x016DF1C4
.equ	keytone_volume               , 0x016DF208
.equ	_flag                        , 0x0179C004
.equ    DspAlarmOut                  , 0x017A2514
; -----------------------------------------------
.equ	hen0_3IndicateOnkeyPressed   , 0x00125F00
.equ	hen1_01InitOnReq             , 0x00125F68
.equ	hen1_20Init_Start_OffPlugged , 0x00126BDC
.equ	hen1_21Init_Start_OnPlugged  , 0x00126C84
.equ	hen3_10SendSPPSwitchOffInd   , 0x00127B48
.equ    RTC_Gen_Alarm                , 0x0013958C
; -----------------------------------------------
; адреса патча Talker v2
.equ    fTalker                      , 0x006FAA80
.equ    tlkCalcBuf                   , 0x006FABC8
.equ    tlkOpenFile                  , 0x006FAC1C
.equ    tlkReadFile                  , 0x006FAC28
.equ    tlkCloseFile                 , 0x006FAC34
.equ	tlkPlaySmafMelody            , 0x006FAC40
.equ	tlkRtcGetTime                , 0x006FAC10
; -----------------------------------------------
; ExtAPI
.equ	ExtText_Table                , 0x007551F0
.equ	ExtTextHandler_Table         , 0x00755620
.equ	__my0_4if_Table              , 0x00755AA0
.equ	__my0_5task_Table            , 0x00755F10
; ===============================================
