00001 // console.h 00002 // Data structures to simulate the behavior of a terminal 00003 // I/O device. A terminal has two parts -- a keyboard input, 00004 // and a display output, each of which produces/accepts 00005 // characters sequentially. 00006 // 00007 // The console hardware device is asynchronous. When a character is 00008 // written to the device, the routine returns immediately, and an 00009 // interrupt handler is called later when the I/O completes. 00010 // For reads, an interrupt handler is called when a character arrives. 00011 // 00012 // In either case, the serial line connecting the computer 00013 // to the console has limited bandwidth (like a modem!), and so 00014 // each character takes measurable time. 00015 // 00016 // The user of the device registers itself to be called "back" when 00017 // the read/write interrupts occur. There is a separate interrupt 00018 // for read and write, and the device is "duplex" -- a character 00019 // can be outgoing and incoming at the same time. 00020 // 00021 // DO NOT CHANGE -- part of the machine emulation 00022 // 00023 // Copyright (c) 1992-1996 The Regents of the University of California. 00024 // All rights reserved. See copyright.h for copyright notice and limitation 00025 // of liability and disclaimer of warranty provisions. 00026 00027 #ifndef CONSOLE_H 00028 #define CONSOLE_H 00029 00030 #include "copyright.h" 00031 #include "utility.h" 00032 #include "callback.h" 00033 00034 // The following two classes define the input (and output) side of a 00035 // hardware console device. Input (and output) to the device is simulated 00036 // by reading (and writing) to the UNIX file "readFile" (and "writeFile"). 00037 // 00038 // Since input (and output) to the device is asynchronous, the interrupt 00039 // handler "callWhenAvail" is called when a character has arrived to be 00040 // read in (and "callWhenDone" is called when an output character has been 00041 // "put" so that the next character can be written). 00042 // 00043 // In practice, usually a single hardware thing that does both 00044 // serial input and serial output. But conceptually simpler to 00045 // use two objects. 00046 00047 class ConsoleInput : public CallBackObj { 00048 public: 00049 ConsoleInput(char *readFile, CallBackObj *toCall); 00050 // initialize hardware console input 00051 ~ConsoleInput(); // clean up console emulation 00052 00053 char GetChar(); // Poll the console input. If a char is 00054 // available, return it. Otherwise, return EOF. 00055 // "callWhenAvail" is called whenever there is 00056 // a char to be gotten 00057 00058 void CallBack(); // Invoked when a character arrives 00059 // from the keyboard. 00060 00061 private: 00062 int readFileNo; // UNIX file emulating the keyboard 00063 CallBackObj *callWhenAvail; // Interrupt handler to call when 00064 // there is a char to be read 00065 char incoming; // Contains the character to be read, 00066 // if there is one available. 00067 // Otherwise contains EOF. 00068 }; 00069 00070 class ConsoleOutput : public CallBackObj { 00071 public: 00072 ConsoleOutput(char *writeFile, CallBackObj *toCall); 00073 // initialize hardware console output 00074 ~ConsoleOutput(); // clean up console emulation 00075 00076 void PutChar(char ch); // Write "ch" to the console display, 00077 // and return immediately. "callWhenDone" 00078 // will called when the I/O completes. 00079 00080 void CallBack(); // Invoked when next character can be put 00081 // out to the display. 00082 00083 private: 00084 int writeFileNo; // UNIX file emulating the display 00085 CallBackObj *callWhenDone; // Interrupt handler to call when 00086 // the next char can be put 00087 bool putBusy; // Is a PutChar operation in progress? 00088 // If so, you can't do another one! 00089 }; 00090 00091 #endif // CONSOLE_H