Reading Floppy Drive

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.
Rahman

Reading Floppy Drive

Post by Rahman »

I have started programming FDC in the DMA mode. I assigned the track buffer at the address 0x80000 and initialized the DMA controller. I just wanted to know that should I enable paging before reading sectors from the floppy drive, since the page number of the track buffer should be sent to the DMA controller (?). I haven't got this point clear, whether this 64 kb pages are same as that of the 4 kb pages. Whenever I read sector from the FDD, the command starts execution, but terminates abnormally. The status registers returned are:

ST0 = 40h
ST1 = 4h
ST2 = 10h

But it returns the correct CHS value.
I have given my DMA initialization code:

Code: Select all

void ReadSector(int block)
{
  ...
  InitDMA (2, 0x80000, DEV_READ, 512);
  ...
}

void InitDMA(UCHAR Channel, long tBuffer, UCHAR Mode, int Size)
{
   __asm__ __volatile__ ("cli");
   outb (DMA_MASK, 4 | Channel);
   outb (DMA_FLIPFLOP, 0);
   if(Mode == DEV_READ)
    outb (DMA_MODE, 0x58 | Channel);
   else
    outb (DMA_MODE, 0x54 | Channel);
   outb (DMA_PAGE, tBuffer >> 16);
   outb (DMA_ADDRESS, 0x00);
   outb (DMA_ADDRESS, (tBuffer & 0xFF));
   outb (DMA_ADDRESS, (tBuffer & 0xFFFF) >> 8);
   outb (DMA_SIZE, Size & 0xFF);
   outb (DMA_SIZE, (Size & 0xFFFF) >> 8);
   outb (DMA_MASK, 2);
   __asm__ __volatile__ ("sti");
}
What would be wrong with this code ?
thank u.
Rahman

Re:Reading Floppy Drive

Post by Rahman »

