Subversion Repositories Kolibri OS

Rev

Rev 7548 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2014. All rights reserved.         ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  terminal for KolibriOS                                         ;;
  7. ;;                                                                 ;;
  8. ;;  Written by hidnplayr@kolibrios.org                             ;;
  9. ;;                                                                 ;;
  10. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  11. ;;             Version 2, June 1991                                ;;
  12. ;;                                                                 ;;
  13. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  14.  
  15. format binary as ""
  16.  
  17. use32
  18.         org     0x0
  19.  
  20.         db      'MENUET01'
  21.         dd      0x1
  22.         dd      START
  23.         dd      I_END
  24.         dd      IM_END+0x1000
  25.         dd      IM_END+0x1000
  26.         dd      0, 0
  27.  
  28. include '../../proc32.inc'
  29. include '../../macros.inc'
  30. include '../../dll.inc'
  31. include '../../develop/libraries/box_lib/trunk/box_lib.mac'
  32.  
  33.  
  34. START:
  35.  
  36.         mcall   68, 11
  37.  
  38.         stdcall dll.Load, @IMPORT
  39.         or      eax, eax
  40.         jnz     exit
  41.  
  42.         mcall   40, EVM_MOUSE + EVM_MOUSE_FILTER + EVM_REDRAW + EVM_BUTTON + EVM_KEY
  43.  
  44.         invoke  init_checkbox, ch1
  45.  
  46. red_win:
  47.         call draw_window
  48.  
  49. mainloop:
  50.         mcall   10
  51.  
  52.         dec     eax
  53.         jz      red_win
  54.  
  55.         dec     eax
  56.         jz      key
  57.  
  58.         dec     eax
  59.         jz      button
  60.  
  61.         invoke  edit_box_mouse, edit1
  62.         invoke  edit_box_mouse, edit2
  63.         invoke  edit_box_mouse, edit3
  64.         invoke  edit_box_mouse, edit4
  65.  
  66.         invoke  option_box_mouse, Option_boxs1
  67.         invoke  option_box_mouse, Option_boxs2
  68.  
  69.         invoke  check_box_mouse, ch1
  70.  
  71.         jmp     mainloop
  72.  
  73. button:
  74.         mcall   17
  75.  
  76.         cmp     ah, 0x10        ; connect button
  77.         je      open_connection
  78.  
  79.         test    ah , ah
  80.         jz      mainloop
  81. exit:
  82.         mcall   -1
  83.  
  84. key:
  85.         mcall   2
  86.  
  87.         cmp     ah, 13          ; enter key
  88.         je      open_connection
  89.  
  90.         invoke  edit_box_key, edit1
  91.         invoke  edit_box_key, edit2
  92.         invoke  edit_box_key, edit3
  93.         invoke  edit_box_key, edit4
  94.  
  95.         jmp     mainloop
  96.  
  97.  
  98.  
  99. draw_window:
  100. ; get system colors
  101.         mcall   48, 3, sc, 40
  102.  
  103.         mcall   12,1
  104.         mov     edx, [sc.work]
  105.         or      edx, 0x34000000
  106.         xor     esi, esi
  107.         mov     edi, str_title
  108.         mcall   0, 50 shl 16 + 400, 30 shl 16 + 180
  109.  
  110.         mov     ebx, 5 shl 16 + 15
  111.         mov     ecx, 0x80000000
  112.         or      ecx, [sc.work_text]
  113.         mov     edx, str_port
  114.         mcall   4
  115.         mov     ebx, 5 shl 16 + 35
  116.         mov     edx, str_speed
  117.         mcall
  118.         mov     ebx, 5 shl 16 + 55
  119.         mov     edx, str_data
  120.         mcall
  121.         mov     ebx, 5 shl 16 + 75
  122.         mov     edx, str_stop
  123.         mcall
  124.  
  125.         mov     ebx, 180 shl 16 + 10
  126.         mov     edx, str_parity
  127.         mcall
  128.         mov     ebx, 270 shl 16 + 10
  129.         mov     edx, str_flow
  130.         mcall
  131.  
  132.         invoke  edit_box_draw, edit1
  133.         invoke  edit_box_draw, edit2
  134.         invoke  edit_box_draw, edit3
  135.         invoke  edit_box_draw, edit4
  136.  
  137.         invoke  option_box_draw, Option_boxs1
  138.         invoke  option_box_draw, Option_boxs2
  139.  
  140.         invoke  check_box_draw, ch1
  141.  
  142.         mov     esi, [sc.work_button]
  143.         mcall   8, 270 shl 16 + 100, 105 shl 16 + 16, 0x10
  144.  
  145.         mov     ecx, 0x80000000
  146.         or      ecx, [sc.work_button_text]
  147.         mcall   4, 300 shl 16 + 110, , str_open
  148.  
  149.  
  150.         mov     edx, [sc.work_graph]
  151.         mcall   38, 0 shl 16 + 390, 135 shl 16 + 135
  152.  
  153.         mov     ecx, 0x80000000
  154.         or      ecx, [sc.work_text]
  155.         mcall   4, 5 shl 16 + 142, , [errormsg]
  156.  
  157.         mcall   12, 2
  158.         ret
  159.  
  160.  
  161. open_connection:
  162.  
  163.         mov     [errormsg], err_none    ; clear previous error message
  164.  
  165. ; Read the serial port name, and convert it to a port number
  166.         cmp     byte[ed_port+4], 0
  167.         jne     .port_error
  168.         mov     eax, dword[ed_port]
  169.         or      eax, 0x20202020         ; convert to lowercase
  170.         cmp     eax, 'com1'
  171.         je      .com1
  172.         cmp     eax, 'com2'
  173.         je      .com2
  174.         cmp     eax, 'com3'
  175.         je      .com3
  176.         cmp     eax, 'com4'
  177.         je      .com4
  178.   .port_error:
  179.         mov     [errormsg], err_port
  180.         jmp     red_win
  181.  
  182.   .com1:
  183.         mov     [port], 0x3f8
  184.         jmp     .port_ok
  185.   .com2:
  186.         mov     [port], 0x2f8
  187.         jmp     .port_ok
  188.   .com3:
  189.         mov     [port], 0x3e8
  190.         jmp     .port_ok
  191.   .com4:
  192.         mov     [port], 0x2e8
  193.   .port_ok:
  194.  
  195. ; reserve the com port so we can work with it
  196.         xor     ebx, ebx
  197.         movzx   ecx, [port]
  198.         mov     edx, ecx
  199.         add     edx, 7
  200.         mcall   46
  201.         test    eax, eax
  202.         jz      .port_reserved
  203.         mov     [errormsg], err_reserve
  204.         jmp     red_win
  205.   .port_reserved:
  206.  
  207. ; disable com interrupts
  208. ; (We cannot receive them on the application level :( )
  209.         mov     dx, [port]
  210.         inc     dx
  211.         mov     al, 0
  212.         out     dx, al
  213.  
  214. ; Set speed:
  215. ; Convert the ascii decimal number that user entered
  216. ; So we can do some math with it
  217.         mov     esi, ed_speed
  218.         xor     eax, eax
  219.         xor     ebx, ebx
  220.   .convert_loop:
  221.         lodsb
  222.         test    al, al
  223.         jz      .convert_done
  224.         sub     al, '0'
  225.         jb      .invalid_speed
  226.         cmp     al, 9
  227.         ja      .invalid_speed
  228.         lea     ebx, [ebx + 4*ebx]
  229.         shl     ebx, 1
  230.         add     ebx, eax
  231.         jmp     .convert_loop
  232.   .invalid_speed:
  233.         call    free_port
  234.         mov     [errormsg], err_speed
  235.         jmp     red_win
  236.   .convert_done:
  237.         test    ebx, ebx
  238.         jz      .invalid_speed
  239.  
  240. ; We now have the speed setting in ebx
  241. ; calculate the divisor latch value as 115200/ebx
  242.         xor     edx, edx
  243.         mov     eax, 115200
  244.         div     ebx
  245.         test    edx, edx
  246.         jnz     .invalid_speed
  247.         cmp     eax, 0xffff
  248.         ja      .invalid_speed
  249.         mov     bx, ax
  250.  
  251. ; enable Divisor latch
  252.         mov     dx, [port]
  253.         add     dx, 3
  254.         mov     al, 1 shl 7     ; dlab bit
  255.         out     dx, al
  256.  
  257. ; Set divisor latch value
  258.         mov     dx, [port]
  259.         mov     al, bl
  260.         out     dx, al
  261.         inc     dx
  262.         mov     al, bh
  263.         out     dx, al
  264.  
  265. ; Check the parity type
  266.         xor     bl, bl
  267.         cmp     [option_group1], op1    ; none
  268.         je      .parity_ok
  269.  
  270.         mov     bl, 001b shl 3
  271.         cmp     [option_group1], op2    ; odd
  272.         je      .parity_ok
  273.  
  274.         mov     bl, 011b shl 3
  275.         cmp     [option_group1], op3    ; even
  276.         je      .parity_ok
  277.  
  278.         mov     bl, 101b shl 3
  279.         cmp     [option_group1], op4    ; mark
  280.         je      .parity_ok
  281.  
  282.         mov     bl, 111b shl 3
  283.         cmp     [option_group1], op5    ; space
  284.         je      .parity_ok
  285.         jmp     exit2                   ; something went terribly wrong
  286.   .parity_ok:
  287.  
  288. ; Check number of stop bits
  289.         cmp     [ed_stop], '1'
  290.         je      .stop_ok
  291.         cmp     [ed_stop], '2'
  292.         jne     .invalid_stop
  293.         or      bl, 1 shl 2     ; number of stop bits
  294.         jmp     .stop_ok
  295.   .invalid_stop:
  296.         call    free_port
  297.         mov     [errormsg], err_stopbits
  298.         jmp     red_win
  299.   .stop_ok:
  300.  
  301. ; Check number of data bits
  302.         mov     al, [ed_data]
  303.         cmp     al, '8'
  304.         ja      .invalid_data
  305.         sub     al, '5'
  306.         jae     .data_ok
  307.   .invalid_data:
  308.         call    free_port
  309.         mov     [errormsg], err_databits
  310.         jmp     red_win
  311.   .data_ok:
  312.         or      al, bl
  313. ; Program data bits, stop bits and parity in the UART
  314.         mov     dx, [port]
  315.         add     dx, 3           ; Line Control Register
  316.         out     dx, al
  317.  
  318. ; clear +  enable fifo (64 bytes), 1 byte trigger level
  319.         mov     dx, [port]
  320.         inc     dx
  321.         inc     dx
  322.         mov     al, 0x7 + 1 shl 5
  323.         out     dx, al
  324.  
  325. ; flow control
  326.         mov     dx, [port]
  327.         add     dx, 4
  328.         mov     al, 0xb
  329.         out     dx, al
  330.  
  331. ; Hide our GUI window and open the console
  332.         mcall   40, 0           ; disable all events
  333.         mcall   67, 0, 0, 0, 0  ; hide window
  334.         mcall   12, 1
  335.         mcall   12, 2
  336.  
  337.         invoke  con_start, 1
  338.         invoke  con_init, 80, 25, 80, 25, str_title
  339.  
  340. console_loop:
  341.         mcall   5, 1            ; wait 10 ms
  342.  
  343.         invoke  con_get_flags
  344.         test    eax, 0x200      ; con window closed?
  345.         jnz     exit2
  346.  
  347.   .tx_loop:
  348.         invoke  con_kbhit
  349.         test    eax, eax        ; did user press a key?
  350.         jz      .rx_loop
  351.  
  352.         invoke  con_getch2      ; get the pressed key from buffer
  353.         mov     dx, [port]
  354.         out     dx, al
  355.  
  356.         test    [ch1.flags], ch_flag_en ; does user want us to echo locally?
  357.         je      .tx_loop
  358.  
  359.         and     eax, 0xff
  360.         push    eax
  361.         invoke  con_write_asciiz, esp   ; print the character
  362.         pop     eax
  363.         jmp     .tx_loop
  364.  
  365.   .rx_loop:
  366.         mov     dx, [port]
  367.         add     dx, 5           ; Line status register
  368.         in      al, dx
  369.         test    al, 1           ; Data ready?
  370.         jz      console_loop
  371.  
  372.         mov     dx, [port]      ; Read character from buffer
  373.         in      al, dx
  374.  
  375.         and     eax, 0xff
  376.         push    eax
  377.         invoke  con_write_asciiz, esp   ; print the character
  378.         pop     eax
  379.  
  380.         jmp     .rx_loop
  381.  
  382.  
  383. exit2:
  384.  
  385.         call    free_port
  386.         mcall   -1      ; exit
  387.  
  388. free_port:
  389.  
  390.         xor     ebx, ebx
  391.         inc     ebx
  392.         movzx   ecx, [port]
  393.         mov     edx, ecx
  394.         add     edx, 7
  395.         mcall   46
  396.  
  397.         ret
  398.  
  399. ;-------------------------
  400. ; DATA
  401.  
  402. align 16
  403. @IMPORT:
  404.  
  405. library box_lib, 'box_lib.obj',\
  406.         console, 'console.obj'
  407.  
  408. import  box_lib,\
  409.         edit_box_draw,          'edit_box',\
  410.         edit_box_key,           'edit_box_key',\
  411.         edit_box_mouse,         'edit_box_mouse',\
  412.         init_checkbox,          'init_checkbox2',\
  413.         check_box_draw,         'check_box_draw2',\
  414.         check_box_mouse,        'check_box_mouse2',\
  415.         option_box_draw,        'option_box_draw',\
  416.         option_box_mouse,       'option_box_mouse'
  417.  
  418. import  console,\
  419.         con_start,              'START',\
  420.         con_init,               'con_init',\
  421.         con_exit,               'con_exit',\
  422.         con_getch2,             'con_getch2',\
  423.         con_write_asciiz,       'con_write_asciiz',\
  424.         con_get_flags,          'con_get_flags',\
  425.         con_kbhit,              'con_kbhit'
  426.  
  427. edit1   edit_box 60, 90, 10, 0xffffff, 0x6f9480, 0, 0, 0, 8, ed_port, mouse_dd, ed_focus, 4, 4
  428. edit2   edit_box 60, 90, 30, 0xffffff, 0x6a9480, 0, 0, 0, 7, ed_speed, mouse_dd, ed_figure_only, 4, 4
  429. edit3   edit_box 60, 90, 50, 0xffffff, 0x6a9480, 0, 0, 0, 1, ed_data, mouse_dd, ed_figure_only, 1, 1
  430. edit4   edit_box 60, 90, 70, 0xffffff, 0x6a9480, 0, 0, 0, 1, ed_stop, mouse_dd, ed_figure_only, 1, 1
  431.  
  432. ed_port         db "COM1",0,0,0,0,0,0
  433. ed_speed        db "9600",0,0,0
  434. ed_data         db "8",0
  435. ed_stop         db "1",0
  436.  
  437. option_group1   dd op1
  438. op1     option_box option_group1, 180, 27, 6, 12, 0xffffff, 0, 0, str_none, 4
  439. op2     option_box option_group1, 180, 44, 6, 12, 0xffffff, 0, 0, str_odd, 3
  440. op3     option_box option_group1, 180, 61, 6, 12, 0xffffff, 0, 0, str_even, 4
  441. op4     option_box option_group1, 180, 78, 6, 12, 0xffffff, 0, 0, str_mark, 4
  442. op5     option_box option_group1, 180, 95, 6, 12, 0xffffff, 0, 0, str_space, 5
  443.  
  444. option_group2   dd op6
  445. op6     option_box option_group2, 270, 27, 6, 12, 0xffffff, 0, 0, str_none, 4
  446. ;op7     option_box option_group2, 270, 44, 6, 12, 0xffffff, 0, 0, str_xon, 8, 10b
  447. ;op8     option_box option_group2, 270, 61, 6, 12, 0xffffff, 0, 0, str_rts, 7, 1b
  448. ;op9     option_box option_group2, 270, 78, 6, 12, 0xffffff, 0, 0, str_dsr, 7
  449.  
  450. ch1     check_box2 5 shl 16 + 12, 105 shl 16 + 12, 5, 0xffffff, 0x000000, 0x000000, str_echo, ch_flag_middle
  451.  
  452. Option_boxs1    dd op1, op2, op3, op4, op5, 0
  453. Option_boxs2    dd op6, 0 ;op7, op8, op9, 0
  454.  
  455. str_title       db 'Terminal', 0
  456. str_port        db 'Serial port:', 0
  457. str_speed       db 'Speed (baud):', 0
  458. str_data        db 'Data bits:', 0
  459. str_parity      db 'Parity:', 0
  460. str_flow        db 'Flow control:', 0
  461. str_stop        db 'Stop bits:', 0
  462.  
  463. str_open        db 'Open', 0
  464.  
  465. str_none        db 'None'
  466. str_odd         db 'Odd'
  467. str_even        db 'Even'
  468. str_mark        db 'Mark'
  469. str_space       db 'Space'
  470. ;str_xon         db 'XON/XOFF'
  471. ;str_rts         db 'RTS/CTS'
  472. ;str_dsr         db 'DSR/DTR'
  473.  
  474. str_echo        db 'Local echo', 0
  475.  
  476. errormsg        dd err_none
  477. err_none        db 0
  478. err_port        db 'Invalid serial port.', 0
  479. err_reserve     db 'The port is already in use.', 0
  480. err_speed       db 'Incorrect speed setting.', 0
  481. err_stopbits    db 'Invalid number of stop bits. Must be 1 or 2.', 0
  482. err_databits    db 'Invalid number of data bits. Must be between 5 and 8.', 0
  483.  
  484. I_END:
  485.  
  486. mouse_dd        dd ?
  487. echo            db ?
  488. port            dw ?
  489. sc              system_colors
  490.  
  491. IM_END: