Linux preempt-rt

Check our new training course

Real-Time Linux with PREEMPT_RT

Check our new training course
with Creative Commons CC-BY-SA
lecture and lab materials

Bootlin logo

Elixir Cross Referencer

   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
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 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
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
#define FALSE 0
#define TRUE  1
#include <stdarg.h>

extern void cnputc(char c);

static char *_sprintk_ptr;

static _sputc(char c)
{
   *_sprintk_ptr++ = c;
   *_sprintk_ptr = '\0';
}

_sprintk(char *buf, char const *fmt, ...)
{
	int ret;
	va_list ap;

	va_start(ap, fmt);
	_sprintk_ptr = buf;
	ret = _vprintk(_sputc, fmt, ap);
	va_end(ap);
	return (ret);
}

_vsprintk(char *buf, char const *fmt, va_list ap)
{
	int ret;

	_sprintk_ptr = buf;
	ret = _vprintk(_sputc, fmt, ap);
	return (ret);
}

_printk(char const *fmt, ...)
{
	int ret;
	va_list ap;

	va_start(ap, fmt);
	ret = _vprintk(cnputc, fmt, ap);
	va_end(ap);
	return (ret);
}

#define is_digit(c) ((c >= '0') && (c <= '9'))

int
_vprintk(putc, fmt0, ap)
   int (*putc)();
   const char *fmt0;
   va_list ap;
{
   char c, sign, *cp;
   int left_prec, right_prec, zero_fill, length, pad, pad_on_right;
   char buf[32];
   long val;
   while (c = *fmt0++)
   {
      if (c == '%')
      {
         c = *fmt0++;
         left_prec = right_prec = pad_on_right = 0;
         if (c == '-')
         {
            c = *fmt0++;
            pad_on_right++;
         }
         if (c == '0')
         {
            zero_fill = TRUE;
            c = *fmt0++;
         } else
         {
            zero_fill = FALSE;
         }
         while (is_digit(c))
         {
            left_prec = (left_prec * 10) + (c - '0');
            c = *fmt0++;
         }
         if (c == '.')
         {
            c = *fmt0++;
            zero_fill++;
            while (is_digit(c))
            {
               right_prec = (right_prec * 10) + (c - '0');
               c = *fmt0++;
            }
         } else
         {
            right_prec = left_prec;
         }
         sign = '\0';
         switch (c)
         {
            case 'd':
            case 'x':
            case 'X':
               val = va_arg(ap, long);
               switch (c)
               {
                  case 'd':
                     if (val < 0)
                     {
                        sign = '-';
                        val = -val;
                     }
                     length = _cvt(val, buf, 10, "0123456789");
                     break;
                  case 'x':
                     length = _cvt(val, buf, 16, "0123456789abcdef");
                     break;
                  case 'X':
                     length = _cvt(val, buf, 16, "0123456789ABCDEF");
                     break;
               }
               cp = buf;
               break;
            case 's':
               cp = va_arg(ap, char *);
               length = strlen(cp);
               break;
            case 'c':
               c = va_arg(ap, long /*char*/);
               (*putc)(c);
               continue;
            default:
               (*putc)('?');
         }
         pad = left_prec - length;
         if (sign != '\0')
         {
            pad--;
         }
         if (zero_fill)
         {
            c = '0';
            if (sign != '\0')
            {
               (*putc)(sign);
               sign = '\0';
            }
         } else
         {
            c = ' ';
         }
         if (!pad_on_right)
         {
            while (pad-- > 0)
            {
               (*putc)(c);
            }
         }
         if (sign != '\0')
         {
            (*putc)(sign);
         }
         while (length-- > 0)
         {
            (*putc)(c = *cp++);
            if (c == '\n')
            {
               (*putc)('\r');
            }
         }
         if (pad_on_right)
         {
            while (pad-- > 0)
            {
               (*putc)(c);
            }
         }
      } else
      {
         (*putc)(c);
         if (c == '\n')
         {
            (*putc)('\r');
         }
      }
   }
}

static _cvt(unsigned long val, char *buf, long radix, char *digits)
{
   char temp[80];
   char *cp = temp;
   int length = 0;
   if (val == 0)
   { /* Special case */
      *cp++ = '0';
   } else
   while (val)
   {
      *cp++ = digits[val % radix];
      val /= radix;
   }
   while (cp != temp)
   {
      *buf++ = *--cp;
      length++;
   }
   *buf = '\0';
   return (length);
}

