00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "copyright.h"
00017 #include "disk.h"
00018 #include "system.h"
00019
00020
00021
00022
00023 #define MagicNumber 0x456789ab
00024 #define MagicSize sizeof(int)
00025
00026 #define DiskSize (MagicSize + (NumSectors * SectorSize))
00027
00028
00029 static void DiskDone(int arg) { ((Disk *)arg)->HandleInterrupt(); }
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 Disk::Disk(char* name, VoidFunctionPtr callWhenDone, int callArg)
00044 {
00045 int magicNum;
00046 int tmp = 0;
00047
00048 DEBUG('d', "Initializing the disk, 0x%x 0x%x\n", callWhenDone, callArg);
00049 handler = callWhenDone;
00050 handlerArg = callArg;
00051 lastSector = 0;
00052 bufferInit = 0;
00053
00054 fileno = OpenForReadWrite(name, FALSE);
00055 if (fileno >= 0) {
00056 Read(fileno, (char *) &magicNum, MagicSize);
00057 ASSERT(magicNum == MagicNumber);
00058 } else {
00059 fileno = OpenForWrite(name);
00060 magicNum = MagicNumber;
00061 WriteFile(fileno, (char *) &magicNum, MagicSize);
00062
00063
00064 Lseek(fileno, DiskSize - sizeof(int), 0);
00065 WriteFile(fileno, (char *)&tmp, sizeof(int));
00066 }
00067 active = FALSE;
00068 }
00069
00070
00071
00072
00073
00074
00075
00076 Disk::~Disk()
00077 {
00078 Close(fileno);
00079 }
00080
00081
00082
00083
00084
00085
00086 static void
00087 PrintSector (bool writing, int sector, char *data)
00088 {
00089 int *p = (int *) data;
00090
00091 if (writing)
00092 printf("Writing sector: %d\n", sector);
00093 else
00094 printf("Reading sector: %d\n", sector);
00095 for (unsigned int i = 0; i < (SectorSize/sizeof(int)); i++)
00096 printf("%x ", p[i]);
00097 printf("\n");
00098 }
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 void
00116 Disk::ReadRequest(int sectorNumber, char* data)
00117 {
00118 int ticks = ComputeLatency(sectorNumber, FALSE);
00119
00120 ASSERT(!active);
00121 ASSERT((sectorNumber >= 0) && (sectorNumber < NumSectors));
00122
00123 DEBUG('d', "Reading from sector %d\n", sectorNumber);
00124 Lseek(fileno, SectorSize * sectorNumber + MagicSize, 0);
00125 Read(fileno, data, SectorSize);
00126 if (DebugIsEnabled('d'))
00127 PrintSector(FALSE, sectorNumber, data);
00128
00129 active = TRUE;
00130 UpdateLast(sectorNumber);
00131 stats->numDiskReads++;
00132 interrupt->Schedule(DiskDone, (int) this, ticks, DiskInt);
00133 }
00134
00135 void
00136 Disk::WriteRequest(int sectorNumber, char* data)
00137 {
00138 int ticks = ComputeLatency(sectorNumber, TRUE);
00139
00140 ASSERT(!active);
00141 ASSERT((sectorNumber >= 0) && (sectorNumber < NumSectors));
00142
00143 DEBUG('d', "Writing to sector %d\n", sectorNumber);
00144 Lseek(fileno, SectorSize * sectorNumber + MagicSize, 0);
00145 WriteFile(fileno, data, SectorSize);
00146 if (DebugIsEnabled('d'))
00147 PrintSector(TRUE, sectorNumber, data);
00148
00149 active = TRUE;
00150 UpdateLast(sectorNumber);
00151 stats->numDiskWrites++;
00152 interrupt->Schedule(DiskDone, (int) this, ticks, DiskInt);
00153 }
00154
00155
00156
00157
00158
00159
00160
00161 void
00162 Disk::HandleInterrupt ()
00163 {
00164 active = FALSE;
00165 (*handler)(handlerArg);
00166 }
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 int
00180 Disk::TimeToSeek(int newSector, int *rotation)
00181 {
00182 int newTrack = newSector / SectorsPerTrack;
00183 int oldTrack = lastSector / SectorsPerTrack;
00184 int seek = abs(newTrack - oldTrack) * SeekTime;
00185
00186 int over = (stats->totalTicks + seek) % RotationTime;
00187
00188
00189
00190 *rotation = 0;
00191 if (over > 0)
00192 *rotation = RotationTime - over;
00193 return seek;
00194 }
00195
00196
00197
00198
00199
00200
00201
00202 int
00203 Disk::ModuloDiff(int to, int from)
00204 {
00205 int toOffset = to % SectorsPerTrack;
00206 int fromOffset = from % SectorsPerTrack;
00207
00208 return ((toOffset - fromOffset) + SectorsPerTrack) % SectorsPerTrack;
00209 }
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 int
00233 Disk::ComputeLatency(int newSector, bool writing)
00234 {
00235 int rotation;
00236 int seek = TimeToSeek(newSector, &rotation);
00237 int timeAfter = stats->totalTicks + seek + rotation;
00238
00239 #ifndef NOTRACKBUF // turn this on if you don't want the track buffer stuff
00240
00241 if ((writing == FALSE) && (seek == 0)
00242 && (((timeAfter - bufferInit) / RotationTime)
00243 > ModuloDiff(newSector, bufferInit / RotationTime))) {
00244 DEBUG('d', "Request latency = %d\n", RotationTime);
00245 return RotationTime;
00246 }
00247 #endif
00248
00249 rotation += ModuloDiff(newSector, timeAfter / RotationTime) * RotationTime;
00250
00251 DEBUG('d', "Request latency = %d\n", seek + rotation + RotationTime);
00252 return(seek + rotation + RotationTime);
00253 }
00254
00255
00256
00257
00258
00259
00260
00261 void
00262 Disk::UpdateLast(int newSector)
00263 {
00264 int rotate;
00265 int seek = TimeToSeek(newSector, &rotate);
00266
00267 if (seek != 0)
00268 bufferInit = stats->totalTicks + seek + rotate;
00269 lastSector = newSector;
00270 DEBUG('d', "Updating last sector = %d, %d\n", lastSector, bufferInit);
00271 }
The University of Southern California does not screen or control the content on this website and thus does not guarantee the accuracy, integrity, or quality of such content. All content on this website is provided by and is the sole responsibility of the person from which such content originated, and such content does not necessarily reflect the opinions of the University administration or the Board of Trustees