Subversion Repositories HelenOS-historic

Rev

Rev 839 | Rev 886 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 839 Rev 862
1
/*
1
/*
2
 * Copyright (C) 2006 Ondrej Palkovsky
2
 * Copyright (C) 2006 Ondrej Palkovsky
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
#include <genarch/fb/font-8x16.h>
29
#include <genarch/fb/font-8x16.h>
30
#include <genarch/fb/fb.h>
30
#include <genarch/fb/fb.h>
31
#include <console/chardev.h>
31
#include <console/chardev.h>
32
#include <console/console.h>
32
#include <console/console.h>
33
#include <panic.h>
33
#include <panic.h>
34
 
34
 
-
 
35
#include "helenos.xbm"
-
 
36
 
35
SPINLOCK_INITIALIZE(fb_lock);
37
SPINLOCK_INITIALIZE(fb_lock);
36
 
38
 
37
static __u8 *fbaddress=NULL;
39
static __u8 *fbaddress=NULL;
38
static int xres,yres;
40
static int xres,yres;
39
static int position=0;
41
static int position=0;
40
static int columns=0;
42
static int columns=0;
41
static int rows=0;
43
static int rows=0;
42
static int pixelbytes=0;
44
static int pixelbytes=0;
43
 
45
 
44
#define COL_WIDTH   8
46
#define COL_WIDTH   8
45
#define ROW_HEIGHT  (FONT_SCANLINES)
47
#define ROW_HEIGHT  (FONT_SCANLINES)
46
#define ROW_PIX     (xres*ROW_HEIGHT*pixelbytes)
48
#define ROW_PIX     (xres*ROW_HEIGHT*pixelbytes)
47
 
49
 
48
#define BGCOLOR   0x000080
50
#define BGCOLOR   0x000080
49
#define FGCOLOR   0xffff00
51
#define FGCOLOR   0xffff00
-
 
52
#define LOGOCOLOR 0x2020b0
50
 
53
 
51
#define RED(x,bits)    ((x >> (16+8-bits)) & ((1<<bits)-1))
54
#define RED(x,bits)    ((x >> (16+8-bits)) & ((1<<bits)-1))
52
#define GREEN(x,bits)  ((x >> (8+8-bits)) & ((1<<bits)-1))
55
#define GREEN(x,bits)  ((x >> (8+8-bits)) & ((1<<bits)-1))
53
#define BLUE(x,bits)   ((x >> (8-bits)) & ((1<<bits)-1))
56
#define BLUE(x,bits)   ((x >> (8-bits)) & ((1<<bits)-1))
54
 
57
 
55
#define POINTPOS(x,y)   ((y*xres+x)*pixelbytes)
58
#define POINTPOS(x,y)   ((y*xres+x)*pixelbytes)
56
 
59
 
57
/***************************************************************/
60
/***************************************************************/
58
/* Pixel specific fuctions */
61
/* Pixel specific fuctions */
59
 
62
 
60
static void (*putpixel)(int x,int y,int color);
63
static void (*putpixel)(int x,int y,int color);
61
static int (*getpixel)(int x,int y);
64
static int (*getpixel)(int x,int y);
62
 
65
 
63
/** Put pixel - 24-bit depth, 1 free byte */
66
/** Put pixel - 24-bit depth, 1 free byte */
64
static void putpixel_4byte(int x,int y,int color)
67
static void putpixel_4byte(int x,int y,int color)
65
{
68
{
66
    int startbyte = POINTPOS(x,y);
69
    int startbyte = POINTPOS(x,y);
67
 
70
 
68
    *((__u32 *)(fbaddress+startbyte)) = color;
71
    *((__u32 *)(fbaddress+startbyte)) = color;
69
}
72
}
70
 
73
 
71
/** Return pixel color */
74
/** Return pixel color */
72
static int getpixel_4byte(int x,int y)
75
static int getpixel_4byte(int x,int y)
73
{
76
{
74
    int startbyte = POINTPOS(x,y);
77
    int startbyte = POINTPOS(x,y);
75
 
78
 
76
    return *((__u32 *)(fbaddress+startbyte)) & 0xffffff;
79
    return *((__u32 *)(fbaddress+startbyte)) & 0xffffff;
77
}
80
}
78
 
