PS/2 mouse driver

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
St8ic

Re:PS/2 mouse driver

Post by St8ic »

Is that only for BL? Should I be saving and restoring BL, CL and DL?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:PS/2 mouse driver

Post by Pype.Clicker »

what do you mean by "saving and restoring" ?

I think you definitely should get something like "Easy Assembler" in a bookstore before you go further ...
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:PS/2 mouse driver

Post by Candy »

Code: Select all

cmp [bl+3],1
...

Code: Select all

test bl,2<<3
teeny weeny error, it's 2^3 or 1 << 3. At least, doing the first bit would become very hard, you'd have to do 2 << -1.

Code: Select all

test bl, 1<<3
St8ic

Re:PS/2 mouse driver

Post by St8ic »

lol....pwn'd by Pype. I'll be sure to stop by a bookstore right after I get this one thing resolved:

All of the following lines give the 'invalid combination of opcode and operands' error in NASM. Only need to solve one of the lines and apply it to the other seventeen!

Code: Select all

          add byte [cl+8],[xmovement]
          add byte [cl+7],[xmovement]
          add byte [cl+6],[xmovement]
          add byte [cl+5],[xmovement]
          add byte [cl+4],[xmovement]
          add byte [cl+3],[xmovement]
          add byte [bl+8],[xmovement]
          add byte [bl+7],[xmovement]
          add [xmovement],[xlocation]
          add byte [dl+8],[ymovement]
          add byte [dl+7],[ymovement]
          add byte [dl+6],[ymovement]
          add byte [dl+5],[ymovement]
          add byte [dl+4],[ymovement]
          add byte [dl+3],[ymovement]
          add byte [bl+6],[ymovement]
          add byte [bl+5],[ymovement]
          add [ymovement],[ylocation]
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:PS/2 mouse driver

Post by Candy »

St8ic wrote: lol....pwn'd by Pype. I'll be sure to stop by a bookstore right after I get this one thing resolved:

All of the following lines give the 'invalid combination of opcode and operands' error in NASM. Only need to solve one of the lines and apply it to the other seventeen!
You cannot access memory twice in an operand. Fix them to something using registers.

PS: what part of xmovement do you want in those lines? And PLEASE - learn about bitfields.

Do you accidentally mix up intel notation with at&t notation?

Code: Select all

          and cl, 0FCh
          mov al, bl
          shr al, 6
          and al, 3
          add cl, al
          and dl, 0FCh
          shr bl, 4
          and bl, 3
          add dl, bl
          mov [xmovement], cl
          mov [ymovement], dl
          add [xlocation], cl
          add [ylocation], dl
Note, this code is based on such a list of assumptions that you CAN NOT trust it to work. Read it over at least 5 times with the packet definition by hand.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:PS/2 mouse driver

Post by Pype.Clicker »

-*- bunk -*-

2<<3 ... oh yeah... i should have been sleeping by that time ::)

@"invalid combination etc.":
- you cannot use 8bits registers as memory base or index. Only 16bits.
- in 16bits addressing, the only registers combination you may use for referencing memory are [bx+si], [bx+di], [bp+si] or [bp+di]. There may be something about sp aswell, but i can't remember if it counts as a "base" or an "index" register ... [cx+offset] is forbidden.
- as Candy said, you may only have one memory reference per instruction. add [mem1],[mem2] is to be replaced by mov reg,[mem2]; add [mem1],reg.
- the whole instructions you issued are nonsense anyway. What the docs show you are *bits* not *bytes* ... Remember ? [b7b6b5b4][a3a2a1a0] == b*16+a = a0 + 2*a1+ 4*a2 + 8*a3 + 16*b4 etc.
DennisCGc

Re:PS/2 mouse driver

Post by DennisCGc »

