console.c revision 1.4 1 /* $NetBSD: console.c,v 1.4 1998/08/22 10:55:37 scw Exp $ */
2
3 /*
4 *
5 * Copyright (c) 1995 Charles D. Cranor and Seth Widoff
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Charles D. Cranor
19 * and Seth Widoff.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34 /*
35 * console i/o
36 */
37
38 #include "sboot.h"
39
40 /*
41 * hardware
42 */
43
44 struct zs_hw {
45 volatile u_char ctl;
46 volatile u_char data;
47 };
48
49 struct zs_hw *zs = (struct zs_hw *)CONS_ZS_ADDR;
50
51 /*
52 * consinit: init console
53 */
54
55 consinit()
56
57 {
58 int mark = time();
59 int rr1;
60 while (1) {
61 if (time() > mark + 5) break;
62 zs->ctl = 1; rr1 = zs->ctl;
63 zs->ctl = 0;
64 if ((rr1 & 0x1) == 1 && (zs->ctl & 0x4) == 4) break; /* zs_drain! */
65 }
66 zs->ctl = 9; zs->ctl = 0x00; /* clear interrupt */
67 zs->ctl = 4; zs->ctl = 0x44; /* 16x clk, 1 stop bit */
68 zs->ctl = 5; zs->ctl = 0xea; /* DTR on, 8 bit xmit, xmit on, RTS on */
69 zs->ctl = 3; zs->ctl = 0xc1; /* 8 bit recv, auto cd_cts, recv on */
70 zs->ctl = 1; zs->ctl = 0x00; /* no intrs */
71 zs->ctl = 2; zs->ctl = 0x00; /* no vector */
72 zs->ctl = 10; zs->ctl = 0x00; /* */
73 zs->ctl = 11; zs->ctl = 0x50; /* clocking options */
74 zs->ctl = 12; zs->ctl = 0x0e; /* 9600 baud, part 1 */
75 zs->ctl = 13; zs->ctl = 0x00; /* 9600 baud, part 2 */
76 zs->ctl = 14; zs->ctl = 0x03; /* more clocking options */
77 zs->ctl = 15; zs->ctl = 0x00; /* clear intrs */
78 }
79
80 /*
81 * putchar: put char to console
82 */
83
84 void putchar(char c)
85 {
86 if (c == '\n') putchar('\r'); /* avoid the need for \r\n in printf */
87 zs->ctl = 0;
88 while ((zs->ctl & 0x04) == 0) {
89 zs->ctl = 0;
90 }
91 zs->ctl = 8;
92 zs->ctl = c;
93 }
94
95 /*
96 * cngetc: get 1 char from console
97 */
98
99 char cngetc ()
100 {
101 zs->ctl = 0;
102 while ((zs->ctl & 0x1) == 0) {
103 zs->ctl = 0;
104 }
105 zs->ctl = 8;
106 return zs->ctl;
107 }
108
109 /*
110 * puts: put string to console
111 */
112
113 void puts ( char * str )
114 {
115 while ( *str != '\0' ) {
116 putchar(*str);
117 str++;
118 }
119 }
120
121 /*
122 * ngets: get string from console
123 */
124
125 char *ngets ( char * str, int size )
126 {
127 int i = 0;
128 while ( (i < size - 1) && (str[i] = cngetc()) != '\r') {
129 if ( str[i] == '\b' || str[i] == 0x7F ) {
130 if ( i == 0) continue;
131 i--;
132 puts("\b \b");
133 continue;
134 }
135 putchar(str[i]);
136 i++;
137 }
138 puts("\n");
139 str[i] = '\0';
140 return(&str[i]);
141 }
142
143