81
 
79
/** Draw pixel of given color on screen - 24-bit depth */
82
/** Draw pixel of given color on screen - 24-bit depth */
80
static void putpixel_3byte(int x,int y,int color)
83
static void putpixel_3byte(int x,int y,int color)
81
{
84
{
82
    int startbyte = POINTPOS(x,y);
85
    int startbyte = POINTPOS(x,y);
83
 
86
 
84
    fbaddress[startbyte] = RED(color,8);
87
    fbaddress[startbyte] = RED(color,8);
85
    fbaddress[startbyte+1] = GREEN(color,8);
88
    fbaddress[startbyte+1] = GREEN(color,8);
86
    fbaddress[startbyte+2] = BLUE(color,8);
89
    fbaddress[startbyte+2] = BLUE(color,8);
87
}
90
}
88
 
91
 
89
static int getpixel_3byte(int x,int y)
92
static int getpixel_3byte(int x,int y)
90
{
93
{
91
    int startbyte = POINTPOS(x,y);
94
    int startbyte = POINTPOS(x,y);
92
    int result;
95
    int result;
93
 
96
 
94
    result = fbaddress[startbyte] << 16 \
97
    result = fbaddress[startbyte] << 16 \
95
        | fbaddress[startbyte+1] << 8 \
98
        | fbaddress[startbyte+1] << 8 \
96
        | fbaddress[startbyte+2];
99
        | fbaddress[startbyte+2];
97
    return result;
100
    return result;
98
}
101
}
99
 
102
 
100
/** Put pixel - 16-bit depth (5:6:6) */
103
/** Put pixel - 16-bit depth (5:6:6) */
101
static void putpixel_2byte(int x,int y,int color)
104
static void putpixel_2byte(int x,int y,int color)
102
{
105
{
103
    int startbyte = POINTPOS(x,y);
106
    int startbyte = POINTPOS(x,y);
104
    int compcolor;
107
    int compcolor;
105
 
108
 
106
    /* 5-bit, 5-bits, 5-bits */
109
    /* 5-bit, 5-bits, 5-bits */
107
    compcolor = RED(color,5) << 11 \
110
    compcolor = RED(color,5) << 11 \
108
        | GREEN(color,6) << 5 \
111
        | GREEN(color,6) << 5 \
109
        | BLUE(color,5);
112
        | BLUE(color,5);
110
    *((__u16 *)(fbaddress+startbyte)) = compcolor;
113
    *((__u16 *)(fbaddress+startbyte)) = compcolor;
111
}
114
}
112
 
115
 
113
static int getpixel_2byte(int x,int y)
116
static int getpixel_2byte(int x,int y)
114
{
117
{
115
    int startbyte = POINTPOS(x,y);
118
    int startbyte = POINTPOS(x,y);
116
    int color;
119
    int color;
117
    int red,green,blue;
120
    int red,green,blue;
118
 
121
 
119
    color = *((__u16 *)(fbaddress+startbyte));
122
    color = *((__u16 *)(fbaddress+startbyte));
120
    red = (color >> 11) & 0x1f;
123
    red = (color >> 11) & 0x1f;
121
    green = (color >> 5) & 0x3f;
124
    green = (color >> 5) & 0x3f;
122
    blue = color & 0x1f;
125
    blue = color & 0x1f;
123
    return (red << (16+3)) | (green << (8+2)) | (blue << 3);
126
    return (red << (16+3)) | (green << (8+2)) | (blue << 3);
124
}
127
}
125
 
128
 
126
/** Put pixel - 8-bit depth (3:3:2) */
129
/** Put pixel - 8-bit depth (3:3:2) */
127
static void putpixel_1byte(int x,int y,int color)
130
static void putpixel_1byte(int x,int y,int color)
128
{
131
{
129
    int compcolor;
132
    int compcolor;
130
 
133
 
131
    /* 3-bit, 3-bits, 2-bits */
134
    /* 3-bit, 3-bits, 2-bits */
132
    compcolor = RED(color,3) << 5 \
135
    compcolor = RED(color,3) << 5 \
133
        | GREEN(color,3) << 2 \
136
        | GREEN(color,3) << 2 \
134
        | BLUE(color,2);
137
        | BLUE(color,2);
135
    fbaddress[POINTPOS(x,y)] = compcolor;
138
    fbaddress[POINTPOS(x,y)] = compcolor;
136
}
139
}
137
 