St8ic, difficult, READING >:(
It would be a lot easier, when using the I/O ports, even for you :o
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:PS/2 mouse driver

Post by Candy »

DennisCGc wrote: St8ic, difficult, READING >:(
It would be a lot easier, when using the I/O ports, even for you :o
First lesson to become a good software engineer: don't force your ideas on others, and be prepared to listen to their ideas, no matter how bad they sound. Offer them help to get them working and convince them to your idea by showing them that even with your honest help, your idea still outperforms their idea.

and one thing of netiquette, don't shout to people that don't listen, ignore them (this is talking from a don't-annoy-people perspective).
DennisCGc

Re:PS/2 mouse driver

Post by DennisCGc »

Candy wrote:
DennisCGc wrote: St8ic, difficult, READING >:(
It would be a lot easier, when using the I/O ports, even for you :o
First lesson to become a good software engineer: don't force your ideas on others, and be prepared to listen to their ideas, no matter how bad they sound. Offer them help to get them working and convince them to your idea by showing them that even with your honest help, your idea still outperforms their idea.

and one thing of netiquette, don't shout to people that don't listen, ignore them (this is talking from a don't-annoy-people perspective).
I didn't mean to shout, anyway ::) , so if I hurt any feelings, sorry ::)
But anyway, you should agree, what's better ?
Using int 0x15, or use the I/O ports.
The int 0x15 makes a lot of unnecassary problems.
Again, I'm sorry.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:PS/2 mouse driver

Post by Candy »

DennisCGc wrote: But anyway, you should agree, what's better ?
You should agree that being "better" is not only how easy it is to program. If he wants to comply to some IBM PS standard, his best bet is to use those functions and not the ports. If he wants to try that method for consideration or because he just plain wants that method, let him. It is and remains his choice. That's one of the basic principles of this forum. This forum is for designing and that means that you look at all options, compare them equally and only after you've seen their capacity, decide one or the other is better.
Using int 0x15, or use the I/O ports.
The int 0x15 makes a lot of unnecassary problems.
Does it? It offers you a plain interface, no real problems with ports, no hardware access that can screw up some hardware device, no syncing, no remote mode or anything like that. Aren't they unnecessary problems?

Yet, end of discussion, it remains his/her choice what s/he wants to do. Now, do you have some advice on how to properly apply the int 015h solution?
DennisCGc

Re:PS/2 mouse driver

Post by DennisCGc »

As suggested, he should switch the whole time to pmode and rmode.
And he should get himself a book, called the BIOS book (or something alike)
Or the indispensable hardware book (again, or something alike)
Or this http://www.ctyme.com/intr/rb-1605.htm
As earlier suggested ;)
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:PS/2 mouse driver

Post by Pype.Clicker »

@denis: st8tic never mentionned he'd like to use pmode ... his target is unreal mode, where BIOS-assisted mouse could be interresting, especially if the BIOS is able to handle PS2, serial mouse, usb mouse and pointing devices such as touchpad and "small red mushroom" transparently ...
DennisCGc

Re:PS/2 mouse driver

Post by DennisCGc »

Ah... I should read it before I answer ;)
But, okay, I was just trying to help..
St8ic

Re:PS/2 mouse driver

Post by St8ic »

Thanks for the suggestion, Dennis, but I think I'll stick to this way :). Candy's code compiles, but hangs the OS.

Maybe because I just move the values of the X and Y movement indicator bits into [xmovement]. I need to actualy retrieve the value (between -128 and 127) from these 8 bits.

Maybe because my plot pixel code (used as a temporary cursor) is buggy:

Code: Select all

mov ah,0Ch
mov al,0x0
mov cx,[xlocation]
mov dx,[ylocation]
int 0x10
Maybe because Candy's code apparently has alot of assumptions that aren't true. What are some of them?
Engin

Re:PS/2 mouse driver

Post by Engin »

Here is an excellent mouse driver (pseudo) code...
------------------------------------------------


%ifndef MOUSE_ASM
%define MOUSE_ASM

; void Mouse_Interrupt_Handler ( void )
; void MOUSE_INIT ( void )
; void InstallMouseListener ( Address )
; int ReadLastMousePacket ( void )
; bool IsMouseEventOccured ( void )

; void MouseListenerAddress ( X_Movement , Y_Movement , bool Left , bool Middle , bool Right )

void proc Mouse_Interrupt_Handler   ; << Interrupt Habdler
   %push Mouse_Interrupt_Handler_Context
   %define IRQ_12_EIP   EBP+4
   %define IRQ_12_CS   EBP+8
   %define IRQ_12_EFLAGS   EBP+12
   %define IRQ_12_ESP   EBP+16   ; May not exist
   %define IRQ_12_SS   EBP+20   ; May not exist
   %assign %$localsize 0
   %stacksize flat
   enter   %$localsize,0
      cli
      pushad
      invoke PIC_IS_SERV , dword 12
      .ifl retval , eq , dword true

         invoke KB_DataWait
         InPortB 0x60
         mov cl , al
         invoke KB_DataWait
         InPortB 0x60
         mov bl , al
         invoke KB_DataWait
         InPortB 0x60
         push eax
         mov byte [ esp + 1 ] , bl
         mov byte [ esp + 2 ] , cl
         mov byte [ esp + 3 ] , 0x00
         pop ebx
         mov dword [ MousePacketData ] , ebx
         mov dword [ IRQ_12_OCCURED ] , true

         invoke PIC_EOI , dword 12
         .ifl [ MouseListenerAddress ] , ne , dword 0x00

            mov al , [ MousePacketData + 2 ]
            push eax
            and al , 00010000b   ; get X sign-bit
            .if al , eq , 00010000b
               mov ebx , 0xFFFFFFFF
            .else
               mov ebx , 0x00000000
            .endif
            pop eax
            push eax
            and al , 00100000b   ; get Y sign-bit
            .if al , eq , 00100000b
               mov ecx , 0xFFFFFFFF
            .else
               mov ecx , 0x00000000
            .endif
            mov bl , [ MousePacketData + 1 ]
            mov cl , [ MousePacketData ]
               ; X Movement = EBX
               ; Y Movement = ECX
            pop eax

            mov edx , eax
            and al , 00000010b   ; right
            .if al , eq , 00000010b
               push dword true
            .else
               push dword false
            .endif
            mov eax , edx
            and al , 00000100b   ; middle
            .if al , eq , 00000100b
               push dword true
            .else
               push dword false
            .endif
            mov eax , edx
            and al , 00000001b   ; left
            .if al , eq , 00000001b
               push dword true
            .else
               push dword false
            .endif

            invoke dword [ MouseListenerAddress ] , ebx , ecx ; ( , LEFT , MIDDLE , RIGHT , already pushed )
         .endif
      .endif
      popad
   leave
   iretd
   align 4 , nop
   MouseListenerAddress   dd 0x00
   MousePacketData      dd 0x00
   IRQ_12_OCCURED      dd false

   %define MOUSE_AUXENABLE      0xA8
   %define MOUSE_AUXDISABLE   0xA7
   %define MOUSE_WRITE      0xD4
   %define MOUSE_DEFAULTS      0xF6
   %define MOUSE_ENABLE      0xF4
   %define MOUSE_RESET      0xFF
   %define MOUSE_RECEIVE_ACK   MOUSE_RECEIVE_DATA

   %macro MOUSE_SEND 1
      invoke KB_CommandWait      ; I'm gonna send a command
      OutPortB 0x64 , MOUSE_WRITE   ; Write to Mouse
      invoke KB_CommandWait      ; I'm gonna send a data
      OutPortB 0x60 , %1
   %endmacro
   %macro MOUSE_RECEIVE_DATA 0
      invoke KB_DataWait
      InPortB 0x60
   %endmacro

   bool proc IsMouseEventOccured
      pushfd
      .if dword [ IRQ_12_OCCURED ] , eq , true
         mov dword [ IRQ_12_OCCURED ] , false
         return true
      .else
         return false
      .endif
      popfd
      ret
   endproc

   proc ReadLastMousePacket
      mov eax , dword [ MousePacketData ]
      ret
   endproc

   void proc InstallMouseListener
      push eax
      mov eax , dword [ esp + 8 ]
      mov dword [ MouseListenerAddress ] , eax
      pop eax
      ret 4
   endproc

   void proc MOUSE_INIT
      pushad
      pushfd
      cli
      %ifdef LANGUAGE_ENGLISH
         Println "Initializing PS/2 mouse..."
      %else
         Println "PS/2 fare baslatiliyor..."
      %endif

      invoke KB_CommandWait
      OutPortB 0x64 , MOUSE_AUXENABLE

      invoke KB_CommandWait
      OutPortB 0x64 , 0x20      ; Read Command Byte
      invoke KB_DataWait
      InPortB 0x60         ; Read it
      mov bl , al         ; save it
      or bl , 00000010b      ; set irq-12 enable
      invoke KB_CommandWait      ; Write it
      OutPortB 0x64 , 0x60
      invoke KB_CommandWait
      OutPortB 0x60 , bl

      ; MOUSE_SEND MOUSE_RESET
      ; MOUSE_RECEIVE_ACK      ; ACK
      ; MOUSE_RECEIVE_ACK      ; 2 bytes of Mouse ID
      ; MOUSE_RECEIVE_ACK

      MOUSE_SEND MOUSE_DEFAULTS
      MOUSE_RECEIVE_ACK

      MOUSE_SEND MOUSE_ENABLE
      MOUSE_RECEIVE_ACK

      invoke Load_GATE \
         , dword [ IDT_BASE ] \
         , 0x2C \
         , SELECTOR ( 2 , DESC_SELECT_GDT0 ) \
         , dword Mouse_Interrupt_Handler \
         , DESC_PRESENT | DESC_PL0 | DESC_SYS_INTGATE_32BIT , 0

      invoke PIC_Enable_IRQ , dword 12
      invoke PIC_Enable_IRQ , dword 2
      popfd
      popad
      ret
      KB_CommandWait:
         in al, 64h      ; read status bit "1"
         test al, 00000010b
         jnz KB_Command_Wait
         ret
      KB_DataWait:
         in al, 64h      ; read status bit "0"
         test al, 00000001b
         jz KB_Data_Wait
         ret
   endproc

endproc
%endif
Post Reply