Subversion Repositories HelenOS

Rev

Rev 1994 | Rev 2054 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
836 palkovsky 1
/*
2
 * Copyright (C) 2006 Ondrej Palkovsky
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
1790 jermar 29
/** @addtogroup genarch	
1702 cejka 30
 * @{
31
 */
32
/** @file
33
 */
34
 
837 palkovsky 35
#include <genarch/fb/font-8x16.h>
1993 decky 36
#include <genarch/fb/visuals.h>
837 palkovsky 37
#include <genarch/fb/fb.h>
836 palkovsky 38
#include <console/chardev.h>
39
#include <console/console.h>
1317 vana 40
#include <sysinfo/sysinfo.h>
1316 jermar 41
#include <mm/slab.h>
1371 decky 42
#include <align.h>
839 palkovsky 43
#include <panic.h>
896 jermar 44
#include <memstr.h>
1316 jermar 45
#include <config.h>
1682 palkovsky 46
#include <bitops.h>
47
#include <print.h>
2015 jermar 48
#include <ddi/ddi.h>
836 palkovsky 49
 
862 palkovsky 50
#include "helenos.xbm"
51
 
2015 jermar 52
static parea_t fb_parea;		/**< Physical memory area for fb. */
53
 
836 palkovsky 54
SPINLOCK_INITIALIZE(fb_lock);
55
 
1780 jermar 56
static uint8_t *fbaddress = NULL;
836 palkovsky 57
 
1780 jermar 58
static uint8_t *blankline = NULL;
59
static uint8_t *dbbuffer = NULL; /* Buffer for fast scrolling console */
1682 palkovsky 60
static int dboffset;
1316 jermar 61
 
1135 decky 62
static unsigned int xres = 0;
63
static unsigned int yres = 0;
64
static unsigned int scanline = 0;
65
static unsigned int pixelbytes = 0;
1875 jermar 66
#ifdef FB_INVERT_COLORS
67
static bool invert_colors = true;
68
#else
69
static bool invert_colors = false;
70
#endif
836 palkovsky 71
 
1135 decky 72
static unsigned int position = 0;
73
static unsigned int columns = 0;
74
static unsigned int rows = 0;
836 palkovsky 75
 
1135 decky 76
#define COL_WIDTH	8
77
#define ROW_BYTES	(scanline * FONT_SCANLINES)
836 palkovsky 78
 
1135 decky 79
#define BGCOLOR		0x000080
80
#define FGCOLOR		0xffff00
81
#define LOGOCOLOR	0x2020b0
82
 
83
#define RED(x, bits)	((x >> (16 + 8 - bits)) & ((1 << bits) - 1))
84
#define GREEN(x, bits)	((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
85
#define BLUE(x, bits)	((x >> (8 - bits)) & ((1 << bits) - 1))
86
 
1682 palkovsky 87
#define POINTPOS(x, y)	((y) * scanline + (x) * pixelbytes)
1135 decky 88
 
838 palkovsky 89
/***************************************************************/
90
/* Pixel specific fuctions */
91
 
1682 palkovsky 92
static void (*rgb2scr)(void *, int);
93
static int (*scr2rgb)(void *);
839 palkovsky 94
 
1875 jermar 95
static inline int COLOR(int color)
96
{
97
	return invert_colors ? ~color : color;
98
}
99
 
1682 palkovsky 100
/* Conversion routines between different color representations */
1993 decky 101
static void rgb_byte0888(void *dst, int rgb)
836 palkovsky 102
{
1990 decky 103
	*((int *) dst) = rgb;
836 palkovsky 104
}
105
 
1993 decky 106
static int byte0888_rgb(void *src)
838 palkovsky 107
{
1990 decky 108
	return (*((int *) src)) & 0xffffff;
839 palkovsky 109
}
110
 
1994 decky 111
static void bgr_byte0888(void *dst, int rgb)
112
{
113
	*((uint32_t *) dst) = BLUE(rgb, 8) << 16 | GREEN(rgb, 8) << 8 | RED(rgb, 8);
114
}
115
 
116
static int byte0888_bgr(void *src)
117
{
118
	int color = *(uint32_t *)(src);
119
	return ((color & 0xff) << 16) | (((color >> 8) & 0xff) << 8) | ((color >> 16) & 0xff);
120
}
121
 
