Page 1 of 1

Floppy timeout [Not Solved] & GCC optimizations [Solved]

Posted: Sun May 03, 2009 4:53 am
by kay10
Hey everybody,
I've got 2 problems.
First problem while resetting the floppy controller.
I know the reset isn't fully implemented yet, but that's not the problem.
It works on a real pc(!) and in VirtualPC, but not in VmWare or Bochs, so I can't do any "low-level" debugging.
In Bochs, I get timeout messages after "printk("Floppy 3\n\r");", after "printk("Floppy 5\n\r");" and then it does an infinite loop while waiting for the floppy IRQ, so my question is whether Bochs emulates the floppy drive correctly or is it just a problem with my code? :?
In VmWare, the timeout messages don't appear, but it also waits endlessly for the floppy IRQ.

Code: Select all

BYTE fdc_reset()
{
	printk("Floppy 1\n\r");
	if(fdc_send_byte(FLOPPY_PRIMARY | FLOPPY_DOR, 0))
	{
		printk("Timeout! :(\n\r");
	}

	printk("Floppy 2\n\r");
	timer_wait(10);

	printk("Floppy 3\n\r");
	if(fdc_send_byte(FLOPPY_PRIMARY | FLOPPY_DOR, 0x4))
	{
		printk("Timeout! :(\n\r");
	}

	printk("Floppy 4\n\r");
	timer_wait(50);

	printk("Floppy 5\n\r");
	if(fdc_send_byte(FLOPPY_PRIMARY | FLOPPY_CCR, 0))
	{
		printk("Timeout! :(\n\r");
	}

	printk("Floppy 6\n\r");
	irq_wait(FLOPPY_IRQ, 0);
}
My second problem is that my code never works, if I add "-Os" to the gcc parameters, so gcc breaks my code with "optimizations"
while doing "timer_wait(10);", instead of waiting, it does an inifnite loop like "JMP $". :shock:
If you need more code I'll post it.
Every help will be appreciated. :)

Re: Floppy timeout & GCC code optimizations

Posted: Sun May 03, 2009 7:41 am
by Love4Boobies
Uhm. Maybe you could provide a little bit more info? For instance, we don't know what your functions actually do. And did that "JMP $" remark actually come from the generated assembler file or disassambled binary?

Re: Floppy timeout & GCC code optimizations

Posted: Sun May 03, 2009 8:37 am
by kay10
I know of the infinite loop because bochs internal debugger showed me something like this
jmp .+0xfffffffe
And then this code gets executed again and again and again...
I'm 100% sure this is a cause of "-Os" because I tried it without it, and then it works.
Is it possible to change my code so I can still use "-Os"?

Here are some other parts of my code:

Code: Select all

void timer_wait(unsigned int ticks)
{
	while(ticks)
	{
		if(irqs_fired[TIMER_IRQ])
		{
			ticks--;
			irqs_fired[TIMER_IRQ] = 0;
		}
	}
}
The Timer handler sets "irqs_fired[TIMER_IRQ]" to 1 every time it gets called.
I did this, so I don't have to watch out for a wrap around in the total tick count, which gets incremented by the timer handler too.

I hope you understand the following parts of my code without my whole constants.

Code: Select all

BYTE fdc_send_byte(WORD floppy_port, BYTE command)
{
	unsigned int timeout = 600;					// 600 * 10ms = 6 Seconds
	while(timeout > 0)
	{
		if(0x80 & inportb(FLOPPY_PRIMARY | FLOPPY_MSR))
		{
			outportb(floppy_port, command);
			return 0;
		}
		timeout--;
		timer_wait(1);
	}
	return 1;
}

Code: Select all

BYTE fdc_read_byte(WORD floppy_port)
{
	unsigned int timeout = 600;					// 600 * 10ms = 6 Seconds
	while(timeout > 0)
	{
		if(0xC0 & inportb(FLOPPY_PRIMARY | FLOPPY_MSR))
		{
			return inportb(FLOPPY_PRIMARY | floppy_port);
		}
		timeout--;
		timer_wait(1);
	}
	return 1;
}
IRQ Wait looks like this.
It is possible not to use a timeout, if the parameter "time" is 0, so this could be an infinte loop,
but I already double checked my code whether "time" is set to 0. This can't be the reason.

Code: Select all

int irq_wait(unsigned char irq, unsigned int time)
{
	char timeout = 1;
	if(!time) timeout = 0;
	while(!irqs_fired[irq])
	{
		timer_wait(1);
		if(!timeout) continue;
		if(!time) return 1;
		time--;
	}
	irqs_fired[irq] = 0;
	return 0;
}

Re: Floppy timeout & GCC code optimizations

Posted: Sun May 03, 2009 9:27 am
by giszo
You should make the irqs_fired array as volatile if it's not that already.

Re: Floppy timeout & GCC code optimizations

Posted: Sun May 03, 2009 9:43 am
by einsteinjunior
As said above ,you shouldf make your array volatile.
Also,i am sure you coded the inport and outport functions in gcc assembly.
Just make sure that the code is volatile asm code or else,the compiler may still do
some optimisation at that level.

Re: Floppy timeout & GCC code optimizations

Posted: Sun May 03, 2009 10:30 am
by kay10
Thanks, now I can use "-Os" again. :)
My inport and outport functions were already volatile asm code,
but making my array volatile was the solution.

That's cool, but my other problem is still not solved.
I can't debug my floppy code with bochs as I said above.

Maybe it has something to do with the timing?
I think it because sometimes bochs is really slow compared to a real pc and sometimes it executes code too quickly.
For example the timer interrupt gets called more often in a second than on a real pc.

Re: Floppy timeout & GCC code optimizations

Posted: Sun May 03, 2009 11:59 am
by tarrox
Let him wait longer, that was my solution of the same Problem with Bochs.

Re: Floppy timeout & GCC code optimizations

Posted: Mon May 04, 2009 11:18 am
by kay10
I let him wait for 10 seconds now and ist still don't work.
How long do you wait for your floppy drive to become ready? A minute? :shock:

I'm sure it's only a little thing to change...
Maybe I should just continue to work on my kernel, without bochs, because at the very beginning of my development,
virtual pc didn't accept my bootloader. And now, it works, but I don't now the odd reason.