/*
 * Console I/O interface
 */

typedef const (*proc)();
typedef int dev_t;

#define FALSE 0
#define TRUE  1

#define CRT_PORT 0x3D4  /* Pick one */

static int init = FALSE;
static int is_crt = 0;
static int port = 0;
static int line_num = 0;
#define MAX_LINES 24

char
cngetc()
{
   int s = _disable_interrupts();
   char c = '\0';
   if (port == CRT_PORT)
   {
      c = CRT_getc(port);
   } else
   if (port)
   {
      c = NS16550_getc(port);
   }
   _enable_interrupts(s);
   return (c);
}

cntstc()
{
   return (0);
}

char _cn_trace[1024];
char *_cnp = _cn_trace;

/*
 * Console kernel output character routine.
 */
void
cnputc(char c)
{
   *_cnp++ = c;
   if (_cnp == &_cn_trace[sizeof(_cn_trace)])
   {
      _cnp = _cn_trace;
   }
   if (!init)
   {
      if (is_crt = CRT_init())
      {
         port = CRT_PORT;
      } else
      {
         port = NS16550_init(0);
      }
      init = TRUE;
   }
   if (port == CRT_PORT)
   {
      CRT_putc(port, c);
   } else
   if (port)
   {
      NS16550_putc(port, c);
   }
   if (c == '\n')
   {
      if (line_num >= 0) line_num++;
   }
   if (c == '\r')
   {
      if (line_num >= MAX_LINES)
      {
         line_num = 0;
         _cnpause();
      }
   }
}

_cnpause()
{
   int c;
   int s = _disable_interrupts();
   _printk("-- More? ");
   while ((c = cngetc()) == 0);
   _printk("\r         \r");  /* Erase prompt */
   if (c == ' ')
   {
      line_num = 0;
   } else
   if (c == 'n')
   {
      line_num = -1;  /* Turn off pause */
   } else
   if ((c == '\r') || (c == '\n'))
   {
      line_num = MAX_LINES-1;
   } else
   if (c == 0x03) /* ^C */
   {
   	abort();
   } else
   {
      line_num = MAX_LINES - (MAX_LINES/3);
   }
   _enable_interrupts(s);
}

cnpause()
{
   int c;
   int s = _disable_interrupts();
   printk("-- More? ");
   while ((c = cngetc()) == 0);
   printk("\r         \r");  /* Erase prompt */
   _enable_interrupts(s);
}

/*
 * COM1 NS16550 support
 */

struct NS16550
	{
		unsigned char rbr;  /* 0 */
		unsigned char ier;  /* 1 */
		unsigned char fcr;  /* 2 */
		unsigned char lcr;  /* 3 */
		unsigned char mcr;  /* 4 */
		unsigned char lsr;  /* 5 */
		unsigned char msr;  /* 6 */
		unsigned char scr;  /* 7 */
	};

#define thr rbr
#define iir fcr
#define dll rbr
#define dlm ier

#define LSR_DR   0x01  /* Data ready */
#define LSR_OE   0x02  /* Overrun */
#define LSR_PE   0x04  /* Parity error */
#define LSR_FE   0x08  /* Framing error */
#define LSR_BI   0x10  /* Break */
#define LSR_THRE 0x20  /* Xmit holding register empty */
#define LSR_TEMT 0x40  /* Xmitter empty */
#define LSR_ERR  0x80  /* Error */

#define COM1	0x800003F8
#define COM2	0x800002F8

typedef struct NS16550 *NS16550_t;

const NS16550_t COM_PORTS[] = { COM1, COM2};

volatile struct NS16550 *
NS16550_init(int chan)
{
	volatile struct NS16550 *com_port;
	volatile unsigned char xx;
	com_port = (struct NS16550 *) COM_PORTS[chan];
	/* See if port is present */
	com_port->lcr = 0x00;
	com_port->ier = 0xFF;
#if 0	
	if (com_port->ier != 0x0F) return ((struct NS16550 *)0);
#endif	
	com_port->ier = 0x00;
	com_port->lcr = 0x80;  /* Access baud rate */
	com_port->dll = 12;  /* 9600 baud */
	com_port->dlm = 12 >> 8;
	com_port->lcr = 0x03;  /* 8 data, 1 stop, no parity */
	com_port->mcr = 0x03;  /* RTS/DTR */
	com_port->fcr = 0x07;  /* Clear & enable FIFOs */
	return (com_port);
}


