Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
836 palkovsky 1
/*
3707 decky 2
 * Copyright (c) 2008 Martin Decky
2071 jermar 3
 * Copyright (c) 2006 Ondrej Palkovsky
836 palkovsky 4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
15
 * - The name of the author may not be used to endorse or promote products
16
 *   derived from this software without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29
 
3707 decky 30
/** @addtogroup genarch
1702 cejka 31
 * @{
32
 */
33
/** @file
34
 */
35
 
837 palkovsky 36
#include <genarch/fb/font-8x16.h>
1993 decky 37
#include <genarch/fb/visuals.h>
837 palkovsky 38
#include <genarch/fb/fb.h>
836 palkovsky 39
#include <console/chardev.h>
40
#include <console/console.h>
1317 vana 41
#include <sysinfo/sysinfo.h>
1316 jermar 42
#include <mm/slab.h>
3707 decky 43
#include <align.h>
839 palkovsky 44
#include <panic.h>
896 jermar 45
#include <memstr.h>
1316 jermar 46
#include <config.h>
1682 palkovsky 47
#include <bitops.h>
48
#include <print.h>
2015 jermar 49
#include <ddi/ddi.h>
2054 jermar 50
#include <arch/types.h>
836 palkovsky 51
 
3707 decky 52
SPINLOCK_INITIALIZE(fb_lock);
862 palkovsky 53
 
3707 decky 54
/**< Physical memory area for fb. */
55
static parea_t fb_parea;
2015 jermar 56
 
3707 decky 57
static uint8_t *fb_addr;
58
static uint8_t *backbuf;
59
static uint8_t *glyphs;
836 palkovsky 60
 
3707 decky 61
static void *bgpixel;
836 palkovsky 62
 
3707 decky 63
static unsigned int xres;
64
static unsigned int yres;
1316 jermar 65
 
3707 decky 66
static unsigned int scanline;
67
static unsigned int glyphscanline;
836 palkovsky 68
 
3707 decky 69
static unsigned int pixelbytes;
70
static unsigned int glyphbytes;
71
 
72
static unsigned int cols;
73
static unsigned int rows;
1135 decky 74
static unsigned int position = 0;
836 palkovsky 75
 
3707 decky 76
#define BG_COLOR     0x000080
77
#define FG_COLOR     0xffff00
836 palkovsky 78
 
3707 decky 79
#define CURSOR       219
1135 decky 80
 
3707 decky 81
#define RED(x, bits)         ((x >> (8 + 8 + 8 - bits)) & ((1 << bits) - 1))
82
#define GREEN(x, bits)       ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
83
#define BLUE(x, bits)        ((x >> (8 - bits)) & ((1 << bits) - 1))
1135 decky 84
 
3707 decky 85
#define COL2X(col)           ((col) * FONT_WIDTH)
86
#define ROW2Y(row)           ((row) * FONT_SCANLINES)
1135 decky 87
 
3707 decky 88
#define X2COL(x)             ((x) / FONT_WIDTH)
89
#define Y2ROW(y)             ((y) / FONT_SCANLINES)
838 palkovsky 90
 
3707 decky 91
#define FB_POS(x, y)         ((y) * scanline + (x) * pixelbytes)
92
#define BB_POS(col, row)     ((row) * cols + (col))
93
#define GLYPH_POS(glyph, y)  ((glyph) * glyphbytes + (y) * glyphscanline)
839 palkovsky 94
 
1875 jermar 95
 
3707 decky 96
static void (*rgb_conv)(void *, uint32_t);
836 palkovsky 97
 
839 palkovsky 98
 
3707 decky 99
/** ARGB 8:8:8:8 conversion
100
 *
101
 */
102
static void rgb_0888(void *dst, uint32_t rgb)
1994 decky 103
{
3707 decky 104
    *((uint32_t *) dst) = rgb & 0xffffff;
1994 decky 105
}
106
 
3707 decky 107
 
108
/** ABGR 8:8:8:8 conversion
109
 *
110
 */
