//
//*********************************************************
// 4200E to AHL - Handler
//*********************************************************
// load into modul3
#define print_fifo_old_data     // printout the deleted data if buffer is full
//
#include "gdcglob.txt"
//
#define debug_l1
#define debug_input
#define debug_output
//
//*********************************************************
// LOCAL
//*********************************************************
// Counters
#define cnt_ret         2
//
//*********************************************************
// local parameter
//*********************************************************
// counter
// used counter 14
//	counter 15 from os
//
//*********************************************************
//
        setbaud 1,9600,8,n,1
        setp    0x7f02,0x02
	mode_reco
//
// INIT
//
	setl	11,2
	call	init_b0
	call	init_b1
	call	init_b2
	call	init_b3
	call	init_b4
	call	init_b5
        compd   "init ",s14,195
        jz      skip_init
//               4       5
//               234567890123
        movd    "000000201100",s14,242
        call    dbs_tln_init
skip_init:
        movd    "init ",s14,195
        movd    "000 ",input_state
	movd	"000 ",old_state
        movd    "                    ",param_1
        movd    "                    >/n",param_2
//
	setrts	off
	sett	10
	wait	tf_sign
        movd    " 4200eAHL V2.0 (4200ah20.DW3) by J. Hoerhager AOHE/n",wstr_m37,0
	out	wstr_m37,0,c4
//
//*****************************************************
//
error_main:
        setp    0x7f02,0x02
	mode_reco
        movd    " 4200eAHL V2.0 Modul 3 Ready/n",wstr_m37,100
	out	wstr_m37,100,c4
//
// ********************************************************
//
main:
	sett	100
//
main_1:
	setl	11,1
	setrts	on
	wait	ans_sign,tf_sign,im_sign
	case	ans_sign,test_im_sign
//
//*************************************
// message from modul 1 via buffer "input_b_m1"
//
	setl	11,2
	readb	wstr_m3,0,input_b_m1
	jnz	process_ticket		// if buffer is empty wait
        readb   wstr_m3,0,input_b_ahl   // message from AHL
        jnz     process_ahl
        readb   wstr_m3,0,mess_b        // message to send
        jnz     mess_send
        jmp     main_1
//*************************************
// sending mess to channel
//
mess_send:
        wrs     wstr_m3,0,150
        movd    "/l/n",wstr_m3,0
        wrs     wstr_m3,0,10

        jmp     main_send_sig
//*************************************
// process message from AHL
//
process_ahl:
#ifdef debug_input
	out	wstr_m3,0,c4
#endif
        compd   "1",ahl_in_print
        jnz     ahl_in1
        movd    " RdB:    /n",print_str,0
        movs    wstr_m3,0,print_str,5,100
        writeb  print_str,0,mess_b
	sendsig ans_sign,c3
ahl_in1:
	compd	"I",wstr_m3,0
	jz	ahl_interogation
	compd	"A",wstr_m3,0
	jz	ahl_incheck
	compd	"M",wstr_m3,0
	jz	ahl_modifikation
	compd	"D",wstr_m3,0
	jz	ahl_outcheck
	movd	"R     INV  J/n",wstr_m37,0
	movs	wstr_m3,1,wstr_m37,1,5
        compd   "1",ahl_in_print
        jnz     ahl_in2
        movd    "WrB:    /n",print_str,0
        movs    wstr_m37,0,print_str,5,100
        writeb  print_str,0,mess_b
	sendsig ans_sign,c3
ahl_in2:
	writeb	wstr_m37,0,reply_b
	sendsig ans_sign,c2	//signal to AHL
	jmp	main_send_sig
//
process_ticket:
#ifdef debug_input
	out	wstr_m3,0,c4
#endif
        compd   "1",dhs2_out
        jnz     dhs_out1
        movd    "    /n",print_str,0
        movs    wstr_m3,0,print_str,0,160
        compd   "1",protokoll
        jnz     test_p2
        movs    print_str,123,print_str,106,16
        movd    "/n",print_str,122
        jmp     proto_end
test_p2:
proto_end:
        writeb  print_str,0,mess_b
	sendsig ans_sign,c3
dhs_out1:
//
	compd	"/n",wstr_m3,0          // only cr
	jz     main_send_sig
//
	jr	input_state
	jmp	state_0 	//common
	jmp	state_1 	//ticket
	jmp	state_2
	jmp	state_3
	jmp	state_4
	jmp	state_5
	jmp	state_6
	jmp	state_7
	jmp	state_8
	jmp	state_9
	jmp	state_10	// ticket line 1
	jmp	state_11	// ticket line 2
	jmp	state_12	// ticket line 3
	jmp	state_13	// ticket line 4
	jmp	state_14	// ticket line 5
	jmp	state_15	// ticket line 6
	jmp	state_16	// ticket line 7 ... 17 (checkin ticket)
	jmp	state_17	// Ticket line for telefonzelle
	jmp	state_18
	jmp	state_19
	jmp	state_20	// ticket line 7 ... 12 (checkout ticket)

//
state_0:
#ifdef debug_l1
	movd	" State_0_modul_3/n",wstr_m37,200
	out	wstr_m37,200,c4
#endif
	movd	"000 ",input_state
	compd	"-----",wstr_m3,0
	jz	head_line1
//
	compd	"^L/n",wstr_m3,0
	jz	main_send_sig
//
//***********************************************
// Release 3.1
//***********************************************
//
	compd	" ????? A???? -->",wstr_m3,0
	jz	smdr_r31_out
	compd	" ????? A???? |->",wstr_m3,0
	jz	smdr_r31_out
	compd	" ????? A???? CS?",wstr_m3,0
	jz	smdr_r31_out
//
	compd	" ????? A???? <--",wstr_m3,0
	jz	smdr_r31_in
	compd	" ????? A???? <-|",wstr_m3,0
	jz	smdr_r31_in
//
	compd	" ????? A????     ZI",wstr_m3,0
	jz	termin_r31
//
	compd	" ????? A????    ",wstr_m3,0
	jz	room_r31_state
//
//*********************************************************
	jmp	main_send_sig
//
//*********************************************************
//
state_1:
state_2:
state_3:
state_4:
state_5:
state_6:
state_7:
state_8:
state_9:
state_18:
state_19:
	jmp	main_send_sig
//
//*********************************************************
// terminruf R31
//
termin_r31:
	call	r31terminruf
	jmp	main_send_sig
//
//*********************************************************
// Zimmerstatus
room_r31_state:
        compd   "Zusatzkosten",r31_dhs2_extern
	jz	smdr_r31_out
//
// Dataentry
//
	call	r31data_entry
	jmp	main_send_sig
//
//*********************************************************
// headline 1 test which headline
//
head_line1:
	compd	"---/n",wstr_m3,58
        jnz     main_send_sig
//
//*************************************
// first line or last line of a ticket
//
#ifdef debug_l1
	movd	" State_010_modul_3/n",wstr_m37,200
	out	wstr_m37,200,c4
#endif
	movd	"010 ",input_state      // next state line 2
	movd	"                                                                              /n",dhs2_save,0
	movd	"                                                                              /n",dhs2_save,70
main_send_sig:
	sendsig ans_sign,c3
	jmp	main
//*********************************************************
// state 10 ticketoutput
// line customer
//
state_10:
	compd	"  |/n",wstr_m3,58
	jnz	state_0
        movd    "011 ",input_state
	jmp	main_send_sig
//*************************************
// ticket line with date
//
state_11:
	compd	"  |/n",wstr_m3,58
	jnz	state_0
//
//
	movd	"012 ",input_state
	movs	wstr_m3,23,check_hour,2
	movs	wstr_m3,26,check_min,2
	movs	wstr_m3,10,check_day,2
	movs	wstr_m3,15,check_year,4
//
	movd	"01",check_month
	compd	k_januar,wstr_m3,6
	jz	main_send_sig
//
	movd	"02",check_month
	compd	k_februar,wstr_m3,6
	jz	main_send_sig
//
	movd	"03",check_month
	compd	k_maerz,wstr_m3,6
	jz	main_send_sig
//
	movd	"04",check_month
	compd	k_april,wstr_m3,6
	jz	main_send_sig
//
	movd	"05",check_month
	compd	k_mai,wstr_m3,6
	jz	main_send_sig
//
	movd	"06",check_month
	compd	k_juni,wstr_m3,6
	jz	main_send_sig
//
	movd	"07",check_month
	compd	k_juli,wstr_m3,6
	jz	main_send_sig
//
	movd	"08",check_month
	compd	k_august,wstr_m3,6
	jz	main_send_sig
//
	movd	"09",check_month
	compd	k_september,wstr_m3,6
	jz	main_send_sig
//
	movd	"10",check_month
	compd	k_oktober,wstr_m3,6
	jz	main_send_sig
//
	movd	"11",check_month
	compd	k_november,wstr_m3,6
	jz	main_send_sig
//
	movd	"12",check_month
	compd	k_dezember,wstr_m3,6
	jz	main_send_sig
//
	jmp	main_send_sig
//*************************************
// ticket line empty line
//
state_12:
	compd	"  |/n",wstr_m3,58
	jnz	state_0
	movd	"013 ",input_state
	jmp	main_send_sig
//*************************************
// ticket line NAME
//
state_13:
	compd	"  |/n",wstr_m3,58
	jnz	state_0
	compd	"| Name :",wstr_m3,0
	jnz	test_st_17		// test for Telefonzelle
//
	movs	ticket_parameter,check_name,l_name
//
	movd	"014 ",input_state
	jmp	main_send_sig
//
test_st_17:
	compd	"| Telefonzelle",wstr_m3,0
	jnz	state_0
	movd	"017 ",input_state
	jmp	main_send_sig
//
state_17:
	compd	"---/n",wstr_m3,58
	jnz	main_send_sig
	movd	"000 ",input_state
	jmp	main_send_sig
//
//*************************************
// ticket line Zimmernummer
// save extension number
//
state_14:
	compd	"  |/n",wstr_m3,58
	jnz	state_0
	compd	"| Zimmernummer :",wstr_m3,0
	jnz	state_0
	//
	movs	ticket_parameter,check_ext,l_extension
	//
	movd	"015",input_state
	jmp	main_send_sig
//*************************************
// ticket line Sprache if checkin
//	    or Anzahl if checkout
//	    or Vorkasse if checkout
//	    or Leerzeile same state
//
state_15:
	compd	"  |/n",wstr_m3,58
	jnz	state_0
//
	compd	"| Sprache",wstr_m3,0
	jnz	test_checkout
	//
	movs	ticket_parameter,check_language,l_language
	//
	movd	"016",input_state
	jmp	main_send_sig
//
test_checkout:
	compd	"| Vorkasse :",wstr_m3,0
	jnz	test_anzahl
// same state
	movs	ticket_parameter,check_vorkasse,l_vorkasse
	jmp	main_send_sig
//
test_anzahl:
	compd	"| Anzahl der",wstr_m3,0
	jnz	test_empty_line
	//
	movs	ticket_parameter,check_calls,l_calls
	//
	movd	"020 ",input_state
	jmp	main_send_sig
//*************************************
test_empty_line:
	compd	"| ?   ",wstr_m3,0
	jz     sk_e10_state
	compd	"! ?   ",wstr_m3,0
	jnz	state_0
sk_e10_state:
// same state
	jmp	main_send_sig
//
//*************************************
// ticket checkin for extension stored in extension number
// wait for ticket last line
//
state_16:
	compd	"---/n",wstr_m3,58
	jz	state_16skip
//
	compd	"| Passwort",wstr_m3,0
	jnz	st16_n1
	//
	movs	ticket_parameter,check_password,l_password
	//
	jmp	main_send_sig
st16_n1:
	compd	"| Durch",wstr_m3,0
	jnz	st16_n2
	//
	movs	ticket_parameter,check_ddi,l_ddi
	//
	jmp	main_send_sig
st16_n2:
	compd	"| Anzahl",wstr_m3,0
	jnz	st16_n3
	//
	movs	ticket_parameter,check_calls,l_calls
	//
	jmp	main_send_sig
st16_n3:
	compd	"| Ruhe",wstr_m3,0
	jnz	st16_n4
	//
	movs	ticket_parameter,check_dnd,l_dnd
	//
	jmp	main_send_sig
st16_n4:
	compd	"| Nachr",wstr_m3,0
	jnz	st16_n5
	//
	movs	ticket_parameter,check_mess,l_mess
	//
	jmp	main_send_sig
st16_n5:
	compd	"| Vork",wstr_m3,0
	jnz	st16_n6
	//
	movs	ticket_parameter,check_vorkasse,l_vorkasse
	//
	jmp	main_send_sig
st16_n6:
	compd	"| Gespr",wstr_m3,0
	jnz	st16_n7
	//
	movs	ticket_parameter,check_costs,l_costs
	//
	jmp	main_send_sig
st16_n7:
        compd   "| Amts",wstr_m3,0
        jnz     st16_n8
	//
        movs    ticket_parameter,check_amtsge,l_amtsge
	//
	jmp	main_send_sig
st16_n8:
	jmp	main_send_sig
state_16skip:
//
//***********************************************
//***********************************************
// CHECKIN TICKET
//***********************************************
//***********************************************
//
// search extension number in dbs
//
        movs    check_ext,param_1,5
        call    to_left
        movs    param_2,dbs_search_str,5
	call	find_ext
	jnz	else_inotfound
//
// extension found
        compd   "I",dbs_str,_ext_state
	jnz	else_iextstate
//
	call	send_m_mess
	jmp	eif_iextstate
//
else_iextstate:
	call	send_a_mess
        movd    "I",dbs_str,_ext_state
	acall	write_dbs
//
eif_iextstate:
//
	jmp	eif_onotfound
//
else_inotfound:
	call	find_free
	jnz	skip_idef
	movd	"     I /n",dbs_str,0
        movs    check_ext,param_1,5
        call    to_left
        movs    param_2,dbs_str,0,5
	acall	write_dbs
skip_idef:
	call	send_a_mess
//
eif_inotfound:
	movd	"000 ",input_state
	jmp	main_send_sig
//
//*********************************************************
//*********************************************************
// Checkout ticket
//
// open dbs 2 tln data
// save status checkout
// calulate cost_colsed and clear cost_open
// print smdr records
// unlink tln_top and link free_pool_last
// until all records are printed
//
state_20:
// first wait for end of ticket
//
	compd	"---/n",wstr_m3,58
	jz	state_20skip
	compd	"| Gesamt",wstr_m3,0
	jnz	st20_n1
	//
	movs	ticket_parameter,check_ges_cost,l_ges_cost
	jmp	main_send_sig
st20_n1:
	compd	"| Soll",wstr_m3,0
	jnz	st20_n2
	//
	movs	ticket_parameter,check_soll_cost,l_soll_cost
	jmp	main_send_sig