NS16550_putc(volatile struct NS16550 *com_port, unsigned char c)
{
	volatile int i;
	while ((com_port->lsr & LSR_THRE) == 0) ;
	com_port->thr = c;
}

unsigned char NS16550_getc(volatile struct NS16550 *com_port)
{
	while ((com_port->lsr & LSR_DR) == 0) ;
	return (com_port->rbr);
}

NS16550_test(volatile struct NS16550 *com_port)
{
	return ((com_port->lsr & LSR_DR) != 0);
}

typedef unsigned short u_short;
typedef unsigned char  u_char;

#define	COL		80
#define	ROW		25
#define	CHR		2
#define MONO_BASE	0x3B4
#define MONO_BUF	0xB0000
#define CGA_BASE	0x3D4
#define CGA_BUF		0xB8000
#define ISA_mem		((unsigned char *)0xC0000000)
#define ISA_io		((unsigned char *)0x80000000)

unsigned char background = 0;
unsigned char foreground = 6;

unsigned int addr_6845;
unsigned short *Crtat;
int lastpos;
int scroll;

static void
outb(int port, unsigned char c)
{
	ISA_io[port] = c;
}

static unsigned char
inb(int port)
{
	return (ISA_io[port]);
}

/*
 * The current state of virtual displays
 */
struct screen {
	u_short *cp;		/* the current character address */
	enum state {
		NORMAL,			/* no pending escape */
		ESC,			/* saw ESC */
		EBRAC,			/* saw ESC[ */
		EBRACEQ			/* saw ESC[= */
	} state;		/* command parser state */
	int	cx;		/* the first escape seq argument */
	int	cy;		/* the second escap seq argument */
	int	*accp;		/* pointer to the current processed argument */
	int	row;		/* current column */
	int	so;		/* standout mode */
	u_short color;		/* normal character color */
	u_short color_so;	/* standout color */
	u_short save_color;	/* saved normal color */
	u_short save_color_so;	/* saved standout color */
} screen;

/*
 * Color and attributes for normal, standout and kernel output
 * are stored in the least-significant byte of a u_short
 * so they don't have to be shifted for use.
 * This is all byte-order dependent.
 */
#define	CATTR(x) (x)		/* store color/attributes un-shifted */
#define	ATTR_ADDR(which) (((u_char *)&(which))+1) /* address of attributes */

unsigned short	pccolor;		/* color/attributes for tty output */
unsigned short	pccolor_so;		/* color/attributes, standout mode */

/*
 * cursor() sets an offset (0-1999) into the 80x25 text area   
 */
static void
cursor()
{
 	int pos = screen.cp - Crtat;

	if (lastpos != pos) {
		outb(addr_6845, 14);
		outb(addr_6845+1, pos >> 8);
		outb(addr_6845, 15);
		outb(addr_6845+1, pos);
		lastpos = pos;
	}
}

static void
initscreen()
{
	struct screen *d = &screen;

	pccolor = CATTR((background<<4)|foreground);
	pccolor_so = CATTR((foreground<<4)|background);
	d->color = pccolor;
	d->save_color = pccolor;
	d->color_so = pccolor_so;
	d->save_color_so = pccolor_so;
}


#define	wrtchar(c, d) { \
	*(d->cp) = c; \
	d->cp++; \
	d->row++; \
}

fillw(unsigned short val, unsigned short *buf, int num)
{
	/* Need to byte swap value */
	unsigned short tmp;
	tmp = val;
	while (num-- > 0)
	{
		*buf++ = tmp;
	}
}

/*
 * CRT_putc (nee sput) has support for emulation of the 'ibmpc' termcap entry.
 * This is a bare-bones implementation of a bare-bones entry
 * One modification: Change li#24 to li#25 to reflect 25 lines
 * "ca" is the color/attributes value (left-shifted by 8)
 * or 0 if the current regular color for that screen is to be used.
 */
