Guest User

Untitled

a guest
May 8th, 2012
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.17 KB | None | 0 0
  1. #include "floppy.h"
  2. #include <cstdio>
  3. #include <dayos.h>
  4.  
  5.  
  6. /// DMA Funktionen TODO: Einheitliches DMA System
  7.  
  8. void WriteTransfer()
  9. {
  10.     outb(0x0A, 0x06); // Channel 2 Mask
  11.     outb(0x0B, 0x5A); // DMA-Write
  12.    
  13.     outb(0x0A, 0x02); // Channel 2 Unmask
  14. }
  15.  
  16. void ReadTransfer()
  17. {
  18.     outb(0x0A, 0x06); // Channel 2 Mask
  19.     outb(0x0B, 0x56); // DMA-Read
  20.    
  21.     outb(0x0A, 0x02); // Channel 2 Unmask
  22. }
  23.  
  24. bool SendData(unsigned char data)
  25. {
  26.     for(int i = 0; i < 2000; i++)
  27.     {
  28.         if((inb(MSR) & 0xC0) == 0x80)
  29.         {
  30.             outb(FIFO, data);
  31.             inb(MSR);
  32.             return true;
  33.         }
  34.     }
  35.    
  36.     return false;
  37. }
  38.  
  39. unsigned char ReadData()
  40. {
  41.     for(int i = 0; i < 2000; i++)
  42.     {
  43.         if((inb(MSR) & 0xD0) == 0xD0)
  44.         {
  45.             return inb(FIFO);
  46.         }
  47.     }
  48.    
  49.     return 0;
  50. }
  51.  
  52. void WaitIRQ()
  53. {
  54.     Block(WAIT_FOR_MESSAGE);
  55. }
  56.  
  57. void Reset()
  58. {
  59.     outb(DRSR, 0 | 0x80);
  60.    
  61.     WaitIRQ();
  62. }
  63.  
  64. void Sense(unsigned char* status, unsigned char* cylinder)
  65. {
  66.     SendData(CMD_SENSE);
  67.     *status = ReadData();
  68.     *cylinder = ReadData();
  69. }
  70.  
  71. void MotorOn()
  72. {
  73.     uint8_t status = inb(0x3F2);
  74.     outb(0x3F2, (status & 0x0F) | (0x10));
  75.     Sleep(500);
  76.     //printf("Motor on\n");
  77. }
  78.  
  79. void MotorOff()
  80. {
  81.     uint8_t status = inb(0x3F2);
  82.     outb(0x3F2, status & ~(0x10));
  83.     //printf("Motor off\n");
  84. }
  85.  
  86. bool Calibrate()
  87. {
  88.     MotorOn();
  89.    
  90.     for(int i = 0; i < 5; i++)
  91.     {
  92.         SendData(CMD_CALIBRATE);
  93.         SendData(0);
  94.         WaitIRQ();
  95.     }
  96.    
  97.     MotorOff();
  98. }
  99.  
  100. bool Seek(unsigned char cylinder, unsigned char head)
  101. {
  102.     unsigned char st0, cyl0;
  103.  
  104.     int i = 0;
  105.     for(i = 0; i < 5; i++)
  106.     {
  107.         SendData(CMD_SEEK);
  108.         SendData((head) << 2 | 0);
  109.         SendData(cylinder);
  110.  
  111.         WaitIRQ();
  112.        
  113.         Sense(&st0, &cyl0);
  114.  
  115.         Sleep(15);
  116.        
  117.         if(cyl0 == cylinder)
  118.             return true;
  119.     }
  120.  
  121.     return false;
  122. }
  123.  
  124. bool ReadSector(unsigned char cylinder, unsigned char head, unsigned char sector)
  125. {
  126.     unsigned char cyl0, status;
  127.  
  128.     ReadTransfer();
  129.    
  130.     MotorOn();
  131.    
  132.     Seek(cylinder, head);
  133.    
  134.     for (int i = 0; i<5; i++)
  135.     {
  136.         SendData(CMD_READ);
  137.         SendData((head<<2) | 0);
  138.         SendData(cylinder);
  139.         SendData(head);
  140.         SendData(sector);
  141.        
  142.         SendData(2);
  143.         SendData(18);
  144.         SendData(27);
  145.         SendData(0xFF);
  146.        
  147.         WaitIRQ();
  148.            
  149.         unsigned char st0 = ReadData();  //st0
  150.         ReadData();  //st1
  151.         ReadData();  //st2
  152.         ReadData();  //cylinder
  153.         ReadData();  //head
  154.         ReadData();  //sector
  155.         ReadData();  //Sektorgröße
  156.        
  157.     Sense(&cyl0, &status);
  158.    
  159.         if((st0 & 0xC0) == 0)
  160.         {
  161.           MotorOff();
  162.           return true;
  163.         }
  164.     }
  165.    
  166.     MotorOff();
  167.     return false;
  168. }
  169.  
  170. bool ReadSector(unsigned int lba)
  171. {
  172.     unsigned char sector = (lba % 18) + 1;
  173.     unsigned char cylinder = (lba / 18) / 2;
  174.     unsigned char head = (lba / 18) % 2;
  175.    
  176.     return ReadSector(cylinder, head, sector);
  177. }
  178.  
  179. void DetectDrives()
  180. {
  181.     unsigned char c;
  182.     outb(0x70, 0x10);
  183.     c = inb(0x71);
  184.    
  185.     char drive = c >> 4;
  186.    
  187.     if(drive == 0)
  188.     {
  189.         printf("FLOPPY: Kein Floppy Laufwerk vorhanden!\n");
  190.         while(1);
  191.     }
  192.    
  193.     if(drive != 4)
  194.     {
  195.         printf("FLOPPY: Ihr Diskettenlaufwerk ist nicht unterstuetzt!\n");
  196.         while(1);
  197.     }
  198.    
  199.     printf("FLOPPY: Sie besitzen ein 3.5\" 1.44 MB Diskettenlaufwerk.\n");
  200. }
  201.  
  202. void InitFDC()
  203. {
  204.     DetectDrives();
  205.  
  206.     unsigned char st0, cyl0;
  207.    
  208.     Reset();
  209.     //outb(DOR, 0);
  210.  
  211.     Sleep(20);
  212.     outb(0x3F4, 0);
  213.     outb(DOR, 0x0C);
  214.    
  215.     WaitIRQ();
  216.          
  217.     Sense(&st0, &cyl0);
  218.  
  219.     outb(CCR, 0);    
  220.    
  221.     SendData(CMD_CONFIGURE);
  222.     SendData(0);
  223.     SendData((0 << 6) | (0 << 5) | (0 << 4) | 8);
  224.     SendData(0);
  225.    
  226.     SendData(CMD_LOCK);
  227.     ReadData();
  228.  
  229.     SendData(CMD_SPECIFY);
  230.     SendData(0xDF);
  231.     SendData(0x02);
  232.    
  233.     uint8_t tmp = inb(DOR);
  234.     outb(DOR, (tmp & (~0x03)) | 0);
  235.  
  236.     Seek(1, 0);
  237.  
  238.     Calibrate();
  239. }
Advertisement
Add Comment
Please, Sign In to add comment