st20_n2:
	compd	"| Gutha",wstr_m3,0
	jnz	st20_n3
	//
	movs	ticket_parameter,check_guth_cost,l_guth_cost
	jmp	main_send_sig
st20_n3:
	jmp	main_send_sig
state_20skip:
//
//***********************************************
//***********************************************
// CHECKOUT TICKET
//***********************************************
//***********************************************
//
// search extension number in dbs
//
        movs    check_ext,param_1,5
        call    to_left
        movs    param_2,dbs_search_str,5
	call	find_ext
	jnz	else_onotfound
//
// extension found
//   test substate
        compd   "T",dbs_str,_ext_flag
	jnz	else_osub_t
//
	call	send_t_mess
        movd    " ",dbs_str,_ext_flag
	acall	write_dbs
	jmp	eif_osub_t
//
else_osub_t:
        compd   "I",dbs_str,_ext_state
	jnz	else_oextstate
//
	call	send_t_mess
	call	send_d_mess
        movd    "O",dbs_str,_ext_state
	acall	write_dbs
	jmp	eif_oextstate
//
else_oextstate:
	call	send_t_mess
	call	send_d_mess
        movd    "O",dbs_str,_ext_state
	acall	write_dbs
//
eif_oextstate:
//
eif_osub_t:
//
	jmp	eif_onotfound
//
else_onotfound:
	call	find_free
	jnz	skip_odef
	movd	"     O /n",dbs_str,0
        movs    check_ext,param_1,5
        call    to_left
        movs    param_2,dbs_str,0,5
	acall	write_dbs
skip_odef:
	call	send_t_mess
	call	send_d_mess
//
eif_onotfound:
	movd	"000 ",input_state
	jmp	main_send_sig
//
//
//*********************************************************
send_t_mess:
        movd    "Tnnnnnccccsssssssssdddddddddbbbbbbbbb/n",wstr_m37,0
        movs    check_ext,param_1,5
        call    to_right
        movs    param_r5,wstr_m37,_tel_account_ext,5
        movs    check_calls,param_1,4
        call    to_right
        movs    param_r4,wstr_m37,_tel_account_ncomm,4
        movs    check_costs,param_1,9
        call    up_to_space
        movs    param_2,wstr_m37,_tel_account_cost,9
        movs    check_vorkasse,param_1,9
        call    up_to_space
        movs    param_2,wstr_m37,_tel_account_deposit,9
        compd   "0",check_soll_cost
        jnz     open_soll
        movs    check_guth_cost,param_1,9
        call    up_to_space
        movs    param_2,wstr_m37,_tel_account_balance,9
        jmp     send_buffer
//
open_soll:
        movs    check_soll_cost,param_1,9
        call    up_to_space
        movd    "-",wstr_m37,_tel_account_balance
        movs    param_2,wstr_m37,_tel_account_balance1,8
//
//***********************************************
//
send_buffer:
        buffst                  //
        jnz     send_1_b
        movd    "Delete FIFO:  /n",print_str,0
        buffrd  print_str,13
#ifdef print_fifo_old_data
        compd   "1",print_fifo_old
        jnz     fifo_old1
        writeb  print_str,0,mess_b
	sendsig ans_sign,c3
fifo_old1:
        out     print_str,0,c4
#endif
        jmp     send_buffer     // test once more if datarecord is to short
send_1_b:
        compd   "1",ahl_out_print
        jnz     ahl_out1
        movd    "WrB:    /n",print_str,0
        movs    wstr_m37,0,print_str,5,100
        writeb  print_str,0,mess_b
	sendsig ans_sign,c3
ahl_out1:
//
        buffwr  wstr_m37,0
//        buffst
        out     wstr_m37,0,c4
        ret
//
//*********************************************************
send_d_mess:
        movd    "Dnnnnn/n",wstr_m37,0
        movs    check_ext,param_1,5
        call    to_right
        movs    param_r5,wstr_m37,_tel_account_ext,5
        jmp     send_buffer
//
//*********************************************************
send_a_mess:
//                     M                     Vgru    do
        movd    "Annnnn nnnnnnnnnnnnnnnnnnnnl    pppp  dddddddddm     d/n",wstr_m37,0
        movs    check_ext,param_1,5
        call    to_right
        movs    param_r5,wstr_m37,_arrive_ext,5
        movs    check_name,wstr_m37,_arrive_name,20
//
        movd    "1",wstr_m37,_arrive_language
        compd   "Deu",check_language
        jz      lang_end
        movd    "2",wstr_m37,_arrive_language
        compd   "Eng",check_language
        jz      lang_end
        movd    "3",wstr_m37,_arrive_language
        compd   "Fr",check_language
        jz      lang_end
        movd    "4",wstr_m37,_arrive_language
        compd   "It",check_language
        jz      lang_end
        movd    " ",wstr_m37,_arrive_language
lang_end:
//
        movs    check_password,wstr_m37,_arrive_password,4
        movs    check_vorkasse,param_1,9
        call    up_to_space
        movs    param_2,wstr_m37,_arrive_deposit,9
//
        movd    "0",wstr_m37,_arrive_dnd
        compd   "aus",check_dnd
        jz      dnd_end
        movd    "1",wstr_m37,_arrive_dnd
        compd   "ein",check_dnd
        jz      dnd_end
        movd    " ",wstr_m37,_arrive_dnd
dnd_end:
//
        movd    "0",wstr_m37,_arrive_mess
        compd   "nein",check_mess
        jz      mess_end
        movd    "1",wstr_m37,_arrive_mess
        compd   "ja",check_mess
        jz      mess_end
        movd    " ",wstr_m37,_arrive_mess
mess_end:
//
        jmp     send_buffer
//
//*********************************************************
send_m_mess:
//                         1111111111222222222233333333334444444444555555
//               01234567890123456789012345678901234567890123456789012345
//               .12345M12345678901234567890lvgggccccppfffffffffmrrrrrd
//                     M                     Vgru    do
        movd    "Mnnnnn nnnnnnnnnnnnnnnnnnnnl    pppp  dddddddddm     d/n",wstr_m37,0
        movs    check_ext,param_1,5
        call    to_right
        movs    param_r5,wstr_m37,_arrive_ext,5
        movs    check_name,wstr_m37,_arrive_name,20
//
        movd    "1",wstr_m37,_arrive_language
        compd   "Deu",check_language
        jz      lang_mend
        movd    "2",wstr_m37,_arrive_language
        compd   "Eng",check_language
        jz      lang_mend
        movd    "3",wstr_m37,_arrive_language
        compd   "Fr",check_language
        jz      lang_mend
        movd    "4",wstr_m37,_arrive_language
        compd   "It",check_language
        jz      lang_mend
        movd    " ",wstr_m37,_arrive_language
lang_mend:
//
        movs    check_password,wstr_m37,_arrive_password,4
        movs    check_vorkasse,param_1,9
        call    up_to_space
        movs    param_2,wstr_m37,_arrive_deposit,9
//
        movd    "0",wstr_m37,_arrive_dnd
        compd   "aus",check_dnd
        jz      dnd_mend
        movd    "1",wstr_m37,_arrive_dnd
        compd   "ein",check_dnd
        jz      dnd_mend
        movd    " ",wstr_m37,_arrive_dnd