void 
CRT_putc(int port, u_char c)
{
	struct screen *d = &screen;
	u_short *base;
	int i, j;
	u_short *pp;

	base = Crtat;

	switch (d->state) {
	case NORMAL:
		switch (c) {
		case 0x0:		/* Ignore pad characters */
			return;

		case 0x1B:
			d->state = ESC;
			break;

		case '\t':
			do {
				wrtchar(d->color | ' ', d);
			} while (d->row % 8);
			break;

		case '\b':  /* non-destructive backspace */
			if (d->cp > base) {
				d->cp--;
				d->row--;
				if (d->row < 0)
					d->row += COL;	/* prev column */
			}
			break;

		case '\r':
			d->cp -= d->row;
			d->row = 0;
			break;

		case '\n':
			d->cp += COL;
			break;

		case '\007':
			break;

		default:
			if (d->so) {
				wrtchar(d->color_so|(c<<8), d); 
			} else {
				wrtchar(d->color | (c<<8), d); 
			}
			if (d->row >= COL)
				d->row = 0;
			break;
		}
		break;

	case EBRAC:
		/*
		 * In this state, the action at the end of the switch
		 * on the character type is to go to NORMAL state,
		 * and intermediate states do a return rather than break.
		 */
		switch (c) {
		case 'm':
			d->so = d->cx;
			break;

		case 'A': /* back one row */
			if (d->cp >= base + COL)
				d->cp -= COL;
			break;

		case 'B': /* down one row */
			d->cp += COL;
			break;

		case 'C': /* right cursor */
			d->cp++;
			d->row++;
			break;

		case 'D': /* left cursor */
			if (d->cp > base) {
				d->cp--;
				d->row--;
				if (d->row < 0)
					d->row += COL;	/* prev column ??? */
			}
			break;

		case 'J': /* Clear to end of display */
			fillw(d->color|(' '<<8), d->cp, base + COL * ROW - d->cp);
			break;

		case 'K': /* Clear to EOL */
			fillw(d->color|(' '<<8), d->cp, COL - (d->cp - base) % COL);
			break;

		case 'H': /* Cursor move */
			if (d->cx > ROW)
				d->cx = ROW;
			if (d->cy > COL)
				d->cy = COL;
			if (d->cx == 0 || d->cy == 0) {
				d->cp = base;
				d->row = 0;
			} else {
				d->cp = base + (d->cx - 1) * COL + d->cy - 1;
				d->row = d->cy - 1;
			}
			break;

		case '_': /* set cursor */
			if (d->cx)
				d->cx = 1;		/* block */
			else
				d->cx = 12;	/* underline */
			outb(addr_6845, 10);
			outb(addr_6845+1, d->cx);
			outb(addr_6845, 11);
			outb(addr_6845+1, 13);
			break;

		case ';': /* Switch params in cursor def */
			d->accp = &d->cy;
			return;

		case '=': /* ESC[= color change */
			d->state = EBRACEQ;
			return;

		case 'L':	/* Insert line */
			i = (d->cp - base) / COL;
			/* avoid deficiency of bcopy implementation */
			pp = base + COL * (ROW-2);
			for (j = ROW - 1 - i; j--; pp -= COL)
				bcopy(pp, pp + COL, COL * CHR);
			fillw(d->color|(' '<<8), base + i * COL, COL);
			break;
			
		case 'M':	/* Delete line */
			i = (d->cp - base) / COL;
			pp = base + i * COL;
			bcopy(pp + COL, pp, (ROW-1 - i)*COL*CHR);
			fillw(d->color|(' '<<8), base + COL * (ROW - 1), COL);
			break;

		default: /* Only numbers valid here */
			if ((c >= '0') && (c <= '9')) {
				*(d->accp) *= 10;
				*(d->accp) += c - '0';
				return;
			} else
				break;
		}
		d->state = NORMAL;
		break;

	case EBRACEQ: {
		/*
		 * In this state, the action at the end of the switch
		 * on the character type is to go to NORMAL state,
		 * and intermediate states do a return rather than break.
		 */
		u_char *colp;

		/*
		 * Set foreground/background color
		 * for normal mode, standout mode
		 * or kernel output.
		 * Based on code from kentp@svmp03.
		 */
		switch (c) {
		case 'F':
			colp = ATTR_ADDR(d->color);
	do_fg:
			*colp = (*colp & 0xf0) | (d->cx);
			break;

		case 'G':
			colp = ATTR_ADDR(d->color);
	do_bg:
			*colp = (*colp & 0xf) | (d->cx << 4);
			break;

		case 'H':
			colp = ATTR_ADDR(d->color_so);
			goto do_fg;

		case 'I':
			colp = ATTR_ADDR(d->color_so);
			goto do_bg;

		case 'S':
			d->save_color = d->color;
			d->save_color_so = d->color_so;
			break;

		case 'R':
			d->color = d->save_color;
			d->color_so = d->save_color_so;
			break;

		default: /* Only numbers valid here */
			if ((c >= '0') && (c <= '9')) {
				d->cx *= 10;
				d->cx += c - '0';
				return;
			} else
				break;
		}
		d->state = NORMAL;
	    }
	    break;

	case ESC:
		switch (c) {
		case 'c':	/* Clear screen & home */
			fillw(d->color|(' '<<8), base, COL * ROW);
			d->cp = base;
			d->row = 0;
			d->state = NORMAL;
			break;
		case '[':	/* Start ESC [ sequence */
			d->state = EBRAC;
			d->cx = 0;
			d->cy = 0;
			d->accp = &d->cx;
			break;
		default: /* Invalid, clear state */
			d->state = NORMAL;
			break;
		}
		break;
	}
	if (d->cp >= base + (COL * ROW)) { /* scroll check */
		bcopy(base + COL, base, COL * (ROW - 1) * CHR);
		fillw(d->color|(' '<<8), base + COL * (ROW - 1), COL);
		d->cp -= COL;
	}	
	cursor();
}