1993 decky 122
static void rgb_byte888(void *dst, int rgb)
839 palkovsky 123
{
1780 jermar 124
	uint8_t *scr = dst;
1871 jermar 125
#if defined(FB_INVERT_ENDIAN)
1682 palkovsky 126
	scr[0] = RED(rgb, 8);
127
	scr[1] = GREEN(rgb, 8);
128
	scr[2] = BLUE(rgb, 8);
1298 vana 129
#else
1682 palkovsky 130
	scr[2] = RED(rgb, 8);
131
	scr[1] = GREEN(rgb, 8);
132
	scr[0] = BLUE(rgb, 8);
133
#endif
839 palkovsky 134
}
135
 
1993 decky 136
static int byte888_rgb(void *src)
839 palkovsky 137
{
1780 jermar 138
	uint8_t *scr = src;
1871 jermar 139
#if defined(FB_INVERT_ENDIAN)
1682 palkovsky 140
	return scr[0] << 16 | scr[1] << 8 | scr[2];
1298 vana 141
#else
1682 palkovsky 142
	return scr[2] << 16 | scr[1] << 8 | scr[0];
1298 vana 143
#endif	
838 palkovsky 144
}
145
 
1993 decky 146
/**  16-bit depth (5:5:5) */
147
static void rgb_byte555(void *dst, int rgb)
148
{
149
	/* 5-bit, 5-bits, 5-bits */ 
150
	*((uint16_t *) dst) = RED(rgb, 5) << 10 | GREEN(rgb, 5) << 5 | BLUE(rgb, 5);
151
}
152
 
153
/** 16-bit depth (5:5:5) */
154
static int byte555_rgb(void *src)
155
{
156
	int color = *(uint16_t *)(src);
157
	return (((color >> 10) & 0x1f) << (16 + 3)) | (((color >> 5) & 0x1f) << (8 + 3)) | ((color & 0x1f) << 3);
158
}
159
 
1682 palkovsky 160
/**  16-bit depth (5:6:5) */
1993 decky 161
static void rgb_byte565(void *dst, int rgb)
839 palkovsky 162
{
1682 palkovsky 163
	/* 5-bit, 6-bits, 5-bits */ 
1990 decky 164
	*((uint16_t *) dst) = RED(rgb, 5) << 11 | GREEN(rgb, 6) << 5 | BLUE(rgb, 5);
839 palkovsky 165
}
166
 
1682 palkovsky 167
/** 16-bit depth (5:6:5) */
1993 decky 168
static int byte565_rgb(void *src)
839 palkovsky 169
{
1780 jermar 170
	int color = *(uint16_t *)(src);
1135 decky 171
	return (((color >> 11) & 0x1f) << (16 + 3)) | (((color >> 5) & 0x3f) << (8 + 2)) | ((color & 0x1f) << 3);
839 palkovsky 172
}
173
 
1837 jermar 174
/** Put pixel - 8-bit depth (color palette/3:2:3)
175
 *
176
 * Even though we try 3:2:3 color scheme here, an 8-bit framebuffer
177
 * will most likely use a color palette. The color appearance
178
 * will be pretty random and depend on the default installed
179
 * palette. This could be fixed by supporting custom palette
180
 * and setting it to simulate the 8-bit truecolor.
181
 */
1993 decky 182
static void rgb_byte8(void *dst, int rgb)
839 palkovsky 183
{
1990 decky 184
	*((uint8_t *) dst) = RED(rgb, 3) << 5 | GREEN(rgb, 2) << 3 | BLUE(rgb, 3);
1135 decky 185
}
839 palkovsky 186
 
1837 jermar 187
/** Return pixel color - 8-bit depth (color palette/3:2:3)
188
 *
1993 decky 189
 * See the comment for rgb_byte().
1837 jermar 190
 */
1993 decky 191
static int byte8_rgb(void *src)
1135 decky 192
{
1780 jermar 193
	int color = *(uint8_t *)src;
1135 decky 194
	return (((color >> 5) & 0x7) << (16 + 5)) | (((color >> 3) & 0x3) << (8 + 6)) | ((color & 0x7) << 5);
839 palkovsky 195
}
196
 
