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