Subversion Repositories HelenOS

Rev

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