1682 palkovsky 197
static void putpixel(unsigned int x, unsigned int y, int color)
1135 decky 198
{
1990 decky 199
	(*rgb2scr)(&fbaddress[POINTPOS(x, y)], COLOR(color));
1682 palkovsky 200
 
201
	if (dbbuffer) {
202
		int dline = (y + dboffset) % yres;
1990 decky 203
		(*rgb2scr)(&dbbuffer[POINTPOS(x, dline)], COLOR(color));
1682 palkovsky 204
	}
1135 decky 205
}
839 palkovsky 206
 
1682 palkovsky 207
/** Get pixel from viewport */
208
static int getpixel(unsigned int x, unsigned int y)
209
{
210
	if (dbbuffer) {
211
		int dline = (y + dboffset) % yres;
1990 decky 212
		return COLOR((*scr2rgb)(&dbbuffer[POINTPOS(x, dline)]));
1682 palkovsky 213
	}
1990 decky 214
	return COLOR((*scr2rgb)(&fbaddress[POINTPOS(x, y)]));
1682 palkovsky 215
}
1135 decky 216
 
1682 palkovsky 217
 
1135 decky 218
/** Fill screen with background color */
219
static void clear_screen(void)
839 palkovsky 220
{
1135 decky 221
	unsigned int y;
839 palkovsky 222
 
1682 palkovsky 223
	for (y = 0; y < yres; y++) {
1990 decky 224
		memcpy(&fbaddress[scanline * y], blankline, xres * pixelbytes);
1682 palkovsky 225
		if (dbbuffer)
1990 decky 226
			memcpy(&dbbuffer[scanline * y], blankline, xres * pixelbytes);
1682 palkovsky 227
	}
839 palkovsky 228
}
229
 
1135 decky 230
 
838 palkovsky 231
/** Scroll screen one row up */
232
static void scroll_screen(void)
233
{
1780 jermar 234
	uint8_t *lastline = &fbaddress[(rows - 1) * ROW_BYTES];
1682 palkovsky 235
	int firstsz;
838 palkovsky 236
 
1682 palkovsky 237
	if (dbbuffer) {
1993 decky 238
		memcpy(&dbbuffer[dboffset * scanline], blankline, ROW_BYTES);
1682 palkovsky 239
 
240
		dboffset = (dboffset + FONT_SCANLINES) % yres;
1990 decky 241
		firstsz = yres - dboffset;
838 palkovsky 242
 
1990 decky 243
		memcpy(fbaddress, &dbbuffer[scanline * dboffset], firstsz * scanline);
244
		memcpy(&fbaddress[firstsz * scanline], dbbuffer, dboffset * scanline);
1682 palkovsky 245
	} else {
246
		memcpy((void *) fbaddress, (void *) &fbaddress[ROW_BYTES], scanline * yres - ROW_BYTES);
247
		/* Clear last row */
248
		memcpy((void *) lastline, (void *) blankline, ROW_BYTES);
249
	}
838 palkovsky 250
}
251
 
252
 
1135 decky 253
static void invert_pixel(unsigned int x, unsigned int y)
836 palkovsky 254
{
1682 palkovsky 255
	putpixel(x, y, ~getpixel(x, y));
836 palkovsky 256
}
257
 
258
 
259
/** Draw one line of glyph at a given position */
1135 decky 260
static void draw_glyph_line(unsigned int glline, unsigned int x, unsigned int y)
836 palkovsky 261
{
1135 decky 262
	unsigned int i;
836 palkovsky 263
 
1135 decky 264
	for (i = 0; i < 8; i++)
265
		if (glline & (1 << (7 - i))) {
1682 palkovsky 266
			putpixel(x + i, y, FGCOLOR);
836 palkovsky 267
		} else
1682 palkovsky 268
			putpixel(x + i, y, BGCOLOR);
836 palkovsky 269
}
270
 
838 palkovsky 271
/***************************************************************/
272
/* Character-console functions */
273
 
836 palkovsky 274
/** Draw character at given position */
1780 jermar 275
static void draw_glyph(uint8_t glyph, unsigned int col, unsigned int row)
836 palkovsky 276
{
1135 decky 277
	unsigned int y;
836 palkovsky 278
 
1135 decky 279
	for (y = 0; y < FONT_SCANLINES; y++)
280
		draw_glyph_line(fb_font[glyph * FONT_SCANLINES + y], col * COL_WIDTH, row * FONT_SCANLINES + y);
836 palkovsky 281
}
282
 
