Rev 1875 | Rev 1981 | 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 | |||
1790 | jermar | 29 | /** @addtogroup genarch |
1702 | cejka | 30 | * @{ |
31 | */ |
||
32 | /** @file |
||
33 | */ |
||
34 | |||
837 | palkovsky | 35 | #include <genarch/fb/font-8x16.h> |
36 | #include <genarch/fb/fb.h> |
||
836 | palkovsky | 37 | #include <console/chardev.h> |
38 | #include <console/console.h> |
||
1317 | vana | 39 | #include <sysinfo/sysinfo.h> |
1316 | jermar | 40 | #include <mm/slab.h> |
1371 | decky | 41 | #include <align.h> |
839 | palkovsky | 42 | #include <panic.h> |
896 | jermar | 43 | #include <memstr.h> |
1316 | jermar | 44 | #include <config.h> |
1682 | palkovsky | 45 | #include <bitops.h> |
46 | #include <print.h> |
||
836 | palkovsky | 47 | |
862 | palkovsky | 48 | #include "helenos.xbm" |
49 | |||
836 | palkovsky | 50 | SPINLOCK_INITIALIZE(fb_lock); |
51 | |||
1780 | jermar | 52 | static uint8_t *fbaddress = NULL; |
836 | palkovsky | 53 | |
1780 | jermar | 54 | static uint8_t *blankline = NULL; |
55 | static uint8_t *dbbuffer = NULL; /* Buffer for fast scrolling console */ |
||
1682 | palkovsky | 56 | static int dboffset; |
1316 | jermar | 57 | |
1135 | decky | 58 | static unsigned int xres = 0; |
59 | static unsigned int yres = 0; |
||
60 | static unsigned int scanline = 0; |
||
1327 | decky | 61 | static unsigned int bitspp = 0; |
1135 | decky | 62 | static unsigned int pixelbytes = 0; |
1875 | jermar | 63 | #ifdef FB_INVERT_COLORS |
64 | static bool invert_colors = true; |
||
65 | #else |
||
66 | static bool invert_colors = false; |
||
67 | #endif |
||
836 | palkovsky | 68 | |
1135 | decky | 69 | static unsigned int position = 0; |
70 | static unsigned int columns = 0; |
||
71 | static unsigned int rows = 0; |
||
836 | palkovsky | 72 | |
1135 | decky | 73 | #define COL_WIDTH 8 |
74 | #define ROW_BYTES (scanline * FONT_SCANLINES) |
||
836 | palkovsky | 75 | |
1135 | decky | 76 | #define BGCOLOR 0x000080 |
77 | #define FGCOLOR 0xffff00 |
||
78 | #define LOGOCOLOR 0x2020b0 |
||
79 | |||
80 | #define RED(x, bits) ((x >> (16 + 8 - bits)) & ((1 << bits) - 1)) |
||
81 | #define GREEN(x, bits) ((x >> (8 + 8 - bits)) & ((1 << bits) - 1)) |
||
82 | #define BLUE(x, bits) ((x >> (8 - bits)) & ((1 << bits) - 1)) |
||
83 | |||
1682 | palkovsky | 84 | #define POINTPOS(x, y) ((y) * scanline + (x) * pixelbytes) |
1135 | decky | 85 | |
838 | palkovsky | 86 | /***************************************************************/ |
87 | /* Pixel specific fuctions */ |
||
88 | |||
1682 | palkovsky | 89 | static void (*rgb2scr)(void *, int); |
90 | static int (*scr2rgb)(void *); |
||
839 | palkovsky | 91 | |
1875 | jermar | 92 | static inline int COLOR(int color) |
93 | { |
||
94 | return invert_colors ? ~color : color; |
||
95 | } |
||
96 | |||
1682 | palkovsky | 97 | /* Conversion routines between different color representations */ |
98 | static void rgb_4byte(void *dst, int rgb) |
||
836 | palkovsky | 99 | { |
1682 | palkovsky | 100 | *(int *)dst = rgb; |
836 | palkovsky | 101 | } |
102 | |||
1682 | palkovsky | 103 | static int byte4_rgb(void *src) |
838 | palkovsky | 104 | { |
1682 | palkovsky | 105 | return (*(int *)src) & 0xffffff; |
839 | palkovsky | 106 | } |
107 | |||
1682 | palkovsky | 108 | static void rgb_3byte(void *dst, int rgb) |
839 | palkovsky | 109 | { |
1780 | jermar | 110 | uint8_t *scr = dst; |
1871 | jermar | 111 | #if defined(FB_INVERT_ENDIAN) |
1682 | palkovsky | 112 | scr[0] = RED(rgb, 8); |
113 | scr[1] = GREEN(rgb, 8); |
||
114 | scr[2] = BLUE(rgb, 8); |
||
1298 | vana | 115 | #else |
1682 | palkovsky | 116 | scr[2] = RED(rgb, 8); |
117 | scr[1] = GREEN(rgb, 8); |
||
118 | scr[0] = BLUE(rgb, 8); |
||
119 | #endif |
||
839 | palkovsky | 120 | } |
121 | |||
1682 | palkovsky | 122 | static int byte3_rgb(void *src) |
839 | palkovsky | 123 | { |
1780 | jermar | 124 | uint8_t *scr = src; |
1871 | jermar | 125 | #if defined(FB_INVERT_ENDIAN) |
1682 | palkovsky | 126 | return scr[0] << 16 | scr[1] << 8 | scr[2]; |
1298 | vana | 127 | #else |
1682 | palkovsky | 128 | return scr[2] << 16 | scr[1] << 8 | scr[0]; |
1298 | vana | 129 | #endif |
838 | palkovsky | 130 | } |
131 | |||
1682 | palkovsky | 132 | /** 16-bit depth (5:6:5) */ |
133 | static void rgb_2byte(void *dst, int rgb) |
||
839 | palkovsky | 134 | { |
1682 | palkovsky | 135 | /* 5-bit, 6-bits, 5-bits */ |
1780 | jermar | 136 | *((uint16_t *)(dst)) = RED(rgb, 5) << 11 | GREEN(rgb, 6) << 5 | BLUE(rgb, 5); |
839 | palkovsky | 137 | } |
138 | |||
1682 | palkovsky | 139 | /** 16-bit depth (5:6:5) */ |
140 | static int byte2_rgb(void *src) |
||
839 | palkovsky | 141 | { |
1780 | jermar | 142 | int color = *(uint16_t *)(src); |
1135 | decky | 143 | return (((color >> 11) & 0x1f) << (16 + 3)) | (((color >> 5) & 0x3f) << (8 + 2)) | ((color & 0x1f) << 3); |
839 | palkovsky | 144 | } |
145 | |||
1837 | jermar | 146 | /** Put pixel - 8-bit depth (color palette/3:2:3) |
147 | * |
||
148 | * Even though we try 3:2:3 color scheme here, an 8-bit framebuffer |
||
149 | * will most likely use a color palette. The color appearance |
||
150 | * will be pretty random and depend on the default installed |
||
151 | * palette. This could be fixed by supporting custom palette |
||
152 | * and setting it to simulate the 8-bit truecolor. |
||
153 | */ |
||
1682 | palkovsky | 154 | static void rgb_1byte(void *dst, int rgb) |
839 | palkovsky | 155 | { |
1780 | jermar | 156 | *(uint8_t *)dst = RED(rgb, 3) << 5 | GREEN(rgb, 2) << 3 | BLUE(rgb, 3); |
1135 | decky | 157 | } |
839 | palkovsky | 158 | |
1837 | jermar | 159 | /** Return pixel color - 8-bit depth (color palette/3:2:3) |
160 | * |
||
161 | * See the comment for rgb_1byte(). |
||
162 | */ |
||
1682 | palkovsky | 163 | static int byte1_rgb(void *src) |
1135 | decky | 164 | { |
1780 | jermar | 165 | int color = *(uint8_t *)src; |
1135 | decky | 166 | return (((color >> 5) & 0x7) << (16 + 5)) | (((color >> 3) & 0x3) << (8 + 6)) | ((color & 0x7) << 5); |
839 | palkovsky | 167 | } |
168 | |||
1682 | palkovsky | 169 | static void putpixel(unsigned int x, unsigned int y, int color) |
1135 | decky | 170 | { |
1875 | jermar | 171 | (*rgb2scr)(&fbaddress[POINTPOS(x,y)], COLOR(color)); |
1682 | palkovsky | 172 | |
173 | if (dbbuffer) { |
||
174 | int dline = (y + dboffset) % yres; |
||
1875 | jermar | 175 | (*rgb2scr)(&dbbuffer[POINTPOS(x,dline)], COLOR(color)); |
1682 | palkovsky | 176 | } |
1135 | decky | 177 | } |
839 | palkovsky | 178 | |
1682 | palkovsky | 179 | /** Get pixel from viewport */ |
180 | static int getpixel(unsigned int x, unsigned int y) |
||
181 | { |
||
182 | if (dbbuffer) { |
||
183 | int dline = (y + dboffset) % yres; |
||
1875 | jermar | 184 | return COLOR((*scr2rgb)(&dbbuffer[POINTPOS(x,dline)])); |
1682 | palkovsky | 185 | } |
1875 | jermar | 186 | return COLOR((*scr2rgb)(&fbaddress[POINTPOS(x,y)])); |
1682 | palkovsky | 187 | } |
1135 | decky | 188 | |
1682 | palkovsky | 189 | |
1135 | decky | 190 | /** Fill screen with background color */ |
191 | static void clear_screen(void) |
||
839 | palkovsky | 192 | { |
1135 | decky | 193 | unsigned int y; |
839 | palkovsky | 194 | |
1682 | palkovsky | 195 | for (y = 0; y < yres; y++) { |
196 | memcpy(&fbaddress[scanline*y], blankline, xres*pixelbytes); |
||
197 | if (dbbuffer) |
||
198 | memcpy(&dbbuffer[scanline*y], blankline, xres*pixelbytes); |
||
199 | } |
||
839 | palkovsky | 200 | } |
201 | |||
1135 | decky | 202 | |
838 | palkovsky | 203 | /** Scroll screen one row up */ |
204 | static void scroll_screen(void) |
||
205 | { |
||
1780 | jermar | 206 | uint8_t *lastline = &fbaddress[(rows - 1) * ROW_BYTES]; |
1682 | palkovsky | 207 | int firstsz; |
838 | palkovsky | 208 | |
1682 | palkovsky | 209 | if (dbbuffer) { |
210 | memcpy(&dbbuffer[dboffset*scanline], blankline, FONT_SCANLINES*scanline); |
||
211 | |||
212 | dboffset = (dboffset + FONT_SCANLINES) % yres; |
||
213 | firstsz = yres-dboffset; |
||
838 | palkovsky | 214 | |
1682 | palkovsky | 215 | memcpy(fbaddress, &dbbuffer[scanline*dboffset], firstsz*scanline); |
216 | memcpy(&fbaddress[firstsz*scanline], dbbuffer, dboffset*scanline); |
||
217 | } else { |
||
218 | memcpy((void *) fbaddress, (void *) &fbaddress[ROW_BYTES], scanline * yres - ROW_BYTES); |
||
219 | /* Clear last row */ |
||
220 | memcpy((void *) lastline, (void *) blankline, ROW_BYTES); |
||
221 | } |
||
838 | palkovsky | 222 | } |
223 | |||
224 | |||
1135 | decky | 225 | static void invert_pixel(unsigned int x, unsigned int y) |
836 | palkovsky | 226 | { |
1682 | palkovsky | 227 | putpixel(x, y, ~getpixel(x, y)); |
836 | palkovsky | 228 | } |
229 | |||
230 | |||
231 | /** Draw one line of glyph at a given position */ |
||
1135 | decky | 232 | static void draw_glyph_line(unsigned int glline, unsigned int x, unsigned int y) |
836 | palkovsky | 233 | { |
1135 | decky | 234 | unsigned int i; |
836 | palkovsky | 235 | |
1135 | decky | 236 | for (i = 0; i < 8; i++) |
237 | if (glline & (1 << (7 - i))) { |
||
1682 | palkovsky | 238 | putpixel(x + i, y, FGCOLOR); |
836 | palkovsky | 239 | } else |
1682 | palkovsky | 240 | putpixel(x + i, y, BGCOLOR); |
836 | palkovsky | 241 | } |
242 | |||
838 | palkovsky | 243 | /***************************************************************/ |
244 | /* Character-console functions */ |
||
245 | |||
836 | palkovsky | 246 | /** Draw character at given position */ |
1780 | jermar | 247 | static void draw_glyph(uint8_t glyph, unsigned int col, unsigned int row) |
836 | palkovsky | 248 | { |
1135 | decky | 249 | unsigned int y; |
836 | palkovsky | 250 | |
1135 | decky | 251 | for (y = 0; y < FONT_SCANLINES; y++) |
252 | draw_glyph_line(fb_font[glyph * FONT_SCANLINES + y], col * COL_WIDTH, row * FONT_SCANLINES + y); |
||
836 | palkovsky | 253 | } |
254 | |||
255 | /** Invert character at given position */ |
||
1135 | decky | 256 | static void invert_char(unsigned int col, unsigned int row) |
836 | palkovsky | 257 | { |
1135 | decky | 258 | unsigned int x; |
259 | unsigned int y; |
||
836 | palkovsky | 260 | |
1135 | decky | 261 | for (x = 0; x < COL_WIDTH; x++) |
262 | for (y = 0; y < FONT_SCANLINES; y++) |
||
263 | invert_pixel(col * COL_WIDTH + x, row * FONT_SCANLINES + y); |
||
836 | palkovsky | 264 | } |
265 | |||
266 | /** Draw character at default position */ |
||
267 | static void draw_char(char chr) |
||
268 | { |
||
1135 | decky | 269 | draw_glyph(chr, position % columns, position / columns); |
836 | palkovsky | 270 | } |
271 | |||
1135 | decky | 272 | static void draw_logo(unsigned int startx, unsigned int starty) |
862 | palkovsky | 273 | { |
1135 | decky | 274 | unsigned int x; |
275 | unsigned int y; |
||
276 | unsigned int byte; |
||
277 | unsigned int rowbytes; |
||
862 | palkovsky | 278 | |
1135 | decky | 279 | rowbytes = (helenos_width - 1) / 8 + 1; |
862 | palkovsky | 280 | |
1135 | decky | 281 | for (y = 0; y < helenos_height; y++) |
282 | for (x = 0; x < helenos_width; x++) { |
||
283 | byte = helenos_bits[rowbytes * y + x / 8]; |
||
862 | palkovsky | 284 | byte >>= x % 8; |
285 | if (byte & 1) |
||
1875 | jermar | 286 | putpixel(startx + x, starty + y, COLOR(LOGOCOLOR)); |
862 | palkovsky | 287 | } |
288 | } |
||
289 | |||
838 | palkovsky | 290 | /***************************************************************/ |
291 | /* Stdout specific functions */ |
||
292 | |||
836 | palkovsky | 293 | static void invert_cursor(void) |
294 | { |
||
1135 | decky | 295 | invert_char(position % columns, position / columns); |
836 | palkovsky | 296 | } |
297 | |||
298 | /** Print character to screen |
||
299 | * |
||
300 | * Emulate basic terminal commands |
||
301 | */ |
||
302 | static void fb_putchar(chardev_t *dev, char ch) |
||
303 | { |
||
1287 | vana | 304 | spinlock_lock(&fb_lock); |
1135 | decky | 305 | |
306 | switch (ch) { |
||
1888 | jermar | 307 | case '\n': |
308 | invert_cursor(); |
||
309 | position += columns; |
||
310 | position -= position % columns; |
||
311 | break; |
||
312 | case '\r': |
||
313 | invert_cursor(); |
||
314 | position -= position % columns; |
||
315 | break; |
||
316 | case '\b': |
||
317 | invert_cursor(); |
||
318 | if (position % columns) |
||
319 | position--; |
||
320 | break; |
||
321 | case '\t': |
||
322 | invert_cursor(); |
||
323 | do { |
||
324 | draw_char(' '); |
||
838 | palkovsky | 325 | position++; |
1888 | jermar | 326 | } while ((position % 8) && position < columns * rows); |
327 | break; |
||
328 | default: |
||
329 | draw_char(ch); |
||
330 | position++; |
||
836 | palkovsky | 331 | } |
1135 | decky | 332 | |
333 | if (position >= columns * rows) { |
||
836 | palkovsky | 334 | position -= columns; |
335 | scroll_screen(); |
||
336 | } |
||
1135 | decky | 337 | |
836 | palkovsky | 338 | invert_cursor(); |
1135 | decky | 339 | |
1287 | vana | 340 | spinlock_unlock(&fb_lock); |
836 | palkovsky | 341 | } |
342 | |||
343 | static chardev_t framebuffer; |
||
344 | static chardev_operations_t fb_ops = { |
||
345 | .write = fb_putchar, |
||
346 | }; |
||
347 | |||
348 | |||
349 | /** Initialize framebuffer as a chardev output device |
||
350 | * |
||
1837 | jermar | 351 | * @param addr Physical address of the framebuffer |
352 | * @param x Screen width in pixels |
||
353 | * @param y Screen height in pixels |
||
354 | * @param bpp Bits per pixel (8, 16, 24, 32) |
||
355 | * @param scan Bytes per one scanline |
||
356 | * @param align Request alignment for 24bpp mode. |
||
836 | palkovsky | 357 | */ |
1837 | jermar | 358 | void fb_init(uintptr_t addr, unsigned int x, unsigned int y, unsigned int bpp, unsigned int scan, bool align) |
836 | palkovsky | 359 | { |
1135 | decky | 360 | switch (bpp) { |
1888 | jermar | 361 | case 8: |
362 | rgb2scr = rgb_1byte; |
||
363 | scr2rgb = byte1_rgb; |
||
364 | pixelbytes = 1; |
||
365 | break; |
||
366 | case 16: |
||
367 | rgb2scr = rgb_2byte; |
||
368 | scr2rgb = byte2_rgb; |
||
369 | pixelbytes = 2; |
||
370 | break; |
||
371 | case 24: |
||
372 | rgb2scr = rgb_3byte; |
||
373 | scr2rgb = byte3_rgb; |
||
374 | if (align) |
||
1135 | decky | 375 | pixelbytes = 4; |
1888 | jermar | 376 | else |
377 | pixelbytes = 3; |
||
378 | break; |
||
379 | case 32: |
||
380 | rgb2scr = rgb_4byte; |
||
381 | scr2rgb = byte4_rgb; |
||
382 | pixelbytes = 4; |
||
383 | break; |
||
384 | default: |
||
385 | panic("Unsupported bpp"); |
||
839 | palkovsky | 386 | } |
1371 | decky | 387 | |
388 | unsigned int fbsize = scan * y; |
||
389 | |||
390 | /* Map the framebuffer */ |
||
1780 | jermar | 391 | fbaddress = (uint8_t *) hw_map((uintptr_t) addr, fbsize); |
1371 | decky | 392 | |
836 | palkovsky | 393 | xres = x; |
394 | yres = y; |
||
1327 | decky | 395 | bitspp = bpp; |
1135 | decky | 396 | scanline = scan; |
836 | palkovsky | 397 | |
1135 | decky | 398 | rows = y / FONT_SCANLINES; |
399 | columns = x / COL_WIDTH; |
||
836 | palkovsky | 400 | |
1690 | palkovsky | 401 | sysinfo_set_item_val("fb", NULL, true); |
402 | sysinfo_set_item_val("fb.kind", NULL, 1); |
||
403 | sysinfo_set_item_val("fb.width", NULL, xres); |
||
404 | sysinfo_set_item_val("fb.height", NULL, yres); |
||
405 | sysinfo_set_item_val("fb.bpp", NULL, bpp); |
||
1869 | jermar | 406 | sysinfo_set_item_val("fb.bpp-align", NULL, align); |
1690 | palkovsky | 407 | sysinfo_set_item_val("fb.scanline", NULL, scan); |
408 | sysinfo_set_item_val("fb.address.physical", NULL, addr); |
||
1875 | jermar | 409 | sysinfo_set_item_val("fb.invert-colors", NULL, invert_colors); |
1690 | palkovsky | 410 | |
1682 | palkovsky | 411 | /* Allocate double buffer */ |
412 | int totsize = scanline * yres; |
||
413 | int pages = SIZE2FRAMES(totsize); |
||
414 | int order; |
||
415 | if (pages == 1) |
||
416 | order = 0; |
||
417 | else |
||
418 | order = fnzb(pages-1)+1; |
||
419 | |||
1766 | palkovsky | 420 | dbbuffer = frame_alloc(order,FRAME_ATOMIC | FRAME_KA); |
1760 | palkovsky | 421 | if (!dbbuffer) |
1682 | palkovsky | 422 | printf("Failed to allocate scroll buffer.\n"); |
423 | dboffset = 0; |
||
424 | |||
425 | /* Initialized blank line */ |
||
1780 | jermar | 426 | blankline = (uint8_t *) malloc(ROW_BYTES, FRAME_ATOMIC); |
1766 | palkovsky | 427 | if (!blankline) |
428 | panic("Failed to allocate blank line for framebuffer."); |
||
1875 | jermar | 429 | for (y=0; y < FONT_SCANLINES; y++) { |
430 | for (x=0; x < xres; x++) { |
||
431 | (*rgb2scr)(&blankline[POINTPOS(x,y)], COLOR(BGCOLOR)); |
||
432 | } |
||
433 | } |
||
1682 | palkovsky | 434 | |
435 | clear_screen(); |
||
436 | |||
437 | /* Update size of screen to match text area */ |
||
438 | yres = rows * FONT_SCANLINES; |
||
439 | |||
1135 | decky | 440 | draw_logo(xres - helenos_width, 0); |
836 | palkovsky | 441 | invert_cursor(); |
442 | |||
443 | chardev_initialize("fb", &framebuffer, &fb_ops); |
||
444 | stdout = &framebuffer; |
||
1371 | decky | 445 | |
836 | palkovsky | 446 | } |
1702 | cejka | 447 | |
1790 | jermar | 448 | /** @} |
1702 | cejka | 449 | */ |