consio.c revision 1.9 1 /* $NetBSD: consio.c,v 1.9 2011/07/17 20:54:49 joerg 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 <lib/libkern/libkern.h>
29 #include <lib/libsa/stand.h>
30
31 #include "libx68k.h"
32
33 #include "iocs.h"
34 #include "consio.h"
35
36 enum {
37 ITE = 0,
38 SERIAL = 1,
39 } x68k_console_device;
40
41 int
42 consio_init(int device)
43 {
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
91 if (c == '\n')
92 putchar('\r');
93 switch (x68k_console_device) {
94 case ITE:
95 IOCS_B_PUTC(c);
96 break;
97 case SERIAL:
98 IOCS_OUT232C(c);
99 break;
100 }
101 }
102
103 int
104 check_getchar(void)
105 {
106
107 switch (x68k_console_device) {
108 case ITE:
109 return IOCS_B_KEYSNS() & 0xff;
110 case SERIAL:
111 return IOCS_ISNS232C() & 0xff;
112 }
113
114 return -1;
115 }
116
117 int
118 awaitkey_1sec(void)
119 {
120 int i, c;
121
122 while (check_getchar())
123 getchar();
124
125 for (i = 0; i < 100 && (c = check_getchar()) == 0; i++) {
126 while (MFP_TIMERC > 100)
127 (void)JOYA;
128 while (MFP_TIMERC <= 100)
129 (void)JOYA;
130 }
131
132 while (check_getchar())
133 getchar();
134
135 return c;
136 }
137
138 extern void put_image(int, int);
139
140 void
141 print_title(const char *fmt, ...)
142 {
143 va_list ap;
144
145 if (x68k_console_device == ITE) {
146 int y, y1;
147 char *buf = alloca(240); /* about 3 lines */
148 char *p;
149
150 y = y1 = (IOCS_B_LOCATE(-1, -1) & 0xffff) + 1;
151 put_image(8, y*16-6);
152 IOCS_B_LOCATE(0, y+3);
153 IOCS_B_PRINT("\360D\360a\360e\360m\360o\360n "
154 "\360l\360o\360g\360o "
155 "\360(\360C\360)\3601\3609\3609\3608\360 "
156 "\360b\360y\360 "
157 "\360M\360a\360r\360s\360h\360a\360l\360l\360 "
158 "\360K\360i\360r\360k\360 "
159 "\360M\360c\360K\360u\360s\360i\360c\360k\360.");
160 va_start(ap, fmt);
161 vsnprintf(buf, 240, fmt, ap);
162 va_end(ap);
163 while ((p = strchr(buf, '\n')) != 0) {
164 *p = 0;
165 IOCS_B_LOCATE(9, ++y);
166 IOCS_B_PRINT(buf);
167 buf = p+1;
168 }
169 IOCS_B_LOCATE(9, ++y);
170 IOCS_B_PRINT(buf);
171 IOCS_B_LOCATE(0, y1+5);
172 } else {
173 va_start(ap, fmt);
174 vprintf(fmt, ap);
175 va_end(ap);
176 printf("\n");
177 }
178 }
179