1/*	$NetBSD: consio.c,v 1.12 2024/01/07 07:58:34 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 <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
36enum {
37	ITE = 0,
38	SERIAL = 1,
39} x68k_console_device;
40
41int
42consio_init(int device)
43{
44
45	if (device < 0) {	/* undetermined 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
70int
71getchar(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
87void
88putchar(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
103int
104check_getchar(void)
105{
106	int keycode;
107
108	switch (x68k_console_device) {
109	case ITE:
110		while ((keycode = IOCS_B_KEYSNS()) != 0) {
111			keycode &= 0xff;
112			if (keycode != 0) {
113				/* valid ASCII code */
114				return keycode;
115			}
116			/* discard non ASCII keys (CTRL, OPT.1 etc) */
117			(void)IOCS_B_KEYINP();
118		}
119		/* no input */
120		return 0;
121	case SERIAL:
122		return IOCS_ISNS232C() & 0xff;
123	}
124
125	return -1;
126}
127
128int
129awaitkey_1sec(void)
130{
131	int i, c;
132
133	while (check_getchar())
134		getchar();
135
136	for (i = 0; i < 100 && (c = check_getchar()) == 0; i++) {
137		while (MFP_TIMERC > 100)
138			(void)JOYA;
139		while (MFP_TIMERC <= 100)
140			(void)JOYA;
141	}
142
143	while (check_getchar())
144		getchar();
145
146	return c;
147}
148
149extern void put_image(int, int);
150
151void
152print_title(const char *fmt, ...)
153{
154	va_list ap;
155
156	if (x68k_console_device == ITE) {
157		int y, y1;
158		char *buf = alloca(240); /* about 3 lines */
159		char *p;
160
161		y = y1 = (IOCS_B_LOCATE(-1, -1) & 0xffff) + 1;
162		put_image(8, y*16-6);
163		IOCS_B_LOCATE(0, y+3);
164		IOCS_B_PRINT("\360D\360a\360e\360m\360o\360n "
165			     "\360l\360o\360g\360o "
166			     "\360(\360C\360)\3601\3609\3609\3608\360 "
167			     "\360b\360y\360 "
168			     "\360M\360a\360r\360s\360h\360a\360l\360l\360 "
169			     "\360K\360i\360r\360k\360 "
170			     "\360M\360c\360K\360u\360s\360i\360c\360k\360.");
171		va_start(ap, fmt);
172		vsnprintf(buf, 240, fmt, ap);
173		va_end(ap);
174		while ((p = strchr(buf, '\n')) != 0) {
175			*p = 0;
176			IOCS_B_LOCATE(9, ++y);
177			IOCS_B_PRINT(buf);
178			buf = p+1;
179		}
180		IOCS_B_LOCATE(9, ++y);
181		IOCS_B_PRINT(buf);
182		IOCS_B_LOCATE(0, y1+5);
183	} else {
184		va_start(ap, fmt);
185		vprintf(fmt, ap);
186		va_end(ap);
187		printf("\n");
188	}
189}
190