annotate src/console.cbc @ 265:f9169495d476

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