Subversion Repositories HelenOS-historic

Rev

Rev 1671 | Rev 1683 | 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
 
837 palkovsky 29
#include <genarch/fb/font-8x16.h>
30
#include <genarch/fb/fb.h>
836 palkovsky 31
#include <console/chardev.h>
32
#include <console/console.h>
1317 vana 33
#include <sysinfo/sysinfo.h>
1316 jermar 34
#include <mm/slab.h>
1371 decky 35
#include <align.h>
839 palkovsky 36
#include <panic.h>
896 jermar 37
#include <memstr.h>
1316 jermar 38
#include <config.h>
1682 palkovsky 39
#include <bitops.h>
40
#include <print.h>
836 palkovsky 41
 
862 palkovsky 42
#include "helenos.xbm"
43
 
836 palkovsky 44
SPINLOCK_INITIALIZE(fb_lock);
45
 
1135 decky 46
static __u8 *fbaddress = NULL;
836 palkovsky 47
 
1316 jermar 48
static __u8 *blankline = NULL;
1682 palkovsky 49
static __u8 *dbbuffer = NULL; /* Buffer for fast scrolling console */
50
static int dboffset;
1316 jermar 51
 
1135 decky 52
static unsigned int xres = 0;
53
static unsigned int yres = 0;
54
static unsigned int scanline = 0;
1327 decky 55
static unsigned int bitspp = 0;
1135 decky 56
static unsigned int pixelbytes = 0;
836 palkovsky 57
 
1135 decky 58
static unsigned int position = 0;
59
static unsigned int columns = 0;
60
static unsigned int rows = 0;
836 palkovsky 61
 
62
 
1135 decky 63
#define COL_WIDTH	8
64
#define ROW_BYTES	(scanline * FONT_SCANLINES)
836 palkovsky 65
 
1135 decky 66
#define BGCOLOR		0x000080
67
#define FGCOLOR		0xffff00
68
#define LOGOCOLOR	0x2020b0
69
 
70
#define RED(x, bits)	((x >> (16 + 8 - bits)) & ((1 << bits) - 1))
71
#define GREEN(x, bits)	((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
72
#define BLUE(x, bits)	((x >> (8 - bits)) & ((1 << bits) - 1))
73
 
1682 palkovsky 74
#define POINTPOS(x, y)	((y) * scanline + (x) * pixelbytes)
1135 decky 75
 
838 palkovsky 76
/***************************************************************/
77
/* Pixel specific fuctions */
78
 
1682 palkovsky 79
static void (*rgb2scr)(void *, int);
80
static int (*scr2rgb)(void *);
839 palkovsky 81
 
1682 palkovsky 82
/* Conversion routines between different color representations */
83
static void rgb_4byte(void *dst, int rgb)
836 palkovsky 84
{
1682 palkovsky 85
	*(int *)dst = rgb;
836 palkovsky 86
}
87
 
1682 palkovsky 88
static int byte4_rgb(void *src)
838 palkovsky 89
{
1682 palkovsky 90
	return (*(int *)src) & 0xffffff;
839 palkovsky 91
}
92
 
1682 palkovsky 93
static void rgb_3byte(void *dst, int rgb)
839 palkovsky 94
{
1682 palkovsky 95
	__u8 *scr = dst;
1334 jermar 96
#if (defined(BIG_ENDIAN) || defined(FB_BIG_ENDIAN))
1682 palkovsky 97
	scr[0] = RED(rgb, 8);
98
	scr[1] = GREEN(rgb, 8);
99
	scr[2] = BLUE(rgb, 8);
1298 vana 100
#else
1682 palkovsky 101
	scr[2] = RED(rgb, 8);
102
	scr[1] = GREEN(rgb, 8);
103
	scr[0] = BLUE(rgb, 8);
104
#endif
839 palkovsky 105
}
106
 
1682 palkovsky 107
static int byte3_rgb(void *src)
839 palkovsky 108
{
1682 palkovsky 109
	__u8 *scr = src;
1334 jermar 110
#if (defined(BIG_ENDIAN) || defined(FB_BIG_ENDIAN))
1682 palkovsky 111
	return scr[0] << 16 | scr[1] << 8 | scr[2];
1298 vana 112
#else
1682 palkovsky 113
	return scr[2] << 16 | scr[1] << 8 | scr[0];
1298 vana 114
#endif	
838 palkovsky 115
}
116
 
