Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "floppy.h"
- #include <cstdio>
- #include <dayos.h>
- /// DMA Funktionen TODO: Einheitliches DMA System
- void WriteTransfer()
- {
- outb(0x0A, 0x06); // Channel 2 Mask
- outb(0x0B, 0x5A); // DMA-Write
- outb(0x0A, 0x02); // Channel 2 Unmask
- }
- void ReadTransfer()
- {
- outb(0x0A, 0x06); // Channel 2 Mask
- outb(0x0B, 0x56); // DMA-Read
- outb(0x0A, 0x02); // Channel 2 Unmask
- }
- bool SendData(unsigned char data)
- {
- for(int i = 0; i < 2000; i++)
- {
- if((inb(MSR) & 0xC0) == 0x80)
- {
- outb(FIFO, data);
- inb(MSR);
- return true;
- }
- }
- return false;
- }
- unsigned char ReadData()
- {
- for(int i = 0; i < 2000; i++)
- {
- if((inb(MSR) & 0xD0) == 0xD0)
- {
- return inb(FIFO);
- }
- }
- return 0;
- }
- void WaitIRQ()
- {
- Block(WAIT_FOR_MESSAGE);
- }
- void Reset()
- {
- outb(DRSR, 0 | 0x80);
- WaitIRQ();
- }
- void Sense(unsigned char* status, unsigned char* cylinder)
- {
- SendData(CMD_SENSE);
- *status = ReadData();
- *cylinder = ReadData();
- }
- void MotorOn()
- {
- uint8_t status = inb(0x3F2);
- outb(0x3F2, (status & 0x0F) | (0x10));
- Sleep(500);
- //printf("Motor on\n");
- }
- void MotorOff()
- {
- uint8_t status = inb(0x3F2);
- outb(0x3F2, status & ~(0x10));
- //printf("Motor off\n");
- }
- bool Calibrate()
- {
- MotorOn();
- for(int i = 0; i < 5; i++)
- {
- SendData(CMD_CALIBRATE);
- SendData(0);
- WaitIRQ();
- }
- MotorOff();
- }
- bool Seek(unsigned char cylinder, unsigned char head)
- {
- unsigned char st0, cyl0;
- int i = 0;
- for(i = 0; i < 5; i++)
- {
- SendData(CMD_SEEK);
- SendData((head) << 2 | 0);
- SendData(cylinder);
- WaitIRQ();
- Sense(&st0, &cyl0);
- Sleep(15);
- if(cyl0 == cylinder)
- return true;
- }
- return false;
- }
- bool ReadSector(unsigned char cylinder, unsigned char head, unsigned char sector)
- {
- unsigned char cyl0, status;
- ReadTransfer();
- MotorOn();
- Seek(cylinder, head);
- for (int i = 0; i<5; i++)
- {
- SendData(CMD_READ);
- SendData((head<<2) | 0);
- SendData(cylinder);
- SendData(head);
- SendData(sector);
- SendData(2);
- SendData(18);
- SendData(27);
- SendData(0xFF);
- WaitIRQ();
- unsigned char st0 = ReadData(); //st0
- ReadData(); //st1
- ReadData(); //st2
- ReadData(); //cylinder
- ReadData(); //head
- ReadData(); //sector
- ReadData(); //Sektorgröße
- Sense(&cyl0, &status);
- if((st0 & 0xC0) == 0)
- {
- MotorOff();
- return true;
- }
- }
- MotorOff();
- return false;
- }
- bool ReadSector(unsigned int lba)
- {
- unsigned char sector = (lba % 18) + 1;
- unsigned char cylinder = (lba / 18) / 2;
- unsigned char head = (lba / 18) % 2;
- return ReadSector(cylinder, head, sector);
- }
- void DetectDrives()
- {
- unsigned char c;
- outb(0x70, 0x10);
- c = inb(0x71);
- char drive = c >> 4;
- if(drive == 0)
- {
- printf("FLOPPY: Kein Floppy Laufwerk vorhanden!\n");
- while(1);
- }
- if(drive != 4)
- {
- printf("FLOPPY: Ihr Diskettenlaufwerk ist nicht unterstuetzt!\n");
- while(1);
- }
- printf("FLOPPY: Sie besitzen ein 3.5\" 1.44 MB Diskettenlaufwerk.\n");
- }
- void InitFDC()
- {
- DetectDrives();
- unsigned char st0, cyl0;
- Reset();
- //outb(DOR, 0);
- Sleep(20);
- outb(0x3F4, 0);
- outb(DOR, 0x0C);
- WaitIRQ();
- Sense(&st0, &cyl0);
- outb(CCR, 0);
- SendData(CMD_CONFIGURE);
- SendData(0);
- SendData((0 << 6) | (0 << 5) | (0 << 4) | 8);
- SendData(0);
- SendData(CMD_LOCK);
- ReadData();
- SendData(CMD_SPECIFY);
- SendData(0xDF);
- SendData(0x02);
- uint8_t tmp = inb(DOR);
- outb(DOR, (tmp & (~0x03)) | 0);
- Seek(1, 0);
- Calibrate();
- }
Advertisement
Add Comment
Please, Sign In to add comment