Subversion Repositories HelenOS-historic

Rev

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