-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtty.c
226 lines (199 loc) · 5.78 KB
/
tty.c
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* The terminal driver.
* this driver receives several types of messages:
* DEV_READ: user process read from terminal.
* when read done, a message is fired.
* DEV_WRITE: user process write to terminal.
* when write done, a message is fired.
* HARDWARE_INT: hardware interrupt message
*
* to get all these work, you must:
* 1. write a keyboard interrupt handler, just like our timer interrupt
* FOR THIS, YOU WILL WRITE VERY LITTLE ASSEMBLY CODE!!! CAREFUL!!!
* 2. create a kernel thread start from
* void tty_driver(void);
*
* you may have some problem during merging this code,
* so you must read and understand it before you start.
* */
#include "kernel.h"
#define TTY_BUF_SIZE 256
void init_keymaps(void);
int read_scancode(void);
void update_flags(int);
char get_key(int);
void tty_enqueue(char);
void ring_buffer_enqueue(char*);
void ring_buffer_dequeue(char*);
int ring_buffer_empty(void);
/* the scancode -> ascii code map */
char key_map[256] = {
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
'-', '=', '\b', '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u',
'i', 'o', 'p', '[', ']', '\n', 0, 'a', 's', 'd', 'f', 'g',
'h', 'j', 'k', 'l', ';', '\'', 0, 0, 0, 'z', 'x', 'c', 'v',
'b', 'n', 'm', ',', '.', '/'
};
/* the shifted key map */
char key_map_shift[256] = {
0, 27, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
'_', '+', '\b', '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U',
'I', 'O', 'P', '{', '}', '\n', 0, 'A', 'S', 'D', 'F', 'G',
'H', 'J', 'K', 'L', ':', '"', 0, 0, 0, 'Z', 'X', 'C', 'V',
'B', 'N', 'M', '<', '>', '?'
};
/* functional key scancodes */
#define CTRL_HIT 29
#define CTRL_RELEASE 157
#define ALT_HIT 56
#define ALT_RELEASE 184
#define SHIFT_HIT 42
#define SHIFT_RELEASE 170
#define CAPS_LOCK 58
/* keypressed flags */
static int ctrl = FALSE;
static int alt = FALSE;
static int shift = FALSE;
static int caps = FALSE;
/* IMPORTANT: let this be PID of tty_driver */
void irq1(void) { /* the keyboard interrupt handler */
/* you must see that,
* in this intr handler,
* we did nothing but enqueued a message. */
static struct message m;
m.type = HARDWARE_INT;
ker_int_send(TTY_DRIVER, &m); /* async send */
out_byte(0x20, 0x20);
scheduler();
}
static char tmp_buf[100];
static int read_request = 0;
static int read_stack[NR_PROCESS];
static int read_buffer[NR_PROCESS];
/* the main driver */
void tty_driver(void) {
static struct message m; /* a message for send and receive */
init_keymaps(); /* initialize keymaps */
while (TRUE) { /* forever, wait for message */
receive(&m);
if (m.type == HARDWARE_INT) { /* hardware interrupt */
int i;
int code;
char ch;
code = read_scancode(); /* read something from hardware */
update_flags(code);
ch = get_key(code);
if (ch != 0) {
tty_enqueue(ch); /* if is a valid key, enqueue it */
}
} else if (m.type == DEV_READ) { /* read request */
read_stack[read_request] = m.source; /* just enqueue it */
read_buffer[read_request] = m.p1;
read_request ++;
} else if (m.type == DEV_WRITE) { /* write request */
char ch;
int i, seg = find_proc(m.source)->ds; /* ATTENTION!! YOU MUST IMPLEMENT THIS!!! */
for (i = 0; i < m.p2; i ++) { /* get a byte, a cross-segment copy */
physics_copy(seg,
m.p1 + i,
kernel_segment,
(uint_16)&ch );
printc(ch); /* print it */
if (ch == '\n') printc('\r');
}
m.type = DEV_DONE;
send(m.source, &m); /* send ack */
}
if (read_request > 0 && ring_buffer_empty() == FALSE) { /* if there is buffered text, and someone is reading */
int pid = read_stack[read_request - 1]; /* pop it */
int addr = read_buffer[read_request - 1];
int i, seg = find_proc(pid)->ds; /* same as write */
char *str;
read_request --;
ring_buffer_dequeue(tmp_buf); /* dequeue */
str = tmp_buf;
for (i = 0; *str; i ++, str ++) { /* copy read result */
physics_copy(
kernel_segment,
(uint_16)str, seg, addr + i);
}
m.p1 = i;
m.type = DEV_DONE;
send(pid, &m); /* send ack */
}
}
}
void init_keymaps(void) {
key_map[43] = '\\';
key_map_shift[43] = '|';
key_map[41] = '`';
key_map_shift[41] = '~';
key_map[57] = ' ';
key_map_shift[57] = ' ';
}
int read_scancode(void) {
int code = in_byte(0x60);
int val = in_byte(0x61);
out_byte(0x61, val | 0x80);
out_byte(0x61, val);
return code;
}
void update_flags(int code) {
if (code == CTRL_HIT) ctrl = TRUE;
else if (code == CTRL_RELEASE) ctrl = FALSE;
else if (code == ALT_HIT) alt = TRUE;
else if (code == ALT_RELEASE) alt = FALSE;
else if (code == SHIFT_HIT) shift = TRUE;
else if (code == SHIFT_RELEASE) shift = FALSE;
else if (code == CAPS_LOCK) caps ^= TRUE;
}
char get_key(int code) {
int need_shift = shift;
char ch = key_map[code];
if (code >= 128 || ch == 0) return 0;
if (ch >= 'a' && ch <= 'z' && caps)
need_shift = TRUE;
if (need_shift) return key_map_shift[code];
else return ch;
}
static char ring_buffer[TTY_BUF_SIZE];
static int front = 0, rear = 0;
static char line_buffer[81];
static int cursor = 0;
void ring_buffer_enqueue(char *str) {
for (; *str; str ++) {
ring_buffer[rear] = *str;
rear = (rear + 1) % TTY_BUF_SIZE;
}
ring_buffer[rear] = 0;
rear = (rear + 1) % TTY_BUF_SIZE;
}
void ring_buffer_dequeue(char *str) {
while (ring_buffer[front] != 0) {
*str = ring_buffer[front];
str ++;
front = (front + 1) % TTY_BUF_SIZE;
}
*str = 0;
front = (front + 1) % TTY_BUF_SIZE;
}
int ring_buffer_empty(void) {
return front == rear;
}
void tty_enqueue(char ch) {
if (ch == '\b') {
if (cursor > 0) {
cursor --;
printk("\b \b");
}
} else if (ch == '\n') {
printk("\n");
line_buffer[cursor] = 0;
ring_buffer_enqueue(line_buffer);
cursor = 0;
} else {
if (cursor < 80) {
printk("%c", ch);
line_buffer[cursor ++] = ch;
}
}
}