video_on()
{ /* Enable video */
	outb(0x3C4, 0x01);
	outb(0x3C5, inb(0x3C5)&~20);
}

CRT_init()
{
	unsigned long *PCI_base = (unsigned long *)0x80808010;  /* Magic */
	struct screen *d = &screen;
	if (*PCI_base)
	{ /* No CRT configured */
		return (0);
	}
	video_on();
	d->cp = Crtat = (u_short *)&ISA_mem[0x0B8000];
	addr_6845 = CGA_BASE;
	initscreen();
	fillw(pccolor|(' '<<8), d->cp, COL * ROW);
	return (1);
}

/* Keyboard handler */

#define	L		0x0001	/* locking function */
#define	SHF		0x0002	/* keyboard shift */
#define	ALT		0x0004	/* alternate shift -- alternate chars */
#define	NUM		0x0008	/* numeric shift  cursors vs. numeric */
#define	CTL		0x0010	/* control shift  -- allows ctl function */
#define	CPS		0x0020	/* caps shift -- swaps case of letter */
#define	ASCII		0x0040	/* ascii code for this key */
#define	STP		0x0080	/* stop output */
#define	FUNC		0x0100	/* function key */
#define	SCROLL		0x0200	/* scroll lock key */

/* #include "pcconstab.US" */
/*	BSDI $Id: pcconstab.US,v 1.1.1.1 1994/03/31 13:29:09 gary Exp $	*/
/*-
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * William Jolitz and Don Ahn.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	from @(#)pccons.c	5.11 (Berkeley) 5/21/91
 */

/*
 *	US Keyboard mapping tables
 */

const unsigned short action[] = {
	0,     ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII,	/* scan  0- 7 */
	ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII,	/* scan  8-15 */
	ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII,	/* scan 16-23 */
	ASCII, ASCII, ASCII, ASCII, ASCII,   CTL, ASCII, ASCII,	/* scan 24-31 */
	ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII,	/* scan 32-39 */
	ASCII, ASCII,   SHF, ASCII, ASCII, ASCII, ASCII, ASCII,	/* scan 40-47 */
	ASCII, ASCII, ASCII, ASCII, ASCII, ASCII,   SHF, ASCII,	/* scan 48-55 */
	  ALT, ASCII,   CPS,  FUNC,  FUNC,  FUNC,  FUNC,  FUNC,	/* scan 56-63 */
	 FUNC,  FUNC,  FUNC,  FUNC,  FUNC,   NUM,SCROLL, ASCII,	/* scan 64-71 */
	ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII, ASCII,	/* scan 72-79 */
	ASCII, ASCII, ASCII, ASCII,     0,     0,     0,     0,	/* scan 80-87 */
	0,0,0,0,0,0,0,0,	/* scan 88-95 */
	0,0,0,0,0,0,0,0,	/* scan 96-103 */
	0,0,0,0,0,0,0,0,	/* scan 104-111 */
	0,0,0,0,0,0,0,0,	/* scan 112-119 */
	0,0,0,0,0,0,0,0,	/* scan 120-127 */
};

