Floppy disk read: 224 byte sector?

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
avcado
Member
Member
Posts: 36
Joined: Wed Jan 20, 2021 11:32 am
GitHub: https://github.com/mxtlrr/

Floppy disk read: 224 byte sector?

Post by avcado »

Hello, I've been working on a tiny floppy disk controller driver to read sector 1 off a floppy, without IRQ 6, however using DMA. I read that the procedure to do this is:

Code: Select all

reset controller -> turn on drive for commands -> seek to cylinder 0 -> read sector 1
So, I have this:

Code: Select all

void fdc_send_byte(uint8_t byte){
  for(;;){
    uint8_t msr = inb(MAIN_STATUS_REG);
      if(msr & (1<<7)){ // data register is ready
	// wait until FIFO expects OUT, not IN
	if(!(msr & (1<<6))) {
	  outb(DATA_REGISTER, byte);
	  break;
        }
      }
    }
}

Code: Select all


outb(DIGITAL_OUT_REG, 0x00);        // Reset controller
outb(DIGITAL_OUT_REG, 0b00011100);  // Enable DMA, turn drive A for commands

// seek 
fdc_send_byte(0x0F);
fdc_send_byte(0);
fdc_send_byte(0);

fdc_send_byte(0x06);
fdc_send_byte(0); // Bit 0-1: drive, Bit 2: head
fdc_send_byte(0); // Cylinder
fdc_send_byte(0); // Head
fdc_send_byte(1); // Sector 1
fdc_send_byte(2); // spec states 2 -> 512 bytes.
fdc_send_byte(1); // Max sector (only read one sector)
fdc_send_byte(42); // GAP3 length. CMOS states this floppy is a 5 inch, 27 is for 3 inch
fdc_send_byte(0xFF); // DTL -- unused, set to 0xFF
I know that the seek command has no result phase, but read does, however the results phase for this (retrieved this from FIFO register) is

Code: Select all

ST0: 0b00000010
ST1: 0b00000001
ST2: 0b00000000

Cylinder: 0
Head: 0
Sector: 1
Sector Size: 224
Then reading like this (I'm 80% sure this is wrong):

Code: Select all

for(uint16_t i = 0; i < 513; i++) buf[i] = inb(DATA_REGISTER);
All the bytes in the array are 0, however this is not expected behavior, as the floppy disk just has "Hello World" in it.

--------

Why is the results phase giving me a sector size of 224 bytes? Am I reading from the floppy disk right, or is there something else I need to do beforehand?
Thanks in advance!! :D
Projects: DXB
Octocontrabass
Member
Member
Posts: 5885
Joined: Mon Mar 25, 2013 7:01 pm

Re: Floppy disk read: 224 byte sector?

Post by Octocontrabass »

avcado wrote: Wed Jul 09, 2025 4:03 pmwithout IRQ 6
Why aren't you using IRQs? That's a strange thing to do, and sometimes hardware misbehaves when you do strange things.
avcado wrote: Wed Jul 09, 2025 4:03 pmreset controller -> turn on drive for commands -> seek to cylinder 0 -> read sector 1
After you reset the controller, you probably need four SENSE INTERRUPT commands, a CONFIGURE command, and a SPECIFY command. At some point you also need to set DSR/CCR with the correct data rate for your chosen combination of disk and drive.
avcado wrote: Wed Jul 09, 2025 4:03 pm

Code: Select all

fdc_send_byte(0x06);
How is your disk formatted? You're telling the controller to use FM but PC disks are almost always MFM.
avcado wrote: Wed Jul 09, 2025 4:03 pmThen reading like this (I'm 80% sure this is wrong):
I see three problems. First, reading happens before the results phase, not after. Second, you're reading 513 bytes instead of 512. Finally, this is PIO, not DMA.
Post Reply