when i try to write to floppy, i received the same problem. :(
plz. help me out of this problem.
Rahman

Re:Reading Floppy Drive

Post by Rahman »

should i have to enable paging before implementing floppy driver ?

any help appreciated.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Reading Floppy Drive

Post by Pype.Clicker »

Rahman wrote: should i have to enable paging before implementing floppy driver ?

any help appreciated.
no, this is not required. However, if you do have paging enabled and then want to use your FLoppy code, make sure you noticed that the DMA will use *phyiscal* addresses while the program will use *virtual* addresses.
Rahman

Re:Reading Floppy Drive

Post by Rahman »

i'm now in protected mode. i haven't enabled paging. but still the command doesn't work.
Slasher

Re:Reading Floppy Drive

Post by Slasher »

hi,
did you enable the DMA controller?

Code: Select all

outportb(0x08,0X10); /*FIXED PRIORITY,ENABLE CONTROLLER*/
did you mask the channel off before seting the mode and then back on?

Code: Select all

   outportb(DMA8_CHANNEL_MASK_REG,(channel|4)); /*mask channel,turn off so can be programmed*/

   outportb(DMA8_CHANNEL_MODE_REG,(mode|channel)); /*set mode for channel*/

   outportb(DMA8_CHANNEL_MASK_REG,channel); /*mask channel,turn on*/
how did you start the DMA transfer?

Code: Select all


unsigned char start_dma(unsigned char channel,char mode,unsigned long buffer,unsigned short count)
{
   unsigned short offset;
   unsigned char page;

   if(channel > 3)
      return 0;

   page=(unsigned char)((buffer>>16) & 0xff);
   offset=(unsigned short)(buffer & 0xffff);


   asm("cli");

   outportb(DMA8_CHANNEL_MASK_REG,(channel|4)); /*mask channel,turn off so can be programmed*/

   outportb(DMA8_CLEAR_FLIP_FLOP,0x00); /*clear flip flop*/

   outportb(DMA8_CHANNEL_MODE_REG,(mode|channel)); /*set mode for channel*/

   outportb(dma_address_reg[channel],(offset&0x00ff)); /*out low byte*/

   outportb(dma_address_reg[channel],(offset >>8));/*output high byte*/

   outportb(dma_count_reg[channel],(count & 0x00ff));

   outportb(dma_count_reg[channel],(count >> 8));

   outportb(dma_Page_reg[channel],page);

   asm("sti");

   outportb(DMA8_CHANNEL_MASK_REG,channel); /*unmask channel,turn on*/
   return 1;
}

PS
comment your code so that we know what you are trying to achieve.

you are free to use the code as you like.
Rahman

Re:Reading Floppy Drive

Post by Rahman »

thank you CS, now, i've enabled the DMA controller. but, still it doesn't work. i just wanted to know, does the FDC get interrupted before completing its read command. because, i've used a variable 'fdd_status' for maintaining the status of the floppy. i move FDS_IDLE, whenever the interrupt is called. i've given my ReadData() function which reads the data from the given sector and places it in the address 0x80000.

Code: Select all

void ReadSector(int block)
{
  ... 
  SeekCylinder(cyl);   // seeks the cyl.
  while (fdd_status != FDS_IDLE); // wait till the FDC gets IDLE
  InitDMA (2, 0x80000, DEV_READ, 512); // initialized DMA
  StartFDDMotor(); // starts drive A motor

  outb (FDC_CCR, 0);  // transfer rate 500 kb/s
  SendByte (CMD_READ); // command for read op.
  SendByte (head << 2);  // head bit shifted two times
  SendByte (cyl);  // cylinder
  SendByte (head);  // head
  SendByte (sect);  //sector number
  SendByte (2);  // sector size
  SendByte (FD_SPT);  // no. of sectors per track
  SendByte (FD_GAP3RW);  // length of GAP3 (0x1b)
  SendByte (0xFF);  // data length

  while (fdd_status != FDS_IDLE);

  st0 = ReadByte();  // read st0
  st1 = ReadByte(); // read st1
  st2 = ReadByte(); // read st2
  c = ReadByte(); // read current cyl.
  h = ReadByte(); // read current head
  s = ReadByte(); // read current sector
  ns = ReadByte();  // read sector size

  kprintf("\nST0 = %x",st0);
  kprintf("\nST1 = %x",st1);
  kprintf("\nST2 = %x",st2);
  kprintf("\nCylinder = %x",c);
  kprintf("\nHead = %x",h);
  kprintf("\nSector = %x",s);
  kprintf("\nSector Size = %x",ns);
  StopFDDMotor();  // stop motor from spinning
}
it prints
ST0=40;
ST1=4;
ST2=10
Cylinder = 0
Head = 0
Sector = 1
Sector Size = 2

Code: Select all

fdd_irq()
{
   fdd_status=FDS_IDLE;
}
when i read the 0th sector.

any help appreciated.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Reading Floppy Drive

Post by Pype.Clicker »

your "status" variable need to be declared as volatile in order to have working code.
Rahman

Re:Reading Floppy Drive

Post by Rahman »

i've declared the status variable as volatile. but, still the problem persists. could any one explain me about the steps involved in handling the interrupt after the read command.

any code appreciated.
Slasher

Re:Reading Floppy Drive

Post by Slasher »

here is my floppy interrupt handler

Code: Select all

_floppy_int:
  push eax
  push dword [_floppy_semaphore] ;the floppy semaphore
  call _wake  ;wake up the first task sleeping on the semaphore 
   add esp,4  ;remove the parameter from the stack
   mov al,0x20   ;acknowledge the interrupt to the PIC
   out 0x20,al    ;Master PIC

   pop eax
   iretd
   jmp $
where do you install your floppy interrupt handler? It should be on the 6th entry of the Master PIC. So if you reprogrammed the Master PIC to start from 0x20, then the floppy Interrupt entry is at 0x26.
Rahman

Re:Reading Floppy Drive

Post by Rahman »

yeah, i just wanted to know that after sending the command bytes for reading a sector, will the FDC get interrupted before reading the sector ? should i have to wait for the DMA completion after sending the command.

thank u
Rahman

Re:Reading Floppy Drive

Post by Rahman »

could anyone explain me about the steps that i should follow to read a sector using non-dma mode i.e., how should i receive the data from the FDC through the interrupt handler. when i try to read data from the interrupt handler after issuing the read command, i receive only the status variables as abnormal program termination.
Slasher

Re:Reading Floppy Drive

Post by Slasher »

Non DMA transfer is Not supported by all controller. And DMA transfer is easy.
1. follow the steps for programming the DMA controller but use channel 2 as this is used by floppy
2. Get the info on programming the floppy controller and understand it properly before starting
3. initialize the floppy controller and DMA controller before you start

You can get the needed doc for the DMA and Floppy from
www.osdever.net
Rahman

Re:Reading Floppy Drive

Post by Rahman »

i couldn't find the problem in my floppy driver. so, i have attached my cfloppy.c (floppy driver routines) and the cdma.c (DMA initialization coding). could any one plz. find out the problem and report it to me.

any help appreciated.
thank u.

[attachment deleted by admin]
BI lazy

Re:Reading Floppy Drive

Post by BI lazy »

Well ... how about starting/stopping the floppy motor before doing a reset/recalibrate/seek/readwrite command? How should the floppy seek before the motor is started.

Second: amuse your brains and *install interrupt handlers* before you want to continue with exploring the floppy driver stuff. your code (one *can* call it this way for sure) has several busy waiting loops that make me get goosepimple. this sort of stuff is far easier to be done with interrupt handlers.

third: CodeSlashers Floppy driver tutorial should show you the basic layout of such a thing. Of course, you need some prerequisites: Interrupt handlers, timer handlers (for the delays and the waiting stuff - I f. ex. use nonbusy delays - shall others do work meanwhile)

and now, lean back. fasten your seatbelt and ... get a clue. ];->

aha ... and please learn at least one or two techniques to track down a problem to some specific point.
Post Reply