Subversion Repositories HelenOS

Rev

Rev 1993 | Rev 2015 | 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>
836 palkovsky 48
 
862 palkovsky 49
#include "helenos.xbm"
50
 
836 palkovsky 51
SPINLOCK_INITIALIZE(fb_lock);
52
 
1780 jermar 53
static uint8_t *fbaddress = NULL;
836 palkovsky 54
 
1780 jermar 55
static uint8_t *blankline = NULL;
56
static uint8_t *dbbuffer = NULL; /* Buffer for fast scrolling console */
1682 palkovsky 57
static int dboffset;
1316 jermar 58
 
1135 decky 59
static unsigned int xres = 0;
60
static unsigned int yres = 0;
61
static unsigned int scanline = 0;
62
static unsigned int pixelbytes = 0;
1875 jermar 63
#ifdef FB_INVERT_COLORS
64
static bool invert_colors = true;
65
#else
66
static bool invert_colors = false;
67
#endif
836 palkovsky 68
 
1135 decky 69
static unsigned int position = 0;
70
static unsigned int columns = 0;
71
static unsigned int rows = 0;
836 palkovsky 72
 
1135 decky 73
#define COL_WIDTH   8
74
#define ROW_BYTES   (scanline * FONT_SCANLINES)
836 palkovsky 75
 
1135 decky 76
#define BGCOLOR     0x000080
77
#define FGCOLOR     0xffff00
78
#define LOGOCOLOR   0x2020b0
79
 
80
#define RED(x, bits)    ((x >> (16 + 8 - bits)) & ((1 << bits) - 1))
81
#define GREEN(x, bits)  ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
82
#define BLUE(x, bits)   ((x >> (8 - bits)) & ((1 << bits) - 1))
83
 
1682 palkovsky 84
#define POINTPOS(x, y)  ((y) * scanline + (x) * pixelbytes)
1135 decky 85
 
838 palkovsky 86
/***************************************************************/
87
/* Pixel specific fuctions */
88
 
1682 palkovsky 89
static void (*rgb2scr)(void *, int);
90
static int (*scr2rgb)(void *);
839 palkovsky 91
 
1875 jermar 92
static inline int COLOR(int color)
93
{
94
    return invert_colors ? ~color : color;
95
}
96
 
1682 palkovsky 97
/* Conversion routines between different color representations */
1993 decky 98
static void rgb_byte0888(void *dst, int rgb)
836 palkovsky 99
{
1990 decky 100
    *((int *) dst) = rgb;
836 palkovsky 101
}
102
 
1993 decky 103
static int byte0888_rgb(void *src)
838 palkovsky 104
{
1990 decky 105
    return (*((int *) src)) & 0xffffff;
839 palkovsky 106
}
107
 
1994 decky 108
static void bgr_byte0888(void *dst, int rgb)
109
{
110
    *((uint32_t *) dst) = BLUE(rgb, 8) << 16 | GREEN(rgb, 8) << 8 | RED(rgb, 8);
111
}
112
 
113
static int byte0888_bgr(void *src)
114
{
115
    int color = *(uint32_t *)(src);
116
    return ((color & 0xff) << 16) | (((color >> 8) & 0xff) << 8) | ((color >> 16) & 0xff);
117
}
118
 
1993 decky 119
static void rgb_byte888(void *dst, int rgb)
839 palkovsky 120
{
1780 jermar 121
    uint8_t *scr = dst;
1871 jermar 122
#if defined(FB_INVERT_ENDIAN)
1682 palkovsky 123
    scr[0] = RED(rgb, 8);
124
    scr[1] = GREEN(rgb, 8);
125
    scr[2] = BLUE(rgb, 8);
1298 vana 126
#else
1682 palkovsky 127
    scr[2] = RED(rgb, 8);
128
    scr[1] = GREEN(rgb, 8);
129
    scr[0] = BLUE(rgb, 8);
130
#endif
839 palkovsky 131
}
132
 
