Subversion Repositories HelenOS-historic

Rev

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

Rev 837 Rev 838
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 <print.h>
33
#include <print.h>
34
 
34
 
35
SPINLOCK_INITIALIZE(fb_lock);
35
SPINLOCK_INITIALIZE(fb_lock);
36
 
36
 
37
static unsigned char *fbaddress=NULL;
37
static __u8 *fbaddress=NULL;
38
static int xres,yres;
38
static int xres,yres;
39
static int position=0;
39
static int position=0;
40
static int columns=0;
40
static int columns=0;
41
static int rows=0;
41
static int rows=0;
-
 
42
static int pixelbytes=0;
42
 
43
 
43
#define COL_WIDTH   8
44
#define COL_WIDTH   8
44
#define ROW_HEIGHT  (FONT_SCANLINES)
45
#define ROW_HEIGHT  (FONT_SCANLINES)
45
#define ROW_PIX     (xres*ROW_HEIGHT*3)
46
#define ROW_PIX     (xres*ROW_HEIGHT*pixelbytes)
46
 
47
 
47
#define BGCOLOR   0x000080
48
#define BGCOLOR   0x000080
48
#define FGCOLOR   0xffff00
49
#define FGCOLOR   0xffff00
49
 
50
 
50
#define RED(x)    ((x >> 16) & 0xff)
51
#define RED(x,bits)    ((x >> (16+8-bits)) & ((1<<bits)-1))
51
#define GREEN(x)  ((x >> 8) & 0xff)
52
#define GREEN(x,bits)  ((x >> (8+8-bits)) & ((1<<bits)-1))
52
#define BLUE(x)   (x & 0xff)
53
#define BLUE(x,bits)   ((x >> (8-bits)) & ((1<<bits)-1))
53
 
54
 
54
#define POINTPOS(x,y,colidx)   ((y*xres+x)*3+colidx)
55
#define POINTPOS(x,y)   ((y*xres+x)*pixelbytes)
-
 
56
 
-
 
57
/***************************************************************/
-
 
58
/* Pixel specific fuctions */
55
 
59
 
56
/** Draw pixel of given color on screen */
60
/** Draw pixel of given color on screen */
57
static inline void putpixel(int x,int y,int color)
61
static inline void putpixel(int x,int y,int color)
58
{
62
{
-
 
63
    int startbyte = POINTPOS(x,y);
-
 
64
 
-
 
65
    if (pixelbytes == 3) {
59
    fbaddress[POINTPOS(x,y,0)] = RED(color);
66
        fbaddress[startbyte] = RED(color,8);
60
    fbaddress[POINTPOS(x,y,1)] = GREEN(color);
67
        fbaddress[startbyte+1] = GREEN(color,8);
61
    fbaddress[POINTPOS(x,y,2)] = BLUE(color);
68
        fbaddress[startbyte+2] = BLUE(color,8);
-
 
69
    } else if (pixelbytes == 4) {
-
 
70
        *((__u32 *)(fbaddress+startbyte)) = color;
-
 
71
    } else {
-
 
72
        int compcolor;
-
 
73
        /* 5-bit, 5-bits, 5-bits */
-
 
74
        compcolor = RED(color,5) << 10 \
-
 
75
            | GREEN(color,5) << 5 \
-
 
76
            | BLUE(color,5);
-
 
77
        *((__u16 *)(fbaddress+startbyte)) = compcolor;
-
 
78
    }
-
 
79
}
-
 
80
 
-
 
81
/** Return pixel color */
-
 
82
static inline int getpixel(int x,int y)
-
 
83
{
-
 
84
    int startbyte = POINTPOS(x,y);
-
 
85
    int color;
-
 
86
    int result;
-
 
87
 
-
 
88
    if (pixelbytes == 3) {
-
 
89
        result = fbaddress[startbyte] << 16 \
-
 
90
            | fbaddress[startbyte+1] << 8 \
-
 
91
            | fbaddress[startbyte+2];
-
 
92
    } else if (pixelbytes == 4) {
-
 
93
        result = *((__u32 *)(fbaddress+startbyte)) & 0xffffff;
-
 
94
    } else {
-
 
95
        int red,green,blue;
-
 
96
        color = *((__u16 *)(fbaddress+startbyte));
-
 
97
        red = (color >> 10) & 0x1f;
-
 
98
        green = (color >> 5) & 0x1f;
-
 
99
        blue = color & 0x1f;
-
 
100
        result = (red << 16) | (green << 8) | blue;
-
 
101
    }
-
 
102
    return result;
62
}
103
}
63
 
104
 
-
 
105
static void clear_line(int y);
-
 
106
/** Scroll screen one row up */
-
 
107
static void scroll_screen(void)
-
 
