annotate src/console.c @ 32:96af12a50fdb

fix cbc_read
author mir3636
date Tue, 22 Jan 2019 15:48:35 +0900
parents 96a5833d0d82
children 7a63dacab7f8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 // Console input and output.
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 // Input is from the keyboard or serial port.
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 // Output is written to the screen and serial port.
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 #include "types.h"
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 #include "defs.h"
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 #include "param.h"
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 #include "spinlock.h"
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 #include "fs.h"
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 #include "file.h"
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 #include "memlayout.h"
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 #include "mmu.h"
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 #include "proc.h"
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14
32
96af12a50fdb fix cbc_read
mir3636
parents: 31
diff changeset
15 __code cbc_consoleread1 ();
96af12a50fdb fix cbc_read
mir3636
parents: 31
diff changeset
16 __code cbc_consoleread2 ();
31
mir3636
parents: 30
diff changeset
17
0
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 static void consputc (int);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 static int panicked = 0;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 static struct {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 struct spinlock lock;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 int locking;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 } cons;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 static void printint (int xx, int base, int sign)
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 static char digits[] = "0123456789abcdef";
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 char buf[16];
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 int i;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 uint x;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 if (sign && (sign = xx < 0)) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 x = -xx;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 } else {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 x = xx;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 i = 0;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 do {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 buf[i++] = digits[x % base];
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 } while ((x /= base) != 0);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 if (sign) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 buf[i++] = '-';
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 while (--i >= 0) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 consputc(buf[i]);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 //PAGEBREAK: 50
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 // Print to the console. only understands %d, %x, %p, %s.
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 void cprintf (char *fmt, ...)
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 int i, c, locking;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 uint *argp;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 char *s;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 locking = cons.locking;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 if (locking) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 acquire(&cons.lock);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 if (fmt == 0) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 panic("null fmt");
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 argp = (uint*) (void*) (&fmt + 1);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 for (i = 0; (c = fmt[i] & 0xff) != 0; i++) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 if (c != '%') {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 consputc(c);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 continue;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 c = fmt[++i] & 0xff;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 if (c == 0) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 break;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 switch (c) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 case 'd':
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 printint(*argp++, 10, 1);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 break;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 case 'x':
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 case 'p':
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 printint(*argp++, 16, 0);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 break;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 case 's':
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 if ((s = (char*) *argp++) == 0) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 s = "(null)";
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
101
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 for (; *s; s++) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 consputc(*s);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 break;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
106
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 case '%':
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 consputc('%');
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 break;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
110
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 default:
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 // Print unknown % sequence to draw attention.
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 consputc('%');
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
114 consputc(c);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 break;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
116 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
118
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 if (locking) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 release(&cons.lock);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
123
30
mir3636
parents: 29
diff changeset
124 __code cbc_panic (char *s)
mir3636
parents: 29
diff changeset
125 {
mir3636
parents: 29
diff changeset
126 cli();
mir3636
parents: 29
diff changeset
127
mir3636
parents: 29
diff changeset
128 cons.locking = 0;
mir3636
parents: 29
diff changeset
129
mir3636
parents: 29
diff changeset
130 cprintf("cpu%d: panic: ", cpu->id);
mir3636
parents: 29
diff changeset
131
mir3636
parents: 29
diff changeset
132 show_callstk(s);
mir3636
parents: 29
diff changeset
133 panicked = 1; // freeze other CPU
mir3636
parents: 29
diff changeset
134
mir3636
parents: 29
diff changeset
135 while (1)
mir3636
parents: 29
diff changeset
136 ;
mir3636
parents: 29
diff changeset
137 }
mir3636
parents: 29
diff changeset
138
0
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
139 void panic (char *s)
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
140 {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
141 cli();
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
142
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
143 cons.locking = 0;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
144
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
145 cprintf("cpu%d: panic: ", cpu->id);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
146
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
147 show_callstk(s);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
148 panicked = 1; // freeze other CPU
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
149
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
150 while (1)
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
151 ;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
152 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
153
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
154 //PAGEBREAK: 50
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
155 #define BACKSPACE 0x100
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
156 #define CRTPORT 0x3d4
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
157
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
158 void consputc (int c)
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
159 {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
160 if (panicked) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
161 cli();
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
162 while (1)
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
163 ;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
164 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
165
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
166 if (c == BACKSPACE) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
167 uartputc('\b');
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
168 uartputc(' ');
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
169 uartputc('\b');
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
170 } else {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
171 uartputc(c);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
172 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
173
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
174 // cgaputc(c);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
175 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
176
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
177 #define INPUT_BUF 512
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
178 struct {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
179 struct spinlock lock;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
180 char buf[INPUT_BUF];
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
181 uint r; // Read index
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
182 uint w; // Write index
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
183 uint e; // Edit index
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
184 } input;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
185
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
186 #define C(x) ((x)-'@') // Control-x
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
187 void consoleintr (int (*getc) (void))
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
188 {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
189 int c;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
190
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
191 acquire(&input.lock);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
192
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 while ((c = getc()) >= 0) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
194 switch (c) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
195 case C('P'): // Process listing.
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
196 procdump();
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
197 break;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
198
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
199 case C('U'): // Kill line.
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
200 while ((input.e != input.w) && (input.buf[(input.e - 1) % INPUT_BUF] != '\n')) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
201 input.e--;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
202 consputc(BACKSPACE);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
203 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
204
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
205 break;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
206
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
207 case C('H'):
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
208 case '\x7f': // Backspace
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
209 if (input.e != input.w) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
210 input.e--;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
211 consputc(BACKSPACE);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
212 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
213
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
214 break;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
215
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
216 default:
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
217 if ((c != 0) && (input.e - input.r < INPUT_BUF)) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
218 c = (c == '\r') ? '\n' : c;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
219
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
220 input.buf[input.e++ % INPUT_BUF] = c;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
221 consputc(c);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
222
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
223 if (c == '\n' || c == C('D') || input.e == input.r + INPUT_BUF) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
224 input.w = input.e;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
225 wakeup(&input.r);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
226 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
227 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
228
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
229 break;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
230 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
231 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
232
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
233 release(&input.lock);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
234 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
235
32
96af12a50fdb fix cbc_read
mir3636
parents: 31
diff changeset
236 __code cbc_consoleread2 ()
31
mir3636
parents: 30
diff changeset
237 {
mir3636
parents: 30
diff changeset
238 struct inode *ip = proc->cbc_arg.cbc_console_arg.ip;
32
96af12a50fdb fix cbc_read
mir3636
parents: 31
diff changeset
239 __code(*next)(int ret) = proc->cbc_arg.cbc_console_arg.next;
31
mir3636
parents: 30
diff changeset
240 if (input.r == input.w) {
mir3636
parents: 30
diff changeset
241 if (proc->killed) {
mir3636
parents: 30
diff changeset
242 release(&input.lock);
mir3636
parents: 30
diff changeset
243 ilock(ip);
mir3636
parents: 30
diff changeset
244 goto next(-1);
mir3636
parents: 30
diff changeset
245 }
mir3636
parents: 30
diff changeset
246 goto cbc_sleep(&input.r, &input.lock, cbc_consoleread2);
mir3636
parents: 30
diff changeset
247 }
32
96af12a50fdb fix cbc_read
mir3636
parents: 31
diff changeset
248 goto cbc_consoleread1();
31
mir3636
parents: 30
diff changeset
249 }
mir3636
parents: 30
diff changeset
250
32
96af12a50fdb fix cbc_read
mir3636
parents: 31
diff changeset
251 __code cbc_consoleread1 ()
24
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
252 {
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
253 int cont = 1;
26
Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 24
diff changeset
254 int n = proc->cbc_arg.cbc_console_arg.n;
Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 24
diff changeset
255 int target = proc->cbc_arg.cbc_console_arg.target;
Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 24
diff changeset
256 char* dst = proc->cbc_arg.cbc_console_arg.dst;
Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 24
diff changeset
257 struct inode *ip = proc->cbc_arg.cbc_console_arg.ip;
31
mir3636
parents: 30
diff changeset
258 struct file *f = proc->cbc_arg.cbc_console_arg.f;
32
96af12a50fdb fix cbc_read
mir3636
parents: 31
diff changeset
259 __code(*next)(int ret) = proc->cbc_arg.cbc_console_arg.next;
24
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
260
26
Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents: 24
diff changeset
261 int c = input.buf[input.r++ % INPUT_BUF];
24
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
262
31
mir3636
parents: 30
diff changeset
263 if (c == C('D')) { // EOF
mir3636
parents: 30
diff changeset
264 if (n < target) {
mir3636
parents: 30
diff changeset
265 // Save ^D for next time, to make sure
mir3636
parents: 30
diff changeset
266 // caller gets a 0-byte result.
mir3636
parents: 30
diff changeset
267 input.r--;
mir3636
parents: 30
diff changeset
268 }
mir3636
parents: 30
diff changeset
269 cont = 0;
mir3636
parents: 30
diff changeset
270 }
24
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
271
31
mir3636
parents: 30
diff changeset
272 *dst++ = c;
mir3636
parents: 30
diff changeset
273 --n;
24
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
274
31
mir3636
parents: 30
diff changeset
275 if (c == '\n') {
mir3636
parents: 30
diff changeset
276 cont = 0;
mir3636
parents: 30
diff changeset
277 }
24
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
278
32
96af12a50fdb fix cbc_read
mir3636
parents: 31
diff changeset
279 if (cont == 1) {
96af12a50fdb fix cbc_read
mir3636
parents: 31
diff changeset
280 if (n > 0) {
96af12a50fdb fix cbc_read
mir3636
parents: 31
diff changeset
281 proc->cbc_arg.cbc_console_arg.n = n;
96af12a50fdb fix cbc_read
mir3636
parents: 31
diff changeset
282 proc->cbc_arg.cbc_console_arg.target = target;
96af12a50fdb fix cbc_read
mir3636
parents: 31
diff changeset
283 proc->cbc_arg.cbc_console_arg.dst = dst;
96af12a50fdb fix cbc_read
mir3636
parents: 31
diff changeset
284 proc->cbc_arg.cbc_console_arg.ip = ip;
96af12a50fdb fix cbc_read
mir3636
parents: 31
diff changeset
285 proc->cbc_arg.cbc_console_arg.f = f;
96af12a50fdb fix cbc_read
mir3636
parents: 31
diff changeset
286 proc->cbc_arg.cbc_console_arg.next = next;
96af12a50fdb fix cbc_read
mir3636
parents: 31
diff changeset
287 goto cbc_sleep(&input.r, &input.lock, cbc_consoleread2);
96af12a50fdb fix cbc_read
mir3636
parents: 31
diff changeset
288 }
24
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
289 }
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
290
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
291 release(&input.lock);
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
292 ilock(ip);
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
293
31
mir3636
parents: 30
diff changeset
294 int r = target - n;
mir3636
parents: 30
diff changeset
295
mir3636
parents: 30
diff changeset
296 if (r > 0)
mir3636
parents: 30
diff changeset
297 f->off += r;
mir3636
parents: 30
diff changeset
298 iunlock(f->ip);
mir3636
parents: 30
diff changeset
299
mir3636
parents: 30
diff changeset
300 //goto next(target - n);
mir3636
parents: 30
diff changeset
301
32
96af12a50fdb fix cbc_read
mir3636
parents: 31
diff changeset
302 goto next(r);
24
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
303 }
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
304
31
mir3636
parents: 30
diff changeset
305 __code cbc_consoleread (struct inode *ip, char *dst, int n, struct file *f, __code(*next)(int ret))
29
mir3636
parents: 27
diff changeset
306 {
mir3636
parents: 27
diff changeset
307 uint target;
mir3636
parents: 27
diff changeset
308
mir3636
parents: 27
diff changeset
309 iunlock(ip);
mir3636
parents: 27
diff changeset
310
mir3636
parents: 27
diff changeset
311 target = n;
mir3636
parents: 27
diff changeset
312 acquire(&input.lock);
mir3636
parents: 27
diff changeset
313
31
mir3636
parents: 30
diff changeset
314 if (n > 0) {
mir3636
parents: 30
diff changeset
315 proc->cbc_arg.cbc_console_arg.n = n;
mir3636
parents: 30
diff changeset
316 proc->cbc_arg.cbc_console_arg.target = target;
mir3636
parents: 30
diff changeset
317 proc->cbc_arg.cbc_console_arg.dst = dst;
mir3636
parents: 30
diff changeset
318 proc->cbc_arg.cbc_console_arg.ip = ip;
mir3636
parents: 30
diff changeset
319 proc->cbc_arg.cbc_console_arg.f = f;
mir3636
parents: 30
diff changeset
320 proc->cbc_arg.cbc_console_arg.next = next;
mir3636
parents: 30
diff changeset
321 if (input.r == input.w) {
29
mir3636
parents: 27
diff changeset
322 if (proc->killed) {
mir3636
parents: 27
diff changeset
323 release(&input.lock);
mir3636
parents: 27
diff changeset
324 ilock(ip);
mir3636
parents: 27
diff changeset
325 goto next(-1);
mir3636
parents: 27
diff changeset
326 }
mir3636
parents: 27
diff changeset
327
31
mir3636
parents: 30
diff changeset
328 goto cbc_sleep(&input.r, &input.lock, cbc_consoleread2);
29
mir3636
parents: 27
diff changeset
329 }
31
mir3636
parents: 30
diff changeset
330 goto cbc_consoleread1(0);
29
mir3636
parents: 27
diff changeset
331 }
mir3636
parents: 27
diff changeset
332 }
mir3636
parents: 27
diff changeset
333
0
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
334 int consoleread (struct inode *ip, char *dst, int n)
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
335 {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
336 uint target;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
337 int c;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
338
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
339 iunlock(ip);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
340
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
341 target = n;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
342 acquire(&input.lock);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
343
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
344 while (n > 0) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
345 while (input.r == input.w) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
346 if (proc->killed) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
347 release(&input.lock);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
348 ilock(ip);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
349 return -1;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
350 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
351
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
352 sleep(&input.r, &input.lock);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
353 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
354
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
355 c = input.buf[input.r++ % INPUT_BUF];
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
356
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
357 if (c == C('D')) { // EOF
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
358 if (n < target) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
359 // Save ^D for next time, to make sure
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
360 // caller gets a 0-byte result.
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
361 input.r--;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
362 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
363
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
364 break;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
365 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
366
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
367 *dst++ = c;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
368 --n;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
369
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
370 if (c == '\n') {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
371 break;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
372 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
373 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
374
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
375 release(&input.lock);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
376 ilock(ip);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
377
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
378 return target - n;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
379 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
380
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
381 int consolewrite (struct inode *ip, char *buf, int n)
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
382 {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
383 int i;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
384
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
385 iunlock(ip);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
386
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
387 acquire(&cons.lock);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
388
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
389 for (i = 0; i < n; i++) {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
390 consputc(buf[i] & 0xff);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
391 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
392
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
393 release(&cons.lock);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
394
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
395 ilock(ip);
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
396
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
397 return n;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
398 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
399
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
400 void consoleinit (void)
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
401 {
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
402 initlock(&cons.lock, "console");
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
403 initlock(&input.lock, "input");
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
404
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
405 devsw[CONSOLE].write = consolewrite;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
406 devsw[CONSOLE].read = consoleread;
27
mir3636
parents: 26
diff changeset
407 //cbc_devsw[CONSOLE].write = cbc_consolewrite;
24
36bd61f5c847 rewrite sys_read cbc
mir3636
parents: 0
diff changeset
408 cbc_devsw[CONSOLE].read = cbc_consoleread;
0
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
409
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
410 cons.locking = 1;
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
411 }
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
diff changeset
412