283
/** Invert character at given position */
1135 decky 284
static void invert_char(unsigned int col, unsigned int row)
836 palkovsky 285
{
1135 decky 286
	unsigned int x;
287
	unsigned int y;
836 palkovsky 288
 
1135 decky 289
	for (x = 0; x < COL_WIDTH; x++)
290
		for (y = 0; y < FONT_SCANLINES; y++)
291
			invert_pixel(col * COL_WIDTH + x, row * FONT_SCANLINES + y);
836 palkovsky 292
}
293
 
294
/** Draw character at default position */
295
static void draw_char(char chr)
296
{
1135 decky 297
	draw_glyph(chr, position % columns, position / columns);
836 palkovsky 298
}
299
 
1135 decky 300
static void draw_logo(unsigned int startx, unsigned int starty)
862 palkovsky 301
{
1135 decky 302
	unsigned int x;
303
	unsigned int y;
304
	unsigned int byte;
305
	unsigned int rowbytes;
862 palkovsky 306
 
1135 decky 307
	rowbytes = (helenos_width - 1) / 8 + 1;
862 palkovsky 308
 
1135 decky 309
	for (y = 0; y < helenos_height; y++)
310
		for (x = 0; x < helenos_width; x++) {
311
			byte = helenos_bits[rowbytes * y + x / 8];
862 palkovsky 312
			byte >>= x % 8;
313
			if (byte & 1)
1875 jermar 314
				putpixel(startx + x, starty + y, COLOR(LOGOCOLOR));
862 palkovsky 315
		}
316
}
317
 
838 palkovsky 318
/***************************************************************/
319
/* Stdout specific functions */
320
 
836 palkovsky 321
static void invert_cursor(void)
322
{
1135 decky 323
	invert_char(position % columns, position / columns);
836 palkovsky 324
}
325
 
326
/** Print character to screen
327
 *
328
 *  Emulate basic terminal commands
329
 */
330
static void fb_putchar(chardev_t *dev, char ch)
331
{
1287 vana 332
	spinlock_lock(&fb_lock);
1135 decky 333
 
334
	switch (ch) {
1888 jermar 335
	case '\n':
336
		invert_cursor();
337
		position += columns;
338
		position -= position % columns;
339
		break;
340
	case '\r':
341
		invert_cursor();
342
		position -= position % columns;
343
		break;
344
	case '\b':
345
		invert_cursor();
346
		if (position % columns)
347
			position--;
348
		break;
349
	case '\t':
350
		invert_cursor();
351
		do {
352
			draw_char(' ');
838 palkovsky 353
			position++;
1888 jermar 354
		} while ((position % 8) && position < columns * rows);
355
		break;
356
	default:
357
		draw_char(ch);
358
		position++;
836 palkovsky 359
	}
1135 decky 360
 
361
	if (position >= columns * rows) {
836 palkovsky 362
		position -= columns;
363
		scroll_screen();
364
	}
1135 decky 365
 
836 palkovsky 366
	invert_cursor();
1135 decky 367
 
1287 vana 368
	spinlock_unlock(&fb_lock);
836 palkovsky 369
}
370
 
371
static chardev_t framebuffer;
372
static chardev_operations_t fb_ops = {
373
	.write = fb_putchar,
374
};
375
 
376
 
377
/** Initialize framebuffer as a chardev output device
378
 *
1993 decky 379
 * @param addr   Physical address of the framebuffer
380
 * @param x      Screen width in pixels
381
 * @param y      Screen height in pixels
382
 * @param scan   Bytes per one scanline
383
 * @param visual Color model
384
 *
836 palkovsky 385
 */
