Home | History | Annotate | Line # | Download | only in common
cons_fb.c revision 1.2
      1 /*	$NetBSD: cons_fb.c,v 1.2 2007/02/21 22:59:41 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2004 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by UCHIYAMA Yasushi.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 
     42 #include <lib/libsa/stand.h>
     43 #include <lib/libkern/libkern.h>
     44 
     45 #include <machine/sbd.h>
     46 
     47 #include "console.h"
     48 
     49 struct fb fb;
     50 
     51 void
     52 fb_set_addr(uint32_t fb_addr, uint32_t fb_size, uint32_t font_addr)
     53 {
     54 
     55 	fb.fb_addr = (uint8_t *)fb_addr;
     56 	fb.fb_size = fb_size;
     57 	fb.font_addr = (uint8_t *)font_addr;
     58 
     59 	cons.init = fb_init;
     60 	cons.putc = fb_drawchar;
     61 	cons.scroll = fb_scroll;
     62 	cons.cursor = fb_drawcursor;
     63 }
     64 
     65 void *
     66 fb_get_addr(void)
     67 {
     68 
     69 	return fb.fb_addr;
     70 }
     71 
     72 void
     73 fb_init(void)
     74 {
     75 
     76 	cons.x = X_INIT;
     77 	cons.y = Y_INIT;
     78 	fb.active = TRUE;
     79 	fb_clear(0, 0, FB_WIDTH, FB_HEIGHT, CONS_BG);
     80 }
     81 
     82 void
     83 fb_active(bool on)
     84 {
     85 
     86 	if (fb.active && !on)
     87 		printf("FB disabled.\n");
     88 
     89 	fb.active = on;
     90 
     91 	if (fb.active && on)
     92 		printf("FB enabled.\n");
     93 }
     94 
     95 void
     96 fb_scroll(void)
     97 {
     98 
     99 	if (!fb.active)
    100 		return;
    101 #if 0	/* 1-line scroll */
    102 	cons.y--;
    103 	fb_copy(0, ROM_FONT_HEIGHT, 0, 0,
    104 	    FB_WIDTH, FB_HEIGHT * (CONS_HEIGHT - 1));
    105 	fb_clear(0, cons.y * ROM_FONT_HEIGHT, FB_WIDTH, ROM_FONT_HEIGHT,
    106 	    CONS_BG);
    107 #else	/* jump scroll */
    108 	cons.y /= 2;
    109 	fb_copy(0, cons.y * ROM_FONT_HEIGHT, 0, 0, FB_WIDTH, FB_HEIGHT / 2);
    110 	fb_clear(0, cons.y *ROM_FONT_HEIGHT, FB_WIDTH, FB_HEIGHT / 2, CONS_BG);
    111 #endif
    112 }
    113 
    114 #define	MINMAX(x, min, max)						\
    115 	((x) < (min) ? (min) : ((x) > (max) ? (max) : (x)))
    116 void
    117 fb_clear(int x, int y, int w, int h, int q)
    118 {
    119 	uint8_t *p;
    120 	int i, j, k, xend, yend;
    121 
    122 	if (!fb.active)
    123 		return;
    124 
    125 	x = MINMAX(x, 0, FB_WIDTH);
    126 	y = MINMAX(y, 0, FB_HEIGHT);
    127 	xend = MINMAX(x + w, 0, FB_WIDTH);
    128 	yend = MINMAX(y + h , 0, FB_HEIGHT);
    129 
    130 	p = (uint8_t *)fb.fb_addr + x + y * FB_LINEBYTES;
    131 
    132 	j = xend - x;
    133 	k = j + FB_LINEBYTES - w;
    134 	for (i = y; i < yend; i++, p+= k)
    135 		memset(p, q, j);
    136 }
    137 
    138 void
    139 fb_copy(int x0, int y0, int x1, int y1, int w, int h)
    140 {
    141 	int x1end, y1end, i, j, k;
    142 	uint8_t *p, *q;
    143 
    144 	if (!fb.active)
    145 		return;
    146 
    147 	x0 = MINMAX(x0, 0, FB_WIDTH);
    148 	y0 = MINMAX(y0, 0, FB_HEIGHT);
    149 	x1 = MINMAX(x1, 0, FB_WIDTH);
    150 	y1 = MINMAX(y1, 0, FB_HEIGHT);
    151 	x1end = MINMAX(x1 + w, 0, FB_WIDTH);
    152 	y1end = MINMAX(y1 + h, 0, FB_HEIGHT);
    153 
    154 	p = fb.fb_addr + x1 + y1 * FB_LINEBYTES;
    155 	q = fb.fb_addr + x0 + y0 * FB_LINEBYTES;
    156 
    157 	j = x1end - x1;
    158 	k = j + FB_LINEBYTES - w;
    159 	for (i = y1; i < y1end; i++, p += k, q += k)
    160 		memmove(p, q, j);
    161 }
    162 #undef MINMAX
    163 
    164 void
    165 fb_drawchar(int x, int y, int c)
    166 {
    167 	uint16_t *font_addr;
    168 	int font_ofs;
    169 
    170 	if (!fb.active)
    171 		return;
    172 
    173 	if ((font_ofs = (c & 0x7f) - 0x20) < 0)
    174 		return;
    175 
    176 	font_addr = (uint16_t *)(fb.font_addr +
    177 	    font_ofs * sizeof(uint16_t) * ROM_FONT_HEIGHT);
    178 
    179 	fb_drawfont(x, y, font_addr);
    180 }
    181 
    182 void
    183 fb_drawfont(int x, int y, uint16_t *font_addr)
    184 {
    185 	uint8_t *fb_addr;
    186 	uint16_t bitmap;
    187 	int i, j;
    188 
    189 	if (!fb.active)
    190 		return;
    191 
    192 	fb_addr = fb.fb_addr + x + y * FB_LINEBYTES;
    193 
    194 	for (i = 0;  i < 24; i++) {
    195 		bitmap = *font_addr++;
    196 		for (j = 0; j < 12; j++, bitmap <<= 1)
    197 			fb_addr[j] = bitmap & 0x8000 ? CONS_FG : CONS_BG;
    198 		fb_addr += FB_LINEBYTES;
    199 	}
    200 }
    201 
    202 void
    203 fb_drawcursor(int x, int y)
    204 {
    205 	uint8_t *fb_addr;
    206 	int i, j;
    207 
    208 	if (!fb.active)
    209 		return;
    210 
    211 	fb_addr = fb.fb_addr + x + y * FB_LINEBYTES;
    212 	for (i = 0;  i < 24; i++) {
    213 		for (j = 0; j < 12; j++)
    214 			fb_addr[j] = fb_addr[j] == CONS_FG ? CONS_BG : CONS_FG;
    215 		fb_addr += FB_LINEBYTES;
    216 	}
    217 }
    218