1993 decky 133
static int byte888_rgb(void *src)
839 palkovsky 134
{
1780 jermar 135
    uint8_t *scr = src;
1871 jermar 136
#if defined(FB_INVERT_ENDIAN)
1682 palkovsky 137
    return scr[0] << 16 | scr[1] << 8 | scr[2];
1298 vana 138
#else
1682 palkovsky 139
    return scr[2] << 16 | scr[1] << 8 | scr[0];
1298 vana 140
#endif  
838 palkovsky 141
}
142
 
1993 decky 143
/**  16-bit depth (5:5:5) */
144
static void rgb_byte555(void *dst, int rgb)
145
{
146
    /* 5-bit, 5-bits, 5-bits */
147
    *((uint16_t *) dst) = RED(rgb, 5) << 10 | GREEN(rgb, 5) << 5 | BLUE(rgb, 5);
148
}
149
 
150
/** 16-bit depth (5:5:5) */
151
static int byte555_rgb(void *src)
152
{
153
    int color = *(uint16_t *)(src);
154
    return (((color >> 10) & 0x1f) << (16 + 3)) | (((color >> 5) & 0x1f) << (8 + 3)) | ((color & 0x1f) << 3);
155
}
156
 
1682 palkovsky 157
/**  16-bit depth (5:6:5) */
1993 decky 158
static void rgb_byte565(void *dst, int rgb)
839 palkovsky 159
{
1682 palkovsky 160
    /* 5-bit, 6-bits, 5-bits */
1990 decky 161
    *((uint16_t *) dst) = RED(rgb, 5) << 11 | GREEN(rgb, 6) << 5 | BLUE(rgb, 5);
839 palkovsky 162
}
163
 
1682 palkovsky 164
/** 16-bit depth (5:6:5) */
1993 decky 165
static int byte565_rgb(void *src)
839 palkovsky 166
{
1780 jermar 167
    int color = *(uint16_t *)(src);
1135 decky 168
    return (((color >> 11) & 0x1f) << (16 + 3)) | (((color >> 5) & 0x3f) << (8 + 2)) | ((color & 0x1f) << 3);
839 palkovsky 169
}
170
 
1837 jermar 171
/** Put pixel - 8-bit depth (color palette/3:2:3)
172
 *
173
 * Even though we try 3:2:3 color scheme here, an 8-bit framebuffer
174
 * will most likely use a color palette. The color appearance
175
 * will be pretty random and depend on the default installed
176
 * palette. This could be fixed by supporting custom palette
177
 * and setting it to simulate the 8-bit truecolor.
178
 */
1993 decky 179
static void rgb_byte8(void *dst, int rgb)
839 palkovsky 180
{
1990 decky 181
    *((uint8_t *) dst) = RED(rgb, 3) << 5 | GREEN(rgb, 2) << 3 | BLUE(rgb, 3);
1135 decky 182
}
839 palkovsky 183
 
1837 jermar 184
/** Return pixel color - 8-bit depth (color palette/3:2:3)
185
 *
1993 decky 186
 * See the comment for rgb_byte().
1837 jermar 187
 */
1993 decky 188
static int byte8_rgb(void *src)
1135 decky 189
{
1780 jermar 190
    int color = *(uint8_t *)src;
1135 decky 191
    return (((color >> 5) & 0x7) << (16 + 5)) | (((color >> 3) & 0x3) << (8 + 6)) | ((color & 0x7) << 5);
839 palkovsky 192
}
193
 
1682 palkovsky 194
static void putpixel(unsigned int x, unsigned int y, int color)
1135 decky 195
{
1990 decky 196
    (*rgb2scr)(&fbaddress[POINTPOS(x, y)], COLOR(color));
1682 palkovsky 197
 
198
    if (dbbuffer) {
199
        int dline = (y + dboffset) % yres;
1990 decky 200
        (*rgb2scr)(&dbbuffer[POINTPOS(x, dline)], COLOR(color));
1682 palkovsky 201
    }
1135 decky 202
}
839 palkovsky 203
 
