#include #include #include #include #include "ddisk.h" extern int errno; int dwd=-1, cwd=-1, cwt=-1, cwh=-1; /* current working drive, track, head */ int numt=0, nums=0; char dbuf[4608], tbuf[4608]; /* sets the bytes-per-sector value (0=128,1=256,2=512,3=1024) */ void setbps(unsigned bpsval) { uchar far *dparm=MK_FP(0, 0x78); /* gets disk parm vector */ dparm=MK_FP((dparm[3]<<8)|dparm[2], (dparm[1]<<8)|dparm[0]); dparm[3]=bpsval; } /* initialise system to 256 byte sectors, reads directory track to memory and initialises tracks and sectors/track info returns 0 on success, -1 on error (setting errno) */ int dkinit(unsigned drive) { char buf[256]; setbps(1); /* init to 256 bps */ cwd=drive; if(dkrdabs(20, 1, buf)) { cwd=-1; return -1; } /* read t20,s1 (contains track info) */ if((buf[252]+buf[254]+1)&0xff+0xff&(buf[253]+buf[255]+1)) { cwd=-1; errno=EINVFMT; return -1; } numt=buf[252]; nums=buf[253]; return 0; } /* resets system to 512 byte sectors, ready for exiting */ void dktidy(void) { setbps(2); } /* read absolute sector into buf. sector is read from dbuf if directory track is specified. track=0.. sector=1.. returns 0 on success, -1 on error (setting errno) */ int dkrdabs(unsigned track, unsigned sector, uchar *buf) { char *mybuf=tbuf; int tready; /* references to sector after this point assume sector=0.. */ sector--; tready=(track==cwt && sector/18==cwh); /* check mem */ if(cwd<0) { errno=EINVDRV; return -1; } if(track==20 && sector<18) { mybuf=dbuf; if(dwd==cwd) tready=1; dwd=cwd; } else { if(track>=numt || sector>=nums) { errno=EFAULT; return -1; } cwt=track; cwh=sector/18; } if(!tready) { int retry=2; do { if(!retry--) { cwt=-1; cwh=-1; errno=EINVFMT; return -1; } } while(biosdisk(2, cwd, sector/18, track, 1, 18, mybuf)); } memcpy(buf, &mybuf[(sector%18)<<8], 0x0100); return 0; } /* read sector by lsn (calls dkrdabs so same conditions) tracks and sectors/track info must be initialised by dkinit before a call to this function */ int dkrdlsn(unsigned lsn, uchar *buf) { return dkrdabs(lsn/nums, (lsn%nums)+1, buf); } /* read a numbered (0-159) directory entry returns 0 on success, -1 on error */ int dkrdent(unsigned entno, dkent *ent) { char buf[256]; if(entno>159) { errno=EFAULT; return -1; } dkrdabs(20, (entno/10)+3, buf); memcpy(ent, &buf[(entno%10)*25], 25); return 0; }