//
//#define debug_convert_2824
#define debug_to_c3
//
//*********************************************************
// Modul 0 using
// s1,100 - s1,255
// s13               - fix reserved for the 2824 handling
// s11,100           - transfer parameter to channel 3
//*********************************************************
//  COMMON AFS DEFINES
//
#define csra    0Xfc81
#define csrb    0Xfc89
#define csrc    0Xfc91
#define csrd    0xfc99
#define baud_300        0X44
#define baud_1200       0X66
#define baud_2400       0X88
#define baud_4800       0X99
#define baud_9600       0Xbb
//
#define dbs_search_str  s15,0
//
#define str_dtmf        s15,50
//
#define dbs_para        s15,100
#define dbs_pointer     s15,102
//
#define fdt_str         s15,150
#define dt_str          s15,180
#define lfdt_str        21
#define dt_str_year     s15,180
#define dt_str_month    s15,182
#define dt_str_day      s15,184
#define dt_str_weekday  s15,186
#define dt_str_hour     s15,188
#define dt_str_minute   s15,190
//
#define calc_erg_flag   s15,204
#define calc_p1         s15,210
#define calc_p2         s15,220
#define calc_erg        s15,230
#define calc_erg6       s15,232
#define calc_erg4       s15,234
#define calc_rest       s15,240
//
#define dbs_str         s14
#define dbs_str_0       s14,0
//
#define number_of_dtmf  11
//
//*******************************************************************
// common part for handling the 2824 in voicemode 2 and 3
//
//*******************************************************************
// this is a subroutine
// call convert_l1
// call convert_l2
// input to it: s13,0 is the string read from the 2824 in V3 mode
//
//  output: the display filled with the actual data if it was display
//          command
//          the parameter part a2824_str,100 .... filled with parameter.
//
//  this routine use a new command "acall is_digit_c10".
//  this funktion test the byte in string13,counter10 if it is a digit
//  and returns z if it is a digit (0...9)
//  else nz.
//
//*********************************************************
// S13 used in this modul ********************* fixed *****
//*********************************************************
#define a2824_input_cr       s13,2       //l= 3
//
//*********************************************************
#define a2824_str               s1
//
#define a2824_p1_cr          a2824_str,100     //l= max 3
#define a2824_p1_cr_sub      a2824_str,105     //l= max 3
#define a2824_p1_num         a2824_str,110     //l= max 20
#define a2824_p1_posnum      110
#define a2824_p1_code        a2824_str,130     //l= max 10
#define a2824_p1_poscode     130
#define a2824_p1_mess        a2824_str,140     //l= max 20
#define a2824_p1_posmess     140
#define a2824_p2_cr          a2824_str,160     //l= max 3
#define a2824_p2_cr_sub      a2824_str,165     //l= max 3
#define a2824_p2_num         a2824_str,170     //l= max 20
#define a2824_p2_posnum      170
#define a2824_p2_code        a2824_str,190     //l= max 10
#define a2824_p2_poscode     190
#define a2824_p2_mess        a2824_str,200     //l= max 20
#define a2824_p2_posmess     200
#define a2824_cr_mark        a2824_str,220     //l= 1
#define a2824_cr_last        a2824_str,221     //l= 1  last written to
//
#define current_min          a2824_str,230     //l= 8
#define a2824_time           a2824_str,240     //format hh:mm
#define a2824_hour           a2824_str,240
#define a2824_min            a2824_str,243
//
//
#define a2824_display_l1     0xd400,0
#define a2824_display_l2     0xd400,42   //l= max 41
//
#define a2824_disp_l1_time      0xd400,26
#define a2824_disp_l1_colon     0xd400,28
//
//*********************************************************
//*********************************************************
//*********************************************************
//*********************************************************
//*********************************************************
//*********************************************************
//
#define a2824_a1_cr          s11,100     //l= max 3
#define a2824_a1_cr_sub      s11,105     //l= max 3
#define a2824_a1_num         s11,110     //l= max 20
#define a2824_a1_posnum      110
#define a2824_a1_code        s11,130     //l= max 10
#define a2824_a1_poscode     130
#define a2824_a1_mess        s11,140     //l= max 20
#define a2824_a1_posmess     140
//
#define test_2824_state      s11,240     //l = 10
//*********************************************************
// START
//
        setbaud 1,9600,8,n,1
        mode_reco
        movd    "         ",test_2824_state
        setl    8,1
        setl    15,2
        movd    "Modul 0 LGNOTR V1.0 by J. Hoerhager AOH start/n",s13,0
        out     s13,0,c4