dnd_mend:
//
        movd    "0",wstr_m37,_arrive_mess
        compd   "nein",check_mess
        jz      mess_mend
        movd    "1",wstr_m37,_arrive_mess
        compd   "ja",check_mess
        jz      mess_mend
        movd    " ",wstr_m37,_arrive_mess
mess_mend:
//
        jmp     send_buffer
//
//*********************************************************
//*********************************************************
// SMDR RECO
//*********************************************************
//
smdr_r31_out:
	call	r31outgoing_call
	jmp	main_send_sig
//*********************************************************
// SMDR RECO
//*********************************************************
//
smdr_r31_in:
	call	r31incomming_call
	jmp	main_send_sig
//*********************************************************
//*********************************************************
//
//*******************************************************************
// Terminruf
//
r31terminruf:
        compd "PROG",r31_dhs2_extern
        jz    r31wakeup_w
        compd "GEL",r31_dhs2_extern
        jz    r31wakeup_q
        compd "UNB",r31_dhs2_extern
        jz    r31wakeup_n
        compd "BEST",r31_dhs2_extern
        jz    r31wakeup_a
        ret
//
//#########################################################
//
r31ext_replace:
        replace "A ",r31_dhs2_extension,5
        movs    r31_dhs2_extension,param_1,5
        call    to_right
        movs    param_r5,wstr_m37,_wakeup_ext,5
        ret
//
//
r31wakeup_a:
// wakeup answer
//      "P     W  c                     /n"
        movd  wakeup_def,wstr_m37,0
        call  r31ext_replace
        movd  "A",wstr_m37,_wakeup_cause

// **************************
// transfer answertime
//
        movs  r31_dhs2_time_hour,wstr_m37,_wakeup_acti_hour,2
        movs  r31_dhs2_time_min,wstr_m37,_wakeup_acti_minute,2
        movs  r31_dhs2_date_day,wstr_m37,_wakeup_acti_day,2
        movs  r31_dhs2_date_month,wstr_m37,_wakeup_acti_month,2
        movs  r31_dhs2_date_year,wstr_m37,_wakeup_acti_year,2
//
        jmp     send_buffer
//
//#########################################################
// wakeup not answered
//
r31wakeup_n:
//      "P     W  c                     /n"
        movd  wakeup_def,wstr_m37,0
        call  r31ext_replace
        movd  "N",wstr_m37,_wakeup_cause
        compd   "F",r31_dhs2_wakeup_cause       // extension no answer free
        jz      skip_busy_noanswer
        movd    "B",wstr_m37,_wakeup_cause      //extension no answer busy
skip_busy_noanswer:
// **************************
// transfer no answertime
//
        movs  r31_dhs2_time_hour,wstr_m37,_wakeup_acti_hour,2
        movs  r31_dhs2_time_min,wstr_m37,_wakeup_acti_minute,2
        movs  r31_dhs2_date_day,wstr_m37,_wakeup_acti_day,2
        movs  r31_dhs2_date_month,wstr_m37,_wakeup_acti_month,2
        movs  r31_dhs2_date_year,wstr_m37,_wakeup_acti_year,2
//
        jmp     send_buffer
//
//#########################################################
// wakeup programmed
//
r31wakeup_w:
//      "P     W  c                     /n"
        movd  wakeup_def,wstr_m37,0
        call  r31ext_replace
        movd  "P",wstr_m37,_wakeup_cause
// **************************
// transfer programming time
//
        movs  r31_dhs2_time_hour,wstr_m37,_wakeup_acti_hour,2
        movs  r31_dhs2_time_min,wstr_m37,_wakeup_acti_minute,2
        movs  r31_dhs2_date_day,wstr_m37,_wakeup_acti_day,2
        movs  r31_dhs2_date_month,wstr_m37,_wakeup_acti_month,2
        movs  r31_dhs2_date_year,wstr_m37,_wakeup_acti_year,2

//      transfer wakeuptime

        movs  r31_dhs2_durat_hour,wstr_m37,_wakeup_time_hour,2
        movs  r31_dhs2_durat_min,wstr_m37,_wakeup_time_minute,2
//
        jmp     send_buffer
//
//
//#########################################################
// wakeup cancellation
//
r31wakeup_q:
//      "P     W  c                     /n"
        movd  wakeup_def,wstr_m37,0
        call  r31ext_replace
        movd  "C",wstr_m37,_wakeup_cause
// **************************
// transfer cancellation time
//
        movs  r31_dhs2_time_hour,wstr_m37,_wakeup_acti_hour,2
        movs  r31_dhs2_time_min,wstr_m37,_wakeup_acti_minute,2
        movs  r31_dhs2_date_day,wstr_m37,_wakeup_acti_day,2
        movs  r31_dhs2_date_month,wstr_m37,_wakeup_acti_month,2
        movs  r31_dhs2_date_year,wstr_m37,_wakeup_acti_year,2
//
        jmp     send_buffer
//
//*******************************************************************
// Data_entry
//
r31data_entry:
        compd   "0",roomstate_mess
        jz      r31_do_data_entry
        compd   "1",roomstate_mess
        jz      rstate_t1
rstate_t2:
        movd    "C          /n",wstr_m37,0
        call    r31ext_replace
        movs    r31_dhs2_room_state_num,wstr_m37,6,3
        movs    r31_dhs2_room_state,wstr_m37,10,1
        replace "- ",wstr_m37,6,4
//
        jmp     send_buffer
//
rstate_t1:
        movd    "C          /n",wstr_m37,0
        call  r31ext_replace
        movs  r31_dhs2_room_state,wstr_m37,6,4
        replace "- ",wstr_m37,6,4
//
        jmp     send_buffer
//
r31_do_data_entry:
//      "P      4200                    /n"
        movd  datatransfer_def,wstr_m37,0
        call  r31ext_replace
        movs  r31_dhs2_room_state,wstr_m37,_data_data,4
        replace "- ",wstr_m37,_data_data,4
//
        jmp     send_buffer
//
//*******************************************************************
//*******************************************************************
//
// outgoing call
//
r31outgoing_call:
// **********************************************
// translate extensionnumber
//
        movd    smdr_def,wstr_m37,0
        call    r31ext_replace
//
// **********************************************
// transfer extern number
//
        movs  r31_dhs2_extern,wstr_m37,_smdr_extern,20
        replace ". ",wstr_m37,_smdr_extern,20
        replace "- ",wstr_m37,_smdr_extern,20
//
// **********************************************
// transfer costs
//
        compd   "0",sw_unit_cost
        jz      out_cost
//
        movd    "        ",wstr_m37,_smdr_pulse
        movd    "        ",calc_p1
        movs    r31_dhs2_units,calc_p1,4
        movd    "0 ",calc_p2
        acall   add
        movs    calc_erg,wstr_m37,_smdr_pulse,8
        jmp     out_sw_end
out_cost:
        movs    r31_dhs2_cost,wstr_m37,_smdr_cost,8
out_sw_end:
//
// **********************************************
// transfer time
//
        movs  r31_dhs2_time_hour,wstr_m37,_smdr_hour,2
        movs  r31_dhs2_time_min,wstr_m37,_smdr_minute,2
//
// **********************************************
// transfer date
//
        movs  r31_dhs2_date_day,wstr_m37,_smdr_day,2
        movs  r31_dhs2_date_month,wstr_m37,_smdr_month,2
        movs  r31_dhs2_date_year,wstr_m37,_smdr_year,2
