Subversion Repositories HelenOS

Rev

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