const unsigned char unshift[] = {	/* no shift */
	  0,   033,   '1',   '2',   '3',   '4',   '5',   '6',	/* scan  0- 7 */
	'7',   '8',   '9',   '0',   '-',   '=',   010,  '\t',	/* scan  8-15 */
	'q',   'w',   'e',   'r',   't',   'y',   'u',   'i',	/* scan 16-23 */
	'o',   'p',   '[',   ']',  '\r',   CTL,   'a',   's',	/* scan 24-31 */
	'd',   'f',   'g',   'h',   'j',   'k',   'l',   ';',	/* scan 32-39 */
	'\'',  '`',   SHF,   '\\',  'z',   'x',   'c',   'v',	/* scan 40-47 */
	'b',   'n',   'm',   ',',   '.',   '/',   SHF,   '*',	/* scan 48-55 */
	ALT,   ' ',   CPS,     1,     2,     3,     4,     5,	/* scan 56-63 */
	  6,     7,     8,     9,    10,   NUM,   STP,   '7',	/* scan 64-71 */
	'8',   '9',   '-',   '4',   '5',   '6',   '+',   '1',	/* scan 72-79 */
	'2',   '3',   '0',   0177,    0,     0,     0,     0,	/* scan 80-87 */
	0,0,0,0,0,0,0,0,	/* scan 88-95 */
	0,0,0,0,0,0,0,0,	/* scan 96-103 */
	0,0,0,0,0,0,0,0,	/* scan 104-111 */
	0,0,0,0,0,0,0,0,	/* scan 112-119 */
	0,0,0,0,0,0,0,0,	/* scan 120-127 */
};

const unsigned char shift[] = {	/* shift shift */
	  0,   033,   '!',   '@',   '#',   '$',   '%',   '^',	/* scan  0- 7 */
	'&',   '*',   '(',   ')',   '_',   '+',   010,  '\t',	/* scan  8-15 */
	'Q',   'W',   'E',   'R',   'T',   'Y',   'U',   'I',	/* scan 16-23 */
	'O',   'P',   '{',   '}',  '\r',   CTL,   'A',   'S',	/* scan 24-31 */
	'D',   'F',   'G',   'H',   'J',   'K',   'L',   ':',	/* scan 32-39 */
	'"',   '~',   SHF,   '|',   'Z',   'X',   'C',   'V',	/* scan 40-47 */
	'B',   'N',   'M',   '<',   '>',   '?',   SHF,   '*',	/* scan 48-55 */
	ALT,   ' ',   CPS,     0,     0,   ' ',     0,     0,	/* scan 56-63 */
	  0,     0,     0,     0,     0,   NUM,   STP,   '7',	/* scan 64-71 */
	'8',   '9',   '-',   '4',   '5',   '6',   '+',   '1',	/* scan 72-79 */
	'2',   '3',   '0',  0177,     0,     0,     0,     0,	/* scan 80-87 */
	0,0,0,0,0,0,0,0,	/* scan 88-95 */
	0,0,0,0,0,0,0,0,	/* scan 96-103 */
	0,0,0,0,0,0,0,0,	/* scan 104-111 */
	0,0,0,0,0,0,0,0,	/* scan 112-119 */
	0,0,0,0,0,0,0,0,	/* scan 120-127 */
};