//
// **********************************************
// transfer the duration of an outgoing call
//
        movs    r31_dhs2_durat_sec,wstr_m37,_smdr_dur_sec,2
        movd    "        ",calc_p1
        movs    r31_dhs2_durat_hour,calc_p1,2
        movd    "60 ",calc_p2
        acall   mul
        movs    calc_erg,calc_p2,8
        movs    r31_dhs2_durat_min,calc_p1,2
        acall   add
//        acall   cut_lz
        movs    calc_erg_3dg,wstr_m37,_smdr_duration,3
        compd   "00000???",calc_erg
	jz	smdr_dur_end
        movd    "999999",wstr_m37,_smdr_duration
smdr_dur_end:
//
// **********************************************
// Translate trunknumber
//
// Analog trunk:        00-35   > 00-35 .. C
// ISDN T0:             N0-N7   > 40-47 .. C
// ISDN T2:             Pn:     > 50-59 .. C
// ISDN T0:             T0-T7   > 60-67 .. C
//
        movs    r31_dhs2_line_2dg,wstr_m37,_smdr_trunk_2dg,2
        compd   "0",business_flag               //flag_11
        jz      skip_business
        compd   "....",r31_dhs2_project_number  //dhs2_in,106
        jz      skip_business
        movs    r31_dhs2_project_number,wstr_m37,_smdr_trunk,4
        replace ".0",wstr_m37,_smdr_trunk,4
skip_business:
//
// **********************************************
// Transfer the transfer-marker only
// bcause the DHS2 did not support a transfered extension-number
// in any way (from or to).
//
// **********************************************
// AHL-RECO is now complete
// terminate the roco and write it to the buffer
//
        jmp     send_buffer
//
// **********************************************
// Incoming calls
//
r31incomming_call:
// **********************************************
// translate extensionnumber
//
        movd    smdr_def,wstr_m37,0
        call    r31ext_replace
        movd    "4",wstr_m37,_smdr_call_type
//
// **********************************************
// transfer extern number
//
        movs  r31_dhs2_extern,wstr_m37,_smdr_extern,20
        replace ". ",wstr_m37,_smdr_extern,20
        replace "- ",wstr_m37,_smdr_extern,20
//
// **********************************************
// transfer costs
//
        movs    r31_dhs2_cost,wstr_m37,_smdr_cost,8
//
// **********************************************
// transfer time
//
        movs  r31_dhs2_time_hour,wstr_m37,_smdr_hour,2
        movs  r31_dhs2_time_min,wstr_m37,_smdr_minute,2
//
// **********************************************
// transfer date
//
        movs  r31_dhs2_date_day,wstr_m37,_smdr_day,2
        movs  r31_dhs2_date_month,wstr_m37,_smdr_month,2
        movs  r31_dhs2_date_year,wstr_m37,_smdr_year,2
//
// **********************************************
// transfer the duration of an outgoing call
//
        movs    r31_dhs2_durat_sec,wstr_m37,_smdr_dur_sec,2
        movd    "        ",calc_p1
        movs    r31_dhs2_durat_hour,calc_p1,2
        movd    "60 ",calc_p2
        acall   mul
        movs    calc_erg,calc_p2,8
        movs    r31_dhs2_durat_min,calc_p1,2
        acall   add
        acall   cut_lz
        movs    calc_erg_3dg,wstr_m37,_smdr_duration,3
	compd	"     ???",calc_erg
        jz      smdr_indur_end
        movd    "999999",wstr_m37,_smdr_duration
smdr_indur_end:
//
// **********************************************
// Translate trunknumber
//
// Analog trunk:        00-35   > 00-35 .. C
// ISDN T0:             N0-N7   > 40-47 .. C
// ISDN T2:             Pn:     > 50-59 .. C
// ISDN T0:             T0-T7   > 60-67 .. C
//
        movs    r31_dhs2_line_2dg,wstr_m37,_smdr_trunk_2dg,2
//
// **********************************************
// Transfer the transfer-marker only
// bcause the DHS2 did not support a transfered extension-number
// in any way (from or to).
//
// **********************************************
// AHL-RECO is now complete
// terminate the roco and write it to the buffer
//
        jmp     send_buffer
//
// **********************************************
// **********************************************
//
test_im_sign:
	case	im_sign,test_tf_sign
	setl	11,2
//
// command from channel 4
//
	in	wstr_m37,100
	movd	"Okay wait/n",wstr_m37,0
	out	wstr_m37,0,c4
//************************************
//
// init commands
//
        compd   "init default",wstr_m37,100
        jz      go_init_def
        compd   "init ext_dbs",wstr_m37,100
	jz	go_init_tln
	compd	"init buffers",wstr_m37,100
	jz	go_buff_init
//
// display commands
//
	compd	"disp ext_dbs",wstr_m37,100
	jz	go_tln_display
	compd	"disp config",wstr_m37,100
	jz	go_cfg_display
//
	movd	" command not found/n",wstr_m37,100
	out	wstr_m37,100,c4
//
//******************************************************
main_message:
        movd    " 4200eAHL V2.0 Modul 3 Ready/n",wstr_m37,100
	anss	wstr_m37,100
	jmp	main
//
// *****************************************************
test_tf_sign:
	setl	11,2
	jmp	main
//******************************************************
//
//			   11111111112222222222333333
//		 012345678901234567890123456789012345
//
// init the dbs for extensions
// init the dbs for dataelements
// init all the buffers
//
//
go_buff_init:
	call	init_b0
	call	init_b1
	call	init_b2
	call	init_b3
	call	init_b4
	jmp	main_message
//
go_init_tln:
	call	dbs_tln_init
	jmp	main_message
//
go_init_def:
        movd    "noin",s14,195
        jmp     main_message
go_tln_display:
	call	tln_display
	jmp	main_message
//
go_cfg_display:
	call	cfg_display
	jmp	main_message
//
//***********************************************
// z_ret
z_ret:
	setc	cnt_ret,1
	decc	cnt_ret
	ret
//***********************************************
// NZ_ret
nz_ret:
	setc	cnt_ret,0
	decc	cnt_ret
	ret
//***********************************************
//
//************************************************
// Buffer 0 init
// Aufruf:	 call	 Init_b0
//
init_b0:
	setp	0XD300,badr_0_high	//beginn adresse high
	setp	0XD301,0x00		//beginn adresse low
//
	setp	0Xd302,lbuff_0_high	//bufferlaenge high
	setp	0XD303,0x00		//bufferlaenge low
//
	setp	0Xd304,lbuff_0_high	//anzahl bytes free high
	setp	0XD305,0x00		//anzahl bytes free low
//
	setp	0Xd306,badr_0_high	//input zeiger high
	setp	0XD307,0x00		//input zeiger low
//
	setp	0Xd308,lbuff_0_high	//input laenge bis bufferende high
	setp	0XD309,0x00		//input laenge bis bufferende low
//
	setp	0Xd30A,badr_0_high	//output zeiger high
	setp	0XD30B,0x00		//output zeiger low
//
	setp	0Xd30C,lbuff_0_high	//output laenge bis bufferende high
	setp	0XD30D,0x00		//output laenge bis bufferende low
//
	setp	0XD30E,modnum_0 	//modulnummer falls in modulmem
//
	ret
//
//************************************************
// Buffer 1 init
//
// Aufruf:	 call	 Init_b1
//
init_b1:
	setp	0Xd310,badr_1_high	//beginn adresse high
	setp	0Xd311,0x00		//beginn adresse low
//
	setp	0Xd312,lbuff_1_high	//bufferlaenge high
	setp	0Xd313,0x00		//bufferlaenge low
