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