Subversion Repositories HelenOS-historic

Rev

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