108
{
-
 
109
    int i;
-
 
110
 
-
 
111
    for (i=0;i < (xres*yres*pixelbytes)-ROW_PIX; i++)
-
 
112
        fbaddress[i] = fbaddress[i+ROW_PIX];
-
 
113
 
-
 
114
    /* Clear last row */
-
 
115
    for (i=0; i < ROW_HEIGHT;i++)
-
 
116
        clear_line((rows-1)*ROW_HEIGHT+i);
-
 
117
   
-
 
118
}
-
 
119
 
-
 
120
 
-
 
121
/***************************************************************/
-
 
122
/* Screen specific function */
-
 
123
 
64
/** Fill line with color BGCOLOR */
124
/** Fill line with color BGCOLOR */
65
static void clear_line(int y)
125
static void clear_line(int y)
66
{
126
{
67
    int x;
127
    int x;
68
    for (x=0; x<xres;x++)
128
    for (x=0; x<xres;x++)
69
        putpixel(x,y,BGCOLOR);
129
        putpixel(x,y,BGCOLOR);
70
}
130
}
71
 
131
 
72
/** Fill screen with background color */
132
/** Fill screen with background color */
73
static void clear_screen(void)
133
static void clear_screen(void)
74
{
134
{
75
    int y;
135
    int y;
76
 
136
 
77
    for (y=0; y<yres;y++)
137
    for (y=0; y<yres;y++)
78
        clear_line(y);
138
        clear_line(y);
79
}
139
}
80
 
140
 
81
static void invert_pixel(int x, int y)
141
static void invert_pixel(int x, int y)
82
{
142
{
83
    fbaddress[POINTPOS(x,y,0)] = ~fbaddress[POINTPOS(x,y,0)];
-
 
84
    fbaddress[POINTPOS(x,y,1)] = ~fbaddress[POINTPOS(x,y,1)];
-
 
85
    fbaddress[POINTPOS(x,y,2)] = ~fbaddress[POINTPOS(x,y,2)];
143
    putpixel(x,y, ~getpixel(x,y));
86
}
144
}
87
 
145
 
88
/** Scroll screen one row up */
-
 
89
static void scroll_screen(void)
-
 
90
{
-
 
91
    int i;
-
 
92
 
-
 
93
    for (i=0;i < (xres*yres*3)-ROW_PIX; i++)
-
 
94
        fbaddress[i] = fbaddress[i+ROW_PIX];
-
 
95
 
-
 
96
    /* Clear last row */
-
 
97
    for (i=0; i < ROW_HEIGHT;i++)
-
 
98
        clear_line((rows-1)*ROW_HEIGHT+i);
-
 
99
   
-
 
100
}
-
 
101
 
146
 
102
/** Draw one line of glyph at a given position */
147
/** Draw one line of glyph at a given position */
103
static void draw_glyph_line(int glline, int x, int y)
148
static void draw_glyph_line(int glline, int x, int y)
104
{
149
{
105
    int i;
150
    int i;
106
 
151
 
107
    for (i=0; i < 8; i++)
152
    for (i=0; i < 8; i++)
108
        if (glline & (1 << (7-i))) {
153
        if (glline & (1 << (7-i))) {
109
            putpixel(x+i,y,FGCOLOR);
154
            putpixel(x+i,y,FGCOLOR);
110
        } else
155
        } else
111
            putpixel(x+i,y,BGCOLOR);
156
            putpixel(x+i,y,BGCOLOR);
112
}
157
}
113
 
158
 
-
 
159
/***************************************************************/
-
 
160
/* Character-console functions */
-
 
161
 
114
/** Draw character at given position */
162
/** Draw character at given position */
115
static void draw_glyph(unsigned char glyph, int col, int row)
163
static void draw_glyph(__u8 glyph, int col, int row)
116
{
164
{
117
    int y;
165
    int y;
118
 
166
 
119
    for (y=0; y < FONT_SCANLINES; y++)
167
    for (y=0; y < FONT_SCANLINES; y++)
120
        draw_glyph_line(fb_font[glyph*FONT_SCANLINES+y],
168
        draw_glyph_line(fb_font[glyph*FONT_SCANLINES+y],
121
                col*COL_WIDTH, row*ROW_HEIGHT+y);
169
                col*COL_WIDTH, row*ROW_HEIGHT+y);
122
}
170
}
123
 
171
 