1682 palkovsky 204
/** Get pixel from viewport */
205
static int getpixel(unsigned int x, unsigned int y)
206
{
207
    if (dbbuffer) {
208
        int dline = (y + dboffset) % yres;
1990 decky 209
        return COLOR((*scr2rgb)(&dbbuffer[POINTPOS(x, dline)]));
1682 palkovsky 210
    }
1990 decky 211
    return COLOR((*scr2rgb)(&fbaddress[POINTPOS(x, y)]));
1682 palkovsky 212
}
1135 decky 213
 
1682 palkovsky 214
 
1135 decky 215
/** Fill screen with background color */
216
static void clear_screen(void)
839 palkovsky 217
{
1135 decky 218
    unsigned int y;
839 palkovsky 219
 
1682 palkovsky 220
    for (y = 0; y < yres; y++) {
1990 decky 221
        memcpy(&fbaddress[scanline * y], blankline, xres * pixelbytes);
1682 palkovsky 222
        if (dbbuffer)
1990 decky 223
            memcpy(&dbbuffer[scanline * y], blankline, xres * pixelbytes);
1682 palkovsky 224
    }
839 palkovsky 225
}
226
 
1135 decky 227
 
838 palkovsky 228
/** Scroll screen one row up */
229
static void scroll_screen(void)
230
{
1780 jermar 231
    uint8_t *lastline = &fbaddress[(rows - 1) * ROW_BYTES];
1682 palkovsky 232
    int firstsz;
838 palkovsky 233
 
1682 palkovsky 234
    if (dbbuffer) {
1993 decky 235
        memcpy(&dbbuffer[dboffset * scanline], blankline, ROW_BYTES);
1682 palkovsky 236
 
237
        dboffset = (dboffset + FONT_SCANLINES) % yres;
1990 decky 238
        firstsz = yres - dboffset;
838 palkovsky 239
 
1990 decky 240
        memcpy(fbaddress, &dbbuffer[scanline * dboffset], firstsz * scanline);
241
        memcpy(&fbaddress[firstsz * scanline], dbbuffer, dboffset * scanline);
1682 palkovsky 242
    } else {
243
        memcpy((void *) fbaddress, (void *) &fbaddress[ROW_BYTES], scanline * yres - ROW_BYTES);
244
        /* Clear last row */
245
        memcpy((void *) lastline, (void *) blankline, ROW_BYTES);
246
    }
838 palkovsky 247
}
248
 
249
 
1135 decky 250
static void invert_pixel(unsigned int x, unsigned int y)
836 palkovsky 251
{
1682 palkovsky 252
    putpixel(x, y, ~getpixel(x, y));
836 palkovsky 253
}
254
 
255
 
256
/** Draw one line of glyph at a given position */
1135 decky 257
static void draw_glyph_line(unsigned int glline, unsigned int x, unsigned int y)
836 palkovsky 258
{
1135 decky 259
    unsigned int i;
836 palkovsky 260
 
1135 decky 261
    for (i = 0; i < 8; i++)
262
        if (glline & (1 << (7 - i))) {
1682 palkovsky 263
            putpixel(x + i, y, FGCOLOR);
836 palkovsky 264
        } else
1682 palkovsky 265
            putpixel(x + i, y, BGCOLOR);
836 palkovsky 266
}
267
 
838 palkovsky 268
/***************************************************************/
269
/* Character-console functions */
270
 
836 palkovsky 271
/** Draw character at given position */
1780 jermar 272
static void draw_glyph(uint8_t glyph, unsigned int col, unsigned int row)
836 palkovsky 273
{
1135 decky 274
    unsigned int y;
836 palkovsky 275
 
1135 decky 276
    for (y = 0; y < FONT_SCANLINES; y++)
277
        draw_glyph_line(fb_font[glyph * FONT_SCANLINES + y], col * COL_WIDTH, row * FONT_SCANLINES + y);
836 palkovsky 278
}
279
 
