Chapter 20, Demo 15, how to write a sector to floppy disk?

If you are new to OS Development, plan on spending some time here first before going into the other forums.

Moderator: Moderators

Post Reply
ehenkes
Posts: 34
Joined: Fri Jul 24, 2009 5:35 pm

Chapter 20, Demo 15, how to write a sector to floppy disk?

Post by ehenkes » Thu Aug 06, 2009 4:58 pm

reading sectors works wonderfully. To go on to writing sectors, I tried to change the functions "read" into write functions, and it works. Important: delete FDC_CMD_EXT_SKIP, then it works! :D

in main(...) after successful reading of data

Code: Select all

	/// //////////////////////// TEST FLOPPY DRIVER
flpydsk_set_working_drive(0); // set drive 0 as current drive
flpydsk_install(6);           // floppy disk uses IRQ 6
int retVal = flpydsk_write_sector(1); // should be start of FAT1?
printformat("\nReturn Value of Write Sector: %d\n", retVal);
    /// //////////////////////// TEST FLOPPY DRIVER
flpydsk.c:

Code: Select all

// write a sector
void flpydsk_write_sector_imp(unsigned char head, unsigned char track, unsigned char sector)
{
	ULONG st0, cyl;
	flpydsk_dma_write(); printformat("!"); // set the DMA for write transfer

	flpydsk_send_command(FDC_CMD_WRITE_SECT | FDC_CMD_EXT_MULTITRACK |  FDC_CMD_EXT_DENSITY ); // write a sector
	flpydsk_send_command(head << 2 | _CurrentDrive ); 
	flpydsk_send_command(track);                      
	flpydsk_send_command(head);                       
	flpydsk_send_command(sector);                    
	flpydsk_send_command(FLPYDSK_SECTOR_DTL_512 );    
	flpydsk_send_command(( ( sector + 1 ) >= FLPY_SECTORS_PER_TRACK ) ? FLPY_SECTORS_PER_TRACK : sector + 1 ); 
	flpydsk_send_command(FLPYDSK_GAP3_LENGTH_3_5 );   
	flpydsk_send_command(0xFF);                       

	flpydsk_wait_irq(); 

	int j;
	for(j=0; j<7; ++j)
		flpydsk_read_data(); // read status info
	flpydsk_check_int(&st0,&cyl); // let FDC know we handled interrupt
}

// write a sector
int flpydsk_write_sector(int sectorLBA)
{
	if (_CurrentDrive >= 4) return -1;
	// convert LBA sector to CHS
	int head=0, track=0, sector=1;
	flpydsk_lba_to_chs(sectorLBA, &head, &track, &sector);
	// turn motor on and seek to track
	flpydsk_control_motor(TRUE);
	if(flpydsk_seek (track, head)) return -2;
	// write sector and turn motor off
	flpydsk_write_sector_imp(head, track, sector);
	flpydsk_control_motor(FALSE);
	return 0;
}
But it does not stop at one sector, it goes on with writing to the following sectors. How can I write exactly to the sector selected and nothing else? :o

Is that o.k.?
flpydsk_send_command(FDC_CMD_WRITE_SECT | FDC_CMD_EXT_MULTITRACK | FDC_CMD_EXT_DENSITY );

EDIT: yes, it is!
Last edited by ehenkes on Sun Aug 16, 2009 10:20 am, edited 1 time in total.

ehenkes
Posts: 34
Joined: Fri Jul 24, 2009 5:35 pm

Re: Chapter 20, Demo 15, how to write a sector to floppy disk?

Post by ehenkes » Fri Aug 14, 2009 7:58 pm

This is nonsense:

Code: Select all

flpydsk_send_command(( ( sector + 1 ) >= FLPY_SECTORS_PER_TRACK ) ? FLPY_SECTORS_PER_TRACK : sector + 1 ); 
better:

Code: Select all

flpydsk_send_command( 18 );

ehenkes
Posts: 34
Joined: Fri Jul 24, 2009 5:35 pm

Re: Chapter 20, Demo 15, how to write a sector to floppy disk?

Post by ehenkes » Fri Aug 14, 2009 8:07 pm

current code:

Code: Select all

