1 /* $NetBSD: consio.c,v 1.8 2011/04/12 14:07:35 tsutsui 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 46 if (device < 0) { /* undetemined yet */ 47 if (KEYCTRL & 8) 48 device = ITE; 49 else { 50 IOCS_B_PRINT("No keyboard; " 51 "switching to serial console..."); 52 device = SERIAL; 53 } 54 } 55 56 switch (device) { 57 case ITE: 58 x68k_console_device = ITE; 59 /* set palette here */ 60 IOCS_OS_CURON(); 61 break; 62 case SERIAL: 63 x68k_console_device = SERIAL; 64 IOCS_OS_CUROF(); 65 IOCS_SET232C(SERPARAM); 66 } 67 68 return x68k_console_device; 69 } 70 71 int 72 getchar(void) 73 { 74 int r; 75 76 switch (x68k_console_device) { 77 case ITE: 78 while ((r = IOCS_B_KEYINP() & 0xff) == 0); 79 return r; 80 case SERIAL: 81 while ((r = IOCS_INP232C() & 0xff) == 0); 82 return r; 83 } 84 85 return -1; 86 } 87 88 void 89 putchar(int c) 90 { 91 92 if (c == '\n') 93 putchar('\r'); 94 switch (x68k_console_device) { 95 case ITE: 96 IOCS_B_PUTC(c); 97 break; 98 case SERIAL: 99 IOCS_OUT232C(c); 100 break; 101 } 102 } 103 104 int 105 check_getchar(void) 106 { 107 108 switch (x68k_console_device) { 109 case ITE: 110 return IOCS_B_KEYSNS() & 0xff; 111 case SERIAL: 112 return IOCS_ISNS232C() & 0xff; 113 } 114 115 return -1; 116 } 117 118 int 119 awaitkey_1sec(void) 120 { 121 int i, c; 122 123 while (check_getchar()) 124 getchar(); 125 126 for (i = 0; i < 100 && (c = check_getchar()) == 0; i++) { 127 while (MFP_TIMERC > 100) 128 (void)JOYA; 129 while (MFP_TIMERC <= 100) 130 (void)JOYA; 131 } 132 133 while (check_getchar()) 134 getchar(); 135 136 return c; 137 } 138 139 extern void put_image(int, int); 140 141 void 142 print_title(const char *fmt, ...) 143 { 144 va_list ap; 145 146 if (x68k_console_device == ITE) { 147 int y, y1; 148 char *buf = alloca(240); /* about 3 lines */ 149 char *p; 150 151 y = y1 = (IOCS_B_LOCATE(-1, -1) & 0xffff) + 1; 152 put_image(8, y*16-6); 153 IOCS_B_LOCATE(0, y+3); 154 IOCS_B_PRINT("\360D\360a\360e\360m\360o\360n " 155 "\360l\360o\360g\360o " 156 "\360(\360C\360)\3601\3609\3609\3608\360 " 157 "\360b\360y\360 " 158 "\360M\360a\360r\360s\360h\360a\360l\360l\360 " 159 "\360K\360i\360r\360k\360 " 160 "\360M\360c\360K\360u\360s\360i\360c\360k\360."); 161 va_start(ap, fmt); 162 vsnprintf(buf, 240, fmt, ap); 163 va_end(ap); 164 while ((p = strchr(buf, '\n')) != 0) { 165 *p = 0; 166 IOCS_B_LOCATE(9, ++y); 167 IOCS_B_PRINT(buf); 168 buf = p+1; 169 } 170 IOCS_B_LOCATE(9, ++y); 171 IOCS_B_PRINT(buf); 172 IOCS_B_LOCATE(0, y1+5); 173 } else { 174 va_start(ap, fmt); 175 vprintf(fmt, ap); 176 va_end(ap); 177 printf("\n"); 178 } 179 } 180