280
/** Invert character at given position */
1135 decky 281
static void invert_char(unsigned int col, unsigned int row)
836 palkovsky 282
{
1135 decky 283
    unsigned int x;
284
    unsigned int y;
836 palkovsky 285
 
1135 decky 286
    for (x = 0; x < COL_WIDTH; x++)
287
        for (y = 0; y < FONT_SCANLINES; y++)
288
            invert_pixel(col * COL_WIDTH + x, row * FONT_SCANLINES + y);
836 palkovsky 289
}
290
 
291
/** Draw character at default position */
292
static void draw_char(char chr)
293
{
1135 decky 294
    draw_glyph(chr, position % columns, position / columns);
836 palkovsky 295
}
296
 
1135 decky 297
static void draw_logo(unsigned int startx, unsigned int starty)
862 palkovsky 298
{
1135 decky 299
    unsigned int x;
300
    unsigned int y;
301
    unsigned int byte;
302
    unsigned int rowbytes;
862 palkovsky 303
 
1135 decky 304
    rowbytes = (helenos_width - 1) / 8 + 1;
862 palkovsky 305
 
1135 decky 306
    for (y = 0; y < helenos_height; y++)
307
        for (x = 0; x < helenos_width; x++) {
308
            byte = helenos_bits[rowbytes * y + x / 8];
862 palkovsky 309
            byte >>= x % 8;
310
            if (byte & 1)
1875 jermar 311
                putpixel(startx + x, starty + y, COLOR(LOGOCOLOR));
862 palkovsky 312
        }
313
}
314
 
838 palkovsky 315
/***************************************************************/
316
/* Stdout specific functions */
317
 
836 palkovsky 318
static void invert_cursor(void)
319
{
1135 decky 320
    invert_char(position % columns, position / columns);
836 palkovsky 321
}
322
 
323
/** Print character to screen
324
 *
325
 *  Emulate basic terminal commands
326
 */
327
static void fb_putchar(chardev_t *dev, char ch)
328
{
1287 vana 329
    spinlock_lock(&fb_lock);
1135 decky 330
 
331
    switch (ch) {
1888 jermar 332
    case '\n':
333
        invert_cursor();
334
        position += columns;
335
        position -= position % columns;
336
        break;
337
    case '\r':
338
        invert_cursor();
339
        position -= position % columns;
340
        break;
341
    case '\b':
342
        invert_cursor();
343
        if (position % columns)
344
            position--;
345
        break;
346
    case '\t':
347
        invert_cursor();
348
        do {
349
            draw_char(' ');
838 palkovsky 350
            position++;
1888 jermar 351
        } while ((position % 8) && position < columns * rows);
352
        break;
353
    default:
354
        draw_char(ch);
355
        position++;
836 palkovsky 356
    }
1135 decky 357
 
358
    if (position >= columns * rows) {
836 palkovsky 359
        position -= columns;
360
        scroll_screen();
361
    }
1135 decky 362
 
836 palkovsky 363
    invert_cursor();
1135 decky 364
 
1287 vana 365
    spinlock_unlock(&fb_lock);
836 palkovsky 366
}
367
 
368
static chardev_t framebuffer;
369
static chardev_operations_t fb_ops = {
370
    .write = fb_putchar,
371
};
372
 
373
 
374
/** Initialize framebuffer as a chardev output device
375
 *
1993 decky 376
 * @param addr   Physical address of the framebuffer
377
 * @param x      Screen width in pixels
378
 * @param y      Screen height in pixels
379
 * @param scan   Bytes per one scanline
380
 * @param visual Color model
381
 *
836 palkovsky 382
 */
