/* disks.c - interface to IBM ROM BIOS disk routines */
/* lrs 2/13/89 */

#include <bios.h>
#include <dos.h>
#include "disks.h"

void InitDisks()
  { /* ROM BIOS "Reset Disks" command */
  union REGS reg;
  reg.x.ax = 0;
  int86(0x13,&reg,&reg);
  }    /* InitDisks */
/*-----------------------------------------------------------------------*/
void GetDiskParams(int drive, int* maxcyl, int* maxhead, int* maxsec)
  { /* get size of disk */
    /* (note: this (and the rest) assumes 512 byte sectors */
  union REGS reg;
  reg.h.ah = 8;
  reg.h.dl = drive;
  int86(0x13,&reg,&reg);
  *maxcyl = reg.h.ch + ((reg.h.cl & 0xC0) << 2);
  *maxhead = reg.h.dh;
  *maxsec = reg.h.cl & 0x3F;
  }    /* GetDiskParameters */
/*-----------------------------------------------------------------------*/
int ReadSector (int drive, int cyl, int head, int sec, void far* buf)
  { /* read a 512 byte sector from phys location on disk */
    /* return TRUE if success */
  union REGS reg;
  struct SREGS sreg;

  int retry = 3;;
  do {
    reg.x.ax = 0x0201;
    reg.x.bx = FP_OFF(buf);
    sreg.es = FP_SEG(buf);
    reg.h.ch = cyl & 0xFF;
    /* top two bits (8 & 9) of cyl # hidden in bits 6 & 7 of sector */
    reg.h.cl = sec | ((cyl >> 2) & 0xC0);
    reg.h.dh = head;
    reg.h.dl = drive;
    int86x(0x13,&reg,&reg,&sreg);
    } while ((reg.x.cflag) && (retry-- > 0));
  return(reg.x.cflag == 0);
  }   /* ReadSector */
/*-----------------------------------------------------------------------*/
int WriteSector (int drive, int cyl, int head, int sec, void far* buf)
  { /* write memory block to specified physical sector on disk */
    /* return TRUE if success */
  union REGS reg;
  struct SREGS sreg;
  int retry = 3;

    do {
      reg.x.ax = 0x0301;
      reg.x.bx = FP_OFF(buf);    /* THIS MUST BE FIXED !!!! */
      sreg.es = FP_SEG(buf);
      reg.h.ch = cyl & 0xFF;
      /* top two bits (8 & 9) of cyl # hidden in bits 6 & 7 of sector */
      reg.h.cl = sec | ((cyl >> 2) & 0xC0);
      reg.h.dh = head;
      reg.h.dl = drive;
      int86x(0x13,&reg,&reg,&sreg);
      } while ((reg.x.cflag) && (retry-- > 0));
    return(reg.x.cflag == 0);
  }   /* WriteSector */

/*---- end of disks.c ----*/
