forked from NachoSquad/nachos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StandardConsole.java
161 lines (127 loc) · 3.66 KB
/
StandardConsole.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// PART OF THE MACHINE SIMULATION. DO NOT CHANGE.
package nachos.machine;
import nachos.security.*;
import java.io.IOException;
/**
* A text-based console that uses System.in and System.out.
*/
public class StandardConsole implements SerialConsole {
/**
* Allocate a new standard console.
*
* @param privilege encapsulates privileged access to the Nachos
* machine.
*/
public StandardConsole(Privilege privilege) {
System.out.print(" console");
this.privilege = privilege;
receiveInterrupt = new Runnable() {
public void run() { receiveInterrupt(); }
};
sendInterrupt = new Runnable() {
public void run() { sendInterrupt(); }
};
scheduleReceiveInterrupt();
}
public final void setInterruptHandlers(Runnable receiveInterruptHandler,
Runnable sendInterruptHandler) {
this.receiveInterruptHandler = receiveInterruptHandler;
this.sendInterruptHandler = sendInterruptHandler;
}
private void scheduleReceiveInterrupt() {
privilege.interrupt.schedule(Stats.ConsoleTime, "console read",
receiveInterrupt);
}
/**
* Attempt to read a byte from the object backing this console.
*
* @return the byte read, or -1 of no data is available.
*/
protected int in() {
try {
if (System.in.available() <= 0)
return -1;
return System.in.read();
}
catch (IOException e) {
return -1;
}
}
private int translateCharacter(int c) {
// translate win32 0x0D 0x0A sequence to single newline
if (c == 0x0A && prevCarriageReturn) {
prevCarriageReturn = false;
return -1;
}
prevCarriageReturn = (c == 0x0D);
// invalid if non-ASCII
if (c >= 0x80)
return -1;
// backspace characters
else if (c == 0x04 || c == 0x08 || c == 0x19 || c == 0x1B || c == 0x7F)
return '\b';
// if normal ASCII range, nothing to do
else if (c >= 0x20)
return c;
// newline characters
else if (c == 0x0A || c == 0x0D)
return '\n';
// everything else is invalid
else
return -1;
}
private void receiveInterrupt() {
Lib.assertTrue(incomingKey == -1);
incomingKey = translateCharacter(in());
if (incomingKey == -1) {
scheduleReceiveInterrupt();
}
else {
privilege.stats.numConsoleReads++;
if (receiveInterruptHandler != null)
receiveInterruptHandler.run();
}
}
public final int readByte() {
int key = incomingKey;
if (incomingKey != -1) {
incomingKey = -1;
scheduleReceiveInterrupt();
}
return key;
}
private void scheduleSendInterrupt() {
privilege.interrupt.schedule(Stats.ConsoleTime, "console write",
sendInterrupt);
}
/**
* Write a byte to the object backing this console.
*
* @param value the byte to write.
*/
protected void out(int value) {
System.out.write(value);
System.out.flush();
}
private void sendInterrupt() {
Lib.assertTrue(outgoingKey != -1);
out(outgoingKey);
outgoingKey = -1;
privilege.stats.numConsoleWrites++;
if (sendInterruptHandler != null)
sendInterruptHandler.run();
}
public final void writeByte(int value) {
if (outgoingKey == -1)
scheduleSendInterrupt();
outgoingKey = value&0xFF;
}
private Privilege privilege = null;
private Runnable receiveInterrupt;
private Runnable sendInterrupt;
private Runnable receiveInterruptHandler = null;
private Runnable sendInterruptHandler = null;
private int incomingKey = -1;
private int outgoingKey = -1;
private boolean prevCarriageReturn = false;
}