//
	setp	0Xd314,lbuff_1_high	//anzahl bytes free high
	setp	0Xd315,0x00		//anzahl bytes free low
//
	setp	0Xd316,badr_1_high	//input zeiger high
	setp	0Xd317,0x00		//input zeiger low
//
	setp	0Xd318,lbuff_1_high	//input laenge bis bufferende high
	setp	0Xd319,0x00		//input laenge bis bufferende low
//
	setp	0Xd31A,badr_1_high	//output zeiger high
	setp	0Xd31B,0x00		//output zeiger low
//
	setp	0Xd31C,lbuff_1_high	//output laenge bis bufferende high
	setp	0Xd31D,0x00		//output laenge bis bufferende low
//
	setp	0Xd31E,modnum_1 	//modulnummer falls in modulmem
//
	ret
//
//************************************************
// Buffer 2 init
//
// Aufruf:	 call	 Init_b2
//
init_b2:
	setp	0Xd320,badr_2_high	//beginn adresse high
	setp	0Xd321,0x00		//beginn adresse low
//
	setp	0Xd322,lbuff_2_high	//bufferlaenge high
	setp	0Xd323,0x00		//bufferlaenge low
//
	setp	0Xd324,lbuff_2_high	//anzahl bytes free high
	setp	0Xd325,0x00		//anzahl bytes free low
//
	setp	0Xd326,badr_2_high	//input zeiger high
	setp	0Xd327,0x00		//input zeiger low
//
	setp	0Xd328,lbuff_2_high	//input laenge bis bufferende high
	setp	0Xd329,0x00		//input laenge bis bufferende low
//
	setp	0Xd32A,badr_2_high	//output zeiger high
	setp	0Xd32B,0x00		//output zeiger low
//
	setp	0Xd32C,lbuff_2_high	//output laenge bis bufferende high
	setp	0Xd32D,0x00		//output laenge bis bufferende low
//
	setp	0Xd32E,modnum_2 	//modulnummer falls in modulmem
//
	ret
//
//************************************************
// Buffer 3 init
//
// Aufruf:	 call	 Init_b3
//
init_b3:
	setp	0Xd330,badr_3_high	//beginn adresse high
	setp	0Xd331,0x00		//beginn adresse low
//
	setp	0Xd332,lbuff_3_high	//bufferlaenge high
	setp	0Xd333,0x00		//bufferlaenge low
//
	setp	0Xd334,lbuff_3_high	//anzahl bytes free high
	setp	0Xd335,0x00		//anzahl bytes free low
//
	setp	0Xd336,badr_3_high	//input zeiger high
	setp	0Xd337,0x00		//input zeiger low
//
	setp	0Xd338,lbuff_3_high	//input laenge bis bufferende high
	setp	0Xd339,0x00		//input laenge bis bufferende low
//
	setp	0Xd33A,badr_3_high	//output zeiger high
	setp	0Xd33B,0x00		//output zeiger low
//
	setp	0Xd33C,lbuff_3_high	//output laenge bis bufferende high
	setp	0Xd33D,0x00		//output laenge bis bufferende low
//
	setp	0Xd33E,modnum_3 	//modulnummer falls in modulmem
//
	ret
//
//************************************************
// Buffer 4 init
//
// Aufruf:	 call	 Init_b4
//
init_b4:
	setp	0Xd340,badr_4_high	//beginn adresse high
	setp	0Xd341,0x00		//beginn adresse low
//
	setp	0Xd342,lbuff_4_high	//bufferlaenge high
	setp	0Xd343,0x00		//bufferlaenge low
//
	setp	0Xd344,lbuff_4_high	//anzahl bytes free high
	setp	0Xd345,0x00		//anzahl bytes free low
//
	setp	0Xd346,badr_4_high	//input zeiger high
	setp	0Xd347,0x00		//input zeiger low
//
	setp	0Xd348,lbuff_4_high	//input laenge bis bufferende high
	setp	0Xd349,0x00		//input laenge bis bufferende low
//
	setp	0Xd34A,badr_4_high	//output zeiger high
	setp	0Xd34B,0x00		//output zeiger low
//
	setp	0Xd34C,lbuff_4_high	//output laenge bis bufferende high
	setp	0Xd34D,0x00		//output laenge bis bufferende low
//
	setp	0Xd34E,modnum_4 	//modulnummer falls in modulmem
//
	ret
//
//************************************************
// Buffer 5 init
//
// Aufruf:	 call	 Init_b5
//
init_b5:
	setp	0Xd350,badr_5_high	//beginn adresse high
	setp	0Xd351,0x00		//beginn adresse low
//
	setp	0Xd352,lbuff_5_high	//bufferlaenge high
	setp	0Xd353,0x00		//bufferlaenge low
//
	setp	0Xd354,lbuff_5_high	//anzahl bytes free high
	setp	0Xd355,0x00		//anzahl bytes free low
//
	setp	0Xd356,badr_5_high	//input zeiger high
	setp	0Xd357,0x00		//input zeiger low
//
	setp	0Xd358,lbuff_5_high	//input laenge bis bufferende high
	setp	0Xd359,0x00		//input laenge bis bufferende low
//
	setp	0Xd35A,badr_5_high	//output zeiger high
	setp	0Xd35B,0x00		//output zeiger low
//
	setp	0Xd35C,lbuff_5_high	//output laenge bis bufferende high
	setp	0Xd35D,0x00		//output laenge bis bufferende low
//
	setp	0Xd35E,modnum_5 	//modulnummer falls in modulmem
//
	ret
//
/*
//************************************************
// Buffer 6 init
//
// Aufruf:	 call	 Init_b6
//
init_b6:
	setp	0Xd360,badr_6_high	//beginn adresse high
	setp	0Xd361,0x00		//beginn adresse low
//
	setp	0Xd362,lbuff_6_high	//bufferlaenge high
	setp	0Xd363,0x00		//bufferlaenge low
//
	setp	0Xd364,lbuff_6_high	//anzahl bytes free high
	setp	0Xd365,0x00		//anzahl bytes free low
//
	setp	0Xd366,badr_6_high	//input zeiger high
	setp	0Xd367,0x00		//input zeiger low
//
	setp	0Xd368,lbuff_6_high	//input laenge bis bufferende high
	setp	0Xd369,0x00		//input laenge bis bufferende low
//
	setp	0Xd36A,badr_6_high	//output zeiger high
	setp	0Xd36B,0x00		//output zeiger low
//
	setp	0Xd36C,lbuff_6_high	//output laenge bis bufferende high
	setp	0Xd36D,0x00		//output laenge bis bufferende low
//
	setp	0Xd36E,modnum_6 	//modulnummer falls in modulmem
//
	ret
//
*/
//***********************************************************************
// configuration display
//
cfg_display:
	ret
//***********************************************
// display the TLN dbs
//
tln_display:
	movd	"3 ",dbs_para
	acall	pfirst_dbs
	movd	" Extension table /n",wstr_m37,0
	out	wstr_m37,0,c4
	movd	"                 /n",dbs_str,0
//
tln_ldisplay:
	acall	read_dbs
        compd   "     ",dbs_str,_ext_num
	jz	tln_skipdisplay
        compd   "    D",dbs_str,_ext_num
	jz	tln_skipdisplay
        movd    "/n",dbs_str,_ext_cr
	out	dbs_str,0,c4
tln_skipdisplay:
	acall	pnext_dbs
	jz	tln_ldisplay
