Explanation for machine/console.cc
Purpose: This has the routines to simulate a serial port
to a console device. A console has input (a keyboard) and output (a display).
These are each simulated by operations on UNIX files. The simulated device
is asynchronous, so we have to invoke the interrupt handler (after a simulated
delay), to signal that a byte has arrived and/or that a written byte has
departed.
-
In our nachos files we some dummy functions here and there because C++
is weird about pointers to member functions.
-
ConsoleReadPoll() and ConsoleWriteDone() are the dummy functions
defined here.
-
Console(), initializes the simulation of a hardware console device.
-
readFile is the UNIX file simulating the keyboard (NULL-> use stdin)
-
writeFile is the UNIX file simulating the display (NULL -> use stdout)
-
readAvail (userprog/progtest.cc)
is the interrupt handler called when a character arrives from the
keyboard
-
writeDone
(userprog/progtest.cc) is the interrupt handler
called when a character has been output, so that it is OK to request the
next char be output.
-
VoidFunctionPtr
(threads/utility.h)
-
If the readFile is NULL means use stdin. readFileNo
( machine/console.h) is
the UNIX file emulating the keyboard.
-
If the readFile was not NULL then it means read from
a file. We call OpenForReadWrite()
(machine/sysdep.cc)
which opens a file for reading and writing. Return the file descriptor,
or error if it doesn't exist.
-
If writeFile is NULL means use stdout writeFileNo
(machine/console.h)
is the UNIX file emulating the display.
-
If the writeFile was not NULL then it means write
to a file. We call OpenFileWrite()
(machine/sysdep.cc)which
opens a file for writing. Create it if it doesn't exist; truncate it if
it does already exist. Return the file descriptor.
-
Then we have to set the stuff to emulate asynchronous
interrupts.
-
writeHandler
(machine/console.h)
is the interrupt handler to call when the PutChar I/O completes
-
readHandler
(machine/console.h)
is the interrupt handler to call when a character arrives from the keyboard
-
handlerArg
(machine/console.h)
is argument to be passed to the interrupt handlers
-
putBusy
(machine/console.h)
is set to FALSE. Is a PutChar operation in progress? If yes then we can't
do another one. So sets to false.
-
incoming
(machine/console.h)
is set to EOF meaning end of file has reached. It Contains the character
to be read, if there is one available. Otherwise it contains EOF.
-
callArg
(machine/console.h)
-
Now after setting up the stuff to emulate asynchronous
interrupts we start polling for incoming packets..
-
~Console() cleans up the console emulation
-
If the file is not empty then Close()
(machine/sysdep.cc) that file.( for both readFileNo
and writeFileNo.
-
CheckCharAvail(), is periodically called to
check if a character is available for input from the simulated keyboard
(ex, has it been typed?). And also only read it in if there is buffer space
for it (if the previous character has been grabbed out of the buffer by
the Nachos kernel). Invoke the "read" interrupt handler, once the character
has been put into the buffer.
-
WriteDone() Internal routine called when it
is time to invoke the interrupt handler to tell the Nachos kernel that
the output character has completed.
-
GetChar(), reads a character from the input
buffer, if there is any there. It either returns the character, or
EOF if none buffered.
-
PutChar(), writes a character to the simulated
display, schedule an interrupt to occur in the future, and return.