Home | History | Annotate | Line # | Download | only in common
      1  1.4   martin /*	$NetBSD: cons_fb.c,v 1.4 2008/04/28 20:23:18 martin 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  *
     19  1.1  tsutsui  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  tsutsui  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  tsutsui  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  tsutsui  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  tsutsui  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  tsutsui  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  tsutsui  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  tsutsui  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  tsutsui  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  tsutsui  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  tsutsui  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  tsutsui  */
     31  1.1  tsutsui 
     32  1.1  tsutsui #include <sys/param.h>
     33  1.1  tsutsui #include <sys/systm.h>
     34  1.1  tsutsui 
     35  1.1  tsutsui #include <lib/libsa/stand.h>
     36  1.1  tsutsui #include <lib/libkern/libkern.h>
     37  1.1  tsutsui 
     38  1.1  tsutsui #include <machine/sbd.h>
     39  1.1  tsutsui 
     40  1.1  tsutsui #include "console.h"
     41  1.1  tsutsui 
     42  1.1  tsutsui struct fb fb;
     43  1.1  tsutsui 
     44  1.1  tsutsui void
     45  1.1  tsutsui fb_set_addr(uint32_t fb_addr, uint32_t fb_size, uint32_t font_addr)
     46  1.1  tsutsui {
     47  1.1  tsutsui 
     48  1.1  tsutsui 	fb.fb_addr = (uint8_t *)fb_addr;
     49  1.1  tsutsui 	fb.fb_size = fb_size;
     50  1.1  tsutsui 	fb.font_addr = (uint8_t *)font_addr;
     51  1.1  tsutsui 
     52  1.1  tsutsui 	cons.init = fb_init;
     53  1.1  tsutsui 	cons.putc = fb_drawchar;
     54  1.1  tsutsui 	cons.scroll = fb_scroll;
     55  1.1  tsutsui 	cons.cursor = fb_drawcursor;
     56  1.1  tsutsui }
     57  1.1  tsutsui 
     58  1.1  tsutsui void *
     59  1.1  tsutsui fb_get_addr(void)
     60  1.1  tsutsui {
     61  1.1  tsutsui 
     62  1.1  tsutsui 	return fb.fb_addr;
     63  1.1  tsutsui }
     64  1.1  tsutsui 
     65  1.1  tsutsui void
     66  1.1  tsutsui fb_init(void)
     67  1.1  tsutsui {
     68  1.1  tsutsui 
     69  1.1  tsutsui 	cons.x = X_INIT;
     70  1.1  tsutsui 	cons.y = Y_INIT;
     71  1.3  thorpej 	fb.active = true;
     72  1.1  tsutsui 	fb_clear(0, 0, FB_WIDTH, FB_HEIGHT, CONS_BG);
     73  1.1  tsutsui }
     74  1.1  tsutsui 
     75  1.1  tsutsui void
     76  1.2  thorpej fb_active(bool on)
     77  1.1  tsutsui {
     78  1.1  tsutsui 
     79  1.1  tsutsui 	if (fb.active && !on)
     80  1.1  tsutsui 		printf("FB disabled.\n");
     81  1.1  tsutsui 
     82  1.1  tsutsui 	fb.active = on;
     83  1.1  tsutsui 
     84  1.1  tsutsui 	if (fb.active && on)
     85  1.1  tsutsui 		printf("FB enabled.\n");
     86  1.1  tsutsui }
     87  1.1  tsutsui 
     88  1.1  tsutsui void
     89  1.1  tsutsui fb_scroll(void)
     90  1.1  tsutsui {
     91  1.1  tsutsui 
     92  1.1  tsutsui 	if (!fb.active)
     93  1.1  tsutsui 		return;
     94  1.1  tsutsui #if 0	/* 1-line scroll */
     95  1.1  tsutsui 	cons.y--;
     96  1.1  tsutsui 	fb_copy(0, ROM_FONT_HEIGHT, 0, 0,
     97  1.1  tsutsui 	    FB_WIDTH, FB_HEIGHT * (CONS_HEIGHT - 1));
     98  1.1  tsutsui 	fb_clear(0, cons.y * ROM_FONT_HEIGHT, FB_WIDTH, ROM_FONT_HEIGHT,
     99  1.1  tsutsui 	    CONS_BG);
    100  1.1  tsutsui #else	/* jump scroll */
    101  1.1  tsutsui 	cons.y /= 2;
    102  1.1  tsutsui 	fb_copy(0, cons.y * ROM_FONT_HEIGHT, 0, 0, FB_WIDTH, FB_HEIGHT / 2);
    103  1.1  tsutsui 	fb_clear(0, cons.y *ROM_FONT_HEIGHT, FB_WIDTH, FB_HEIGHT / 2, CONS_BG);
    104  1.1  tsutsui #endif
    105  1.1  tsutsui }
    106  1.1  tsutsui 
    107  1.1  tsutsui #define	MINMAX(x, min, max)						\
    108  1.1  tsutsui 	((x) < (min) ? (min) : ((x) > (max) ? (max) : (x)))
    109  1.1  tsutsui void
    110  1.1  tsutsui fb_clear(int x, int y, int w, int h, int q)
    111  1.1  tsutsui {
    112  1.1  tsutsui 	uint8_t *p;
    113  1.1  tsutsui 	int i, j, k, xend, yend;
    114  1.1  tsutsui 
    115  1.1  tsutsui 	if (!fb.active)
    116  1.1  tsutsui 		return;
    117  1.1  tsutsui 
    118  1.1  tsutsui 	x = MINMAX(x, 0, FB_WIDTH);
    119  1.1  tsutsui 	y = MINMAX(y, 0, FB_HEIGHT);
    120  1.1  tsutsui 	xend = MINMAX(x + w, 0, FB_WIDTH);
    121  1.1  tsutsui 	yend = MINMAX(y + h , 0, FB_HEIGHT);
    122  1.1  tsutsui 
    123  1.1  tsutsui 	p = (uint8_t *)fb.fb_addr + x + y * FB_LINEBYTES;
    124  1.1  tsutsui 
    125  1.1  tsutsui 	j = xend - x;
    126  1.1  tsutsui 	k = j + FB_LINEBYTES - w;
    127  1.1  tsutsui 	for (i = y; i < yend; i++, p+= k)
    128  1.1  tsutsui 		memset(p, q, j);
    129  1.1  tsutsui }
    130  1.1  tsutsui 
    131  1.1  tsutsui void
    132  1.1  tsutsui fb_copy(int x0, int y0, int x1, int y1, int w, int h)
    133  1.1  tsutsui {
    134  1.1  tsutsui 	int x1end, y1end, i, j, k;
    135  1.1  tsutsui 	uint8_t *p, *q;
    136  1.1  tsutsui 
    137  1.1  tsutsui 	if (!fb.active)
    138  1.1  tsutsui 		return;
    139  1.1  tsutsui 
    140  1.1  tsutsui 	x0 = MINMAX(x0, 0, FB_WIDTH);
    141  1.1  tsutsui 	y0 = MINMAX(y0, 0, FB_HEIGHT);
    142  1.1  tsutsui 	x1 = MINMAX(x1, 0, FB_WIDTH);
    143  1.1  tsutsui 	y1 = MINMAX(y1, 0, FB_HEIGHT);
    144  1.1  tsutsui 	x1end = MINMAX(x1 + w, 0, FB_WIDTH);
    145  1.1  tsutsui 	y1end = MINMAX(y1 + h, 0, FB_HEIGHT);
    146  1.1  tsutsui 
    147  1.1  tsutsui 	p = fb.fb_addr + x1 + y1 * FB_LINEBYTES;
    148  1.1  tsutsui 	q = fb.fb_addr + x0 + y0 * FB_LINEBYTES;
    149  1.1  tsutsui 
    150  1.1  tsutsui 	j = x1end - x1;
    151  1.1  tsutsui 	k = j + FB_LINEBYTES - w;
    152  1.1  tsutsui 	for (i = y1; i < y1end; i++, p += k, q += k)
    153  1.1  tsutsui 		memmove(p, q, j);
    154  1.1  tsutsui }
    155  1.1  tsutsui #undef MINMAX
    156  1.1  tsutsui 
    157  1.1  tsutsui void
    158  1.1  tsutsui fb_drawchar(int x, int y, int c)
    159  1.1  tsutsui {
    160  1.1  tsutsui 	uint16_t *font_addr;
    161  1.1  tsutsui 	int font_ofs;
    162  1.1  tsutsui 
    163  1.1  tsutsui 	if (!fb.active)
    164  1.1  tsutsui 		return;
    165  1.1  tsutsui 
    166  1.1  tsutsui 	if ((font_ofs = (c & 0x7f) - 0x20) < 0)
    167  1.1  tsutsui 		return;
    168  1.1  tsutsui 
    169  1.1  tsutsui 	font_addr = (uint16_t *)(fb.font_addr +
    170  1.1  tsutsui 	    font_ofs * sizeof(uint16_t) * ROM_FONT_HEIGHT);
    171  1.1  tsutsui 
    172  1.1  tsutsui 	fb_drawfont(x, y, font_addr);
    173  1.1  tsutsui }
    174  1.1  tsutsui 
    175  1.1  tsutsui void
    176  1.1  tsutsui fb_drawfont(int x, int y, uint16_t *font_addr)
    177  1.1  tsutsui {
    178  1.1  tsutsui 	uint8_t *fb_addr;
    179  1.1  tsutsui 	uint16_t bitmap;
    180  1.1  tsutsui 	int i, j;
    181  1.1  tsutsui 
    182  1.1  tsutsui 	if (!fb.active)
    183  1.1  tsutsui 		return;
    184  1.1  tsutsui 
    185  1.1  tsutsui 	fb_addr = fb.fb_addr + x + y * FB_LINEBYTES;
    186  1.1  tsutsui 
    187  1.1  tsutsui 	for (i = 0;  i < 24; i++) {
    188  1.1  tsutsui 		bitmap = *font_addr++;
    189  1.1  tsutsui 		for (j = 0; j < 12; j++, bitmap <<= 1)
    190  1.1  tsutsui 			fb_addr[j] = bitmap & 0x8000 ? CONS_FG : CONS_BG;
    191  1.1  tsutsui 		fb_addr += FB_LINEBYTES;
    192  1.1  tsutsui 	}
    193  1.1  tsutsui }
    194  1.1  tsutsui 
    195  1.1  tsutsui void
    196  1.1  tsutsui fb_drawcursor(int x, int y)
    197  1.1  tsutsui {
    198  1.1  tsutsui 	uint8_t *fb_addr;
    199  1.1  tsutsui 	int i, j;
    200  1.1  tsutsui 
    201  1.1  tsutsui 	if (!fb.active)
    202  1.1  tsutsui 		return;
    203  1.1  tsutsui 
    204  1.1  tsutsui 	fb_addr = fb.fb_addr + x + y * FB_LINEBYTES;
    205  1.1  tsutsui 	for (i = 0;  i < 24; i++) {
    206  1.1  tsutsui 		for (j = 0; j < 12; j++)
    207  1.1  tsutsui 			fb_addr[j] = fb_addr[j] == CONS_FG ? CONS_BG : CONS_FG;
    208  1.1  tsutsui 		fb_addr += FB_LINEBYTES;
    209  1.1  tsutsui 	}
    210  1.1  tsutsui }
    211