nachos.machine
Class Disk

java.lang.Object
  extended by nachos.machine.Disk

public class Disk
extends java.lang.Object

This class defines a physical disk I/O device. The disk has a single surface, split up into "tracks", and each track split up into "sectors" (the same number of sectors on each track, and each sector has the same number of bytes of storage). Addressing is by sector number -- each sector on the disk is given a unique number: track * SectorsPerTrack + offset within a track. As with other I/O devices, the raw physical disk is an asynchronous device -- requests to read or write portions of the disk return immediately, and an interrupt is invoked later to signal that the operation completed. The physical disk is in fact simulated via operations on a UNIX file. To make life a little more realistic, the simulated time for each operation reflects a "track buffer" -- RAM to store the contents of the current track as the disk head passes by. The idea is that the disk always transfers to the track buffer, in case that data is requested later on. This has the benefit of eliminating the need for "skip-sector" scheduling -- a read request which comes in shortly after the head has passed the beginning of the sector can be satisfied more quickly, because its contents are in the track buffer. Most disks these days now come with a track buffer.


Field Summary
static int NumSectors
          Total number of sectors per disk.
static int NumTracks
          Number of tracks per disk.
static int SectorSize
          Number of bytes per disk sector.
static int SectorsPerTrack
          Number of sector per disk track.
 
Constructor Summary
Disk(java.lang.String name, InterruptHandler callWhenDone)
          Create a simulated disk.
 
Method Summary
static int bytesToInt(byte[] buffer, int pos)
          Utility method to deserialize a sequence of bytes into an integer.
static void intToBytes(int val, byte[] buffer, int pos)
          Utility method to serialize an integer into a sequence of bytes.
static void printSector(boolean writing, int sector, byte[] data)
          Dump the data in a disk read/write request, for debugging.
 void readRequest(int sectorNumber, byte[] data, int index)
          Called to submit a request to read a single disk sector.
 void writeRequest(int sectorNumber, byte[] data, int index)
          Called to submit a request to write a single disk sector.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SectorSize

public static final int SectorSize
Number of bytes per disk sector.

See Also:
Constant Field Values

SectorsPerTrack

public static final int SectorsPerTrack
Number of sector per disk track.

See Also:
Constant Field Values

NumTracks

public static final int NumTracks
Number of tracks per disk.

See Also:
Constant Field Values

NumSectors

public static final int NumSectors
Total number of sectors per disk.

See Also:
Constant Field Values
Constructor Detail

Disk

public Disk(java.lang.String name,
            InterruptHandler callWhenDone)
Create a simulated disk. Open the UNIX file (creating it if it doesn't exist), and check the magic number to make sure it's ok to treat it as Nachos disk storage.

Parameters:
name - Text name of the file simulating the Nachos disk.
callWhenDone - Interrupt handler to be notified each time disk read/write request completes.
Method Detail

readRequest

public void readRequest(int sectorNumber,
                        byte[] data,
                        int index)
Called to submit a request to read a single disk sector. The request is sent to the disk and the method returns immediately. The disk interrupt handler will be called back later, to notify the caller that the requested operation has completed. Note that a disk only allows an entire sector to be read, not part of a sector.

Parameters:
sectorNumber - The disk sector to read.
data - The buffer to hold the incoming bytes.

writeRequest

public void writeRequest(int sectorNumber,
                         byte[] data,
                         int index)
Called to submit a request to write a single disk sector. The request is sent to the disk and the method returns immediately. The disk interrupt handler will be called back later, to notify the caller that the requested operation has completed. Note that a disk only allows an entire sector to be written, not part of a sector.

Parameters:
sectorNumber - The disk sector to write.
data - The bytes to be written.

printSector

public static void printSector(boolean writing,
                               int sector,
                               byte[] data)
Dump the data in a disk read/write request, for debugging.

Parameters:
writing - True if the request is a write request.
sector - The sector number in the request.
data - The buffer for data read/written in the request.

intToBytes

public static void intToBytes(int val,
                              byte[] buffer,
                              int pos)
Utility method to serialize an integer into a sequence of bytes.

Parameters:
val - The integer value to be serialized.
buffer - The buffer into which the value is to be serialized.
pos - Starting offset from the beginning of the buffer at which the serialized bytes representing the value are to be placed.

bytesToInt

public static int bytesToInt(byte[] buffer,
                             int pos)
Utility method to deserialize a sequence of bytes into an integer.

Parameters:
buffer - The buffer from which the value is to be deserialized.
pos - Starting offset from the beginning of the buffer at which the serialized bytes representing the value exist.
Returns:
The integer value represented by the bytes in the buffer.