140
 
138
 
141
 
139
static int getpixel_1byte(int x,int y)
142
static int getpixel_1byte(int x,int y)
140
{
143
{
141
    int color;
144
    int color;
142
    int red,green,blue;
145
    int red,green,blue;
143
 
146
 
144
    color = fbaddress[POINTPOS(x,y)];
147
    color = fbaddress[POINTPOS(x,y)];
145
    red = (color >> 5) & 0x7;
148
    red = (color >> 5) & 0x7;
146
    green = (color >> 5) & 0x7;
149
    green = (color >> 5) & 0x7;
147
    blue = color & 0x3;
150
    blue = color & 0x3;
148
    return (red << (16+5)) | (green << (8+5)) | blue << 6;
151
    return (red << (16+5)) | (green << (8+5)) | blue << 6;
149
}
152
}
150
 
153
 
151
static void clear_line(int y);
154
static void clear_line(int y);
152
/** Scroll screen one row up */
155
/** Scroll screen one row up */
153
static void scroll_screen(void)
156
static void scroll_screen(void)
154
{
157
{
155
    int i;
158
    int i;
156
 
159
 
157
    for (i=0;i < (xres*yres*pixelbytes)-ROW_PIX; i++)
160
    for (i=0;i < (xres*yres*pixelbytes)-ROW_PIX; i++)
158
        fbaddress[i] = fbaddress[i+ROW_PIX];
161
        fbaddress[i] = fbaddress[i+ROW_PIX];
159
 
162
 
160
    /* Clear last row */
163
    /* Clear last row */
161
    for (i=0; i < ROW_HEIGHT;i++)
164
    for (i=0; i < ROW_HEIGHT;i++)
162
        clear_line((rows-1)*ROW_HEIGHT+i);
165
        clear_line((rows-1)*ROW_HEIGHT+i);
163
   
166
   
164
}
167
}
165
 
168
 
166
 
169
 
167
/***************************************************************/
170
/***************************************************************/
168
/* Screen specific function */
171
/* Screen specific function */
169
 
172
 
170
/** Fill line with color BGCOLOR */
173
/** Fill line with color BGCOLOR */
171
static void clear_line(int y)
174
static void clear_line(int y)
172
{
175
{
173
    int x;
176
    int x;
174
    for (x=0; x<xres;x++)
177
    for (x=0; x<xres;x++)
175
        (*putpixel)(x,y,BGCOLOR);
178
        (*putpixel)(x,y,BGCOLOR);
176
}
179
}
177
 
180
 
178
/** Fill screen with background color */
181
/** Fill screen with background color */
179
static void clear_screen(void)
182
static void clear_screen(void)
180
{
183
{
181
    int y;
184
    int y;
182
 
185
 
183
    for (y=0; y<yres;y++)
186
    for (y=0; y<yres;y++)
184
        clear_line(y);
187
        clear_line(y);
185
}
188
}
186
 
189
 
187
static void invert_pixel(int x, int y)
190
static void invert_pixel(int x, int y)
188
{
191
{
189
    (*putpixel)(x,y, ~(*getpixel)(x,y));
192
    (*putpixel)(x,y, ~(*getpixel)(x,y));
190
}
193
}
191
 
194
 
192
 
195
 
193
/** Draw one line of glyph at a given position */
196
/** Draw one line of glyph at a given position */
194
static void draw_glyph_line(int glline, int x, int y)
197
static void draw_glyph_line(int glline, int x, int y)
195
{
198
{
196
    int i;
199
    int i;
197
 
200
 
198
    for (i=0; i < 8; i++)
201
    for (i=0; i < 8; i++)
199
        if (glline & (1 << (7-i))) {
202
        if (glline & (1 << (7-i))) {
200
            (*putpixel)(x+i,y,FGCOLOR);
203
            (*putpixel)(x+i,y,FGCOLOR);
201
        } else
204
        } else
202
            (*putpixel)(x+i,y,BGCOLOR);
205
            (*putpixel)(x+i,y,BGCOLOR);
203
}
206
}
204
 