const unsigned char ctl[] = {	/* CTL shift */
	  0,   033,   '!',   000,   '#',   '$',   '%',   036,	/* scan  0- 7 */
	'&',   '*',   '(',   ')',   037,   '+',   034,'\177',	/* scan  8-15 */
	021,   027,   005,   022,   024,   031,   025,   011,	/* scan 16-23 */
	017,   020,   033,   035,  '\r',   CTL,   001,   023,	/* scan 24-31 */
	004,   006,   007,   010,   012,   013,   014,   ';',	/* scan 32-39 */
	'\'',  '`',   SHF,   034,   032,   030,   003,   026,	/* scan 40-47 */
	002,   016,   015,   '<',   '>',   '?',   SHF,   '*',	/* scan 48-55 */
	ALT,   ' ',   CPS,     0,     0,   ' ',     0,     0,	/* scan 56-63 */
	CPS,     0,     0,     0,     0,     0,     0,     0,	/* scan 64-71 */
	  0,     0,     0,     0,     0,     0,     0,     0,	/* scan 72-79 */
	  0,     0,     0,  0177,     0,     0,     0,     0,	/* scan 80-87 */
	  0,     0,   033,   '7',   '4',   '1',     0,   NUM,	/* scan 88-95 */
	'8',   '5',   '2',     0,   STP,   '9',   '6',   '3',	/* scan 96-103*/
	'.',     0,   '*',   '-',   '+',     0,     0,     0,	/*scan 104-111*/
	0,0,0,0,0,0,0,0,	/* scan 112-119 */
	0,0,0,0,0,0,0,0,	/* scan 120-127 */
};


unsigned char shfts, ctls, alts, caps, num, stp;

#define	KBDATAP		0x60	/* kbd data port */
#define	KBSTATUSPORT	0x61	/* kbd status */
#define	KBSTATP		0x64	/* kbd status port */
#define	KBINRDY		0x01
#define	KBOUTRDY	0x02

#define _x__ 0x00  /* Unknown / unmapped */

const unsigned char keycode[] = {
	_x__, 0x43, 0x41, 0x3F, 0x3D, 0x3B, 0x3C, _x__, /* 0x00-0x07 */
	_x__, 0x44, 0x42, 0x40, 0x3E, 0x0F, 0x29, _x__, /* 0x08-0x0F */
	_x__, 0x38, 0x2A, _x__, 0x1D, 0x10, 0x02, _x__, /* 0x10-0x17 */
	_x__, _x__, 0x2C, 0x1F, 0x1E, 0x11, 0x03, _x__, /* 0x18-0x1F */
	_x__, 0x2E, 0x2D, 0x20, 0x12, 0x05, 0x04, _x__, /* 0x20-0x27 */
	_x__, 0x39, 0x2F, 0x21, 0x14, 0x13, 0x06, _x__, /* 0x28-0x2F */
	_x__, 0x31, 0x30, 0x23, 0x22, 0x15, 0x07, _x__, /* 0x30-0x37 */
	_x__, _x__, 0x32, 0x24, 0x16, 0x08, 0x09, _x__, /* 0x38-0x3F */
	_x__, 0x33, 0x25, 0x17, 0x18, 0x0B, 0x0A, _x__, /* 0x40-0x47 */
	_x__, 0x34, 0x35, 0x26, 0x27, 0x19, 0x0C, _x__, /* 0x48-0x4F */
	_x__, _x__, 0x28, _x__, 0x1A, 0x0D, _x__, _x__, /* 0x50-0x57 */
	0x3A, 0x36, 0x1C, 0x1B, _x__, 0x2B, _x__, _x__, /* 0x58-0x5F */
	_x__, _x__, _x__, _x__, _x__, _x__, 0x0E, _x__, /* 0x60-0x67 */
	_x__, 0x4F, _x__, 0x4B, 0x47, _x__, _x__, _x__, /* 0x68-0x6F */
	0x52, 0x53, 0x50, 0x4C, 0x4D, 0x48, 0x01, 0x45, /* 0x70-0x77 */
	_x__, 0x4E, 0x51, 0x4A, _x__, 0x49, 0x46, 0x54, /* 0x78-0x7F */
};