//
tln_edisplay:
	movd	" END OF TABLE/n",wstr_m37,0
	out    wstr_m37,0,c4
//
	ret
//*********************************************************
// TLN-DBS:
// in modul 3
// begin	0x7000	(28672)
// recos	150
// elements	4
// element0	extension-number l=5		offset 0
// element1	ext_state l=1			offset 5
// element2	sub_state l=1			offset 6
// element3	endofline l=1			offset 7
//						offset 87
dbs_tln_init:
	movd	"3 28672 150 4 5 1 1 1  ",s15,100
	acall	open_dbs
	movd	"       /n",s14,0
	acall	init_dbs
	acall	pfirst_dbs
//
	ret
//***********************************************
// find extension number in dbs 3 element 0
// input: dbs_search_string: extensionnumber to find
//
find_ext:
	movd	"3 0  ",dbs_para
	acall	pfirst_dbs
	acall	find_dbs
	jnz	nz_ret
	acall	read_dbs
	jmp z_ret
//
//***********************************************
// find free record
// input: -
// output: dbs aktueller record is free
//
find_free:
	movd	"3 0  ",dbs_para
	movd	"    ?",dbs_search_str
	acall	pfirst_dbs
	acall	find_dbs
	ret
//
//***********************************************
// to_right	input: param_1 string to justify to right
//		output: param_2 justified string
//			param_1 cleared
//		lenght: 20 char
//     using: counter 10,11,12
//
to_right:
//               01234567890123456789
        movd    "                    >/n",param_2
//      out     param_1,c4
        setc    12,20   //number of char
	setc	10,219	//source
	setc	11,239	//destination
lto_right:
        compd   " ",s14,c10
	jnz	mto_right
	decc	10
	decc	12
	jnz	lto_right
//               01234567890123456789
        movd    "                    ",param_1
//        out     param_1,c4
        ret
//
mto_right:
	movs	s14,c10,s14,c11,1
	decc	10
	decc	11
	decc	12
        jnz     mto_right
//               01234567890123456789
        movd    "                    ",param_1
//        out     param_1,c4
	ret
//***********************************************
// to_left     input: param_1 string to justify to left
//		output: param_2 justified string
//			param_1 cleared
//		lenght: 20 char
//     using: counter 10,11,12
//
to_left:
//               01234567890123456789
        movd    "                    >/n",param_2
	setc	12,20	//number of char
	setc	10,200	//source
	setc	11,220	//destination
lto_left:
        compd   " ",s14,c10
	jnz	mto_left
	incc	10
	decc	12
	jnz	lto_left
//               01234567890123456789
        movd    "                    ",param_1
	ret
//
mto_left:
	movs	s14,c10,s14,c11,1
	incc	10
	incc	11
	decc	12
        jnz     mto_left
//               01234567890123456789
        movd    "                    ",param_1
	ret
//***********************************************
// up_to_space  input: param_1 string to move to parameter 2
//                      until space or 20 chars
//                      leading spaces are not moved
//              output: param_2 transfered string
//			param_1 cleared
//		lenght: 20 char
//     using: counter 10,11,12
//
up_to_space:
//               01234567890123456789
        movd    "                    >/n",param_2
	setc	12,20	//number of char
	setc	10,200	//source
	setc	11,220	//destination
lup_to_space:
        compd   " ",s14,c10
        jnz     mup_to_space
	incc	10
	decc	12
        jnz     lup_to_space
//               01234567890123456789
        movd    "                    ",param_1
	ret
//
mup_to_space:
        movs    s14,c10,s14,c11,1
        incc    10
        incc    11
        decc    12
        jz      eup_to_space
        compd   " ",s14,c10
        jnz     mup_to_space
eup_to_space:
//               01234567890123456789
        movd    "                    ",param_1
	ret
//
//***********************************************
// Messages from PC
//
ahl_interogation:
        movs    wstr_m3,_arrive_ext,param_1,5
        call    to_left
        movs    param_2,dbs_search_str,5
        call    find_ext
        jnz     no_ext_intero
        compd   "I",dbs_str,_ext_state
        jnz     no_in_intero
        movs    param_2,wstr_m3,_arrive_ext,5
        movd    "T",dbs_str,_ext_flag
        acall   write_dbs
        writeb  wstr_m3,0,m0_in         // send to m0
        sendsig ans_sign,c0
        call    c0_wait_answer
        jz      err_reply
//        readb   wstr_m37,0,m0_out
//
send_reply:
        writeb  wstr_m37,0,reply_b
        out     wstr_m37,0,c4
	sendsig ans_sign,c2	//signal to AHL
	jmp	main_send_sig
//
no_in_intero:
        movd    "RnnnnnINV RA/n",wstr_m37,0
        movs    wstr_m3,_arrive_ext,wstr_m37,_arrive_ext,5
        jmp     send_reply
//
no_ext_intero:
        movd    "RnnnnnINV RG/n",wstr_m37,0
        movs    wstr_m3,_arrive_ext,wstr_m37,_arrive_ext,5
        jmp     send_reply
//
err_reply:
        movd    "RerrorINV EE/n",wstr_m37,0
        jmp     send_reply
//
//***********************************************
//
ahl_incheck:
        movs    wstr_m3,_arrive_ext,param_1,5
        call    to_left
        movs    param_2,dbs_search_str,5
        call    find_ext
        jnz     no_ext_arrive
        compd   "I",dbs_str,_ext_state
//        jz      no_in_arrive
//
        movs    param_2,wstr_m3,_arrive_ext,5
        writeb  wstr_m3,0,m0_in         // send to m0
        sendsig ans_sign,c0
        call    c0_wait_answer
        jz      err_reply
//        readb   wstr_m37,0,m0_out
//
        movs    wstr_m3,_arrive_ext,param_1,5
        call    to_left
        movs    param_2,dbs_search_str,5
        call    find_ext
//
        compd   "INV ",wstr_m37,_reply_password
        jnz     in_arrive_save
        compd   "U",wstr_m37,_reply_status2
        jnz     testin_s
        movd    "G",wstr_m37,_reply_status2
        movd    "    D",dbs_str,0
        acall   write_dbs
        jmp     send_reply
//
testin_s:
        compd   "S",wstr_m37,_reply_status2
        jnz     testin_a
        movd    "G",wstr_m37,_reply_status2
        movd    "S",dbs_str,_ext_state
        acall   write_dbs
        jmp     send_reply
//
testin_a:
        movd    "A",wstr_m37,_reply_status2
        movd    "I",dbs_str,_ext_state
        acall   write_dbs
        jmp     send_reply
//
in_arrive_save:
        movd    "I",dbs_str,_ext_state
        acall   write_dbs
        jmp     send_reply
//
no_in_arrive:
        movd    "RnnnnnINV JA/n",wstr_m37,0
        movs    wstr_m3,_arrive_ext,wstr_m37,_arrive_ext,5
        jmp     send_reply
//
no_ext_arrive:
        movs    param_2,wstr_m3,_arrive_ext,5
        writeb  wstr_m3,0,m0_in         // send to m0
        sendsig ans_sign,c0
        call    c0_wait_answer
        jz      err_reply
//        readb   wstr_m37,0,m0_out
//
        movs    wstr_m3,_arrive_ext,param_1,5
        call    to_left
        movs    param_2,dbs_search_str,5
        call    find_ext
//
        compd   "INV ",wstr_m37,_reply_password
        jnz     sin_arrive_save
