Subversion Repositories HelenOS-historic

Rev

Rev 1473 | Rev 1682 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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