int
kbd(noblock)
	int noblock;
{
	unsigned char dt, brk, act;
	int first = 1;	
loop:
	if (noblock) {
		if ((inb(KBSTATP) & KBINRDY) == 0)
			return (-1);
	} else while((inb(KBSTATP) & KBINRDY) == 0)
		;
	dt = inb(KBDATAP);

	brk = dt & 0x80;	/* brk == 1 on key release */
	dt = dt & 0x7f;		/* keycode */

	act = action[dt];
	if (act&SHF)
		shfts = brk ? 0 : 1;
	if (act&ALT)
		alts = brk ? 0 : 1;
	if (act&NUM)
		if (act&L) {
			/* NUM lock */
			if(!brk)
				num = !num;
		} else
			num = brk ? 0 : 1;
	if (act&CTL)
		ctls = brk ? 0 : 1;
	if (act&CPS)
		if (act&L) {
			/* CAPS lock */
			if(!brk)
				caps = !caps;
		} else
			caps = brk ? 0 : 1;
	if (act&STP)
		if (act&L) {
			if(!brk)
				stp = !stp;
		} else
			stp = brk ? 0 : 1;

	if ((act&ASCII) && !brk) {
		unsigned char chr;

		if (shfts)
			chr = shift[dt];
		else if (ctls)
			chr = ctl[dt];
		else
			chr = unshift[dt];

		if (alts)
			chr |= 0x80;

		if (caps && (chr >= 'a' && chr <= 'z'))
			chr -= 'a' - 'A' ;
#define CTRL(s) (s & 0x1F)			
		if ((chr == '\r') || (chr == '\n') || (chr == CTRL('A')) || (chr == CTRL('S')))
		{
			/* Wait for key up */
			while (1)
			{
				while((inb(KBSTATP) & KBINRDY) == 0) ;
				dt = inb(KBDATAP);
				if (dt & 0x80) /* key up */ break;
			}
		}
		return (chr);
	}
	if (first && brk) return (0);  /* Ignore initial 'key up' codes */
	goto loop;
}

scankbd() {
	return (kbd(1) != -1);
}

kbdreset()
{
	unsigned char c;

	/* Enable interrupts and keyboard controller */
	while (inb(KBSTATP) & KBOUTRDY)
		;
	outb(KBSTATP,0x60);
	while (inb(KBSTATP) & KBOUTRDY)
		;
	outb(KBDATAP,0x4D);

	/* Start keyboard stuff RESET */
	while (inb(KBSTATP) & KBOUTRDY)
		;	/* wait input ready */
	outb(KBDATAP,0xFF);	/* RESET */

	while ((c = inb(KBDATAP)) != 0xFA)
		;
}

CRT_getc()
{
	int c;
	while ((c = kbd(0)) == 0) ;
	return(c);
}

CRT_test()
{
	return ((inb(KBSTATP) & KBINRDY) != 0);
}


_dump_buf_with_offset(unsigned char *p, int s, unsigned char *base)
{
   int i, c;
   if ((unsigned int)s > (unsigned int)p)
   {
   	s = (unsigned int)s - (unsigned int)p;
   }
   while (s > 0)
   {
      if (base)
      {
         _printk("%06X: ", (int)p - (int)base);
      } else
      {
         _printk("%06X: ", p);
      }
      for (i = 0;  i < 16;  i++)
      {
         if (i < s)
         {
            _printk("%02X", p[i] & 0xFF);
         } else
         {
            _printk("  ");
         }
         if ((i % 2) == 1) _printk(" ");
         if ((i % 8) == 7) _printk(" ");
      }
      _printk(" |");
      for (i = 0;  i < 16;  i++)
      {
         if (i < s)
         {
            c = p[i] & 0xFF;
            if ((c < 0x20) || (c >= 0x7F)) c = '.';
         } else
         {
            c = ' ';
         }
         _printk("%c", c);
      }
      _printk("|\n");
      s -= 16;
      p += 16;
   }
}

_dump_buf(unsigned char *p, int s)
{
   _dump_buf_with_offset(p, s, 0);
}


dump_buf_with_offset(unsigned char *p, int s, unsigned char *base)
{
   int i, c;
   if ((unsigned int)s > (unsigned int)p)
   {
   	s = (unsigned int)s - (unsigned int)p;
   }
   while (s > 0)
   {
      if (base)
      {
         printk("%06X: ", (int)p - (int)base);
      } else
      {
         printk("%06X: ", p);
      }
      for (i = 0;  i < 16;  i++)
      {
         if (i < s)
         {
            printk("%02X", p[i] & 0xFF);
         } else
         {
            printk("  ");
         }
         if ((i % 2) == 1) printk(" ");
         if ((i % 8) == 7) printk(" ");
      }
      printk(" |");
      for (i = 0;  i < 16;  i++)
      {
         if (i < s)
         {
            c = p[i] & 0xFF;
            if ((c < 0x20) || (c >= 0x7F)) c = '.';
         } else
         {
            c = ' ';
         }
         printk("%c", c);
      }
      printk("|\n");
      s -= 16;
      p += 16;
   }
}

dump_buf(unsigned char *p, int s)
{
   dump_buf_with_offset(p, s, 0);
}