Page 4 of 5

Re: Mouse Location Always Zero

Posted: Wed Jul 20, 2016 12:26 pm
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.

Re: Mouse Location Always Zero

Posted: Wed Jul 20, 2016 3:01 pm
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 
}

Re: Mouse Location Always Zero

Posted: Wed Jul 20, 2016 3:49 pm
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.

Re: Mouse Location Always Zero

Posted: Wed Jul 20, 2016 4:18 pm
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.

Re: Mouse Location Always Zero

Posted: Thu Jul 21, 2016 1:29 am
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;

Re: Mouse Location Always Zero

Posted: Thu Jul 21, 2016 4:28 am
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.

Re: Mouse Location Always Zero

Posted: Thu Jul 21, 2016 7:29 am
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

Re: Mouse Location Always Zero

Posted: Thu Jul 21, 2016 7:42 am
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

Re: Mouse Location Always Zero

Posted: Thu Jul 21, 2016 8:13 am
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...

Re: Mouse Location Always Zero

Posted: Thu Jul 21, 2016 4:05 pm
by Octacone
mouse_handler never gets called... oh no...

Re: Mouse Location Always Zero

Posted: Thu Jul 21, 2016 4:13 pm
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);

Re: Mouse Location Always Zero

Posted: Fri Jul 22, 2016 3:59 am
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.

Re: Mouse Location Always Zero

Posted: Fri Jul 22, 2016 4:05 am
by Ch4ozz
The question is, does your IRQ handler get fired now?

Re: Mouse Location Always Zero

Posted: Fri Jul 22, 2016 4:15 am
by Octacone
Ch4ozz wrote:The question is, does your IRQ handler get fired now?
No, it does not. :|

Re: Mouse Location Always Zero

Posted: Fri Jul 22, 2016 4:20 am
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);