//
        compd   "U",wstr_m37,_reply_status2
        jnz     stestin_s
        movd    "G",wstr_m37,_reply_status2
        jmp     send_reply
//
stestin_s:
        compd   "S",wstr_m37,_reply_status2
        jnz     stestin_a
        movd    "G",wstr_m37,_reply_status2
//
        call    find_free
        jnz     send_reply
        movs    wstr_m37,_arrive_ext,param_1,5
        call    to_left
        movs    param_2,dbs_str,_ext_num,5
        movd    "S /n",dbs_str,_ext_state
        acall   write_dbs
        jmp     send_reply
//
stestin_a:
        movd    "A",wstr_m37,_reply_status2
//
sin_arrive_save:
        call    find_free
        jnz     send_reply
        movs    wstr_m37,_arrive_ext,param_1,5
        call    to_left
        movs    param_2,dbs_str,_ext_num,5
        movd    "I /n",dbs_str,_ext_state
        acall   write_dbs
        jmp     send_reply
//
//***********************************************
//
ahl_modifikation:
        movs    wstr_m3,_arrive_ext,param_1,5
        call    to_left
        movs    param_2,dbs_search_str,5
        call    find_ext
        jnz     no_ext_modify
        compd   "I",dbs_str,_ext_state
//        jnz     no_in_modify
//
        movs    param_2,wstr_m3,_arrive_ext,5
        writeb  wstr_m3,0,m0_in         // send to m0
        sendsig ans_sign,c0
        call    c0_wait_answer
        jz      err_reply
//        readb   wstr_m37,0,m0_out
//
        movs    wstr_m3,_arrive_ext,param_1,5
        call    to_left
        movs    param_2,dbs_search_str,5
        call    find_ext
//
        compd   "INV ",wstr_m37,_reply_password
        jnz     in_modify_save
        compd   "U",wstr_m37,_reply_status2
        jnz     testmodif_s
        movd    "G",wstr_m37,_reply_status2
        movd    "    D",dbs_str,0
        acall   write_dbs
        jmp     send_reply
//
testmodif_s:
        compd   "S",wstr_m37,_reply_status2
        jnz     testmodif_a
        movd    "G",wstr_m37,_reply_status2
        movd    "S",dbs_str,_ext_state
        acall   write_dbs
        jmp     send_reply
//
testmodif_a:
        movd    "A",wstr_m37,_reply_status2
        movd    "O",dbs_str,_ext_state
        acall   write_dbs
        jmp     send_reply
//
in_modify_save:
        jmp     send_reply
//
no_in_modify:
        compd   "O",dbs_str,_ext_state
        jnz     modify_glob_err
        movd    "RnnnnnINV NA/n",wstr_m37,0
        movs    wstr_m3,_arrive_ext,wstr_m37,_arrive_ext,5
        jmp     send_reply
//
modify_glob_err:
        movd    "RnnnnnINV NG/n",wstr_m37,0
        movs    wstr_m3,_arrive_ext,wstr_m37,_arrive_ext,5
        jmp     send_reply
//
no_ext_modify:
        movs    param_2,wstr_m3,_arrive_ext,5
        writeb  wstr_m3,0,m0_in         // send to m0
        sendsig ans_sign,c0
        call    c0_wait_answer
        jz      err_reply
//        readb   wstr_m37,0,m0_out
//
//        movd    "RnnnnnINV NG/n",wstr_m37,0
//        movs    wstr_m3,_arrive_ext,wstr_m37,_arrive_ext,5
        jmp     send_reply
//
//***********************************************
//
ahl_outcheck:
        movs    wstr_m3,_arrive_ext,param_1,5
        call    to_left
        movs    param_2,dbs_search_str,5
        call    find_ext
        jnz     no_ext_outcheck
//
        movs    param_2,wstr_m3,_arrive_ext,5
        writeb  wstr_m3,0,m0_in         // send to m0
        sendsig ans_sign,c0
        call    c0_wait_answer
        jz      err_reply
//        readb   wstr_m37,0,m0_out
//
        movs    wstr_m3,_arrive_ext,param_1,5
        call    to_left
        movs    param_2,dbs_search_str,5
        call    find_ext
//
        compd   "INV ",wstr_m37,_reply_password
        jnz     outcheck_save
        compd   "U",wstr_m37,_reply_status2
        jnz     testout_s
        movd    "G",wstr_m37,_reply_status2
        movd    "    D",dbs_str,0
        acall   write_dbs
        jmp     send_reply
//
testout_s:
        compd   "S",wstr_m37,_reply_status2
        jnz     testout_a
        movd    "G",wstr_m37,_reply_status2
        movd    "S",dbs_str,_ext_state
        acall   write_dbs
        jmp     send_reply
//
testout_a:
        movd    "A",wstr_m37,_reply_status2
        movd    "O",dbs_str,_ext_state
        acall   write_dbs
        jmp     send_reply
//
outcheck_save:
        movd    "O",dbs_str,_ext_state
        acall   write_dbs
        jmp     send_reply
//
no_ext_outcheck:
        movs    param_2,wstr_m3,_arrive_ext,5
        writeb  wstr_m3,0,m0_in         // send to m0
        sendsig ans_sign,c0
        call    c0_wait_answer
        jz      err_reply
//        readb   wstr_m37,0,m0_out
//
        movs    wstr_m3,_arrive_ext,param_1,5
        call    to_left
        movs    param_2,dbs_search_str,5
        call    find_ext
//
        compd   "INV ",wstr_m37,_reply_password
        jnz     sin_outcheck_save
//
        compd   "U",wstr_m37,_reply_status2
        jnz     stestout_s
        movd    "G",wstr_m37,_reply_status2
        jmp     send_reply
//
stestout_s:
        compd   "S",wstr_m37,_reply_status2
        jnz     stestout_a
        movd    "G",wstr_m37,_reply_status2
//
        call    find_free
        jnz     send_reply
        movs    wstr_m37,_arrive_ext,param_1,5
        call    to_left
        movs    param_2,dbs_str,_ext_num,5
        movd    "S /n",dbs_str,_ext_state
        acall   write_dbs
        jmp     send_reply
//
stestout_a:
        movd    "A",wstr_m37,_reply_status2
//
sin_outcheck_save:
        call    find_free
        jnz     send_reply
        movs    wstr_m37,_arrive_ext,param_1,5
        call    to_left
        movs    param_2,dbs_str,_ext_num,5
        movd    "O /n",dbs_str,_ext_state
        acall   write_dbs
        jmp     send_reply
//
//***********************************************
// wait answer from channel 0
//
c0_wait_answer:
        sett    250
        wait    ans_sign,tf_sign
        case    ans_sign,test_c0tf_sign
        readb   wstr_m37,0,m0_out
        jz      c0_wait_answer
        ret
test_c0tf_sign:
        case    tf_sign,c0_wait_answer
c1_wait_answer:
        sett    250
        wait    ans_sign,tf_sign
        case    ans_sign,test_c1tf_sign
        readb   wstr_m37,0,m0_out
        jz      c1_wait_answer
        ret
test_c1tf_sign:
        case    tf_sign,c1_wait_answer
c2_wait_answer:
        sett    250
        wait    ans_sign,tf_sign
        case    ans_sign,test_c2tf_sign
        readb   wstr_m37,0,m0_out
        jz      c2_wait_answer
        ret
test_c2tf_sign:
        case    tf_sign,c2_wait_answer
        jmp     z_ret
//
zzz:
end
