Mouse Location Always Zero

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.
User avatar
Ch4ozz
Member
Member
Posts: 170
Joined: Mon Jul 18, 2016 2:46 pm
Libera.chat IRC: esi

Re: Mouse Location Always Zero

Post by Ch4ozz »

thehardcoreOS wrote:Wait wait wait wait. You said "volatile"? I did not mark my coordinates as volatile. When I do that I get: error: conflicting type qualifiers for ‘mouse_x’

Code: Select all

volatile uint8 mouse_x = 0;
volatile uint8 mouse_y = 0;
I already checked IDT and GDT, they seem fine to me. (using info xyz)
Volatile simply tells the compiler to not optimize out the variables / code whatever you use it on.
For variables it also stops "precaching" the value into registers and on each use it will get loaded again from memory now.
Make sure the IRQ is called, and if it is, please post the code your using to get the mouse coords.
Also make sure its ONLY called when you move the mouse or click a button.
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: Mouse Location Always Zero

Post by Octacone »

Ch4ozz wrote:
thehardcoreOS wrote:Wait wait wait wait. You said "volatile"? I did not mark my coordinates as volatile. When I do that I get: error: conflicting type qualifiers for ‘mouse_x’

Code: Select all

volatile uint8 mouse_x = 0;
volatile uint8 mouse_y = 0;
I already checked IDT and GDT, they seem fine to me. (using info xyz)
Volatile simply tells the compiler to not optimize out the variables / code whatever you use it on.
For variables it also stops "precaching" the value into registers and on each use it will get loaded again from memory now.
Make sure the IRQ is called, and if it is, please post the code your using to get the mouse coords.
Also make sure its ONLY called when you move the mouse or click a button.

Oh nice to know that. Here is "my" code:

Code: Select all

#include "../include/mouse.h"

uint8_t mouse_cycle = 0;
uint8 mouse_byte[3];
volatile uint8 mouse_x = 0;
volatile uint8 mouse_y = 0;

void mouse_handler(struct regs* a_r) 
{
	writeOutput("MouseHandler");
	switch(mouse_cycle) 
	{
		case 0:
			mouse_byte[0] = inportb(0x60);
			mouse_cycle++;
			break;
		case 1:
			mouse_byte[1] = inportb(0x60);
			mouse_cycle++;
			break;
		case 2:
			mouse_byte[2] = inportb(0x60);
			mouse_x = mouse_byte[1];
			mouse_y = mouse_byte[2];
			mouse_cycle = 0;
			writeOutput("X: ");
			writeOutput(int_to_string(mouse_x));
			writeOutput("\r\nY: ");
			writeOutput(int_to_string(mouse_y));
			break;

	}
}

inline void mouse_wait(unsigned char a_type) 
{
	unsigned int time_out = 100000;
	if(a_type == 0) 
	{
		while(time_out--) {
			if((inportb(0x64) & 1) == 1) 
			{
				return;
			}
		}
		return;
	} else 
	{
		while(time_out--) 
		{
			if((inportb(0x64) & 2) == 0) 
			{
				return;
			}
		}
		return;
	}
}

inline void mouse_write(unsigned char a_write) 
{
	mouse_wait(1);
	outportb(0x64, 0xD4);
	mouse_wait(1);
	outportb(0x60, a_write);
}

unsigned char mouse_read() 
{
	mouse_wait(0);
	return inportb(0x60);
}

#define KERNEL_CS 0x08

void mouse_init()
 {
	unsigned char status;

	mouse_wait(1);
	outportb(0x64, 0xA8);

	mouse_wait(1);
	outportb(0x64, 0x20);
	mouse_wait(0);
	status = (inportb(0x60) | 2);
	mouse_wait(1);
	outportb(0x64, 0x60);
	mouse_wait(1);
	outportb(0x60, status);

	mouse_write(0xF6);
	mouse_read();

	mouse_write(0xF4);
	mouse_read();

	irq_install_handler(12, mouse_handler);
	irq_handler(mouse_handler);
	idt_set_gate(12,mouse_handler,KERNEL_CS,0x8E);
	writeOutput("mouse inizialized");
	draw_mouse(); //irrelevant gui function 
}
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
Ch4ozz
Member
Member
Posts: 170
Joined: Mon Jul 18, 2016 2:46 pm
Libera.chat IRC: esi

Re: Mouse Location Always Zero

Post by Ch4ozz »

Your cycle looks not complete.
Try thise one:

Code: Select all

void handle_mouse(CPU_STATE *cpu)
{	
	if (mouse_changed)
	{
		inb(0x60);
		return;
	}
	
	uint32_t val;
	if(mouse_cycle == 0)
	{
		mouse_flags = inb(0x60);
		if (!(mouse_flags & 0x08))
			mouse_cycle = 0;
		else
			mouse_cycle = 1;
	}
	else if(mouse_cycle == 1)
	{
		val = inb(0x60);
		val &= (uint32_t)0xFF;
		
		if (mouse_flags & 0x10)
			mouse_x = (val | 0xFFFFFF00);
		else
			mouse_x = val;
		
		mouse_cycle = 2;
	}
	else if(mouse_cycle == 2)
	{
		val = inb(0x60);
		val &= (uint32_t)0xFF;
		
		if (mouse_flags & 0x20)
			mouse_y = -(val | 0xFFFFFF00);
		else
			mouse_y = -val;

		mouse_changed = true;
		
		mouse_cycle = 0;
	}
}
Dont forget to reset mouse_changed again when your drawing is done.
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: Mouse Location Always Zero

