xref: /netbsd/sys/arch/x68k/stand/libsa/consio.c (revision 6550d01e)
1 /*	$NetBSD: consio.c,v 1.5 2007/11/11 05:20:26 isaki Exp $	*/
2 
3 /*
4  * Copyright (c) 2001 MINOURA Makoto.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <machine/stdarg.h>
29 #include <lib/libkern/libkern.h>
30 #include <lib/libsa/stand.h>
31 
32 #include "libx68k.h"
33 
34 #include "iocs.h"
35 #include "consio.h"
36 
37 enum {
38 	ITE = 0,
39 	SERIAL = 1,
40 } x68k_console_device;
41 
42 int
43 consio_init(int device)
44 {
45 	if (device < 0) {	/* undetemined yet */
46 		if (KEYCTRL & 8)
47 			device = ITE;
48 		else {
49 			IOCS_B_PRINT ("No keyboard; "
50 				      "switching to serial console...");
51 			device = SERIAL;
52 		}
53 	}
54 
55 	switch (device) {
56 	case ITE:
57 		x68k_console_device = ITE;
58 		/* set palette here */
59 		IOCS_OS_CURON ();
60 		break;
61 	case SERIAL:
62 		x68k_console_device = SERIAL;
63 		IOCS_OS_CUROF ();
64 		IOCS_SET232C (SERPARAM);
65 	}
66 
67 	return x68k_console_device;
68 }
69 
70 int
71 getchar(void)
72 {
73 	int r;
74 
75 	switch (x68k_console_device) {
76 	case ITE:
77 		while ((r = IOCS_B_KEYINP () & 0xff) == 0);
78 		return r;
79 	case SERIAL:
80 		while ((r = IOCS_INP232C () & 0xff) == 0);
81 		return r;
82 	}
83 
84 	return -1;
85 }
86 
87 void
88 putchar(int c)
89 {
90 	if (c == '\n')
91 		putchar('\r');
92 	switch (x68k_console_device) {
93 	case ITE:
94 		IOCS_B_PUTC (c);
95 	case SERIAL:
96 		IOCS_OUT232C (c);
97 	}
98 }
99 
100 int
101 check_getchar(void)
102 {
103 	switch (x68k_console_device) {
104 	case ITE:
105 		return IOCS_B_KEYSNS () & 0xff;
106 	case SERIAL:
107 		return IOCS_ISNS232C () & 0xff;
108 	}
109 
110 	return -1;
111 }
112 
113 int
114 awaitkey_1sec(void)
115 {
116 	int i, c;
117 
118 	while (check_getchar())
119 		getchar();
120 
121 	for (i = 0; i < 100 && (c = check_getchar()) == 0; i++) {
122 		while (MFP_TIMERC > 100)
123 			(void)JOYA;
124 		while (MFP_TIMERC <= 100)
125 			(void)JOYA;
126 	}
127 
128 	while (check_getchar())
129 		getchar();
130 
131 	return c;
132 }
133 
134 __dead void
135 panic(const char *fmt,...)
136 {
137 	va_list ap;
138 
139 	va_start(ap, fmt);
140 
141 	printf(fmt, ap);
142 	printf("\n");
143 	va_end(ap);
144 
145 	exit(1);
146 }
147 
148 extern void put_image(int, int);
149 
150 void
151 print_title(const char *fmt, ...)
152 {
153 	va_list ap;
154 
155 	if (x68k_console_device == ITE) {
156 		int y, y1;
157 		char *buf = alloca(240); /* about 3 lines */
158 		char *p;
159 
160 		y = y1 = (IOCS_B_LOCATE(-1, -1) & 0xffff) + 1;
161 		put_image(8, y*16-6);
162 		IOCS_B_LOCATE(0, y+3);
163 		IOCS_B_PRINT("\360D\360a\360e\360m\360o\360n "
164 			     "\360l\360o\360g\360o "
165 			     "\360(\360C\360)\3601\3609\3609\3608\360 "
166 			     "\360b\360y\360 "
167 			     "\360M\360a\360r\360s\360h\360a\360l\360l\360 "
168 			     "\360K\360i\360r\360k\360 "
169 			     "\360M\360c\360K\360u\360s\360i\360c\360k\360.");
170 		va_start(ap, fmt);
171 		vsnprintf(buf, 240, fmt, ap);
172 		va_end(ap);
173 		while ((p = strchr(buf, '\n')) != 0) {
174 			*p = 0;
175 			IOCS_B_LOCATE(9, ++y);
176 			IOCS_B_PRINT(buf);
177 			buf = p+1;
178 		}
179 		IOCS_B_LOCATE(9, ++y);
180 		IOCS_B_PRINT(buf);
181 		IOCS_B_LOCATE(0, y1+5);
182 	} else {
183 		va_start(ap, fmt);
184 		vprintf(fmt, ap);
185 		va_end(ap);
186 		printf("\n");
187 	}
188 }
189