1993 decky 383
void fb_init(uintptr_t addr, unsigned int x, unsigned int y, unsigned int scan, unsigned int visual)
836 palkovsky 384
{
1993 decky 385
    switch (visual) {
386
    case VISUAL_INDIRECT_8:
387
        rgb2scr = rgb_byte8;
388
        scr2rgb = byte8_rgb;
1991 jermar 389
        pixelbytes = 1;
390
        break;
1993 decky 391
    case VISUAL_RGB_5_5_5:
392
        rgb2scr = rgb_byte555;
393
        scr2rgb = byte555_rgb;
1991 jermar 394
        pixelbytes = 2;
395
        break;
1993 decky 396
    case VISUAL_RGB_5_6_5:
397
        rgb2scr = rgb_byte565;
398
        scr2rgb = byte565_rgb;
399
        pixelbytes = 2;
1991 jermar 400
        break;
1993 decky 401
    case VISUAL_RGB_8_8_8:
402
        rgb2scr = rgb_byte888;
403
        scr2rgb = byte888_rgb;
404
        pixelbytes = 3;
405
        break;
406
    case VISUAL_RGB_8_8_8_0:
407
        rgb2scr = rgb_byte888;
408
        scr2rgb = byte888_rgb;
1991 jermar 409
        pixelbytes = 4;
410
        break;
1993 decky 411
    case VISUAL_RGB_0_8_8_8:
412
        rgb2scr = rgb_byte0888;
413
        scr2rgb = byte0888_rgb;
414
        pixelbytes = 4;
415
        break;
1994 decky 416
    case VISUAL_BGR_0_8_8_8:
417
        rgb2scr = bgr_byte0888;
418
        scr2rgb = byte0888_bgr;
419
        pixelbytes = 4;
420
        break;
1991 jermar 421
    default:
1993 decky 422
        panic("Unsupported visual.\n");
839 palkovsky 423
    }
1371 decky 424
 
425
    unsigned int fbsize = scan * y;
426
 
427
    /* Map the framebuffer */
1780 jermar 428
    fbaddress = (uint8_t *) hw_map((uintptr_t) addr, fbsize);
1371 decky 429
 
836 palkovsky 430
    xres = x;
431
    yres = y;
1135 decky 432
    scanline = scan;
836 palkovsky 433
 
1135 decky 434
    rows = y / FONT_SCANLINES;
435
    columns = x / COL_WIDTH;
836 palkovsky 436
 
1690 palkovsky 437
    sysinfo_set_item_val("fb", NULL, true);
438
    sysinfo_set_item_val("fb.kind", NULL, 1);
439
    sysinfo_set_item_val("fb.width", NULL, xres);
440
    sysinfo_set_item_val("fb.height", NULL, yres);
441
    sysinfo_set_item_val("fb.scanline", NULL, scan);
1993 decky 442
    sysinfo_set_item_val("fb.visual", NULL, visual);
1690 palkovsky 443
    sysinfo_set_item_val("fb.address.physical", NULL, addr);
1875 jermar 444
    sysinfo_set_item_val("fb.invert-colors", NULL, invert_colors);
1690 palkovsky 445
 
1682 palkovsky 446
    /* Allocate double buffer */
1990 decky 447
    unsigned int order = fnzb(SIZE2FRAMES(ALIGN_UP(fbsize, FRAME_SIZE))) + 1;
448
    dbbuffer = (uint8_t * ) frame_alloc(order, FRAME_ATOMIC | FRAME_KA);
1760 palkovsky 449
    if (!dbbuffer)
1682 palkovsky 450
        printf("Failed to allocate scroll buffer.\n");
451
    dboffset = 0;
452
 
453
    /* Initialized blank line */
1780 jermar 454
    blankline = (uint8_t *) malloc(ROW_BYTES, FRAME_ATOMIC);
1766 palkovsky 455
    if (!blankline)
456
        panic("Failed to allocate blank line for framebuffer.");
1990 decky 457
    for (y = 0; y < FONT_SCANLINES; y++)
458
        for (x = 0; x < xres; x++)
459
            (*rgb2scr)(&blankline[POINTPOS(x, y)], COLOR(BGCOLOR));
460
 
1682 palkovsky 461
    clear_screen();
462
 
463
    /* Update size of screen to match text area */
464
    yres = rows * FONT_SCANLINES;
465
 
1135 decky 466
    draw_logo(xres - helenos_width, 0);
836 palkovsky 467
    invert_cursor();
468
 
469
    chardev_initialize("fb", &framebuffer, &fb_ops);
470
    stdout = &framebuffer;
471
}
1702 cejka 472
 
1790 jermar 473
/** @}
1702 cejka 474
 */