// write a sector
void flpydsk_write_sector_imp(unsigned char head, unsigned char track, unsigned char sector)
{
	ULONG st0, cyl;
	flpydsk_dma_write(); // set the DMA for write transfer

	flpydsk_send_command( FDC_CMD_WRITE_SECT | FDC_CMD_EXT_MULTITRACK | FDC_CMD_EXT_DENSITY );  // write a sector
	flpydsk_send_command( head << 2 | _CurrentDrive );
	flpydsk_send_command( track);
	flpydsk_send_command( head);
	flpydsk_send_command( sector);
	flpydsk_send_command( FLPYDSK_SECTOR_DTL_512 );
	flpydsk_send_command( 18 );                             // full track
	flpydsk_send_command( FLPYDSK_GAP3_LENGTH_3_5 );
	flpydsk_send_command( 0xFF );

	flpydsk_wait_irq();

	// printformat("Return Values of write_sector:\n");
	int j;    
	for(j=0; j<7; ++j)
	{
		/*int val =*/ flpydsk_read_data(); // read status info: ST0 ST1 ST2 C H S Size(2: 512 Byte)
		// printformat("%d: %d  ",j,val);      		
	}
	// printformat("\n\n");
	flpydsk_check_int(&st0,&cyl); // let FDC know we handled interrupt
}

// write a sector
int flpydsk_write_sector(int sectorLBA)
{
	if (_CurrentDrive >= 4) return -1;
	// convert LBA sector to CHS
	int head=0, track=0, sector=1;
	flpydsk_lba_to_chs(sectorLBA, &head, &track, &sector);
	// turn motor on and seek to track
	flpydsk_control_motor(TRUE);
	if(flpydsk_seek (track, head)) return -2;
	// write sector and turn motor off
	flpydsk_write_sector_imp(head, track, sector);
	flpydsk_control_motor(FALSE);
	return 0;
}

User avatar
Mike
Site Admin
Posts: 465
Joined: Sat Oct 20, 2007 7:58 pm
Contact:

Re: Chapter 20, Demo 15, how to write a sector to floppy disk?

Post by Mike » Fri Aug 14, 2009 9:21 pm

This is nonsense: *code snip*
Hm, now that I think about it I am not sure what I was thinking when I wrote that code :shock: Going to have to fix that...

In any case, you should use FLPY_SECTORS_PER_TRACK as the parameter when calling flpydsk_send_command to avoid magic numbers.
Lead Programmer for BrokenThorn Entertainment, Co.
Website: http://www.brokenthorn.com
Email: webmaster@brokenthorn.com

ehenkes
Posts: 34
Joined: Fri Jul 24, 2009 5:35 pm

Re: Chapter 20, Demo 15, how to write a sector to floppy disk?

Post by ehenkes » Sun Aug 16, 2009 10:11 am

you should use FLPY_SECTORS_PER_TRACK as the parameter
Yes, that's correct. In the beginning, I wanted to write a function

void flpydsk_write_sector_imp(unsigned char head, unsigned char track, unsigned char sector, unsigned char num_of_sectors)

It works wonderfully with num_of_sectors less than 18, if you start exact at track beginnings: sector (LBA) 0, 18, 36, 54, ..., but you get an unbelievable "echo" at this track beginnings and senseless return values, if you use "odd" sector numbers. At the time being, I do not understand this finding. Any comments? Writing to the floppy disc is important.

Here, I read and analyze the root directory:
http://www.henkessoft.de/OS_Dev/OS_Dev3 ... ocId610721
http://www.henkessoft.de/OS_Dev/Downloads/104.rar

Mike, thanks for this FDC!

More links (first is great):
// http://www.isdaman.com/alsos/hardware/fdc/floppy.htm
// http://lowlevel.brainsware.org/wiki/ind ... dress_Mark
// http://www.eit.lth.se/fileadmin/eit/cou ... iption.pdf

ehenkes
Posts: 34
Joined: Fri Jul 24, 2009 5:35 pm

Re: Chapter 20, Demo 15, how to write a sector to floppy disk?

Post by ehenkes » Sun Aug 16, 2009 7:22 pm

http://www.dynacube.net/source/index.html
There you can find the interesting module floppy.h/floppy.c
Nice educational OS from India.

ehenkes
Posts: 34
Joined: Fri Jul 24, 2009 5:35 pm

Re: Chapter 20, Demo 15, how to write a sector to floppy disk?

Post by ehenkes » Sat Aug 29, 2009 4:46 pm

With some PCs you get return values of 0 for all bits, but reads data. What can be the reason?

Code: Select all

printformat("Return Values of read_sector:\n");
int j;
for(j=0; j<7; ++j)
{
	int val = flpydsk_read_data(); // read status info: ST0 ST1 ST2 C H S Size(2: 512 Byte)
	printformat("%d: %d  ",j,val);
}

Post Reply