1682 palkovsky 117
/**  16-bit depth (5:6:5) */
118
static void rgb_2byte(void *dst, int rgb)
839 palkovsky 119
{
1682 palkovsky 120
	/* 5-bit, 6-bits, 5-bits */ 
121
	*((__u16 *)(dst)) = RED(rgb, 5) << 11 | GREEN(rgb, 6) << 5 | BLUE(rgb, 5);
839 palkovsky 122
}
123
 
1682 palkovsky 124
/** 16-bit depth (5:6:5) */
125
static int byte2_rgb(void *src)
839 palkovsky 126
{
1682 palkovsky 127
	int color = *(__u16 *)(src);
1135 decky 128
	return (((color >> 11) & 0x1f) << (16 + 3)) | (((color >> 5) & 0x3f) << (8 + 2)) | ((color & 0x1f) << 3);
839 palkovsky 129
}
130
 
886 jermar 131
/** Put pixel - 8-bit depth (3:2:3) */
1682 palkovsky 132
static void rgb_1byte(void *dst, int rgb)
839 palkovsky 133
{
1682 palkovsky 134
	*(__u8 *)dst = RED(rgb, 3) << 5 | GREEN(rgb, 2) << 3 | BLUE(rgb, 3);
1135 decky 135
}
839 palkovsky 136
 
1135 decky 137
/** Return pixel color - 8-bit depth (3:2:3) */
1682 palkovsky 138
static int byte1_rgb(void *src)
1135 decky 139
{
1682 palkovsky 140
	int color = *(__u8 *)src;
1135 decky 141
	return (((color >> 5) & 0x7) << (16 + 5)) | (((color >> 3) & 0x3) << (8 + 6)) | ((color & 0x7) << 5);
839 palkovsky 142
}
143
 
1682 palkovsky 144
static void putpixel(unsigned int x, unsigned int y, int color)
1135 decky 145
{
1682 palkovsky 146
	(*rgb2scr)(&fbaddress[POINTPOS(x,y)],color);
147
 
148
	if (dbbuffer) {
149
		int dline = (y + dboffset) % yres;
150
		(*rgb2scr)(&dbbuffer[POINTPOS(x,dline)],color);
151
	}
1135 decky 152
}
839 palkovsky 153
 
1682 palkovsky 154
/** Get pixel from viewport */
155
static int getpixel(unsigned int x, unsigned int y)
156
{
157
	if (dbbuffer) {
158
		int dline = (y + dboffset) % yres;
159
		return (*scr2rgb)(&dbbuffer[POINTPOS(x,dline)]);
160
	}
161
	return (*scr2rgb)(&fbaddress[POINTPOS(x,y)]);
162
}
1135 decky 163
 
1682 palkovsky 164
 
1135 decky 165
/** Fill screen with background color */
166
static void clear_screen(void)
839 palkovsky 167
{
1135 decky 168
	unsigned int y;
839 palkovsky 169
 
1682 palkovsky 170
	for (y = 0; y < yres; y++) {
171
		memcpy(&fbaddress[scanline*y], blankline, xres*pixelbytes);
172
		if (dbbuffer)
173
			memcpy(&dbbuffer[scanline*y], blankline, xres*pixelbytes);
174
	}
839 palkovsky 175
}
176
 
1135 decky 177
 
838 palkovsky 178
/** Scroll screen one row up */
179
static void scroll_screen(void)
180
{
1316 jermar 181
	__u8 *lastline = &fbaddress[(rows - 1) * ROW_BYTES];
1682 palkovsky 182
	int firstsz;
838 palkovsky 183
 
1682 palkovsky 184
	if (dbbuffer) {
185
		memcpy(&dbbuffer[dboffset*scanline], blankline, FONT_SCANLINES*scanline);
186
 
187
		dboffset = (dboffset + FONT_SCANLINES) % yres;
188
		firstsz = yres-dboffset;
838 palkovsky 189
 
1682 palkovsky 190
		memcpy(fbaddress, &dbbuffer[scanline*dboffset], firstsz*scanline);
191
		memcpy(&fbaddress[firstsz*scanline], dbbuffer, dboffset*scanline);
192
	} else {
193
		memcpy((void *) fbaddress, (void *) &fbaddress[ROW_BYTES], scanline * yres - ROW_BYTES);
194
		/* Clear last row */
195
		memcpy((void *) lastline, (void *) blankline, ROW_BYTES);
196
	}
838 palkovsky 197
}
198
 
199
 
