Can't read sectors after 35

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.
Post Reply
Zioo

Can't read sectors after 35

Post by Zioo »

Hello!
I've discovered a bug in my FDC code, while i wrote my FAT12 driver. My floppy driver is unable to read all sectors after 35, the seek function fails. But whats the difference between eg. sector 35 and 36? ???

Does anyone have a suggestion to what there can be wrong?

Your can find my source here: http://penguinpower.frac.dk/source/fdc.c
It's on line 167 the seek fails.

Btw. It does work in Bochs, it's on a real pc it fails.
Phugoid

Re:Can't read sectors after 35

Post by Phugoid »

One thing that's special about sector 36 is that it's the first sector on a new double-sided track (multiply tracks by heads to see this). The following code is wrong:

Code: Select all

void block2hts(int block,int *head,int *track,int *sector)
{
    *head = (block % (18 * 2)) / (18);
    *track = block / (18 * 2);
    *sector = block % 18 + 1;
}
The last line should be
*sector = (block % (18 * 2)) % 18 + 1;

But, in fact, you would be best off with some sort of temporary variable, since the first part of that expression is exactly the same as in the calculation for head.

You should also avoid hardcoding the values as best you can.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Can't read sectors after 35

Post by Candy »

Phugoid wrote: One thing that's special about sector 36 is that it's the first sector on a new double-sided track (multiply tracks by heads to see this). The following code is wrong:

Code: Select all

void block2hts(int block,int *head,int *track,int *sector)
{
    *head = (block % (18 * 2)) / (18);
    *track = block / (18 * 2);
    *sector = block % 18 + 1;
}
The last line should be
*sector = (block % (18 * 2)) % 18 + 1;
That's bollocks.

(block % (18*2)) % 18 == block % 18.


But, you are right in thinking it's the edge of cylinders that's causing an error.

Code: Select all

    *head = (block / 18) % 2;
    *track = block / (36);
    *sector = (block % 18) + 1;
These are minor changes, but I think the precedence of % might be fooling you. It does so for & in any case, so I expect it to be so for % as well.
But, in fact, you would be best off with some sort of temporary variable, since the first part of that expression is exactly the same as in the calculation for head.
That's a job for the compiler, not the user. It's called "Common Subexpression Elimination", and it's been in compiler design text books for at least 30 years. I expect the compiler he's using to know about it.
Phugoid

Re:Can't read sectors after 35

Post by Phugoid »

I stand corrected.

Please don't become condescending - all I have done is made a stupid error late at night. Otherwise, I am perfectly aware of
"Common Subexpression Elimination"
and modular arithmetic.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Can't read sectors after 35

Post by Candy »

Phugoid wrote: Please don't become condescending - all I have done is made a stupid error late at night. Otherwise, I am perfectly aware of
"Common Subexpression Elimination"
and modular arithmetic.
Sorry, didn't mean to offend you.
Zioo

Re:Can't read sectors after 35

Post by Zioo »

Thanks for the answers, but it does still not work.

I've added "vsprintf("fdc_track: %i track: %i\n",fdc_track,track);" and it returns "fdc_track: 0 track: 2" on a real pc, and in bochs where it work does it return "fdc_track: 2 track: 2" :-\
Zioo

Re:Can't read sectors after 35

Post by Zioo »

#previous

I added it in the seek function.. ;)
Post Reply