Subversion Repositories HelenOS

Rev

Rev 1787 | Rev 1837 | 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;
1334 jermar 102
#if (defined(BIG_ENDIAN) || defined(FB_BIG_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;
1334 jermar 116
#if (defined(BIG_ENDIAN) || defined(FB_BIG_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
 
886 jermar 137
/** Put pixel - 8-bit depth (3:2:3) */
1682 palkovsky 138
static void rgb_1byte(void *dst, int rgb)
839 palkovsky 139
{
1780 jermar 140
    *(uint8_t *)dst = RED(rgb, 3) << 5 | GREEN(rgb, 2) << 3 | BLUE(rgb, 3);
1135 decky 141
}
839 palkovsky 142
 
1135 decky 143
/** Return pixel color - 8-bit depth (3:2:3) */
1682 palkovsky 144
static int byte1_rgb(void *src)
1135 decky 145
{
1780 jermar 146
    int color = *(uint8_t *)src;
1135 decky 147
    return (((color >> 5) & 0x7) << (16 + 5)) | (((color >> 3) & 0x3) << (8 + 6)) | ((color & 0x7) << 5);
839 palkovsky 148
}
149
 
1682 palkovsky 150
static void putpixel(unsigned int x, unsigned int y, int color)
1135 decky 151
{
1682 palkovsky 152
    (*rgb2scr)(&fbaddress[POINTPOS(x,y)],color);
153
 
154
    if (dbbuffer) {
155
        int dline = (y + dboffset) % yres;
156
        (*rgb2scr)(&dbbuffer[POINTPOS(x,dline)],color);
157
    }
1135 decky 158
}
839 palkovsky 159
 
1682 palkovsky 160
/** Get pixel from viewport */
161
static int getpixel(unsigned int x, unsigned int y)
162
{
163
    if (dbbuffer) {
164
        int dline = (y + dboffset) % yres;
165
        return (*scr2rgb)(&dbbuffer[POINTPOS(x,dline)]);
166
    }
167
    return (*scr2rgb)(&fbaddress[POINTPOS(x,y)]);
168
}
1135 decky 169
 
1682 palkovsky 170
 
1135 decky 171
/** Fill screen with background color */
172
static void clear_screen(void)
839 palkovsky 173
{
1135 decky 174
    unsigned int y;
839 palkovsky 175
 
1682 palkovsky 176
    for (y = 0; y < yres; y++) {
177
        memcpy(&fbaddress[scanline*y], blankline, xres*pixelbytes);
178
        if (dbbuffer)
179
            memcpy(&dbbuffer[scanline*y], blankline, xres*pixelbytes);
180
    }
839 palkovsky 181
}
182
 
1135 decky 183
 
838 palkovsky 184
/** Scroll screen one row up */
185
static void scroll_screen(void)
186
{
1780 jermar 187
    uint8_t *lastline = &fbaddress[(rows - 1) * ROW_BYTES];
1682 palkovsky 188
    int firstsz;
838 palkovsky 189
 
1682 palkovsky 190
    if (dbbuffer) {
191
        memcpy(&dbbuffer[dboffset*scanline], blankline, FONT_SCANLINES*scanline);
192
 
193
        dboffset = (dboffset + FONT_SCANLINES) % yres;
194
        firstsz = yres-dboffset;
838 palkovsky 195
 
1682 palkovsky 196
        memcpy(fbaddress, &dbbuffer[scanline*dboffset], firstsz*scanline);
197
        memcpy(&fbaddress[firstsz*scanline], dbbuffer, dboffset*scanline);
198
    } else {
199
        memcpy((void *) fbaddress, (void *) &fbaddress[ROW_BYTES], scanline * yres - ROW_BYTES);
200
        /* Clear last row */
201
        memcpy((void *) lastline, (void *) blankline, ROW_BYTES);
202
    }
838 palkovsky 203
}
204
 
205
 
1135 decky 206
static void invert_pixel(unsigned int x, unsigned int y)
836 palkovsky 207
{
1682 palkovsky 208
    putpixel(x, y, ~getpixel(x, y));
836 palkovsky 209
}
210
 
211
 
212
/** Draw one line of glyph at a given position */
1135 decky 213
static void draw_glyph_line(unsigned int glline, unsigned int x, unsigned int y)
836 palkovsky 214
{
1135 decky 215
    unsigned int i;
836 palkovsky 216
 
1135 decky 217
    for (i = 0; i < 8; i++)
218
        if (glline & (1 << (7 - i))) {
1682 palkovsky 219
            putpixel(x + i, y, FGCOLOR);
836 palkovsky 220
        } else
1682 palkovsky 221
            putpixel(x + i, y, BGCOLOR);
836 palkovsky 222
}
223
 
838 palkovsky 224
/***************************************************************/
225
/* Character-console functions */
226
 
836 palkovsky 227
/** Draw character at given position */
1780 jermar 228
static void draw_glyph(uint8_t glyph, unsigned int col, unsigned int row)
836 palkovsky 229
{
1135 decky 230
    unsigned int y;
836 palkovsky 231
 
1135 decky 232
    for (y = 0; y < FONT_SCANLINES; y++)
233
        draw_glyph_line(fb_font[glyph * FONT_SCANLINES + y], col * COL_WIDTH, row * FONT_SCANLINES + y);
836 palkovsky 234
}
235
 
236
/** Invert character at given position */
1135 decky 237
static void invert_char(unsigned int col, unsigned int row)
836 palkovsky 238
{
1135 decky 239
    unsigned int x;
240
    unsigned int y;
836 palkovsky 241
 
1135 decky 242
    for (x = 0; x < COL_WIDTH; x++)
243
        for (y = 0; y < FONT_SCANLINES; y++)
244
            invert_pixel(col * COL_WIDTH + x, row * FONT_SCANLINES + y);
836 palkovsky 245
}
246
 
247
/** Draw character at default position */
248
static void draw_char(char chr)
249
{
1135 decky 250
    draw_glyph(chr, position % columns, position / columns);
836 palkovsky 251
}
252
 
1135 decky 253
static void draw_logo(unsigned int startx, unsigned int starty)
862 palkovsky 254
{
1135 decky 255
    unsigned int x;
256
    unsigned int y;
257
    unsigned int byte;
258
    unsigned int rowbytes;
862 palkovsky 259
 
1135 decky 260
    rowbytes = (helenos_width - 1) / 8 + 1;
862 palkovsky 261
 
1135 decky 262
    for (y = 0; y < helenos_height; y++)
263
        for (x = 0; x < helenos_width; x++) {
264
            byte = helenos_bits[rowbytes * y + x / 8];
862 palkovsky 265
            byte >>= x % 8;
266
            if (byte & 1)
1682 palkovsky 267
                putpixel(startx + x, starty + y, LOGOCOLOR);
862 palkovsky 268
        }
269
}
270
 
838 palkovsky 271
/***************************************************************/
272
/* Stdout specific functions */
273
 
836 palkovsky 274
static void invert_cursor(void)
275
{
1135 decky 276
    invert_char(position % columns, position / columns);
836 palkovsky 277
}
278
 
279
/** Print character to screen
280
 *
281
 *  Emulate basic terminal commands
282
 */
283
static void fb_putchar(chardev_t *dev, char ch)
284
{
1287 vana 285
    spinlock_lock(&fb_lock);
1135 decky 286
 
287
    switch (ch) {
288
        case '\n':
289
            invert_cursor();
290
            position += columns;
291
            position -= position % columns;
292
            break;
293
        case '\r':
294
            invert_cursor();
295
            position -= position % columns;
296
            break;
297
        case '\b':
298
            invert_cursor();
299
            if (position % columns)
300
                position--;
301
            break;
302
        case '\t':
303
            invert_cursor();
304
            do {
305
                draw_char(' ');
306
                position++;
1312 palkovsky 307
            } while ((position % 8) && position < columns * rows);
1135 decky 308
            break;
309
        default:
310
            draw_char(ch);
838 palkovsky 311
            position++;
836 palkovsky 312
    }
1135 decky 313
 
314
    if (position >= columns * rows) {
836 palkovsky 315
        position -= columns;
316
        scroll_screen();
317
    }
1135 decky 318
 
836 palkovsky 319
    invert_cursor();
1135 decky 320
 
1287 vana 321
    spinlock_unlock(&fb_lock);
836 palkovsky 322
}
323
 
324
static chardev_t framebuffer;
325
static chardev_operations_t fb_ops = {
326
    .write = fb_putchar,
327
};
328
 
329
 
330
/** Initialize framebuffer as a chardev output device
331
 *
1371 decky 332
 * @param addr Physical address of the framebuffer
1135 decky 333
 * @param x    Screen width in pixels
334
 * @param y    Screen height in pixels
335
 * @param bpp  Bits per pixel (8, 16, 24, 32)
336
 * @param scan Bytes per one scanline
337
 *
836 palkovsky 338
 */
1780 jermar 339
void fb_init(uintptr_t addr, unsigned int x, unsigned int y, unsigned int bpp, unsigned int scan)
836 palkovsky 340
{
1135 decky 341
    switch (bpp) {
342
        case 8:
1682 palkovsky 343
            rgb2scr = rgb_1byte;
344
            scr2rgb = byte1_rgb;
1135 decky 345
            pixelbytes = 1;
346
            break;
347
        case 16:
1682 palkovsky 348
            rgb2scr = rgb_2byte;
349
            scr2rgb = byte2_rgb;
1135 decky 350
            pixelbytes = 2;
351
            break;
352
        case 24:
1682 palkovsky 353
            rgb2scr = rgb_3byte;
354
            scr2rgb = byte3_rgb;
1135 decky 355
            pixelbytes = 3;
356
            break;
357
        case 32:
1682 palkovsky 358
            rgb2scr = rgb_4byte;
359
            scr2rgb = byte4_rgb;
1135 decky 360
            pixelbytes = 4;
361
            break;
362
        default:
363
            panic("Unsupported bpp");
839 palkovsky 364
    }
1371 decky 365
 
366
    unsigned int fbsize = scan * y;
367
 
368
    /* Map the framebuffer */
1780 jermar 369
    fbaddress = (uint8_t *) hw_map((uintptr_t) addr, fbsize);
1371 decky 370
 
836 palkovsky 371
    xres = x;
372
    yres = y;
1327 decky 373
    bitspp = bpp;
1135 decky 374
    scanline = scan;
836 palkovsky 375
 
1135 decky 376
    rows = y / FONT_SCANLINES;
377
    columns = x / COL_WIDTH;
836 palkovsky 378
 
1690 palkovsky 379
    sysinfo_set_item_val("fb", NULL, true);
380
    sysinfo_set_item_val("fb.kind", NULL, 1);
381
    sysinfo_set_item_val("fb.width", NULL, xres);
382
    sysinfo_set_item_val("fb.height", NULL, yres);
383
    sysinfo_set_item_val("fb.bpp", NULL, bpp);
384
    sysinfo_set_item_val("fb.scanline", NULL, scan);
385
    sysinfo_set_item_val("fb.address.physical", NULL, addr);
386
 
1682 palkovsky 387
    /* Allocate double buffer */
388
    int totsize = scanline * yres;
389
    int pages = SIZE2FRAMES(totsize);
390
    int order;
391
    if (pages == 1)
392
        order = 0;
393
    else
394
        order = fnzb(pages-1)+1;
395
 
1766 palkovsky 396
    dbbuffer = frame_alloc(order,FRAME_ATOMIC | FRAME_KA);
1760 palkovsky 397
    if (!dbbuffer)
1682 palkovsky 398
        printf("Failed to allocate scroll buffer.\n");
399
    dboffset = 0;
400
 
401
    /* Initialized blank line */
1780 jermar 402
    blankline = (uint8_t *) malloc(ROW_BYTES, FRAME_ATOMIC);
1766 palkovsky 403
    if (!blankline)
404
        panic("Failed to allocate blank line for framebuffer.");
1682 palkovsky 405
    for (y=0; y < FONT_SCANLINES; y++)
406
        for (x=0; x < xres; x++)
407
            (*rgb2scr)(&blankline[POINTPOS(x,y)],BGCOLOR);
408
 
409
    clear_screen();
410
 
411
    /* Update size of screen to match text area */
412
    yres = rows * FONT_SCANLINES;
413
 
1135 decky 414
    draw_logo(xres - helenos_width, 0);
836 palkovsky 415
    invert_cursor();
416
 
417
    chardev_initialize("fb", &framebuffer, &fb_ops);
418
    stdout = &framebuffer;
1371 decky 419
 
836 palkovsky 420
}
1702 cejka 421
 
1790 jermar 422
/** @}
1702 cejka 423
 */