//
main_start:
//
        call    init_2824
        setc    0,30           //test timeout for sending at*v3
        setc    1,2             //number faults befor sending an alarm
//
main:

//**********************************************************
// Waiting
// input:       timeout ist set
//
wait_2824:
        sett    20                      //timer 2sec
        compd   "   ",a2824_p2_cr       // pending call 2
        jz      wait_rx_2824
        compd   "   ",a2824_a1_cr
        jnz     wait_rx_2824            // call 1 not empty
        movs    a2824_p2_cr,a2824_a1_cr,80
        movd    "   ",a2824_p2_cr       // clear pending call 2

#ifdef  debug_to_c3
        out     a2824_a1_cr,c4
        movd    "sent_to_c3+cntl_sign/n",s13,0
        out     s13,0,c4
#endif
        sendsig        cntl_sign,c3    // request
//
wait_rx_2824:
        wait    tf_sign,rx_sign,im_sign
        case    tf_sign,test_im
        decc    0
        jnz     main
        movd    "AT*G..../n",s13,200    //answer for serurity
        movd    "AT*V3/n",s13,100
//
        out     s13,100,c4
        wrs     s13,100,20
        setc    0,30
        decc    1
        jnz     main
        movd    "         ",test_2824_state     //enable Alarm via channel 3
        setl    15,2            // alarm V24 on
        setc    1,1
        jmp     main
//
//*********************************************************
//
test_im:
        case    im_sign,test_rx
        in      s13,0
        wrs     s13,0,40
//
#ifdef  debug_to_c3
        out     s13,0,c4
#endif
        ansd    "OK/n"
        jmp     wait_rx_2824
//
//*********************************************************
//
test_rx:
        case    rx_sign,wait_rx_2824
        rds     s13,0
        jz      wait_rx_2824
//
        call    convert_2824
//
        compd   "V:",s13,0
        jz      test_cr
        jmp     wait_rx_2824
//
test_cr:
        compd   "PROC",a2824_p1_mess
        jz      wait_rx_2824
        comps   a2824_p1_cr,a2824_a1_cr,3       // cr= pending cr?
        jnz     test_cr_empty
        movs    a2824_p1_cr,a2824_a1_cr,80
        sendsig        cntl_sign,c3
#ifdef  debug_to_c3
        out     a2824_a1_cr,c4
        movd    "sent_to_c3+cntl_sign/n",s13,0
        out     s13,0,c4
#endif
        jmp     wait_rx_2824
//
test_cr_empty:
        compd   "   ",a2824_a1_cr               // cr_aktiv = empty
        jnz     save_cr
        movs    a2824_p1_cr,a2824_a1_cr,80
        sendsig        cntl_sign,c3
#ifdef  debug_to_c3
        out     a2824_a1_cr,c4
        movd    "sent_to_c3+cntl_sign/n",s13,0
        out     s13,0,c4
#endif
        jmp     wait_rx_2824
//
save_cr:
        movs    a2824_p1_cr,a2824_p2_cr,80
        jmp     wait_rx_2824
//
//
//*********************************************************
//**********************************************************
// Testing 2824 - ISDN interface
//
//*********************************************************
//
init_2824:
        movd    "AT*G..../n",s13,200    //answer for serurity
        movd    "ATS0=0E0X9*V3/n",s13,100
//
        out     s13,100,c4
        wrs     s13,100,20
//
// clear callreference
//
        movd    "                                                           /n",a2824_p1_cr
        movs    a2824_p1_cr,a2824_p2_cr,60
        movs    a2824_p1_cr,a2824_a1_cr,60
