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