Subversion Repositories HelenOS

Rev

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