Post by Octacone »

Ch4ozz wrote:Your cycle looks not complete.
Try thise one:
...

Dont forget to reset mouse_changed again when your drawing is done.
I tried and that CPU stage does nothing so I removed it.
Can you please mouse_changed and mouse flags definitions.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
Ch4ozz
Member
Member
Posts: 170
Joined: Mon Jul 18, 2016 2:46 pm
Libera.chat IRC: esi

Re: Mouse Location Always Zero

Post by Ch4ozz »

Code: Select all

unsigned volatile char mouse_flags;
bool volatile mouse_changed = false;
int volatile mouse_x = 0;
int volatile mouse_y = 0;
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: Mouse Location Always Zero

Post by Octacone »

Ch4ozz wrote:

Code: Select all

unsigned volatile char mouse_flags;
bool volatile mouse_changed = false;
int volatile mouse_x = 0;
int volatile mouse_y = 0;
Thanks! Again 00000000000, when I mouse my mouse nothing changes.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: Mouse Location Always Zero

Post by Octacone »

Here is my code(idt,gdt,isr,irq,mouse,kernel): http://pastebin.com/PuFbn8CG
Please see if everything is okay with it.
Note: this code does not have any syntax errors, that is not the problem.
If you see any errors, pointless code, junk code, etc... please tell me
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Mouse Location Always Zero

Post by Combuster »

In all these years, I never had a proper excuse to use this until now... I guess that also means you've got the questionable honour of taking the new world record low home today.

Image





I also spy half a dozen bugs that would be covered by compiler warnings. You are not free to ignore these; please pass the following to the compiler to see how broken your code actually still is:

Code: Select all

-Wall -Wextra -pedantic -Werror -std=c99
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: Mouse Location Always Zero

Post by Octacone »

Combuster wrote:In all these years, I never had a proper excuse to use this until now... I guess that also means you've got the questionable honour of taking the new world record low home today.







I also spy half a dozen bugs that would be covered by compiler warnings. You are not free to ignore these; please pass the following to the compiler to see how broken your code actually still is:

Code: Select all

-Wall -Wextra -pedantic -Werror -std=c99
Don't know if I am supposed to cry or laugh? I use some of those flags and I added the ones I was missing. There was not a single error...
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: Mouse Location Always Zero

Post by Octacone »

mouse_handler never gets called... oh no...
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Mouse Location Always Zero

Post by neon »

I think I mentioned for you to actually verify that your IRQ is being called back in the first page. Is this the first time you actually checked it, and if so, why did you wait so long? This problem could be resolved a lot sooner if you provide the requested information.

We also mentioned that you can't use IRQ 12 here since you remapped the PIC. In other words, this is wrong. (Think of what the PIC's actually do and where the PS/2 Mouse is connected. What, precisely, would the "12" here do and think about why it wouldn't work.)

Code: Select all

idt_set_gate(12,mouse_handler,KERNEL_CS,0x8E);
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: Mouse Location Always Zero

Post by Octacone »

neon wrote:I think I mentioned for you to actually verify that your IRQ is being called back in the first page. Is this the first time you actually checked it, and if so, why did you wait so long? This problem could be resolved a lot sooner if you provide the requested information.

We also mentioned that you can't use IRQ 12 here since you remapped the PIC. In other words, this is wrong. (Think of what the PIC's actually do and where the PS/2 Mouse is connected. What, precisely, would the "12" here do and think about why it wouldn't work.)

Code: Select all

idt_set_gate(12,mouse_handler,KERNEL_CS,0x8E);
I couldn't check it because I was fixing triple fault. So I instead of 12 I used 44, still getting zero.

Code: Select all

void mouse_init()
 {
	unsigned char status;

	mouse_wait(1);
	outportb(0x64, 0xA8);

	mouse_wait(1);
	outportb(0x64, 0x20);
	mouse_wait(0);
	status = (inportb(0x60) | 2);
	mouse_wait(1);
	outportb(0x64, 0x60);
	mouse_wait(1);
	outportb(0x60, status);

	mouse_write(0xF6);
	mouse_read();

	mouse_write(0xF4);
	mouse_read();

	irq_install_handler(44, mouse_handler);
	irq_handler(mouse_handler);
	idt_set_gate(44,mouse_handler,KERNEL_CS,0x8E);
	writeOutput("mouse inizialized");
	draw_mouse();
}
I see my desired message: "mouse initialized". But that is all.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
Ch4ozz
Member
Member
Posts: 170
Joined: Mon Jul 18, 2016 2:46 pm
Libera.chat IRC: esi

Re: Mouse Location Always Zero

Post by Ch4ozz »

The question is, does your IRQ handler get fired now?
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: Mouse Location Always Zero

Post by Octacone »

Ch4ozz wrote:The question is, does your IRQ handler get fired now?
No, it does not. :|
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
Ch4ozz
Member
Member
Posts: 170
Joined: Mon Jul 18, 2016 2:46 pm
Libera.chat IRC: esi

Re: Mouse Location Always Zero

Post by Ch4ozz »

Your unmasking for all interrupts looks wrong to me:

Code: Select all

outportb(0x21, 0x0);
outportb(0xA1, 0x0);
Change it to:

Code: Select all

outportb(0x20, 0x0);
outportb(0xA0, 0x0);
Post Reply