1135 decky 200
static void invert_pixel(unsigned int x, unsigned int y)
836 palkovsky 201
{
1682 palkovsky 202
	putpixel(x, y, ~getpixel(x, y));
836 palkovsky 203
}
204
 
205
 
206
/** Draw one line of glyph at a given position */
1135 decky 207
static void draw_glyph_line(unsigned int glline, unsigned int x, unsigned int y)
836 palkovsky 208
{
1135 decky 209
	unsigned int i;
836 palkovsky 210
 
1135 decky 211
	for (i = 0; i < 8; i++)
212
		if (glline & (1 << (7 - i))) {
1682 palkovsky 213
			putpixel(x + i, y, FGCOLOR);
836 palkovsky 214
		} else
1682 palkovsky 215
			putpixel(x + i, y, BGCOLOR);
836 palkovsky 216
}
217
 
838 palkovsky 218
/***************************************************************/
219
/* Character-console functions */
220
 
836 palkovsky 221
/** Draw character at given position */
1135 decky 222
static void draw_glyph(__u8 glyph, unsigned int col, unsigned int row)
836 palkovsky 223
{
1135 decky 224
	unsigned int y;
836 palkovsky 225
 
1135 decky 226
	for (y = 0; y < FONT_SCANLINES; y++)
227
		draw_glyph_line(fb_font[glyph * FONT_SCANLINES + y], col * COL_WIDTH, row * FONT_SCANLINES + y);
836 palkovsky 228
}
229
 
230
/** Invert character at given position */
1135 decky 231
static void invert_char(unsigned int col, unsigned int row)
836 palkovsky 232
{
1135 decky 233
	unsigned int x;
234
	unsigned int y;
836 palkovsky 235
 
1135 decky 236
	for (x = 0; x < COL_WIDTH; x++)
237
		for (y = 0; y < FONT_SCANLINES; y++)
238
			invert_pixel(col * COL_WIDTH + x, row * FONT_SCANLINES + y);
836 palkovsky 239
}
240
 
241
/** Draw character at default position */
242
static void draw_char(char chr)
243
{
1135 decky 244
	draw_glyph(chr, position % columns, position / columns);
836 palkovsky 245
}
246
 
1135 decky 247
static void draw_logo(unsigned int startx, unsigned int starty)
862 palkovsky 248
{
1135 decky 249
	unsigned int x;
250
	unsigned int y;
251
	unsigned int byte;
252
	unsigned int rowbytes;
862 palkovsky 253
 
1135 decky 254
	rowbytes = (helenos_width - 1) / 8 + 1;
862 palkovsky 255
 
1135 decky 256
	for (y = 0; y < helenos_height; y++)
257
		for (x = 0; x < helenos_width; x++) {
258
			byte = helenos_bits[rowbytes * y + x / 8];
862 palkovsky 259
			byte >>= x % 8;
260
			if (byte & 1)
1682 palkovsky 261
				putpixel(startx + x, starty + y, LOGOCOLOR);
862 palkovsky 262
		}
263
}
264
 
838 palkovsky 265
/***************************************************************/
266
/* Stdout specific functions */
267
 
836 palkovsky 268
static void invert_cursor(void)
269
{
1135 decky 270
	invert_char(position % columns, position / columns);
836 palkovsky 271
}
272
 
273
/** Print character to screen
274
 *
275
 *  Emulate basic terminal commands
276
 */
277
static void fb_putchar(chardev_t *dev, char ch)
278
{
1287 vana 279
	spinlock_lock(&fb_lock);
1135 decky 280
 
281
	switch (ch) {
282
		case '\n':
283
			invert_cursor();
284
			position += columns;
285
			position -= position % columns;
286
			break;
287
		case '\r':
288
			invert_cursor();
289
			position -= position % columns;
290
			break;
291
		case '\b':
292
			invert_cursor();
293
			if (position % columns)
294
				position--;
295
			break;
296
		case '\t':
297
			invert_cursor();
298
			do {
299
				draw_char(' ');
300
				position++;
1312 palkovsky 301
			} while ((position % 8) && position < columns * rows);
1135 decky 302
			break;
303
		default:
304
			draw_char(ch);
838 palkovsky 305
			position++;
836 palkovsky 306
	}
1135 decky 307
 
308
	if (position >= columns * rows) {
836 palkovsky 309
		position -= columns;
310
		scroll_screen();
311
	}
1135 decky 312
 
836 palkovsky 313
	invert_cursor();
1135 decky 314
 
1287 vana 315
	spinlock_unlock(&fb_lock);
836 palkovsky 316
}
317
 
