⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 floppy.c

📁 用于汇编领域的,运用于OS的MAIN函数.基于硬件基础的源代码
💻 C
字号:
#include "hos.h"#define READ_SECTOR_HEAD_DRIVE 1#define READ_SECTOR_TRACK 2#define READ_SECTOR_HEAD 3#define READ_SECTOR 4#define SEEK_HEAD_DRIVE 1#define SEEK_CYLINDER 2unsigned int floppy_code_read_sector[9]=// hard code to read one sector//{0x66, 0x00, 0x00, 0x00, 0x00, 0x02, 18, 0x1b, 0x00};{0xe6, 0x00, 0x00, 0x00, 0x00, 0x02, 18, 0x1b, 0x00};unsigned int floppy_code_calibrate[2]=//calibrate hard code{0x07, 0x00};unsigned int floppy_code_seek[3]=//seek hard code{0x0f, 0x00, 0x00};unsigned int floppy_code_interrupt_status=0x08; // hard code for checking                                                // interrupt statusint g_FloppyInterrupted=0;int cont=0;static int interrupt_count=1;char result_seek[7];//void ReadIt();//void ReadSector(int head, int track, int sector, unsigned char* source, unsigned char * destinity){void ReadWriteSector(int mode,int head, int track, int sector, unsigned char* source, unsigned char * destinity){// mode=0 for read sector; 1 for write sector   int i;   int page, offset;   unsigned int src, dest;   char result[7];   src = (unsigned int) source;   dest = (unsigned int) destinity;   page =(int) (src >> 16); // page number   offset=(int) (src & 0x0ffff); // offset withing the page   FloppyMotorOn();  // set drive motor on   delay(20);   //printk("after motor on\n");   for(i=0; i<5; i++){      // try 5 times	  if (!FloppyCalibrateHead())  // calibrate	     continue;	  if (!FloppySeek(head, track))  //seek	     continue;	  else	     break;   }   //printk("after loop 5 times\n");   if (mode==0)      initializeDMA(page, offset);  // init dma for disk->mem transfer   else      initializeDMAWrite(page, offset);  // init dma for mem->disk transfer         //printk("after init dma\n");   // set head, track, sector   floppy_code_read_sector[READ_SECTOR_HEAD_DRIVE]=head << 2;   floppy_code_read_sector[READ_SECTOR_TRACK]=track;   floppy_code_read_sector[READ_SECTOR_HEAD]=head;   floppy_code_read_sector[READ_SECTOR]=sector;   if (mode==0) // read sector      floppy_code_read_sector[0]=0xe6; //command for read sector    else      floppy_code_read_sector[0]=0xc5; //command for write sector   g_FloppyInterrupted=0;  // fdc will raise irq6, and the fd handler                           // will set this to 1 to indicate the end of read   for(i=0;i<9;i++){      WaitFloppy();  // wait until fdc is ready	  FloppyCode(floppy_code_read_sector[i]);   } // the above loop will read one sector   //printk("aft read/write one sector. wait for int\n");   while (!g_FloppyInterrupted); // wait here   g_FloppyInterrupted=0;   delay(20);   for(i=0;i<7;i++){      WaitFloppy();	  result[i]=ResultFhase(); // check result   }   WaitFloppy();   FloppyMotorOff();   if (mode==0)      for(i=0;i<512;i++)         destinity[i]=source[i]; // copy read data}int lba_to_chs(int lba, int *c, int *h, int *s){// assume we are using 3.5" 1.4MB disk   int temp;   *c = lba/36; //36 sectors per cylinder   temp = lba%36; // num of sectos in the last cylinder   *h = temp/18; // 18 sectors per track   *s = temp % 18 + 1;    return 0;}int ReadRelativeSector(int sect, char *buf, char *buf1){   int cyl, head, sector;   lba_to_chs(sect, &cyl, &head, &sector);//convert to cyl,head,sector   ReadWriteSector(0,head,cyl,sector,buf,buf1);   return 0;}int WriteRelativeSector(int sect, char *buf, char *buf1){   int cyl, head, sector;   lba_to_chs(sect, &cyl, &head, &sector);//convert to cyl,head,sector   printk("bef write to sector. buf:%x\n",(int)buf);   printk("bef write to sector. buf[0]:%x buf[1]:%x\n",(int)buf[0],(int)buf[1]);   ReadWriteSector(1,head,cyl,sector,buf,buf1);   return 0;}int FloppyCalibrateHead(){   int i;   char result[2];   //printk("in floppy calibrate\n");   for(i=0;i<2;i++){     WaitFloppy();	 FloppyCode(floppy_code_calibrate[i]);   }   //printk("aft loop twice\n");   delay(20);   WaitFloppy();   FloppyCode(floppy_code_interrupt_status);   WaitFloppy();   //printk("bef result fhase\n");   result[0]=ResultFhase();   WaitFloppy();   result[1]=ResultFhase();   //printk("bef ret. result[0] is %x\n", result[0]);   if (result[0] != 0x20)      return 0;   else      return 1;}int FloppySeek(int head, int cylinder){   int i;   char result[7];   //printk("in floppyseek\n");   floppy_code_seek[SEEK_CYLINDER] = cylinder;   floppy_code_seek[SEEK_HEAD_DRIVE]=head << 2;   g_FloppyInterrupted=0;   for(i=0;i<3;i++){      WaitFloppy();	  FloppyCode(floppy_code_seek[i]);   }   //printk("aft seek. waiting for int\n");   while (!g_FloppyInterrupted); // wait for int   delay(20);   WaitFloppy();   FloppyCode(floppy_code_interrupt_status);   WaitFloppy();   result[0]=ResultFhase();   WaitFloppy();   result[1]=ResultFhase();   if (result[0] != 0x20)      return 0;  // failure   WaitFloppy();   FloppyCode(0x4a);  // read sector ID   WaitFloppy();   FloppyCode(head << 2);   for(i=0;i<7;i++){      WaitFloppy();	  result_seek[i] = ResultFhase();   }   if (result_seek[3] != cylinder)      return 0; // fail   else      return 1;}void FloppyCode(unsigned int code){   outbyte(0x3f5, code);}void WaitFloppy(){   unsigned int result;   while (1){      result = inbyte(0x3f4);	  if ((result & 0x80) == 0x80)	     break;   }}void floppy_isr(void);void floppy_handler(void){    //printk("in floppy handler\n");	g_FloppyInterrupted=1;	//for(;;);	outbyte(0x20, 0x20);	/* EOI to Master */	outbyte(0xA0, 0x20);	/* EOI to Slave */}void init_floppy(void){    // setting IDT	set_intr_gate(0x26, &floppy_isr);	unmask_irq(6);    // some fdc init here	//fdc_setup();}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -