1 /* $NetBSD: pcdisplay_subr.c,v 1.27 2004/07/28 12:34:04 jmmv Exp $ */ 2 3 /* 4 * Copyright (c) 1995, 1996 Carnegie-Mellon University. 5 * All rights reserved. 6 * 7 * Author: Chris G. Demetriou 8 * 9 * Permission to use, copy, modify and distribute this software and 10 * its documentation is hereby granted, provided that both the copyright 11 * notice and this permission notice appear in all copies of the 12 * software, derivative works or modified versions, and any portions 13 * thereof, and that both notices appear in supporting documentation. 14 * 15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 18 * 19 * Carnegie Mellon requests users of this software to return to 20 * 21 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 22 * School of Computer Science 23 * Carnegie Mellon University 24 * Pittsburgh PA 15213-3890 25 * 26 * any improvements or extensions that they make and grant Carnegie the 27 * rights to redistribute these changes. 28 */ 29 30 #include <sys/cdefs.h> 31 __KERNEL_RCSID(0, "$NetBSD: pcdisplay_subr.c,v 1.27 2004/07/28 12:34:04 jmmv Exp $"); 32 33 #include "opt_wsdisplay_compat.h" /* for WSDISPLAY_CHARFUNCS */ 34 #include "opt_wsmsgattrs.h" /* for WSDISPLAY_CUSTOM_OUTPUT */ 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/device.h> 39 #include <machine/bus.h> 40 41 #include <dev/ic/mc6845reg.h> 42 #include <dev/ic/pcdisplayvar.h> 43 #include <dev/wscons/wsconsio.h> 44 45 #include <dev/wscons/wsdisplayvar.h> 46 47 void 48 pcdisplay_cursor_init(scr, existing) 49 struct pcdisplayscreen *scr; 50 int existing; 51 { 52 #ifdef PCDISPLAY_SOFTCURSOR 53 bus_space_tag_t memt; 54 bus_space_handle_t memh; 55 int off; 56 57 pcdisplay_6845_write(scr->hdl, curstart, 0x10); 58 pcdisplay_6845_write(scr->hdl, curend, 0x10); 59 60 if (existing) { 61 /* 62 * This is the first screen. At this point, scr->mem is NULL 63 * (no backing store), so we can't use pcdisplay_cursor() to 64 * do this. 65 */ 66 memt = scr->hdl->ph_memt; 67 memh = scr->hdl->ph_memh; 68 off = (scr->cursorrow * scr->type->ncols + scr->cursorcol) * 2 69 + scr->dispoffset; 70 71 scr->cursortmp = bus_space_read_2(memt, memh, off); 72 bus_space_write_2(memt, memh, off, scr->cursortmp ^ 0x7700); 73 } else 74 scr->cursortmp = 0; 75 #else 76 /* 77 * Firmware might not have initialized the cursor shape. Make 78 * sure there's something we can see. 79 * Don't touch the hardware if this is not the first screen. 80 */ 81 if (existing) { 82 pcdisplay_6845_write(scr->hdl, curstart, 83 scr->type->fontheight - 2); 84 pcdisplay_6845_write(scr->hdl, curend, 85 scr->type->fontheight - 1); 86 } 87 #endif 88 scr->cursoron = 1; 89 } 90 91 void 92 pcdisplay_cursor(id, on, row, col) 93 void *id; 94 int on, row, col; 95 { 96 #ifdef PCDISPLAY_SOFTCURSOR 97 struct pcdisplayscreen *scr = id; 98 bus_space_tag_t memt = scr->hdl->ph_memt; 99 bus_space_handle_t memh = scr->hdl->ph_memh; 100 int off; 101 102 /* Remove old cursor image */ 103 if (scr->cursoron) { 104 off = scr->cursorrow * scr->type->ncols + scr->cursorcol; 105 if (scr->active) 106 bus_space_write_2(memt, memh, scr->dispoffset + off * 2, 107 scr->cursortmp); 108 else 109 scr->mem[off] = scr->cursortmp; 110 } 111 112 scr->cursorrow = row; 113 scr->cursorcol = col; 114 115 if ((scr->cursoron = on) == 0) 116 return; 117 118 off = (scr->cursorrow * scr->type->ncols + scr->cursorcol); 119 if (scr->active) { 120 off = off * 2 + scr->dispoffset; 121 scr->cursortmp = bus_space_read_2(memt, memh, off); 122 bus_space_write_2(memt, memh, off, scr->cursortmp ^ 0x7700); 123 } else { 124 scr->cursortmp = scr->mem[off]; 125 scr->mem[off] = scr->cursortmp ^ 0x7700; 126 } 127 #else /* PCDISPLAY_SOFTCURSOR */ 128 struct pcdisplayscreen *scr = id; 129 int pos; 130 131 scr->cursorrow = row; 132 scr->cursorcol = col; 133 scr->cursoron = on; 134 135 if (scr->active) { 136 if (!on) 137 pos = 0x3fff; 138 else 139 pos = scr->dispoffset / 2 140 + row * scr->type->ncols + col; 141 142 pcdisplay_6845_write(scr->hdl, cursorh, pos >> 8); 143 pcdisplay_6845_write(scr->hdl, cursorl, pos); 144 } 145 #endif /* PCDISPLAY_SOFTCURSOR */ 146 } 147 148 #if 0 149 unsigned int 150 pcdisplay_mapchar_simple(id, uni) 151 void *id; 152 int uni; 153 { 154 if (uni < 128) 155 return (uni); 156 157 return (1); /* XXX ??? smiley */ 158 } 159 #endif 160 161 void 162 pcdisplay_putchar(id, row, col, c, attr) 163 void *id; 164 int row, col; 165 u_int c; 166 long attr; 167 { 168 struct pcdisplayscreen *scr = id; 169 bus_space_tag_t memt = scr->hdl->ph_memt; 170 bus_space_handle_t memh = scr->hdl->ph_memh; 171 int off; 172 173 off = row * scr->type->ncols + col; 174 175 if (scr->active) 176 bus_space_write_2(memt, memh, scr->dispoffset + off * 2, 177 c | (attr << 8)); 178 else 179 scr->mem[off] = c | (attr << 8); 180 181 scr->visibleoffset = scr->dispoffset; 182 } 183 184 void 185 pcdisplay_copycols(id, row, srccol, dstcol, ncols) 186 void *id; 187 int row, srccol, dstcol, ncols; 188 { 189 struct pcdisplayscreen *scr = id; 190 bus_space_tag_t memt = scr->hdl->ph_memt; 191 bus_space_handle_t memh = scr->hdl->ph_memh; 192 bus_size_t srcoff, dstoff; 193 194 srcoff = dstoff = row * scr->type->ncols; 195 srcoff += srccol; 196 dstoff += dstcol; 197 198 if (scr->active) 199 bus_space_copy_region_2(memt, memh, 200 scr->dispoffset + srcoff * 2, 201 memh, scr->dispoffset + dstoff * 2, 202 ncols); 203 else 204 memcpy(&scr->mem[dstoff], &scr->mem[srcoff], ncols * 2); 205 } 206 207 void 208 pcdisplay_erasecols(id, row, startcol, ncols, fillattr) 209 void *id; 210 int row, startcol, ncols; 211 long fillattr; 212 { 213 struct pcdisplayscreen *scr = id; 214 bus_space_tag_t memt = scr->hdl->ph_memt; 215 bus_space_handle_t memh = scr->hdl->ph_memh; 216 bus_size_t off; 217 u_int16_t val; 218 int i; 219 220 off = row * scr->type->ncols + startcol; 221 222 val = (fillattr << 8) | ' '; 223 224 if (scr->active) 225 bus_space_set_region_2(memt, memh, scr->dispoffset + off * 2, 226 val, ncols); 227 else 228 for (i = 0; i < ncols; i++) 229 scr->mem[off + i] = val; 230 } 231 232 void 233 pcdisplay_copyrows(id, srcrow, dstrow, nrows) 234 void *id; 235 int srcrow, dstrow, nrows; 236 { 237 struct pcdisplayscreen *scr = id; 238 bus_space_tag_t memt = scr->hdl->ph_memt; 239 bus_space_handle_t memh = scr->hdl->ph_memh; 240 int ncols = scr->type->ncols; 241 bus_size_t srcoff, dstoff; 242 243 srcoff = srcrow * ncols + 0; 244 dstoff = dstrow * ncols + 0; 245 246 if (scr->active) 247 bus_space_copy_region_2(memt, memh, 248 scr->dispoffset + srcoff * 2, 249 memh, scr->dispoffset + dstoff * 2, 250 nrows * ncols); 251 else 252 memcpy(&scr->mem[dstoff], &scr->mem[srcoff], 253 nrows * ncols * 2); 254 } 255 256 void 257 pcdisplay_eraserows(id, startrow, nrows, fillattr) 258 void *id; 259 int startrow, nrows; 260 long fillattr; 261 { 262 struct pcdisplayscreen *scr = id; 263 bus_space_tag_t memt = scr->hdl->ph_memt; 264 bus_space_handle_t memh = scr->hdl->ph_memh; 265 bus_size_t off, count; 266 u_int16_t val; 267 u_int i; 268 269 off = startrow * scr->type->ncols; 270 count = nrows * scr->type->ncols; 271 272 val = (fillattr << 8) | ' '; 273 274 if (scr->active) 275 bus_space_set_region_2(memt, memh, scr->dispoffset + off * 2, 276 val, count); 277 else 278 for (i = 0; i < count; i++) 279 scr->mem[off + i] = val; 280 } 281 282 #ifdef WSDISPLAY_CUSTOM_OUTPUT 283 void 284 pcdisplay_replaceattr(id, oldattr, newattr) 285 void *id; 286 long oldattr, newattr; 287 { 288 struct pcdisplayscreen *scr = id; 289 bus_space_tag_t memt = scr->hdl->ph_memt; 290 bus_space_handle_t memh = scr->hdl->ph_memh; 291 int off; 292 uint16_t chardata; 293 294 if (scr->active) 295 for (off = 0; off < scr->type->nrows * scr->type->ncols; 296 off++) { 297 chardata = bus_space_read_2(memt, memh, 298 scr->dispoffset + off * 2); 299 if ((long)(chardata >> 8) == oldattr) 300 bus_space_write_2(memt, memh, 301 scr->dispoffset + off * 2, 302 ((u_int16_t)(newattr << 8)) | 303 (chardata & 0x00FF)); 304 } 305 else 306 for (off = 0; off < scr->type->nrows * scr->type->ncols; 307 off++) { 308 chardata = scr->mem[off]; 309 if ((long)(chardata >> 8) == oldattr) 310 scr->mem[off] = ((u_int16_t)(newattr << 8)) | 311 (chardata & 0x00FF); 312 } 313 } 314 #endif /* WSDISPLAY_CUSTOM_OUTPUT */ 315 316 #ifdef WSDISPLAY_CHARFUNCS 317 int 318 pcdisplay_getwschar(id, wschar) 319 void *id; 320 struct wsdisplay_char *wschar; 321 { 322 struct pcdisplayscreen *scr = id; 323 bus_space_tag_t memt = scr->hdl->ph_memt; 324 bus_space_handle_t memh = scr->hdl->ph_memh; 325 int off; 326 uint16_t chardata; 327 uint8_t attrbyte; 328 329 off = wschar->row * scr->type->ncols + wschar->col; 330 if (off >= scr->type->ncols * scr->type->nrows) 331 return -1; 332 333 if (scr->active) 334 chardata = bus_space_read_2(memt, memh, 335 scr->dispoffset + off * 2); 336 else 337 chardata = scr->mem[off]; 338 339 wschar->letter = (chardata & 0x00FF); 340 wschar->flags = 0; 341 attrbyte = (chardata & 0xFF00) >> 8; 342 if ((attrbyte & 0x08)) wschar->flags |= WSDISPLAY_CHAR_BRIGHT; 343 if ((attrbyte & 0x80)) wschar->flags |= WSDISPLAY_CHAR_BLINK; 344 wschar->foreground = attrbyte & 0x07; 345 wschar->background = (attrbyte >> 4) & 0x07; 346 347 return 0; 348 } 349 350 int 351 pcdisplay_putwschar(id, wschar) 352 void *id; 353 struct wsdisplay_char *wschar; 354 { 355 struct pcdisplayscreen *scr = id; 356 bus_space_tag_t memt = scr->hdl->ph_memt; 357 bus_space_handle_t memh = scr->hdl->ph_memh; 358 int off; 359 uint16_t chardata; 360 uint8_t attrbyte; 361 362 off = wschar->row * scr->type->ncols + wschar->col; 363 if (off >= (scr->type->ncols * scr->type->nrows)) 364 return -1; 365 366 attrbyte = wschar->background & 0x07; 367 if (wschar->flags & WSDISPLAY_CHAR_BLINK) attrbyte |= 0x08; 368 attrbyte <<= 4; 369 attrbyte |= wschar->foreground & 0x07; 370 if (wschar->flags & WSDISPLAY_CHAR_BRIGHT) attrbyte |= 0x08; 371 chardata = (attrbyte << 8) | wschar->letter; 372 373 if (scr->active) 374 bus_space_write_2(memt, memh, scr->dispoffset + off * 2, 375 chardata); 376 else 377 scr->mem[off] = chardata; 378 379 return 0; 380 } 381 #endif /* WSDISPLAY_CHARFUNCS */ 382