124
/** Invert character at given position */
172
/** Invert character at given position */
125
static void invert_char(int col, int row)
173
static void invert_char(int col, int row)
126
{
174
{
127
    int x,y;
175
    int x,y;
128
 
176
 
129
    for (x=0; x < COL_WIDTH; x++)
177
    for (x=0; x < COL_WIDTH; x++)
130
        for (y=0; y < FONT_SCANLINES; y++)
178
        for (y=0; y < FONT_SCANLINES; y++)
131
            invert_pixel(col*COL_WIDTH+x,row*ROW_HEIGHT+y);
179
            invert_pixel(col*COL_WIDTH+x,row*ROW_HEIGHT+y);
132
}
180
}
133
 
181
 
134
/** Draw character at default position */
182
/** Draw character at default position */
135
static void draw_char(char chr)
183
static void draw_char(char chr)
136
{
184
{
137
    draw_glyph(chr, position % columns, position/columns);
185
    draw_glyph(chr, position % columns, position/columns);
138
}
186
}
139
 
187
 
-
 
188
/***************************************************************/
-
 
189
/* Stdout specific functions */
-
 
190
 
140
static void invert_cursor(void)
191
static void invert_cursor(void)
141
{
192
{
142
    invert_char(position % columns, position/columns);
193
    invert_char(position % columns, position/columns);
143
}
194
}
144
 
195
 
145
/** Print character to screen
196
/** Print character to screen
146
 *
197
 *
147
 *  Emulate basic terminal commands
198
 *  Emulate basic terminal commands
148
 */
199
 */
149
static void fb_putchar(chardev_t *dev, char ch)
200
static void fb_putchar(chardev_t *dev, char ch)
150
{
201
{
151
    spinlock_lock(&fb->lock);
202
    spinlock_lock(&fb->lock);
152
 
203
 
153
    if (ch == '\n') {
204
    if (ch == '\n') {
154
        invert_cursor();
205
        invert_cursor();
155
        position += columns;
206
        position += columns;
156
        position -= position % columns;
207
        position -= position % columns;
157
    } else if (ch == '\r') {
208
    } else if (ch == '\r') {
158
        invert_cursor();
209
        invert_cursor();
159
        position -= position % columns;
210
        position -= position % columns;
160
    } else if (ch == '\b') {
211
    } else if (ch == '\b') {
161
        invert_cursor();
212
        invert_cursor();
162
        if (position % columns)
213
        if (position % columns)
163
            position--;
214
            position--;
-
 
215
    } else if (ch == '\t') {
-
 
216
        invert_cursor();
-
 
217
        do {
-
 
218
            draw_char(' ');
-
 
219
            position++;
-
 
220
        } while (position % 8);
164
    } else {
221
    } else {
165
        draw_char(ch);
222
        draw_char(ch);
166
        position++;
223
        position++;
167
    }
224
    }
168
    if (position >= columns*rows) {
225
    if (position >= columns*rows) {
169
        position -= columns;
226
        position -= columns;
170
        scroll_screen();
227
        scroll_screen();
171
    }
228
    }
172
    invert_cursor();
229
    invert_cursor();
173
    spinlock_unlock(&fb->lock);
230
    spinlock_unlock(&fb->lock);
174
}
231
}
175
 
232
 
176
static chardev_t framebuffer;
233
static chardev_t framebuffer;
177
static chardev_operations_t fb_ops = {
234
static chardev_operations_t fb_ops = {
178
    .write = fb_putchar,
235
    .write = fb_putchar,
179
};
236
};
180
 
237
 
181
 
238
 
182
/** Initialize framebuffer as a chardev output device
239
/** Initialize framebuffer as a chardev output device
183
 *
240
 *
184
 * @param addr Address of framebuffer
241
 * @param addr Address of framebuffer
185
 * @param x X resolution
242
 * @param x X resolution
186
 * @param y Y resolution
243
 * @param y Y resolution
-
 
244
 * @param bytes Bytes per pixel (2,3,4)
187
 */
245
 */
188
void fb_init(__address addr, int x, int y)
246
void fb_init(__address addr, int x, int y, int bytes)
189
{
247
{
190
    fbaddress = (unsigned char *)addr;
248
    fbaddress = (unsigned char *)addr;
191
   
249
   
192
    xres = x;
250
    xres = x;
193
    yres = y;
251
    yres = y;
-
 
252
    pixelbytes = bytes;
194
   
253
   
195
    rows = y/ROW_HEIGHT;
254
    rows = y/ROW_HEIGHT;
196
    columns = x/COL_WIDTH;
255
    columns = x/COL_WIDTH;
197
 
256
 
198
    clear_screen();
257
    clear_screen();
199
    invert_cursor();
258
    invert_cursor();
200
 
259
 
201
    chardev_initialize("fb", &framebuffer, &fb_ops);
260
    chardev_initialize("fb", &framebuffer, &fb_ops);
202
    stdout = &framebuffer;
261
    stdout = &framebuffer;
203
}
262
}
204
 
263
 
205
 
264