2

I am writing an OS. I have written "some" keyboard drivers and I wanted make a little mouse support, maybe a cursor. I read several guides on ps/2 mouse especially on osdev wiki and copied a bunch of code from Sanik's to start. (https://forum.osdev.org/viewtopic.php?t=10247)

However I cannot get the mouse to respond. I initialise the mouse and setup irq12 but the output is not consistent. Here is the code.

void mouse_install()
{
  unsigned char _status;  //unsigned char
    
    mouse_write(0xFF);
    mouse_read();

 
  //Enable the interrupts
  mouse_wait(1);
  outb(0x64, 0x20);
  mouse_wait(0);
   _status=inb(0x60);          
   _status = (_status | 2) ;
  mouse_wait(1);
  outb(0x64, 0x60);
  mouse_wait(1);
  outb(0x60, _status);
 mouse_read();
 
 
 
  //Enable the mouse
  mouse_write(0xF4);
  mouse_read();  //Acknowledge

}

I call this directly from the kernel after the interrupts are setup. I know for a fact that irq's work because keyboard works. This next code is the mouse handler. I call a printf after I get the mouse_x.

void irq12_handler(interrupt_frame_t* frame) //struct regs *a_r (not used but just there)
{
    
  switch(mouse_cycle)
  {
    case 0:
      mouse_byte[0]=inb(0x60);
      mouse_cycle++;
      break;
    case 1:
      mouse_byte[1]=inb(0x60);
      mouse_cycle++;
      break;
    case 2:
      mouse_byte[2]=inb(0x60);
      
      
      mouse_x = mouse_byte[1];
      mouse_y = mouse_byte[2];
      
     
      
      mouse_cycle=0;
      break;
  }
    
  printf(mouse_x);
  
  default_irq_handler(frame); // <- This is just outb(0x20, 0x20); outb(0xA0, 0x20);
  
}

The problem is I get 3 outputs at the initalisation and I don't get any more output when I move the mouse. However, the first time I wrote this code it did work. I got a bunch of values outputted when I moved the mouse but the next time I compiled it without changing any code it stopped working which is funny.

What may the problem be? I am running this on QEmu. I can post more code if you request.

Edit: This is th output

enter image description here

Edit: Printf code

Beware very bad code ahead.

void printf(const char* format, ...) 
{
    char * str = format;
    
    char ite;
    int numargs = 0;
    for(int z = 0; z < len(str); z++)
    {
        if(str[z+1] != '%')
            {
                numargs++;
            }
    }
    
    va_list listPointer;

    va_start( listPointer, numargs );
    
    
    for(int z = 0; z < strlen(str); z++)
    {
        ite = str[z];
        
        if(ite == '%')
        {
            switch(str[z+1])
            {
                case 's':
                {
                    char* arg = va_arg( listPointer, char* );
                    
                    for(int b = 0; b < strlen(arg); b++ )
                    {
                        drawchar(arg[b], 0xffffffff, 0x00000000);
                    
                    }
                    
                    break;
                }
                case 'd':
                {
                    int arg = va_arg( listPointer, int );
                    
                    char * conv;
                    itoa(arg, conv, 10);
                    
                    for(int b = 0; b < strlen(conv); b++ )
                    {
                        drawchar(conv[b], 0xffffffff, 0x00000000);
                    
                    }
                    
                    break;
                }
                
            }
            
            
            
            
            
            z++;
        }
        else
        {
            drawchar(ite, 0xffffffff, 0x00000000);
        }
        
        
    }

       //drawstring(current , 0xffffffff, 0x00000000);
    
}


Edit: Request to change printf(mouse_x) to printf("%d", mouse_x)

enter image description here

Important edit:

I just kept making little changes and I didnt change anything related to the mouse code however I got the following output

enter image description here

This is the output I got as I moved the mouse. It is very interesting because I didn't change anything at all. The code that didn't work before now works and it is likely to get broken in the future.

Edit: I ran the code again and I couldnt get the same output. Help.

Edit: I think I know the problem. If I were to run the program and at the moment it runs click on the qemu screen so I can capture the input the mouse is detected and some garbage is printed on the screen. The problem is that if I wait for a moment before clicking the screen I think the mouse is not detected thus it is not emulated as a ps/2 mouse in the qemu therefore it is not initialised. How can I resolve this issue.

Edit: These are the output with printf("%d", mouse_x) they make more sense

enter image description here

These are the deltaX from the mouse which means it works. The problem is with the timing that the mouse is connected to the computer. How can I solve the problem?

  • 1
    What is `printf`? Does it really take *integers* as first argument? – Some programmer dude Sep 21 '20 at 06:33
  • @Someprogrammerdude yea I think it does I am adding the printf code. It treats them as chars. – Özgür Güzeldereli Sep 21 '20 at 06:37
  • 1
    *Assuming* that e.g. `mouse_x` is an integer, then `printf(mouse_x)` will lead to *undefined behavior* as `printf` assumes the first argument is a *string* (really a pointer to the first character of a null-terminated byte string). If you programmed C before, then you should really know how to use `printf` to print integers (the version you have is simplified, and only support the basic `%d` and `%s` formats, but still enough). – Some programmer dude Sep 21 '20 at 06:42
  • @Someprogrammerdude yes I also tried it using printf("%d", mouse_x) which just prints 0. I don't care about the output. The expected behaviour is that it prints "something" as I move the mouse which did actually work at some point. I am adding the screenshot of output of printf("%d", mouse_x). By the way, mouse_x is defined as a signed char NOT an integer. – Özgür Güzeldereli Sep 21 '20 at 06:45
  • Possibly related: [PS/2 Mouse Not Firing](https://stackoverflow.com/questions/38877152/ps-2-mouse-not-firing)? – Some programmer dude Sep 21 '20 at 06:55
  • Also remember that the mouse doesn't send absolute positions, it's rather the *speed* at which the mouse is moved in a specific direction. Therefore it's valid to get zero in one direction, if there's no movement in that axis. – Some programmer dude Sep 21 '20 at 06:59
  • @Someprogrammerdude The op of the question forgot to uncomment some of the code that is why his mouse was broken. – Özgür Güzeldereli Sep 21 '20 at 06:59
  • @Someprogrammerdude yes I know it sends relative positions but I am not trying to make a cursor yet. I cannot even get the speed, the irq just wont fire everytime I move the mouse. – Özgür Güzeldereli Sep 21 '20 at 06:59
  • @Someprogrammerdude I have added several important edits that may help. I have identified the problem for good in the last edit. – Özgür Güzeldereli Sep 21 '20 at 07:52
  • This type of question is often difficult to answer without a [mcve] and isn't well suited for Stackoverflow. Have you considered putting your project into github or some other repository and then asking over on http://forums.osdev.org ? – Michael Petch Sep 21 '20 at 17:57
  • @MichaelPetch I will. Thank you. – Özgür Güzeldereli Sep 22 '20 at 04:50
  • @user3629249 Does it matter... – Özgür Güzeldereli Sep 23 '20 at 07:19
  • @ÖzgürGüzeldereli, Yes, it does matter. – user3629249 Sep 23 '20 at 15:24

0 Answers0