Rev 3692 | Rev 3709 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
836 | palkovsky | 1 | /* |
3707 | decky | 2 | * Copyright (c) 2008 Martin Decky |
2071 | jermar | 3 | * Copyright (c) 2006 Ondrej Palkovsky |
836 | palkovsky | 4 | * All rights reserved. |
5 | * |
||
6 | * Redistribution and use in source and binary forms, with or without |
||
7 | * modification, are permitted provided that the following conditions |
||
8 | * are met: |
||
9 | * |
||
10 | * - Redistributions of source code must retain the above copyright |
||
11 | * notice, this list of conditions and the following disclaimer. |
||
12 | * - Redistributions in binary form must reproduce the above copyright |
||
13 | * notice, this list of conditions and the following disclaimer in the |
||
14 | * documentation and/or other materials provided with the distribution. |
||
15 | * - The name of the author may not be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | */ |
||
29 | |||
3707 | decky | 30 | /** @addtogroup genarch |
1702 | cejka | 31 | * @{ |
32 | */ |
||
33 | /** @file |
||
34 | */ |
||
35 | |||
837 | palkovsky | 36 | #include <genarch/fb/font-8x16.h> |
1993 | decky | 37 | #include <genarch/fb/visuals.h> |
837 | palkovsky | 38 | #include <genarch/fb/fb.h> |
836 | palkovsky | 39 | #include <console/chardev.h> |
40 | #include <console/console.h> |
||
1317 | vana | 41 | #include <sysinfo/sysinfo.h> |
1316 | jermar | 42 | #include <mm/slab.h> |
3707 | decky | 43 | #include <align.h> |
839 | palkovsky | 44 | #include <panic.h> |
896 | jermar | 45 | #include <memstr.h> |
1316 | jermar | 46 | #include <config.h> |
1682 | palkovsky | 47 | #include <bitops.h> |
48 | #include <print.h> |
||
2015 | jermar | 49 | #include <ddi/ddi.h> |
2054 | jermar | 50 | #include <arch/types.h> |
836 | palkovsky | 51 | |
3707 | decky | 52 | SPINLOCK_INITIALIZE(fb_lock); |
862 | palkovsky | 53 | |
3707 | decky | 54 | /**< Physical memory area for fb. */ |
55 | static parea_t fb_parea; |
||
2015 | jermar | 56 | |
3707 | decky | 57 | static uint8_t *fb_addr; |
58 | static uint8_t *backbuf; |
||
59 | static uint8_t *glyphs; |
||
836 | palkovsky | 60 | |
3707 | decky | 61 | static void *bgpixel; |
836 | palkovsky | 62 | |
3707 | decky | 63 | static unsigned int xres; |
64 | static unsigned int yres; |
||
1316 | jermar | 65 | |
3707 | decky | 66 | static unsigned int scanline; |
67 | static unsigned int glyphscanline; |
||
836 | palkovsky | 68 | |
3707 | decky | 69 | static unsigned int pixelbytes; |
70 | static unsigned int glyphbytes; |
||
71 | |||
72 | static unsigned int cols; |
||
73 | static unsigned int rows; |
||
1135 | decky | 74 | static unsigned int position = 0; |
836 | palkovsky | 75 | |
3707 | decky | 76 | #define BG_COLOR 0x000080 |
77 | #define FG_COLOR 0xffff00 |
||
836 | palkovsky | 78 | |
3707 | decky | 79 | #define CURSOR 219 |
1135 | decky | 80 | |
3707 | decky | 81 | #define RED(x, bits) ((x >> (8 + 8 + 8 - bits)) & ((1 << bits) - 1)) |
82 | #define GREEN(x, bits) ((x >> (8 + 8 - bits)) & ((1 << bits) - 1)) |
||
83 | #define BLUE(x, bits) ((x >> (8 - bits)) & ((1 << bits) - 1)) |
||
1135 | decky | 84 | |
3707 | decky | 85 | #define COL2X(col) ((col) * FONT_WIDTH) |
86 | #define ROW2Y(row) ((row) * FONT_SCANLINES) |
||
1135 | decky | 87 | |
3707 | decky | 88 | #define X2COL(x) ((x) / FONT_WIDTH) |
89 | #define Y2ROW(y) ((y) / FONT_SCANLINES) |
||
838 | palkovsky | 90 | |
3707 | decky | 91 | #define FB_POS(x, y) ((y) * scanline + (x) * pixelbytes) |
92 | #define BB_POS(col, row) ((row) * cols + (col)) |
||
93 | #define GLYPH_POS(glyph, y) ((glyph) * glyphbytes + (y) * glyphscanline) |
||
839 | palkovsky | 94 | |
1875 | jermar | 95 | |
3707 | decky | 96 | static void (*rgb_conv)(void *, uint32_t); |
836 | palkovsky | 97 | |
839 | palkovsky | 98 | |
3707 | decky | 99 | /** ARGB 8:8:8:8 conversion |
100 | * |
||
101 | */ |
||
102 | static void rgb_0888(void *dst, uint32_t rgb) |
||
1994 | decky | 103 | { |
3707 | decky | 104 | *((uint32_t *) dst) = rgb & 0xffffff; |
1994 | decky | 105 | } |
106 | |||
3707 | decky | 107 | |
108 | /** ABGR 8:8:8:8 conversion |
||
109 | * |
||
110 | */ |
||
111 | static void bgr_0888(void *dst, uint32_t rgb) |
||
1994 | decky | 112 | { |
3707 | decky | 113 | *((uint32_t *) dst) |
114 | = (BLUE(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | RED(rgb, 8); |
||
1994 | decky | 115 | } |
116 | |||
3707 | decky | 117 | |
118 | /** BGR 8:8:8 conversion |
||
119 | * |
||
120 | */ |
||
121 | static void rgb_888(void *dst, uint32_t rgb) |
||
839 | palkovsky | 122 | { |
1871 | jermar | 123 | #if defined(FB_INVERT_ENDIAN) |
3707 | decky | 124 | *((uint32_t *) dst) |
125 | = (BLUE(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | RED(rgb, 8) |
||
126 | | (*((uint32_t *) dst) & 0xff0000); |
||
1298 | vana | 127 | #else |
3707 | decky | 128 | *((uint32_t *) dst) |
129 | = (rgb & 0xffffff) | (*((uint32_t *) dst) & 0xff0000); |
||
1682 | palkovsky | 130 | #endif |
839 | palkovsky | 131 | } |
132 | |||
838 | palkovsky | 133 | |
3707 | decky | 134 | /** RGB 5:5:5 conversion |
135 | * |
||
136 | */ |
||
137 | static void rgb_555(void *dst, uint32_t rgb) |
||
1993 | decky | 138 | { |
3707 | decky | 139 | *((uint16_t *) dst) |
140 | = (RED(rgb, 5) << 10) | (GREEN(rgb, 5) << 5) | BLUE(rgb, 5); |
||
1993 | decky | 141 | } |
142 | |||
143 | |||
3707 | decky | 144 | /** RGB 5:6:5 conversion |
145 | * |
||
146 | */ |
||
147 | static void rgb_565(void *dst, uint32_t rgb) |
||
839 | palkovsky | 148 | { |
3707 | decky | 149 | *((uint16_t *) dst) |
150 | = (RED(rgb, 5) << 11) | (GREEN(rgb, 6) << 5) | BLUE(rgb, 5); |
||
839 | palkovsky | 151 | } |
152 | |||
153 | |||
3707 | decky | 154 | /** RGB 3:2:3 |
1837 | jermar | 155 | * |
156 | * Even though we try 3:2:3 color scheme here, an 8-bit framebuffer |
||
157 | * will most likely use a color palette. The color appearance |
||
158 | * will be pretty random and depend on the default installed |
||
159 | * palette. This could be fixed by supporting custom palette |
||
160 | * and setting it to simulate the 8-bit truecolor. |
||
3692 | rimsky | 161 | * |
3707 | decky | 162 | * Currently we set the palette on the ia32 and sparc64 port. |
3692 | rimsky | 163 | * |
164 | * Note that the byte is being inverted by this function. The reason is |
||
165 | * that we would like to use a color palette where the white color code |
||
166 | * is 0 and the black color code is 255, as some machines (SunBlade 1500) |
||
167 | * use these codes for black and white and prevent to set codes |
||
168 | * 0 and 255 to other colors. |
||
1837 | jermar | 169 | */ |
3707 | decky | 170 | static void rgb_323(void *dst, uint32_t rgb) |
839 | palkovsky | 171 | { |
3707 | decky | 172 | *((uint8_t *) dst) |
173 | = ~((RED(rgb, 3) << 5) | (GREEN(rgb, 2) << 3) | BLUE(rgb, 3)); |
||
1135 | decky | 174 | } |
839 | palkovsky | 175 | |
3707 | decky | 176 | |
177 | /** Draw character at given position |
||
1837 | jermar | 178 | * |
179 | */ |
||
3707 | decky | 180 | static void draw_glyph(uint8_t glyph, unsigned int col, unsigned int row) |
1135 | decky | 181 | { |
3707 | decky | 182 | unsigned int x = COL2X(col); |
183 | unsigned int y = ROW2Y(row); |
||
184 | unsigned int yd; |
||
185 | |||
186 | backbuf[BB_POS(col, row)] = glyph; |
||
187 | |||
188 | for (yd = 0; yd < FONT_SCANLINES; yd++) |
||
189 | memcpy(&fb_addr[FB_POS(x, y + yd)], |
||
190 | &glyphs[GLYPH_POS(glyph, yd)], glyphscanline); |
||
839 | palkovsky | 191 | } |
192 | |||
1682 | palkovsky | 193 | |
3707 | decky | 194 | /** Scroll screen down by one row |
195 | * |
||
196 | * |
||
197 | */ |
||
838 | palkovsky | 198 | static void scroll_screen(void) |
199 | { |
||
3707 | decky | 200 | unsigned int row; |
201 | |||
202 | for (row = 0; row < rows; row++) { |
||
203 | unsigned int y = ROW2Y(row); |
||
204 | unsigned int yd; |
||
2054 | jermar | 205 | |
3707 | decky | 206 | for (yd = 0; yd < FONT_SCANLINES; yd++) { |
207 | unsigned int x; |
||
208 | unsigned int col; |
||
209 | |||
210 | for (col = 0, x = 0; col < cols; col++, x += FONT_WIDTH) { |
||
211 | uint8_t glyph; |
||
212 | |||
213 | if (row < rows - 1) { |
||
214 | if (backbuf[BB_POS(col, row)] == backbuf[BB_POS(col, row + 1)]) |
||
215 | continue; |
||
216 | |||
217 | glyph = backbuf[BB_POS(col, row + 1)]; |
||
218 | } else |
||
219 | glyph = 0; |
||
220 | |||
221 | memcpy(&fb_addr[FB_POS(x, y + yd)], |
||
222 | &glyphs[GLYPH_POS(glyph, yd)], glyphscanline); |
||
223 | } |
||
2086 | jermar | 224 | } |
1682 | palkovsky | 225 | } |
3707 | decky | 226 | |
227 | memcpy(backbuf, backbuf + cols, cols * (rows - 1)); |
||
228 | memsetb(&backbuf[BB_POS(0, rows - 1)], cols, 0); |
||
838 | palkovsky | 229 | } |
230 | |||
231 | |||
3707 | decky | 232 | static void cursor_put(void) |
836 | palkovsky | 233 | { |
3707 | decky | 234 | draw_glyph(CURSOR, position % cols, position / cols); |
836 | palkovsky | 235 | } |
236 | |||
237 | |||
3707 | decky | 238 | static void cursor_remove(void) |
836 | palkovsky | 239 | { |
3707 | decky | 240 | draw_glyph(0, position % cols, position / cols); |
836 | palkovsky | 241 | } |
242 | |||
838 | palkovsky | 243 | |
836 | palkovsky | 244 | /** Print character to screen |
245 | * |
||
3707 | decky | 246 | * Emulate basic terminal commands. |
247 | * |
||
836 | palkovsky | 248 | */ |
249 | static void fb_putchar(chardev_t *dev, char ch) |
||
250 | { |
||
1287 | vana | 251 | spinlock_lock(&fb_lock); |
1135 | decky | 252 | |
253 | switch (ch) { |
||
1888 | jermar | 254 | case '\n': |
3707 | decky | 255 | cursor_remove(); |
256 | position += cols; |
||
257 | position -= position % cols; |
||
1888 | jermar | 258 | break; |
259 | case '\r': |
||
3707 | decky | 260 | cursor_remove(); |
261 | position -= position % cols; |
||
1888 | jermar | 262 | break; |
263 | case '\b': |
||
3707 | decky | 264 | cursor_remove(); |
265 | if (position % cols) |
||
1888 | jermar | 266 | position--; |
267 | break; |
||
268 | case '\t': |
||
3707 | decky | 269 | cursor_remove(); |
1888 | jermar | 270 | do { |
3707 | decky | 271 | draw_glyph((uint8_t) ' ', position % cols, position / cols); |
838 | palkovsky | 272 | position++; |
3707 | decky | 273 | } while ((position % 8) && (position < cols * rows)); |
1888 | jermar | 274 | break; |
275 | default: |
||
3707 | decky | 276 | draw_glyph((uint8_t) ch, position % cols, position / cols); |
1888 | jermar | 277 | position++; |
836 | palkovsky | 278 | } |
1135 | decky | 279 | |
3707 | decky | 280 | if (position >= cols * rows) { |
281 | position -= cols; |
||
836 | palkovsky | 282 | scroll_screen(); |
283 | } |
||
1135 | decky | 284 | |
3707 | decky | 285 | cursor_put(); |
1135 | decky | 286 | |
1287 | vana | 287 | spinlock_unlock(&fb_lock); |
836 | palkovsky | 288 | } |
289 | |||
290 | static chardev_t framebuffer; |
||
291 | static chardev_operations_t fb_ops = { |
||
292 | .write = fb_putchar, |
||
293 | }; |
||
294 | |||
295 | |||
3707 | decky | 296 | /** Render glyphs |
297 | * |
||
298 | * Convert glyphs from device independent font |
||
299 | * description to current visual representation. |
||
300 | * |
||
301 | */ |
||
302 | static void render_glyphs(void) |
||
303 | { |
||
304 | unsigned int glyph; |
||
305 | |||
306 | for (glyph = 0; glyph < FONT_GLYPHS; glyph++) { |
||
307 | unsigned int y; |
||
308 | |||
309 | for (y = 0; y < FONT_SCANLINES; y++) { |
||
310 | unsigned int x; |
||
311 | |||
312 | for (x = 0; x < FONT_WIDTH; x++) |
||
313 | rgb_conv(&glyphs[GLYPH_POS(glyph, y) + x * pixelbytes], |
||
314 | (fb_font[glyph * FONT_SCANLINES + y] & (1 << (7 - x))) ? FG_COLOR : BG_COLOR); |
||
315 | } |
||
316 | } |
||
317 | |||
318 | rgb_conv(bgpixel, BG_COLOR); |
||
319 | } |
||
320 | |||
321 | |||
322 | /** Refresh the screen |
||
323 | * |
||
324 | */ |
||
325 | void fb_redraw(void) |
||
326 | { |
||
327 | unsigned int row; |
||
328 | |||
329 | for (row = 0; row < rows; row++) { |
||
330 | unsigned int y = ROW2Y(row); |
||
331 | unsigned int yd; |
||
332 | |||
333 | for (yd = 0; yd < FONT_SCANLINES; yd++) { |
||
334 | unsigned int x; |
||
335 | unsigned int col; |
||
336 | |||
337 | for (col = 0, x = 0; col < cols; col++, x += FONT_WIDTH) |
||
338 | memcpy(&fb_addr[FB_POS(x, y + yd)], |
||
339 | &glyphs[GLYPH_POS(backbuf[BB_POS(col, row)], yd)], |
||
340 | glyphscanline); |
||
341 | } |
||
342 | } |
||
343 | |||
344 | if (COL2X(cols) < xres) { |
||
345 | unsigned int y; |
||
346 | |||
347 | for (y = 0; y < yres; y++) { |
||
348 | unsigned int x; |
||
349 | |||
350 | for (x = COL2X(cols); x < xres; x++) |
||
351 | memcpy(&fb_addr[FB_POS(x, y)], bgpixel, pixelbytes); |
||
352 | } |
||
353 | } |
||
354 | |||
355 | if (ROW2Y(rows) < yres) { |
||
356 | unsigned int y; |
||
357 | |||
358 | for (y = ROW2Y(rows); y < yres; y++) { |
||
359 | unsigned int x; |
||
360 | |||
361 | for (x = 0; x < xres; x++) |
||
362 | memcpy(&fb_addr[FB_POS(x, y)], bgpixel, pixelbytes); |
||
363 | } |
||
364 | } |
||
365 | } |
||
366 | |||
367 | |||
836 | palkovsky | 368 | /** Initialize framebuffer as a chardev output device |
369 | * |
||
3707 | decky | 370 | * @param addr Physical address of the framebuffer |
371 | * @param x Screen width in pixels |
||
372 | * @param y Screen height in pixels |
||
373 | * @param scan Bytes per one scanline |
||
374 | * @param visual Color model |
||
375 | * |
||
836 | palkovsky | 376 | */ |
3672 | jermar | 377 | void fb_init(fb_properties_t *props) |
836 | palkovsky | 378 | { |
3672 | jermar | 379 | switch (props->visual) { |
1993 | decky | 380 | case VISUAL_INDIRECT_8: |
3707 | decky | 381 | rgb_conv = rgb_323; |
1991 | jermar | 382 | pixelbytes = 1; |
383 | break; |
||
1993 | decky | 384 | case VISUAL_RGB_5_5_5: |
3707 | decky | 385 | rgb_conv = rgb_555; |
1991 | jermar | 386 | pixelbytes = 2; |
387 | break; |
||
1993 | decky | 388 | case VISUAL_RGB_5_6_5: |
3707 | decky | 389 | rgb_conv = rgb_565; |
1993 | decky | 390 | pixelbytes = 2; |
1991 | jermar | 391 | break; |
1993 | decky | 392 | case VISUAL_RGB_8_8_8: |
3707 | decky | 393 | rgb_conv = rgb_888; |
1993 | decky | 394 | pixelbytes = 3; |
395 | break; |
||
396 | case VISUAL_RGB_8_8_8_0: |
||
3707 | decky | 397 | rgb_conv = rgb_888; |
1991 | jermar | 398 | pixelbytes = 4; |
399 | break; |
||
1993 | decky | 400 | case VISUAL_RGB_0_8_8_8: |
3707 | decky | 401 | rgb_conv = rgb_0888; |
1993 | decky | 402 | pixelbytes = 4; |
403 | break; |
||
1994 | decky | 404 | case VISUAL_BGR_0_8_8_8: |
3707 | decky | 405 | rgb_conv = bgr_0888; |
1994 | decky | 406 | pixelbytes = 4; |
407 | break; |
||
1991 | jermar | 408 | default: |
1993 | decky | 409 | panic("Unsupported visual.\n"); |
839 | palkovsky | 410 | } |
1371 | decky | 411 | |
3672 | jermar | 412 | xres = props->x; |
413 | yres = props->y; |
||
414 | scanline = props->scan; |
||
836 | palkovsky | 415 | |
3707 | decky | 416 | cols = xres / FONT_WIDTH; |
417 | rows = yres / FONT_SCANLINES; |
||
418 | |||
419 | glyphscanline = FONT_WIDTH * pixelbytes; |
||
420 | glyphbytes = glyphscanline * FONT_SCANLINES; |
||
421 | |||
422 | unsigned int fbsize = scanline * yres; |
||
423 | unsigned int bbsize = cols * rows; |
||
424 | unsigned int glyphsize = FONT_GLYPHS * glyphbytes; |
||
425 | |||
426 | backbuf = (uint8_t *) malloc(bbsize, 0); |
||
427 | if (!backbuf) |
||
428 | panic("Unable to allocate backbuffer.\n"); |
||
429 | |||
430 | glyphs = (uint8_t *) malloc(glyphsize, 0); |
||
431 | if (!glyphs) |
||
432 | panic("Unable to allocate glyphs.\n"); |
||
433 | |||
434 | bgpixel = malloc(pixelbytes, 0); |
||
435 | if (!bgpixel) |
||
436 | panic("Unable to allocate background pixel.\n"); |
||
437 | |||
438 | memsetb(backbuf, bbsize, 0); |
||
439 | memsetb(glyphs, glyphsize, 0); |
||
440 | memsetb(bgpixel, pixelbytes, 0); |
||
441 | |||
442 | render_glyphs(); |
||
443 | |||
444 | fb_addr = (uint8_t *) hw_map((uintptr_t) props->addr, fbsize); |
||
445 | |||
3692 | rimsky | 446 | fb_parea.pbase = (uintptr_t) props->addr + props->offset; |
3707 | decky | 447 | fb_parea.vbase = (uintptr_t) fb_addr; |
2015 | jermar | 448 | fb_parea.frames = SIZE2FRAMES(fbsize); |
449 | fb_parea.cacheable = false; |
||
450 | ddi_parea_register(&fb_parea); |
||
3707 | decky | 451 | |
1690 | palkovsky | 452 | sysinfo_set_item_val("fb", NULL, true); |
453 | sysinfo_set_item_val("fb.kind", NULL, 1); |
||
454 | sysinfo_set_item_val("fb.width", NULL, xres); |
||
455 | sysinfo_set_item_val("fb.height", NULL, yres); |
||
3707 | decky | 456 | sysinfo_set_item_val("fb.scanline", NULL, scanline); |
3672 | jermar | 457 | sysinfo_set_item_val("fb.visual", NULL, props->visual); |
458 | sysinfo_set_item_val("fb.address.physical", NULL, props->addr); |
||
1990 | decky | 459 | |
3707 | decky | 460 | fb_redraw(); |
461 | |||
836 | palkovsky | 462 | chardev_initialize("fb", &framebuffer, &fb_ops); |
463 | stdout = &framebuffer; |
||
464 | } |
||
1702 | cejka | 465 | |
1790 | jermar | 466 | /** @} |
1702 | cejka | 467 | */ |