207
 
205
/***************************************************************/
208
/***************************************************************/
206
/* Character-console functions */
209
/* Character-console functions */
207
 
210
 
208
/** Draw character at given position */
211
/** Draw character at given position */
209
static void draw_glyph(__u8 glyph, int col, int row)
212
static void draw_glyph(__u8 glyph, int col, int row)
210
{
213
{
211
    int y;
214
    int y;
212
 
215
 
213
    for (y=0; y < FONT_SCANLINES; y++)
216
    for (y=0; y < FONT_SCANLINES; y++)
214
        draw_glyph_line(fb_font[glyph*FONT_SCANLINES+y],
217
        draw_glyph_line(fb_font[glyph*FONT_SCANLINES+y],
215
                col*COL_WIDTH, row*ROW_HEIGHT+y);
218
                col*COL_WIDTH, row*ROW_HEIGHT+y);
216
}
219
}
217
 
220
 
218
/** Invert character at given position */
221
/** Invert character at given position */
219
static void invert_char(int col, int row)
222
static void invert_char(int col, int row)
220
{
223
{
221
    int x,y;
224
    int x,y;
222
 
225
 
223
    for (x=0; x < COL_WIDTH; x++)
226
    for (x=0; x < COL_WIDTH; x++)
224
        for (y=0; y < FONT_SCANLINES; y++)
227
        for (y=0; y < FONT_SCANLINES; y++)
225
            invert_pixel(col*COL_WIDTH+x,row*ROW_HEIGHT+y);
228
            invert_pixel(col*COL_WIDTH+x,row*ROW_HEIGHT+y);
226
}
229
}
227
 
230
 
228
/** Draw character at default position */
231
/** Draw character at default position */
229
static void draw_char(char chr)
232
static void draw_char(char chr)
230
{
233
{
231
    draw_glyph(chr, position % columns, position/columns);
234
    draw_glyph(chr, position % columns, position/columns);
232
}
235
}
233
 
236
 
-
 
237
static void draw_logo(int startx, int starty)
-
 
238
{
-
 
239
    int x,y;
-
 
240
    int byte;
-
 
241
    int rowbytes;
-
 
242
 
-
 
243
    rowbytes = (helenos_width-1)/8 + 1;
-
 
244
 
-
 
245
    for (y=0;y < helenos_height;y++)
-
 
246
        for (x=0;x < helenos_width; x++ ) {
-
 
247
            byte = helenos_bits[rowbytes*y + x/8];
-
 
248
            byte >>= x % 8;
-
 
249
            if (byte & 1)
-
 
250
                (*putpixel)(startx+x,starty+y,LOGOCOLOR);
-
 
251
        }
-
 
252
}
-
 
253
 
234
/***************************************************************/
254
/***************************************************************/
235
/* Stdout specific functions */
255
/* Stdout specific functions */
236
 
256
 
237
static void invert_cursor(void)
257
static void invert_cursor(void)
238
{
258
{
239
    invert_char(position % columns, position/columns);
259
    invert_char(position % columns, position/columns);
240
}
260
}
241
 
261
 
242
/** Print character to screen
262
/** Print character to screen
243
 *
263
 *
244
 *  Emulate basic terminal commands
264
 *  Emulate basic terminal commands
245
 */
265
 */
246
static void fb_putchar(chardev_t *dev, char ch)
266
static void fb_putchar(chardev_t *dev, char ch)
247
{
267
{
248
    spinlock_lock(&fb->lock);
268
    spinlock_lock(&fb->lock);
249
 
269
 
250
    if (ch == '\n') {
270
    if (ch == '\n') {
251
        invert_cursor();
271
        invert_cursor();
252
        position += columns;
272
        position += columns;
253
        position -= position % columns;
273
        position -= position % columns;
254
    } else if (ch == '\r') {
274
    } else if (ch == '\r') {
255
        invert_cursor();
275
        invert_cursor();
256
        position -= position % columns;
276
        position -= position % columns;
257
    } else if (ch == '\b') {
277
    } else if (ch == '\b') {
258
        invert_cursor();
278
        invert_cursor();
259
        if (position % columns)
279
        if (position % columns)
260
            position--;
280
            position--;
261
    } else if (ch == '\t') {
281
    } else if (ch == '\t') {
262
        invert_cursor();
282
        invert_cursor();
263
        do {
283
        do {
264
            draw_char(' ');
284
            draw_char(' ');
265
            position++;
285
            position++;
266
        } while (position % 8);
286
        } while (position % 8);
267
    } else {
287
    } else {
268
        draw_char(ch);
288
        draw_char(ch);
269
        position++;
289
        position++;
270
    }
290
    }
271
    if (position >= columns*rows) {
291
    if (position >= columns*rows) {
272
        position -= columns;
292
        position -= columns;
273
        scroll_screen();
293
        scroll_screen();
274
    }
294
    }
275
    invert_cursor();
295
    invert_cursor();
276
    spinlock_unlock(&fb->lock);
296
    spinlock_unlock(&fb->lock);
277
}
297
}
278
 
298
 
279
static chardev_t framebuffer;
299
static chardev_t framebuffer;
280
static chardev_operations_t fb_ops = {
300
static chardev_operations_t fb_ops = {
281
    .write = fb_putchar,
301
    .write = fb_putchar,
282
};
302
};
283
 
303
 
284
 
304
 
285
/** Initialize framebuffer as a chardev output device
305
/** Initialize framebuffer as a chardev output device
286
 *
306
 *
287
 * @param addr Address of framebuffer
307
 * @param addr Address of framebuffer
288
 * @param x X resolution
308
 * @param x X resolution
289
 * @param y Y resolution
309
 * @param y Y resolution
290
 * @param bytes Bytes per pixel (2,3,4)
310
 * @param bytes Bytes per pixel (2,3,4)
291
 */
311
 */
292
void fb_init(__address addr, int x, int y, int bytes)
312
void fb_init(__address addr, int x, int y, int bytes)
293
{
313
{
294
    fbaddress = (unsigned char *)addr;
314
    fbaddress = (unsigned char *)addr;
295
 
315
 
296
    switch (bytes) {
316
    switch (bytes) {
297
    case 1:
317
    case 1:
298
        putpixel = putpixel_1byte;
318
        putpixel = putpixel_1byte;
299
        getpixel = getpixel_1byte;
319
        getpixel = getpixel_1byte;
300
        break;
320
        break;
301
    case 2:
321
    case 2:
302
        putpixel = putpixel_2byte;
322
        putpixel = putpixel_2byte;
303
        getpixel = getpixel_2byte;
323
        getpixel = getpixel_2byte;
304
        break;
324
        break;
305
    case 3:
325
    case 3:
306
        putpixel = putpixel_3byte;
326
        putpixel = putpixel_3byte;
307
        getpixel = getpixel_3byte;
327
        getpixel = getpixel_3byte;
308
        break;
328
        break;
309
    case 4:
329
    case 4:
310
        putpixel = putpixel_4byte;
330
        putpixel = putpixel_4byte;
311
        getpixel = getpixel_4byte;
331
        getpixel = getpixel_4byte;
312
        break;
332
        break;
313
    default:
333
    default:
314
        panic("Unsupported color depth");
334
        panic("Unsupported color depth");
315
    }
335
    }
316
   
336
   
317
    xres = x;
337
    xres = x;
318
    yres = y;
338
    yres = y;
319
    pixelbytes = bytes;
339
    pixelbytes = bytes;
320
   
340
   
321
    rows = y/ROW_HEIGHT;
341
    rows = y/ROW_HEIGHT;
322
    columns = x/COL_WIDTH;
342
    columns = x/COL_WIDTH;
323
 
343
 
324
    clear_screen();
344
    clear_screen();
-
 
345
    draw_logo(xres-helenos_width, 0);
325
    invert_cursor();
346
    invert_cursor();
326
 
347
 
327
    chardev_initialize("fb", &framebuffer, &fb_ops);
348
    chardev_initialize("fb", &framebuffer, &fb_ops);
328
    stdout = &framebuffer;
349
    stdout = &framebuffer;
329
}
350
}
330
 
351
 
331
 
352