OSDev. PS/2 Mouse not working. IRQ 12 doesn't fire when mouse is moved

1

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?

c
osdev
asked on Stack Overflow Sep 21, 2020 by Özgür Güzeldereli • edited Sep 21, 2020 by Özgür Güzeldereli

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0