Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. section .text
  2.  
  3. ;dl=1 read, dl=2 write
  4. ps2wait:
  5.       mov         ecx, 1000
  6. .1:   in          al, 64h
  7.       and         al, dl
  8.       jnz         .2
  9.       dec         ecx
  10.       jnz         .1
  11. .2:   ret
  12. ps2wr:
  13.       mov         dh, al
  14.       mov         dl, 2
  15.       call        ps2wait
  16.       mov         al, 0D4h
  17.       out         64h, al
  18.       call        ps2wait
  19.       mov         al, dh
  20.       out         60h, al
  21.       ;no ret, fall into read code to read ack
  22. ps2rd:
  23.       mov         dl, 1
  24.       call        ps2wait
  25.       in          al, 60h
  26.       ret
  27.  
  28.       ;initialize legacy ps2 user input
  29. global _initPS2
  30. _initPS2:
  31.       xor         eax, eax
  32.       mov         dl, 2
  33.       call         ps2wait
  34.       mov         al, 0A8h
  35.       out         64h, al
  36.       ;get ack
  37.       call         ps2rd
  38.       ;some compaq voodoo magic to enable irq12
  39.       mov         dl, 2
  40.       call         ps2wait
  41.       mov         al, 020h
  42.       out         64h, al
  43.       mov         dl, 1
  44.       call         ps2wait
  45.       in         al, 60h
  46.       bts         ax, 1
  47.       btr         ax, 5
  48.       mov         bl, al
  49.       mov         dl, 2
  50.       call         ps2wait
  51.       mov         al, 060h
  52.       out         64h, al
  53.       call         ps2wait
  54.       mov         al, bl
  55.       out         60h, al
  56.       ;get optional ack
  57.       mov         dl, 1
  58.       call         ps2wait
  59.  
  60.       ;restore to defaults
  61.       mov         al, 0F6h
  62.       call         ps2wr
  63.       ;enable Z axis
  64.       mov         al, 0F3h
  65.       call         ps2wr
  66.       mov         al, 200
  67.       call         ps2wr
  68.       mov         al, 0F3h
  69.       call         ps2wr
  70.       mov         al, 100
  71.       call         ps2wr
  72.       mov         al, 0F3h
  73.       call         ps2wr
  74.       mov         al, 80
  75.       call         ps2wr
  76.       mov         al, 0F2h
  77.       call         ps2wr
  78.       call         ps2rd
  79.       mov         byte [packetsize], 3
  80.       or         al, al
  81.       jz         .noZaxis
  82.       mov         byte [packetsize], 4
  83. .noZaxis:   ;enable 4th and 5th _buttons
  84.       mov         al, 0F3h
  85.       call         ps2wr
  86.       mov         al, 200
  87.       call         ps2wr
  88.       mov         al, 0F3h
  89.       call         ps2wr
  90.       mov         al, 200
  91.       call         ps2wr
  92.       mov         al, 0F3h
  93.       call         ps2wr
  94.       mov         al, 80
  95.       call         ps2wr
  96.       mov         al, 0F2h
  97.       call         ps2wr
  98.       call         ps2rd
  99.  
  100.       ;set sample rate
  101.       mov         al, 0F3h
  102.       call         ps2wr
  103.       mov         al, byte[samplerate] ;200
  104.       call         ps2wr
  105.       ;set resolution
  106.       mov         al, 0E8h
  107.       call         ps2wr
  108.       mov         al, byte [resolution] ;3
  109.       call         ps2wr
  110.       ;set scaling 1:1
  111.       mov         al, 0E6h
  112.       call         ps2wr
  113.       ;enable
  114.       mov         al, 0F4h
  115.       call         ps2wr
  116.  
  117.       ;reset variables
  118.       xor         eax, eax
  119.       mov         dword [cnt], eax
  120.       mov         dword [y], eax
  121.  
  122.       ret
  123.  
  124.  
  125. global _mouse_handler
  126. _mouse_handler:
  127.       xor         ecx, ecx
  128.       mov         cx, 1000
  129.       xor         eax, eax
  130. .waitkey:   in         al, 64h
  131.       dec         cl
  132.       jnz         .1
  133.       ret
  134. .1:
  135.       and         al, 20h
  136.       jz         .waitkey
  137.       in         al, 60h
  138.       mov         cl, al
  139.       in         al, 61h
  140.       out         61h, al
  141.       xor         ebx, ebx
  142.       mov         bl, byte [cnt]
  143.       add         ebx, packet
  144.       mov         byte [ebx], cl
  145.       inc         byte [cnt]
  146.       mov         bl, byte [cnt]
  147.       cmp         bl, byte [packetsize]
  148.       jb         .end
  149.       mov         byte [cnt], 0
  150.  
  151. ;----------------------------------------------------
  152.       ;get buttons state
  153. .2:
  154.       mov         al, byte [packet]
  155.       and         al, 7
  156.       mov         cl, byte [packet+3]
  157.       and         cl, 0F0h
  158.       cmp         cl, 0F0h
  159.       je         .no45btn
  160.       shr         cl, 1
  161.       or         al, cl
  162.  
  163. .no45btn:
  164.  
  165.     mov al, byte [packet]
  166.     cmp al, 0x40
  167.     je .overflow
  168.     cmp al, 0x80
  169.     je .overflow
  170.  
  171. .overflow:
  172.     mov al, 0
  173.     mov [_buttons], al
  174. jmp .next
  175.  
  176.     mov bh, al
  177.     and bh, 01
  178.     cmp bh, al
  179.     jnz .mo1
  180.  
  181.  
  182.     cmp al, 0
  183.     je .mo1
  184.     cmp al, 0000001b
  185.     je .mo2
  186.     cmp al, 0000010b
  187.     je .mo3
  188.     jmp .next
  189.  
  190.     mov al, 4
  191.     jmp .next
  192. .mo3:
  193.     mov al, 3
  194.     jmp .next
  195. .mo2:
  196.     mov al, 2
  197.     jmp .next
  198. .mo1:
  199.     mov al, 12
  200.     jmp .next
  201.  
  202. .next:
  203. ;-----------------------------------------------
  204.       ;get delta X
  205.       movsx      eax, byte [packet+1]
  206.       add         word [x], ax
  207.       ;get delta Y
  208.       movsx      eax, byte [packet+2]
  209.       add         word [y], ax
  210.       ;get delta Z
  211.       mov         cl, byte [packet+3]
  212.       and         cl, 0Fh
  213.       cmp         cl, 8
  214.       jb         .3
  215.       or         cl, 0F0h
  216. .3:   movsx      eax, cl
  217.       add         word [z], ax
  218.  
  219.     mov eax, [x]
  220.     mov [__mouse_x], eax
  221.  
  222.     mov eax, [y]
  223.     neg eax
  224.     mov [__mouse_y], eax
  225.  
  226.  
  227. ;buttons,x,y,z variables contains valid values now.
  228. ;here you possibly want to send a mouse event message to your gui process.
  229. .end:      
  230.     ret
  231.  
  232. section .data
  233. global __mouse_x
  234. global __mouse_y
  235. global  _buttons
  236.  
  237. __mouse_x:      dw 0
  238. __mouse_y:      dw 0
  239.  
  240. packetsize: db 0
  241. resolution: db 3
  242. samplerate: db 100
  243.  
  244. packet: dd 0   ;raw packet
  245. ;keep them together
  246. cnt: db 0      ;byte counter
  247. _buttons: db 0  ;buttons, each bit represents one button from bits 1-5, 0-released, 1-pressed
  248. x: dw 0        ;position on x,y,z axis
  249. y: dw 0
  250. z: dw 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement