This source file includes following definitions.
- _sputc
- _sprintk
- _vsprintk
- _printk
- _vprintk
- _cvt
- _disable_interrupts
- CRT_getc
- s
- NS16550_init
- c
- c
- _cnpause
- _disable_interrupts
- _printk
- _printk
- abort
- s
- _disable_interrupts
- printk
- printk
- s
- video_on
- CRT_init
- kbd
- scankbd
- kbdreset
- CRT_getc
- CRT_test
- _dump_buf_with_offset
- _dump_buf
- dump_buf_with_offset
- dump_buf
1 #define FALSE 0
2 #define TRUE 1
3 #include <stdarg.h>
4
5 extern void cnputc(char c);
6 char cngetc(void);
7 int cntstc(void);
8 void _cnpause(void);
9 void cnpause(void);
10 void video_on(void);
11 int CRT_init(void);
12 int kbd(int noblock);
13 int scankbd(void);
14 static char *_sprintk_ptr;
15 void kbdreset(void);
16 int CRT_test(void);
17 int CRT_putc(int , unsigned char );
18
19 int CRT_getc(void);
20 int _vprintk( int (*putc)(), const char *fmt0, va_list ap);
21 static _cvt(unsigned long val, char *buf, long radix, char *digits);
22 static void cursor(void);
23 static void initscreen(void );
24
25
26
27
28
29 struct NS16550
30 {
31 unsigned char rbr;
32 unsigned char ier;
33 unsigned char fcr;
34 unsigned char lcr;
35 unsigned char mcr;
36 unsigned char lsr;
37 unsigned char msr;
38 unsigned char scr;
39 };
40
41 #define thr rbr
42 #define iir fcr
43 #define dll rbr
44 #define dlm ier
45
46 #define LSR_DR 0x01
47 #define LSR_OE 0x02
48 #define LSR_PE 0x04
49 #define LSR_FE 0x08
50 #define LSR_BI 0x10
51 #define LSR_THRE 0x20
52 #define LSR_TEMT 0x40
53 #define LSR_ERR 0x80
54
55 #define COM1 0x800003F8
56 #define COM2 0x800002F8
57
58 typedef struct NS16550 *NS16550_t;
59
60 const NS16550_t COM_PORTS[] = { COM1,COM2};
61
62 volatile struct NS16550 *NS16550_init(int chan);
63 void NS16550_putc(volatile struct NS16550 *com_port, unsigned char c);
64 unsigned char NS16550_getc(volatile struct NS16550 *com_port);
65 static _sputc(char c)
66 {
67 *_sprintk_ptr++ = c;
68 *_sprintk_ptr = '\0';
69 }
70
71 _sprintk(char *buf, char const *fmt, ...)
72 {
73 int ret;
74 va_list ap;
75
76 va_start(ap, fmt);
77 _sprintk_ptr = buf;
78 ret = _vprintk(_sputc, fmt, ap);
79 va_end(ap);
80 return (ret);
81 }
82
83 _vsprintk(char *buf, char const *fmt, va_list ap)
84 {
85 int ret;
86
87 _sprintk_ptr = buf;
88 ret = _vprintk(_sputc, fmt, ap);
89 return (ret);
90 }
91
92 _printk(char const *fmt, ...)
93 {
94 int ret;
95 va_list ap;
96
97 va_start(ap, fmt);
98 ret = _vprintk(cnputc, fmt, ap);
99 va_end(ap);
100 return (ret);
101 }
102
103 #define is_digit(c) ((c >= '0') && (c <= '9'))
104
105 int _vprintk( int (*putc)(), const char *fmt0, va_list ap)
106 {
107 char c, sign, *cp;
108 int left_prec, right_prec, zero_fill, length, pad, pad_on_right;
109 char buf[32];
110 long val;
111 while (c = *fmt0++)
112 {
113 if (c == '%')
114 {
115 c = *fmt0++;
116 left_prec = right_prec = pad_on_right = 0;
117 if (c == '-')
118 {
119 c = *fmt0++;
120 pad_on_right++;
121 }
122 if (c == '0')
123 {
124 zero_fill = TRUE;
125 c = *fmt0++;
126 } else
127 {
128 zero_fill = FALSE;
129 }
130 while (is_digit(c))
131 {
132 left_prec = (left_prec * 10) + (c - '0');
133 c = *fmt0++;
134 }
135 if (c == '.')
136 {
137 c = *fmt0++;
138 zero_fill++;
139 while (is_digit(c))
140 {
141 right_prec = (right_prec * 10) + (c - '0');
142 c = *fmt0++;
143 }
144 } else
145 {
146 right_prec = left_prec;
147 }
148 sign = '\0';
149 switch (c)
150 {
151 case 'd':
152 case 'x':
153 case 'X':
154 val = va_arg(ap, long);
155 switch (c)
156 {
157 case 'd':
158 if (val < 0)
159 {
160 sign = '-';
161 val = -val;
162 }
163 length = _cvt(val, buf, 10, "0123456789");
164 break;
165 case 'x':
166 length = _cvt(val, buf, 16, "0123456789abcdef");
167 break;
168 case 'X':
169 length = _cvt(val, buf, 16, "0123456789ABCDEF");
170 break;
171 }
172 cp = buf;
173 break;
174 case 's':
175 cp = va_arg(ap, char *);
176 length = strlen(cp);
177 break;
178 case 'c':
179 c = va_arg(ap, long );
180 (*putc)(c);
181 continue;
182 default:
183 (*putc)('?');
184 }
185 pad = left_prec - length;
186 if (sign != '\0')
187 {
188 pad--;
189 }
190 if (zero_fill)
191 {
192 c = '0';
193 if (sign != '\0')
194 {
195 (*putc)(sign);
196 sign = '\0';
197 }
198 } else
199 {
200 c = ' ';
201 }
202 if (!pad_on_right)
203 {
204 while (pad-- > 0)
205 {
206 (*putc)(c);
207 }
208 }
209 if (sign != '\0')
210 {
211 (*putc)(sign);
212 }
213 while (length-- > 0)
214 {
215 (*putc)(c = *cp++);
216 if (c == '\n')
217 {
218 (*putc)('\r');
219 }
220 }
221 if (pad_on_right)
222 {
223 while (pad-- > 0)
224 {
225 (*putc)(c);
226 }
227 }
228 } else
229 {
230 (*putc)(c);
231 if (c == '\n')
232 {
233 (*putc)('\r');
234 }
235 }
236 }
237 }
238
239 static _cvt(unsigned long val, char *buf, long radix, char *digits)
240 {
241 char temp[80];
242 char *cp = temp;
243 int length = 0;
244 if (val == 0)
245 {
246 *cp++ = '0';
247 } else
248 while (val)
249 {
250 *cp++ = digits[val % radix];
251 val /= radix;
252 }
253 while (cp != temp)
254 {
255 *buf++ = *--cp;
256 length++;
257 }
258 *buf = '\0';
259 return (length);
260 }
261
262
263
264
265 typedef const (*proc)();
266 typedef int dev_t;
267
268 #define FALSE 0
269 #define TRUE 1
270
271 #define CRT_PORT 0x3D4
272
273 static int init = FALSE;
274 static int is_crt = 0;
275 static int port = 0;
276 static int line_num = 0;
277 #define MAX_LINES 24
278
279 char cngetc(void)
280 {
281 int s = _disable_interrupts();
282 char c = '\0';
283 if (port == CRT_PORT)
284 {
285
286 c = CRT_getc();
287 } else
288 if (port)
289 {
290 c = NS16550_getc((struct NS16550 *)port);
291 }
292 _enable_interrupts(s);
293 return (c);
294 }
295
296 int cntstc(void)
297 {
298 return (0);
299 }
300
301 char _cn_trace[1024];
302 char *_cnp = _cn_trace;
303
304
305
306
307 void
308 cnputc(char c)
309 {
310 *_cnp++ = c;
311 if (_cnp == &_cn_trace[sizeof(_cn_trace)])
312 {
313 _cnp = _cn_trace;
314 }
315 if (!init)
316 {
317 if (is_crt = CRT_init())
318 {
319 port = CRT_PORT;
320 } else
321 {
322 port =(int) NS16550_init(0);
323 }
324 init = TRUE;
325 }
326 if (port == CRT_PORT)
327 {
328 CRT_putc(port, c);
329 } else
330 if (port)
331 {
332 NS16550_putc((struct NS16550 *)port, c);
333 }
334 if (c == '\n')
335 {
336 if (line_num >= 0) line_num++;
337 }
338 if (c == '\r')
339 {
340 if (line_num >= MAX_LINES)
341 {
342 line_num = 0;
343 _cnpause();
344 }
345 }
346 }
347
348 void _cnpause(void)
349 {
350 int c;
351 int s = _disable_interrupts();
352 _printk("-- More? ");
353 while ((c = cngetc()) == 0);
354 _printk("\r \r");
355 if (c == ' ')
356 {
357 line_num = 0;
358 } else
359 if (c == 'n')
360 {
361 line_num = -1;
362 } else
363 if ((c == '\r') || (c == '\n'))
364 {
365 line_num = MAX_LINES-1;
366 } else
367 if (c == 0x03)
368 {
369 abort();
370 } else
371 {
372 line_num = MAX_LINES - (MAX_LINES/3);
373 }
374 _enable_interrupts(s);
375 }
376
377 void cnpause(void)
378 {
379 int c;
380 int s = _disable_interrupts();
381 printk("-- More? ");
382 while ((c = cngetc()) == 0);
383 printk("\r \r");
384 _enable_interrupts(s);
385 }
386
387 volatile struct NS16550 *NS16550_init(int chan)
388 {
389 volatile struct NS16550 *com_port;
390 volatile unsigned char xx;
391 com_port = (struct NS16550 *) COM_PORTS[chan];
392
393 com_port->lcr = 0x00;
394 com_port->ier = 0xFF;
395 #if 0
396 if (com_port->ier != 0x0F) return ((struct NS16550 *)0);
397 #endif
398 com_port->ier = 0x00;
399 com_port->lcr = 0x80;
400 com_port->dll = 12;
401 com_port->dlm = 12 >> 8;
402 com_port->lcr = 0x03;
403 com_port->mcr = 0x03;
404 com_port->fcr = 0x07;
405 return (com_port);
406 }
407
408
409 void NS16550_putc(volatile struct NS16550 *com_port, unsigned char c)
410 {
411 volatile int i;
412 while ((com_port->lsr & LSR_THRE) == 0) ;
413 com_port->thr = c;
414 }
415
416
417 unsigned char NS16550_getc(volatile struct NS16550 *com_port)
418 {
419 while ((com_port->lsr & LSR_DR) == 0) ;
420 return (com_port->rbr);
421 }
422
423 NS16550_test(volatile struct NS16550 *com_port)
424 {
425 return ((com_port->lsr & LSR_DR) != 0);
426 }
427
428 typedef unsigned short u_short;
429 typedef unsigned char u_char;
430
431 #define COL 80
432 #define ROW 25
433 #define CHR 2
434 #define MONO_BASE 0x3B4
435 #define MONO_BUF 0xB0000
436 #define CGA_BASE 0x3D4
437 #define CGA_BUF 0xB8000
438 #define ISA_mem ((unsigned char *)0xC0000000)
439 #define ISA_io ((unsigned char *)0x80000000)
440
441 unsigned char background = 0;
442 unsigned char foreground = 6;
443
444 unsigned int addr_6845;
445 unsigned short *Crtat;
446 int lastpos;
447 int scroll;
448
449 static void
450 outb(int port, unsigned char c)
451 {
452 ISA_io[port] = c;
453 }
454
455 static unsigned char
456 inb(int port)
457 {
458 return (ISA_io[port]);
459 }
460
461
462
463
464 struct screen {
465 u_short *cp;
466 enum state {
467 NORMAL,
468 ESC,
469 EBRAC,
470 EBRACEQ
471 } state;
472 int cx;
473 int cy;
474 int *accp;
475 int row;
476 int so;
477 u_short color;
478 u_short color_so;
479 u_short save_color;
480 u_short save_color_so;
481 } screen;
482
483
484
485
486
487
488
489 #define CATTR(x) (x)
490 #define ATTR_ADDR(which) (((u_char *)&(which))+1)
491
492 unsigned short pccolor;
493 unsigned short pccolor_so;
494
495
496
497
498 static void cursor(void)
499 {
500 int pos = screen.cp - Crtat;
501
502 if (lastpos != pos) {
503 outb(addr_6845, 14);
504 outb(addr_6845+1, pos >> 8);
505 outb(addr_6845, 15);
506 outb(addr_6845+1, pos);
507 lastpos = pos;
508 }
509 }
510
511 static void initscreen(void )
512 {
513 struct screen *d = &screen;
514
515 pccolor = CATTR((background<<4)|foreground);
516 pccolor_so = CATTR((foreground<<4)|background);
517 d->color = pccolor;
518 d->save_color = pccolor;
519 d->color_so = pccolor_so;
520 d->save_color_so = pccolor_so;
521 }
522
523
524 #define wrtchar(c, d) { \
525 *(d->cp) = c; \
526 d->cp++; \
527 d->row++; \
528 }
529
530 fillw(unsigned short val, unsigned short *buf, int num)
531 {
532
533 unsigned short tmp;
534 tmp = val;
535 while (num-- > 0)
536 {
537 *buf++ = tmp;
538 }
539 }
540
541
542
543
544
545
546
547
548 int CRT_putc(int port, unsigned char c)
549 {
550 struct screen *d = &screen;
551 u_short *base;
552 int i, j;
553 u_short *pp;
554
555 base = Crtat;
556
557 switch (d->state) {
558 case NORMAL:
559 switch (c) {
560 case 0x0:
561 return;
562
563 case 0x1B:
564 d->state = ESC;
565 break;
566
567 case '\t':
568 do {
569 wrtchar(d->color | ' ', d);
570 } while (d->row % 8);
571 break;
572
573 case '\b':
574 if (d->cp > base) {
575 d->cp--;
576 d->row--;
577 if (d->row < 0)
578 d->row += COL;
579 }
580 break;
581
582 case '\r':
583 d->cp -= d->row;
584 d->row = 0;
585 break;
586
587 case '\n':
588 d->cp += COL;
589 break;
590
591 case '\007':
592 break;
593
594 default:
595 if (d->so) {
596 wrtchar(d->color_so|(c<<8), d);
597 } else {
598 wrtchar(d->color | (c<<8), d);
599 }
600 if (d->row >= COL)
601 d->row = 0;
602 break;
603 }
604 break;
605
606 case EBRAC:
607
608
609
610
611
612 switch (c) {
613 case 'm':
614 d->so = d->cx;
615 break;
616
617 case 'A':
618 if (d->cp >= base + COL)
619 d->cp -= COL;
620 break;
621
622 case 'B':
623 d->cp += COL;
624 break;
625
626 case 'C':
627 d->cp++;
628 d->row++;
629 break;
630
631 case 'D':
632 if (d->cp > base) {
633 d->cp--;
634 d->row--;
635 if (d->row < 0)
636 d->row += COL;
637 }
638 break;
639
640 case 'J':
641 fillw(d->color|(' '<<8), d->cp, base + COL * ROW - d->cp);
642 break;
643
644 case 'K':
645 fillw(d->color|(' '<<8), d->cp, COL - (d->cp - base) % COL);
646 break;
647
648 case 'H':
649 if (d->cx > ROW)
650 d->cx = ROW;
651 if (d->cy > COL)
652 d->cy = COL;
653 if (d->cx == 0 || d->cy == 0) {
654 d->cp = base;
655 d->row = 0;
656 } else {
657 d->cp = base + (d->cx - 1) * COL + d->cy - 1;
658 d->row = d->cy - 1;
659 }
660 break;
661
662 case '_':
663 if (d->cx)
664 d->cx = 1;
665 else
666 d->cx = 12;
667 outb(addr_6845, 10);
668 outb(addr_6845+1, d->cx);
669 outb(addr_6845, 11);
670 outb(addr_6845+1, 13);
671 break;
672
673 case ';':
674 d->accp = &d->cy;
675 return;
676
677 case '=':
678 d->state = EBRACEQ;
679 return;
680
681 case 'L':
682 i = (d->cp - base) / COL;
683
684 pp = base + COL * (ROW-2);
685 for (j = ROW - 1 - i; j--; pp -= COL)
686 bcopy(pp, pp + COL, COL * CHR);
687 fillw(d->color|(' '<<8), base + i * COL, COL);
688 break;
689
690 case 'M':
691 i = (d->cp - base) / COL;
692 pp = base + i * COL;
693 bcopy(pp + COL, pp, (ROW-1 - i)*COL*CHR);
694 fillw(d->color|(' '<<8), base + COL * (ROW - 1), COL);
695 break;
696
697 default:
698 if ((c >= '0') && (c <= '9')) {
699 *(d->accp) *= 10;
700 *(d->accp) += c - '0';
701 return;
702 } else
703 break;
704 }
705 d->state = NORMAL;
706 break;
707
708 case EBRACEQ: {
709
710
711
712
713
714 u_char *colp;
715
716
717
718
719
720
721
722 switch (c) {
723 case 'F':
724 colp = ATTR_ADDR(d->color);
725 do_fg:
726 *colp = (*colp & 0xf0) | (d->cx);
727 break;
728
729 case 'G':
730 colp = ATTR_ADDR(d->color);
731 do_bg:
732 *colp = (*colp & 0xf) | (d->cx << 4);
733 break;
734
735 case 'H':
736 colp = ATTR_ADDR(d->color_so);
737 goto do_fg;
738
739 case 'I':
740 colp = ATTR_ADDR(d->color_so);
741 goto do_bg;
742
743 case 'S':
744 d->save_color = d->color;
745 d->save_color_so = d->color_so;
746 break;
747
748 case 'R':
749 d->color = d->save_color;
750 d->color_so = d->save_color_so;
751 break;
752
753 default:
754 if ((c >= '0') && (c <= '9')) {
755 d->cx *= 10;
756 d->cx += c - '0';
757 return;
758 } else
759 break;
760 }
761 d->state = NORMAL;
762 }
763 break;
764
765 case ESC:
766 switch (c) {
767 case 'c':
768 fillw(d->color|(' '<<8), base, COL * ROW);
769 d->cp = base;
770 d->row = 0;
771 d->state = NORMAL;
772 break;
773 case '[':
774 d->state = EBRAC;
775 d->cx = 0;
776 d->cy = 0;
777 d->accp = &d->cx;
778 break;
779 default:
780 d->state = NORMAL;
781 break;
782 }
783 break;
784 }
785 if (d->cp >= base + (COL * ROW)) {
786 bcopy(base + COL, base, COL * (ROW - 1) * CHR);
787 fillw(d->color|(' '<<8), base + COL * (ROW - 1), COL);
788 d->cp -= COL;
789 }
790 cursor();
791 }
792
793 void video_on(void)
794 {
795 outb(0x3C4, 0x01);
796 outb(0x3C5, inb(0x3C5)&~20);
797 }
798
799 int CRT_init(void)
800 {
801 unsigned long *PCI_base = (unsigned long *)0x80808010;
802 struct screen *d = &screen;
803 if (*PCI_base)
804 {
805 return (0);
806 }
807 video_on();
808 d->cp = Crtat = (u_short *)&ISA_mem[0x0B8000];
809 addr_6845 = CGA_BASE;
810 initscreen();
811 fillw(pccolor|(' '<<8), d->cp, COL * ROW);
812 return (1);
813 }
814
815
816
817 #define L 0x0001
818 #define SHF 0x0002
819 #define ALT 0x0004
820 #define NUM 0x0008
821 #define CTL 0x0010
822 #define CPS 0x0020
823 #define ASCII 0x0040
824 #define STP 0x0080
825 #define FUNC 0x0100
826 #define SCROLL 0x0200
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872 const unsigned short action[] = {
873 0, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII,
874 ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII,
875 ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII,
876 ASCII, ASCII, ASCII, ASCII, ASCII, CTL, ASCII, ASCII,
877 ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII,
878 ASCII, ASCII, SHF, ASCII, ASCII, ASCII, ASCII, ASCII,
879 ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, SHF, ASCII,
880 ALT, ASCII, CPS, FUNC, FUNC, FUNC, FUNC, FUNC,
881 FUNC, FUNC, FUNC, FUNC, FUNC, NUM,SCROLL, ASCII,
882 ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII,
883 ASCII, ASCII, ASCII, ASCII, 0, 0, 0, 0,
884 0,0,0,0,0,0,0,0,
885 0,0,0,0,0,0,0,0,
886 0,0,0,0,0,0,0,0,
887 0,0,0,0,0,0,0,0,
888 0,0,0,0,0,0,0,0,
889 };
890
891 const unsigned char unshift[] = {
892 0, 033, '1', '2', '3', '4', '5', '6',
893 '7', '8', '9', '0', '-', '=', 010, '\t',
894 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
895 'o', 'p', '[', ']', '\r', CTL, 'a', 's',
896 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
897 '\'', '`', SHF, '\\', 'z', 'x', 'c', 'v',
898 'b', 'n', 'm', ',', '.', '/', SHF, '*',
899 ALT, ' ', CPS, 1, 2, 3, 4, 5,
900 6, 7, 8, 9, 10, NUM, STP, '7',
901 '8', '9', '-', '4', '5', '6', '+', '1',
902 '2', '3', '0', 0177, 0, 0, 0, 0,
903 0,0,0,0,0,0,0,0,
904 0,0,0,0,0,0,0,0,
905 0,0,0,0,0,0,0,0,
906 0,0,0,0,0,0,0,0,
907 0,0,0,0,0,0,0,0,
908 };
909
910 const unsigned char shift[] = {
911 0, 033, '!', '@', '#', '$', '%', '^',
912 '&', '*', '(', ')', '_', '+', 010, '\t',
913 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
914 'O', 'P', '{', '}', '\r', CTL, 'A', 'S',
915 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
916 '"', '~', SHF, '|', 'Z', 'X', 'C', 'V',
917 'B', 'N', 'M', '<', '>', '?', SHF, '*',
918 ALT, ' ', CPS, 0, 0, ' ', 0, 0,
919 0, 0, 0, 0, 0, NUM, STP, '7',
920 '8', '9', '-', '4', '5', '6', '+', '1',
921 '2', '3', '0', 0177, 0, 0, 0, 0,
922 0,0,0,0,0,0,0,0,
923 0,0,0,0,0,0,0,0,
924 0,0,0,0,0,0,0,0,
925 0,0,0,0,0,0,0,0,
926 0,0,0,0,0,0,0,0,
927 };
928
929 const unsigned char ctl[] = {
930 0, 033, '!', 000, '#', '$', '%', 036,
931 '&', '*', '(', ')', 037, '+', 034,'\177',
932 021, 027, 005, 022, 024, 031, 025, 011,
933 017, 020, 033, 035, '\r', CTL, 001, 023,
934 004, 006, 007, 010, 012, 013, 014, ';',
935 '\'', '`', SHF, 034, 032, 030, 003, 026,
936 002, 016, 015, '<', '>', '?', SHF, '*',
937 ALT, ' ', CPS, 0, 0, ' ', 0, 0,
938 CPS, 0, 0, 0, 0, 0, 0, 0,
939 0, 0, 0, 0, 0, 0, 0, 0,
940 0, 0, 0, 0177, 0, 0, 0, 0,
941 0, 0, 033, '7', '4', '1', 0, NUM,
942 '8', '5', '2', 0, STP, '9', '6', '3',
943 '.', 0, '*', '-', '+', 0, 0, 0,
944 0,0,0,0,0,0,0,0,
945 0,0,0,0,0,0,0,0,
946 };
947
948
949 unsigned char shfts, ctls, alts, caps, num, stp;
950
951 #define KBDATAP 0x60
952 #define KBSTATUSPORT 0x61
953 #define KBSTATP 0x64
954 #define KBINRDY 0x01
955 #define KBOUTRDY 0x02
956
957 #define _x__ 0x00
958
959 const unsigned char keycode[] = {
960 _x__, 0x43, 0x41, 0x3F, 0x3D, 0x3B, 0x3C, _x__,
961 _x__, 0x44, 0x42, 0x40, 0x3E, 0x0F, 0x29, _x__,
962 _x__, 0x38, 0x2A, _x__, 0x1D, 0x10, 0x02, _x__,
963 _x__, _x__, 0x2C, 0x1F, 0x1E, 0x11, 0x03, _x__,
964 _x__, 0x2E, 0x2D, 0x20, 0x12, 0x05, 0x04, _x__,
965 _x__, 0x39, 0x2F, 0x21, 0x14, 0x13, 0x06, _x__,
966 _x__, 0x31, 0x30, 0x23, 0x22, 0x15, 0x07, _x__,
967 _x__, _x__, 0x32, 0x24, 0x16, 0x08, 0x09, _x__,
968 _x__, 0x33, 0x25, 0x17, 0x18, 0x0B, 0x0A, _x__,
969 _x__, 0x34, 0x35, 0x26, 0x27, 0x19, 0x0C, _x__,
970 _x__, _x__, 0x28, _x__, 0x1A, 0x0D, _x__, _x__,
971 0x3A, 0x36, 0x1C, 0x1B, _x__, 0x2B, _x__, _x__,
972 _x__, _x__, _x__, _x__, _x__, _x__, 0x0E, _x__,
973 _x__, 0x4F, _x__, 0x4B, 0x47, _x__, _x__, _x__,
974 0x52, 0x53, 0x50, 0x4C, 0x4D, 0x48, 0x01, 0x45,
975 _x__, 0x4E, 0x51, 0x4A, _x__, 0x49, 0x46, 0x54,
976 };
977
978 int kbd(int noblock)
979 {
980 unsigned char dt, brk, act;
981 int first = 1;
982 loop:
983 if (noblock) {
984 if ((inb(KBSTATP) & KBINRDY) == 0)
985 return (-1);
986 } else while((inb(KBSTATP) & KBINRDY) == 0)
987 ;
988 dt = inb(KBDATAP);
989
990 brk = dt & 0x80;
991 dt = dt & 0x7f;
992
993 act = action[dt];
994 if (act&SHF)
995 shfts = brk ? 0 : 1;
996 if (act&ALT)
997 alts = brk ? 0 : 1;
998 if (act&NUM)
999 if (act&L) {
1000
1001 if(!brk)
1002 num = !num;
1003 } else
1004 num = brk ? 0 : 1;
1005 if (act&CTL)
1006 ctls = brk ? 0 : 1;
1007 if (act&CPS)
1008 if (act&L) {
1009
1010 if(!brk)
1011 caps = !caps;
1012 } else
1013 caps = brk ? 0 : 1;
1014 if (act&STP)
1015 if (act&L) {
1016 if(!brk)
1017 stp = !stp;
1018 } else
1019 stp = brk ? 0 : 1;
1020
1021 if ((act&ASCII) && !brk) {
1022 unsigned char chr;
1023
1024 if (shfts)
1025 chr = shift[dt];
1026 else if (ctls)
1027 chr = ctl[dt];
1028 else
1029 chr = unshift[dt];
1030
1031 if (alts)
1032 chr |= 0x80;
1033
1034 if (caps && (chr >= 'a' && chr <= 'z'))
1035 chr -= 'a' - 'A' ;
1036 #define CTRL(s) (s & 0x1F)
1037 if ((chr == '\r') || (chr == '\n') || (chr == CTRL('A')) || (chr == CTRL('S')))
1038 {
1039
1040 while (1)
1041 {
1042 while((inb(KBSTATP) & KBINRDY) == 0) ;
1043 dt = inb(KBDATAP);
1044 if (dt & 0x80) break;
1045 }
1046 }
1047 return (chr);
1048 }
1049 if (first && brk) return (0);
1050 goto loop;
1051 }
1052
1053 int scankbd(void)
1054 {
1055 return (kbd(1) != -1);
1056 }
1057
1058 void kbdreset(void)
1059 {
1060 unsigned char c;
1061
1062
1063 while (inb(KBSTATP) & KBOUTRDY)
1064 ;
1065 outb(KBSTATP,0x60);
1066 while (inb(KBSTATP) & KBOUTRDY)
1067 ;
1068 outb(KBDATAP,0x4D);
1069
1070
1071 while (inb(KBSTATP) & KBOUTRDY)
1072 ;
1073 outb(KBDATAP,0xFF);
1074
1075 while ((c = inb(KBDATAP)) != 0xFA)
1076 ;
1077 }
1078
1079 int CRT_getc(void)
1080 {
1081 int c;
1082 while ((c = kbd(0)) == 0) ;
1083 return(c);
1084 }
1085
1086 int CRT_test(void)
1087 {
1088 return ((inb(KBSTATP) & KBINRDY) != 0);
1089 }
1090
1091
1092 _dump_buf_with_offset(unsigned char *p, int s, unsigned char *base)
1093 {
1094 int i, c;
1095 if ((unsigned int)s > (unsigned int)p)
1096 {
1097 s = (unsigned int)s - (unsigned int)p;
1098 }
1099 while (s > 0)
1100 {
1101 if (base)
1102 {
1103 _printk("%06X: ", (int)p - (int)base);
1104 } else
1105 {
1106 _printk("%06X: ", p);
1107 }
1108 for (i = 0; i < 16; i++)
1109 {
1110 if (i < s)
1111 {
1112 _printk("%02X", p[i] & 0xFF);
1113 } else
1114 {
1115 _printk(" ");
1116 }
1117 if ((i % 2) == 1) _printk(" ");
1118 if ((i % 8) == 7) _printk(" ");
1119 }
1120 _printk(" |");
1121 for (i = 0; i < 16; i++)
1122 {
1123 if (i < s)
1124 {
1125 c = p[i] & 0xFF;
1126 if ((c < 0x20) || (c >= 0x7F)) c = '.';
1127 } else
1128 {
1129 c = ' ';
1130 }
1131 _printk("%c", c);
1132 }
1133 _printk("|\n");
1134 s -= 16;
1135 p += 16;
1136 }
1137 }
1138
1139 _dump_buf(unsigned char *p, int s)
1140 {
1141 _dump_buf_with_offset(p, s, 0);
1142 }
1143
1144
1145 dump_buf_with_offset(unsigned char *p, int s, unsigned char *base)
1146 {
1147 int i, c;
1148 if ((unsigned int)s > (unsigned int)p)
1149 {
1150 s = (unsigned int)s - (unsigned int)p;
1151 }
1152 while (s > 0)
1153 {
1154 if (base)
1155 {
1156 printk("%06X: ", (int)p - (int)base);
1157 } else
1158 {
1159 printk("%06X: ", p);
1160 }
1161 for (i = 0; i < 16; i++)
1162 {
1163 if (i < s)
1164 {
1165 printk("%02X", p[i] & 0xFF);
1166 } else
1167 {
1168 printk(" ");
1169 }
1170 if ((i % 2) == 1) printk(" ");
1171 if ((i % 8) == 7) printk(" ");
1172 }
1173 printk(" |");
1174 for (i = 0; i < 16; i++)
1175 {
1176 if (i < s)
1177 {
1178 c = p[i] & 0xFF;
1179 if ((c < 0x20) || (c >= 0x7F)) c = '.';
1180 } else
1181 {
1182 c = ' ';
1183 }
1184 printk("%c", c);
1185 }
1186 printk("|\n");
1187 s -= 16;
1188 p += 16;
1189 }
1190 }
1191
1192 dump_buf(unsigned char *p, int s)
1193 {
1194 dump_buf_with_offset(p, s, 0);
1195 }
1196
1197