318
static chardev_t framebuffer;
319
static chardev_operations_t fb_ops = {
320
	.write = fb_putchar,
321
};
322
 
323
 
324
/** Initialize framebuffer as a chardev output device
325
 *
1371 decky 326
 * @param addr Physical address of the framebuffer
1135 decky 327
 * @param x    Screen width in pixels
328
 * @param y    Screen height in pixels
329
 * @param bpp  Bits per pixel (8, 16, 24, 32)
330
 * @param scan Bytes per one scanline
331
 *
836 palkovsky 332
 */
1135 decky 333
void fb_init(__address addr, unsigned int x, unsigned int y, unsigned int bpp, unsigned int scan)
836 palkovsky 334
{
1135 decky 335
	switch (bpp) {
336
		case 8:
1682 palkovsky 337
			rgb2scr = rgb_1byte;
338
			scr2rgb = byte1_rgb;
1135 decky 339
			pixelbytes = 1;
340
			break;
341
		case 16:
1682 palkovsky 342
			rgb2scr = rgb_2byte;
343
			scr2rgb = byte2_rgb;
1135 decky 344
			pixelbytes = 2;
345
			break;
346
		case 24:
1682 palkovsky 347
			rgb2scr = rgb_3byte;
348
			scr2rgb = byte3_rgb;
1135 decky 349
			pixelbytes = 3;
350
			break;
351
		case 32:
1682 palkovsky 352
			rgb2scr = rgb_4byte;
353
			scr2rgb = byte4_rgb;
1135 decky 354
			pixelbytes = 4;
355
			break;
356
		default:
357
			panic("Unsupported bpp");
839 palkovsky 358
	}
1371 decky 359
 
360
	unsigned int fbsize = scan * y;
361
 
362
	/* Map the framebuffer */
1382 decky 363
	fbaddress = (__u8 *) hw_map((__address) addr, fbsize);
1371 decky 364
 
836 palkovsky 365
	xres = x;
366
	yres = y;
1327 decky 367
	bitspp = bpp;
1135 decky 368
	scanline = scan;
836 palkovsky 369
 
1135 decky 370
	rows = y / FONT_SCANLINES;
371
	columns = x / COL_WIDTH;
836 palkovsky 372
 
1682 palkovsky 373
	/* Allocate double buffer */
374
	int totsize = scanline * yres;
375
	int pages = SIZE2FRAMES(totsize);
376
	int order;
377
	int rc;
378
	if (pages == 1)
379
		order = 0;
380
	else
381
		order = fnzb(pages-1)+1;
382
 
383
	pfn_t frame = frame_alloc_rc(order,FRAME_ATOMIC,&rc);
384
	if (!rc)
385
		dbbuffer = (void *)PA2KA(PFN2ADDR(frame));
386
	else 
387
		printf("Failed to allocate scroll buffer.\n");
388
	dboffset = 0;
389
 
390
	/* Initialized blank line */
1671 jermar 391
	blankline = (__u8 *) malloc(ROW_BYTES, FRAME_ATOMIC);
392
	ASSERT(blankline);
1682 palkovsky 393
	for (y=0; y < FONT_SCANLINES; y++)
394
		for (x=0; x < xres; x++)
395
			(*rgb2scr)(&blankline[POINTPOS(x,y)],BGCOLOR);
396
 
397
	clear_screen();
398
 
399
	/* Update size of screen to match text area */
400
	yres = rows * FONT_SCANLINES;
401
 
1135 decky 402
	draw_logo(xres - helenos_width, 0);
836 palkovsky 403
	invert_cursor();
404
 
405
	chardev_initialize("fb", &framebuffer, &fb_ops);
406
	stdout = &framebuffer;
1371 decky 407
 
1327 decky 408
	sysinfo_set_item_val("fb", NULL, true);
1473 decky 409
	sysinfo_set_item_val("fb.kind", NULL, 1);
1371 decky 410
	sysinfo_set_item_val("fb.width", NULL, x);
411
	sysinfo_set_item_val("fb.height", NULL, y);
412
	sysinfo_set_item_val("fb.bpp", NULL, bpp);
413
	sysinfo_set_item_val("fb.scanline", NULL, scan);
414
	sysinfo_set_item_val("fb.address.physical", NULL, addr);
1682 palkovsky 415
 
836 palkovsky 416
}