summaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'kernel')
-rw-r--r--kernel/floppy.c115
1 files changed, 104 insertions, 11 deletions
diff --git a/kernel/floppy.c b/kernel/floppy.c
index a3c1c2a..8be440f 100644
--- a/kernel/floppy.c
+++ b/kernel/floppy.c
@@ -11,19 +11,18 @@
* * http://forum.osdev.org/viewtopic.php?f=1&t=13538
* * http://www.brokenthorn.com/Resources/OSDev20.html
* * http://bochs.sourceforge.net/cgi-bin/lxr/source/iodev/floppy.cc
- *
- *
- *
- *
+ * * http://viralpatel.net/taj/tutorial/chs_translation.php
*
*/
+
#include "x86.h"
-#define FLPY_SECTORS_PER_TRACK 80
+#define FLPY_SECTORS_PER_TRACK 18
+
static volatile int _CurrentDrive=0;
static volatile uint8_t _FloppyDiskIRQ = 0;
-// temporary dma hack
+// temporary dma hack
//! initialize DMA to use phys addr 1k-64k
void flpydsk_initialize_dma () {
@@ -324,8 +323,51 @@ void flpydsk_check_int (uint32_t* st0, uint32_t* cyl) {
*cyl = flpydsk_read_data ();
}
+void flpydsk_write_sector_imp (uint8_t head, uint8_t track, uint8_t sector) {
+
+ sector=1;
+ //! set the DMA for read transfer
+ flpydsk_dma_write ();
+
+ //flpydsk_drive_data (13, 1, 0xf, 0);
+ scr_put_string_nl("floppy: writing (head/track/sector)");
+
+ uint32_t st0, cyl;
+
+ //! write a sector
+ flpydsk_send_command (
+ FDC_CMD_WRITE_SECT | FDC_CMD_EXT_MULTITRACK |
+ FDC_CMD_EXT_DENSITY);
+ flpydsk_send_command ( head << 2 | _CurrentDrive );
+ flpydsk_send_command ( track);
+ flpydsk_send_command ( head); // first head should match with above!
+ flpydsk_send_command ( sector);
+ flpydsk_send_command ( FLPYDSK_SECTOR_DTL_512 );
+ flpydsk_send_command ( 3 ); // number of tracks we want
+
+ /*
+ 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 );
+
+ //! wait for irq
+ flpydsk_wait_irq ();
+
+ //! read status info
+ int j;
+ for (j=0; j<7; j++)
+ flpydsk_read_data ();
+
+ //! let FDC know we handled interrupt
+ flpydsk_check_int (&st0,&cyl);
+}
void flpydsk_read_sector_imp (uint8_t head, uint8_t track, uint8_t sector) {
+ sector=1;
//! set the DMA for read transfer
flpydsk_dma_read ();
@@ -344,9 +386,13 @@ void flpydsk_read_sector_imp (uint8_t head, uint8_t track, uint8_t sector) {
flpydsk_send_command ( head);
flpydsk_send_command ( sector);
flpydsk_send_command ( FLPYDSK_SECTOR_DTL_512 );
+ flpydsk_send_command ( 3 ); // number of tracks we want
+ /*
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 );
@@ -401,9 +447,15 @@ int flpydsk_seek ( uint32_t cyl, uint32_t head )
void flpydsk_lba_to_chs (int lba,int *head,int *track,int *sector) {
- *head = ( lba % ( FLPY_SECTORS_PER_TRACK * 2 ) ) / ( FLPY_SECTORS_PER_TRACK );
- *track = lba / ( FLPY_SECTORS_PER_TRACK * 2 );
- *sector = lba % FLPY_SECTORS_PER_TRACK + 1;
+ //cylinder
+ //
+// *track = lba % ( FLPY_SECTORS_PER_TRACK * 2 );
+ /// *head = ( lba % FLPY_SECTORS_PER_TRACK ) % 2;//* 2 ) ) / ( FLPY_SECTORS_PER_TRACK );
+ //*sector = (lba % FLPY_SECTORS_PER_TRACK) +1;
+
+ *head = ( lba % ( FLPY_SECTORS_PER_TRACK * 2 ) ) / ( FLPY_SECTORS_PER_TRACK );
+ *track = lba / ( FLPY_SECTORS_PER_TRACK * 2 );
+ *sector = lba % FLPY_SECTORS_PER_TRACK + 1;
}
@@ -440,6 +492,38 @@ uint8_t* flpydsk_read_sector (int sectorLBA) {
return (uint8_t*) 0x0;
}
+uint8_t* flpydsk_write_sector (int sectorLBA) {
+
+ scr_put_string("floppy: writing sector:");
+ scr_put_hex(sectorLBA);
+ scr_put_string_nl(".");
+
+
+ if (_CurrentDrive >= 4)
+ return 0;
+
+ //! 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
+ // start motor
+ flpydsk_motor_on();
+
+ if (flpydsk_seek (track, head) != 0)
+ return 0;
+
+ //! read sector and turn motor off
+ flpydsk_write_sector_imp (head, track, sector);
+ // flpydsk_control_motor (false);
+ flpydsk_motor_off();
+
+
+ //! warning: this is a bit hackish
+ //return (uint8_t*) DMA_BUFFER;
+ return (uint8_t*) 0x0;
+}
void floppy_init()
{
@@ -455,14 +539,23 @@ void floppy_init()
// flpydsk_drive_data (13, 1, 0xf, 0);
// flpydsk_drive_data (3,16,240,0);
//
- flpydsk_read_sector(1);
+ //
+
+ //
+ scr_put_string_nl("floppy: test read (sector 0).");
+ flpydsk_read_sector(0);
+ scr_put_string_nl("floppy: data: ");
uint16_t *dma=0xb000;
int i;
for(i=0;i<10;i++)
scr_put_hex(dma[i]);
-
scr_put_string_nl("");
+ //
+ scr_put_string_nl("floppy: test write (sector 100).");
+ flpydsk_write_sector(100);
+
+
}