Subversion Repositories HelenOS-historic

Rev

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