1993 decky 386
void fb_init(uintptr_t addr, unsigned int x, unsigned int y, unsigned int scan, unsigned int visual)
836 palkovsky 387
{
1993 decky 388
	switch (visual) {
389
	case VISUAL_INDIRECT_8:
390
		rgb2scr = rgb_byte8;
391
		scr2rgb = byte8_rgb;
1991 jermar 392
		pixelbytes = 1;
393
		break;
1993 decky 394
	case VISUAL_RGB_5_5_5:
395
		rgb2scr = rgb_byte555;
396
		scr2rgb = byte555_rgb;
1991 jermar 397
		pixelbytes = 2;
398
		break;
1993 decky 399
	case VISUAL_RGB_5_6_5:
400
		rgb2scr = rgb_byte565;
401
		scr2rgb = byte565_rgb;
402
		pixelbytes = 2;
1991 jermar 403
		break;
1993 decky 404
	case VISUAL_RGB_8_8_8:
405
		rgb2scr = rgb_byte888;
406
		scr2rgb = byte888_rgb;
407
		pixelbytes = 3;
408
		break;
409
	case VISUAL_RGB_8_8_8_0:
410
		rgb2scr = rgb_byte888;
411
		scr2rgb = byte888_rgb;
1991 jermar 412
		pixelbytes = 4;
413
		break;
1993 decky 414
	case VISUAL_RGB_0_8_8_8:
415
		rgb2scr = rgb_byte0888;
416
		scr2rgb = byte0888_rgb;
417
		pixelbytes = 4;
418
		break;
1994 decky 419
	case VISUAL_BGR_0_8_8_8:
420
		rgb2scr = bgr_byte0888;
421
		scr2rgb = byte0888_bgr;
422
		pixelbytes = 4;
423
		break;
1991 jermar 424
	default:
1993 decky 425
		panic("Unsupported visual.\n");
839 palkovsky 426
	}
1371 decky 427
 
428
	unsigned int fbsize = scan * y;
429
 
430
	/* Map the framebuffer */
1780 jermar 431
	fbaddress = (uint8_t *) hw_map((uintptr_t) addr, fbsize);
1371 decky 432
 
836 palkovsky 433
	xres = x;
434
	yres = y;
1135 decky 435
	scanline = scan;
836 palkovsky 436
 
1135 decky 437
	rows = y / FONT_SCANLINES;
438
	columns = x / COL_WIDTH;
836 palkovsky 439
 
2015 jermar 440
	fb_parea.pbase = (uintptr_t) addr;
441
	fb_parea.vbase = (uintptr_t) fbaddress;
442
	fb_parea.frames = SIZE2FRAMES(fbsize);
443
	fb_parea.cacheable = false;
444
	ddi_parea_register(&fb_parea);
445
 
1690 palkovsky 446
	sysinfo_set_item_val("fb", NULL, true);
447
	sysinfo_set_item_val("fb.kind", NULL, 1);
448
	sysinfo_set_item_val("fb.width", NULL, xres);
449
	sysinfo_set_item_val("fb.height", NULL, yres);
450
	sysinfo_set_item_val("fb.scanline", NULL, scan);
1993 decky 451
	sysinfo_set_item_val("fb.visual", NULL, visual);
1690 palkovsky 452
	sysinfo_set_item_val("fb.address.physical", NULL, addr);
2015 jermar 453
	sysinfo_set_item_val("fb.address.color", NULL, PAGE_COLOR((uintptr_t)
454
		fbaddress));
1875 jermar 455
	sysinfo_set_item_val("fb.invert-colors", NULL, invert_colors);
1690 palkovsky 456
 
1682 palkovsky 457
	/* Allocate double buffer */
1990 decky 458
	unsigned int order = fnzb(SIZE2FRAMES(ALIGN_UP(fbsize, FRAME_SIZE))) + 1;
459
	dbbuffer = (uint8_t * ) frame_alloc(order, FRAME_ATOMIC | FRAME_KA);
1760 palkovsky 460
	if (!dbbuffer)
1682 palkovsky 461
		printf("Failed to allocate scroll buffer.\n");
462
	dboffset = 0;
463
 
464
	/* Initialized blank line */
1780 jermar 465
	blankline = (uint8_t *) malloc(ROW_BYTES, FRAME_ATOMIC);
1766 palkovsky 466
	if (!blankline)
467
		panic("Failed to allocate blank line for framebuffer.");
1990 decky 468
	for (y = 0; y < FONT_SCANLINES; y++)
469
		for (x = 0; x < xres; x++)
470
			(*rgb2scr)(&blankline[POINTPOS(x, y)], COLOR(BGCOLOR));
471
 
1682 palkovsky 472
	clear_screen();
473
 
474
	/* Update size of screen to match text area */
475
	yres = rows * FONT_SCANLINES;
476
 
1135 decky 477
	draw_logo(xres - helenos_width, 0);
836 palkovsky 478
	invert_cursor();
479
 
480
	chardev_initialize("fb", &framebuffer, &fb_ops);
481
	stdout = &framebuffer;
482
}
1702 cejka 483
 
1790 jermar 484
/** @}
1702 cejka 485
 */