Guest User

Untitled

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