111
static void bgr_0888(void *dst, uint32_t rgb)
1994 decky 112
{
3707 decky 113
    *((uint32_t *) dst)
114
        = (BLUE(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | RED(rgb, 8);
1994 decky 115
}
116
 
3707 decky 117
 
118
/** BGR 8:8:8 conversion
119
 *
120
 */
121
static void rgb_888(void *dst, uint32_t rgb)
839 palkovsky 122
{
1871 jermar 123
#if defined(FB_INVERT_ENDIAN)
3707 decky 124
    *((uint32_t *) dst)
125
        = (BLUE(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | RED(rgb, 8)
126
        | (*((uint32_t *) dst) & 0xff0000);
1298 vana 127
#else
3707 decky 128
    *((uint32_t *) dst)
129
        = (rgb & 0xffffff) | (*((uint32_t *) dst) & 0xff0000);
1682 palkovsky 130
#endif
839 palkovsky 131
}
132
 
838 palkovsky 133
 
3707 decky 134
/** RGB 5:5:5 conversion
135
 *
136
 */
137
static void rgb_555(void *dst, uint32_t rgb)
1993 decky 138
{
3707 decky 139
    *((uint16_t *) dst)
140
        = (RED(rgb, 5) << 10) | (GREEN(rgb, 5) << 5) | BLUE(rgb, 5);
1993 decky 141
}
142
 
143
 
3707 decky 144
/** RGB 5:6:5 conversion
145
 *
146
 */
147
static void rgb_565(void *dst, uint32_t rgb)
839 palkovsky 148
{
3707 decky 149
    *((uint16_t *) dst)
150
        = (RED(rgb, 5) << 11) | (GREEN(rgb, 6) << 5) | BLUE(rgb, 5);
839 palkovsky 151
}
152
 
153
 
3707 decky 154
/** RGB 3:2:3
1837 jermar 155
 *
156
 * Even though we try 3:2:3 color scheme here, an 8-bit framebuffer
157
 * will most likely use a color palette. The color appearance
158
 * will be pretty random and depend on the default installed
159
 * palette. This could be fixed by supporting custom palette
160
 * and setting it to simulate the 8-bit truecolor.
3692 rimsky 161
 *
3709 decky 162
 * Currently we set the palette on the ia32, amd64 and sparc64 port.
3692 rimsky 163
 *
164
 * Note that the byte is being inverted by this function. The reason is
165
 * that we would like to use a color palette where the white color code
3709 decky 166
 * is 0 and the black color code is 255, as some machines (Sun Blade 1500)
3692 rimsky 167
 * use these codes for black and white and prevent to set codes
168
 * 0 and 255 to other colors.
3709 decky 169
 *
1837 jermar 170
 */
3707 decky 171
static void rgb_323(void *dst, uint32_t rgb)
839 palkovsky 172
{
3707 decky 173
    *((uint8_t *) dst)
174
        = ~((RED(rgb, 3) << 5) | (GREEN(rgb, 2) << 3) | BLUE(rgb, 3));
1135 decky 175
}
839 palkovsky 176
 
3707 decky 177
 
178
/** Draw character at given position
1837 jermar 179
 *
180
 */
3707 decky 181
static void draw_glyph(uint8_t glyph, unsigned int col, unsigned int row)
1135 decky 182
{
3707 decky 183
    unsigned int x = COL2X(col);
184
    unsigned int y = ROW2Y(row);
185
    unsigned int yd;
186
 
187
    backbuf[BB_POS(col, row)] = glyph;
188
 
189
    for (yd = 0; yd < FONT_SCANLINES; yd++)
190
        memcpy(&fb_addr[FB_POS(x, y + yd)],
191
            &glyphs[GLYPH_POS(glyph, yd)], glyphscanline);
839 palkovsky 192
}
193
 
1682 palkovsky 194
 
3707 decky 195
/** Scroll screen down by one row
196
 *
197
 *
198
 */
838 palkovsky 199
static void scroll_screen(void)
200
{
3707 decky 201
    unsigned int row;
202
 
203
    for (row = 0; row < rows; row++) {
204
        unsigned int y = ROW2Y(row);
205
        unsigned int yd;
2054 jermar 206
 
3707 decky 207
        for (yd = 0; yd < FONT_SCANLINES; yd++) {
208
            unsigned int x;
209
            unsigned int col;
210
 
211
            for (col = 0, x = 0; col < cols; col++, x += FONT_WIDTH) {
212
                uint8_t glyph;
213
 
214
                if (row < rows - 1) {
215
                    if (backbuf[BB_POS(col, row)] == backbuf[BB_POS(col, row + 1)])
216
                        continue;
217
 
218
                    glyph = backbuf[BB_POS(col, row + 1)];
219
                } else
220
                    glyph = 0;
221
 
222
                memcpy(&fb_addr[FB_POS(x, y + yd)],
223
                    &glyphs[GLYPH_POS(glyph, yd)], glyphscanline);
224
            }
2086 jermar 225
        }
1682 palkovsky 226
    }
3707 decky 227
 
228
    memcpy(backbuf, backbuf + cols, cols * (rows - 1));
229
    memsetb(&backbuf[BB_POS(0, rows - 1)], cols, 0);
838 palkovsky 230
}
231
 
232
 
3707 decky 233
static void cursor_put(void)
836 palkovsky 234
{
3707 decky 235
    draw_glyph(CURSOR, position % cols, position / cols);
836 palkovsky 236
}
237
 
238
 
3707 decky 239
static void cursor_remove(void)
836 palkovsky 240
{
3707 decky 241
    draw_glyph(0, position % cols, position / cols);
836 palkovsky 242
}
243
 
838 palkovsky 244
 
836 palkovsky 245
/** Print character to screen
246
 *
3707 decky 247
 * Emulate basic terminal commands.
248
 *
836 palkovsky 249
 */
250
static void fb_putchar(chardev_t *dev, char ch)
251
{
1287 vana 252
    spinlock_lock(&fb_lock);
1135 decky 253
 
254
    switch (ch) {
1888 jermar 255
    case '\n':
3707 decky 256
        cursor_remove();
257
        position += cols;
258
        position -= position % cols;
1888 jermar 259
        break;
260
    case '\r':
3707 decky 261
        cursor_remove();
262
        position -= position % cols;
1888 jermar 263
        break;
264
    case '\b':
3707 decky 265
        cursor_remove();
266
        if (position % cols)
1888 jermar 267
            position--;
268
        break;
269
    case '\t':
3707 decky 270
        cursor_remove();
1888 jermar 271
        do {
3707 decky 272
            draw_glyph((uint8_t) ' ', position % cols, position / cols);
838 palkovsky 273
            position++;
3707 decky 274
        } while ((position % 8) && (position < cols * rows));
1888 jermar 275
        break;
276
    default:
3707 decky 277
        draw_glyph((uint8_t) ch, position % cols, position / cols);
1888 jermar 278
        position++;
836 palkovsky 279
    }
1135 decky 280
 
3707 decky 281
    if (position >= cols * rows) {
282
        position -= cols;
836 palkovsky 283
        scroll_screen();
284
    }
1135 decky 285
 
3707 decky 286
    cursor_put();
1135 decky 287
 
1287 vana 288
    spinlock_unlock(&fb_lock);
836 palkovsky 289
}
290
 
291
static chardev_t framebuffer;
292
static chardev_operations_t fb_ops = {
293
    .write = fb_putchar,
294
};
295
 
296
 
3707 decky 297
/** Render glyphs
298
 *
299
 * Convert glyphs from device independent font
300
 * description to current visual representation.
301
 *
302
 */
303
static void render_glyphs(void)
304
{
305
    unsigned int glyph;
306
 
307
    for (glyph = 0; glyph < FONT_GLYPHS; glyph++) {
308
        unsigned int y;
309
 
310
        for (y = 0; y < FONT_SCANLINES; y++) {
311
            unsigned int x;
312
 
313
            for (x = 0; x < FONT_WIDTH; x++)
314
                rgb_conv(&glyphs[GLYPH_POS(glyph, y) + x * pixelbytes],
315
                    (fb_font[glyph * FONT_SCANLINES + y] & (1 << (7 - x))) ? FG_COLOR : BG_COLOR);
316
        }
317
    }
318
 
319
    rgb_conv(bgpixel, BG_COLOR);
320
}
321
 
322
 
323
/** Refresh the screen
324
 *
325
 */
326
void fb_redraw(void)
327
{
328
    unsigned int row;
329
 
330
    for (row = 0; row < rows; row++) {
331
        unsigned int y = ROW2Y(row);
332
        unsigned int yd;
333
 
334
        for (yd = 0; yd < FONT_SCANLINES; yd++) {
335
            unsigned int x;
336
            unsigned int col;
337
 
338
            for (col = 0, x = 0; col < cols; col++, x += FONT_WIDTH)
339
                memcpy(&fb_addr[FB_POS(x, y + yd)],
340
                &glyphs[GLYPH_POS(backbuf[BB_POS(col, row)], yd)],
341
                glyphscanline);
342
        }
343
    }
344
 
345
    if (COL2X(cols) < xres) {
346
        unsigned int y;
347
 
348
        for (y = 0; y < yres; y++) {
349
            unsigned int x;
350
 
351
            for (x = COL2X(cols); x < xres; x++)
352
                memcpy(&fb_addr[FB_POS(x, y)], bgpixel, pixelbytes);
353
        }
354
    }
355
 
356
    if (ROW2Y(rows) < yres) {
357
        unsigned int y;
358
 
359
        for (y = ROW2Y(rows); y < yres; y++) {
360
            unsigned int x;
361
 
362
            for (x = 0; x < xres; x++)
363
                memcpy(&fb_addr[FB_POS(x, y)], bgpixel, pixelbytes);
364
        }
365
    }
366
}
367
 
368
 
836 palkovsky 369
/** Initialize framebuffer as a chardev output device
370
 *
3707 decky 371
 * @param addr   Physical address of the framebuffer
372
 * @param x      Screen width in pixels
373
 * @param y      Screen height in pixels
374
 * @param scan   Bytes per one scanline
375
 * @param visual Color model
376
 *
836 palkovsky 377
 */
3672 jermar 378
void fb_init(fb_properties_t *props)
836 palkovsky 379
{
3672 jermar 380
    switch (props->visual) {
1993 decky 381
    case VISUAL_INDIRECT_8:
3707 decky 382
        rgb_conv = rgb_323;
1991 jermar 383
        pixelbytes = 1;
384
        break;
1993 decky 385
    case VISUAL_RGB_5_5_5:
3707 decky 386
        rgb_conv = rgb_555;
1991 jermar 387
        pixelbytes = 2;
388
        break;
1993 decky 389
    case VISUAL_RGB_5_6_5:
3707 decky 390
        rgb_conv = rgb_565;
1993 decky 391
        pixelbytes = 2;
1991 jermar 392
        break;
1993 decky 393
    case VISUAL_RGB_8_8_8:
3707 decky 394
        rgb_conv = rgb_888;
1993 decky 395
        pixelbytes = 3;
396
        break;
397
    case VISUAL_RGB_8_8_8_0:
3707 decky 398
        rgb_conv = rgb_888;
1991 jermar 399
        pixelbytes = 4;
400
        break;
1993 decky 401
    case VISUAL_RGB_0_8_8_8:
3707 decky 402
        rgb_conv = rgb_0888;
1993 decky 403
        pixelbytes = 4;
404
        break;
1994 decky 405
    case VISUAL_BGR_0_8_8_8:
3707 decky 406
        rgb_conv = bgr_0888;
1994 decky 407
        pixelbytes = 4;
408
        break;
1991 jermar 409
    default:
1993 decky 410
        panic("Unsupported visual.\n");
839 palkovsky 411
    }
1371 decky 412
 
3672 jermar 413
    xres = props->x;
414
    yres = props->y;
415
    scanline = props->scan;
836 palkovsky 416
 
3707 decky 417
    cols = xres / FONT_WIDTH;
418
    rows = yres / FONT_SCANLINES;
419
 
420
    glyphscanline = FONT_WIDTH * pixelbytes;
421
    glyphbytes = glyphscanline * FONT_SCANLINES;
422
 
423
    unsigned int fbsize = scanline * yres;
424
    unsigned int bbsize = cols * rows;
425
    unsigned int glyphsize = FONT_GLYPHS * glyphbytes;
426
 
427
    backbuf = (uint8_t *) malloc(bbsize, 0);
428
    if (!backbuf)
429
        panic("Unable to allocate backbuffer.\n");
430
 
431
    glyphs = (uint8_t *) malloc(glyphsize, 0);
432
    if (!glyphs)
433
        panic("Unable to allocate glyphs.\n");
434
 
435
    bgpixel = malloc(pixelbytes, 0);
436
    if (!bgpixel)
437
        panic("Unable to allocate background pixel.\n");
438
 
439
    memsetb(backbuf, bbsize, 0);
440
    memsetb(glyphs, glyphsize, 0);
441
    memsetb(bgpixel, pixelbytes, 0);
442
 
443
    render_glyphs();
444
 
445
    fb_addr = (uint8_t *) hw_map((uintptr_t) props->addr, fbsize);
446
 
3692 rimsky 447
    fb_parea.pbase = (uintptr_t) props->addr + props->offset;
3707 decky 448
    fb_parea.vbase = (uintptr_t) fb_addr;
2015 jermar 449
    fb_parea.frames = SIZE2FRAMES(fbsize);
450
    fb_parea.cacheable = false;
451
    ddi_parea_register(&fb_parea);
3707 decky 452
 
1690 palkovsky 453
    sysinfo_set_item_val("fb", NULL, true);
454
    sysinfo_set_item_val("fb.kind", NULL, 1);
455
    sysinfo_set_item_val("fb.width", NULL, xres);
456
    sysinfo_set_item_val("fb.height", NULL, yres);
3707 decky 457
    sysinfo_set_item_val("fb.scanline", NULL, scanline);
3672 jermar 458
    sysinfo_set_item_val("fb.visual", NULL, props->visual);
459
    sysinfo_set_item_val("fb.address.physical", NULL, props->addr);
1990 decky 460
 
3707 decky 461
    fb_redraw();
462
 
836 palkovsky 463
    chardev_initialize("fb", &framebuffer, &fb_ops);
464
    stdout = &framebuffer;
465
}
1702 cejka 466
 
1790 jermar 467
/** @}
1702 cejka 468
 */