//
        movd    "F",a2824_p1_cr_sub
        movd    "F",a2824_p2_cr_sub
        ret
//
//*********************************************************
//
convert_2824:
//
//      input: s13,0 string from 2824

#ifdef debug_convert_2824
    out s13,0,c4
#endif
//
        compd   "G:",s13,0
        jnz     test_othermess
        acall   encode16
        wrs     s13,200,20
#ifdef debug_convert_2824
    out s13,200,c4
#endif
        setc    1,2
        movd    "2824conn",test_2824_state
        setl    15,0                    //alarm v24 off
        ret
test_othermess:
//
// commands in mode V1
//
        compd   "V:",s13,0
        jz      conv_voice
        compd   "D:",s13,0
        jz      conv_data
        compd   "R:",s13,0
        jz      conv_aoc
//
// commands in mode V3
//
        compd   "0:",s13,0
        jz      conv_line1
        compd   "1:",s13,0
        jz      conv_line2
        compd   "C:",s13,0
        jz      conv_cursor
        compd   "L:",s13,0
        jz      conv_lcd
        out     s13,0,c4
        ret
//
//*********************************************************
// subroutinen
//
//*********************************************************
// command L: lcd
//     parameter: 0
//
conv_lcd:
        ret
//
//*********************************************************
// command C: cursor position
// parameter:   position , line , on/off
//
conv_cursor:
    ret
//
//*********************************************************
// command 1: text for line 2
// parameter: displaytext
//
conv_line2:
    acall   convert_l2
#ifdef debug_convert_2824
    out a2824_display_l2,c4
#endif
    ret
//
//*********************************************************
// command 0: text for line 1
// parameter: displaytext
//
conv_line1:
    acall   convert_l1
        compd   ":",a2824_disp_l1_colon         // test for time in the display
        jnz     sk1_line1_conv                  // if time - transfer
        movs    a2824_disp_l1_time,a2824_time,5
sk1_line1_conv:
#ifdef debug_convert_2824
    out a2824_display_l1,c4
#endif
    ret
//
//*********************************************************
// command: R: AOC-Parameter
// parameter: CR, currency, commas, text, p1, p2
//
// not used
//
conv_aoc:
    ret
//
//
//*********************************************************
// command: D: data command
// parameter: CR, callingnumber, code, message
//
conv_data:
//
    movd    "                                                           /n",a2824_p1_cr
    movd    "D",a2824_p1_cr_sub      //set it busy
    jmp     conv_common
//*********************************************************
// command: V: voice command
// parameter: CR, callingnumber, code, message
//
conv_voice:
//
    movd    "                                                           /n",a2824_p1_cr
    movd    "V ",a2824_p1_cr_sub      //set it busy
conv_common:
    setc    10,2
    acall   is_digit_c10
    jnz     save_p1_mess
    movs    a2824_input_cr,a2824_p1_cr,3    //transfer CR
//
save_a2824_p1:
    setc    10,6
    acall   is_digit_c10
    jnz     save_p1_mess
//
// save callingnumber in p1
//
    setc    11,a2824_p1_posnum
    call    tr_s13_s14
//
    acall   is_digit_c10
    jnz     save_p1_mess
//
// save code in p1
//
    setc    11,a2824_p1_poscode
    call    tr_s13_s14
//
save_p1_mess:
    movs    s13,c10,a2824_p1_mess,20
#ifdef debug_convert_2824
    out     a2824_p1_cr,c4
#endif
    ret
//
//*********************************************************
// tr_s13_s14
// input:   source s13,cnt10
//          dest   a2824_str,cnt11
//          parameter end = "/"
//
// output:  source s13,cnt10 points behind the end_char
//
tr_s13_s14:
    setc    12,20
//
ltr_s13_s14:
    movs    s13,c10,a2824_str,c11,1
    incc    10
    incc    11
    decc    12
    jz      exit_tr_error
    compd   "//",s13,c10
    jnz     ltr_s13_s14
    incc    10
    ret
//
exit_tr_error:
    decc    12
    ret
//
end
